Módulo:Modifier

Fonte: Terraria Wiki
Saltar para a navegação Saltar para a pesquisa

A documentação para este módulo pode ser criada na página Módulo:Modifier/doc

local trim = mw.text.trim
local ceil = math.ceil
local cargo = mw.ext.cargo
local possibleModifiers = require('Module:Possible modifiers').getPossibleModifiers

local currentFrame

local getArg = function(key)
	local value = currentFrame.args[key]
	if not value then
		return nil
	end
	value = trim(value)
	if value == '' then
		return nil
	end
	return value
end

local round = function(x)
	-- MidpointRounding.ToEven instead of MidpointRounding.AwayFromZero (see https://docs.microsoft.com/en-us/dotnet/api/system.math.round?view=net-5.0#System_Math_Round_System_Double_)
	if (x+0.5) == ceil(x) then -- is midpoint
		return (x-0.5) % 2 == 0 and x-0.5 or x+0.5 -- round down if lower number is even, round up if it is odd
	else -- is not midpoint
		return ceil(x-0.5) -- regular rounding
	end
end

local function arrayToSet(t)
	local set = {}
	for _, v in ipairs(t) do
		set[v] = true
	end
	return set
end

local function getToolModifier(item, modifierinfoSpeed)
	if item.possibleModifiers[15] ~= nil then -- can get Light
		local low = ''
		local alt = ''
		local combatmodifier = ''
		if item.possibleModifiers[81] ~= nil then -- can get Legendary
			combatmodifier = 81
		elseif item.possibleModifiers[44] ~= nil then -- can get Agile
			combatmodifier = 44
			low = 'low'
		end
		-- check if Light causes the same toolspeed bonus as the combat modifier option
		if combatmodifier ~= '' and item.toolspeed ~= '' then
			if round(item.toolspeed*modifierinfoSpeed[combatmodifier]) == round(item.toolspeed*modifierinfoSpeed[15]) then
				alt = 'alt'
			end
		end
		return 'harvest' .. low .. alt
	end
	return ''
end

local function getWeaponModifier(item)
	if item.damagetype == 'melee' then
		if item.possibleModifiers[81] then -- can get Legendary
			return 'melee'
		elseif item.possibleModifiers[59] then -- can get Godly
			return 'meleealt'
		elseif item.possibleModifiers[43] then -- can get Deadly
			return 'nokb'
		elseif item.possibleModifiers[60] then -- can get Demonic
			return 'nokbalt'
		end 
	elseif item.damagetype == 'ranged' then
		if item.possibleModifiers[82] then -- can get Unreal
			return 'ranged'
		elseif item.possibleModifiers[60] then -- can get Demonic
			return 'rangednokb'
		end
	elseif item.damagetype == 'magic' then
		if item.possibleModifiers[83] then -- can get Mythical
			return 'magic'
		elseif item.possibleModifiers[28] then -- can get Masterful
			return 'magiclow'
		elseif item.possibleModifiers[59] then -- can get Godly
			return 'magicverylow'
		elseif item.possibleModifiers[26] then -- can get Mystic
			return 'magicnokb'
		elseif item.possibleModifiers[60] then -- can get Demonic
			return 'magiclownokb'
		end
	elseif item.damagetype == 'summon' then
		if item.listcat:find('whips', 1, true) then
			return 'whip'
		elseif item.possibleModifiers[57] then -- can get Ruthless
			return 'summon'
		elseif item.possibleModifiers[43] then -- can get Deadly
			return 'nokb'
		elseif item.possibleModifiers[60] then -- can get Demonic
			return 'nokbalt'
		end
	end
	return 'none'
end


-----------------------------------------------------------------
-- main return object
return {

go = function(frame, args)
	-- init cache
	currentFrame = frame
	
	local itemname = getArg('itemname')
	local itemid = getArg('itemid')
	
	local modifierinfoSpeed = {
		[44] = getArg('modifierinfo_speed:44'),
		[81] = getArg('modifierinfo_speed:81'),
		[15] = getArg('modifierinfo_speed:15'),
	}
	
	-- fetch info about the item:
	local item = cargo.query('Items', 'type, listcat, damagetype, toolspeed', {
		where = 'name="' .. itemname .. '" AND itemid='.. itemid,
		limit = 1,
	})[1]
	item.possibleModifiers = possibleModifiers(itemid)
	if #item.possibleModifiers == 0 then -- couldn't find any possible modifiers for this item
		return 'none'
	end
	item.possibleModifiers = arrayToSet(item.possibleModifiers)

	-- normalize
	for _, stat in ipairs({'type', 'listcat', 'damagetype', 'toolspeed'}) do
		item[stat] = item[stat] == nil and '' or item[stat]:lower() -- nil to empty string, non-nil to lowercase
	end
	if item.damagetype:find('melee', 1, true) then
		item.damagetype = 'melee'
	elseif item.damagetype:find('ranged', 1, true) then
		item.damagetype = 'ranged'
	elseif item.damagetype:find('magic', 1, true) then
		item.damagetype = 'magic'
	elseif item.damagetype:find('summon', 1, true) then
		item.damagetype = 'summon'
	end
	item.toolspeed = item.toolspeed:gsub('^%D-(%d+).*$', '%1') -- strip non-numerical characters
	
	local isTool = item['type']:find('tool', 1, true) and not item['type']:find('weapon', 1, true) -- has tool type and does not have weapon type
	
	local result = ''
	if isTool then
		result = getToolModifier(item, modifierinfoSpeed)
		if result == '' then
			item.damagetype = 'melee'
		end
	end
	if result == '' then
		result = getWeaponModifier(item)
	end
	
	return result
	
end,
}