Skip to content

Instantly share code, notes, and snippets.

View HybridDog's full-sized avatar
💭

HybridDog

💭
  • We store your personal data in the United States. See our privacy policy for more information.
View GitHub Profile
@HybridDog
HybridDog / trianglesphere.lua
Created April 18, 2020 12:05
Creating a holow sphere with many triangles in minetest
local function trianglesphere(pos, r, edgecnt)
-- Positions on the unit sphere
local nodes = {
{0, 1, 0},
{1/math.sqrt(2), -1/math.sqrt(2), 0},
{-1/math.sqrt(3), -1/math.sqrt(3), -1/math.sqrt(3)},
{-1/math.sqrt(3), -1/math.sqrt(3), 1/math.sqrt(3)},
}
-- Indices to nodes: from, to and opposite points for triangles
-- Invariance: e[1] < e[2] and e[3] < e[4]
@HybridDog
HybridDog / light_detection.lua
Created September 2, 2019 18:12
Find minetest sunlight
-- See daynightratio.h:23
local dnrvalues = {
{4250 + 125, 150},
{4500 + 125, 150},
{4750 + 125, 250},
{5000 + 125, 350},
{5250 + 125, 500},
{5500 + 125, 675},
{5750 + 125, 875},
// this is two times as fast when compiled with -Ofast
// see https://graphics.ethz.ch/~cengizo/Files/Sig15PerceptualDownscaling.pdf
#include <stdlib.h> // malloc, EXIT_*
#include <string.h> // memset
#include <math.h>
#include <png.h>
#define SQR_NP 2 // squareroot of the patch size, recommended: 2
@HybridDog
HybridDog / digcalc.lua
Last active March 9, 2022 00:44
minetest digging time calculation
-- note that there's also minetest.get_dig_params
local mindelay = 0.15 -- game.cpp:3888
-- copied from tool.cpp:90
local function get_dig_time(groups, toolcaps)
-- dig_immediate have a fixed time
if groups.dig_immediate == 2 then
return 0.5
end
if groups.dig_immediate == 3 then
return mindelay
local function fine_pointed(pos, camera_pos, pos_off, look_dir)
local offset, nc
local oc = {}
for c,v in pairs(pos_off) do
if v == 0 then
oc[#oc + 1] = c
else
offset = v
nc = c
@HybridDog
HybridDog / chatwrap.lua
Created January 8, 2017 11:33
on_chatmessage wraps
local t
minetest.register_on_chat_message(function()
t = minetest.get_us_time()
end)
for _ = 1,1000 do
minetest.register_on_chat_message(function() end)
end
minetest.register_on_chat_message(function()
print("direct: " .. minetest.get_us_time() - t .. " ms")
end)
@HybridDog
HybridDog / stringtotablebt.lua
Last active July 5, 2016 08:23
string to table benchmark test
local function subtt(str)
local t = {}
for i = 1, #str do
t[i] = str:sub(i, i)
end
return t
end
local function gsubtt(str)
local t = {}
local TIME = 0.5
local clock = minetest.get_us_time
local us = TIME * 1000000
local function benchmark_function(fct, ...)
local start = clock()
local fin = start
local total = 0
while fin - start < us do
fct(...)
-- maximum distance a protector can have
local MAX_DISTANCE = 10
local function get(tab, pname,z,y,x)
local data = tab[pname]
if data then
local data = tab[z]
if data then
data = data[y]
if data then
@HybridDog
HybridDog / ldecay_speed_test.lua
Last active May 7, 2016 05:44
how often those functions get executed in three seconds
local os_clock = minetest.get_us_time
local TIME = 3*1000000
local function time_sth(fct, ...)
local start = os_clock()
local fin = start
local total = 0
while fin-start < TIME do
fct(...)
total = total + 1