Skip to content

Instantly share code, notes, and snippets.

View appgurueu's full-sized avatar

Lars Müller appgurueu

View GitHub Profile
@appgurueu
appgurueu / slowlocals.lua
Created February 20, 2023 18:44
Pathological example to show that due to upvalue copying locals can in fact be slightly slower than globals (environmentals)
local vars = {}
for i = 1, 50 do table.insert(vars, ("v%d"):format(i)) end
local all_vars, all_vals = table.concat(vars, ", "), "1" .. (", 2"):rep(#vars - 1)
local pathological_code = ([[
local %s = %s
return function(get_all)
if get_all then return %s end
return v1
end
]]):format(all_vars, all_vals, all_vars)
@appgurueu
appgurueu / stdbin.c
Created February 1, 2023 07:09
Script to launch a program after reopening stdin in binary mode on Windows (untested)
#include <stdio.h>
#include <unistd.h>
#include <assert.h>
int main(int argc, char** argv) {
if (argc < 1) {
printf("Arguments: <program> {args}\n");
return 1;
}
@appgurueu
appgurueu / infinite_loops.md
Last active January 29, 2023 13:21
Infinite Loops

Infinite Loops

In Lua.

Lawful

Good

A classic.

@appgurueu
appgurueu / leetcode_787.go
Created January 26, 2023 21:35
Leetcode Problem 787. "Cheapest Flights Within K Stops" Solution
// Solution to https://leetcode.com/problems/cheapest-flights-within-k-stops/description/
// probably not the intended solution; uses an abridged Dijkstra to solve the much more general problem
// of finding all shortest paths to a given node using exactly k nodes
package main
import (
"math"
"container/heap"
)
@appgurueu
appgurueu / number_to_string.lua
Created January 3, 2023 21:52
Compact but hacky and inefficient float number to string using a binary search on the precision
-- Lookup table for format strings
local formats = {}
for i = 1, 17 do
formats[i] = ("%%.%dg"):format(i)
end
local function _number_to_string(num, min_digits, max_digits)
if min_digits > max_digits then
return
end
@appgurueu
appgurueu / init.lua
Last active September 1, 2022 07:02
Fix for tile bug on Minetest 5.5
-- Written by appgurueu, licensed under the MIT license
local function patch_tile_texture(tex)
if not (tex:match"^%[combine:" or tex:match"^%[inventorycube{") then
return
end
-- Prevent Minetest 5.5 from prepending "blank.png^" for 5.4 and older clients which don't support [png
-- See https://github.com/minetest/minetest/pull/12210
return "(" .. tex .. ")"
end
@appgurueu
appgurueu / mt_unknown_entity_remove.lua
Created June 27, 2022 16:38
Small, hacky Minetest utility to get rid of "unknown entities" as they are activated
--[[
Small, hacky Minetest utility to get rid of "unknown entities" as they are activated. Licensed under CC0.
Caveat: `/spawnentity <name>` will fail with "<name> failed to spawn" rather than "Cannot spawn an unknown entity."
for unknown entities as the unknown entity will immediately be removed inside `on_activate`.
]]
setmetatable(minetest.registered_entities, {
__index = function(_, name)
-- Default entity def if `minetest.registered_entities[name]` would otherwise be `nil`
-- Necessarily leads to `minetest.registered_entities[name]` being truthy for unknown entities as well
@appgurueu
appgurueu / init.lua
Last active March 9, 2022 00:53
modlib - init.lua - template
-- Table helpers
table_ext= {
tablecopy = function(t)
return table.copy(t)
end,
count = function(table)
local count = 0
for _ in pairs(table) do
@appgurueu
appgurueu / init.lua
Created March 7, 2020 17:17
Minetest Waypoint Testmod
-- Use this to test waypoint capabilities
minetest.register_chatcommand(
"test_waypoints",
{
description = "tests waypoint capabilities",
func = function(name)
local player = minetest.get_player_by_name(name)
minetest.chat_send_all(
"regular: " ..
player:hud_add {
@appgurueu
appgurueu / init.lua
Last active March 9, 2022 00:44
Shuts down the server if it lags too much ("antilag" Minetest mod init.lua)
local min_lag = 1 -- in seconds
local min_times = 10 -- has to occur 10x
local times = 0
minetest.register_globalstep(function(lag)
if lag < min_lag then
times = 0
else
times = times + 1
if times >= min_times then
minetest.request_shutdown("Server shutting down due to high latency (lag).")