载入中...
搜索中...
未找到
RTSCasualtyCleanup.cpp
浏览该文件的文档.
1#include <algorithm>
2#include <new>
3#include <set>
4#include <tuple>
5#include "common/Profile.h"
6#include "crowd/Crowd.h"
7#include "rts/RTS.h"
8
9namespace eve::rts {
10Result<void> RTS::prepareUnitRemovalBatch(std::span<const ecs::EntityHandle> units) {
11 EV_PROFILE_MODULE("rts", "RTS::prepareUnitRemovalBatch");
12 // Scope: this facade's owned Unit handles; no ECS View is active.
13 // Read Identity/Containment/Crowd/Weapon; write only the same typed references as removeUnitRoot.
14 // Crowd removal is prepared before reference publication; structural release follows this method.
15 try {
16 using Key = std::tuple<ecs::Table*, std::type_index, std::uint32_t, std::uint32_t>;
17 const auto key = [](ecs::EntityHandle h) { return Key{h.table, h.type, h.id, h.generation}; };
18 std::set<Key> dead, weapons;
19 crowd::AgentRemovalBatch agents;
20 agents.agents.reserve(units.size());
21 for (auto handle : units) {
22 auto* unit = dynamic_cast<Unit*>(ecs::try_get(handle));
23 if (!unit || !unit->containment()->occupants.empty())
26 "Casualty batch requires current units with no occupants", "cleanup.units"));
27 dead.insert(key(handle));
28 weapons.insert(key(unit->weapon()->link.handle()));
29 if (crowd_ && crowdLifetime_.isAlive() && unit->crowd()->link.isBound()) {
30 const auto settings = unit->crowd();
31 agents.agents.push_back({settings->runtimeAgent, settings->runtimeOwner});
32 }
33 }
34 if (!agents.agents.empty()) {
35 auto removed = crowd_->removeAgents(agents);
36 if (!removed) return Result<void>::failure(removed.status());
37 }
38 const auto removed = [&](ecs::EntityHandle h) { return dead.contains(key(h)); };
39 for (auto h : resourceNodes_)
40 if (auto* node = dynamic_cast<ResourceNode*>(ecs::try_get(h)))
41 std::erase_if(node->harvest()->workers, removed);
42 for (auto h : buildings_)
43 if (auto* building = dynamic_cast<Building*>(ecs::try_get(h))) {
44 std::erase_if(building->construction()->builders, removed);
45 std::erase_if(building->garrison()->occupants, removed);
46 std::erase_if(building->rally()->reinforcements, removed);
47 if (removed(building->rally()->transport)) {
48 building->rally()->transport = {};
49 building->rally()->transportActive = false;
50 }
51 building->capture()->blockedByGarrison = !building->garrison()->occupants.empty();
52 }
53 for (auto h : units_)
54 if (auto* unit = dynamic_cast<Unit*>(ecs::try_get(h))) {
55 std::erase_if(unit->containment()->occupants, removed);
56 if (unit->containment()->container.isBound() && removed(unit->containment()->container.handle()))
57 unit->containment()->container = {};
58 if (removed(unit->combat()->target)) unit->combat()->target = {};
59 if (removed(unit->tactics()->escortTarget)) unit->tactics()->escortTarget = {};
60 if (removed(unit->supply()->assignedTarget)) unit->supply()->assignedTarget = {};
61 if (removed(unit->supply()->convoyLeader)) unit->supply()->convoyLeader = {};
62 }
63 for (auto h : factions_)
64 if (auto* faction = dynamic_cast<Faction*>(ecs::try_get(h)))
65 std::erase_if(faction->members()->units, removed);
66 for (auto h : players_)
67 if (auto* player = dynamic_cast<Player*>(ecs::try_get(h)))
68 std::erase_if(player->selection()->units, removed);
69 std::erase_if(weapons_, [&](auto h) { return weapons.contains(key(h)); });
70 std::erase_if(units_, removed);
72 } catch (const std::bad_alloc&) {
74 Diagnostic::error(DiagnosticCode::Failed, "Cannot prepare casualty cleanup storage", "cleanup.units"));
75 }
76}
77} // namespace eve::rts
SQObjectPtr key
Definition ECS.cpp:155
int h
OceanWakeSettings settings
PrimitiveHandle handle
#define EV_PROFILE_MODULE(module, name)
Profile the enclosing scope, tagged with a module for grouping.
Definition Profile.h:158
RTS module owner and phase-one composition profile entry point.
const RoadNode * node
int removed
TacticalUnit * unit
std::vector< UnitCandidate > units
std::map< std::string, std::span< const std::uint8_t > > members
static Diagnostic error(DiagnosticCode code, std::string message, std::string path={})
Construct an error diagnostic with the standard error severity.
bool isAlive() const noexcept
Query original owner liveness; owner-thread only.
static Result success(T value)
Construct a successful result owning value.
Definition Result.h:185
static Result failure(Status status)
Construct a failed result from a structured status.
Definition Result.h:196
static Status success(StatusCode code=StatusCode::Ok)
Construct a successful status with an explicit non-error outcome.
Definition Status.cpp:19
Result< AgentBatchReceipt > removeAgents(const AgentRemovalBatch &batch)
Atomically remove live references; every reference is checked before any removal. @ownership Borrows ...