102 Dalmatians: Puppies to the Rescue
Decompilation Source Reference | GitLab repository
Loading...
Searching...
No Matches
spatial.c
Go to the documentation of this file.
1/* Recovered game implementation; original addresses identify provenance. */
10#include "pcdogs/game/globals.h"
11#include "pcdogs/game/types.h"
13#include "pcdogs/level/types.h"
15#include "pcdogs/math/types.h"
19#include <math.h>
20#include <stdarg.h>
21#include <string.h>
22
23// Original pcdogs.exe: no direct counterpart (decomp helper)
24// Divides the signed high:low 64-bit word pair by a signed 32-bit divisor using x86 IDIV, preserving divide-by-zero and quotient-overflow traps.
25static inline int32_t Math_DivideSignedWordPair(int32_t high, uint32_t low, int32_t divisor)
26{
27 int32_t quotient, remainder;
28
29 __asm__("idivl %4" : "=a"(quotient), "=d"(remainder)
30 : "a"(low), "d"(high), "rm"(divisor) : "cc");
31
32 return quotient;
33}
34
35// Original pcdogs.exe: 0x403ce0
36// Converts an encoded actor sound selector using the template's signed sound-index base, returning -1 for a missing template or negative base.
37int32_t Audio_ResolveActorSoundIndex(struct Pkg_ActorTemplate const *actor_template, int32_t encoded_sound_selector)
38{
39 if (!actor_template)
41
42 int16_t sound_index_base = actor_template->sound_index_base;
43
44 if (sound_index_base >= 0)
45 return (int32_t)sound_index_base + encoded_sound_selector - AUDIO_ACTOR_SELECTOR_BASE;
46
48}
49
50// Original pcdogs.exe: 0x403e80
51// Queues a level sound, suppressing flagged duplicates by definition and emitter-pointer identity. The sound index is unchecked, and borrowed position storage must remain valid until the sound is detached or stopped.
52void Audio_PlayLevelSoundIndexAtPosition(int32_t sound_index, struct Math_Vec3I32 *position)
53{
55
56 if (!level_data)
57 return;
58
59 struct Audio_SoundDefinition *sound_list = level_data->sound_list;
60 // The caller supplies an in-range sound index.
61 struct Audio_SoundDefinition *sound_def = &sound_list[sound_index];
62 int32_t existing_sound_slot;
63
64 if (sound_def->flags & AUDIO_SOUND_LOOP)
65 // Duplicate suppression compares definition and emitter pointers, not coordinates.
66 existing_sound_slot = Audio_FindSoundByType(AUDIO_INVALID_SOUND_INDEX, sound_def, position);
67
68 // Sound flag bit 0 is loop playback: suppress duplicate loop at same definition+position.
69 if (!(sound_def->flags & AUDIO_SOUND_LOOP) || existing_sound_slot < 0)
70 // The borrowed emitter position must outlive playback or be detached first.
71 Audio_PlaySound3D(sound_def, position);
72}
73
74// Original pcdogs.exe: 0x403ed0
75// Stops sounds matching emitter-pointer identity and an optional definition; mode -2 instead snapshots and detaches nonlooping sounds and stops looping ones. Nonnegative indices are unchecked, while negative values match any definition.
76void Audio_StopOrUpdateSoundsAtPosition(int32_t sound_index_or_mode, struct Math_Vec3I32 *position, char looping_only)
77{
79
80 if (!level_data)
81 return;
82
83 struct Audio_SoundDefinition *sound_def;
84
85 if (sound_index_or_mode >= 0)
86 {
87 // Nonnegative indices are unchecked; negative modes use a definition wildcard.
88 sound_def = &level_data->sound_list[sound_index_or_mode];
89
90 // When looping_only is set, ignore definitions without loop flag bit 0.
91 if (looping_only && !(sound_def->flags & AUDIO_SOUND_LOOP))
92 return;
93 }
94 else
95 sound_def = NULL;
96
97 for (int32_t slot_index = Audio_FindSoundByType(AUDIO_INVALID_SOUND_INDEX, sound_def, position); slot_index >= 0;
98 slot_index = Audio_FindSoundByType(AUDIO_INVALID_SOUND_INDEX, sound_def, position))
99 {
100 if (sound_index_or_mode != AUDIO_DETACH_EMITTER_MODE)
101 Audio_StopSound(slot_index);
102 // Mode -2 snapshots and detaches the emitter; looping sounds are stopped.
103 else if (!Audio_UpdateSoundPosition(slot_index))
104 Audio_StopSound(slot_index);
105 }
106}
107
108// Original pcdogs.exe: 0x436370
109// Checks four cooldown entries for the actor and sound key, allowing events on their starting frame or after expiry and rejecting intervening frames.
110uint8_t Audio_CanRegisterCollisionSound(struct Audio_CollisionSoundCooldownEntry *entries, struct Actor_State *other_actor, int32_t sound_key)
111{
112 struct Audio_CollisionSoundCooldownEntry *cooldown_entry = entries;
113
114 for (int32_t i = 0; i < AUDIO_COLLISION_COOLDOWN_COUNT;)
115 {
116 if (cooldown_entry->other_actor == other_actor && cooldown_entry->sound_key == sound_key)
117 {
118 uint32_t current_frame = (uint32_t)g_frameCounter;
119
120 if (cooldown_entry->start_frame != current_frame)
121 return current_frame >= cooldown_entry->expires_frame;
122
123 break;
124 }
125
126 i += 1;
127 cooldown_entry = &cooldown_entry[1];
128 }
129
130 return 1;
131}
132
133// Original pcdogs.exe: 0x4363b0
134// Rejects suppressed or cooling-down collision sounds, otherwise records impact data in an expired cooldown slot. Returns success even when all four slots are occupied and no entry is written.
135uint8_t Audio_RegisterCollisionSoundEvent(struct Actor_State *actor, struct Actor_State *other_actor, int32_t sound_key, uint32_t packed_impact_cooldown)
136{
137 struct Player_ControllerRecordView *controller = actor->record_ptr;
138
140 {
141 return 0;
142 }
143
145
147 {
148 return 0;
149 }
150
151 int32_t cooldown_entry_index = 0;
152
153 while (entries->expires_frame >= (uint32_t)g_frameCounter)
154 {
155 cooldown_entry_index += 1;
156 entries = &entries[1];
157
158 if (cooldown_entry_index >= AUDIO_COLLISION_COOLDOWN_COUNT)
159 {
160 return 1;
161 }
162 }
163
165 &entries->velocity, &entries->impact_velocity);
166 entries->other_actor = other_actor;
167 entries->sound_key = sound_key;
168 entries->start_frame = g_frameCounter;
169 entries->expires_frame = (packed_impact_cooldown & AUDIO_COLLISION_COOLDOWN_MASK) + g_frameCounter;
170
171 return 1;
172}
173
174// Original pcdogs.exe: 0x440ed0
175// Allocates and queues a sound definition, retaining the emitter pointer and snapshotting emitter and listener positions when provided. Returns -1 for unavailable samples, disallowed overlay playback, or allocation failure.
176int32_t Audio_PlaySound3D(struct Audio_SoundDefinition *sound_def, struct Math_Vec3I32 *position)
177{
178 // soundDescriptor contains: sound data ptr, volume, pitch, pan, flags
179 // position is 3D vector [x, y, z] for spatial audio
182
183 int32_t result = Audio_AllocateSoundSlot(sound_def); // Find available sound slot for playback
184
185 if (result < 0)
186 return result;
187
188 g_soundEntries[result].sound_def_ptr = sound_def;
189 g_soundEntries[result].position_ptr = position;
190
191 if (position)
192 {
193 // Copy 3D position vector (x, y, z) to sound slot
194 g_soundEntries[result].sound_pos.x = position->x;
195 g_soundEntries[result].sound_pos.y = position->y;
196 g_soundEntries[result].sound_pos.z = position->z;
197
198 struct Math_Vec3I32 *listener_position = &g_cameraData.eye_pos;
199
200 // Choose listener position based on audio mode flags
202 listener_position = &g_cameraData.target;
203
204 // Copy listener position for 3D audio calculation
205 g_soundEntries[result].listener_pos.x = listener_position->x;
206 g_soundEntries[result].listener_pos.y = listener_position->y;
207 g_soundEntries[result].listener_pos.z = listener_position->z;
208 }
209
210 return result;
211}
212
213// Original pcdogs.exe: 0x441080
214// Copies a borrowed emitter position into slot-owned storage and rebinds the pointer there. Returns false for looping definitions, allowing callers to stop them instead of retaining a frozen position.
215BOOL Audio_UpdateSoundPosition(int32_t slot_index)
216{
217 struct Audio_SoundDefinition *sound_def_ptr = g_soundEntries[slot_index].sound_def_ptr;
218
219 if (!sound_def_ptr)
220 return 1;
221
222 struct Math_Vec3I32 *position_ptr = g_soundEntries[slot_index].position_ptr;
223
224 if (position_ptr)
225 {
226 g_soundEntries[slot_index].sound_pos.x = position_ptr->x;
227 g_soundEntries[slot_index].sound_pos.y = position_ptr->y;
228
229 int32_t z = position_ptr->z;
230
231 g_soundEntries[slot_index].position_ptr = (struct Math_Vec3I32 *)&g_soundEntries[slot_index].sound_pos;
232 g_soundEntries[slot_index].sound_pos.z = z;
233 }
234
235 return !(sound_def_ptr->flags & AUDIO_SOUND_LOOP);
236}
237
238// Original pcdogs.exe: 0x4415c0
239// Computes distance attenuation and camera-relative pan, returning false when the requested volume becomes inaudible. The pan output is required, and attenuation divisions retain x86 signed division traps.
240uint8_t Audio_CalculateSpatialVolumeAndPan(struct Math_Vec3I32 *sound_position, uint32_t audible_radius, int32_t *out_volume_q12, int16_t *out_pan_angle)
241{
242 int32_t dx = 0, dz = 0;
243 uint32_t distance = 0;
244
245 if (sound_position)
246 {
248 ? &g_cameraData.eye_pos
249 : &g_cameraData.target;
250
251 dx = listener->x - sound_position->x;
252 dz = listener->z - sound_position->z;
253 distance = Math_ApproxDistance3DWeighted(dx, listener->y - sound_position->y, dz);
254
255 if (distance >= audible_radius && out_volume_q12)
256 {
257 *out_volume_q12 = 0;
258
259 return 0;
260 }
261 }
262
263 if (out_volume_q12)
264 {
265 uint32_t quarter_radius = audible_radius >> 2;
266 int32_t volume = AUDIO_Q12_ONE;
267
268 if (distance > quarter_radius)
269 {
270 int32_t remaining = audible_radius - distance;
271
272 volume = Math_DivideSignedWordPair(remaining >> AUDIO_Q12_HIGH_WORD_SHIFT, (uint32_t)remaining << AUDIO_Q12_SHIFT,
273 audible_radius - quarter_radius);
274 }
275
276 *out_volume_q12 = volume;
277
278 if (!volume)
279 return 0;
280 }
281
283 {
284 *out_pan_angle = AUDIO_PAN_QUARTER_TURN;
285
286 return 1;
287 }
288
289 int32_t angle = Math_CalculateAtan2FP12(-dz, -dx);
290 int32_t pan = (g_cameraData.look_yaw - (uint16_t)angle + AUDIO_PAN_QUARTER_TURN) & AUDIO_PAN_WRAP_MASK;
291
292 if (pan > AUDIO_PAN_HALF_TURN)
293 pan = AUDIO_Q12_ONE - pan;
294
295 if (distance < audible_radius)
296 {
297 int32_t scale = Math_DivideSignedWordPair((int32_t)distance >> AUDIO_Q12_HIGH_WORD_SHIFT, distance << AUDIO_Q12_SHIFT, audible_radius);
298
300 }
301
302 int16_t packed_pan = (int16_t)pan;
303 *out_pan_angle = packed_pan == AUDIO_PAN_HALF_TURN ? AUDIO_PAN_HALF_TURN - 1 : packed_pan;
304
305 return 1;
306}
#define ACTOR_BEHAVIOR_WORKLIST_MEMBER
Definition constants.h:34
#define AUDIO_INVALID_SOUND_INDEX
Definition constants.h:49
#define AUDIO_SOUND_LOOP
Definition constants.h:5
#define AUDIO_DETACH_EMITTER_MODE
Definition constants.h:50
#define AUDIO_PAN_WRAP_MASK
Definition constants.h:28
#define AUDIO_COLLISION_COOLDOWN_MASK
Definition constants.h:54
#define AUDIO_COLLISION_COOLDOWN_COUNT
Definition constants.h:53
#define AUDIO_Q12_ONE
Definition constants.h:24
#define AUDIO_Q12_HIGH_WORD_SHIFT
Definition constants.h:23
#define AUDIO_PAN_NEAR_LISTENER_DIAMETER
Definition constants.h:58
#define AUDIO_COLLISION_IMPACT_SHIFT
Definition constants.h:55
#define AUDIO_PAN_HALF_TURN
Definition constants.h:26
#define AUDIO_PAN_NEAR_LISTENER_RADIUS
Definition constants.h:57
#define AUDIO_ACTOR_SELECTOR_BASE
Definition constants.h:52
#define AUDIO_Q12_SHIFT
Definition constants.h:22
#define AUDIO_PAN_QUARTER_TURN
Definition constants.h:27
void Audio_StopSound(int32_t slot_index)
Definition channels.c:89
int32_t Audio_FindSoundByType(int32_t slot_index, struct Audio_SoundDefinition *sound_def, struct Math_Vec3I32 *position)
Definition channels.c:214
int32_t Audio_AllocateSoundSlot(struct Audio_SoundDefinition *sound_def)
Definition channels.c:145
#define g_soundEntries
Definition globals.h:39
#define g_cameraData
Definition globals.h:63
void Collision_CalculateImpactContactData(struct Actor_State *actor, struct Actor_State *other_actor, int32_t collision_fraction_q12, struct Math_Vec3I32 *out_contact_pos, struct Math_Vec3I32 *out_impact_motion)
Definition actors.c:355
#define GAME_TRANSITION_SCRIPT_PAUSED
Definition constants.h:20
#define GAME_OVERLAY_SKIP_OBJECT_RENDER
Definition constants.h:36
#define Game_FrameTransitionFlags
Definition globals.h:47
#define Game_FrameOverlayFlags
Definition globals.h:44
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 pkg_resource_current_level_data
Definition globals.h:117
uint8_t Audio_CanRegisterCollisionSound(struct Audio_CollisionSoundCooldownEntry *entries, struct Actor_State *other_actor, int32_t sound_key)
Definition spatial.c:110
uint8_t Audio_RegisterCollisionSoundEvent(struct Actor_State *actor, struct Actor_State *other_actor, int32_t sound_key, uint32_t packed_impact_cooldown)
Definition spatial.c:135
uint8_t Audio_CalculateSpatialVolumeAndPan(struct Math_Vec3I32 *sound_position, uint32_t audible_radius, int32_t *out_volume_q12, int16_t *out_pan_angle)
Definition spatial.c:240
int32_t Audio_ResolveActorSoundIndex(struct Pkg_ActorTemplate const *actor_template, int32_t encoded_sound_selector)
Definition spatial.c:37
static int32_t Math_DivideSignedWordPair(int32_t high, uint32_t low, int32_t divisor)
Definition spatial.c:25
void Audio_PlayLevelSoundIndexAtPosition(int32_t sound_index, struct Math_Vec3I32 *position)
Definition spatial.c:52
BOOL Audio_UpdateSoundPosition(int32_t slot_index)
Definition spatial.c:215
int32_t Audio_PlaySound3D(struct Audio_SoundDefinition *sound_def, struct Math_Vec3I32 *position)
Definition spatial.c:176
void Audio_StopOrUpdateSoundsAtPosition(int32_t sound_index_or_mode, struct Math_Vec3I32 *position, char looping_only)
Definition spatial.c:76
int32_t behavior_flags
Definition types.h:407
void * record_ptr
Definition types.h:477
struct Actor_State * other_actor
Definition types.h:155
struct Math_Vec3I32 impact_velocity
Definition types.h:157
struct Math_Vec3I32 velocity
Definition types.h:156
void ** sample_table_ptr
Definition types.h:67
struct Audio_SoundDefinition * sound_list
Definition types.h:46
int32_t y
Definition types.h:471
int32_t x
Definition types.h:470
int32_t z
Definition types.h:472
int16_t sound_index_base
Definition types.h:166
struct Audio_CollisionSoundCooldownEntry collision_sound_cooldowns[0x4]
Definition types.h:197
uint16_t reaction_animation_ticks
Definition types.h:136
#define g_frameCounter
Definition values.h:501