Replace inPopulatedChunks with player counter for NPCs

This commit is contained in:
Gent S 2020-11-22 15:24:34 -05:00
parent 71d4f331b5
commit d1baa0d9f9
3 changed files with 19 additions and 1 deletions

View File

@ -172,6 +172,7 @@ void ChunkManager::addPlayerToChunks(std::set<Chunk*> chnks, CNSocket* sock) {
// add npcs // add npcs
for (int32_t id : chunk->NPCs) { for (int32_t id : chunk->NPCs) {
BaseNPC* npc = NPCManager::NPCs[id]; BaseNPC* npc = NPCManager::NPCs[id];
npc->playersInView++;
if (npc->appearanceData.iHP <= 0) if (npc->appearanceData.iHP <= 0)
continue; continue;
@ -248,6 +249,7 @@ void ChunkManager::addNPCToChunks(std::set<Chunk*> chnks, int32_t id) {
for (CNSocket* sock : chunk->players) { for (CNSocket* sock : chunk->players) {
// send to socket // send to socket
sock->sendPacket((void*)&enterBusData, P_FE2CL_TRANSPORTATION_ENTER, sizeof(sP_FE2CL_TRANSPORTATION_ENTER)); sock->sendPacket((void*)&enterBusData, P_FE2CL_TRANSPORTATION_ENTER, sizeof(sP_FE2CL_TRANSPORTATION_ENTER));
npc->playersInView++;
} }
} }
break; break;
@ -259,6 +261,7 @@ void ChunkManager::addNPCToChunks(std::set<Chunk*> chnks, int32_t id) {
for (CNSocket* sock : chunk->players) { for (CNSocket* sock : chunk->players) {
// send to socket // send to socket
sock->sendPacket((void*)&enterEggData, P_FE2CL_SHINY_ENTER, sizeof(sP_FE2CL_SHINY_ENTER)); sock->sendPacket((void*)&enterEggData, P_FE2CL_SHINY_ENTER, sizeof(sP_FE2CL_SHINY_ENTER));
npc->playersInView++;
} }
} }
break; break;
@ -271,6 +274,7 @@ void ChunkManager::addNPCToChunks(std::set<Chunk*> chnks, int32_t id) {
for (CNSocket* sock : chunk->players) { for (CNSocket* sock : chunk->players) {
// send to socket // send to socket
sock->sendPacket((void*)&enterData, P_FE2CL_NPC_ENTER, sizeof(sP_FE2CL_NPC_ENTER)); sock->sendPacket((void*)&enterData, P_FE2CL_NPC_ENTER, sizeof(sP_FE2CL_NPC_ENTER));
npc->playersInView++;
} }
} }
break; break;
@ -286,6 +290,8 @@ void ChunkManager::removePlayerFromChunks(std::set<Chunk*> chnks, CNSocket* sock
// remove NPCs from view // remove NPCs from view
for (int32_t id : chunk->NPCs) { for (int32_t id : chunk->NPCs) {
BaseNPC* npc = NPCManager::NPCs[id]; BaseNPC* npc = NPCManager::NPCs[id];
npc->playersInView--;
switch (npc->npcClass) { switch (npc->npcClass) {
case NPC_BUS: case NPC_BUS:
INITSTRUCT(sP_FE2CL_TRANSPORTATION_EXIT, exitBusData); INITSTRUCT(sP_FE2CL_TRANSPORTATION_EXIT, exitBusData);
@ -332,6 +338,7 @@ void ChunkManager::removeNPCFromChunks(std::set<Chunk*> chnks, int32_t id) {
for (CNSocket* sock : chunk->players) { for (CNSocket* sock : chunk->players) {
// send to socket // send to socket
sock->sendPacket((void*)&exitBusData, P_FE2CL_TRANSPORTATION_EXIT, sizeof(sP_FE2CL_TRANSPORTATION_EXIT)); sock->sendPacket((void*)&exitBusData, P_FE2CL_TRANSPORTATION_EXIT, sizeof(sP_FE2CL_TRANSPORTATION_EXIT));
npc->playersInView--;
} }
} }
break; break;
@ -343,6 +350,7 @@ void ChunkManager::removeNPCFromChunks(std::set<Chunk*> chnks, int32_t id) {
for (CNSocket* sock : chunk->players) { for (CNSocket* sock : chunk->players) {
// send to socket // send to socket
sock->sendPacket((void*)&exitEggData, P_FE2CL_SHINY_EXIT, sizeof(sP_FE2CL_SHINY_EXIT)); sock->sendPacket((void*)&exitEggData, P_FE2CL_SHINY_EXIT, sizeof(sP_FE2CL_SHINY_EXIT));
npc->playersInView--;
} }
} }
break; break;
@ -356,6 +364,7 @@ void ChunkManager::removeNPCFromChunks(std::set<Chunk*> chnks, int32_t id) {
for (CNSocket* sock : chunk->players) { for (CNSocket* sock : chunk->players) {
// send to socket // send to socket
sock->sendPacket((void*)&exitData, P_FE2CL_NPC_EXIT, sizeof(sP_FE2CL_NPC_EXIT)); sock->sendPacket((void*)&exitData, P_FE2CL_NPC_EXIT, sizeof(sP_FE2CL_NPC_EXIT));
npc->playersInView--;
} }
} }
break; break;
@ -427,6 +436,9 @@ std::vector<ChunkPos> ChunkManager::getChunksInMap(uint64_t mapNum) {
return chnks; return chnks;
} }
/*
* Used only for eggs; use npc->playersInView for everything visible
*/
bool ChunkManager::inPopulatedChunks(std::set<Chunk*>* chnks) { bool ChunkManager::inPopulatedChunks(std::set<Chunk*>* chnks) {
for (auto it = chnks->begin(); it != chnks->end(); it++) { for (auto it = chnks->begin(); it != chnks->end(); it++) {

View File

@ -691,9 +691,12 @@ void MobManager::step(CNServer *serv, time_t currTime) {
for (auto& pair : Mobs) { for (auto& pair : Mobs) {
// skip chunks without players // skip chunks without players
if (!ChunkManager::inPopulatedChunks(pair.second->viewableChunks)) if (pair.second->playersInView == 0) //(!ChunkManager::inPopulatedChunks(pair.second->viewableChunks))
continue; continue;
if (pair.second->playersInView < 0)
std::cout << "[WARN] Weird playerview value " << pair.second->playersInView << std::endl;
// skip mob movement and combat if disabled // skip mob movement and combat if disabled
if (!simulateMobs && pair.second->state != MobState::DEAD if (!simulateMobs && pair.second->state != MobState::DEAD
&& pair.second->state != MobState::RETREAT) && pair.second->state != MobState::RETREAT)

View File

@ -11,6 +11,8 @@ public:
ChunkPos chunkPos; ChunkPos chunkPos;
std::set<Chunk*>* viewableChunks; std::set<Chunk*>* viewableChunks;
int playersInView;
BaseNPC() {}; BaseNPC() {};
BaseNPC(int x, int y, int z, int angle, uint64_t iID, int type, int id) { BaseNPC(int x, int y, int z, int angle, uint64_t iID, int type, int id) {
appearanceData.iX = x; appearanceData.iX = x;
@ -27,6 +29,7 @@ public:
chunkPos = std::make_tuple(0, 0, 0); chunkPos = std::make_tuple(0, 0, 0);
viewableChunks = new std::set<Chunk*>(); viewableChunks = new std::set<Chunk*>();
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) { 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; npcClass = classType;