Files
DarkflameServer/dScripts/02_server/Map/General/KillCounterUtil.cpp
Aaron Kimbrell 6ad3168659 feat(scripts): L_KILL_COUNTER_UTIL and the Fire Temple spinner volumes
The kill counter counts its spawn networks' deaths (or the events its config names) to max and back to min and fires
the limit events at their groups. L_BSPINNERS_VOL and L_DSPINNERS_VOL extend it: at max each player in the volume sees
the room's cinematic and 2 s later the rail quick build's network is activated.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-28 11:32:10 -06:00

129 lines
5.0 KiB
C++

#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<bool>(name);
}
}
// The spawner of each spawnNw1, spawnNw2, ... the object names, up to the first it does not
std::vector<Spawner*> KillCounterUtil::SpawnNetworks(Entity* self) {
std::vector<Spawner*> 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<int32_t>(u"min") : DefaultCounterMin;
self->SetVar<int32_t>(u"min", cntMin);
self->SetVar<int32_t>(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<int32_t>(u"max", cntMax);
} else {
self->SetVar<int32_t>(u"max", self->GetVarAs<int32_t>(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<bool>(u"killCounterListening", subscribe);
if (!subscribe || self->GetVar<bool>(u"killCounterSubscribed")) return;
self->SetVar<bool>(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<bool>(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<int32_t>(u"counter", self->GetVar<int32_t>(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<int32_t>(u"counter");
const auto cntMax = self->GetVar<int32_t>(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<int32_t>(u"counter", counter);
} else if (modType == "decrement") {
counter--;
const auto cntMin = self->GetVar<int32_t>(u"min");
if (counter == cntMin) {
SendEvent(self, false);
ProcessEvent(self, false);
} else if (counter < cntMin) {
return;
}
self->SetVar<int32_t>(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);
}
}