unify Weapon and StationWeapon components

This commit is contained in:
2026-05-22 22:06:30 +02:00
parent bd488db8ef
commit ea79d76953
7 changed files with 89 additions and 157 deletions

View File

@@ -114,7 +114,7 @@ void ArenaSimulation::placeStructures()
auto placeArenaStation = [&](const ArenaStationEntry& entry, bool isEnemy)
{
float hp = 0.0f;
StationWeapon weapon;
Weapon weapon;
weapon.cooldownTicks = 0.0f;
weapon.currentTarget = std::nullopt;
const double lv = static_cast<double>(entry.level);
@@ -155,7 +155,7 @@ void ArenaSimulation::placeStructures()
}
const entt::entity stationEntity = m_admin.spawnStation(
anchor, parsed.footprint, absCells, hp, hp, isEnemy);
m_admin.addComponent<StationWeapon>(stationEntity, weapon);
m_admin.addComponent<Weapon>(stationEntity, weapon);
m_buildingSystem->registerTileOccupancy(absCells, allocateId());
};
@@ -500,3 +500,4 @@ void ArenaSimulation::updateStatus()
std::lock_guard<std::mutex> lock(m_statusMutex);
m_status = newStatus;
}

View File

@@ -84,8 +84,6 @@ struct StationBody
std::vector<QPoint> bodyCells;
};
// StationWeapon remains defined in Building.h.
// ---------------------------------------------------------------------------
// Scrap components
// ---------------------------------------------------------------------------
@@ -105,3 +103,4 @@ struct DespawnAt
// ---------------------------------------------------------------------------
struct HqProxy { char unused = 0; };

View File

@@ -56,17 +56,6 @@ struct ConstructionSite
std::optional<ShipLayoutConfig> shipLayout;
};
// Weapon state for stationary structures (defence stations).
// Distinct from Ship::Weapon; stations have no movement intent.
struct StationWeapon
{
float damage;
float range;
float fireRateHz;
float cooldownTicks;
std::optional<entt::entity> currentTarget;
};
// A fully constructed, operational building.
struct Building
{
@@ -90,3 +79,4 @@ struct Building
// Module layout for shipyards (REQ-MOD-LAYOUT).
std::optional<ShipLayoutConfig> shipLayout;
};

View File

