Files
DarkflameServer/dScripts/ai/WILD/WildMedusa.cpp
Aaron Kimbrell cf940f13f9 feat(scripts): Medusa and snake parts run the shared Lua AI
The Medusa (LOT 6602) wanders 25 round its spawn, aggroes within 15,
chases to 8 at double speed, casts its skill every cooldown and tethers
home at 50; the parts are Passive guards. Their Set tables replace the
values put on the combat and movement AI before, which they have no rows
for. The parts' onTimerDone and onDie replace the shared ones as in the
original.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-27 22:50:50 -06:00

101 lines
4.1 KiB
C++

#include "WildMedusa.h"
#include "WildMedusaData.h"
#include "Entity.h"
#include "EntityInfo.h"
#include "EntityManager.h"
#include "GameMessages.h"
#include "EffectsMessages.h"
#include "MovementAIComponent.h"
namespace {
// SetFaction { faction = 4 } and PlayFXEffect { effectType = effect }
void TurnHostile(Entity* self, const std::u16string& effect) {
GameMessages::SetFaction setFaction{};
setFaction.target = self->GetObjectID();
setFaction.factionID = MedusaSnakes::HOSTILE_FACTION;
setFaction.Send();
if (!effect.empty()) GameMessages::PlayFXEffect(self->GetObjectID(), -1, effect, "").Send(UNASSIGNED_SYSTEM_ADDRESS);
}
};
// L_WILD_MEDUSA.lua onStartup
void WildMedusa::OnStartup(Entity* self) {
// The original adds a SnakeStartup timer here with nothing to handle it
self->AddTimer("SnakeStartup", MedusaSnakes::STARTUP_TIME);
self->AddToGroup(MedusaSnakes::GROUP);
// Its OverrideFriction is commented out in the original
// Spawn snake pieces: RESMGR:LoadObject { owner = self } at the offsets
const auto position = self->GetPosition();
for (const auto& part : MedusaSnakes::PARTS) {
EntityInfo info{};
info.lot = part.lot;
info.pos = position + part.offset;
info.rot = QuatUtils::IDENTITY;
info.spawnerID = self->GetObjectID();
auto* const spawned = Game::entityManager->CreateEntity(info);
if (spawned) Game::entityManager->ConstructEntity(spawned);
}
// loadOnce, getVarables, CreateStates, oStart
StartLuaAI(self, MedusaSnakes::MedusaAi());
}
// L_WILD_MEDUSA.lua onNotifyObject
void WildMedusa::OnNotifyObject(Entity* self, Entity* sender, const std::string& name, int32_t param1, int32_t param2) {
if (name == MedusaSnakes::MEDUSA_NOTIFY) TurnHostile(self, MedusaSnakes::MEDUSA_EFFECT);
}
// L_WILD_snake_0N.lua onStartup
void WildSnake::OnStartup(Entity* self) {
self->AddTimer("SnakeStartup", MedusaSnakes::STARTUP_TIME);
self->AddTimer("SnakeFollow", MedusaSnakes::FOLLOW_TIME);
const auto* const part = MedusaSnakes::GetPart(self->GetLOT());
if (part && part->turnsHostileOn[0] == '\0') self->AddTimer("SnakeGlow", MedusaSnakes::GLOW_TIME);
// loadOnce, getVarables, CreateStates, oStart
StartLuaAI(self, MedusaSnakes::SnakeAi());
}
// L_WILD_snake_0N.lua onDie
void WildSnake::OnLuaDie(Entity* self, Entity* killer) {
const auto* const part = MedusaSnakes::GetPart(self->GetLOT());
if (!part) return;
for (auto* const snake : Game::entityManager->GetEntitiesInGroup(MedusaSnakes::GROUP)) {
snake->NotifyObject(self, part->notifyOnDeath);
}
}
// L_WILD_snake_01..04.lua onNotifyObject (part 5 has none)
void WildSnake::OnNotifyObject(Entity* self, Entity* sender, const std::string& name, int32_t param1, int32_t param2) {
const auto* const part = MedusaSnakes::GetPart(self->GetLOT());
if (!part || part->turnsHostileOn[0] == '\0' || name != part->turnsHostileOn) return;
TurnHostile(self, part->hostileEffect);
}
// L_WILD_snake_0N.lua onTimerDone: it replaces the shared one, so a part never handles MeanderPause or AggroEmote
void WildSnake::OnLuaTimerDone(Entity* self, const std::string& timerName) {
const auto* const part = MedusaSnakes::GetPart(self->GetLOT());
if (!part) return;
if (timerName == "SnakeStartup") {
self->AddToGroup(MedusaSnakes::GROUP);
// OverrideFriction { bEnableOverride = true, fFriction = 10 }: the client sends it only inside its own
// process (a friction phantom physics effect sends it to the objects in it), it is no network message
// and the server's movement has no friction, so there is nothing to do here
// Part 5 is the first to fight
if (part->turnsHostileOn[0] == '\0') TurnHostile(self, u"");
} else if (timerName == "SnakeFollow") {
auto* const movement = self->GetComponent<MovementAIComponent>();
if (!movement) return;
for (auto* const friend_ : Game::entityManager->GetEntitiesInGroup(MedusaSnakes::GROUP)) {
if (friend_->GetLOT() == part->follows) {
movement->FollowTarget(friend_->GetObjectID(), MedusaSnakes::FOLLOW_RADIUS, MedusaSnakes::FOLLOW_SPEED, MedusaSnakes::FOLLOW_KEEP);
}
}
} else if (timerName == "SnakeGlow") {
GameMessages::PlayFXEffect(self->GetObjectID(), -1, MedusaSnakes::GLOW_EFFECT, "").Send(UNASSIGNED_SYSTEM_ADDRESS);
}
}