revert CustomSpeedMods change because they don't read from player profiles anymore; that's more important than cleaning up the damn code

This commit is contained in:
AJ Kelly
2012-01-20 13:23:13 -06:00
parent 4ed12ba28c
commit dd6c42c4f5
+76 -52
View File
@@ -1,13 +1,9 @@
--[[ --[[
Custom Speed Mods v2.2 (for StepMania 5) Custom Speed Mods v2.1 (for StepMania 5)
by AJ Kelly of KKI Labs ( http://kki.ajworld.net/ ) by AJ Kelly of KKI Labs ( http://kki.ajworld.net/ )
changelog: changelog:
v2.2 (StepMania 5 alpha 2) [by FSX]
* Rewrite table management code.
* Add code to make sure that there are speed mods and that they are correct.
v2.1 (StepMania 5 Preview 2) v2.1 (StepMania 5 Preview 2)
* Added support for m-Mods. * Added support for m-Mods.
@@ -52,7 +48,20 @@ end
-- Tries to parse the file at path. If successful, returns a table of mods. -- Tries to parse the file at path. If successful, returns a table of mods.
-- If it can't open the file, it will write a fallback set of mods. -- If it can't open the file, it will write a fallback set of mods.
local function ParseSpeedModFile(path) local function ParseSpeedModFile(path)
local function Failure() local file = RageFileUtil.CreateRageFile()
if file:Open(path, 1) then
-- success
local contents = file:Read()
mods = split(',',contents)
-- strip any whitespace
for i=1,#mods do
string.gsub(mods[i], "%s", "")
end
file:destroy()
return mods
else
-- error; write a fallback mod file and return it -- error; write a fallback mod file and return it
local fallbackString = "0.5x,1x,1.5x,2x,3x,4x,5x,6x,7x,8x,C250,C450,m550" local fallbackString = "0.5x,1x,1.5x,2x,3x,4x,5x,6x,7x,8x,C250,C450,m550"
Trace("[CustomSpeedMods]: Could not read SpeedMods; writing fallback to "..path) Trace("[CustomSpeedMods]: Could not read SpeedMods; writing fallback to "..path)
@@ -61,46 +70,49 @@ local function ParseSpeedModFile(path)
file:destroy() file:destroy()
return split(',',fallbackString) return split(',',fallbackString)
end end
local file = RageFileUtil.CreateRageFile()
if file:Open(path, 1) then
-- success
local contents = file:Read()
mods = split(',',contents)
-- strip any whitespace and check
for i=1,#mods do
string.gsub(mods[i], "%s", "")
if not(mods[i]:find("%d+\.?%d*x") or mods[i]:find("[cmCM]%d+")) then
mods[i] = nil
end
end
if #mods==0 then return Failure() end
file:destroy()
return mods
else
return Failure()
end
end end
-- InvertTable(tbl) -- MarkDupes(src,parent)
-- Returns a table where a key-value pair is swapped. -- Marks duplicates in src from any matches in parent.
local function InvertTable(tbl) -- the overall mods are usually used as the parent.
local rTable = {} local function MarkDupes(src,parent)
for k,v in pairs(tbl) do for iPar=1,#parent do
rTable[v] = k for iSrc=1,#src do
if parent[iPar] == src[iSrc] then
src[iSrc] = "XXX"
end end
return rTable end
end
return src
end
-- RemoveMarked(src)
-- Removes any values marked for deletion.
local function RemoveMarked(src)
for iSrc=1,#src do
if src[iSrc] == "XXX" then
table.remove(src,iSrc)
end
end
return src
end end
-- MergeTables(parent,child) -- MergeTables(parent,child)
-- Puts two tables together, overwriting values -- Adds the child's contents to the parent.
-- with the same key. -- the overall mods are usually used as the parent.
local function MergeTables(parent, child) local function MergeTables(parent,child)
for k,v in pairs(parent) do child = RemoveMarked(child)
child[k] = v if #child == 0 then
return parent
end end
local addMe = true
for iC=1,#child do
if addMe then
table.insert(parent,child[iC])
end
end
return parent
end end
-- code in this function is based off of code in -- code in this function is based off of code in
@@ -175,8 +187,8 @@ local function GetSpeedMods()
-- figure out how many players we have to deal with. -- figure out how many players we have to deal with.
local numPlayers = GAMESTATE:GetNumPlayersEnabled() local numPlayers = GAMESTATE:GetNumPlayersEnabled()
-- load machine to finalMods -- load machine
finalMods = InvertTable(ParseSpeedModFile(profileDirs.Machine..baseFilename)) local machineMods = ParseSpeedModFile(profileDirs.Machine..baseFilename)
local playerMods = {} local playerMods = {}
for pn in ivalues(GAMESTATE:GetHumanPlayers()) do for pn in ivalues(GAMESTATE:GetHumanPlayers()) do
@@ -189,22 +201,34 @@ local function GetSpeedMods()
end end
end end
-- join players, overwriting duplicates -- mine for duplicates, first pass
machineMods = MarkDupes(machineMods,finalMods)
for ply=1,#playerMods do for ply=1,#playerMods do
MergeTables(finalMods,InvertTable(playerMods[ply])) playerMods[ply] = MarkDupes(playerMods[ply],finalMods)
end
-- remove XXX, first pass
machineMods = RemoveMarked(machineMods);
for ply=1,#playerMods do
playerMods[ply] = RemoveMarked(playerMods[ply])
end
-- mine for duplicates, second pass (machine <-> player)
for ply=1,#playerMods do
playerMods[ply] = MarkDupes(playerMods[ply],machineMods)
end
-- remove XXX, second pass
machineMods = RemoveMarked(machineMods)
for ply=1,#playerMods do
playerMods[ply] = RemoveMarked(playerMods[ply])
end end
-- convert into an unsorted integer-indexed table -- merge zone
do finalMods = MergeTables(finalMods,machineMods)
local curIndex = 1 for ply=1,#playerMods do
local newFinalMods = {} finalMods = MergeTables(finalMods,playerMods[ply])
for k,v in pairs(finalMods) do
newFinalMods[curIndex] = k
curIndex = curIndex + 1
end
finalMods = newFinalMods
end end
-- final removal of XXX before sorting
finalMods = RemoveMarked(finalMods)
-- sort the mods before returning them -- sort the mods before returning them
return SpeedModSort(finalMods) return SpeedModSort(finalMods)
end end