simulate items on splitters and fix bugs where buildings could not pull from splitters

This commit is contained in:
2026-04-23 21:54:59 +02:00
parent 78f746d352
commit ea30d2ab7b
3 changed files with 323 additions and 58 deletions

View File

@@ -66,6 +66,7 @@ void BeltSystem::placeSplitter(QPoint tile, Rotation outputA, Rotation outputB)
st.outputA = outputA;
st.outputB = outputB;
st.nextOutputIsA = true;
st.backDir = Rotation::North; // irrelevant until back is set
m_splitters[key(tile)] = st;
}
@@ -104,45 +105,79 @@ bool BeltSystem::tryPutItem(QPoint tile, Item item)
std::optional<Item> BeltSystem::tryTakeItem(Port port)
{
const std::map<std::pair<int, int>, BeltTile>::iterator it = m_belts.find(key(port.tile));
if (it == m_belts.end())
{
return std::nullopt;
}
if (it->second.direction != port.direction)
const std::map<std::pair<int, int>, BeltTile>::iterator beltIt = m_belts.find(key(port.tile));
if (beltIt != m_belts.end())
{
if (beltIt->second.direction != port.direction)
{
return std::nullopt;
}
BeltTile& bt = beltIt->second;
if (bt.front && bt.front->progress >= 1.0)
{
const Item taken = bt.front->item;
bt.front = bt.back;
bt.back = std::nullopt;
return taken;
}
return std::nullopt;
}
BeltTile& bt = it->second;
if (bt.front && bt.front->progress >= 1.0)
const std::map<std::pair<int, int>, SplitterTile>::iterator splIt =
m_splitters.find(key(port.tile));
if (splIt != m_splitters.end())
{
const Item taken = bt.front->item;
bt.front = bt.back;
bt.back = std::nullopt;
return taken;
SplitterTile& st = splIt->second;
if (port.direction == st.outputA && st.frontA && st.frontA->progress >= 1.0)
{
const Item taken = st.frontA->item;
st.frontA = std::nullopt;
return taken;
}
if (port.direction == st.outputB && st.frontB && st.frontB->progress >= 1.0)
{
const Item taken = st.frontB->item;
st.frontB = std::nullopt;
return taken;
}
}
return std::nullopt;
}
std::optional<ItemType> BeltSystem::peekItem(Port port) const
{
const std::map<std::pair<int, int>, BeltTile>::const_iterator it =
const std::map<std::pair<int, int>, BeltTile>::const_iterator beltIt =
m_belts.find(key(port.tile));
if (it == m_belts.end())
{
return std::nullopt;
}
if (it->second.direction != port.direction)
if (beltIt != m_belts.end())
{
if (beltIt->second.direction != port.direction)
{
return std::nullopt;
}
const BeltTile& bt = beltIt->second;
if (bt.front && bt.front->progress >= 1.0)
{
return bt.front->item.type;
}
return std::nullopt;
}
const BeltTile& bt = it->second;
if (bt.front && bt.front->progress >= 1.0)
const std::map<std::pair<int, int>, SplitterTile>::const_iterator splIt =
m_splitters.find(key(port.tile));
if (splIt != m_splitters.end())
{
return bt.front->item.type;
const SplitterTile& st = splIt->second;
if (port.direction == st.outputA && st.frontA && st.frontA->progress >= 1.0)
{
return st.frontA->item.type;
}
if (port.direction == st.outputB && st.frontB && st.frontB->progress >= 1.0)
{
return st.frontB->item.type;
}
}
return std::nullopt;
}
@@ -164,7 +199,9 @@ void BeltSystem::clearTiles(const std::vector<QPoint>& tiles)
const std::map<std::pair<int, int>, SplitterTile>::iterator sIt = m_splitters.find(key(tile));
if (sIt != m_splitters.end())
{
sIt->second.heldItem = std::nullopt;
sIt->second.back = std::nullopt;
sIt->second.frontA = std::nullopt;
sIt->second.frontB = std::nullopt;
}
}
}
@@ -216,10 +253,44 @@ void BeltSystem::advanceProgress()
}
}
}
for (std::map<std::pair<int, int>, SplitterTile>::iterator it = m_splitters.begin();
it != m_splitters.end(); ++it)
{
SplitterTile& st = it->second;
if (st.back)
{
st.back->progress += m_progressPerTick;
if (st.back->progress > 0.5)
{
st.back->progress = 0.5;
}
}
if (st.frontA)
{
st.frontA->progress += m_progressPerTick;
if (st.frontA->progress > 1.0)
{
st.frontA->progress = 1.0;
}
}
if (st.frontB)
{
st.frontB->progress += m_progressPerTick;
if (st.frontB->progress > 1.0)
{
st.frontB->progress = 1.0;
}
}
}
}
void BeltSystem::moveItemsToNextTile()
{
// Belt items advancing into the next tile.
for (std::map<std::pair<int, int>, BeltTile>::iterator it = m_belts.begin();
it != m_belts.end(); ++it)
{
@@ -246,18 +317,47 @@ void BeltSystem::moveItemsToNextTile()
}
else if (nextSplitter != m_splitters.end())
{
if (!nextSplitter->second.heldItem)
if (!nextSplitter->second.back)
{
nextSplitter->second.heldItem = bt.front->item;
nextSplitter->second.back = BeltItemSlot{bt.front->item, 0.0};
nextSplitter->second.backDir = bt.direction;
bt.front = bt.back;
bt.back = std::nullopt;
}
// else: splitter busy — item stays blocked at progress 1.0.
// else: splitter back occupied — item stays blocked at progress 1.0.
}
// else: no tile registered (e.g. open space, or building input port).
// Items leaving into unregistered tiles are not consumed here — the
// building pull step uses tryTakeItem for that.
}
// Splitter front slots advancing into downstream belt tiles.
for (std::map<std::pair<int, int>, SplitterTile>::iterator it = m_splitters.begin();
it != m_splitters.end(); ++it)
{
SplitterTile& st = it->second;
const QPoint here = QPoint(it->first.first, it->first.second);
if (st.frontA && st.frontA->progress >= 1.0)
{
const QPoint dest = adjacentTile(here, st.outputA);
if (tryPlaceOnBelt(dest, st.frontA->item))
{
st.frontA = std::nullopt;
}
// else: downstream belt full or absent — item stays at progress 1.0
// for building pickup via tryTakeItem.
}
if (st.frontB && st.frontB->progress >= 1.0)
{
const QPoint dest = adjacentTile(here, st.outputB);
if (tryPlaceOnBelt(dest, st.frontB->item))
{
st.frontB = std::nullopt;
}
}
}
}
void BeltSystem::routeSplitterItems()
@@ -266,12 +366,12 @@ void BeltSystem::routeSplitterItems()
it != m_splitters.end(); ++it)
{
SplitterTile& st = it->second;
if (!st.heldItem)
if (!st.back || st.back->progress < 0.5)
{
continue;
}
const Item& item = *st.heldItem;
const Item& item = st.back->item;
const bool matchesA = st.filterA.empty() ||
std::find(st.filterA.begin(), st.filterA.end(), item.type) != st.filterA.end();
@@ -280,42 +380,52 @@ void BeltSystem::routeSplitterItems()
if (matchesA && !matchesB)
{
const QPoint dest = adjacentTile(QPoint(it->first.first, it->first.second), st.outputA);
if (tryPlaceOnBelt(dest, item))
if (!st.frontA)
{
st.heldItem = std::nullopt;
st.frontA = BeltItemSlot{item, 0.0};
st.back = std::nullopt;
}
}
else if (matchesB && !matchesA)
{
const QPoint dest = adjacentTile(QPoint(it->first.first, it->first.second), st.outputB);
if (tryPlaceOnBelt(dest, item))
if (!st.frontB)
{
st.heldItem = std::nullopt;
st.frontB = BeltItemSlot{item, 0.0};
st.back = std::nullopt;
}
}
else if (matchesA && matchesB)
{
// Alternation: try preferred output first, fall back to other.
const Rotation preferred = st.nextOutputIsA ? st.outputA : st.outputB;
const Rotation fallback = st.nextOutputIsA ? st.outputB : st.outputA;
// Alternation: try preferred output first, fall back to other if preferred full.
const bool preferA = st.nextOutputIsA;
const QPoint prefDest = adjacentTile(QPoint(it->first.first, it->first.second), preferred);
const QPoint fbDest = adjacentTile(QPoint(it->first.first, it->first.second), fallback);
if (tryPlaceOnBelt(prefDest, item))
if (preferA && !st.frontA)
{
st.heldItem = std::nullopt;
st.nextOutputIsA = !st.nextOutputIsA;
st.frontA = BeltItemSlot{item, 0.0};
st.back = std::nullopt;
st.nextOutputIsA = false;
}
else if (tryPlaceOnBelt(fbDest, item))
else if (!preferA && !st.frontB)
{
st.heldItem = std::nullopt;
// nextOutputIsA stays: preferred was blocked, so we still owe it next.
st.frontB = BeltItemSlot{item, 0.0};
st.back = std::nullopt;
st.nextOutputIsA = true;
}
// else both blocked — item stays.
else if (preferA && !st.frontB)
{
// Preferred (A) is full — fall back to B; nextOutputIsA stays.
st.frontB = BeltItemSlot{item, 0.0};
st.back = std::nullopt;
}
else if (!preferA && !st.frontA)
{
// Preferred (B) is full — fall back to A; nextOutputIsA stays.
st.frontA = BeltItemSlot{item, 0.0};
st.back = std::nullopt;
}
// else both fronts occupied — back stays.
}
// else (!matchesA && !matchesB): stall — item stays in splitter.
// else (!matchesA && !matchesB): stall — back stays.
}
}
@@ -381,4 +491,39 @@ void BeltSystem::forEachVisualItem(QRect viewportTiles,
visit(vi);
}
}
for (const std::pair<const std::pair<int, int>, SplitterTile>& entry : m_splitters)
{
const QPoint tile(entry.first.first, entry.first.second);
if (!viewportTiles.contains(tile))
{
continue;
}
const SplitterTile& st = entry.second;
if (st.back)
{
VisualItem vi;
vi.type = st.back->item.type;
vi.worldPos = slotWorldPos(tile, st.backDir, st.back->progress);
visit(vi);
}
if (st.frontA)
{
VisualItem vi;
vi.type = st.frontA->item.type;
vi.worldPos = slotWorldPos(tile, st.outputA, st.frontA->progress);
visit(vi);
}
if (st.frontB)
{
VisualItem vi;
vi.type = st.frontB->item.type;
vi.worldPos = slotWorldPos(tile, st.outputB, st.frontB->progress);
visit(vi);
}
}
}