3
3
#include " Player.hpp"
4
4
#include " MobAI.hpp"
5
5
#include " NPCManager.hpp"
6
+ #include " Bucket.hpp"
6
7
7
8
#include < assert.h>
8
9
@@ -12,11 +13,11 @@ using namespace Chunking;
12
13
* The initial chunkPos value before a player is placed into the world.
13
14
*/
14
15
const ChunkPos Chunking::INVALID_CHUNK = {};
15
- constexpr size_t MAX_PC_PER_AROUND = (CN_PACKET_BUFFER_SIZE - 4 ) / sizeof (sPCAppearanceData );
16
- constexpr size_t MAX_NPC_PER_AROUND = (CN_PACKET_BUFFER_SIZE - 4 ) / sizeof (sNPCAppearanceData );
17
- constexpr size_t MAX_SHINY_PER_AROUND = (CN_PACKET_BUFFER_SIZE - 4 ) / sizeof (sShinyAppearanceData );
18
- constexpr size_t MAX_TRANSPORTATION_PER_AROUND = (CN_PACKET_BUFFER_SIZE - 4 ) / sizeof (sTransportationAppearanceData );
19
- constexpr size_t MAX_IDS_PER_AROUND_DEL = (CN_PACKET_BUFFER_SIZE - 4 ) / sizeof (int32_t );
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 );
20
21
21
22
std::map<ChunkPos, Chunk*> Chunking::chunks;
22
23
@@ -81,36 +82,32 @@ void Chunking::untrackEntity(ChunkPos chunkPos, const EntityRef ref) {
81
82
deleteChunk (chunkPos);
82
83
}
83
84
84
- template <class T >
85
- static void sendAroundPacket (const EntityRef recipient, std::vector<std::vector<T >>& slices, size_t maxCnt , uint32_t packetId) {
85
+ template <class T , size_t N >
86
+ static void sendAroundPacket (const EntityRef recipient, std::vector<Bucket<T, N >>& buckets , uint32_t packetId) {
86
87
assert (recipient.kind == EntityKind::PLAYER);
87
88
88
89
uint8_t pktBuf[CN_PACKET_BUFFER_SIZE];
89
- for (const auto & slice : slices ) {
90
+ for (const auto & bucket : buckets ) {
90
91
memset (pktBuf, 0 , CN_PACKET_BUFFER_SIZE);
91
- int count = slice.size ();
92
- assert (count <= maxCnt);
92
+ int count = bucket.size ();
93
93
*((int32_t *)pktBuf) = count;
94
94
T* data = (T*)(pktBuf + sizeof (int32_t ));
95
95
for (size_t i = 0 ; i < count; i++) {
96
- data[i] = slice[i] ;
96
+ data[i] = bucket. get (i). value () ;
97
97
}
98
98
recipient.sock ->sendPacket (pktBuf, packetId, sizeof (int32_t ) + (count * sizeof (T)));
99
99
}
100
100
}
101
101
102
- static void sendAroundDelPacket (const EntityRef recipient, std::vector<std::vector<int32_t >>& slices, bool isTransportation, uint32_t packetId) {
102
+ template <size_t N>
103
+ static void sendAroundDelPacket (const EntityRef recipient, std::vector<Bucket<int32_t , N>>& buckets, bool isTransportation, uint32_t packetId) {
103
104
assert (recipient.kind == EntityKind::PLAYER);
104
105
105
- size_t maxCnt = MAX_IDS_PER_AROUND_DEL;
106
- if (isTransportation)
107
- maxCnt -= 1 ; // account for eTT. sad.
108
-
109
106
uint8_t pktBuf[CN_PACKET_BUFFER_SIZE];
110
- for (const auto & slice : slices ) {
107
+ for (const auto & bucket : buckets ) {
111
108
memset (pktBuf, 0 , CN_PACKET_BUFFER_SIZE);
112
- int count = slice .size ();
113
- assert (count <= maxCnt );
109
+ int count = bucket .size ();
110
+ assert (count <= N );
114
111
115
112
size_t baseSize;
116
113
if (isTransportation) {
@@ -125,39 +122,42 @@ static void sendAroundDelPacket(const EntityRef recipient, std::vector<std::vect
125
122
int32_t * ids = (int32_t *)(pktBuf + baseSize);
126
123
127
124
for (size_t i = 0 ; i < count; i++) {
128
- ids[i] = slice[i] ;
125
+ ids[i] = bucket. get (i). value () ;
129
126
}
130
127
recipient.sock ->sendPacket (pktBuf, packetId, baseSize + (count * sizeof (int32_t )));
131
128
}
132
129
}
133
130
134
- template <class T >
135
- static void bufferAppearanceData (std::vector<std::vector<T>>& slices, const T& data, size_t maxCnt) {
136
- if (slices.empty ())
137
- slices.push_back (std::vector<T>());
138
- std::vector<T>& slice = slices[slices.size () - 1 ];
139
- slice.push_back (data);
140
- if (slice.size () == maxCnt)
141
- slices.push_back (std::vector<T>());
131
+ template <class T , size_t N>
132
+ static void bufferAppearanceData (std::vector<Bucket<T, N>>& buckets, const T& data) {
133
+ if (buckets.empty ())
134
+ buckets.push_back (Bucket<T, N>());
135
+ Bucket<T, N>& bucket = buckets[buckets.size () - 1 ];
136
+ assert (!bucket.isFull ());
137
+ bucket.add (data);
138
+ if (bucket.isFull ())
139
+ buckets.push_back (Bucket<T, N>());
142
140
}
143
141
144
- static void bufferIdForDisappearance (std::vector<std::vector<int32_t >>& slices, int32_t id, size_t maxCnt) {
145
- if (slices.empty ())
146
- slices.push_back (std::vector<int32_t >());
147
- std::vector<int32_t >& slice = slices[slices.size () - 1 ];
148
- slice.push_back (id);
149
- if (slice.size () == maxCnt)
150
- slices.push_back (std::vector<int32_t >());
142
+ template <size_t N>
143
+ static void bufferIdForDisappearance (std::vector<Bucket<int32_t , N>>& buckets, int32_t id) {
144
+ 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 ());
148
+ bucket.add (id);
149
+ if (bucket.isFull ())
150
+ buckets.push_back (Bucket<int32_t , N>());
151
151
}
152
152
153
153
void Chunking::addEntityToChunks (std::set<Chunk*> chnks, const EntityRef ref) {
154
154
Entity *ent = ref.getEntity ();
155
155
bool alive = ent->isExtant ();
156
156
157
- std::vector<std::vector <sPCAppearanceData >> pcAppearances;
158
- std::vector<std::vector <sNPCAppearanceData >> npcAppearances;
159
- std::vector<std::vector <sShinyAppearanceData >> shinyAppearances;
160
- std::vector<std::vector <sTransportationAppearanceData >> transportationAppearances;
157
+ std::vector<Bucket <sPCAppearanceData , MAX_PC_PER_AROUND >> pcAppearances;
158
+ std::vector<Bucket <sNPCAppearanceData , MAX_NPC_PER_AROUND >> npcAppearances;
159
+ std::vector<Bucket <sShinyAppearanceData , MAX_SHINY_PER_AROUND >> shinyAppearances;
160
+ std::vector<Bucket <sTransportationAppearanceData , MAX_TRANSPORTATION_PER_AROUND >> transportationAppearances;
161
161
for (Chunk *chunk : chnks) {
162
162
for (const EntityRef otherRef : chunk->entities ) {
163
163
// skip oneself
@@ -181,27 +181,27 @@ void Chunking::addEntityToChunks(std::set<Chunk*> chnks, const EntityRef ref) {
181
181
{
182
182
case EntityKind::PLAYER:
183
183
pcData = dynamic_cast <Player*>(other)->getAppearanceData ();
184
- bufferAppearanceData (pcAppearances, pcData, MAX_PC_PER_AROUND );
184
+ bufferAppearanceData (pcAppearances, pcData);
185
185
break ;
186
186
case EntityKind::SIMPLE_NPC:
187
187
npcData = dynamic_cast <BaseNPC*>(other)->getAppearanceData ();
188
- bufferAppearanceData (npcAppearances, npcData, MAX_NPC_PER_AROUND );
188
+ bufferAppearanceData (npcAppearances, npcData);
189
189
break ;
190
190
case EntityKind::COMBAT_NPC:
191
191
npcData = dynamic_cast <CombatNPC*>(other)->getAppearanceData ();
192
- bufferAppearanceData (npcAppearances, npcData, MAX_NPC_PER_AROUND );
192
+ bufferAppearanceData (npcAppearances, npcData);
193
193
break ;
194
194
case EntityKind::MOB:
195
195
npcData = dynamic_cast <Mob*>(other)->getAppearanceData ();
196
- bufferAppearanceData (npcAppearances, npcData, MAX_NPC_PER_AROUND );
196
+ bufferAppearanceData (npcAppearances, npcData);
197
197
break ;
198
198
case EntityKind::EGG:
199
199
eggData = dynamic_cast <Egg*>(other)->getShinyAppearanceData ();
200
- bufferAppearanceData (shinyAppearances, eggData, MAX_SHINY_PER_AROUND );
200
+ bufferAppearanceData (shinyAppearances, eggData);
201
201
break ;
202
202
case EntityKind::BUS:
203
203
busData = dynamic_cast <Bus*>(other)->getTransportationAppearanceData ();
204
- bufferAppearanceData (transportationAppearances, busData, MAX_TRANSPORTATION_PER_AROUND );
204
+ bufferAppearanceData (transportationAppearances, busData);
205
205
break ;
206
206
default :
207
207
break ;
@@ -220,23 +220,23 @@ void Chunking::addEntityToChunks(std::set<Chunk*> chnks, const EntityRef ref) {
220
220
return ; // nothing to send
221
221
222
222
if (!pcAppearances.empty ())
223
- sendAroundPacket (ref, pcAppearances, MAX_PC_PER_AROUND, P_FE2CL_PC_AROUND);
223
+ sendAroundPacket (ref, pcAppearances, P_FE2CL_PC_AROUND);
224
224
if (!npcAppearances.empty ())
225
- sendAroundPacket (ref, npcAppearances, MAX_NPC_PER_AROUND, P_FE2CL_NPC_AROUND);
225
+ sendAroundPacket (ref, npcAppearances, P_FE2CL_NPC_AROUND);
226
226
if (!shinyAppearances.empty ())
227
- sendAroundPacket (ref, shinyAppearances, MAX_SHINY_PER_AROUND, P_FE2CL_SHINY_AROUND);
227
+ sendAroundPacket (ref, shinyAppearances, P_FE2CL_SHINY_AROUND);
228
228
if (!transportationAppearances.empty ())
229
- sendAroundPacket (ref, transportationAppearances, MAX_TRANSPORTATION_PER_AROUND, P_FE2CL_TRANSPORTATION_AROUND);
229
+ sendAroundPacket (ref, transportationAppearances, P_FE2CL_TRANSPORTATION_AROUND);
230
230
}
231
231
232
232
void Chunking::removeEntityFromChunks (std::set<Chunk*> chnks, const EntityRef ref) {
233
233
Entity *ent = ref.getEntity ();
234
234
bool alive = ent->isExtant ();
235
235
236
- std::vector<std::vector <int32_t >> pcDisappearances;
237
- std::vector<std::vector <int32_t >> npcDisappearances;
238
- std::vector<std::vector <int32_t >> shinyDisappearances;
239
- std::vector<std::vector <int32_t >> transportationDisappearances;
236
+ std::vector<Bucket <int32_t , MAX_IDS_PER_AROUND_DEL >> pcDisappearances;
237
+ std::vector<Bucket <int32_t , MAX_IDS_PER_AROUND_DEL >> npcDisappearances;
238
+ std::vector<Bucket <int32_t , MAX_IDS_PER_AROUND_DEL >> shinyDisappearances;
239
+ std::vector<Bucket <int32_t , MAX_IDS_PER_AROUND_DEL - 1 >> transportationDisappearances;
240
240
for (Chunk *chunk : chnks) {
241
241
for (const EntityRef otherRef : chunk->entities ) {
242
242
// skip oneself
@@ -257,21 +257,21 @@ void Chunking::removeEntityFromChunks(std::set<Chunk*> chnks, const EntityRef re
257
257
{
258
258
case EntityKind::PLAYER:
259
259
id = dynamic_cast <Player*>(other)->iID ;
260
- bufferIdForDisappearance (pcDisappearances, id, MAX_IDS_PER_AROUND_DEL );
260
+ bufferIdForDisappearance (pcDisappearances, id);
261
261
break ;
262
262
case EntityKind::SIMPLE_NPC:
263
263
case EntityKind::COMBAT_NPC:
264
264
case EntityKind::MOB:
265
265
id = dynamic_cast <BaseNPC*>(other)->id ;
266
- bufferIdForDisappearance (npcDisappearances, id, MAX_IDS_PER_AROUND_DEL );
266
+ bufferIdForDisappearance (npcDisappearances, id);
267
267
break ;
268
268
case EntityKind::EGG:
269
269
id = dynamic_cast <Egg*>(other)->id ;
270
- bufferIdForDisappearance (shinyDisappearances, id, MAX_IDS_PER_AROUND_DEL );
270
+ bufferIdForDisappearance (shinyDisappearances, id);
271
271
break ;
272
272
case EntityKind::BUS:
273
273
id = dynamic_cast <Bus*>(other)->id ;
274
- bufferIdForDisappearance (transportationDisappearances, id, MAX_IDS_PER_AROUND_DEL - 1 );
274
+ bufferIdForDisappearance (transportationDisappearances, id);
275
275
break ;
276
276
default :
277
277
break ;
0 commit comments