102 Dalmatians: Puppies to the Rescue
Decompilation Source Reference | GitLab repository
Loading...
Searching...
No Matches
lifecycle.c
Go to the documentation of this file.
1/* Recovered game implementation; original addresses identify provenance. */
10#include "pcdogs/actor/types.h"
16#include "pcdogs/entity/types.h"
17#include "pcdogs/game/globals.h"
18#include "pcdogs/game/types.h"
20#include "pcdogs/level/types.h"
22#include "pcdogs/math/types.h"
27#include <math.h>
28#include <stdarg.h>
29#include <string.h>
30
31static int32_t Entity_IndexInLevel(const struct Level_Data *level, const struct Entity *entity)
32{
33 int32_t byte_offset = (int32_t)((uintptr_t)entity - (uintptr_t)level->entity_array);
34 return byte_offset / (int32_t)sizeof *entity;
35}
36
37// Original pcdogs.exe: 0x405860
38// Tests whether a one-based entity selector appears in the active work list. The bounds check uses only the selector's low word while addressing uses the full value, and the scan trusts the count of the four-slot list.
39BOOL Entity_IsInActiveList(int32_t entity_index_1based)
40{
42 uint32_t slot = (uint32_t)entity_index_1based - 1;
43
44 /* 0x40586c compares AX with the unsigned count word. The address below
45 * still uses all 32 bits of the decremented index, with wrapping arithmetic. */
46 if ((uint16_t)slot >= (uint16_t)level->entity_count)
47 return 0;
48
49 uintptr_t entity_address = (uintptr_t)level->entity_array + slot * ENTITY_RECORD_STRIDE;
51
52 for (int32_t i = 0; i < active->active_count; ++i)
53 {
54 if ((uintptr_t)active->active_entities[i] == entity_address)
55 return 1;
56 }
57
58 return 0;
59}
60
61// Original pcdogs.exe: 0x405950
62// Updates an entity property and mirrors applicable changes into its live actor, or restores selected defaults for value -1. Property dispatch uses the low byte and flag selectors alias modulo 32; restoration can reset derived runtime state instead of restoring every dependent field.
63void Entity_SetActorProperty(struct Entity *entity, enum Actor_PropID prop_id, int32_t value)
64{
65 struct Entity *entity_state = entity;
66 struct Actor *runtime_actor = entity_state->runtime_actor;
67
68 if (runtime_actor)
69 entity = (struct Entity *)(uintptr_t)(runtime_actor->record_ptr);
70 else
71 entity = (struct Entity *)(uintptr_t)(runtime_actor);
72
73 int32_t property_value = value;
74 uint8_t property_id_byte = (uint8_t)prop_id;
75 bool restore_saved_value = property_value == ENTITY_PROPERTY_RESTORE_SAVED;
76 uint32_t property_index = (uint32_t)property_id_byte;
77
78 if (property_index <= PROP_COLLHEIGHT)
79 {
80 switch (property_index)
81 {
82 case PROP_MAX_HEALTH:
83 {
85
86 if (!restore_saved_value)
87 saved_param_max_health = property_value;
88 else
90
91 struct Player_ControllerRecordView *health_record = (struct Player_ControllerRecordView *)(uintptr_t)(entity);
92
93 entity_state->param_max_health = saved_param_max_health;
94
95 if (!health_record)
96 return;
97
98 health_record->health_or_energy = saved_param_max_health;
99
100 break;
101 }
102
103 case PROP_SPEED:
104 {
106 {
107 if (!restore_saved_value)
108 entity_state->param_max_speed_2 = property_value;
109 else
110 entity_state->param_max_speed_2 = entity_state->saved_param_max_speed_2;
111 }
112 else if (!restore_saved_value)
113 entity_state->param_accel_0 = property_value;
114 else
115 entity_state->param_accel_0 = entity_state->saved_param_accel_0;
116
117 break;
118 }
119
120 case PROP_ROTATION:
121 {
122 if (!restore_saved_value)
123 {
124 int64_t turn_step_product = (int64_t)ENTITY_ANGLE_SCALE_Q12 * value;
125
126 entity_state->param_turn_step =
127 (int32_t)(int16_t)((((uint32_t)((uint64_t)turn_step_product >> MATH_Q12_SHIFT)) / ENTITY_FIXED_UPDATE_RATE) >> MATH_Q12_SHIFT);
128 }
129 else
130 entity_state->param_turn_step = entity_state->saved_param_turn_step;
131
132 break;
133 }
134
135 case PROP_PARAM_108:
136 {
137 if (!restore_saved_value)
138 {
139 int64_t parameter_108_product = (int64_t)ENTITY_ANGLE_SCALE_Q12 * value;
140
141 entity_state->param_108 = (uint32_t)((uint64_t)parameter_108_product >> MATH_Q12_SHIFT);
142 }
143 else
144 entity_state->param_108 = entity_state->saved_param_108;
145
146 break;
147 }
148
149 case PROP_PUSHXZ:
150 {
151 if (!restore_saved_value)
152 entity_state->param_10C = property_value;
153 else
154 entity_state->param_10C = entity_state->saved_param_10C;
155
156 break;
157 }
158
159 case PROP_RESPONSEXZ:
160 {
161 if (!restore_saved_value)
162 {
163 int64_t parameter_110_product = (int64_t)ENTITY_ANGLE_SCALE_Q12 * value;
164
165 entity_state->param_110 = (uint32_t)((uint64_t)parameter_110_product >> MATH_Q12_SHIFT);
166 }
167 else
168 entity_state->param_110 = entity_state->saved_param_110;
169
170 break;
171 }
172
173 case PROP_LIVESPEED:
174 {
175 int32_t saved_param_114;
176
177 if (!restore_saved_value)
178 saved_param_114 = property_value;
179 else
180 saved_param_114 = entity_state->saved_param_114;
181
182 entity_state->param_114 = saved_param_114;
183
184 if (entity_state->target_offset_step < 0)
185 {
186 property_value = -(property_value);
187 value = property_value;
188 }
189
190 int64_t offset_step_product = (int64_t)ENTITY_ANGLE_SCALE_Q12 * value;
191
192 entity_state->target_offset_step = ((uint32_t)((uint64_t)offset_step_product >> MATH_Q12_SHIFT)) / ENTITY_FIXED_UPDATE_RATE;
193
194 break;
195 }
196
197 case PROP_CAMERAXZ:
198 {
199 if (!restore_saved_value)
200 entity_state->param_118 = property_value;
201 else
202 entity_state->param_118 = entity_state->saved_param_118;
203
204 break;
205 }
206
207 case PROP_COLLRADIUS:
208 {
209 if (!restore_saved_value)
210 {
211 int64_t radius_squared = (int64_t)value * value;
212
213 entity_state->collision_radius_sq = (uint32_t)((uint64_t)radius_squared >> MATH_Q12_SHIFT);
214 }
215 else
216 entity_state->collision_radius_sq = entity_state->saved_collision_radius_sq;
217
218 break;
219 }
220
221 case PROP_COLLHEIGHT:
222 {
223 if (!restore_saved_value)
224 {
225 int64_t height_squared = (int64_t)value * value;
226
227 entity_state->collision_height_sq = (uint32_t)((uint64_t)height_squared >> MATH_Q12_SHIFT);
228 }
229 else
230 entity_state->collision_height_sq = entity_state->saved_collision_height_sq;
231
232 break;
233 }
234 }
235 }
236 else if (property_id_byte >= ENTITY_PROPERTY_FLAG_BASE)
237 {
238 int32_t property_flag_mask = 1 << (((uint8_t)property_index - ENTITY_PROPERTY_FLAG_BASE) & ENTITY_FLAG_INDEX_MASK);
239
240 entity_state->flags &= ~property_flag_mask;
241
242 if (!restore_saved_value)
243 {
244 if (!property_value)
245 property_flag_mask = 0;
246
247 entity_state->flags |= property_flag_mask;
248 Actor_ApplyEntitySlotFlags((struct Actor_State *)(uintptr_t)(runtime_actor));
249 }
250 else
251 {
252 entity_state->flags |= property_flag_mask & entity_state->saved_flags;
253 Actor_ApplyEntitySlotFlags((struct Actor_State *)(uintptr_t)(runtime_actor));
254 }
255 }
256 else if (property_id_byte >= PROP_TRACEMODE0 && property_id_byte <= PROP_TRACEMODE3)
257 {
258 uint8_t saved_param_direction_mode;
259
260 if (!restore_saved_value)
261 saved_param_direction_mode = (uint8_t)property_index - (int32_t)PROP_TRACEMODE0;
262 else
263 {
264 property_value = 0;
265 saved_param_direction_mode = entity_state->saved_param_direction_mode;
266 }
267
268 entity_state->param_direction_mode = saved_param_direction_mode;
269 }
270
271 struct Player_ControllerRecordView *updated_record = (struct Player_ControllerRecordView *)(uintptr_t)(entity);
272
273 if (updated_record)
274 {
275 if (!updated_record->reaction_animation_ticks)
276 Actor_CopyEntityDefaultsToRecord((struct Actor_State *)(uintptr_t)(entity_state->runtime_actor));
277
278 char direction_property_id = (uint8_t)prop_id;
279
280 if (direction_property_id >= (int32_t)PROP_TRACEMODE0 && direction_property_id <= (int32_t)PROP_TRACEMODE3)
281 ((struct Player_ControllerRecordView *)entity)->direction_mode_height_offset = property_value;
282 }
283}
284
285// Original pcdogs.exe: 0x405c50
286// Clones and initializes an entity's runtime actor, collision records, position, and property state. The spawn gate is set before template lookup and cloning, so failure prevents another attempt until teardown clears it.
287struct Actor_State *Entity_SpawnActor(struct Entity *entity)
288{
289 if (entity->runtime_target_pos)
290 return 0;
291
292 struct Math_Vec3I32 **entity_position_binding = &entity->position_ptr;
293
294 entity->runtime_target_pos = &entity->bonus_respawn_pos;
295 memcpy(entity_position_binding, &entity->runtime_target_pos, ENTITY_RUNTIME_BINDING_BYTES);
297
298 struct Actor *actor_template_header = entity->actor_template_header;
299
300 entity->runtime_actor = actor_template_header;
301
302 if (!actor_template_header)
303 return 0;
304
306 (struct Actor_State *)actor_template_header->list_next, (struct Actor_State **)&Actor_CollisionListHead);
307
308 entity->runtime_actor = (struct Actor *)actor;
309
310 if (!actor)
311 return 0;
312
313 int32_t behavior_flags = actor->behavior_flags;
314
315 actor->roll_angle = 0;
317
318 int64_t facing_product = (int64_t)ENTITY_ANGLE_SCALE_Q12 * entity->param_F0;
319 int32_t angle = (int32_t)(facing_product >> MATH_Q12_SHIFT) >> MATH_Q12_SHIFT;
320
321 actor->facing_target = (uint16_t)angle;
322 actor->facing_current = (uint16_t)angle;
323 actor->facing_blend = MATH_Q12_ONE;
324 Math_BuildMatrixRotationY(&actor->rot_mat, NULL, angle);
326
327 struct Math_Vec3I32 *runtime_target_pos = entity->runtime_target_pos;
328
329 *entity_position_binding = &actor->position;
330 actor->position.x = runtime_target_pos->x;
331 actor->position.y = runtime_target_pos->y;
332 actor->position.z = runtime_target_pos->z;
333
334 struct Player_ControllerRecordView *runtime_record = actor->record_ptr;
335 struct Component_CollisionRecord *component_record = (struct Component_CollisionRecord *)((uint8_t *)runtime_record + sizeof *runtime_record);
336
337 if (!actor->component_count)
338 component_record = (struct Component_CollisionRecord *)runtime_record->contact_slots;
339
340 int32_t controller_record_bytes = (char *)component_record - (char *)runtime_record;
341
342 memset(runtime_record, 0, controller_record_bytes);
343 runtime_record->component_records = component_record;
344
345 int component_countdown = 2;
346 void **component_definition_slot = &entity->shared_ref_50;
347 bool more_components;
348
349 do
350 {
351 component_record->damage_cooldown = 0;
352 component_record->reference_actor = NULL;
353 component_record->definition = *component_definition_slot;
354
355 if (component_record->position_source_actor)
356 component_record->position_source_actor = (struct Actor_State *)((char *)component_record->position_source_actor + ((char *)actor - (char *)entity->actor_template_header->list_next));
357
358 component_definition_slot = &component_definition_slot[1];
359 component_record = &component_record[1];
360 more_components = component_countdown != 1;
361 component_countdown -= 1;
362 } while (more_components);
363
364 int32_t entity_index = Entity_IndexInLevel(pkg_resource_current_level_data, entity);
365
366 runtime_record->owner_entity_index = (uint16_t)entity_index;
368 memcpy(&runtime_record->position_ptr, entity_position_binding, ENTITY_RUNTIME_BINDING_BYTES);
369 runtime_record->lives_remaining = ENTITY_UNLIMITED_LIVES;
370 actor->fade_timer = 0;
371 actor->ground_collision_node = NULL;
372
373 return actor;
374}
375
376// Original pcdogs.exe: 0x405e20
377// Enables parent ground-orientation processing when its child, scale, and collision dimensions satisfy the spawn-time gates. It restores the post-clone behavior flag without starting the orientation blend timer.
379{
380 struct Actor *runtime_actor = entity->runtime_actor;
381
382 if (!runtime_actor || !runtime_actor->parent_component)
383 return;
384
385 struct Actor *child_actor = runtime_actor->child_actor;
386
387 if (!child_actor || !(uint16_t)child_actor->scale_xy || !(uint16_t)child_actor->visual_scale.y)
388 return;
389
390 int16_t collision_height_hi = runtime_actor->collision_height_hi;
391
392 if (collision_height_hi >= runtime_actor->collision_radius && collision_height_hi >= runtime_actor->collision_height)
393 return;
394
395 int32_t behavior_flags = runtime_actor->behavior_flags;
396
398 runtime_actor->behavior_flags = behavior_flags;
399}
400
401// Original pcdogs.exe: 0x405e80
402// Releases an entity's actor bindings, repairs any collision-list detachment, marks the actor for removal, and clears transient entity state. It optionally restores saved flags and collision dimensions.
403void Entity_DestroyActor(struct Entity *entity, int32_t restore_defaults)
404{
405 struct Actor *runtime_actor = entity->runtime_actor;
406
407 if (runtime_actor)
408 {
409 uint16_t flags = (int16_t)entity->flags;
410
411 entity->runtime_actor = NULL;
412
413 // Restore detached collision-list membership before tearing down the actor.
414 if (((uint32_t)flags >> 8 & 0xff) & (ENTITY_FLAG_COLLISION_LIST_UNLINKED >> 8))
415 Actor_AddToCollisionList((struct Actor_State *)(runtime_actor));
416
417 struct Player_ControllerRecordView *release_record = (struct Player_ControllerRecordView *)(runtime_actor->record_ptr);
418
420 Actor_ReleaseBindings(release_record);
421 }
422 else
424
426 entity->position_ptr = &entity->bonus_respawn_pos;
427 entity->runtime_target_pos = NULL;
428 entity->active_flag = 0;
429 entity->attach_offset_x = 0;
430 entity->default_anim_state = 0;
431 __builtin_memset(&entity->local_vars, 0, sizeof(entity->local_vars));
432 entity->script_timer = 0;
433 entity->behavior_index = 0;
434 entity->behavior_stack[0] = 0;
435 entity->behavior_stack[1] = 0;
436 entity->behavior_stack[2] = 0;
437
438 if (!restore_defaults)
439 return;
440
441 uint32_t saved_flags = entity->saved_flags;
442 uint32_t saved_collision_height_sq = entity->saved_collision_height_sq;
443
444 saved_flags |= ENTITY_FLAG_RESTORE_DEFAULTS;
446 entity->collision_height_sq = saved_collision_height_sq;
447 entity->flags = saved_flags;
448}
449
450// Original pcdogs.exe: 0x406d50
451// Updates visibility, spawning, destruction, and collision-list membership, returning whether the entity may run its script. A nonnull requester forces reactivation past the frustum gate; separate distance origins and enter/leave margins provide hysteresis. The per-pass guard rejects repeated updates until the script owner clears it.
452BOOL Entity_UpdateVisibilityAndSpawn(struct Entity *entity, struct Entity *reactivation_requester_or_null)
453{
454 struct Entity *entity_state = entity;
455 uint16_t flags = (int16_t)entity_state->flags;
456 struct Actor *runtime_actor = entity_state->runtime_actor;
457
458 if (((uint32_t)flags >> 8 & 0xff) & (ENTITY_FLAG_COLLISION_LIST_UNLINKED >> 8))
459 {
460 // A nested-script requester can force reactivation without the frustum test.
461 if (reactivation_requester_or_null)
462 {
463 uint32_t flags_5 = entity_state->flags;
464
465 // Relink the live actor as its collision-detached flag is cleared.
467 entity_state->flags = flags_5;
468
469 if (runtime_actor)
470 Actor_AddToCollisionList((struct Actor_State *)(uintptr_t)(runtime_actor));
471 }
472 else if (!(((uint32_t)flags >> 8 & 0xff) & (ENTITY_FLAG_SCRIPT_PAUSED >> 8)))
473 {
474 uint32_t cull_flags;
475 int32_t cull_radius;
476
477 if (!runtime_actor)
478 {
479 cull_flags = 0;
480 cull_radius = 0;
481 }
482 else
483 {
484 cull_radius = (int32_t)runtime_actor->cull_radius;
485 cull_flags = runtime_actor->behavior_flags & ACTOR_BEHAVIOR_CULL_MODE;
486 }
487
488 if (Graphics_CheckFrustumCull(entity_state->position_ptr, cull_radius + ENTITY_VISIBILITY_ENTER_MARGIN, cull_flags) && entity_state)
489 {
490 uint32_t flags_5 = entity_state->flags;
491
492 // Relink the live actor as its collision-detached flag is cleared.
494 entity_state->flags = flags_5;
495
496 if (runtime_actor)
497 Actor_AddToCollisionList((struct Actor_State *)(uintptr_t)(runtime_actor));
498 }
499 }
500 }
501
502 uint32_t flags_1 = entity_state->flags;
503
504 if (((uint32_t)flags_1 >> 8 & 0xff) & (ENTITY_FLAG_UPDATE_GUARD >> 8))
505 return 0;
506
507 flags_1 |= ENTITY_FLAG_UPDATE_GUARD;
508 entity_state->flags = flags_1;
509
510 int32_t distance_delta[3];
511
512 if (runtime_actor || ((uint32_t)flags_1 >> 8 & 0xff) & (ENTITY_FLAG_ACTIVATION_REQUEST >> 8))
513 {
515 {
516 struct Level_Data *active_level = pkg_resource_current_level_data;
517 int32_t active_entity_index = Entity_IndexInLevel(active_level, entity_state);
518 int32_t current_active_entity_index = (uint16_t)(active_level->current_entity_index);
519
520 if (active_entity_index != current_active_entity_index && entity_state->collision_height_sq != ENTITY_DISTANCE_LIMIT_DISABLED)
521 {
522 struct EntityNavigationWorkListBuffer *active_worklist =
524 int32_t active_entry_index = 0;
525
526 if (active_worklist->active_count > 0)
527 {
528 while (true)
529 {
530 struct Math_Vec3I32 *position_ptr = entity_state->position_ptr;
531
532 distance_delta[0] = position_ptr->x - (uint32_t)active_worklist->active_entities[active_entry_index]->position_ptr->x;
533 distance_delta[1] = position_ptr->y - (uint32_t)active_worklist->active_entities[active_entry_index]->position_ptr->y;
534 distance_delta[2] = position_ptr->z - (uint32_t)active_worklist->active_entities[active_entry_index]->position_ptr->z;
535
536 if ((uint32_t)(((uint64_t)((int64_t)distance_delta[0] * distance_delta[0]) + (uint64_t)((int64_t)distance_delta[1] * distance_delta[1]) + (uint64_t)((int64_t)distance_delta[2] * distance_delta[2])) >> MATH_Q12_SHIFT) < entity->collision_height_sq)
537 {
538 active_worklist =
540
541 break;
542 }
543
544 active_worklist =
546 active_entry_index += 1;
547
548 if (active_entry_index >= (int32_t)active_worklist->active_count)
549 break;
550
551 entity_state = entity;
552 }
553
554 entity_state = entity;
555 }
556
557 if (active_entry_index == active_worklist->active_count)
558 {
559 uint32_t flags_3 = entity_state->flags;
560
561 flags_3 &= ~(ENTITY_FLAG_ACTIVATION_REQUEST);
562 entity_state->flags = flags_3;
563 }
564 }
565 }
566
567 int32_t restore_defaults = entity_state->flags & (ENTITY_FLAG_ACTIVATION_REQUEST | ENTITY_FLAG_RESTORE_DEFAULTS);
568
569 if (restore_defaults != ENTITY_FLAG_ACTIVATION_REQUEST && (!runtime_actor || runtime_actor->lifecycle_flags & ACTOR_LIFECYCLE_DESTROYING || !((struct Player_ControllerRecordView *)runtime_actor->record_ptr)->death_stage))
570 Entity_DestroyActor(entity_state, restore_defaults);
571 }
572 else
573 {
574 if (((uint32_t)flags_1 >> 8 & 0xff) & ((ENTITY_FLAG_RESTORE_DEFAULTS >> 8) | (ENTITY_FLAG_SCRIPT_PAUSED >> 8)))
575 return 0;
576
578 {
579 if (!((uint8_t)flags_1 & ENTITY_FLAG_ACTIVE_WORKLIST_REQUEST))
580 {
581 struct Level_Data *spawn_level = pkg_resource_current_level_data;
582 int32_t spawn_entity_index = Entity_IndexInLevel(spawn_level, entity_state);
583 int32_t current_spawn_entity_index = (uint16_t)(spawn_level->current_entity_index);
584
585 if (spawn_entity_index != current_spawn_entity_index && entity_state->collision_radius_sq != ENTITY_DISTANCE_LIMIT_DISABLED)
586 {
587 struct EntityNavigationWorkListBuffer *active_entity_list =
589 int32_t spawn_entry_index = 0;
590
591 if (active_entity_list->active_count > 0)
592 {
593 while (true)
594 {
595 distance_delta[0] = entity_state->bonus_respawn_pos.x - (uint32_t)active_entity_list->active_entities[spawn_entry_index]->position_ptr->x;
596 distance_delta[1] = entity_state->bonus_respawn_pos.y - (uint32_t)active_entity_list->active_entities[spawn_entry_index]->position_ptr->y;
597 distance_delta[2] = entity_state->bonus_respawn_pos.z - (uint32_t)active_entity_list->active_entities[spawn_entry_index]->position_ptr->z;
598
599 if ((uint32_t)(((uint64_t)((int64_t)distance_delta[0] * distance_delta[0]) + (uint64_t)((int64_t)distance_delta[1] * distance_delta[1]) + (uint64_t)((int64_t)distance_delta[2] * distance_delta[2])) >> MATH_Q12_SHIFT) < entity->collision_radius_sq)
600 {
601 active_entity_list = EntityNavigationWorkList_ActiveBufferPtr;
602
603 break;
604 }
605
606 active_entity_list = EntityNavigationWorkList_ActiveBufferPtr;
607 spawn_entry_index += 1;
608
609 if (spawn_entry_index >= active_entity_list->active_count)
610 break;
611
612 entity_state = entity;
613 }
614
615 entity_state = entity;
616 }
617
618 if ((uint32_t)spawn_entry_index == (uint32_t)active_entity_list->active_count)
619 return 0;
620 }
621 }
622
623 uint32_t flags_2 = entity_state->flags;
624
626 entity_state->flags = flags_2;
627 }
628 }
629
630 uint32_t flags_4 = entity_state->flags;
631
633 return 0;
634
635 int32_t param_10_c;
636
637 if ((uint8_t)flags_4 & ENTITY_FLAG_FACING_REQUEST)
638 param_10_c = entity_state->param_10C;
639 else
640 param_10_c = 0;
641
643 entity_state->flags = flags_4;
644
645 if (((uint32_t)flags_4 >> 8 & 0xff) & (ENTITY_FLAG_RESTORE_DEFAULTS >> 8))
646 {
647 flags_4 &= ~(ENTITY_FLAG_ACTIVATION_REQUEST);
648 entity_state->flags = flags_4;
649 }
650 else
651 {
652 if (!runtime_actor)
653 {
654 struct Actor_State *spawned_actor = Entity_SpawnActor(entity_state);
655
656 runtime_actor = (struct Actor *)(uintptr_t)(spawned_actor);
657 }
658
659 if (runtime_actor)
660 {
661 struct Player_ControllerRecordView *runtime_record = (struct Player_ControllerRecordView *)(uintptr_t)(runtime_actor->record_ptr);
662
663 memset(runtime_record, 0, ENTITY_CONTROLLER_RESET_BYTES);
665 }
666 }
667
669 entity_state->path_best_distance = 0;
670
671 if (param_10_c)
672 {
673 int32_t target_offset_step = entity_state->target_offset_step;
674
675 if (target_offset_step)
676 {
677 int32_t next_offset_phase = entity_state->target_offset_phase + target_offset_step;
678 uint32_t offset_phase_upper_bound = entity_state->param_110 >> 1;
679
680 entity_state->target_offset_phase = next_offset_phase;
681
682 int32_t offset_phase_lower_bound = -(offset_phase_upper_bound);
683
684 if (next_offset_phase <= offset_phase_lower_bound)
685 {
686 entity_state->target_offset_phase = offset_phase_lower_bound;
687 entity_state->target_offset_step = -(target_offset_step);
688 }
689 else if (next_offset_phase >= (int32_t)offset_phase_upper_bound)
690 {
691 entity_state->target_offset_phase = offset_phase_upper_bound;
692 entity_state->target_offset_step = -(target_offset_step);
693 }
694 }
695 else
696 {
697 int32_t pending_live_speed = entity_state->param_114;
698
699 if (pending_live_speed)
700 Entity_SetActorProperty(entity_state, PROP_LIVESPEED, pending_live_speed);
701 }
702 }
703 else
704 {
705 entity_state->target_offset_phase = 0;
706 entity_state->target_offset_step = 0;
707 }
708
710 // Scripts are eligible only when pause suppression and collision-detached bits are clear.
712
713 uint32_t cull_flags_1;
714 int32_t cull_radius_1;
715
716 if (!runtime_actor)
717 {
718 cull_flags_1 = 0;
719 cull_radius_1 = 0;
720 }
721 else
722 {
723 cull_radius_1 = (int32_t)runtime_actor->cull_radius;
724 cull_flags_1 = runtime_actor->behavior_flags & ACTOR_BEHAVIOR_CULL_MODE;
725 }
726
727 if (!Graphics_CheckFrustumCull(entity_state->position_ptr, cull_radius_1 + ENTITY_VISIBILITY_LEAVE_MARGIN, cull_flags_1))
728 {
729 uint32_t flags_6 = entity_state->flags;
730
731 // The wider visibility margin controls collision-list detachment.
733 entity_state->flags = flags_6;
734
735 if (runtime_actor)
736 {
737 struct Actor *collision_list_actor = Actor_CollisionListHead;
738 struct Actor **collision_list_link = &Actor_CollisionListHead;
739
740 for (; collision_list_actor != runtime_actor;
741 collision_list_actor = *collision_list_link)
742 collision_list_link = (struct Actor **)(uintptr_t)(collision_list_actor);
743
744 *collision_list_link = runtime_actor->list_next;
745 }
746 }
747
749}
750
751// Original pcdogs.exe: 0x409560
752// Returns the current active entity's actor or the first active actor found in the level entity array. The fallback uses the active work-list count as its bound rather than the entity-array count.
754{
756
757 if (!level || (uint16_t)level->entity_count == 0)
758 return 0;
759
760 struct Entity *entity_array = level->entity_array;
761 int32_t current_entity_index = (uint16_t)(level->current_entity_index);
762 struct Entity *current_entity = &entity_array[current_entity_index];
763
764 if (*(uint8_t *)&current_entity->flags & ENTITY_FLAG_ACTIVE_WORKLIST_REQUEST)
765 return (struct Actor_State *)current_entity->runtime_actor;
766
767 int32_t entity_index = 0;
768 int32_t active_count = EntityNavigationWorkList_ActiveBufferPtr->active_count;
769
770 if (active_count > 0)
771 {
772 do
773 {
774 if (entity_array->flags & ENTITY_FLAG_ACTIVE_WORKLIST_REQUEST)
775 return (struct Actor_State *)entity_array->runtime_actor;
776
777 entity_index += 1;
778 entity_array = &entity_array[1];
779 } while (entity_index < active_count);
780 }
781
782 return 0;
783}
784
785// Original pcdogs.exe: 0x410f90
786// Requests activation and spawning when an entity has neither a live actor nor the existing activation flag pattern. It changes flags and performs side effects without returning a reliable actor pointer.
788{
789 if (entity->runtime_actor)
790 return;
791
792 uint32_t flags = entity->flags;
793
795 return;
796
799 entity->flags = flags;
800 Entity_SpawnActor(entity);
801}
#define ACTOR_BEHAVIOR_CULL_MODE
Definition constants.h:9
#define ACTOR_BEHAVIOR_GROUND_TILT_ENABLED
Definition constants.h:21
#define ACTOR_BEHAVIOR_GROUND_TRACKING
Definition constants.h:12
#define ACTOR_LIFECYCLE_DESTROYING
Definition constants.h:43
Actor_PropID
Definition enums.h:7
@ PROP_LIVESPEED
Definition enums.h:28
@ PROP_RESPONSEXZ
Definition enums.h:25
@ PROP_PARAM_108
Definition enums.h:19
@ PROP_COLLRADIUS
Definition enums.h:34
@ PROP_COLLHEIGHT
Definition enums.h:37
@ PROP_PUSHXZ
Definition enums.h:22
@ PROP_TRACEMODE0
Definition enums.h:40
@ PROP_SPEED
Definition enums.h:13
@ PROP_ROTATION
Definition enums.h:16
@ PROP_TRACEMODE3
Definition enums.h:49
@ PROP_CAMERAXZ
Definition enums.h:31
@ PROP_MAX_HEALTH
Definition enums.h:10
void Actor_AddToCollisionList(struct Actor_State *actor)
Definition collision.c:1414
void Actor_ReleaseBindings(struct Player_ControllerRecordView *record)
Definition attachments.c:68
#define Actor_CollisionListHead
Definition globals.h:58
struct Actor_State * Actor_CloneTemplateWithTemplateRelativeFixups(struct Actor_State *template_actor, struct Actor_State **list_head)
Definition lifecycle.c:311
void Actor_CopyEntityDefaultsToRecord(struct Actor_State *actor)
Definition lifecycle.c:250
void Actor_ApplyEntitySlotFlags(struct Actor_State *actor)
Definition lifecycle.c:166
#define AUDIO_DETACH_EMITTER_MODE
Definition constants.h:50
void Audio_StopOrUpdateSoundsAtPosition(int32_t sound_index_or_mode, struct Math_Vec3I32 *position, char looping_only)
Definition spatial.c:76
void Component_RefreshCollisionRanges(struct Component_CollisionRecord *collision_records, int32_t slot_count)
Definition collision.c:42
#define ENTITY_DISTANCE_LIMIT_DISABLED
Definition constants.h:44
#define ENTITY_CONTROLLER_COLLISION_SLOT_COUNT
Definition constants.h:45
#define ENTITY_FLAG_PERSISTENT_BIT_17
Definition constants.h:24
#define ENTITY_RUNTIME_BINDING_BYTES
Definition constants.h:41
#define ENTITY_ANGLE_SCALE_Q12
Definition constants.h:32
#define ENTITY_FLAG_COLLISION_LIST_UNLINKED
Definition constants.h:21
#define ENTITY_UNLIMITED_LIVES
Definition constants.h:43
#define ENTITY_FLAG_PAUSE_EXEMPT
Definition constants.h:26
#define ENTITY_CONTROLLER_RESET_BYTES
Definition constants.h:42
#define ENTITY_FLAG_NAVIGATION_GOAL_ENABLED
Definition constants.h:23
#define ENTITY_PROPERTY_FLAG_BASE
Definition constants.h:30
#define ENTITY_VISIBILITY_ENTER_MARGIN
Definition constants.h:34
#define ENTITY_PROPERTY_RESTORE_SAVED
Definition constants.h:29
#define ENTITY_FLAG_FACING_REQUEST
Definition constants.h:10
#define ENTITY_FIXED_UPDATE_RATE
Definition constants.h:38
#define ENTITY_VISIBILITY_LEAVE_MARGIN
Definition constants.h:35
#define ENTITY_FLAG_ACTIVE_WORKLIST_REQUEST
Definition constants.h:9
#define ENTITY_FLAG_GROUP_REVERSE
Definition constants.h:14
#define ENTITY_FLAG_INDEX_MASK
Definition constants.h:37
#define ENTITY_FLAG_SCRIPT_PAUSED
Definition constants.h:20
@ ENTITY_DIRECTION_SPEED_HEIGHT_OFFSET_FIRST
Definition constants.h:50
#define ENTITY_FLAG_ACTIVE_WORKLIST_DEFERRED
Definition constants.h:11
#define ENTITY_FLAG_ACTIVATION_REQUEST
Definition constants.h:17
#define ENTITY_FLAG_UPDATE_GUARD
Definition constants.h:19
#define ENTITY_FLAG_RESTORE_DEFAULTS
Definition constants.h:18
#define ENTITY_ANIMATION_PENDING
Definition constants.h:39
#define ENTITY_FLAG_DAMAGE_REACTION_DISABLED
Definition constants.h:27
#define ENTITY_RECORD_STRIDE
Definition constants.h:31
#define ENTITY_FLAG_PERSISTENT_BIT_15
Definition constants.h:22
#define ENTITY_FLAG_ACTOR_COLLISION_SCAN
Definition constants.h:25
#define EntityNavigationWorkList_ActiveBufferPtr
Definition globals.h:12
BOOL Entity_UpdateVisibilityAndSpawn(struct Entity *entity, struct Entity *reactivation_requester_or_null)
Definition lifecycle.c:452
void Entity_SetActorProperty(struct Entity *entity, enum Actor_PropID prop_id, int32_t value)
Definition lifecycle.c:63
void Entity_EnsureCameraActorActive(struct Entity *entity)
Definition lifecycle.c:787
static int32_t Entity_IndexInLevel(const struct Level_Data *level, const struct Entity *entity)
Definition lifecycle.c:31
struct Actor_State * Entity_SpawnActor(struct Entity *entity)
Definition lifecycle.c:287
void Entity_EnableChildGroundOrientation(struct Entity *entity)
Definition lifecycle.c:378
void Entity_DestroyActor(struct Entity *entity, int32_t restore_defaults)
Definition lifecycle.c:403
BOOL Entity_IsInActiveList(int32_t entity_index_1based)
Definition lifecycle.c:39
struct Actor_State * Entity_GetCurrentOrFirstActiveActor()
Definition lifecycle.c:753
#define GAME_TRANSITION_SCRIPT_PAUSED
Definition constants.h:20
#define Game_FrameTransitionFlags
Definition globals.h:47
int32_t Graphics_CheckFrustumCull(struct Math_Vec3I32 *position, int32_t cull_radius, uint32_t cull_flags)
Definition clipping.c:772
#define MATH_Q12_SHIFT
Definition constants.h:8
#define MATH_Q12_ONE
Definition constants.h:9
void Math_BuildMatrixRotationY(struct Math_Matrix3x3I16 *out_matrix, struct Math_Matrix3x3I16 const *left_factor, int32_t angle)
Definition matrices.c:76
#define pkg_resource_current_level_data
Definition globals.h:117
uint32_t Timer_GetElapsedTickCountThunk()
Definition timer.c:9
uint8_t component_count
Definition types.h:430
int16_t facing_current
Definition types.h:447
struct Collision_Node * ground_collision_node
Definition types.h:451
int32_t behavior_flags
Definition types.h:407
int16_t facing_target
Definition types.h:448
void * record_ptr
Definition types.h:477
int16_t fade_timer
Definition types.h:438
int16_t facing_blend
Definition types.h:449
struct Math_Matrix3x3I16 rot_mat
Definition types.h:383
int16_t roll_angle
Definition types.h:450
struct Math_Vec3I32 position
Definition types.h:385
Definition types.h:118
struct Pkg_ActorRecord * record_ptr
Definition types.h:207
uint8_t lifecycle_flags
Definition types.h:139
struct Component_Instance * parent_component
Definition types.h:190
struct Actor * list_next
Definition types.h:119
int32_t scale_xy
Definition types.h:145
int32_t behavior_flags
Definition types.h:160
struct Math_Vec2I16 visual_scale
Definition types.h:146
int16_t collision_height_hi
Definition types.h:175
struct Actor * child_actor
Definition types.h:208
int16_t collision_height
Definition types.h:174
int16_t cull_radius
Definition types.h:177
int16_t collision_radius
Definition types.h:176
struct Component_Definition * definition
Definition types.h:43
struct Actor_State * reference_actor
Definition types.h:48
uint16_t damage_cooldown
Definition types.h:45
struct Actor_State * position_source_actor
Definition types.h:46
struct Entity * active_entities[0x4]
Definition types.h:133
Definition types.h:22
uint8_t saved_param_direction_mode
Definition types.h:67
int32_t param_10C
Definition types.h:100
uint8_t param_direction_mode
Definition types.h:96
uint32_t flags
Definition types.h:25
struct Math_Vec3I32 * runtime_target_pos
Definition types.h:49
int16_t attach_offset_x
Definition types.h:42
int32_t saved_param_114
Definition types.h:73
uint32_t collision_radius_sq
Definition types.h:36
int32_t saved_param_max_speed_2
Definition types.h:56
int32_t param_max_health
Definition types.h:98
int32_t param_max_speed_2
Definition types.h:85
int32_t script_timer
Definition types.h:109
int32_t saved_param_118
Definition types.h:74
struct Math_Vec3I32 bonus_respawn_pos
Definition types.h:35
struct Actor * actor_template_header
Definition types.h:46
uint32_t collision_height_sq
Definition types.h:37
struct Math_Vec3I32 * position_ptr
Definition types.h:78
uint8_t behavior_index
Definition types.h:110
uint32_t saved_flags
Definition types.h:120
int32_t param_F0
Definition types.h:90
uint32_t saved_collision_height_sq
Definition types.h:119
struct Actor * runtime_actor
Definition types.h:107
int32_t param_110
Definition types.h:101
int32_t saved_param_110
Definition types.h:72
int32_t param_114
Definition types.h:102
int32_t param_108
Definition types.h:99
void * shared_ref_50
Definition types.h:47
int32_t saved_param_10C
Definition types.h:71
uint32_t saved_collision_radius_sq
Definition types.h:118
int32_t saved_param_max_health
Definition types.h:69
int32_t param_accel_0
Definition types.h:84
int32_t param_turn_step
Definition types.h:91
int32_t saved_param_108
Definition types.h:70
int32_t target_offset_step
Definition types.h:117
int32_t saved_param_accel_0
Definition types.h:55
int32_t target_offset_phase
Definition types.h:116
int32_t param_118
Definition types.h:103
int16_t default_anim_state
Definition types.h:43
int32_t path_best_distance
Definition types.h:112
int32_t local_vars[0x9]
Definition types.h:44
uint8_t behavior_stack[0x3]
Definition types.h:111
int32_t saved_param_turn_step
Definition types.h:62
uint8_t active_flag
Definition types.h:38
int16_t entity_count
Definition types.h:44
int16_t current_entity_index
Definition types.h:43
struct Entity * entity_array
Definition types.h:45
int16_t y
Definition types.h:403
int32_t y
Definition types.h:471
int32_t x
Definition types.h:470
int32_t z
Definition types.h:472
struct Actor_ContactSlot contact_slots[4]
Definition types.h:216
struct Math_Vec3I32 * position_ptr
Definition types.h:137
int32_t direction_mode_height_offset
Definition types.h:131
uint16_t reaction_animation_ticks
Definition types.h:136
struct Component_CollisionRecord * component_records
Definition types.h:209