Skip to content

Instantly share code, notes, and snippets.

@ZwerOxotnik
ZwerOxotnik / OptimalHex2rgb.lua
Last active September 13, 2022 10:27
Converts HEX code to numbers in RGB for Lua 5.x
do
local match = string.match
local cashedShortHEX = {["0"] = 0, ["1"] = 17, ["2"] = 34, ["3"] = 51, ["4"] = 68, ["5"] = 85, ["6"] = 102, ["7"] = 119, ["8"] = 136, ["9"] = 153, A = 170, B = 187, C = 204, E = 238, F = 255, a = 170, b = 187, c = 204, f = 255}
-- Converts HEX code to numbers in RGB
---@param hex string
---@return number red, number green, number blue
function hex2rgb(hex)
local length = #hex
if length == 7 then
local r, g, b = match(hex, '.(..)(..)(..)')
@ZwerOxotnik
ZwerOxotnik / binSearch.lua
Last active September 8, 2022 06:10
Binary search for Lua 5.x
do
local floor = math.floor
---@param tbl any[]
---@param value any
---@param iStart integer?
---@param iEnd integer?
---@param reversed boolean?
---@return any?
function binSearch(tbl, value, iStart, iEnd, reversed)
-- Initialise numbers
@ZwerOxotnik
ZwerOxotnik / lua2p.sh
Created December 12, 2021 14:43
lua2p (bash script + vscode task)
#!/bin/bash
### Compile *.lua2p files to *.lua files and format them
### https://github.com/ReFreezed/LuaPreprocess
use_cmd=0
case "$OSTYPE" in
win32|cygwin|msys) use_cmd=1 ;;
esac
@ZwerOxotnik
ZwerOxotnik / lint.yml
Last active November 23, 2021 08:44
lint.yml for Cuberite
on:
push:
branches:
- main
name: Lint
jobs:
lint:
runs-on: ubuntu-latest
steps:
@ZwerOxotnik
ZwerOxotnik / .luacheckrc
Last active November 23, 2021 08:43
Generated .luacheckrc for Cuberite
-- This file is the config file for the tool named Luacheck
-- Documentation: https://luacheck.readthedocs.io/en/stable/index.html
-- Ignore unused function and loop arguments
unused_args = false
-- Allow self defined globals
allow_defined = true
-- Ignore this functions
# game.players vs game.get_player
/measured-command for 1, 100 do local p = game.players[game.player.index] end
/measured-command for 1, 100 do local p = game.get_player(game.player.index) end
# Strings as key in tables vs comparing strings
/c SUBSPACE_ENTITIES_FOR_BUILT = {
["subspace-item-injector"] = true,
["subspace-item-extractor"] = true,
["subspace-fluid-injector"] = true,
@ZwerOxotnik
ZwerOxotnik / control.lua
Last active April 6, 2021 07:55
Just an example for Factorio
-- This is just an example file
-- interacting with https://lua-api.factorio.com/latest/LuaCommandProcessor.html#LuaCommandProcessor.add_command
local function attach_commands(module)
for key, command in pairs(module.commands) do
local name = command.name or key
local description = command.description or {"special-command-description"}
commands.add_command(name, description, command.func)
-- maybe even other actions...
end
@ZwerOxotnik
ZwerOxotnik / control.lua
Last active April 4, 2021 19:23
Some LuaRemote example in Factorio
-- This is just an example file for some case of LuaRemote
if remote.interfaces["another-interface"] then
-- You can just check via script.active_mods["mod_name"] instead if it's 100% compatible
-- but if it's an interface from scenario you can do it
-- And look at https://lua-api.factorio.com/1.1.30/LuaBootstrap.html#LuaBootstrap.level it may help in such rare cases
require "my-another-file"
end
-- Also, don't use remote.call here, it won't work