-
Notifications
You must be signed in to change notification settings - Fork 10
/
Sfx.cs
443 lines (381 loc) · 16.1 KB
/
Sfx.cs
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Terraria;
namespace Prism.API.Audio
{
public delegate bool PlaySoundEvent(SfxEntry entry, Vector2 position, ref int variant, ref float volume, ref float pitch, ref float pan);
public static partial class Sfx
{
const string OBS_REASON = "Please use an overload that takes an SfxEntry instead.";
internal static Dictionary<string, SfxEntry> VanillaDict = new Dictionary<string, SfxEntry>();
static Dictionary<KeyValuePair<SfxEntry, int>, SoundEffectInstance> instanceMap = new Dictionary<KeyValuePair<SfxEntry, int>, SoundEffectInstance>();
static List<KeyValuePair<SfxEntry, int>> toR = new List<KeyValuePair<SfxEntry, int>>();
internal static void UpdateInstances()
{
if ((!Main.hasFocus || Main.gamePaused) && Main.netMode == 0)
{
foreach (var i in Main._trackedSounds)
i.Value.Pause();
Main._areSoundsPaused = true;
}
else if (Main._areSoundsPaused)
{
foreach (var i in Main._trackedSounds)
i.Value.Play();
Main._areSoundsPaused = false;
}
else
{
foreach (var i in Main._trackedSounds)
i.Value.Update();
CleanupLingeringInstances();
}
}
static Tuple<int, float, float, float> CalcParams(SfxEntry e, Vector2 pos, int v, PlaySoundEvent onPlay)
{
var volSetting = e.IsAmbient ? (Main.gameInactive ? 0f : Main.ambientVolume) : Main.soundVolume;
if (volSetting <= 0f)
return null;
bool shouldPlay = true;
float
vol = 1f, pitch = 0f, pan = 0f;
if (pos.X <= 0f || pos.Y <= 0f || Single.IsNaN(pos.X) || Single.IsNaN(pos.Y))
shouldPlay = true;
else
{
#pragma warning disable RECS0091 // Use 'var' keyword when possible
// to the code analyser: learn to C#
Rectangle
#pragma warning restore RECS0091
screen = new Rectangle((int)(Main.screenPosition.X - Main.screenWidth * 2), (int)(Main.screenPosition.Y - Main.screenHeight * 2), Main.screenWidth * 5, Main.screenHeight * 5),
sound = new Rectangle((int)pos.X, (int)pos.Y, 1, 1);
shouldPlay |= sound.Intersects(screen);
if (shouldPlay)
{
var screenCentre = new Vector2(Main.screenPosition.X + Main.screenWidth * 0.5f, Main.screenPosition.Y + Main.screenHeight * 0.5f);
pan = (pos.X - screenCentre.X) / (Main.screenWidth * 0.5f);
vol = 1f - Vector2.Distance(pos, screenCentre) / (Main.screenWidth * 1.5f);
}
}
pan = MathHelper.Clamp(pan, -1f, 1f);
vol = Math.Min(vol * volSetting, 1f);
if (vol <= 0f)
return null;
if (onPlay != null && !onPlay(e, pos, ref v, ref vol, ref pitch, ref pan))
return null;
vol = MathHelper.Clamp(vol , 0f, 1f);
pitch = MathHelper.Clamp(pitch, -1f, 1f);
pan = MathHelper.Clamp(pan , -1f, 1f);
return Tuple.Create(v, vol, pitch, pan);
}
static void ApplyParams(SoundEffectInstance inst, Tuple<int, float, float, float> t)
{
inst.Volume = t.Item2;
inst.Pitch = t.Item3;
inst.Pan = t.Item4;
}
static void CleanupLingeringInstances()
{
SoundEffectInstance inst;
for (int i = 0; i < Main.ActiveSoundInstances.Count; i++)
{
inst = Main.ActiveSoundInstances[i];
if (inst == null || inst.IsDisposed || inst.State == SoundState.Stopped)
{
if (inst != null && !inst.IsDisposed)
inst.Dispose();
Main.ActiveSoundInstances.RemoveAt(i--);
}
}
toR.Clear();
foreach (var kvp in instanceMap)
{
inst = kvp.Value;
if (inst == null || inst.IsDisposed || inst.State == SoundState.Stopped)
{
if (inst != null && !inst.IsDisposed)
inst.Dispose();
toR.Add(kvp.Key);
}
}
for (int i = 0; i < toR.Count; i++)
instanceMap.Remove(toR[i]);
toR.Clear();
foreach (var i in Main._trackedSounds)
if (!i.Value.IsPlaying)
Main._trackedSounds.Remove(i.Id);
}
public static SoundEffectInstance Play(SfxEntry entry, Vector2 position, int variant, PlaySoundEvent onPlay)
{
if (Main.dedServ || WorldGen.gen || Main.netMode == 2)
return null;
if (entry.Variants < 2)
variant = 0;
else if (variant == -1)
variant = entry.Variants < 2 ? 0 : Main.rand.Next(entry.Variants);
var kvp = new KeyValuePair<SfxEntry, int>(entry, variant);
var t = CalcParams(entry, position, variant, onPlay);
if (t == null || t.Item2 <= 0f)
return null;
SoundEffectInstance inst;
var b = entry.PlayBehaviour(variant);
SoundEffectInstance inst_;
switch (b)
{
case SfxPlayBehaviour.MultipleInstances:
inst = entry.GetInstance(variant);
if (inst == null)
return null;
Main.ActiveSoundInstances.Add(inst); // don't GC
break;
case SfxPlayBehaviour.PlayIfStopped:
case SfxPlayBehaviour.PlayIfStoppedUpdateParams:
if (instanceMap.TryGetValue(kvp, out inst_))
{
if (inst_.State == SoundState.Stopped)
inst = inst_;
else
{
if (b == SfxPlayBehaviour.PlayIfStoppedUpdateParams)
ApplyParams(inst_, t);
return null;
}
}
else
{
inst = entry.GetInstance(variant);
if (inst == null)
return null;
instanceMap.Add(kvp, inst);
}
break;
case SfxPlayBehaviour.Singleton:
if (instanceMap.TryGetValue(kvp, out inst_))
{
inst_.Stop();
instanceMap.Remove(kvp);
}
inst = entry.GetInstance(variant);
if (inst == null)
return null;
instanceMap.Add(kvp, inst);
break;
// required, compiler will complain about 'inst' not being assigned to otherwise
// and this is more foolproof than setting 'inst' to null.
default:
return null;
}
ApplyParams(inst, t);
inst.Play(); // !
Main.ActiveSoundInstances.Add(inst);
//CleanupLingeringInstances();
return inst;
}
public static SoundEffectInstance Play(SfxEntry entry, Vector2 position, int variant, float volMod = Single.NaN, float pitch = Single.NaN, float panMod = Single.NaN)
{
return Play(entry, position, variant, (SfxEntry e, Vector2 p, ref int v, ref float vol, ref float pi, ref float pan) =>
{
if (!Single.IsNaN(volMod))
vol *= volMod;
if (!Single.IsNaN(pitch ))
pi = pitch;
if (!Single.IsNaN(panMod))
pan *= panMod;
return true;
});
}
public static SoundEffectInstance Play(SfxEntry entry, Vector2 position)
{
return Play(entry, position, -1, null);
}
public static SoundEffectInstance Play(SfxEntry entry, Point tilePos, int variant, PlaySoundEvent onPlay)
{
return Play(entry, tilePos.ToVector2() * 16f, variant, onPlay);
}
public static SoundEffectInstance Play(SfxEntry entry, Point tilePos, int variant, float volMod = Single.NaN, float pitch = Single.NaN, float panMod = Single.NaN)
{
return Play(entry, tilePos.ToVector2() * 16f, variant, (SfxEntry e, Vector2 p, ref int v, ref float vol, ref float pi, ref float pan) =>
{
if (!Single.IsNaN(vol))
vol *= volMod;
if (!Single.IsNaN(pi))
pi = pitch;
if (!Single.IsNaN(pan))
pan *= panMod;
return true;
});
}
public static SoundEffectInstance Play(SfxEntry entry, Point tilePos)
{
return Play(entry, tilePos.ToVector2() * 16f, -1, null);
}
[Obsolete(OBS_REASON)]
public static SoundEffectInstance Play(int type, Vector2 position, int style = -1, float vol_ = 1, float pitch_ = 0)
{
return Play(ById(type), position, style, (SfxEntry e, Vector2 p, ref int v, ref float vol, ref float pitch, ref float pan) =>
{
vol *= vol_ ;
pitch += pitch_;
switch (type)
{
case 0: // DigBlock
pitch = Main.rand.Next(-10, 11) * 0.01f;
break;
case 1: // PlayerHit
break;
case 2:
switch (v)
{
case 37:
case 123:
vol *= 0.5f;
break;
case 124:
case 125:
vol *= 0.65f;
break;
case 1:
var r = Main.rand.Next(3);
if (r > 0)
v = 17 + r;
break;
case 53:
case 55:
vol *= 0.75f;
if (v == 55)
vol *= 0.75f;
break;
}
switch (v)
{
case 47:
pitch = Main.rand.Next(-5, 6) * 0.19f;
break;
case 53:
pitch = Main.rand.Next(-20, -11) * 0.02f;
break;
case 55:
pitch = Main.rand.Next(10, 21) * 0.02f;
break;
case 26:
case 35:
vol *= 0.75f;
pitch = Main.harpNote;
break;
default:
pitch = Main.rand.Next(-6, 7) * 0.01f;
break;
}
break;
case 3:
if (v >= 20 && v <= 55)
vol *= 0.5f;
if (v == 57)
vol *= 0.6f;
pitch = Main.rand.Next(-10, 11) * 0.01f;
break;
case 4:
if (v >= 23 && v <= 57)
vol *= 0.5f;
if (v == 61 || v == 62)
vol *= 0.6f;
pitch = Main.rand.Next(-10, 11) * 0.01f;
break;
case 6:
pitch = Main.rand.Next(-30, 31) * 0.01f;
break;
case 7:
case 16:
case 17:
case 19:
case 27:
case 28:
pitch = Main.rand.Next(-10, 11) * 0.01f;
break;
case 8:
case 9:
pitch = Main.rand.Next(-20, 21) * 0.01f;
break;
case 14:
vol *= 0.4f;
break;
case 26:
pitch = Main.rand.Next(-10, 11) * 0.01f;
vol *= 0.9f;
break;
case 29:
if (v == 100 || v == 101)
vol *= 0.25f;
if ((v >= 93 && v <= 99) || v == 103 || v == 102)
vol *= 0.4f;
if ((v >= 24 && v <= 87) || v == 92)
vol *= 0.5f;
if (v == 104)
vol *= 0.55f;
if (v >= 88 && v <= 91)
vol *= 0.7f;
pitch = Main.rand.Next(-10, 11) * 0.01f;
break;
case 30:
vol *= 0.75f;
pitch = (v == 12 ? Main.rand.Next(-40, 21) : Main.rand.Next(-70, 1)) * 0.01f;
break;
case 31:
vol *= 0.35f;
pitch = Main.rand.Next(-40, 21) * 0.01f;
break;
case 32:
vol *= 0.15f;
pitch = Main.rand.Next(-70, 26) * 0.01f;
break;
case 33:
vol *= 0.2f;
pitch = Main.rand.Next(-10, 31) * 0.01f;
break;
case 34:
case 35:
vol *= Math.Min(v / 50f, 1f) * 0.2f; // WTF, red?
if (type == 34)
pitch = -0.2f;
break;
case 36:
if (v <= 0)
pitch += 0.6f;
break;
case 37:
case 38:
if (type == 37)
vol *= v * 0.05f; // WTF, red?
pitch = Main.rand.Next(-40, 41) * 0.01f;
break;
case 39:
vol *= 0.5f;
pitch = Main.rand.Next(-30, 31) * 0.01f;
break;
case 41:
// (dist sound-player + 1)^-1
vol = 1f / (1f + (p - Main.player[Main.myPlayer].position).Length());
pitch = Main.rand.Next(-10, 11) * 0.01f;
break;
case 42:
// nothing special
break;
}
return true;
});
}
[Obsolete(OBS_REASON)]
public static SoundEffectInstance Play(int type, Point tilePos, int style = -1, float vol = 1, float pitch = 0)
{
return Play(type, tilePos.ToVector2() * 16f, style, vol, pitch);
}
[Obsolete(OBS_REASON)]
public static SoundEffectInstance Play(int type, int x = -1, int y = -1, int style = -1, float vol = 1, float pitch = 0)
{
return Play(type, new Vector2(x, y), style, vol, pitch);
}
}
}