[refactor] Sync with master

This was initially a merge commit that got ironed out by rebase cleanup.
Notable changes are:
- Copying EntityRef instead of referencing it
- Some changes to includes
This commit is contained in:
gsemaj 2022-07-30 22:50:03 -07:00 committed by gsemaj
parent 31677e2638
commit 4ece1bb89b
No known key found for this signature in database
GPG Key ID: 24B96BAA40497929
6 changed files with 22 additions and 30 deletions

View File

@ -26,7 +26,7 @@ static void newChunk(ChunkPos pos) {
// add the chunk to the cache of all players and NPCs in the surrounding chunks // add the chunk to the cache of all players and NPCs in the surrounding chunks
std::set<Chunk*> surroundings = getViewableChunks(pos); std::set<Chunk*> surroundings = getViewableChunks(pos);
for (Chunk* c : surroundings) for (Chunk* c : surroundings)
for (const EntityRef& ref : c->entities) for (const EntityRef ref : c->entities)
ref.getEntity()->viewableChunks.insert(chunk); ref.getEntity()->viewableChunks.insert(chunk);
} }
@ -41,14 +41,14 @@ static void deleteChunk(ChunkPos pos) {
// remove the chunk from the cache of all players and NPCs in the surrounding chunks // remove the chunk from the cache of all players and NPCs in the surrounding chunks
std::set<Chunk*> surroundings = getViewableChunks(pos); std::set<Chunk*> surroundings = getViewableChunks(pos);
for(Chunk* c : surroundings) for(Chunk* c : surroundings)
for (const EntityRef& ref : c->entities) for (const EntityRef ref : c->entities)
ref.getEntity()->viewableChunks.erase(chunk); ref.getEntity()->viewableChunks.erase(chunk);
chunks.erase(pos); // remove from map chunks.erase(pos); // remove from map
delete chunk; // free from memory delete chunk; // free from memory
} }
void Chunking::trackEntity(ChunkPos chunkPos, const EntityRef& ref) { void Chunking::trackEntity(ChunkPos chunkPos, const EntityRef ref) {
if (!chunkExists(chunkPos)) if (!chunkExists(chunkPos))
return; // shouldn't happen return; // shouldn't happen
@ -58,7 +58,7 @@ void Chunking::trackEntity(ChunkPos chunkPos, const EntityRef& ref) {
chunks[chunkPos]->nplayers++; chunks[chunkPos]->nplayers++;
} }
void Chunking::untrackEntity(ChunkPos chunkPos, const EntityRef& ref) { void Chunking::untrackEntity(ChunkPos chunkPos, const EntityRef ref) {
if (!chunkExists(chunkPos)) if (!chunkExists(chunkPos))
return; // do nothing if chunk doesn't even exist return; // do nothing if chunk doesn't even exist
@ -75,13 +75,13 @@ void Chunking::untrackEntity(ChunkPos chunkPos, const EntityRef& ref) {
deleteChunk(chunkPos); deleteChunk(chunkPos);
} }
void Chunking::addEntityToChunks(std::set<Chunk*> chnks, const EntityRef& ref) { void Chunking::addEntityToChunks(std::set<Chunk*> chnks, const EntityRef ref) {
Entity *ent = ref.getEntity(); Entity *ent = ref.getEntity();
bool alive = ent->isExtant(); bool alive = ent->isExtant();
// TODO: maybe optimize this, potentially using AROUND packets? // TODO: maybe optimize this, potentially using AROUND packets?
for (Chunk *chunk : chnks) { for (Chunk *chunk : chnks) {
for (const EntityRef& otherRef : chunk->entities) { for (const EntityRef otherRef : chunk->entities) {
// skip oneself // skip oneself
if (ref == otherRef) if (ref == otherRef)
continue; continue;
@ -107,13 +107,13 @@ void Chunking::addEntityToChunks(std::set<Chunk*> chnks, const EntityRef& ref) {
} }
} }
void Chunking::removeEntityFromChunks(std::set<Chunk*> chnks, const EntityRef& ref) { void Chunking::removeEntityFromChunks(std::set<Chunk*> chnks, const EntityRef ref) {
Entity *ent = ref.getEntity(); Entity *ent = ref.getEntity();
bool alive = ent->isExtant(); bool alive = ent->isExtant();
// TODO: same as above // TODO: same as above
for (Chunk *chunk : chnks) { for (Chunk *chunk : chnks) {
for (const EntityRef& otherRef : chunk->entities) { for (const EntityRef otherRef : chunk->entities) {
// skip oneself // skip oneself
if (ref == otherRef) if (ref == otherRef)
continue; continue;
@ -154,7 +154,7 @@ static void emptyChunk(ChunkPos chunkPos) {
// unspawn all of the mobs/npcs // unspawn all of the mobs/npcs
std::set refs(chunk->entities); std::set refs(chunk->entities);
for (const EntityRef& ref : refs) { for (const EntityRef ref : refs) {
if (ref.kind == EntityKind::PLAYER) if (ref.kind == EntityKind::PLAYER)
assert(0); assert(0);
@ -163,7 +163,7 @@ static void emptyChunk(ChunkPos chunkPos) {
} }
} }
void Chunking::updateEntityChunk(const EntityRef& ref, ChunkPos from, ChunkPos to) { void Chunking::updateEntityChunk(const EntityRef ref, ChunkPos from, ChunkPos to) {
Entity* ent = ref.getEntity(); Entity* ent = ref.getEntity();
// move to other chunk's player set // move to other chunk's player set
@ -267,7 +267,7 @@ void Chunking::createInstance(uint64_t instanceID) {
std::cout << "Creating instance " << instanceID << std::endl; std::cout << "Creating instance " << instanceID << std::endl;
for (ChunkPos &coords : templateChunks) { for (ChunkPos &coords : templateChunks) {
for (const EntityRef& ref : chunks[coords]->entities) { for (const EntityRef ref : chunks[coords]->entities) {
if (ref.kind == EntityKind::PLAYER) if (ref.kind == EntityKind::PLAYER)
continue; continue;

View File

@ -1,13 +1,11 @@
#pragma once #pragma once
#include "Entities.hpp" #include "EntityRef.hpp"
#include <set> #include <set>
#include <map> #include <map>
#include <vector> #include <vector>
struct EntityRef;
class Chunk { class Chunk {
public: public:
std::set<EntityRef> entities; std::set<EntityRef> entities;
@ -34,13 +32,13 @@ namespace Chunking {
extern const ChunkPos INVALID_CHUNK; extern const ChunkPos INVALID_CHUNK;
void updateEntityChunk(const EntityRef& ref, ChunkPos from, ChunkPos to); void updateEntityChunk(const EntityRef ref, ChunkPos from, ChunkPos to);
void trackEntity(ChunkPos chunkPos, const EntityRef& ref); void trackEntity(ChunkPos chunkPos, const EntityRef ref);
void untrackEntity(ChunkPos chunkPos, const EntityRef& ref); void untrackEntity(ChunkPos chunkPos, const EntityRef ref);
void addEntityToChunks(std::set<Chunk*> chnks, const EntityRef& ref); void addEntityToChunks(std::set<Chunk*> chnks, const EntityRef ref);
void removeEntityFromChunks(std::set<Chunk*> chnks, const EntityRef& ref); void removeEntityFromChunks(std::set<Chunk*> chnks, const EntityRef ref);
bool chunkExists(ChunkPos chunk); bool chunkExists(ChunkPos chunk);
ChunkPos chunkPosAt(int posX, int posY, uint64_t instanceID); ChunkPos chunkPosAt(int posX, int posY, uint64_t instanceID);

View File

@ -1,19 +1,16 @@
#pragma once #pragma once
#include "core/Core.hpp" #include "core/Core.hpp"
#include "Chunking.hpp"
#include "EntityRef.hpp" #include "EntityRef.hpp"
#include "Buffs.hpp" #include "Buffs.hpp"
#include "Chunking.hpp"
#include "Groups.hpp"
#include <set> #include <set>
#include <map> #include <map>
#include <functional> #include <functional>
/* forward declaration(s) */
class Chunk;
struct Group;
enum class AIState { enum class AIState {
INACTIVE, INACTIVE,
ROAMING, ROAMING,

View File

@ -4,6 +4,7 @@
#include "Player.hpp" #include "Player.hpp"
#include "PlayerManager.hpp" #include "PlayerManager.hpp"
#include "Entities.hpp"
/* /*
* NOTE: Variadic response packets that list group members are technically * NOTE: Variadic response packets that list group members are technically

View File

@ -1,13 +1,8 @@
#pragma once #pragma once
#include "Entities.hpp" #include "EntityRef.hpp"
#include <vector> #include <vector>
#include <map>
#include <list>
/* forward declaration(s) */
struct Player;
struct Group { struct Group {
std::vector<EntityRef> members; std::vector<EntityRef> members;

View File

@ -5,6 +5,7 @@
#include "Transport.hpp" #include "Transport.hpp"
#include "Chunking.hpp" #include "Chunking.hpp"
#include "Entities.hpp"
#include <map> #include <map>
#include <unordered_map> #include <unordered_map>