1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
|
#ifndef TIBIA_CONTAINERS_HH_
#define TIBIA_CONTAINERS_HH_ 1
#include "common.hh"
// NOTE(fusion): What the actual fuck. This is an ever growing dynamic array
// with each access through `operator()` growing it to make sure the requested
// index is valid, even for negative indices.
template<typename T>
struct vector{
// REGULAR FUNCTIONS
// =========================================================================
vector(int min, int max, int block){
int space = (max - min) + 1;
if(space < 1){
error("vector: Ungueltige Feldgroesse %d bis %d.\n", min, max);
space = 1;
}
if(block < 0){
error("vector: Ungueltige Blockgroesse %d.\n", block);
block = 0;
}
this->min = min;
this->max = max;
this->start = min;
this->space = space;
this->block = block;
this->initialized = false;
this->entry = new T[this->space];
}
vector(int min, int max, int block, T init) : vector(min, max, block) {
this->initialized = true;
this->init = init;
for(int i = 0; i < this->space; i += 1){
this->entry[i] = init;
}
}
// TODO(fusion): Probably missing some inlined destructor?
T *at(int index){
// TODO(fusion): This is probably not the best way to achieve this.
while(index < this->start){
int increment = this->block;
if(increment == 0){
increment = this->space;
}
T *entry = new T[this->space + increment];
for(int i = this->min; i <= this->max; i += 1){
int old_index = i - this->start;
int new_index = old_index + increment;
// TODO(fusion): Do we actually need to swap elements here? I'm
// assuming some non-trivial structures that would invoke their
// destructors when `this->entry` gets deleted just below.
std::swap(entry[new_index], this->entry[old_index]);
}
if(this->entry != NULL){
delete[] this->entry;
}
this->entry = entry;
this->start -= increment;
this->space += increment;
}
while(index >= (this->start + this->space)){
int increment = this->block;
if(increment == 0){
increment = this->space;
}
T *entry = new[this->space + increment];
for(int i = this->min; i <= this->max; i += 1){
int old_index = i - this->start;
int new_index = old_index;
// TODO(fusion): Same as above.
std::swap(entry[new_index], this->entry[old_index]);
}
if(this->entry != NULL){
delete[] this->entry;
}
this->entry = entry;
this->space += increment;
}
while(index < this->min){
this->min -= 1;
if(this->initialized){
this->entry[this->min - this->start] = this->init;
}
}
while(index > this->max){
this->max += 1;
if(this->initialized){
this->entry[this->max - this->start] = this->init;
}
}
return &this->entry[index - this->start];
}
// DATA
// =========================================================================
int min;
int max;
int start;
int space;
int block;
bool initialized;
T init;
T *entry;
};
template<typename K, typename T>
struct priority_queue_entry{
K Key;
T Data;
};
template<typename K, typename T>
struct priority_queue{
// REGULAR FUNCTIONS
// =========================================================================
priority_queue(int capacity, int increment){
Entry = new vector<priority_queue_entry<K, T>>(1, capacity, increment);
Entries = 0;
}
// TODO(fusion): Probably missing some inlined destructor?
void insert(K Key, T *Data){
this->Entries += 1;
int CurrentIndex = this->Entries;
*this->Entry->at(CurrentIndex) = {Key, *Data};
while(CurrentIndex > 1){
int ParentIndex = CurrentIndex / 2;
priority_queue_entry<K, T> *Current = this->Entry->at(CurrentIndex);
priority_queue_entry<K, T> *Parent = this->Entry->at(ParentIndex);
if(Parent->Key <= Current->Key)
break;
std::swap(*Current, *Parent);
CurrentIndex = ParentIndex;
}
}
void deleteMin(void){
if(this->Entries < 1){
error("priority_queue::deleteMin: Warteschlange ist leer.\n");
return;
}
if(this->Entries > 1){
int CurrentIndex = 1;
int LastIndex = this->Entries;
std::swap(*this->Entry->at(CurrentIndex),
*this->Entry->at(LastIndex));
// TODO(fusion): This may be an oversight but the decompiled version
// checks in the loop below would INCLUDE `LastIndex`, which I assume
// is a bug? The first and last elements were just swapped and what
// was the first element in the queue is now at the end and should be
// considered "removed".
while(1){
int SmallestIndex = CurrentIndex * 2;
if(SmallestIndex >= LastIndex){
break;
}
priority_queue_entry<K, T> *Smallest = this->Entry->at(SmallestIndex);
if((SmallestIndex + 1) < LastIndex){
priority_queue_entry<K, T> *Other = this->Entry->at(SmallestIndex + 1);
if(Other->Key < Smallest->Key){
Smallest = Other;
SmallestIndex += 1;
}
}
priority_queue_entry<K, T> *Current = this->Entry->at(CurrentIndex);
if(Current->Key <= Smallest->Key){
break;
}
std::swap(*Current, *Smallest);
CurrentIndex = SmallestIndex;
}
}
this->Entries -= 1;
}
// DATA
// =========================================================================
vector<priority_queue_entry<K, T>> *Entry;
int Entries;
};
// TODO(fusion): We only use this structure two global queues:
// priority_queue<uint32, uint32> ToDoQueue(5000, 1000);
// priority_queue<uint32, TAttackWave*> AttackWaveQueue(100, 100);
typedef<typename T>
struct matrix{
// REGULAR FUNCTIONS
// =========================================================================
matrix(int xmin, int xmax, int ymin, int ymax){
int dx = (xmax - xmin) + 1;
int dy = (ymax - ymin) + 1;
if(dx < 1 || dy < 1){
error("matrix: Ungueltige Feldgroesse %d..%d, %d..%d.\n", xmin, xmax, ymin, ymax);
if(dx < 1){
dx = 1;
}
if(dy < 1){
dy = 1;
}
}
this->xmin = xmin;
this->ymin = ymin;
this->dx = dx;
this->dy = dy;
this->entry = new T[dx * dy];
}
matrix(int xmin, int xmax, int ymin, int ymax, T init) : matrix(xmin, xmax, ymin, ymax) {
int count = this->dx * this->dy;
for(int i = 0; i < count; i += 1){
this->entry[i] = init;
}
}
// TODO(fusion): Probably missing some inlined destructor?
T *at(int x, int y){
int xoffset = x - this->xmin;
int yoffset = y - this->ymin;
if(xoffset < 0 || xoffset >= this->dx || yoffset < 0 || yoffset >= this->dy){
error("matrix::operator(): Ungueltiger Index %d/%d.\n", x, y);
return &this->entry[0];
}else{
// TODO(fusion): Are we really storing this in row major order?
return &this->entry[yoffset * this->dx + xoffset];
}
}
// DATA
// =========================================================================
int xmin;
int ymin;
int dx;
int dy;
T *entry;
};
typedef<typename T>
struct matrix3d{
// REGULAR FUNCTIONS
// =========================================================================
matrix3d(int xmin, int xmax, int ymin, int ymax, int zmin, int zmax){
int dx = (xmax - xmin) + 1;
int dy = (ymax - ymin) + 1;
int dz = (zmax - zmin) + 1;
if(dx < 1 || dy < 1 || dz < 1){
error("matrix3d: Ungueltige Feldgroesse %d..%d, %d..%d, %d..%d.\n",
xmin, xmax, ymin, ymax, zmin, zmax);
if(dx < 1){
dx = 1;
}
if(dy < 1){
dy = 1;
}
if(dz < 1){
dz = 1;
}
}
this->xmin = xmin;
this->ymin = ymin;
this->dx = dx;
this->dy = dy;
this->entry = new T[dx * dy];
}
matrix3d(int xmin, int xmax, int ymin, int ymax, int zmin, int zmax, T init)
: matrix(xmin, xmax, ymin, ymax, zmin, zmax) {
int count = this->dx * this->dy * this->dz;
for(int i = 0; i < count; i += 1){
this->entry[i] = init;
}
}
// TODO(fusion): Probably missing some inlined destructor?
T *at(int x, int y, int z){
int xoffset = x - this->xmin;
int yoffset = y - this->ymin;
int zoffset = z - this->zmin;
if(xoffset < 0 || xoffset >= this->dx
|| yoffset < 0 || yoffset >= this->dy
|| zoffset < 0 || zoffset >= this->dz){
error("matrix3d::operator(): Ungueltiger Index %d/%d/%d.\n", x, y, z);
return &this->entry[0];
}else{
// TODO(fusion): Same as `matrix::at` on the XY plane.
return &this->entry[zoffset * this->dx * this->dy
+ yoffset * this->dx
+ xoffset];
}
}
// DATA
// =========================================================================
int xmin;
int ymin;
int zmin;
int dx;
int dy;
int dz;
T *entry;
};
#endif //TIBIA_CONTAINERS_HH_
|