Reimplement chunk position caching

This commit is contained in:
Gent S 2020-11-18 20:37:58 -05:00
parent e50a4c2edd
commit dae3b24093
7 changed files with 25 additions and 26 deletions

View File

@ -51,6 +51,8 @@ void ChunkManager::updatePlayerChunk(CNSocket* sock, ChunkPos from, ChunkPos to)
// update views // update views
removePlayerFromChunks(toExit, sock); removePlayerFromChunks(toExit, sock);
addPlayerToChunks(toEnter, sock); addPlayerToChunks(toEnter, sock);
plr->chunkPos = to; // update cached chunk position
} }
void ChunkManager::updateNPCChunk(int32_t id, ChunkPos from, ChunkPos to) { 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 // update views
removeNPCFromChunks(toExit, id); removeNPCFromChunks(toExit, id);
addNPCToChunks(toEnter, id); addNPCToChunks(toEnter, id);
npc->chunkPos = to;
} }
void ChunkManager::trackPlayer(ChunkPos chunkPos, CNSocket* sock) { 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, NPCManager::updateNPCPosition(newID, baseNPC->appearanceData.iX, baseNPC->appearanceData.iY, baseNPC->appearanceData.iZ,
instanceID, baseNPC->appearanceData.iAngle); instanceID, baseNPC->appearanceData.iAngle);
// force chunk update // 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 { } else {

View File

