Report abuse

02 CustomCodeDetector.lua (lua)

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
--[[--------------------------------------------------------------------------
Custom Code Detector by Daisuke Master
------------------------------------------------------------------------------
-= Whats this? =-
Just that, a custom codedetector, you can add the codenames as exact mods
and this thingy will do whatever you wanted to do with the oh-so-known
harcoded codedetector.
I kind of don't like the codedetector for various reasons, that's why I made
this (also the remaining stuff happened by accident lol), anyway, if you feel
like wanting this in your theme, go ahead! no problem, just let me know that
you're using it and that this was useful for you ^^

-= How to use =-
First of all, put this lua module under the Scripts folder of your theme.
Using codeset, put this in a CodeMessageCommand, inside a function command,
under ScreenSelectMusic, like this:

<code>
CodeMessageCommand=function(self)
	CustomCodeDetector()
end;
</code>

You'll need codename and player to be passed as arguments, you can get those
somewhere:

<code>
CodeMessageCommand=function(self,params)
	CustomCodeDetector(params.Name, params.PlayerNumber)
end;
</code>

If you like to be classy like me:

<code>
CodeMessageCommand=function(self,params)
	local code = params.Name
	local player = params.PlayerNumber
	if not GAMESTATE:IsHumanPlayer(player) then return end;
	
	CustomCodeDetector(code, player)
end;
</code>

Niceties:

<code>
CodeMessageCommand=function(self,params)
	local code = params.Name
	local player = params.PlayerNumber
	if not GAMESTATE:IsHumanPlayer(player) then return end;
	
	if CustomCodeDetector(code, player, true) then
		--idk what to put here so eh...
	else
		--handle here other codes for misc purposes
	end
	
	SOUND:PlayOnce(THEME:GetPathS("ScreenSelectMusic","Options"))
end;
</code>
Extras: you can set a third param as true to deactivate sound and
the message broadcast to handle those outside (example above)

What more? Oh yes, you'll need a CodeNames line under ScreenSelectMusic

<code>
[ScreenSelectMusic]
CodeNames="Noteskin,Speed,Cancel,Dark,Mirror"
;main codes
CodeNoteskin="Up,Up,Down,Down,Left,Right,Left,Right"
CodeSpeed="Left,Right,Left,Right,Down"
CodeCancel="Select,Select"
;secondary codes
CodeDark="Down,Down,Left,Right,Down"
CodeMirror="Down,Left,Up,Down,Right,Up"
</code>

The first three (speed, noteskin and cancle er... cancel) are mandatory, the
rest are optional (dark and mirror as examples)

-= Caveats =-
*I don't know what will happen if illegal modifiers are set into the CodeNames
line under ScreenSelectMusic, so try at your own risk (not that anything bad
but a crash may happen, I don't know so I'm just warning you).
*I don't know what will happen if you add percentages and/or approach into the
modifiers (for example >Code50% tornado="@Right-Up")
*Ignore the previous two points, I just added a mod validator function, if your
mod does not exists there just add it.
*You just can't set multiple modifiers in one code because of how codes are
separated (by commas)
*FOR PUMP-SM USERS: Watch out when using center as part of your code, it's
treated as start button and you know what happens when you press start,
solution: you can use two part selection in metrics along with twopart
confirms only so you can doublepress center with no problems, remember that
only sm-ssc can do that (goes the same to ez2/popn users with footdown/red
buttons).
*Don't try weird things with this.

-= TODO list =-
*Previous speed and noteskins
*Go lazy and lowercase GetPlayerOptions

-= Licensing =-
IMO it's important, I don't like to see my work being hijacked by someone else
and claiming as it's own (others don't like that either) so here we go:

I'm licensing this under Creative Commons by-sa 3.0
http://creativecommons.org/licenses/by-sa/3.0/

This means that:

You can share and adapt this code as you like/need/want withouth worry, but
you must attribute me, and if you release your modified work you must license
it with the same license as this.

-= Credits =-
ºDaisuMaster

-= Thanks =-
ºmain stepmania devteam (they created SM)
ºsm-ssc devteam (they forked sm4 and tweaked it in an astounding way)
ºcesarmades (this guy inspired me somehow)

-= Final notes =-
That's all you should need to know for now, and as I said, feel free to use
this and let me know if this was useful for you ^^
--]]--------------------------------------------------------------------------
local function ReadSpeedMods()
	--this reads speeds from a file (Cmods are not allowed, I don't like to
	--deal with these)
	local dir = THEME:GetCurrentThemeDirectory() .. "speeds.txt"
	local speeds = File.Read( dir )
	if not speeds then
		speeds = {"1x","2x","3x","4x","5x","8x"}
		speeds = join("\r\n",speeds)
		File.Write( dir , speeds)
		return speeds
	end
	
	speeds = string.gsub(speeds,"\r","")
	speeds = split("\n", speeds)
	
	--[[local rebuild = false
	for k,v in ipairs(speeds) do
		if not string.find(v,"%dx") or not string.find(v,"%%d.dx") then
			rebuild = true
			table.remove(speed,k)
		end
	end
	if rebuild then
		--cleanup
		File.Write( dir , join("\r\n",speeds))
	end]]
	
	return speeds
end

