diff --git a/files/capture.lua b/files/capture.lua index 56cbbde..5830f7f 100644 --- a/files/capture.lua +++ b/files/capture.lua @@ -3,81 +3,11 @@ -- This software is released under the MIT License. -- https://opensource.org/licenses/MIT -dofile("mods/noita-mapcap/files/util.lua") -dofile("mods/noita-mapcap/files/external.lua") -dofile("mods/noita-mapcap/files/ws.lua") - ---dofile("data/scripts/lib/utilities.lua") -dofile("data/scripts/perks/perk_list.lua") - -if not async then -- Check if lib is already loaded - dofile("data/scripts/lib/coroutines.lua") -end - local CAPTURE_PIXEL_SIZE = 2 -- in FullHD a ingame pixel is expected to be 2 real pixels local CAPTURE_GRID_SIZE = 128 -- in ingame pixels local CAPTURE_DELAY = 15 -- in frames local CAPTURE_FORCE_HP = 40 -- * 25HP -local function getPlayer() - local players = EntityGetWithTag("player_unit") - if players == nil or #players < 1 then - return nil - end - return players[1] -end - -local function getPlayerPos() - return EntityGetTransform(getPlayer()) -end - -local function teleportPlayer(x, y) - EntitySetTransform(getPlayer(), x, y) -end - -local 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 - -local function addEffectToEntity(entity, gameEffect) - local gameEffectComp = GetGameEffectLoadTo(entity, gameEffect, true) - if gameEffectComp ~= nil then - ComponentSetValue(gameEffectComp, "frames", "-1") - end -end - -local 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 - local function preparePlayer() local playerEntity = getPlayer() addEffectToEntity(playerEntity, "PROTECTION_ALL") @@ -95,7 +25,7 @@ local function resetPlayer() setPlayerHP(CAPTURE_FORCE_HP) end -local function startCapturing() +function startCapturing() local ox, oy = getPlayerPos() ox, oy = math.floor(ox / CAPTURE_GRID_SIZE) * CAPTURE_GRID_SIZE, math.floor(oy / CAPTURE_GRID_SIZE) * CAPTURE_GRID_SIZE local x, y = ox, oy @@ -141,34 +71,3 @@ local function startCapturing() end ) end - --- #### UI #### - -local gui = GuiCreate() - -async_loop( - function() - if gui ~= nil then - GuiStartFrame(gui) - - GuiLayoutBeginVertical(gui, 50, 20) - if GuiButton(gui, 0, 0, "Start capturing map", 1) then - startCapturing() - GuiDestroy(gui) - gui = nil - end - GuiTextCentered(gui, 0, 0, "Don't do anything while the capturing process is running!") - GuiTextCentered(gui, 0, 0, "Use ESC and close the game to stop the process.") - --[[if GuiButton(gui, 0, 0, "DEBUG globals", 1) then - local file = io.open("mods/noita-mapcap/output/globals.txt", "w") - for i, v in pairs(_G) do - file:write(i .. "\n") - end - file:close() - end]] - GuiLayoutEnd(gui) - end - - wait(0) - end -) diff --git a/files/compatibility.lua b/files/compatibility.lua new file mode 100644 index 0000000..0f7bd92 --- /dev/null +++ b/files/compatibility.lua @@ -0,0 +1,39 @@ +-- Copyright (c) 2019 David Vogel +-- +-- This software is released under the MIT License. +-- https://opensource.org/licenses/MIT + +-- Some code to make noita's lua more conform to standard lua + +-- Globally overwrite print function to behave more like expected +local oldPrint = print +function print(...) + local arg = {...} + + stringArgs = {} + + for i, v in ipairs(arg) do + table.insert(stringArgs, tostring(v)) + end + + oldPrint(unpack(stringArgs)) +end + +-- Overwrite print to copy its output into a file +--[[local logFile = io.open("lualog.txt", "w") +function print(...) + local arg = {...} + + stringArgs = {} + + local result = "" + for i, v in ipairs(arg) do + table.insert(stringArgs, tostring(v)) + result = result .. tostring(v) .. "\t" + end + result = result .. "\n" + logFile:write(result) + logFile:flush() + + oldPrint(unpack(stringArgs)) +end]] diff --git a/files/init.lua b/files/init.lua new file mode 100644 index 0000000..7d1d745 --- /dev/null +++ b/files/init.lua @@ -0,0 +1,15 @@ +-- Copyright (c) 2019 David Vogel +-- +-- This software is released under the MIT License. +-- https://opensource.org/licenses/MIT + +if not async then + dofile("data/scripts/lib/coroutines.lua") +end +dofile("data/scripts/perks/perk_list.lua") + +dofile("mods/noita-mapcap/files/compatibility.lua") +dofile("mods/noita-mapcap/files/util.lua") +dofile("mods/noita-mapcap/files/external.lua") +dofile("mods/noita-mapcap/files/capture.lua") +dofile("mods/noita-mapcap/files/ui.lua") diff --git a/files/luacomponent.xml b/files/luacomponent.xml new file mode 100644 index 0000000..e7c1b2a --- /dev/null +++ b/files/luacomponent.xml @@ -0,0 +1,8 @@ + + + + \ No newline at end of file diff --git a/files/ui.lua b/files/ui.lua new file mode 100644 index 0000000..27aa3d5 --- /dev/null +++ b/files/ui.lua @@ -0,0 +1,31 @@ +-- Copyright (c) 2019 David Vogel +-- +-- This software is released under the MIT License. +-- https://opensource.org/licenses/MIT + +async_loop( + function() + if modGUI ~= nil then + GuiStartFrame(modGUI) + + GuiLayoutBeginVertical(modGUI, 50, 20) + if GuiButton(modGUI, 0, 0, "Start capturing map", 1) then + startCapturing() + GuiDestroy(modGUI) + modGUI = nil + end + GuiTextCentered(modGUI, 0, 0, "Don't do anything while the capturing process is running!") + GuiTextCentered(modGUI, 0, 0, "Use ESC and close the game to stop the process.") + --[[if GuiButton(gui, 0, 0, "DEBUG globals", 1) then + local file = io.open("mods/noita-mapcap/output/globals.txt", "w") + for i, v in pairs(_G) do + file:write(i .. "\n") + end + file:close() + end]] + GuiLayoutEnd(modGUI) + end + + wait(0) + end +) diff --git a/files/util.lua b/files/util.lua index 1e70c95..f3cabc1 100644 --- a/files/util.lua +++ b/files/util.lua @@ -3,7 +3,7 @@ -- This software is released under the MIT License. -- https://opensource.org/licenses/MIT -function splitStringByLength(string, length) +function SplitStringByLength(string, length) local chunks = {} for i = 1, #string, length do table.insert(chunks, string:sub(i, i + length - 1)) @@ -12,7 +12,8 @@ function splitStringByLength(string, length) end -- Improved version of GamePrint, that behaves more like print. -function IngamePrint(...) +local oldGamePrint = GamePrint +function GamePrint(...) local arg = {...} local result = "" @@ -23,22 +24,66 @@ function IngamePrint(...) for line in result:gmatch("[^\r\n]+") do for i, v in ipairs(splitStringByLength(line, 100)) do - GamePrint(v) + oldGamePrint(v) end end end --- Globally overwrite print function and write output into a logfile -local logFile = io.open("lualog.txt", "w") -function print(...) - local arg = {...} - - local result = "" - for i, v in ipairs(arg) do - result = result .. tostring(v) .. "\t" +function getPlayer() + local players = EntityGetWithTag("player_unit") + if players == nil or #players < 1 then + return nil end - result = result .. "\n" - - logFile:write(result) - logFile:flush() + 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 diff --git a/init.lua b/init.lua index cd2dd91..10fc426 100644 --- a/init.lua +++ b/init.lua @@ -1,18 +1,15 @@ -function OnModPreInit() - -- print("Mod - OnModPreInit()") -- first this is called for all mods -end - -function OnModInit() - -- print("Mod - OnModInit()") -- after that this is called for all mods -end - -function OnModPostInit() - -- print("Mod - OnModPostInit()") -- then this is called for all mods -end +dofile("mods/noita-mapcap/files/init.lua") function OnPlayerSpawned(player_entity) + --EntityLoad("mods/noita-mapcap/files/luacomponent.xml") -- ffi isn't accessible from inside lua components, scrap that idea + modGUI = GuiCreate() +end + +function OnWorldPostUpdate() -- this is called every time the game has finished updating the world + wake_up_waiting_threads(1) -- Coroutines aren't run every frame in this sandbox, do it manually here. end --- this code runs when all mods' filesystems are registered -ModLuaFileAppend("data/scripts/director_init.lua", "mods/noita-mapcap/files/capture.lua") ModMagicNumbersFileAdd("mods/noita-mapcap/files/magic_numbers.xml") -- override some game constants + +-- Only works up to the 16-10-2019 version of noita. And even then, ffi and other nice stuff is only accessible here. +--ModLuaFileAppend("data/scripts/director_init.lua", "mods/noita-mapcap/files/init.lua") diff --git a/mod.xml b/mod.xml index 85e1404..ac6fab0 100644 --- a/mod.xml +++ b/mod.xml @@ -1,3 +1,4 @@ + description="This mod screen-captures a large part of the map, and stores the screenshot as image." + request_no_api_restrictions="1"> \ No newline at end of file