2021-03-16 22:29:13 +00:00
|
|
|
#include "Chunking.hpp"
|
2020-09-17 22:45:43 +00:00
|
|
|
#include "PlayerManager.hpp"
|
|
|
|
#include "NPCManager.hpp"
|
|
|
|
#include "settings.hpp"
|
2021-03-13 22:55:16 +00:00
|
|
|
#include "Combat.hpp"
|
2021-03-17 21:28:24 +00:00
|
|
|
#include "Eggs.hpp"
|
2020-09-17 22:45:43 +00:00
|
|
|
|
2021-03-16 22:29:13 +00:00
|
|
|
using namespace Chunking;
|
2020-09-17 22:45:43 +00:00
|
|
|
|
2022-07-22 00:40:33 +00:00
|
|
|
/*
|
|
|
|
* The initial chunkPos value before a player is placed into the world.
|
|
|
|
*/
|
|
|
|
const ChunkPos Chunking::INVALID_CHUNK = {};
|
|
|
|
|
2021-03-16 22:29:13 +00:00
|
|
|
std::map<ChunkPos, Chunk*> Chunking::chunks;
|
2020-09-17 22:45:43 +00:00
|
|
|
|
2021-03-16 21:06:10 +00:00
|
|
|
static void newChunk(ChunkPos pos) {
|
2020-11-18 00:07:04 +00:00
|
|
|
if (chunkExists(pos)) {
|
2022-07-22 00:40:33 +00:00
|
|
|
std::cout << "[WARN] Tried to create a chunk that already exists" << std::endl;
|
2020-11-18 00:07:04 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-10-20 22:55:58 +00:00
|
|
|
Chunk *chunk = new Chunk();
|
2020-10-23 18:47:30 +00:00
|
|
|
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);
|
2021-03-21 01:42:45 +00:00
|
|
|
for (Chunk* c : surroundings)
|
|
|
|
for (const EntityRef& ref : c->entities)
|
|
|
|
ref.getEntity()->viewableChunks.insert(chunk);
|
2020-11-19 22:19:46 +00:00
|
|
|
}
|
|
|
|
|
2021-03-16 21:06:10 +00:00
|
|
|
static void deleteChunk(ChunkPos pos) {
|
2020-11-19 22:19:46 +00:00
|
|
|
if (!chunkExists(pos)) {
|
2022-07-22 00:40:33 +00:00
|
|
|
std::cout << "[WARN] Tried to delete a chunk that doesn't exist" << std::endl;
|
2020-11-19 22:19:46 +00:00
|
|
|
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)
|
2021-03-21 01:42:45 +00:00
|
|
|
for (const EntityRef& ref : c->entities)
|
|
|
|
ref.getEntity()->viewableChunks.erase(chunk);
|
2020-11-19 22:19:46 +00:00
|
|
|
|
|
|
|
chunks.erase(pos); // remove from map
|
|
|
|
delete chunk; // free from memory
|
2020-10-23 18:47:30 +00:00
|
|
|
}
|
|
|
|
|
2021-03-21 01:42:45 +00:00
|
|
|
void Chunking::trackEntity(ChunkPos chunkPos, const EntityRef& ref) {
|
2020-11-18 00:07:04 +00:00
|
|
|
if (!chunkExists(chunkPos))
|
|
|
|
return; // shouldn't happen
|
2020-09-17 22:45:43 +00:00
|
|
|
|
2021-03-21 01:42:45 +00:00
|
|
|
chunks[chunkPos]->entities.insert(ref);
|
2020-10-23 18:47:30 +00:00
|
|
|
|
2022-04-13 19:56:12 +00:00
|
|
|
if (ref.kind == EntityKind::PLAYER)
|
2021-03-21 01:42:45 +00:00
|
|
|
chunks[chunkPos]->nplayers++;
|
2020-09-17 22:45:43 +00:00
|
|
|
}
|
|
|
|
|
2021-03-21 01:42:45 +00:00
|
|
|
void Chunking::untrackEntity(ChunkPos chunkPos, const EntityRef& ref) {
|
2020-11-18 00:07:04 +00:00
|
|
|
if (!chunkExists(chunkPos))
|
|
|
|
return; // do nothing if chunk doesn't even exist
|
|
|
|
|
|
|
|
Chunk* chunk = chunks[chunkPos];
|
2020-09-17 22:45:43 +00:00
|
|
|
|
2021-03-21 01:42:45 +00:00
|
|
|
chunk->entities.erase(ref); // gone
|
2020-09-17 22:45:43 +00:00
|
|
|
|
2022-04-13 19:56:12 +00:00
|
|
|
if (ref.kind == EntityKind::PLAYER)
|
2021-03-21 01:42:45 +00:00
|
|
|
chunks[chunkPos]->nplayers--;
|
|
|
|
assert(chunks[chunkPos]->nplayers >= 0);
|
2020-09-17 22:45:43 +00:00
|
|
|
|
2021-03-21 01:42:45 +00:00
|
|
|
// if chunk is completely empty, free it
|
|
|
|
if (chunk->entities.size() == 0)
|
2020-11-19 22:19:46 +00:00
|
|
|
deleteChunk(chunkPos);
|
2020-09-17 22:45:43 +00:00
|
|
|
}
|
|
|
|
|
2021-03-21 01:42:45 +00:00
|
|
|
void Chunking::addEntityToChunks(std::set<Chunk*> chnks, const EntityRef& ref) {
|
|
|
|
Entity *ent = ref.getEntity();
|
2022-04-11 14:26:57 +00:00
|
|
|
bool alive = ent->isExtant();
|
2020-10-05 00:03:13 +00:00
|
|
|
|
2021-03-21 01:42:45 +00:00
|
|
|
// TODO: maybe optimize this, potentially using AROUND packets?
|
|
|
|
for (Chunk *chunk : chnks) {
|
|
|
|
for (const EntityRef& otherRef : chunk->entities) {
|
|
|
|
// skip oneself
|
|
|
|
if (ref == otherRef)
|
2020-11-18 00:07:04 +00:00
|
|
|
continue;
|
2020-09-21 19:55:34 +00:00
|
|
|
|
2021-03-21 01:42:45 +00:00
|
|
|
Entity *other = otherRef.getEntity();
|
2020-09-21 19:55:34 +00:00
|
|
|
|
2021-03-21 01:42:45 +00:00
|
|
|
// notify all visible players of the existence of this Entity
|
2022-04-13 19:56:12 +00:00
|
|
|
if (alive && otherRef.kind == EntityKind::PLAYER) {
|
2021-03-21 01:42:45 +00:00
|
|
|
ent->enterIntoViewOf(otherRef.sock);
|
2020-11-18 00:07:04 +00:00
|
|
|
}
|
2020-09-23 14:53:06 +00:00
|
|
|
|
2021-03-21 01:42:45 +00:00
|
|
|
// notify this *player* of the existence of all visible Entities
|
2022-04-13 19:56:12 +00:00
|
|
|
if (ref.kind == EntityKind::PLAYER && other->isExtant()) {
|
2021-03-21 01:42:45 +00:00
|
|
|
other->enterIntoViewOf(ref.sock);
|
2020-11-18 00:07:04 +00:00
|
|
|
}
|
2021-04-08 17:25:30 +00:00
|
|
|
|
|
|
|
// for mobs, increment playersInView
|
2022-04-13 19:56:12 +00:00
|
|
|
if (ref.kind == EntityKind::MOB && otherRef.kind == EntityKind::PLAYER)
|
2021-04-08 17:25:30 +00:00
|
|
|
((Mob*)ent)->playersInView++;
|
2022-04-13 19:56:12 +00:00
|
|
|
if (otherRef.kind == EntityKind::MOB && ref.kind == EntityKind::PLAYER)
|
2021-04-08 17:25:30 +00:00
|
|
|
((Mob*)other)->playersInView++;
|
2020-11-18 00:07:04 +00:00
|
|
|
}
|
2020-10-15 02:36:38 +00:00
|
|
|
}
|
2020-10-13 22:30:19 +00:00
|
|
|
}
|
|
|
|
|
2021-03-21 01:42:45 +00:00
|
|
|
void Chunking::removeEntityFromChunks(std::set<Chunk*> chnks, const EntityRef& ref) {
|
|
|
|
Entity *ent = ref.getEntity();
|
2022-04-11 14:26:57 +00:00
|
|
|
bool alive = ent->isExtant();
|
2020-10-13 22:30:19 +00:00
|
|
|
|
2021-03-21 01:42:45 +00:00
|
|
|
// TODO: same as above
|
|
|
|
for (Chunk *chunk : chnks) {
|
|
|
|
for (const EntityRef& otherRef : chunk->entities) {
|
|
|
|
// skip oneself
|
|
|
|
if (ref == otherRef)
|
|
|
|
continue;
|
2020-10-13 22:30:19 +00:00
|
|
|
|
2021-03-21 01:42:45 +00:00
|
|
|
Entity *other = otherRef.getEntity();
|
|
|
|
|
|
|
|
// notify all visible players of the departure of this Entity
|
2022-04-13 19:56:12 +00:00
|
|
|
if (alive && otherRef.kind == EntityKind::PLAYER) {
|
2021-03-21 01:42:45 +00:00
|
|
|
ent->disappearFromViewOf(otherRef.sock);
|
2020-11-18 00:07:04 +00:00
|
|
|
}
|
2021-03-21 01:42:45 +00:00
|
|
|
|
|
|
|
// notify this *player* of the departure of all visible Entities
|
2022-04-13 19:56:12 +00:00
|
|
|
if (ref.kind == EntityKind::PLAYER && other->isExtant()) {
|
2021-03-21 01:42:45 +00:00
|
|
|
other->disappearFromViewOf(ref.sock);
|
2020-10-13 22:30:19 +00:00
|
|
|
}
|
2021-04-08 17:25:30 +00:00
|
|
|
|
|
|
|
// for mobs, decrement playersInView
|
2022-04-13 19:56:12 +00:00
|
|
|
if (ref.kind == EntityKind::MOB && otherRef.kind == EntityKind::PLAYER)
|
2021-04-08 17:25:30 +00:00
|
|
|
((Mob*)ent)->playersInView--;
|
2022-04-13 19:56:12 +00:00
|
|
|
if (otherRef.kind == EntityKind::MOB && ref.kind == EntityKind::PLAYER)
|
2021-04-08 17:25:30 +00:00
|
|
|
((Mob*)other)->playersInView--;
|
2020-10-13 22:30:19 +00:00
|
|
|
}
|
|
|
|
}
|
2020-11-18 00:07:04 +00:00
|
|
|
}
|
2020-10-13 22:30:19 +00:00
|
|
|
|
2021-03-16 21:06:10 +00:00
|
|
|
static void emptyChunk(ChunkPos chunkPos) {
|
2020-11-18 00:07:04 +00:00
|
|
|
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-10-13 22:30:19 +00:00
|
|
|
|
2020-11-18 00:07:04 +00:00
|
|
|
Chunk* chunk = chunks[chunkPos];
|
2020-10-13 22:30:19 +00:00
|
|
|
|
2021-03-21 01:42:45 +00:00
|
|
|
if (chunk->nplayers > 0) {
|
2020-11-18 00:07:04 +00:00
|
|
|
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-10-13 22:30:19 +00:00
|
|
|
|
2020-11-18 00:07:04 +00:00
|
|
|
// unspawn all of the mobs/npcs
|
2021-03-21 01:42:45 +00:00
|
|
|
std::set refs(chunk->entities);
|
|
|
|
for (const EntityRef& ref : refs) {
|
2022-04-13 19:56:12 +00:00
|
|
|
if (ref.kind == EntityKind::PLAYER)
|
2021-03-21 01:42:45 +00:00
|
|
|
assert(0);
|
|
|
|
|
2020-11-18 00:07:04 +00:00
|
|
|
// every call of this will check if the chunk is empty and delete it if so
|
2021-03-21 01:42:45 +00:00
|
|
|
NPCManager::destroyNPC(ref.id);
|
2020-11-18 00:07:04 +00:00
|
|
|
}
|
2020-09-23 14:53:06 +00:00
|
|
|
}
|
|
|
|
|
2021-03-21 01:42:45 +00:00
|
|
|
void Chunking::updateEntityChunk(const EntityRef& ref, ChunkPos from, ChunkPos to) {
|
|
|
|
Entity* ent = ref.getEntity();
|
2021-03-16 21:06:10 +00:00
|
|
|
|
2021-05-02 17:51:59 +00:00
|
|
|
// move to other chunk's player set
|
|
|
|
untrackEntity(from, ref); // this will delete the chunk if it's empty
|
|
|
|
|
2021-03-16 21:06:10 +00:00
|
|
|
// if the new chunk doesn't exist, make it first
|
|
|
|
if (!chunkExists(to))
|
|
|
|
newChunk(to);
|
|
|
|
|
2021-03-21 01:42:45 +00:00
|
|
|
trackEntity(to, ref);
|
2021-03-16 21:06:10 +00:00
|
|
|
|
|
|
|
// 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
|
2021-03-21 01:42:45 +00:00
|
|
|
removeEntityFromChunks(toExit, ref);
|
|
|
|
addEntityToChunks(toEnter, ref);
|
2021-03-16 21:06:10 +00:00
|
|
|
|
2021-03-21 01:42:45 +00:00
|
|
|
ent->chunkPos = to; // update cached chunk position
|
2021-03-16 21:06:10 +00:00
|
|
|
// updated cached viewable chunks
|
2021-03-21 01:42:45 +00:00
|
|
|
ent->viewableChunks.clear();
|
|
|
|
ent->viewableChunks.insert(newViewables.begin(), newViewables.end());
|
2021-03-16 21:06:10 +00:00
|
|
|
}
|
|
|
|
|
2021-03-16 22:29:13 +00:00
|
|
|
bool Chunking::chunkExists(ChunkPos chunk) {
|
2020-09-21 19:55:34 +00:00
|
|
|
return chunks.find(chunk) != chunks.end();
|
|
|
|
}
|
|
|
|
|
2021-03-16 22:29:13 +00:00
|
|
|
ChunkPos Chunking::chunkPosAt(int posX, int posY, uint64_t instanceID) {
|
2022-07-22 00:40:33 +00:00
|
|
|
return ChunkPos(posX / (settings::VIEWDISTANCE / 3), posY / (settings::VIEWDISTANCE / 3), instanceID);
|
2020-09-17 22:45:43 +00:00
|
|
|
}
|
|
|
|
|
2021-03-16 22:29:13 +00:00
|
|
|
std::set<Chunk*> Chunking::getViewableChunks(ChunkPos chunk) {
|
2020-11-18 00:07:04 +00:00
|
|
|
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++) {
|
2022-07-22 00:40:33 +00:00
|
|
|
ChunkPos pos = ChunkPos(x+i, y+z, inst);
|
2020-10-05 00:03:13 +00:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-10-13 03:42:47 +00:00
|
|
|
/*
|
|
|
|
* inefficient algorithm to get all chunks from a specific instance
|
|
|
|
*/
|
2021-05-06 15:41:24 +00:00
|
|
|
std::vector<ChunkPos> Chunking::getChunksInMap(uint64_t mapNum) {
|
2020-11-16 14:59:53 +00:00
|
|
|
std::vector<ChunkPos> chnks;
|
2020-10-13 03:42:47 +00:00
|
|
|
|
2021-03-16 21:06:10 +00:00
|
|
|
for (auto it = chunks.begin(); it != chunks.end(); it++) {
|
2020-10-13 03:42:47 +00:00
|
|
|
if (std::get<2>(it->first) == mapNum) {
|
|
|
|
chnks.push_back(it->first);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return chnks;
|
|
|
|
}
|
|
|
|
|
2020-11-22 20:24:34 +00:00
|
|
|
/*
|
|
|
|
* Used only for eggs; use npc->playersInView for everything visible
|
|
|
|
*/
|
2021-03-16 22:29:13 +00:00
|
|
|
bool Chunking::inPopulatedChunks(std::set<Chunk*>* chnks) {
|
2020-11-19 22:19:46 +00:00
|
|
|
for (auto it = chnks->begin(); it != chnks->end(); it++) {
|
2021-03-21 01:42:45 +00:00
|
|
|
if ((*it)->nplayers > 0)
|
2020-09-22 18:33:10 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2020-10-13 03:42:47 +00:00
|
|
|
|
2021-03-16 22:29:13 +00:00
|
|
|
void Chunking::createInstance(uint64_t instanceID) {
|
2021-03-16 21:06:10 +00:00
|
|
|
std::vector<ChunkPos> templateChunks = getChunksInMap(MAPNUM(instanceID)); // base instance chunks
|
2021-03-21 02:54:24 +00:00
|
|
|
|
|
|
|
// only instantiate if the instance doesn't exist already
|
|
|
|
if (getChunksInMap(instanceID).size() != 0) {
|
|
|
|
std::cout << "Instance " << instanceID << " already exists" << std::endl;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::cout << "Creating instance " << instanceID << std::endl;
|
|
|
|
for (ChunkPos &coords : templateChunks) {
|
|
|
|
for (const EntityRef& ref : chunks[coords]->entities) {
|
2022-04-13 19:56:12 +00:00
|
|
|
if (ref.kind == EntityKind::PLAYER)
|
2021-03-21 02:54:24 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
int npcID = ref.id;
|
|
|
|
BaseNPC* baseNPC = (BaseNPC*)ref.getEntity();
|
|
|
|
|
|
|
|
// make a copy of each NPC in the template chunks and put them in the new instance
|
2022-04-13 19:56:12 +00:00
|
|
|
if (baseNPC->kind == EntityKind::MOB) {
|
2021-03-21 02:54:24 +00:00
|
|
|
if (((Mob*)baseNPC)->groupLeader != 0 && ((Mob*)baseNPC)->groupLeader != npcID)
|
|
|
|
continue; // follower; don't copy individually
|
|
|
|
|
2021-06-20 18:37:37 +00:00
|
|
|
Mob* newMob = new Mob(baseNPC->x, baseNPC->y, baseNPC->z, baseNPC->angle,
|
|
|
|
instanceID, baseNPC->type, NPCManager::NPCData[baseNPC->type], NPCManager::nextId--);
|
|
|
|
NPCManager::NPCs[newMob->id] = newMob;
|
2021-03-21 02:54:24 +00:00
|
|
|
|
|
|
|
// if in a group, copy over group members as well
|
|
|
|
if (((Mob*)baseNPC)->groupLeader != 0) {
|
2021-06-20 18:37:37 +00:00
|
|
|
newMob->groupLeader = newMob->id; // set leader ID for new leader
|
2021-03-21 02:54:24 +00:00
|
|
|
Mob* mobData = (Mob*)baseNPC;
|
|
|
|
for (int i = 0; i < 4; i++) {
|
|
|
|
if (mobData->groupMember[i] != 0) {
|
2021-04-26 17:50:51 +00:00
|
|
|
int followerID = NPCManager::nextId--; // id for follower
|
2021-03-21 02:54:24 +00:00
|
|
|
BaseNPC* baseFollower = NPCManager::NPCs[mobData->groupMember[i]]; // follower from template
|
|
|
|
// new follower instance
|
2021-06-20 18:37:37 +00:00
|
|
|
Mob* newMobFollower = new Mob(baseFollower->x, baseFollower->y, baseFollower->z, baseFollower->angle,
|
|
|
|
instanceID, baseFollower->type, NPCManager::NPCData[baseFollower->type], followerID);
|
2021-03-21 02:54:24 +00:00
|
|
|
// add follower to NPC maps
|
|
|
|
NPCManager::NPCs[followerID] = newMobFollower;
|
|
|
|
// set follower-specific properties
|
2021-06-20 18:37:37 +00:00
|
|
|
newMobFollower->groupLeader = newMob->id;
|
2021-03-21 02:54:24 +00:00
|
|
|
newMobFollower->offsetX = ((Mob*)baseFollower)->offsetX;
|
|
|
|
newMobFollower->offsetY = ((Mob*)baseFollower)->offsetY;
|
|
|
|
// add follower copy to leader copy
|
|
|
|
newMob->groupMember[i] = followerID;
|
2021-04-14 00:57:24 +00:00
|
|
|
NPCManager::updateNPCPosition(followerID, baseFollower->x, baseFollower->y, baseFollower->z,
|
2021-06-20 18:37:37 +00:00
|
|
|
instanceID, baseFollower->angle);
|
2020-11-24 03:55:44 +00:00
|
|
|
}
|
|
|
|
}
|
2020-10-14 18:36:38 +00:00
|
|
|
}
|
2021-06-20 18:37:37 +00:00
|
|
|
NPCManager::updateNPCPosition(newMob->id, baseNPC->x, baseNPC->y, baseNPC->z,
|
|
|
|
instanceID, baseNPC->angle);
|
2021-03-21 02:54:24 +00:00
|
|
|
} else {
|
2021-10-19 22:30:53 +00:00
|
|
|
BaseNPC* newNPC = new BaseNPC(baseNPC->angle, instanceID, baseNPC->type, NPCManager::nextId--);
|
2021-06-20 18:37:37 +00:00
|
|
|
NPCManager::NPCs[newNPC->id] = newNPC;
|
|
|
|
NPCManager::updateNPCPosition(newNPC->id, baseNPC->x, baseNPC->y, baseNPC->z,
|
|
|
|
instanceID, baseNPC->angle);
|
2020-10-13 03:42:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-16 21:06:10 +00:00
|
|
|
static void destroyInstance(uint64_t instanceID) {
|
|
|
|
std::vector<ChunkPos> instanceChunks = 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-13 03:42:47 +00:00
|
|
|
}
|
|
|
|
}
|
2020-10-19 02:30:12 +00:00
|
|
|
|
2021-03-16 22:29:13 +00:00
|
|
|
void Chunking::destroyInstanceIfEmpty(uint64_t instanceID) {
|
2020-10-19 02:30:12 +00:00
|
|
|
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];
|
|
|
|
|
2021-03-21 01:42:45 +00:00
|
|
|
if (chunk->nplayers > 0)
|
2020-10-19 02:30:12 +00:00
|
|
|
return; // there are still players inside
|
|
|
|
}
|
|
|
|
|
|
|
|
destroyInstance(instanceID);
|
|
|
|
}
|