@@ -16,31 +16,30 @@ void CombatSystem::tick(Tick currentTick,
std::vector<FireEvent>& outFireEvents)
{
// Ship weapons.
admin.forEach<Weapon, ThreatResponse, Position>(
[&](entt::entity e, Weapon& weapon, ThreatResponse& threat, Position& pos)
admin.forEach<Weapon, ThreatResponse, Position, Faction>(
[&](entt::entity e, Weapon& weapon, ThreatResponse& threat, Position& pos, Faction& faction)
{
resolveShipWeapon(e, weapon, threat, pos, currentTick, admin, outFireEvents);
weapon.currentTarget = threat.currentTarget;
resolveWeapon(e, weapon, pos, faction, currentTick, admin, outFireEvents);
});
// Station weapons.
admin.forEach<StationWeapon, Position, Faction>(
[&](entt::entity e, StationWeapon& weapon, Position& pos, Faction& faction)
admin.forEach<Weapon, Position, Faction>(
[&](entt::entity e, Weapon& weapon, Position& pos, Faction& faction)
{
resolveStationWeapon(e, weapon, pos, faction, currentTick, admin, outFireEvents);
resolveWeapon(e, weapon, pos, faction, currentTick, admin, outFireEvents);
});
}
void CombatSystem::resolveShipWeapon(entt::entity shipEntity, Weapon& weapon,
const ThreatResponse& threat,
const Position& pos, Tick currentTick,
void CombatSystem::resolveWeapon(
entt::entity shipEntity,
Weapon& weapon,
const Position& ownPos,
const Faction& ownFaction,
Tick currentTick,
EntityAdmin& admin,
std::vector<FireEvent>& out)
{
if (!threat.currentTarget)
{
return;
}
if (weapon.cooldownTicks > 0.0f)
{
weapon.cooldownTicks -= 1.0f;
@@ -50,39 +49,6 @@ void CombatSystem::resolveShipWeapon(entt::entity shipEntity, Weapon& weapon,
return;
}
const entt::entity targetEntity = *threat.currentTarget;
if (!admin.isValid(targetEntity) || !admin.hasAll<Position>(targetEntity))
{
return;
}
const QVector2D targetPos = admin.get<Position>(targetEntity).value;
const float dist = (pos.value - targetPos).length();
if (dist > weapon.range)
{
return;
}
m_pendingDamage.push_back({targetEntity, weapon.damage,
currentTick + kWeaponImpactDelayTicks});
FireEvent evt;
evt.shooter = shipEntity;
evt.target = targetEntity;
evt.emittedAt = currentTick;
out.push_back(evt);
weapon.cooldownTicks = static_cast<float>(kTickRateHz) / weapon.fireRateHz;
}
void CombatSystem::resolveStationWeapon(entt::entity stationEntity,
StationWeapon& weapon,
const Position& stationPos,
const Faction& stationFaction,
Tick currentTick,
EntityAdmin& admin,
std::vector<FireEvent>& out)
{
// Validate or clear existing target.
if (weapon.currentTarget)
{
@@ -93,8 +59,8 @@ void CombatSystem::resolveStationWeapon(entt::entity stationEntity,
}
else
{
const float dist = (stationPos.value - admin.get<Position>(t).value).length();
if (dist > weapon.range)
const float distanceSquared = (ownPos.value - admin.get<Position>(t).value).lengthSquared();
if (distanceSquared > weapon.range * weapon.range)
{
weapon.currentTarget = std::nullopt;
}
@@ -104,22 +70,22 @@ void CombatSystem::resolveStationWeapon(entt::entity stationEntity,
// Acquire a new target if needed (nearest opposing-faction ship).
if (!weapon.currentTarget)
{
float bestDist = weapon.range;
float bestDistanceSquared = weapon.range * weapon.range;
admin.forEach<ShipIdentity, Position, Faction>(
[&](entt::entity candidate, const ShipIdentity& /*si*/,
const Position& candidatePos, const Faction& candidateFaction)
{
const bool isValidTarget = stationFaction.isEnemy
const bool isValidTarget = ownFaction.isEnemy
? !candidateFaction.isEnemy
: candidateFaction.isEnemy;
if (!isValidTarget)
{
return;
}
const float dist = (candidatePos.value - stationPos.value).length();
if (dist < bestDist)
const float distanceSquared = (candidatePos.value - ownPos.value).lengthSquared();
if (distanceSquared < bestDistanceSquared)
{
bestDist = dist;
bestDistanceSquared = distanceSquared;
weapon.currentTarget = candidate;
}
});
@@ -130,33 +96,12 @@ void CombatSystem::resolveStationWeapon(entt::entity stationEntity,
return;
}
if (weapon.cooldownTicks > 0.0f)
{
weapon.cooldownTicks -= 1.0f;
}
if (weapon.cooldownTicks > 0.0f)
{
return;
}
const entt::entity targetEntity = *weapon.currentTarget;
if (!admin.isValid(targetEntity) || !admin.hasAll<Position>(targetEntity))
{
weapon.currentTarget = std::nullopt;
return;
}
const QVector2D targetPos = admin.get<Position>(targetEntity).value;
if ((stationPos.value - targetPos).length() > weapon.range)
{
return;
}
m_pendingDamage.push_back({targetEntity, weapon.damage,
currentTick + kWeaponImpactDelayTicks});
FireEvent evt;
evt.shooter = stationEntity;
evt.shooter = shipEntity;
evt.target = targetEntity;
evt.emittedAt = currentTick;
out.push_back(evt);
@@ -183,3 +128,4 @@ void CombatSystem::applyPendingDamage(Tick currentTick, EntityAdmin& admin)
}
}
}

View File

@@ -39,19 +39,15 @@ private:
std::vector<PendingDamage> m_pendingDamage;
void resolveShipWeapon(entt::entity shipEntity, Weapon& weapon,
const ThreatResponse& threat,
const Position& pos, Tick currentTick,
EntityAdmin& admin,
std::vector<FireEvent>& out);
void resolveStationWeapon(entt::entity stationEntity,
StationWeapon& weapon,
const Position& stationPos,
const Faction& stationFaction,
void resolveWeapon(
entt::entity shipEntity,
Weapon& weapon,
const Position& ownPos,
const Faction& ownFaction,
Tick currentTick,
EntityAdmin& admin,
std::vector<FireEvent>& out);
const GameConfig& m_config;
};

View File

@@ -224,7 +224,7 @@ void Simulation::placeInitialStructures()
const float psHp = static_cast<float>(
m_config.stations.playerStation.hpFormula.evaluate(psLevel));
StationWeapon psWeapon;
Weapon psWeapon;
psWeapon.damage = static_cast<float>(
m_config.stations.playerStation.damageFormula.evaluate(psLevel));
psWeapon.range = static_cast<float>(
@@ -246,7 +246,7 @@ void Simulation::placeInitialStructures()
}
m_playerStation1Entity = m_admin.spawnStation(
anchor, psParsed.footprint, absCells, psHp, psHp, false);
m_admin.addComponent<StationWeapon>(m_playerStation1Entity, psWeapon);
m_admin.addComponent<Weapon>(m_playerStation1Entity, psWeapon);
m_buildingSystem->registerTileOccupancy(absCells, allocateId());
}
{
@@ -258,7 +258,7 @@ void Simulation::placeInitialStructures()
}
m_playerStation2Entity = m_admin.spawnStation(
anchor, psParsed.footprint, absCells, psHp, psHp, false);
m_admin.addComponent<StationWeapon>(m_playerStation2Entity, psWeapon);
m_admin.addComponent<Weapon>(m_playerStation2Entity, psWeapon);
m_buildingSystem->registerTileOccupancy(absCells, allocateId());
}
@@ -285,7 +285,7 @@ void Simulation::placeEnemyStationSet(int generation)
const float esHp = static_cast<float>(
m_config.stations.enemyStation.hpFormula.evaluate(genD));
StationWeapon esWeapon;
Weapon esWeapon;
esWeapon.damage = static_cast<float>(
m_config.stations.enemyStation.damageFormula.evaluate(genD));
esWeapon.range = static_cast<float>(
@@ -307,7 +307,7 @@ void Simulation::placeEnemyStationSet(int generation)
}
m_currentEnemyStationEntities[0] = m_admin.spawnStation(
anchor, esParsed.footprint, absCells, esHp, esHp, true);
m_admin.addComponent<StationWeapon>(m_currentEnemyStationEntities[0], esWeapon);
m_admin.addComponent<Weapon>(m_currentEnemyStationEntities[0], esWeapon);
m_buildingSystem->registerTileOccupancy(absCells, allocateId());
}
{
@@ -319,7 +319,7 @@ void Simulation::placeEnemyStationSet(int generation)
}
m_currentEnemyStationEntities[1] = m_admin.spawnStation(
anchor, esParsed.footprint, absCells, esHp, esHp, true);
m_admin.addComponent<StationWeapon>(m_currentEnemyStationEntities[1], esWeapon);
m_admin.addComponent<Weapon>(m_currentEnemyStationEntities[1], esWeapon);
m_buildingSystem->registerTileOccupancy(absCells, allocateId());
}
}

View File

@@ -165,9 +165,9 @@ TEST_CASE("WaveSystem: player stations have weapon set", "[wave]")
const Simulation sim(loadConfig(), 42);
int armedPlayerStations = 0;
sim.admin().forEach<StationBody, Faction, StationWeapon>(
sim.admin().forEach<StationBody, Faction, Weapon>(
[&](entt::entity /*e*/, const StationBody& /*sb*/, const Faction& f,
const StationWeapon& w)
const Weapon& w)
{
if (!f.isEnemy)
{
@@ -185,9 +185,9 @@ TEST_CASE("WaveSystem: enemy stations have weapon set", "[wave]")
const Simulation sim(loadConfig(), 42);
int armedEnemyStations = 0;
sim.admin().forEach<StationBody, Faction, StationWeapon>(
sim.admin().forEach<StationBody, Faction, Weapon>(
[&](entt::entity /*e*/, const StationBody& /*sb*/, const Faction& f,
const StationWeapon& w)
const Weapon& w)
{
if (f.isEnemy)
{