OpenFusion/src/ChunkManager.cpp

125 lines
3.5 KiB
C++
Raw Normal View History

2020-09-17 22:45:43 +00:00
#include "ChunkManager.hpp"
#include "PlayerManager.hpp"
#include "NPCManager.hpp"
#include "settings.hpp"
2020-10-01 01:44:37 +00:00
std::map<std::tuple<int, int, int>, Chunk*> ChunkManager::chunks;
2020-09-17 22:45:43 +00:00
void ChunkManager::init() {} // stubbed
2020-10-01 01:44:37 +00:00
void ChunkManager::addNPC(int posX, int posY, int instanceID, int32_t id) {
std::tuple<int, int, int> pos = grabChunk(posX, posY, instanceID);
2020-09-17 22:45:43 +00:00
// make chunk if it doesn't exist!
if (chunks.find(pos) == chunks.end()) {
chunks[pos] = new Chunk();
chunks[pos]->players = std::set<CNSocket*>();
chunks[pos]->NPCs = std::set<int32_t>();
}
Chunk* chunk = chunks[pos];
chunk->NPCs.insert(id);
}
2020-10-01 01:44:37 +00:00
void ChunkManager::addPlayer(int posX, int posY, int instanceID, CNSocket* sock) {
std::tuple<int, int, int> pos = grabChunk(posX, posY, instanceID);
2020-09-17 22:45:43 +00:00
// make chunk if it doesn't exist!
if (chunks.find(pos) == chunks.end()) {
chunks[pos] = new Chunk();
chunks[pos]->players = std::set<CNSocket*>();
chunks[pos]->NPCs = std::set<int32_t>();
}
Chunk* chunk = chunks[pos];
chunk->players.insert(sock);
}
2020-10-01 01:44:37 +00:00
void ChunkManager::removePlayer(std::tuple<int, int, int> chunkPos, CNSocket* sock) {
2020-09-21 19:55:34 +00:00
if (!checkChunk(chunkPos))
return; // do nothing if chunk doesn't even exist
2020-09-21 19:55:34 +00:00
Chunk* chunk = chunks[chunkPos];
chunk->players.erase(sock); // gone
// TODO: if players and NPCs are empty, free chunk and remove it from surrounding views
}
2020-10-01 01:44:37 +00:00
void ChunkManager::removeNPC(std::tuple<int, int, int> chunkPos, int32_t id) {
if (!checkChunk(chunkPos))
return; // do nothing if chunk doesn't even exist
Chunk* chunk = chunks[chunkPos];
chunk->NPCs.erase(id); // gone
// TODO: if players and NPCs are empty, free chunk and remove it from surrounding views
}
2020-10-01 01:44:37 +00:00
bool ChunkManager::checkChunk(std::tuple<int, int, int> chunk) {
2020-09-21 19:55:34 +00:00
return chunks.find(chunk) != chunks.end();
}
2020-10-01 01:44:37 +00:00
std::tuple<int, int, int> ChunkManager::grabChunk(int posX, int posY, int instanceID) {
return std::make_tuple(posX / (settings::CHUNKSIZE / 3), posY / (settings::CHUNKSIZE / 3), instanceID);
2020-09-17 22:45:43 +00:00
}
2020-10-01 01:44:37 +00:00
std::vector<Chunk*> ChunkManager::grabChunks(std::tuple<int, int, int> chunk) {
2020-09-21 19:55:34 +00:00
std::vector<Chunk*> chnks;
chnks.reserve(9);
2020-09-17 22:45:43 +00:00
2020-10-01 01:44:37 +00:00
int x, y, inst;
std::tie(x, y, inst) = chunk;
2020-09-21 19:55:34 +00:00
// grabs surrounding chunks if they exist
2020-09-17 22:45:43 +00:00
for (int i = -1; i < 2; i++) {
for (int z = -1; z < 2; z++) {
2020-10-01 01:44:37 +00:00
std::tuple<int, int, int> pos = std::make_tuple(x+i, y+z, inst);
2020-09-21 19:55:34 +00:00
// if chunk exists, add it to the vector
if (checkChunk(pos))
chnks.push_back(chunks[pos]);
2020-09-17 22:45:43 +00:00
}
}
2020-09-21 19:55:34 +00:00
return chnks;
2020-09-17 22:45:43 +00:00
}
// returns the chunks that aren't shared (only from from)
std::vector<Chunk*> ChunkManager::getDeltaChunks(std::vector<Chunk*> from, std::vector<Chunk*> to) {
std::vector<Chunk*> delta;
for (Chunk* i : from) {
bool found = false;
// search for it in the other array
for (Chunk* z : to) {
if (i == z) {
found = true;
break;
}
}
// add it to the vector if we didn't find it!
if (!found)
delta.push_back(i);
}
return delta;
}
2020-10-01 01:44:37 +00:00
bool ChunkManager::inPopulatedChunks(int posX, int posY, int instanceID) {
auto chunk = ChunkManager::grabChunk(posX, posY, instanceID);
auto nearbyChunks = ChunkManager::grabChunks(chunk);
for (Chunk *c: nearbyChunks) {
if (!c->players.empty())
return true;
}
return false;
}