-
Notifications
You must be signed in to change notification settings - Fork 3
/
Script.c
331 lines (257 loc) · 8.24 KB
/
Script.c
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
#strict 2
local statiByPlayer;
local maxPlayerIndex;
local names, enviromentconditions, volumes, looplists, loopsingles;
local playlists;
local playlistnames;
//local currentplaylistIndex;
//local currentplaylist;
// Local variable usage:
// Local(0..highest playerid) - Local index coresponds to player id
// = Array (
// 0 = Array([amount of env conditions numbers: between 0 and 3, whereas 0 means stop that sound and 3 means play that music
// 1 = index of current playing music or -1
// )
func Initialize() {
names = [];
enviromentconditions = [];
volumes = [];
looplists = [];
loopsingles = [];
playlists = [];
playlistnames = [];
AddDefaultPlaylists();
if (GameCall("MusicTheme")) {
SetPlaylist(GameCall("MusicTheme"));
RemoveObject();
return;
}
// First trigger to be added = lowest priority
// Last added trigger = highest priority
// Music with higher priority stops music with lower priority
AddTrigger("peaceful", ["IsNight_0"], 100, 0, 0);
AddTrigger("peacefulnightloops", ["IsNight_1"], 100, 0, 1);
AddTrigger("hell", ["nearbyMat_DuroLava", "minPosY_85"], 100, 0, 0);
AddEffect("Detect", this, 1, 40, this);
}
func FxDetectTimer() {
//Log("%v", Local(0));
checkPlayerCount();
updatePlayerStati();
playApplicableMusic();
}
func AddDefaultPlaylists() {
CreatePlaylist("peaceful", [
"484019_Blind-Alley.ogg",
"515887_Red.ogg",
"550015_Rebuild.ogg",
"559676_In-a-While.ogg",
"560546_Homecoming.ogg",
"561630_Hey-You.ogg",
"566414_AGP3-03Still-so-man.ogg",
"576319_Disappointing-Love-.ogg",
"Matt-Porter - Waiting (Clean) (ID 465344).ogg",
"Grandvision - Soaring Soren.ogg",
"Semaphore - Uprising Good (ID 438371).ogg",
"Sirtroymoon - A Moonlight Waltz (ID 126585).ogg",
"gamejunkie - Child in my arms (ID 432848).ogg",
"Steve Syz - #4 mp3.ogg"
]);
CreatePlaylist("peacefulnightloops", [
"Night Loop 1.ogg",
"Night Loop 2.ogg"
]);
CreatePlaylist("hell", [
"567844_Hello-ZeppSaw-Theme.ogg",
"568516_Dungeon-Deep-1.ogg",
"Bezo - UWXXIX - Demon Lord (Fix) (ID 424274).ogg",
"Brian Czernikowski (Cherni-Braiton) - unknown title.ogg"
]);
}
// param looplist does nothing currently!
func AddTrigger(string name, array conditions, int volume, int looplist, int loopsingle) {
ArrayPush(names, name);
ArrayPush(enviromentconditions, conditions);
ArrayPush(volumes, volume);
ArrayPush(looplists, looplist);
ArrayPush(loopsingles, loopsingle);
}
func RemoveTrigger(string name) {
var index = ArrayIndexOf(names, name);
if (index > -1) {
ArraySlice(names, index);
ArraySlice(enviromentconditions, index);
ArraySlice(volumes, index);
ArraySlice(looplists, index);
ArraySlice(loopsingles, index);
}
}
// Plays a playlist, overridding completely all triggers
func SetPlaylist(name) {
var index = ArrayIndexOf(playlistnames, name);
var playlist = playlists[index];
SetPlayList(ImplodeArray(";", playlist));
}
func CreatePlaylist(playlistname, songs) {
if (!songs) songs = [];
ArrayPush(playlistnames, playlistname);
ArrayPush(playlists, songs);
}
func AddSongToPlaylist(playlistname, song) {
var index = ArrayIndexOf(playlistnames, playlistname);
ArrayPush(playlists[index], playlistname);
}
func RemoveSongFromPlaylist(playlistname, song) {
var index = ArrayIndexOf(playlistnames, playlistname);
var songindex = ArrayIndexOf(playlists[index], song);
ArraySlice(playlists[index], songindex);
}
// If currently running a song of another playlist, this func will fade to a song of the other playlist
/*func SetPlaylist(playlistname) {
currentplaylist = playlistname;
currentplaylistIndex = ArrayIndexOf(playlistnames, playlistname);
if (currentplaylistIndex != -1) {
SetPlayList(ImplodeArray(";", playlists[currentplaylistIndex]));
}
}*/
func checkPlayerCount() {
var playerid;
for (var i = 0; i < GetPlayerCount(C4PT_User); i++) {
playerid= GetPlayerByIndex(i, C4PT_User);
if (!Local(playerid)) {
Local(playerid) = CreateArray(2);
Local(playerid)[0] = CreateArray(GetLength(enviromentconditions));
Local(playerid)[1] = -1;
}
}
}
func updatePlayerStati() {
var clonk, plr=0, playerid, envnum, found = 0;
var type, target;
while (clonk = GetCursor(plr++)) {
playerid = GetPlayerByIndex(GetOwner(clonk));
for (envnum = 0; envnum < GetLength(enviromentconditions); envnum++) {
found = 1;
// Log("checking %d", envnum);
for (var prop in enviromentconditions[envnum]) {
var tmp = ExplodeString("_", prop);
type = tmp[0];
target = tmp[1];
if (type == "IsNight") {
found = found && Format("%d", IsNight()) == target;
}
if (type == "minPosY") {
found = found && clonk->GetY() >= (StringToInt(target) * LandscapeHeight()) / 100;
}
if (type == "maxPosY") {
found = found && clonk->GetY() <= (StringToInt(target) * LandscapeHeight()) / 100;
}
if (type == "minPosX") {
found = found && clonk->GetX() >= (StringToInt(target) * LandscapeWidth()) / 100;
}
if (type == "maxPosX") {
found = found && clonk->GetX() <= (StringToInt(target) * LandscapeWidth()) / 100;
}
if (type == "inMat") {
found = found && GetMaterial(clonk->GetX(), clonk->GetY()) == Material(target);
}
// Log("%v %v %v", prop, target, found);
if (type == "nearbyMat") {
var haveMat = 0;
for (var attempt = 0; attempt < 80; attempt++) {
if (GetMaterial(clonk->GetX()+RandomX(-100, 100), clonk->GetY()+RandomX(-100, 100)) == Material(target)) {
haveMat = 1;
break;
}
}
found = found && haveMat;
}
if (type == "nearbyDefcall") {
var haveCall = 0;
for (var obj in FindObjects(Find_Distance(30, clonk->GetX(), clonk->GetY()))) {
if (DefinitionCall(GetID(obj), target)) {
haveCall = 1;
break;
}
}
found = found && haveCall;
}
if (type == "nearbyId") {
found = found && FindObject2(Find_ID(C4Id(target)), Find_Distance(30, clonk->GetX(), clonk->GetY()));
}
}
updatePlayerProp(playerid, envnum, found);
}
}
}
func updatePlayerProp(playerid, envnum, increase) {
if (increase) {
Local(playerid)[0][envnum] = Min(3, Local(playerid)[0][envnum]+1);
} else {
Local(playerid)[0][envnum] = Max(0, Local(playerid)[0][envnum]-1);
}
}
private func playApplicableMusic() {
var lastplayerid = GetPlayerByIndex(GetPlayerCount(C4PT_User)-1, C4PT_User);
var plrnum = 0;
for (var plrid = 0; plrid <= lastplayerid; plrid++) {
if (Local(plrid)) {
for (var amb=0; amb <= GetLength(names); amb++) {
if (Local(plrid)[0][amb] <= 0) {
stopMusic(amb, plrid, plrnum);
}
//if (Local(plrid)[0][amb] >= 3 ) Log("%d > %d", amb, Local(plrid)[1]);
if (Local(plrid)[0][amb] >= 3 && amb > Local(plrid)[1]) {
startMusic(amb, names[amb], plrid, plrnum, volumes[amb], looplists[amb], loopsingles[amb]);
}
}
plrnum++;
}
}
}
func stopMusic(listindex, plrid, plrnum) {
// Only stop this music if its actually currently playing
if (Local(plrid)[1] == listindex) {
stopMusicForPlayer(plrnum);
Local(plrid)[1] = -1;
}
}
func startMusic(listindex, name, plrid, plrnum, volume, looplist, loopsingle) {
if (listindex > Local(plrid)[1]) {
stopMusicForPlayer(plrnum);
} else {
return;
}
MusicLevel(volume);
var index = ArrayIndexOf(playlistnames, name);
var playlist = playlists[index];
Log("playlist is %v", playlist);
if (loopsingle) {
var single = playlist[RandomX(0, GetLength(playlist))];
Log("single song of that is %v", single);
setPlayListForPlayer(plrnum, single);
} else {
setPlayListForPlayer(plrnum, ImplodeArray(";", playlist));
}
Local(plrid)[1] = listindex;
}
func stopMusicForPlayer(plrnum) {
if (GetPortrait(GetCursor(plrnum))) {
//Log("stopped music");
//Music();
}
}
func setPlayListForPlayer(plrnum, playlist) {
if (GetPortrait(GetCursor(plrnum))) {
Log("playing list %v", playlist);
SetPlayList(playlist);
}
}
/*
func SetShuffle(mode) {
shuffle = mode;
}
func SetLoop(mode) {
loop = mode;
}
*/