mirror of
https://github.com/OpenFusionProject/OpenFusion.git
synced 2024-11-22 13:30:06 +00:00
minor chunk refactor
This commit is contained in:
parent
b67a0b6946
commit
4cc1cf4f7e
@ -27,7 +27,7 @@ void ChunkManager::addNPC(int posX, int posY, int32_t 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) {
|
||||
@ -45,26 +45,41 @@ void ChunkManager::addPlayer(int posX, int posY, CNSocket* 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) {
|
||||
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*> delta;
|
||||
delta.reserve(9);
|
||||
std::vector<Chunk*> ChunkManager::grabChunks(std::pair<int, int> chunk) {
|
||||
std::vector<Chunk*> chnks;
|
||||
chnks.reserve(9);
|
||||
|
||||
// grabs surrounding chunks if they exist
|
||||
for (int i = -1; i < 2; i++) {
|
||||
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 (chunks.find(pos) != chunks.end()) {
|
||||
delta.push_back(chunks[pos]);
|
||||
}
|
||||
// if chunk exists, add it to the vector
|
||||
if (checkChunk(pos))
|
||||
chnks.push_back(chunks[pos]);
|
||||
}
|
||||
}
|
||||
|
||||
return delta;
|
||||
return chnks;
|
||||
}
|
||||
|
||||
// returns the chunks that aren't shared (only from from)
|
||||
|
@ -21,7 +21,9 @@ namespace ChunkManager {
|
||||
|
||||
void addNPC(int posX, int posY, int32_t id);
|
||||
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::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);
|
||||
}
|
||||
|
@ -71,7 +71,7 @@ void NPCManager::removeNPC(int32_t id) {
|
||||
exitData.iNPC_ID = id;
|
||||
|
||||
// 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) {
|
||||
// send to socket
|
||||
sock->sendPacket((void*)&exitData, P_FE2CL_NPC_EXIT, sizeof(sP_FE2CL_NPC_EXIT));
|
||||
|
@ -99,9 +99,8 @@ void PlayerManager::removePlayerFromChunks(std::vector<Chunk*> chunks, CNSocket*
|
||||
}
|
||||
}
|
||||
|
||||
// remove us from that old stinky chunk (+ a sanity check)
|
||||
if (ChunkManager::chunks.find(players[sock].chunkPos) != ChunkManager::chunks.end())
|
||||
ChunkManager::chunks[players[sock].chunkPos]->players.erase(sock);
|
||||
// remove us from that old stinky chunk
|
||||
ChunkManager::removePlayer(players[sock].chunkPos, 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;
|
||||
|
||||
// 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
|
||||
removePlayerFromChunks(ChunkManager::getDeltaChunks(view.currentChunks, allChunks), sock);
|
||||
|
Loading…
Reference in New Issue
Block a user