Skip to content

Commit 353f90c

Browse files
PR feedback + cleanup
1 parent 73aab0b commit 353f90c

File tree

2 files changed

+42
-46
lines changed

2 files changed

+42
-46
lines changed

src/Bucket.hpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
#include <array>
44
#include <optional>
55

6+
#include <assert.h>
7+
68
template<class T, size_t N>
79
class Bucket {
810
std::array<T, N> buf;
@@ -13,9 +15,8 @@ class Bucket {
1315
}
1416

1517
void add(const T& item) {
16-
if (sz < N) {
17-
buf[sz++] = item;
18-
}
18+
assert(sz < N);
19+
buf[sz++] = item;
1920
}
2021

2122
std::optional<T> get(size_t idx) const {

src/Chunking.cpp

Lines changed: 38 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,12 @@ using namespace Chunking;
1313
* The initial chunkPos value before a player is placed into the world.
1414
*/
1515
const ChunkPos Chunking::INVALID_CHUNK = {};
16-
constexpr size_t MAX_PC_PER_AROUND = (CN_PACKET_BUFFER_SIZE - sizeof(int32_t)) / sizeof(sPCAppearanceData);
17-
constexpr size_t MAX_NPC_PER_AROUND = (CN_PACKET_BUFFER_SIZE - sizeof(int32_t)) / sizeof(sNPCAppearanceData);
18-
constexpr size_t MAX_SHINY_PER_AROUND = (CN_PACKET_BUFFER_SIZE - sizeof(int32_t)) / sizeof(sShinyAppearanceData);
19-
constexpr size_t MAX_TRANSPORTATION_PER_AROUND = (CN_PACKET_BUFFER_SIZE - sizeof(int32_t)) / sizeof(sTransportationAppearanceData);
20-
constexpr size_t MAX_IDS_PER_AROUND_DEL = (CN_PACKET_BUFFER_SIZE - sizeof(int32_t)) / sizeof(int32_t);
16+
constexpr size_t MAX_PC_PER_AROUND = (CN_PACKET_BUFFER_SIZE - 3 * sizeof(int32_t)) / sizeof(sPCAppearanceData);
17+
constexpr size_t MAX_NPC_PER_AROUND = (CN_PACKET_BUFFER_SIZE - 3 * sizeof(int32_t)) / sizeof(sNPCAppearanceData);
18+
constexpr size_t MAX_SHINY_PER_AROUND = (CN_PACKET_BUFFER_SIZE - 3 * sizeof(int32_t)) / sizeof(sShinyAppearanceData);
19+
constexpr size_t MAX_TRANSPORTATION_PER_AROUND = (CN_PACKET_BUFFER_SIZE - 3 * sizeof(int32_t)) / sizeof(sTransportationAppearanceData);
20+
constexpr size_t MAX_IDS_PER_AROUND_DEL = (CN_PACKET_BUFFER_SIZE - 3 * sizeof(int32_t)) / sizeof(int32_t);
21+
constexpr size_t MAX_TRANSPORTATION_IDS_PER_AROUND_DEL = MAX_IDS_PER_AROUND_DEL - 1; // 1 less for eTT
2122

2223
std::map<ChunkPos, Chunk*> Chunking::chunks;
2324

@@ -83,7 +84,7 @@ void Chunking::untrackEntity(ChunkPos chunkPos, const EntityRef ref) {
8384
}
8485

8586
template<class T, size_t N>
86-
static void sendAroundPacket(const EntityRef recipient, std::vector<Bucket<T, N>>& buckets, uint32_t packetId) {
87+
static void sendAroundPackets(const EntityRef recipient, std::vector<Bucket<T, N>>& buckets, uint32_t packetId) {
8788
assert(recipient.kind == EntityKind::PLAYER);
8889

8990
uint8_t pktBuf[CN_PACKET_BUFFER_SIZE];
@@ -100,7 +101,7 @@ static void sendAroundPacket(const EntityRef recipient, std::vector<Bucket<T, N>
100101
}
101102

102103
template<size_t N>
103-
static void sendAroundDelPacket(const EntityRef recipient, std::vector<Bucket<int32_t, N>>& buckets, bool isTransportation, uint32_t packetId) {
104+
static void sendAroundDelPackets(const EntityRef recipient, std::vector<Bucket<int32_t, N>>& buckets, uint32_t packetId) {
104105
assert(recipient.kind == EntityKind::PLAYER);
105106

106107
uint8_t pktBuf[CN_PACKET_BUFFER_SIZE];
@@ -110,7 +111,7 @@ static void sendAroundDelPacket(const EntityRef recipient, std::vector<Bucket<in
110111
assert(count <= N);
111112

112113
size_t baseSize;
113-
if (isTransportation) {
114+
if (packetId == P_FE2CL_AROUND_DEL_TRANSPORTATION) {
114115
sP_FE2CL_AROUND_DEL_TRANSPORTATION* pkt = (sP_FE2CL_AROUND_DEL_TRANSPORTATION*)pktBuf;
115116
pkt->eTT = 3;
116117
pkt->iCnt = count;
@@ -131,23 +132,21 @@ static void sendAroundDelPacket(const EntityRef recipient, std::vector<Bucket<in
131132
template<class T, size_t N>
132133
static void bufferAppearanceData(std::vector<Bucket<T, N>>& buckets, const T& data) {
133134
if (buckets.empty())
134-
buckets.push_back(Bucket<T, N>());
135-
Bucket<T, N>& bucket = buckets[buckets.size() - 1];
136-
assert(!bucket.isFull());
135+
buckets.push_back({});
136+
auto& bucket = buckets[buckets.size() - 1];
137137
bucket.add(data);
138138
if (bucket.isFull())
139-
buckets.push_back(Bucket<T, N>());
139+
buckets.push_back({});
140140
}
141141

142142
template<size_t N>
143143
static void bufferIdForDisappearance(std::vector<Bucket<int32_t, N>>& buckets, int32_t id) {
144144
if (buckets.empty())
145-
buckets.push_back(Bucket<int32_t, N>());
146-
Bucket<int32_t, N>& bucket = buckets[buckets.size() - 1];
147-
assert(!bucket.isFull());
145+
buckets.push_back({});
146+
auto& bucket = buckets[buckets.size() - 1];
148147
bucket.add(id);
149148
if (bucket.isFull())
150-
buckets.push_back(Bucket<int32_t, N>());
149+
buckets.push_back({});
151150
}
152151

153152
void Chunking::addEntityToChunks(std::set<Chunk*> chnks, const EntityRef ref) {
@@ -177,8 +176,7 @@ void Chunking::addEntityToChunks(std::set<Chunk*> chnks, const EntityRef ref) {
177176
sNPCAppearanceData npcData;
178177
sShinyAppearanceData eggData;
179178
sTransportationAppearanceData busData;
180-
switch(otherRef.kind)
181-
{
179+
switch(otherRef.kind) {
182180
case EntityKind::PLAYER:
183181
pcData = dynamic_cast<Player*>(other)->getAppearanceData();
184182
bufferAppearanceData(pcAppearances, pcData);
@@ -216,17 +214,16 @@ void Chunking::addEntityToChunks(std::set<Chunk*> chnks, const EntityRef ref) {
216214
}
217215
}
218216

219-
if (ref.kind != EntityKind::PLAYER)
220-
return; // nothing to send
221-
222-
if (!pcAppearances.empty())
223-
sendAroundPacket(ref, pcAppearances, P_FE2CL_PC_AROUND);
224-
if (!npcAppearances.empty())
225-
sendAroundPacket(ref, npcAppearances, P_FE2CL_NPC_AROUND);
226-
if (!shinyAppearances.empty())
227-
sendAroundPacket(ref, shinyAppearances, P_FE2CL_SHINY_AROUND);
228-
if (!transportationAppearances.empty())
229-
sendAroundPacket(ref, transportationAppearances, P_FE2CL_TRANSPORTATION_AROUND);
217+
if (ref.kind == EntityKind::PLAYER) {
218+
if (!pcAppearances.empty())
219+
sendAroundPackets(ref, pcAppearances, P_FE2CL_PC_AROUND);
220+
if (!npcAppearances.empty())
221+
sendAroundPackets(ref, npcAppearances, P_FE2CL_NPC_AROUND);
222+
if (!shinyAppearances.empty())
223+
sendAroundPackets(ref, shinyAppearances, P_FE2CL_SHINY_AROUND);
224+
if (!transportationAppearances.empty())
225+
sendAroundPackets(ref, transportationAppearances, P_FE2CL_TRANSPORTATION_AROUND);
226+
}
230227
}
231228

232229
void Chunking::removeEntityFromChunks(std::set<Chunk*> chnks, const EntityRef ref) {
@@ -236,7 +233,7 @@ void Chunking::removeEntityFromChunks(std::set<Chunk*> chnks, const EntityRef re
236233
std::vector<Bucket<int32_t, MAX_IDS_PER_AROUND_DEL>> pcDisappearances;
237234
std::vector<Bucket<int32_t, MAX_IDS_PER_AROUND_DEL>> npcDisappearances;
238235
std::vector<Bucket<int32_t, MAX_IDS_PER_AROUND_DEL>> shinyDisappearances;
239-
std::vector<Bucket<int32_t, MAX_IDS_PER_AROUND_DEL - 1>> transportationDisappearances;
236+
std::vector<Bucket<int32_t, MAX_TRANSPORTATION_IDS_PER_AROUND_DEL>> transportationDisappearances;
240237
for (Chunk *chunk : chnks) {
241238
for (const EntityRef otherRef : chunk->entities) {
242239
// skip oneself
@@ -253,8 +250,7 @@ void Chunking::removeEntityFromChunks(std::set<Chunk*> chnks, const EntityRef re
253250
// notify this *player* of the departure of all visible Entities
254251
if (ref.kind == EntityKind::PLAYER && other->isExtant()) {
255252
int32_t id;
256-
switch(otherRef.kind)
257-
{
253+
switch(otherRef.kind) {
258254
case EntityKind::PLAYER:
259255
id = dynamic_cast<Player*>(other)->iID;
260256
bufferIdForDisappearance(pcDisappearances, id);
@@ -286,17 +282,16 @@ void Chunking::removeEntityFromChunks(std::set<Chunk*> chnks, const EntityRef re
286282
}
287283
}
288284

289-
if (ref.kind != EntityKind::PLAYER)
290-
return; // nothing to send
291-
292-
if (!pcDisappearances.empty())
293-
sendAroundDelPacket(ref, pcDisappearances, false, P_FE2CL_AROUND_DEL_PC);
294-
if (!npcDisappearances.empty())
295-
sendAroundDelPacket(ref, npcDisappearances, false, P_FE2CL_AROUND_DEL_NPC);
296-
if (!shinyDisappearances.empty())
297-
sendAroundDelPacket(ref, shinyDisappearances, false, P_FE2CL_AROUND_DEL_SHINY);
298-
if (!transportationDisappearances.empty())
299-
sendAroundDelPacket(ref, transportationDisappearances, true, P_FE2CL_AROUND_DEL_TRANSPORTATION);
285+
if (ref.kind == EntityKind::PLAYER) {
286+
if (!pcDisappearances.empty())
287+
sendAroundDelPackets(ref, pcDisappearances, P_FE2CL_AROUND_DEL_PC);
288+
if (!npcDisappearances.empty())
289+
sendAroundDelPackets(ref, npcDisappearances, P_FE2CL_AROUND_DEL_NPC);
290+
if (!shinyDisappearances.empty())
291+
sendAroundDelPackets(ref, shinyDisappearances, P_FE2CL_AROUND_DEL_SHINY);
292+
if (!transportationDisappearances.empty())
293+
sendAroundDelPackets(ref, transportationDisappearances, P_FE2CL_AROUND_DEL_TRANSPORTATION);
294+
}
300295
}
301296

302297
static void emptyChunk(ChunkPos chunkPos) {

0 commit comments

Comments
 (0)