@ -1115,7 +1115,7 @@ bool MobManager::aggroCheck(Mob *mob, time_t currTime) {
CNSocket *closest = nullptr; CNSocket *closest = nullptr;
int closestDistance = INT_MAX; int closestDistance = INT_MAX;
std::set<Chunk*> chunks = ChunkManager::getViewableChunks(ChunkManager::chunkPosAt(mob->appearanceData.iX, mob->appearanceData.iY, mob->instanceID)); std::set<Chunk*> chunks = ChunkManager::getViewableChunks(mob->chunkPos);
for (Chunk *chunk : chunks) { for (Chunk *chunk : chunks) {
for (CNSocket *s : chunk->players) { for (CNSocket *s : chunk->players) {
Player *plr = s->plr; Player *plr = s->plr;

View File

@ -9,7 +9,6 @@ public:
NPCClass npcClass; NPCClass npcClass;
uint64_t instanceID; uint64_t instanceID;
ChunkPos chunkPos; ChunkPos chunkPos;
std::vector<Chunk*> currentChunks;
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) {
@ -24,6 +23,8 @@ public:
appearanceData.iNPC_ID = id; appearanceData.iNPC_ID = id;
instanceID = iID; 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) { 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;

View File

@ -62,19 +62,18 @@ void NPCManager::destroyNPC(int32_t id) {
} }
BaseNPC* entity = NPCs[id]; BaseNPC* entity = NPCs[id];
ChunkPos chunkPos = ChunkManager::chunkPosAt(entity->appearanceData.iX, entity->appearanceData.iY, entity->instanceID);
// sanity check // sanity check
if (!ChunkManager::chunkExists(chunkPos)) { if (!ChunkManager::chunkExists(entity->chunkPos)) {
std::cout << "chunk not found!" << std::endl; std::cout << "chunk not found!" << std::endl;
return; return;
} }
// remove NPC from the chunk // remove NPC from the chunk
ChunkManager::untrackNPC(chunkPos, id); ChunkManager::untrackNPC(entity->chunkPos, id);
// remove from viewable chunks // remove from viewable chunks
ChunkManager::removeNPCFromChunks(ChunkManager::getViewableChunks(chunkPos), id); ChunkManager::removeNPCFromChunks(ChunkManager::getViewableChunks(entity->chunkPos), id);
// remove from mob manager // remove from mob manager
if (MobManager::Mobs.find(id) != MobManager::Mobs.end()) 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) { void NPCManager::updateNPCPosition(int32_t id, int X, int Y, int Z, uint64_t I, int angle) {
BaseNPC* npc = NPCs[id]; BaseNPC* npc = NPCs[id];
npc->appearanceData.iAngle = angle; 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); ChunkPos newChunk = ChunkManager::chunkPosAt(X, Y, I);
npc->appearanceData.iX = X; npc->appearanceData.iX = X;
npc->appearanceData.iY = Y; 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) { void NPCManager::sendToViewable(BaseNPC *npc, void *buf, uint32_t type, size_t size) {
std::set<Chunk*> chunks = ChunkManager::getViewableChunks(ChunkManager::chunkPosAt(npc->appearanceData.iX, npc->appearanceData.iY, npc->instanceID)); std::set<Chunk*> chunks = ChunkManager::getViewableChunks(npc->chunkPos);
for (Chunk *chunk : chunks) { for (Chunk *chunk : chunks) {
for (CNSocket *s : chunk->players) { for (CNSocket *s : chunk->players) {
s->sendPacket(buf, type, size); 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); updateNPCPosition(id, plr->x, plr->y, plr->z, plr->instanceID, 0);
// force chunk update // 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->deadUntil = 0;
egg.second->appearanceData.iHP = 400; 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); egg.first);
} }
} }
@ -896,8 +895,7 @@ void NPCManager::eggPickup(CNSocket* sock, CNPacketData* data) {
if (egg->summoned) if (egg->summoned)
destroyNPC(eggId); destroyNPC(eggId);
else { else {
ChunkManager::removeNPCFromChunks(ChunkManager::getViewableChunks(ChunkManager::chunkPosAt(egg->appearanceData.iX, egg->appearanceData.iY, egg->instanceID)), ChunkManager::removeNPCFromChunks(ChunkManager::getViewableChunks(egg->chunkPos), eggId);
eggId);
egg->dead = true; egg->dead = true;
egg->deadUntil = getTime() + (time_t)type->regen * 1000; egg->deadUntil = getTime() + (time_t)type->regen * 1000;
egg->appearanceData.iHP = 0; egg->appearanceData.iHP = 0;

View File

@ -77,7 +77,6 @@ struct Player {
bool buddiesSynced; bool buddiesSynced;
int64_t buddyIDs[50]; int64_t buddyIDs[50];
//ChunkPos chunkPos; ChunkPos chunkPos;
std::set<Chunk*>* currentChunks;
time_t lastHeartbeat; time_t lastHeartbeat;
}; };

View File

@ -53,8 +53,7 @@ void PlayerManager::addPlayer(CNSocket* key, Player plr) {
memcpy(p, &plr, sizeof(Player)); memcpy(p, &plr, sizeof(Player));
players[key] = p; players[key] = p;
//p->chunkPos = std::make_tuple(0, 0, 0); p->chunkPos = std::make_tuple(0, 0, 0);
p->currentChunks = new std::set<Chunk*>();
p->lastHeartbeat = 0; p->lastHeartbeat = 0;
key->plr = p; key->plr = p;
@ -75,11 +74,9 @@ void PlayerManager::removePlayer(CNSocket* key) {
// save player to DB // save player to DB
Database::updatePlayer(plr); Database::updatePlayer(plr);
ChunkPos chunkPos = ChunkManager::chunkPosAt(plr->x, plr->y, plr->instanceID);
// remove player visually and untrack // remove player visually and untrack
ChunkManager::removePlayerFromChunks(ChunkManager::getViewableChunks(chunkPos), key); ChunkManager::removePlayerFromChunks(ChunkManager::getViewableChunks(plr->chunkPos), key);
ChunkManager::untrackPlayer(chunkPos, key); ChunkManager::untrackPlayer(plr->chunkPos, key);
std::cout << getPlayerName(key->plr) << " has left!" << std::endl; 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) { void PlayerManager::updatePlayerPosition(CNSocket* sock, int X, int Y, int Z, uint64_t I, int angle) {
Player* plr = getPlayer(sock); Player* plr = getPlayer(sock);
plr->angle = angle; plr->angle = angle;
ChunkPos oldChunk = ChunkManager::chunkPosAt(plr->x, plr->y, plr->instanceID); ChunkPos oldChunk = plr->chunkPos;
ChunkPos newChunk = ChunkManager::chunkPosAt(X, Y, I); ChunkPos newChunk = ChunkManager::chunkPosAt(X, Y, I);
plr->x = X; plr->x = X;
plr->y = Y; 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) { void PlayerManager::sendToViewable(CNSocket* sock, void* buf, uint32_t type, size_t size) {
Player* plr = getPlayer(sock); Player* plr = getPlayer(sock);
std::set<Chunk*> chunks = ChunkManager::getViewableChunks(ChunkManager::chunkPosAt(plr->x, plr->y, plr->instanceID)); std::set<Chunk*> chunks = ChunkManager::getViewableChunks(plr->chunkPos);
for (Chunk* chunk : chunks) { for (Chunk* chunk : chunks) {
for (CNSocket* otherSock : chunk->players) { for (CNSocket* otherSock : chunk->players) {
if (otherSock == sock) if (otherSock == sock)
@ -322,7 +319,7 @@ void PlayerManager::loadPlayer(CNSocket* sock, CNPacketData* data) {
// reload players & NPCs // reload players & NPCs
updatePlayerPosition(sock, plr->x, plr->y, plr->z, plr->instanceID, plr->angle); 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)); sock->sendPacket((void*)&response, P_FE2CL_REP_PC_LOADING_COMPLETE_SUCC, sizeof(sP_FE2CL_REP_PC_LOADING_COMPLETE_SUCC));
} }

View File

@ -44,7 +44,7 @@ void TableData::init() {
NPCManager::NPCs[nextId] = tmp; NPCManager::NPCs[nextId] = tmp;
NPCManager::updateNPCPosition(nextId, npc["x"], npc["y"], npc["z"], instanceID, npc["angle"]); 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++; nextId++;
if (npc["id"] == 641 || npc["id"] == 642) if (npc["id"] == 641 || npc["id"] == 642)
@ -212,7 +212,7 @@ void TableData::init() {
NPCManager::NPCs[nextId] = tmp; NPCManager::NPCs[nextId] = tmp;
MobManager::Mobs[nextId] = (Mob*)NPCManager::NPCs[nextId]; MobManager::Mobs[nextId] = (Mob*)NPCManager::NPCs[nextId];
NPCManager::updateNPCPosition(nextId, npc["iX"], npc["iY"], npc["iZ"], instanceID, npc["iAngle"]); 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++; nextId++;
} }