· 6 years ago · Sep 23, 2019, 10:26 AM
1-- Victory themes, by zxyspku and Lat'
2
3local cv_victorythemes = CV_RegisterVar({"victorythemes", "Off", 0, CV_OnOff})
4
5local victorythemes = {}
6local labels = {}
7
8-- we use a mapload hook to reset the var used below
9-- otherwise the finish music would never play again since it'd still be true :dying:
10addHook("MapLoad", function()
11 for p in players.iterate
12 p.vict_changedmusic = nil
13 end
14end)
15
16local function DM_Vict_GetMusic(p, skin)
17 victorythemes[skin] = $ ~= nil and $ or {} -- make sure this table is initialized
18
19 if (p.kartstuff[k_position] == 1) -- first we check if we finished in first
20 -- now we check for if the 1st place finish theme exists first (it's in the first index)
21
22 if victorythemes[skin][1] -- check if it exists
23 return victorythemes[skin][1] -- we found it, return it
24 else -- it doesnt exist...
25 -- for the convenience of the user, we'll attempt to fallback on the okay finish theme
26
27 if victorythemes[skin][2] -- check if it exists again...
28 return victorythemes[skin][2] -- we found our fallback, return it
29 end
30 end
31
32 -- if we couldnt find anything after all that, we'll return the default KRWIN
33 return "KRWIN"
34 else
35 -- we're gonna do the same as above, but now prioritizing the okay finish theme
36 -- as well as defaulting to "KROK"
37
38 if victorythemes[skin][2]
39 return victorythemes[skin][2]
40 else
41 if victorythemes[skin][1]
42 return victorythemes[skin][1]
43 end
44 end
45
46 return "KROK"
47 end
48end
49
50DM_FuckYou("addvictorytheme", function(player, skin, lump, label)
51 if (player ~= consoleplayer) then return end
52
53 if not (skin) then
54 CONS_Printf(player, "addvictorytheme <skin> <lump>: Sets the victory theme for the given skin to the provided lump")
55 return
56 end
57
58 if not (lump) then error("no music lump provided") return end
59
60 -- when working with tables inside tables remember to do this
61 -- this initializes the table so we can use it without anything breaking
62 victorythemes[skin] = $ ~= nil and $ or {}
63
64 -- we'll store this information in the first index of a table
65 -- this creates a table in a table
66 -- lua's greatest strength, tables in tables
67
68 victorythemes[skin][1] = lump:upper()
69 if (label) then
70 -- initialize
71 labels[skin] = $ ~= nil and $ or {}
72
73 -- same with the labels table
74 labels[skin][1] = label
75 end
76 CONS_Printf(player, "[Victory Themes] Victory theme for \""..skin.."\" has been set to \x82"..lump.."\x80")
77end)
78
79DM_FuckYou("addokayfinishtheme", function(player, skin, lump, label)
80 if (player ~= consoleplayer) then return end
81
82 if not (skin) then
83 CONS_Printf(player, "addokayfinishtheme <skin> <lump>: Sets the okay victory theme for the given skin to the provided lump")
84 return
85 end
86
87 if not (lump) then error("no music lump provided") return end
88
89 victorythemes[skin] = $ ~= nil and $ or {}
90
91 -- we'll store this in the second index, instead
92 victorythemes[skin][2] = lump:upper()
93 if (label) then
94 labels[skin] = $ ~= nil and $ or {}
95 labels[skin][2] = label
96 end
97 CONS_Printf(player, "[Victory Themes] Okay finish theme for \""..skin.."\" has been set to \x82"..lump.."\x80")
98end)
99
100COM_AddCommand("delvictorytheme", function(player, skin)
101 if (player ~= consoleplayer) then return end
102
103 if not (skin) then
104 CONS_Printf(player, "delvictorytheme <skin>: Removes the currently set victory themes for the provided skin")
105 return
106 end
107
108 victorythemes[skin] = nil
109 if (labels[skin]) then
110 labels[skin] = nil
111 end
112 CONS_Printf(player, "[Victory Themes] Victory themes for \""..skin.."\" removed")
113end)
114
115COM_AddCommand("listvictorythemes", function(player)
116 if (player ~= consoleplayer) then return end
117
118 if (next(victorythemes) == nil) then
119 CONS_Printf(player, "Victory theme table is empty")
120 return
121 end
122
123 CONS_Printf(player, "Victory Theme List")
124
125 -- this loop goes through each index in victory themes
126 -- k is the index (the skin we're checking)
127 -- v is the table of the index (where our themes are stored)
128 for k, v in pairs(victorythemes) do
129 -- initialize tables
130 v = $ ~= nil and $ or {}
131 labels[k] = $ ~= nil and $ or {}
132
133 -- create the starting string
134 local str = ""
135
136 -- check if 1st place victory theme exists
137 if victorythemes[k][1]
138 str = k.." (Victory): "..v[1]
139 if (labels[k][1]) then
140 str = str.." - "..labels[k][1]
141 end
142 CONS_Printf(player, str)
143 end
144
145 -- check if okay finish theme exists
146 if v[2]
147 str = k.." (Okay Finish): "..v[2]
148 if (labels[k][2]) then
149 str = str.." - "..labels[k][2]
150 end
151 CONS_Printf(player, str)
152 end
153 end
154end)
155
156addHook("ThinkFrame", do
157 -- we use this to check if the var is enabled
158 -- if it was enabled, it'd return 1, which would make the condition true
159 if not (cv_victorythemes.value) then return end
160
161 -- this is just consoleplayer stuff copypasted from another script
162 -- consoleplayers are the players that joined the game, and doesnt change
163 -- of course, this means consoleplayers are different for each player and arent synched in netgames
164 if not (consoleplayer) then return end
165
166 local cp, cp2, cp3, cp4 = consoleplayer, secondarydisplayplayer, thirddisplayplayer, fourthdisplayplayer
167
168 -- for all our player checks we need to check if .mo exists
169 -- and for every player that isnt the first player we need to check if the splitplayer exists, too
170 if (cp.mo and cp.exiting and not K_IsPlayerLosing(cp)) -- we want to know whether the player has finished the race and has won (isn't losing)
171 or ((cp2 and cp2.mo) and cp2.exiting and not K_IsPlayerLosing(cp2)) -- copypaste the above condition for the second console player (splitscreen support)
172 or ((cp3 and cp3.mo) and cp3.exiting and not K_IsPlayerLosing(cp3)) -- ditto, for the third consoleplayer
173 or ((cp4 and cp4.mo) and cp4.exiting and not K_IsPlayerLosing(cp4)) -- ditto, for the fourth consoleplayer
174 -- we dont want to change the music again if it's already changed
175 -- this fixes finish music playing twice
176 if (cp.vict_changedmusic) then return end
177
178 -- splitscreen support (REALLY TEDIOUS)
179 -- I dont recommend spltscreen support unless absolutely necessary
180 -- for splitscreen we want to play the music of the first splitscreen player to finish
181
182 -- we'll assume the starting index is 1 for non-splitscreen (first player)
183 local vict_player = cp
184 -- now we check for splitscreen players (while also making sure they exist in the first place)
185 if ((cp2 and cp2.mo) and cp2.exiting and not K_IsPlayerLosing(cp2)) -- check if the second player finished first
186 vict_player = cp2
187 elseif ((cp2 and cp2.mo) and cp3.exiting and not K_IsPlayerLosing(cp3)) -- ditto, for the third player
188 vict_player = cp3
189 elseif ((cp2 and cp2.mo) and cp4.exiting and not K_IsPlayerLosing(cp4)) -- ditto, for the fourth player
190 vict_player = cp4
191 end
192
193 -- we call the function here to quickly return what music we'll use
194 -- the arguments the function needs are the player and what skin index to check
195 -- we put vict_player.mo.skin in to return the skin index of the player we retrieved above
196 -- mo.skin is the skin we're playing as, as text, so playing as Sonic returns "sonic"
197 S_ChangeMusic(DM_Vict_GetMusic(vict_player, vict_player.mo.skin), true, cp)
198
199 -- we set the var check here to trigger the return function at the start of the code block
200 cp.vict_changedmusic = true
201 end
202end)