Difference between revisions of "Module:Stat Factors Table"

From RimWorld Wiki
Jump to navigation Jump to search
(Creating module for the template "Stat Factors Table Row". Slightly different implementation from test, so it may take a few edits to get working.)
 
m (It seems skillBonus was not properly sanitized.)
 
(6 intermediate revisions by the same user not shown)
Line 1: Line 1:
local getArgs = require('Module:Arguments').getArgs
+
local getArgs -- lazily initialized
 
local p = {}
 
local p = {}
--local wrap = {} -- Holds wrapper functions that process arguments from #invoke. These act as intemediary between functions meant for #invoke and functions meant for Lua.
+
local wrap = {} -- Holds wrapper functions that process arguments from #invoke. These act as intemediary between functions meant for #invoke and functions meant for Lua.
 +
 
 +
local function unpackNumberArgs(args)
 +
-- Returns an unpacked list of arguments specified with numerical keys.
 +
local ret = {}
 +
for k, v in pairs(args) do
 +
if type(k) == 'number' then
 +
table.insert(ret, v)
 +
end
 +
end
 +
return unpack(ret)
 +
end
  
 
--Stat Factors Table Row
 
--Stat Factors Table Row
function p.TableRow(argumento)
+
 
local args = getArgs(argumento)
+
function wrap.TableRow(args)
return p._TableRow(args)
+
return p._TableRow(unpackNumberArgs(args))
 
end
 
end
 
 
function p._TableRow(skillBase, skillBonus, statMin, statMax, capImportance, capLimit, resultCols, LV, Ln)
 
function p._TableRow(skillBase, skillBonus, statMin, statMax, capImportance, capLimit, resultCols, LV, Ln)
argumentos={skillBase,skillBonus,statMin,statMax,capImportance,capLimit,resultCols,LV,Ln}
+
--Remove local if these variables are to be used somewhere else.
 +
local argumentos={skillBase,skillBonus,statMin,statMax,capImportance,capLimit,resultCols,LV,Ln}
 
 
 
for i = 1,8 do --This should prevent errors if a number is not defined.
 
for i = 1,8 do --This should prevent errors if a number is not defined.
Line 17: Line 28:
 
end
 
end
 
end
 
end
-- Do note that statMin, statMax are handled by "Template:Stat Factors Table".
+
-- Do note that statMin, statMax are handled by "Template:Stat Factors Table" (including defaults)
-- While I could set fallbacks, I decided against it.
+
 
+
if tonumber(Ln)==nil then --Sanitizes input and allows for Ln=0.
if tonumber(Ln)==nil then --Sanitizes input and allows for 0.
+
factor = skillBase + (tonumber(skillBonus) or 0 )* LV
factor = skillBase + skillBonus * LV
 
 
else
 
else
 
factor = Ln
 
factor = Ln
Line 45: Line 55:
 
 
 
return "|-\r\n!"..LV.."\r\n|"..R_Pval..R_Sval..R_Tval
 
return "|-\r\n!"..LV.."\r\n|"..R_Pval..R_Sval..R_Tval
 +
-- There are more efficient ways, but this works.
 
end
 
end
 +
 +
--[[
 +
Wrapper function that does basic argument processing. This ensures that all functions from #invoke can use either the current
 +
frame or the parent frame, and it also trims whitespace for all arguments and removes blank arguments.
 +
]]
 +
 +
local mt = { __index = function(t, k)
 +
return function(frame)
 +
if not getArgs then
 +
getArgs = require('Module:Arguments').getArgs
 +
end
 +
return wrap[k](getArgs(frame))  -- Argument processing is left to Module:Arguments. Whitespace is trimmed and blank arguments are removed.
 +
end
 +
end }
 +
 +
return setmetatable(p, mt)
 +
  
 
-- END OF MODULE
 
-- END OF MODULE

Latest revision as of 12:51, 12 August 2024

This module is meant to be used by Template:Stat Factors Table Row, which in turn is meant to be used alogside Template:Stat Factors Table.

This module returns the rows used in said table. It accept 8 inputs: skillBase, skillBonus, statMin, statMax, capImportance, capLimit, resultCols, LV, Ln

Any value not given is assumed 0. There are exceptions for the default values of Stat Maximum (99999999) and statMin (0), both of which are defined on Template:Stat Factors Table.

This can probably be further improved. Regardless, this represents a 71% improvement over the ParserFunctions method.


local getArgs -- lazily initialized
local p = {}
local wrap = {} -- Holds wrapper functions that process arguments from #invoke. These act as intemediary between functions meant for #invoke and functions meant for Lua.

local function unpackNumberArgs(args)
	-- Returns an unpacked list of arguments specified with numerical keys.
	local ret = {}
	for k, v in pairs(args) do
		if type(k) == 'number' then
			table.insert(ret, v)
		end
	end
	return unpack(ret)
end

--Stat Factors Table Row

function wrap.TableRow(args)
	return p._TableRow(unpackNumberArgs(args))
end
function p._TableRow(skillBase, skillBonus, statMin, statMax, capImportance, capLimit, resultCols, LV, Ln)
	--Remove local if these variables are to be used somewhere else. 
	local argumentos={skillBase,skillBonus,statMin,statMax,capImportance,capLimit,resultCols,LV,Ln}
	
	for i = 1,8 do --This should prevent errors if a number is not defined.
		if type(argumentos[i])~='number' then
			argumentos[i]=0
		end
	end
	-- Do note that statMin, statMax are handled by "Template:Stat Factors Table" (including defaults)

	if tonumber(Ln)==nil then --Sanitizes input and allows for Ln=0.
		factor = skillBase + (tonumber(skillBonus) or 0 )* LV
	else
		factor = Ln
	end

	local Pval = math.min(math.max(factor,statMin),statMax)
	R_Pval = tostring(math.floor(Pval*10000+0.5)/100).."%" -- This formats the number as a 2 digit percent value, rounded up.
	if tonumber(resultCols)>1 then
		Pval = factor * ( 1 + capImportance * math.min(capLimit-1, 0.25))
		Pval = math.min(math.max(Pval,statMin),statMax)
		R_Sval="<td>"..tostring(math.floor(Pval*10000+0.5)/100).."% </td>"
	else
		R_Sval=""
	end

	if tonumber(resultCols)>2 then
		Pval = factor * ( 1 + capImportance * math.min(capLimit-1, 0.5))
		Pval = math.min(math.max(Pval,statMin),statMax)
		R_Tval="<td>"..tostring(math.floor(Pval*10000+0.5)/100).."% </td>"
	else
		R_Tval=""
	end
	
	return "|-\r\n!"..LV.."\r\n|"..R_Pval..R_Sval..R_Tval
	-- There are more efficient ways, but this works.
end

--[[
Wrapper function that does basic argument processing. This ensures that all functions from #invoke can use either the current
frame or the parent frame, and it also trims whitespace for all arguments and removes blank arguments.
]]

local mt = { __index = function(t, k)
	return function(frame)
		if not getArgs then
			getArgs = require('Module:Arguments').getArgs
		end
		return wrap[k](getArgs(frame))  -- Argument processing is left to Module:Arguments. Whitespace is trimmed and blank arguments are removed.
	end
end }

return setmetatable(p, mt)


-- END OF MODULE