noita-mapcap/files/util.lua
David Vogel f58b005155 Several fixes and improvements
- Change vscode Lua runtime to LuaJIT
- Remove worm entity overrides
- Remove preparePlayer() that sets HP and other stuff
- Change how entites are captured
- Increase entity capture radius
- Capture entities as soon as possible
- Modify entites immediately after they are captured
- Make stringArgs in print local
- Fix JSON marshaling of Noita vectors
- Fix NoitaEntity:GetComponents() and NoitaEntity:GetFirstComponent()
- Add varArg support to NoitaComponent:SetValue() and NoitaComponent:ObjectSetValue()
- Update some EmmyLua annotations
- Fix custom GamePrint
- Add table.pack function that is missing in LuaJIT
2022-07-18 22:07:53 +02:00

118 lines
2.8 KiB
Lua

-- Copyright (c) 2019-2022 David Vogel
--
-- This software is released under the MIT License.
-- https://opensource.org/licenses/MIT
function SplitStringByLength(string, length)
local chunks = {}
for i = 1, #string, length do
table.insert(chunks, string:sub(i, i + length - 1))
end
return chunks
end
-- Improved version of GamePrint, that behaves more like print.
local oldGamePrint = GamePrint
function GamePrint(...)
local arg = {...}
local result = ""
for i, v in ipairs(arg) do
result = result .. tostring(v) .. " "
end
for line in result:gmatch("[^\r\n]+") do
for i, v in ipairs(SplitStringByLength(line, 100)) do
oldGamePrint(v)
end
end
end
function getPlayer()
local players = EntityGetWithTag("player_unit")
if players == nil or #players < 1 then
return nil
end
return players[1]
end
function getPlayerPos()
return EntityGetTransform(getPlayer())
end
function teleportPlayer(x, y)
EntitySetTransform(getPlayer(), x, y)
end
function setPlayerHP(hp)
local damagemodels = EntityGetComponent(getPlayer(), "DamageModelComponent")
if damagemodels ~= nil then
for i, damagemodel in ipairs(damagemodels) do
ComponentSetValue(damagemodel, "max_hp", hp)
ComponentSetValue(damagemodel, "hp", hp)
end
end
end
function addEffectToEntity(entity, gameEffect)
local gameEffectComp = GetGameEffectLoadTo(entity, gameEffect, true)
if gameEffectComp ~= nil then
ComponentSetValue(gameEffectComp, "frames", "-1")
end
end
function addPerkToPlayer(perkID)
local playerEntity = getPlayer()
local x, y = getPlayerPos()
local perkData = get_perk_with_id(perk_list, perkID)
-- Add effect
addEffectToEntity(playerEntity, perkData.game_effect)
-- Add ui icon etc
--[[local perkIcon = EntityCreateNew("")
EntityAddComponent(
perkIcon,
"UIIconComponent",
{
name = perkData.ui_name,
description = perkData.ui_description,
icon_sprite_file = perkData.ui_icon
}
)
EntityAddChild(playerEntity, perkIcon)]]
--local effect = EntityLoad("data/entities/misc/effect_protection_all.xml", x, y)
--EntityAddChild(playerEntity, effect)
end
function fileExists(fileName)
local f = io.open(fileName, "r")
if f ~= nil then
io.close(f)
return true
else
return false
end
end
function progressBarString(progress, look)
local factor = progress.Progress / progress.Max
local count = math.ceil(look.BarLength * factor)
local barString = string.rep(look.CharFull, count) .. string.rep(look.CharEmpty, look.BarLength - count)
return string.format(look.Format, barString, progress.Progress, progress.Max, factor * 100)
end
---Returns a new table with all arguments stored into keys `1`, `2`, etc. and with a field `"n"` with the total number of arguments.
---@param ... any
---@return table
function table.pack(...)
t = {...}
t.n = select("#", ...)
return t
end