free the buffer setup and belt registration from BuildingSystem
This commit is contained in:
@@ -47,139 +47,6 @@ BuildingSystem::BuildingSystem(const GameConfig& config,
|
||||
// Private helpers
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
void BuildingSystem::initBuffers(Building& b, const RecipeDef& recipe) const
|
||||
{
|
||||
b.inputBuffer.counts.clear();
|
||||
b.inputBuffer.caps.clear();
|
||||
for (const RecipeIngredient& ing : recipe.inputs)
|
||||
{
|
||||
const ItemType type{ing.item};
|
||||
b.inputBuffer.counts[type] = 0;
|
||||
b.inputBuffer.caps[type] = 2 * ing.amount;
|
||||
}
|
||||
|
||||
b.outputBuffer.items.clear();
|
||||
if (b.type == BuildingType::ReprocessingPlant)
|
||||
{
|
||||
// 1× max-per-roll (REQ-MAT-OUTPUT-BUFFER-REPROCESSING).
|
||||
int maxAmount = 0;
|
||||
for (const RecipeOutput& out : recipe.outputs)
|
||||
{
|
||||
if (out.amount > maxAmount)
|
||||
{
|
||||
maxAmount = out.amount;
|
||||
}
|
||||
}
|
||||
b.outputBuffer.capacity = maxAmount;
|
||||
}
|
||||
else
|
||||
{
|
||||
// 2× per-cycle output.
|
||||
int totalAmount = 0;
|
||||
for (const RecipeOutput& out : recipe.outputs)
|
||||
{
|
||||
totalAmount += out.amount;
|
||||
}
|
||||
b.outputBuffer.capacity = 2 * totalAmount;
|
||||
}
|
||||
}
|
||||
|
||||
void BuildingSystem::initAutoBuffers(Building& b) const
|
||||
{
|
||||
b.inputBuffer.counts.clear();
|
||||
b.inputBuffer.caps.clear();
|
||||
|
||||
// Union the inputs of every recipe of this building type; the cap for each
|
||||
// item is twice the largest per-cycle requirement across those recipes.
|
||||
// Output capacity follows the same rules as initBuffers: the Reprocessing
|
||||
// Plant holds one cycle's max output (REQ-MAT-OUTPUT-BUFFER-REPROCESSING),
|
||||
// other auto buildings hold twice the largest per-cycle output.
|
||||
int outputCapacity = 0;
|
||||
for (const RecipeDef& recipe : m_config.recipes.recipes)
|
||||
{
|
||||
if (recipe.building != b.type)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
for (const RecipeIngredient& ing : recipe.inputs)
|
||||
{
|
||||
const ItemType type{ing.item};
|
||||
b.inputBuffer.counts[type] = 0;
|
||||
b.inputBuffer.caps[type] =
|
||||
std::max(b.inputBuffer.caps[type], 2 * ing.amount);
|
||||
}
|
||||
|
||||
if (b.type == BuildingType::ReprocessingPlant)
|
||||
{
|
||||
int maxAmount = 0;
|
||||
for (const RecipeOutput& out : recipe.outputs)
|
||||
{
|
||||
maxAmount = std::max(maxAmount, out.amount);
|
||||
}
|
||||
outputCapacity = std::max(outputCapacity, maxAmount);
|
||||
}
|
||||
else
|
||||
{
|
||||
int totalAmount = 0;
|
||||
for (const RecipeOutput& out : recipe.outputs)
|
||||
{
|
||||
totalAmount += out.amount;
|
||||
}
|
||||
outputCapacity = std::max(outputCapacity, 2 * totalAmount);
|
||||
}
|
||||
}
|
||||
|
||||
b.outputBuffer.items.clear();
|
||||
b.outputBuffer.capacity = outputCapacity;
|
||||
}
|
||||
|
||||
void BuildingSystem::initShipyardBuffers(Building& b) const
|
||||
{
|
||||
b.inputBuffer.counts.clear();
|
||||
b.inputBuffer.caps.clear();
|
||||
b.outputBuffer.items.clear();
|
||||
b.outputBuffer.capacity = 0;
|
||||
const ShipDef* def = m_config.ships.findShipDef(b.recipeId);
|
||||
if (!def)
|
||||
{
|
||||
return;
|
||||
}
|
||||
for (const RecipeIngredient& ing : def->schematic.materials)
|
||||
{
|
||||
const ItemType type{ing.item};
|
||||
b.inputBuffer.counts[type] = 0;
|
||||
b.inputBuffer.caps[type] = 2 * ing.amount;
|
||||
}
|
||||
if (b.shipLayout.has_value())
|
||||
{
|
||||
for (const PlacedModule& pm : b.shipLayout->placedModules)
|
||||
{
|
||||
const ModuleDef* modDef = m_config.modules.findModuleDef(pm.moduleId);
|
||||
if (!modDef)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
for (const RecipeIngredient& ing : modDef->materials)
|
||||
{
|
||||
const ItemType type{ing.item};
|
||||
b.inputBuffer.counts.try_emplace(type, 0);
|
||||
b.inputBuffer.caps[type] += 2 * ing.amount;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void BuildingSystem::initSalvageBayBuffer(Building& b) const
|
||||
{
|
||||
// Salvage Bay has no recipe-driven buffer; its output-buffer holding size for
|
||||
// ship drop-off is config-defined (REQ-BLD-SALVAGE-BAY).
|
||||
b.outputBuffer.items.clear();
|
||||
const BuildingDef* def = m_config.buildings.findBuildingDef(BuildingType::SalvageBay);
|
||||
b.outputBuffer.capacity =
|
||||
(def && def->outputBufferCapacity) ? *def->outputBufferCapacity : 0;
|
||||
}
|
||||
|
||||
|
||||
std::vector<Item> BuildingSystem::rollReprocessingOutput(const RecipeDef& recipe)
|
||||
{
|
||||
@@ -397,7 +264,7 @@ void BuildingSystem::setRecipe(FactoryState& state, BuildingId id, const std::st
|
||||
{
|
||||
if (building.type == BuildingType::Shipyard)
|
||||
{
|
||||
initShipyardBuffers(building);
|
||||
initShipyardBuffers(m_config, building);
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -441,7 +308,7 @@ void BuildingSystem::setShipLayout(FactoryState& state, BuildingId id, const Shi
|
||||
for (std::vector<BeltItemSlot>& lane : building.incomingItems) { lane.clear(); }
|
||||
if (!building.recipeId.empty() && building.type == BuildingType::Shipyard)
|
||||
{
|
||||
initShipyardBuffers(building);
|
||||
initShipyardBuffers(m_config, building);
|
||||
}
|
||||
return;
|
||||
}
|
||||
@@ -525,19 +392,19 @@ void BuildingSystem::tickConstruction(FactoryState& state, Tick currentTick)
|
||||
|
||||
if (building.type == BuildingType::SalvageBay)
|
||||
{
|
||||
initSalvageBayBuffer(building);
|
||||
initSalvageBayBuffer(m_config, building);
|
||||
}
|
||||
else if (isAutoRecipeBuildingType(building.type))
|
||||
{
|
||||
// Smelter/Reprocessing Plant need no recipe selection; buffers are set
|
||||
// up from all recipes of the type (REQ-BLD-SMELTER, REQ-BLD-REPROCESSING).
|
||||
initAutoBuffers(building);
|
||||
initAutoBuffers(m_config, building);
|
||||
}
|
||||
else if (!building.recipeId.empty())
|
||||
{
|
||||
if (building.type == BuildingType::Shipyard)
|
||||
{
|
||||
initShipyardBuffers(building);
|
||||
initShipyardBuffers(m_config, building);
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -551,7 +418,7 @@ void BuildingSystem::tickConstruction(FactoryState& state, Tick currentTick)
|
||||
|
||||
// Register with BeltSystem before the move (mask/building stays valid). Any
|
||||
// filters configured while under construction carry over (REQ-BLD-SITE-CONFIG).
|
||||
reregisterBeltTile(building, front.splitterFilterA, front.splitterFilterB);
|
||||
reregisterBeltTile(m_belts, m_config, building, front.splitterFilterA, front.splitterFilterB);
|
||||
|
||||
state.buildings.push_back(std::move(building));
|
||||
|
||||
@@ -570,34 +437,6 @@ void BuildingSystem::tickConstruction(FactoryState& state, Tick currentTick)
|
||||
}
|
||||
}
|
||||
|
||||
void BuildingSystem::reregisterBeltTile(const Building& building,
|
||||
const std::vector<ItemType>& splitterFilterA,
|
||||
const std::vector<ItemType>& splitterFilterB)
|
||||
{
|
||||
switch (building.type)
|
||||
{
|
||||
case BuildingType::Belt:
|
||||
m_belts.placeBelt(building.anchor, building.rotation);
|
||||
break;
|
||||
case BuildingType::Splitter:
|
||||
assert(building.outputPorts.size() >= 2);
|
||||
m_belts.placeSplitter(building.anchor,
|
||||
building.outputPorts[0].direction,
|
||||
building.outputPorts[1].direction);
|
||||
m_belts.setSplitterFilters(building.anchor, splitterFilterA, splitterFilterB);
|
||||
break;
|
||||
case BuildingType::TunnelEntry:
|
||||
m_belts.placeTunnelEntry(building.anchor, building.rotation,
|
||||
m_config.world.tunnelMaxDistance_tiles);
|
||||
break;
|
||||
case BuildingType::TunnelExit:
|
||||
m_belts.placeTunnelExit(building.anchor, building.rotation);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void BuildingSystem::tickDeconstruction(FactoryState& state, Tick currentTick)
|
||||
{
|
||||
TRACE();
|
||||
@@ -659,7 +498,7 @@ void BuildingSystem::cancelDeconstruction(FactoryState& state, BuildingId id)
|
||||
if (Building* building = findBuilding(state, id))
|
||||
{
|
||||
building->queuedForDeconstruction = false;
|
||||
reregisterBeltTile(*building, it->splitterFilterA, it->splitterFilterB);
|
||||
reregisterBeltTile(m_belts, m_config, *building, it->splitterFilterA, it->splitterFilterB);
|
||||
}
|
||||
|
||||
state.deconstructionQueue.erase(it);
|
||||
@@ -1151,7 +990,7 @@ void BuildingSystem::rotateInPlace(FactoryState& state, BuildingId id, Rotation
|
||||
}
|
||||
|
||||
m_belts.removeTile(b.anchor);
|
||||
reregisterBeltTile(b, splitterFilterA, splitterFilterB);
|
||||
reregisterBeltTile(m_belts, m_config, b, splitterFilterA, splitterFilterB);
|
||||
}
|
||||
|
||||
return;
|
||||
@@ -1191,7 +1030,7 @@ BuildingId BuildingSystem::placeImmediate(FactoryState& state, BuildingType type
|
||||
|
||||
if (type == BuildingType::SalvageBay)
|
||||
{
|
||||
initSalvageBayBuffer(building);
|
||||
initSalvageBayBuffer(m_config, building);
|
||||
}
|
||||
|
||||
state.buildings.push_back(std::move(building));
|
||||
|
||||
Reference in New Issue
Block a user