diff --git a/src/ChunkManager.cpp b/src/ChunkManager.cpp index b350b8b..9e65da5 100644 --- a/src/ChunkManager.cpp +++ b/src/ChunkManager.cpp @@ -172,6 +172,7 @@ void ChunkManager::addPlayerToChunks(std::set chnks, CNSocket* sock) { // add npcs for (int32_t id : chunk->NPCs) { BaseNPC* npc = NPCManager::NPCs[id]; + npc->playersInView++; if (npc->appearanceData.iHP <= 0) continue; @@ -248,6 +249,7 @@ void ChunkManager::addNPCToChunks(std::set chnks, int32_t id) { for (CNSocket* sock : chunk->players) { // send to socket sock->sendPacket((void*)&enterBusData, P_FE2CL_TRANSPORTATION_ENTER, sizeof(sP_FE2CL_TRANSPORTATION_ENTER)); + npc->playersInView++; } } break; @@ -259,6 +261,7 @@ void ChunkManager::addNPCToChunks(std::set chnks, int32_t id) { for (CNSocket* sock : chunk->players) { // send to socket sock->sendPacket((void*)&enterEggData, P_FE2CL_SHINY_ENTER, sizeof(sP_FE2CL_SHINY_ENTER)); + npc->playersInView++; } } break; @@ -271,6 +274,7 @@ void ChunkManager::addNPCToChunks(std::set chnks, int32_t id) { for (CNSocket* sock : chunk->players) { // send to socket sock->sendPacket((void*)&enterData, P_FE2CL_NPC_ENTER, sizeof(sP_FE2CL_NPC_ENTER)); + npc->playersInView++; } } break; @@ -286,6 +290,8 @@ void ChunkManager::removePlayerFromChunks(std::set chnks, CNSocket* sock // remove NPCs from view for (int32_t id : chunk->NPCs) { BaseNPC* npc = NPCManager::NPCs[id]; + npc->playersInView--; + switch (npc->npcClass) { case NPC_BUS: INITSTRUCT(sP_FE2CL_TRANSPORTATION_EXIT, exitBusData); @@ -332,6 +338,7 @@ void ChunkManager::removeNPCFromChunks(std::set chnks, int32_t id) { for (CNSocket* sock : chunk->players) { // send to socket sock->sendPacket((void*)&exitBusData, P_FE2CL_TRANSPORTATION_EXIT, sizeof(sP_FE2CL_TRANSPORTATION_EXIT)); + npc->playersInView--; } } break; @@ -343,6 +350,7 @@ void ChunkManager::removeNPCFromChunks(std::set chnks, int32_t id) { for (CNSocket* sock : chunk->players) { // send to socket sock->sendPacket((void*)&exitEggData, P_FE2CL_SHINY_EXIT, sizeof(sP_FE2CL_SHINY_EXIT)); + npc->playersInView--; } } break; @@ -356,6 +364,7 @@ void ChunkManager::removeNPCFromChunks(std::set chnks, int32_t id) { for (CNSocket* sock : chunk->players) { // send to socket sock->sendPacket((void*)&exitData, P_FE2CL_NPC_EXIT, sizeof(sP_FE2CL_NPC_EXIT)); + npc->playersInView--; } } break; @@ -427,6 +436,9 @@ std::vector ChunkManager::getChunksInMap(uint64_t mapNum) { return chnks; } +/* + * Used only for eggs; use npc->playersInView for everything visible + */ bool ChunkManager::inPopulatedChunks(std::set* chnks) { for (auto it = chnks->begin(); it != chnks->end(); it++) { diff --git a/src/MobManager.cpp b/src/MobManager.cpp index 51f2134..a032cce 100644 --- a/src/MobManager.cpp +++ b/src/MobManager.cpp @@ -691,9 +691,12 @@ void MobManager::step(CNServer *serv, time_t currTime) { for (auto& pair : Mobs) { // skip chunks without players - if (!ChunkManager::inPopulatedChunks(pair.second->viewableChunks)) + if (pair.second->playersInView == 0) //(!ChunkManager::inPopulatedChunks(pair.second->viewableChunks)) continue; + if (pair.second->playersInView < 0) + std::cout << "[WARN] Weird playerview value " << pair.second->playersInView << std::endl; + // skip mob movement and combat if disabled if (!simulateMobs && pair.second->state != MobState::DEAD && pair.second->state != MobState::RETREAT) diff --git a/src/NPC.hpp b/src/NPC.hpp index 38d6b9d..c5c7b67 100644 --- a/src/NPC.hpp +++ b/src/NPC.hpp @@ -11,6 +11,8 @@ public: ChunkPos chunkPos; std::set* viewableChunks; + int playersInView; + BaseNPC() {}; BaseNPC(int x, int y, int z, int angle, uint64_t iID, int type, int id) { appearanceData.iX = x; @@ -27,6 +29,7 @@ public: chunkPos = std::make_tuple(0, 0, 0); viewableChunks = new std::set(); + playersInView = 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;