102 Dalmatians: Puppies to the Rescue
Decompilation Source Reference | GitLab repository
Loading...
Searching...
No Matches
audio_commands.c
Go to the documentation of this file.
1/* Recovered game implementation; original addresses identify provenance. */
10#include "pcdogs/audio/types.h"
11#include "pcdogs/entity/types.h"
12#include "pcdogs/level/types.h"
17#include <math.h>
18#include <stdarg.h>
19#include <string.h>
20
21// Original pcdogs.exe: 0x410d50
22// Resolves a level, actor-template, or global sound selector and plays it at the current entity's position, restoring the level sound-list pointer afterward.
23void Script_OpPlaySoundIndex(struct Entity *entity, uint8_t **script_cursor_inout)
24{
25 (void)entity;
26 int32_t sound_selector = 0;
27 uint8_t *operand_cursor = &(*script_cursor_inout)[1];
28
29 *script_cursor_inout = operand_cursor;
30
31 struct Level_Data *current_level = pkg_resource_current_level_data;
32 struct Audio_SoundDefinition *saved_sound_list = current_level->sound_list;
33
34 sound_selector = (uint8_t)(operand_cursor[-1]);
35
36 if (sound_selector >= SCRIPT_GLOBAL_SOUND_BASE)
37 {
38 if (sound_selector < SCRIPT_ACTOR_SOUND_BASE)
39 {
40 sound_selector = Audio_ResolveActorSoundIndex(
41 (const struct Pkg_ActorTemplate *)g_scriptCurrentEntity->actor_template_header, sound_selector);
42
43 if (sound_selector < 0)
44 return;
45 }
46 else
47 {
48 current_level->sound_list = g_soundDefinitionTable;
49 sound_selector -= SCRIPT_ACTOR_SOUND_BASE;
50 }
51 }
52
54 pkg_resource_current_level_data->sound_list = saved_sound_list;
55}
56
57// Original pcdogs.exe: 0x410dc0
58// Resolves a sound selector and stops or updates matching sounds at the current entity's position. Selector 0xff passes -1, and the temporary global sound-list substitution is restored.
59void Script_OpStopSound(struct Entity *entity, uint8_t **script_cursor_inout)
60{
61 (void)entity;
62 int32_t sound_selector = 0;
63 uint8_t *operand_cursor = (*script_cursor_inout + 1);
64
65 *script_cursor_inout = operand_cursor;
66
67 struct Level_Data *current_level = pkg_resource_current_level_data;
68 struct Audio_SoundDefinition *saved_sound_list = current_level->sound_list;
69
70 sound_selector = (uint8_t)(operand_cursor[-1]);
71
72 if (sound_selector == SCRIPT_SOUND_NONE)
73 sound_selector = -1;
74 else if (sound_selector < SCRIPT_ACTOR_SOUND_BASE)
75 {
76 sound_selector = Audio_ResolveActorSoundIndex(
77 (const struct Pkg_ActorTemplate *)(g_scriptCurrentEntity->actor_template_header), sound_selector);
78
79 if (sound_selector < 0)
80 return;
81 }
82 else
83 {
84 current_level->sound_list = g_soundDefinitionTable;
85 sound_selector -= SCRIPT_ACTOR_SOUND_BASE;
86 }
87
88 Audio_StopOrUpdateSoundsAtPosition(sound_selector, g_scriptCurrentEntity->position_ptr, 0);
89 pkg_resource_current_level_data->sound_list = saved_sound_list;
90}
91
92// Original pcdogs.exe: 0x4111b0
93// Decodes a music transition identifier, volume, mode, and optional Q12 timing values, marks the animation timer active, and requests the transition.
94void Script_OpTriggerMusicTransition(struct Entity *entity, uint8_t **script_cursor_inout)
95{
96 (void)entity;
98
99 uint8_t *operand_cursor = (*script_cursor_inout + 1);
100
101 *script_cursor_inout = (operand_cursor);
102
103 uint8_t music_transition_id = operand_cursor[-1];
104
105 *script_cursor_inout = (&operand_cursor[4]);
106
107 int32_t volume_percent = (uint32_t)(*(uint8_t *)operand_cursor) << 24;
108
109 volume_percent |= (uint32_t)(operand_cursor[1]) << 16;
110 volume_percent |= (uint32_t)(operand_cursor[2]) << 8;
111 volume_percent |= operand_cursor[3];
112 *script_cursor_inout = (&operand_cursor[5]);
113
114 uint8_t mode_byte = operand_cursor[4];
115 struct Level_TransitionTimingData music_transition_timing;
116
117 if (!mode_byte)
118 {
119 *script_cursor_inout = (&operand_cursor[0x11]);
120 __builtin_memset(&music_transition_timing, 0, sizeof(music_transition_timing));
121 Audio_TriggerMusicTransition((uint32_t)music_transition_id, (uint32_t)mode_byte,
122 &music_transition_timing, volume_percent);
123
124 return;
125 }
126
127 *script_cursor_inout = (&operand_cursor[9]);
128
129 int32_t start_delay = (uint32_t)(operand_cursor[5]) << 24;
130
131 start_delay |= (uint32_t)(operand_cursor[7]) << 8;
132 start_delay |= (uint32_t)(operand_cursor[6]) << 16;
133 start_delay |= operand_cursor[8];
134 *script_cursor_inout = (&operand_cursor[0xd]);
135
136 // Mode 0 passes zeroed music params while preserving leading duration/timing operand.
137 music_transition_timing.start_delay_fp12 = start_delay;
138
139 int32_t mid_duration = (uint32_t)(operand_cursor[0xb]) << 8;
140
141 mid_duration |= (uint32_t)(operand_cursor[9]) << 24;
142 mid_duration |= (uint32_t)(operand_cursor[0xa]) << 16;
143 mid_duration |= operand_cursor[0xc];
144
145 music_transition_timing.mid_duration_fp12 = mid_duration;
146 *script_cursor_inout = (&operand_cursor[0x11]);
147
148 int32_t end_duration = (uint32_t)(operand_cursor[0xd]) << 24;
149
150 end_duration |= (uint32_t)(operand_cursor[0xf]) << 8;
151 end_duration |= (uint32_t)(operand_cursor[0xe]) << 16;
152 end_duration |= operand_cursor[0x10];
153 music_transition_timing.end_duration_fp12 = end_duration;
154 Audio_TriggerMusicTransition((uint32_t)music_transition_id, (uint32_t)mode_byte,
155 &music_transition_timing, volume_percent);
156}
157
158// Original pcdogs.exe: 0x4112c0
159// Consumes music-fade operands and applies the converted volume and duration only when the enable byte is zero. Volume scaling and duration conversion retain the original integer wrap and truncation.
160void Script_OpTriggerMusicFade(struct Entity *entity, uint8_t **script_cursor_inout)
161{
162 (void)entity;
163 uint8_t *operand_cursor = (*script_cursor_inout + 1);
164
165 *script_cursor_inout = operand_cursor;
166
167 char enable_flag = operand_cursor[-1];
168
169 *script_cursor_inout = &operand_cursor[4];
170
171 int32_t volume_percent = (uint32_t)(*(uint8_t *)operand_cursor) << 24;
172
173 volume_percent |= (uint32_t)(operand_cursor[2]) << 8;
174 // Main animation state machine for player/AI actors. Transitions between:
175 // - 0: Idle
176 // - 2-3: Walk/Run (based on speed threshold 0x6c1)
177 // - 4: Landing (from jump)
178 // - 5: Falling
179 // - 6: Death
180 // - 7: Pushing/Interacting
181 // - 8-9: Jump states
182 // - 0x12: Bark (if bone trail available)
183 // - 0x13: Alt bark (flag 0x40)
184 // - 0x15-0x16: Inactive idle/walk
185 // Checks input state at g_currentInputState for button 0x40 (bark). Animation progress checked at 0xfff
186 // (100%) for completion.
187 volume_percent |= (uint32_t)(operand_cursor[1]) << 16;
188 volume_percent |= operand_cursor[3];
189 *script_cursor_inout = &operand_cursor[5];
190
191 int32_t fade_duration_raw;
192
193 if (operand_cursor[4])
194 {
195 *script_cursor_inout = &operand_cursor[9];
196
197 int32_t fade_duration = (uint32_t)(operand_cursor[5]) << 24;
198
199 fade_duration |= (uint32_t)(operand_cursor[7]) << 8;
200 fade_duration |= (uint32_t)(operand_cursor[6]) << 16;
201 fade_duration |= operand_cursor[8];
202 fade_duration_raw = fade_duration;
203 }
204 else
205 {
206 *script_cursor_inout = &operand_cursor[9];
207 fade_duration_raw = 0;
208 }
209
210 if (!enable_flag)
212 (int16_t)((fade_duration_raw * SCRIPT_FRAMES_PER_SECOND) >> MATH_Q12_SHIFT));
213}
214
215// Original pcdogs.exe: 0x411360
216// Starts a selected positional sound and branches to the wait target while it remains active, restoring the prior sound-list pointer on each playback path. The started latch is shared by all emitters using the sound definition, while the live-sound lookup also requires the current position pointer. When no matching sound remains, it clears that shared latch and continues.
217void Script_OpPlaySoundBlockOrWait(struct Entity *entity, uint8_t **script_cursor_inout)
218{
219 (void)entity;
220 uint32_t block_delta = 0; // Skip 2-byte offset field to get to sound index byte
221 int32_t sound_selector_raw = 0; // Update IP to point after offset field
222 uint8_t *operand_cursor = (*script_cursor_inout + 2);
223
224 *script_cursor_inout = operand_cursor; // Read 2-byte offset value (for conditional branching)
225 // Update IP to skip sound index byte - now points to next instruction
226 block_delta = (uint32_t)(operand_cursor[-2]) << 8;
227 block_delta |= operand_cursor[-1];
228
229 uint8_t *block_end = &operand_cursor[block_delta];
230
231 // Read sound index byte (0-255). Will be used to look up sound definition.
232 *script_cursor_inout = &operand_cursor[1];
233
234 // Get level sound table pointer (offset +0x10 from level base)
235 struct Level_Data *current_level = pkg_resource_current_level_data;
236
237 // Check if sound index > 99. If so, it's either actor state sound (100-199) or global sound (200+).
238 sound_selector_raw = (uint8_t)(*(uint8_t *)operand_cursor);
239
240 // Check if index < 200 (actor state sound range: 100-199)
241 struct Audio_SoundEntry *saved_sound_list = (struct Audio_SoundEntry *)(current_level->sound_list);
242 int32_t sound_selector = sound_selector_raw;
243
244 // Get actor state-specific sound index. Actor state at offset +0x4c.
245 if (sound_selector >= SCRIPT_GLOBAL_SOUND_BASE)
246 {
247 // Sound index 0-99: level sounds, 100-199: actor state, 200+: global
248 if (sound_selector < SCRIPT_ACTOR_SOUND_BASE)
249 {
250 // Switch sound table to global sound table at 0x634b14
251 // Get sound flags byte from sound definition at offset +4 (each entry is 0x14/20 bytes)
252 sound_selector = Audio_ResolveActorSoundIndex((const struct Pkg_ActorTemplate *)(g_scriptCurrentEntity->actor_template_header),
253 sound_selector);
254
255 if (sound_selector < 0)
256 return;
257 }
258 else
259 {
260 sound_selector -= SCRIPT_ACTOR_SOUND_BASE;
261 current_level->sound_list = g_soundDefinitionTable;
262 }
263 }
264
265 // Check bit 6 (0x40) - if set, prevent duplicate playback of this sound
266 struct Audio_SoundDefinition *sound_list = pkg_resource_current_level_data->sound_list;
267 // Search for this sound type in active playback list. Actor position at offset +0xc0.
268 uint8_t sound_flags = sound_list[sound_selector].flags;
269 struct Audio_SoundDefinition *sound_def = &sound_list[sound_selector];
270
271 if (!(sound_flags & AUDIO_SOUND_PROTECT_FROM_REPLACEMENT))
272 {
273 // Flag 0x40: prevent duplicate sound instances
274 // If sound not found (not already playing), clear the 0x40 flag and restore original sound table, then return
275 sound_def->flags = sound_flags | AUDIO_SOUND_PROTECT_FROM_REPLACEMENT;
276 // Clear bit 6 (0x40) mask with 0xbf (10111111) to allow future playback
277 Audio_PlaySound3D(sound_def, g_scriptCurrentEntity->position_ptr);
278 // Sound already playing - skip forward in bytecode by offset amount (conditional branch)
279 *script_cursor_inout = block_end;
280 // Not a duplicate-prevention sound, or not currently playing. Set bit 6 to mark as playing.
281 pkg_resource_current_level_data->sound_list = (struct Audio_SoundDefinition *)(saved_sound_list);
282
283 return;
284 }
285
287 {
288 *script_cursor_inout = block_end;
289 pkg_resource_current_level_data->sound_list = (struct Audio_SoundDefinition *)(saved_sound_list);
290
291 return;
292 }
293
294 sound_def->flags &= (uint8_t)~AUDIO_SOUND_PROTECT_FROM_REPLACEMENT;
295 pkg_resource_current_level_data->sound_list = (struct Audio_SoundDefinition *)(saved_sound_list);
296}
#define g_animationTimerState
Definition globals.h:22
#define AUDIO_INVALID_SOUND_INDEX
Definition constants.h:49
#define AUDIO_SOUND_PROTECT_FROM_REPLACEMENT
Definition constants.h:12
int32_t Audio_ResolveActorSoundIndex(struct Pkg_ActorTemplate const *actor_template, int32_t encoded_sound_selector)
Definition spatial.c:37
int32_t Audio_SetMusicFadeTarget(int32_t target_volume, int16_t frame_count)
Definition music.c:194
int32_t Audio_FindSoundByType(int32_t slot_index, struct Audio_SoundDefinition *sound_def, struct Math_Vec3I32 *position)
Definition channels.c:214
void Audio_PlayLevelSoundIndexAtPosition(int32_t sound_index, struct Math_Vec3I32 *position)
Definition spatial.c:52
void Audio_TriggerMusicTransition(int32_t track_index, int32_t transition_mode, struct Level_TransitionTimingData const *transition_timing, int32_t volume_percent)
Definition music.c:22
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
#define g_soundDefinitionTable
Definition globals.h:54
void Script_OpStopSound(struct Entity *entity, uint8_t **script_cursor_inout)
void Script_OpPlaySoundIndex(struct Entity *entity, uint8_t **script_cursor_inout)
void Script_OpPlaySoundBlockOrWait(struct Entity *entity, uint8_t **script_cursor_inout)
void Script_OpTriggerMusicTransition(struct Entity *entity, uint8_t **script_cursor_inout)
void Script_OpTriggerMusicFade(struct Entity *entity, uint8_t **script_cursor_inout)
#define MATH_Q12_SHIFT
Definition constants.h:8
#define pkg_resource_current_level_data
Definition globals.h:117
#define SCRIPT_FRAMES_PER_SECOND
Definition constants.h:71
#define SCRIPT_PERCENT_MAX
Definition constants.h:72
#define SCRIPT_ACTOR_SOUND_BASE
Definition constants.h:76
#define SCRIPT_GLOBAL_SOUND_BASE
Definition constants.h:75
@ SCRIPT_SOUND_NONE
Definition constants.h:103
#define g_scriptCurrentEntity
Definition globals.h:32
Definition types.h:22
struct Audio_SoundDefinition * sound_list
Definition types.h:46