-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.c
474 lines (441 loc) · 8.09 KB
/
game.c
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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
#include <stdbool.h>
#include <stdint.h>
#include "game.h"
#include "history.h"
#define MAX_LASERS 256
struct tile board[16][16];
int tank_y, tank_x;
int tank_orient;
int tank_action;
int tank_sliding_dir;
bool tank_alive;
int num_lasers, num_visual_lasers;
struct laser lasers[MAX_LASERS], visual_lasers[256];
bool active;
int Dx[4] = {0,1,0,-1};
int Dy[4] = {-1,0,1,0};
struct laser *add_laser(int y, int x, int color, int dir)
{
struct laser *l = &lasers[num_lasers++];
l->y = y;
l->x = x;
l->dir = dir;
l->style = color << 4 | (dir&1);
return l;
}
bool check_antitank(int dir, struct point *p)
{
int y = tank_y;
int x = tank_x;
switch (dir) {
case NORTH:
while (y-- > 0) {
uint8_t obj = board[y][x].fg;
if (obj) {
if (obj == F_ANTI_S) {
p->x = x;
p->y = y;
return true;
}
return false;
}
}
return false;
case EAST:
while (x++ < 15) {
uint8_t obj = board[y][x].fg;
if (obj) {
if (obj == F_ANTI_W) {
p->x = x;
p->y = y;
return true;
}
return false;
}
}
return false;
case SOUTH:
while (y++ < 15) {
uint8_t obj = board[y][x].fg;
if (obj) {
if (obj == F_ANTI_N) {
p->x = x;
p->y = y;
return true;
}
return false;
}
}
return false;
case WEST:
while (x-- > 0) {
uint8_t obj = board[y][x].fg;
if (obj) {
if (obj == F_ANTI_E) {
p->x = x;
p->y = y;
return true;
}
return false;
}
}
return false;
}
return false;
}
void fix_tile(int y, int x)
{
struct tile *t = &board[y][x];
if (t->bg == B_WATER) {
record_sink(y, x, t->fg);
if (t->fg == F_CRATE) t->bg = B_BRIDGE;
t->fg = 0;
}
}
void try_nudge_object(int y, int x, int dir)
{
int yy = y + Dy[dir];
int xx = x + Dx[dir];
if ((yy|xx)>>4 == 0 && board[yy][xx].fg == 0) {
record_move_object(y, x, dir);
board[yy][xx].fg = board[y][x].fg;
board[y][x].fg = 0;
fix_tile(yy, xx);
}
}
void fix_laser(struct laser *l)
{
int y = l->y;
int x = l->x;
if (tank_alive) {
if (y == tank_y && x == tank_x) {
record_die();
tank_alive = false;
l->dir = -1;
return;
}
}
uint8_t obj = board[y][x].fg;
switch (obj) {
case F_WALL:
case F_BLOWN_ANTI_N:
case F_BLOWN_ANTI_E:
case F_BLOWN_ANTI_S:
case F_BLOWN_ANTI_W:
l->dir = -1;
break;
case F_CRATE:
try_nudge_object(y, x, l->dir);
l->dir = -1;
break;
case F_BRICK:
record_break_brick(y, x);
board[y][x].fg = 0;
l->dir = -1;
break;
case F_ANTI_N:
case F_ANTI_E:
case F_ANTI_S:
case F_ANTI_W:
if (obj - F_ANTI_N == ((l->dir+2)&3)) {
record_break_anti(y, x);
board[y][x].fg += 4;
} else {
try_nudge_object(y, x, l->dir);
}
l->dir = -1;
break;
case F_MIRROR_NE:
switch (l->dir) {
case SOUTH:
case WEST:
l->dir = (3-l->dir)&3;
l->style = (l->style & 0xf0) | 2;
break;
default:
try_nudge_object(y, x, l->dir);
l->dir = -1;
}
break;
case F_MIRROR_SE:
switch (l->dir) {
case WEST:
case NORTH:
l->dir = (1-l->dir)&3;
l->style = (l->style & 0xf0) | 3;
break;
default:
try_nudge_object(y, x, l->dir);
l->dir = -1;
}
break;
case F_MIRROR_SW:
switch (l->dir) {
case NORTH:
case EAST:
l->dir = (3-l->dir)&3;
l->style = (l->style & 0xf0) | 4;
break;
default:
try_nudge_object(y, x, l->dir);
l->dir = -1;
}
break;
case F_MIRROR_NW:
switch (l->dir) {
case EAST:
case SOUTH:
l->dir = (1-l->dir)&3;
l->style = (l->style & 0xf0) | 5;
break;
default:
try_nudge_object(y, x, l->dir);
l->dir = -1;
}
break;
case F_ROT_MIRROR_NE:
switch (l->dir) {
case SOUTH:
case WEST:
l->dir = (3-l->dir)&3;
l->style = (l->style & 0xf0) | 2;
break;
default:
record_rotate_mirror(y, x);
board[y][x].fg = F_ROT_MIRROR_SE;
l->dir = -1;
}
break;
case F_ROT_MIRROR_SE:
switch (l->dir) {
case WEST:
case NORTH:
l->dir = (1-l->dir)&3;
l->style = (l->style & 0xf0) | 3;
break;
default:
record_rotate_mirror(y, x);
board[y][x].fg = F_ROT_MIRROR_SW;
l->dir = -1;
}
break;
case F_ROT_MIRROR_SW:
switch (l->dir) {
case NORTH:
case EAST:
l->dir = (3-l->dir)&3;
l->style = (l->style & 0xf0) | 4;
break;
default:
record_rotate_mirror(y, x);
board[y][x].fg = F_ROT_MIRROR_NW;
l->dir = -1;
}
break;
case F_ROT_MIRROR_NW:
switch (l->dir) {
case EAST:
case SOUTH:
l->dir = (1-l->dir)&3;
l->style = (l->style & 0xf0) | 5;
break;
default:
record_rotate_mirror(y, x);
board[y][x].fg = F_ROT_MIRROR_NE;
l->dir = -1;
}
break;
}
}
bool is_clear(int y, int x, int dir)
{
y += Dy[dir];
x += Dx[dir];
return (y|x)>>4 == 0 && board[y][x].fg == 0;
}
int tank_mover_direction(void)
{
uint8_t bg = board[tank_y][tank_x].bg;
int dir;
switch (bg) {
case B_BELT_N:
case B_BELT_E:
case B_BELT_S:
case B_BELT_W:
dir = bg - B_BELT_N;
break;
case B_ICE:
case B_THIN_ICE:
dir = tank_sliding_dir;
break;
default:
dir = -1;
}
return dir >= 0 && is_clear(tank_y, tank_x, dir) ? dir : -1;
}
void move_tank(int dir)
{
tank_x += Dx[dir];
tank_y += Dy[dir];
}
void update_laser(struct laser *l)
{
switch (l->dir) {
case NORTH:
if (l->y > 0) l->y--;
else l->dir = -1;
break;
case EAST:
if (l->x < 15) l->x++;
else l->dir = -1;
break;
case SOUTH:
if (l->y < 15) l->y++;
else l->dir = -1;
break;
case WEST:
if (l->x > 0) l->x--;
else l->dir = -1;
break;
}
if (l->dir >= 0) {
// straighten lasers bent by mirrors
l->style = (l->style & 0xf0) | (l->dir&1);
fix_laser(l);
}
}
void tick(void)
{
#if 0
if (tank_action == 0 && num_lasers == 0) {
int d = tank_mover_direction();
if (d < 0) return;
}
#endif
/*
* Let the tank fire laser.
*/
if (tank_action == FIRE) {
if (num_lasers < MAX_LASERS) {
int y = tank_y;
int x = tank_x;
record_move_tank(y, x, tank_orient);
struct laser *l = add_laser(y, x, 2, tank_orient);
// if instantaneous firing is not desired, comment out
// this loop
while (l->dir >= 0) {
update_laser(l);
if (l->dir >= 0 || !board[l->y][l->x].fg) {
struct laser *vl =
&visual_lasers[num_visual_lasers++];
vl->y = l->y;
vl->x = l->x;
vl->style = l->style;
}
}
}
tank_action = 0;
}
/*
* Update lasers.
*/
for (int i=0; i<num_lasers; i++) {
update_laser(&lasers[i]);
}
/* 'd' is the direction in which the tank moves. -1 means tank does
not move. This must be done before breaking thin ice. */
int d = tank_mover_direction();
/*
* Break thin ice.
*/
uint8_t bg = board[tank_y][tank_x].bg;
if (bg == B_THIN_ICE) {
record_break_ice(tank_y, tank_x);
board[tank_y][tank_x].bg = B_WATER;
}
/*
* Update tank position.
*/
if (d >= 0) {
move_tank(d);
tank_action = 0;
} else {
tank_sliding_dir = -1;
}
switch (tank_action) {
int dir;
case TURN_UP:
case TURN_RIGHT:
case TURN_DOWN:
case TURN_LEFT:
tank_orient = tank_action - TURN_UP;
break;
case MOVE_UP:
case MOVE_RIGHT:
case MOVE_DOWN:
case MOVE_LEFT:
/* d == -1 */
dir = tank_action - MOVE_UP;
if (is_clear(tank_y, tank_x, dir)) {
record_move_tank(tank_y, tank_x, dir);
move_tank(dir);
d = dir;
}
break;
}
tank_action = 0;
/*
* Update flags according to the object the tank is sitting on.
*/
bg = board[tank_y][tank_x].bg;
switch (bg) {
case B_FLAG:
active = false;
break;
case B_WATER:
record_die();
tank_alive = false;
break;
case B_ICE:
case B_THIN_ICE:
tank_sliding_dir = d;
break;
}
/*
* Check if tank is in sight of anti-tanks.
*/
if (tank_alive) {
struct point anti;
for (int d=0; d<4; d++) {
if (check_antitank(d, &anti)) {
if (num_lasers < MAX_LASERS) {
struct laser *l = add_laser(anti.y, anti.x, 1, (d+2)&3);
// in case laser hits player immediately
update_laser(l);
}
}
}
}
/*
* Remove lasers marked for removal.
*/
for (int i=0; i<num_lasers; i++) {
if (lasers[i].dir < 0) {
int r = i+1;
int w = i;
while (r < num_lasers) {
lasers[w++] = lasers[r++];
while (lasers[r].dir < 0) r++;
}
num_lasers = w;
}
}
}
void try_set_tank_action(int a)
{
if (tank_alive && num_lasers == 0 && tank_action == 0 &&
tank_mover_direction() < 0)
{
tank_action = a;
}
}