16,254
edits
(Add new 1.1.2 modifiers) |
(getModifiersDifference: Initial implementation) |
||
Line 964: | Line 964: | ||
return skillArray | return skillArray | ||
end | |||
-- Returns a modifiers table indicating modifiersNew less modifiersOld | |||
-- The returned table can be passed to getModifiersText to obtain the | |||
-- result in a human readable format | |||
function p.getModifiersDifference(modifiersOld, modifiersNew) | |||
local modHasPrefix = {} | |||
local modDiff, modDiffBase = {}, {} | |||
local allMods = { | |||
{ ["mods"] = modifiersNew, ["mult"] = 1 }, | |||
{ ["mods"] = modifiersOld, ["mult"] = -1 } | |||
} | |||
-- Generate modifiers table containing only variances | |||
-- Create modDiffBase with mod base names (less 'increased'/'decreased' prefixes), | |||
for i, modDef in ipairs(allMods) do | |||
for modName, value in pairs(modDef.mods) do | |||
local modBaseName, modIsIncrease = modName, true | |||
if Shared.startsWith(modName, "increased") or Shared.startsWith(modName, "decreased") then | |||
modBaseName = string.sub(modName, 10) | |||
modIsIncrease = Shared.startsWith(modName, "increased") | |||
modHasPrefix[modBaseName] = true | |||
end | |||
local modMult = (modIsIncrease and 1 or -1) * modDef.mult | |||
if type(value) == 'table' then | |||
-- Skill specific modifier | |||
if modDiffBase[modBaseName] == nil then | |||
modDiffBase[modBaseName] = {} | |||
end | |||
for j, subVal in ipairs(value) do | |||
if type(subVal) == 'table' and subVal.skillID ~= nil then | |||
modDiffBase[modBaseName][subVal.skillID] = (modDiffBase[modBaseName][subVal.skillID] or 0) + subVal.value * modMult | |||
end | |||
end | |||
else | |||
modDiffBase[modBaseName] = (modDiffBase[modBaseName] or 0) + value * modMult | |||
end | |||
end | |||
end | |||
-- Transform modDiffBase into modDiff with the appropriate mod name within the return value | |||
for modBaseName, value in pairs(modDiffBase) do | |||
if type(value) == 'table' then | |||
-- Skill specific modifier | |||
for skillID, subVal in pairs(value) do | |||
local modName = nil | |||
if modHasPrefix[modBaseName] then | |||
modName = (subVal < 0 and 'decreased' or 'increased') .. modBaseName | |||
else | |||
modName = modBaseName | |||
end | |||
if modDiff[modName] == nil then | |||
modDiff[modName] = {} | |||
end | |||
table.insert(modDiff[modName], { ["skillID"] = skillID, ["value"] = math.abs(subVal) }) | |||
end | |||
else | |||
local modName = nil | |||
if modHasPrefix[modBaseName] then | |||
modName = (value < 0 and 'decreased' or 'increased') .. modBaseName | |||
else | |||
modName = modBaseName | |||
end | |||
modDiff[modName] = (modDiff[modName] or 0) + math.abs(value) | |||
end | |||
end | |||
return modDiff | |||
end | end | ||