#include "SelectedBuildingPanel.h" #include #include #include #include #include #include #include #include #include #include "BeltSystem.h" #include "Command.h" #include "CommandRequestedEvent.h" #include "DynamicBodyComponent.h" #include "EntityAdmin.h" #include "EntitySelectedEvent.h" #include "EventManager.h" #include "FactionComponent.h" #include "HealthComponent.h" #include "ModuleOwnerComponent.h" #include "ShipIdentityComponent.h" #include "ShipStatsCalculator.h" #include "ShipStatsPanel.h" #include "ThreatCostCalculator.h" #include "StationBodyComponent.h" #include "TickAdvancedEvent.h" #include "Building.h" #include "BuildingSystem.h" #include "BuildingType.h" #include "ItemType.h" #include "LayoutDialogRequestedEvent.h" #include "ModulesConfig.h" #include "RecipeSelectionDialog.h" #include "RecipeSelectionRequestedEvent.h" #include "Rotation.h" #include "ShipLayoutPreview.h" #include "Simulation.h" #include "WeaponComponent.h" namespace { QString buildingTypeName(BuildingType type) { const std::string id = buildingTypeId(type); QString result; bool nextUpper = true; for (char c : id) { if (c == '_') { result += ' '; nextUpper = true; } else if (nextUpper) { result += static_cast(std::toupper(static_cast(c))); nextUpper = false; } else { result += c; } } return result; } bool isProductionBuilding(BuildingType type) { return type == BuildingType::Miner || type == BuildingType::Smelter || type == BuildingType::Assembler || type == BuildingType::ReprocessingPlant || type == BuildingType::Shipyard; } bool isBeltLike(BuildingType type) { return type == BuildingType::Belt || type == BuildingType::Splitter || type == BuildingType::TunnelEntry || type == BuildingType::TunnelExit; } QString rotationLabel(Rotation r) { switch (r) { case Rotation::North: return QObject::tr("North (↑)"); case Rotation::East: return QObject::tr("East (→)"); case Rotation::South: return QObject::tr("South (↓)"); case Rotation::West: return QObject::tr("West (←)"); } return ""; } } // namespace SelectedBuildingPanel::SelectedBuildingPanel(Simulation* sim, const GameConfig* config, QWidget* parent) : QWidget(parent) , m_sim(sim) , m_config(config) , m_singleBuildingId(kInvalidBuildingId) , m_splitterTile(0, 0) { m_layout = new QVBoxLayout(this); m_layout->setContentsMargins(8, 8, 8, 8); m_layout->setSpacing(4); m_layout->setAlignment(Qt::AlignTop); m_titleLabel = new QLabel(this); m_recipeSelectButton = new QPushButton(this); m_clearBeltBtn = new QPushButton(tr("Clear Items"), this); m_filterALabel = new QLabel(this); m_filterAList = new QListWidget(this); m_filterBLabel = new QLabel(this); m_filterBList = new QListWidget(this); m_layoutPreview = new ShipLayoutPreview(this); m_configureLayoutBtn = new QPushButton(tr("Configure Layout"), this); m_buffersLabel = new QLabel(this); m_buffersLabel->setWordWrap(true); m_filterAList->setMaximumHeight(100); m_filterBList->setMaximumHeight(100); m_layout->addWidget(m_titleLabel); m_layout->addWidget(m_recipeSelectButton); m_layout->addWidget(m_layoutPreview); m_layout->addWidget(m_configureLayoutBtn); m_layout->addWidget(m_clearBeltBtn); m_layout->addWidget(m_filterALabel); m_layout->addWidget(m_filterAList); m_layout->addWidget(m_filterBLabel); m_layout->addWidget(m_filterBList); m_layout->addWidget(m_buffersLabel); connect(m_recipeSelectButton, &QPushButton::clicked, this, &SelectedBuildingPanel::onSelectRecipeClicked); connect(m_clearBeltBtn, &QPushButton::clicked, this, &SelectedBuildingPanel::onClearBelt); connect(m_configureLayoutBtn, &QPushButton::clicked, this, [this]() { if (m_singleBuildingId != kInvalidBuildingId) { EventManager::getInstance()->sendEventImmediately( std::make_shared(m_singleBuildingId)); } }); connect(m_filterAList, &QListWidget::itemChanged, this, &SelectedBuildingPanel::onSplitterFilterChanged); connect(m_filterBList, &QListWidget::itemChanged, this, &SelectedBuildingPanel::onSplitterFilterChanged); m_entityTitleLabel = new QLabel(this); QFont titleFont = m_entityTitleLabel->font(); titleFont.setBold(true); m_entityTitleLabel->setFont(titleFont); m_layout->addWidget(m_entityTitleLabel); m_entityTitleLabel->hide(); m_entityStatsPanel = new ShipStatsPanel(config, this); m_layout->addWidget(m_entityStatsPanel); m_entityStatsPanel->hide(); m_stationStatsLabel = new QLabel(this); m_stationStatsLabel->setWordWrap(true); m_layout->addWidget(m_stationStatsLabel); m_stationStatsLabel->hide(); buildEmpty(); registerForEvents(); } SelectedBuildingPanel::~SelectedBuildingPanel() { unregisterForEvents(); } void SelectedBuildingPanel::onSelectionChanged(const std::vector& ids) { m_selectedBuildingIds = ids; if (!ids.empty()) { clearEntityDisplay(); } rebuild(); } void SelectedBuildingPanel::rebuild() { if (m_selectedBuildingIds.empty()) { buildEmpty(); } else if (m_selectedBuildingIds.size() == 1) { buildSingle(m_selectedBuildingIds[0]); } else { buildMulti(m_selectedBuildingIds); } } void SelectedBuildingPanel::hideAllWidgets() { m_titleLabel->hide(); m_recipeSelectButton->hide(); m_layoutPreview->hide(); m_configureLayoutBtn->hide(); m_clearBeltBtn->hide(); m_filterALabel->hide(); m_filterAList->hide(); m_filterBLabel->hide(); m_filterBList->hide(); m_buffersLabel->hide(); } void SelectedBuildingPanel::clearContent() { m_singleBuildingId = kInvalidBuildingId; hideAllWidgets(); } void SelectedBuildingPanel::buildEmpty() { clearContent(); m_entityTitleLabel->hide(); m_entityStatsPanel->hide(); m_stationStatsLabel->hide(); } void SelectedBuildingPanel::buildSingle(BuildingId id) { m_singleBuildingId = id; hideAllWidgets(); const Building* b = m_sim->buildings().findBuilding(id); const ConstructionSite* s = b ? nullptr : m_sim->buildings().findSite(id); if (!b && !s) { buildEmpty(); return; } m_singleIsSite = (s != nullptr); // A construction site exposes the same configuration as the operational // building it will become (REQ-BLD-SITE-CONFIG). The only difference is // that its buffer/production rows are replaced by a construction-progress // line, since a site has no buffers and runs no production cycle. const BuildingType type = b ? b->type : s->type; const std::string& recipeId = b ? b->recipeId : s->recipeId; const std::optional& shipLayout = b ? b->shipLayout : s->shipLayout; const QPoint anchor = b ? b->anchor : s->anchor; m_titleLabel->setText(m_singleIsSite ? tr("(Building) %1").arg(buildingTypeName(type)) : buildingTypeName(type)); m_titleLabel->show(); m_buffersLabel->show(); if (isProductionBuilding(type)) { const std::vector options = buildRecipeSelectionOptions(type, *m_sim, *m_config); const RecipeSelectionOption* current = nullptr; for (const RecipeSelectionOption& option : options) { if (option.id == recipeId) { current = &option; break; } } if (current && !current->id.empty()) { m_recipeSelectButton->setText(current->caption); m_recipeSelectButton->setToolTip(current->tooltip); } else { const QString placeholder = (type == BuildingType::Shipyard) ? tr("Select schematic") : tr("Select recipe"); m_recipeSelectButton->setText(placeholder); m_recipeSelectButton->setToolTip(QString()); } m_recipeSelectButton->show(); if (type == BuildingType::Shipyard && !recipeId.empty()) { const ShipDef* sDef = findShipDef(recipeId); if (sDef && !sDef->layout.empty()) { ShipLayoutConfig layout; if (shipLayout.has_value()) { layout = *shipLayout; } m_layoutPreview->setShipAndLayout( sDef->layout, layout, &m_config->modules.modules); m_layoutPreview->show(); m_configureLayoutBtn->show(); } else { m_layoutPreview->hide(); m_configureLayoutBtn->hide(); } } else { m_layoutPreview->hide(); m_configureLayoutBtn->hide(); } } else { m_recipeSelectButton->hide(); m_layoutPreview->hide(); m_configureLayoutBtn->hide(); } // Belt "Clear" removes items from a live belt tile; a construction site has // none and is not registered with BeltSystem yet, so hide it for sites. if (isBeltLike(type) && !m_singleIsSite) { m_clearBeltBtn->show(); } else { m_clearBeltBtn->hide(); } if (type == BuildingType::Splitter) { std::optional info; if (m_singleIsSite) { info = m_sim->buildings().getSiteSplitterInfo(id); } else { m_splitterTile = anchor; info = m_sim->belts().getSplitterInfo(m_splitterTile); } buildSplitterFilters(info); } else { m_filterALabel->hide(); m_filterAList->hide(); m_filterBLabel->hide(); m_filterBList->hide(); } if (m_singleIsSite) { QString progress; if (s->completesAt == 0) { progress = tr("Queued"); } else { const BuildingDef* def = nullptr; for (const BuildingDef& d : m_config->buildings.buildings) { if (d.type == s->type) { def = &d; break; } } if (def && def->constructionTimeSeconds > 0) { const Tick duration = secondsToTicks(def->constructionTimeSeconds); const Tick elapsed = m_sim->currentTick() - (s->completesAt - duration); const int pct = static_cast( std::max(Tick(0), std::min(duration, elapsed)) * 100 / duration); progress = tr("%1% complete").arg(pct); } else { progress = tr("Building..."); } } m_buffersLabel->setText(progress); } else { refreshBuffers(b); } } void SelectedBuildingPanel::refreshBuffers(const Building* b) { const RecipeDef* recipe = findRecipe(b); const ShipDef* shipDef = (b->type == BuildingType::Shipyard) ? findShipDef(b->recipeId) : nullptr; QString bufText; if (!b->inputBuffer.counts.empty()) { bufText += tr("Input: "); for (const std::pair& entry : b->inputBuffer.counts) { int perCycle = 0; if (recipe) { for (const RecipeIngredient& ing : recipe->inputs) { if (ing.item == entry.first.id) { perCycle = ing.amount; break; } } } else if (shipDef) { for (const RecipeIngredient& mat : shipDef->schematic.materials) { if (mat.item == entry.first.id) { perCycle = mat.amount; break; } } if (b->shipLayout.has_value()) { for (const PlacedModule& pm : b->shipLayout->placedModules) { for (const ModuleDef& modDef : m_config->modules.modules) { if (modDef.id == pm.moduleId) { for (const RecipeIngredient& ing : modDef.materials) { if (ing.item == entry.first.id) { perCycle += ing.amount; } } break; } } } } } bufText += QString::fromStdString(entry.first.id) + ": " + QString::number(entry.second); if (perCycle > 0) { bufText += "/" + QString::number(perCycle); } bufText += " "; } bufText += "\n"; } if (recipe && !recipe->outputs.empty()) { std::map outCounts; for (const Item& item : b->outputBuffer.items) { outCounts[item.type.id]++; } bufText += tr("Output: "); for (const RecipeOutput& out : recipe->outputs) { const std::map::const_iterator it = outCounts.find(out.item); const int count = (it != outCounts.end()) ? it->second : 0; bufText += QString::fromStdString(out.item) + ": " + QString::number(count) + "/" + QString::number(out.amount) + " "; } } else if (!b->outputBuffer.items.empty()) { std::map outCounts; for (const Item& item : b->outputBuffer.items) { outCounts[item.type.id]++; } bufText += tr("Output: "); for (const std::pair& entry : outCounts) { bufText += QString::fromStdString(entry.first) + ": " + QString::number(entry.second) + " "; } } if (isProductionBuilding(b->type) && (recipe || shipDef)) { double durationSeconds = recipe ? recipe->durationSeconds : shipDef->schematic.productionTimeSeconds; if (shipDef && b->shipLayout.has_value()) { for (const PlacedModule& pm : b->shipLayout->placedModules) { for (const ModuleDef& modDef : m_config->modules.modules) { if (modDef.id == pm.moduleId) { durationSeconds += modDef.productionTimeSeconds; break; } } } } bufText += tr("Cycle: %1 s\n").arg(durationSeconds, 0, 'f', 1); if (b->production.has_value()) { const Tick cycleTicks = secondsToTicks(durationSeconds); const Tick completesAt = b->production->completesAt; const Tick currentTick = m_sim->currentTick(); const Tick elapsed = currentTick - (completesAt - cycleTicks); const int pct = static_cast( std::max(Tick(0), std::min(cycleTicks, elapsed)) * 100 / cycleTicks); bufText += tr("Progress: %1%\n").arg(pct); } else { bufText += tr("Progress: idle\n"); } } m_buffersLabel->setText(bufText); if (b->type == BuildingType::Shipyard && shipDef && !shipDef->layout.empty()) { ShipLayoutConfig layout; if (b->shipLayout.has_value()) { layout = *b->shipLayout; } m_layoutPreview->setShipAndLayout( shipDef->layout, layout, &m_config->modules.modules); } } const RecipeDef* SelectedBuildingPanel::findRecipe(const Building* b) const { if (b->recipeId.empty()) { return nullptr; } for (const RecipeDef& r : m_config->recipes.recipes) { if (r.id == b->recipeId && r.building == b->type) { return &r; } } return nullptr; } const ShipDef* SelectedBuildingPanel::findShipDef(const std::string& id) const { if (id.empty()) { return nullptr; } for (const ShipDef& s : m_config->ships.ships) { if (s.id == id) { return &s; } } return nullptr; } void SelectedBuildingPanel::handleEvent(std::shared_ptr /*event*/) { if (m_selectedEntity.has_value()) { refreshEntityStats(); return; } if (m_singleBuildingId == kInvalidBuildingId) { return; } const Building* b = m_sim->buildings().findBuilding(m_singleBuildingId); if (b) { if (m_titleLabel->text().startsWith(tr("(Building) "))) { rebuild(); } else { refreshBuffers(b); } return; } const ConstructionSite* s = m_sim->buildings().findSite(m_singleBuildingId); if (s) { rebuild(); return; } buildEmpty(); } void SelectedBuildingPanel::buildMulti(const std::vector& ids) { m_singleBuildingId = kInvalidBuildingId; m_recipeSelectButton->hide(); m_clearBeltBtn->hide(); m_filterALabel->hide(); m_filterAList->hide(); m_filterBLabel->hide(); m_filterBList->hide(); m_buffersLabel->hide(); std::map counts; for (BuildingId id : ids) { const Building* b = m_sim->buildings().findBuilding(id); if (b) { counts[b->type]++; continue; } const ConstructionSite* s = m_sim->buildings().findSite(id); if (s) { counts[s->type]++; } } bool hasBelt = false; QString text; for (const std::pair& entry : counts) { text += buildingTypeName(entry.first) + ": " + QString::number(entry.second) + "\n"; if (isBeltLike(entry.first)) { hasBelt = true; } } m_titleLabel->setText(text.trimmed()); m_titleLabel->show(); if (hasBelt) { m_clearBeltBtn->show(); } } void SelectedBuildingPanel::onSelectRecipeClicked() { if (m_singleBuildingId == kInvalidBuildingId) { return; } // The emit is synchronous: MainWindow pauses the game, runs the modal // selection dialog, applies the chosen recipe/schematic, and restores the // speed before this returns. rebuild() then refreshes the button caption, // tooltip, preview, and buffers for the new selection. EventManager::getInstance()->sendEventImmediately( std::make_shared(m_singleBuildingId)); rebuild(); } void SelectedBuildingPanel::buildSplitterFilters( const std::optional& info) { if (!info.has_value()) { m_filterALabel->hide(); m_filterAList->hide(); m_filterBLabel->hide(); m_filterBList->hide(); return; } const std::vector items = allItemIds(); auto populateList = [&](QListWidget* list, QLabel* label, const QString& dirLabel, const std::vector& filter) { label->setText(tr("%1 filter (empty = all):").arg(dirLabel)); list->blockSignals(true); list->clear(); for (const std::string& itemId : items) { if (!m_sim->isItemUnlocked(itemId)) { continue; } QListWidgetItem* row = new QListWidgetItem( QString::fromStdString(itemId), list); const bool checked = filter.empty() ? false : std::find(filter.begin(), filter.end(), ItemType{itemId}) != filter.end(); row->setCheckState(checked ? Qt::Checked : Qt::Unchecked); row->setFlags(row->flags() | Qt::ItemIsUserCheckable); } list->blockSignals(false); label->show(); list->show(); }; populateList(m_filterAList, m_filterALabel, rotationLabel(info->outputA), info->filterA); populateList(m_filterBList, m_filterBLabel, rotationLabel(info->outputB), info->filterB); } void SelectedBuildingPanel::onSplitterFilterChanged() { if (m_singleBuildingId == kInvalidBuildingId) { return; } auto collectFilter = [](QListWidget* list) -> std::vector { std::vector filter; for (int i = 0; i < list->count(); ++i) { const QListWidgetItem* row = list->item(i); if (row->checkState() == Qt::Checked) { filter.push_back(ItemType{row->text().toStdString()}); } } return filter; }; if (m_singleIsSite) { std::shared_ptr command = std::make_shared(); command->id = m_singleBuildingId; command->filterA = collectFilter(m_filterAList); command->filterB = collectFilter(m_filterBList); EventManager::getInstance()->sendEventImmediately( std::make_shared(command)); } else { std::shared_ptr command = std::make_shared(); command->tile = m_splitterTile; command->filterA = collectFilter(m_filterAList); command->filterB = collectFilter(m_filterBList); EventManager::getInstance()->sendEventImmediately( std::make_shared(command)); } } std::vector SelectedBuildingPanel::allItemIds() const { std::set seen; for (const RecipeDef& recipe : m_config->recipes.recipes) { for (const RecipeIngredient& ing : recipe.inputs) { seen.insert(ing.item); } for (const RecipeOutput& out : recipe.outputs) { seen.insert(out.item); } } return std::vector(seen.begin(), seen.end()); } void SelectedBuildingPanel::onClearBelt() { std::vector tiles; for (BuildingId id : m_selectedBuildingIds) { const Building* b = m_sim->buildings().findBuilding(id); if (b && isBeltLike(b->type)) { for (const QPoint& cell : b->bodyCells) { tiles.push_back(cell); } } } if (!tiles.empty()) { std::shared_ptr command = std::make_shared(); command->tiles = std::move(tiles); EventManager::getInstance()->sendEventImmediately( std::make_shared(command)); } } void SelectedBuildingPanel::handleEvent(std::shared_ptr event) { if (event->entity.has_value()) { m_selectedEntity = event->entity; m_selectedBuildingIds.clear(); clearContent(); EntityAdmin& admin = m_sim->admin(); entt::entity entity = *m_selectedEntity; if (!admin.isValid(entity)) { clearEntityDisplay(); return; } if (admin.hasAll(entity)) { buildEntityShip(entity); } else if (admin.hasAll(entity)) { buildEntityStation(entity); } } else { clearEntityDisplay(); } } void SelectedBuildingPanel::buildEntityShip(entt::entity entity) { EntityAdmin& admin = m_sim->admin(); const ShipIdentityComponent& identity = admin.get(entity); const HealthComponent& health = admin.get(entity); m_entityTitleLabel->setText(tr("Ship: %1") .arg(QString::fromStdString(identity.schematicId))); m_entityTitleLabel->show(); const ShipStats stats = buildShipStatsFromEntity(admin, entity); m_entityStatsPanel->refreshFromLive(stats, health.hp); m_entityStatsPanel->setDebugDrawEnabled(m_debugDraw); for (const ShipDef& def : m_config->ships.ships) { if (def.id == identity.schematicId) { double threat = calculateShipThreatCost( m_config->threatCosts, *m_config, def.id, def.defaultModules); m_entityStatsPanel->setThreatCost(threat); break; } } m_entityStatsPanel->show(); m_stationStatsLabel->hide(); } void SelectedBuildingPanel::buildEntityStation(entt::entity entity) { EntityAdmin& admin = m_sim->admin(); const HealthComponent& health = admin.get(entity); m_entityTitleLabel->setText(tr("Defence Station")); m_entityTitleLabel->show(); float totalDps = 0.0f; float maxRange = 0.0f; bool hasWeapons = false; admin.forEach( [&](entt::entity /*child*/, const ModuleOwnerComponent& owner, const WeaponComponent& w) { if (owner.owner != entity) { return; } hasWeapons = true; totalDps += w.damage * w.fireRateHz; if (w.range_tiles > maxRange) { maxRange = w.range_tiles; } }); QString statsText = tr("HP: %1 / %2") .arg(static_cast(health.hp + 0.5f)) .arg(static_cast(health.maxHp + 0.5f)); if (hasWeapons) { statsText += tr("\nDPS: %1").arg(QString::number(static_cast(totalDps), 'f', 1)); statsText += tr("\nRange: %1 tiles").arg(QString::number(static_cast(maxRange), 'f', 1)); } m_stationStatsLabel->setText(statsText); m_stationStatsLabel->show(); m_entityStatsPanel->hide(); } void SelectedBuildingPanel::refreshEntityStats() { if (!m_selectedEntity.has_value()) { return; } EntityAdmin& admin = m_sim->admin(); entt::entity entity = *m_selectedEntity; if (!admin.isValid(entity)) { clearEntityDisplay(); return; } const HealthComponent& health = admin.get(entity); if (health.hp <= 0.0f) { clearEntityDisplay(); return; } if (admin.hasAll(entity)) { const ShipStats stats = buildShipStatsFromEntity(admin, entity); m_entityStatsPanel->refreshFromLive(stats, health.hp); } else if (admin.hasAll(entity)) { buildEntityStation(entity); } } void SelectedBuildingPanel::clearEntityDisplay() { m_selectedEntity = std::nullopt; m_entityTitleLabel->hide(); m_entityStatsPanel->hide(); m_stationStatsLabel->hide(); } void SelectedBuildingPanel::handleEvent(std::shared_ptr event) { onSelectionChanged(event->ids); } void SelectedBuildingPanel::handleEvent(std::shared_ptr event) { m_debugDraw = event->active; m_entityStatsPanel->setDebugDrawEnabled(event->active); }