Module:Sandbox/User:Sfoxrs/divert

Revision as of 13:49, 10 March 2021 by en>Sfoxrs
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Documentation for this module may be created at Module:Sandbox/User:Sfoxrs/divert/doc

local p = {}
local lang = mw.getContentLanguage()
local yesno = require ("Module:Yesno")
local htnmlext = require('Module:Mw.html extension')

function p.main(frame)
	
    local args = frame:getParent().args
    
    local setting = args.setting
    local damageTaken = tonumber(args.damageTaken) or 1
    local adrenalineDesired = tonumber(args.adrenalineDesired) or 0
    local shieldTier = tonumber(args.shieldTier) or 1
    local nati = yesno(args.nati) or false
    
    local divisor = 200 - shieldTier
    
	if (setting=="Adrenaline") then 
		return calcAdrenalineGained(damageTaken,shieldTier,nati,divisor)
	elseif (setting=="Damage") then 
		return calcDamageNeeded(adrenalineDesired,shieldTier,nati,divisor)
	end
	
end

function calcAdrenalineGained(damageTaken,shieldTier,nati,divisor)
	
    local upto3k = math.floor(math.min(damageTaken,3000)/divisor) * 0.8
    local upto6k = math.floor(math.max(math.min(damageTaken-3000,3000),0)/divisor) * 0.6
    local upto9k = math.floor(math.max(math.min(damageTaken-6000,3000),0)/divisor) * 0.5
    
    local adrenGain = math.min(upto3k + upto6k + upto9k,50)
    
    if (nati) then adrenGain = adrenGain * 2 end
    
    local toreturnp = mw.html.create('div')
    toreturnp:css('margin', '0 0 0')
		:wikitext(string.format('Adrenaline gained: %s%%', adrenGain))
		:done()
    
    return toreturnp
    
end

function calcDamageNeeded(adrenalineDesired,shieldTier,nati,divisor)
	
	--Adrenaline values used are 10x what they should be for the purpose of fixing a comparison issue (this includes the adrenaline returned from Divert: 0.8 -> 8, 0.6 -> 6, 0.5 -> 5). This is later corrected in one of the outputs by dividing the adrenaline value by 10.
	adrenalineDesired = math.floor(adrenalineDesired*10)
	local natiMod = 0
	if (nati) then natiMod = 1 end
	
	local nAdrenalineValuesPerBlock = math.floor(3000/divisor)
	local upTo3kBlock = nAdrenalineValuesPerBlock * 8 * (1 + natiMod)
	local upTo6kBlock = nAdrenalineValuesPerBlock * (8 + 6) * (1 + natiMod)
	local upTo9kBlock = nAdrenalineValuesPerBlock * (8 + 6 + 5) * (1 + natiMod)
	
	local minimumDamageNeeded = -999
	
	if (adrenalineDesired <= upTo3kBlock) then
		minimumDamageNeeded = math.ceil(adrenalineDesired/8/(1 + natiMod))*divisor
	elseif (adrenalineDesired > upTo3kBlock and adrenalineDesired <= upTo6kBlock) then
		minimumDamageNeeded = 3000 + math.ceil((adrenalineDesired-upTo3kBlock)/6/(1 + natiMod))*divisor
	elseif (adrenalineDesired > upTo6kBlock and adrenalineDesired <= upTo9kBlock) then
		minimumDamageNeeded = 6000 + math.ceil((adrenalineDesired-upTo6kBlock)/5/(1 + natiMod))*divisor
	end
	
	if(adrenalineDesired > 500  and adrenalineDesired <= 1000 and natiMod == 0) then minimumDamageNeeded = -1
	elseif(adrenalineDesired > 1000) then minimumDamageNeeded = -2
	end
	
	local toreturnp = mw.html.create('div')	
    toreturnp:css('margin', '0 0 0')
    :IF(minimumDamageNeeded > -1)
		:wikitext(string.format('Minimum damage to Divert: %s', lang:formatNum(math.ceil(minimumDamageNeeded))))
    	:done()
	:ELSEIF(minimumDamageNeeded == -1)
		:wikitext(string.format('Not possible to gain %s%% adrenaline with this shield tier and Natural Instinct setting.', adrenalineDesired/10))
    	:done()	
    :ELSEIF(minimumDamageNeeded == -2)
    	:wikitext('Not possible to have Divert give over 100% adrenaline.')
    	:done()	
    :ELSE()
    	:wikitext('Error in minimum damage calculation.')
    	:done()	
    :END()
    
    return toreturnp

end

return p