Toggle menu
1.3K
1.7K
705
23.9K
Veloren Wiki
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

Module:Weapon

From Veloren Wiki

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

local weapons_table = mw.text.jsonDecode(mw.getCurrentFrame():expandTemplate{title='Template:Weapons.json'})



function round(number, decimals)
    local power = 10^decimals
    return math.floor(number * power) / power
end

function percent(number)
    return round((number - 1) * 100, 2) .. "%"
end

function choose(var, default)
	if var ~= nil then
		return var
	end
	return default
end

function get_info(weapon_name, field, default)
	for k, v in pairs(weapons_table) do
		if v['name'] == weapon_name then
			return choose(v[field], default)
		end
	end
	return default
end




local p = {}



p.getWeaponImage = function(frame)
	local weapon_name = frame.args[1]
	return "Veloren_" .. weapon_name .. ".png"
end

p.getWeaponName = function(frame)
	local weapon_name = frame.args[1]
	return weapon_name
end

p.getWeaponPath = function(frame)
	local weapon_name = frame.args[1]
	local value = get_info(weapon_name, 'path', nil)
	return value
end

p.getWeaponQuality = function(frame)
	local weapon_name = frame.args[1]
	local value = get_info(weapon_name, 'quality', 'Common')
	return frame:expandTemplate{ title = value }
end

p.getWeaponDesc = function(frame)
	local weapon_name = frame.args[1]
	local value = get_info(weapon_name, 'desc', nil)
	return value
end



p.getWeaponDamage = function(frame)
	local weapon_name = frame.args[1]
	local value = get_info(weapon_name, 'power', 0)
	return value * 10
end

p.getWeaponRating = function(frame)
	local weapon_name = frame.args[1]
	local value = get_info(weapon_name, 'rating', 0)
	return round(value, 2) * 10
end

p.getWeaponCritChance = function(frame)
	local weapon_name = frame.args[1]
	local value = get_info(weapon_name, 'critchance', 0)
	return round(value * 100, 2) .. "%"
end



p.getWeaponSpeed = function(frame)
	local weapon_name = frame.args[1]
	local value = get_info(weapon_name, 'speed', 1)
	return percent(value)
end

p.getWeaponPoise = function(frame)
	local weapon_name = frame.args[1]
	local poise = get_info(weapon_name, 'poise', 1)
	return percent(poise)
end

p.getWeaponRange = function(frame)
	local weapon_name = frame.args[1]
	local value = get_info(weapon_name, 'range', 1)
	return percent(value)
end

p.getWeaponEnergy = function(frame)
	local weapon_name = frame.args[1]
	local value = get_info(weapon_name, 'energy', 1)
	return percent(value)
end

p.getWeaponBuff = function(frame)
	local weapon_name = frame.args[1]
	local value = get_info(weapon_name, 'buff', 1)
	return percent(value)
end





p.getWeapon = function(frame)
	local name = frame.args[1]
	
	for k, v in pairs(weapons_table) do
		if v['name'] == name then
			image = name
			path = choose(v['path'], nil)
			quality = choose(v['quality'], 'Common')
			desc = choose(v['desc'], nil)
			
			damage = choose(v['damage'], 0) 
			rating = choose(v['rating'], 0)
			critchance = choose(v['critchance'], 0)
			
			poise = choose(v['poise'], 1)
			speed = choose(v['speed'], 1)
			range = choose(v['range'], 1)
			energy = choose(v['energy'], 1)
			buff = choose(v['buff'], 1)
		end
	end
	
	txt = frame:expandTemplate{ title = 'Infobox weapon', args = {
		image = image .. '.png',
		title = name,
		description = desc,
		quality = quality,
		
		power = damage*10,
		rating = rating,
		critchance = critchance*10 .. '%',
		
		poise = percent(poise),
		speed = percent(speed),
		range = percent(range),
		energy = percent(energy),
		buff = percent(buff),
	} }
	return txt
end

return p
Cookies help us deliver our services. By using our services, you agree to our use of cookies.