From 7c9038cf10f68551a5d75c14ce705d035fb8e853 Mon Sep 17 00:00:00 2001 From: gsemaj Date: Wed, 13 Apr 2022 00:54:13 -0400 Subject: [PATCH] (WIP) onCombatStart hook implementation --- src/Combat.cpp | 7 ++++++- src/MobAI.cpp | 44 ++++++++++++++++++-------------------------- src/MobAI.hpp | 1 - 3 files changed, 24 insertions(+), 28 deletions(-) diff --git a/src/Combat.cpp b/src/Combat.cpp index 047c80c..2b9a251 100644 --- a/src/Combat.cpp +++ b/src/Combat.cpp @@ -59,7 +59,7 @@ int CombatNPC::takeDamage(EntityRef src, int amt) { if (mob->state == AIState::ROAMING) { assert(mob->target == nullptr && src.type == EntityType::PLAYER); // players only for now - MobAI::enterCombat(src.sock, mob); + mob->transition(AIState::COMBAT, src); if (mob->groupLeader != 0) MobAI::followToCombat(mob); @@ -138,6 +138,11 @@ void CombatNPC::transition(AIState newState, EntityRef src) { onRoamStart(); break; case AIState::COMBAT: + /* TODO: fire any triggered events + for (NPCEvent& event : NPCManager::NPCEvents) + if (event.trigger == ON_COMBAT && event.npcType == type) + event.handler(src, this); + */ onCombatStart(src); break; case AIState::RETREAT: diff --git a/src/MobAI.cpp b/src/MobAI.cpp index d9d430e..cc73de5 100644 --- a/src/MobAI.cpp +++ b/src/MobAI.cpp @@ -73,13 +73,13 @@ void MobAI::followToCombat(Mob *mob) { if (followerMob->state != AIState::ROAMING) // only roaming mobs should transition to combat continue; - enterCombat(mob->target, followerMob); + followerMob->transition(AIState::COMBAT, mob->target); } if (leadMob->state != AIState::ROAMING) return; - enterCombat(mob->target, leadMob); + leadMob->transition(AIState::COMBAT, mob->target); } } @@ -168,7 +168,7 @@ bool MobAI::aggroCheck(Mob *mob, time_t currTime) { if (closest != nullptr) { // found closest player. engage. - enterCombat(closest, mob); + mob->transition(AIState::COMBAT, closest); if (mob->groupLeader != 0) followToCombat(mob); @@ -390,27 +390,6 @@ static void useAbilities(Mob *mob, time_t currTime) { return; } -void MobAI::enterCombat(CNSocket *sock, Mob *mob) { - mob->target = sock; - mob->state = AIState::COMBAT; - mob->nextMovement = getTime(); - mob->nextAttack = 0; - - mob->roamX = mob->x; - mob->roamY = mob->y; - mob->roamZ = mob->z; - - int skillID = (int)mob->data["m_iPassiveBuff"]; // cast passive - std::vector targetData = {1, mob->id, 0, 0, 0}; - for (auto& pwr : Abilities::Powers) - if (pwr.skillType == Abilities::SkillTable[skillID].skillType) - pwr.handle(mob->id, targetData, skillID, Abilities::SkillTable[skillID].durationTime[0], Abilities::SkillTable[skillID].powerIntensity[0]); - - for (NPCEvent& event : NPCManager::NPCEvents) // trigger an ON_COMBAT - if (event.trigger == ON_COMBAT && event.npcType == mob->type) - event.handler(sock, mob); -} - static void drainMobHP(Mob *mob, int amount) { size_t resplen = sizeof(sP_FE2CL_CHAR_TIME_BUFF_TIME_TICK) + sizeof(sSkillResult_Damage); assert(resplen < CN_PACKET_BUFFER_SIZE - 8); @@ -758,7 +737,7 @@ void Mob::retreatStep(time_t currTime) { } void Mob::onInactive() { - // stub + // no-op } void Mob::onRoamStart() { @@ -766,7 +745,20 @@ void Mob::onRoamStart() { } void Mob::onCombatStart(EntityRef src) { - // stub + assert(src.type == EntityType::PLAYER); + target = src.sock; + nextMovement = getTime(); + nextAttack = 0; + + roamX = x; + roamY = y; + roamZ = z; + + int skillID = (int)data["m_iPassiveBuff"]; // cast passive + std::vector targetData = { 1, id, 0, 0, 0 }; + for (auto& pwr : Abilities::Powers) + if (pwr.skillType == Abilities::SkillTable[skillID].skillType) + pwr.handle(id, targetData, skillID, Abilities::SkillTable[skillID].durationTime[0], Abilities::SkillTable[skillID].powerIntensity[0]); } void Mob::onRetreat() { diff --git a/src/MobAI.hpp b/src/MobAI.hpp index 7ad9353..dbb267b 100644 --- a/src/MobAI.hpp +++ b/src/MobAI.hpp @@ -96,5 +96,4 @@ namespace MobAI { void clearDebuff(Mob *mob); void followToCombat(Mob *mob); void groupRetreat(Mob *mob); - void enterCombat(CNSocket *sock, Mob *mob); }