OpenFusion/src/ChunkManager.cpp

535 lines
21 KiB
C++
Raw Normal View History

2020-09-17 22:45:43 +00:00
#include "ChunkManager.hpp"
#include "PlayerManager.hpp"
#include "NPCManager.hpp"
#include "settings.hpp"
#include "MobManager.hpp"
2020-09-17 22:45:43 +00:00
2020-11-16 14:59:53 +00:00
std::map<ChunkPos, Chunk*> ChunkManager::chunks;
2020-09-17 22:45:43 +00:00
void ChunkManager::init() {} // stubbed
2020-11-16 14:59:53 +00:00
void ChunkManager::newChunk(ChunkPos pos) {
2020-11-18 00:07:04 +00:00
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*>();
chunk->NPCs = std::set<int32_t>();
chunks[pos] = chunk;
2020-11-19 22:19:46 +00:00
// add the chunk to the cache of all players and NPCs in the surrounding chunks
std::set<Chunk*> surroundings = getViewableChunks(pos);
for (Chunk* c : surroundings) {
for (CNSocket* sock : c->players)
PlayerManager::getPlayer(sock)->viewableChunks->insert(chunk);
for (int32_t id : c->NPCs)
NPCManager::NPCs[id]->viewableChunks->insert(chunk);
}
}
void ChunkManager::deleteChunk(ChunkPos pos) {
if (!chunkExists(pos)) {
std::cout << "[WARN] Tried to delete a chunk that doesn't exist\n";
return;
}
Chunk* chunk = chunks[pos];
// remove the chunk from the cache of all players and NPCs in the surrounding chunks
std::set<Chunk*> surroundings = getViewableChunks(pos);
for(Chunk* c : surroundings)
{
for (CNSocket* sock : c->players)
PlayerManager::getPlayer(sock)->viewableChunks->erase(chunk);
for (int32_t id : c->NPCs)
NPCManager::NPCs[id]->viewableChunks->erase(chunk);
}
chunks.erase(pos); // remove from map
delete chunk; // free from memory
}
2020-11-18 00:07:04 +00:00
void ChunkManager::updatePlayerChunk(CNSocket* sock, ChunkPos from, ChunkPos to) {
Player* plr = PlayerManager::getPlayer(sock);
// if the new chunk doesn't exist, make it first
if (!ChunkManager::chunkExists(to))
newChunk(to);
// 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);
2020-11-19 01:37:58 +00:00
plr->chunkPos = to; // update cached chunk position
2020-11-19 22:19:46 +00:00
// updated cached viewable chunks
plr->viewableChunks->clear();
plr->viewableChunks->insert(newViewables.begin(), newViewables.end());
}
2020-11-18 00:07:04 +00:00
void ChunkManager::updateNPCChunk(int32_t id, ChunkPos from, ChunkPos to) {
BaseNPC* npc = NPCManager::NPCs[id];
// if the new chunk doesn't exist, make it first
if (!ChunkManager::chunkExists(to))
newChunk(to);
// move to other chunk's player set
untrackNPC(from, id); // this will delete the chunk if it's empty
trackNPC(to, id);
// 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
removeNPCFromChunks(toExit, id);
addNPCToChunks(toEnter, id);
2020-11-19 01:37:58 +00:00
2020-11-19 22:19:46 +00:00
npc->chunkPos = to; // update cached chunk position
// updated cached viewable chunks
npc->viewableChunks->clear();
npc->viewableChunks->insert(newViewables.begin(), newViewables.end());
2020-11-18 00:07:04 +00:00
}
2020-09-17 22:45:43 +00:00
2020-11-18 00:07:04 +00:00
void ChunkManager::trackPlayer(ChunkPos chunkPos, CNSocket* sock) {
if (!chunkExists(chunkPos))
return; // shouldn't happen
2020-09-17 22:45:43 +00:00
2020-11-18 00:07:04 +00:00
chunks[chunkPos]->players.insert(sock);
}
2020-11-18 00:07:04 +00:00
void ChunkManager::trackNPC(ChunkPos chunkPos, int32_t id) {
if (!chunkExists(chunkPos))
return; // shouldn't happen
2020-11-18 00:07:04 +00:00
chunks[chunkPos]->NPCs.insert(id);
2020-09-17 22:45:43 +00:00
}
2020-11-18 00:07:04 +00:00
void ChunkManager::untrackPlayer(ChunkPos chunkPos, CNSocket* sock) {
if (!chunkExists(chunkPos))
return; // do nothing if chunk doesn't even exist
Chunk* chunk = chunks[chunkPos];
2020-09-17 22:45:43 +00:00
2020-11-18 00:07:04 +00:00
chunk->players.erase(sock); // gone
2020-11-18 00:07:04 +00:00
// if chunk is empty, free it
2020-11-19 22:19:46 +00:00
if (chunk->NPCs.size() == 0 && chunk->players.size() == 0)
deleteChunk(chunkPos);
2020-11-18 00:07:04 +00:00
}
2020-09-17 22:45:43 +00:00
2020-11-18 00:07:04 +00:00
void ChunkManager::untrackNPC(ChunkPos chunkPos, int32_t id) {
if (!chunkExists(chunkPos))
return; // do nothing if chunk doesn't even exist
2020-09-17 22:45:43 +00:00
2020-11-18 00:07:04 +00:00
Chunk* chunk = chunks[chunkPos];
2020-11-18 00:07:04 +00:00
chunk->NPCs.erase(id); // gone
2020-11-18 00:07:04 +00:00
// if chunk is empty, free it
2020-11-19 22:19:46 +00:00
if (chunk->NPCs.size() == 0 && chunk->players.size() == 0)
deleteChunk(chunkPos);
2020-09-17 22:45:43 +00:00
}
2020-11-18 00:07:04 +00:00
void ChunkManager::addPlayerToChunks(std::set<Chunk*> chnks, CNSocket* sock) {
INITSTRUCT(sP_FE2CL_PC_NEW, newPlayer);
2020-11-18 00:07:04 +00:00
for (Chunk* chunk : chnks) {
// add npcs
for (int32_t id : chunk->NPCs) {
BaseNPC* npc = NPCManager::NPCs[id];
npc->playersInView++;
2020-09-21 19:55:34 +00:00
2020-11-18 00:07:04 +00:00
if (npc->appearanceData.iHP <= 0)
continue;
2020-09-21 19:55:34 +00:00
2020-11-18 00:07:04 +00:00
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;
}
}
2020-11-18 00:07:04 +00:00
// add players
for (CNSocket* otherSock : chunk->players) {
if (sock == otherSock)
continue; // that's us :P
2020-11-18 00:07:04 +00:00
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));
}
}
2020-09-21 19:55:34 +00:00
}
2020-11-18 00:07:04 +00:00
void ChunkManager::addNPCToChunks(std::set<Chunk*> chnks, int32_t id) {
BaseNPC* npc = NPCManager::NPCs[id];
2020-11-18 00:07:04 +00:00
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 };
2020-11-18 00:07:04 +00:00
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));
npc->playersInView++;
2020-11-18 00:07:04 +00:00
}
}
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));
npc->playersInView++;
2020-11-18 00:07:04 +00:00
}
}
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));
npc->playersInView++;
2020-11-18 00:07:04 +00:00
}
}
break;
}
}
2020-11-18 00:07:04 +00:00
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];
npc->playersInView--;
2020-11-18 00:07:04 +00:00
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;
}
}
2020-11-18 00:07:04 +00:00
// 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));
}
}
}
2020-11-18 00:07:04 +00:00
void ChunkManager::removeNPCFromChunks(std::set<Chunk*> chnks, int32_t id) {
BaseNPC* npc = NPCManager::NPCs[id];
2020-11-18 00:07:04 +00:00
switch (npc->npcClass) {
case NPC_BUS:
INITSTRUCT(sP_FE2CL_TRANSPORTATION_EXIT, exitBusData);
exitBusData.eTT = 3;
exitBusData.iT_ID = id;
2020-11-18 00:07:04 +00:00
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));
npc->playersInView--;
}
}
2020-11-18 00:07:04 +00:00
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));
npc->playersInView--;
2020-11-18 00:07:04 +00:00
}
}
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));
npc->playersInView--;
}
}
2020-11-18 00:07:04 +00:00
break;
}
2020-11-18 00:07:04 +00:00
}
2020-11-18 00:07:04 +00:00
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
}
2020-11-18 00:07:04 +00:00
Chunk* chunk = chunks[chunkPos];
2020-11-18 00:07:04 +00:00
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
}
2020-11-18 00:07:04 +00:00
// 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);
}
}
2020-11-18 00:07:04 +00:00
bool ChunkManager::chunkExists(ChunkPos chunk) {
2020-09-21 19:55:34 +00:00
return chunks.find(chunk) != chunks.end();
}
2020-11-18 00:07:04 +00:00
ChunkPos ChunkManager::chunkPosAt(int posX, int posY, uint64_t instanceID) {
return std::make_tuple(posX / (settings::VIEWDISTANCE / 3), posY / (settings::VIEWDISTANCE / 3), instanceID);
2020-09-17 22:45:43 +00:00
}
2020-11-18 00:07:04 +00:00
std::set<Chunk*> ChunkManager::getViewableChunks(ChunkPos chunk) {
std::set<Chunk*> chnks;
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-11-16 14:59:53 +00:00
ChunkPos pos = std::make_tuple(x+i, y+z, inst);
2020-11-18 00:07:04 +00:00
// if chunk exists, add it to the set
if (chunkExists(pos))
chnks.insert(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
}
/*
* inefficient algorithm to get all chunks from a specific instance
*/
2020-11-16 14:59:53 +00:00
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) {
chnks.push_back(it->first);
}
}
return chnks;
}
/*
* Used only for eggs; use npc->playersInView for everything visible
*/
2020-11-19 22:19:46 +00:00
bool ChunkManager::inPopulatedChunks(std::set<Chunk*>* chnks) {
2020-11-19 22:19:46 +00:00
for (auto it = chnks->begin(); it != chnks->end(); it++) {
if (!(*it)->players.empty())
return true;
}
return false;
}
void ChunkManager::createInstance(uint64_t instanceID) {
2020-11-16 14:59:53 +00:00
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;
2020-11-16 14:59:53 +00:00
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
BaseNPC* baseNPC = NPCManager::NPCs[npcID];
if (baseNPC->npcClass == NPC_MOB) {
2020-11-24 03:55:44 +00:00
if (((Mob*)baseNPC)->groupLeader != 0 && ((Mob*)baseNPC)->groupLeader != npcID)
continue; // follower; don't copy individually
Mob* newMob = new Mob(baseNPC->appearanceData.iX, baseNPC->appearanceData.iY, baseNPC->appearanceData.iZ, baseNPC->appearanceData.iAngle,
2020-11-24 03:55:44 +00:00
instanceID, baseNPC->appearanceData.iNPCType, ((Mob*)baseNPC)->maxHealth, NPCManager::NPCData[baseNPC->appearanceData.iNPCType], NPCManager::nextId++);
NPCManager::NPCs[newMob->appearanceData.iNPC_ID] = newMob;
MobManager::Mobs[newMob->appearanceData.iNPC_ID] = newMob;
// if in a group, copy over group members as well
if (((Mob*)baseNPC)->groupLeader != 0) {
newMob->groupLeader = newMob->appearanceData.iNPC_ID; // set leader ID for new leader
Mob* mobData = (Mob*)baseNPC;
for (int i = 0; i < 4; i++) {
if (mobData->groupMember[i] != 0) {
int followerID = NPCManager::nextId++; // id for follower
BaseNPC* baseFollower = NPCManager::NPCs[mobData->groupMember[i]]; // follower from template
// new follower instance
Mob* newMobFollower = new Mob(baseFollower->appearanceData.iX, baseFollower->appearanceData.iY, baseFollower->appearanceData.iZ, baseFollower->appearanceData.iAngle,
instanceID, baseFollower->appearanceData.iNPCType, ((Mob*)baseFollower)->maxHealth, NPCManager::NPCData[baseFollower->appearanceData.iNPCType], followerID);
// add follower to NPC maps
NPCManager::NPCs[followerID] = newMobFollower;
MobManager::Mobs[followerID] = newMobFollower;
// set follower-specific properties
newMobFollower->groupLeader = newMob->appearanceData.iNPC_ID;
newMobFollower->offsetX = ((Mob*)baseFollower)->offsetX;
newMobFollower->offsetY = ((Mob*)baseFollower)->offsetY;
// add follower copy to leader copy
newMob->groupMember[i] = followerID;
NPCManager::updateNPCPosition(followerID, baseFollower->appearanceData.iX, baseFollower->appearanceData.iY, baseFollower->appearanceData.iZ,
instanceID, baseFollower->appearanceData.iAngle);
}
}
}
NPCManager::updateNPCPosition(newMob->appearanceData.iNPC_ID, baseNPC->appearanceData.iX, baseNPC->appearanceData.iY, baseNPC->appearanceData.iZ,
instanceID, baseNPC->appearanceData.iAngle);
} else {
BaseNPC* newNPC = new BaseNPC(baseNPC->appearanceData.iX, baseNPC->appearanceData.iY, baseNPC->appearanceData.iZ, baseNPC->appearanceData.iAngle,
2020-11-24 03:55:44 +00:00
instanceID, baseNPC->appearanceData.iNPCType, NPCManager::nextId++);
NPCManager::NPCs[newNPC->appearanceData.iNPC_ID] = newNPC;
NPCManager::updateNPCPosition(newNPC->appearanceData.iNPC_ID, baseNPC->appearanceData.iX, baseNPC->appearanceData.iY, baseNPC->appearanceData.iZ,
instanceID, baseNPC->appearanceData.iAngle);
}
}
}
} else {
std::cout << "Instance " << instanceID << " already exists" << std::endl;
}
}
void ChunkManager::destroyInstance(uint64_t instanceID) {
2020-10-25 22:14:35 +00:00
2020-11-16 14:59:53 +00:00
std::vector<ChunkPos> instanceChunks = ChunkManager::getChunksInMap(instanceID);
2020-10-25 22:14:35 +00:00
std::cout << "Deleting instance " << instanceID << " (" << instanceChunks.size() << " chunks)" << std::endl;
2020-11-16 14:59:53 +00:00
for (ChunkPos& coords : instanceChunks) {
2020-11-18 00:07:04 +00:00
emptyChunk(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
2020-11-16 14:59:53 +00:00
std::vector<ChunkPos> sourceChunkCoords = getChunksInMap(instanceID);
2020-10-19 02:30:12 +00:00
2020-11-16 14:59:53 +00:00
for (ChunkPos& coords : sourceChunkCoords) {
2020-10-19 02:30:12 +00:00
Chunk* chunk = chunks[coords];
if (chunk->players.size() > 0)
return; // there are still players inside
}
destroyInstance(instanceID);
}