Difference between revisions of "Module:Thought"

From RimWorld Wiki
Jump to navigation Jump to search
m (Undo revision 149428 by Arcangelus (talk))
Tag: Undo
m
Line 1: Line 1:
 
--[[
 
--[[
Template: Preprocessor visited node count 969/1,000,000
+
The Preprocessor visited node count was reduced from 969 to 508.
Module:  Preprocessor visited node count 508/1,000,000
 
Now that it's working properly, let's see if I can improve it.
 
 
]]
 
]]
 
local p = {}
 
local p = {}
 
local getArgs = require('Module:Arguments').getArgs
 
local getArgs = require('Module:Arguments').getArgs
local valores_ingresados={"value", "value2", "value3", "value4", "value5", "value6", "value7", "value8", "value9" }
+
--local valores_ingresados={"value", "value2", "value3", "value4", "value5", "value6", "value7", "value8", "value9" }
  
 
function p.main(frame)
 
function p.main(frame)
 
local args = getArgs(frame)
 
local args = getArgs(frame)
-- Los valores están en args en la forma { "value" = A, "value2"= B....}
 
 
local desc = tostring(args["desc"])
 
local desc = tostring(args["desc"])
 
local label = tostring(args["label"])
 
local label = tostring(args["label"])
Line 17: Line 14:
 
local multi = tonumber(args["multi"]) or 1
 
local multi = tonumber(args["multi"]) or 1
 
-- local valores = {args["value"], args["value2"], args["value3"], args["value4"], args["value5"], args["value6"], args["value7"], args["value8"], args["value9"]}
 
-- local valores = {args["value"], args["value2"], args["value3"], args["value4"], args["value5"], args["value6"], args["value7"], args["value8"], args["value9"]}
 +
-- The advantage of setting the list this way is that no nil values will get into the list.
 +
-- This means that using value9 w/o value3 is possible.
 
local valores = {}
 
local valores = {}
 
