Cube  Diff

Differences From Artifact [91b933ce07]:

To Artifact [f965fc3ef4]:


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
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



+
+







+






-
-
-
+
-






+
-
+




-
-







#include "cube.h"

#import "DynamicEntity.h"

#include <SDL_mixer.h>

VARP(soundvol, 0, 255, 255);
VARP(musicvol, 0, 128, 255);
bool nosound = false;

#define MAXCHAN 32
#define SOUNDFREQ 22050
#define MAXVOL MIX_MAX_VOLUME

struct soundloc {
	OFVector3D loc;
	bool inuse;
} soundlocs[MAXCHAN];

#include <SDL_mixer.h>
#define MAXVOL MIX_MAX_VOLUME
Mix_Music *mod = NULL;
static Mix_Music *mod = NULL;
void *stream = NULL;

void
stopsound()
{
	if (nosound)
		return;

	if (mod) {
	if (mod != NULL) {
		Mix_HaltMusic();
		Mix_FreeMusic(mod);
		mod = NULL;
	}
	if (stream != NULL)
		stream = NULL;
}

VAR(soundbufferlen, 128, 1024, 4096);

void
initsound()
{
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
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







+

+

















-
-
+















+
+
+



+
-
+

-
+







}

void
music(OFString *name)
{
	if (nosound)
		return;

	stopsound();

	if (soundvol && musicvol) {
		name = [name stringByReplacingOccurrencesOfString:@"\\"
		                                       withString:@"/"];
		OFString *path =
		    [OFString stringWithFormat:@"packages/%@", name];
		OFIRI *IRI = [Cube.sharedInstance.gameDataIRI
		    IRIByAppendingPathComponent:path];

		if ((mod = Mix_LoadMUS(
		         IRI.fileSystemRepresentation.UTF8String)) != NULL) {
			Mix_PlayMusic(mod, -1);
			Mix_VolumeMusic((musicvol * MAXVOL) / 255);
		}
	}
}
COMMAND(music, ARG_1STR)

vector<Mix_Chunk *> samples;

static OFMutableData *samples;
static OFMutableArray<OFString *> *snames;

int
registersound(OFString *name)
{
	int i = 0;
	for (OFString *iter in snames) {
		if ([iter isEqual:name])
			return i;

		i++;
	}

	if (snames == nil)
		snames = [[OFMutableArray alloc] init];
	if (samples == nil)
		samples = [[OFMutableData alloc]
		    initWithItemSize:sizeof(Mix_Chunk *)];

	[snames addObject:[name stringByReplacingOccurrencesOfString:@"\\"
	                                                  withString:@"/"]];
	Mix_Chunk *sample = NULL;
	samples.add(NULL);
	[samples addItem:&sample];

	return samples.length() - 1;
	return samples.count - 1;
}
COMMAND(registersound, ARG_1EST)

void
cleansound()
{
	if (nosound)
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
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







+


+




+

+

-
-
-
+
+
+
+




+
-
+





-
-
+

-
+





-
+


+


+









int soundsatonce = 0, lastsoundmillis = 0;

void
playsound(int n, const OFVector3D *loc)
{
	if (nosound)
		return;

	if (!soundvol)
		return;

	if (lastmillis == lastsoundmillis)
		soundsatonce++;
	else
		soundsatonce = 1;

	lastsoundmillis = lastmillis;

	if (soundsatonce > 5)
		return; // avoid bursts of sounds with heavy packetloss
		        // and in sp
	if (n < 0 || n >= samples.length()) {
		// avoid bursts of sounds with heavy packetloss and in sp
		return;

	if (n < 0 || n >= samples.count) {
		conoutf(@"unregistered sound: %d", n);
		return;
	}

	Mix_Chunk **sample = (Mix_Chunk **)[samples mutableItemAtIndex:n];
	if (!samples[n]) {
	if (*sample == NULL) {
		OFString *path = [OFString
		    stringWithFormat:@"packages/sounds/%@.wav", snames[n]];
		OFIRI *IRI = [Cube.sharedInstance.gameDataIRI
		    IRIByAppendingPathComponent:path];

		samples[n] =
		    Mix_LoadWAV(IRI.fileSystemRepresentation.UTF8String);
		*sample = Mix_LoadWAV(IRI.fileSystemRepresentation.UTF8String);

		if (!samples[n]) {
		if (*sample == NULL) {
			conoutf(@"failed to load sample: %@", IRI.string);
			return;
		}
	}

	int chan = Mix_PlayChannel(-1, samples[n], 0);
	int chan = Mix_PlayChannel(-1, *sample, 0);
	if (chan < 0)
		return;

	if (loc)
		newsoundloc(chan, loc);

	updatechanvol(chan, loc);
}

void
sound(int n)
{
	playsound(n, NULL);
}
COMMAND(sound, ARG_1INT)