diff --git a/dScripts/ai/WILD/CMakeLists.txt b/dScripts/ai/WILD/CMakeLists.txt index 6aed1bc10..8f76bddae 100644 --- a/dScripts/ai/WILD/CMakeLists.txt +++ b/dScripts/ai/WILD/CMakeLists.txt @@ -13,5 +13,5 @@ set(DSCRIPTS_SOURCES_AI_WILD "WildPants.cpp") add_library(dScriptsAiWild OBJECT ${DSCRIPTS_SOURCES_AI_WILD}) -target_include_directories(dScriptsAiWild PUBLIC ".") +target_include_directories(dScriptsAiWild PUBLIC "." "${CMAKE_CURRENT_SOURCE_DIR}/../LuaAI") target_precompile_headers(dScriptsAiWild REUSE_FROM dScriptsBase) diff --git a/dScripts/ai/WILD/WildMedusa.cpp b/dScripts/ai/WILD/WildMedusa.cpp index b42e87edc..4cb62bfac 100644 --- a/dScripts/ai/WILD/WildMedusa.cpp +++ b/dScripts/ai/WILD/WildMedusa.cpp @@ -39,7 +39,7 @@ void WildMedusa::OnStartup(Entity* self) { } // loadOnce, getVarables, CreateStates, oStart - MedusaSnakes::LuaAi::Apply(self, MedusaSnakes::LuaAi::MEDUSA); + StartLuaAI(self, MedusaSnakes::MedusaAi()); } // L_WILD_MEDUSA.lua onNotifyObject @@ -55,11 +55,11 @@ void WildSnake::OnStartup(Entity* self) { if (part && part->turnsHostileOn[0] == '\0') self->AddTimer("SnakeGlow", MedusaSnakes::GLOW_TIME); // loadOnce, getVarables, CreateStates, oStart - MedusaSnakes::LuaAi::Apply(self, MedusaSnakes::LuaAi::SNAKE); + StartLuaAI(self, MedusaSnakes::SnakeAi()); } // L_WILD_snake_0N.lua onDie -void WildSnake::OnDie(Entity* self, Entity* killer) { +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)) { @@ -74,8 +74,8 @@ void WildSnake::OnNotifyObject(Entity* self, Entity* sender, const std::string& TurnHostile(self, part->hostileEffect); } -// L_WILD_snake_0N.lua onTimerDone -void WildSnake::OnTimerDone(Entity* self, std::string timerName) { +// 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; diff --git a/dScripts/ai/WILD/WildMedusa.h b/dScripts/ai/WILD/WildMedusa.h index a03237aeb..171d9b0f5 100644 --- a/dScripts/ai/WILD/WildMedusa.h +++ b/dScripts/ai/WILD/WildMedusa.h @@ -1,18 +1,18 @@ #pragma once -#include "CppScripts.h" +#include "LuaAIScript.h" // L_WILD_MEDUSA.lua: spawns the five snake parts around it and turns hostile once the last of them dies -class WildMedusa final : public CppScripts::Script { +class WildMedusa final : public LuaAIScript { public: void OnStartup(Entity* self) override; void OnNotifyObject(Entity* self, Entity* sender, const std::string& name, int32_t param1 = 0, int32_t param2 = 0) override; }; // L_WILD_snake_01..05.lua: a snake part that follows the part before it and, when it dies, turns the next one hostile -class WildSnake final : public CppScripts::Script { +class WildSnake final : public LuaAIScript { public: void OnStartup(Entity* self) override; - void OnDie(Entity* self, Entity* killer) override; + void OnLuaDie(Entity* self, Entity* killer) override; void OnNotifyObject(Entity* self, Entity* sender, const std::string& name, int32_t param1 = 0, int32_t param2 = 0) override; - void OnTimerDone(Entity* self, std::string timerName) override; + void OnLuaTimerDone(Entity* self, const std::string& timerName) override; }; diff --git a/dScripts/ai/WILD/WildMedusaData.cpp b/dScripts/ai/WILD/WildMedusaData.cpp index 30c1c3f30..c396e9003 100644 --- a/dScripts/ai/WILD/WildMedusaData.cpp +++ b/dScripts/ai/WILD/WildMedusaData.cpp @@ -1,8 +1,6 @@ #include "WildMedusaData.h" #include "Entity.h" -#include "BaseCombatAIComponent.h" -#include "MovementAIComponent.h" const MedusaSnakes::Part* MedusaSnakes::GetPart(const LOT lot) { for (const auto& part : PARTS) { @@ -11,29 +9,52 @@ const MedusaSnakes::Part* MedusaSnakes::GetPart(const LOT lot) { return nullptr; } -void MedusaSnakes::LuaAi::Apply(Entity* self, const Settings& set) { - auto* const combat = self->GetComponent(); - if (combat) { - // oStart: the aggroRadius proximity, for an Aggressive one only - if (set.aggression == Aggression::Aggressive) combat->SetAggroRadius(set.aggroRadius); - // aggro.onEnter: SetTetherPoint { radius = tetherRadius } and - // FollowTarget { radius = AggroDist, speed = AggroSpeed * 2, keepFollowing = true } - combat->SetHardTetherRadius(set.tetherRadius); - combat->SetAttackRadius(set.aggroDist); - combat->SetPursuitSpeed(PursuitSpeed(set)); - // tether.onEnter: GoTo { speed = tetherSpeed } - combat->SetTetherSpeed(set.tetherSpeed); - } - - // conductRadius: no conduct proximity is made, nothing reads it - - auto* const movement = self->GetComponent(); - if (movement) { - // Meander: GoTo { speed = WanderSpeed } within wanderRadius, then a WanderDelayMin..Max pause. Only a - // Wander one has the Meander state; a Guard one stays put - movement->SetWanderRadius(set.wanderRadius); - movement->SetWanderSpeed(set.wanderSpeed); - movement->SetWanderDelay(set.wanderDelayMin, set.wanderDelayMax); - movement->SetWanderChance(set.movementType == MovementType::Wander ? set.wanderChance : 0.0f); - } +const LuaAI::Settings& MedusaSnakes::MedusaAi() { + static const LuaAI::Settings settings = [] { + LuaAI::Settings set{}; + set.aggroRadius = 15.0f; + set.tetherRadius = 50.0f; + set.tetherSpeed = 1.0f; + set.wanderRadius = 25; + set.aggression = LuaAI::Aggression::Aggressive; + set.aggroDist = 8.0f; + set.aggroSpeed = 1.0f; + set.aggroEmote = false; + set.aggroEmoteDelay = 1.0f; + set.movementType = LuaAI::MovementType::Wander; + set.wanderChance = 100; + set.wanderDelayMin = 1; + set.wanderDelayMax = 10; + set.wanderSpeed = 0.65f; + set.wanderEmote = false; + set.wanderEmoteWeight = 30; + set.wanderEmoteType1 = u"salute"; + // Its Set['SuspendLuaAI'] = true is wiped by the Set = {} after it + return set; + }(); + return settings; +} + +// The same for all five parts +const LuaAI::Settings& MedusaSnakes::SnakeAi() { + static const LuaAI::Settings settings = [] { + LuaAI::Settings set{}; + set.aggroRadius = 50.0f; + set.tetherRadius = 50.0f; + set.tetherSpeed = 8.0f; + set.wanderRadius = 8; + set.aggression = LuaAI::Aggression::Passive; + set.aggroDist = 7.0f; + set.aggroSpeed = 3.5f; + set.movementType = LuaAI::MovementType::Guard; + set.wanderChance = 0; + set.wanderDelayMin = 9999; + set.wanderDelayMax = 9999; + set.wanderSpeed = 0.5f; + set.wanderEmote = false; + set.wanderEmoteWeight = 30; + set.wanderEmoteType1 = u"salute"; + return set; + }(); + return settings; } diff --git a/dScripts/ai/WILD/WildMedusaData.h b/dScripts/ai/WILD/WildMedusaData.h index fd2832ea8..65777e3f1 100644 --- a/dScripts/ai/WILD/WildMedusaData.h +++ b/dScripts/ai/WILD/WildMedusaData.h @@ -1,11 +1,12 @@ #pragma once // The Medusa (L_WILD_MEDUSA.lua) and its five snake parts (L_WILD_snake_01..05.lua): the parts it spawns, the -// chain of notifications that turns each one hostile when the one before it dies, and the Lua AI settings (the -// scripts' Set tables) they run with. +// chain of notifications that turns each one hostile when the one before it dies, and the Set tables they run the +// shared Lua AI with. #include "dCommonVars.h" #include "NiPoint3.h" +#include "LuaAIMachine.h" #include #include @@ -59,35 +60,7 @@ namespace MedusaSnakes { // The part with this LOT, or nullptr const Part* GetPart(LOT lot); - // The Lua AI settings of the scripts' Set tables that change the server's AI - namespace LuaAi { - enum class Aggression { Aggressive, Neutral, Passive, PassiveAggres }; - enum class MovementType { Guard, Wander }; - - struct Settings { - float aggroRadius; - // Read only by the conduct proximity, which oStart never makes: no effect - float conductRadius; - float tetherRadius; - float tetherSpeed; - float wanderRadius; - Aggression aggression; - float aggroDist; - float aggroSpeed; - MovementType movementType; - float wanderChance; - float wanderDelayMin; - float wanderDelayMax; - float wanderSpeed; - }; - - constexpr Settings MEDUSA{ 15.0f, 15.0f, 50.0f, 1.0f, 25.0f, Aggression::Aggressive, 8.0f, 1.0f, MovementType::Wander, 100.0f, 1.0f, 10.0f, 0.65f }; - constexpr Settings SNAKE{ 50.0f, 15.0f, 50.0f, 8.0f, 8.0f, Aggression::Passive, 7.0f, 3.5f, MovementType::Guard, 0.0f, 9999.0f, 9999.0f, 0.5f }; - - // The aggro state's FollowTarget speed: AggroSpeed * 2 - constexpr float PursuitSpeed(const Settings& set) { return set.aggroSpeed * 2.0f; } - - // oStart and the states it makes: puts the settings on the entity's combat and movement AI, where it has them - void Apply(Entity* self, const Settings& set); - }; + // The scripts' Set tables, for the shared Lua AI + const LuaAI::Settings& MedusaAi(); + const LuaAI::Settings& SnakeAi(); }; diff --git a/tests/dGameTests/CMakeLists.txt b/tests/dGameTests/CMakeLists.txt index bcbf2e087..1939749c6 100644 --- a/tests/dGameTests/CMakeLists.txt +++ b/tests/dGameTests/CMakeLists.txt @@ -56,7 +56,7 @@ file(COPY ${COMPONENT_TEST_DATA} DESTINATION ${CMAKE_CURRENT_BINARY_DIR}) # Add the executable. Remember to add all tests above this! add_executable(dGameTests ${DGAMETEST_SOURCES}) -target_include_directories(dGameTests PRIVATE "." "${PROJECT_SOURCE_DIR}/dChatServer" "${PROJECT_SOURCE_DIR}/dScripts") +target_include_directories(dGameTests PRIVATE "." "${PROJECT_SOURCE_DIR}/dChatServer" "${PROJECT_SOURCE_DIR}/dScripts" "${PROJECT_SOURCE_DIR}/dScripts/ai/LuaAI") # Tests against the live CDClient look for it in the source tree's build folder target_compile_definitions(dGameTests PRIVATE PROJECT_SOURCE_DIR="${PROJECT_SOURCE_DIR}") diff --git a/tests/dGameTests/MedusaScriptsTests.cpp b/tests/dGameTests/MedusaScriptsTests.cpp index b75de0149..cdbcbb183 100644 --- a/tests/dGameTests/MedusaScriptsTests.cpp +++ b/tests/dGameTests/MedusaScriptsTests.cpp @@ -186,39 +186,36 @@ TEST_F(MedusaScriptsTest, FollowHaltsWithinTheRadiusAndKeepsFollowing) { 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(-1); - auto* const combat = medusa->AddComponent(-1); +// 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(-1)->SetPosition({ 100.0f, 10.0f, -50.0f }); auto* const movement = medusa->AddComponent(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); + 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: no aggro proximity (its aggro radius stays), no wandering - Entity snakeEntity(0x4000000000c0102LL, info); - auto* const snake = &snakeEntity; + // A Passive guard that still runs its own timers + auto* const snake = Make(PARTS[0].lot); snake->AddComponent(-1); - auto* const snakeCombat = snake->AddComponent(-1); - auto* const snakeMovement = snake->AddComponent(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); + snake->AddComponent(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()->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); }