Module:NoitaUtils

From Noita Wiki
Jump to navigation Jump to search

This module exists to support functionality of various templates used on the wiki.

Usage

Invoke functions from this module like so:

{{#invoke:NoitaUtils|functionName|argument}}

Functions

getEnemyGoldValue

Calculates the amount of gold an enemy will drop based on their in-game HP values.

Parameters

  • hp - the numeric HP value of the enemy in-game.

Example

Enemy HP: 100; Gold: {{#invoke:NoitaUtils|getEnemyGoldValue|hp = 100}}

Enemy HP: 100; Gold: 40

getUIDamageValue

Calculates the UI value for damage from the internal damage numbers in the XML / lua.

Parameters

  • damage - the internal numeric damage value from the lua/xml file.

Example

Internal Damage: 1.0; UI Damage: {{#invoke:NoitaUtils|getUIDamageValue|damage = 1.0}}

Internal Damage: 1.0; UI Damage: 25

getUIHealthValue

Calculates the UI value for HP from the internal HP values in the XML / lua.

Parameters

  • hp - the internal numeric hp value from the lua/xml file.

Example

Internal HP: 1.7; UI HP: {{#invoke:NoitaUtils|getUIHealthValue|hp = 1.7}}

Internal HP: 1.7; UI HP: 42.5

getTimeValue

Calculates the time value in seconds from an input value in frames.

Parameters

  • frames - the internal numeric frames value from the lua/xml file.

Example

Time in frames: 1600; Time in seconds: {{#invoke:NoitaUtils|getTimeValue|frames = 1600}}

Time in frames: 1600; Time in seconds: 26.667

getFramesInDuration

Calculates the number of frames in a period of time given an input value in seconds.

Parameters

  • duration - a period of time in seconds.

Example

There are {{#invoke:NoitaUtils|getFramesInDuration|duration = 1.32}} frames in a period of 1.32s.

There are 79.2 frames in a period of 1.32s.

getRealExplosionDamage

Calculates the 'true' in game UI damage from a given explosion damage UI number value.

Parameters

  • damage - the explosion damage shown in the spell tooltip.

Example

Stated spell explosion damage: 400; Real spell explosion damage: {{#invoke:NoitaUtils|getRealExplosionDamage|damage = 400}}

Stated spell explosion damage: 400; Real spell explosion damage: 100

Example

Stated spell explosion damage: 40 (x3); Real spell explosion damage: {{#invoke:NoitaUtils|getRealExplosionDamage|damage = 40 (x3)}}

Stated spell explosion damage: 40 (x3); Real spell explosion damage: 10 (x3)




local p = {}
local framesPerSecond = 60

local function trim(str)
  return str:gsub("^%s*(.-)%s*$", "%1")
end

local function is_empty(str)
	return str == nil or str.len(trim(str)) == 0
end

local function append_table(tbl, data)
	tbl[#tbl + 1] = data
end

local parseFloat = require('Module:Common').string.parseFloat -- this function gets the first number from a string

function getUIHealthOrDamageValue(internalValue)
    return 25 * internalValue	
end

function formatNumeric(number)
	return ("%.5g"):format(tonumber(number))
end

function split(str, delimiter)
	local chunks = {}
	
	for c in (str..delimiter):gmatch("(.-)"..delimiter) do
	   table.insert(chunks, c)
	end
	
	return chunks
end

function subrange(t, first, last)
  local sub = {}
  
  for i = first, last do
    sub[#sub + 1] = t[i]
  end
  
  return sub
end

function p.getEnemyGoldValue(frame)
	local hpValue = frame.args["hp"]
	local internalHpValue = tonumber(hpValue) / 25
	
	return formatNumeric(math.max(1, math.floor(internalHpValue)) * 10)
end

function p.getUIDamageValue(frame)
	local internalDamageValue = tonumber(frame.args["damage"])
	return formatNumeric(getUIHealthOrDamageValue(internalDamageValue))
end

function p.getUIHealthValue(frame)
	local internalHPValue = tonumber(frame.args["hp"])
	return formatNumeric(getUIHealthOrDamageValue(internalHPValue))
end

function p.getTimeValue(frame)
	local timeInFrames = tonumber(frame.args["frames"])
	return formatNumeric(timeInFrames / framesPerSecond)
end

function p.getFramesInDuration(frame)
	local durationInSeconds = tonumber( parseFloat(frame.args["duration"]) )
	return formatNumeric(durationInSeconds * framesPerSecond)
end

function p.getRealExplosionDamage(frame)
	local damage = frame.args["damage"]
	local damageNum = 0.0
	local suffix = ""
	
	if string.find(damage, " ") then
		local parts = split(damage, " ")
		p.parts = parts
		damageNum = tonumber(parts[1])
		p.testDamageNum = damageNum
		suffix = " " .. table.concat(subrange(parts, 2, #parts), " ")
	else
		damageNum = tonumber(damage)
	end
	
	local explosionDamage = damageNum / 4.0
	
	return formatNumeric(explosionDamage) .. suffix
end

return p