minor chunk refactor

This commit is contained in:
CPunch 2020-09-21 14:55:34 -05:00
parent b67a0b6946
commit 4cc1cf4f7e
4 changed files with 32 additions and 16 deletions

View File

@ -27,7 +27,7 @@ void ChunkManager::addNPC(int posX, int posY, int32_t id) {
chunk->NPCs.insert(id); chunk->NPCs.insert(id);
NPCManager::addNPC(grabChunks(pos.first, pos.second), id); NPCManager::addNPC(grabChunks(pos), id);
} }
void ChunkManager::addPlayer(int posX, int posY, CNSocket* sock) { void ChunkManager::addPlayer(int posX, int posY, CNSocket* sock) {
@ -45,26 +45,41 @@ void ChunkManager::addPlayer(int posX, int posY, CNSocket* sock) {
chunk->players.insert(sock); chunk->players.insert(sock);
} }
void ChunkManager::removePlayer(std::pair<int, int> chunkPos, CNSocket* sock) {
if (!checkChunk(chunkPos))
return; // do nothing if chunk doesn't even exist
Chunk* chunk = chunks[chunkPos];
chunk->players.erase(sock); // gone
// TODO: if players and NPCs are empty, free chunk and remove it from surrounding views
}
bool ChunkManager::checkChunk(std::pair<int, int> chunk) {
return chunks.find(chunk) != chunks.end();
}
std::pair<int, int> ChunkManager::grabChunk(int posX, int posY) { std::pair<int, int> ChunkManager::grabChunk(int posX, int posY) {
return std::make_pair<int, int>(posX / (settings::PLAYERDISTANCE / 3), posY / (settings::PLAYERDISTANCE / 3)); return std::make_pair<int, int>(posX / (settings::PLAYERDISTANCE / 3), posY / (settings::PLAYERDISTANCE / 3));
} }
std::vector<Chunk*> ChunkManager::grabChunks(int chunkX, int chunkY) { std::vector<Chunk*> ChunkManager::grabChunks(std::pair<int, int> chunk) {
std::vector<Chunk*> delta; std::vector<Chunk*> chnks;
delta.reserve(9); chnks.reserve(9);
// grabs surrounding chunks if they exist
for (int i = -1; i < 2; i++) { for (int i = -1; i < 2; i++) {
for (int z = -1; z < 2; z++) { for (int z = -1; z < 2; z++) {
std::pair<int, int> pos(chunkX+i, chunkY+z); std::pair<int, int> pos(chunk.first+i, chunk.second+z);
// if chunk exists, add it to the delta // if chunk exists, add it to the vector
if (chunks.find(pos) != chunks.end()) { if (checkChunk(pos))
delta.push_back(chunks[pos]); chnks.push_back(chunks[pos]);
}
} }
} }
return delta; return chnks;
} }
// returns the chunks that aren't shared (only from from) // returns the chunks that aren't shared (only from from)

View File

@ -21,7 +21,9 @@ namespace ChunkManager {
void addNPC(int posX, int posY, int32_t id); void addNPC(int posX, int posY, int32_t id);
void addPlayer(int posX, int posY, CNSocket* sock); void addPlayer(int posX, int posY, CNSocket* sock);
void removePlayer(std::pair<int, int> chunkPos, CNSocket* sock);
bool checkChunk(std::pair<int, int> chunk);
std::pair<int, int> grabChunk(int posX, int posY); std::pair<int, int> grabChunk(int posX, int posY);
std::vector<Chunk*> grabChunks(int chunkX, int chunkY); std::vector<Chunk*> grabChunks(std::pair<int, int> chunkPos);
std::vector<Chunk*> getDeltaChunks(std::vector<Chunk*> from, std::vector<Chunk*> to); std::vector<Chunk*> getDeltaChunks(std::vector<Chunk*> from, std::vector<Chunk*> to);
} }

View File

@ -71,7 +71,7 @@ void NPCManager::removeNPC(int32_t id) {
exitData.iNPC_ID = id; exitData.iNPC_ID = id;
// remove it from the clients // remove it from the clients
for (Chunk* chunk : ChunkManager::grabChunks(pos.first, pos.second)) { for (Chunk* chunk : ChunkManager::grabChunks(pos)) {
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));

View File

@ -99,9 +99,8 @@ void PlayerManager::removePlayerFromChunks(std::vector<Chunk*> chunks, CNSocket*
} }
} }
// remove us from that old stinky chunk (+ a sanity check) // remove us from that old stinky chunk
if (ChunkManager::chunks.find(players[sock].chunkPos) != ChunkManager::chunks.end()) ChunkManager::removePlayer(players[sock].chunkPos, sock);
ChunkManager::chunks[players[sock].chunkPos]->players.erase(sock);
} }
void PlayerManager::addPlayerToChunks(std::vector<Chunk*> chunks, CNSocket* sock) { void PlayerManager::addPlayerToChunks(std::vector<Chunk*> chunks, CNSocket* sock) {
@ -169,7 +168,7 @@ void PlayerManager::updatePlayerPosition(CNSocket* sock, int X, int Y, int Z) {
return; return;
// add player to chunk // add player to chunk
std::vector<Chunk*> allChunks = ChunkManager::grabChunks(newPos.first, newPos.second); std::vector<Chunk*> allChunks = ChunkManager::grabChunks(newPos);
// first, remove all the old npcs & players from the old chunks // first, remove all the old npcs & players from the old chunks
removePlayerFromChunks(ChunkManager::getDeltaChunks(view.currentChunks, allChunks), sock); removePlayerFromChunks(ChunkManager::getDeltaChunks(view.currentChunks, allChunks), sock);