valores[#valores+1]=args["value"]
 
valores[#valores+1]=args["value"]
Line 36: Line 35:
  
 
function p._main(desc, label, duration, stack, multi, valores)
 
function p._main(desc, label, duration, stack, multi, valores)
local final_string = '<abbr title="'..desc..'">'..(label:gsub("^%l", string.upper))..'</abbr>'..(duration and " [[mood]] for "..duration.." [[Time|days]]" or " [[mood]]")
+
local final_string = duration and " [[mood]] for "..duration.." [[Time|days]]" or " [[mood]]"
  
 
-- "valores" is a list of all values given to the function.
 
-- "valores" is a list of all values given to the function.
-- This checks if the second value exist
+
-- This checks if a second value exist
 
if valores[2] then
 
if valores[2] then
-- return p.Thought(valores)..'<abbr title="'..desc..'">'..(label:gsub("^%l", string.upper))..'</abbr>'..final_string
+
return p.Thought(valores)..'<abbr title="'..desc..'">'..(label:gsub("^%l", string.upper))..'</abbr>'..final_string
return p.Thought(valores, final_string)
 
 
else --Only one value
 
else --Only one value
return p.stacks(stack, multi, valores[1], final_string)
+
return p.stacks(stack, multi, valores[1])..'<abbr title="'..desc..'">'..(label:gsub("^%l", string.upper))..'</abbr>'..final_string
-- return p.stacks(stack, multi, valores[1])..'<abbr title="'..desc..'">'..(label:gsub("^%l", string.upper))..'</abbr>'..final_string
 
 
end
 
end
 
end
 
end
  
function p.Thought(valores, final_string)--This is just the value section.
+
--[[
 +
This function returns a string if more than 1 value was defined.
 +
1. It adds all the numbers to a list
 +
1.2 Red for negatves, Green otherwise
 +
2. It concatenates all elements of said lists, wth some extras to make sense.
 +
]]
 +
 
 +
function p.Thought(valores)
 
local valores_buscados={}
 
local valores_buscados={}
 
for i, j in ipairs(valores) do
 
for i, j in ipairs(valores) do
Line 61: Line 65:
 
valores_buscados[#valores_buscados+1]=vy
 
valores_buscados[#valores_buscados+1]=vy
 
end
 
end
return "<b>"..table.concat(valores_buscados,"<b>/</b>").."</b> "..final_string
+
return "<b>"..table.concat(valores_buscados,"<b>/</b>").."</b> "..'<abbr title="'..desc..'">'..(label:gsub("^%l", string.upper))..'</abbr>'..final_string
-- return "<b>"..table.concat(valores_buscados,"<b>/</b>").."</b> "..'<abbr title="'..desc..'">'..(label:gsub("^%l", string.upper))..'</abbr>'..final_string
 
 
end
 
end
  
function p.stacks(stack, multi, value, final_string)--This is just the stacking section.
+
--[[
 +
This function return a string for the case that only 1 value was defined.
 +
1.  If a stack value was defined:
 +
1.1 Is stack equal to 1
 +
1.2 Any other case.
 +
2.  If a stack was not defined, then check the multiplier
 +
2.1 If the multiplier is equal or above one
 +
2.2 If the multiplier is below 1.
 +
 
 +
Finally, some final code to decide what color to use.
 +
1. Green for positive.
 +
2. None for 0.
 +
3. Red for negatives.
 +
]]
 +
 
 +
function p.stacks(stack, multi, value)
 
local text=""
 
local text=""
-- multi = tonumber(multi) or 1 -- Now has a default value of 1.
 
 
if stack then
 
if stack then
 
if mumlti == 1 then
 
if mumlti == 1 then
Line 83: Line 100:
  
 
if tonumber(value)>0 then
 
if tonumber(value)>0 then
return '<abbr title="'..text..'"><b><font color="forestgreen">'..value..'</font></b></abbr> '..final_string
+
return '<abbr title="'..text..'"><b><font color="forestgreen">'..value..'</font></b></abbr> '
 
elseif tonumber(value) == 0 then
 
elseif tonumber(value) == 0 then
 
return '<abbr title="'..text..'"><br>'..value..'</br></abbr> '
 
return '<abbr title="'..text..'"><br>'..value..'</br></abbr> '
 
else
 
else
return '<abbr title="'..text..'"><b><font color="firebrick">'..value..'</font></b></abbr> '..final_string
+
return '<abbr title="'..text..'"><b><font color="firebrick">'..value..'</font></b></abbr> '
 
end
 
end
 
end
 
end
  
 
return p
 
return p

Revision as of 23:23, 16 August 2024

Documentation for this module may be created at Module:Thought/doc

--[[
The Preprocessor visited node count was reduced from 969 to 508.
]]
local p = {}
local getArgs = require('Module:Arguments').getArgs
--local valores_ingresados={"value", "value2", "value3", "value4", "value5", "value6", "value7", "value8", "value9" }

function p.main(frame)
	local args = getArgs(frame)
	local desc = tostring(args["desc"])
	local label = tostring(args["label"])
	local duration = tonumber(args["duration"])
	local stack = tonumber(args["stack"])
	local multi = tonumber(args["multi"]) or 1
--	local valores = {args["value"], args["value2"], args["value3"], args["value4"], args["value5"], args["value6"], args["value7"], args["value8"], args["value9"]}
-- The advantage of setting the list this way is that no nil values will get into the list.
-- This means that using value9 w/o value3 is possible.
	local valores = {}
	valores[#valores+1]=args["value"]
	valores[#valores+1]=args["value2"]
	valores[#valores+1]=args["value3"]
	valores[#valores+1]=args["value4"]
	valores[#valores+1]=args["value5"]
	valores[#valores+1]=args["value6"]
	valores[#valores+1]=args["value7"]
	valores[#valores+1]=args["value8"]
	valores[#valores+1]=args["value9"]
	-- I believe args calls something very different on subsecuent functions.
	if tonumber(args["value"]) then
		return p._main(desc, label, duration, stack, multi, valores)
		else
			return "Missing value field."
	end
end

function p._main(desc, label, duration, stack, multi, valores)
	local final_string = duration and " [[mood]] for "..duration.." [[Time|days]]" or " [[mood]]"

	-- "valores" is a list of all values given to the function.
	-- This checks if a second value exist
	if valores[2] then
		return p.Thought(valores)..'<abbr title="'..desc..'">'..(label:gsub("^%l", string.upper))..'</abbr>'..final_string
	else --Only one value
		return p.stacks(stack, multi, valores[1])..'<abbr title="'..desc..'">'..(label:gsub("^%l", string.upper))..'</abbr>'..final_string
	end
end

--[[
This function returns a string if more than 1 value was defined.
1. It adds all the numbers to a list
1.2 Red for negatves, Green otherwise
2. It concatenates all elements of said lists, wth some extras to make sense.
]]

function p.Thought(valores)
	local valores_buscados={}
	for i, j in ipairs(valores) do
		local vx = valores[i]
		local vy = ""
		if tonumber(vx) then -- A number.
			if tonumber(vx)<0 then vy='<b><font color="firebrick">'..vx.."</font></b>" else vy='<b><font color="forestgreen">'..vx.."</font></b>" end
		else
			vy='<b>'..vx.."</b>" --The idea is to prevent a hard to track error
		end
		valores_buscados[#valores_buscados+1]=vy
	end
	return "<b>"..table.concat(valores_buscados,"<b>/</b>").."</b> "..'<abbr title="'..desc..'">'..(label:gsub("^%l", string.upper))..'</abbr>'..final_string
end

--[[
This function return a string for the case that only 1 value was defined.
1.  If a stack value was defined:
1.1 Is stack equal to 1
1.2 Any other case.
2.  If a stack was not defined, then check the multiplier
2.1 If the multiplier is equal or above one
2.2 If the multiplier is below 1.

Finally, some final code to decide what color to use.
1. Green for positive.
2. None for 0.
3. Red for negatives.
]]

function p.stacks(stack, multi, value)
	local text=""
	if stack then
		if mumlti == 1 then
			text = "Stacking "..stack.." times for a maximum of "..tostring(value*stack)
		else
			text = "Stacking "..stack.." times with a "..multi.." multiplier for maximum of "..tostring(value*( 1 - multi^stack)/(1 - multi))
		end
	else
		if multi >= 1 then --I want to avoid the case of really large numbers.
			text = "Stacking infinitely"
		else
			text = "Stacking with a "..multi.." multiplier for maximum of "..tostring(value*( 1 - multi^100)/(1 - multi) )
		end
	end

	if tonumber(value)>0 then
		return '<abbr title="'..text..'"><b><font color="forestgreen">'..value..'</font></b></abbr> '
	elseif tonumber(value) == 0 then
		return '<abbr title="'..text..'"><br>'..value..'</br></abbr> '
	else
		return '<abbr title="'..text..'"><b><font color="firebrick">'..value..'</font></b></abbr> '
	end
end

return p