2020-09-16 19:46:15 +00:00
|
|
|
#include "MobManager.hpp"
|
2020-08-27 20:16:52 +00:00
|
|
|
#include "PlayerManager.hpp"
|
|
|
|
#include "NPCManager.hpp"
|
2020-08-28 16:25:03 +00:00
|
|
|
#include "ItemManager.hpp"
|
2020-09-10 16:40:38 +00:00
|
|
|
#include "MissionManager.hpp"
|
2020-08-27 20:16:52 +00:00
|
|
|
|
2020-09-24 01:18:41 +00:00
|
|
|
#include <cmath>
|
2020-08-28 16:25:03 +00:00
|
|
|
#include <assert.h>
|
2020-08-27 20:16:52 +00:00
|
|
|
|
2020-09-16 23:43:48 +00:00
|
|
|
std::map<int32_t, Mob*> MobManager::Mobs;
|
|
|
|
|
2020-09-16 19:46:15 +00:00
|
|
|
void MobManager::init() {
|
2020-09-22 19:31:08 +00:00
|
|
|
REGISTER_SHARD_TIMER(step, 200);
|
|
|
|
|
2020-08-27 20:16:52 +00:00
|
|
|
REGISTER_SHARD_PACKET(P_CL2FE_REQ_PC_ATTACK_NPCs, pcAttackNpcs);
|
|
|
|
|
|
|
|
REGISTER_SHARD_PACKET(P_CL2FE_REQ_PC_COMBAT_BEGIN, combatBegin);
|
|
|
|
REGISTER_SHARD_PACKET(P_CL2FE_REQ_PC_COMBAT_END, combatEnd);
|
|
|
|
REGISTER_SHARD_PACKET(P_CL2FE_DOT_DAMAGE_ONOFF, dotDamageOnOff);
|
|
|
|
}
|
|
|
|
|
2020-09-16 19:46:15 +00:00
|
|
|
void MobManager::pcAttackNpcs(CNSocket *sock, CNPacketData *data) {
|
2020-08-27 20:16:52 +00:00
|
|
|
sP_CL2FE_REQ_PC_ATTACK_NPCs* pkt = (sP_CL2FE_REQ_PC_ATTACK_NPCs*)data->buf;
|
2020-08-28 20:01:49 +00:00
|
|
|
Player *plr = PlayerManager::getPlayer(sock);
|
2020-08-27 20:16:52 +00:00
|
|
|
|
2020-08-28 14:09:26 +00:00
|
|
|
// sanity check
|
|
|
|
if (!validInVarPacket(sizeof(sP_CL2FE_REQ_PC_ATTACK_NPCs), pkt->iNPCCnt, sizeof(int32_t), data->size)) {
|
|
|
|
std::cout << "[WARN] bad sP_CL2FE_REQ_PC_ATTACK_NPCs packet size\n";
|
2020-08-27 20:16:52 +00:00
|
|
|
return;
|
|
|
|
}
|
2020-08-28 14:09:26 +00:00
|
|
|
|
2020-08-27 20:16:52 +00:00
|
|
|
int32_t *pktdata = (int32_t*)((uint8_t*)data->buf + sizeof(sP_CL2FE_REQ_PC_ATTACK_NPCs));
|
|
|
|
|
2020-08-28 14:09:26 +00:00
|
|
|
/*
|
|
|
|
* Due to the possibility of multiplication overflow (and regular buffer overflow),
|
2020-08-28 19:42:00 +00:00
|
|
|
* both incoming and outgoing variable-length packets must be validated, at least if
|
|
|
|
* the number of trailing structs isn't well known (ie. it's from the client).
|
2020-08-28 14:09:26 +00:00
|
|
|
*/
|
|
|
|
if (!validOutVarPacket(sizeof(sP_FE2CL_PC_ATTACK_NPCs_SUCC), pkt->iNPCCnt, sizeof(sAttackResult))) {
|
2020-08-28 16:25:03 +00:00
|
|
|
std::cout << "[WARN] bad sP_FE2CL_PC_ATTACK_NPCs_SUCC packet size\n";
|
2020-08-28 14:09:26 +00:00
|
|
|
return;
|
|
|
|
}
|
2020-08-27 20:16:52 +00:00
|
|
|
|
|
|
|
// initialize response struct
|
|
|
|
size_t resplen = sizeof(sP_FE2CL_PC_ATTACK_NPCs_SUCC) + pkt->iNPCCnt * sizeof(sAttackResult);
|
2020-09-09 19:08:31 +00:00
|
|
|
uint8_t respbuf[CN_PACKET_BUFFER_SIZE];
|
2020-08-28 14:09:26 +00:00
|
|
|
|
2020-08-27 20:16:52 +00:00
|
|
|
memset(respbuf, 0, resplen);
|
2020-08-28 14:09:26 +00:00
|
|
|
|
2020-08-27 20:16:52 +00:00
|
|
|
sP_FE2CL_PC_ATTACK_NPCs_SUCC *resp = (sP_FE2CL_PC_ATTACK_NPCs_SUCC*)respbuf;
|
|
|
|
sAttackResult *respdata = (sAttackResult*)(respbuf+sizeof(sP_FE2CL_PC_ATTACK_NPCs_SUCC));
|
|
|
|
|
|
|
|
resp->iNPCCnt = pkt->iNPCCnt;
|
|
|
|
|
|
|
|
for (int i = 0; i < pkt->iNPCCnt; i++) {
|
2020-09-16 23:43:48 +00:00
|
|
|
if (Mobs.find(pktdata[i]) == Mobs.end()) {
|
2020-08-27 20:16:52 +00:00
|
|
|
// not sure how to best handle this
|
|
|
|
std::cout << "[WARN] pcAttackNpcs: mob ID not found" << std::endl;
|
|
|
|
return;
|
|
|
|
}
|
2020-09-16 23:43:48 +00:00
|
|
|
Mob *mob = Mobs[pktdata[i]];
|
2020-08-27 20:16:52 +00:00
|
|
|
|
2020-09-24 01:18:41 +00:00
|
|
|
// cannot kill mobs multiple times; cannot harm retreating mobs
|
|
|
|
if (mob->state != MobState::ROAMING && mob->state != MobState::COMBAT) {
|
|
|
|
respdata[i].iID = mob->appearanceData.iNPC_ID;
|
|
|
|
respdata[i].iHP = mob->appearanceData.iHP;
|
|
|
|
respdata[i].iHitFlag = 2; // probably still necessary
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mob->state == MobState::ROAMING) {
|
|
|
|
assert(mob->target == nullptr);
|
|
|
|
mob->target = sock;
|
|
|
|
mob->state = MobState::COMBAT;
|
|
|
|
mob->nextMovement = getTime();
|
2020-09-24 20:55:33 +00:00
|
|
|
//std::cout << "combat state\n";
|
2020-09-24 01:18:41 +00:00
|
|
|
}
|
|
|
|
|
2020-09-16 23:43:48 +00:00
|
|
|
mob->appearanceData.iHP -= 100;
|
2020-08-27 20:16:52 +00:00
|
|
|
|
2020-09-22 20:22:10 +00:00
|
|
|
// wake up sleeping monster
|
2020-09-24 01:18:41 +00:00
|
|
|
// TODO: remove client-side bit somehow
|
2020-09-22 20:22:10 +00:00
|
|
|
mob->appearanceData.iConditionBitFlag &= ~CSB_BIT_MEZ;
|
|
|
|
|
2020-09-16 23:43:48 +00:00
|
|
|
if (mob->appearanceData.iHP <= 0)
|
2020-09-24 01:18:41 +00:00
|
|
|
killMob(mob->target, mob);
|
2020-09-16 23:43:48 +00:00
|
|
|
|
|
|
|
respdata[i].iID = mob->appearanceData.iNPC_ID;
|
2020-08-27 20:16:52 +00:00
|
|
|
respdata[i].iDamage = 100;
|
2020-09-16 23:43:48 +00:00
|
|
|
respdata[i].iHP = mob->appearanceData.iHP;
|
2020-09-09 19:08:31 +00:00
|
|
|
respdata[i].iHitFlag = 2; // hitscan, not a rocket or a grenade
|
2020-08-27 20:16:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sock->sendPacket((void*)respbuf, P_FE2CL_PC_ATTACK_NPCs_SUCC, resplen);
|
2020-08-28 20:01:49 +00:00
|
|
|
|
2020-09-09 19:08:31 +00:00
|
|
|
// a bit of a hack: these are the same size, so we can reuse the response packet
|
2020-08-28 20:01:49 +00:00
|
|
|
assert(sizeof(sP_FE2CL_PC_ATTACK_NPCs_SUCC) == sizeof(sP_FE2CL_PC_ATTACK_NPCs));
|
|
|
|
sP_FE2CL_PC_ATTACK_NPCs *resp1 = (sP_FE2CL_PC_ATTACK_NPCs*)respbuf;
|
|
|
|
|
|
|
|
resp1->iPC_ID = plr->iID;
|
|
|
|
|
|
|
|
// send to other players
|
2020-09-17 22:45:43 +00:00
|
|
|
PlayerManager::sendToViewable(sock, (void*)respbuf, P_FE2CL_PC_ATTACK_NPCs, resplen);
|
2020-08-27 20:16:52 +00:00
|
|
|
}
|
|
|
|
|
2020-09-24 01:18:41 +00:00
|
|
|
void MobManager::npcAttackPc(Mob *mob) {
|
|
|
|
// player pointer has already been validated
|
|
|
|
Player *plr = PlayerManager::getPlayer(mob->target);
|
|
|
|
|
|
|
|
const size_t resplen = sizeof(sP_FE2CL_PC_ATTACK_NPCs_SUCC) + sizeof(sAttackResult);
|
|
|
|
assert(resplen < CN_PACKET_BUFFER_SIZE - 8);
|
|
|
|
uint8_t respbuf[CN_PACKET_BUFFER_SIZE];
|
|
|
|
memset(respbuf, 0, resplen);
|
|
|
|
|
|
|
|
sP_FE2CL_NPC_ATTACK_PCs *pkt = (sP_FE2CL_NPC_ATTACK_PCs*)respbuf;
|
|
|
|
sAttackResult *atk = (sAttackResult*)(respbuf + sizeof(sP_FE2CL_NPC_ATTACK_PCs));
|
|
|
|
|
|
|
|
plr->HP += (int)mob->data["m_iPower"]; // already negative
|
|
|
|
|
|
|
|
pkt->iNPC_ID = mob->appearanceData.iNPC_ID;
|
|
|
|
pkt->iPCCnt = 1;
|
|
|
|
|
|
|
|
atk->iID = plr->iID;
|
|
|
|
atk->iDamage = -(int)mob->data["m_iPower"];
|
|
|
|
atk->iHP = plr->HP;
|
|
|
|
atk->iHitFlag = 2;
|
|
|
|
|
|
|
|
mob->target->sendPacket((void*)respbuf, P_FE2CL_NPC_ATTACK_PCs, resplen);
|
|
|
|
PlayerManager::sendToViewable(mob->target, (void*)&pkt, P_FE2CL_NPC_ATTACK_PCs, resplen);
|
|
|
|
|
|
|
|
if (plr->HP <= 0) {
|
|
|
|
mob->target = nullptr;
|
|
|
|
mob->state = MobState::RETREAT;
|
2020-09-24 20:55:33 +00:00
|
|
|
//std::cout << "retreat state\n";
|
2020-09-24 01:18:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-16 19:46:15 +00:00
|
|
|
void MobManager::combatBegin(CNSocket *sock, CNPacketData *data) {} // stub
|
|
|
|
void MobManager::combatEnd(CNSocket *sock, CNPacketData *data) {} // stub
|
|
|
|
void MobManager::dotDamageOnOff(CNSocket *sock, CNPacketData *data) {} // stub
|
2020-08-27 20:16:52 +00:00
|
|
|
|
2020-09-16 19:46:15 +00:00
|
|
|
void MobManager::giveReward(CNSocket *sock) {
|
2020-08-27 20:16:52 +00:00
|
|
|
Player *plr = PlayerManager::getPlayer(sock);
|
|
|
|
|
2020-08-28 16:25:03 +00:00
|
|
|
const size_t resplen = sizeof(sP_FE2CL_REP_REWARD_ITEM) + sizeof(sItemReward);
|
2020-09-08 01:01:47 +00:00
|
|
|
assert(resplen < CN_PACKET_BUFFER_SIZE - 8);
|
2020-08-28 16:25:03 +00:00
|
|
|
// we know it's only one trailing struct, so we can skip full validation
|
|
|
|
|
|
|
|
uint8_t respbuf[resplen]; // not a variable length array, don't worry
|
|
|
|
sP_FE2CL_REP_REWARD_ITEM *reward = (sP_FE2CL_REP_REWARD_ITEM *)respbuf;
|
|
|
|
sItemReward *item = (sItemReward *)(respbuf + sizeof(sP_FE2CL_REP_REWARD_ITEM));
|
|
|
|
|
|
|
|
// don't forget to zero the buffer!
|
|
|
|
memset(respbuf, 0, resplen);
|
|
|
|
|
2020-08-27 20:16:52 +00:00
|
|
|
// update player
|
|
|
|
plr->money += 50;
|
2020-09-23 21:04:58 +00:00
|
|
|
MissionManager::updateFusionMatter(sock, 70);
|
2020-08-27 20:16:52 +00:00
|
|
|
|
2020-08-28 16:25:03 +00:00
|
|
|
// simple rewards
|
|
|
|
reward->m_iCandy = plr->money;
|
|
|
|
reward->m_iFusionMatter = plr->fusionmatter;
|
|
|
|
reward->iFatigue = 100; // prevents warning message
|
|
|
|
reward->iFatigue_Level = 1;
|
|
|
|
reward->iItemCnt = 1; // remember to update resplen if you change this
|
|
|
|
|
|
|
|
int slot = ItemManager::findFreeSlot(plr);
|
|
|
|
if (slot == -1) {
|
|
|
|
// no room for an item, but you still get FM and taros
|
2020-09-09 19:08:31 +00:00
|
|
|
reward->iItemCnt = 0;
|
2020-08-28 16:25:03 +00:00
|
|
|
sock->sendPacket((void*)respbuf, P_FE2CL_REP_REWARD_ITEM, sizeof(sP_FE2CL_REP_REWARD_ITEM));
|
|
|
|
} else {
|
|
|
|
// item reward
|
|
|
|
item->sItem.iType = 9;
|
|
|
|
item->sItem.iID = 1;
|
|
|
|
item->iSlotNum = slot;
|
|
|
|
item->eIL = 1; // Inventory Location. 1 means player inventory.
|
|
|
|
|
|
|
|
// update player
|
|
|
|
plr->Inven[slot] = item->sItem;
|
|
|
|
|
|
|
|
sock->sendPacket((void*)respbuf, P_FE2CL_REP_REWARD_ITEM, resplen);
|
|
|
|
}
|
2020-09-23 19:44:27 +00:00
|
|
|
|
2020-08-27 20:16:52 +00:00
|
|
|
}
|
2020-09-16 23:43:48 +00:00
|
|
|
|
|
|
|
void MobManager::killMob(CNSocket *sock, Mob *mob) {
|
|
|
|
mob->state = MobState::DEAD;
|
2020-09-24 01:18:41 +00:00
|
|
|
mob->target = nullptr;
|
2020-09-22 20:22:10 +00:00
|
|
|
mob->appearanceData.iConditionBitFlag = 0;
|
2020-09-16 23:43:48 +00:00
|
|
|
mob->killedTime = getTime(); // XXX: maybe introduce a shard-global time for each step?
|
|
|
|
|
2020-09-24 20:55:33 +00:00
|
|
|
//std::cout << "dead state mob " << mob->appearanceData.iNPC_ID << std::endl;
|
2020-09-16 23:43:48 +00:00
|
|
|
|
|
|
|
giveReward(sock);
|
|
|
|
MissionManager::mobKilled(sock, mob->appearanceData.iNPCType);
|
|
|
|
|
2020-09-21 22:21:43 +00:00
|
|
|
mob->despawned = false;
|
2020-09-16 23:43:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void MobManager::deadStep(Mob *mob, time_t currTime) {
|
2020-09-21 22:21:43 +00:00
|
|
|
auto chunk = ChunkManager::grabChunk(mob->appearanceData.iX, mob->appearanceData.iY);
|
|
|
|
auto chunks = ChunkManager::grabChunks(chunk);
|
|
|
|
|
|
|
|
// despawn the mob after a short delay
|
|
|
|
if (mob->killedTime != 0 && !mob->despawned && currTime - mob->killedTime > 2000) {
|
|
|
|
mob->despawned = true;
|
|
|
|
|
|
|
|
INITSTRUCT(sP_FE2CL_NPC_EXIT, pkt);
|
|
|
|
|
|
|
|
pkt.iNPC_ID = mob->appearanceData.iNPC_ID;
|
|
|
|
|
|
|
|
for (Chunk *chunk : chunks) {
|
|
|
|
for (CNSocket *s : chunk->players) {
|
|
|
|
s->sendPacket(&pkt, P_FE2CL_NPC_EXIT, sizeof(sP_FE2CL_NPC_EXIT));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-16 23:43:48 +00:00
|
|
|
if (mob->killedTime != 0 && currTime - mob->killedTime < mob->regenTime * 100)
|
|
|
|
return;
|
|
|
|
|
|
|
|
std::cout << "respawning mob " << mob->appearanceData.iNPC_ID << " with HP = " << mob->maxHealth << std::endl;
|
|
|
|
|
|
|
|
mob->appearanceData.iHP = mob->maxHealth;
|
|
|
|
mob->state = MobState::ROAMING;
|
2020-09-24 20:55:33 +00:00
|
|
|
//std::cout << "roaming state\n";
|
2020-09-16 23:43:48 +00:00
|
|
|
|
|
|
|
INITSTRUCT(sP_FE2CL_NPC_NEW, pkt);
|
|
|
|
|
|
|
|
pkt.NPCAppearanceData = mob->appearanceData;
|
|
|
|
|
2020-09-21 21:13:13 +00:00
|
|
|
// notify all nearby players
|
|
|
|
for (Chunk *chunk : chunks) {
|
|
|
|
for (CNSocket *s : chunk->players) {
|
|
|
|
s->sendPacket(&pkt, P_FE2CL_NPC_NEW, sizeof(sP_FE2CL_NPC_NEW));
|
|
|
|
}
|
2020-09-16 23:43:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-24 01:18:41 +00:00
|
|
|
void MobManager::combatStep(Mob *mob, time_t currTime) {
|
|
|
|
assert(mob->target != nullptr);
|
|
|
|
|
|
|
|
// sanity check: did the target player lose connection?
|
|
|
|
if (PlayerManager::players.find(mob->target) == PlayerManager::players.end()) {
|
|
|
|
mob->target = nullptr;
|
|
|
|
mob->state = MobState::RETREAT;
|
2020-09-24 20:55:33 +00:00
|
|
|
//std::cout << "RETREAT state\n";
|
2020-09-24 01:18:41 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
Player *plr = PlayerManager::getPlayer(mob->target);
|
|
|
|
|
|
|
|
// skip attack/move if stunned or asleep
|
|
|
|
if (mob->appearanceData.iConditionBitFlag & (CSB_BIT_STUN|CSB_BIT_MEZ))
|
|
|
|
return;
|
|
|
|
|
|
|
|
int distance = hypot(plr->x - mob->appearanceData.iX, plr->y - mob->appearanceData.iY);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If the mob is close enough to attack, do so. If not, get closer.
|
|
|
|
* No, I'm not 100% sure this is how it's supposed to work.
|
|
|
|
*/
|
|
|
|
if (distance <= (int)mob->data["m_iAtkRange"]) {
|
2020-09-24 20:55:33 +00:00
|
|
|
//std::cout << "attack logic\n";
|
2020-09-24 01:18:41 +00:00
|
|
|
// attack logic
|
|
|
|
if (mob->nextAttack == 0) {
|
|
|
|
mob->nextAttack = currTime + (int)mob->data["m_iInitalTime"] * 100; // I *think* this is what this is
|
|
|
|
npcAttackPc(mob);
|
|
|
|
} else if (mob->nextAttack != 0 && currTime >= mob->nextAttack) {
|
|
|
|
mob->nextAttack = currTime + (int)mob->data["m_iDelayTime"] * 100;
|
|
|
|
npcAttackPc(mob);
|
|
|
|
}
|
|
|
|
} else {
|
2020-09-24 20:55:33 +00:00
|
|
|
//std::cout << "movement logic\n";
|
|
|
|
//std::cout << "distance: " << distance << " attack range: " << mob->data["m_iAtkRange"] << std::endl;
|
2020-09-24 01:18:41 +00:00
|
|
|
// movement logic
|
|
|
|
if (mob->nextMovement != 0 && currTime < mob->nextMovement)
|
|
|
|
return;
|
|
|
|
mob->nextMovement = currTime + (int)mob->data["m_iDelayTime"] * 100;
|
|
|
|
|
2020-09-24 20:55:33 +00:00
|
|
|
int speed = mob->data["m_iRunSpeed"];
|
|
|
|
|
|
|
|
// halve movement speed if snared
|
|
|
|
if (mob->appearanceData.iConditionBitFlag & CSB_BIT_DN_MOVE_SPEED)
|
|
|
|
speed /= 2;
|
|
|
|
|
|
|
|
auto targ = lerp(mob->appearanceData.iX, mob->appearanceData.iY, mob->target->plr->x, mob->target->plr->y, speed);
|
|
|
|
|
2020-09-24 01:18:41 +00:00
|
|
|
INITSTRUCT(sP_FE2CL_NPC_MOVE, pkt);
|
|
|
|
|
|
|
|
pkt.iNPC_ID = mob->appearanceData.iNPC_ID;
|
2020-09-24 20:55:33 +00:00
|
|
|
pkt.iSpeed = speed;
|
|
|
|
pkt.iToX = mob->appearanceData.iX = targ.first;
|
|
|
|
pkt.iToY = mob->appearanceData.iY = targ.second;
|
|
|
|
pkt.iToZ = mob->appearanceData.iZ;
|
2020-09-24 01:18:41 +00:00
|
|
|
|
|
|
|
auto chunk = ChunkManager::grabChunk(mob->appearanceData.iX, mob->appearanceData.iY);
|
|
|
|
auto chunks = ChunkManager::grabChunks(chunk);
|
|
|
|
|
|
|
|
// notify all nearby players
|
|
|
|
for (Chunk *chunk : chunks) {
|
|
|
|
for (CNSocket *s : chunk->players) {
|
|
|
|
s->sendPacket(&pkt, P_FE2CL_NPC_MOVE, sizeof(sP_FE2CL_NPC_MOVE));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-24 20:55:33 +00:00
|
|
|
// retreat if kited too far
|
|
|
|
distance = hypot(mob->appearanceData.iX - mob->spawnX, mob->appearanceData.iY - mob->spawnY);
|
|
|
|
if (distance >= mob->data["m_iCombatRange"]) {
|
|
|
|
mob->target = nullptr;
|
|
|
|
mob->state = MobState::RETREAT;
|
|
|
|
//std::cout << "retreat state\n";
|
|
|
|
}
|
2020-09-24 01:18:41 +00:00
|
|
|
}
|
|
|
|
|
2020-09-24 20:55:33 +00:00
|
|
|
/*
|
|
|
|
* TODO: precalculate a path, lerp through it, repeat.
|
|
|
|
* The way it works right now, mobs only move around a little bit. This will result
|
|
|
|
* in more natural movement, and will mesh well with pre-calculated paths (for Don Doom,
|
|
|
|
* Bad Max, etc.) once those have been made.
|
|
|
|
*/
|
2020-09-22 18:33:10 +00:00
|
|
|
void MobManager::roamingStep(Mob *mob, time_t currTime) {
|
2020-09-24 20:55:33 +00:00
|
|
|
// some mobs don't move (and we mustn't divide/modulus by zero)
|
|
|
|
if (mob->idleRange == 0)
|
|
|
|
return;
|
|
|
|
|
2020-09-22 18:33:10 +00:00
|
|
|
if (mob->nextMovement != 0 && currTime < mob->nextMovement)
|
|
|
|
return;
|
|
|
|
|
|
|
|
int delay = (int)mob->data["m_iDelayTime"] * 1000;
|
|
|
|
mob->nextMovement = currTime + delay/2 + rand() % (delay/2);
|
|
|
|
|
2020-09-22 20:22:10 +00:00
|
|
|
// skip move if stunned or asleep
|
|
|
|
if (mob->appearanceData.iConditionBitFlag & (CSB_BIT_STUN|CSB_BIT_MEZ))
|
|
|
|
return;
|
|
|
|
|
2020-09-22 18:33:10 +00:00
|
|
|
INITSTRUCT(sP_FE2CL_NPC_MOVE, pkt);
|
|
|
|
int xStart = mob->spawnX - mob->idleRange/2;
|
|
|
|
int yStart = mob->spawnY - mob->idleRange/2;
|
|
|
|
|
2020-09-24 20:55:33 +00:00
|
|
|
int farX = xStart + rand() % mob->idleRange;
|
|
|
|
int farY = yStart + rand() % mob->idleRange;
|
|
|
|
int speed = mob->data["m_iWalkSpeed"];
|
|
|
|
|
|
|
|
// halve movement speed if snared
|
|
|
|
if (mob->appearanceData.iConditionBitFlag & CSB_BIT_DN_MOVE_SPEED)
|
|
|
|
speed /= 2;
|
|
|
|
|
|
|
|
auto targ = lerp(mob->appearanceData.iX, mob->appearanceData.iY, farX, farY, speed);
|
|
|
|
|
2020-09-22 18:33:10 +00:00
|
|
|
pkt.iNPC_ID = mob->appearanceData.iNPC_ID;
|
2020-09-24 20:55:33 +00:00
|
|
|
pkt.iSpeed = speed;
|
|
|
|
pkt.iToX = mob->appearanceData.iX = targ.first;
|
|
|
|
pkt.iToY = mob->appearanceData.iY = targ.second;
|
2020-09-22 18:33:10 +00:00
|
|
|
pkt.iToZ = mob->appearanceData.iZ;
|
|
|
|
|
|
|
|
auto chunk = ChunkManager::grabChunk(mob->appearanceData.iX, mob->appearanceData.iY);
|
|
|
|
auto chunks = ChunkManager::grabChunks(chunk);
|
|
|
|
|
|
|
|
// notify all nearby players
|
|
|
|
for (Chunk *chunk : chunks) {
|
|
|
|
for (CNSocket *s : chunk->players) {
|
|
|
|
s->sendPacket(&pkt, P_FE2CL_NPC_MOVE, sizeof(sP_FE2CL_NPC_MOVE));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-24 01:18:41 +00:00
|
|
|
void MobManager::retreatStep(Mob *mob, time_t currTime) {
|
|
|
|
// distance between spawn point and current location
|
|
|
|
int distance = hypot(mob->appearanceData.iX - mob->spawnX, mob->appearanceData.iY - mob->spawnY);
|
|
|
|
|
|
|
|
if (distance > mob->data["m_iIdleRange"]) {
|
|
|
|
INITSTRUCT(sP_FE2CL_NPC_MOVE, pkt);
|
|
|
|
|
|
|
|
auto chunk = ChunkManager::grabChunk(mob->appearanceData.iX, mob->appearanceData.iY);
|
|
|
|
auto chunks = ChunkManager::grabChunks(chunk);
|
|
|
|
|
2020-09-24 20:55:33 +00:00
|
|
|
auto targ = lerp(mob->appearanceData.iX, mob->appearanceData.iY, mob->spawnX, mob->spawnY, mob->data["m_iRunSpeed"]);
|
|
|
|
|
2020-09-24 01:18:41 +00:00
|
|
|
pkt.iNPC_ID = mob->appearanceData.iNPC_ID;
|
2020-09-24 20:55:33 +00:00
|
|
|
pkt.iSpeed = mob->data["m_iRunSpeed"];
|
|
|
|
pkt.iToX = mob->appearanceData.iX = targ.first;
|
|
|
|
pkt.iToY = mob->appearanceData.iY = targ.second;
|
2020-09-24 01:18:41 +00:00
|
|
|
pkt.iToZ = mob->appearanceData.iZ;
|
|
|
|
|
|
|
|
// notify all nearby players
|
|
|
|
for (Chunk *chunk : chunks) {
|
|
|
|
for (CNSocket *s : chunk->players) {
|
|
|
|
s->sendPacket(&pkt, P_FE2CL_NPC_MOVE, sizeof(sP_FE2CL_NPC_MOVE));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// if we got there
|
|
|
|
if (distance <= mob->data["m_iIdleRange"]) {
|
|
|
|
mob->state = MobState::ROAMING;
|
2020-09-24 20:55:33 +00:00
|
|
|
//std::cout << "roaming state\n";
|
2020-09-24 01:18:41 +00:00
|
|
|
mob->appearanceData.iHP = mob->maxHealth;
|
|
|
|
mob->killedTime = 0;
|
|
|
|
mob->nextAttack = 0;
|
|
|
|
mob->appearanceData.iConditionBitFlag = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-22 19:31:08 +00:00
|
|
|
void MobManager::step(CNServer *serv, time_t currTime) {
|
2020-09-16 23:43:48 +00:00
|
|
|
for (auto& pair : Mobs) {
|
2020-09-22 18:33:10 +00:00
|
|
|
int x = pair.second->appearanceData.iX;
|
|
|
|
int y = pair.second->appearanceData.iY;
|
|
|
|
|
|
|
|
// skip chunks without players
|
|
|
|
if (!ChunkManager::inPopulatedChunks(x, y))
|
|
|
|
continue;
|
|
|
|
|
2020-09-22 18:53:44 +00:00
|
|
|
// skip mob movement and combat if disabled
|
|
|
|
if (!settings::SIMULATEMOBS && pair.second->state != MobState::DEAD)
|
|
|
|
continue;
|
|
|
|
|
2020-09-16 23:43:48 +00:00
|
|
|
switch (pair.second->state) {
|
2020-09-24 20:55:33 +00:00
|
|
|
case MobState::INACTIVE:
|
|
|
|
// no-op
|
|
|
|
break;
|
2020-09-22 18:33:10 +00:00
|
|
|
case MobState::ROAMING:
|
|
|
|
roamingStep(pair.second, currTime);
|
|
|
|
break;
|
2020-09-24 01:18:41 +00:00
|
|
|
case MobState::COMBAT:
|
|
|
|
combatStep(pair.second, currTime);
|
|
|
|
break;
|
|
|
|
case MobState::RETREAT:
|
|
|
|
retreatStep(pair.second, currTime);
|
|
|
|
break;
|
2020-09-16 23:43:48 +00:00
|
|
|
case MobState::DEAD:
|
|
|
|
deadStep(pair.second, currTime);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-09-24 20:55:33 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Dynamic lerp; distinct from TransportManager::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.
|
|
|
|
*/
|
|
|
|
std::pair<int,int> MobManager::lerp(int x1, int y1, int x2, int y2, int speed) {
|
|
|
|
std::pair<int,int> ret = {};
|
|
|
|
|
|
|
|
int distance = hypot(x1 - x2, y1 - y2);
|
|
|
|
int lerps = distance / speed;
|
|
|
|
|
|
|
|
// interpolate only the first point
|
|
|
|
float frac = 1.0f / (lerps+1);
|
|
|
|
ret.first = (x1 * (1.0f - frac)) + (x2 * frac);
|
|
|
|
ret.second = (y1 * (1.0f - frac)) + (y2 * frac);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|