mirror of
https://github.com/OpenFusionProject/OpenFusion.git
synced 2024-11-05 06:50:04 +00:00
CHUNKPOS macro -> ChunkPos typedef
This commit is contained in:
parent
b22ba781c8
commit
8ad3f3aabd
@ -32,8 +32,8 @@
|
|||||||
#define MAPNUM(x) ((x) & 0xffffffff)
|
#define MAPNUM(x) ((x) & 0xffffffff)
|
||||||
#define PLAYERID(x) ((x) >> 32)
|
#define PLAYERID(x) ((x) >> 32)
|
||||||
|
|
||||||
// macro for chunk position type
|
// typedef for chunk position tuple
|
||||||
#define CHUNKPOS std::tuple<int, int, uint64_t>
|
typedef std::tuple<int, int, uint64_t> ChunkPos;
|
||||||
|
|
||||||
// TODO: rewrite U16toU8 & U8toU16 to not use codecvt
|
// TODO: rewrite U16toU8 & U8toU16 to not use codecvt
|
||||||
|
|
||||||
|
@ -4,11 +4,11 @@
|
|||||||
#include "settings.hpp"
|
#include "settings.hpp"
|
||||||
#include "MobManager.hpp"
|
#include "MobManager.hpp"
|
||||||
|
|
||||||
std::map<CHUNKPOS, Chunk*> ChunkManager::chunks;
|
std::map<ChunkPos, Chunk*> ChunkManager::chunks;
|
||||||
|
|
||||||
void ChunkManager::init() {} // stubbed
|
void ChunkManager::init() {} // stubbed
|
||||||
|
|
||||||
void ChunkManager::newChunk(CHUNKPOS pos) {
|
void ChunkManager::newChunk(ChunkPos pos) {
|
||||||
Chunk *chunk = new Chunk();
|
Chunk *chunk = new Chunk();
|
||||||
|
|
||||||
chunk->players = std::set<CNSocket*>();
|
chunk->players = std::set<CNSocket*>();
|
||||||
@ -17,7 +17,7 @@ void ChunkManager::newChunk(CHUNKPOS pos) {
|
|||||||
chunks[pos] = chunk;
|
chunks[pos] = chunk;
|
||||||
}
|
}
|
||||||
|
|
||||||
void ChunkManager::populateNewChunk(Chunk* chunk, CHUNKPOS pos) {// add the new chunk to every player and mob that's near it
|
void ChunkManager::populateNewChunk(Chunk* chunk, ChunkPos pos) {// add the new chunk to every player and mob that's near it
|
||||||
for (Chunk *c : grabChunks(pos)) {
|
for (Chunk *c : grabChunks(pos)) {
|
||||||
if (c == chunk)
|
if (c == chunk)
|
||||||
continue;
|
continue;
|
||||||
@ -31,7 +31,7 @@ void ChunkManager::populateNewChunk(Chunk* chunk, CHUNKPOS pos) {// add the new
|
|||||||
}
|
}
|
||||||
|
|
||||||
void ChunkManager::addNPC(int posX, int posY, uint64_t instanceID, int32_t id) {
|
void ChunkManager::addNPC(int posX, int posY, uint64_t instanceID, int32_t id) {
|
||||||
CHUNKPOS pos = grabChunk(posX, posY, instanceID);
|
ChunkPos pos = grabChunk(posX, posY, instanceID);
|
||||||
|
|
||||||
bool newChunkUsed = false;
|
bool newChunkUsed = false;
|
||||||
|
|
||||||
@ -54,7 +54,7 @@ void ChunkManager::addNPC(int posX, int posY, uint64_t instanceID, int32_t id) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void ChunkManager::addPlayer(int posX, int posY, uint64_t instanceID, CNSocket* sock) {
|
void ChunkManager::addPlayer(int posX, int posY, uint64_t instanceID, CNSocket* sock) {
|
||||||
CHUNKPOS pos = grabChunk(posX, posY, instanceID);
|
ChunkPos pos = grabChunk(posX, posY, instanceID);
|
||||||
|
|
||||||
bool newChunkUsed = false;
|
bool newChunkUsed = false;
|
||||||
|
|
||||||
@ -76,7 +76,7 @@ void ChunkManager::addPlayer(int posX, int posY, uint64_t instanceID, CNSocket*
|
|||||||
populateNewChunk(chunk, pos);
|
populateNewChunk(chunk, pos);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ChunkManager::removePlayer(CHUNKPOS chunkPos, CNSocket* sock) {
|
bool ChunkManager::removePlayer(ChunkPos chunkPos, CNSocket* sock) {
|
||||||
if (!checkChunk(chunkPos))
|
if (!checkChunk(chunkPos))
|
||||||
return false; // do nothing if chunk doesn't even exist
|
return false; // do nothing if chunk doesn't even exist
|
||||||
|
|
||||||
@ -96,7 +96,7 @@ bool ChunkManager::removePlayer(CHUNKPOS chunkPos, CNSocket* sock) {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ChunkManager::removeNPC(CHUNKPOS chunkPos, int32_t id) {
|
bool ChunkManager::removeNPC(ChunkPos chunkPos, int32_t id) {
|
||||||
if (!checkChunk(chunkPos))
|
if (!checkChunk(chunkPos))
|
||||||
return false; // do nothing if chunk doesn't even exist
|
return false; // do nothing if chunk doesn't even exist
|
||||||
|
|
||||||
@ -116,7 +116,7 @@ bool ChunkManager::removeNPC(CHUNKPOS chunkPos, int32_t id) {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
void ChunkManager::destroyChunk(CHUNKPOS chunkPos) {
|
void ChunkManager::destroyChunk(ChunkPos chunkPos) {
|
||||||
if (!checkChunk(chunkPos))
|
if (!checkChunk(chunkPos))
|
||||||
return; // chunk doesn't exist, we don't need to do anything
|
return; // chunk doesn't exist, we don't need to do anything
|
||||||
|
|
||||||
@ -158,15 +158,15 @@ void ChunkManager::destroyChunk(CHUNKPOS chunkPos) {
|
|||||||
delete chunk;
|
delete chunk;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ChunkManager::checkChunk(CHUNKPOS chunk) {
|
bool ChunkManager::checkChunk(ChunkPos chunk) {
|
||||||
return chunks.find(chunk) != chunks.end();
|
return chunks.find(chunk) != chunks.end();
|
||||||
}
|
}
|
||||||
|
|
||||||
CHUNKPOS ChunkManager::grabChunk(int posX, int posY, uint64_t instanceID) {
|
ChunkPos ChunkManager::grabChunk(int posX, int posY, uint64_t instanceID) {
|
||||||
return std::make_tuple(posX / (settings::VIEWDISTANCE / 3), posY / (settings::VIEWDISTANCE / 3), instanceID);
|
return std::make_tuple(posX / (settings::VIEWDISTANCE / 3), posY / (settings::VIEWDISTANCE / 3), instanceID);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<Chunk*> ChunkManager::grabChunks(CHUNKPOS chunk) {
|
std::vector<Chunk*> ChunkManager::grabChunks(ChunkPos chunk) {
|
||||||
std::vector<Chunk*> chnks;
|
std::vector<Chunk*> chnks;
|
||||||
chnks.reserve(9);
|
chnks.reserve(9);
|
||||||
|
|
||||||
@ -177,7 +177,7 @@ std::vector<Chunk*> ChunkManager::grabChunks(CHUNKPOS chunk) {
|
|||||||
// grabs surrounding chunks if they exist
|
// 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++) {
|
||||||
CHUNKPOS pos = std::make_tuple(x+i, y+z, inst);
|
ChunkPos pos = std::make_tuple(x+i, y+z, inst);
|
||||||
|
|
||||||
// if chunk exists, add it to the vector
|
// if chunk exists, add it to the vector
|
||||||
if (checkChunk(pos))
|
if (checkChunk(pos))
|
||||||
@ -214,8 +214,8 @@ std::vector<Chunk*> ChunkManager::getDeltaChunks(std::vector<Chunk*> from, std::
|
|||||||
/*
|
/*
|
||||||
* inefficient algorithm to get all chunks from a specific instance
|
* inefficient algorithm to get all chunks from a specific instance
|
||||||
*/
|
*/
|
||||||
std::vector<CHUNKPOS> ChunkManager::getChunksInMap(uint64_t mapNum) {
|
std::vector<ChunkPos> ChunkManager::getChunksInMap(uint64_t mapNum) {
|
||||||
std::vector<CHUNKPOS> chnks;
|
std::vector<ChunkPos> chnks;
|
||||||
|
|
||||||
for (auto it = ChunkManager::chunks.begin(); it != ChunkManager::chunks.end(); it++) {
|
for (auto it = ChunkManager::chunks.begin(); it != ChunkManager::chunks.end(); it++) {
|
||||||
if (std::get<2>(it->first) == mapNum) {
|
if (std::get<2>(it->first) == mapNum) {
|
||||||
@ -240,10 +240,10 @@ bool ChunkManager::inPopulatedChunks(int posX, int posY, uint64_t instanceID) {
|
|||||||
|
|
||||||
void ChunkManager::createInstance(uint64_t instanceID) {
|
void ChunkManager::createInstance(uint64_t instanceID) {
|
||||||
|
|
||||||
std::vector<CHUNKPOS> templateChunks = ChunkManager::getChunksInMap(MAPNUM(instanceID)); // base instance chunks
|
std::vector<ChunkPos> templateChunks = ChunkManager::getChunksInMap(MAPNUM(instanceID)); // base instance chunks
|
||||||
if (ChunkManager::getChunksInMap(instanceID).size() == 0) { // only instantiate if the instance doesn't exist already
|
if (ChunkManager::getChunksInMap(instanceID).size() == 0) { // only instantiate if the instance doesn't exist already
|
||||||
std::cout << "Creating instance " << instanceID << std::endl;
|
std::cout << "Creating instance " << instanceID << std::endl;
|
||||||
for (CHUNKPOS &coords : templateChunks) {
|
for (ChunkPos &coords : templateChunks) {
|
||||||
for (int npcID : chunks[coords]->NPCs) {
|
for (int npcID : chunks[coords]->NPCs) {
|
||||||
// make a copy of each NPC in the template chunks and put them in the new instance
|
// make a copy of each NPC in the template chunks and put them in the new instance
|
||||||
int newID = NPCManager::nextId++;
|
int newID = NPCManager::nextId++;
|
||||||
@ -268,9 +268,9 @@ void ChunkManager::createInstance(uint64_t instanceID) {
|
|||||||
|
|
||||||
void ChunkManager::destroyInstance(uint64_t instanceID) {
|
void ChunkManager::destroyInstance(uint64_t instanceID) {
|
||||||
|
|
||||||
std::vector<CHUNKPOS> instanceChunks = ChunkManager::getChunksInMap(instanceID);
|
std::vector<ChunkPos> instanceChunks = ChunkManager::getChunksInMap(instanceID);
|
||||||
std::cout << "Deleting instance " << instanceID << " (" << instanceChunks.size() << " chunks)" << std::endl;
|
std::cout << "Deleting instance " << instanceID << " (" << instanceChunks.size() << " chunks)" << std::endl;
|
||||||
for (CHUNKPOS& coords : instanceChunks) {
|
for (ChunkPos& coords : instanceChunks) {
|
||||||
destroyChunk(coords);
|
destroyChunk(coords);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -279,9 +279,9 @@ void ChunkManager::destroyInstanceIfEmpty(uint64_t instanceID) {
|
|||||||
if (PLAYERID(instanceID) == 0)
|
if (PLAYERID(instanceID) == 0)
|
||||||
return; // don't clean up overworld/IZ chunks
|
return; // don't clean up overworld/IZ chunks
|
||||||
|
|
||||||
std::vector<CHUNKPOS> sourceChunkCoords = getChunksInMap(instanceID);
|
std::vector<ChunkPos> sourceChunkCoords = getChunksInMap(instanceID);
|
||||||
|
|
||||||
for (CHUNKPOS& coords : sourceChunkCoords) {
|
for (ChunkPos& coords : sourceChunkCoords) {
|
||||||
Chunk* chunk = chunks[coords];
|
Chunk* chunk = chunks[coords];
|
||||||
|
|
||||||
if (chunk->players.size() > 0)
|
if (chunk->players.size() > 0)
|
||||||
|
@ -25,20 +25,20 @@ namespace ChunkManager {
|
|||||||
void init();
|
void init();
|
||||||
void cleanup();
|
void cleanup();
|
||||||
|
|
||||||
extern std::map<CHUNKPOS, Chunk*> chunks;
|
extern std::map<ChunkPos, Chunk*> chunks;
|
||||||
|
|
||||||
void newChunk(CHUNKPOS pos);
|
void newChunk(ChunkPos pos);
|
||||||
void populateNewChunk(Chunk* chunk, CHUNKPOS pos);
|
void populateNewChunk(Chunk* chunk, ChunkPos pos);
|
||||||
void addNPC(int posX, int posY, uint64_t instanceID, int32_t id);
|
void addNPC(int posX, int posY, uint64_t instanceID, int32_t id);
|
||||||
void addPlayer(int posX, int posY, uint64_t instanceID, CNSocket* sock);
|
void addPlayer(int posX, int posY, uint64_t instanceID, CNSocket* sock);
|
||||||
bool removePlayer(CHUNKPOS chunkPos, CNSocket* sock);
|
bool removePlayer(ChunkPos chunkPos, CNSocket* sock);
|
||||||
bool removeNPC(CHUNKPOS chunkPos, int32_t id);
|
bool removeNPC(ChunkPos chunkPos, int32_t id);
|
||||||
bool checkChunk(CHUNKPOS chunk);
|
bool checkChunk(ChunkPos chunk);
|
||||||
void destroyChunk(CHUNKPOS chunkPos);
|
void destroyChunk(ChunkPos chunkPos);
|
||||||
CHUNKPOS grabChunk(int posX, int posY, uint64_t instanceID);
|
ChunkPos grabChunk(int posX, int posY, uint64_t instanceID);
|
||||||
std::vector<Chunk*> grabChunks(CHUNKPOS chunkPos);
|
std::vector<Chunk*> grabChunks(ChunkPos 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);
|
||||||
std::vector<CHUNKPOS> getChunksInMap(uint64_t mapNum);
|
std::vector<ChunkPos> getChunksInMap(uint64_t mapNum);
|
||||||
bool inPopulatedChunks(int posX, int posY, uint64_t instanceID);
|
bool inPopulatedChunks(int posX, int posY, uint64_t instanceID);
|
||||||
|
|
||||||
void createInstance(uint64_t);
|
void createInstance(uint64_t);
|
||||||
|
@ -8,7 +8,7 @@ public:
|
|||||||
sNPCAppearanceData appearanceData;
|
sNPCAppearanceData appearanceData;
|
||||||
NPCClass npcClass;
|
NPCClass npcClass;
|
||||||
uint64_t instanceID;
|
uint64_t instanceID;
|
||||||
CHUNKPOS chunkPos;
|
ChunkPos chunkPos;
|
||||||
std::vector<Chunk*> currentChunks;
|
std::vector<Chunk*> currentChunks;
|
||||||
|
|
||||||
BaseNPC() {};
|
BaseNPC() {};
|
||||||
|
@ -185,7 +185,7 @@ void NPCManager::updateNPCPosition(int32_t id, int X, int Y, int Z) {
|
|||||||
npc->appearanceData.iY = Y;
|
npc->appearanceData.iY = Y;
|
||||||
npc->appearanceData.iZ = Z;
|
npc->appearanceData.iZ = Z;
|
||||||
|
|
||||||
CHUNKPOS newPos = ChunkManager::grabChunk(X, Y, npc->instanceID);
|
ChunkPos newPos = ChunkManager::grabChunk(X, Y, npc->instanceID);
|
||||||
|
|
||||||
// nothing to be done (but we should also update currentChunks to add/remove stale chunks)
|
// nothing to be done (but we should also update currentChunks to add/remove stale chunks)
|
||||||
if (newPos == npc->chunkPos) {
|
if (newPos == npc->chunkPos) {
|
||||||
|
@ -229,7 +229,7 @@ void PlayerManager::updatePlayerPosition(CNSocket* sock, int X, int Y, int Z) {
|
|||||||
|
|
||||||
void PlayerManager::updatePlayerChunk(CNSocket* sock, int X, int Y, uint64_t instanceID) {
|
void PlayerManager::updatePlayerChunk(CNSocket* sock, int X, int Y, uint64_t instanceID) {
|
||||||
PlayerView& view = players[sock];
|
PlayerView& view = players[sock];
|
||||||
CHUNKPOS newPos = ChunkManager::grabChunk(X, Y, view.plr->instanceID);
|
ChunkPos newPos = ChunkManager::grabChunk(X, Y, view.plr->instanceID);
|
||||||
|
|
||||||
// nothing to be done
|
// nothing to be done
|
||||||
if (newPos == view.chunkPos)
|
if (newPos == view.chunkPos)
|
||||||
|
@ -13,7 +13,7 @@
|
|||||||
struct WarpLocation;
|
struct WarpLocation;
|
||||||
|
|
||||||
struct PlayerView {
|
struct PlayerView {
|
||||||
CHUNKPOS chunkPos;
|
ChunkPos chunkPos;
|
||||||
std::vector<Chunk*> currentChunks;
|
std::vector<Chunk*> currentChunks;
|
||||||
Player *plr;
|
Player *plr;
|
||||||
time_t lastHeartbeat;
|
time_t lastHeartbeat;
|
||||||
|
Loading…
Reference in New Issue
Block a user