mirror of
https://github.com/OpenFusionProject/OpenFusion.git
synced 2025-11-30 04:20:06 +00:00
(WIP) Start implementing ICombatant
Start by replacing `hitMob` with `takeDamage` interface function. Simplify `pcAttackChars` a little by utilizing the new interface, then add more interface functions as needed. A lot of the combat logic is tied to the `Mob` class. Need to start moving stuff over to CombatNPC.
This commit is contained in:
182
src/Combat.cpp
182
src/Combat.cpp
@@ -17,8 +17,9 @@ using namespace Combat;
|
||||
/// Player Id -> Bullet Id -> Bullet
|
||||
std::map<int32_t, std::map<int8_t, Bullet>> Combat::Bullets;
|
||||
|
||||
void Player::takeDamage(EntityRef src, int amt) {
|
||||
// stubbed
|
||||
int Player::takeDamage(EntityRef src, int amt) {
|
||||
HP -= amt;
|
||||
return amt;
|
||||
}
|
||||
|
||||
void Player::heal(EntityRef src, int amt) {
|
||||
@@ -29,8 +30,54 @@ bool Player::isAlive() {
|
||||
return HP > 0;
|
||||
}
|
||||
|
||||
void CombatNPC::takeDamage(EntityRef src, int amt) {
|
||||
// stubbed
|
||||
int Player::getCurrentHP() {
|
||||
return HP;
|
||||
}
|
||||
|
||||
int32_t Player::getID() {
|
||||
return iID;
|
||||
}
|
||||
|
||||
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 != MobState::ROAMING && mob->state != MobState::COMBAT) {
|
||||
return 0; // no damage
|
||||
}
|
||||
|
||||
if (mob->skillStyle >= 0)
|
||||
return 0; // don't hurt a mob casting corruption
|
||||
|
||||
if (mob->state == MobState::ROAMING) {
|
||||
assert(mob->target == nullptr && src.type == EntityType::PLAYER); // players only for now
|
||||
MobAI::enterCombat(src.sock, mob);
|
||||
|
||||
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)
|
||||
killMob(mob->target, mob);
|
||||
|
||||
return amt;
|
||||
}
|
||||
|
||||
void CombatNPC::heal(EntityRef src, int amt) {
|
||||
@@ -41,6 +88,14 @@ bool CombatNPC::isAlive() {
|
||||
return hp > 0;
|
||||
}
|
||||
|
||||
int CombatNPC::getCurrentHP() {
|
||||
return hp;
|
||||
}
|
||||
|
||||
int32_t CombatNPC::getID() {
|
||||
return id;
|
||||
}
|
||||
|
||||
static std::pair<int,int> getDamage(int attackPower, int defensePower, bool shouldCrit,
|
||||
bool batteryBoost, int attackerStyle,
|
||||
int defenderStyle, int difficulty) {
|
||||
@@ -152,7 +207,7 @@ static void pcAttackNpcs(CNSocket *sock, CNPacketData *data) {
|
||||
else
|
||||
plr->batteryW = 0;
|
||||
|
||||
damage.first = hitMob(sock, mob, damage.first);
|
||||
damage.first = mob->takeDamage(sock, damage.first);
|
||||
|
||||
respdata[i].iID = mob->id;
|
||||
respdata[i].iDamage = damage.first;
|
||||
@@ -205,42 +260,6 @@ void Combat::npcAttackPc(Mob *mob, time_t currTime) {
|
||||
}
|
||||
}
|
||||
|
||||
int Combat::hitMob(CNSocket *sock, Mob *mob, int damage) {
|
||||
// cannot kill mobs multiple times; cannot harm retreating mobs
|
||||
if (mob->state != MobState::ROAMING && mob->state != MobState::COMBAT) {
|
||||
return 0; // no damage
|
||||
}
|
||||
|
||||
if (mob->skillStyle >= 0)
|
||||
return 0; // don't hurt a mob casting corruption
|
||||
|
||||
if (mob->state == MobState::ROAMING) {
|
||||
assert(mob->target == nullptr);
|
||||
MobAI::enterCombat(sock, mob);
|
||||
|
||||
if (mob->groupLeader != 0)
|
||||
MobAI::followToCombat(mob);
|
||||
}
|
||||
|
||||
mob->hp -= damage;
|
||||
|
||||
// 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)
|
||||
killMob(mob->target, mob);
|
||||
|
||||
return damage;
|
||||
}
|
||||
|
||||
/*
|
||||
* When a group of players is doing missions together, we want them to all get
|
||||
* quest items at the same time, but we don't want the odds of quest item
|
||||
@@ -432,7 +451,7 @@ static void pcAttackChars(CNSocket *sock, CNPacketData *data) {
|
||||
return;
|
||||
|
||||
// Unlike the attack mob packet, attacking players packet has an 8-byte trail (Instead of 4 bytes).
|
||||
if (!validInVarPacket(sizeof(sP_CL2FE_REQ_PC_ATTACK_CHARs), pkt->iTargetCnt, sizeof(int32_t) * 2, data->size)) {
|
||||
if (!validInVarPacket(sizeof(sP_CL2FE_REQ_PC_ATTACK_CHARs), pkt->iTargetCnt, sizeof(sGM_PVPTarget), data->size)) {
|
||||
std::cout << "[WARN] bad sP_CL2FE_REQ_PC_ATTACK_CHARs packet size\n";
|
||||
return;
|
||||
}
|
||||
@@ -456,11 +475,20 @@ static void pcAttackChars(CNSocket *sock, CNPacketData *data) {
|
||||
resp->iTargetCnt = pkt->iTargetCnt;
|
||||
|
||||
for (int i = 0; i < pkt->iTargetCnt; i++) {
|
||||
if (pktdata[i*2+1] == 1) { // eCT == 1; attack player
|
||||
Player *target = nullptr;
|
||||
|
||||
ICombatant* target = nullptr;
|
||||
sGM_PVPTarget* targdata = (sGM_PVPTarget*)(pktdata + i * 2);
|
||||
std::pair<int, int> damage;
|
||||
|
||||
if (pkt->iTargetCnt > 1)
|
||||
damage.first = plr->groupDamage;
|
||||
else
|
||||
damage.first = plr->pointDamage;
|
||||
|
||||
if (targdata->eCT == 1) { // eCT == 1; attack player
|
||||
|
||||
for (auto& pair : PlayerManager::players) {
|
||||
if (pair.second->iID == pktdata[i*2]) {
|
||||
if (pair.second->iID == targdata->iID) {
|
||||
target = pair.second;
|
||||
break;
|
||||
}
|
||||
@@ -472,67 +500,41 @@ static void pcAttackChars(CNSocket *sock, CNPacketData *data) {
|
||||
return;
|
||||
}
|
||||
|
||||
std::pair<int,int> damage;
|
||||
damage = getDamage(damage.first, ((Player*)target)->defense, true, (plr->batteryW > 6 + plr->level), -1, -1, 0);
|
||||
|
||||
if (pkt->iTargetCnt > 1)
|
||||
damage.first = plr->groupDamage;
|
||||
else
|
||||
damage.first = plr->pointDamage;
|
||||
|
||||
damage = getDamage(damage.first, target->defense, true, (plr->batteryW > 6 + plr->level), -1, -1, 0);
|
||||
|
||||
if (plr->batteryW >= 6 + plr->level)
|
||||
plr->batteryW -= 6 + plr->level;
|
||||
else
|
||||
plr->batteryW = 0;
|
||||
|
||||
target->HP -= damage.first;
|
||||
|
||||
respdata[i].eCT = pktdata[i*2+1];
|
||||
respdata[i].iID = target->iID;
|
||||
respdata[i].iDamage = damage.first;
|
||||
respdata[i].iHP = target->HP;
|
||||
respdata[i].iHitFlag = damage.second; // hitscan, not a rocket or a grenade
|
||||
} else { // eCT == 4; attack mob
|
||||
if (NPCManager::NPCs.find(pktdata[i*2]) == NPCManager::NPCs.end()) {
|
||||
|
||||
if (NPCManager::NPCs.find(targdata->iID) == NPCManager::NPCs.end()) {
|
||||
// not sure how to best handle this
|
||||
std::cout << "[WARN] pcAttackChars: NPC ID not found" << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
BaseNPC* npc = NPCManager::NPCs[pktdata[i * 2]];
|
||||
BaseNPC* npc = NPCManager::NPCs[targdata->iID];
|
||||
if (npc->kind != EntityType::MOB) {
|
||||
std::cout << "[WARN] pcAttackChars: NPC is not a mob" << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
Mob* mob = (Mob*)npc;
|
||||
|
||||
std::pair<int,int> damage;
|
||||
|
||||
if (pkt->iTargetCnt > 1)
|
||||
damage.first = plr->groupDamage;
|
||||
else
|
||||
damage.first = plr->pointDamage;
|
||||
|
||||
target = mob;
|
||||
int difficulty = (int)mob->data["m_iNpcLevel"];
|
||||
|
||||
damage = getDamage(damage.first, (int)mob->data["m_iProtection"], true, (plr->batteryW > 6 + difficulty),
|
||||
Nanos::nanoStyle(plr->activeNano), (int)mob->data["m_iNpcStyle"], difficulty);
|
||||
|
||||
if (plr->batteryW >= 6 + difficulty)
|
||||
plr->batteryW -= 6 + difficulty;
|
||||
else
|
||||
plr->batteryW = 0;
|
||||
|
||||
damage.first = hitMob(sock, mob, damage.first);
|
||||
|
||||
respdata[i].eCT = pktdata[i*2+1];
|
||||
respdata[i].iID = mob->id;
|
||||
respdata[i].iDamage = damage.first;
|
||||
respdata[i].iHP = mob->hp;
|
||||
respdata[i].iHitFlag = damage.second; // hitscan, not a rocket or a grenade
|
||||
}
|
||||
|
||||
if (plr->batteryW >= 6 + plr->level)
|
||||
plr->batteryW -= 6 + plr->level;
|
||||
else
|
||||
plr->batteryW = 0;
|
||||
|
||||
damage.first = target->takeDamage(sock, damage.first);
|
||||
|
||||
respdata[i].eCT = targdata->eCT;
|
||||
respdata[i].iID = target->getID();
|
||||
respdata[i].iDamage = damage.first;
|
||||
respdata[i].iHP = target->getCurrentHP();
|
||||
respdata[i].iHitFlag = damage.second; // hitscan, not a rocket or a grenade
|
||||
}
|
||||
|
||||
sock->sendPacket((void*)respbuf, P_FE2CL_PC_ATTACK_CHARs_SUCC, resplen);
|
||||
@@ -724,7 +726,7 @@ static void projectileHit(CNSocket* sock, CNPacketData* data) {
|
||||
int difficulty = (int)mob->data["m_iNpcLevel"];
|
||||
damage = getDamage(damage.first, (int)mob->data["m_iProtection"], true, bullet->weaponBoost, Nanos::nanoStyle(plr->activeNano), (int)mob->data["m_iNpcStyle"], difficulty);
|
||||
|
||||
damage.first = hitMob(sock, mob, damage.first);
|
||||
damage.first = mob->takeDamage(sock, damage.first);
|
||||
|
||||
respdata[i].iID = mob->id;
|
||||
respdata[i].iDamage = damage.first;
|
||||
|
||||
Reference in New Issue
Block a user