From 486a996fa76a03df79c21427c6e0cf550a05de2d Mon Sep 17 00:00:00 2001 From: Gent Semaj Date: Mon, 28 Oct 2024 20:12:44 -0700 Subject: [PATCH] PR feedback + cleanup --- src/Bucket.hpp | 7 ++-- src/Chunking.cpp | 89 +++++++++++++++++++++++------------------------- 2 files changed, 46 insertions(+), 50 deletions(-) diff --git a/src/Bucket.hpp b/src/Bucket.hpp index fc3f52b..d62f4dd 100644 --- a/src/Bucket.hpp +++ b/src/Bucket.hpp @@ -3,6 +3,8 @@ #include #include +#include + template class Bucket { std::array buf; @@ -13,9 +15,8 @@ public: } void add(const T& item) { - if (sz < N) { - buf[sz++] = item; - } + assert(sz < N); + buf[sz++] = item; } std::optional get(size_t idx) const { diff --git a/src/Chunking.cpp b/src/Chunking.cpp index fec1cdc..a3facaa 100644 --- a/src/Chunking.cpp +++ b/src/Chunking.cpp @@ -13,11 +13,12 @@ using namespace Chunking; * The initial chunkPos value before a player is placed into the world. */ const ChunkPos Chunking::INVALID_CHUNK = {}; -constexpr size_t MAX_PC_PER_AROUND = (CN_PACKET_BUFFER_SIZE - sizeof(int32_t)) / sizeof(sPCAppearanceData); -constexpr size_t MAX_NPC_PER_AROUND = (CN_PACKET_BUFFER_SIZE - sizeof(int32_t)) / sizeof(sNPCAppearanceData); -constexpr size_t MAX_SHINY_PER_AROUND = (CN_PACKET_BUFFER_SIZE - sizeof(int32_t)) / sizeof(sShinyAppearanceData); -constexpr size_t MAX_TRANSPORTATION_PER_AROUND = (CN_PACKET_BUFFER_SIZE - sizeof(int32_t)) / sizeof(sTransportationAppearanceData); -constexpr size_t MAX_IDS_PER_AROUND_DEL = (CN_PACKET_BUFFER_SIZE - sizeof(int32_t)) / sizeof(int32_t); +constexpr size_t MAX_PC_PER_AROUND = (CN_PACKET_BODY_SIZE - sizeof(int32_t)) / sizeof(sPCAppearanceData); +constexpr size_t MAX_NPC_PER_AROUND = (CN_PACKET_BODY_SIZE - sizeof(int32_t)) / sizeof(sNPCAppearanceData); +constexpr size_t MAX_SHINY_PER_AROUND = (CN_PACKET_BODY_SIZE - sizeof(int32_t)) / sizeof(sShinyAppearanceData); +constexpr size_t MAX_TRANSPORTATION_PER_AROUND = (CN_PACKET_BODY_SIZE - sizeof(int32_t)) / sizeof(sTransportationAppearanceData); +constexpr size_t MAX_IDS_PER_AROUND_DEL = (CN_PACKET_BODY_SIZE - sizeof(int32_t)) / sizeof(int32_t); +constexpr size_t MAX_TRANSPORTATION_IDS_PER_AROUND_DEL = MAX_IDS_PER_AROUND_DEL - 1; // 1 less for eTT std::map Chunking::chunks; @@ -83,12 +84,12 @@ void Chunking::untrackEntity(ChunkPos chunkPos, const EntityRef ref) { } template -static void sendAroundPacket(const EntityRef recipient, std::vector>& buckets, uint32_t packetId) { +static void sendAroundPackets(const EntityRef recipient, std::vector>& buckets, uint32_t packetId) { assert(recipient.kind == EntityKind::PLAYER); - uint8_t pktBuf[CN_PACKET_BUFFER_SIZE]; + uint8_t pktBuf[CN_PACKET_BODY_SIZE]; for (const auto& bucket : buckets) { - memset(pktBuf, 0, CN_PACKET_BUFFER_SIZE); + memset(pktBuf, 0, CN_PACKET_BODY_SIZE); int count = bucket.size(); *((int32_t*)pktBuf) = count; T* data = (T*)(pktBuf + sizeof(int32_t)); @@ -100,17 +101,17 @@ static void sendAroundPacket(const EntityRef recipient, std::vector } template -static void sendAroundDelPacket(const EntityRef recipient, std::vector>& buckets, bool isTransportation, uint32_t packetId) { +static void sendAroundDelPackets(const EntityRef recipient, std::vector>& buckets, uint32_t packetId) { assert(recipient.kind == EntityKind::PLAYER); - uint8_t pktBuf[CN_PACKET_BUFFER_SIZE]; + uint8_t pktBuf[CN_PACKET_BODY_SIZE]; for (const auto& bucket : buckets) { - memset(pktBuf, 0, CN_PACKET_BUFFER_SIZE); + memset(pktBuf, 0, CN_PACKET_BODY_SIZE); int count = bucket.size(); assert(count <= N); size_t baseSize; - if (isTransportation) { + if (packetId == P_FE2CL_AROUND_DEL_TRANSPORTATION) { sP_FE2CL_AROUND_DEL_TRANSPORTATION* pkt = (sP_FE2CL_AROUND_DEL_TRANSPORTATION*)pktBuf; pkt->eTT = 3; pkt->iCnt = count; @@ -131,23 +132,21 @@ static void sendAroundDelPacket(const EntityRef recipient, std::vector static void bufferAppearanceData(std::vector>& buckets, const T& data) { if (buckets.empty()) - buckets.push_back(Bucket()); - Bucket& bucket = buckets[buckets.size() - 1]; - assert(!bucket.isFull()); + buckets.push_back({}); + auto& bucket = buckets[buckets.size() - 1]; bucket.add(data); if (bucket.isFull()) - buckets.push_back(Bucket()); + buckets.push_back({}); } template static void bufferIdForDisappearance(std::vector>& buckets, int32_t id) { if (buckets.empty()) - buckets.push_back(Bucket()); - Bucket& bucket = buckets[buckets.size() - 1]; - assert(!bucket.isFull()); + buckets.push_back({}); + auto& bucket = buckets[buckets.size() - 1]; bucket.add(id); if (bucket.isFull()) - buckets.push_back(Bucket()); + buckets.push_back({}); } void Chunking::addEntityToChunks(std::set chnks, const EntityRef ref) { @@ -177,8 +176,7 @@ void Chunking::addEntityToChunks(std::set chnks, const EntityRef ref) { sNPCAppearanceData npcData; sShinyAppearanceData eggData; sTransportationAppearanceData busData; - switch(otherRef.kind) - { + switch(otherRef.kind) { case EntityKind::PLAYER: pcData = dynamic_cast(other)->getAppearanceData(); bufferAppearanceData(pcAppearances, pcData); @@ -216,17 +214,16 @@ void Chunking::addEntityToChunks(std::set chnks, const EntityRef ref) { } } - if (ref.kind != EntityKind::PLAYER) - return; // nothing to send - - if (!pcAppearances.empty()) - sendAroundPacket(ref, pcAppearances, P_FE2CL_PC_AROUND); - if (!npcAppearances.empty()) - sendAroundPacket(ref, npcAppearances, P_FE2CL_NPC_AROUND); - if (!shinyAppearances.empty()) - sendAroundPacket(ref, shinyAppearances, P_FE2CL_SHINY_AROUND); - if (!transportationAppearances.empty()) - sendAroundPacket(ref, transportationAppearances, P_FE2CL_TRANSPORTATION_AROUND); + if (ref.kind == EntityKind::PLAYER) { + if (!pcAppearances.empty()) + sendAroundPackets(ref, pcAppearances, P_FE2CL_PC_AROUND); + if (!npcAppearances.empty()) + sendAroundPackets(ref, npcAppearances, P_FE2CL_NPC_AROUND); + if (!shinyAppearances.empty()) + sendAroundPackets(ref, shinyAppearances, P_FE2CL_SHINY_AROUND); + if (!transportationAppearances.empty()) + sendAroundPackets(ref, transportationAppearances, P_FE2CL_TRANSPORTATION_AROUND); + } } void Chunking::removeEntityFromChunks(std::set chnks, const EntityRef ref) { @@ -236,7 +233,7 @@ void Chunking::removeEntityFromChunks(std::set chnks, const EntityRef re std::vector> pcDisappearances; std::vector> npcDisappearances; std::vector> shinyDisappearances; - std::vector> transportationDisappearances; + std::vector> transportationDisappearances; for (Chunk *chunk : chnks) { for (const EntityRef otherRef : chunk->entities) { // skip oneself @@ -253,8 +250,7 @@ void Chunking::removeEntityFromChunks(std::set chnks, const EntityRef re // notify this *player* of the departure of all visible Entities if (ref.kind == EntityKind::PLAYER && other->isExtant()) { int32_t id; - switch(otherRef.kind) - { + switch(otherRef.kind) { case EntityKind::PLAYER: id = dynamic_cast(other)->iID; bufferIdForDisappearance(pcDisappearances, id); @@ -286,17 +282,16 @@ void Chunking::removeEntityFromChunks(std::set chnks, const EntityRef re } } - if (ref.kind != EntityKind::PLAYER) - return; // nothing to send - - if (!pcDisappearances.empty()) - sendAroundDelPacket(ref, pcDisappearances, false, P_FE2CL_AROUND_DEL_PC); - if (!npcDisappearances.empty()) - sendAroundDelPacket(ref, npcDisappearances, false, P_FE2CL_AROUND_DEL_NPC); - if (!shinyDisappearances.empty()) - sendAroundDelPacket(ref, shinyDisappearances, false, P_FE2CL_AROUND_DEL_SHINY); - if (!transportationDisappearances.empty()) - sendAroundDelPacket(ref, transportationDisappearances, true, P_FE2CL_AROUND_DEL_TRANSPORTATION); + if (ref.kind == EntityKind::PLAYER) { + if (!pcDisappearances.empty()) + sendAroundDelPackets(ref, pcDisappearances, P_FE2CL_AROUND_DEL_PC); + if (!npcDisappearances.empty()) + sendAroundDelPackets(ref, npcDisappearances, P_FE2CL_AROUND_DEL_NPC); + if (!shinyDisappearances.empty()) + sendAroundDelPackets(ref, shinyDisappearances, P_FE2CL_AROUND_DEL_SHINY); + if (!transportationDisappearances.empty()) + sendAroundDelPackets(ref, transportationDisappearances, P_FE2CL_AROUND_DEL_TRANSPORTATION); + } } static void emptyChunk(ChunkPos chunkPos) {