-
-
Notifications
You must be signed in to change notification settings - Fork 31
/
cl_sound.cpp
305 lines (268 loc) · 8.37 KB
/
cl_sound.cpp
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
/** @file cl_sound.cpp Clientside sounds.
*
* @authors Copyright © 2003-2013 Jaakko Keränen <jaakko.keranen@iki.fi>
* @authors Copyright © 2006-2015 Daniel Swanson <danij@dengine.net>
*
* @par License
* GPL: http://www.gnu.org/licenses/gpl.html
*
* <small>This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version. This program is distributed in the hope that it
* will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details. You should have received a copy of the GNU
* General Public License along with this program; if not, see:
* http://www.gnu.org/licenses</small>
*/
#include "de_base.h"
#include "client/cl_sound.h"
#include "api_client.h"
#include "client/cl_player.h"
#include "api_sound.h"
#include "network/net_msg.h"
#include "world/map.h"
#include "world/p_players.h"
#include "Sector"
#include <de/LogBuffer>
using namespace de;
void Cl_ReadSoundDelta(deltatype_t type)
{
LOG_AS("Cl_ReadSoundDelta");
/// @todo Do not assume the CURRENT map.
world::Map &map = App_World().map();
dint sound = 0, soundFlags = 0;
mobj_t *cmo = 0;
thid_t mobjId = 0;
Sector *sector = 0;
Polyobj *poly = 0;
LineSide *side = 0;
mobj_t *emitter = 0;
duint16 const deltaId = Reader_ReadUInt16(::msgReader);
byte const flags = Reader_ReadByte(::msgReader);
bool skip = false;
if (type == DT_SOUND)
{
// Delta ID is the sound ID.
sound = deltaId;
}
else if (type == DT_MOBJ_SOUND) // Mobj as emitter
{
mobjId = deltaId;
if ((cmo = ClMobj_Find(mobjId)) != nullptr)
{
ClientMobjThinkerData::RemoteSync *info = ClMobj_GetInfo(cmo);
if (info->flags & CLMF_HIDDEN)
{
// We can't play sounds from hidden mobjs, because we
// aren't sure exactly where they are located.
cmo = nullptr;
LOG_NET_VERBOSE("Can't find sound emitter ") << mobjId;
}
else
{
emitter = cmo;
}
}
}
else if (type == DT_SECTOR_SOUND) // Plane as emitter
{
dint index = deltaId;
if (!(sector = map.sectorPtr(index)))
{
LOG_NET_WARNING("Received sound delta has invalid sector index %i") << index;
skip = true;
}
}
else if (type == DT_SIDE_SOUND) // Side section as emitter
{
dint index = deltaId;
if (!(side = map.sidePtr(index)))
{
LOG_NET_WARNING("Received sound delta has invalid side index %i") << index;
skip = true;
}
}
else // DT_POLY_SOUND
{
dint index = deltaId;
LOG_NET_XVERBOSE("DT_POLY_SOUND: poly=%d", index);
if (!(emitter = (mobj_t *) (poly = map.polyobjPtr(index))))
{
LOG_NET_WARNING("Received sound delta has invalid polyobj index %i") << index;
skip = true;
}
}
if (type != DT_SOUND)
{
// The sound ID.
sound = Reader_ReadUInt16(::msgReader);
}
if (type == DT_SECTOR_SOUND && !skip)
{
// Select the emitter for the sound.
if (flags & SNDDF_PLANE_FLOOR)
{
emitter = (mobj_t *) §or->floor().soundEmitter();
}
else if (flags & SNDDF_PLANE_CEILING)
{
emitter = (mobj_t *) §or->ceiling().soundEmitter();
}
else
{
// Must be the sector's sound emitter, then.
emitter = (mobj_t *) §or->soundEmitter();
}
}
if (type == DT_SIDE_SOUND && !skip)
{
if (flags & SNDDF_SIDE_MIDDLE)
emitter = (mobj_t *) &side->middleSoundEmitter();
else if (flags & SNDDF_SIDE_TOP)
emitter = (mobj_t *) &side->topSoundEmitter();
else if (flags & SNDDF_SIDE_BOTTOM)
emitter = (mobj_t *) &side->bottomSoundEmitter();
}
dfloat volume = 1;
if (flags & SNDDF_VOLUME)
{
byte b = Reader_ReadByte(::msgReader);
if (b == 255)
{
// Full volume, no attenuation.
soundFlags |= DDSF_NO_ATTENUATION;
}
else
{
volume = b / 127.0f;
}
}
if (flags & SNDDF_REPEAT)
{
soundFlags |= DDSF_REPEAT;
}
// The delta has been read. Are we skipping?
if (skip) return;
// Now the entire delta has been read.
// Should we start or stop a sound?
if (volume > 0 && sound > 0)
{
// Do we need to queue this sound?
if (type == DT_MOBJ_SOUND && !cmo)
{
// Create a new Hidden clmobj.
cmo = map.clMobjFor(mobjId, true/*create*/);
ClientMobjThinkerData::RemoteSync *info = ClMobj_GetInfo(cmo);
info->flags |= CLMF_HIDDEN | CLMF_SOUND;
info->sound = sound;
info->volume = volume;
// The sound will be started when the clmobj is unhidden.
return;
}
// We will start a sound.
if (type != DT_SOUND && !emitter)
{
// Not enough information.
LOG_NET_VERBOSE("Cl_ReadSoundDelta2(%i): Insufficient data, snd=%i") << type << sound;
return;
}
// Sounds originating from the viewmobj should really originate
// from the real player mobj.
if (cmo && cmo->thinker.id == ClPlayer_State(consolePlayer)->clMobjId)
{
emitter = DD_Player(consolePlayer)->publicData().mo;
}
// First stop any sounds originating from the same emitter.
// We allow only one sound per emitter.
if (type != DT_SOUND && emitter)
{
S_StopSound(0, emitter);
}
S_LocalSoundAtVolume(sound | soundFlags, emitter, volume);
}
else if (sound >= 0)
{
// We must stop a sound. We'll only stop sounds from
// specific sources.
if (emitter)
{
S_StopSound(sound, emitter);
}
}
}
void Cl_Sound()
{
LOG_AS("Cl_Sound");
/// @todo Do not assume the CURRENT map.
world::Map &map = App_World().map();
byte const flags = Reader_ReadByte(::msgReader);
// Sound ID.
dint sound;
if (flags & SNDF_SHORT_SOUND_ID)
{
sound = Reader_ReadUInt16(::msgReader);
}
else
{
sound = Reader_ReadByte(::msgReader);
}
// Is the ID valid?
if (sound < 1 || sound >= DED_Definitions()->sounds.size())
{
LOGDEV_NET_WARNING("Invalid sound ID %i") << sound;
return;
}
LOGDEV_NET_XVERBOSE("id %i", sound);
dint volume = 127;
if (flags & SNDF_VOLUME)
{
volume = Reader_ReadByte(::msgReader);
if (volume > 127)
{
volume = 127;
sound |= DDSF_NO_ATTENUATION;
}
}
if (flags & SNDF_ID)
{
thid_t sourceId = Reader_ReadUInt16(::msgReader);
if (mobj_t *cmob = ClMobj_Find(sourceId))
{
S_LocalSoundAtVolume(sound, cmob, volume / 127.0f);
}
}
else if (flags & SNDF_SECTOR)
{
auto num = dint( Reader_ReadPackedUInt16(::msgReader) );
if (num >= map.sectorCount())
{
LOG_NET_WARNING("Invalid sector number %i") << num;
return;
}
auto *mob = (mobj_t *) &map.sector(num).soundEmitter();
//S_StopSound(0, mob);
S_LocalSoundAtVolume(sound, mob, volume / 127.0f);
}
else if (flags & SNDF_ORIGIN)
{
coord_t pos[3];
pos[0] = Reader_ReadInt16(::msgReader);
pos[1] = Reader_ReadInt16(::msgReader);
pos[2] = Reader_ReadInt16(::msgReader);
S_LocalSoundAtVolumeFrom(sound, nullptr, pos, volume / 127.0f);
}
else if (flags & SNDF_PLAYER)
{
dint const player = (flags & 0xf0) >> 4;
DENG2_ASSERT(player >= 0 && player < DDMAXPLAYERS);
S_LocalSoundAtVolume(sound, DD_Player(player)->publicData().mo, volume / 127.0f);
}
else
{
// Play it from "somewhere".
LOGDEV_NET_VERBOSE("Unspecified origin for sound %i") << sound;
S_LocalSoundAtVolume(sound, nullptr, volume / 127.0f);
}
}