mirror of
https://github.com/OpenFusionProject/OpenFusion.git
synced 2024-11-04 22:40:05 +00:00
Possible fix for chunking desyncs + CHUNKPOS macro
This commit is contained in:
parent
cc74b01f72
commit
b22ba781c8
@ -32,6 +32,9 @@
|
||||
#define MAPNUM(x) ((x) & 0xffffffff)
|
||||
#define PLAYERID(x) ((x) >> 32)
|
||||
|
||||
// macro for chunk position type
|
||||
#define CHUNKPOS std::tuple<int, int, uint64_t>
|
||||
|
||||
// TODO: rewrite U16toU8 & U8toU16 to not use codecvt
|
||||
|
||||
std::string U16toU8(char16_t* src);
|
||||
|
@ -4,11 +4,11 @@
|
||||
#include "settings.hpp"
|
||||
#include "MobManager.hpp"
|
||||
|
||||
std::map<std::tuple<int, int, uint64_t>, Chunk*> ChunkManager::chunks;
|
||||
std::map<CHUNKPOS, Chunk*> ChunkManager::chunks;
|
||||
|
||||
void ChunkManager::init() {} // stubbed
|
||||
|
||||
void ChunkManager::newChunk(std::tuple<int, int, uint64_t> pos) {
|
||||
void ChunkManager::newChunk(CHUNKPOS pos) {
|
||||
Chunk *chunk = new Chunk();
|
||||
|
||||
chunk->players = std::set<CNSocket*>();
|
||||
@ -17,7 +17,7 @@ void ChunkManager::newChunk(std::tuple<int, int, uint64_t> pos) {
|
||||
chunks[pos] = chunk;
|
||||
}
|
||||
|
||||
void ChunkManager::populateNewChunk(Chunk* chunk, std::tuple<int, int, uint64_t> 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)) {
|
||||
if (c == chunk)
|
||||
continue;
|
||||
@ -31,7 +31,7 @@ void ChunkManager::populateNewChunk(Chunk* chunk, std::tuple<int, int, uint64_t>
|
||||
}
|
||||
|
||||
void ChunkManager::addNPC(int posX, int posY, uint64_t instanceID, int32_t id) {
|
||||
std::tuple<int, int, uint64_t> pos = grabChunk(posX, posY, instanceID);
|
||||
CHUNKPOS pos = grabChunk(posX, posY, instanceID);
|
||||
|
||||
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) {
|
||||
std::tuple<int, int, uint64_t> pos = grabChunk(posX, posY, instanceID);
|
||||
CHUNKPOS pos = grabChunk(posX, posY, instanceID);
|
||||
|
||||
bool newChunkUsed = false;
|
||||
|
||||
@ -76,7 +76,7 @@ void ChunkManager::addPlayer(int posX, int posY, uint64_t instanceID, CNSocket*
|
||||
populateNewChunk(chunk, pos);
|
||||
}
|
||||
|
||||
bool ChunkManager::removePlayer(std::tuple<int, int, uint64_t> chunkPos, CNSocket* sock) {
|
||||
bool ChunkManager::removePlayer(CHUNKPOS chunkPos, CNSocket* sock) {
|
||||
if (!checkChunk(chunkPos))
|
||||
return false; // do nothing if chunk doesn't even exist
|
||||
|
||||
@ -96,7 +96,7 @@ bool ChunkManager::removePlayer(std::tuple<int, int, uint64_t> chunkPos, CNSocke
|
||||
return false;
|
||||
}
|
||||
|
||||
bool ChunkManager::removeNPC(std::tuple<int, int, uint64_t> chunkPos, int32_t id) {
|
||||
bool ChunkManager::removeNPC(CHUNKPOS chunkPos, int32_t id) {
|
||||
if (!checkChunk(chunkPos))
|
||||
return false; // do nothing if chunk doesn't even exist
|
||||
|
||||
@ -116,7 +116,7 @@ bool ChunkManager::removeNPC(std::tuple<int, int, uint64_t> chunkPos, int32_t id
|
||||
return false;
|
||||
}
|
||||
|
||||
void ChunkManager::destroyChunk(std::tuple<int, int, uint64_t> chunkPos) {
|
||||
void ChunkManager::destroyChunk(CHUNKPOS chunkPos) {
|
||||
if (!checkChunk(chunkPos))
|
||||
return; // chunk doesn't exist, we don't need to do anything
|
||||
|
||||
@ -158,15 +158,15 @@ void ChunkManager::destroyChunk(std::tuple<int, int, uint64_t> chunkPos) {
|
||||
delete chunk;
|
||||
}
|
||||
|
||||
bool ChunkManager::checkChunk(std::tuple<int, int, uint64_t> chunk) {
|
||||
bool ChunkManager::checkChunk(CHUNKPOS chunk) {
|
||||
return chunks.find(chunk) != chunks.end();
|
||||
}
|
||||
|
||||
std::tuple<int, int, uint64_t> 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);
|
||||
}
|
||||
|
||||
std::vector<Chunk*> ChunkManager::grabChunks(std::tuple<int, int, uint64_t> chunk) {
|
||||
std::vector<Chunk*> ChunkManager::grabChunks(CHUNKPOS chunk) {
|
||||
std::vector<Chunk*> chnks;
|
||||
chnks.reserve(9);
|
||||
|
||||
@ -177,7 +177,7 @@ std::vector<Chunk*> ChunkManager::grabChunks(std::tuple<int, int, uint64_t> chun
|
||||
// grabs surrounding chunks if they exist
|
||||
for (int i = -1; i < 2; i++) {
|
||||
for (int z = -1; z < 2; z++) {
|
||||
std::tuple<int, int, uint64_t> 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 (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
|
||||
*/
|
||||
std::vector<std::tuple<int, int, uint64_t>> ChunkManager::getChunksInMap(uint64_t mapNum) {
|
||||
std::vector<std::tuple<int, int, uint64_t>> chnks;
|
||||
std::vector<CHUNKPOS> ChunkManager::getChunksInMap(uint64_t mapNum) {
|
||||
std::vector<CHUNKPOS> chnks;
|
||||
|
||||
for (auto it = ChunkManager::chunks.begin(); it != ChunkManager::chunks.end(); it++) {
|
||||
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) {
|
||||
|
||||
std::vector<std::tuple<int, int, uint64_t>> 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
|
||||
std::cout << "Creating instance " << instanceID << std::endl;
|
||||
for (std::tuple<int, int, uint64_t> &coords : templateChunks) {
|
||||
for (CHUNKPOS &coords : templateChunks) {
|
||||
for (int npcID : chunks[coords]->NPCs) {
|
||||
// make a copy of each NPC in the template chunks and put them in the new instance
|
||||
int newID = NPCManager::nextId++;
|
||||
@ -268,9 +268,9 @@ void ChunkManager::createInstance(uint64_t instanceID) {
|
||||
|
||||
void ChunkManager::destroyInstance(uint64_t instanceID) {
|
||||
|
||||
std::vector<std::tuple<int, int, uint64_t>> instanceChunks = ChunkManager::getChunksInMap(instanceID);
|
||||
std::vector<CHUNKPOS> instanceChunks = ChunkManager::getChunksInMap(instanceID);
|
||||
std::cout << "Deleting instance " << instanceID << " (" << instanceChunks.size() << " chunks)" << std::endl;
|
||||
for (std::tuple<int, int, uint64_t>& coords : instanceChunks) {
|
||||
for (CHUNKPOS& coords : instanceChunks) {
|
||||
destroyChunk(coords);
|
||||
}
|
||||
}
|
||||
@ -279,9 +279,9 @@ void ChunkManager::destroyInstanceIfEmpty(uint64_t instanceID) {
|
||||
if (PLAYERID(instanceID) == 0)
|
||||
return; // don't clean up overworld/IZ chunks
|
||||
|
||||
std::vector<std::tuple<int, int, uint64_t>> sourceChunkCoords = getChunksInMap(instanceID);
|
||||
std::vector<CHUNKPOS> sourceChunkCoords = getChunksInMap(instanceID);
|
||||
|
||||
for (std::tuple<int, int, uint64_t>& coords : sourceChunkCoords) {
|
||||
for (CHUNKPOS& coords : sourceChunkCoords) {
|
||||
Chunk* chunk = chunks[coords];
|
||||
|
||||
if (chunk->players.size() > 0)
|
||||
|
@ -1,6 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include "CNProtocol.hpp"
|
||||
#include "CNStructs.hpp"
|
||||
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
@ -24,20 +25,20 @@ namespace ChunkManager {
|
||||
void init();
|
||||
void cleanup();
|
||||
|
||||
extern std::map<std::tuple<int, int, uint64_t>, Chunk*> chunks;
|
||||
extern std::map<CHUNKPOS, Chunk*> chunks;
|
||||
|
||||
void newChunk(std::tuple<int, int, uint64_t> pos);
|
||||
void populateNewChunk(Chunk* chunk, std::tuple<int, int, uint64_t> pos);
|
||||
void newChunk(CHUNKPOS pos);
|
||||
void populateNewChunk(Chunk* chunk, CHUNKPOS pos);
|
||||
void addNPC(int posX, int posY, uint64_t instanceID, int32_t id);
|
||||
void addPlayer(int posX, int posY, uint64_t instanceID, CNSocket* sock);
|
||||
bool removePlayer(std::tuple<int, int, uint64_t> chunkPos, CNSocket* sock);
|
||||
bool removeNPC(std::tuple<int, int, uint64_t> chunkPos, int32_t id);
|
||||
bool checkChunk(std::tuple<int, int, uint64_t> chunk);
|
||||
void destroyChunk(std::tuple<int, int, uint64_t> chunkPos);
|
||||
std::tuple<int, int, uint64_t> grabChunk(int posX, int posY, uint64_t instanceID);
|
||||
std::vector<Chunk*> grabChunks(std::tuple<int, int, uint64_t> chunkPos);
|
||||
bool removePlayer(CHUNKPOS chunkPos, CNSocket* sock);
|
||||
bool removeNPC(CHUNKPOS chunkPos, int32_t id);
|
||||
bool checkChunk(CHUNKPOS chunk);
|
||||
void destroyChunk(CHUNKPOS chunkPos);
|
||||
CHUNKPOS grabChunk(int posX, int posY, uint64_t instanceID);
|
||||
std::vector<Chunk*> grabChunks(CHUNKPOS chunkPos);
|
||||
std::vector<Chunk*> getDeltaChunks(std::vector<Chunk*> from, std::vector<Chunk*> to);
|
||||
std::vector<std::tuple<int, int, uint64_t>> getChunksInMap(uint64_t mapNum);
|
||||
std::vector<CHUNKPOS> getChunksInMap(uint64_t mapNum);
|
||||
bool inPopulatedChunks(int posX, int posY, uint64_t instanceID);
|
||||
|
||||
void createInstance(uint64_t);
|
||||
|
@ -8,7 +8,7 @@ public:
|
||||
sNPCAppearanceData appearanceData;
|
||||
NPCClass npcClass;
|
||||
uint64_t instanceID;
|
||||
std::tuple<int, int, uint64_t> chunkPos;
|
||||
CHUNKPOS chunkPos;
|
||||
std::vector<Chunk*> currentChunks;
|
||||
|
||||
BaseNPC() {};
|
||||
|
@ -185,7 +185,7 @@ void NPCManager::updateNPCPosition(int32_t id, int X, int Y, int Z) {
|
||||
npc->appearanceData.iY = Y;
|
||||
npc->appearanceData.iZ = Z;
|
||||
|
||||
std::tuple<int, int, uint64_t> 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)
|
||||
if (newPos == npc->chunkPos) {
|
||||
|
@ -175,6 +175,9 @@ void PlayerManager::addPlayerToChunks(std::vector<Chunk*> chunks, CNSocket* sock
|
||||
|
||||
// add players
|
||||
for (CNSocket* otherSock : chunk->players) {
|
||||
if (sock == otherSock)
|
||||
continue;
|
||||
|
||||
Player *otherPlr = players[otherSock].plr;
|
||||
Player *plr = players[sock].plr;
|
||||
|
||||
@ -226,13 +229,14 @@ void PlayerManager::updatePlayerPosition(CNSocket* sock, int X, int Y, int Z) {
|
||||
|
||||
void PlayerManager::updatePlayerChunk(CNSocket* sock, int X, int Y, uint64_t instanceID) {
|
||||
PlayerView& view = players[sock];
|
||||
std::tuple<int, int, uint64_t> newPos = ChunkManager::grabChunk(X, Y, view.plr->instanceID);
|
||||
CHUNKPOS newPos = ChunkManager::grabChunk(X, Y, view.plr->instanceID);
|
||||
|
||||
// nothing to be done
|
||||
if (newPos == view.chunkPos)
|
||||
return;
|
||||
|
||||
// add player to chunk
|
||||
ChunkManager::addPlayer(X, Y, view.plr->instanceID, sock);
|
||||
std::vector<Chunk*> allChunks = ChunkManager::grabChunks(newPos);
|
||||
|
||||
Chunk *chunk = nullptr;
|
||||
@ -249,7 +253,6 @@ void PlayerManager::updatePlayerChunk(CNSocket* sock, int X, int Y, uint64_t ins
|
||||
|
||||
view.chunkPos = newPos;
|
||||
view.currentChunks = allChunks;
|
||||
ChunkManager::addPlayer(X, Y, view.plr->instanceID, sock); // takes care of adding the player to the chunk if it exists or not
|
||||
}
|
||||
|
||||
void PlayerManager::sendPlayerTo(CNSocket* sock, int X, int Y, int Z, uint64_t I) {
|
||||
|
@ -13,7 +13,7 @@
|
||||
struct WarpLocation;
|
||||
|
||||
struct PlayerView {
|
||||
std::tuple<int, int, uint64_t> chunkPos;
|
||||
CHUNKPOS chunkPos;
|
||||
std::vector<Chunk*> currentChunks;
|
||||
Player *plr;
|
||||
time_t lastHeartbeat;
|
||||
|
Loading…
Reference in New Issue
Block a user