Toggle menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

Module:DiceUtils

From bg3.wiki

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

local p = {}

function p.calculateDamage(frame)
	args = frame.args
	
	result = {}
	damage = {
		Acid = {},
		Cold = {},
		Fire = {},
		Force = {},
		Healing = {},
		Lightning = {},
		Necrotic = {},
		Poison = {},
		Radiant = {},
		Thunder = {},
		Psychic = {},
		Physical = {},
		Piercing = {},
		Bludgeoning = {},
		Slashing = {}
	}
	
	for damage_die in string.gmatch(args.dice, "([^,]+)") do
		idx = string.find(damage_die, ":")
		die = string.sub(damage_die, 1, idx - 1)
		d_type = string.sub(damage_die, idx + 1)
		
		table.insert(damage[d_type], die)
	end
	
	for d_type, dice in pairs(damage) do
		if not (next(dice) == nil) then
			min_damage = 0
			max_damage = 0
			if not (frame.args.modifier == nil) then
				min_damage = min_damage + frame.args.modifier
				max_damage = max_damage + frame.args.modifier
			end
			
			avg_str = ""
			for i, die in ipairs(dice) do
				idx = string.find(die, "d")
				count = tonumber(string.sub(die, 1, idx -1))
				value = tonumber(string.sub(die, idx + 1))
				min_damage = min_damage + count
				max_damage = max_damage + value * count
				
				avg_str = string.format("(%d~%d)", min_damage, max_damage)
			end
			
			if not (frame.args.modifier == nil) then
				if frame.args.modifier > 0 then
					damage_str = string.format("%s +%d %s", table.concat(dice, " + "), frame.args.modifier, avg_str)
				else
					damage_str = string.format("%s %d %s", table.concat(dice, " + "), frame.args.modifier, avg_str)
				end
			else
				damage_str = string.format("%s %s", table.concat(dice, " + "), avg_str)
			end

			if frame.args.append_type then
				damage_str = string.format("%s %s", damage_str, d_type)
			end
			
			table.insert(result, damage_str)
		end
	end
	
	return table.concat(result, " + ")

end

return p