Difference between revisions of "Module:Stat Factors Table"

From RimWorld Wiki
Jump to navigation Jump to search
m
m
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.
  
 
--Stat Factors Table Row
 
--Stat Factors Table Row
function wrap.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}
 
argumentos={skillBase,skillBonus,statMin,statMax,capImportance,capLimit,resultCols,LV,Ln}
Line 45: Line 44:
 
 
 
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
 +
  
 
-- END OF MODULE
 
-- END OF MODULE

Revision as of 18:06, 11 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.

--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)
	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".
	-- While I could set fallbacks, I decided against it.
	
	if tonumber(Ln)==nil then --Sanitizes input and allows for 0.
		factor = skillBase + skillBonus * 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


-- END OF MODULE