Skip to content

Instantly share code, notes, and snippets.

View appgurueu's full-sized avatar

Lars Müller appgurueu

View GitHub Profile
@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
@appgurueu
appgurueu / dont_do_ddos.md
Created March 3, 2022 10:21
Why you should not partake in the ongoing coordinated DDoSing activities

Why you should not partake in the ongoing coordinated DDoSing activities

  • It might hit the wrong targets. Do you really trust whatever tool you run and whatever list you received to only hit what you wanted to hit? DDoSing will also unnecessarily increase network load on the route from you to the target.
  • It's illegal and might come back at you. DDoSing is illegal on multiple levels. It's usually easy to prove through server logs. Your ISP might - often automatically - ban you. The targets of your attacks might "strike back" or use your attacks as a pretense to inflict even more harm to your region. You can merely hope that you will not be persecuted for your naive criminal offenses.
  • It's risky. You'll - one way or another - be running foreign code on your machine. That always comes with a risk (which can only partially be reduced through virtualization, be it through the browser environment or something like Docker). Even if no control over your machine is gained, your IP at the very least
@appgurueu
appgurueu / js_var.lua
Created August 22, 2021 10:32
JS var keyword
local _G = _G
return function(implicit_declaration, stacklevel)
assert(implicit_declaration == "var" or implicit_declaration == "global" or not implicit_declaration)
stacklevel = (stacklevel or 1) + 1
local _P = getfenv(stacklevel + 1)
local _V = {}
local declarations = {}
local environment = {
_P = _P,
_V = _V,
@appgurueu
appgurueu / golike_exports.lua
Created August 22, 2021 10:29
Go-inspired exports based on variable capitalization
local parent = getfenv(1)
local private = {}
local public = {}
setfenv(1, setmetatable({}, {__newindex = function(self, key, value)
local first = key:sub(1, 1);
((first >= "A" and first <= "Z") and public or private)[key] = value
end, __index = function(self, key)
local value = private[key]
if value ~= nil then
return value
@appgurueu
appgurueu / trinary_logic.lua
Created August 22, 2021 10:26
Trinary logic implemented in Lua using debug.setmetatable
-- Set boolean metatable
debug.setmetatable(false, {
__add = function(a, b)
if b == nil then
return b + a
end
return a or b
end,
__mul = function(a, b)
if b == nil then