-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsokoban.cpp
More file actions
1398 lines (1117 loc) · 39.2 KB
/
sokoban.cpp
File metadata and controls
1398 lines (1117 loc) · 39.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
/*
******************** TODO: ********************
NEXT DO THE VISUAL/GRAPHICS OF THE GAME because it looks like shit currently:
- Editor UI
- Visual Interpolation of moves (not completely)
- Text fader of level
- Time controlled animation
- Shadow mapping
- Bloom, Light Emitters
- Lightmaps
- Transitions between levels
WEEKEND:
- Fix the leaks.
- Save game on exit
- Save game on multi-levels levelsets
- Undo must handle entities destructions/creations.
- We should not lock the movement of the player when someone was dead by a gate! (Do something else)
- Handling noclip
THESE COULD BE DONE LATER WHEN I'M BORED
- Menu
- Sound
For now, we actually won't care about the buffered_moves, because we will update the position immediately after moving, so there is also no need for the visual interpolation of the move.
But that will be the next goal to do.
2024/07/31
I imagine the game loop is as folows:
- clear the temporary storage
- update time
- poll events for screen resize and inputs (keyboard/mouse)
- handle resizes
- read the input from the poll events
- simulation of sokoban:
- reset the entities that were moved in the previous frame.
- buffer the input (because we might not have done with the visual interpolation, so we don't want to skip out on any rapid inputs)
- validate the input
- enact a move if done processing the previous one
- get the current active hero
- check if the potential move is valid
- if yes, proceed
- get the associated entities that will be affected with the move
- sync those entities with the mover
- move the entities physically (by physically, we mean updating the logical grid so that their discrete positions are changed) if the center of the mover crossed the tile spacing.
- update the orientation of the entities, so that the visual interpolation can interpolate between the starting theta and the target theta.
- add the visual interpolation for the move.
- draw the game onto the framebuffer:
- display the framebuffer to the backbuffer
- swap buffers
- process hotload changes
*/
#include "sokoban.h"
#include "main.h"
#include "player_control.h"
#include "time_info.h"
#include "metadata.h"
#include "undo.h"
#include "vars.h"
#include "animation_player.h"
#include "animation_channel.h"
Gameplay_Visuals gameplay_visuals;
Entity_Manager *sokoban_entity_manager = NULL;
RArr<Entity*> entities_moved_this_frame;
i32 current_level_index = -1;
bool noclip = false;
bool loading_first_level = true;
void post_move_reevaluate(Entity_Manager *manager);
void change_active_state(Guy *guy, bool active, bool force)
{
if (guy->base->dead) return;
auto s = &guy->animation_state;
if (active)
{
if ((s->current_state != Human_Animation_State::ACTIVE) || force)
{
animate(guy, Human_Animation_State::ACTIVE);
}
}
else
{
if ((s->current_state != Human_Animation_State::INACTIVE) || force)
{
animate(guy, Human_Animation_State::INACTIVE);
}
}
}
// @Cleanup: This doesn't need to be this complicated
void init_level_general(bool reset)
{
auto manager = sokoban_entity_manager;
if (manager)
{
for (auto e : manager->all_entities)
{
array_add(&manager->entities_to_clean_up, e);
}
if (reset) reset_entity_manager(manager);
}
else
{
sokoban_entity_manager = make_entity_manager();
manager = sokoban_entity_manager;
reset = true;
current_level_index = 0;
}
if (reset)
{
// Reset the undo system
reset_undo(manager->undo_handler);
auto set = global_context.current_level_set;
assert((set != NULL));
assert((set->level_names.count));
String level_name = set->level_names[current_level_index];
#ifdef DEVELOPER_MODE
// @Hack: Initially switch to this level on startup
if (loading_first_level)
{
i64 probe_level_index = 0;
for (auto it : set->level_names)
{
if (it == OVERRIDE_LEVEL_NAME)
{
level_name = it;
current_level_index = probe_level_index;
}
probe_level_index += 1;
}
loading_first_level = false;
}
#endif
// @Temporary @Hardcoded: level_path as files in the data/levels directory
String level_path = tprint(String("data/levels/%s.ascii_level"), temp_c_string(level_name));
load_ascii_level(manager, level_path);
for (auto guy : manager->_by_guys)
{
if (guy->base->animation_player) reset_animations(guy->base->animation_player); // This is because right now, not every guy has an Animation_Player.
change_active_state(guy, guy->active, true);
}
// After load the level, mark the beginning of the undo system
undo_mark_beginning(manager);
}
}
void advance_level(i32 delta, bool due_to_victory)
{
current_level_index += delta;
auto set = global_context.current_level_set;
assert((set != NULL));
auto num_levels = set->level_names.count;
assert(num_levels);
// @Incomplete:
// if (current_level_index == num_levels && due_to_victory)
// {
// // Where highest distance is the longest distance from a guy to the door block.
// // Imagine a guy moved toward a door block and some other guys were already waiting
// // for him. So we make the transition slowly to fade out the screen.
// start_waiting_for_victory(highest_guy_distance);
// printf("You actually won!!!!\n");
// exit(1);
// }
Clamp(¤t_level_index, 0, static_cast<i32>(num_levels - 1));
init_level_general(true);
}
void init_sokoban()
{
assert((LEVEL_SET_NAME.count));
auto set = catalog_find(&level_set_catalog, LEVEL_SET_NAME);
global_context.current_level_set = set;
// Must init all the metadata before doing anying related to it.
init_metadata_for_all_entities_type();
init_variables();
init_level_general(true);
}
Entity_Manager *get_entity_manager()
{
return sokoban_entity_manager;
}
Transaction_Id get_next_transaction_id(Entity_Manager *manager)
{
auto result = manager->next_transaction_id_to_issue;
manager->next_transaction_id_to_issue += 1;
return result;
}
bool maybe_retire_next_transaction(Entity_Manager *manager)
{
auto id = manager->next_transaction_id_to_retire;
if (id == manager->next_transaction_id_to_issue) return false;
for (auto it : manager->moving_entities)
{
auto v = &it->visual_interpolation;
if (v->start_time < 0) continue;
if (v->transaction_id) return false;
assert((v->transaction_id >= manager->next_transaction_id_to_retire));
}
if (id == manager->waiting_on_player_transaction)
{
manager->waiting_on_player_transaction = 0;
}
manager->next_transaction_id_to_retire += 1;
return true;
}
template <class Target_Type>
Target_Type *find_type_at(Proximity_Grid *grid, Vector3 pos)
{
RArr<Entity*> found;
found.allocator = {global_context.temporary_storage, __temporary_allocator};
find_at_position(grid, pos, &found);
for (auto e : found)
{
if (cmp_var_type_to_type(e->type, Target_Type))
{
return Down<Target_Type>(e);
}
}
return NULL;
}
Entity *find_pullable_at(Proximity_Grid *grid, Vector3 pos)
{
RArr<Entity*> found;
found.allocator = {global_context.temporary_storage, __temporary_allocator};
find_at_position(grid, pos, &found);
for (auto e : found)
{
if (e->dead) continue;
if (cmp_var_type_to_type(e->type, Rock)) return e;
// else if (cmp_var_type_to_type(e->type, Monster)) return e;
else if (cmp_var_type_to_type(e->type, Guy)) return e;
}
return NULL;
}
// @Cleanup: remove the use of hit_obstance, instead, return NULL.
Entity *find_teleportable_at(Proximity_Grid *grid, Vector3 pos, bool *hit_obstacle)
{
RArr<Entity*> found;
found.allocator = {global_context.temporary_storage, __temporary_allocator};
find_at_position(grid, pos, &found);
for (auto e : found)
{
if (e->dead) continue;
if (cmp_var_type_to_type(e->type, Rock))
{
return e;
}
// else if (cmp_var_type_to_type(e->type, Monster)) return e;
else if (cmp_var_type_to_type(e->type, Guy))
{
return e;
}
else if (cmp_var_type_to_type(e->type, Wall))
{
*hit_obstacle = true;
}
else if (cmp_var_type_to_type(e->type, Gate))
{
auto gate = Down<Gate>(e);
if (!gate->open) *hit_obstacle = true;
}
}
return NULL;
}
bool find_heavy_thing_at(Proximity_Grid *grid, Vector3 pos)
{
RArr<Entity*> found;
found.allocator = {global_context.temporary_storage, __temporary_allocator};
find_at_position(grid, pos, &found);
for (auto e : found)
{
if (e->dead) continue;
if (cmp_var_type_to_type(e->type, Wall)) return true;
if (cmp_var_type_to_type(e->type, Rock)) return true;
if (cmp_var_type_to_type(e->type, Guy)) return true;
// if (cmp_var_type_to_type(e->type, Monster)) return true;
}
return false;
}
Guy *get_active_hero(Entity_Manager *manager)
{
auto index = manager->active_hero_index;
if (0 <= index && (index < manager->turn_order.count))
{
auto id = manager->turn_order[index];
auto e = find_entity(manager, id);
if (e && (cmp_var_type_to_type(e->type, Guy)))
{
auto guy = Down<Guy>(e);
return guy;
}
}
return NULL;
}
RArr<Guy*> get_clones_of_active_hero(Entity_Manager *manager)
{
RArr<Guy*> clones;
clones.allocator = {global_context.temporary_storage, __temporary_allocator};
auto active_hero = get_active_hero(manager);
if (!active_hero) return clones;
Pid id_of_host = active_hero->base->entity_id;
for (auto it : manager->_by_guys)
{
if (it == active_hero) continue;
Pid clone_of = it->clone_of_id;
// @Fixme: FIX this, because clones entities should not have the same entity_id as one another.
// Instead, use turn_order_id to find the clones
if ((clone_of != it->base->entity_id) &&
(clone_of == id_of_host))
{
array_add(&clones, it);
}
}
return clones;
}
Vector3 vector_to_dot_with;
bool sort_by_dot_product(Entity *a, Entity *b)
{
auto a_dot = glm::dot(a->position, vector_to_dot_with);
auto b_dot = glm::dot(b->position, vector_to_dot_with);
return a_dot < b_dot;
}
Direction reverse_direction(Direction dir)
{
auto new_dir = static_cast<Direction>((static_cast<u32>(dir) + 2) & 3);
return new_dir;
}
f32 wrap_degrees(f32 dtheta)
{
if (dtheta < -180) dtheta += 360;
if (dtheta > 180) dtheta -= 360;
return dtheta;
}
void turn_toward(Guy *guy, Vector3 delta)
{
if (!(delta.x || delta.y)) return;
auto theta = static_cast<f32>(atan2(delta.y, delta.x));
auto desired_theta_target = theta * (360 / TAU);
auto e = guy->base;
e->theta_current = wrap_degrees(e->theta_current);
auto magnitude = desired_theta_target - e->theta_current;
if (magnitude > 180) desired_theta_target -= 360;
if (magnitude < -180) desired_theta_target += 360;
e->theta_target = desired_theta_target;
}
inline
Door *is_on_door(Entity *e) // Called in check_for_victory
{
auto grid = e->manager->proximity_grid;
auto result = find_type_at<Door>(grid, e->position);
return result;
}
void check_for_victory(Entity_Manager *manager)
{
auto guys = manager->_by_guys;
if (guys.count == 0) return;
RArr<Door*> unoccupied_doors;
unoccupied_doors.allocator = {global_context.temporary_storage, __temporary_allocator};
for (auto it : manager->_by_doors) array_add(&unoccupied_doors, it);
bool all_on_doors = true;
for (auto it : guys)
{
auto door = is_on_door(it->base);
if (door && !it->base->dead)
{
// @Fixme: @Bug @Bug: This is wrong code here.
// @Fixme: @Bug @Bug: This is wrong code here.
// @Fixme: @Bug @Bug: This is wrong code here.
array_unordered_remove_by_value(&unoccupied_doors, door);
}
else
{
all_on_doors = false;
continue;
}
}
// We can't return in the loop because we might do some animation for all the guys!
if (!all_on_doors) return;
// @Incomplete: Handle level transitions.
advance_level(+1, true);
}
void update_physical_position(Entity *e, Vector3 new_position)
{
auto manager = e->manager;
auto grid = manager->proximity_grid;
auto delta = new_position - e->position;
auto [success, removed_entity] = remove_from_grid(grid, e);
if (!success)
{
printf("[update_physical_position] Was not able to move guy with id %d\n", e->entity_id);
return;
}
assert((removed_entity == e));
e->position = new_position;
// SuperDumb @Hack: Remove this immediately. @Fixme
{
f32 theta_target = 0.0f;
if (delta.x > 0) theta_target = 0;
if (delta.x < 0) theta_target = 180;
if (delta.y > 0) theta_target = 90;
if (delta.y < 0) theta_target = 270;
e->theta_target = theta_target;
}
add_to_grid(grid, e);
if (!doing_undo)
{
array_add_if_unique(&entities_moved_this_frame, e);
}
}
void move_entity(Entity *e, Vector3 delta, Move_Type move_type = Move_Type::LINEAR, Pid sync_id = 0, f32 duration = -1.0f, Transaction_Id transaction_id = 0, Source_Location loc = Source_Location::current())
{
auto manager = e->manager;
auto new_position = e->position + delta;
if (!doing_undo)
{
if ((duration < 0) && (cmp_var_type_to_type(e->type, Guy)))
{
// auto override_speed = get_move_speed_override(Down<Guy>(e));
auto override_speed = -1.0;
if (override_speed > 0) duration = 1 / override_speed;
}
// Pid moving_onto_id; // @Note: Used this with mobile_supporter (I think it is related to falling).
add_visual_interpolation(e, e->position, new_position, move_type, duration, transaction_id, loc);
// if (moving_onto_id) e->visual_interpolation.moving_onto_id = moving_onto_id;
if (sync_id != e->entity_id) e->visual_interpolation.sync_id = sync_id; // Callers can be sloppy and pass their own id; in those cases we igonre it. This simpilifies some 'if' statements.
}
update_physical_position(e, new_position);
}
void set_teleport_times(Entity *e, f32 pre_time, f32 post_time)
{
auto v = &e->visual_interpolation;
if (v->move_type != Move_Type::TELEPORT) return; // Sanity check, in case something happens.
v->teleport_pre_time = pre_time;
v->teleport_post_time = post_time;
}
bool can_go(Entity *mover, Proximity_Grid *grid, Vector3 desired_pos, Vector3 delta, bool can_push, RArr<Entity*> *pushes, bool voluntarily = true) // Pid id_to_ignore = 0
{
auto index = position_to_index(grid->gridlike, desired_pos);
if (index < 0) return false; // Out of bounds!
// If anyone is moving into this position, we can't go.
auto manager = mover->manager;
for (auto it : manager->moving_entities)
{
// auto v = &it->visual_interpolation;
if (it == mover) continue;
// if (v->start_time < 0) continue;
// if (glm::distance(v->physical_end, desired_pos) < 0.5f)
// {
// printf("BLOCKING V IS: %p (Open debugger for more info)\n", v);
// return false;
// }
}
RArr<Entity*> found;
found.allocator = {global_context.temporary_storage, __temporary_allocator};
find_at_position(grid, desired_pos, &found);
for (auto e : found)
{
// if (e->entity_id == id_to_ignore) continue;
// if (e->entity->scheduled_for_destruction) continue;
if (cmp_var_type_to_type(e->type, Guy))
{
auto guy = Down<Guy>(e);
if (guy->base->dead) continue;
}
if (e->entity_id == mover->entity_id &&
(delta != Vector3(0, 0, 0)))
{
logprint("can_go", "Next position of should not have the current mover there!!!!\n");
assert(0);
return true;
}
if (cmp_var_type_to_type(e->type, Gate))
{
auto gate = Down<Gate>(e);
if (!gate->open) return false;
}
if (cmp_var_type_to_type(e->type, Wall))
{
return false;
}
if (cmp_var_type_to_type(e->type, Rock) ||
cmp_var_type_to_type(e->type, Guy))
{
if (!can_push) return false;
auto new_desired_pos = e->position + delta;
if (can_go(mover, grid, new_desired_pos, delta, can_push, pushes, false))
{
array_add(pushes, e);
}
else
{
return false;
}
}
}
// Return true because the target position is empty.
return true;
}
void move_individual_guy(Guy *guy, Vector3 delta, Transaction_Id transaction_id = 0)
{
// turn_toward(guy, delta);
auto manager = guy->base->manager;
auto grid = manager->proximity_grid;
auto original_pos = guy->base->position;
if (!noclip)
{
auto wall = find_type_at<Wall>(grid, original_pos);
if (wall) delta = Vector3(0, 0, 0);
}
bool someone_moved = false;
bool did_pulled = false;
bool did_pushed = false;
if (guy->can_teleport) // Collapse this with the below movement for push and pull.
{
Vector3 teleport_delta = delta;
// The other entity to swap with.
Entity *other = NULL;
auto index = position_to_index(grid->gridlike, original_pos + teleport_delta);
// Stop if out of bound
while (index != -1)
{
bool found_obstacle = false;
other = find_teleportable_at(grid, original_pos + teleport_delta, &found_obstacle);
if (other)
{
delta = teleport_delta;
break;
}
if (found_obstacle)
{
// Only move by one block because there is nothing to swap between.
teleport_delta = delta;
break;
}
teleport_delta += delta;
index = position_to_index(grid->gridlike, original_pos + teleport_delta);
}
if (index == -1)
{
// Should not get here
assert(0);
}
if (other)
{
auto pre1 = gameplay_visuals.wizard_teleport_pre_time;
auto post1 = gameplay_visuals.wizard_teleport_pre_time;
auto pre2 = gameplay_visuals.endpoint_teleport_pre_time;
auto post2 = gameplay_visuals.endpoint_teleport_pre_time;
move_entity(guy->base, teleport_delta, Move_Type::TELEPORT, 0, pre1 + post1);
move_entity(other, teleport_delta * -1.0f, Move_Type::TELEPORT, 0, pre2 + post2);
set_teleport_times(guy->base, pre1, post1);
set_teleport_times(other, pre2, post2);
}
else
{
// Which means there are nothing to swap with.
// Then we check if we can go regularly to the desired position.
bool can = can_go(guy->base, grid, original_pos + teleport_delta, delta, guy->can_push, NULL, true);
if (!can) return;
move_entity(guy->base, teleport_delta, Move_Type::LINEAR, 0, -1, transaction_id);
}
someone_moved = true;
}
else
{
auto target_position = original_pos + delta;
RArr<Entity*> pushed;
pushed.allocator = {global_context.temporary_storage, __temporary_allocator};
bool voluntarily = true;
bool can = can_go(guy->base, grid, target_position, delta, guy->can_push, &pushed, voluntarily);
if (!can) return;
move_entity(guy->base, delta, Move_Type::LINEAR, 0, -1, transaction_id);
auto sync_id = guy->base->entity_id;
if (pushed.count)
{
did_pushed = true;
}
for (auto it : pushed)
{
// Setting teleport to false because you don't want the wizard to teleport when pushed.
move_entity(it, delta, Move_Type::LINEAR, sync_id, -1, transaction_id);
}
if (guy->can_pull)
{
auto pulled_entity = find_pullable_at(grid, original_pos - delta);
if (pulled_entity)
{
// We assume that the original position of the Thief is not occupied
// Right after it has moved.
// @Todo: Should we should do a can_go check here?
move_entity(pulled_entity, delta, Move_Type::LINEAR, sync_id, -1, transaction_id);
}
did_pulled = true;
}
someone_moved = true;
}
// We know that we have moved if we reach here, so we call undo handler to cache to differences.
auto undo_handler = manager->undo_handler;
undo_end_frame(undo_handler);
post_move_reevaluate(manager);
if (!guy->base->dead && someone_moved)
{
if (did_pulled)
{
animate(guy, Human_Animation_State::PULLING);
}
else if (did_pushed)
{
animate(guy, Human_Animation_State::PUSHING);
}
else
{
animate(guy, Human_Animation_State::WALKING);
}
}
check_for_victory(manager); // @Incomplete: Wait for all movement visuals to end before transitioning levels.
}
void animate(Guy *guy, Human_Animation_State::Gameplay_State state)
{
if (!guy->base->animation_player) return;
auto s = &guy->animation_state;
if (guy->base->dead && (state == Human_Animation_State::DEAD)) return; // @Hack so that we don't need to worry about this stuff elsewhere....
// This means that we are animating that state right now so don't change our state.
if ((s->current_state == state) && (guy->base->animation_player->channels.count))
{
return;
}
s->current_state = state;
auto graph = human_animation_graph;
switch (state)
{
case Human_Animation_State::UNINITIALIZED: {
logprint("animate", "Error: Attempted to animate '%s' by setting its state to UNINITIALIZED, which should not happen!\n", temp_c_string(guy->base->mesh_name));
} break;
case Human_Animation_State::INACTIVE: {
send_message(graph, s, String("go_state_inactive"), String("StateInactive"));
} break;
case Human_Animation_State::ACTIVE: {
send_message(graph, s, String("go_state_active"), String("StateActive"));
} break;
case Human_Animation_State::WALKING: {
send_message(graph, s, String("go_state_walking"), String("StateWalking"));
} break;
case Human_Animation_State::PUSHING: {
send_message(graph, s, String("go_state_pushing"), String("StatePushing"));
} break;
case Human_Animation_State::PULLING: {
send_message(graph, s, String("go_state_pulling"), String("StatePulling"));
} break;
case Human_Animation_State::DEAD: {
send_message(graph, s, String("go_state_dead"), String("StateDead"));
} break;
}
/*
auto names_table = &s->animation_names->name_to_anim_name;
auto play_or_skip = [](Table<String, String> *names_table, String string_state, Entity *e) {
auto [anim_name, success] = table_find(names_table, string_state);
if (!success)
{
logprint("animate", "Couldn't find the animation for the state '%s', not playing animation...\n", temp_c_string(string_state));
return;
}
play_animation(e, anim_name);
};
switch (state)
{
case Human_Animation_State::INACTIVE: {
play_or_skip(names_table, String("inactive"), guy->base);
} break;
case Human_Animation_State::ACTIVE: {
play_or_skip(names_table, String("active"), guy->base);
} break;
case Human_Animation_State::WALKING: {
play_or_skip(names_table, String("walking"), guy->base);
} break;
case Human_Animation_State::PUSHING: {
play_or_skip(names_table, String("pushing"), guy->base);
} break;
case Human_Animation_State::PULLING: {
play_or_skip(names_table, String("pulling"), guy->base);
} break;
case Human_Animation_State::DEAD: {
play_or_skip(names_table, String("dead"), guy->base);
} break;
}
*/
}
void evaluate_gates(Entity_Manager *manager)
{
// We only open gate(s) of a certain flavor if all the switches of that
// flavor is pressed.
u32 cached_maybe_gates_should_open = 0;
u32 should_switch_not_be_evaluated = 0;
//
// First we evaluate all the switches and press it if there is something heavy on it.
//
auto grid = manager->proximity_grid;
for (auto it : manager->_by_switches)
{
auto pos = it->base->position;
auto flavor = it->flavor;
auto unpress = (1 << flavor) & should_switch_not_be_evaluated;
if (!unpress && find_heavy_thing_at(grid, pos))
{
it->pressed = true;
cached_maybe_gates_should_open |= (1 << flavor);
}
else
{
it->pressed = false;
cached_maybe_gates_should_open &= ~(1 << flavor);
should_switch_not_be_evaluated |= (1 << flavor);
}
}
//
// Next we active the gates accordingly
//
for (auto it : manager->_by_gates)
{
auto bit_mask = 1 << it->flavor;
bool should_open = (bit_mask & cached_maybe_gates_should_open);
it->open = should_open;
// Any entity that is under the closing gate will be destroyed!
if (!should_open)
{
RArr<Entity*> found;
found.allocator = {global_context.temporary_storage, __temporary_allocator};
find_at_position(grid, it->base->position, &found);
for (auto to_destroy : found)
{
if (to_destroy == it->base) continue;
to_destroy->dead = true;
// @Fixme: we should not cleanup this guy, instead, make him dead and then render him differently.
array_add(&manager->entities_to_clean_up, to_destroy);
}
}
}
}
void post_move_reevaluate(Entity_Manager *manager)
{
evaluate_gates(manager);
// evaluate_doors(manager);
}
void post_undo_reevaluate(Entity_Manager *manager)
{
evaluate_gates(manager);
// This is a @Hack, but we need to change the active_hero_index before updating all the active
// guys' flags, this is because when we apply the diff's, the manager's active_hero_index doesn't
// change. So here, we need to manually set it back.
//
// See entity_manager.cpp, the part where we set the turn_index
{
i32 active_index = manager->active_hero_index;
bool has_set_active_index = false;
for (auto guy : manager->_by_guys)
{
if (guy->active)
{
assert(!has_set_active_index);
active_index = guy->turn_order_index;
}
}
manager->active_hero_index = active_index;
}
update_guy_active_flags(manager);
}
void enact_move(Entity_Manager *manager, Buffered_Move move, bool is_interrupt_move = false)
{
// if ((transition_mode == Transition::WAITING_FOR_VICTORY) || (transition_mode == Transition::VICTORY)) return;
assert(!is_interrupt_move);
if (is_interrupt_move) return;
auto guy_id = move.id;
auto delta = move.delta;
auto caused_changes = false;
auto some_not_dead = false;
RArr<Entity*> clones_to_move;
clones_to_move.allocator = {global_context.temporary_storage, __temporary_allocator};
// @Cleanup: Factor out to a get_clones_of function (remove the get_clones_of_active_hero_function)
auto guys = manager->_by_guys;
for (auto other : guys)
{
if (other->base->scheduled_for_destruction) continue;
if ((other->clone_of_id == guy_id) || other->base->entity_id == guy_id)
{
array_add(&clones_to_move, other->base);
}
}
auto e = find_entity(manager, guy_id);
Guy *guy = NULL;
if (cmp_var_type_to_type(e->type, Guy)) guy = Down<Guy>(e);
// Is this too specific? I don't think it is right anymore...
if (guy && guy->base->dead) return; // No move for the dead
auto transaction_id = get_next_transaction_id(manager);
manager->waiting_on_player_transaction = transaction_id;
if (clones_to_move.count)
{
vector_to_dot_with = delta;
array_qsort(&clones_to_move, sort_by_dot_product);