-
Notifications
You must be signed in to change notification settings - Fork 2
/
zombies.c
648 lines (585 loc) · 13.3 KB
/
zombies.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
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
#include "zombies.h"
#include "display.h"
#include "plants.h"
#include <audio/sidfx.h>
SIDFX SIDFXZombieFume[1] = {{
6000, 4096,
SID_CTRL_GATE | SID_CTRL_NOISE,
SID_ATK_2 | SID_DKY_6,
0xf0 | SID_DKY_240,
-400, 0,
2, 12,
10
}};
SIDFX SIDFXZombieChomp[1] = {{
2000, 4096,
SID_CTRL_GATE | SID_CTRL_NOISE,
SID_ATK_2 | SID_DKY_6,
0xf0 | SID_DKY_240,
-100, 0,
2, 12,
0
}};
// MSBs for the zombie sprites in all rows
char zombies_msbx[5];
char zombies_basemsbx;
void zombies_clear(void)
{
// Move the zombies off screen
for(char y=0; y<5; y++)
{
for(char i=0; i < ZOMBIE_SPRITES; i++)
rirq_data(zombieMux[y], 1 * ZOMBIE_SPRITES + i, 0);
zombies_msbx[y] = 0;
rirq_data(zombieMux[y], 4 * ZOMBIE_SPRITES, zombies_basemsbx);
}
}
void zombies_init(void)
{
// Clear zombie list
for(char i=0; i<5; i++)
{
zombies_first[i] = 0xff;
zombies_count[i] = 0;
}
// Init zombie free list
for(char i=0; i<31; i++)
zombies[i].next = i + 1;
zombies[31].next = 0xff;
zombies_free = 0;
}
bool zombies_add(char x, char y, ZombieType type, char extra)
{
// Check for zombie in free list and sprite limit of 6 per row
if (zombies_free != 0xff && zombies_count[y] < 6)
{
// Get a zombie node from the free list
char s = zombies_free;
zombies_free = zombies[s].next;
// Init zombie node
zombies[s].x = x;
zombies[s].phase = 0;
zombies[s].delay = 0;
zombies[s].speed = 112 + (rand() & 15);
zombies[s].extra = extra;
// Set zombie live per type
switch (type)
{
case ZOMBIE_BASE:
case ZOMBIE_BACKUP:
case ZOMBIE_BACKUP_RAISE:
zombies[s].live = 20;
break;
case ZOMBIE_CONE:
zombies[s].live = 44;
break;
case ZOMBIE_BUCKET:
zombies[s].live = 117;
break;
case ZOMBIE_POLE:
zombies[s].live = 50;
zombies[s].speed *= 2;
break;
case ZOMBIE_PAPER:
zombies[s].live = 15;
break;
case ZOMBIE_SCREENDOOR:
zombies[s].live = 117;
break;
case ZOMBIE_FOOTBALL:
zombies[s].live = 127;
zombies[s].speed *= 2;
break;
case ZOMBIE_DANCER:
zombies[s].live = 50;
break;
case ZOMBIE_RESURRECT:
zombies[s].live = 20;
break;
}
zombies[s].frozen = 0;
zombies[s].type = type;
// Attach zombie node to row list
zombies[s].next = zombies_first[y];
zombies_first[y] = s;
zombies_count[y]++;
// Success
return true;
}
else
return false;
}
void zombies_grave(ZombieType type)
{
// Loop over all rows
for(char y=0; y<5; y++)
{
// Loop over all plants
char p = plant_first[y];
while (p != 0xff)
{
// If it is a tombstone, add a zombie
if (plant_grid[y][p].type == PT_TOMBSTONE)
zombies_add(p * 16 + 24, y, type, 0);
p = plant_grid[y][p].next;
}
}
}
void zombies_set_msbx(char mask, char val)
{
// Check if msb changed
if ((zombies_basemsbx & mask) != val)
{
// Loop over all rows and set zombie msb
zombies_basemsbx = (zombies_basemsbx & ~mask) | val;
for(char y=0; y<5; y++)
{
rirq_data(zombieMux[y], 4 * ZOMBIE_SPRITES, zombies_msbx[y] | zombies_basemsbx);
}
}
}
void zombie_damage(char s, char damage)
{
// New (un)live of zombie
int live = zombies[s].live - damage;
// Clamp
if (live < -128)
live = -128;
zombies[s].live = live;
// Mark zombie for flashing
zombies[s].frozen |= 0x80;
}
void zombies_splash(char x, char y, char w, char damage)
{
// Loop over all zombies in row
char s = zombies_first[y];
while (s != 0xff)
{
// Damage the ones near to us
if (zombies[s].x < x + w && zombies[s].x + w >= x)
zombie_damage(s, damage);
s = zombies[s].next;
}
}
void zombies_freeze_all(char frost)
{
// Loop over all rows
for(char y=0; y<5; y++)
{
// Loop over all zombies
char s = zombies_first[y];
while (s != 0xff)
{
// Freeze
zombies[s].frozen += frost;
s = zombies[s].next;
}
}
}
void zombies_fume(char x, char y, char w)
{
bool fume = false;
// Loop over all zombies in row
char s = zombies_first[y];
while (s != 0xff)
{
// Is zombie in fume zone
if (zombies[s].x < x + w && zombies[s].x >= x)
{
// Add damage
if (zombies[s].type == ZOMBIE_SCREENDOOR)
zombie_damage(s, 12);
else
zombie_damage(s, 2);
fume = true;
}
s = zombies[s].next;
}
if (fume)
sidfx_play(2, SIDFXZombieFume, 1);
}
// Find zombie backup dancers
void zombie_find_backup(char y, char s, char * b)
{
// So far no dancers found
b[0] = 0xff;
b[1] = 0xff;
b[2] = 0xff;
b[3] = 0xff;
// Loop over zombies in same row
char z = zombies_first[y];
while (z != 0xff)
{
// Is this a backup dancer
if (zombies[z].type == ZOMBIE_BACKUP && zombies[z].extra == s)
{
if (zombies[z].x < zombies[s].x)
b[1] = z;
else
b[2] = z;
}
z = zombies[z].next;
}
if (y > 0)
{
// Loop over zombies in row above
z = zombies_first[y - 1];
while (z != 0xff)
{
// Is this a backup dancer
if (zombies[z].type == ZOMBIE_BACKUP && zombies[z].extra == s)
b[0] = z;
z = zombies[z].next;
}
}
if (y < 4)
{
// Loop over zombies in row below
z = zombies_first[y + 1];
while (z != 0xff)
{
// Is this a backup dancer
if (zombies[z].type == ZOMBIE_BACKUP && zombies[z].extra == s)
b[3] = z;
z = zombies[z].next;
}
}
}
// Keep dancer zombie in synch with backup dancers
void zombie_sync_backup(char y, char s)
{
char b[4];
zombie_find_backup(y, s, b);
char minx = zombies[s].x;
if (b[0] != 0xff && zombies[b[0]].x > minx + 1)
minx = zombies[b[0]].x;
if (b[1] != 0xff && zombies[b[1]].x + 16 > minx + 1)
minx = zombies[b[1]].x + 16;
if (b[2] != 0xff && zombies[b[2]].x - 16 > minx + 1)
minx = zombies[b[2]].x - 16;
if (b[3] != 0xff && zombies[b[3]].x > minx + 1)
minx = zombies[b[3]].x;
zombies[s].x = minx;
}
// Release backup dancers if main zombie undies
void zombie_free_backup(char y, char s)
{
char b[4];
zombie_find_backup(y, s, b);
if (b[0] != 0xff)
zombies[b[0]].type = ZOMBIE_BASE;
if (b[1] != 0xff)
zombies[b[1]].type = ZOMBIE_BASE;
if (b[2] != 0xff)
zombies[b[2]].type = ZOMBIE_BASE;
if (b[3] != 0xff)
zombies[b[3]].type = ZOMBIE_BASE;
}
// Add missing backup dancers to zombie
void zombie_add_backup(char y, char s)
{
char b[4];
zombie_find_backup(y, s, b);
if (y > 0 && b[0] == 0xff)
zombies_add(zombies[s].x, y - 1, ZOMBIE_BACKUP_RAISE, s);
if (b[1] == 0xff)
zombies_add(zombies[s].x - 16, y, ZOMBIE_BACKUP_RAISE, s);
if (b[2] == 0xff)
zombies_add(zombies[s].x + 16, y, ZOMBIE_BACKUP_RAISE, s);
if (y < 4 && b[3] == 0xff)
zombies_add(zombies[s].x, y + 1, ZOMBIE_BACKUP_RAISE, s);
}
bool zombies_advance(char y)
{
// No MSB so far
char msbx = 0;
char nz = 0;
// Left and right boundary
char left = 0xff, right = 0;
bool lost = false;
// Loop over all zombies in row
char p = 0xff;
char s = zombies_first[y];
while (s != 0xff)
{
char n = zombies[s].next;
char color = VCOL_MED_GREY;
char px = (zombies[s].x - 16) >> 4;
// Check if zombie is realy "dead"
if (zombies[s].live <= 0)
{
switch (zombies[s].type)
{
case ZOMBIE_POLE:
case ZOMBIE_VAULT:
case ZOMBIE_BASE:
case ZOMBIE_ANGRY:
case ZOMBIE_BACKUP:
// Base zombies become a corps
zombies[s].type = ZOMBIE_CORPSE;
zombies[s].phase = 0;
break;
case ZOMBIE_CONE:
case ZOMBIE_BUCKET:
case ZOMBIE_SCREENDOOR:
case ZOMBIE_FOOTBALL:
// Protected zombies become base zombies and get a second live
zombies[s].type = ZOMBIE_BASE;
zombies[s].live += 20;
break;
case ZOMBIE_PAPER:
zombies[s].type = ZOMBIE_ANGRY;
zombies[s].live += 27;
zombies[s].speed *= 2;
break;
case ZOMBIE_CORPSE:
// Corpse zombies decay
zombies[s].phase++;
if (zombies[s].phase == 4)
zombies[s].type = ZOMBIE_NONE;
break;
case ZOMBIE_DANCER:
// Backups of dancer are released
zombie_free_backup(y, s);
zombies[s].type = ZOMBIE_CORPSE;
zombies[s].phase = 0;
break;
}
}
if (zombies[s].type >= ZOMBIE_BASE)
{
// Flash zombie
if (zombies[s].frozen & 0x80)
{
zombies[s].frozen &= 0x7f;
color = VCOL_LT_GREY;
}
// Turn zombie blue
if (zombies[s].frozen)
{
color = VCOL_LT_BLUE;
zombies[s].frozen--;
}
else if (zombies[s].type == ZOMBIE_VAULT)
{
// Vaulting zombie
unsigned d = zombies[s].delay + 128;
zombies[s].delay = d;
zombies[s].x -= 2;
if (d & 0x0100)
{
zombies[s].phase++;
if (zombies[s].phase >= 4)
{
zombies[s].type = ZOMBIE_BASE;
}
}
}
else if (zombies[s].type != ZOMBIE_BACKUP_RAISE && zombies[s].type != ZOMBIE_RESURRECT && px < 9 && plant_grid[y][px].type > PT_GROUND)
{
// Position blocked
if (zombies[s].type == ZOMBIE_POLE)
{
zombies[s].type = ZOMBIE_VAULT;
zombies[s].phase = 0;
zombies[s].delay = 0;
}
else
{
zombies[s].phase++;
// Eat from plant every 4th cycle
if (zombies[s].phase >= 10)
{
zombies[s].phase = 6;
// Reduce plant live
plant_grid[y][px].live--;
if (plant_grid[y][px].live == 0)
{
// Plant killed (may need to restore grave if it was a grave digger)
if (plant_grid[y][px].type == PT_GRAVEDIGGER_0)
plant_grid[y][px].type = PT_TOMBSTONE;
else
plant_remove(px, y);
plant_draw(px, y);
}
sidfx_play(2, SIDFXZombieChomp, 1);
}
}
}
else
{
unsigned d = zombies[s].delay + zombies[s].speed;
zombies[s].delay = d;
// Carry over into position MSB
if (d & 0x0100)
{
if (zombies[s].type == ZOMBIE_DANCER)
zombie_sync_backup(y, s);
if (zombies[s].type == ZOMBIE_DANCER && zombies[s].extra == 5)
{
if (zombies[s].phase == 0)
zombie_add_backup(y, s);
}
else if (zombies[s].type == ZOMBIE_RESURRECT || zombies[s].type == ZOMBIE_BACKUP_RAISE)
{
}
else if (zombies[s].type == ZOMBIE_BACKUP && zombies[s].extra != 0xff)
{
char dx = zombies[zombies[s].extra].x;
char x = zombies[s].x;
if (x + 4 < dx)
x = dx - 16;
else if (x > dx + 4)
x = dx + 16;
else
x = dx;
zombies[s].x = x;
}
else
zombies[s].x --;
// Animate zombie
zombies[s].phase++;
if (zombies[s].phase >= 6)
{
zombies[s].phase = 0;
if (zombies[s].type == ZOMBIE_DANCER)
{
zombies[s].extra++;
if (zombies[s].extra == 6)
{
zombies[s].extra = 0;
}
}
else if (zombies[s].type == ZOMBIE_RESURRECT)
{
zombies[s].type = ZOMBIE_BASE;
}
else if (zombies[s].type == ZOMBIE_BACKUP_RAISE)
{
zombies[s].type = ZOMBIE_BACKUP;
}
}
}
}
if (px < 9)
{
// Eaten by chomper
if (plant_grid[y][px].type == PT_CHOMPER_0 || plant_grid[y][px].type == PT_CHOMPER_1)
{
zombies[s].type = ZOMBIE_NONE;
plant_grid[y][px].type = PT_CHOMPER_EAT_0;
plant_grid[y][px].cool = 150;
plant_draw(px, y);
}
else if (px > 0 && (plant_grid[y][px - 1].type == PT_CHOMPER_0 || plant_grid[y][px - 1].type == PT_CHOMPER_1))
{
zombies[s].type = ZOMBIE_NONE;
plant_grid[y][px - 1].type = PT_CHOMPER_EAT_0;
plant_grid[y][px - 1].cool = 150;
plant_draw(px - 1, y);
}
}
}
if (zombies[s].type != ZOMBIE_NONE)
{
if (zombies[s].x == 0)
lost = true;
// Check for emergency lawn mower
if (zombies[s].x < 12 && mower_start(y))
zombies[s].x = 12;
// Update zombie bounds
if (zombies[s].x < left)
left = zombies[s].x;
if (zombies[s].x > right)
right = zombies[s].x;
// Check sprite animation
if (nz < ZOMBIE_SPRITES)
{
unsigned x = zombies[s].x << 1;
char img = zombies[s].phase;
switch (zombies[s].type)
{
case ZOMBIE_CORPSE:
img += 100 + 16;
break;
case ZOMBIE_BASE:
case ZOMBIE_BACKUP:
img += 0 + 16;
break;
case ZOMBIE_CONE:
img += 10 + 16;
break;
case ZOMBIE_POLE:
img += 20 + 16;
break;
case ZOMBIE_VAULT:
img += 26 + 16;
break;
case ZOMBIE_BUCKET:
img += 30 + 16;
break;
case ZOMBIE_PAPER:
img += 40 + 16;
break;
case ZOMBIE_ANGRY:
img += 50 + 16;
break;
case ZOMBIE_SCREENDOOR:
img += 60 + 16;
break;
case ZOMBIE_FOOTBALL:
img += 70 + 16;
break;
case ZOMBIE_DANCER:
img += 80 + 16;
break;
case ZOMBIE_BACKUP_RAISE:
case ZOMBIE_RESURRECT:
img += 90 + 16;
break;
}
// Update multiplexer
rirq_data(zombieMux[y], 1 * ZOMBIE_SPRITES + nz, x & 0xff);
rirq_data(zombieMux[y], 2 * ZOMBIE_SPRITES + nz, img);
rirq_data(zombieMux[y], 3 * ZOMBIE_SPRITES + nz, color);
// Update MSB
if (x & 0x100)
msbx |= 1 << nz;
nz++;
}
p = s;
}
else
{
// Remove zombie from list
zombies_count[y]--;
if (p == 0xff)
zombies_first[y] = n;
else
zombies[p].next = n;
zombies[s].next = zombies_free;
zombies_free = s;
}
s = n;
}
// Fill remaining sprite slots
while (nz < ZOMBIE_SPRITES)
{
rirq_data(zombieMux[y], 1 * ZOMBIE_SPRITES + nz, 0);
nz++;
}
// Set zombie msb
zombies_msbx[y] = msbx;
rirq_data(zombieMux[y], 4 * ZOMBIE_SPRITES, msbx | zombies_basemsbx);
zombies_left[y] = left;
zombies_right[y] = right;
return lost;
}
bool zombies_done(void)
{
#pragma unroll(full)
for(char i=0; i<5; i++)
if (zombies_first[i] != 0xff)
return false;
return true;
}