Debian Codegraph @

prboom2/src/dsda/sfx.c

raw
1 //
2 // Copyright(C) 2021 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 SFX
16 //
17
18 #include <stdlib.h>
19 #include <string.h>
20 #include <ctype.h>
21
22 #include "doomtype.h"
23
24 #include "dsda/configuration.h"
25
26 #include "sfx.h"
27
28 sfxinfo_t* S_sfx;
29 int num_sfx;
30 static int deh_soundnames_size;
31 static char** deh_soundnames;
32 static byte* sfx_state;
33 static int highest_index;
34
35 static void dsda_ResetSFX(int from, int to) {
36 int i;
37
38 for (i = from; i < to; ++i) {
39 S_sfx[i].priority = 127;
40 S_sfx[i].pitch = -1;
41 }
42 }
43
44 static void dsda_PrepAllocation(void) {
45 static int first_allocation = true;
46
47 if (first_allocation) {
48 sfxinfo_t* source = S_sfx;
49
50 first_allocation = false;
51 S_sfx = malloc(num_sfx * sizeof(*S_sfx));
52 memcpy(S_sfx, source, num_sfx * sizeof(*S_sfx));
53 }
54 }
55
56 static void dsda_EnsureCapacity(int limit) {
57 while (limit >= num_sfx) {
58 int old_num_sfx = num_sfx;
59
60 dsda_PrepAllocation();
61
62 num_sfx *= 2;
63
64 S_sfx = realloc(S_sfx, num_sfx * sizeof(*S_sfx));
65 memset(S_sfx + old_num_sfx, 0, (num_sfx - old_num_sfx) * sizeof(*S_sfx));
66
67 if (sfx_state) {
68 sfx_state = realloc(sfx_state, num_sfx * sizeof(*sfx_state));
69 memset(sfx_state + old_num_sfx, 0,
70 (num_sfx - old_num_sfx) * sizeof(*sfx_state));
71 }
72
73 dsda_ResetSFX(old_num_sfx, num_sfx);
74 }
75 }
76
77 int dsda_GetDehSFXIndex(const char* key, size_t length) {
78 int i;
79
80 // offset "ds" for dehacked names
81 for (i = 1; i < num_sfx; ++i)
82 if (
83 S_sfx[i].name &&
84 strlen(S_sfx[i].name + 2) == length &&
85 !strnicmp(S_sfx[i].name + 2, key, length) &&
86 !sfx_state[i]
87 ) {
88 sfx_state[i] = true; // sfx has been edited
89 return i;
90 }
91
92 return -1;
93 }
94
95 int dsda_GetOriginalSFXIndex(const char* key) {
96 int i;
97 const char* c;
98
99 for (i = 1; deh_soundnames[i]; ++i)
100 if (!strncasecmp(deh_soundnames[i], key, 6))
101 return i;
102
103 // is it a number?
104 for (c = key; *c; c++)
105 if (!isdigit(*c))
106 return -1;
107
108 i = atoi(key);
109 dsda_EnsureCapacity(i);
110
111 return i;
112 }
113
114 static sfxinfo_t* dsda_SFXAtIndex(int index) {
115 dsda_EnsureCapacity(index);
116
117 if (index > highest_index)
118 highest_index = index;
119
120 return &S_sfx[index];
121 }
122
123 sfxinfo_t* dsda_GetDehSFX(int index) {
124 return dsda_SFXAtIndex(index);
125 }
126
127 sfxinfo_t* dsda_NewSFX(int* index) {
128 *index = highest_index + 1;
129
130 return dsda_SFXAtIndex(*index);
131 }
132
133 void dsda_InitializeSFX(sfxinfo_t* source, int count) {
134 int i;
135 extern int raven;
136
137 num_sfx = count;
138 highest_index = count - 1;
139 deh_soundnames_size = num_sfx + 1;
140
141 S_sfx = source;
142
143 if (raven) return;
144
145 deh_soundnames = malloc(deh_soundnames_size * sizeof(*deh_soundnames));
146 for (i = 1; i < num_sfx; i++)
147 if (S_sfx[i].name != NULL)
148 deh_soundnames[i] = strdup(S_sfx[i].name + 2); // offset "ds" for dehacked names
149 else
150 deh_soundnames[i] = NULL;
151 deh_soundnames[0] = NULL;
152 deh_soundnames[num_sfx] = NULL;
153
154 sfx_state = calloc(num_sfx, sizeof(*sfx_state));
155 }
156
157 void dsda_FreeDehSFX(void) {
158 int i;
159
160 if (deh_soundnames)
161 for (i = 0; i < deh_soundnames_size; i++)
162 if (deh_soundnames[i])
163 free(deh_soundnames[i]);
164
165 free(deh_soundnames);
166 free(sfx_state);
167
168 deh_soundnames = NULL;
169 sfx_state = NULL;
170 }
171
172 static int dsda_parallel_sfx_limit;
173 static int dsda_parallel_sfx_window;
174
175 void dsda_InitParallelSFXFilter(void) {
176 dsda_parallel_sfx_limit = dsda_IntConfig(dsda_config_parallel_sfx_limit);
177 dsda_parallel_sfx_window = dsda_IntConfig(dsda_config_parallel_sfx_window);
178 }
179
180 dboolean dsda_BlockSFX(sfxinfo_t *sfx) {
181 extern int gametic;
182
183 if (!dsda_parallel_sfx_limit) return false;
184
185 if (gametic - sfx->parallel_tic >= dsda_parallel_sfx_window) {
186 sfx->parallel_tic = gametic;
187 sfx->parallel_count = 0;
188 }
189
190 ++sfx->parallel_count;
191
192 return sfx->parallel_count > dsda_parallel_sfx_limit;
193 }