Debian Codegraph @

prboom2/src/dsda/ambient.cpp

raw
1 //
2 // Copyright(C) 2023 by Ryan Krafnick
3 //
4 // This program is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU General Public License
6 // as published by the Free Software Foundation; either version 2
7 // of the License, or (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // DESCRIPTION:
15 // DSDA Ambient
16 //
17
18 #include <string>
19 #include <cstring>
20 #include <unordered_map>
21
22 extern "C" {
23 #include "m_random.h"
24 #include "lprintf.h"
25 #include "p_tick.h"
26 #include "s_sound.h"
27 #include "sounds.h"
28 #include "w_wad.h"
29 #include "z_zone.h"
30
31 #include "dsda/sfx.h"
32 }
33
34 #include "scanner.h"
35
36 #include "ambient.h"
37
38 typedef struct {
39 char* lump_name;
40 int sfx_id;
41 } named_sfx_t;
42
43 std::unordered_map<std::string, named_sfx_t> name_to_sfx;
44 std::unordered_map<int, ambient_sfx_t> id_to_ambient_sfx;
45
46 dboolean dsda_IsLoopingAmbientSFX(int sfx_id) {
47 for (auto &amb_sfx : id_to_ambient_sfx) {
48 if (amb_sfx.second.sfx_id == sfx_id && amb_sfx.second.min_tics < 0)
49 return true;
50 }
51
52 return false;
53 }
54
55 static ambient_sfx_t* dsda_AmbientSFX(int id) {
56 return id_to_ambient_sfx[id].sfx_id ? &id_to_ambient_sfx[id] : NULL;
57 }
58
59 static int dsda_AmbientWaitTime(ambient_sfx_t* amb_sfx) {
60 return amb_sfx->min_tics + M_Random() * (amb_sfx->max_tics - amb_sfx->min_tics) / 255;
61 }
62
63 void dsda_UpdateAmbientSource(ambient_source_t* source) {
64 if (source->wait_tics > 0) {
65 --source->wait_tics;
66 return;
67 }
68
69 // looping
70 if (source->data.min_tics < 0) {
71 source->wait_tics = 35;
72
73 if (!source->data.attenuation) {
74 S_AdjustVolume(source->data.volume);
75 S_LoopVoidSound(source->data.sfx_id, source->wait_tics + 1);
76 S_ResetAdjustments();
77 }
78 else {
79 S_AdjustAttenuation(source->data.attenuation);
80 S_AdjustVolume(source->data.volume);
81 S_LoopMobjSound(source->mobj, source->data.sfx_id, source->wait_tics + 1);
82 S_ResetAdjustments();
83 }
84 }
85 else {
86 source->wait_tics = dsda_AmbientWaitTime(&source->data);
87
88 if (!source->data.attenuation) {
89 S_AdjustVolume(source->data.volume);
90 S_StartVoidSound(source->data.sfx_id);
91 S_ResetAdjustments();
92 }
93 else {
94 S_AdjustAttenuation(source->data.attenuation);
95 S_AdjustVolume(source->data.volume);
96 S_StartMobjSound(source->mobj, source->data.sfx_id);
97 S_ResetAdjustments();
98 }
99 }
100 }
101
102 void dsda_SpawnAmbientSource(mobj_t* mobj) {
103 ambient_source_t* source;
104 ambient_sfx_t* data;
105
106 data = dsda_AmbientSFX(mobj->iden_nums);
107 if (!data)
108 return;
109
110 source = (ambient_source_t*) Z_MallocLevel(sizeof(*source));
111 source->mobj = NULL;
112 P_SetTarget(&source->mobj, mobj);
113 source->data = *data;
114 source->data.sound_name = NULL;
115 source->wait_tics = dsda_AmbientWaitTime(data);
116 source->thinker.function = (think_t) dsda_UpdateAmbientSource;
117 P_AddThinker(&source->thinker);
118 }
119
120 static void dsda_ParseAmbient(Scanner &scanner) {
121 ambient_sfx_t amb_sfx = { 0 };
122 int id;
123
124 scanner.MustGetInteger();
125 id = scanner.number;
126
127 scanner.MustGetString();
128 amb_sfx.sound_name = Z_Strdup(scanner.string);
129
130 scanner.MustGetString();
131 if (scanner.StringMatch("point")) {
132 amb_sfx.attenuation = scanner.CheckFloat() ? scanner.decimal : 1.0;
133 scanner.MustGetString();
134 }
135 else if (scanner.StringMatch("world") || scanner.StringMatch("surround")) {
136 amb_sfx.attenuation = 0;
137 scanner.MustGetString();
138 }
139 else {
140 amb_sfx.attenuation = 0;
141 }
142
143 if (scanner.StringMatch("continuous")) {
144 amb_sfx.min_tics = -1;
145 amb_sfx.max_tics = -1;
146 }
147 else if (scanner.StringMatch("random")) {
148 scanner.MustGetFloat();
149 amb_sfx.min_tics = 35 * scanner.decimal;
150 scanner.MustGetFloat();
151 amb_sfx.max_tics = 35 * scanner.decimal;
152 }
153 else if (scanner.StringMatch("periodic")) {
154 scanner.MustGetFloat();
155 amb_sfx.min_tics = 35 * scanner.decimal;
156 amb_sfx.max_tics = amb_sfx.min_tics;
157 }
158
159 scanner.MustGetFloat();
160 amb_sfx.volume = BETWEEN(0.0, 1.0, scanner.decimal);
161 if (amb_sfx.attenuation < 0)
162 amb_sfx.attenuation = 1;
163
164 if (!amb_sfx.min_tics && !amb_sfx.max_tics) {
165 lprintf(LO_WARN, "Ambient sound %d has invalid parameters\n", id);
166 Z_Free(amb_sfx.sound_name);
167 return;
168 }
169
170 if (id_to_ambient_sfx[id].sound_name)
171 Z_Free(id_to_ambient_sfx[id].sound_name);
172
173 id_to_ambient_sfx[id] = amb_sfx;
174 }
175
176 static void dsda_ParseSndInfoLine(Scanner &scanner) {
177 if (!scanner.CheckString()) {
178 scanner.GetNextToken();
179 scanner.SkipLine();
180 return;
181 }
182
183 if (!stricmp(scanner.string, "$ambient"))
184 dsda_ParseAmbient(scanner);
185 else if (scanner.string[0] == '$') {
186 scanner.SkipLine();
187 return;
188 }
189 else {
190 std::string name(scanner.string);
191
192 scanner.CheckToken('='); // Optional
193
194 if (!scanner.CheckString()) {
195 lprintf(LO_WARN, "Invalid SNDINFO: name \"%s\" expects string sound lump\n", name.c_str());
196
197 scanner.GetNextToken();
198 scanner.SkipLine();
199 return;
200 }
201
202 if (!W_LumpNameExists(scanner.string)) {
203 lprintf(LO_WARN, "Sound lump \"%s\" does not exist\n", scanner.string);
204 return;
205 }
206
207 if (name_to_sfx[name].lump_name)
208 Z_Free(name_to_sfx[name].lump_name);
209
210 name_to_sfx[name].lump_name = Z_Strdup(scanner.string);
211 }
212 }
213
214 static void dsda_ResolveAmbientSounds(void) {
215 for (auto &named_sfx : name_to_sfx) {
216 sfxinfo_t* new_sfx;
217 int id;
218
219 new_sfx = dsda_NewSFX(&id);
220 new_sfx->name = named_sfx.second.lump_name;
221 named_sfx.second.sfx_id = id;
222
223 lprintf(LO_DEBUG, "Named sound: %s -> %s %d\n", named_sfx.first.c_str(), new_sfx->name, id);
224 }
225
226 for (auto &amb_sfx : id_to_ambient_sfx) {
227 amb_sfx.second.sfx_id = name_to_sfx[amb_sfx.second.sound_name].sfx_id;
228
229 if (!amb_sfx.second.sfx_id)
230 lprintf(LO_WARN, "Sound \"%s\" does not exist\n", amb_sfx.second.sound_name);
231
232 lprintf(LO_DEBUG, "Ambient sound: %d att: %f, vol: %f, t: %d %d, sfx: %d\n",
233 amb_sfx.first,
234 amb_sfx.second.attenuation,
235 amb_sfx.second.volume,
236 amb_sfx.second.min_tics,
237 amb_sfx.second.max_tics,
238 amb_sfx.second.sfx_id);
239 }
240 }
241
242 void dsda_LoadAmbientSndInfo(void) {
243 int lump;
244
245 lump = W_CheckNumForName("SNDINFO");
246
247 if (lump == LUMP_NOT_FOUND)
248 return;
249
250 Scanner scanner((const char*) W_LumpByNum(lump), W_LumpLength(lump));
251
252 scanner.SetErrorCallback(I_Error);
253
254 while (scanner.TokensLeft())
255 dsda_ParseSndInfoLine(scanner);
256
257 dsda_ResolveAmbientSounds();
258 }