Skip to content

Instantly share code, notes, and snippets.

@Zbizu
Zbizu / getOS.lua
Last active April 2, 2024 07:55
operating system (OS) detection in Lua
function getOS()
-- ask LuaJIT first
if jit then
return jit.os
end
-- Unix, Linux variants
local fh,err = assert(io.popen("uname -o 2>/dev/null","r"))
if fh then
osname = fh:read()
@Zbizu
Zbizu / getVariables.lua
Last active December 24, 2023 13:38
get all Lua functions (and globals)
-- usage: just call it in a Lua environment and look for a file called out.txt
-- lists all global variables in a file
function writeAllGlobals()
local file = io.open("out.txt", "w+")
local seen={}
local function dump(t,i)
seen[t]=true
local s={}
@Zbizu
Zbizu / preview.md
Last active June 27, 2023 12:29
protocol 12 icons test

new_icon

@Zbizu
Zbizu / findFiles.lua
Last active May 26, 2023 21:55
Loop through all Lua files in a single directory
if getOS():lower() == "windows" then
for path in io.popen("cd \"data\lib\" && dir *.lua /b/s"):lines() do
print(path) -- path = file name/directory
end
else
for path in io.popen("cd data/lib && find . -type f | grep .lua"):lines() do
print(path)
end
end
@Zbizu
Zbizu / getVector.cpp
Last active March 24, 2023 17:04
Lua get a vector of integers for C++
void LuaScriptInterface::getIntVector(lua_State* L, int32_t arg, std::vector<uint32_t>& vec)
{
lua_pushnil(L);
while (lua_next(L, arg) != 0) {
if (lua_isnumber(L, -1)) {
vec.push_back(lua_tointeger(L, -1));
}
lua_pop(L, 1);
}
local files = {"door_locked.png",
"door_locked_small.png",
"door_normal.png",
"door_normal_small.png",
"door_magic.png",
"door_magic_small.png",
"door_quest.png",
"door_quest_small.png"}
function c()
@Zbizu
Zbizu / dumptable.lua
Last active June 13, 2022 11:17
dump lua table contents to string
-- usage: dumpTable(t)
-- to dump to file:
--[[
local str = dumpTable(t)
local f = io.open("dumped_table.txt", "w+")
f:write(str)
f:close()
]]
local function getTabulation(depth)
@Zbizu
Zbizu / how_to_run_1098_version12.md
Last active June 2, 2022 13:56
How to run 10.98 with version 12
@Zbizu
Zbizu / loader.lua
Created April 11, 2022 09:40
Lua folder iterator example
------------------------------
-- Loader config
------------------------------
local mapGeneratorDir = "data/map_generator/"
------------------------------
-- loader
------------------------------
dofile(mapGeneratorDir .. 'config.lua')
local prefix = ">> "
@Zbizu
Zbizu / relocate.lua
Created April 2, 2022 02:56
move all creatures and items from one position to another
-- no need to redefine this in every script, just paste it to your libs
function Position:moveAll(newPos)
local tile = Tile(self)
if tile then
local tileStack = tile:getItems()
for _, tileItem in pairs(tileStack) do
local itemType = tileItem:getType()
if itemType:isMovable() then