In Lua.
A classic.
| -- 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 |
| 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 |
| 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, |
| -- 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 |
| 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 |
| --[[ | |
| 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 |
| -- 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 |
| // 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" | |
| ) |