mirror of
https://github.com/OpenFusionProject/OpenFusion.git
synced 2026-01-16 13:00:05 +00:00
added basework for instancing
This commit is contained in:
@@ -3,12 +3,12 @@
|
||||
#include "NPCManager.hpp"
|
||||
#include "settings.hpp"
|
||||
|
||||
std::map<std::pair<int, int>, Chunk*> ChunkManager::chunks;
|
||||
std::map<std::tuple<int, int, int>, Chunk*> ChunkManager::chunks;
|
||||
|
||||
void ChunkManager::init() {} // stubbed
|
||||
|
||||
void ChunkManager::addNPC(int posX, int posY, int32_t id) {
|
||||
std::pair<int, int> pos = grabChunk(posX, posY);
|
||||
void ChunkManager::addNPC(int posX, int posY, int instanceID, int32_t id) {
|
||||
std::tuple<int, int, int> pos = grabChunk(posX, posY, instanceID);
|
||||
|
||||
// make chunk if it doesn't exist!
|
||||
if (chunks.find(pos) == chunks.end()) {
|
||||
@@ -22,8 +22,8 @@ void ChunkManager::addNPC(int posX, int posY, int32_t id) {
|
||||
chunk->NPCs.insert(id);
|
||||
}
|
||||
|
||||
void ChunkManager::addPlayer(int posX, int posY, CNSocket* sock) {
|
||||
std::pair<int, int> pos = grabChunk(posX, posY);
|
||||
void ChunkManager::addPlayer(int posX, int posY, int instanceID, CNSocket* sock) {
|
||||
std::tuple<int, int, int> pos = grabChunk(posX, posY, instanceID);
|
||||
|
||||
// make chunk if it doesn't exist!
|
||||
if (chunks.find(pos) == chunks.end()) {
|
||||
@@ -37,7 +37,7 @@ void ChunkManager::addPlayer(int posX, int posY, CNSocket* sock) {
|
||||
chunk->players.insert(sock);
|
||||
}
|
||||
|
||||
void ChunkManager::removePlayer(std::pair<int, int> chunkPos, CNSocket* sock) {
|
||||
void ChunkManager::removePlayer(std::tuple<int, int, int> chunkPos, CNSocket* sock) {
|
||||
if (!checkChunk(chunkPos))
|
||||
return; // do nothing if chunk doesn't even exist
|
||||
|
||||
@@ -48,7 +48,7 @@ void ChunkManager::removePlayer(std::pair<int, int> chunkPos, CNSocket* sock) {
|
||||
// TODO: if players and NPCs are empty, free chunk and remove it from surrounding views
|
||||
}
|
||||
|
||||
void ChunkManager::removeNPC(std::pair<int, int> chunkPos, int32_t id) {
|
||||
void ChunkManager::removeNPC(std::tuple<int, int, int> chunkPos, int32_t id) {
|
||||
if (!checkChunk(chunkPos))
|
||||
return; // do nothing if chunk doesn't even exist
|
||||
|
||||
@@ -59,22 +59,25 @@ void ChunkManager::removeNPC(std::pair<int, int> chunkPos, int32_t id) {
|
||||
// TODO: if players and NPCs are empty, free chunk and remove it from surrounding views
|
||||
}
|
||||
|
||||
bool ChunkManager::checkChunk(std::pair<int, int> chunk) {
|
||||
bool ChunkManager::checkChunk(std::tuple<int, 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::CHUNKSIZE / 3), posY / (settings::CHUNKSIZE / 3));
|
||||
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);
|
||||
}
|
||||
|
||||
std::vector<Chunk*> ChunkManager::grabChunks(std::pair<int, int> chunk) {
|
||||
std::vector<Chunk*> ChunkManager::grabChunks(std::tuple<int, int, int> chunk) {
|
||||
std::vector<Chunk*> chnks;
|
||||
chnks.reserve(9);
|
||||
|
||||
int x, y, inst;
|
||||
std::tie(x, y, inst) = chunk;
|
||||
|
||||
// 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(chunk.first+i, chunk.second+z);
|
||||
std::tuple<int, int, int> pos = std::make_tuple(x+i, y+z, inst);
|
||||
|
||||
// if chunk exists, add it to the vector
|
||||
if (checkChunk(pos))
|
||||
@@ -108,8 +111,8 @@ std::vector<Chunk*> ChunkManager::getDeltaChunks(std::vector<Chunk*> from, std::
|
||||
return delta;
|
||||
}
|
||||
|
||||
bool ChunkManager::inPopulatedChunks(int posX, int posY) {
|
||||
auto chunk = ChunkManager::grabChunk(posX, posY);
|
||||
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) {
|
||||
|
||||
Reference in New Issue
Block a user