102 Dalmatians: Puppies to the Rescue
Decompilation Source Reference | GitLab repository
Loading...
Searching...
No Matches
properties.c
Go to the documentation of this file.
1/* Recovered game implementation; original addresses identify provenance. */
10#include <math.h>
11#include <stdarg.h>
12#include <string.h>
13
14// Original pcdogs.exe: 0x42e890
15// Returns the component damage word and writes one to the optional output flag.
16uint16_t Component_GetDamageAmount(struct Component_ProjectileActorView *projectile, int32_t *out_hit_flag)
17{ // Get damage amount from component definition at offset 0x50. Sets damage type to 1 if out param provided.
18 struct Component_Definition *definition = projectile->spawn_context->definition;
19
20 if (out_hit_flag)
21 *(uint32_t *)out_hit_flag = 1;
22
23 return definition->damage_amount;
24}
25
26// Original pcdogs.exe: 0x42f270
27// Returns the actor's unsigned component team identifier, or minus one for a null actor.
28int32_t Component_GetTeamID(struct Actor_State *actor)
29{ // Get team ID from component definition at offset 0x48. Returns 0xFFFFFFFF if component is NULL.
30 if (!actor)
31 return COMPONENT_NO_TEAM;
32
33 int32_t team_id = (uint8_t)(((struct Component_SpawnParams *)actor->record_ptr)->definition->team_id);
34
35 return team_id;
36}
37
38// Original pcdogs.exe: 0x42f300
39// Returns the Q12 movement speed limit, or zero for a null parameter block.
41{ // Get next component in linked list chain at offset +8. Returns NULL if component is NULL.
42 if (movement)
43 return movement->speed_limit_q12;
44
45 return 0;
46}
47
48// Original pcdogs.exe: 0x42f310
49// Returns the signed 16-bit movement turn step, or zero for a null parameter block.
51{ // Get component flags (ushort) at offset 0x20. Returns 0 if component is NULL.
52 if (movement)
53 return movement->turn_step_angle12;
54
55 return 0;
56}
57
58// Original pcdogs.exe: 0x42f320
59// Returns the Q12 movement acceleration, or zero for a null parameter block.
61{
62 if (movement)
63 return movement->acceleration_q12;
64
65 return 0;
66}
67
68// Original pcdogs.exe: 0x42f350
69// Packs the low words of the component's two range parameters into one 32-bit value. Consumers interpret the two halves as signed values.
70uint32_t Component_GetSpeedRange(struct Actor_State *actor)
71{
72 int32_t minimum_speed_word = 0;
73 struct Component_Definition *component_definition =
75
76 minimum_speed_word = (uint16_t)((uint16_t)component_definition->speed_min);
77
78 return (uint32_t)component_definition->speed_max << 0x10 | minimum_speed_word;
79}
80
81// Original pcdogs.exe: 0x42f780
82// Computes launch yaw and pitch from defaults, target tracking, ballistic adjustment, and scatter, then wraps angles and builds orientation.
84{
85 struct Component_Definition *active_definition = definition;
86 struct Component_ProjectileActorView *projectile = projectile_input;
87 int16_t launch_pitch = active_definition->base_pitch_angle12;
88
89 if (((uint16_t)active_definition->flags >> 8) & (COMPONENT_FLAG_BALLISTIC_PITCH >> 8))
90 {
91 struct Component_SpawnParams *spawn_context = projectile->spawn_context;
92
93 if (spawn_context->state_flags & COMPONENT_STATE_HAS_TARGET)
94 {
95 struct Actor *attached_actor = spawn_context->attached_actor;
96 int32_t target_delta_x;
97 int32_t target_height_delta_q12;
98 int32_t target_z;
99
100 if (attached_actor != COMPONENT_FIXED_POSITION_TARGET)
101 {
102 int32_t projectile_y_for_actor = projectile->position.y;
103
104 target_delta_x = attached_actor->world_render_pos.x - projectile->position.x;
105 target_height_delta_q12 = attached_actor->world_render_pos.y - projectile_y_for_actor;
106
107 target_z = attached_actor->world_render_pos.z;
108 }
109 else
110 {
111 int32_t projectile_y_for_position = projectile->position.y;
112
113 target_delta_x = spawn_context->target_pos.x - projectile->position.x;
114 target_height_delta_q12 = spawn_context->target_pos.y - projectile_y_for_position;
115
116 target_z = spawn_context->target_pos.z;
117 }
118
119 int32_t target_delta_z = target_z - projectile->position.z;
120 int32_t horizontal_distance_q12;
121
122 horizontal_distance_q12 = Math_SqrtFP12((uint32_t)(((uint64_t)((int64_t)target_delta_x * target_delta_x) + (uint64_t)((int64_t)target_delta_z * target_delta_z)) >> MATH_Q12_SHIFT));
123
124 if (target_height_delta_q12 || horizontal_distance_q12)
125 {
126 int32_t gravity = definition->gravity;
127 int32_t launch_speed = definition->initial_vel_xz.z;
128 int64_t horizontal_distance_squared = (int64_t)horizontal_distance_q12 * horizontal_distance_q12;
129 int32_t horizontal_distance_squared_q12 = (uint32_t)((uint64_t)horizontal_distance_squared >> MATH_Q12_SHIFT);
130 int64_t height_squared = (int64_t)target_height_delta_q12 * target_height_delta_q12;
131 int32_t target_distance_squared_q12 = ((uint32_t)((uint64_t)height_squared >> MATH_Q12_SHIFT)) + horizontal_distance_squared_q12;
132 int64_t gravity_height = (int64_t)gravity * target_height_delta_q12;
133 int64_t launch_speed_squared = (int64_t)launch_speed * launch_speed;
134 int64_t trajectory_term = (int64_t)horizontal_distance_squared_q12 * (int32_t)(((uint32_t)((uint64_t)gravity_height >> MATH_Q12_SHIFT)) - ((uint32_t)((uint64_t)launch_speed_squared >> MATH_Q12_SHIFT)));
135 int32_t trajectory_term_q12 = (uint32_t)((uint64_t)trajectory_term >> MATH_Q12_SHIFT);
136 int64_t gravity_horizontal_squared = (int64_t)horizontal_distance_squared_q12 * gravity;
137 int32_t gravity_horizontal_squared_q12 = (uint32_t)((uint64_t)gravity_horizontal_squared >> MATH_Q12_SHIFT);
138 int64_t gravity_term_squared = (int64_t)gravity_horizontal_squared_q12 * gravity_horizontal_squared_q12;
139 int64_t trajectory_term_squared = (int64_t)trajectory_term_q12 * trajectory_term_q12;
140 int32_t trajectory_term_squared_q12 = (uint32_t)((uint64_t)trajectory_term_squared >> MATH_Q12_SHIFT);
141 int32_t gravity_term_squared_q12 = (uint32_t)((uint64_t)gravity_term_squared >> MATH_Q12_SHIFT);
142 int64_t discriminant_product = (int64_t)target_distance_squared_q12 * gravity_term_squared_q12;
143 int32_t discriminant_product_q12 = (uint32_t)((uint64_t)discriminant_product >> MATH_Q12_SHIFT);
144
145 if (trajectory_term_squared_q12 - discriminant_product_q12 >= 0)
146 {
147 int32_t trajectory_root_numerator = -((Math_SqrtFP12(trajectory_term_squared_q12 - discriminant_product_q12) + trajectory_term_q12));
148 int32_t solved_launch_pitch;
149
150 solved_launch_pitch = Math_AcosRatioFP12(
151 Math_SqrtFP12(((int64_t)trajectory_root_numerator * MATH_Q12_ONE) / (target_distance_squared_q12 * 2)), launch_speed);
152 launch_pitch = solved_launch_pitch;
153 }
154 }
155
156 active_definition = definition;
157 projectile = projectile_input;
158 }
159 }
160
161 int16_t wrapped_launch_pitch = launch_pitch & MATH_ANGLE_WRAP_MASK;
162
163 projectile->pitch_angle = wrapped_launch_pitch;
164 projectile->yaw_angle = (active_definition->yaw_offset_angle12 + projectile->yaw_angle) & MATH_ANGLE_WRAP_MASK;
165
166 int32_t angle_y = (active_definition->pitch_offset_angle12 + wrapped_launch_pitch) & MATH_ANGLE_WRAP_MASK;
167
168 projectile->pitch_angle = (uint16_t)angle_y;
169
170 int32_t initial_target_turn_step_angle12 = active_definition->initial_target_turn_step_angle12;
171
172 if (initial_target_turn_step_angle12 > 0)
173 Component_TrackTarget(projectile, active_definition, (uint16_t)initial_target_turn_step_angle12);
174
175 int32_t angular_scatter_width_angle12 = active_definition->angular_scatter_width_angle12;
176
177 if (angular_scatter_width_angle12)
178 {
179 int16_t yaw_scatter = Math_GenerateRandom(angular_scatter_width_angle12);
180
181 projectile->yaw_angle = ((int16_t)(active_definition->angular_scatter_width_angle12 >> 1) + projectile->yaw_angle - yaw_scatter) & MATH_ANGLE_WRAP_MASK;
182
183 int16_t pitch_scatter;
184
185 pitch_scatter = Math_GenerateRandom(active_definition->angular_scatter_width_angle12);
186 projectile->pitch_angle = ((int16_t)(active_definition->angular_scatter_width_angle12 >> 1) - pitch_scatter + projectile->pitch_angle) & MATH_ANGLE_WRAP_MASK;
187 }
188
189 int32_t angle_x;
190
191 if (!(((uint16_t)active_definition->flags >> 8) & (COMPONENT_FLAG_YAW_ONLY_ORIENTATION >> 8)))
192 angle_x = -((int32_t)projectile->pitch_angle);
193 else
194 angle_x = 0;
195
196 angle_y = projectile->yaw_angle;
197 Math_BuildMatrixRotationXY(&projectile->rot_mat, NULL, angle_x, angle_y);
198}
199
200// Original pcdogs.exe: 0x42fb20
201// Plays a nonnegative sound entry from the selected component slot at the actor's position. Null actors are ignored, but the slot index is not validated.
202void Component_PlayPositionalSound(struct Component_Definition *component_def, struct Actor_State *actor, int32_t sound_slot_index)
203{
204 if (!actor)
205 return;
206
207 int32_t sound_index = component_def->sound_ids[sound_slot_index];
208
209 if (sound_index >= 0)
210 Audio_PlayLevelSoundIndexAtPosition(sound_index, &actor->position);
211}
212
213// Original pcdogs.exe: 0x4306a0
214// Scales the forward direction by launch speed to set projectile velocity and builds the selected full or yaw-only orientation.
216{
217 Math_BuildMatrixRotationXY(&projectile->rot_mat, NULL, -(int32_t)projectile->pitch_angle, projectile->yaw_angle);
218
219 int32_t speed = definition->initial_vel_xz.z;
220
221 projectile->velocity.x = (int32_t)(((int64_t)projectile->rot_mat.m02 * speed) >> MATH_Q12_SHIFT);
222 projectile->velocity.y = (int32_t)(((int64_t)projectile->rot_mat.m12 * speed) >> MATH_Q12_SHIFT);
223 projectile->velocity.z = (int32_t)(((int64_t)projectile->rot_mat.m22 * speed) >> MATH_Q12_SHIFT);
224
225 if ((definition->flags & COMPONENT_FLAG_YAW_ONLY_ORIENTATION) && projectile->pitch_angle)
226 Math_BuildMatrixRotationY(&projectile->rot_mat, NULL, projectile->yaw_angle);
227}
void Audio_PlayLevelSoundIndexAtPosition(int32_t sound_index, struct Math_Vec3I32 *position)
Definition spatial.c:52
#define COMPONENT_NO_TEAM
Definition constants.h:37
#define COMPONENT_FIXED_POSITION_TARGET
Definition constants.h:28
#define COMPONENT_FLAG_YAW_ONLY_ORIENTATION
Definition constants.h:17
#define COMPONENT_STATE_HAS_TARGET
Definition constants.h:23
#define COMPONENT_FLAG_BALLISTIC_PITCH
Definition constants.h:19
void Component_TrackTarget(struct Component_ProjectileActorView *comp, struct Component_Definition *definition, int16_t turn_rate)
Definition collision.c:383
#define MATH_Q12_SHIFT
Definition constants.h:8
#define MATH_ANGLE_WRAP_MASK
Definition constants.h:16
#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
int32_t Math_SqrtFP12(uint32_t value)
int32_t Math_AcosRatioFP12(int32_t numerator, int32_t denominator)
uint32_t Math_GenerateRandom(uint32_t range)
void Math_BuildMatrixRotationXY(struct Math_Matrix3x3I16 *out_matrix, struct Math_Matrix3x3I16 const *left_factor, int32_t angle_x, int32_t angle_y)
Definition matrices.c:120
int32_t Component_GetMovementSpeedLimitQ12(struct Component_MovementParametersView *movement)
Definition properties.c:40
void Component_PlayPositionalSound(struct Component_Definition *component_def, struct Actor_State *actor, int32_t sound_slot_index)
Definition properties.c:202
int32_t Component_GetMovementAccelerationQ12(struct Component_MovementParametersView *movement)
Definition properties.c:60
void Component_CalculateOrientation(struct Component_ProjectileActorView *projectile_input, struct Component_Definition *definition)
Definition properties.c:83
void Component_SetVelocityFromDirection(struct Component_ProjectileActorView *projectile, struct Component_Definition *definition)
Definition properties.c:215
int16_t Component_GetMovementTurnStep(struct Component_MovementParametersView *movement)
Definition properties.c:50
uint32_t Component_GetSpeedRange(struct Actor_State *actor)
Definition properties.c:70
uint16_t Component_GetDamageAmount(struct Component_ProjectileActorView *projectile, int32_t *out_hit_flag)
Definition properties.c:16
int32_t Component_GetTeamID(struct Actor_State *actor)
Definition properties.c:28
void * record_ptr
Definition types.h:477
struct Math_Vec3I32 position
Definition types.h:385
Definition types.h:118
struct Math_Vec3I32 world_render_pos
Definition types.h:173
int32_t speed_max
Definition types.h:90
int32_t gravity
Definition types.h:85
uint16_t damage_amount
Definition types.h:112
int32_t initial_target_turn_step_angle12
Definition types.h:93
int32_t angular_scatter_width_angle12
Definition types.h:92
int32_t pitch_offset_angle12
Definition types.h:95
int32_t sound_ids[0x3]
Definition types.h:118
int32_t yaw_offset_angle12
Definition types.h:94
int16_t speed_min
Definition types.h:88
int32_t base_pitch_angle12
Definition types.h:87
uint32_t flags
Definition types.h:74
struct Math_Vec2I32XZ initial_vel_xz
Definition types.h:84
struct Math_Vec3I32 position
Definition types.h:169
struct Math_Matrix3x3I16 rot_mat
Definition types.h:167
struct Component_SpawnParams * spawn_context
Definition types.h:221
struct Math_Vec3I32 velocity
Definition types.h:213
struct Math_Vec3I32 target_pos
Definition types.h:326
struct Component_Definition * definition
Definition types.h:322
uint8_t state_flags
Definition types.h:333
struct Actor * attached_actor
Definition types.h:327
int16_t m12
Definition types.h:167
int16_t m22
Definition types.h:170
int16_t m02
Definition types.h:164
int32_t z
Definition types.h:436
int32_t y
Definition types.h:471
int32_t x
Definition types.h:470
int32_t z
Definition types.h:472