102 Dalmatians: Puppies to the Rescue
Decompilation Source Reference | GitLab repository
Loading...
Searching...
No Matches
movement.c
Go to the documentation of this file.
1/* Recovered game implementation; original addresses identify provenance. */
14#include "pcdogs/actor/types.h"
19#include "pcdogs/entity/types.h"
20#include "pcdogs/game/globals.h"
21#include "pcdogs/level/types.h"
23#include "pcdogs/math/types.h"
25#include "pcdogs/scene/types.h"
28#include <math.h>
29#include <stdarg.h>
30#include <string.h>
31
32// Original pcdogs.exe: 0x404320
33// Resolves an absolute, relative, or target-facing angle and stores the actor's 12-bit facing target. Temporary path-result coordinates are restored, and the return value reports whether the initial facing already matches.
34bool Actor_SetFacingTarget(struct Entity *entity, int32_t target_selector, int32_t angle_degrees_q12)
35{
36 int64_t angle_conversion_product;
37 struct Actor *runtime_actor = entity->runtime_actor;
38 int16_t entity_initial_facing;
39
40 if (runtime_actor)
41 entity_initial_facing = (runtime_actor->facing_blend + runtime_actor->facing_current) & PHYSICS_ANGLE_MASK;
42 else
43 entity_initial_facing = (int16_t)entity->param_F0;
44
45 angle_conversion_product = (int64_t)ENTITY_ANGLE_SCALE_Q12 * angle_degrees_q12;
46 angle_degrees_q12 = ((int32_t)((angle_conversion_product) >> PHYSICS_FIXED_FRACTION_BITS)) >> PHYSICS_FIXED_FRACTION_BITS;
47
48 if (target_selector != ACTOR_FACING_ABSOLUTE)
49 {
50 int32_t scaled_entity_index;
51
52 if (target_selector != ACTOR_FACING_RELATIVE)
53 {
54 int32_t entity_index_product_high;
55
56 entity_index_product_high =
57 (int32_t)((uint64_t)((int64_t)ENTITY_INDEX_RECIPROCAL * ((char *)entity - (char *)pkg_resource_current_level_data->entity_array)) >> 32);
58
59 scaled_entity_index = entity_index_product_high >> 6;
60 }
61
62 if (target_selector == ACTOR_FACING_RELATIVE || target_selector == scaled_entity_index + (scaled_entity_index >> 0x1f) + 1)
63 angle_degrees_q12 += (int32_t)entity_initial_facing;
64 else
65 {
66 int32_t attach_offset_x;
67 int32_t saved_path_result_x = entity->path_result.x;
68 int32_t saved_path_result_y = entity->path_result.y;
69 int32_t saved_path_result_z = entity->path_result.z;
70
71 if (target_selector)
72 attach_offset_x =
73 Actor_TracePath(entity, target_selector, INT32_MAX, INT32_MAX, 0);
74 else
75 attach_offset_x = (int32_t)entity->attach_offset_x;
76
77 struct Math_Vec3I32 *position_ptr = entity->position_ptr;
78 int32_t target_delta_x = entity->path_result.x - position_ptr->x;
79 int32_t target_delta_z = entity->path_result.z - position_ptr->z;
80
81 entity->path_result = (struct Math_Vec3I32){
82 .x = saved_path_result_x,
83 .y = saved_path_result_y,
84 .z = saved_path_result_z};
85
86 if (!attach_offset_x)
87 return (uint8_t)attach_offset_x;
88
89 if ((uint32_t)(target_delta_x + ACTOR_POSITION_FACING_EPSILON) > (2 * ACTOR_POSITION_FACING_EPSILON) || (uint32_t)(target_delta_z + ACTOR_POSITION_FACING_EPSILON) > (2 * ACTOR_POSITION_FACING_EPSILON))
90
91 angle_degrees_q12 += (int32_t)Math_CalculateAtan2FP12(target_delta_z, target_delta_x);
92 else if (target_selector > 0)
93 {
94 struct Entity *entity_array = pkg_resource_current_level_data->entity_array;
95 struct Actor_State *target_actor =
96 (struct Actor_State *)entity_array[target_selector - 1].runtime_actor;
97 int32_t target_entity_facing;
98
99 if (target_actor)
100 {
101 uint16_t target_blend_bits = target_actor->facing_blend;
102 uint16_t target_current_facing_bits = target_actor->facing_current;
103
104 target_entity_facing = (target_blend_bits + target_current_facing_bits) & PHYSICS_ANGLE_MASK;
105 }
106 else
107 target_entity_facing = entity_array[target_selector - 1].param_F0;
108
109 angle_degrees_q12 += target_entity_facing;
110 }
111 }
112 }
113
114 int16_t target_facing_q12 = (uint16_t)angle_degrees_q12 & PHYSICS_ANGLE_MASK;
115
116 if (runtime_actor)
117 runtime_actor->facing_target = target_facing_q12;
118
119 return entity_initial_facing == target_facing_q12;
120}
121
122// Original pcdogs.exe: 0x4044b0
123// Clears an entity actor's velocity and starts movement toward its stored path target, converting Q12 duration to frame units. Entities without runtime actors are ignored.
124void Actor_ResetVelocityAndMoveToTarget(struct Entity *entity, int16_t facing_selector, int32_t facing_angle_degrees_q12, int32_t duration_q12)
125{
126 struct Actor *runtime_actor = entity->runtime_actor;
127
128 if (runtime_actor)
129 {
130 runtime_actor->velocity = (struct Math_Vec3I32){
131 .z = 0,
132 .y = 0,
133 .x = 0};
134
135 Actor_MoveToTarget((struct Actor_State *)(runtime_actor), &entity->path_target, facing_selector,
136 facing_angle_degrees_q12, (duration_q12 * ACTOR_ANIMATION_FRAMES_PER_SECOND) >> PHYSICS_FIXED_FRACTION_BITS);
137 }
138}
139
140// Original pcdogs.exe: 0x404500
141// Stores a movement target, shifts attached position sources by the same delta, and saves velocity and facing parameters. A zero low-word duration snaps immediately; otherwise the full duration feeds the fixed-point velocity calculation.
142void Actor_MoveToTarget(struct Actor_State *actor, struct Math_Vec3I32 const *target_pos, int16_t facing_selector, int32_t facing_angle_degrees_q12, int32_t duration_frames)
143{
144 struct Actor_MovementRecordView *movement_record = actor->record_ptr;
145
146 if (movement_record->attachment_lifetime > 0)
147 Actor_ReleaseAttachment(movement_record);
148
149 struct Math_Vec3I32 *move_target_position;
150
151 if (!target_pos)
152 {
153 move_target_position = &movement_record->move_target;
154 movement_record->move_target.z = 0;
155 movement_record->move_target.y = 0;
156 move_target_position->x = 0;
157 }
158 else
159 {
160 move_target_position = &movement_record->move_target;
161 // The target coordinates are copied; ownership remains with the caller.
162 move_target_position->x = target_pos->x;
163 move_target_position->y = target_pos->y;
164 move_target_position->z = target_pos->z;
165 }
166
167 int32_t delta_x = move_target_position->x - actor->position.x;
168 int32_t delta_y = movement_record->move_target.y - actor->position.y;
169 int32_t delta_z = movement_record->move_target.z - actor->position.z;
170 int32_t saved_target_delta_y = delta_y;
171
172 for (int32_t component_index = 0; component_index < 2; component_index++)
173 {
174 struct Actor_State *source_actor_x =
175 movement_record->collision_records[component_index].position_source_actor;
176
177 if (source_actor_x)
178 {
179 source_actor_x->position.x += delta_x;
180
181 struct Actor_State *source_actor_y =
182 movement_record->collision_records[component_index].position_source_actor;
183
184 source_actor_y->position.y += delta_y;
185
186 struct Actor_State *source_actor_z =
187 movement_record->collision_records[component_index].position_source_actor;
188
189 source_actor_z->position.z += delta_z;
190 delta_y = saved_target_delta_y;
191 }
192 }
193
194 movement_record->saved_velocity.x = actor->velocity.x;
195 movement_record->saved_velocity.y = actor->velocity.y;
196 movement_record->facing_selector = facing_selector;
197 movement_record->saved_velocity.z = actor->velocity.z;
198 // The move countdown retains only the low 16 bits of the duration.
199 movement_record->move_frames_remaining = (uint16_t)duration_frames;
200 movement_record->facing_angle_degrees_q12 = facing_angle_degrees_q12;
201
202 if (!(uint16_t)duration_frames)
203 {
205
206 return;
207 }
208
209 int32_t step_scale_q12 = PHYSICS_MOVEMENT_DURATION_SCALE_Q24 / (duration_frames << PHYSICS_FIXED_FRACTION_BITS);
210 int64_t velocity_x_product_wide = (int64_t)step_scale_q12 * delta_x;
211 int32_t velocity_x_product_high = (int32_t)((uint64_t)velocity_x_product_wide >> 32);
212 int32_t velocity_x_product_low = (uint32_t)velocity_x_product_wide;
213
214 actor->velocity.x = (uint32_t)velocity_x_product_low >> PHYSICS_FIXED_FRACTION_BITS | velocity_x_product_high << PHYSICS_Q12_PRODUCT_HIGH_SHIFT;
215
216 int64_t velocity_y_product_wide = (int64_t)step_scale_q12 * saved_target_delta_y;
217 int32_t velocity_y_product_high = (int32_t)((uint64_t)velocity_y_product_wide >> 32);
218 int32_t velocity_y_product_low = (uint32_t)velocity_y_product_wide;
219
220 actor->velocity.y = (uint32_t)velocity_y_product_low >> PHYSICS_FIXED_FRACTION_BITS | velocity_y_product_high << PHYSICS_Q12_PRODUCT_HIGH_SHIFT;
221
222 int64_t velocity_z_product_wide = (int64_t)step_scale_q12 * delta_z;
223 int32_t velocity_z_product_high = (int32_t)((uint64_t)velocity_z_product_wide >> 32);
224 int32_t velocity_z_product_low = (uint32_t)velocity_z_product_wide;
225
226 actor->velocity.z = (uint32_t)velocity_z_product_low >> PHYSICS_FIXED_FRACTION_BITS | velocity_z_product_high << PHYSICS_Q12_PRODUCT_HIGH_SHIFT;
227 Actor_StartTransition(actor, movement_record);
228}
229
230// Original pcdogs.exe: 0x404700
231// Snaps the actor to its stored target, restores saved velocity, and resolves ground and child state before updating facing. Ground projection can adjust the snapped height.
233{
234 struct Actor_MovementRecordView *movement_record = actor->record_ptr;
235
236 if (actor->transition_phase == ACTOR_PHASE_LEAVING || actor->position.x != movement_record->move_target.x || actor->position.z != movement_record->move_target.z || actor->position.y != movement_record->move_target.y)
237 {
238 actor->position.x = movement_record->move_target.x;
239 actor->position.y = movement_record->move_target.y;
240 actor->position.z = movement_record->move_target.z;
241 actor->velocity.x = movement_record->saved_velocity.x;
242 actor->velocity.y = movement_record->saved_velocity.y;
243 actor->velocity.z = movement_record->saved_velocity.z;
244
248
249 int32_t snapped_behavior_flags = actor->behavior_flags | (ACTOR_BEHAVIOR_AIRBORNE | ACTOR_BEHAVIOR_COLLISION_CORRECTION);
250
251 actor->ground_collision_node = NULL;
252 actor->behavior_flags = snapped_behavior_flags;
253
254 while (true)
255 {
257
258 if (actor->ground_collision_node)
259 break;
260
261 if (actor->parent_actor)
262 break;
263
264 int32_t behavior_flags = actor->behavior_flags;
265
266 if (!((uint8_t)behavior_flags & ACTOR_BEHAVIOR_AIRBORNE))
267 break;
268
269 behavior_flags &= ~ACTOR_BEHAVIOR_AIRBORNE;
270 actor->behavior_flags = behavior_flags;
271 }
272
273 int32_t behavior_flags_2 = actor->behavior_flags;
274
275 behavior_flags_2 &= ~ACTOR_BEHAVIOR_MOVEMENT_LOCKED;
276 actor->behavior_flags = behavior_flags_2;
277 movement_record->move_frames_remaining = 0;
278 movement_record->movement_counter_e = 0;
279 movement_record->movement_counter_d = 0;
280
281 struct Collision_Node *ground_collision_node = actor->ground_collision_node;
282
283 if (ground_collision_node)
284 {
285 Collision_ProjectPointOntoPolygonSurface(ground_collision_node,
286 actor->ground_contact_polygon, &actor->position, &movement_record->move_target);
287
288 int32_t behavior_flags_1 = actor->behavior_flags;
289
290 if (!((uint8_t)behavior_flags_1 & ACTOR_BEHAVIOR_AIRBORNE) || movement_record->move_target.y - actor->position.y > -ACTOR_MOVE_TARGET_GROUND_HEIGHT_SLOP)
291 {
292 behavior_flags_1 &= ~ACTOR_BEHAVIOR_AIRBORNE;
293 actor->position.y = movement_record->move_target.y;
294 actor->behavior_flags = behavior_flags_1;
295
298 }
299
300 struct Actor_State *child_actor_1 = actor->child_actor;
301
302 if (child_actor_1)
303 {
304 child_actor_1->position.x = movement_record->move_target.x;
305 child_actor_1->position.y = movement_record->move_target.y;
306 child_actor_1->position.z = movement_record->move_target.z;
307
308 struct Actor_State *child_actor_2 = actor->child_actor;
309
310 child_actor_2->lifecycle_flags |= ACTOR_LIFECYCLE_HIDDEN;
311
312 struct Scene_Node *render_root = actor->render_node_list[0];
313 uint8_t traversal_flags = render_root->traversal_flags;
314
316
318
319 struct Scene_Node *render_root_after_orientation =
320 actor->render_node_list[0];
321
322 render_root_after_orientation->traversal_flags &= ~((traversal_flags & SCENE_NODE_DISABLED) ^ SCENE_NODE_DISABLED);
323 }
324 }
325 else
326 {
327 struct Actor_State *child_actor = actor->child_actor;
328
329 actor->position.y = movement_record->move_target.y;
331
332 if (child_actor)
334 }
335
338 }
339
341 &pkg_resource_current_level_data->entity_array[(int32_t)movement_record->entity_index],
342 (int32_t)movement_record->facing_selector, movement_record->facing_angle_degrees_q12);
343 actor->facing_current = actor->facing_target;
345}
346
347// Original pcdogs.exe: 0x409970
348// Counts down a scripted move, waiting for a transitioning parent when necessary, then snaps the actor and clears the owning entity's transition flag when allowed. The countdown uses 16-bit arithmetic.
350{
351 struct Actor_State *parent_actor = actor->parent_actor;
352 struct Actor_MovementRecordView *movement_record = actor->record_ptr;
353
354 if (parent_actor)
355 {
356 if (parent_actor->transition_phase == ACTOR_PHASE_LEAVING)
357 return;
358
359 movement_record->move_frames_remaining = 1;
360 }
361
362 movement_record->move_frames_remaining -= 1;
363
364 if (movement_record->move_frames_remaining)
365 return;
366
368
369 struct Level_Data *current_level = pkg_resource_current_level_data;
370 int32_t entity_index;
371
373 entity_index = (int32_t)movement_record->entity_index;
374
376 return;
377
378 struct Entity *owner_entity =
379 &current_level->entity_array[(int32_t)movement_record->entity_index];
380 uint32_t flags = owner_entity->flags;
381
383 owner_entity->flags = flags;
384}
385
386// Original pcdogs.exe: 0x40f370
387// Consumes a distance budget while advancing root-motion spline samples and updating the animation tick. A clamped nonlooping endpoint can prevent progress, and zero budget with zero displacement can reach division by zero.
388void Actor_ApplySplineMovement(struct Scene_Type1RenderOwnerView *owner, struct Animation_SplineChannelVec3 *spline_track, int32_t tick_delta)
389{
390 struct Scene_Type1RenderOwnerView *motion_owner = owner;
391 int32_t remaining_distance_budget = (int32_t)motion_owner->animation_step_q6 << 6;
392
393 do
394 {
395 int32_t segment_start_tick = motion_owner->animation_tick;
396 int32_t track_end_tick = spline_track->packed_time_bias_and_end_time >> 0xe & ANIMATION_SPLINE_TIME_Q6_MASK;
397 int32_t requested_animation_tick = motion_owner->animation_tick + tick_delta;
398
399 motion_owner->animation_tick = requested_animation_tick;
400
401 if ((uint32_t)requested_animation_tick >= (uint32_t)track_end_tick)
402 {
403 if (!(spline_track->channel_flags & ((int)ANIMATION_SPLINE_WRAP_SAMPLE_TIME)))
404 {
405 int32_t requested_tick_is_negative = (uint8_t)(requested_animation_tick < 0);
406
407 motion_owner->animation_tick = (requested_tick_is_negative - 1) & track_end_tick;
408 }
409 else if ((uint32_t)requested_animation_tick > (uint32_t)track_end_tick)
410 {
411 int32_t previous_animation_tick = requested_animation_tick - tick_delta;
412
413 motion_owner->animation_tick = previous_animation_tick;
414
415 if (tick_delta >= 0)
416 {
417 if (previous_animation_tick == track_end_tick)
418 break;
419
420 motion_owner->animation_tick = track_end_tick;
421 }
422 else
423 {
424 if (!previous_animation_tick)
425 break;
426
427 motion_owner->animation_tick = 0;
428 }
429 }
430 }
431
432 int32_t animation_tick = motion_owner->animation_tick;
433
435 {
436 struct Math_Vec3I32 sampled_target_pos; // Evaluate spline at current time to get target position
437
438 Animation_InterpolateVec3(animation_tick, spline_track, &sampled_target_pos);
439
440 struct Math_Vec3I32 *local_position = &motion_owner->transform.local_position;
441 struct Math_Vec3I32 segment_delta = (struct Math_Vec3I32){
442 .x = sampled_target_pos.x - motion_owner->transform.local_position.x,
443 .y = sampled_target_pos.y - motion_owner->transform.local_position.y,
444 .z = sampled_target_pos.z - motion_owner->transform.local_position.z};
445
446 int32_t segment_distance = // Calculate distance to target using FastSquareRoot for normalization
447 Math_SqrtFP12((int32_t)((uint64_t)(((int64_t)(segment_delta.z) * segment_delta.z) + ((int64_t)(segment_delta.x) * segment_delta.x) + ((int64_t)(segment_delta.y) * segment_delta.y)) >> PHYSICS_FIXED_FRACTION_BITS));
448
449 // Consume short segments completely and clip longer segments to the remaining budget.
450 if (segment_distance >= remaining_distance_budget)
451 {
452 int32_t segment_fraction_q12 =
453 ((int64_t)remaining_distance_budget * PHYSICS_FIXED_ONE) / segment_distance;
454
455 segment_delta.x = (int32_t)((uint64_t)(((int64_t)(segment_fraction_q12)*segment_delta.x)) >> PHYSICS_FIXED_FRACTION_BITS);
456 segment_delta.y = (int32_t)((uint64_t)(((int64_t)(segment_fraction_q12)*segment_delta.y)) >> PHYSICS_FIXED_FRACTION_BITS);
457
458 int32_t clipped_delta_z = (int32_t)((uint64_t)(((int64_t)(segment_fraction_q12)*segment_delta.z)) >> PHYSICS_FIXED_FRACTION_BITS);
459 int32_t clipped_delta_x = segment_delta.x;
460
461 segment_delta.z = clipped_delta_z;
462 owner->transform.local_position = (struct Math_Vec3I32){
463 .x = owner->transform.local_position.x + clipped_delta_x,
464 .y = owner->transform.local_position.y + segment_delta.y,
465 .z = owner->transform.local_position.z + clipped_delta_z};
466
467 // Scale the tick delta by the partial-segment fraction before applying the reduced advance.
468 int32_t clipped_tick_delta = (int32_t)((uint64_t)(((int64_t)(segment_fraction_q12)*tick_delta)) >> PHYSICS_FIXED_FRACTION_BITS);
469
470 owner->animation_tick = segment_start_tick;
471
472 int32_t clipped_animation_tick = segment_start_tick + clipped_tick_delta;
473 int32_t packed_time_bias_and_end_time = spline_track->packed_time_bias_and_end_time;
474
475 owner->animation_tick = clipped_animation_tick;
476
477 int32_t clipped_track_end_tick = packed_time_bias_and_end_time >> 0xe & ANIMATION_SPLINE_TIME_Q6_MASK;
478
479 if ((uint32_t)clipped_animation_tick < (uint32_t)clipped_track_end_tick)
480 break;
481
482 if (!(spline_track->channel_flags & ((int)ANIMATION_SPLINE_WRAP_SAMPLE_TIME)))
483 {
484 int32_t clipped_tick_is_negative = (uint8_t)(clipped_animation_tick < 0);
485
486 owner->animation_tick = (clipped_tick_is_negative - 1) & clipped_track_end_tick;
487
488 return;
489 }
490
491 if ((uint32_t)clipped_animation_tick <= (uint32_t)clipped_track_end_tick)
492 break;
493
494 int32_t unclipped_previous_tick = clipped_animation_tick - clipped_tick_delta;
495
496 owner->animation_tick = unclipped_previous_tick;
497
498 if (clipped_tick_delta >= 0)
499 {
500 if (unclipped_previous_tick != clipped_track_end_tick)
501 owner->animation_tick = clipped_track_end_tick;
502 }
503 else if (unclipped_previous_tick)
504 {
505 owner->animation_tick = 0;
506
507 return;
508 }
509
510 break;
511 }
512
513 int32_t sampled_target_y = sampled_target_pos.y;
514
515 motion_owner = owner;
516 remaining_distance_budget -= segment_distance;
517 local_position->x = sampled_target_pos.x;
518
519 int32_t sampled_target_z = sampled_target_pos.z;
520
521 local_position->y = sampled_target_y;
522 local_position->z = sampled_target_z;
523 }
524 else
525 Animation_InterpolateVec3(animation_tick, spline_track,
526 &motion_owner->transform.local_position);
527 } while (remaining_distance_budget);
528}
529
530// Original pcdogs.exe: 0x437b10
531// Turns toward horizontal velocity by a bounded angular step and rebuilds orientation. Returns whether the pre-step angle difference lies outside the inclusive range from minus 113 to 113.
532BOOL Actor_UpdateRotationFromVelocity(struct Actor_State *actor, struct Math_Vec3I32 const *velocity, int32_t turn_step)
533{
534 int32_t x = velocity->x;
535
536 // Keep the previous target facing while both horizontal velocity components are within the dead zone.
537 if ((uint32_t)x + ((uint32_t)ACTOR_VELOCITY_FACING_EPSILON) > (2u * ACTOR_VELOCITY_FACING_EPSILON) || (uint32_t)velocity->z + ((uint32_t)ACTOR_VELOCITY_FACING_EPSILON) > (2u * ACTOR_VELOCITY_FACING_EPSILON))
538 {
539 x = Math_CalculateAtan2FP12(velocity->z, x);
540
541 int32_t blended_facing = actor->facing_blend + actor->facing_current;
542
543 actor->facing_target = (uint16_t)x;
545 actor->facing_current = (blended_facing & PHYSICS_ANGLE_MASK);
546 }
547
548 uint16_t target_facing = actor->facing_target;
549 int16_t facing_current = actor->facing_current;
550 int32_t velocity_facing = (int32_t)(int16_t)target_facing;
551 int32_t facing_current_1 = (int32_t)facing_current;
552 int32_t facing_error = facing_current_1 - velocity_facing;
553
554 if (facing_current_1 != velocity_facing)
555 {
556 x = Math_ClampAngleDelta(target_facing, (actor->facing_blend + facing_current) & PHYSICS_ANGLE_MASK,
557 (uint16_t)turn_step);
558 actor->facing_current = (uint16_t)x;
560 actor->facing_target = (uint16_t)x;
561 }
562
563 uint16_t blended_facing = actor->facing_blend + actor->facing_current;
564
565 Math_BuildMatrixRotationY(&actor->rot_mat, NULL, blended_facing & PHYSICS_ANGLE_MASK);
566
567 return !(facing_error >= (-ACTOR_FACING_TARGET_TOLERANCE) && facing_error <= ACTOR_FACING_TARGET_TOLERANCE);
568}
569
570// Original pcdogs.exe: 0x443220
571// Sets the right-movement state and clears the actor's direction bit.
573{
574 int32_t behavior_flags = actor->behavior_flags;
575
576 behavior_flags |= ACTOR_ROOT_MOTION_RIGHT;
577 actor->behavior_flags = behavior_flags;
578 actor->lifecycle_flags &= ((uint8_t)~ACTOR_LIFECYCLE_DISABLED);
579}
580
581// Original pcdogs.exe: 0x443240
582// Sets the left-movement state, clears incompatible movement flags, and sets the actor's direction bit.
584{
585 int32_t left_movement_flags = actor->behavior_flags & ((uint32_t)~(ACTOR_BEHAVIOR_TRAIL_OVERRIDE | ACTOR_BEHAVIOR_MOVEMENT_ACTIVE));
586
587 left_movement_flags |= ACTOR_ROOT_MOTION_LEFT;
588 actor->behavior_flags = left_movement_flags;
590}
591
592// Original pcdogs.exe: 0x443270
593// Normalizes direction state and polls movement signals into the command buffer. The result indicates whether any signal matched, rather than how many records were written.
594uint8_t Actor_PollMovementSignals(struct Actor_State *actor, int32_t max_events)
595{
598
599 return Signal_Poll(actor->movement_signals, max_events);
600}
601
602// Original pcdogs.exe: 0x443330
603// Advances the root-motion timeline using movement speed and direction when the applicable movement flags are set.
605{
606 int32_t movement_flags = actor->behavior_flags;
607
608 if (!(movement_flags & (ACTOR_BEHAVIOR_MOVEMENT_ACTIVE | ACTOR_ROOT_MOTION_STEP)))
609 return;
610
611 int32_t vertical_speed = (int32_t)actor->move_anim_speed;
612
613 if (movement_flags & ACTOR_ROOT_MOTION_REVERSE)
614 vertical_speed = -(vertical_speed);
615
616 Actor_AdvanceRootMotionTick((struct Scene_Type1RenderOwnerView *)(actor), vertical_speed);
617}
618
619// Original pcdogs.exe: 0x4433e0
620// Executes the first active command among three movement-command slots, updating direction, speed, and path state. Later active slots are left for subsequent calls.
622{
623 actor->behavior_flags &= ((int32_t)((uint32_t)~(ACTOR_BEHAVIOR_BYPASS_COLLISION | ACTOR_ROOT_MOTION_STEP)));
624
625 if (Actor_PollMovementSignals(actor, 3))
626 {
627 int32_t command_slot = 0;
628 struct Actor_MovementCommand *command_cursor = actor->movement_commands;
629
630 while (true)
631 {
632 if (command_cursor->active)
633 {
634 int32_t command_opcode = (int32_t)(uint8_t)command_cursor->opcode;
635
636 if (command_opcode > ACTOR_MOVE_LATCH_STOP)
637 break;
638
639 int32_t cleared_command_flags;
640
641 switch (command_opcode)
642 {
643 case ACTOR_MOVE_RESET:
644 {
646
647 int32_t stopped_movement_flags = actor->behavior_flags & ((int32_t)((uint32_t)~(ACTOR_BEHAVIOR_MOVEMENT_ACTIVE | ACTOR_ROOT_MOTION_REVERSE | ACTOR_ROOT_MOTION_LATCHED | ACTOR_ROOT_MOTION_STEP)));
648
649 stopped_movement_flags |= ACTOR_BEHAVIOR_MOVEMENT_LOCKED;
650 actor->behavior_flags = stopped_movement_flags;
651
652 return;
653 }
654
655 case ACTOR_MOVE_RIGHT:
656 {
658
659 break;
660 }
661
662 case ACTOR_MOVE_LEFT:
663 {
664 Actor_SetMovingLeft(actor);
665
666 break;
667 }
668
671 {
672 if (command_opcode == ACTOR_MOVE_STEP_FORWARD)
673 {
674 int32_t behavior_flags_4 = actor->behavior_flags;
675
676 behavior_flags_4 &= ~ACTOR_BEHAVIOR_MOVEMENT_ACTIVE;
678 }
679
680 int16_t forward_speed_percent = (int16_t)command_cursor->packed_parameters;
681
682 if (forward_speed_percent)
683 actor->move_anim_speed = (int16_t)(((uint32_t)(uint16_t)forward_speed_percent << 6) / PHYSICS_PERCENT_SCALE);
684
685 int32_t behavior_flags = actor->behavior_flags;
686
688 break;
689
690 if (!(behavior_flags & ACTOR_ROOT_MOTION_LATCHED))
691 {
692 if (actor->anim_tick)
693 actor->anim_tick = -((int32_t)actor->move_anim_speed);
694
695 cleared_command_flags = actor->behavior_flags & ((int32_t)((uint32_t)~ACTOR_ROOT_MOTION_REVERSE));
696 }
697 else
698 {
699 int32_t forward_behavior_flags = behavior_flags & ((int32_t)((uint32_t)~ACTOR_ROOT_MOTION_LATCHED));
700
701 actor->behavior_flags = forward_behavior_flags;
702 cleared_command_flags = forward_behavior_flags & ((int32_t)((uint32_t)~ACTOR_ROOT_MOTION_REVERSE));
703 }
704
705 int32_t updated_behavior_flags = cleared_command_flags;
706
707 actor->behavior_flags = cleared_command_flags;
708
709 if (updated_behavior_flags & ACTOR_ROOT_MOTION_STEP)
710 break;
711
712 updated_behavior_flags |= ACTOR_BEHAVIOR_MOVEMENT_ACTIVE;
713 actor->behavior_flags = updated_behavior_flags;
714
715 break;
716 }
717
720 {
721 if (command_opcode == ACTOR_MOVE_STEP_REVERSE)
722 {
723 int32_t behavior_flags_5 = actor->behavior_flags;
724
725 behavior_flags_5 &= ~ACTOR_BEHAVIOR_MOVEMENT_ACTIVE;
727 }
728
729 int16_t reverse_speed_percent = (int16_t)command_cursor->packed_parameters;
730
731 if (reverse_speed_percent)
732 actor->move_anim_speed = (int16_t)(((uint32_t)(uint16_t)reverse_speed_percent << 6) / PHYSICS_PERCENT_SCALE);
733
734 int32_t behavior_flags_1 = actor->behavior_flags;
735
737 break;
738
739 if (!(behavior_flags_1 & ACTOR_ROOT_MOTION_LATCHED))
740 {
741 int32_t animation_frame_count = (uint16_t)(((const struct Scene_Type1RenderOwnerView *)actor)->animation_frame_count);
742
743 if (actor->anim_tick != (animation_frame_count << 6) - PHYSICS_ANIMATION_FRAME_TICKS)
744 actor->anim_tick = -((int32_t)actor->move_anim_speed);
745 }
746 else
747 actor->behavior_flags = behavior_flags_1 & ((int32_t)((uint32_t)~ACTOR_ROOT_MOTION_LATCHED));
748
749 cleared_command_flags = actor->behavior_flags | ACTOR_ROOT_MOTION_REVERSE;
750
751 int32_t updated_behavior_flags = cleared_command_flags;
752
753 actor->behavior_flags = cleared_command_flags;
754
755 if (updated_behavior_flags & ACTOR_ROOT_MOTION_STEP)
756 break;
757
758 updated_behavior_flags |= ACTOR_BEHAVIOR_MOVEMENT_ACTIVE;
759 actor->behavior_flags = updated_behavior_flags;
760
761 break;
762 }
763
765 {
766 int16_t command_speed_percent = (int16_t)command_cursor->packed_parameters;
767
768 if (command_speed_percent)
769 actor->move_anim_speed = (int16_t)(((uint32_t)(uint16_t)command_speed_percent << 6) / PHYSICS_PERCENT_SCALE);
770
771 int32_t finished_movement_flags = actor->behavior_flags & ((int32_t)((uint32_t)~ACTOR_ROOT_MOTION_LATCHED));
772
773 finished_movement_flags |= ACTOR_BEHAVIOR_MOVEMENT_ACTIVE;
774 actor->behavior_flags = finished_movement_flags;
775
776 break;
777 }
778
779 case ACTOR_MOVE_PAUSE:
780 {
781 int32_t behavior_flags_3 = actor->behavior_flags;
782
783 behavior_flags_3 &= ~ACTOR_BEHAVIOR_MOVEMENT_ACTIVE;
784 actor->behavior_flags = behavior_flags_3;
785
786 return;
787 }
788
790 {
791 return;
792 }
793
795 {
796 int32_t behavior_flags_2 = actor->behavior_flags;
797
798 if (!(behavior_flags_2 & ACTOR_BEHAVIOR_MOVEMENT_ACTIVE))
799 break;
800
801 behavior_flags_2 &= ~ACTOR_BEHAVIOR_MOVEMENT_ACTIVE;
802 actor->behavior_flags = behavior_flags_2 | ACTOR_ROOT_MOTION_LATCHED;
803
804 break;
805 }
806 }
807
808 break;
809 }
810 else
811 {
812 command_slot += 1;
813 command_cursor = &command_cursor[1];
814
815 if (command_slot >= 3)
816 break;
817 }
818 }
819 }
820
823}
824
825// Original pcdogs.exe: 0x443680
826// Applies movement signals to a bounded variable or compares the variable against an unsigned threshold, then updates direction and movement.
828{
830
831 if (actor->progress_signal)
832 {
833 if (Actor_PollMovementSignals(actor, 1))
834 {
835 uint32_t updated_progress_bits;
836
837 if (actor->progress_comparison)
838 {
839 /* Signal_Poll advances EDX past the single matched slot. The
840 * native getter call replaces DL with the progress ID. */
841 uintptr_t end_of_signal = (uintptr_t)&actor->movement_signals[1];
842 uint32_t reference_slot = (end_of_signal & ~UINT32_C(0xff)) | actor->movement_handler_index;
843 uint32_t previous_script_progress = Script_GetVariableById(reference_slot);
844 int32_t progress_operand = actor->progress_operand;
845
846 if (progress_operand >= 0)
847 {
848 uint32_t candidate_progress_bits = progress_operand + previous_script_progress;
849
850 updated_progress_bits =
851 candidate_progress_bits >= previous_script_progress ? candidate_progress_bits : UINT32_MAX;
852 }
853 else
854 {
855 int32_t decreased_progress_bits = progress_operand + previous_script_progress;
856
857 updated_progress_bits = (uint32_t)previous_script_progress < (uint32_t)decreased_progress_bits ? 0 : (uint32_t)decreased_progress_bits;
858 }
859 }
860 else
861 updated_progress_bits = (uint32_t)actor->progress_operand;
862
863 /* Retail replaces AL with the ID before pushing the entire word. */
864 uint32_t storage_slot = (updated_progress_bits & ~UINT32_C(0xff)) | actor->movement_handler_index;
865 Script_SetVariableById(storage_slot, (int32_t)updated_progress_bits);
866 }
867
869
870 return;
871 }
872
873 uint32_t current_progress = Script_GetVariableById(actor->movement_handler_index);
874 uint32_t threshold = (uint32_t)actor->progress_operand;
875 bool reached = current_progress == threshold;
876 if (actor->progress_comparison > 0)
877 reached = current_progress > threshold;
878 else if (actor->progress_comparison < 0)
879 reached = current_progress < threshold;
880
881 if (reached && (actor->lifecycle_flags & ACTOR_LIFECYCLE_DISABLED))
883 else if (!reached && !(actor->lifecycle_flags & ACTOR_LIFECYCLE_DISABLED))
884 Actor_SetMovingLeft(actor);
885
887}
#define ACTOR_ROOT_MOTION_REVERSE
Definition constants.h:24
#define ACTOR_POSITION_FACING_EPSILON
Definition constants.h:211
#define ACTOR_BEHAVIOR_MOVEMENT_LOCKED
Definition constants.h:16
#define ACTOR_MOVE_TARGET_GROUND_HEIGHT_SLOP
Definition constants.h:265
#define ACTOR_ROOT_MOTION_STEP
Definition constants.h:26
@ ACTOR_MOVE_LEFT
Definition constants.h:58
@ ACTOR_MOVE_STEP_REVERSE
Definition constants.h:62
@ ACTOR_MOVE_REVERSE
Definition constants.h:61
@ ACTOR_MOVE_LATCH_STOP
Definition constants.h:66
@ ACTOR_MOVE_SKIP_UPDATE
Definition constants.h:65
@ ACTOR_MOVE_RIGHT
Definition constants.h:57
@ ACTOR_MOVE_RESET
Definition constants.h:56
@ ACTOR_MOVE_RESUME
Definition constants.h:63
@ ACTOR_MOVE_STEP_FORWARD
Definition constants.h:60
@ ACTOR_MOVE_FORWARD
Definition constants.h:59
@ ACTOR_MOVE_PAUSE
Definition constants.h:64
#define ACTOR_BEHAVIOR_AIRBORNE
Definition constants.h:13
#define ACTOR_ROOT_MOTION_LATCHED
Definition constants.h:25
#define ACTOR_VELOCITY_FACING_EPSILON
Definition constants.h:241
#define ACTOR_LIFECYCLE_HIDDEN
Definition constants.h:40
#define ACTOR_LIFECYCLE_DISABLED
Definition constants.h:39
#define ACTOR_BEHAVIOR_MOVING_LEFT
Definition constants.h:17
#define ACTOR_ANIMATION_FRAMES_PER_SECOND
Definition constants.h:204
@ ACTOR_FACING_ABSOLUTE
Definition constants.h:190
@ ACTOR_FACING_RELATIVE
Definition constants.h:189
#define ACTOR_ROOT_MOTION_LEFT
Definition constants.h:71
@ ACTOR_PHASE_LEAVING
Definition constants.h:152
#define ACTOR_FACING_TARGET_TOLERANCE
Definition constants.h:210
#define ACTOR_BEHAVIOR_TRAIL_OVERRIDE
Definition constants.h:10
#define ACTOR_BEHAVIOR_BYPASS_COLLISION
Definition constants.h:18
#define ACTOR_ROOT_MOTION_RIGHT
Definition constants.h:72
#define ACTOR_BEHAVIOR_MOVEMENT_ACTIVE
Definition constants.h:19
#define ACTOR_BEHAVIOR_PREPROCESSED
Definition constants.h:20
#define ACTOR_BEHAVIOR_COLLISION_CORRECTION
Definition constants.h:33
void Actor_UpdateWorldRenderPosition(struct Actor_State *actor)
Definition rendering.c:686
int32_t Actor_TracePath(struct Entity *entity, int32_t target_selector, int32_t max_path_distance, int32_t best_path_distance, int32_t trace_flags)
void Actor_StartTransition(struct Actor_State *actor, struct Actor_MovementRecordView *record)
Definition lifecycle.c:221
void Actor_ApplyEntitySlotFlags(struct Actor_State *actor)
Definition lifecycle.c:166
void Actor_ReleaseAttachment(struct Actor_MovementRecordView *record)
Definition attachments.c:34
void Actor_AdvanceRootMotionTick(struct Scene_Type1RenderOwnerView *owner, int32_t tick_delta)
Definition animation.c:596
void Actor_UpdateGroundOrientation(struct Actor_State *actor)
Definition collision.c:366
void Actor_ResetAnimationChains(struct Actor_State *actor)
Definition animation.c:714
void Actor_SetMovingRight(struct Actor_State *actor)
Definition movement.c:572
void Actor_MoveToTarget(struct Actor_State *actor, struct Math_Vec3I32 const *target_pos, int16_t facing_selector, int32_t facing_angle_degrees_q12, int32_t duration_frames)
Definition movement.c:142
void Actor_ApplyVerticalMovement(struct Actor_State *actor)
Definition movement.c:604
bool Actor_SetFacingTarget(struct Entity *entity, int32_t target_selector, int32_t angle_degrees_q12)
Definition movement.c:34
uint8_t Actor_PollMovementSignals(struct Actor_State *actor, int32_t max_events)
Definition movement.c:594
void Actor_ProcessMovementBehavior(struct Actor_State *actor)
Definition movement.c:827
BOOL Actor_UpdateRotationFromVelocity(struct Actor_State *actor, struct Math_Vec3I32 const *velocity, int32_t turn_step)
Definition movement.c:532
void Actor_ProcessSnapAndEntityUpdate(struct Actor_State *actor)
Definition movement.c:349
void Actor_SetMovingLeft(struct Actor_State *actor)
Definition movement.c:583
void Actor_ProcessMovementCommands(struct Actor_State *actor)
Definition movement.c:621
void Actor_ResetVelocityAndMoveToTarget(struct Entity *entity, int16_t facing_selector, int32_t facing_angle_degrees_q12, int32_t duration_q12)
Definition movement.c:124
void Actor_ApplySplineMovement(struct Scene_Type1RenderOwnerView *owner, struct Animation_SplineChannelVec3 *spline_track, int32_t tick_delta)
Definition movement.c:388
void Actor_SnapToPosition(struct Actor_State *actor)
Definition movement.c:232
#define ANIMATION_SPLINE_REPEAT_ENDPOINT
Definition constants.h:31
#define ANIMATION_SPLINE_TIME_Q6_MASK
Definition constants.h:37
#define ANIMATION_SPLINE_WRAP_SAMPLE_TIME
Definition enums.h:55
void Animation_InterpolateVec3(uint32_t frame_time, struct Animation_SplineChannelVec3 *vec3_track, struct Math_Vec3I32 *out_vec3)
@ COLLISION_NODE_ORIENTED_BOX
Definition constants.h:8
void Collision_UpdateActorGroundBarycentric(struct Actor_State *actor)
Definition ground.c:107
void Collision_ProjectPointOntoPolygonSurface(struct Collision_Node const *collision_node, struct Collision_Polygon const *contact_poly, struct Math_Vec3I32 const *world_pos, struct Math_Vec3I32 *out_world_pos)
Definition ground.c:233
void Collision_ResolveActorGroundContact(struct Actor_State *actor)
Definition ground.c:621
#define ENTITY_ANGLE_SCALE_Q12
Definition constants.h:32
#define ENTITY_FLAG_PAUSE_EXEMPT
Definition constants.h:26
#define ENTITY_INDEX_RECIPROCAL
Definition constants.h:33
#define ENTITY_FLAG_SCRIPT_PAUSED
Definition constants.h:20
#define GAME_TRANSITION_SCRIPT_PAUSED
Definition constants.h:20
#define Game_FrameTransitionFlags
Definition globals.h:47
void Math_BuildMatrixRotationY(struct Math_Matrix3x3I16 *out_matrix, struct Math_Matrix3x3I16 const *left_factor, int32_t angle)
Definition matrices.c:76
int32_t Math_SqrtFP12(uint32_t value)
uint16_t Math_ClampAngleDelta(int16_t target_angle, int16_t current_angle, int16_t max_delta)
int16_t Math_CalculateAtan2FP12(int32_t y, int32_t x)
#define pkg_resource_current_level_data
Definition globals.h:117
#define PHYSICS_ANGLE_MASK
Definition constants.h:8
#define PHYSICS_MOVEMENT_DURATION_SCALE_Q24
Definition constants.h:35
#define PHYSICS_PERCENT_SCALE
Definition constants.h:13
#define PHYSICS_Q12_PRODUCT_HIGH_SHIFT
Definition constants.h:30
#define PHYSICS_FIXED_ONE
Definition constants.h:7
#define PHYSICS_ANIMATION_FRAME_TICKS
Definition constants.h:12
#define PHYSICS_FIXED_FRACTION_BITS
Definition constants.h:6
#define SCENE_NODE_DISABLED
Definition constants.h:24
void Script_SetVariableById(uint32_t progress_var_slot, int32_t progress_value)
Definition interpreter.c:69
uint8_t Signal_Poll(struct Signal_QueueEntry *event_buffer, int32_t max_events)
Definition signals.c:37
int32_t Script_GetVariableById(uint32_t progress_var_slot)
Definition interpreter.c:59
int32_t packed_parameters
Definition types.h:108
uint16_t active
Definition types.h:96
int32_t facing_angle_degrees_q12
Definition types.h:323
uint16_t move_frames_remaining
Definition types.h:321
struct Math_Vec3I32 saved_velocity
Definition types.h:320
struct Component_CollisionRecord * collision_records
Definition types.h:325
uint8_t movement_counter_e
Definition types.h:313
uint8_t movement_counter_d
Definition types.h:312
struct Math_Vec3I32 move_target
Definition types.h:319
int16_t anim_seq_timer
Definition types.h:389
struct Math_Vec3I32 velocity
Definition types.h:446
int16_t move_anim_speed
Definition types.h:428
struct Scene_Node ** render_node_list
Definition types.h:440
struct Signal_QueueEntry movement_signals[3]
Definition types.h:461
int16_t facing_current
Definition types.h:447
int32_t progress_comparison
Definition types.h:468
uint8_t transition_phase
Definition types.h:434
uint8_t lifecycle_flags
Definition types.h:392
struct Actor_MovementCommand movement_commands[3]
Definition types.h:462
uint8_t movement_handler_index
Definition types.h:433
int32_t progress_operand
Definition types.h:470
struct Actor_State * child_actor
Definition types.h:478
struct Collision_Polygon * ground_contact_polygon
Definition types.h:452
struct Collision_Node * ground_collision_node
Definition types.h:451
uint8_t behavior_flags_high
Definition types.h:412
int32_t behavior_flags
Definition types.h:407
int32_t anim_tick
Definition types.h:427
struct Actor_State * parent_actor
Definition types.h:488
int16_t facing_target
Definition types.h:448
void * record_ptr
Definition types.h:477
int16_t facing_blend
Definition types.h:449
struct Math_Matrix3x3I16 rot_mat
Definition types.h:383
struct Math_Vec3I32 position
Definition types.h:385
uint32_t progress_signal
Definition types.h:466
Definition types.h:118
int16_t facing_current
Definition types.h:200
struct Math_Vec3I32 velocity
Definition types.h:199
int16_t facing_blend
Definition types.h:202
int16_t facing_target
Definition types.h:201
int32_t packed_time_bias_and_end_time
Definition types.h:381
Animation_SplineChannelFlags channel_flags
Definition types.h:385
uint8_t node_type
Definition types.h:286
struct Actor_State * position_source_actor
Definition types.h:46
Definition types.h:22
uint32_t flags
Definition types.h:25
int16_t attach_offset_x
Definition types.h:42
struct Math_Vec3I32 path_target
Definition types.h:113
struct Math_Vec3I32 * position_ptr
Definition types.h:78
int32_t param_F0
Definition types.h:90
struct Actor * runtime_actor
Definition types.h:107
struct Math_Vec3I32 path_result
Definition types.h:114
struct Entity * entity_array
Definition types.h:45
int32_t y
Definition types.h:471
int32_t x
Definition types.h:470
int32_t z
Definition types.h:472
uint8_t traversal_flags
Definition types.h:130
struct Math_Vec3I32 local_position
Definition types.h:259
struct Scene_TransformNodePrefix transform
Definition types.h:324