Skip to content

Instantly share code, notes, and snippets.

View appgurueu's full-sized avatar

Lars Müller appgurueu

View GitHub Profile
import "sort"
// This is not particularly efficient.
// A more efficient implementation might either be B-Tree like,
// might eliminate the index, might distinguish parents and leaves,
// and might strip parents of their values;
// to alleviate the mem management overhead, I'd wish
// for an arena allocator, but this isn't Zig
type PSlice[T any] struct {
@appgurueu
appgurueu / dumpcdb.lua
Created April 11, 2023 18:33
Dump CDB into the current directory using `wget`
#!/usr/bin/env luajit
-- See https://content.minetest.net/help/api/
local http_request = require"http.request"
local lunajson = require"lunajson"
do
-- URI escaping utilities
-- See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURI
local uri_unescaped_chars = {}
@appgurueu
appgurueu / lua51_vs_luajit_compat.yaml
Last active April 10, 2023 19:08
Lua 5.1 vs LuaJIT Lua 5.2 compatibility semgrep rules
# Rules for programs which must run under both LuaJIT and PUC Lua 5.1
# and which thus may not rely on the 5.2 LuaJIT compatibility features
# See http://luajit.org/extensions.html
rules:
- id: xpcall-args
pattern-either:
# theoretically could be fine e.g. if (empty vararg!), but practically never is
- pattern: xpcall($FN, $ERRHAND, $EXP, ...)
# the following could also theoretically be fine, but practically are a major code smell
- pattern: xpcall($FN1, $FN2(...))
@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 / 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 / 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 / mtreraycast.lua
Created June 6, 2022 19:33
Minetest: Redo tool raycast
local eye_pos = player:get_pos()
eye_pos.y = eye_pos.y + player:get_properties().eye_height
local first, third = player:get_eye_offset()
if not vector.equals(first, third) then minetest.log("warning", "First & third person eye offsets don't match, assuming first person") end
eye_pos = vector.add(eye_pos, vector.divide(first, 10))
local def = player:get_wielded_item():get_definition()
for pointed_thing in minetest.raycast(eye_pos, vector.add(eye_pos, vector.multiply(player:get_look_dir(), def.range or 4)), true, def.liquids_pointable) do if pointed_thing.ref ~= player then --[[do something]] end 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