mirror of
https://github.com/DarkflameUniverse/DarkflameServer.git
synced 2026-11-30 01:41:40 +00:00
feat(scripts): L_WAVES_MANAGER_SERVER, and scripts can listen to another object's FireEvents
Entity::Subscribe takes "FireEvent" (the Lua's notification request on FireEvent): the subscriber's script hears every FireEvent the object is sent (NotifyFireEvent). The Fire Temple waves manager uses it to hear its spinners being sent SpawnedEnemiesDead, which the spinners send to their own group once their enemies are dead (level config name_deactivated_event / group_deactivated_event). LEVEL:GetCinematicInfo is the camera path's time. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -589,6 +589,8 @@ void Entity::Subscribe(LWOOBJID scriptObjId, CppScripts::Script* scriptToAdd, co
|
||||
} else if (notificationName == "PlayerResurrectionFinished") {
|
||||
LOG("Subscribing to PlayerResurrectionFinished");
|
||||
m_Subscriptions[scriptObjId][notificationName] = scriptToAdd;
|
||||
} else if (notificationName == "FireEvent") {
|
||||
m_Subscriptions[scriptObjId][notificationName] = scriptToAdd;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -600,6 +602,8 @@ void Entity::Unsubscribe(LWOOBJID scriptObjId, const std::string& notificationNa
|
||||
} else if (notificationName == "PlayerResurrectionFinished") {
|
||||
LOG("Unsubscribing from PlayerResurrectionFinished");
|
||||
m_Subscriptions[scriptObjId].erase(notificationName);
|
||||
} else if (notificationName == "FireEvent") {
|
||||
m_Subscriptions[scriptObjId].erase(notificationName);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -999,6 +1003,14 @@ void Entity::OnCollisionLeavePhantom(const LWOOBJID otherEntity) {
|
||||
|
||||
void Entity::OnFireEventServerSide(Entity* sender, std::string args, int32_t param1, int32_t param2, int32_t param3) {
|
||||
GetScript()->OnFireEventServerSide(this, sender, args, param1, param2, param3);
|
||||
// The scripts that asked to hear of this object's FireEvents (a Lua notification request); a copy, as one may cancel
|
||||
const auto subscriptions = m_Subscriptions;
|
||||
for (const auto& [subscriberID, scripts] : subscriptions) {
|
||||
const auto it = scripts.find("FireEvent");
|
||||
if (it == scripts.end()) continue;
|
||||
auto* const subscriber = Game::entityManager->GetEntity(subscriberID);
|
||||
if (subscriber) it->second->NotifyFireEvent(subscriber, this, args);
|
||||
}
|
||||
}
|
||||
|
||||
void Entity::OnActivityStateChangeRequest(LWOOBJID senderID, int32_t value1, int32_t value2, const std::u16string& stringValue) {
|
||||
|
||||
@@ -8,6 +8,7 @@ set(DSCRIPTS_SOURCES_02_SERVER_MAP_GENERAL
|
||||
"EnemyClearThreat.cpp"
|
||||
"EventCounterUtil.cpp"
|
||||
"ExplodingAsset.cpp"
|
||||
"FireTempleCinematics.cpp"
|
||||
"FrictionVolumeServer.cpp"
|
||||
"ForceVolumeServer.cpp"
|
||||
"GrowingFlower.cpp"
|
||||
@@ -28,6 +29,7 @@ set(DSCRIPTS_SOURCES_02_SERVER_MAP_GENERAL
|
||||
"TouchMissionUpdateServer.cpp"
|
||||
"VisToggleNotifierServer.cpp"
|
||||
"NTNaomiDirtServer.cpp"
|
||||
"WavesManagerServer.cpp"
|
||||
"WishingWellServer.cpp"
|
||||
"ZoneFireTempleServer.cpp")
|
||||
|
||||
|
||||
35
dScripts/02_server/Map/General/FireTempleCinematics.cpp
Normal file
35
dScripts/02_server/Map/General/FireTempleCinematics.cpp
Normal file
@@ -0,0 +1,35 @@
|
||||
#include "FireTempleCinematics.h"
|
||||
#include "Entity.h"
|
||||
#include "EntityManager.h"
|
||||
#include "EffectsMessages.h"
|
||||
#include "PhantomPhysicsComponent.h"
|
||||
#include "dpEntity.h"
|
||||
#include "dZoneManager.h"
|
||||
#include "Zone.h"
|
||||
|
||||
std::optional<float> FireTempleCinematics::GetCinematicInfo(const std::string& pathName) {
|
||||
const auto* const zone = Game::zoneManager->GetZone();
|
||||
const auto* const path = zone ? zone->GetPath(pathName) : nullptr;
|
||||
if (!path) return std::nullopt;
|
||||
float time = 0.0f;
|
||||
for (size_t i = 0; i + 1 < path->pathWaypoints.size(); i++) time += path->pathWaypoints[i].camera.time;
|
||||
return time;
|
||||
}
|
||||
|
||||
std::vector<Entity*> FireTempleCinematics::ObjectsInVolume(Entity* volume) {
|
||||
std::vector<Entity*> objects;
|
||||
auto* const phantom = volume ? volume->GetComponent<PhantomPhysicsComponent>() : nullptr;
|
||||
if (!phantom || !phantom->GetdpEntity()) return objects;
|
||||
for (const auto id : phantom->GetdpEntity()->GetCurrentlyCollidingObjects()) {
|
||||
if (auto* const object = Game::entityManager->GetEntity(id)) objects.push_back(object);
|
||||
}
|
||||
return objects;
|
||||
}
|
||||
|
||||
void FireTempleCinematics::PlayCinematic(Entity* player, const std::u16string& pathName) {
|
||||
if (!player || !player->IsPlayer()) return;
|
||||
GameMessages::PlayCinematic cinematic;
|
||||
cinematic.target = player->GetObjectID();
|
||||
cinematic.pathName = pathName;
|
||||
cinematic.Send(player->GetSystemAddress());
|
||||
}
|
||||
16
dScripts/02_server/Map/General/FireTempleCinematics.h
Normal file
16
dScripts/02_server/Map/General/FireTempleCinematics.h
Normal file
@@ -0,0 +1,16 @@
|
||||
#pragma once
|
||||
#include <string>
|
||||
#include <optional>
|
||||
|
||||
class Entity;
|
||||
|
||||
// Shared by the Fire Temple room managers (L_WAVES_MANAGER_SERVER, L_BOSS_MANAGER_SERVER)
|
||||
namespace FireTempleCinematics {
|
||||
// LEVEL:GetCinematicInfo(path): the camera path's time (its waypoints' times but the last one's), nothing without
|
||||
// the path
|
||||
std::optional<float> GetCinematicInfo(const std::string& pathName);
|
||||
// Every object in the phantom volumes of the group, the Lua's GetObjectsInPhysicsBounds
|
||||
std::vector<Entity*> ObjectsInVolume(Entity* volume);
|
||||
// PlayCinematic{ pathName, rerouteID = player } to a player
|
||||
void PlayCinematic(Entity* player, const std::u16string& pathName);
|
||||
}
|
||||
122
dScripts/02_server/Map/General/WavesManagerServer.cpp
Normal file
122
dScripts/02_server/Map/General/WavesManagerServer.cpp
Normal file
@@ -0,0 +1,122 @@
|
||||
#include "WavesManagerServer.h"
|
||||
#include "Entity.h"
|
||||
#include "EntityManager.h"
|
||||
#include "BuffComponent.h"
|
||||
#include "dZoneManager.h"
|
||||
#include "Spawner.h"
|
||||
#include "FireTempleCinematics.h"
|
||||
|
||||
namespace {
|
||||
constexpr float CamDelay = 1.0f;
|
||||
const std::string EnemySpawnerPre = "Waves_";
|
||||
const std::string EnemySpawnerPost = "_Spawner";
|
||||
const std::u16string DoorCam = u"WavesDoorCam";
|
||||
const std::u16string SpawningCam = u"WavesSpawnerSpinners";
|
||||
const std::string RoomVolume = "WavesRoomVolume";
|
||||
const std::string DoorIndicators = "WavesDoorIndicator";
|
||||
const std::string Door = "WavesDoor";
|
||||
const std::string ElevatorSpinner = "WavesElevatorSpinner";
|
||||
const std::string SpawnerSpinners = "WavesSpinner";
|
||||
|
||||
// The first object of the group, sent the event
|
||||
void FireFirst(Entity* self, const std::string& group, const std::string& event) {
|
||||
const auto objects = Game::entityManager->GetEntitiesInGroup(group);
|
||||
for (auto* const object : objects) {
|
||||
if (!object) continue;
|
||||
object->OnFireEventServerSide(self, event);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const std::vector<std::vector<WavesManagerServer::Enemy>>& WavesManagerServer::Waves() {
|
||||
static const std::vector<std::vector<Enemy>> waves = {
|
||||
{ { "Blacksmith", 3, 2 }, { "Hand", 2, 1 }, { "Marksman", 2, 1 } },
|
||||
{ { "Hand", 2, 2 }, { "Scientist", 4, 2 }, { "Wolf", 2, 1 }, { "Beetle", 2, 1 } },
|
||||
{ { "Blacksmith", 2, 1 }, { "Scientist", 2, 1 }, { "Marksman", 2, 1 }, { "Overseer", 2, 1 }, { "Wolf", 2, 1 },
|
||||
{ "Beetle", 1, 1 }, { "Hand", 3, 1 }, { "Bonezai", 1, 1 } },
|
||||
};
|
||||
return waves;
|
||||
}
|
||||
|
||||
void WavesManagerServer::OnCollisionPhantom(Entity* self, Entity* target) {
|
||||
if (self->GetVar<bool>(u"bStarted")) return;
|
||||
self->SetVar<bool>(u"bStarted", true);
|
||||
auto* const zoneControl = Game::entityManager->GetZoneControlEntity();
|
||||
if (zoneControl && zoneControl->GetVar<int32_t>(u"initialPlayerCount") > 2) self->SetVar<bool>(u"LargeTeam", true);
|
||||
ActivateWaveSpinners(self, 1);
|
||||
}
|
||||
|
||||
void WavesManagerServer::NotifyFireEvent(Entity* self, Entity* other, const std::string& args) {
|
||||
if (args != "SpawnedEnemiesDead") return;
|
||||
other->Unsubscribe(self->GetObjectID(), "FireEvent");
|
||||
if (!self->HasVar(u"SpinnersActive")) return;
|
||||
auto activeSpinners = self->GetVar<int32_t>(u"SpinnersActive") - 1;
|
||||
if (activeSpinners < 0) {
|
||||
LOG("Warning!! too many spinners deactivated");
|
||||
return;
|
||||
}
|
||||
self->SetVar<int32_t>(u"SpinnersActive", activeSpinners);
|
||||
if (activeSpinners > 0) return;
|
||||
self->CancelTimer("ShowDoor");
|
||||
self->AddTimer("ShowDoor", CamDelay);
|
||||
}
|
||||
|
||||
void WavesManagerServer::ActivateWaveSpinners(Entity* self, const int32_t waveNum) {
|
||||
if (waveNum > static_cast<int32_t>(Waves().size())) return;
|
||||
PlayCamera(self, SpawningCam);
|
||||
self->SetVar<int32_t>(u"CurrentWave", waveNum);
|
||||
for (const auto& enemy : Waves()[waveNum - 1]) {
|
||||
const auto spawners = Game::zoneManager->GetSpawnersByName(EnemySpawnerPre + enemy.name + EnemySpawnerPost);
|
||||
if (spawners.empty() || !spawners.front()) continue;
|
||||
auto* const spawner = spawners.front();
|
||||
const auto spawnNum = self->GetVar<bool>(u"LargeTeam") ? enemy.largeNum : enemy.smallNum;
|
||||
if (spawnNum <= 0) continue;
|
||||
spawner->m_Info.maxToSpawn = spawnNum;
|
||||
spawner->SetNumToMaintain(spawnNum);
|
||||
auto spinnersUp = self->GetVar<int32_t>(u"SpinnersActive");
|
||||
for (auto* const spinner : Game::entityManager->GetEntitiesInGroup(SpawnerSpinners + enemy.name)) {
|
||||
if (!spinner) continue;
|
||||
spinner->OnFireEventServerSide(self, "SpawnEnemies");
|
||||
spinner->Subscribe(self->GetObjectID(), this, "FireEvent");
|
||||
spinnersUp++;
|
||||
}
|
||||
self->SetVar<int32_t>(u"SpinnersActive", spinnersUp);
|
||||
}
|
||||
}
|
||||
|
||||
void WavesManagerServer::PlayCamera(Entity* self, const std::u16string& cine) {
|
||||
const auto roomTrigger = Game::entityManager->GetEntitiesInGroup(RoomVolume);
|
||||
for (auto* const trigger : roomTrigger) {
|
||||
if (!trigger) continue;
|
||||
const auto players = FireTempleCinematics::ObjectsInVolume(trigger);
|
||||
if (players.empty()) return;
|
||||
for (auto* const player : players) {
|
||||
if (auto* const buff = player->GetComponent<BuffComponent>()) buff->RemoveBuff(60);
|
||||
FireTempleCinematics::PlayCinematic(player, cine);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void WavesManagerServer::OnTimerDone(Entity* self, std::string timerName) {
|
||||
if (timerName == "ShowDoor") {
|
||||
PlayCamera(self, DoorCam);
|
||||
self->CancelTimer("TurnOnIndicator");
|
||||
self->AddTimer("TurnOnIndicator", 1.0f);
|
||||
const auto cineTime = FireTempleCinematics::GetCinematicInfo(GeneralUtils::UTF16ToWTF8(DoorCam)).value_or(3.0f);
|
||||
self->CancelTimer("SpawnNextWave");
|
||||
self->AddTimer("SpawnNextWave", cineTime);
|
||||
} else if (timerName == "TurnOnIndicator") {
|
||||
const auto currentWave = self->GetVar<int32_t>(u"CurrentWave");
|
||||
const auto indicators = Game::entityManager->GetEntitiesInGroup(DoorIndicators + std::to_string(currentWave));
|
||||
if (indicators.empty()) return;
|
||||
FireFirst(self, DoorIndicators + std::to_string(currentWave), "IndicatorOn");
|
||||
if (currentWave == static_cast<int32_t>(Waves().size())) {
|
||||
if (Game::entityManager->GetEntitiesInGroup(Door).empty()) return;
|
||||
FireFirst(self, Door, "IndicatorOn");
|
||||
FireFirst(self, ElevatorSpinner, "TurnOn");
|
||||
}
|
||||
} else if (timerName == "SpawnNextWave") {
|
||||
ActivateWaveSpinners(self, self->GetVar<int32_t>(u"CurrentWave") + 1);
|
||||
}
|
||||
}
|
||||
25
dScripts/02_server/Map/General/WavesManagerServer.h
Normal file
25
dScripts/02_server/Map/General/WavesManagerServer.h
Normal file
@@ -0,0 +1,25 @@
|
||||
#pragma once
|
||||
#include "CppScripts.h"
|
||||
|
||||
// L_WAVES_MANAGER_SERVER.lua: the Fire Temple waves room. The first touch starts wave 1: each enemy kind's spawner
|
||||
// network (Waves_<name>_Spawner) gets its count for the team size, the spinners of that kind (group WavesSpinner<name>)
|
||||
// are sent SpawnEnemies, and the manager listens for each to be sent SpawnedEnemiesDead (the spinners send it to their
|
||||
// own group once their enemies are dead). When the last is, the players in the room see the door camera, the wave's
|
||||
// door indicator lights, and the next wave starts after the camera; after the last wave the door and the elevator
|
||||
// spinner are turned on.
|
||||
class WavesManagerServer : public CppScripts::Script {
|
||||
public:
|
||||
struct Enemy {
|
||||
std::string name;
|
||||
int32_t largeNum;
|
||||
int32_t smallNum;
|
||||
};
|
||||
static const std::vector<std::vector<Enemy>>& Waves();
|
||||
|
||||
void OnCollisionPhantom(Entity* self, Entity* target) override;
|
||||
void NotifyFireEvent(Entity* self, Entity* other, const std::string& args) override;
|
||||
void OnTimerDone(Entity* self, std::string timerName) override;
|
||||
private:
|
||||
void ActivateWaveSpinners(Entity* self, int32_t waveNum);
|
||||
static void PlayCamera(Entity* self, const std::u16string& cine);
|
||||
};
|
||||
@@ -283,6 +283,7 @@
|
||||
#include "ZoneFireTempleServer.h"
|
||||
#include "EventCounterUtil.h"
|
||||
#include "BossGarmadonTornado.h"
|
||||
#include "WavesManagerServer.h"
|
||||
#include "SimpleOnceOnlyMoverSpinnerEvent.h"
|
||||
#include "ImaginationShrineServer.h"
|
||||
#include "Lieutenant.h"
|
||||
@@ -703,6 +704,7 @@ namespace {
|
||||
{"scripts\\02_server\\Map\\firetemple\\L_DOOR_INDICATOR_SERVER.lua", []() {return new DoorIndicatorServer();}},
|
||||
{"scripts\\02_server\\Map\\firetemple\\L_ELEMENTAL_CHESTS_SERVER.lua", []() {return new ElementalChestsServer();}},
|
||||
{"scripts\\02_server\\Map\\firetemple\\L_ZONE_FIRE_TEMPLE_SERVER.lua", []() {return new ZoneFireTempleServer();}},
|
||||
{"scripts\\02_server\\Map\\firetemple\\L_WAVES_MANAGER_SERVER.lua", []() {return new WavesManagerServer();}},
|
||||
// (the CDServer row writes this path with forward slashes)
|
||||
{"scripts/02_server/Map/firetemple/L_BOSS_GARMADON_TORNADO.lua", []() {return new BossGarmadonTornado();}},
|
||||
{"scripts\\02_server\\Map\\General\\L_KILL_COUNTER_UTIL.lua", []() {return new KillCounterUtil();}},
|
||||
|
||||
@@ -192,6 +192,13 @@ namespace CppScripts {
|
||||
|
||||
virtual void NotifyPlayerResurrectionFinished(Entity& self, GameMessages::PlayerResurrectionFinished& msg) {};
|
||||
|
||||
/**
|
||||
* Invoked when an object self subscribed to ("FireEvent") was sent a FireEvent.
|
||||
*
|
||||
* Equivalent to 'function notifyFireEvent(self, other, msg)'
|
||||
*/
|
||||
virtual void NotifyFireEvent(Entity* self, Entity* other, const std::string& args) {};
|
||||
|
||||
/**
|
||||
* Invoked when a player has responsed to a mission.
|
||||
*
|
||||
|
||||
@@ -12,6 +12,8 @@
|
||||
#include "EffectsMessages.h"
|
||||
#include "02_server/Map/General/EventCounterUtil.h"
|
||||
#include "02_server/Map/General/BossGarmadonTornado.h"
|
||||
#include "02_server/Map/General/WavesManagerServer.h"
|
||||
#include "EntityManager.h"
|
||||
|
||||
namespace {
|
||||
// A kill counter extension that records its limit events
|
||||
@@ -163,3 +165,41 @@ TEST_F(FireTempleScriptsTest, TornadoBombsSoonerForLaterElements) {
|
||||
tornado.OnNotifyObject(&entity, &entity, "NewElement", 0);
|
||||
EXPECT_EQ(entity.GetVar<std::string>(u"CurrentElement"), "lightning");
|
||||
}
|
||||
|
||||
// L_WAVES_MANAGER_SERVER: it hears its spinners being sent SpawnedEnemiesDead (a FireEvent subscription), stops
|
||||
// listening to each, and when the last is done waits a moment and shows the door
|
||||
TEST_F(FireTempleScriptsTest, WavesManagerHearsItsSpinnersFinish) {
|
||||
// Creating an entity looks its LOT up in the CDClient
|
||||
CDClientDatabase::Connect(":memory:");
|
||||
CDClientDatabase::ExecuteDML("CREATE TABLE IF NOT EXISTS ComponentsRegistry (id INTEGER, component_type INTEGER, component_id INTEGER);");
|
||||
CDClientDatabase::ExecuteDML("CREATE TABLE IF NOT EXISTS Objects (id INTEGER, name TEXT, type TEXT, interactionDistance REAL);");
|
||||
auto* const manager = Game::entityManager->CreateEntity(info, nullptr, nullptr, false, 288300744895900210);
|
||||
auto* const first = Game::entityManager->CreateEntity(info, nullptr, nullptr, false, 288300744895900211);
|
||||
auto* const second = Game::entityManager->CreateEntity(info, nullptr, nullptr, false, 288300744895900212);
|
||||
WavesManagerServer waves;
|
||||
manager->SetVar<int32_t>(u"SpinnersActive", 2);
|
||||
first->Subscribe(manager->GetObjectID(), &waves, "FireEvent");
|
||||
second->Subscribe(manager->GetObjectID(), &waves, "FireEvent");
|
||||
|
||||
first->OnFireEventServerSide(first, "SomethingElse");
|
||||
EXPECT_EQ(manager->GetVar<int32_t>(u"SpinnersActive"), 2);
|
||||
first->OnFireEventServerSide(first, "SpawnedEnemiesDead");
|
||||
EXPECT_EQ(manager->GetVar<int32_t>(u"SpinnersActive"), 1);
|
||||
// No longer listening to the first
|
||||
first->OnFireEventServerSide(first, "SpawnedEnemiesDead");
|
||||
EXPECT_EQ(manager->GetVar<int32_t>(u"SpinnersActive"), 1);
|
||||
manager->Update(0.0f);
|
||||
EXPECT_FALSE(manager->HasTimer("ShowDoor"));
|
||||
|
||||
second->OnFireEventServerSide(second, "SpawnedEnemiesDead");
|
||||
EXPECT_EQ(manager->GetVar<int32_t>(u"SpinnersActive"), 0);
|
||||
manager->Update(0.0f);
|
||||
EXPECT_TRUE(manager->HasTimer("ShowDoor"));
|
||||
}
|
||||
|
||||
// The waves as the Lua has them: three, the last with eight kinds
|
||||
TEST_F(FireTempleScriptsTest, WavesManagerWaves) {
|
||||
ASSERT_EQ(WavesManagerServer::Waves().size(), 3u);
|
||||
EXPECT_EQ(WavesManagerServer::Waves()[2].size(), 8u);
|
||||
EXPECT_EQ(WavesManagerServer::Waves()[0][0].largeNum, 3);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user