local function IsValidMod(m)
	--check mods here, see PlayerOptions.cpp if you want/need more
	--throwing the most common/used here
	--todo: regex formats and bind the mods into a table so setups like 50% dark would work
	if m == "Boost"
	or m == "Brake"
	or m == "Wave"
	or m == "Expand"
	or m == "Boomerang"
	or m == "Drunk"
	or m == "Dizzy"
	or m == "Confusion"
	or m == "Mini"
	or m == "Tiny"
	or m == "Flip"
	or m == "Invert"
	or m == "Tornado"
	or m == "Tipsy"
	or m == "Bumpy"
	or m == "Beat"
	or m == "XMode"
	or m == "Hidden"
	or m == "Sudden"
	or m == "Stealth"
	or m == "Blink"
	or m == "RandomVanish"
	or m == "Reverse"
	or m == "Split"
	or m == "Alternate"
	or m == "Cross"
	or m == "Centered"
	or m == "Dark"
	or m == "RandomAttacks"
	or m == "SongAttacks"
	or m == "PlayerAutoPlay"
	or m == "Mirror"
	or m == "Left"
	or m == "Right"
	or m == "Shuffle"
	or m == "SoftShuffle"
	or m == "SuperShuffle"
	then
		return true
	else
		return false
	end
end

--local function Tracef(...) Trace(string.format(...)) end

function CustomCodeDetector(code,player,sound_and_broadcast)
	assert(code,"[CUSTOMCODEDETECTOR]: an input code must be given.")
	assert(player, "[CUSTOMCODEDETECTOR]: a player number must be given.")
	if sound_and_broadcast then
		Trace("[CUSTOMCODEDETECTOR]: playeroptions message broadcast and sound playback are deactivated")
	end
	--playerstates lets you get and set modifiers per player via lua
	local playerstate = GAMESTATE:GetPlayerState(player)
	--nice, I didn't knew that playermods could be get as an array.
	local playermods = playerstate:GetPlayerOptionsArray('ModsLevel_Preferred')
	--local playermods = playerstate:GetPlayerOptions('ModsLevel_Preferred')
	--playermods = split(", ",playermods)
	
	local noteskins = NOTESKIN:GetNoteSkinNames()
	
	--woot! noteskins via codes!! and not hardcoded!!! :awesome:
	if code == "Noteskin" then
		--do changes here
		local found = false
		--iterate every noteskin in every playermod
		for i,j in ipairs(playermods) do
			for k,l in ipairs(noteskins) do
				--we got a match
				if j == l then
					found = true
					local index = k+1
					--avoiding overflow
					if index > #noteskins then index = 1 end
					--replacing
					Tracef("[CUSTOMCODEDETECTOR]: Setting noteskin '%s' in place of '%s'", noteskins[index], playermods[i])
					playermods[i] = noteskins[index]
				end
			end
		end
		
		--no matches
		if not found then
			for k,v in ipairs(noteskins) do
				if string.lower(v) == "default" then
					--set the one next to default or the first if it's the last
					local idx = k+1
					if idx > #noteskins then idx = 1 end
					Tracef("[CUSTOMCODEDETECTOR]: Setting noteskin '%s'", noteskins[idx])
					table.insert(playermods, noteskins[idx])
				end
			end
		end
	--like notekins but with speeds
	elseif code == "Speed" then
		--read your speeds from a file (by theme)
		local speeds = ReadSpeedMods()
		local found = false
		
		for i,j in ipairs(playermods) do
			for k,l in ipairs(speeds) do
				if j == l then
					found = true
					local index = k+1
					if index > #speeds then index = 1 end
					Tracef("[CUSTOMCODEDETECTOR]: Setting speedmod '%s' in place of '%s'", speeds[index], playermods[i])
					playermods[i] = speeds[index]
				end
			end
		end
		if not found then
			--most likely is a 1x, set the next
			for k,v in ipairs(speeds) do
				if v == "1x" then
					local idx = k+1
					if idx > #speeds then idx = 1 end
					Tracef("[CUSTOMCODEDETECTOR]: Setting speedmod '%s'", speeds[idx])
					table.insert(playermods, speeds[idx])
				end
			end
		end
	elseif code == "Cancel" then
		local defaultmods = PREFSMAN:GetPreference("DefaultModifiers")
		defaultmods = split(",", defaultmods)
		--set 1x and clear other mods, also default noteskin (along with the default mods)
		playermods = { "1x","default" }
		for k,v in ipairs(defaultmods) do
			table.insert(playermods, v)
		end
	else
		if IsValidMod(code) then
			--other mod codes, code name must be the exact mod
			local found = false
			for i,j in ipairs(playermods) do
				if j == code then
					--remove it
					found = true
					Tracef("[CUSTOMCODEDETECTOR]: Setting modifier '%s'", code)
					table.remove(playermods, i)
				end
			end
			
			if not found then
				Tracef("[CUSTOMCODEDETECTOR]: Unsetting modifier '%s'", code)
				table.insert(playermods, code)
			end
		else
			--SOUND:PlayOnce(THEME:GetPathS("MusicWheel","locked"))
			Tracef("[CUSTOMCODEDETECTOR]: The mod '%s' could not be set", code)
			return false
		end
	end
	
	--let's check if noteskins aren't missing (we're avoiding crashes here...)
	local found = false
	--iterate again uh
	for i,j in ipairs(playermods) do
		for k,l in ipairs(noteskins) do
			if j == l then
				found = true
			end
		end
	end
	--apparently there aren't any noteskins...
	if not found then
		table.insert(playermods, "default")
	end
	
	--join
	playermods = join(", ",playermods)
	
	--set the mods
	playerstate:SetPlayerOptions('ModsLevel_Preferred',playermods)
	--optional stuff
	if not sound_and_broadcast then
		--play some sound
		SOUND:PlayOnce(THEME:GetPathS("ScreenSelectMusic","Options"))
		--send some message
		MESSAGEMAN:Broadcast("PlayerOptionsChanged", { PlayerNumber = player })
	end
	--if we get here then everything went just fine
	return true
end