## FANDOM

38,841 Pages

```-- <nowiki>
--
-- Implements {{Template:Calculator/Combat level}}
--

local p = {}

function p.calc( frame )
local args = frame:getParent().args
local attack = tonumber(args.attack or 1)
local strength = tonumber(args.strength or 1)
local defence = tonumber(args.defence or 1)
local ranged = tonumber(args.ranged or 1)
local magic = tonumber(args.magic or 1)
local constitution = tonumber(args.constitution or 10)
local prayer = tonumber(args.prayer or 1)
local summoning = tonumber(args.summoning or 1)

return p._calc( attack, strength, defence, ranged, magic, constitution, prayer, summoning )
end

function p._calc ( attack, strength, defence, ranged, magic, constitution, prayer, summoning )
-- This will be used several times:
local attstr = attack + strength

-- Used formula, with every subcalculation being floored:
-- 1/4 * [1.3 * Max(Att+Str, 2*Mag, 2*Rng) + Def + Hp + Pray/2 + Summ/2]
local lvl = ( math.max( attstr, magic * 2, ranged * 2 ) * 1.3 + defence + constitution + math.floor( prayer * 0.5 ) + math.floor( summoning *0.5 ) ) * 0.25

-- Calculate what's needed for another combat level
-- formula: 1 - (current level - floor(current level)) / weight
-- The weight of pray/summ for example is 1/8, that of def/hp is 1/4 etc.
local HpDef = math.ceil( ( 1 - ( lvl % 1 ) ) * 4 )
local PraySumm = math.ceil( ( 1 - ( lvl % 1 ) ) * 8 )
local AttStr, Mage, Range
local cbtype

if attstr >= 2*magic and attstr >= 2*ranged then
cbtype = 'melee'
AttStr = math.ceil ( ( 1 - ( lvl % 1 ) ) / 0.325 )
Mage = math.ceil( ( attstr - magic * 2 ) / 2 + ( 1 - ( lvl % 1 ) ) / 0.65 )
Range = math.ceil( ( attstr - ranged * 2 ) / 2 + ( 1 - ( lvl % 1 ) ) / 0.65 )
else
-- calculate att/str levels needed for combat level up:
-- first calculate how many levels to get to make your combat melee-based, then add the amount of levels needed from there.
AttStr = math.max( ranged, magic ) * 2 - attstr + math.ceil( ( 1 - ( lvl % 1 ) ) / 0.325 )

-- store this value in variable Mage first: assume mage-based combat first
Mage = math.ceil( ( 1 - ( lvl % 1 ) ) / 0.65 )
if ranged > magic then
cbtype = 'ranged'
-- move the calculated value to variable Range: the combat is range-based
Range = Mage
-- same logic for melee: amount of levels to get mage-based combat PLUS levels to another cb from there
Mage = ranged - magic + Range
else
cbtype = 'magic'
-- same logic again
Range = magic - ranged + Mage
end
end

local level = 'Your combat level is <b>' .. math.floor( lvl ) .. '</b>, ' .. cbtype .. '-based. '
local tips = 'For level ' .. ( math.floor( lvl ) + 1 ) .. ', you need one of:\n*' ..
HpDef .. ' [[Constitution]] or [[Defence]] levels,\n*' ..
(prayer % 2 == 0 and PraySumm+1 or PraySumm) .. ' [[Prayer]] levels,\n*' ..
(summoning % 2 == 0 and PraySumm+1 or PraySumm) .. ' [[Summoning]] levels,\n*' ..
AttStr .. ' [[Attack]] or [[Strength]] levels,\n*' ..
Mage .. ' [[Magic]] levels,\n*' ..
Range .. ' [[Ranged]] levels.\n'
return level .. tips

end

return p```