-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
2448 lines (2135 loc) · 90.4 KB
/
main.c
File metadata and controls
2448 lines (2135 loc) · 90.4 KB
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
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
*--------------------------------------
* Program Name: BTDCE
* Author: Everyday Code & EKB
* License:
* Description: "BTD remake for the TI-84 Plus CE."
*--------------------------------------
*/
// standard libraries
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
// graphing calc libraries
#include <debug.h> // for dbg_printf()
#include <fileioc.h>
#include <graphx.h>
#include <keypadc.h>
#include <sys/rtc.h>
// converted graphics files
#include "gfx/gfx.h" // palette only
#include "gfx/btdtw1_gfx.h" // tower sprites 1 (appvar)
#include "gfx/btdtw2_gfx.h" // tower sprites 2 (appvar)
#include "gfx/btdbln_gfx.h" // bloon sprites (appvar)
#include "gfx/btdui_gfx.h" // ui sprites (appvar)
// our code
#include "angle_lut.h"
#include "bloons.h"
#include "freeplay.h"
#include "list.h"
#include "path.h"
#include "save.h"
#include "spacial_partition.h"
#include "structs.h"
#include "towers.h"
#include "utils.h"
#define SCREEN_WIDTH 320
#define SCREEN_HEIGHT 240
#define MAX_BLOONS 75 /* hard cap — children deferred and drip-fed back in */
#define FREEZE_DURATION 30 /* frames bloon stays frozen (~0.5s) */
#define SLOW_DURATION 90 /* frames bloon stays slowed */
#define SLOW_FACTOR 2 /* speed divisor when glued */
#define KEY_DELAY 8 /* frames between menu key repeats (~150ms) */
/* Speed button position */
#define SPEED_BTN_X (SCREEN_WIDTH - 10 - 32)
#define SPEED_BTN_Y (SCREEN_HEIGHT - 10 - 32)
#define SPEED_BTN_W 32
#define SPEED_BTN_H 32
#define SP_CELL_SIZE 40 /* spatial partition cell size (bigger = fewer boundary misses) */
/* Cursor acceleration: ramps from 2 to 6 px/frame over ~20 frames of holding */
static uint8_t cursor_hold_frames = 0;
gfx_sprite_t* bloon_sprite_table[NUM_BLOON_TYPES];
gfx_sprite_t* bloon_sprite_regrow[NUM_BLOON_TYPES];
gfx_sprite_t* bloon_sprite_camo[NUM_BLOON_TYPES];
gfx_sprite_t* bloon_sprite_regrow_camo[NUM_BLOON_TYPES];
gfx_sprite_t* tower_sprite_table[NUM_TOWER_TYPES];
gfx_sprite_t* tower_projectile_table[NUM_TOWER_TYPES];
/* Tower sprites all face DOWN in raw images (corrected by +128 in draw). */
static const uint8_t tower_native_angle[NUM_TOWER_TYPES] = {
192, /* DART */
192, /* TACK */
192, /* SNIPER */
192, /* BOMB */
192, /* BOOMERANG */
192, /* NINJA */
0, /* ICE */
192, /* GLUE */
};
/* Projectile sprite native facing direction. */
static const uint8_t proj_native_angle[NUM_TOWER_TYPES] = {
64, /* DART: big_dart faces down */
192, /* TACK: tack spike faces up */
192, /* SNIPER: N/A (hitscan) */
192, /* BOMB: bomb_small faces up */
0, /* BOOMERANG: wood_rang_right faces right */
192, /* NINJA: ninja_star faces up (radial) */
0, /* ICE: N/A (area) */
0, /* GLUE: N/A (no sprite) */
};
/* ── Difficulty Cost Adjustment ──────────────────────────────────────── */
static uint8_t g_difficulty = 1; /* 0=easy, 1=medium, 2=hard; set by difficulty screen */
uint16_t adjusted_cost(uint16_t base) {
uint16_t cost;
switch (g_difficulty) {
case 0: cost = (base * 85 + 50) / 100; break; /* Easy: 0.85x */
case 2: cost = (base * 108 + 50) / 100; break; /* Hard: 1.08x */
default: return base; /* Medium: 1.0x */
}
return ((cost + 2) / 5) * 5; /* Round to nearest $5 (wiki standard) */
}
/* ── Apply Upgrades ──────────────────────────────────────────────────── */
void apply_upgrades(tower_t* tower) {
const tower_data_t* base = &TOWER_DATA[tower->type];
/* Start from base stats */
tower->damage = base->damage;
tower->pierce = base->pierce;
tower->range = base->range;
tower->damage_type = base->damage_type;
tower->can_see_camo = base->can_see_camo;
tower->projectile_count = base->projectile_count;
tower->projectile_speed = base->projectile_speed;
tower->sprite = tower_sprite_table[tower->type];
/* Reset ability fields */
tower->splash_radius = (tower->type == TOWER_BOMB) ? 8 : 0; /* bombs always explode */
tower->is_homing = 0;
tower->stun_on_hit = 0;
tower->has_aura = 0;
tower->dot_damage = 0;
tower->dot_interval = 0;
tower->slow_duration = SLOW_DURATION;
tower->moab_damage_mult = 1;
tower->permafrost = 0;
tower->distraction = 0;
tower->glue_soak = 0;
tower->strips_camo = 0;
int atk_pct_mod = 0; /* cumulative attack speed % modifier */
/* Apply upgrades from both paths */
for (int path = 0; path < 2; path++) {
for (int level = 0; level < tower->upgrades[path]; level++) {
const upgrade_t* upg = &TOWER_UPGRADES[tower->type][path][level];
tower->damage += upg->delta_damage;
tower->pierce += upg->delta_pierce;
tower->range += upg->delta_range;
atk_pct_mod += upg->delta_atk_pct;
tower->projectile_count += upg->delta_proj_count;
if (upg->grants_camo) tower->can_see_camo = 1;
if (upg->damage_type_override) tower->damage_type = upg->damage_type_override;
/* Ability fields */
tower->splash_radius += upg->delta_splash;
if (upg->grants_homing) tower->is_homing = 1;
if (upg->grants_stun > tower->stun_on_hit) tower->stun_on_hit = upg->grants_stun;
if (upg->grants_aura) tower->has_aura = 1;
tower->dot_damage += upg->delta_dot_damage;
tower->dot_interval = (int8_t)tower->dot_interval + upg->delta_dot_interval;
if (upg->moab_mult > tower->moab_damage_mult) tower->moab_damage_mult = upg->moab_mult;
if (upg->grants_permafrost) tower->permafrost = 1;
if (upg->grants_distraction) tower->distraction = 1;
if (upg->grants_glue_soak) tower->glue_soak = 1;
if (upg->grants_strips_camo) tower->strips_camo = 1;
tower->slow_duration += upg->delta_slow_duration;
}
}
/* Compute effective cooldown from base attack frames + percentage modifier */
int effective_frames = (int)base->atk_frames;
if (atk_pct_mod != 0) {
effective_frames = (effective_frames * (100 + atk_pct_mod)) / 100;
if (effective_frames < 2) effective_frames = 2; /* minimum 2 frames */
}
tower->cooldown = (uint16_t)effective_frames;
}
/* ── Prediction & Targeting ──────────────────────────────────────────── */
position_t predict_bloon_position(bloon_t* bloon, path_t* path) {
position_t predicted_pos = bloon->position;
if (bloon->segment < path->num_points - 1) {
position_t target = path->points[bloon->segment + 1];
int dx = target.x - bloon->position.x;
int dy = target.y - bloon->position.y;
int dist = abs(dx) + abs(dy); // Manhattan distance (fast)
if (dist > 0) {
int speed = BLOON_DATA[bloon->type].speed_fp >> 8;
// Look ahead factor of 3
predicted_pos.x += (dx * speed * 3) / dist;
predicted_pos.y += (dy * speed * 3) / dist;
}
}
return predicted_pos;
}
bloon_t* find_target_bloon(game_t* game, tower_t* tower) {
bloon_t* target = NULL;
int range_sq = (int)tower->range * (int)tower->range;
int best_val = 0;
int best_dist_sq = 0;
bool have_target = false;
list_ele_t* curr_box = game->bloons->inited_boxes->head;
while (curr_box != NULL) {
list_ele_t* curr_elem = ((queue_t*)(curr_box->value))->head;
while (curr_elem != NULL) {
bloon_t* bloon = (bloon_t*)(curr_elem->value);
/* Skip camo bloons if tower can't see camo */
if ((bloon->modifiers & MOD_CAMO) && !tower->can_see_camo) {
curr_elem = curr_elem->next;
continue;
}
int dx = bloon->position.x - tower->position.x;
int dy = bloon->position.y - tower->position.y;
int dist_sq = dx * dx + dy * dy;
if (dist_sq <= range_sq) {
bool better = false;
switch (tower->target_mode) {
case TARGET_FIRST: {
/* Furthest along path (highest segment, then highest progress) */
int val = (int)bloon->segment * 1000 + (int)(bloon->progress >> 4);
if (!have_target || val > best_val) {
best_val = val;
better = true;
}
break;
}
case TARGET_LAST: {
/* Least along path */
int val = (int)bloon->segment * 1000 + (int)(bloon->progress >> 4);
if (!have_target || val < best_val) {
best_val = val;
better = true;
}
break;
}
case TARGET_STRONG: {
/* Highest RBE in range */
int val = (int)BLOON_DATA[bloon->type].rbe;
if (!have_target || val > best_val) {
best_val = val;
better = true;
}
break;
}
case TARGET_CLOSE:
default: {
/* Closest distance to tower */
if (!have_target || dist_sq < best_dist_sq) {
best_dist_sq = dist_sq;
better = true;
}
break;
}
}
if (better) {
target = bloon;
have_target = true;
}
}
curr_elem = curr_elem->next;
}
curr_box = curr_box->next;
}
return target;
}
/* ── Integer Angle Calculation ───────────────────────────────────────── */
uint8_t calculate_angle_int(position_t start, position_t target) {
int16_t dx = target.x - start.x;
int16_t dy = target.y - start.y;
return iatan2(dy, dx);
}
/* ── Init Functions ──────────────────────────────────────────────────── */
tower_t* initTower(game_t* game, uint8_t type) {
tower_t* tower = safe_malloc(sizeof(tower_t), __LINE__);
memset(tower, 0, sizeof(tower_t));
tower->type = type;
tower->position = game->cursor;
tower->target_mode = TARGET_FIRST;
tower->upgrades[0] = 0;
tower->upgrades[1] = 0;
tower->total_invested = adjusted_cost(TOWER_DATA[type].cost);
tower->pop_count = 0;
tower->facing_angle = 0;
tower->tick = 0;
apply_upgrades(tower);
return tower;
}
bloon_t* initBloon(game_t* game, uint8_t type, uint8_t modifiers) {
bloon_t* bloon = safe_malloc(sizeof(bloon_t), __LINE__);
memset(bloon, 0, sizeof(bloon_t));
bloon->type = type;
bloon->modifiers = modifiers;
bloon->hp = BLOON_DATA[type].hp;
bloon->regrow_max = (modifiers & MOD_REGROW) ? type : 0;
bloon->regrow_timer = REGROW_INTERVAL;
bloon->position.x = 0 - 16; // start offscreen
bloon->position.y = game->path->points[0].y;
return bloon;
}
projectile_t* initProjectile(game_t* game, tower_t* tower, uint8_t angle) {
projectile_t* projectile = safe_malloc(sizeof(projectile_t), __LINE__);
memset(projectile, 0, sizeof(projectile_t));
projectile->position.x = tower->position.x;
projectile->position.y = tower->position.y;
projectile->speed = tower->projectile_speed;
projectile->pierce = tower->pierce;
projectile->damage = tower->damage;
projectile->damage_type = tower->damage_type;
projectile->sprite = tower_projectile_table[tower->type];
projectile->angle = angle;
projectile->lifetime = 120; /* ~2 seconds at 60fps, despawn after */
projectile->owner = (void*)tower;
/* Carry ability fields from tower */
projectile->splash_radius = tower->splash_radius;
projectile->is_homing = tower->is_homing;
projectile->stun_duration = tower->stun_on_hit;
projectile->can_see_camo = tower->can_see_camo;
projectile->dot_damage = tower->dot_damage;
projectile->dot_interval = tower->dot_interval;
projectile->glue_soak = tower->glue_soak;
projectile->strips_camo = tower->strips_camo;
(void)game;
return projectile;
}
/* ── Path Collision Check ────────────────────────────────────────────── */
bool boxesCollide(position_t p1, int width1, int height1, position_t p2,
int width2, int height2) {
return (p1.x < p2.x + width2 && p1.x + width1 > p2.x &&
p1.y < p2.y + height2 && p1.y + height1 > p2.y);
}
bool on_path(game_t* game, position_t pos, int w, int h) {
size_t num_rects = game->path->num_points - 1;
for (size_t i = 0; i < num_rects; i++) {
rectangle_t* r = &game->path->rectangles[i];
/* Use bounding box of the rectangle */
int rx = r->upper_left.x;
int ry = r->upper_left.y;
int rw = r->lower_right.x - r->upper_left.x;
int rh = r->lower_right.y - r->upper_left.y;
if (rw < 0) { rx += rw; rw = -rw; }
if (rh < 0) { ry += rh; rh = -rh; }
if (boxesCollide(pos, w, h, (position_t){rx, ry}, rw, rh)) {
return true;
}
}
return false;
}
bool overlaps_tower(game_t* game, position_t pos, int w, int h) {
list_ele_t* curr = game->towers->head;
while (curr != NULL) {
tower_t* t = (tower_t*)(curr->value);
int half = t->sprite->width / 2;
position_t tl = { t->position.x - half, t->position.y - half };
if (boxesCollide(pos, w, h, tl, t->sprite->width, t->sprite->height)) {
return true;
}
curr = curr->next;
}
return false;
}
/* ── Key Handling ────────────────────────────────────────────────────── */
void handlePlayingKeys(game_t* game) {
kb_Scan();
/* Movement - accelerates on hold (2 → 6 px/frame over ~20 frames) */
{
bool moving = (kb_Data[7] & (kb_Up | kb_Down | kb_Left | kb_Right)) != 0;
if (moving) {
if (cursor_hold_frames < 60) cursor_hold_frames++;
} else {
cursor_hold_frames = 0;
}
int spd = 2 + cursor_hold_frames / 15; /* 2,3,4,5,6 */
if (spd > 6) spd = 6;
if (kb_Data[7] & kb_Up) game->cursor.y -= spd;
if (kb_Data[7] & kb_Down) game->cursor.y += spd;
if (kb_Data[7] & kb_Left) game->cursor.x -= spd;
if (kb_Data[7] & kb_Right) game->cursor.x += spd;
}
/* Clamp cursor to screen */
if (game->cursor.x < 0) game->cursor.x = 0;
if (game->cursor.y < 0) game->cursor.y = 0;
if (game->cursor.x >= SCREEN_WIDTH) game->cursor.x = SCREEN_WIDTH - 1;
if (game->cursor.y >= SCREEN_HEIGHT) game->cursor.y = SCREEN_HEIGHT - 1;
/* Debounce action keys */
if (game->key_delay > 0) { game->key_delay--; return; }
/* + key: open buy menu */
if (kb_Data[6] & kb_Add) {
game->screen = SCREEN_BUY_MENU;
game->buy_menu_cursor = 0;
game->key_delay = KEY_DELAY;
return;
}
/* 2nd key: start round / toggle fast forward */
if (kb_Data[1] & kb_2nd) {
if (!game->round_active) {
game->round_active = true;
} else {
game->fast_forward = !game->fast_forward;
}
game->key_delay = KEY_DELAY;
}
/* Enter key */
if (kb_Data[6] & kb_Enter) {
/* Check speed/start button (cursor click) */
if (game->cursor.x >= SPEED_BTN_X && game->cursor.x < SPEED_BTN_X + SPEED_BTN_W &&
game->cursor.y >= SPEED_BTN_Y && game->cursor.y < SPEED_BTN_Y + SPEED_BTN_H) {
if (!game->round_active) {
game->round_active = true;
} else {
game->fast_forward = !game->fast_forward;
}
game->key_delay = KEY_DELAY;
} else if (game->cursor_type == CURSOR_SELECTED) {
/* Try to place tower (cursor = center of tower) */
uint8_t type = game->selected_tower_type;
uint16_t cost = adjusted_cost(TOWER_DATA[type].cost);
gfx_sprite_t* spr = tower_sprite_table[type];
int half_w = spr->width / 2;
int half_h = spr->height / 2;
position_t tl = { game->cursor.x - half_w, game->cursor.y - half_h };
bool can_afford = game->SANDBOX || (game->coins >= (int16_t)cost);
bool valid_pos = !on_path(game, tl, spr->width, spr->height) &&
!overlaps_tower(game, tl, spr->width, spr->height);
if (can_afford && valid_pos) {
if (!game->SANDBOX) game->coins -= cost;
tower_t* tower = initTower(game, type);
queue_insert_head(game->towers, (void*)tower);
game->cursor_type = CURSOR_NONE;
}
} else {
/* Try to select existing tower for upgrade (positions are centers) */
list_ele_t* curr = game->towers->head;
while (curr != NULL) {
tower_t* t = (tower_t*)(curr->value);
int half = t->sprite->width / 2;
if (game->cursor.x >= t->position.x - half &&
game->cursor.x < t->position.x + half &&
game->cursor.y >= t->position.y - half &&
game->cursor.y < t->position.y + half) {
game->selected_tower = t;
game->upgrade_path_sel = 0;
game->screen = SCREEN_UPGRADE;
game->key_delay = KEY_DELAY;
return;
}
curr = curr->next;
}
}
game->key_delay = KEY_DELAY;
}
/* Del key: deselect tower placement */
if (kb_Data[1] & kb_Del) {
game->cursor_type = CURSOR_NONE;
game->key_delay = KEY_DELAY;
}
/* Mode key: cycle target mode on hovered tower */
if (kb_Data[1] & kb_Mode) {
list_ele_t* curr = game->towers->head;
while (curr != NULL) {
tower_t* t = (tower_t*)(curr->value);
int half = t->sprite->width / 2;
if (game->cursor.x >= t->position.x - half &&
game->cursor.x < t->position.x + half &&
game->cursor.y >= t->position.y - half &&
game->cursor.y < t->position.y + half) {
t->target_mode = (t->target_mode + 1) % 4;
break;
}
curr = curr->next;
}
game->key_delay = KEY_DELAY;
}
/* Trace key: toggle sandbox */
if (kb_Data[1] & kb_Trace) {
game->SANDBOX = !game->SANDBOX;
game->key_delay = KEY_DELAY;
}
/* Graph key: skip round (sandbox only) */
if ((kb_Data[1] & kb_Graph) && game->SANDBOX) {
/* Clear all bloons */
list_ele_t* curr_box = game->bloons->inited_boxes->head;
while (curr_box != NULL) {
list_ele_t* next_box = curr_box->next;
list_ele_t* curr_elem = ((queue_t*)(curr_box->value))->head;
while (curr_elem != NULL) {
list_ele_t* next = curr_elem->next;
sp_remove(game->bloons, ((bloon_t*)(curr_elem->value))->position,
curr_elem, free);
curr_elem = next;
}
curr_box = next_box;
}
game->round_state.complete = true;
game->key_delay = KEY_DELAY;
}
/* Clear key: save & return to title */
if (kb_Data[6] & kb_Clear) {
save_game(game);
game->menu_cursor = 0;
game->screen = SCREEN_TITLE;
game->key_delay = KEY_DELAY * 3; /* extra debounce so title doesn't double-clear */
}
}
void handleBuyMenu(game_t* game) {
kb_Scan();
if (game->key_delay > 0) { game->key_delay--; return; }
/* Arrow navigation in 2x4 grid */
if (kb_Data[7] & kb_Right) {
if (game->buy_menu_cursor < NUM_TOWER_TYPES - 1)
game->buy_menu_cursor++;
game->key_delay = KEY_DELAY;
}
if (kb_Data[7] & kb_Left) {
if (game->buy_menu_cursor > 0)
game->buy_menu_cursor--;
game->key_delay = KEY_DELAY;
}
if (kb_Data[7] & kb_Down) {
if (game->buy_menu_cursor + 4 < NUM_TOWER_TYPES)
game->buy_menu_cursor += 4;
game->key_delay = KEY_DELAY;
}
if (kb_Data[7] & kb_Up) {
if (game->buy_menu_cursor >= 4)
game->buy_menu_cursor -= 4;
game->key_delay = KEY_DELAY;
}
/* Enter: select tower to place */
if (kb_Data[6] & kb_Enter) {
game->selected_tower_type = game->buy_menu_cursor;
game->cursor_type = CURSOR_SELECTED;
game->screen = SCREEN_PLAYING;
game->key_delay = KEY_DELAY;
return;
}
/* Del or Clear: cancel */
if ((kb_Data[1] & kb_Del) || (kb_Data[6] & kb_Clear)) {
game->cursor_type = CURSOR_NONE;
game->screen = SCREEN_PLAYING;
game->key_delay = KEY_DELAY;
}
}
void handleUpgradeScreen(game_t* game) {
kb_Scan();
if (game->key_delay > 0) { game->key_delay--; return; }
tower_t* tower = game->selected_tower;
/* Left/Right: switch selected upgrade path */
if (kb_Data[7] & kb_Left) {
game->upgrade_path_sel = 0;
game->key_delay = KEY_DELAY;
}
if (kb_Data[7] & kb_Right) {
game->upgrade_path_sel = 1;
game->key_delay = KEY_DELAY;
}
/* Enter: buy next upgrade on selected path */
if (kb_Data[6] & kb_Enter) {
uint8_t path = game->upgrade_path_sel;
uint8_t other = 1 - path;
/* Max 4 on primary path, but secondary path capped at 2.
* If the OTHER path already has 3+, this path can only go to 2. */
uint8_t max_level = (tower->upgrades[other] >= 3) ? 2 : 4;
if (tower->upgrades[path] < max_level) {
uint16_t cost = adjusted_cost(TOWER_UPGRADES[tower->type][path][tower->upgrades[path]].cost);
bool can_afford = game->SANDBOX || (game->coins >= (int16_t)cost);
if (can_afford) {
if (!game->SANDBOX) game->coins -= cost;
tower->total_invested += cost;
tower->upgrades[path]++;
apply_upgrades(tower);
}
}
game->key_delay = KEY_DELAY;
}
/* Mode key: cycle target mode in upgrade screen */
if (kb_Data[1] & kb_Mode) {
tower->target_mode = (tower->target_mode + 1) % 4;
game->key_delay = KEY_DELAY;
}
/* − key: sell tower */
if (kb_Data[6] & kb_Sub) {
uint16_t refund = (tower->total_invested * 70) / 100;
if (!game->SANDBOX) game->coins += refund;
/* Remove tower from the towers list */
list_ele_t* curr = game->towers->head;
while (curr != NULL) {
if ((tower_t*)(curr->value) == tower) {
remove_and_delete(game->towers, curr, free);
break;
}
curr = curr->next;
}
game->selected_tower = NULL;
game->screen = SCREEN_PLAYING;
game->key_delay = KEY_DELAY;
return;
}
/* Del or Clear: back to playing */
if ((kb_Data[1] & kb_Del) || (kb_Data[6] & kb_Clear)) {
game->selected_tower = NULL;
game->screen = SCREEN_PLAYING;
game->key_delay = KEY_DELAY;
}
}
/* ── Drawing Helpers ──────────────────────────────────────────────────── */
/* Get appropriate bloon sprite based on type + modifiers + state */
gfx_sprite_t* get_bloon_sprite(bloon_t* bloon) {
if (bloon->type == BLOON_MOAB) {
if (bloon->slow_timer > 0) return moab_acid;
if (bloon->hp <= 50) return moab_damaged_3;
if (bloon->hp <= 100) return moab_damaged_2;
if (bloon->hp <= 150) return moab_damaged_1;
return moab_undamaged;
}
if (bloon->type == BLOON_RED && bloon->slow_timer > 0) return red_acid;
/* Select sprite table based on camo/regrow modifiers */
uint8_t mods = bloon->modifiers & (MOD_CAMO | MOD_REGROW);
switch (mods) {
case MOD_REGROW: return bloon_sprite_regrow[bloon->type];
case MOD_CAMO: return bloon_sprite_camo[bloon->type];
case MOD_CAMO | MOD_REGROW: return bloon_sprite_regrow_camo[bloon->type];
default: return bloon_sprite_table[bloon->type];
}
}
/* Get travel direction angle (0-255) from current path segment */
uint8_t bloon_direction(bloon_t* bloon, path_t* path) {
uint16_t seg = bloon->segment;
if (seg >= path->num_points - 1) {
if (path->num_points >= 2) seg = path->num_points - 2;
else return 0;
}
int16_t dx = path->points[seg + 1].x - path->points[seg].x;
int16_t dy = path->points[seg + 1].y - path->points[seg].y;
return iatan2(dy, dx);
}
/* HUD bar: HP, Round, Coins -- drawn on ALL screens */
void drawHUD(game_t* game) {
gfx_SetColor(24); /* dark gray bar */
gfx_FillRectangle(0, 0, SCREEN_WIDTH, 14);
gfx_SetColor(80);
gfx_HorizLine(0, 14, SCREEN_WIDTH);
gfx_SetTextFGColor(255); /* yellow - visible in this palette */
gfx_PrintStringXY("HP: ", 4, 3);
gfx_PrintInt(game->hearts, 1);
gfx_PrintStringXY("Round: ", 80, 3);
gfx_PrintInt(game->round + 1, 1);
gfx_PrintChar('/');
if (game->freeplay) {
gfx_PrintString("FP");
} else {
gfx_PrintInt(game->max_round + 1, 1);
}
gfx_PrintStringXY("$", 180, 3);
gfx_PrintInt(game->coins, 1);
if (game->SANDBOX) {
gfx_PrintStringXY("SBX", 280, 3);
}
}
/* ── Drawing Functions ───────────────────────────────────────────────── */
void drawCursor(game_t* game) {
int x = game->cursor.x;
int y = game->cursor.y;
if (game->cursor_type == CURSOR_SELECTED) {
gfx_sprite_t* spr = tower_sprite_table[game->selected_tower_type];
int half = spr->width / 2;
gfx_TransparentSprite(spr, x - half, y - half);
/* Range circle: red if invalid placement, white if valid */
uint8_t range = TOWER_DATA[game->selected_tower_type].range;
position_t tl = { x - half, y - half };
bool valid = !on_path(game, tl, spr->width, spr->height) &&
!overlaps_tower(game, tl, spr->width, spr->height);
gfx_SetColor(valid ? 30 : 133); /* green or red */
gfx_Circle(x, y, range);
/* Cost label above tower */
{
uint16_t cost = adjusted_cost(TOWER_DATA[game->selected_tower_type].cost);
bool can_afford = game->SANDBOX || game->coins >= (int24_t)cost;
gfx_SetTextFGColor(can_afford ? 255 : 133); /* white or red */
int cy = y - half - 12;
if (cy < 0) cy = y + half + 2;
gfx_SetTextXY(x - 12, cy);
gfx_PrintChar('$');
gfx_PrintInt(cost, 1);
}
} else {
/* Small selection circle */
gfx_SetColor(255);
gfx_Circle(x, y, 5);
}
}
void drawMap(game_t* game) {
gfx_SetColor(158);
gfx_FillRectangle(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
drawGamePath(game);
}
void drawSpeedButton(game_t* game) {
/* Check if cursor is hovering over the button */
bool hover = (game->cursor.x >= SPEED_BTN_X &&
game->cursor.x < SPEED_BTN_X + SPEED_BTN_W &&
game->cursor.y >= SPEED_BTN_Y &&
game->cursor.y < SPEED_BTN_Y + SPEED_BTN_H);
if (!game->round_active) {
/* Start button: red background */
gfx_SetColor(133);
gfx_FillRectangle(SPEED_BTN_X, SPEED_BTN_Y, SPEED_BTN_W, SPEED_BTN_H);
gfx_SetColor(hover ? 255 : 200);
gfx_Rectangle(SPEED_BTN_X, SPEED_BTN_Y, SPEED_BTN_W, SPEED_BTN_H);
gfx_SetTextScale(2, 2);
gfx_SetTextFGColor(hover ? 255 : 200);
gfx_PrintStringXY(">", SPEED_BTN_X + 10, SPEED_BTN_Y + 8);
gfx_SetTextScale(1, 1);
/* "[2nd]" label centered above the button */
gfx_SetTextFGColor(255);
int lbl_w = gfx_GetStringWidth("[2nd]");
gfx_PrintStringXY("[2nd]", SPEED_BTN_X + (SPEED_BTN_W - lbl_w) / 2, SPEED_BTN_Y - 12);
return;
}
/* Speed button (round is active) */
gfx_SetColor(game->fast_forward ? 40 : 8);
gfx_FillRectangle(SPEED_BTN_X, SPEED_BTN_Y, SPEED_BTN_W, SPEED_BTN_H);
if (game->fast_forward) {
gfx_SetColor(255);
} else if (hover) {
gfx_SetColor(148);
} else {
gfx_SetColor(80);
}
gfx_Rectangle(SPEED_BTN_X, SPEED_BTN_Y, SPEED_BTN_W, SPEED_BTN_H);
gfx_SetTextScale(2, 2);
gfx_SetTextFGColor(game->fast_forward ? 255 : (hover ? 148 : 80));
if (game->fast_forward) {
gfx_PrintStringXY(">>", SPEED_BTN_X + 4, SPEED_BTN_Y + 8);
} else {
gfx_PrintStringXY(">", SPEED_BTN_X + 10, SPEED_BTN_Y + 8);
}
gfx_SetTextScale(1, 1);
}
void drawStats(game_t* game) {
drawHUD(game);
}
void drawTowers(game_t* game) {
static const char TARGET_CHARS[] = "FLSC";
list_ele_t* curr_elem = game->towers->head;
while (curr_elem != NULL) {
tower_t* tower = (tower_t*)(curr_elem->value);
int half = tower->sprite->width / 2;
if (tower->type == TOWER_TACK || tower->type == TOWER_ICE) {
gfx_TransparentSprite(tower->sprite,
tower->position.x - half,
tower->position.y - half);
} else {
uint8_t rot = (uint8_t)(tower->facing_angle - tower_native_angle[tower->type] + 128);
gfx_RotatedScaledTransparentSprite(
tower->sprite,
tower->position.x - half,
tower->position.y - half,
rot,
64
);
}
/* Show range circle + target mode + [Enter] hint if cursor hovers */
if (game->cursor.x >= tower->position.x - half &&
game->cursor.x < tower->position.x + half &&
game->cursor.y >= tower->position.y - half &&
game->cursor.y < tower->position.y + half) {
gfx_SetColor(255);
gfx_Circle(tower->position.x, tower->position.y, tower->range);
gfx_SetTextFGColor(148);
char buf[2] = { TARGET_CHARS[tower->target_mode], '\0' };
int tx = tower->position.x - 28;
int ty = tower->position.y - half - 10;
if (tx < 0) tx = 0;
gfx_PrintStringXY(buf, tx, ty);
gfx_SetTextFGColor(255);
gfx_PrintString(" [Enter]");
}
curr_elem = curr_elem->next;
}
}
void drawBloons(game_t* game) {
list_ele_t* curr_box = game->bloons->inited_boxes->head;
while (curr_box != NULL) {
list_ele_t* curr_elem = ((queue_t*)(curr_box->value))->head;
while (curr_elem != NULL) {
bloon_t* bloon = (bloon_t*)(curr_elem->value);
gfx_sprite_t* spr = get_bloon_sprite(bloon);
/* Center sprite on bloon position */
int draw_x = bloon->position.x - (spr->width / 2);
int draw_y = bloon->position.y - (spr->height / 2);
if (bloon->type == BLOON_MOAB) {
/* MOABs rotate to face travel direction.
* MOAB sprite native orientation = facing left (128).
* rotation = travel_angle - 128 */
uint8_t dir = bloon_direction(bloon, game->path);
uint8_t rot = (uint8_t)(dir - 128); /* MOAB native=left(128), CW rotation */
gfx_RotatedScaledTransparentSprite(spr, draw_x, draw_y,
rot, 64);
} else {
gfx_TransparentSprite(spr, draw_x, draw_y);
}
/* Freeze indicator: blue border */
if (bloon->freeze_timer > 0) {
gfx_SetColor(0x5F);
gfx_Rectangle(draw_x - 1, draw_y - 1,
spr->width + 2, spr->height + 2);
}
/* Stun indicator: yellow border */
if (bloon->stun_timer > 0) {
gfx_SetColor(148);
gfx_Rectangle(draw_x - 1, draw_y - 1,
spr->width + 2, spr->height + 2);
}
/* Glue indicator: green dot (non-MOAB, non-red which have acid sprite) */
if (bloon->slow_timer > 0 && bloon->type != BLOON_RED && bloon->type != BLOON_MOAB) {
gfx_SetColor(0x07);
gfx_FillCircle(bloon->position.x, bloon->position.y - (spr->height / 2) - 3, 2);
}
curr_elem = curr_elem->next;
}
curr_box = curr_box->next;
}
}
void drawProjectiles(game_t* game) {
list_ele_t* curr_box = game->projectiles->inited_boxes->head;
while (curr_box != NULL) {
list_ele_t* curr_elem = ((queue_t*)(curr_box->value))->head;
while (curr_elem != NULL) {
projectile_t* projectile = (projectile_t*)(curr_elem->value);
if (projectile->sprite != NULL) {
int half = projectile->sprite->width / 2;
/* SDK rotation is CW: rot = travel_angle - native */
uint8_t native = 192; /* default: up */
if (projectile->owner != NULL) {
native = proj_native_angle[((tower_t*)projectile->owner)->type];
}
uint8_t rot = (uint8_t)(projectile->angle - native);
gfx_RotatedScaledTransparentSprite(
projectile->sprite,
projectile->position.x - half,
projectile->position.y - half,
rot,
64 /* 100% scale */
);
} else {
/* Glue: draw small green circle */
gfx_SetColor(0x07); /* green-ish */
gfx_FillCircle(projectile->position.x, projectile->position.y, 3);
}
curr_elem = curr_elem->next;
}
curr_box = curr_box->next;
}
}
void drawBuyMenu(game_t* game) {
/* Full-screen black background */
gfx_SetColor(0);
gfx_FillRectangle(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
/* HUD at top */
drawHUD(game);
/* 2 rows x 4 columns grid */
const int cell_w = 78;
const int cell_h = 92;
const int grid_x = 2;
const int grid_y = 18;
for (int i = 0; i < NUM_TOWER_TYPES; i++) {
int col = i % 4;
int row = i / 4;
int cx = grid_x + col * (cell_w + 1);
int cy = grid_y + row * (cell_h + 2);
/* Cell border */
gfx_SetColor(80); /* gray border */
gfx_Rectangle(cx, cy, cell_w, cell_h);
/* Highlight selected cell */
if (i == game->buy_menu_cursor) {
gfx_SetColor(148); /* yellow highlight */
gfx_Rectangle(cx, cy, cell_w, cell_h);
gfx_Rectangle(cx + 1, cy + 1, cell_w - 2, cell_h - 2);
}
/* Tower sprite centered horizontally, near top of cell */
gfx_sprite_t* spr = tower_sprite_table[i];
int sx = cx + (cell_w - spr->width) / 2;
int sy = cy + 4;
gfx_TransparentSprite(spr, sx, sy);
/* Tower name centered below sprite */
gfx_SetTextFGColor(255);
int name_w = gfx_GetStringWidth(TOWER_NAMES[i]);
gfx_PrintStringXY(TOWER_NAMES[i], cx + (cell_w - name_w) / 2,
cy + cell_h - 24);
/* Cost centered below name */
{
char buf[8];
buf[0] = '$';
/* int to string for cost */
uint16_t c = adjusted_cost(TOWER_DATA[i].cost);
int len = 1;
if (c >= 1000) buf[len++] = '0' + (c / 1000) % 10;
if (c >= 100) buf[len++] = '0' + (c / 100) % 10;
if (c >= 10) buf[len++] = '0' + (c / 10) % 10;
buf[len++] = '0' + c % 10;
buf[len] = '\0';
int cost_w = gfx_GetStringWidth(buf);
gfx_PrintStringXY(buf, cx + (cell_w - cost_w) / 2, cy + cell_h - 12);
}
}
/* Bottom hint */
gfx_SetTextFGColor(255);
gfx_PrintStringXY("[Enter] Buy [Del] Back", 80, SCREEN_HEIGHT - 10);
}
void drawUpgradeScreen(game_t* game) {
tower_t* tower = game->selected_tower;
if (tower == NULL) return;
/* Full-screen black background */