From c113ea4a6cabccd60bb9af560d93b0375d2379ba Mon Sep 17 00:00:00 2001 From: dongresource Date: Sun, 10 Sep 2023 20:02:52 +0200 Subject: [PATCH] Refactor and generalize NPCEvent logic --- src/Combat.cpp | 10 ++++------ src/NPCManager.cpp | 12 +++++------- src/NPCManager.hpp | 13 ++++--------- 3 files changed, 13 insertions(+), 22 deletions(-) diff --git a/src/Combat.cpp b/src/Combat.cpp index 48598b1..80fbab1 100644 --- a/src/Combat.cpp +++ b/src/Combat.cpp @@ -316,12 +316,10 @@ void CombatNPC::transition(AIState newState, EntityRef src) { transition(AIState::INACTIVE, id); } - // TODO: Properly refactor this - if (newState == AIState::DEAD && src.kind == EntityKind::PLAYER) { - for (NPCEvent& event : NPCManager::NPCEvents) - if (event.trigger == ON_KILLED && event.npcType == type) - event.handler(src.sock, this); - } + // trigger special NPCEvents, if applicable + for (NPCEvent& event : NPCManager::NPCEvents) + if (event.triggerState == newState && event.npcType == type) + event.handler(this); } #pragma endregion diff --git a/src/NPCManager.cpp b/src/NPCManager.cpp index 8a368bf..1d80a93 100644 --- a/src/NPCManager.cpp +++ b/src/NPCManager.cpp @@ -293,13 +293,12 @@ BaseNPC* NPCManager::getNearestNPC(std::set* chunks, int X, int Y, int Z return npc; } -// TODO: Move this to MobAI, possibly +// TODO: Move this to separate file in ai/ subdir when implementing more events #pragma region NPCEvents // summon right arm and stage 2 body -static void lordFuseStageTwo(CNSocket *sock, BaseNPC *npc) { +static void lordFuseStageTwo(CombatNPC *npc) { Mob *oldbody = (Mob*)npc; // adaptium, stun - Player *plr = PlayerManager::getPlayer(sock); std::cout << "Lord Fuse stage two" << std::endl; @@ -320,9 +319,8 @@ static void lordFuseStageTwo(CNSocket *sock, BaseNPC *npc) { } // summon left arm and stage 3 body -static void lordFuseStageThree(CNSocket *sock, BaseNPC *npc) { +static void lordFuseStageThree(CombatNPC *npc) { Mob *oldbody = (Mob*)npc; - Player *plr = PlayerManager::getPlayer(sock); std::cout << "Lord Fuse stage three" << std::endl; @@ -342,8 +340,8 @@ static void lordFuseStageThree(CNSocket *sock, BaseNPC *npc) { } std::vector NPCManager::NPCEvents = { - NPCEvent(2466, ON_KILLED, lordFuseStageTwo), - NPCEvent(2467, ON_KILLED, lordFuseStageThree), + NPCEvent(2466, AIState::DEAD, lordFuseStageTwo), + NPCEvent(2467, AIState::DEAD, lordFuseStageThree), }; #pragma endregion NPCEvents diff --git a/src/NPCManager.hpp b/src/NPCManager.hpp index c8738d0..ec497d3 100644 --- a/src/NPCManager.hpp +++ b/src/NPCManager.hpp @@ -14,20 +14,15 @@ #define RESURRECT_HEIGHT 400 -enum Trigger { - ON_KILLED, - ON_COMBAT -}; - -typedef void (*NPCEventHandler)(CNSocket*, BaseNPC*); +typedef void (*NPCEventHandler)(CombatNPC*); struct NPCEvent { int32_t npcType; - int trigger; + AIState triggerState; NPCEventHandler handler; - NPCEvent(int32_t t, int tr, NPCEventHandler hndlr) - : npcType(t), trigger(tr), handler(hndlr) {} + NPCEvent(int32_t t, AIState tr, NPCEventHandler hndlr) + : npcType(t), triggerState(tr), handler(hndlr) {} }; namespace NPCManager {