mirror of
https://github.com/DarkflameUniverse/DarkflameServer.git
synced 2026-11-30 01:41:40 +00:00
225 lines
10 KiB
C++
225 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);
|
|
}
|
|
|
|
// The Set tables, put on the combat and movement AI as the Lua AI's states use them
|
|
TEST_F(MedusaScriptsTest, LuaAiSettingsAreApplied) {
|
|
Entity medusaEntity(0x4000000000c0101LL, info);
|
|
auto* const medusa = &medusaEntity;
|
|
medusa->AddComponent<SimplePhysicsComponent>(-1);
|
|
auto* const combat = medusa->AddComponent<BaseCombatAIComponent>(-1);
|
|
auto* const movement = medusa->AddComponent<MovementAIComponent>(0, MovementAIInfo{});
|
|
LuaAi::Apply(medusa, LuaAi::MEDUSA);
|
|
EXPECT_FLOAT_EQ(combat->GetAggroRadius(), 15.0f);
|
|
EXPECT_FLOAT_EQ(combat->GetHardTetherRadius(), 50.0f);
|
|
EXPECT_FLOAT_EQ(combat->GetTetherSpeed(), 1.0f);
|
|
EXPECT_FLOAT_EQ(combat->GetAttackRadius(), 8.0f);
|
|
EXPECT_FLOAT_EQ(combat->GetPursuitSpeed(), 2.0f);
|
|
EXPECT_FLOAT_EQ(movement->GetInfo().wanderRadius, 25.0f);
|
|
EXPECT_FLOAT_EQ(movement->GetInfo().wanderSpeed, 0.65f);
|
|
EXPECT_FLOAT_EQ(movement->GetInfo().wanderChance, 100.0f);
|
|
EXPECT_FLOAT_EQ(movement->GetInfo().wanderDelayMin, 1.0f);
|
|
EXPECT_FLOAT_EQ(movement->GetInfo().wanderDelayMax, 10.0f);
|
|
|
|
// A Passive Guard: no aggro proximity (its aggro radius stays), no wandering
|
|
Entity snakeEntity(0x4000000000c0102LL, info);
|
|
auto* const snake = &snakeEntity;
|
|
snake->AddComponent<SimplePhysicsComponent>(-1);
|
|
auto* const snakeCombat = snake->AddComponent<BaseCombatAIComponent>(-1);
|
|
auto* const snakeMovement = snake->AddComponent<MovementAIComponent>(0, MovementAIInfo{});
|
|
const float aggroBefore = snakeCombat->GetAggroRadius();
|
|
LuaAi::Apply(snake, LuaAi::SNAKE);
|
|
EXPECT_FLOAT_EQ(snakeCombat->GetAggroRadius(), aggroBefore);
|
|
EXPECT_FLOAT_EQ(snakeCombat->GetHardTetherRadius(), 50.0f);
|
|
EXPECT_FLOAT_EQ(snakeCombat->GetTetherSpeed(), 8.0f);
|
|
EXPECT_FLOAT_EQ(snakeCombat->GetAttackRadius(), 7.0f);
|
|
EXPECT_FLOAT_EQ(snakeCombat->GetPursuitSpeed(), 7.0f);
|
|
EXPECT_FLOAT_EQ(snakeMovement->GetInfo().wanderRadius, 8.0f);
|
|
EXPECT_FLOAT_EQ(snakeMovement->GetInfo().wanderChance, 0.0f);
|
|
EXPECT_FLOAT_EQ(snakeMovement->GetInfo().wanderDelayMin, 9999.0f);
|
|
}
|