Module:Silverhawks

From the RuneScape Wiki, the wiki for all things RuneScape
Jump to navigation Jump to search
Module documentation
This documentation is transcluded from Module:Silverhawks/doc. [edit] [history] [purge]
Module:Silverhawks's function feathers_for_level is invoked by Calculator:Silverhawks.
Module:Silverhawks requires Module:Addcommas.
Module:Silverhawks requires Module:Currency short.
Module:Silverhawks requires Module:ExchangeLite.
Module:Silverhawks requires Module:Experience/data.
Module:Silverhawks requires Module:Silverhawks/data.

--
-- Implements [[Calculator:Silverhawks]]
--
local p = {}

local feather_exp = require('Module:Silverhawks/data')
local XPAtLevel = require('Module:Experience/data')
local commas = require('Module:Addcommas')
local currency = require('Module:Currency_short')._amount
local shfPrice = require('Module:ExchangeLite').price('Silverhawk feathers')

function p.feathers_for_level(frame)
	local target_level = frame.args.level
	local current_xp = frame.args.xp
	local bonus_xp = frame.args.bonusxp
	local dxp = tonumber(frame.args.dxp) or 0

	if type(current_xp) == 'string' then
		 current_xp = commas._strip(current_xp)
		 current_xp = tonumber(current_xp)
	end

	if type(bonus_xp) == 'string' then
		 bonus_xp = commas._strip(bonus_xp)
		 bonus_xp = tonumber(bonus_xp)
	end

	if bonus_xp == nil then
		bonus_xp = 0
	end

	if type(target_level) == 'string' then
		 target_level = commas._strip(target_level)
		 target_level = tonumber(target_level)
	end

	local current_lvl
	local function level_at_xp(xp)
		local level
		for i=120,1,-1 do
		   if XPAtLevel[i] <= xp then
				level = i
				break
		   end
		end
		return level
	end
    
    if current_xp == 0 then
    	current_level = 1
    elseif current_xp > 120 then
        current_level = level_at_xp(current_xp)
    else
        current_level = current_xp
        current_xp = XPAtLevel[current_level]
    end
    local initial_xp = current_xp
    local initial_level = current_level
    
    local target_xp
    if target_level > 120 then
        target_xp = target_level
        target_level = level_at_xp(target_xp)
    else
	    target_xp = XPAtLevel[target_level]
    end
    
	local feathers = 0

	while current_xp < target_xp do
		feathers = feathers + 1
		current_level = level_at_xp(current_xp)
		local xp_per_feather = feather_exp[current_level]
		local bonus_xp_per
		if bonus_xp >= xp_per_feather then
			bonus_xp_per = xp_per_feather
			bonus_xp = bonus_xp - xp_per_feather
		elseif bonus_xp > 0 then
			bonus_xp_per = bonus_xp
			bonus_xp = 0
		else
			bonus_xp_per = 0
		end

		current_xp = current_xp + (1.0 + dxp) * xp_per_feather + bonus_xp_per
	end

    return 'To train from ' .. commas._add(initial_xp) .. ' experience (level ' .. initial_level .. ') to ' .. commas._add(target_xp) .. ' experience (level ' .. target_level .. '), you will need ' .. commas._add(feathers) .. ' feathers, costing ' .. currency(feathers * shfPrice, 'coins', true) .. '.'
end

--
-- Module access point
--
function p._feathers_for_level(args)
    return p.feathers_for_level(args)
end

return p