mirror of
https://github.com/DarkflameUniverse/DarkflameServer.git
synced 2026-11-30 01:41:40 +00:00
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>
222 lines
10 KiB
C++
222 lines
10 KiB
C++
// The Medusa (L_WILD_MEDUSA.lua) and its snake parts (L_WILD_snake_01..05.lua): the parts it spawns, the chain that
|
|
// turns them hostile one by one, how they follow, and the Lua AI settings they run with.
|
|
|
|
#include "GameDependencies.h"
|
|
#include "CDClientDatabase.h"
|
|
|
|
#include "Entity.h"
|
|
#include "EntityManager.h"
|
|
#include "GameMessages.h"
|
|
#include "EffectsMessages.h"
|
|
#include "CppScripts.h"
|
|
#include "ScriptComponent.h"
|
|
#include "DestroyableComponent.h"
|
|
#include "MovementAIComponent.h"
|
|
#include "BaseCombatAIComponent.h"
|
|
#include "SimplePhysicsComponent.h"
|
|
#include "ai/WILD/WildMedusa.h"
|
|
#include "ai/WILD/WildMedusaData.h"
|
|
|
|
#include <gtest/gtest.h>
|
|
|
|
using namespace MedusaSnakes;
|
|
|
|
namespace {
|
|
const char* ScriptPath(const LOT lot) {
|
|
switch (lot) {
|
|
case 6602: return "scripts\\ai\\WILD\\L_WILD_MEDUSA.lua";
|
|
case 6564: return "scripts\\ai\\WILD\\L_WILD_snake_01.lua";
|
|
case 6565: return "scripts\\ai\\WILD\\L_WILD_snake_02.lua";
|
|
case 6566: return "scripts\\ai\\WILD\\L_WILD_snake_03.lua";
|
|
case 6567: return "scripts\\ai\\WILD\\L_WILD_snake_04.lua";
|
|
default: return "scripts\\ai\\WILD\\L_WILD_snake_05.lua";
|
|
}
|
|
}
|
|
|
|
// Whether the packets sent since the last clear include the PlayFXEffect { effectType = effect } on the target
|
|
bool SentEffect(const LWOOBJID target, const std::u16string& effect) {
|
|
auto* const mock = static_cast<dServerMock*>(Game::server);
|
|
const auto sent = mock->GetSentPackets();
|
|
mock->ClearSentPackets();
|
|
GameMessages::PlayFXEffect(target, -1, effect, "").Send(UNASSIGNED_SYSTEM_ADDRESS);
|
|
const auto expected = mock->GetSentPackets().back().bytes;
|
|
mock->ClearSentPackets();
|
|
return std::ranges::any_of(sent, [&](const CapturedPacket& packet) { return packet.bytes == expected; });
|
|
}
|
|
};
|
|
|
|
class MedusaScriptsTest : public GameDependenciesTest {
|
|
protected:
|
|
void SetUp() override {
|
|
SetUpDependencies();
|
|
// Creating an entity looks its LOT up in the CDClient
|
|
CDClientDatabase::Connect(":memory:");
|
|
for (const auto* const table : {
|
|
"ComponentsRegistry (id INTEGER, component_type INTEGER, component_id INTEGER)",
|
|
"Objects (id INTEGER, name TEXT, type TEXT, interactionDistance REAL)",
|
|
"Factions (faction INTEGER, enemyList TEXT)",
|
|
"BaseCombatAIComponent (id INTEGER, aggroRadius REAL, tetherSpeed REAL, pursuitSpeed REAL, softTetherRadius REAL, hardTetherRadius REAL, minRoundLength REAL, maxRoundLength REAL, combatRoundLength REAL)",
|
|
"ObjectSkills (objectTemplate INTEGER, skillID INTEGER, castOnType INTEGER, AICombatWeight INTEGER)",
|
|
"SkillBehavior (skillID INTEGER, behaviorID INTEGER)",
|
|
}) {
|
|
CDClientDatabase::ExecuteDML(std::string("CREATE TABLE IF NOT EXISTS ") + table + ";");
|
|
}
|
|
}
|
|
void TearDown() override { TearDownDependencies(); }
|
|
|
|
// An object of the LOT in the snake group, running its registered script
|
|
Entity* Make(const LOT lot, const NiPoint3& pos = NiPoint3Constant::ZERO) {
|
|
auto objectInfo = info;
|
|
objectInfo.lot = lot;
|
|
objectInfo.pos = pos;
|
|
auto* const entity = Game::entityManager->CreateEntity(objectInfo, nullptr, nullptr, false, nextID++);
|
|
entity->AddComponent<DestroyableComponent>(-1)->SetFaction(11);
|
|
entity->AddComponent<ScriptComponent>(-1, "", false)->SetScript(CppScripts::GetScript(entity, ScriptPath(lot)));
|
|
entity->AddToGroup(GROUP);
|
|
return entity;
|
|
}
|
|
|
|
static int32_t Faction(Entity* entity) {
|
|
const auto& factions = entity->GetComponent<DestroyableComponent>()->GetFactionIDs();
|
|
return factions.empty() ? 0 : factions[0];
|
|
}
|
|
|
|
LWOOBJID nextID = 0x4000000000c0001LL;
|
|
};
|
|
|
|
TEST_F(MedusaScriptsTest, ScriptsAreRegistered) {
|
|
auto* const medusa = Make(MEDUSA_LOT);
|
|
EXPECT_NE(dynamic_cast<WildMedusa*>(medusa->GetScript()), nullptr);
|
|
for (const auto& part : PARTS) EXPECT_NE(dynamic_cast<WildSnake*>(Make(part.lot)->GetScript()), nullptr) << part.lot;
|
|
}
|
|
|
|
// onDie of part 5 -> next1 -> part 4 ... part 1 -> next5 -> the Medusa, each turning faction 4 with its effect
|
|
TEST_F(MedusaScriptsTest, EachDeathTurnsTheNextOneHostile) {
|
|
auto* const medusa = Make(MEDUSA_LOT);
|
|
std::array<Entity*, 5> parts{};
|
|
for (size_t i = 0; i < PARTS.size(); i++) parts[i] = Make(PARTS[i].lot);
|
|
|
|
const std::array<const char16_t*, 4> effects{ u"yellow", u"green", u"blue", u"purple" };
|
|
for (int32_t dying = 4; dying >= 1; dying--) {
|
|
static_cast<dServerMock*>(Game::server)->ClearSentPackets();
|
|
auto* const next = parts[dying - 1];
|
|
EXPECT_EQ(Faction(next), 11);
|
|
parts[dying]->GetScript()->OnDie(parts[dying], nullptr);
|
|
EXPECT_EQ(Faction(next), HOSTILE_FACTION) << "after part " << dying + 1;
|
|
EXPECT_TRUE(SentEffect(next->GetObjectID(), effects[4 - dying]));
|
|
EXPECT_EQ(Faction(medusa), 11);
|
|
}
|
|
|
|
static_cast<dServerMock*>(Game::server)->ClearSentPackets();
|
|
parts[0]->GetScript()->OnDie(parts[0], nullptr);
|
|
EXPECT_EQ(Faction(medusa), HOSTILE_FACTION);
|
|
EXPECT_TRUE(SentEffect(medusa->GetObjectID(), MEDUSA_EFFECT));
|
|
}
|
|
|
|
// Part 5's SnakeStartup turns it hostile; the others stay as they are
|
|
TEST_F(MedusaScriptsTest, PartFiveStartsHostile) {
|
|
auto* const first = Make(6564);
|
|
auto* const fifth = Make(6568);
|
|
first->GetScript()->OnTimerDone(first, "SnakeStartup");
|
|
fifth->GetScript()->OnTimerDone(fifth, "SnakeStartup");
|
|
EXPECT_EQ(Faction(first), 11);
|
|
EXPECT_EQ(Faction(fifth), HOSTILE_FACTION);
|
|
}
|
|
|
|
// RESMGR:LoadObject at +-5 around the Medusa, owner = the Medusa
|
|
TEST_F(MedusaScriptsTest, MedusaSpawnsItsPartsAroundIt) {
|
|
auto* const medusa = Make(MEDUSA_LOT);
|
|
medusa->AddComponent<SimplePhysicsComponent>(-1)->SetPosition({ 100.0f, 10.0f, -50.0f });
|
|
medusa->GetScript()->OnStartup(medusa);
|
|
|
|
const std::array<NiPoint3, 5> expected{ { { 100, 10, -45 }, { 100, 10, -55 }, { 105, 10, -50 }, { 95, 10, -50 }, { 105, 10, -55 } } };
|
|
for (size_t i = 0; i < PARTS.size(); i++) {
|
|
const auto spawned = Game::entityManager->GetEntitiesByLOT(PARTS[i].lot);
|
|
ASSERT_EQ(spawned.size(), 1u) << PARTS[i].lot;
|
|
EXPECT_EQ(spawned[0]->GetDefaultPosition(), expected[i]) << PARTS[i].lot;
|
|
EXPECT_EQ(spawned[0]->GetSpawnerID(), medusa->GetObjectID());
|
|
}
|
|
EXPECT_EQ(medusa->GetGroups(), (std::vector<std::string>{ GROUP }));
|
|
}
|
|
|
|
// SnakeFollow: FollowTarget { the part before it, radius = 4.5, speed = 1, keepFollowing = true }
|
|
TEST_F(MedusaScriptsTest, PartsFollowThePartBeforeThem) {
|
|
auto* const medusa = Make(MEDUSA_LOT);
|
|
medusa->AddComponent<SimplePhysicsComponent>(-1);
|
|
std::array<Entity*, 5> parts{};
|
|
for (size_t i = 0; i < PARTS.size(); i++) {
|
|
parts[i] = Make(PARTS[i].lot);
|
|
parts[i]->AddComponent<SimplePhysicsComponent>(-1);
|
|
parts[i]->AddComponent<MovementAIComponent>(0, MovementAIInfo{});
|
|
}
|
|
for (size_t i = 0; i < parts.size(); i++) {
|
|
parts[i]->GetScript()->OnTimerDone(parts[i], "SnakeFollow");
|
|
const auto* const movement = parts[i]->GetComponent<MovementAIComponent>();
|
|
const auto followed = i == 0 ? medusa->GetObjectID() : parts[i - 1]->GetObjectID();
|
|
EXPECT_EQ(movement->GetFollowedTarget(), followed) << i;
|
|
EXPECT_FLOAT_EQ(movement->GetFollowRadius(), 4.5f);
|
|
EXPECT_FLOAT_EQ(movement->GetFollowSpeed(), 1.0f);
|
|
EXPECT_TRUE(movement->GetKeepFollowing());
|
|
}
|
|
}
|
|
|
|
// It stops within the radius, and walks after the target again once it is out of it
|
|
TEST_F(MedusaScriptsTest, FollowHaltsWithinTheRadiusAndKeepsFollowing) {
|
|
auto* const target = Make(MEDUSA_LOT, { 20.0f, 0.0f, 0.0f });
|
|
target->AddComponent<SimplePhysicsComponent>(-1)->SetPosition({ 20.0f, 0.0f, 0.0f });
|
|
auto* const follower = Make(6564);
|
|
follower->AddComponent<SimplePhysicsComponent>(-1);
|
|
auto* const movement = follower->AddComponent<MovementAIComponent>(0, MovementAIInfo{});
|
|
|
|
movement->FollowTarget(target->GetObjectID(), 4.5f, 1.0f, true);
|
|
for (float time = 0.0f; time < 30.0f; time += 0.05f) movement->Run(0.05f);
|
|
const float x = follower->GetPosition().x;
|
|
EXPECT_GT(x, 10.0f);
|
|
EXPECT_LE(20.0f - x, 4.6f);
|
|
|
|
target->GetComponent<SimplePhysicsComponent>()->SetPosition({ 60.0f, 0.0f, 0.0f });
|
|
for (float time = 0.0f; time < 60.0f; time += 0.05f) movement->Run(0.05f);
|
|
EXPECT_GT(follower->GetPosition().x, 50.0f);
|
|
EXPECT_LE(60.0f - follower->GetPosition().x, 4.6f);
|
|
EXPECT_EQ(movement->GetFollowedTarget(), target->GetObjectID());
|
|
|
|
// Without keepFollowing it stops following once it arrives
|
|
movement->FollowTarget(target->GetObjectID(), 0.0f, 1.0f, false);
|
|
for (float time = 0.0f; time < 30.0f; time += 0.05f) movement->Run(0.05f);
|
|
EXPECT_EQ(movement->GetFollowedTarget(), LWOOBJID_EMPTY);
|
|
}
|
|
|
|
// onStartup ends with loadOnce, getVarables, CreateStates and oStart: the shared Lua AI with each Set table
|
|
TEST_F(MedusaScriptsTest, TheyRunTheSharedLuaAi) {
|
|
auto* const medusa = Make(MEDUSA_LOT);
|
|
medusa->AddComponent<SimplePhysicsComponent>(-1)->SetPosition({ 100.0f, 10.0f, -50.0f });
|
|
auto* const movement = medusa->AddComponent<MovementAIComponent>(0, MovementAIInfo{});
|
|
medusa->GetScript()->OnStartup(medusa);
|
|
const auto* const machine = LuaAIScript::GetMachine(medusa);
|
|
ASSERT_NE(machine, nullptr);
|
|
// An Aggressive wanderer: off to a point within 25 of where it spawned
|
|
EXPECT_EQ(machine->GetState(), LuaAI::State::Meander);
|
|
EXPECT_EQ(machine->GetSettings().aggression, LuaAI::Aggression::Aggressive);
|
|
EXPECT_FLOAT_EQ(machine->GetSettings().aggroRadius, 15.0f);
|
|
EXPECT_FALSE(movement->AtFinalWaypoint());
|
|
const auto destination = movement->GetDestination();
|
|
EXPECT_LE(std::abs(destination.x - 100.0f), 25.0f);
|
|
EXPECT_LE(std::abs(destination.z + 50.0f), 25.0f);
|
|
|
|
// A Passive guard that still runs its own timers
|
|
auto* const snake = Make(PARTS[0].lot);
|
|
snake->AddComponent<SimplePhysicsComponent>(-1);
|
|
snake->AddComponent<MovementAIComponent>(0, MovementAIInfo{});
|
|
snake->GetScript()->OnStartup(snake);
|
|
const auto* const snakeMachine = LuaAIScript::GetMachine(snake);
|
|
ASSERT_NE(snakeMachine, nullptr);
|
|
EXPECT_EQ(snakeMachine->GetState(), LuaAI::State::Idle);
|
|
EXPECT_EQ(snakeMachine->GetSettings().aggression, LuaAI::Aggression::Passive);
|
|
EXPECT_FLOAT_EQ(snakeMachine->GetSettings().tetherSpeed, 8.0f);
|
|
snake->GetScript()->OnTimerDone(snake, "SnakeFollow");
|
|
EXPECT_EQ(snake->GetComponent<MovementAIComponent>()->GetFollowedTarget(), medusa->GetObjectID());
|
|
// A death enters the Dead state before the part's own onDie
|
|
snake->GetScript()->OnDie(snake, nullptr);
|
|
EXPECT_EQ(snakeMachine->GetState(), LuaAI::State::Dead);
|
|
}
|