2020-09-17 22:45:43 +00:00
|
|
|
#include "ChunkManager.hpp"
|
|
|
|
#include "PlayerManager.hpp"
|
|
|
|
#include "NPCManager.hpp"
|
|
|
|
#include "settings.hpp"
|
2020-10-14 18:36:38 +00:00
|
|
|
#include "MobManager.hpp"
|
2020-09-17 22:45:43 +00:00
|
|
|
|
2020-10-12 16:55:41 +00:00
|
|
|
std::map<std::tuple<int, int, uint64_t>, Chunk*> ChunkManager::chunks;
|
2020-09-17 22:45:43 +00:00
|
|
|
|
|
|
|
void ChunkManager::init() {} // stubbed
|
|
|
|
|
2020-10-20 22:55:58 +00:00
|
|
|
void ChunkManager::newChunk(std::tuple<int, int, uint64_t> pos) {
|
|
|
|
Chunk *chunk = new Chunk();
|
|
|
|
|
|
|
|
chunk->players = std::set<CNSocket*>();
|
|
|
|
chunk->NPCs = std::set<int32_t>();
|
|
|
|
|
2020-10-23 18:47:30 +00:00
|
|
|
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
|
2020-10-20 22:55:58 +00:00
|
|
|
for (Chunk *c : grabChunks(pos)) {
|
|
|
|
if (c == chunk)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
for (CNSocket *s : c->players)
|
|
|
|
PlayerManager::players[s].currentChunks.push_back(chunk);
|
|
|
|
|
|
|
|
for (int32_t id : c->NPCs)
|
|
|
|
NPCManager::NPCs[id]->currentChunks.push_back(chunk);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-12 16:55:41 +00:00
|
|
|
void ChunkManager::addNPC(int posX, int posY, uint64_t instanceID, int32_t id) {
|
|
|
|
std::tuple<int, int, uint64_t> pos = grabChunk(posX, posY, instanceID);
|
2020-09-17 22:45:43 +00:00
|
|
|
|
2020-10-23 18:47:30 +00:00
|
|
|
bool newChunkUsed = false;
|
|
|
|
|
2020-09-17 22:45:43 +00:00
|
|
|
// make chunk if it doesn't exist!
|
2020-10-23 18:47:30 +00:00
|
|
|
if (chunks.find(pos) == chunks.end()) {
|
2020-10-20 22:55:58 +00:00
|
|
|
newChunk(pos);
|
2020-10-23 18:47:30 +00:00
|
|
|
newChunkUsed = true;
|
|
|
|
}
|
2020-09-17 22:45:43 +00:00
|
|
|
|
|
|
|
Chunk* chunk = chunks[pos];
|
|
|
|
|
2020-10-23 18:47:30 +00:00
|
|
|
if (newChunkUsed)
|
|
|
|
NPCManager::NPCs[id]->currentChunks.push_back(chunk);
|
|
|
|
|
2020-09-17 22:45:43 +00:00
|
|
|
chunk->NPCs.insert(id);
|
2020-10-23 18:47:30 +00:00
|
|
|
|
|
|
|
// we must update other players after the NPC is added to chunk
|
|
|
|
if (newChunkUsed)
|
|
|
|
populateNewChunk(chunk, pos);
|
2020-09-17 22:45:43 +00:00
|
|
|
}
|
|
|
|
|
2020-10-12 16:55:41 +00:00
|
|
|
void ChunkManager::addPlayer(int posX, int posY, uint64_t instanceID, CNSocket* sock) {
|
|
|
|
std::tuple<int, int, uint64_t> pos = grabChunk(posX, posY, instanceID);
|
2020-09-17 22:45:43 +00:00
|
|
|
|
2020-10-23 18:47:30 +00:00
|
|
|
bool newChunkUsed = false;
|
|
|
|
|
2020-09-17 22:45:43 +00:00
|
|
|
// make chunk if it doesn't exist!
|
2020-10-23 18:47:30 +00:00
|
|
|
if (chunks.find(pos) == chunks.end()) {
|
2020-10-20 22:55:58 +00:00
|
|
|
newChunk(pos);
|
2020-10-23 18:47:30 +00:00
|
|
|
newChunkUsed = true;
|
|
|
|
}
|
2020-09-17 22:45:43 +00:00
|
|
|
|
|
|
|
Chunk* chunk = chunks[pos];
|
|
|
|
|
2020-10-23 18:47:30 +00:00
|
|
|
if (newChunkUsed)
|
|
|
|
PlayerManager::players[sock].currentChunks.push_back(chunk);
|
|
|
|
|
2020-09-17 22:45:43 +00:00
|
|
|
chunk->players.insert(sock);
|
2020-10-23 18:47:30 +00:00
|
|
|
|
|
|
|
// we must update other players after this player is added to chunk
|
|
|
|
if (newChunkUsed)
|
|
|
|
populateNewChunk(chunk, pos);
|
2020-09-17 22:45:43 +00:00
|
|
|
}
|
|
|
|
|
2020-10-12 16:55:41 +00:00
|
|
|
bool ChunkManager::removePlayer(std::tuple<int, int, uint64_t> chunkPos, CNSocket* sock) {
|
2020-09-21 19:55:34 +00:00
|
|
|
if (!checkChunk(chunkPos))
|
2020-10-15 02:36:38 +00:00
|
|
|
return false; // do nothing if chunk doesn't even exist
|
2020-10-05 00:03:13 +00:00
|
|
|
|
2020-09-21 19:55:34 +00:00
|
|
|
Chunk* chunk = chunks[chunkPos];
|
|
|
|
|
|
|
|
chunk->players.erase(sock); // gone
|
|
|
|
|
2020-10-13 22:30:19 +00:00
|
|
|
// if players and NPCs are empty, free chunk and remove it from surrounding views
|
2020-10-15 02:36:38 +00:00
|
|
|
if (chunk->NPCs.size() == 0 && chunk->players.size() == 0) {
|
2020-10-13 22:30:19 +00:00
|
|
|
destroyChunk(chunkPos);
|
2020-10-15 02:36:38 +00:00
|
|
|
|
|
|
|
// the chunk we left was destroyed
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// the chunk we left was not destroyed
|
|
|
|
return false;
|
2020-09-21 19:55:34 +00:00
|
|
|
}
|
|
|
|
|
2020-10-12 16:55:41 +00:00
|
|
|
bool ChunkManager::removeNPC(std::tuple<int, int, uint64_t> chunkPos, int32_t id) {
|
2020-09-23 14:53:06 +00:00
|
|
|
if (!checkChunk(chunkPos))
|
2020-10-15 02:36:38 +00:00
|
|
|
return false; // do nothing if chunk doesn't even exist
|
2020-09-23 14:53:06 +00:00
|
|
|
|
|
|
|
Chunk* chunk = chunks[chunkPos];
|
|
|
|
|
|
|
|
chunk->NPCs.erase(id); // gone
|
|
|
|
|
2020-10-13 22:30:19 +00:00
|
|
|
// if players and NPCs are empty, free chunk and remove it from surrounding views
|
2020-10-15 02:36:38 +00:00
|
|
|
if (chunk->NPCs.size() == 0 && chunk->players.size() == 0) {
|
2020-10-13 22:30:19 +00:00
|
|
|
destroyChunk(chunkPos);
|
2020-10-15 02:36:38 +00:00
|
|
|
|
|
|
|
// the chunk we left was destroyed
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// the chunk we left was not destroyed
|
|
|
|
return false;
|
2020-10-13 22:30:19 +00:00
|
|
|
}
|
|
|
|
|
2020-10-12 16:55:41 +00:00
|
|
|
void ChunkManager::destroyChunk(std::tuple<int, int, uint64_t> chunkPos) {
|
2020-10-13 22:30:19 +00:00
|
|
|
if (!checkChunk(chunkPos))
|
|
|
|
return; // chunk doesn't exist, we don't need to do anything
|
|
|
|
|
|
|
|
Chunk* chunk = chunks[chunkPos];
|
|
|
|
|
|
|
|
// unspawn all of the mobs/npcs
|
2020-10-15 19:46:07 +00:00
|
|
|
std::set npcIDs(chunk->NPCs);
|
|
|
|
for (uint32_t id : npcIDs) {
|
2020-10-13 22:30:19 +00:00
|
|
|
NPCManager::destroyNPC(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
// we also need to remove it from all NPCs/Players views
|
|
|
|
for (Chunk* otherChunk : grabChunks(chunkPos)) {
|
|
|
|
if (otherChunk == chunk)
|
|
|
|
continue;
|
2020-10-19 17:26:14 +00:00
|
|
|
|
2020-10-13 22:30:19 +00:00
|
|
|
// 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) {
|
|
|
|
PlayerView* plyr = &PlayerManager::players[sock];
|
|
|
|
if (std::find(plyr->currentChunks.begin(), plyr->currentChunks.end(), chunk) != plyr->currentChunks.end()) {
|
|
|
|
plyr->currentChunks.erase(std::remove(plyr->currentChunks.begin(), plyr->currentChunks.end(), chunk), plyr->currentChunks.end());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
assert(chunk->players.size() == 0);
|
|
|
|
|
|
|
|
// remove from the map
|
|
|
|
chunks.erase(chunkPos);
|
|
|
|
|
|
|
|
delete chunk;
|
2020-09-23 14:53:06 +00:00
|
|
|
}
|
|
|
|
|
2020-10-12 16:55:41 +00:00
|
|
|
bool ChunkManager::checkChunk(std::tuple<int, int, uint64_t> chunk) {
|
2020-09-21 19:55:34 +00:00
|
|
|
return chunks.find(chunk) != chunks.end();
|
|
|
|
}
|
|
|
|
|
2020-10-12 16:55:41 +00:00
|
|
|
std::tuple<int, int, uint64_t> ChunkManager::grabChunk(int posX, int posY, uint64_t instanceID) {
|
2020-10-14 21:15:02 +00:00
|
|
|
return std::make_tuple(posX / (settings::VIEWDISTANCE / 3), posY / (settings::VIEWDISTANCE / 3), instanceID);
|
2020-09-17 22:45:43 +00:00
|
|
|
}
|
|
|
|
|
2020-10-12 16:55:41 +00:00
|
|
|
std::vector<Chunk*> ChunkManager::grabChunks(std::tuple<int, int, uint64_t> 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-12 16:55:41 +00:00
|
|
|
int x, y;
|
|
|
|
uint64_t inst;
|
2020-10-01 01:44:37 +00:00
|
|
|
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-12 16:55:41 +00:00
|
|
|
std::tuple<int, int, uint64_t> pos = std::make_tuple(x+i, y+z, inst);
|
2020-10-05 00:03:13 +00:00
|
|
|
|
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-09-21 21:30:05 +00:00
|
|
|
}
|
2020-09-22 18:33:10 +00:00
|
|
|
|
2020-10-13 03:42:47 +00:00
|
|
|
/*
|
|
|
|
* 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;
|
|
|
|
|
|
|
|
for (auto it = ChunkManager::chunks.begin(); it != ChunkManager::chunks.end(); it++) {
|
|
|
|
if (std::get<2>(it->first) == mapNum) {
|
|
|
|
chnks.push_back(it->first);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return chnks;
|
|
|
|
}
|
|
|
|
|
2020-10-12 16:55:41 +00:00
|
|
|
bool ChunkManager::inPopulatedChunks(int posX, int posY, uint64_t instanceID) {
|
2020-10-01 01:44:37 +00:00
|
|
|
auto chunk = ChunkManager::grabChunk(posX, posY, instanceID);
|
2020-09-22 18:33:10 +00:00
|
|
|
auto nearbyChunks = ChunkManager::grabChunks(chunk);
|
|
|
|
|
|
|
|
for (Chunk *c: nearbyChunks) {
|
|
|
|
if (!c->players.empty())
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2020-10-13 03:42:47 +00:00
|
|
|
|
|
|
|
void ChunkManager::createInstance(uint64_t instanceID) {
|
|
|
|
|
2020-10-18 20:43:22 +00:00
|
|
|
std::vector<std::tuple<int, int, uint64_t>> templateChunks = ChunkManager::getChunksInMap(MAPNUM(instanceID)); // base instance chunks
|
2020-10-13 03:42:47 +00:00
|
|
|
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 (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++;
|
2020-10-14 18:36:38 +00:00
|
|
|
BaseNPC* baseNPC = NPCManager::NPCs[npcID];
|
|
|
|
if (baseNPC->npcClass == NPC_MOB) {
|
|
|
|
Mob* newMob = new Mob(baseNPC->appearanceData.iX, baseNPC->appearanceData.iY, baseNPC->appearanceData.iZ, baseNPC->appearanceData.iAngle,
|
2020-10-20 22:55:58 +00:00
|
|
|
instanceID, baseNPC->appearanceData.iNPCType, ((Mob*)baseNPC)->maxHealth, NPCManager::NPCData[baseNPC->appearanceData.iNPCType], newID);
|
2020-10-14 18:36:38 +00:00
|
|
|
NPCManager::NPCs[newID] = newMob;
|
|
|
|
MobManager::Mobs[newID] = newMob;
|
|
|
|
} else {
|
|
|
|
BaseNPC* newNPC = new BaseNPC(baseNPC->appearanceData.iX, baseNPC->appearanceData.iY, baseNPC->appearanceData.iZ, baseNPC->appearanceData.iAngle,
|
|
|
|
instanceID, baseNPC->appearanceData.iNPCType, newID);
|
|
|
|
NPCManager::NPCs[newID] = newNPC;
|
|
|
|
}
|
|
|
|
NPCManager::updateNPCInstance(newID, instanceID); // make sure the npc state gets updated
|
2020-10-13 03:42:47 +00:00
|
|
|
}
|
|
|
|
}
|
2020-10-19 17:26:14 +00:00
|
|
|
} else {
|
2020-10-13 03:42:47 +00:00
|
|
|
std::cout << "Instance " << instanceID << " already exists" << std::endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ChunkManager::destroyInstance(uint64_t instanceID) {
|
|
|
|
std::cout << "Deleting instance " << instanceID << std::endl;
|
|
|
|
std::vector<std::tuple<int, int, uint64_t>> instanceChunks = ChunkManager::getChunksInMap(instanceID);
|
|
|
|
for (std::tuple<int, int, uint64_t>& coords : instanceChunks) {
|
|
|
|
destroyChunk(coords);
|
|
|
|
}
|
|
|
|
}
|
2020-10-19 02:30:12 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
for (std::tuple<int, int, uint64_t>& coords : sourceChunkCoords) {
|
|
|
|
Chunk* chunk = chunks[coords];
|
|
|
|
|
|
|
|
if (chunk->players.size() > 0)
|
|
|
|
return; // there are still players inside
|
|
|
|
}
|
|
|
|
|
|
|
|
destroyInstance(instanceID);
|
|
|
|
}
|