sm64coopdx/mods/day-night-cycle/a-utils.lua

121 lines
3.8 KiB
Lua
Raw Normal View History

2024-01-01 21:41:28 +00:00
if SM64COOPDX_VERSION == nil then return end
2023-12-18 22:01:33 +00:00
-- localize functions to improve performance
2024-06-30 19:38:31 +00:00
local string_format,table_insert,math_floor,math_ceil,level_is_vanilla_level,djui_hud_get_color,djui_hud_set_color,djui_hud_print_text,type,obj_get_first_with_behavior_id = string.format,table.insert,math.floor,math.ceil,level_is_vanilla_level,djui_hud_get_color,djui_hud_set_color,djui_hud_print_text,type,obj_get_first_with_behavior_id
2023-12-18 22:01:33 +00:00
2024-06-30 19:38:31 +00:00
--- @param cond boolean
--- Human readable ternary operator
2023-12-18 22:01:33 +00:00
function if_then_else(cond, ifTrue, ifFalse)
if cond then return ifTrue end
return ifFalse
end
2024-06-30 19:38:31 +00:00
--- @param s string
--- Splits a string into a table by spaces
2023-12-18 22:01:33 +00:00
function split(s)
local result = {}
2024-06-30 19:38:31 +00:00
for match in (s):gmatch(string_format("[^%s]+", " ")) do
2023-12-18 22:01:33 +00:00
table_insert(result, match)
end
return result
end
2024-06-30 19:38:31 +00:00
--- @param x number
--- @return integer
--- Rounds up or down depending on the decimal position of `x`
function math_round(x)
return if_then_else(x - math_floor(x) >= 0.5, math_ceil(x), math_floor(x))
end
--- @param a number
--- @param b number
--- @param t number
--- Linearly interpolates between two points using a delta
function lerp(a, b, t)
return a * (1 - t) + b * t
end
--- @param a number
--- @param b number
--- @param t number
--- Linearly interpolates between two points using a delta but rounds the final value
function lerp_round(a, b, t)
return math_round(lerp(a, b, t))
end
--- @param a number
--- @param b number
--- @param t number
--- Linearly interpolates between two points using a delta but ceils the final value
function lerp_ceil(a, b, t)
return math_ceil(lerp(a, b, t))
end
2023-12-18 22:01:33 +00:00
--- @param a Color
--- @param b Color
--- @return Color
2024-06-30 19:38:31 +00:00
--- Linearly interpolates between two colors using a delta
2023-12-18 22:01:33 +00:00
function color_lerp(a, b, t)
return {
2024-06-30 19:38:31 +00:00
r = lerp_round(a.r, b.r, t),
g = lerp_round(a.g, b.g, t),
b = lerp_round(a.b, b.b, t)
2023-12-18 22:01:33 +00:00
}
end
2024-06-30 19:38:31 +00:00
--- @param priority integer
--- @param seqId SeqId
2023-12-18 22:01:33 +00:00
function SEQUENCE_ARGS(priority, seqId)
return ((priority << 8) | seqId)
end
2024-06-30 19:38:31 +00:00
--- @param value boolean
--- Returns an on or off string depending on value
2023-12-18 22:01:33 +00:00
function on_or_off(value)
if value then return "\\#00ff00\\ON" end
return "\\#ff0000\\OFF"
end
2024-06-30 19:38:31 +00:00
--- @param levelNum LevelNum
--- Returns whether or not the local player is in a vanilla level
2023-12-18 22:01:33 +00:00
function in_vanilla_level(levelNum)
return gNetworkPlayers[0].currLevelNum == levelNum and level_is_vanilla_level(levelNum)
end
2024-06-30 19:38:31 +00:00
--- @param message string
--- @param x number
--- @param y number
--- @param scale number
--- @param outlineBrightness number
--- Prints outlined DJUI HUD text
function djui_hud_print_outlined_text(message, x, y, scale, outlineBrightness)
local color = djui_hud_get_color()
djui_hud_set_color(color.r * outlineBrightness, color.g * outlineBrightness, color.b * outlineBrightness, color.a)
djui_hud_print_text(message, x - 1, y, scale)
djui_hud_print_text(message, x + 1, y, scale)
djui_hud_print_text(message, x, y - 1, scale)
djui_hud_print_text(message, x, y + 1, scale)
djui_hud_set_color(color.r, color.g, color.b, color.a)
djui_hud_print_text(message, x, y, scale)
end
--- @param table table
--- Clones a table out of an existing one, useful for making referenceless tables
function table_clone(table)
local clone = {}
for key, value in pairs(table) do
if type(value) == "table" then
clone[key] = table_clone(value) -- recursive call for nested tables
else
clone[key] = value
end
end
return clone
end
function check_common_hud_render_cancels()
local action = gMarioStates[0].action
return obj_get_first_with_behavior_id(id_bhvActSelector) ~= nil or
gNetworkPlayers[0].currActNum == 99 or
action == ACT_END_PEACH_CUTSCENE or action == ACT_END_WAVING_CUTSCENE or action == ACT_CREDITS_CUTSCENE
2023-12-18 22:01:33 +00:00
end