using System.Collections.Generic;
using UnityEngine;
///
/// 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.
///
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;
}
///
/// 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).
///
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;
}
///
/// 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.
///
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));
}
/// Removes an item's current occupancy footprint entirely. Call before re-placing/rotating it.
public void Vacate(ItemData item)
{
Clear(item.gridPosition, item.currentFootprint);
}
///
/// 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.
///
public IEnumerable FreeColumnStarts(int rowIndex = 0)
{
for (int x = 0; x < lineCount; x++)
{
if (cellOwner[x, rowIndex, 0] == null) yield return x;
}
}
/// True if the depth lane (x, z) is either unreserved or already reserved for itemID.
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;
}