From 5963ea06be0df5a9f730a38be33093544849f5f4 Mon Sep 17 00:00:00 2001 From: gsemaj Date: Sat, 16 Apr 2022 20:01:58 -0400 Subject: [PATCH] Move mob aggro logic into `takeDamage` override God that feels good --- src/Combat.cpp | 35 +----------------------------- src/MobAI.cpp | 59 +++++++++++++++++++++++++++++++++++++++----------- src/MobAI.hpp | 1 + 3 files changed, 48 insertions(+), 47 deletions(-) diff --git a/src/Combat.cpp b/src/Combat.cpp index a7c4e70..0082363 100644 --- a/src/Combat.cpp +++ b/src/Combat.cpp @@ -44,41 +44,8 @@ void Player::step(time_t currTime) { int CombatNPC::takeDamage(EntityRef src, int amt) { - /* REFACTOR: all of this logic is strongly coupled to mobs. - * come back to this when more of it is moved to CombatNPC. - * remove this cast when done */ - Mob* mob = (Mob*)this; - - // cannot kill mobs multiple times; cannot harm retreating mobs - if (mob->state != AIState::ROAMING && mob->state != AIState::COMBAT) { - return 0; // no damage - } - - if (mob->skillStyle >= 0) - return 0; // don't hurt a mob casting corruption - - if (mob->state == AIState::ROAMING) { - assert(mob->target == nullptr && src.kind == EntityKind::PLAYER); // players only for now - mob->transition(AIState::COMBAT, src); - - if (mob->groupLeader != 0) - MobAI::followToCombat(mob); - } - hp -= amt; - - // wake up sleeping monster - if (mob->cbf & CSB_BIT_MEZ) { - mob->cbf &= ~CSB_BIT_MEZ; - - INITSTRUCT(sP_FE2CL_CHAR_TIME_BUFF_TIME_OUT, pkt1); - pkt1.eCT = 2; - pkt1.iID = mob->id; - pkt1.iConditionBitFlag = mob->cbf; - NPCManager::sendToViewable(mob, &pkt1, P_FE2CL_CHAR_TIME_BUFF_TIME_OUT, sizeof(sP_FE2CL_CHAR_TIME_BUFF_TIME_OUT)); - } - - if (mob->hp <= 0) + if (hp <= 0) transition(AIState::DEAD, src); return amt; diff --git a/src/MobAI.cpp b/src/MobAI.cpp index 9453bc7..e1d4308 100644 --- a/src/MobAI.cpp +++ b/src/MobAI.cpp @@ -16,6 +16,52 @@ using namespace MobAI; bool MobAI::simulateMobs = settings::SIMULATEMOBS; +void Mob::step(time_t currTime) { + if (playersInView < 0) + std::cout << "[WARN] Weird playerview value " << playersInView << std::endl; + + // skip movement and combat if disabled or not in view + if ((!MobAI::simulateMobs || playersInView == 0) && state != AIState::DEAD + && state != AIState::RETREAT) + return; + + // call superclass step + CombatNPC::step(currTime); +} + +int Mob::takeDamage(EntityRef src, int amt) { + + // cannot kill mobs multiple times; cannot harm retreating mobs + if (state != AIState::ROAMING && state != AIState::COMBAT) { + return 0; // no damage + } + + if (skillStyle >= 0) + return 0; // don't hurt a mob casting corruption + + if (state == AIState::ROAMING) { + assert(target == nullptr && src.kind == EntityKind::PLAYER); // TODO: players only for now + transition(AIState::COMBAT, src); + + if (groupLeader != 0) + MobAI::followToCombat(this); + } + + // wake up sleeping monster + if (cbf & CSB_BIT_MEZ) { + cbf &= ~CSB_BIT_MEZ; + + INITSTRUCT(sP_FE2CL_CHAR_TIME_BUFF_TIME_OUT, pkt1); + pkt1.eCT = 2; + pkt1.iID = id; + pkt1.iConditionBitFlag = cbf; + NPCManager::sendToViewable(this, &pkt1, P_FE2CL_CHAR_TIME_BUFF_TIME_OUT, sizeof(sP_FE2CL_CHAR_TIME_BUFF_TIME_OUT)); + } + + // call superclass takeDamage + return CombatNPC::takeDamage(src, amt); +} + /* * Dynamic lerp; distinct from Transport::lerp(). This one doesn't care about height and * only returns the first step, since the rest will need to be recalculated anyway if chasing player. @@ -423,19 +469,6 @@ void MobAI::incNextMovement(Mob* mob, time_t currTime) { mob->nextMovement = currTime + delay / 2 + Rand::rand(delay / 2); } -void Mob::step(time_t currTime) { - if (playersInView < 0) - std::cout << "[WARN] Weird playerview value " << playersInView << std::endl; - - // skip movement and combat if disabled or not in view - if ((!MobAI::simulateMobs || playersInView == 0) && state != AIState::DEAD - && state != AIState::RETREAT) - return; - - // call superclass step - CombatNPC::step(currTime); -} - void MobAI::deadStep(CombatNPC* npc, time_t currTime) { Mob* self = (Mob*)npc; diff --git a/src/MobAI.hpp b/src/MobAI.hpp index 45a768a..cb6bfc8 100644 --- a/src/MobAI.hpp +++ b/src/MobAI.hpp @@ -96,6 +96,7 @@ struct Mob : public CombatNPC { ~Mob() {} + virtual int takeDamage(EntityRef src, int amt) override; virtual void step(time_t currTime) override; auto operator[](std::string s) {