-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathentity_manager.cpp
More file actions
927 lines (737 loc) · 29.1 KB
/
entity_manager.cpp
File metadata and controls
927 lines (737 loc) · 29.1 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
// @Leak @Leak @Leak
#include "entity_manager.h"
#include "file_utils.h"
#include "undo.h"
#include "main.h"
#include "animation_player.h"
void init_proximity_grid(Proximity_Grid *grid)
{
init_gridlike(&grid->gridlike);
init(&grid->entities_snapped_to_grid);
}
constexpr Vector3 DEFAULT_CAMERA_POSITION = Vector3(0, -7, 22);
void init_default_camera(Camera *camera)
{
camera->position = DEFAULT_CAMERA_POSITION;
// Will be set by the user
camera->forward_vector = Vector3(0, 0, 0);
camera->phi = -M_PI / 5.0f;
camera->theta = M_PI / 2.0f;
camera->orientation.w = 1;
// // get_ori_from_rot(&camera->orientation, Vector3(1, 0, 0), camera->phi);
// get_orientation_from_angles(camera->theta, camera->phi);
camera->fov_vertical = 35.0f;
camera->z_near = 0.2f;
camera->z_far = 200.0f;
refresh_camera_matrices(camera);
}
Entity_Manager *make_entity_manager()
{
auto manager = New<Entity_Manager>(false);
bucket_array_init(&manager->entity_array);
array_init(&manager->entities_to_clean_up);
array_init(&manager->all_entities);
array_init(&manager->moving_entities);
array_init(&manager->_by_guys);
array_init(&manager->_by_doors);
array_init(&manager->_by_gates);
array_init(&manager->_by_switches);
array_init(&manager->_by_rocks);
array_init(&manager->_by_walls);
array_init(&manager->_by_floors);
manager->proximity_grid = New<Proximity_Grid>(false);
init_proximity_grid(manager->proximity_grid);
init_default_camera(&manager->camera);
manager->next_entity_id = 1; // Starts with ID of 1, in order to nullify or invalidate an id with 0
manager->active_hero_index = 0;
manager->next_transaction_id_to_issue = 1;
manager->next_transaction_id_to_retire = 1;
array_init(&manager->turn_order);
array_init(&manager->buffered_moves);
auto undo_handler = New<Undo_Handler>(false);
init_undo_handler(undo_handler);
manager->undo_handler = undo_handler;
return manager;
}
inline
void init_general_entity(Entity *e)
{
// e->position = Vector3(0, 0, 0);
// e->visual_position = Vector3(0, 0, 0);
e->scale = 1.0f;
e->orientation = Quaternion(1, 0, 0, 0);
// No need to set these, we just do them for clarity.
e->theta_current = 0.0f;
e->theta_target = 0.0f;
e->bounding_radius = 0.5f; // Default for a cube block
e->bounding_center = Vector3(0.5, 0.5, 0.5);
e->manager = NULL;
e->locator = {};
// e->entity_id = 0;
e->derived_pointer = NULL;
e->use_override_color = false;
e->dead = false;
e->mesh = NULL;
e->scheduled_for_destruction = false;
reset(&e->visual_interpolation);
}
inline
void init_guy(Guy *guy)
{
// Make sure to init the entity base class first!
guy->can_push = false;
guy->can_pull = false;
guy->can_teleport = false;
guy->active = false;
guy->facing_direction = Direction::WEST;
guy->turn_order_index = -1;
// {
// // @Fixme: @Hack: Because #if defined doesn't initialized the Animation State somehow?
// Human_Animation_State dummy;
// guy->animation_state = dummy;
// }
}
inline
void register_entity(Entity_Manager *manager, Entity *e)
{
e->entity_id = manager->next_entity_id;
e->manager = manager;
manager->next_entity_id += 1;
array_add(&manager->all_entities, e);
#define add_entities_to_their_lists(entity_type, array_name) \
if (cmp_var_type_to_type(e->type, entity_type)) array_add(array_name, Down<entity_type>(e));
// Add the entity to its corresponding arrays
add_entities_to_their_lists(Guy, &manager->_by_guys);
add_entities_to_their_lists(Door, &manager->_by_doors);
add_entities_to_their_lists(Gate, &manager->_by_gates);
add_entities_to_their_lists(Switch, &manager->_by_switches);
add_entities_to_their_lists(Rock, &manager->_by_rocks);
add_entities_to_their_lists(Wall, &manager->_by_walls);
add_entities_to_their_lists(Floor, &manager->_by_floors);
#undef add_entities_to_their_lists
#define add_moving_entities(entity_type, array_name) \
if (cmp_var_type_to_type(e->type, entity_type)) array_add(array_name, e);
auto moving_list = &manager->moving_entities;
add_moving_entities(Guy, moving_list);
add_moving_entities(Rock, moving_list);
add_moving_entities(Gate, moving_list);
#undef add_moving_entities
}
// @Temporary: Remove texture_2d_name later, since only door needs it
// When we have triangle mesh for the entities, they will use the texture
// that lives inside the triangle list info instead of relying on this.
template <class Entity_Type>
Entity_Type *Make(Entity_Manager *manager, i32 x, i32 y, String texture_2d_name)
{
auto result = New<Entity_Type>();
auto ret = find_and_occupy_empty_slot(&manager->entity_array);
init_general_entity(ret.first);
result->base = ret.first;
result->base->locator = ret.second;
result->base->derived_pointer = result;
_set_type_Type(result->base->type, Entity_Type);
result->base->position.x = static_cast<f32>(x);
result->base->position.y = static_cast<f32>(y);
result->base->position.z = 0;
result->base->visual_position = result->base->position;
result->base->map = catalog_find(&texture_catalog, texture_2d_name);
register_entity(manager, result->base);
return result;
}
// Eventually, this will replace the other Make() function
template <class Entity_Type>
Entity_Type *Make_2024(Entity_Manager *manager, Vector3 pos)
{
auto result = New<Entity_Type>(false);
auto ret = find_and_occupy_empty_slot(&manager->entity_array);
init_general_entity(ret.first);
result->base = ret.first;
result->base->locator = ret.second;
result->base->derived_pointer = result;
_set_type_Type(result->base->type, Entity_Type);
result->base->position = pos;
result->base->visual_position = result->base->position;
result->base->map = NULL;
register_entity(manager, result->base);
return result;
}
void allocate_data_for_grid(Proximity_Grid *grid, i32 num_x, i32 num_y)
{
auto gridlike = &grid->gridlike;
gridlike->squares_x = num_x;
gridlike->squares_y = num_y;
auto num_squares = num_x * num_y;
gridlike->num_squares = num_squares;
}
Switch *make_switch(Entity_Manager *manager, i32 x, i32 y, String texture_name, i32 flavor)
{
auto swit = Make<Switch>(manager, x, y, texture_name);
swit->flavor = flavor;
swit->pressed = false;
swit->base->use_override_color = true;
Vector4 color;
// @Hardcoded color for the swit:
if (flavor == 1) color = Vector4(1, 1, 0, 1);
else if (flavor == 2) color = Vector4(1, 0, 1, 1);
else color = Vector4(1, 0, 0, 1);
swit->base->override_color = color;
return swit;
}
Gate *make_gate(Entity_Manager *manager, i32 x, i32 y, String texture_name, i32 flavor)
{
auto gate = Make<Gate>(manager, x, y, texture_name);
gate->flavor = flavor;
gate->open = false;
gate->base->use_override_color = true;
Vector4 color;
// @Hardcoded color for the gate:
if (flavor == 1) color = Vector4(1, 1, 0, 1);
else if (flavor == 2) color = Vector4(1, 0, 1, 1);
else color = Vector4(1, 0, 0, 1);
gate->base->override_color = color;
return gate;
}
#define error(c_agent, handler, ...) \
printf("[%s] Error at line %d: ", c_agent, handler.line_number); \
printf(__VA_ARGS__);
void load_ascii_level(Entity_Manager *manager, String full_path)
{
Text_File_Handler handler;
String agent("load_ascii_level");
start_file(&handler, full_path, agent);
if (handler.failed) return;
auto c_agent = reinterpret_cast<char*>(temp_c_string(agent));
auto grid = manager->proximity_grid;
i32 num_x = 0;
i32 num_y = 0;
i32 processed_rows = 0;
bool allocated_data_for_grid = false;
Vector3 camera_pos = DEFAULT_CAMERA_POSITION;
while (true)
{
auto [line, found] = consume_next_line(&handler);
if (!found) break;
if (line[0] == ':')
{
advance(&line, 1);
eat_spaces(&line);
auto [command, remainder] = break_by_spaces(line);
if (command == String("width"))
{
if (num_x)
{
error(c_agent, handler, "Redefinition of number of squares in x for level '%s', skipping...\n",
temp_c_string(full_path));
continue;
}
bool success = false;
auto ret = string_to_int(remainder, &success);
if (!success)
{
error(c_agent, handler, "Unable to number of squares in x for level '%s'.\n",
temp_c_string(full_path));
continue;
}
num_x = ret.first;
if (ret.second)
{
error(c_agent, handler, "Junk at the end of line '%s'.\n", temp_c_string(ret.second));
}
}
else if (command == String("height"))
{
if (num_y)
{
error(c_agent, handler, "Redefinition of number of squares in x for level '%s', skipping...\n",
temp_c_string(full_path));
continue;
}
bool success = false;
auto ret = string_to_int(remainder, &success);
if (!success)
{
error(c_agent, handler, "Unable to read number of squares in y for level '%s'.\n",
temp_c_string(full_path));
continue;
}
num_y = ret.first;
if (ret.second)
{
error(c_agent, handler, "Junk at the end of line '%s'.\n", temp_c_string(ret.second));
}
}
else if (command == String("map"))
{
if (remainder)
{
error(c_agent, handler, "Junk at the end of line '%s'.\n", temp_c_string(remainder));
}
if ((num_x <= 0) || (num_y <= 0))
{
error(c_agent, handler, "Cannot allocate data for grid when size of it is (%d, %d).\n",
num_x, num_y);
continue;
}
allocate_data_for_grid(grid, num_x, num_y);
allocated_data_for_grid = true;
}
else if (command == String("camera"))
{
while (remainder)
{
auto [command, args] = break_by_spaces(remainder);
if (command == String("south"))
{
bool success = false;
auto [distance, rhs] = string_to_float(args, &success);
if (!success)
{
error(c_agent, handler, "Unable to parse the south distance of the camera.\n");
break;
}
// The negative sign here means that we are switching to a y-forward axis,
// because south in our engine means negative in y.
camera_pos.y = -distance;
args = rhs;
}
else if (command == String("up"))
{
bool success = false;
auto [distance, rhs] = string_to_float(args, &success);
if (!success)
{
error(c_agent, handler, "Unable to parse the up distance of the camera.\n");
break;
}
camera_pos.z = distance;
args = rhs;
}
else
{
error(c_agent, handler, "Unknown camera property '%s'.\n", temp_c_string(command));
break;
}
remainder = args;
eat_spaces(&remainder);
}
}
else
{
printf("************************************ COMMAND: '%s', REMAINDER: '%s'\n",
temp_c_string(command), temp_c_string(remainder));
}
}
else
{
assert(line.count > 0);
if (!allocated_data_for_grid)
{
error(c_agent, handler, "Must call command 'map' before specifying the level map.\n");
continue;
}
if (num_x != line.count)
{
error(c_agent, handler, "Mismatch dimensions in squares_x! Gridlike's squares_x = %d while line count is %ld.\n", num_x, line.count);
}
if (processed_rows >= num_y)
{
error(c_agent, handler, "The number of rows (%d) is more than squares_y (%d) in proximity grid, skipping!!!!\n", processed_rows, num_y);
continue;
}
i32 column_index = 0;
//
// For now, some tile does not
for (auto c : line)
{
i32 x = column_index;
i32 y = num_y - processed_rows - 1;
Entity *e = NULL;
Entity *e2 = NULL;
if (c == '*')
{
e = Make<Wall>(manager, x, y, String("chisled_stone"))->base;
e2 = Make<Floor>(manager, x, y, String("quartz"))->base;
}
else if (c == '%')
{
e = Make<Rock>(manager, x, y, String("wood"))->base;
e2 = Make<Floor>(manager, x, y, String("quartz"))->base;
}
else if (c == '{')
{
auto swit = make_switch(manager, x, y, String("candy"), 1);
e = swit->base;
e2 = Make<Floor>(manager, x, y, String("quartz"))->base;
}
else if (c == '}')
{
auto swit = make_switch(manager, x, y, String("candy"), 2);
e = swit->base;
e2 = Make<Floor>(manager, x, y, String("quartz"))->base;
}
else if (c == '[')
{
auto gate = make_gate(manager, x, y, String("candy"), 1);
e = gate->base;
e2 = Make<Floor>(manager, x, y, String("quartz"))->base;
}
else if (c == ']')
{
auto gate = make_gate(manager, x, y, String("candy"), 2);
e = gate->base;
e2 = Make<Floor>(manager, x, y, String("quartz"))->base;
}
else if (c == '.')
{
// This is the floor.
e2 = Make<Floor>(manager, x, y, String("quartz"))->base;
}
else if (c == '$')
{
auto door = Make<Door>(manager, x, y, String("door"));
door->open = true;
e = door->base;
e2 = Make<Floor>(manager, x, y, String("quartz"))->base;
}
// @Cleanup: once we start doing meshes, consolidate the
// making of the guys.
else if (c == 'F' || c == '1')
{
// @Cleanup: Create a make_guy procedure.
auto guy = Make_2024<Guy>(manager, Vector3(x, y, 0));
init_guy(guy);
// set_mesh(guy->base, String("only-red"));
set_mesh(guy->base, String("another_vampire")); // @Incomplete: Use the correct red guy later when we have the animations for it.
guy->can_push = true;
bool active = false;
if (c == 'F') active = true;
guy->active = active;
// On loading level, we make the clone id of everyone themselves,
// However, we will correct these id when we make the turn order.
guy->clone_of_id = guy->base->entity_id;
e = guy->base;
e2 = Make<Floor>(manager, x, y, String("quartz"))->base;
}
else if (c == 'T' || c == '2')
{
auto guy = Make<Guy>(manager, x, y, String("thief"));
init_guy(guy);
guy->can_pull = true;
bool active = false;
if (c == 'T') active = true;
guy->active = active;
// On loading level, we make the clone id of everyone themselves,
// However, we will correct these id when we make the turn order.
guy->clone_of_id = guy->base->entity_id;
e = guy->base;
e2 = Make<Floor>(manager, x, y, String("quartz"))->base;
}
else if (c == 'W' || c == '3')
{
auto guy = Make<Guy>(manager, x, y, String("wizard"));
init_guy(guy);
guy->can_teleport = true;
bool active = false;
if (c == 'W') active = true;
guy->active = active;
// On loading level, we make the clone id of everyone themselves,
// However, we will correct these id when we make the turn order.
guy->clone_of_id = guy->base->entity_id;
e = guy->base;
e2 = Make<Floor>(manager, x, y, String("quartz"))->base;
}
else
{
error(c_agent, handler, "Unknown ascii entity '%c', making it a wall!\n", c);
e = Make<Wall>(manager, x, y, String("purple"))->base;
}
if (e)
{
add_to_grid(grid, e);
}
column_index += 1;
}
processed_rows += 1;
}
}
deinit(&handler);
// printf("sx %d, sy %d\n", num_x, num_y);
// printf("floors count: %ld\n", manager->_by_floors.count);
// printf("walls count: %ld\n", manager->_by_walls.count);
// printf("doors count: %ld\n", manager->_by_doors.count);
// printf("guys count: %ld\n", manager->_by_guys.count);
// printf("gates count: %ld\n", manager->_by_gates.count);
// printf("switches count: %ld\n", manager->_by_switches.count);
// printf("rocks count: %ld\n", manager->_by_rocks.count);
// Making the turn order for the manager
{
array_reserve(&manager->turn_order, manager->_by_guys.count);
i32 turn_index = 0;
Guy *first_active_guy = NULL;
// @Fixme: I think all guys should be inside the turn order, however,
// for clones, we can use turn_order_id to really get which turn we are in.
for (auto guy : manager->_by_guys)
{
// If we have multiple active dudes in the
// level, we make all of the dudes be clones
// of the first active dude.
if (first_active_guy && guy->active)
{
Pid id_of_host = first_active_guy->base->entity_id;
// Correct the id of the parasite.
guy->clone_of_id = id_of_host;
// We don't add them to the turn order because
// they are controlled by their host.
continue;
}
Pid id = guy->base->entity_id;
array_add(&manager->turn_order, id);
guy->turn_order_index = turn_index;
// Only the first active dude will get to here, because the rest
// of the active dudes are handled in the above if clause.
if (guy->active)
{
manager->active_hero_index = turn_index;
first_active_guy = guy;
}
turn_index += 1;
}
}
// @Hardcoded @Temporary: Fix the camera control so that each level contains a different camera position and such.
// @Cleanup: Is this whole thing redundant now that we have set_matrix_for_entities?
{
auto camera = &manager->camera;
// The north distance of the camera, in the *.ascii_level file, we choose to specify
// the south distance, in part because it is more common.
camera->position.y = camera_pos.y;
// The up distance of the camera (how high is the camera compared to the map).
camera->position.z = camera_pos.z;
// @Hack: @Hardcoded: Generally, we want our camera to be at the center of the map width,
// but we may want to customize this later.
camera->position.x = (num_x - 1) * 0.5;
camera->theta = M_PI / 2.0f;
Vector3 point_of_interest;
point_of_interest.x = manager->camera.position.x;
point_of_interest.y = (num_y - 1) * 0.5;
point_of_interest.z = 0;
camera->fov_vertical = 25; // @Hardcoded:
camera->forward_vector = point_of_interest - camera->position;
// @Cleanup: Is there a faster way to do this?
auto dot = glm::dot(camera->forward_vector, Vector3(0, 1, 0));
auto len = glm::length(camera->forward_vector);
camera->phi = -acos(dot / len);
refresh_camera_matrices(camera);
}
}
Entity *find_entity(Entity_Manager *manager, Pid id)
{
for (auto e : manager->all_entities)
{
if (e->entity_id == id)
{
return e;
}
}
return NULL;
}
// @Cutnpaste from table_find_multiple
void find_at_position(Proximity_Grid *grid, Vector3 pos, RArr<Entity*> *results)
{
auto position_index = position_to_index(grid->gridlike, pos);
auto table = &grid->entities_snapped_to_grid;
if (!table->allocated) return;
if (!results->allocator) results->allocator = table->allocator;
// walk code
auto mask = (u32)(table->allocated - 1);
auto hash = table->hash_function(position_index);
if (hash < FIRST_VALID_HASH) hash += FIRST_VALID_HASH;
auto index = hash & mask;
u32 probe_increment = 1;
while (table->entries[index].hash)
{
auto entry = &table->entries[index];
if (entry->hash == hash)
{
if (table->cmp_function(entry->key, position_index))
{
array_add(results, entry->value);
}
}
index = (index + probe_increment) & mask;
probe_increment += 1;
}
}
void release_entity(Entity *e)
{
assert((e->manager != NULL));
bucket_array_remove(&e->manager->entity_array, e->locator);
}
// @Note: Should we really add only one main layer to the proximity grid?
// To me, this sounds like a reasonable thing to do for a while because all
// the game logic happens within one layer of the game.
void add_to_grid(Proximity_Grid *grid, Entity *e)
{
auto index = position_to_index(grid->gridlike, e->position);
if (index == -1)
{
logprint("add_to_grid", "Entity's position (%g, %g, %g) is out of bounds for the grid (%ld, %ld)!\n",
e->position.x, e->position.y, e->position.z,
grid->gridlike.squares_x, grid->gridlike.squares_y);
return;
}
auto data = &grid->entities_snapped_to_grid;
table_add(data, index, e);
}
// @Cutnpaste from table_remove
my_pair<bool, Entity*> remove_from_grid(Proximity_Grid *grid, Entity *e)
{
auto entity_index = position_to_index(grid->gridlike, e->position);
auto id = e->entity_id;
auto table = &grid->entities_snapped_to_grid;
if (!table->allocated)
{
return {false, NULL};
}
// walk code
auto mask = (u32)(table->allocated - 1);
auto hash = table->hash_function(entity_index);
if (hash < FIRST_VALID_HASH) hash += FIRST_VALID_HASH;
auto index = hash & mask; // same as hash % table->allocated [less clock-cycle]
u32 probe_increment = 1;
while (table->entries[index].hash)
{
auto entry = &table->entries[index];
if ((entry->hash == hash) && table->cmp_function(entry->key, entity_index))
{
auto entry_value = entry->value;
if (entry_value && entry_value->entity_id == id)
{
entry->hash = REMOVED_HASH;
table->count -= 1;
return {true, entry_value};
}
}
index = (index + probe_increment) & mask;
probe_increment += 1;
}
return {false, NULL};
}
void post_frame_cleanup(Entity_Manager *manager)
{
for (auto e : manager->entities_to_clean_up)
{
my_free(e->derived_pointer);
release_entity(e);
}
array_reset(&manager->entities_to_clean_up);
// auto grid = manager->proximity_grid;
// for (auto e : manager->entities_to_clean_up)
// {
// auto [success, removed_entity] = remove_from_grid(grid, e);
// if (!success)
// {
// printf("[post_frame_cleanup]: Was not able to clean up entity with id %d\n", e->entity_id);
// }
// }
// array_reset(&manager->entities_to_clean_up);
}
void reset_entity_manager(Entity_Manager *manager)
{
auto proximity_grid = manager->proximity_grid;
table_reset(&proximity_grid->entities_snapped_to_grid);
// @Hack:
if (manager->all_entities.count)
{
post_frame_cleanup(manager);
}
else
{
array_reset(&manager->entities_to_clean_up);
}
bucket_array_reset(&manager->entity_array);
array_reset(&manager->all_entities);
array_reset(&manager->moving_entities);
array_reset(&manager->_by_guys);
array_reset(&manager->_by_doors);
array_reset(&manager->_by_gates);
array_reset(&manager->_by_switches);
array_reset(&manager->_by_rocks);
array_reset(&manager->_by_walls);
array_reset(&manager->_by_floors);
array_reset(&manager->turn_order);
array_reset(&manager->buffered_moves);
manager->next_entity_id = 0;
manager->active_hero_index = 0;
manager->next_transaction_id_to_issue = 1;
manager->next_transaction_id_to_retire = 1;
manager->waiting_on_player_transaction = 0;
manager->player_transaction_caused_changes = false;
}
void set_mesh(Entity *e, Triangle_Mesh *mesh)
{
assert(mesh);
e->mesh = mesh;
auto p0 = &mesh->approximate_bounding_box_p0;
auto p1 = &mesh->approximate_bounding_box_p1;
e->bounding_radius = glm::distance(*p0, *p1) * .5f;
e->bounding_center = (*p0 + *p1) * .5f;
// We may want to add materials stuff to the mesh themselves...
if (cmp_var_type_to_type(e->type, Guy))
{
add_animation_to_guy(Down<Guy>(e));
}
else if (cmp_var_type_to_type(e->type, Switch) || cmp_var_type_to_type(e->type, Gate))
{
add_animation_player(e);
// play_animation(e, "open", looping=false, frozen=true);
}
/*
else if (cmp_var_type_to_type(e->type, Animating)) // I think this is the scenery and stuff..
{
add_animation_player(e);
if (mesh->name) play_animation(e, mesh->name, looping=true, frozen=false);
}
*/
}
void set_mesh(Entity *e, String name)
{
e->mesh_name = copy_string(name); // @Incomplete: This should be redundant once we do the new entity text format. We are @Leaking here, but we will revisit this once we do the new format.
// printf("Mesh name is '%s'\n", temp_c_string(name));
auto mesh = catalog_find(&mesh_catalog, name);
// if (!mesh) mesh = the_missing_mesh; // @Incomplete: We don't have a thing that represents this right now.
if (!mesh) assert(0); // @Incomplete: See above comment
set_mesh(e, mesh);
}
void add_animation_to_guy(Guy *guy)
{
auto mesh_name = guy->base->mesh_name;
assert(mesh_name.count);
//
// Animation stuff:
//
auto names = catalog_find(&animation_names_catalog, mesh_name);
if (!names)
{
logprint("add_animation_to_guy", "Unable to load animations for Guy '%s'!\n", temp_c_string(mesh_name));
return; // Guy will not have animation player if there is no mapping for him.....
}
auto s = &guy->animation_state;
s->animation_names = names;
s->entity = guy->base;
add_animation_player(guy->base);
}
void add_animation_player(Entity *e)
{
if (e->animation_player) // Maybe we are caling this because we changed the mesh.
{
reset_animations(e->animation_player);
set_mesh(e->animation_player, e->mesh);
return;
}
auto player = New<Animation_Player>();
player->remove_locomotion = true; // Animate all entities in-place.
e->animation_player = player;
// Should we make a default animation_channel for each of the thing?
// auto channel = add_animation_channel(player);
set_mesh(player, e->mesh);
}