Module:ActionCost

From Fallen London Wiki

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

p = {}

-- Calculates the cost of a storylet, assuming you try until you succeed
function p.calcCost(frame)
	-- I ain't typing frame. every time
	local args = frame.args
	
	-- Create an array for qualities gained on success
	local successQuals = {}
	-- Check if there are, in fact, any success qualities
	if tonumber(args.sucs) > 0 then
		-- If so, loop through them
		for i = 0, (tonumber(args.sucs) - 1) do
			-- and add the name and gain to the array
			table.insert(successQuals, {args["snum" .. (i+1)], args["squal" .. (i+1)]})
		end
	end
	
	-- Do the exact same thing but for failures
	local failureQuals = {}
	if tonumber(args.fals) > 0 then
		for i = 0, (tonumber(args.fals) - 1) do
			table.insert(failureQuals, {args["num" .. (i+1)], args["qual" .. (i+1)]})
		end
	end
	
	-- Find the probability of success (decimal)
	local successChance = math.min(0.6 * args.stat/args.diff, 1)
	-- Check if we're using second chances
	if args.chance == "true" then
		-- and if so, do the math to change the success chance to one appropriate with second chances
		local invProb = 1 - successChance
		successChance = 1 - invProb * invProb
	end
	-- finally, convert the probability to a percentage and floor it (to match FL)
	successChance = math.floor(100 * successChance)
	-- store the average number of failures before a success
	local costMult = 100/successChance - 1
	
	-- create a new table to store every quality
	local costs = {}
	-- loop through all the success qualities
	for key, value in pairs(successQuals) do
		-- get the gain on success and store it under the name in costs
		costs[value[2]] = value[1]
	end
	-- loop through all the failure qualities
	for key, value in pairs(failureQuals) do
		-- check if the quality is already in the list
		if costs[value[2]] then
			-- if so: get the gain on failure, multiply by the avg number of failures, and add it to the pre-existing value
			costs[value[2]] = costs[value[2]] + value[1] * costMult
		else
			-- otherwise, just set it to the gain on failure multiplied by the avg number of failures
			costs[value[2]] = value[1] * costMult
		end
	end
	-- check if we're doing second chances
	if args.chance == "true" then
		-- check if the second chance is already in the list
		if costs[args.ctype] then
			-- if so, subtract the second chances based on the total number of second chances used (avg num of failures + 1)
			costs[args.ctype] = costs[args.ctype] - costMult - 1
		else
			-- otherwise, just set it to the negative number of second chances used
			costs[args.ctype] = - costMult - 1
		end
	end
	
	-- create two lists, gains and losses
	local losses = {}
	local gains = {}
	-- loop through the costs list
	for key, value in pairs(costs) do
		-- if we're gaining a quality
		if tonumber(value) > 0 then
			-- then put it in the gains list
			gains[key] = tostring(math.floor(tonumber(value)*100)/100)
		end
		-- if we're losing a quality
		if tonumber(value) < 0 then
			-- then put it in the losses list
			losses[key] = tostring(math.floor(-tonumber(value)*100)/100)
		end
	end
	
	-- and output it all as a giant string
	str = "(Average)<br>Action Cost: " .. math.floor(100*args.act*(costMult+1))/100 .. "<br>Gains:<br><ul>"
	for key, value in pairs(gains) do
		str = str .. "<li> " .. value .. " " .. key .. "<br>"
	end
	str = str .. "</ul>Losses:<br><ul>"
	for key, value in pairs(losses) do
		str = str .. "<li> " .. value .. " " .. key .. "<br>"
	end
	return str .. "</ul>"
end

return p