@@ -13,11 +13,14 @@ using namespace Chunking;
13
13
* The initial chunkPos value before a player is placed into the world.
14
14
*/
15
15
const ChunkPos Chunking::INVALID_CHUNK = {};
16
- constexpr size_t MAX_PC_PER_AROUND = CN_PACKET_BODY_SIZE / sizeof (sPCAppearanceData );
17
- constexpr size_t MAX_NPC_PER_AROUND = CN_PACKET_BODY_SIZE / sizeof (sNPCAppearanceData );
18
- constexpr size_t MAX_SHINY_PER_AROUND = CN_PACKET_BODY_SIZE / sizeof (sShinyAppearanceData );
19
- constexpr size_t MAX_TRANSPORTATION_PER_AROUND = CN_PACKET_BODY_SIZE / sizeof (sTransportationAppearanceData );
20
- constexpr size_t MAX_IDS_PER_AROUND_DEL = CN_PACKET_BODY_SIZE / sizeof (int32_t );
16
+
17
+ constexpr size_t MAX_PC_PER_AROUND = (CN_PACKET_BODY_SIZE - sizeof (int32_t )) / sizeof (sPCAppearanceData );
18
+ constexpr size_t MAX_NPC_PER_AROUND = (CN_PACKET_BODY_SIZE - sizeof (int32_t )) / sizeof (sNPCAppearanceData );
19
+ constexpr size_t MAX_SHINY_PER_AROUND = (CN_PACKET_BODY_SIZE - sizeof (int32_t )) / sizeof (sShinyAppearanceData );
20
+ constexpr size_t MAX_TRANSPORTATION_PER_AROUND = (CN_PACKET_BODY_SIZE - sizeof (int32_t )) / sizeof (sTransportationAppearanceData );
21
+
22
+ constexpr size_t MAX_IDS_PER_AROUND_DEL = (CN_PACKET_BODY_SIZE - sizeof (int32_t )) / sizeof (int32_t );
23
+ constexpr size_t MAX_TRANSPORTATION_IDS_PER_AROUND_DEL = MAX_IDS_PER_AROUND_DEL - 1 ; // 1 less because of eTT
21
24
22
25
std::map<ChunkPos, Chunk*> Chunking::chunks;
23
26
@@ -83,7 +86,7 @@ void Chunking::untrackEntity(ChunkPos chunkPos, const EntityRef ref) {
83
86
}
84
87
85
88
template <class T , size_t N>
86
- static void sendAroundPacket (const EntityRef recipient, std::vector<Bucket<T, N>>& buckets, uint32_t packetId) {
89
+ static void sendAroundPackets (const EntityRef recipient, std::vector<Bucket<T, N>>& buckets, uint32_t packetId) {
87
90
assert (recipient.kind == EntityKind::PLAYER);
88
91
89
92
uint8_t pktBuf[CN_PACKET_BODY_SIZE];
@@ -100,7 +103,7 @@ static void sendAroundPacket(const EntityRef recipient, std::vector<Bucket<T, N>
100
103
}
101
104
102
105
template <size_t N>
103
- static void sendAroundDelPacket (const EntityRef recipient, std::vector<Bucket<int32_t , N>>& buckets, bool isTransportation , uint32_t packetId) {
106
+ static void sendAroundDelPackets (const EntityRef recipient, std::vector<Bucket<int32_t , N>>& buckets, uint32_t packetId) {
104
107
assert (recipient.kind == EntityKind::PLAYER);
105
108
106
109
uint8_t pktBuf[CN_PACKET_BODY_SIZE];
@@ -110,7 +113,7 @@ static void sendAroundDelPacket(const EntityRef recipient, std::vector<Bucket<in
110
113
assert (count <= N);
111
114
112
115
size_t baseSize;
113
- if (isTransportation ) {
116
+ if (packetId == P_FE2CL_AROUND_DEL_TRANSPORTATION ) {
114
117
sP_FE2CL_AROUND_DEL_TRANSPORTATION * pkt = (sP_FE2CL_AROUND_DEL_TRANSPORTATION *)pktBuf;
115
118
pkt->eTT = 3 ;
116
119
pkt->iCnt = count;
@@ -131,23 +134,21 @@ static void sendAroundDelPacket(const EntityRef recipient, std::vector<Bucket<in
131
134
template <class T , size_t N>
132
135
static void bufferAppearanceData (std::vector<Bucket<T, N>>& buckets, const T& data) {
133
136
if (buckets.empty ())
134
- buckets.push_back (Bucket<T, N>());
135
- Bucket<T, N>& bucket = buckets[buckets.size () - 1 ];
136
- assert (!bucket.isFull ());
137
+ buckets.push_back ({});
138
+ auto & bucket = buckets[buckets.size () - 1 ];
137
139
bucket.add (data);
138
140
if (bucket.isFull ())
139
- buckets.push_back (Bucket<T, N>() );
141
+ buckets.push_back ({} );
140
142
}
141
143
142
144
template <size_t N>
143
145
static void bufferIdForDisappearance (std::vector<Bucket<int32_t , N>>& buckets, int32_t id) {
144
146
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 ());
147
+ buckets.push_back ({});
148
+ auto & bucket = buckets[buckets.size () - 1 ];
148
149
bucket.add (id);
149
150
if (bucket.isFull ())
150
- buckets.push_back (Bucket< int32_t , N>() );
151
+ buckets.push_back ({} );
151
152
}
152
153
153
154
void Chunking::addEntityToChunks (std::set<Chunk*> chnks, const EntityRef ref) {
@@ -177,34 +178,33 @@ void Chunking::addEntityToChunks(std::set<Chunk*> chnks, const EntityRef ref) {
177
178
sNPCAppearanceData npcData;
178
179
sShinyAppearanceData eggData;
179
180
sTransportationAppearanceData busData;
180
- switch (otherRef.kind )
181
- {
182
- case EntityKind::PLAYER:
183
- pcData = dynamic_cast <Player*>(other)->getAppearanceData ();
184
- bufferAppearanceData (pcAppearances, pcData);
185
- break ;
186
- case EntityKind::SIMPLE_NPC:
187
- npcData = dynamic_cast <BaseNPC*>(other)->getAppearanceData ();
188
- bufferAppearanceData (npcAppearances, npcData);
189
- break ;
190
- case EntityKind::COMBAT_NPC:
191
- npcData = dynamic_cast <CombatNPC*>(other)->getAppearanceData ();
192
- bufferAppearanceData (npcAppearances, npcData);
193
- break ;
194
- case EntityKind::MOB:
195
- npcData = dynamic_cast <Mob*>(other)->getAppearanceData ();
196
- bufferAppearanceData (npcAppearances, npcData);
197
- break ;
198
- case EntityKind::EGG:
199
- eggData = dynamic_cast <Egg*>(other)->getShinyAppearanceData ();
200
- bufferAppearanceData (shinyAppearances, eggData);
201
- break ;
202
- case EntityKind::BUS:
203
- busData = dynamic_cast <Bus*>(other)->getTransportationAppearanceData ();
204
- bufferAppearanceData (transportationAppearances, busData);
205
- break ;
206
- default :
207
- break ;
181
+ switch (otherRef.kind ) {
182
+ case EntityKind::PLAYER:
183
+ pcData = dynamic_cast <Player*>(other)->getAppearanceData ();
184
+ bufferAppearanceData (pcAppearances, pcData);
185
+ break ;
186
+ case EntityKind::SIMPLE_NPC:
187
+ npcData = dynamic_cast <BaseNPC*>(other)->getAppearanceData ();
188
+ bufferAppearanceData (npcAppearances, npcData);
189
+ break ;
190
+ case EntityKind::COMBAT_NPC:
191
+ npcData = dynamic_cast <CombatNPC*>(other)->getAppearanceData ();
192
+ bufferAppearanceData (npcAppearances, npcData);
193
+ break ;
194
+ case EntityKind::MOB:
195
+ npcData = dynamic_cast <Mob*>(other)->getAppearanceData ();
196
+ bufferAppearanceData (npcAppearances, npcData);
197
+ break ;
198
+ case EntityKind::EGG:
199
+ eggData = dynamic_cast <Egg*>(other)->getShinyAppearanceData ();
200
+ bufferAppearanceData (shinyAppearances, eggData);
201
+ break ;
202
+ case EntityKind::BUS:
203
+ busData = dynamic_cast <Bus*>(other)->getTransportationAppearanceData ();
204
+ bufferAppearanceData (transportationAppearances, busData);
205
+ break ;
206
+ default :
207
+ break ;
208
208
}
209
209
}
210
210
@@ -216,17 +216,16 @@ void Chunking::addEntityToChunks(std::set<Chunk*> chnks, const EntityRef ref) {
216
216
}
217
217
}
218
218
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);
219
+ if (ref.kind == EntityKind::PLAYER) {
220
+ if (!pcAppearances.empty ())
221
+ sendAroundPackets (ref, pcAppearances, P_FE2CL_PC_AROUND);
222
+ if (!npcAppearances.empty ())
223
+ sendAroundPackets (ref, npcAppearances, P_FE2CL_NPC_AROUND);
224
+ if (!shinyAppearances.empty ())
225
+ sendAroundPackets (ref, shinyAppearances, P_FE2CL_SHINY_AROUND);
226
+ if (!transportationAppearances.empty ())
227
+ sendAroundPackets (ref, transportationAppearances, P_FE2CL_TRANSPORTATION_AROUND);
228
+ }
230
229
}
231
230
232
231
void Chunking::removeEntityFromChunks (std::set<Chunk*> chnks, const EntityRef ref) {
@@ -236,7 +235,7 @@ void Chunking::removeEntityFromChunks(std::set<Chunk*> chnks, const EntityRef re
236
235
std::vector<Bucket<int32_t , MAX_IDS_PER_AROUND_DEL>> pcDisappearances;
237
236
std::vector<Bucket<int32_t , MAX_IDS_PER_AROUND_DEL>> npcDisappearances;
238
237
std::vector<Bucket<int32_t , MAX_IDS_PER_AROUND_DEL>> shinyDisappearances;
239
- std::vector<Bucket<int32_t , MAX_IDS_PER_AROUND_DEL - 1 >> transportationDisappearances;
238
+ std::vector<Bucket<int32_t , MAX_TRANSPORTATION_IDS_PER_AROUND_DEL >> transportationDisappearances;
240
239
for (Chunk *chunk : chnks) {
241
240
for (const EntityRef otherRef : chunk->entities ) {
242
241
// skip oneself
@@ -253,28 +252,27 @@ void Chunking::removeEntityFromChunks(std::set<Chunk*> chnks, const EntityRef re
253
252
// notify this *player* of the departure of all visible Entities
254
253
if (ref.kind == EntityKind::PLAYER && other->isExtant ()) {
255
254
int32_t id;
256
- switch (otherRef.kind )
257
- {
258
- case EntityKind::PLAYER:
259
- id = dynamic_cast <Player*>(other)->iID ;
260
- bufferIdForDisappearance (pcDisappearances, id);
261
- break ;
262
- case EntityKind::SIMPLE_NPC:
263
- case EntityKind::COMBAT_NPC:
264
- case EntityKind::MOB:
265
- id = dynamic_cast <BaseNPC*>(other)->id ;
266
- bufferIdForDisappearance (npcDisappearances, id);
267
- break ;
268
- case EntityKind::EGG:
269
- id = dynamic_cast <Egg*>(other)->id ;
270
- bufferIdForDisappearance (shinyDisappearances, id);
271
- break ;
272
- case EntityKind::BUS:
273
- id = dynamic_cast <Bus*>(other)->id ;
274
- bufferIdForDisappearance (transportationDisappearances, id);
275
- break ;
276
- default :
277
- break ;
255
+ switch (otherRef.kind ) {
256
+ case EntityKind::PLAYER:
257
+ id = dynamic_cast <Player*>(other)->iID ;
258
+ bufferIdForDisappearance (pcDisappearances, id);
259
+ break ;
260
+ case EntityKind::SIMPLE_NPC:
261
+ case EntityKind::COMBAT_NPC:
262
+ case EntityKind::MOB:
263
+ id = dynamic_cast <BaseNPC*>(other)->id ;
264
+ bufferIdForDisappearance (npcDisappearances, id);
265
+ break ;
266
+ case EntityKind::EGG:
267
+ id = dynamic_cast <Egg*>(other)->id ;
268
+ bufferIdForDisappearance (shinyDisappearances, id);
269
+ break ;
270
+ case EntityKind::BUS:
271
+ id = dynamic_cast <Bus*>(other)->id ;
272
+ bufferIdForDisappearance (transportationDisappearances, id);
273
+ break ;
274
+ default :
275
+ break ;
278
276
}
279
277
}
280
278
@@ -286,17 +284,16 @@ void Chunking::removeEntityFromChunks(std::set<Chunk*> chnks, const EntityRef re
286
284
}
287
285
}
288
286
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);
287
+ if (ref.kind == EntityKind::PLAYER) {
288
+ if (!pcDisappearances.empty ())
289
+ sendAroundDelPackets (ref, pcDisappearances, P_FE2CL_AROUND_DEL_PC);
290
+ if (!npcDisappearances.empty ())
291
+ sendAroundDelPackets (ref, npcDisappearances, P_FE2CL_AROUND_DEL_NPC);
292
+ if (!shinyDisappearances.empty ())
293
+ sendAroundDelPackets (ref, shinyDisappearances, P_FE2CL_AROUND_DEL_SHINY);
294
+ if (!transportationDisappearances.empty ())
295
+ sendAroundDelPackets (ref, transportationDisappearances, P_FE2CL_AROUND_DEL_TRANSPORTATION);
296
+ }
300
297
}
301
298
302
299
static void emptyChunk (ChunkPos chunkPos) {
0 commit comments