Refactor and generalize NPCEvent logic

This commit is contained in:
dongresource 2023-09-10 20:02:52 +02:00 committed by gsemaj
parent 2924a27eb4
commit 6a69388822
No known key found for this signature in database
GPG Key ID: 24B96BAA40497929
3 changed files with 13 additions and 22 deletions

View File

@ -316,12 +316,10 @@ void CombatNPC::transition(AIState newState, EntityRef src) {
transition(AIState::INACTIVE, id); transition(AIState::INACTIVE, id);
} }
// TODO: Properly refactor this // trigger special NPCEvents, if applicable
if (newState == AIState::DEAD && src.kind == EntityKind::PLAYER) {
for (NPCEvent& event : NPCManager::NPCEvents) for (NPCEvent& event : NPCManager::NPCEvents)
if (event.trigger == ON_KILLED && event.npcType == type) if (event.triggerState == newState && event.npcType == type)
event.handler(src.sock, this); event.handler(this);
}
} }
#pragma endregion #pragma endregion

View File

@ -293,13 +293,12 @@ BaseNPC* NPCManager::getNearestNPC(std::set<Chunk*>* chunks, int X, int Y, int Z
return npc; return npc;
} }
// TODO: Move this to MobAI, possibly // TODO: Move this to separate file in ai/ subdir when implementing more events
#pragma region NPCEvents #pragma region NPCEvents
// summon right arm and stage 2 body // 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 Mob *oldbody = (Mob*)npc; // adaptium, stun
Player *plr = PlayerManager::getPlayer(sock);
std::cout << "Lord Fuse stage two" << std::endl; 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 // summon left arm and stage 3 body
static void lordFuseStageThree(CNSocket *sock, BaseNPC *npc) { static void lordFuseStageThree(CombatNPC *npc) {
Mob *oldbody = (Mob*)npc; Mob *oldbody = (Mob*)npc;
Player *plr = PlayerManager::getPlayer(sock);
std::cout << "Lord Fuse stage three" << std::endl; std::cout << "Lord Fuse stage three" << std::endl;
@ -342,8 +340,8 @@ static void lordFuseStageThree(CNSocket *sock, BaseNPC *npc) {
} }
std::vector<NPCEvent> NPCManager::NPCEvents = { std::vector<NPCEvent> NPCManager::NPCEvents = {
NPCEvent(2466, ON_KILLED, lordFuseStageTwo), NPCEvent(2466, AIState::DEAD, lordFuseStageTwo),
NPCEvent(2467, ON_KILLED, lordFuseStageThree), NPCEvent(2467, AIState::DEAD, lordFuseStageThree),
}; };
#pragma endregion NPCEvents #pragma endregion NPCEvents

View File

@ -14,20 +14,15 @@
#define RESURRECT_HEIGHT 400 #define RESURRECT_HEIGHT 400
enum Trigger { typedef void (*NPCEventHandler)(CombatNPC*);
ON_KILLED,
ON_COMBAT
};
typedef void (*NPCEventHandler)(CNSocket*, BaseNPC*);
struct NPCEvent { struct NPCEvent {
int32_t npcType; int32_t npcType;
int trigger; AIState triggerState;
NPCEventHandler handler; NPCEventHandler handler;
NPCEvent(int32_t t, int tr, NPCEventHandler hndlr) NPCEvent(int32_t t, AIState tr, NPCEventHandler hndlr)
: npcType(t), trigger(tr), handler(hndlr) {} : npcType(t), triggerState(tr), handler(hndlr) {}
}; };
namespace NPCManager { namespace NPCManager {