mirror of
https://github.com/OpenFusionProject/OpenFusion.git
synced 2025-11-30 12:30:06 +00:00
Experimental chunk refactor.
This commit is contained in:
@@ -9,6 +9,11 @@ std::map<ChunkPos, Chunk*> ChunkManager::chunks;
|
||||
void ChunkManager::init() {} // stubbed
|
||||
|
||||
void ChunkManager::newChunk(ChunkPos pos) {
|
||||
if (chunkExists(pos)) {
|
||||
std::cout << "[WARN] Tried to create a chunk that already exists\n";
|
||||
return;
|
||||
}
|
||||
|
||||
Chunk *chunk = new Chunk();
|
||||
|
||||
chunk->players = std::set<CNSocket*>();
|
||||
@@ -17,158 +22,340 @@ void ChunkManager::newChunk(ChunkPos pos) {
|
||||
chunks[pos] = chunk;
|
||||
}
|
||||
|
||||
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;
|
||||
void ChunkManager::updatePlayerChunk(CNSocket* sock, ChunkPos from, ChunkPos to) {
|
||||
Player* plr = PlayerManager::getPlayer(sock);
|
||||
|
||||
for (CNSocket *s : c->players)
|
||||
PlayerManager::getPlayer(s)->currentChunks->push_back(chunk);
|
||||
// if the new chunk doesn't exist, make it first
|
||||
if (!ChunkManager::chunkExists(to))
|
||||
newChunk(to);
|
||||
|
||||
for (int32_t id : c->NPCs)
|
||||
NPCManager::NPCs[id]->currentChunks.push_back(chunk);
|
||||
}
|
||||
// move to other chunk's player set
|
||||
untrackPlayer(from, sock); // this will delete the chunk if it's empty
|
||||
trackPlayer(to, sock);
|
||||
|
||||
// calculate viewable chunks from both points
|
||||
std::set<Chunk*> oldViewables = getViewableChunks(from);
|
||||
std::set<Chunk*> newViewables = getViewableChunks(to);
|
||||
std::set<Chunk*> toExit, toEnter;
|
||||
|
||||
/*
|
||||
* Calculate diffs. This is done to prevent phasing on chunk borders.
|
||||
* toExit will contain old viewables - new viewables, so the player will only be exited in chunks that are out of sight.
|
||||
* toEnter contains the opposite: new viewables - old viewables, chunks where we previously weren't visible from before.
|
||||
*/
|
||||
std::set_difference(oldViewables.begin(), oldViewables.end(), newViewables.begin(), newViewables.end(),
|
||||
std::inserter(toExit, toExit.end())); // chunks we must be EXITed from (old - new)
|
||||
std::set_difference(newViewables.begin(), newViewables.end(), oldViewables.begin(), oldViewables.end(),
|
||||
std::inserter(toEnter, toEnter.end())); // chunks we must be ENTERed into (new - old)
|
||||
|
||||
// update views
|
||||
removePlayerFromChunks(toExit, sock);
|
||||
addPlayerToChunks(toEnter, sock);
|
||||
}
|
||||
|
||||
void ChunkManager::addNPC(int posX, int posY, uint64_t instanceID, int32_t id) {
|
||||
ChunkPos pos = grabChunk(posX, posY, instanceID);
|
||||
void ChunkManager::updateNPCChunk(int32_t id, ChunkPos from, ChunkPos to) {
|
||||
BaseNPC* npc = NPCManager::NPCs[id];
|
||||
|
||||
bool newChunkUsed = false;
|
||||
// if the new chunk doesn't exist, make it first
|
||||
if (!ChunkManager::chunkExists(to))
|
||||
newChunk(to);
|
||||
|
||||
// make chunk if it doesn't exist!
|
||||
if (chunks.find(pos) == chunks.end()) {
|
||||
newChunk(pos);
|
||||
newChunkUsed = true;
|
||||
}
|
||||
// move to other chunk's player set
|
||||
untrackNPC(from, id); // this will delete the chunk if it's empty
|
||||
trackNPC(to, id);
|
||||
|
||||
Chunk* chunk = chunks[pos];
|
||||
// calculate viewable chunks from both points
|
||||
std::set<Chunk*> oldViewables = getViewableChunks(from);
|
||||
std::set<Chunk*> newViewables = getViewableChunks(to);
|
||||
std::set<Chunk*> toExit, toEnter;
|
||||
|
||||
if (newChunkUsed)
|
||||
NPCManager::NPCs[id]->currentChunks.push_back(chunk);
|
||||
/*
|
||||
* Calculate diffs. This is done to prevent phasing on chunk borders.
|
||||
* toExit will contain old viewables - new viewables, so the player will only be exited in chunks that are out of sight.
|
||||
* toEnter contains the opposite: new viewables - old viewables, chunks where we previously weren't visible from before.
|
||||
*/
|
||||
std::set_difference(oldViewables.begin(), oldViewables.end(), newViewables.begin(), newViewables.end(),
|
||||
std::inserter(toExit, toExit.end())); // chunks we must be EXITed from (old - new)
|
||||
std::set_difference(newViewables.begin(), newViewables.end(), oldViewables.begin(), oldViewables.end(),
|
||||
std::inserter(toEnter, toEnter.end())); // chunks we must be ENTERed into (new - old)
|
||||
|
||||
chunk->NPCs.insert(id);
|
||||
|
||||
// we must update other players after the NPC is added to chunk
|
||||
if (newChunkUsed)
|
||||
populateNewChunk(chunk, pos);
|
||||
// update views
|
||||
removeNPCFromChunks(toExit, id);
|
||||
addNPCToChunks(toEnter, id);
|
||||
}
|
||||
|
||||
void ChunkManager::addPlayer(int posX, int posY, uint64_t instanceID, CNSocket* sock) {
|
||||
ChunkPos pos = grabChunk(posX, posY, instanceID);
|
||||
void ChunkManager::trackPlayer(ChunkPos chunkPos, CNSocket* sock) {
|
||||
if (!chunkExists(chunkPos))
|
||||
return; // shouldn't happen
|
||||
|
||||
bool newChunkUsed = false;
|
||||
|
||||
// make chunk if it doesn't exist!
|
||||
if (chunks.find(pos) == chunks.end()) {
|
||||
newChunk(pos);
|
||||
newChunkUsed = true;
|
||||
}
|
||||
|
||||
Chunk* chunk = chunks[pos];
|
||||
|
||||
if (newChunkUsed)
|
||||
PlayerManager::getPlayer(sock)->currentChunks->push_back(chunk);
|
||||
|
||||
chunk->players.insert(sock);
|
||||
|
||||
// we must update other players after this player is added to chunk
|
||||
if (newChunkUsed)
|
||||
populateNewChunk(chunk, pos);
|
||||
chunks[chunkPos]->players.insert(sock);
|
||||
}
|
||||
|
||||
bool ChunkManager::removePlayer(ChunkPos chunkPos, CNSocket* sock) {
|
||||
if (!checkChunk(chunkPos))
|
||||
return false; // do nothing if chunk doesn't even exist
|
||||
void ChunkManager::trackNPC(ChunkPos chunkPos, int32_t id) {
|
||||
if (!chunkExists(chunkPos))
|
||||
return; // shouldn't happen
|
||||
|
||||
chunks[chunkPos]->NPCs.insert(id);
|
||||
}
|
||||
|
||||
void ChunkManager::untrackPlayer(ChunkPos chunkPos, CNSocket* sock) {
|
||||
if (!chunkExists(chunkPos))
|
||||
return; // do nothing if chunk doesn't even exist
|
||||
|
||||
Chunk* chunk = chunks[chunkPos];
|
||||
|
||||
chunk->players.erase(sock); // gone
|
||||
|
||||
// if players and NPCs are empty, free chunk and remove it from surrounding views
|
||||
// if chunk is empty, free it
|
||||
if (chunk->NPCs.size() == 0 && chunk->players.size() == 0) {
|
||||
destroyChunk(chunkPos);
|
||||
|
||||
// the chunk we left was destroyed
|
||||
return true;
|
||||
chunks.erase(chunkPos); // remove from map
|
||||
delete chunk; // free from memory
|
||||
}
|
||||
|
||||
// the chunk we left was not destroyed
|
||||
return false;
|
||||
}
|
||||
|
||||
bool ChunkManager::removeNPC(ChunkPos chunkPos, int32_t id) {
|
||||
if (!checkChunk(chunkPos))
|
||||
return false; // do nothing if chunk doesn't even exist
|
||||
void ChunkManager::untrackNPC(ChunkPos chunkPos, int32_t id) {
|
||||
if (!chunkExists(chunkPos))
|
||||
return; // do nothing if chunk doesn't even exist
|
||||
|
||||
Chunk* chunk = chunks[chunkPos];
|
||||
|
||||
chunk->NPCs.erase(id); // gone
|
||||
|
||||
// if players and NPCs are empty, free chunk and remove it from surrounding views
|
||||
// if chunk is empty, free it
|
||||
if (chunk->NPCs.size() == 0 && chunk->players.size() == 0) {
|
||||
destroyChunk(chunkPos);
|
||||
|
||||
// the chunk we left was destroyed
|
||||
return true;
|
||||
chunks.erase(chunkPos); // remove from map
|
||||
delete chunk; // free from memory
|
||||
}
|
||||
|
||||
// the chunk we left was not destroyed
|
||||
return false;
|
||||
}
|
||||
|
||||
void ChunkManager::destroyChunk(ChunkPos chunkPos) {
|
||||
if (!checkChunk(chunkPos))
|
||||
void ChunkManager::addPlayerToChunks(std::set<Chunk*> chnks, CNSocket* sock) {
|
||||
INITSTRUCT(sP_FE2CL_PC_NEW, newPlayer);
|
||||
|
||||
for (Chunk* chunk : chnks) {
|
||||
// add npcs
|
||||
for (int32_t id : chunk->NPCs) {
|
||||
BaseNPC* npc = NPCManager::NPCs[id];
|
||||
|
||||
if (npc->appearanceData.iHP <= 0)
|
||||
continue;
|
||||
|
||||
switch (npc->npcClass) {
|
||||
case NPC_BUS:
|
||||
INITSTRUCT(sP_FE2CL_TRANSPORTATION_ENTER, enterBusData);
|
||||
enterBusData.AppearanceData = { 3, npc->appearanceData.iNPC_ID, npc->appearanceData.iNPCType, npc->appearanceData.iX, npc->appearanceData.iY, npc->appearanceData.iZ };
|
||||
sock->sendPacket((void*)&enterBusData, P_FE2CL_TRANSPORTATION_ENTER, sizeof(sP_FE2CL_TRANSPORTATION_ENTER));
|
||||
break;
|
||||
case NPC_EGG:
|
||||
INITSTRUCT(sP_FE2CL_SHINY_ENTER, enterEggData);
|
||||
NPCManager::npcDataToEggData(&npc->appearanceData, &enterEggData.ShinyAppearanceData);
|
||||
sock->sendPacket((void*)&enterEggData, P_FE2CL_SHINY_ENTER, sizeof(sP_FE2CL_SHINY_ENTER));
|
||||
break;
|
||||
default:
|
||||
INITSTRUCT(sP_FE2CL_NPC_ENTER, enterData);
|
||||
enterData.NPCAppearanceData = NPCManager::NPCs[id]->appearanceData;
|
||||
sock->sendPacket((void*)&enterData, P_FE2CL_NPC_ENTER, sizeof(sP_FE2CL_NPC_ENTER));
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// add players
|
||||
for (CNSocket* otherSock : chunk->players) {
|
||||
if (sock == otherSock)
|
||||
continue; // that's us :P
|
||||
|
||||
Player* otherPlr = PlayerManager::getPlayer(otherSock);
|
||||
Player* plr = PlayerManager::getPlayer(sock);
|
||||
|
||||
newPlayer.PCAppearanceData.iID = plr->iID;
|
||||
newPlayer.PCAppearanceData.iHP = plr->HP;
|
||||
newPlayer.PCAppearanceData.iLv = plr->level;
|
||||
newPlayer.PCAppearanceData.iX = plr->x;
|
||||
newPlayer.PCAppearanceData.iY = plr->y;
|
||||
newPlayer.PCAppearanceData.iZ = plr->z;
|
||||
newPlayer.PCAppearanceData.iAngle = plr->angle;
|
||||
newPlayer.PCAppearanceData.PCStyle = plr->PCStyle;
|
||||
newPlayer.PCAppearanceData.Nano = plr->Nanos[plr->activeNano];
|
||||
newPlayer.PCAppearanceData.iPCState = plr->iPCState;
|
||||
newPlayer.PCAppearanceData.iSpecialState = plr->iSpecialState;
|
||||
memcpy(newPlayer.PCAppearanceData.ItemEquip, plr->Equip, sizeof(sItemBase) * AEQUIP_COUNT);
|
||||
|
||||
otherSock->sendPacket((void*)&newPlayer, P_FE2CL_PC_NEW, sizeof(sP_FE2CL_PC_NEW));
|
||||
|
||||
newPlayer.PCAppearanceData.iID = otherPlr->iID;
|
||||
newPlayer.PCAppearanceData.iHP = otherPlr->HP;
|
||||
newPlayer.PCAppearanceData.iLv = otherPlr->level;
|
||||
newPlayer.PCAppearanceData.iX = otherPlr->x;
|
||||
newPlayer.PCAppearanceData.iY = otherPlr->y;
|
||||
newPlayer.PCAppearanceData.iZ = otherPlr->z;
|
||||
newPlayer.PCAppearanceData.iAngle = otherPlr->angle;
|
||||
newPlayer.PCAppearanceData.PCStyle = otherPlr->PCStyle;
|
||||
newPlayer.PCAppearanceData.Nano = otherPlr->Nanos[otherPlr->activeNano];
|
||||
newPlayer.PCAppearanceData.iPCState = otherPlr->iPCState;
|
||||
newPlayer.PCAppearanceData.iSpecialState = otherPlr->iSpecialState;
|
||||
memcpy(newPlayer.PCAppearanceData.ItemEquip, otherPlr->Equip, sizeof(sItemBase) * AEQUIP_COUNT);
|
||||
|
||||
sock->sendPacket((void*)&newPlayer, P_FE2CL_PC_NEW, sizeof(sP_FE2CL_PC_NEW));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void ChunkManager::addNPCToChunks(std::set<Chunk*> chnks, int32_t id) {
|
||||
BaseNPC* npc = NPCManager::NPCs[id];
|
||||
|
||||
switch (npc->npcClass) {
|
||||
case NPC_BUS:
|
||||
INITSTRUCT(sP_FE2CL_TRANSPORTATION_ENTER, enterBusData);
|
||||
enterBusData.AppearanceData = { 3, npc->appearanceData.iNPC_ID, npc->appearanceData.iNPCType, npc->appearanceData.iX, npc->appearanceData.iY, npc->appearanceData.iZ };
|
||||
|
||||
for (Chunk* chunk : chnks) {
|
||||
for (CNSocket* sock : chunk->players) {
|
||||
// send to socket
|
||||
sock->sendPacket((void*)&enterBusData, P_FE2CL_TRANSPORTATION_ENTER, sizeof(sP_FE2CL_TRANSPORTATION_ENTER));
|
||||
}
|
||||
}
|
||||
break;
|
||||
case NPC_EGG:
|
||||
INITSTRUCT(sP_FE2CL_SHINY_ENTER, enterEggData);
|
||||
NPCManager::npcDataToEggData(&npc->appearanceData, &enterEggData.ShinyAppearanceData);
|
||||
|
||||
for (Chunk* chunk : chnks) {
|
||||
for (CNSocket* sock : chunk->players) {
|
||||
// send to socket
|
||||
sock->sendPacket((void*)&enterEggData, P_FE2CL_SHINY_ENTER, sizeof(sP_FE2CL_SHINY_ENTER));
|
||||
}
|
||||
}
|
||||
break;
|
||||
default:
|
||||
// create struct
|
||||
INITSTRUCT(sP_FE2CL_NPC_ENTER, enterData);
|
||||
enterData.NPCAppearanceData = npc->appearanceData;
|
||||
|
||||
for (Chunk* chunk : chnks) {
|
||||
for (CNSocket* sock : chunk->players) {
|
||||
// send to socket
|
||||
sock->sendPacket((void*)&enterData, P_FE2CL_NPC_ENTER, sizeof(sP_FE2CL_NPC_ENTER));
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void ChunkManager::removePlayerFromChunks(std::set<Chunk*> chnks, CNSocket* sock) {
|
||||
INITSTRUCT(sP_FE2CL_PC_EXIT, exitPlayer);
|
||||
|
||||
// for chunks that need the player to be removed from
|
||||
for (Chunk* chunk : chnks) {
|
||||
|
||||
// remove NPCs from view
|
||||
for (int32_t id : chunk->NPCs) {
|
||||
BaseNPC* npc = NPCManager::NPCs[id];
|
||||
switch (npc->npcClass) {
|
||||
case NPC_BUS:
|
||||
INITSTRUCT(sP_FE2CL_TRANSPORTATION_EXIT, exitBusData);
|
||||
exitBusData.eTT = 3;
|
||||
exitBusData.iT_ID = id;
|
||||
sock->sendPacket((void*)&exitBusData, P_FE2CL_TRANSPORTATION_EXIT, sizeof(sP_FE2CL_TRANSPORTATION_EXIT));
|
||||
break;
|
||||
case NPC_EGG:
|
||||
INITSTRUCT(sP_FE2CL_SHINY_EXIT, exitEggData);
|
||||
exitEggData.iShinyID = id;
|
||||
sock->sendPacket((void*)&exitEggData, P_FE2CL_SHINY_EXIT, sizeof(sP_FE2CL_SHINY_EXIT));
|
||||
break;
|
||||
default:
|
||||
INITSTRUCT(sP_FE2CL_NPC_EXIT, exitData);
|
||||
exitData.iNPC_ID = id;
|
||||
sock->sendPacket((void*)&exitData, P_FE2CL_NPC_EXIT, sizeof(sP_FE2CL_NPC_EXIT));
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// remove players from eachother's views
|
||||
for (CNSocket* otherSock : chunk->players) {
|
||||
if (sock == otherSock)
|
||||
continue; // that's us :P
|
||||
exitPlayer.iID = PlayerManager::getPlayer(sock)->iID;
|
||||
otherSock->sendPacket((void*)&exitPlayer, P_FE2CL_PC_EXIT, sizeof(sP_FE2CL_PC_EXIT));
|
||||
exitPlayer.iID = PlayerManager::getPlayer(otherSock)->iID;
|
||||
sock->sendPacket((void*)&exitPlayer, P_FE2CL_PC_EXIT, sizeof(sP_FE2CL_PC_EXIT));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void ChunkManager::removeNPCFromChunks(std::set<Chunk*> chnks, int32_t id) {
|
||||
BaseNPC* npc = NPCManager::NPCs[id];
|
||||
|
||||
switch (npc->npcClass) {
|
||||
case NPC_BUS:
|
||||
INITSTRUCT(sP_FE2CL_TRANSPORTATION_EXIT, exitBusData);
|
||||
exitBusData.eTT = 3;
|
||||
exitBusData.iT_ID = id;
|
||||
|
||||
for (Chunk* chunk : chnks) {
|
||||
for (CNSocket* sock : chunk->players) {
|
||||
// send to socket
|
||||
sock->sendPacket((void*)&exitBusData, P_FE2CL_TRANSPORTATION_EXIT, sizeof(sP_FE2CL_TRANSPORTATION_EXIT));
|
||||
}
|
||||
}
|
||||
break;
|
||||
case NPC_EGG:
|
||||
INITSTRUCT(sP_FE2CL_SHINY_EXIT, exitEggData);
|
||||
exitEggData.iShinyID = id;
|
||||
|
||||
for (Chunk* chunk : chnks) {
|
||||
for (CNSocket* sock : chunk->players) {
|
||||
// send to socket
|
||||
sock->sendPacket((void*)&exitEggData, P_FE2CL_SHINY_EXIT, sizeof(sP_FE2CL_SHINY_EXIT));
|
||||
}
|
||||
}
|
||||
break;
|
||||
default:
|
||||
// create struct
|
||||
INITSTRUCT(sP_FE2CL_NPC_EXIT, exitData);
|
||||
exitData.iNPC_ID = id;
|
||||
|
||||
// remove it from the clients
|
||||
for (Chunk* chunk : chnks) {
|
||||
for (CNSocket* sock : chunk->players) {
|
||||
// send to socket
|
||||
sock->sendPacket((void*)&exitData, P_FE2CL_NPC_EXIT, sizeof(sP_FE2CL_NPC_EXIT));
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void ChunkManager::emptyChunk(ChunkPos chunkPos) {
|
||||
if (!chunkExists(chunkPos)) {
|
||||
std::cout << "[WARN] Tried to empty chunk that doesn't exist\n";
|
||||
return; // chunk doesn't exist, we don't need to do anything
|
||||
}
|
||||
|
||||
Chunk* chunk = chunks[chunkPos];
|
||||
|
||||
if (chunk->players.size() > 0) {
|
||||
std::cout << "[WARN] Tried to empty chunk that still had players\n";
|
||||
return; // chunk doesn't exist, we don't need to do anything
|
||||
}
|
||||
|
||||
// unspawn all of the mobs/npcs
|
||||
std::set npcIDs(chunk->NPCs);
|
||||
for (uint32_t id : npcIDs) {
|
||||
// every call of this will check if the chunk is empty and delete it if so
|
||||
NPCManager::destroyNPC(id);
|
||||
}
|
||||
|
||||
// we also need to remove it from all NPCs/Players views
|
||||
for (Chunk* otherChunk : grabChunks(chunkPos)) {
|
||||
if (otherChunk == chunk)
|
||||
continue;
|
||||
|
||||
// remove from NPCs
|
||||
for (uint32_t id : otherChunk->NPCs) {
|
||||
if (std::find(NPCManager::NPCs[id]->currentChunks.begin(), NPCManager::NPCs[id]->currentChunks.end(), chunk) != NPCManager::NPCs[id]->currentChunks.end()) {
|
||||
NPCManager::NPCs[id]->currentChunks.erase(std::remove(NPCManager::NPCs[id]->currentChunks.begin(), NPCManager::NPCs[id]->currentChunks.end(), chunk), NPCManager::NPCs[id]->currentChunks.end());
|
||||
}
|
||||
}
|
||||
|
||||
// remove from players
|
||||
for (CNSocket* sock : otherChunk->players) {
|
||||
Player* plr = PlayerManager::getPlayer(sock);
|
||||
if (std::find(plr->currentChunks->begin(), plr->currentChunks->end(), chunk) != plr->currentChunks->end()) {
|
||||
plr->currentChunks->erase(std::remove(plr->currentChunks->begin(), plr->currentChunks->end(), chunk), plr->currentChunks->end());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
assert(chunk->players.size() == 0);
|
||||
|
||||
// remove from the map
|
||||
chunks.erase(chunkPos);
|
||||
|
||||
delete chunk;
|
||||
}
|
||||
|
||||
bool ChunkManager::checkChunk(ChunkPos chunk) {
|
||||
bool ChunkManager::chunkExists(ChunkPos chunk) {
|
||||
return chunks.find(chunk) != chunks.end();
|
||||
}
|
||||
|
||||
ChunkPos ChunkManager::grabChunk(int posX, int posY, uint64_t instanceID) {
|
||||
ChunkPos ChunkManager::chunkPosAt(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(ChunkPos chunk) {
|
||||
std::vector<Chunk*> chnks;
|
||||
chnks.reserve(9);
|
||||
std::set<Chunk*> ChunkManager::getViewableChunks(ChunkPos chunk) {
|
||||
std::set<Chunk*> chnks;
|
||||
|
||||
int x, y;
|
||||
uint64_t inst;
|
||||
@@ -179,38 +366,15 @@ std::vector<Chunk*> ChunkManager::grabChunks(ChunkPos chunk) {
|
||||
for (int z = -1; z < 2; z++) {
|
||||
ChunkPos pos = std::make_tuple(x+i, y+z, inst);
|
||||
|
||||
// if chunk exists, add it to the vector
|
||||
if (checkChunk(pos))
|
||||
chnks.push_back(chunks[pos]);
|
||||
// if chunk exists, add it to the set
|
||||
if (chunkExists(pos))
|
||||
chnks.insert(chunks[pos]);
|
||||
}
|
||||
}
|
||||
|
||||
return chnks;
|
||||
}
|
||||
|
||||
// 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;
|
||||
}
|
||||
|
||||
/*
|
||||
* inefficient algorithm to get all chunks from a specific instance
|
||||
*/
|
||||
@@ -227,8 +391,7 @@ std::vector<ChunkPos> ChunkManager::getChunksInMap(uint64_t mapNum) {
|
||||
}
|
||||
|
||||
bool ChunkManager::inPopulatedChunks(int posX, int posY, uint64_t instanceID) {
|
||||
auto chunk = ChunkManager::grabChunk(posX, posY, instanceID);
|
||||
auto nearbyChunks = ChunkManager::grabChunks(chunk);
|
||||
auto nearbyChunks = ChunkManager::getViewableChunks(chunkPosAt(posX, posY, instanceID));
|
||||
|
||||
for (Chunk *c: nearbyChunks) {
|
||||
if (!c->players.empty())
|
||||
@@ -258,7 +421,10 @@ void ChunkManager::createInstance(uint64_t instanceID) {
|
||||
instanceID, baseNPC->appearanceData.iNPCType, newID);
|
||||
NPCManager::NPCs[newID] = newNPC;
|
||||
}
|
||||
NPCManager::updateNPCInstance(newID, instanceID); // make sure the npc state gets updated
|
||||
NPCManager::updateNPCPosition(newID, baseNPC->appearanceData.iX, baseNPC->appearanceData.iY, baseNPC->appearanceData.iZ,
|
||||
instanceID, baseNPC->appearanceData.iAngle);
|
||||
// force chunk update
|
||||
updateNPCChunk(newID, {0, 0, 0}, chunkPosAt(baseNPC->appearanceData.iX, baseNPC->appearanceData.iY, instanceID));
|
||||
}
|
||||
}
|
||||
} else {
|
||||
@@ -271,7 +437,7 @@ void ChunkManager::destroyInstance(uint64_t instanceID) {
|
||||
std::vector<ChunkPos> instanceChunks = ChunkManager::getChunksInMap(instanceID);
|
||||
std::cout << "Deleting instance " << instanceID << " (" << instanceChunks.size() << " chunks)" << std::endl;
|
||||
for (ChunkPos& coords : instanceChunks) {
|
||||
destroyChunk(coords);
|
||||
emptyChunk(coords);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user