102 Dalmatians: Puppies to the Rescue
Decompilation Source Reference | GitLab repository
Loading...
Searching...
No Matches
path_tracing.c
Go to the documentation of this file.
1/* Recovered game implementation; original addresses identify provenance. */
14#include "pcdogs/actor/types.h"
18#include "pcdogs/entity/types.h"
19#include "pcdogs/game/types.h"
22#include "pcdogs/level/types.h"
24#include "pcdogs/math/types.h"
28#include "pcdogs/scene/types.h"
29#include <math.h>
30#include <stdarg.h>
31#include <string.h>
32
33/* The two Q12 products are shifted individually before the native 32-bit add. */
34static int32_t project_horizontal(int32_t angle, int32_t dx, int32_t dz)
35{
36 int32_t sin_q12 = Math_SinQ14(angle) >> 2;
37 int32_t cos_q12 = Math_SinQ14(PHYSICS_ANGLE_QUARTER_TURN - angle) >> 2;
38 int32_t x = (int32_t)(((int64_t)sin_q12 * dx) >> PHYSICS_FIXED_FRACTION_BITS);
39 int32_t z = (int32_t)(((int64_t)cos_q12 * dz) >> PHYSICS_FIXED_FRACTION_BITS);
40 return x + z;
41}
42
43static bool target_in_view_cone(int32_t facing, int32_t half_angle, int32_t dx, int32_t dz, int32_t radius)
44{
45 if ((int16_t)half_angle >= PHYSICS_ANGLE_HALF_TURN)
46 return true;
47
48 int32_t forward = project_horizontal(facing, dx, dz);
49 if ((int16_t)half_angle <= PHYSICS_ANGLE_QUARTER_TURN)
50 {
51 return forward >= -radius &&
52 project_horizontal(facing + half_angle - PHYSICS_ANGLE_QUARTER_TURN, dx, dz) >= -radius &&
53 project_horizontal(facing - half_angle + PHYSICS_ANGLE_QUARTER_TURN, dx, dz) >= -radius;
54 }
55
56 return forward >= -radius ||
57 project_horizontal(facing + half_angle + PHYSICS_ANGLE_QUARTER_TURN, dx, dz) <= radius ||
58 project_horizontal(facing - half_angle - PHYSICS_ANGLE_QUARTER_TURN, dx, dz) <= radius;
59}
60
61// Resolves entity or navigation targets recursively and applies distance, cone, and collision gates. Path state can be written before a final distance rejection, so a zero result does not imply no side effects.
62int32_t Actor_TracePathWithSteeringEntity(struct Entity *source_entity, int32_t target_selector, int32_t max_path_distance,
63 int32_t best_path_distance, int32_t trace_flags, struct Entity *steering_entity)
64{
65 if (!source_entity) // NULL sourceRoom = use player camera position as origin
66 {
67 source_entity = (struct Entity *)(&g_pathTraceWorkBuffer);
70 (*(uint8_t *)&g_pathTraceState2) = 0;
71 }
72
73 struct Math_Vec3I32 *position_ptr = source_entity->position_ptr;
74 struct Actor *runtime_actor = source_entity->runtime_actor;
75 int32_t resolved_target_selector = target_selector;
76 struct Math_Vec3I32 end_pos; // Target types -20000 to -0x4D22 reserved for special object searches
77 uint32_t target_distance;
78 int32_t target_delta_x;
79 int32_t dy;
80 int32_t target_delta_z;
81 bool target_visible = false;
82 bool apply_target_steering = false;
83
84 if (resolved_target_selector < ACTOR_TARGET_COMMAND_FIRST || resolved_target_selector >= ACTOR_TARGET_COMMAND_END)
85 {
86 int32_t *active_entity_words = (int32_t *)(EntityNavigationWorkList_ActiveBufferPtr);
87 struct Level_Data *target_level = pkg_resource_current_level_data;
88 struct Math_Vec3I32 *position = NULL;
89 int32_t target_radius;
90 struct Math_Vec3I32 *resolved_target_position;
91 bool resolve_target_position = false;
92
93 if (resolved_target_selector > ACTOR_TARGET_ALL_ENTITIES)
94 {
95 if (resolved_target_selector)
96 resolve_target_position = true;
97 else
98 {
99 int32_t attached_target_selector = (int32_t)source_entity->attach_offset_x;
100
101 target_selector = attached_target_selector;
102
103 if (attached_target_selector)
104 {
106 position = &source_entity->path_result;
107
108 resolved_target_selector = target_selector;
109 resolve_target_position = true;
110 }
111 }
112 }
113 else if (resolved_target_selector == ACTOR_TARGET_ALL_ENTITIES ||
114 resolved_target_selector == ACTOR_TARGET_ENTITY_SCAN)
115 {
116 if (resolved_target_selector == ACTOR_TARGET_ALL_ENTITIES)
117 {
118 int32_t all_entities_trace_flags = trace_flags;
119
120 all_entities_trace_flags |= ACTOR_TRACE_INCLUDE_PERSISTENT;
121 trace_flags = all_entities_trace_flags;
122 }
123
124 int32_t all_entities_target_update_mask = trace_flags & (ACTOR_TRACE_USE_TARGET_POSITION | ACTOR_TRACE_USE_TARGET_RESULT);
125
126 if (!all_entities_target_update_mask)
127 {
128 end_pos.x = source_entity->path_target.x;
129 end_pos.y = source_entity->path_target.y;
130 end_pos.z = source_entity->path_target.z;
131 }
132
133 int32_t saved_all_entities_distance = source_entity->path_best_distance;
134
135 source_entity->path_best_distance = INT32_MAX;
136
137 struct Level_Data *candidate_level = pkg_resource_current_level_data;
138
139 target_selector = 0;
140
141 // Iterate through all actors in level looking for valid targets
142 struct Entity *entity_array = candidate_level->entity_array;
143 int32_t candidate_entity_selector = 1;
144
145 steering_entity = entity_array;
146
147 if (candidate_level->entity_count >= 1)
148 {
149 int32_t candidate_entity_count;
150
151 do
152 {
153 if (entity_array != source_entity)
154 {
155 uint32_t flags = entity_array->flags;
156
158 {
159 struct Actor *candidate_actor;
160
161 if (!(flags & ENTITY_FLAG_ACTIVATION_REQUEST) ||
163 (candidate_actor = entity_array->runtime_actor) && candidate_actor->transition_phase != ACTOR_PHASE_LEAVING))
164 {
165 int32_t candidate_trace_flags = trace_flags;
166
167 candidate_trace_flags |= ACTOR_TRACE_KEEP_NEAREST;
168
169 // Recursive call to trace path to each candidate actor
170 int32_t candidate_trace_result = Actor_TracePath(source_entity,
171 candidate_entity_selector, INT32_MAX, best_path_distance,
172 candidate_trace_flags);
173
174 if (candidate_trace_result)
175 {
176 target_selector = candidate_trace_result;
177 best_path_distance = source_entity->path_best_distance;
178 }
179 }
180 }
181 }
182
183 candidate_entity_selector += 1;
184 entity_array = &entity_array[1];
185 candidate_entity_count = (uint16_t)pkg_resource_current_level_data->entity_count;
186 } while (candidate_entity_selector <= candidate_entity_count);
187
188 steering_entity = entity_array;
189 }
190
191 source_entity->path_best_distance = saved_all_entities_distance;
192
193 if (target_selector)
194 {
195 target_delta_x = source_entity->path_target.x - position_ptr->x;
196 dy = source_entity->path_target.y - position_ptr->y;
197 target_delta_z = source_entity->path_target.z - position_ptr->z;
198
199 if (!all_entities_target_update_mask)
200 source_entity->path_target = end_pos;
201
202 target_distance = (uint32_t)best_path_distance;
203
204 target_visible = true;
205 apply_target_steering = true;
206 }
207 }
208 else
209 {
210 if ((uint32_t)resolved_target_selector + 0x8000u > 9u)
211 resolve_target_position = true;
212 else
213 {
214 switch (resolved_target_selector)
215 {
217 {
218 void *sole_active_actor;
219 int32_t sole_active_entity_address;
220
221 if (*(uint32_t *)active_entity_words == 1)
222 {
223 sole_active_entity_address = active_entity_words[1];
224 steering_entity = (struct Entity *)(uintptr_t)sole_active_entity_address;
225 sole_active_actor = (void *)((uint32_t)(uintptr_t)((const struct Entity *)(uintptr_t)sole_active_entity_address)->runtime_actor);
226 }
227
228 if (EntityNavigationWorkList_ActiveBufferPtr->active_count != 1 || !sole_active_actor)
229 {
230 int32_t active_list_target_update_mask = trace_flags & (ACTOR_TRACE_USE_TARGET_POSITION | ACTOR_TRACE_USE_TARGET_RESULT);
231
232 if (!active_list_target_update_mask)
233 {
234 end_pos.x = source_entity->path_target.x;
235 end_pos.y = source_entity->path_target.y;
236 end_pos.z = source_entity->path_target.z;
237 }
238
239 int32_t saved_active_list_distance = source_entity->path_best_distance;
240
241 source_entity->path_best_distance = INT32_MAX;
242
243 int32_t *navigation_work_list = (int32_t *)(EntityNavigationWorkList_ActiveBufferPtr);
244 int32_t i = 0;
245
246 target_selector = 0;
247
248 if (*(uint32_t *)navigation_work_list > 0)
249 {
250 int32_t best_active_list_distance = best_path_distance;
251 uintptr_t active_list_byte_offset = 4;
252
253 do
254 {
255 int32_t active_candidate_address =
256 (uint32_t)(uintptr_t)((const struct EntityNavigationWorkListBuffer *)navigation_work_list)->active_entities[(active_list_byte_offset - offsetof(struct EntityNavigationWorkListBuffer, active_entities)) / sizeof(struct Entity *)];
257
258 steering_entity = (struct Entity *)(uintptr_t)active_candidate_address;
259
260 if ((uintptr_t)active_candidate_address != (uintptr_t)source_entity)
261 {
262 void *active_candidate_actor = (void *)((uint32_t)(uintptr_t)((const struct Entity *)(uintptr_t)active_candidate_address)->runtime_actor);
263
264 if (active_candidate_actor
265 // Reject candidates with both entity state bits 0x6000 set, and exclude actor transition phase three separately.
266 && (*(uint32_t *)active_candidate_address & (ENTITY_FLAG_SCRIPT_PAUSED | ENTITY_FLAG_COLLISION_LIST_UNLINKED)) != (ENTITY_FLAG_SCRIPT_PAUSED | ENTITY_FLAG_COLLISION_LIST_UNLINKED) && ((const struct Actor_State *)active_candidate_actor)->transition_phase != ACTOR_PHASE_LEAVING)
267 {
268 int32_t active_candidate_trace_flags = trace_flags;
269
270 active_candidate_trace_flags |= ACTOR_TRACE_KEEP_NEAREST;
271
272 int32_t active_index_product_high;
273
274 active_index_product_high = (int32_t)((uint64_t)((int64_t)(int32_t)(ENTITY_INDEX_RECIPROCAL) * (int32_t)((active_candidate_address - (intptr_t)pkg_resource_current_level_data->entity_array))) >> 32);
275
276 int32_t active_index_quotient = active_index_product_high >> 6;
277 int32_t active_candidate_trace_result = Actor_TracePath(source_entity,
278 active_index_quotient + ((uint32_t)active_index_quotient >> 0x1f) + 1, INT32_MAX,
279 best_active_list_distance, active_candidate_trace_flags);
280
281 if (active_candidate_trace_result)
282 {
283 best_active_list_distance = source_entity->path_best_distance;
284 target_selector = active_candidate_trace_result;
285 }
286 }
287 }
288
289 navigation_work_list = (int32_t *)(EntityNavigationWorkList_ActiveBufferPtr);
290 i += 1;
291 active_list_byte_offset += 4;
292 } while ((uint32_t)i < *(uint32_t *)navigation_work_list);
293
294 best_path_distance = best_active_list_distance;
295 }
296
297 source_entity->path_best_distance = saved_active_list_distance;
298
299 if (target_selector)
300 {
301 target_delta_x = source_entity->path_target.x - position_ptr->x;
302 dy = source_entity->path_target.y - position_ptr->y;
303 target_delta_z = source_entity->path_target.z - position_ptr->z;
304
305 if (!active_list_target_update_mask)
306 source_entity->path_target = end_pos;
307
308 target_distance = (uint32_t)best_path_distance;
309
310 target_visible = true;
311 apply_target_steering = true;
312 }
313 }
314 else if ((uintptr_t)sole_active_entity_address != (uintptr_t)source_entity
315 // Either entity flag alone passes this combined-mask test.
316 && (*(uint32_t *)sole_active_entity_address & (ENTITY_FLAG_SCRIPT_PAUSED | ENTITY_FLAG_COLLISION_LIST_UNLINKED)) != (ENTITY_FLAG_SCRIPT_PAUSED | ENTITY_FLAG_COLLISION_LIST_UNLINKED) && ((const struct Actor_State *)sole_active_actor)->transition_phase != ACTOR_PHASE_LEAVING)
317 {
318 int32_t sole_index_product_high;
319
320 sole_index_product_high = (int32_t)((uint64_t)((int64_t)(int32_t)(ENTITY_INDEX_RECIPROCAL) * (int32_t)((sole_active_entity_address - (intptr_t)target_level->entity_array))) >> 32);
321
322 int32_t sole_index_quotient = sole_index_product_high >> 6;
323
324 resolved_target_selector = sole_index_quotient + ((uint32_t)sole_index_quotient >> 0x1f) + 1;
325 target_selector = resolved_target_selector;
326
327 resolve_target_position = true;
328 }
329 break;
330 }
331
335 {
336 if (source_entity->bonus_respawn_mode)
337 {
338 int32_t active_respawn_index = (uint8_t)(source_entity->active_flag);
339
340 target_selector = ((int32_t)UINT32_MAX) - active_respawn_index;
341
342 resolved_target_selector = target_selector;
343 resolve_target_position = true;
344 }
345
346 break;
347 }
348
350 {
351 int32_t respawn_target_update_mask = trace_flags & (ACTOR_TRACE_USE_TARGET_POSITION | ACTOR_TRACE_USE_TARGET_RESULT);
352
353 if (!respawn_target_update_mask)
354 {
355 end_pos.x = source_entity->path_target.x;
356 end_pos.y = source_entity->path_target.y;
357 end_pos.z = source_entity->path_target.z;
358 }
359
360 int32_t saved_respawn_distance = source_entity->path_best_distance;
361
362 source_entity->path_best_distance = INT32_MAX;
363 target_selector = 0;
364
365 int32_t respawn_candidate_ordinal = 1;
366
367 if (source_entity->bonus_respawn_mode >= 1)
368 {
369 int32_t best_respawn_distance = best_path_distance;
370 int32_t respawn_trace_flags = trace_flags;
371
372 respawn_trace_flags |= ACTOR_TRACE_KEEP_NEAREST;
373
374 int32_t respawn_target_selector = ((int32_t)UINT32_MAX);
375
376 bool more_respawn_targets;
377
378 do
379 {
380 int32_t respawn_trace_result = Actor_TracePath(source_entity,
381 respawn_target_selector, INT32_MAX, best_respawn_distance, respawn_trace_flags);
382
383 if (respawn_trace_result)
384 {
385 best_respawn_distance = source_entity->path_best_distance;
386 target_selector = respawn_trace_result;
387 }
388
389 int32_t respawn_target_count = (uint8_t)(source_entity->bonus_respawn_mode);
390
391 respawn_target_selector -= 1;
392 more_respawn_targets = respawn_candidate_ordinal + 1 <= respawn_target_count;
393 respawn_candidate_ordinal += 1;
394 } while (more_respawn_targets);
395
396 best_path_distance = best_respawn_distance;
397 }
398
399 source_entity->path_best_distance = saved_respawn_distance;
400
401 if (target_selector)
402 {
403 target_delta_x = source_entity->path_target.x - position_ptr->x;
404 dy = source_entity->path_target.y - position_ptr->y;
405 target_delta_z = source_entity->path_target.z - position_ptr->z;
406
407 if (!respawn_target_update_mask)
408 source_entity->path_target = end_pos;
409
410 target_distance = (uint32_t)best_path_distance;
411
412 target_visible = true;
413 apply_target_steering = true;
414 }
415
416 break;
417 }
418
421 {
422 resolve_target_position = true;
423
424 break;
425 }
426
428 {
429 if (source_entity->bonus_respawn_mode)
430 {
431 target_selector = ((int32_t)UINT32_MAX);
432 resolved_target_selector = ((int32_t)UINT32_MAX);
433 resolve_target_position = true;
434 }
435
436 break;
437 }
438
440 {
441 int32_t last_respawn_ordinal = (uint8_t)(source_entity->bonus_respawn_mode);
442
443 target_selector = -(last_respawn_ordinal);
444 resolved_target_selector = target_selector;
445 resolve_target_position = true;
446
447 break;
448 }
449 }
450 }
451 }
452
453 if (resolve_target_position)
454 {
455 if (resolved_target_selector == ACTOR_TARGET_NAVIGATION_POSITION)
456 {
457 target_radius = 0;
458 resolved_target_position = &source_entity->navigation_command_position;
459 }
460 else
461 {
462 if (trace_flags & ACTOR_TRACE_NAVIGATION_RECORDS)
463 {
464 struct Nav_Command *command = &EntityNavigationWorkList_ActiveBufferPtr->nav_commands[resolved_target_selector - 1];
465 resolved_target_position = &command->pos;
466 target_radius = command->speed;
467 }
468 else if (resolved_target_selector == ACTOR_TARGET_BONUS_RESPAWN_POSITION)
469 {
470 target_radius = 0;
471 resolved_target_position = &source_entity->bonus_respawn_pos;
472 }
473 else if (resolved_target_selector < 0)
474 {
475 target_radius = 0;
476
477 if (resolved_target_selector >= ((int32_t)0xffffff9b))
478 {
479 resolved_target_position = &source_entity->bonus_respawn_target_pos[-resolved_target_selector - 1];
480 }
481 else
482 {
483 resolved_target_position = NULL;
484
485 for (struct Scene_Node *target_scene_node = g_objectNodeRoot->child_node_list_ptr;
486 target_scene_node; target_scene_node = target_scene_node->next_in_resource_list)
487 {
488 if ((uint8_t)target_scene_node->type_specific_secondary_render_or_sound_index == (uint8_t)(resolved_target_selector - 0x68))
489 {
490 resolved_target_position = &target_scene_node->world_pos;
491
492 break;
493 }
494 }
495 }
496 }
497 else if (resolved_target_selector > 0)
498 {
499 steering_entity = &target_level->entity_array[resolved_target_selector - 1];
500 uint32_t selected_entity_flags = steering_entity->flags;
501
502 if (!((selected_entity_flags ^ trace_flags) & ACTOR_TRACE_MATCH_ACTIVATION) || (steering_entity->runtime_actor && selected_entity_flags & ENTITY_FLAG_RESTORE_DEFAULTS))
503 {
504 target_radius = 0;
505 resolved_target_position = NULL;
506 }
507 else
508 {
509 resolved_target_position = steering_entity->position_ptr;
510 target_radius = ACTOR_TARGET_DEFAULT_RADIUS;
511 }
512 }
513 else
514 return 0;
515 }
516
517 if (!position)
518 position = resolved_target_position;
519
520 if (position)
521 {
522 target_delta_x = position->x - position_ptr->x;
523 target_delta_z = position->z - position_ptr->z;
524
525 int32_t direction_mode_height_offset;
526
527 if (source_entity->param_direction_mode < ENTITY_DIRECTION_SPEED_HEIGHT_OFFSET_FIRST || !runtime_actor)
528 direction_mode_height_offset = 0;
529 else
530 direction_mode_height_offset =
532
533 dy = position->y - position_ptr->y + direction_mode_height_offset;
534
535 target_distance = (uint32_t)Math_ApproxDistance3DWeighted(target_delta_x, dy, target_delta_z);
536
537 /* Native 0x405392 uses unsigned JA after a wrapping ADD. */
538 if (target_distance <= (uint32_t)target_radius + (uint32_t)best_path_distance)
539 {
540 int32_t candidate_trace_flags = trace_flags;
541
542 if (!(candidate_trace_flags & ACTOR_TRACE_KEEP_NEAREST) || target_distance < (uint32_t)source_entity->path_best_distance)
543 {
544 if (!((uint8_t)candidate_trace_flags & ACTOR_TRACE_VIEW_CONE))
545 {
546 target_visible = true;
547 }
548 else
549 {
550 if ((void *)source_entity == (void *)&g_pathTraceWorkBuffer)
551 {
552 int32_t passes_frustum =
553 Graphics_CheckFrustumCull(position, target_radius >> 6, 0);
554
555 if (!passes_frustum)
556 return passes_frustum;
557
558 target_visible = true;
559 }
560 else
561 {
562 int32_t source_facing_angle;
563
564 if (!runtime_actor)
565 {
566 int64_t source_facing_product_wide = (int64_t)(int32_t)(ENTITY_ANGLE_SCALE_Q12) * (int32_t)(source_entity->param_F0);
567
568 source_facing_angle = ((int32_t)((uint64_t)source_facing_product_wide >> PHYSICS_FIXED_FRACTION_BITS)) >> PHYSICS_FIXED_FRACTION_BITS;
569 }
570 else
571 {
572 source_facing_angle = (runtime_actor->facing_blend + runtime_actor->facing_current) & PHYSICS_ANGLE_MASK;
573 }
574
575 int32_t angle = source_facing_angle + (source_entity->target_offset_phase >> PHYSICS_FIXED_FRACTION_BITS);
576 int32_t view_half_angle = source_entity->param_108 >> 0xd;
577 if (target_in_view_cone(angle, view_half_angle, target_delta_x, target_delta_z, target_radius))
578 {
579 if ((uint8_t)trace_flags & ACTOR_TRACE_SKIP_ACTOR_OCCLUSION || !runtime_actor)
580 {
581 target_visible = true;
582 apply_target_steering = true;
583 }
584 else
585 {
586 int32_t y = runtime_actor->world_render_pos.y;
587
588 end_pos.x = target_delta_x + runtime_actor->world_render_pos.x;
589
590 int32_t collision_end_z = runtime_actor->world_render_pos.z + target_delta_z;
591
592 end_pos.y = y + dy;
593
594 int32_t x = runtime_actor->world_render_pos.x;
595
596 end_pos.z = collision_end_z;
597
598 int32_t collision_start_z = runtime_actor->world_render_pos.z;
599 struct Math_Vec3I32 start_pos;
600
601 start_pos.x = x;
602 start_pos.y = runtime_actor->world_render_pos.y + ((int32_t)runtime_actor->collision_height_hi << 6 >> 1);
603 start_pos.z = collision_start_z;
604
606 &end_pos, NULL, ACTOR_PATH_OCCLUSION_PROBE_RADIUS, ((int32_t)UINT32_MAX)))
607 {
608 target_visible = true;
609 apply_target_steering = true;
610 }
611 }
612 }
613 }
614 }
615 }
616 }
617 }
618 }
619 }
620 else if (EntityNavigationWorkList_ActiveBufferPtr->nav_command_count)
621 {
622 int32_t navigation_target_filter = trace_flags;
623 int32_t attach_offset_x;
624
625 if (!trace_flags)
626 {
627 attach_offset_x = (int32_t)source_entity->attach_offset_x;
628 navigation_target_filter = attach_offset_x;
629 }
630
631 if (trace_flags || attach_offset_x > 0)
632 {
633 trace_flags = 0;
634
635 int32_t source_index_product_high;
636
637 source_index_product_high = (int32_t)((uint64_t)((int64_t)(int32_t)(ENTITY_INDEX_RECIPROCAL) * (int32_t)(((intptr_t)source_entity - (intptr_t)pkg_resource_current_level_data->entity_array))) >> 32);
638
639 int32_t source_index_quotient = source_index_product_high >> 6;
640 uint8_t navigation_command_type = (uint8_t)(resolved_target_selector - ACTOR_TARGET_COMMAND_FIRST);
641
642 int32_t source_selector = source_index_quotient + ((uint32_t)source_index_quotient >> 0x1f) + 1;
643
644 if (!best_path_distance)
645 best_path_distance = NAVIGATION_COMMAND_UNLIMITED_SPEED;
646
647 target_selector = 0;
648
649 int32_t navigation_command_index = 0;
650
651 end_pos.x = source_entity->path_target.x;
652 int32_t saved_path_distance = source_entity->path_best_distance;
653 end_pos.y = source_entity->path_target.y;
654 source_entity->path_best_distance = INT32_MAX;
655
656 struct EntityNavigationWorkListBuffer *navigation_work_list_local =
658
659 end_pos.z = source_entity->path_target.z;
660
661 if (navigation_work_list_local->nav_command_count > 0)
662 {
663 while (true)
664 {
665 if (navigation_work_list_local->nav_commands[navigation_command_index].command_type == navigation_command_type)
666 {
667 int32_t command_target_selector_a = (int32_t)navigation_work_list_local->nav_commands[navigation_command_index].target_selector_a;
668
669 if (command_target_selector_a != source_selector)
670 {
671 int32_t command_target_selector_b = (int32_t)navigation_work_list_local->nav_commands[navigation_command_index].target_selector_b;
672
673 if (command_target_selector_b == source_selector ||
674 command_target_selector_b == NAVIGATION_COMMAND_DEFAULT_SELECTOR ||
675 (command_target_selector_b == ACTOR_TRACE_FILTER_ACTIVE_ENTITIES && Entity_IsInActiveList(source_selector)) ||
676 (command_target_selector_b == ACTOR_TRACE_FILTER_INACTIVE_ENTITIES && !Entity_IsInActiveList(source_selector)))
677 {
678 if (command_target_selector_a == navigation_target_filter ||
679 navigation_target_filter == NAVIGATION_COMMAND_DEFAULT_SELECTOR ||
680 (navigation_target_filter == ACTOR_TRACE_FILTER_INACTIVE_ENTITIES &&
681 (command_target_selector_a == NAVIGATION_COMMAND_DEFAULT_SELECTOR || !Entity_IsInActiveList(command_target_selector_a))) ||
682 (navigation_target_filter == ACTOR_TRACE_FILTER_ACTIVE_ENTITIES &&
683 command_target_selector_a != NAVIGATION_COMMAND_DEFAULT_SELECTOR && Entity_IsInActiveList(command_target_selector_a)))
684 {
685 if (Actor_TracePath(source_entity, navigation_command_index + 1, INT32_MAX,
687 {
688 best_path_distance = source_entity->path_best_distance;
689 target_selector = (int32_t)EntityNavigationWorkList_ActiveBufferPtr->nav_commands[navigation_command_index].target_selector_a;
690 }
691 }
692 }
693 }
694 }
695
696 navigation_work_list_local = EntityNavigationWorkList_ActiveBufferPtr;
697
698 int32_t next_navigation_command_index = navigation_command_index + 1;
699
700 navigation_command_index = next_navigation_command_index;
701
702 if (next_navigation_command_index >= navigation_work_list_local->nav_command_count)
703 break;
704 }
705 }
706
707 source_entity->path_best_distance = saved_path_distance;
708
709 if (target_selector)
710 {
711 target_delta_x = source_entity->path_target.x - position_ptr->x;
712 target_delta_z = source_entity->path_target.z - position_ptr->z;
713 dy = source_entity->path_target.y - position_ptr->y;
714
715 int32_t saved_path_target_y = end_pos.y;
716
717 source_entity->path_target.x = end_pos.x;
718
719 int32_t saved_path_target_z = end_pos.z;
720
721 source_entity->path_target.y = saved_path_target_y;
722 source_entity->path_target.z = saved_path_target_z;
723 target_distance = (uint32_t)best_path_distance;
724 source_entity->navigation_command_position.x = position_ptr->x + target_delta_x;
725 source_entity->navigation_command_position.y = position_ptr->y + dy;
726 source_entity->navigation_command_position.z = target_delta_z + position_ptr->z;
727
728 target_visible = true;
729 apply_target_steering = true;
730 }
731 }
732 }
733
734 if (!target_visible)
735 return 0;
736
737 if (apply_target_steering)
738 {
739 int32_t steering_trace_flags = trace_flags & (ACTOR_TRACE_VIEW_CONE | ACTOR_TRACE_KEEP_NEAREST);
740
741 if (steering_trace_flags == ACTOR_TRACE_VIEW_CONE)
742 {
743 int32_t steering_facing_angle;
744
745 if (!runtime_actor)
746 {
747 int64_t steering_facing_product_wide = (int64_t)(int32_t)(ENTITY_ANGLE_SCALE_Q12) * (int32_t)((uint32_t)steering_entity->param_F0);
748
749 steering_facing_angle = ((int32_t)((uint64_t)steering_facing_product_wide >> PHYSICS_FIXED_FRACTION_BITS)) >> PHYSICS_FIXED_FRACTION_BITS;
750 }
751 else
752 {
753 steering_facing_angle = (runtime_actor->facing_blend + runtime_actor->facing_current) & PHYSICS_ANGLE_MASK;
754 }
755
756 int32_t target_relative_phase = (((int32_t)Math_CalculateAtan2FP12(target_delta_z, target_delta_x) - (int32_t)(int16_t)steering_facing_angle - PHYSICS_ANGLE_HALF_TURN) << PHYSICS_FIXED_FRACTION_BITS & PHYSICS_SIGNED_PHASE_MASK) - ACTOR_STEERING_PHASE_BIAS;
757
758 source_entity->target_offset_phase = target_relative_phase;
759
760 if (target_relative_phase < (-ACTOR_STEERING_PHASE_LIMIT))
762 else if (target_relative_phase > ACTOR_STEERING_PHASE_LIMIT)
763 source_entity->target_offset_phase = ((uint32_t)ACTOR_STEERING_PHASE_LIMIT);
764
765 source_entity->target_offset_step = 0;
766 }
767 }
768
770 {
771 int32_t direction_mode_height_offset_for_result;
772
773 if (source_entity->param_direction_mode < ENTITY_DIRECTION_SPEED_HEIGHT_OFFSET_FIRST || !runtime_actor)
774 direction_mode_height_offset_for_result = 0;
775 else
776 direction_mode_height_offset_for_result = ((struct Player_ControllerRecordView *)runtime_actor->record_ptr)->direction_mode_height_offset;
777
778 source_entity->path_result.x = position_ptr->x + target_delta_x;
779 source_entity->path_result.y = position_ptr->y + dy - direction_mode_height_offset_for_result;
780 source_entity->path_result.z = target_delta_z + position_ptr->z;
781 }
782 else
783 {
784 source_entity->path_best_distance = (int32_t)target_distance;
785 source_entity->path_target.x = position_ptr->x + target_delta_x;
786 source_entity->path_target.y = position_ptr->y + dy;
787 source_entity->path_target.z = target_delta_z + position_ptr->z;
788
789 if (target_selector < 0)
790 {
791 int32_t result_respawn_target_count = (uint8_t)(source_entity->bonus_respawn_mode);
792
793 if (target_selector >= -(result_respawn_target_count))
794 {
795 source_entity->active_flag = UINT8_MAX - (uint8_t)target_selector;
796
797 if (target_distance <= (uint32_t)max_path_distance)
798 {
799 uint32_t reached_target_flags = source_entity->flags;
800
801 reached_target_flags |= ENTITY_FLAG_GROUP_BIT_9;
802 source_entity->flags = reached_target_flags;
803 }
804 }
805 }
806 }
807
808 if (target_distance <= (uint32_t)max_path_distance)
809 return target_selector;
810
811 return 0;
812}
#define ACTOR_TRACE_USE_TARGET_RESULT
Definition constants.h:75
#define ACTOR_TARGET_DEFAULT_RADIUS
Definition constants.h:206
@ ACTOR_TARGET_BONUS_RESPAWN_POSITION
Definition constants.h:167
@ ACTOR_TARGET_ALL_ENTITIES
Definition constants.h:168
@ ACTOR_TARGET_CURRENT_RESPAWN
Definition constants.h:158
@ ACTOR_TARGET_ENTITY_SCAN
Definition constants.h:160
@ ACTOR_TARGET_LAST_RESPAWN
Definition constants.h:164
@ ACTOR_TARGET_NAVIGATION_POSITION_ALTERNATE
Definition constants.h:165
@ ACTOR_TARGET_CURRENT_RESPAWN_FALLBACK
Definition constants.h:166
@ ACTOR_TARGET_NEAREST_RESPAWN
Definition constants.h:159
@ ACTOR_TARGET_NAVIGATION_POSITION
Definition constants.h:161
@ ACTOR_TARGET_COMMAND_END
Definition constants.h:170
@ ACTOR_TARGET_ACTIVE_ENTITIES
Definition constants.h:157
@ ACTOR_TARGET_CURRENT_RESPAWN_ALTERNATE
Definition constants.h:162
@ ACTOR_TARGET_COMMAND_FIRST
Definition constants.h:169
@ ACTOR_TARGET_FIRST_RESPAWN
Definition constants.h:163
#define ACTOR_TRACE_VIEW_CONE
Definition constants.h:73
@ ACTOR_TRACE_FILTER_INACTIVE_ENTITIES
Definition constants.h:254
@ ACTOR_TRACE_FILTER_ACTIVE_ENTITIES
Definition constants.h:253
#define ACTOR_TRACE_SKIP_ACTOR_OCCLUSION
Definition constants.h:76
#define ACTOR_TRACE_NAVIGATION_RECORDS
Definition constants.h:78
#define ACTOR_TRACE_KEEP_NEAREST
Definition constants.h:77
#define ACTOR_PATH_OCCLUSION_PROBE_RADIUS
Definition constants.h:242
#define ACTOR_TRACE_USE_TARGET_POSITION
Definition constants.h:74
#define ACTOR_STEERING_PHASE_BIAS
Definition constants.h:208
#define ACTOR_TRACE_INCLUDE_PERSISTENT
Definition constants.h:80
@ ACTOR_PHASE_LEAVING
Definition constants.h:152
#define ACTOR_TRACE_MATCH_ACTIVATION
Definition constants.h:79
#define ACTOR_STEERING_PHASE_LIMIT
Definition constants.h:207
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)
#define g_cameraData
Definition globals.h:63
#define ENTITY_FLAG_GROUP_BIT_9
Definition constants.h:16
#define ENTITY_ANGLE_SCALE_Q12
Definition constants.h:32
#define ENTITY_FLAG_COLLISION_LIST_UNLINKED
Definition constants.h:21
#define ENTITY_INDEX_RECIPROCAL
Definition constants.h:33
#define ENTITY_FLAG_ACTIVE_WORKLIST_REQUEST
Definition constants.h:9
#define ENTITY_FLAG_SCRIPT_PAUSED
Definition constants.h:20
@ ENTITY_DIRECTION_SPEED_HEIGHT_OFFSET_FIRST
Definition constants.h:50
#define ENTITY_FLAG_ACTIVATION_REQUEST
Definition constants.h:17
#define ENTITY_FLAG_RESTORE_DEFAULTS
Definition constants.h:18
BOOL Entity_IsInActiveList(int32_t entity_index_1based)
Definition lifecycle.c:39
#define EntityNavigationWorkList_ActiveBufferPtr
Definition globals.h:12
int32_t Graphics_CheckFrustumCull(struct Math_Vec3I32 *position, int32_t cull_radius, uint32_t cull_flags)
Definition clipping.c:772
int32_t Math_SinQ14(int32_t angle)
int32_t Math_ApproxDistance3DWeighted(int32_t dx, int32_t dy, int32_t dz)
Definition fixed_point.c:59
int16_t Math_CalculateAtan2FP12(int32_t y, int32_t x)
#define NAVIGATION_COMMAND_DEFAULT_SELECTOR
Definition constants.h:11
#define NAVIGATION_COMMAND_UNLIMITED_SPEED
Definition constants.h:12
#define pkg_resource_current_level_data
Definition globals.h:117
static bool target_in_view_cone(int32_t facing, int32_t half_angle, int32_t dx, int32_t dz, int32_t radius)
static int32_t project_horizontal(int32_t angle, int32_t dx, int32_t dz)
int32_t Actor_TracePathWithSteeringEntity(struct Entity *source_entity, int32_t target_selector, int32_t max_path_distance, int32_t best_path_distance, int32_t trace_flags, struct Entity *steering_entity)
#define PHYSICS_SIGNED_PHASE_MASK
Definition constants.h:19
#define PHYSICS_ANGLE_HALF_TURN
Definition constants.h:10
#define PHYSICS_ANGLE_MASK
Definition constants.h:8
#define PHYSICS_ANGLE_QUARTER_TURN
Definition constants.h:9
#define PHYSICS_FIXED_FRACTION_BITS
Definition constants.h:6
int32_t Physics_CalculateMovementWithCollision(struct Math_Vec3I32 const *start_pos, struct Math_Vec3I32 const *end_pos, struct Actor_State const *collision_actor, int16_t probe_radius, int32_t collision_flags)
Definition movement.c:38
#define g_objectNodeRoot
Definition globals.h:12
Definition types.h:118
int16_t facing_current
Definition types.h:200
uint8_t transition_phase
Definition types.h:187
struct Pkg_ActorRecord * record_ptr
Definition types.h:207
int16_t facing_blend
Definition types.h:202
int16_t collision_height_hi
Definition types.h:175
struct Math_Vec3I32 world_render_pos
Definition types.h:173
struct Entity * active_entities[0x4]
Definition types.h:133
struct Nav_Command nav_commands[0x64]
Definition types.h:135
Definition types.h:22
uint8_t param_direction_mode
Definition types.h:96
uint32_t flags
Definition types.h:25
uint8_t bonus_respawn_mode
Definition types.h:39
int16_t attach_offset_x
Definition types.h:42
struct Math_Vec3I32 path_target
Definition types.h:113
struct Math_Vec3I32 navigation_command_position
Definition types.h:115
struct Math_Vec3I32 bonus_respawn_pos
Definition types.h:35
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
int32_t param_108
Definition types.h:99
struct Math_Vec3I32 * bonus_respawn_target_pos
Definition types.h:41
struct Math_Vec3I32 path_result
Definition types.h:114
int32_t target_offset_step
Definition types.h:117
int32_t target_offset_phase
Definition types.h:116
int32_t path_best_distance
Definition types.h:112
uint8_t active_flag
Definition types.h:38
int16_t entity_count
Definition types.h:44
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
int16_t target_selector_b
Definition types.h:73
int16_t target_selector_a
Definition types.h:72
struct Math_Vec3I32 pos
Definition types.h:75
uint32_t command_type
Definition types.h:71
int32_t speed
Definition types.h:74
struct Math_Vec3I32 * position_ptr
Definition types.h:137
int32_t direction_mode_height_offset
Definition types.h:131
int32_t global_type_g_pathTraceState1
Definition values.h:158
#define g_pathTraceState1
Definition values.h:159
#define g_pathTraceState3
Definition values.h:162
#define g_pathTraceState2
Definition values.h:156
#define g_pathTraceWorkBuffer
Definition values.h:153