This repository was archived by the owner on Feb 24, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 158
Expand file tree
/
Copy pathscript_audio.cpp
More file actions
347 lines (279 loc) · 8.25 KB
/
script_audio.cpp
File metadata and controls
347 lines (279 loc) · 8.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "../core/lua.h"
#include "script.h"
#include "../core/system.h"
#include "../core/console.h"
#include "../audio/audio.h"
#include "../gameflow.h"
#include "../engine.h"
#include "../world.h"
int Script_ParseAudio(lua_State *lua, struct audio_settings_s *as)
{
if(lua)
{
int top = lua_gettop(lua);
lua_getglobal(lua, "audio");
lua_getfield(lua, -1, "music_volume");
as->music_volume = lua_tonumber(lua, -1);
lua_pop(lua, 1);
lua_getfield(lua, -1, "sound_volume");
as->sound_volume = lua_tonumber(lua, -1);
lua_pop(lua, 1);
lua_getfield(lua, -1, "use_effects");
as->use_effects = lua_tointeger(lua, -1);
lua_pop(lua, 1);
lua_getfield(lua, -1, "listener_is_player");
as->listener_is_player = lua_tointeger(lua, -1);
lua_pop(lua, 1);
lua_settop(lua, top);
return 1;
}
return -1;
}
/*
* Specific functions to get specific parameters from script.
*/
int Script_GetGlobalSound(lua_State *lua, int global_sound_id)
{
int sound_id = 0;
if(lua)
{
int top = lua_gettop(lua);
lua_getglobal(lua, "getGlobalSound");
if(lua_isfunction(lua, -1))
{
lua_pushinteger(lua, World_GetVersion());
lua_pushinteger(lua, global_sound_id);
if(lua_CallAndLog(lua, 2, 1, 0))
{
sound_id = lua_tointeger(lua, -1);
}
}
lua_settop(lua, top);
}
return sound_id;
}
int Script_GetSecretTrackNumber(lua_State *lua)
{
lua_Integer track_number = 0;
if(lua)
{
int top = lua_gettop(lua);
lua_getglobal(lua, "getSecretTrackNumber");
if(lua_isfunction(lua, -1))
{
lua_pushinteger(lua, World_GetVersion());
if(lua_CallAndLog(lua, 1, 1, 0))
{
track_number = lua_tointeger(lua, -1);
}
}
lua_settop(lua, top);
}
return (int)track_number;
}
int Script_GetNumTracks(lua_State *lua)
{
lua_Integer num_tracks = 0;
if(lua)
{
int top = lua_gettop(lua);
lua_getglobal(lua, "getNumTracks");
if(lua_isfunction(lua, -1))
{
lua_pushinteger(lua, World_GetVersion());
if(lua_CallAndLog(lua, 1, 1, 0))
{
num_tracks = lua_tointeger(lua, -1);
}
}
lua_settop(lua, top);
}
return (int)num_tracks;
}
bool Script_GetOverridedSamplesInfo(lua_State *lua, int *num_samples, int *num_sounds, char *sample_name_mask)
{
bool result = false;
if(lua)
{
int top = lua_gettop(lua);
lua_getglobal(lua, "getOverridedSamplesInfo");
const char *real_path;
if(lua_isfunction(lua, -1))
{
lua_pushinteger(lua, World_GetVersion());
if(lua_CallAndLog(lua, 1, 3, 0))
{
size_t string_length = 0;
real_path = lua_tolstring(lua, -1, &string_length);
*num_sounds = lua_tointeger(lua, -2);
*num_samples = lua_tointeger(lua, -3);
strncpy(sample_name_mask, real_path, string_length);
result = ((*num_sounds != -1) && (*num_samples != -1) && (strcmp(real_path, "NONE") != 0));
}
}
lua_settop(lua, top);
}
// If Lua environment doesn't exist or script function returned -1 in one of the
// fields, it means that corresponding sample override table is missing or not
// valid - hence, return false.
return result;
}
bool Script_GetOverridedSample(lua_State *lua, int sound_id, int *first_sample_number, int *samples_count)
{
bool result = false;
if(lua)
{
int top = lua_gettop(lua);
lua_getglobal(lua, "getOverridedSample");
if(lua_isfunction(lua, -1))
{
lua_pushinteger(lua, World_GetVersion());
lua_pushinteger(lua, Gameflow_GetCurrentLevelID());
lua_pushinteger(lua, sound_id);
if(lua_CallAndLog(lua, 3, 2, 0))
{
*first_sample_number = (int)lua_tointeger(lua, -2);
*samples_count = (int)lua_tointeger(lua, -1);
result = ((*first_sample_number != -1) && (*samples_count != -1));
}
}
lua_settop(lua, top);
}
return result;
}
bool Script_GetSoundtrack(lua_State *lua, int track_index, char *file_path, int file_path_len, int *load_method, int *stream_type)
{
bool result = false;
if(lua)
{
int top = lua_gettop(lua);
const char *real_path;
lua_getglobal(lua, "getTrackInfo");
if(lua_isfunction(lua, -1))
{
lua_pushinteger(lua, World_GetVersion());
lua_pushinteger(lua, track_index);
if(lua_CallAndLog(lua, 2, 3, 0))
{
size_t string_length = 0;
real_path = lua_tolstring(lua, -3, &string_length);
*stream_type = (int)lua_tointeger(lua, -2);
*load_method = (int)lua_tointeger(lua, -1);
// Lua returns constant string pointer, which we can't assign to
// provided argument; so we need to straightly copy it.
strncpy(file_path, Engine_GetBasePath(), file_path_len);
strncat(file_path, real_path, file_path_len);
result = (*stream_type != -1);
}
}
lua_settop(lua, top);
}
// If Lua wasn't able to extract file path from the script, most likely it means
// that entry is broken or missing, or wrong track ID was specified. So we return
// FALSE in such cases.
return result;
}
/*
* General gameplay functions
*/
int lua_PlayStream(lua_State *lua)
{
int top = lua_gettop(lua);
if(top >= 1)
{
int id = lua_tointeger(lua, 1);
if(id >= 0)
{
uint8_t mask = (top >= 2) ? (lua_tointeger(lua, 2)) : (0);
Audio_StreamPlay(id, mask);
}
else
{
Con_Warning("wrong stream id");
}
}
else
{
Con_Warning("playStream: expecting arguments (stream_id, (mask))");
}
return 0;
}
int lua_PlaySound(lua_State *lua)
{
int top = lua_gettop(lua);
if(top >= 1)
{
uint32_t id = lua_tointeger(lua, 1); // uint_t can't been less zero, reduce number of comparations
int ent_id = -1;
if((top >= 2) && World_GetEntityByID(ent_id = lua_tointeger(lua, 2)) == NULL)
{
ent_id = -1;
}
int result;
if(ent_id >= 0)
{
result = Audio_Send(id, TR_AUDIO_EMITTER_ENTITY, ent_id);
}
else
{
result = Audio_Send(id, TR_AUDIO_EMITTER_GLOBAL);
}
if(result < 0)
{
switch(result)
{
case TR_AUDIO_SEND_NOCHANNEL:
Con_Warning("send ignored: no free channels");
break;
case TR_AUDIO_SEND_NOSAMPLE:
Con_Warning("send ignored: no sample");
break;
}
}
}
else
{
Con_Warning("playSound: expecting arguments (sound_id, (entity_id))");
}
return 0;
}
int lua_StopSound(lua_State *lua)
{
int top = lua_gettop(lua);
if(top >= 1)
{
uint32_t id = lua_tointeger(lua, 1);
int ent_id = -1;
if((top >= 2) && World_GetEntityByID(ent_id = lua_tointeger(lua, 2)) == NULL)
{
ent_id = -1;
}
int result;
if(ent_id == -1)
{
result = Audio_Kill(id, TR_AUDIO_EMITTER_GLOBAL);
}
else
{
result = Audio_Kill(id, TR_AUDIO_EMITTER_ENTITY, ent_id);
}
if(result < 0)
{
Con_Warning("audio with id = %d not played", id);
}
}
else
{
Con_Warning("stopSound: expecting arguments (sound_id, (entity_id))");
}
return 0;
}
void Script_LuaRegisterAudioFuncs(lua_State *lua)
{
lua_register(lua, "playSound", lua_PlaySound);
lua_register(lua, "stopSound", lua_StopSound);
lua_register(lua, "playStream", lua_PlayStream);
}