-
Notifications
You must be signed in to change notification settings - Fork 59
/
GridFunctions.hpp
453 lines (397 loc) · 10.7 KB
/
GridFunctions.hpp
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
#pragma once
#include "mrboom.h"
#include "common.hpp"
#include "MrboomHelper.hpp"
#include <algorithm> // std::min
#ifndef DEBUG
#define NDEBUG
#endif
#include "assert.h"
#define MAX_PIXELS_PER_FRAME 8
#pragma pack(push, 1)
typedef struct bombInfo
{
dd infojoueur;
dd countDown;
dd offsetCell;
dw flameSize;
dw remote;
dw adderX; //+1,0,-1
dw adderY; //+1,0,-1
dw offsetX; // 0 = middle
dw offsetY;
void cell(int cell)
{
offsetCell = CELLX(cell) + CELLY(cell) * grid_size_x_with_padding;
}
int x()
{
return(CELLXWITHPADDING(offsetCell));
};
int y()
{
return(CELLYWITHPADDING(offsetCell));
};
int getPlayer()
{
int bombPlayer = -1; //will return -1 on a end of level 2 bomb.
if (offsetof(struct Mem, j1) == infojoueur)
{
bombPlayer = 0;
}
if (offsetof(struct Mem, j2) == infojoueur)
{
bombPlayer = 1;
}
if (offsetof(struct Mem, j3) == infojoueur)
{
bombPlayer = 2;
}
if (offsetof(struct Mem, j4) == infojoueur)
{
bombPlayer = 3;
}
if (offsetof(struct Mem, j5) == infojoueur)
{
bombPlayer = 4;
}
if (offsetof(struct Mem, j6) == infojoueur)
{
bombPlayer = 5;
}
if (offsetof(struct Mem, j7) == infojoueur)
{
bombPlayer = 6;
}
if (offsetof(struct Mem, j8) == infojoueur)
{
bombPlayer = 7;
}
return(bombPlayer);
}
} bombInfo;
#pragma pack(pop)
#pragma pack(push, 1)
typedef struct travelCostGrid
{
uint32_t _travelCostGrid[grid_size_x][grid_size_y]; // safe to walk walking distance, TRAVELCOST_CANTGO if cant go, -7 to +8 if player is here...
uint32_t travelCostGridJumpLeftRight[grid_size_x][grid_size_y];
uint32_t travelCostGridJumpUpDown[grid_size_x][grid_size_y];
uint32_t cost(int i, int j, int direction) const
{
return(std::min(jumpingCost(i, j, direction), _travelCostGrid[i][j])); // min: to be able to jump on a flame and come back later using the walking way. (otherwise the comeback breaks the path)
}
bool wouldInvolveJumping(int i, int j, int direction) const
{
return(jumpingCost(i, j, direction) < _travelCostGrid[i][j]);
}
uint32_t cost(int i, int j) const
{
return(_travelCostGrid[i][j]);
}
uint32_t cost(int cell) const
{
return(cost(CELLX(cell), CELLY(cell)));
}
void setWalkingCost(int cell, uint32_t cost)
{
_travelCostGrid[CELLX(cell)][CELLY(cell)] = cost;
}
void printCell(int i, int j)
{
int lr = costLeftRight(i, j);
int up = costUpDown(i, j);
int w = cost(i, j);
if (TRAVELCOST_CANTGO != w)
{
log_debug(" %03d ", w);
}
else
{
if ((lr != TRAVELCOST_CANTGO) || (up != TRAVELCOST_CANTGO))
{
if (TRAVELCOST_CANTGO != lr)
{
log_debug("%03d/", lr);
}
else
{
log_debug("---/");
}
if (TRAVELCOST_CANTGO != up)
{
log_debug("%03d ", up);
}
else
{
log_debug("--- ");
}
}
else
{
log_debug(" --- ");
}
}
}
uint32_t jumpingCost(int i, int j, int direction) const
{
switch (direction)
{
case button_left:
return(travelCostGridJumpLeftRight[i][j]);
break;
case button_right:
return(travelCostGridJumpLeftRight[i][j]);
break;
case button_up:
return(travelCostGridJumpUpDown[i][j]);
break;
case button_down:
return(travelCostGridJumpUpDown[i][j]);
break;
default:
assert(0);
break;
}
return(0);
}
uint32_t jumpingCost(int cell, int direction) const
{
int i = CELLX(cell);
int j = CELLY(cell);
return(jumpingCost(i, j, direction));
}
uint32_t costLeftRight(int i, int j) const
{
return(jumpingCost(i, j, button_left));
}
uint32_t costUpDown(int i, int j) const
{
return(jumpingCost(i, j, button_up));
}
void setJumpingCost(int i, int j, uint32_t cost, int direction)
{
switch (direction)
{
case button_left:
travelCostGridJumpLeftRight[i][j] = cost;
break;
case button_right:
travelCostGridJumpLeftRight[i][j] = cost;
break;
case button_up:
travelCostGridJumpUpDown[i][j] = cost;
break;
case button_down:
travelCostGridJumpUpDown[i][j] = cost;
break;
default:
assert(0);
break;
}
}
void setJumpingCost(int cell, uint32_t cost, int direction)
{
int i = CELLX(cell);
int j = CELLY(cell);
return(setJumpingCost(i, j, cost, direction));
}
bool canWalk(int i, int j) const
{
return(_travelCostGrid[i][j] != TRAVELCOST_CANTGO);
}
bool canWalk(int cell) const
{
return(canWalk(CELLX(cell), CELLY(cell)));
}
void init()
{
for (int j = 0; j < grid_size_y; j++)
{
for (int i = 0; i < grid_size_x; i++)
{
_travelCostGrid[i][j] = TRAVELCOST_CANTGO;
travelCostGridJumpLeftRight[i][j] = TRAVELCOST_CANTGO;
travelCostGridJumpUpDown[i][j] = TRAVELCOST_CANTGO;
}
}
}
void print()
{
for (int i = 0; i < grid_size_x; i++)
{
log_debug("__%03d__ ", i);
}
log_debug("\n");
for (int j = 0; j < grid_size_y; j++)
{
for (int i = 0; i < grid_size_x; i++)
{
printCell(i, j);
}
log_debug("-%03d-", j);
log_debug("\n");
}
}
} travelCostGrid;
#pragma pack(pop)
enum Button howToGo(int player, int toX, int toY, const travelCostGrid& travelGrid, bool&shouldJump);
bool isPlayerFastestToCell(int player, int x, int y);
bool isSameTeamTwoFastestToCell(int x, int y);
typedef void (*FunctionWithBombInfo)(struct bombInfo *);
void iterateOnBombs(FunctionWithBombInfo f);
typedef void (*FunctionWithFlameDrawingHelpfulData)(int, int, int, int, uint32_t[grid_size_x][grid_size_y], bool[grid_size_x][grid_size_y], int&);
void drawBombFlames(int player, int cell, int flameSize, FunctionWithFlameDrawingHelpfulData f, uint32_t[grid_size_x][grid_size_y], bool[grid_size_x][grid_size_y], int&);
void updateBestExplosionGrid(int player, uint32_t bestExplosionsGrid[grid_size_x][grid_size_y], const travelCostGrid& travelGrid, const uint32_t flameGrid[grid_size_x][grid_size_y], const bool dangerGrid[grid_size_x][grid_size_y]);
void updateTravelGrid(int player, bool ignoreFlames, travelCostGrid& travelGrid, const uint32_t flameGrid[grid_size_x][grid_size_y], const bool dangerGrid[grid_size_x][grid_size_y]);
void updateFlameAndDangerGridsWithBombs(int player, uint32_t flameGrid[grid_size_x][grid_size_y], bool dangerGrid[grid_size_x][grid_size_y]);
void updateDangerGridWithMonstersSickPlayersAndCulDeSacs(int player, bool dangerGrid[grid_size_x][grid_size_y]);
void updateDangerGridWithMonster4CellsTerritories(bool dangerGrid[grid_size_x][grid_size_y]);
bool flameInCell(int x, int y);
Bonus inline bonusInCell(int x, int y)
{
/*
* ;1 = bombe... (2,3,4) respirant... si c sup a 4; on est mort...
* ;5 = centre de bombe. de 5 a 11
* ;12 = ligne droite...
* ;19 = arrondie ligne droite vers la gauche...
* ;26 = arrondie ligne droite vers la droite
* ;33 = ligne verti
* ;40 arrondie verti vers le haut
* ;47-- bas
* ;54-- bonus bombe... de 54 a 63 (offset 144)
* ;64-- bonus flamme... de 64 a 73 (offset 144+320*16)
* ;74-- tete de mort de 74 a 83
* ;84-- bonus parre balle. de 84 a 93
* ;94-- bonus COEUR !!!
* ;104 -- bonus bombe retardement
* ;114 --- bonus pousseur
* ;124 --- patins a roulettes
* ;134 --- HORLOGE
* ;horloge
* bonus_4 134
* bonus_3 144,1,tribombe
* bonus_6 154
* ;oeuf
* bonus_5 193
*/
db z = m.truc2[x + y * grid_size_x_with_padding];
if ((z >= 54) && (z < 194))
{
if (z < 64)
{
return(bonus_bomb);
}
if (z < 74)
{
return(bonus_flame);
}
if (z < 84)
{
return(bonus_skull);
}
if (z < 94)
{
return(bonus_bulletproofjacket);
}
if (z < 104)
{
return(bonus_heart);
}
if (z < 114)
{
return(bonus_remote);
}
if (z < 124)
{
return(bonus_push);
}
if (z < 134)
{
return(bonus_roller);
}
if (z < 144)
{
return(bonus_time);
}
if (z < 154)
{
return(bonus_tribomb);
}
if (z < 164)
{
return(bonus_banana);
}
return(bonus_egg);
}
return(no_bonus);
}
bool monsterInCell(int x, int y);
bool playerInCell(int x, int y);
bool enemyInCell(int player, int x, int y);
bool enemyAroundCell(int player, int x, int y);
bool isCellCulDeSac(int x, int y);
extern int lastBombGridUpdate;
extern struct bombInfo *bombsGrid[grid_size_x][grid_size_y]; // NULL if no bomb, pointer to the bomb in m.liste_bombe
static void updateBombGrid(struct bombInfo *bomb)
{
bombsGrid[bomb->x()][bomb->y()] = bomb;
}
int inline updateBombGrid()
{
memset(bombsGrid, 0, sizeof(bombsGrid));
iterateOnBombs(updateBombGrid);
return(frameNumber());
}
bool inline bombInCell(int x, int y)
{
if ((!lastBombGridUpdate) || (frameNumber() != lastBombGridUpdate))
{
lastBombGridUpdate = updateBombGrid();
}
return(bombsGrid[x][y] != NULL);
}
bool somethingThatWouldStopFlame(int x, int y);
bool inline mudbrickInCell(int x, int y)
{
db brickKind = m.truc[x + y * grid_size_x_with_padding];
return(brickKind == 2);
}
bool inline brickInCell(int x, int y)
{
db brickKind = m.truc[x + y * grid_size_x_with_padding];
return((brickKind == 1) || ((brickKind >= 3) && (brickKind <= 11)));
}
bool inline brickOrSkullBonus(int x, int y)
{
if (brickInCell(x, y))
{
return(true);
}
if (mudbrickInCell(x, y))
{
return(true);
}
if (bonusInCell(x, y) == bonus_skull)
{
return(true);
}
return(false);
}
bool inline somethingThatIsNoTABombAndThatWouldStopPlayer(int x, int y)
{
if (brickOrSkullBonus(x, y))
{
return(true);
}
if (monsterInCell(x, y))
{
return(true);
}
return(false);
}
void updateMonsterIsComingGrid(bool monsterIsComingGrid[NUMBER_OF_CELLS]);
bool canPlayerBeReachedByMonster(int player);
bool canPlayerReachEnemy(int player);
void printCellInfo(int cell);
bool shouldActivateRemote(int player);