#include "KillCounterUtil.h" #include "Entity.h" #include "EntityManager.h" #include "dZoneManager.h" #include "Spawner.h" #include "Logger.h" namespace { // Where the counter starts from unless the object sets min constexpr int32_t DefaultCounterMin = 0; bool IsFalse(Entity* self, const std::u16string& name) { return self->HasVar(name) && !self->GetVar(name); } } // The spawner of each spawnNw1, spawnNw2, ... the object names, up to the first it does not std::vector KillCounterUtil::SpawnNetworks(Entity* self) { std::vector spawners; for (int32_t count = 1; self->HasVar(GeneralUtils::ASCIIToUTF16("spawnNw" + std::to_string(count))); count++) { const auto name = self->GetVarAsString(GeneralUtils::ASCIIToUTF16("spawnNw" + std::to_string(count))); const auto found = Game::zoneManager->GetSpawnersByName(name); spawners.push_back(found.empty() ? nullptr : found.front()); } return spawners; } void KillCounterUtil::OnStartup(Entity* self) { const auto cntMin = self->HasVar(u"min") ? self->GetVarAs(u"min") : DefaultCounterMin; self->SetVar(u"min", cntMin); self->SetVar(u"counter", cntMin); const auto spawnNetworks = SpawnNetworks(self); if (!self->HasVar(u"max")) { int32_t cntMax = 1; if (!spawnNetworks.empty()) { int32_t cntTally = 0; for (auto* const spawner : spawnNetworks) { if (spawner && spawner->m_Info.maxToSpawn != -1) cntTally += spawner->m_Info.maxToSpawn; } if (cntTally == 0) LOG_DEBUG("Kill counter %llu has only infinite spawn networks", self->GetObjectID()); else cntMax = cntTally; } else { LOG_DEBUG("Kill counter %llu has no max and no spawn networks", self->GetObjectID()); } self->SetVar(u"max", cntMax); } else { self->SetVar(u"max", self->GetVarAs(u"max")); } if (IsFalse(self, u"bManageNotifies")) return; if (!spawnNetworks.empty()) SpawnerNotifies(self, true); else LOG_DEBUG("Kill counter %llu manages notifies without spawn networks", self->GetObjectID()); } // Listening to the deaths of what the networks spawn (once per network; stopping only mutes it) void KillCounterUtil::SpawnerNotifies(Entity* self, const bool subscribe) { self->SetVar(u"killCounterListening", subscribe); if (!subscribe || self->GetVar(u"killCounterSubscribed")) return; self->SetVar(u"killCounterSubscribed", true); const auto selfID = self->GetObjectID(); for (auto* const spawner : SpawnNetworks(self)) { if (!spawner) continue; spawner->AddSpawnedEntityDieCallback([this, selfID]() { auto* const counter = Game::entityManager->GetEntity(selfID); if (counter && counter->GetVar(u"killCounterListening")) ModifyCounter(counter, "increment"); }); } } void KillCounterUtil::OnFireEventServerSide(Entity* self, Entity* sender, std::string args, int32_t param1, int32_t param2, int32_t param3) { const auto name = GeneralUtils::ASCIIToUTF16(args); if (!self->HasVar(name)) return; const auto eventType = self->GetVarAsString(name); if (eventType == "increment" || eventType == "decrement") ModifyCounter(self, eventType); else if (eventType == "stopNotifies") SpawnerNotifies(self, false); else if (eventType == "startNotifies") SpawnerNotifies(self, true); else if (eventType == "reset") ResetCounter(self); } void KillCounterUtil::ResetCounter(Entity* self) { self->SetVar(u"counter", self->GetVar(u"min")); if (IsFalse(self, u"bManageNotifies")) { SpawnerNotifies(self, false); return; } if (!SpawnNetworks(self).empty()) SpawnerNotifies(self, true); else LOG_DEBUG("Kill counter %llu manages notifies without spawn networks", self->GetObjectID()); } void KillCounterUtil::ModifyCounter(Entity* self, const std::string& modType) { auto counter = self->GetVar(u"counter"); const auto cntMax = self->GetVar(u"max"); if (modType == "increment") { counter++; if (counter == cntMax) { SendEvent(self, true); ProcessEvent(self, true); if (IsFalse(self, u"bAutoShutoff")) return; SpawnerNotifies(self, false); } else if (counter > cntMax) { return; } self->SetVar(u"counter", counter); } else if (modType == "decrement") { counter--; const auto cntMin = self->GetVar(u"min"); if (counter == cntMin) { SendEvent(self, false); ProcessEvent(self, false); } else if (counter < cntMin) { return; } self->SetVar(u"counter", counter); } } // The limit's event (maxHit_event / minHit_event) to every object in its group (maxHit_group / minHit_group) void KillCounterUtil::SendEvent(Entity* self, const bool upperLimitReached) { const std::u16string eventVar = upperLimitReached ? u"maxHit_event" : u"minHit_event"; const std::u16string groupVar = upperLimitReached ? u"maxHit_group" : u"minHit_group"; if (!self->HasVar(eventVar) || !self->HasVar(groupVar)) return; const auto eventName = self->GetVarAsString(eventVar); const auto eventGroup = self->GetVarAsString(groupVar); for (auto* const obj : Game::entityManager->GetEntitiesInGroup(eventGroup)) { if (obj) obj->OnFireEventServerSide(self, eventName); } }