cargo component refactoring

This commit is contained in:
2026-06-21 18:35:02 +02:00
parent 665060bcd2
commit a472ec196c
17 changed files with 188 additions and 114 deletions

View File

@@ -4,12 +4,13 @@
#include <stdexcept>
#include <utility>
#include "CargoComponent.h"
#include "DynamicBodyComponent.h"
#include "EntityAdmin.h"
#include "HealthComponent.h"
#include "ModuleOwnerComponent.h"
#include "RepairToolComponent.h"
#include "SalvageCargoComponent.h"
#include "SalvagerComponent.h"
#include "SensorRangeComponent.h"
#include "Tick.h"
#include "WeaponComponent.h"
@@ -66,6 +67,10 @@ ShipStats calculateShipStats(const GameConfig& config,
std::vector<SalvageInstance> salvageInstances;
std::vector<RepairInstance> repairInstances;
// Cargo capacity is a ship-level stat (REQ-MOD-CARGO-CAPACITY): base is the sum
// of every salvage module's contribution.
double cargoCapacityBase = 0.0;
for (const PlacedModule& pm : modules)
{
const ModuleDef* def = findModuleDef(pm.moduleId);
@@ -85,6 +90,8 @@ ShipStats calculateShipStats(const GameConfig& config,
}
if (def->salvageCapability)
{
cargoCapacityBase += def->salvageCapability->cargoCapacityFormula.evaluate(mx);
SalvageInstance si;
si.range_tiles = static_cast<float>(def->salvageCapability->collectionRangeFormula.evaluate(mx) / tileSize);
si.rate = static_cast<float>(def->salvageCapability->collectionRateFormula.evaluate(mx));
@@ -106,6 +113,7 @@ ShipStats calculateShipStats(const GameConfig& config,
std::map<std::string, std::pair<double, double>> weaponMods;
std::map<std::string, std::pair<double, double>> salvageMods;
std::map<std::string, std::pair<double, double>> repairMods;
std::map<std::string, std::pair<double, double>> cargoMods;
for (const PlacedModule& pm : modules)
{
@@ -123,15 +131,16 @@ ShipStats calculateShipStats(const GameConfig& config,
const bool isWeaponStat = (sm.stat == "damage"
|| sm.stat == "attack_range"
|| sm.stat == "attack_rate");
const bool isSalvageStat = (sm.stat == "collection_range"
|| sm.stat == "cargo_capacity");
const bool isSalvageStat = (sm.stat == "collection_range");
const bool isRepairStat = (sm.stat == "repair_rate"
|| sm.stat == "repair_range");
const bool isCargoStat = (sm.stat == "cargo_capacity");
std::map<std::string, std::pair<double, double>>* target = &hullMods;
if (isWeaponStat) { target = &weaponMods; }
if (isSalvageStat) { target = &salvageMods; }
if (isRepairStat) { target = &repairMods; }
if (isCargoStat) { target = &cargoMods; }
std::pair<double, double>& acc = (*target)[sm.stat];
if (sm.modifierType == "multiplicative")
@@ -232,6 +241,14 @@ ShipStats calculateShipStats(const GameConfig& config,
result.salvage = ShipStats::SalvageStats{combinedRate, maxRange};
}
// Cargo capacity is a ship-level stat: apply [module.cargo] modifiers to the
// summed base (REQ-MOD-CARGO-CAPACITY).
{
float capacity = static_cast<float>(cargoCapacityBase);
applyMod(capacity, "cargo_capacity", cargoMods);
result.cargoCapacity = static_cast<int>(capacity + 0.5f);
}
// Apply repair modifiers and compute combined stats.
if (!repairInstances.empty())
{
@@ -287,8 +304,8 @@ ShipStats buildShipStatsFromEntity(const EntityAdmin& admin, entt::entity shipEn
if (w.range_tiles > weaponMaxRange) { weaponMaxRange = w.range_tiles; }
});
admin.forEach<ModuleOwnerComponent, SalvageCargoComponent>(
[&](entt::entity /*child*/, const ModuleOwnerComponent& owner, const SalvageCargoComponent& s)
admin.forEach<ModuleOwnerComponent, SalvagerComponent>(
[&](entt::entity /*child*/, const ModuleOwnerComponent& owner, const SalvagerComponent& s)
{
if (owner.owner != shipEntity) { return; }
hasSalvage = true;
@@ -299,6 +316,12 @@ ShipStats buildShipStatsFromEntity(const EntityAdmin& admin, entt::entity shipEn
if (s.collectionRange_tiles > salvageMaxRange) { salvageMaxRange = s.collectionRange_tiles; }
});
admin.forEach<CargoComponent>(
[&](entt::entity ship, const CargoComponent& c)
{
if (ship == shipEntity) { result.cargoCapacity = c.maxCapacity; }
});
admin.forEach<ModuleOwnerComponent, RepairToolComponent>(
[&](entt::entity /*child*/, const ModuleOwnerComponent& owner, const RepairToolComponent& r)
{

View File

@@ -24,6 +24,7 @@ struct ShipStats
float maneuveringAcceleration_tpss;
float angularAcceleration_radpss;
float maxRotationSpeed_radps;
int cargoCapacity = 0;
struct WeaponStats
{