diff --git a/src/ChunkManager.cpp b/src/ChunkManager.cpp index 79ce826..6728909 100644 --- a/src/ChunkManager.cpp +++ b/src/ChunkManager.cpp @@ -51,6 +51,8 @@ void ChunkManager::updatePlayerChunk(CNSocket* sock, ChunkPos from, ChunkPos to) // update views removePlayerFromChunks(toExit, sock); addPlayerToChunks(toEnter, sock); + + plr->chunkPos = to; // update cached chunk position } void ChunkManager::updateNPCChunk(int32_t id, ChunkPos from, ChunkPos to) { @@ -82,6 +84,8 @@ void ChunkManager::updateNPCChunk(int32_t id, ChunkPos from, ChunkPos to) { // update views removeNPCFromChunks(toExit, id); addNPCToChunks(toEnter, id); + + npc->chunkPos = to; } void ChunkManager::trackPlayer(ChunkPos chunkPos, CNSocket* sock) { @@ -424,7 +428,7 @@ void ChunkManager::createInstance(uint64_t instanceID) { NPCManager::updateNPCPosition(newID, baseNPC->appearanceData.iX, baseNPC->appearanceData.iY, baseNPC->appearanceData.iZ, instanceID, baseNPC->appearanceData.iAngle); // force chunk update - updateNPCChunk(newID, {0, 0, 0}, chunkPosAt(baseNPC->appearanceData.iX, baseNPC->appearanceData.iY, instanceID)); + updateNPCChunk(newID, std::make_tuple(0, 0, 0), chunkPosAt(baseNPC->appearanceData.iX, baseNPC->appearanceData.iY, instanceID)); } } } else { diff --git a/src/MobManager.cpp b/src/MobManager.cpp index dab4a3c..cdd5c79 100644 --- a/src/MobManager.cpp +++ b/src/MobManager.cpp @@ -1115,7 +1115,7 @@ bool MobManager::aggroCheck(Mob *mob, time_t currTime) { CNSocket *closest = nullptr; int closestDistance = INT_MAX; - std::set chunks = ChunkManager::getViewableChunks(ChunkManager::chunkPosAt(mob->appearanceData.iX, mob->appearanceData.iY, mob->instanceID)); + std::set chunks = ChunkManager::getViewableChunks(mob->chunkPos); for (Chunk *chunk : chunks) { for (CNSocket *s : chunk->players) { Player *plr = s->plr; diff --git a/src/NPC.hpp b/src/NPC.hpp index b73a215..080b06f 100644 --- a/src/NPC.hpp +++ b/src/NPC.hpp @@ -9,7 +9,6 @@ public: NPCClass npcClass; uint64_t instanceID; ChunkPos chunkPos; - std::vector currentChunks; BaseNPC() {}; BaseNPC(int x, int y, int z, int angle, uint64_t iID, int type, int id) { @@ -24,6 +23,8 @@ public: appearanceData.iNPC_ID = id; instanceID = iID; + + chunkPos = std::make_tuple(0, 0, 0); }; BaseNPC(int x, int y, int z, int angle, uint64_t iID, int type, int id, NPCClass classType) : BaseNPC(x, y, z, angle, iID, type, id) { npcClass = classType; diff --git a/src/NPCManager.cpp b/src/NPCManager.cpp index 6fe202e..a6862ca 100644 --- a/src/NPCManager.cpp +++ b/src/NPCManager.cpp @@ -62,19 +62,18 @@ void NPCManager::destroyNPC(int32_t id) { } BaseNPC* entity = NPCs[id]; - ChunkPos chunkPos = ChunkManager::chunkPosAt(entity->appearanceData.iX, entity->appearanceData.iY, entity->instanceID); // sanity check - if (!ChunkManager::chunkExists(chunkPos)) { + if (!ChunkManager::chunkExists(entity->chunkPos)) { std::cout << "chunk not found!" << std::endl; return; } // remove NPC from the chunk - ChunkManager::untrackNPC(chunkPos, id); + ChunkManager::untrackNPC(entity->chunkPos, id); // remove from viewable chunks - ChunkManager::removeNPCFromChunks(ChunkManager::getViewableChunks(chunkPos), id); + ChunkManager::removeNPCFromChunks(ChunkManager::getViewableChunks(entity->chunkPos), id); // remove from mob manager if (MobManager::Mobs.find(id) != MobManager::Mobs.end()) @@ -92,7 +91,7 @@ void NPCManager::destroyNPC(int32_t id) { void NPCManager::updateNPCPosition(int32_t id, int X, int Y, int Z, uint64_t I, int angle) { BaseNPC* npc = NPCs[id]; npc->appearanceData.iAngle = angle; - ChunkPos oldChunk = ChunkManager::chunkPosAt(npc->appearanceData.iX, npc->appearanceData.iY, npc->instanceID); + ChunkPos oldChunk = npc->chunkPos; ChunkPos newChunk = ChunkManager::chunkPosAt(X, Y, I); npc->appearanceData.iX = X; npc->appearanceData.iY = Y; @@ -104,7 +103,7 @@ void NPCManager::updateNPCPosition(int32_t id, int X, int Y, int Z, uint64_t I, } void NPCManager::sendToViewable(BaseNPC *npc, void *buf, uint32_t type, size_t size) { - std::set chunks = ChunkManager::getViewableChunks(ChunkManager::chunkPosAt(npc->appearanceData.iX, npc->appearanceData.iY, npc->instanceID)); + std::set chunks = ChunkManager::getViewableChunks(npc->chunkPos); for (Chunk *chunk : chunks) { for (CNSocket *s : chunk->players) { s->sendPacket(buf, type, size); @@ -485,7 +484,7 @@ void NPCManager::npcSummonHandler(CNSocket* sock, CNPacketData* data) { updateNPCPosition(id, plr->x, plr->y, plr->z, plr->instanceID, 0); // force chunk update - ChunkManager::updateNPCChunk(id, { 0, 0, 0 }, ChunkManager::chunkPosAt(plr->x, plr->y, plr->instanceID)); + ChunkManager::updateNPCChunk(id, std::make_tuple(0, 0, 0), ChunkManager::chunkPosAt(plr->x, plr->y, plr->instanceID)); } } @@ -727,7 +726,7 @@ void NPCManager::eggStep(CNServer* serv, time_t currTime) { egg.second->deadUntil = 0; egg.second->appearanceData.iHP = 400; - ChunkManager::addNPCToChunks(ChunkManager::getViewableChunks(ChunkManager::chunkPosAt(egg.second->appearanceData.iX, egg.second->appearanceData.iY, egg.second->instanceID)), + ChunkManager::addNPCToChunks(ChunkManager::getViewableChunks(egg.second->chunkPos), egg.first); } } @@ -896,8 +895,7 @@ void NPCManager::eggPickup(CNSocket* sock, CNPacketData* data) { if (egg->summoned) destroyNPC(eggId); else { - ChunkManager::removeNPCFromChunks(ChunkManager::getViewableChunks(ChunkManager::chunkPosAt(egg->appearanceData.iX, egg->appearanceData.iY, egg->instanceID)), - eggId); + ChunkManager::removeNPCFromChunks(ChunkManager::getViewableChunks(egg->chunkPos), eggId); egg->dead = true; egg->deadUntil = getTime() + (time_t)type->regen * 1000; egg->appearanceData.iHP = 0; diff --git a/src/Player.hpp b/src/Player.hpp index 0060347..a78a027 100644 --- a/src/Player.hpp +++ b/src/Player.hpp @@ -77,7 +77,6 @@ struct Player { bool buddiesSynced; int64_t buddyIDs[50]; - //ChunkPos chunkPos; - std::set* currentChunks; + ChunkPos chunkPos; time_t lastHeartbeat; }; diff --git a/src/PlayerManager.cpp b/src/PlayerManager.cpp index 45076cf..ba9745f 100644 --- a/src/PlayerManager.cpp +++ b/src/PlayerManager.cpp @@ -53,8 +53,7 @@ void PlayerManager::addPlayer(CNSocket* key, Player plr) { memcpy(p, &plr, sizeof(Player)); players[key] = p; - //p->chunkPos = std::make_tuple(0, 0, 0); - p->currentChunks = new std::set(); + p->chunkPos = std::make_tuple(0, 0, 0); p->lastHeartbeat = 0; key->plr = p; @@ -75,11 +74,9 @@ void PlayerManager::removePlayer(CNSocket* key) { // save player to DB Database::updatePlayer(plr); - ChunkPos chunkPos = ChunkManager::chunkPosAt(plr->x, plr->y, plr->instanceID); - // remove player visually and untrack - ChunkManager::removePlayerFromChunks(ChunkManager::getViewableChunks(chunkPos), key); - ChunkManager::untrackPlayer(chunkPos, key); + ChunkManager::removePlayerFromChunks(ChunkManager::getViewableChunks(plr->chunkPos), key); + ChunkManager::untrackPlayer(plr->chunkPos, key); std::cout << getPlayerName(key->plr) << " has left!" << std::endl; @@ -106,7 +103,7 @@ void PlayerManager::removePlayer(CNSocket* key) { void PlayerManager::updatePlayerPosition(CNSocket* sock, int X, int Y, int Z, uint64_t I, int angle) { Player* plr = getPlayer(sock); plr->angle = angle; - ChunkPos oldChunk = ChunkManager::chunkPosAt(plr->x, plr->y, plr->instanceID); + ChunkPos oldChunk = plr->chunkPos; ChunkPos newChunk = ChunkManager::chunkPosAt(X, Y, I); plr->x = X; plr->y = Y; @@ -294,7 +291,7 @@ void PlayerManager::enterPlayer(CNSocket* sock, CNPacketData* data) { void PlayerManager::sendToViewable(CNSocket* sock, void* buf, uint32_t type, size_t size) { Player* plr = getPlayer(sock); - std::set chunks = ChunkManager::getViewableChunks(ChunkManager::chunkPosAt(plr->x, plr->y, plr->instanceID)); + std::set chunks = ChunkManager::getViewableChunks(plr->chunkPos); for (Chunk* chunk : chunks) { for (CNSocket* otherSock : chunk->players) { if (otherSock == sock) @@ -322,7 +319,7 @@ void PlayerManager::loadPlayer(CNSocket* sock, CNPacketData* data) { // reload players & NPCs updatePlayerPosition(sock, plr->x, plr->y, plr->z, plr->instanceID, plr->angle); - ChunkManager::updatePlayerChunk(sock, { 0, 0, 0 }, ChunkManager::chunkPosAt(plr->x, plr->y, plr->instanceID)); + ChunkManager::updatePlayerChunk(sock, std::make_tuple(0, 0, 0), ChunkManager::chunkPosAt(plr->x, plr->y, plr->instanceID)); sock->sendPacket((void*)&response, P_FE2CL_REP_PC_LOADING_COMPLETE_SUCC, sizeof(sP_FE2CL_REP_PC_LOADING_COMPLETE_SUCC)); } diff --git a/src/TableData.cpp b/src/TableData.cpp index b8550c5..764e3fe 100644 --- a/src/TableData.cpp +++ b/src/TableData.cpp @@ -44,7 +44,7 @@ void TableData::init() { NPCManager::NPCs[nextId] = tmp; NPCManager::updateNPCPosition(nextId, npc["x"], npc["y"], npc["z"], instanceID, npc["angle"]); - ChunkManager::updateNPCChunk(nextId, {0, 0, 0}, ChunkManager::chunkPosAt(npc["x"], npc["y"], instanceID)); + ChunkManager::updateNPCChunk(nextId, std::make_tuple(0, 0, 0), ChunkManager::chunkPosAt(npc["x"], npc["y"], instanceID)); nextId++; if (npc["id"] == 641 || npc["id"] == 642) @@ -212,7 +212,7 @@ void TableData::init() { NPCManager::NPCs[nextId] = tmp; MobManager::Mobs[nextId] = (Mob*)NPCManager::NPCs[nextId]; NPCManager::updateNPCPosition(nextId, npc["iX"], npc["iY"], npc["iZ"], instanceID, npc["iAngle"]); - ChunkManager::updateNPCChunk(nextId, { 0, 0, 0 }, ChunkManager::chunkPosAt(npc["iX"], npc["iY"], instanceID)); + ChunkManager::updateNPCChunk(nextId, std::make_tuple(0, 0, 0), ChunkManager::chunkPosAt(npc["iX"], npc["iY"], instanceID)); nextId++; }