-
Notifications
You must be signed in to change notification settings - Fork 483
Expand file tree
/
Copy pathtactical.h
More file actions
1305 lines (1159 loc) · 51.2 KB
/
Copy pathtactical.h
File metadata and controls
1305 lines (1159 loc) · 51.2 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
#include <stdlib.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <time.h>
#include "raylib.h"
#include "maps.h"
#define CELL_EMPTY 0
#define CELL_GROUND 1
#define CELL_HOLE 2
#define CELL_WALL 3
#define MAX_TEXT_ANIMATIONS 100 // max number of text animations
const Color COLOR_BACKGROUND = {6, 24, 24, 255}; // window background
const Color COLOR_CELL_GRASS = {150, 200, 150, 255}; // top of WALL cells
const Color COLOR_CELL_DIRT = {80, 50, 50, 255}; // side of WALL cells
const Color COLOR_CELL_GROUND = {150, 150, 170, 255}; // GROUND cells
// const Color COLOR_CELL_GRASS = {163, 197, 69, 255}; // top of WALL cells
// const Color COLOR_CELL_DIRT = {40, 20, 5, 255}; // side of WALL cells
// const Color COLOR_CELL_GROUND = {112, 123, 111, 255}; // GROUND cells
const Color COLOR_CELL_BORDER = RAYWHITE; // border of GROUND cells
const Color COLOR_CELL_MOVE = DARKGREEN;
const Color COLOR_CELL_MOVE_TEXT = RAYWHITE;
const Color COLOR_ACTIVE_PLAYER = RAYWHITE; // border around active player circle
const Color COLOR_PLAYER1 = RED; // player 1 color (character and circle)
const Color COLOR_PLAYER2 = GREEN; // player 2 color (character and circle)
const Color COLOR_TEXT_DEFAULT = RAYWHITE; // main text color
const Color COLOR_HEALTH = RED;
const Color COLOR_ACTION_POINTS = SKYBLUE;
const Color COLOR_MOVEMENT_POINTS = LIME;
const Color COLOR_SPELL = GOLD;
const Color COLOR_SPELL_COOLDOWN = BROWN;
const Color COLOR_CELL_SPELL = BEIGE;
const Color COLOR_CELL_ACTIVE_SPELL = {255, 161, 0, 255};
const Color COLOR_CELL_INACTIVE_SPELL = {150, 150, 255, 255};
const Color COLOR_ENTITY_NAME = PURPLE;
const Color COLOR_ENTITY_NAME_HOVER = YELLOW;
// TODO many leaks...
// forward declarations
typedef struct Tactical Tactical;
typedef struct Entity Entity;
typedef struct Spell Spell;
typedef struct GameRenderer GameRenderer;
// arghh annoying
void add_animation_text(Tactical* env, GameRenderer* renderer, const char* text, int cell, Color color, int font_size, float duration);
void compute_movement(Tactical* env, Entity* entity);
typedef struct Tactical {
int num_agents;
unsigned char* observations;
int* actions;
float* rewards;
unsigned int n_entities;
Entity* entities;
// pointers to entities (these won't be allocated), assume 1v1 for now
Entity* player1;
Entity* player2;
Entity* current_player;
int current_player_idx;
unsigned int map_width;
unsigned int map_height;
unsigned int map_size; // width * height
unsigned int* map;
Entity** cell_to_entity;
unsigned int* movement_path;
int* movement_distance;
} Tactical;
// Entity (player, summoned creature...)
struct Entity {
const char* name;
int cell;
Color color;
int health_points_total;
int action_points_total;
int movement_points_total;
int health_points_current;
int action_points_current;
int movement_points_current;
// spells
Spell* spells;
int spell_count;
};
struct Spell {
const char* name;
int ap_cost;
int cooldown;
int remaining_cooldown;
int range; // TODO add different range types (default, in a line, in diagonal...)
bool line_of_sight; // whether spell can be casted across walls
bool modifiable_range; // whether spell range can be increased or decreased from other spells
bool cast_in_line; // whether spell range can be increased or decreased from other spells
// TODO add a "zone of effect" shape that lists the deltas that the spell touches around the cell
void (*effect)(Tactical*, Entity*, int, Spell*); // pointer to a function that takes in the env, the caster and the target cell
bool (*render_animation)(Tactical*, GameRenderer*, int, int, float, Spell*); // pointer to a function that takes in the env, the renderer, the caster cell, the target cell and the progress (in seconds), that renders the spell animation and returns true when the animation is finished
int damage; // damage dealt by the spell
int animation_state;
int aoe_range; // 0: single-target; 1: 5 cells in total (1+4); 2: 13 cells total (1+4+8)
};
struct GameRenderer {
int width;
int height;
float cw; // cell width
float ch; // cell height
float offset_x; // offset for the whole map
float offset_y; // offset for the whole map
float dy; // vertical offset for wall cells
// current cell (if any) under the mouse cursor
int mx;
int my;
int mrow;
int mcol;
int mcell;
int mcell_type;
bool* movement_cells;
Spell* active_spell;
bool* spell_cells;
bool* active_spell_cells;
// for drawing
float *xa, *xb, *xc, *xd, *xe, *ya, *yb, *yc, *yd, *ye;
// animations (move)
Entity* move_anim_entity;
int* move_anim_path;
int move_anim_path_idx;
int move_anim_path_length;
float move_anim_progress;
float move_anim_dx; // delta in position with respect to the center of the cell
float move_anim_dy;
float move_anim_cells_per_second;
// animations (text)
char* text_anim_texts[MAX_TEXT_ANIMATIONS];
float text_anim_x0[MAX_TEXT_ANIMATIONS];
float text_anim_x1[MAX_TEXT_ANIMATIONS];
float text_anim_y0[MAX_TEXT_ANIMATIONS];
float text_anim_y1[MAX_TEXT_ANIMATIONS];
float text_anim_progress[MAX_TEXT_ANIMATIONS];
float text_anim_duration[MAX_TEXT_ANIMATIONS]; // in seconds
Color text_anim_color[MAX_TEXT_ANIMATIONS];
int text_anim_font_size[MAX_TEXT_ANIMATIONS];
int text_anim_count;
// animations (spells) -- only one at a time
Spell* spell_anim;
float spell_anim_progress;
int spell_anim_caster_cell;
int spell_anim_target_cell;
clock_t last_render_time;
double dt; // in seconds
float max_fps;
};
void free_tactical(Tactical* env) {
free(env->rewards);
free(env->observations);
free(env->actions);
free(env->map);
free(env->movement_path);
free(env->movement_distance);
free(env->entities);
free(env); // do this last
}
int get_cell(Tactical* env, int row, int col) {
if (row < 0 || row >= env->map_height) return -1;
if (col < 0 || col >= env->map_width) return -1;
return row * env->map_width + col;
}
int get_row(Tactical* env, int cell) {
return cell / env->map_width;
}
int get_col(Tactical* env, int cell) {
return cell % env->map_width;
}
int get_cell_with_delta(Tactical* env, int cell, int delta_row, int delta_col) {
return get_cell(env, get_row(env, cell) + delta_row, get_col(env, cell) + delta_col);
}
////////////
// SPELLS //
////////////
void update_cooldowns(Entity* entity) {
for (int i = 0; i < entity->spell_count; ++i) {
if (entity->spells[i].remaining_cooldown > 0) {
entity->spells[i].remaining_cooldown--;
}
}
}
void cast_spell(Tactical* env, Entity* caster, Spell* spell, int target_cell) {
// check if the spell can be cast
if (caster->action_points_current < spell->ap_cost) {
printf("Not enough action points to cast %s.\n", spell->name);
return;
}
if (spell->remaining_cooldown > 0) {
printf("Spell %s is on cooldown for %d more turns.\n", spell->name, spell->remaining_cooldown);
return;
}
// cast the spell
spell->effect(env, caster, target_cell, spell);
spell->animation_state = 0;
caster->action_points_current -= spell->ap_cost;
spell->remaining_cooldown = spell->cooldown;
}
void spell_explosive_arrow(Tactical* env, Entity* caster, int target_cell, Spell* spell) {
for (int delta_row = -spell->aoe_range; delta_row <= spell->aoe_range; ++delta_row) {
for (int delta_col = -(spell->aoe_range - abs(delta_row)); delta_col <= spell->aoe_range - abs(delta_row); ++delta_col) {
int cell = get_cell_with_delta(env, target_cell, delta_row, delta_col);
if (env->map[cell] != CELL_GROUND) continue;
Entity* target = env->cell_to_entity[cell];
if (target) {
target->health_points_current -= spell->damage;
}
}
}
}
bool spell_explosive_arrow_anim(Tactical* env, GameRenderer* renderer, int caster_cell, int target_cell, float t, Spell* spell) {
float xe0 = renderer->xe[caster_cell];
float xe1 = renderer->xe[target_cell];
float ye0 = renderer->ye[caster_cell];
float ye1 = renderer->ye[target_cell];
float phase1_duration = 0.5;
float phase2_duration = 0.2;
Vector2 vec = GetSplinePointBezierQuad(
(Vector2){xe0, ye0 - 2 * renderer->ch},
(Vector2){(xe0 + xe1) / 2, (ye0 + ye1) / 2 - 200},
(Vector2){xe1, ye1},
fmin(t / phase1_duration, 1.0));
if (t <= phase1_duration) {
DrawCircle(vec.x, vec.y, 10, (Color){255, 0, 0, 255});
} else if (t <= phase1_duration + phase2_duration) {
if (spell->animation_state == 0) {
spell->animation_state = 1;
// get all players hit
for (int delta_row = -spell->aoe_range; delta_row <= spell->aoe_range; ++delta_row) {
for (int delta_col = -(spell->aoe_range - abs(delta_row)); delta_col <= spell->aoe_range - abs(delta_row); ++delta_col) {
int cell = get_cell_with_delta(env, target_cell, delta_row, delta_col);
if (env->map[cell] != CELL_GROUND) continue;
Entity* target = env->cell_to_entity[cell];
if (target) {
add_animation_text(env, renderer, TextFormat("-%i HP", spell->damage),
cell, COLOR_HEALTH, 20, 1.2);
}
}
}
}
DrawCircle(vec.x, vec.y, 10 + (t - phase1_duration) * 400,
(Color){255, 0, 0, 255 * (1 - (t - phase1_duration) / phase2_duration)});
} else {
return true;
}
return false;
}
Spell create_spell_explosive_arrow() {
Spell spell;
spell.name = "Explosive Arrow";
spell.ap_cost = 4;
spell.cooldown = 0;
spell.remaining_cooldown = 0;
spell.line_of_sight = true;
spell.cast_in_line = false;
spell.range = 11;
spell.damage = 200;
spell.aoe_range = 2;
spell.effect = spell_explosive_arrow;
spell.render_animation = spell_explosive_arrow_anim;
return spell;
}
void spell_flying_arrow(Tactical* env, Entity* caster, int target_cell, Spell* spell) {
Entity* target = env->cell_to_entity[target_cell];
if (target) {
target->health_points_current -= spell->damage;
}
}
bool spell_flying_arrow_anim(Tactical* env, GameRenderer* renderer, int caster_cell, int target_cell, float t, Spell* spell) {
float xe0 = renderer->xe[caster_cell];
float xe1 = renderer->xe[target_cell];
float ye0 = renderer->ye[caster_cell];
float ye1 = renderer->ye[target_cell];
float phase1_duration = 0.8;
Vector2 vec = GetSplinePointBezierQuad(
(Vector2){xe0, ye0 - 2 * renderer->ch},
(Vector2){(xe0 + xe1) / 2, (ye0 + ye1) / 2 - 700},
(Vector2){xe1, ye1},
fmin(t / phase1_duration, 1.0));
if (t <= phase1_duration) {
DrawCircle(vec.x, vec.y, 10, (Color){0, 255, 0, 255});
} else {
Entity* target = env->cell_to_entity[target_cell];
if (target) {
add_animation_text(env, renderer, TextFormat("-%i HP", spell->damage),
target_cell, COLOR_HEALTH, 20, 1.2);
}
return true;
}
return false;
}
Spell create_spell_flying_arrow() {
Spell spell;
spell.name = "Flying Arrow";
spell.ap_cost = 3;
spell.cooldown = 0;
spell.remaining_cooldown = 0;
spell.line_of_sight = false;
spell.cast_in_line = false;
spell.range = 14;
spell.damage = 100;
spell.aoe_range = 0;
spell.effect = spell_flying_arrow;
spell.render_animation = spell_flying_arrow_anim;
return spell;
}
void spell_rooting_arrow(Tactical* env, Entity* caster, int target_cell, Spell* spell) {
Entity* target = env->cell_to_entity[target_cell];
if (target) {
target->movement_points_current -= 3;
if (target == env->current_player)
compute_movement(env, target);
}
}
bool spell_rooting_arrow_anim(Tactical* env, GameRenderer* renderer, int caster_cell, int target_cell, float t, Spell* spell) {
float xe0 = renderer->xe[caster_cell];
float xe1 = renderer->xe[target_cell];
float ye0 = renderer->ye[caster_cell];
float ye1 = renderer->ye[target_cell];
float phase1_duration = 0.3;
float phase2_duration = 0.3;
if (t <= phase1_duration) {
float progress = t / phase1_duration;
DrawLineEx(
(Vector2){xe0, ye0},
(Vector2){(1-progress)*xe0 + progress*xe1, (1-progress)*ye0 + progress*ye1},
4, (Color){200, 100, 0, 255});
} else if (t <= phase1_duration + phase2_duration) {
if (spell->animation_state == 0) {
spell->animation_state = 1;
// get all players hit
Entity* target = env->cell_to_entity[target_cell];
if (target) {
add_animation_text(env, renderer, "-3 MP", target_cell, COLOR_MOVEMENT_POINTS, 20, 1.2);
}
}
DrawLineEx((Vector2){xe0, ye0}, (Vector2){xe1, ye1}, 4, (Color){200, 100, 0, 255});
float progress = (t - phase1_duration) / phase2_duration;
for (int i = -3; i <= 3; ++i) {
float angle = -M_PI/2 + i * M_PI/12;
float new_x = xe1 + cos(angle) * 200;
float new_y = ye1 + -sin(angle) * 200;
DrawLineEx(
(Vector2){xe1, ye1},
(Vector2){(1-progress)*xe1 + progress*new_x, (1-progress)*ye1 + progress*new_y},
4, (Color){200, 100, 0, 255});
}
} else {
return true;
}
return false;
}
Spell create_spell_rooting_arrow() {
Spell spell;
spell.name = "Rooting Arrow";
spell.ap_cost = 2;
spell.cooldown = 2;
spell.remaining_cooldown = 0;
spell.line_of_sight = true;
spell.cast_in_line = false;
spell.range = 8;
spell.damage = 0;
spell.aoe_range = 0;
spell.effect = spell_rooting_arrow;
spell.render_animation = spell_rooting_arrow_anim;
return spell;
}
void spell_wind_arrow(Tactical* env, Entity* caster, int target_cell, Spell* spell) {
Entity* target = env->cell_to_entity[target_cell];
if (target) {
target->health_points_current -= spell->damage;
}
}
bool spell_wind_arrow_anim(Tactical* env, GameRenderer* renderer, int caster_cell, int target_cell, float t, Spell* spell) {
float xe0 = renderer->xe[caster_cell];
float xe1 = renderer->xe[target_cell];
float ye0 = renderer->ye[caster_cell];
float ye1 = renderer->ye[target_cell];
float phase1_duration = 0.4;
if (t <= phase1_duration) {
float progress = t / phase1_duration;
DrawLineEx((Vector2){xe0, ye0-renderer->ch}, (Vector2){xe1, ye1-renderer->ch}, 4,
(Color){50, 200, 50, 125 + 125 * sin(progress * 10)});
} else {
Entity* target = env->cell_to_entity[target_cell];
if (target) {
add_animation_text(env, renderer, TextFormat("-%i HP", spell->damage),
target_cell, COLOR_HEALTH, 20, 1.2);
}
return true;
}
return false;
}
Spell create_spell_wind_arrow() {
Spell spell;
spell.name = "Wind Arrow";
spell.ap_cost = 4;
spell.cooldown = 0;
spell.remaining_cooldown = 0;
spell.line_of_sight = true;
spell.cast_in_line = true;
spell.range = 999;
spell.damage = 400;
spell.aoe_range = 0;
spell.effect = spell_wind_arrow;
spell.render_animation = spell_wind_arrow_anim;
return spell;
}
void spell_swift_rabbit(Tactical* env, Entity* caster, int target_cell, Spell* spell) {
Entity* target = env->cell_to_entity[target_cell];
if (target) {
target->movement_points_current += 5;
compute_movement(env, target);
}
}
bool spell_swift_rabbit_anim(Tactical* env, GameRenderer* renderer, int caster_cell, int target_cell, float t, Spell* spell) {
Entity* target = env->cell_to_entity[target_cell];
if (target && spell->animation_state == 0) {
add_animation_text(env, renderer, "+5 MP",
target_cell, COLOR_MOVEMENT_POINTS, 20, 1.2);
}
spell->animation_state = 1;
float phase1_duration = 0.3;
if (t <= phase1_duration) {
DrawCircle(renderer->xe[caster_cell], renderer->ye[caster_cell], t * 3000, (Color){0, 255, 0, 255 - t/0.3*255});
} else {
return true;
}
return false;
}
Spell create_spell_swift_rabbit() {
Spell spell;
spell.name = "Swift Rabbit";
spell.ap_cost = 2;
spell.cooldown = 4;
spell.remaining_cooldown = 0;
spell.line_of_sight = true;
spell.cast_in_line = false;
spell.range = 0;
spell.damage = 0;
spell.aoe_range = 0;
spell.effect = spell_swift_rabbit;
spell.render_animation = spell_swift_rabbit_anim;
return spell;
}
void assign_spells(Entity* entity) {
// TODO assign different spells based on class
entity->spell_count = 5;
entity->spells = malloc(entity->spell_count * sizeof(Spell));
entity->spells[0] = create_spell_explosive_arrow();
entity->spells[1] = create_spell_flying_arrow();
entity->spells[2] = create_spell_rooting_arrow();
entity->spells[3] = create_spell_wind_arrow();
entity->spells[4] = create_spell_swift_rabbit();
}
void compute_observations(Tactical* env) {
}
void compute_movement(Tactical* env, Entity* entity) {
// Do a BFS from the entity's current position to find all reachable cells
// within a distance of the entity's available movement points.
// Store the result in env->movement_path, where each reachable cell
// points to the previous cell in the path, and in env->movement_distance,
// where each reachable cell stores the distance to the player (or -1 if unreachable).
// reset arrays
for (int i = 0; i < env->map_size; ++i) {
env->movement_path[i] = 0;
env->movement_distance[i] = -1;
}
// compute walkable cells mask
bool* walkable_cells = calloc(env->map_size, sizeof(bool));
for (int i = 0; i < env->map_size; ++i) {
// set ground cells to be walkable (TODO this should be pre-computed)
if (env->map[i] == CELL_GROUND) {
walkable_cells[i] = true;
}
// set all cells with entities to be non-walkable (TODO this should be updated whenever an entity moves or is added/removed)
for (int j = 0; j < env->n_entities; ++j) {
const unsigned int cell = env->entities[j].cell;
walkable_cells[cell] = false;
}
}
// TODO these can be calloc'ed once and reused (memset them to 0 each time this function is called)
// EDIT: no, don't use memset for arrays of int, dangerous
int* queue = calloc(env->map_size, sizeof(int));
int* visited = calloc(env->map_size, sizeof(int));
int* distances = calloc(env->map_size, sizeof(int));
int front = 0;
int rear = 0;
// TODO can be static
const int next_row_delta[4] = {1, -1, 0, 0};
const int next_col_delta[4] = {0, 0, 1, -1};
int start_pos = entity->cell;
queue[rear++] = start_pos;
visited[start_pos] = 1;
distances[start_pos] = 0;
while (front < rear) {
int current = queue[front++];
int row = current / env->map_width;
int col = current % env->map_width;
int current_distance = distances[current];
if (current_distance >= entity->movement_points_current)
continue;
// explore neighbors
for (int i = 0; i < 4; ++i) {
int next_row = row + next_row_delta[i];
int next_col = col + next_col_delta[i];
// boundary check
if (next_row < 0 || next_col < 0 || next_row >= env->map_height || next_col >= env->map_width)
continue;
int next = next_row * env->map_width + next_col;
// skip if already visited or not a ground cell
if (visited[next] || !walkable_cells[next])
continue;
// mark as visited and record distance
visited[next] = 1;
distances[next] = current_distance + 1;
env->movement_path[next] = current; // store previous cell in the path
env->movement_distance[next] = distances[next]; // store previous cell in the path
// enqueue neighbor
queue[rear++] = next;
}
}
// cleanup
free(queue);
free(visited);
free(distances);
}
void move_entity(Tactical* env, Entity* entity, const int cell) {
env->cell_to_entity[entity->cell] = NULL;
entity->cell = cell;
env->cell_to_entity[entity->cell] = entity;
entity->movement_points_current -= env->movement_distance[cell];
compute_movement(env, entity);
}
bool try_move_entity(Tactical* env, Entity* entity, const int cell) {
// TODO i don't like this. Checks should be in game logic, not renderer.
if (env->movement_path[cell]) {
move_entity(env, entity, cell);
return true;
}
return false;
}
Tactical* init_tactical() {
Tactical* env = calloc(1, sizeof(Tactical));
env->num_agents = 1;
env->rewards = calloc(env->num_agents, sizeof(float));
env->observations = calloc(env->num_agents*121*121*4, sizeof(unsigned char));
env->actions = calloc(env->num_agents*1, sizeof(int));
// init map
int map_id = 3; // ok
char* map_str = get_map(map_id);
env->map_height = get_map_height(map_id);
env->map_width = get_map_width(map_id);
env->map_size = env->map_height * env->map_width;
env->map = calloc(env->map_height * env->map_width, sizeof(unsigned int));
for (int i = 0; i < env->map_height; i++) {
for (int j = 0; j < env->map_width; j++) {
int idx = i * env->map_width + j;
switch (map_str[idx]) {
case '-': env->map[idx] = CELL_EMPTY; break;
case '.': env->map[idx] = CELL_GROUND; break;
case '|': env->map[idx] = CELL_HOLE; break;
case '#': env->map[idx] = CELL_WALL; break;
default: printf("Invalid map character <%c> at row <%i> and column <%i>\n", map1[idx], i, j); exit(1);
}
}
}
env->cell_to_entity = (Entity**)calloc(env->map_size, sizeof(Entity*));
// init players
env->entities = calloc(2, sizeof(Entity));
env->n_entities = 2;
env->player1 = &env->entities[0];
env->player2 = &env->entities[1];
env->player1->name = "Player 1";
env->player1->cell = get_cell(env, 17, 13);
env->player1->color = COLOR_PLAYER1;
env->player1->health_points_total = 2500;
env->player1->action_points_total = 12;
env->player1->movement_points_total = 6;
env->player1->health_points_current = 2500;
env->player1->action_points_current = 12;
env->player1->movement_points_current = 6;
env->player2->name = "Player 2";
env->player2->cell = get_cell(env, 12, 13);
env->player2->color = COLOR_PLAYER2;
env->player2->health_points_total = 2500;
env->player2->action_points_total = 12;
env->player2->movement_points_total = 6;
env->player2->health_points_current = 2500;
env->player2->action_points_current = 12;
env->player2->movement_points_current = 6;
env->cell_to_entity[env->player1->cell] = env->player1;
env->cell_to_entity[env->player2->cell] = env->player2;
assign_spells(env->player1);
assign_spells(env->player2);
// // define a class
// Class warrior = {
// "Warrior",
// (Spell[]){push}, // List of spells
// 1 // Spell count
// };
env->current_player_idx = 0;
env->current_player = &env->entities[env->current_player_idx];
env->movement_path = calloc(env->map_size, sizeof(unsigned int));
env->movement_distance = calloc(env->map_size, sizeof(int));
compute_movement(env, env->current_player);
return env;
}
void next_player(Tactical* env) {
// reset current player AP and MP
env->current_player->movement_points_current = env->current_player->movement_points_total;
env->current_player->action_points_current = env->current_player->action_points_total;
// decrease current player cooldowns
update_cooldowns(env->current_player);
// switch to next player
env->current_player_idx = (env->current_player_idx + 1) % env->n_entities;
env->current_player = &env->entities[env->current_player_idx];
compute_movement(env, env->current_player);
}
void reset(Tactical* env) {
compute_observations(env);
}
int step(Tactical* env) {
if (false) {
reset(env);
int winner = 2;
return winner;
}
compute_observations(env);
return 0;
}
////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////// RENDERING ///////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////
GameRenderer* init_game_renderer(Tactical* env) {
GameRenderer* renderer = (GameRenderer*)calloc(1, sizeof(GameRenderer));
renderer->width = 1200;
renderer->height = 900;
renderer->movement_cells = malloc(env->map_size * sizeof(bool));
renderer->spell_cells = malloc(env->map_size * sizeof(bool));
renderer->active_spell_cells = malloc(env->map_size * sizeof(bool));
renderer->active_spell = NULL;
// TODO fill the screen automatically (these are hardcoded for map 2)
renderer->cw = 80;
renderer->ch = renderer->cw / 2;
renderer->offset_x = 560;
renderer->offset_y = -200;
renderer->dy = renderer->ch * 0.4;
renderer->mcell = -1;
renderer->mcell_type = -1;
renderer->move_anim_path = calloc(env->map_size, sizeof(int));
renderer->move_anim_cells_per_second = 6;
renderer->text_anim_count = 0;
renderer->spell_anim = NULL;
renderer->xa = calloc(env->map_size, sizeof(float));
renderer->xb = calloc(env->map_size, sizeof(float));
renderer->xc = calloc(env->map_size, sizeof(float));
renderer->xd = calloc(env->map_size, sizeof(float));
renderer->xe = calloc(env->map_size, sizeof(float));
renderer->ya = calloc(env->map_size, sizeof(float));
renderer->yb = calloc(env->map_size, sizeof(float));
renderer->yc = calloc(env->map_size, sizeof(float));
renderer->yd = calloc(env->map_size, sizeof(float));
renderer->ye = calloc(env->map_size, sizeof(float));
for (int row = 0; row < env->map_height; ++row) {
for (int col = 0; col < env->map_width; ++col) {
int cell = get_cell(env, row, col);
renderer->xa[cell] = renderer->offset_x + 0.5 * renderer->cw * (col - row);
renderer->xb[cell] = renderer->xa[cell] - renderer->cw / 2;
renderer->xc[cell] = renderer->xa[cell] + renderer->cw / 2;
renderer->xd[cell] = renderer->xa[cell];
renderer->xe[cell] = renderer->xa[cell];
renderer->ya[cell] = renderer->offset_y + 0.5 * renderer->ch * (col + row + 2);
renderer->yb[cell] = renderer->ya[cell] + renderer->ch / 2;
renderer->yc[cell] = renderer->ya[cell] + renderer->ch / 2;
renderer->yd[cell] = renderer->ya[cell] + renderer->ch;
renderer->ye[cell] = renderer->yb[cell];
}
}
renderer->last_render_time = clock();
renderer->dt = 0.0f;
renderer->max_fps = 120;
InitWindow(renderer->width, renderer->height, "Tactical RL");
SetTargetFPS(60);
return renderer;
}
int get_cell_at_cursor(GameRenderer* renderer, Tactical* env) {
// to get the formula: we know that cell (row, col) starts at coordinates
// x = offset_x + 0.5 * cw * (col - row);
// y = offset_y + 0.5 * ch * (col + row + 2);
// solve this 2x2 linear system to write (row, col)) as a function of (x, y) and we get the formulas below
const int mx = GetMouseX();
const int my = GetMouseY();
const int mrow = floor((my - renderer->offset_y) / renderer->ch - (mx - renderer->offset_x) / renderer->cw - 1);
const int mcol = floor((my - renderer->offset_y) / renderer->ch + (mx - renderer->offset_x) / renderer->cw - 1);
renderer->mx = mx;
renderer->my = my;
renderer->mrow = mrow;
renderer->mcol = mcol;
const int mcell = (mrow < 0 || mcol < 0 || mrow >= env->map_height || mcol >= env->map_width) ? -1 : get_cell(env, mrow, mcol);
return mcell;
}
void draw_debug_info(GameRenderer* renderer) {
//DrawText(TextFormat("%i FPS", (int)(1 / renderer->dt)), 10, 10, 20, COLOR_TEXT_DEFAULT);
DrawText(TextFormat("Mouse: %i, %i", renderer->mx, renderer->my), 150, 10, 15, COLOR_TEXT_DEFAULT);
DrawText(TextFormat("Cell: %i (row %i, col %i)", renderer->mcell, renderer->mrow, renderer->mcol), 150, 30, 15, COLOR_TEXT_DEFAULT);
DrawText(TextFormat("Cell type: %s",
renderer->mcell_type == CELL_EMPTY ? "EMPTY" :
renderer->mcell_type == CELL_GROUND ? "GROUND" :
renderer->mcell_type == CELL_HOLE ? "HOLE" :
renderer->mcell_type == CELL_WALL ? "WALL" :
renderer->mcell_type == -1 ? "NONE" : "UNKNOWN"), 150, 45, 15, COLOR_TEXT_DEFAULT);
}
void draw_player(GameRenderer* renderer, Entity* player, float x, float y, Color color, bool is_current_player, Color cell_color) {
// draw the little guy
if (is_current_player) DrawEllipse(x, y, 0.33 * renderer->cw, 0.33 * renderer->ch, COLOR_ACTIVE_PLAYER);
DrawEllipse(x, y, 0.3 * renderer->cw, 0.3 * renderer->ch, color);
DrawEllipse(x, y, 0.23 * renderer->cw, 0.23 * renderer->ch, cell_color);
DrawLineEx((Vector2){x - 0.1 * renderer->cw, y}, (Vector2){x, y - 0.7 * renderer->ch}, 2, color);
DrawLineEx((Vector2){x + 0.1 * renderer->cw, y}, (Vector2){x, y - 0.7 * renderer->ch}, 2, color);
DrawLineEx((Vector2){x, y - 0.7 * renderer->ch}, (Vector2){x, y - 1.1 * renderer->ch}, 2, color);
DrawLineEx((Vector2){x, y - 1.0 * renderer->ch}, (Vector2){x - 0.2 * renderer->cw, y - 0.8 * renderer->ch}, 2, color);
DrawLineEx((Vector2){x, y - 1.0 * renderer->ch}, (Vector2){x + 0.2 * renderer->cw, y - 0.8 * renderer->ch}, 2, color);
DrawCircle(x, y - 1.3 * renderer->ch, 0.2 * renderer->ch, color);
// draw hp, ap and mp above the little guy
DrawText(TextFormat("%i", player->health_points_current),
x - MeasureText(TextFormat("%i", player->health_points_current), 15) / 2, y - 2.0 * renderer->ch, 15, COLOR_HEALTH);
DrawText(TextFormat("%i", player->action_points_current),
x - MeasureText(TextFormat("%i", player->action_points_current), 15) - 4, y - 2.4 * renderer->ch, 15, COLOR_ACTION_POINTS);
DrawText(TextFormat("%i", player->movement_points_current),
x + 4, y - 2.4 * renderer->ch, 15, COLOR_MOVEMENT_POINTS);
}
void draw_cells_and_entities(GameRenderer* renderer, Tactical* env) {
// draw isometric cells
// (ground)
// a
// b e c (b<->c = cw)
// d
// (a<->d = ch)
// first draw ground cells
for (int cell = 0; cell < env->map_size; ++cell) {
int cell_type = env->map[cell];
if (cell_type == CELL_GROUND) {
// draw isometric cell (a, b, c, d)
Color cell_color = COLOR_CELL_GROUND;
if (renderer->movement_cells[cell]) {
cell_color = COLOR_CELL_MOVE;
} else if (renderer->active_spell) {
if (renderer->active_spell_cells[cell]) {
cell_color = COLOR_CELL_ACTIVE_SPELL;
} else if (renderer->spell_cells[cell]) {
cell_color = COLOR_CELL_SPELL;
} else {
cell_color = COLOR_CELL_INACTIVE_SPELL;
}
}
// DrawTriangleStrip((Vector2[]){{xa, ya}, {xb, yb}, {xc, yc}, {xd, yd}}, 4, cell_color);
DrawTriangleStrip((Vector2[]){
{renderer->xa[cell], renderer->ya[cell]},
{renderer->xb[cell], renderer->yb[cell]},
{renderer->xc[cell], renderer->yc[cell]},
{renderer->xd[cell], renderer->yd[cell]}}, 4, cell_color);
if (renderer->movement_cells[cell]) {
const unsigned int dist = env->movement_distance[cell];
const char* text = TextFormat("%i", dist);
DrawText(text,
renderer->xe[cell] - MeasureText(text, 12) / 2,
renderer->ye[cell] - 6, 12, COLOR_CELL_MOVE_TEXT);
}
// draw white border around cell
DrawLineStrip((Vector2[]){
{renderer->xa[cell], renderer->ya[cell]},
{renderer->xb[cell], renderer->yb[cell]},
{renderer->xd[cell], renderer->yd[cell]},
{renderer->xc[cell], renderer->yc[cell]},
{renderer->xa[cell], renderer->ya[cell]}}, 5, COLOR_CELL_BORDER);
}
}
// then draw walls and entities alternatively, from top-left to bottom-right, for correct z-order
bool draw_horizontally = true; // draw row by row, from top-left to bottom-right (if false: column by column)
if (renderer->move_anim_entity) {
int col = get_col(env, renderer->move_anim_path[renderer->move_anim_path_idx]);
int col_next = get_col(env, renderer->move_anim_path[renderer->move_anim_path_idx + 1]);
if (col == col_next) {
draw_horizontally = false; // this is all for correct depth (z-order) rendering
}
}
for (int i = 0; i < env->map_size; ++i) {
int row, col;
if (draw_horizontally) {
row = i / env->map_width;
col = i % env->map_width;
} else {
row = i % env->map_height;
col = i / env->map_height;
}
int cell = get_cell(env, row, col);
int cell_type = env->map[cell];
if (cell_type == CELL_WALL) {
// draw isometric cell (a, b, c, d) shifted up by dy ("grass")
DrawTriangleStrip((Vector2[]){
{renderer->xa[cell], renderer->ya[cell] - renderer->dy},
{renderer->xb[cell], renderer->yb[cell] - renderer->dy},
{renderer->xc[cell], renderer->yc[cell] - renderer->dy},
{renderer->xd[cell], renderer->yd[cell] - renderer->dy}}, 4, COLOR_CELL_GRASS);
// draw connections between (a, b, c, d) and the shifted up cell ("dirt")
DrawTriangleStrip((Vector2[]){
{renderer->xc[cell], renderer->yc[cell]},
{renderer->xc[cell], renderer->yc[cell] - renderer->dy},
{renderer->xd[cell], renderer->yd[cell]},
{renderer->xd[cell], renderer->yd[cell] - renderer->dy},
{renderer->xb[cell], renderer->yb[cell]},
{renderer->xb[cell], renderer->yb[cell] - renderer->dy}}, 6, COLOR_CELL_DIRT);
}
// draw entity at cell (if any)
Color cell_color = COLOR_CELL_GROUND;
if (renderer->movement_cells[cell]) {
cell_color = COLOR_CELL_MOVE;
} else if (renderer->active_spell && renderer->spell_cells[cell]) {
cell_color = cell == renderer->mcell ? COLOR_CELL_ACTIVE_SPELL : COLOR_CELL_SPELL;
}
Entity* entity = env->cell_to_entity[cell];
if (entity && entity != renderer->move_anim_entity) {
draw_player(renderer, entity,
renderer->xe[cell], renderer->ye[cell],
entity->color, entity == env->current_player, cell_color);
}
// if entity is under move animation, handle it differently
if (renderer->move_anim_entity && renderer->move_anim_path[renderer->move_anim_path_idx] == cell) {
draw_player(renderer, renderer->move_anim_entity,
renderer->xe[cell] + renderer->move_anim_dx, renderer->ye[cell] + renderer->move_anim_dy,
renderer->move_anim_entity->color, renderer->move_anim_entity == env->current_player, cell_color);
}
}
}
void draw_player_dashboard(GameRenderer* renderer, Entity* dashboard_entity, bool is_current_player) {
// Health, action points, movement points
DrawText(dashboard_entity->name, 40, renderer->height - 150, 25,
is_current_player ? COLOR_ENTITY_NAME : COLOR_ENTITY_NAME_HOVER);
DrawText(TextFormat("HP: %i / %i", dashboard_entity->health_points_current, dashboard_entity->health_points_total),
40, renderer->height - 120, 25, COLOR_HEALTH);
DrawText(TextFormat("AP: %i / %i", dashboard_entity->action_points_current, dashboard_entity->action_points_total),
40, renderer->height - 90, 25, COLOR_ACTION_POINTS);
DrawText(TextFormat("MP: %i / %i", dashboard_entity->movement_points_current, dashboard_entity->movement_points_total),
40, renderer->height - 60, 25, COLOR_MOVEMENT_POINTS);
// Spells
DrawText("Spells", 300, renderer->height - 150, 20, COLOR_TEXT_DEFAULT);
for (int i = 0; i < dashboard_entity->spell_count; ++i) {
Spell* spell = &dashboard_entity->spells[i];
bool cooldown = spell->remaining_cooldown > 0;
bool spell_active = !cooldown && dashboard_entity->action_points_current >= spell->ap_cost;
DrawText(TextFormat("[%i]", i+1), 300, renderer->height - 125 + i * 20, 20, spell_active ? COLOR_SPELL : COLOR_SPELL_COOLDOWN);
DrawText(TextFormat("(%i AP)", spell->ap_cost), 300 + 30, renderer->height - 125 + i * 20, 20, cooldown ? COLOR_SPELL_COOLDOWN : COLOR_ACTION_POINTS);
if (spell->remaining_cooldown > 0) {
DrawText(TextFormat("%s (cooldown: %i)", spell->name, spell->remaining_cooldown),
300 + 100, renderer->height - 125 + i * 20, 20, COLOR_SPELL_COOLDOWN);
} else {
DrawText(spell->name,
300 + 100, renderer->height - 125 + i * 20, 20, spell_active ? COLOR_SPELL : COLOR_SPELL_COOLDOWN);
}
}
}
void update_animation_move(Tactical* env, GameRenderer* renderer) {
renderer->move_anim_progress += renderer->dt * renderer->move_anim_cells_per_second;
if (renderer->move_anim_progress >= 1) {
renderer->move_anim_progress = fmod(renderer->move_anim_progress, 1);
renderer->move_anim_path_idx += 1;
}
if (renderer->move_anim_path_idx == renderer->move_anim_path_length) {
// reached last cell: stop animation
renderer->move_anim_entity = NULL;
} else {
int current_cell = renderer->move_anim_path[renderer->move_anim_path_idx];
int next_cell = renderer->move_anim_path[renderer->move_anim_path_idx + 1];
int current_row = get_row(env, current_cell);
int next_row = get_row(env, next_cell);
int current_col = get_col(env, current_cell);
int next_col = get_col(env, next_cell);
int move_dx, move_dy;
if (next_row == current_row + 1) {
move_dx = -1;
move_dy = 1;
} else if (next_row == current_row - 1) {
move_dx = 1;
move_dy = -1;
} else if (next_col == current_col + 1) {