File size: 6,474 Bytes
90a6dae
 
 
 
 
35113a1
 
 
 
 
 
 
90a6dae
 
 
 
 
 
 
 
 
 
35113a1
 
 
 
 
90a6dae
 
 
 
 
 
 
 
 
 
 
35113a1
90a6dae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35113a1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
90a6dae
 
 
 
 
 
 
35113a1
90a6dae
 
 
 
 
 
 
 
 
 
 
35113a1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
90a6dae
 
 
 
 
 
 
 
 
35113a1
 
 
90a6dae
35113a1
90a6dae
 
 
35113a1
90a6dae
 
 
35113a1
 
 
 
 
 
 
90a6dae
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// Occupancy grid for the 3D inventory volume.
/// Axes: X = line (width slots, horizontal scan axis), Y = row (height slots, vertical fill axis),
/// Z = depth lane (into the bag, faces away from camera).
///
/// Placement priority (fresh items):
///   Scan X columns left to right. For each column, try to fill Y (State0), then State1, then
///   State2, before moving to the next X column. Depth (Z > 0) is NEVER used for fresh items -
///   it is reserved exclusively for duplicate-stacking behind an existing stack of the same itemID.
/// </summary>
public class InventoryGrid3D
{
    public readonly int lineCount;   // X - width slots per line
    public readonly int rowCount;    // Y - height slots per column
    public readonly int depthCount;  // Z - depth lanes

    // cellOwner[x, y, z] = the ItemData occupying that cell, or null if free.
    private readonly ItemData[,,] cellOwner;

    // Depth-lane reservation: (x, z) -> itemID that owns this depth lane for stacking.
    // A depth lane becomes reserved the first time any cell at that (x, z) with z > 0 is occupied
    // by a given itemID, and is released once no cells at that (x, z) belong to that itemID anymore.
    private readonly Dictionary<(int x, int z), string> depthLaneOwner = new Dictionary<(int, int), string>();

    public InventoryGrid3D(int lineCount, int rowCount, int depthCount)
    {
        this.lineCount = lineCount;
        this.rowCount = rowCount;
        this.depthCount = depthCount;
        cellOwner = new ItemData[lineCount, rowCount, depthCount];
    }

    public bool InBounds(Vector3Int origin, Vector3Int footprint)
    {
        if (origin.x < 0 || origin.y < 0 || origin.z < 0) return false;
        if (footprint.x <= 0 || footprint.y <= 0 || footprint.z <= 0) return false; // guard against malformed 0/negative footprints
        if (origin.x + footprint.x > lineCount) return false;
        if (origin.y + footprint.y > rowCount) return false;
        if (origin.z + footprint.z > depthCount) return false;
        return true;
    }

    /// <summary>
    /// Checks whether footprint fits at origin, ignoring cells currently owned by "ignoreOwner"
    /// (used when re-checking space for an item that's about to be re-placed/rotated in place).
    /// </summary>
    public bool IsRegionFree(Vector3Int origin, Vector3Int footprint, ItemData ignoreOwner = null)
    {
        if (!InBounds(origin, footprint)) return false;

        for (int x = origin.x; x < origin.x + footprint.x; x++)
            for (int y = origin.y; y < origin.y + footprint.y; y++)
                for (int z = origin.z; z < origin.z + footprint.z; z++)
                {
                    ItemData occupant = cellOwner[x, y, z];
                    if (occupant != null && occupant != ignoreOwner) return false;
                }
        return true;
    }

    /// <summary>
    /// Like IsRegionFree, but additionally rejects any Z>0 cell whose depth lane is reserved for a
    /// *different* itemID. Use this for fresh placement so unrelated items never spill into another
    /// item's stacking depth. Duplicate-stacking placement for "itemID" should use this too, so it
    /// naturally reuses/extends its own reserved lanes but never steals someone else's.
    /// </summary>
    public bool IsRegionFreeForItem(Vector3Int origin, Vector3Int footprint, string itemID, ItemData ignoreOwner = null)
    {
        if (!IsRegionFree(origin, footprint, ignoreOwner)) return false;

        for (int x = origin.x; x < origin.x + footprint.x; x++)
            for (int z = origin.z; z < origin.z + footprint.z; z++)
            {
                if (z == 0) continue; // z=0 (front layer) is never reserved
                if (depthLaneOwner.TryGetValue((x, z), out string owner) && owner != itemID)
                    return false;
            }
        return true;
    }

    public void Occupy(Vector3Int origin, Vector3Int footprint, ItemData owner)
    {
        for (int x = origin.x; x < origin.x + footprint.x; x++)
            for (int y = origin.y; y < origin.y + footprint.y; y++)
                for (int z = origin.z; z < origin.z + footprint.z; z++)
                {
                    cellOwner[x, y, z] = owner;
                    if (z > 0) depthLaneOwner[(x, z)] = owner.itemID;
                }
    }

    public void Clear(Vector3Int origin, Vector3Int footprint)
    {
        for (int x = origin.x; x < origin.x + footprint.x; x++)
            for (int y = origin.y; y < origin.y + footprint.y; y++)
                for (int z = origin.z; z < origin.z + footprint.z; z++)
                {
                    if (cellOwner[x, y, z] != null) cellOwner[x, y, z] = null;
                }

        // Release any depth lanes that are no longer occupied by anyone after this clear.
        for (int x = origin.x; x < origin.x + footprint.x; x++)
            for (int z = Mathf.Max(1, origin.z); z < origin.z + footprint.z; z++)
            {
                ReleaseDepthLaneIfEmpty(x, z);
            }
    }

    private void ReleaseDepthLaneIfEmpty(int x, int z)
    {
        for (int y = 0; y < rowCount; y++)
        {
            if (cellOwner[x, y, z] != null) return; // still occupied by someone
        }
        depthLaneOwner.Remove((x, z));
    }

    /// <summary>Removes an item's current occupancy footprint entirely. Call before re-placing/rotating it.</summary>
    public void Vacate(ItemData item)
    {
        Clear(item.gridPosition, item.currentFootprint);
    }

    /// <summary>
    /// Returns every free width-slot start index (x) on the front depth layer (z=0), scanned left
    /// to right. "Free" here just means cell (x, rowIndex, 0) is empty - full fit validation still
    /// happens via IsRegionFree / IsRegionFreeForItem.
    /// </summary>
    public IEnumerable<int> FreeColumnStarts(int rowIndex = 0)
    {
        for (int x = 0; x < lineCount; x++)
        {
            if (cellOwner[x, rowIndex, 0] == null) yield return x;
        }
    }

    /// <summary>True if the depth lane (x, z) is either unreserved or already reserved for itemID.</summary>
    public bool DepthLaneAvailableFor(int x, int z, string itemID)
    {
        if (z == 0) return true;
        return !depthLaneOwner.TryGetValue((x, z), out string owner) || owner == itemID;
    }

    public bool CellIsFree(int x, int y, int z) => cellOwner[x, y, z] == null;
}