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
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 {

View File

@ -1115,7 +1115,7 @@ bool MobManager::aggroCheck(Mob *mob, time_t currTime) {
CNSocket *closest = nullptr;
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 (CNSocket *s : chunk->players) {
Player *plr = s->plr;

View File

@ -9,7 +9,6 @@ public:
NPCClass npcClass;
uint64_t instanceID;
ChunkPos chunkPos;
std::vector<Chunk*> 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;

View File

@ -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<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 (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;

View File

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

View File

@ -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<Chunk*>();
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<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 (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));
}

View File

@ -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++;
}