DarkflameServer/dScripts/BaseRandomServer.cpp

173 lines
4.2 KiB
C++
Raw Permalink Normal View History

#include "BaseRandomServer.h"
#include "dZoneManager.h"
#include "Spawner.h"
#include "dLogger.h"
#include "Entity.h"
2022-07-28 13:39:57 +00:00
void BaseRandomServer::BaseStartup(Entity* self) {
self->SetVar<std::string>(u"SpawnState", "min");
self->SetVar<bool>(u"JustChanged", false);
2022-07-28 13:39:57 +00:00
CheckEvents(self);
SpawnMapZones(self);
}
2022-07-28 13:39:57 +00:00
void BaseRandomServer::CheckEvents(Entity* self) {
// TODO: Add events?
}
2022-07-28 13:39:57 +00:00
void BaseRandomServer::SpawnMapZones(Entity* self) {
for (const auto& pair : sectionMultipliers) {
const auto sectionName = zonePrefix + "_" + zoneName + "_" + pair.first;
2022-07-28 13:39:57 +00:00
SpawnSection(self, sectionName, pair.second);
}
2022-07-28 13:39:57 +00:00
if (zoneName == "str") {
SpawnNamedEnemy(self);
}
2022-07-28 13:39:57 +00:00
self->SetVar(u"bInit", true);
}
2022-07-28 13:39:57 +00:00
void BaseRandomServer::SpawnSection(Entity* self, const std::string& sectionName, float iMultiplier) {
Zone* spawnLoad = GetRandomLoad(self, sectionName);
2022-07-28 13:39:57 +00:00
if (spawnLoad == nullptr) {
Game::logger->Log("BaseRandomServer", "Failed to find section: %s", sectionName.c_str());
2022-07-28 13:39:57 +00:00
return;
}
2022-07-28 13:39:57 +00:00
for (const auto& spawnerData : spawnLoad->entries) {
if (spawnerData.name.empty()) {
continue;
}
2022-07-28 13:39:57 +00:00
const auto spawnNum = std::floor(spawnerData.num * iMultiplier);
const auto spawnerName = sectionName + "_" + spawnerData.name;
2022-07-28 13:39:57 +00:00
SetSpawnerNetwork(self, spawnerName, spawnNum, spawnerData.lot);
}
}
2022-07-28 13:39:57 +00:00
void BaseRandomServer::SetSpawnerNetwork(Entity* self, const std::string& spawnerName, int32_t spawnNum, LOT spawnLOT) {
const auto& spawners = Game::zoneManager->GetSpawnersByName(spawnerName);
2022-07-28 13:39:57 +00:00
if (spawnLOT == 11217 && spawnNum > 1) {
spawnNum = 1;
}
2022-07-28 13:39:57 +00:00
if (spawners.empty()) {
Game::logger->Log("BaseRandomServer", "Failed to find spawner: %s", spawnerName.c_str());
2022-07-28 13:39:57 +00:00
return;
}
2022-07-28 13:39:57 +00:00
auto* spawner = spawners[0];
2022-07-28 13:39:57 +00:00
if (spawnLOT != 0) {
spawner->SetSpawnLot(spawnLOT);
spawner->SetRespawnTime(respawnTime);
}
2022-07-28 13:39:57 +00:00
if (spawnNum != 0) {
spawner->SetNumToMaintain(spawnNum);
}
2022-07-28 13:39:57 +00:00
if (spawnerName == "Named_Enemies") {
spawner->SoftReset();
}
2022-07-28 13:39:57 +00:00
spawner->Activate();
2022-07-28 13:39:57 +00:00
if (std::find(spawnersWatched.begin(), spawnersWatched.end(), spawner) != spawnersWatched.end()) {
return;
}
2022-07-28 13:39:57 +00:00
spawner->AddSpawnedEntityDieCallback([this, self, spawner]() {
NotifySpawnerOfDeath(self, spawner);
});
2022-07-28 13:39:57 +00:00
spawnersWatched.push_back(spawner);
}
2022-07-28 13:39:57 +00:00
BaseRandomServer::Zone* BaseRandomServer::GetRandomLoad(Entity* self, const std::string& sectionName) {
const auto zoneInfo = GeneralUtils::SplitString(sectionName, '_');
2022-07-28 13:39:57 +00:00
int32_t totalWeight = 0;
2022-07-28 13:39:57 +00:00
for (const auto& load : zones) {
totalWeight += load.iChance;
}
2022-07-28 13:39:57 +00:00
const auto randWeight = GeneralUtils::GenerateRandomNumber<int32_t>(0, totalWeight);
2022-07-28 13:39:57 +00:00
int32_t weight = 0;
for (auto& zone : zones) {
weight += zone.iChance;
2022-07-28 13:39:57 +00:00
if (randWeight <= weight) {
return &zone;
}
}
2022-07-28 13:39:57 +00:00
return nullptr;
}
2022-07-28 13:39:57 +00:00
void BaseRandomServer::NotifySpawnerOfDeath(Entity* self, Spawner* spawner) {
const auto& spawnerName = spawner->GetName();
2022-07-28 13:39:57 +00:00
if (spawnerName == "Named_Enemies") {
NamedEnemyDeath(self, spawner);
2022-07-28 13:39:57 +00:00
return;
}
2022-07-28 13:39:57 +00:00
const auto& sectionName = spawnerName.substr(0, spawnerName.size() - 7);
2022-07-28 13:39:57 +00:00
const auto variableName = u"mobsDead" + GeneralUtils::ASCIIToUTF16(sectionName);
2022-07-28 13:39:57 +00:00
auto mobDeathCount = self->GetVar<int32_t>(variableName);
2022-07-28 13:39:57 +00:00
mobDeathCount++;
2022-07-28 13:39:57 +00:00
if (mobDeathCount >= mobDeathResetNumber) {
const auto& zoneInfo = GeneralUtils::SplitString(sectionName, '_');
2022-07-28 13:39:57 +00:00
SpawnSection(self, sectionName, sectionMultipliers[zoneInfo[sectionIDConst - 1]]);
}
2022-07-28 13:39:57 +00:00
self->SetVar(variableName, mobDeathCount);
}
2022-07-28 13:39:57 +00:00
void BaseRandomServer::NamedEnemyDeath(Entity* self, Spawner* spawner) {
const auto spawnDelay = GeneralUtils::GenerateRandomNumber<float>(1, 2) * 450;
2022-07-28 13:39:57 +00:00
self->AddTimer("SpawnNewEnemy", spawnDelay);
}
2022-07-28 13:39:57 +00:00
void BaseRandomServer::SpawnersUp(Entity* self) {
}
2022-07-28 13:39:57 +00:00
void BaseRandomServer::SpawnersDown(Entity* self) {
}
2022-07-28 13:39:57 +00:00
void BaseRandomServer::BaseOnTimerDone(Entity* self, const std::string& timerName) {
NamedTimerDone(self, timerName);
}
2022-07-28 13:39:57 +00:00
void BaseRandomServer::SpawnNamedEnemy(Entity* self) {
const auto enemy = namedMobs[GeneralUtils::GenerateRandomNumber<int32_t>(0, namedMobs.size() - 1)];
2022-07-28 13:39:57 +00:00
SetSpawnerNetwork(self, "Named_Enemies", 1, enemy);
}
2022-07-28 13:39:57 +00:00
void BaseRandomServer::NamedTimerDone(Entity* self, const std::string& timerName) {
if (timerName == "SpawnNewEnemy") {
SpawnNamedEnemy(self);
}
}