rolling-9

This commit is contained in:
Lars Mueller 2020-03-23 20:20:43 +01:00
parent a2cca67e48
commit 892a27deef
8 changed files with 148 additions and 25 deletions

@ -7,7 +7,7 @@ function define(name, def)
end end
function new(classname, ...) function new(classname, ...)
local obj = get(classname).new(...) local obj = get(classname).new(...)
obj = setmetatable(obj, {__index = classes[classname]}) -- TODO ? metatable add __call with setfenv obj = setmetatable(obj, {__index = classes[classname]})
return obj return obj
end end
function get(classname) function get(classname)

6
func.lua Normal file

@ -0,0 +1,6 @@
function curry(func, ...)
local args = {...}
return function(...)
return func(unpack(args), ...)
end
end

@ -3,7 +3,7 @@ if _VERSION then
if _VERSION < "Lua 5" then if _VERSION < "Lua 5" then
error("Outdated Lua version! modlib requires Lua 5 or greater.") error("Outdated Lua version! modlib requires Lua 5 or greater.")
end end
if _VERSION > "Lua 5.1" then -- TODO automatically use _ENV instead of s/getfenv if _VERSION > 5.1 if _VERSION > "Lua 5.1" then
-- not throwing error("Too new Lua version! modlib requires Lua 5.1 or smaller.") anymore -- not throwing error("Too new Lua version! modlib requires Lua 5.1 or smaller.") anymore
unpack = unpack or table.unpack -- unpack was moved to table.unpack in Lua 5.2 unpack = unpack or table.unpack -- unpack was moved to table.unpack in Lua 5.2
loadstring = load loadstring = load
@ -47,14 +47,13 @@ local function loadfile_exports(filename)
return env return env
end end
-- TODO automatically know current mod
local components = { local components = {
mod = {}, mod = {},
class = {}, class = {},
conf = {}, conf = {},
data = {}, data = {},
file = {}, file = {},
func = {},
log = {}, log = {},
minetest = {}, minetest = {},
number = {}, number = {},
@ -80,4 +79,7 @@ end
modlib.mod.loadfile_exports = loadfile_exports modlib.mod.loadfile_exports = loadfile_exports
-- complete the string library (=metatable) with text helpers
modlib.table.complete(string, modlib.text)
_ml = modlib _ml = modlib

@ -1,5 +1,4 @@
-- MT extension -- MT extension
-- TODO add formspec queues and event system + sync handler
delta_times={} delta_times={}
delays={} delays={}
callbacks={} callbacks={}

@ -9,12 +9,13 @@ function include(modname, file)
end end
-- loadfile with table env -- loadfile with table env
function include_namespace(classname, filename) function include_namespace(classname, filename, parent_namespace)
_G[classname] = setmetatable(_G[classname] or {}, {__index = _G, __call = _G}) parent_namespace = parent_namespace or _G
parent_namespace[classname] = setmetatable(parent_namespace[classname] or {}, {__index = parent_namespace, __call = parent_namespace})
local class = assert(loadfile(filename)) local class = assert(loadfile(filename))
setfenv(class, _G[classname]) setfenv(class, parent_namespace[classname])
class() class()
return _G[classname] return parent_namespace[classname]
end end
-- runs main.lua in table env -- runs main.lua in table env

@ -3,3 +3,43 @@ function round(number, steps) -- Rounds a number
steps = steps or 1 steps = steps or 1
return math.floor(number * steps + 0.5) / steps return math.floor(number * steps + 0.5) / steps
end end
local c0 = ("0"):byte()
local cA = ("A"):byte()
function default_digit_function(digit)
if digit <= 9 then
return string.char(c0+digit)
end
return string.char(cA+digit-10)
end
default_precision = 10
function tostring(number, base, digit_function, precision)
digit_function = digit_function or default_digit_function
precision = precision or default_precision
local out = {}
if number < 0 then
table.insert(out, "-")
number = -number
end
local digit
while number >= base do
digit = math.floor(number % base)
table.insert(out, digit_function(digit))
number = number / base
end
digit = math.floor(number)
table.insert(out, digit_function(digit))
modlib.table.reverse(out)
number = number % 1
if number >= math.pow(base, precision) then
table.insert(out, ".")
-- precision >= 0 eventually redundant
while precision >= 0 and number >= math.pow(base, precision) do
number = number * base
digit = math.floor(number % base)
table.insert(out, digit_function(digit))
number = number - digit
precision = precision - 1
end
end
return table.concat(out)
end

106
table.lua

@ -47,6 +47,8 @@ function tablecopy(t)
return table.copy(t) return table.copy(t)
end end
copy = tablecopy
function count(table) function count(table)
local count = 0 local count = 0
for _ in pairs(table) do for _ in pairs(table) do
@ -59,10 +61,29 @@ function is_empty(table)
return next(table) == nil return next(table) == nil
end end
function foreach(t, func)
for k, v in pairs(t) do
func(k, v)
end
end
function foreach_value(t, func)
for _, v in pairs(t) do
func(v)
end
end
function foreach_key(t, func)
for k, _ in pairs(t) do
func(k)
end
end
function map(t, func) function map(t, func)
for k, v in pairs(t) do for k, v in pairs(t) do
t[k]=func(v) t[k]=func(v)
end end
return t
end end
function process(t, func) function process(t, func)
@ -79,14 +100,17 @@ function call(funcs, ...)
end end
end end
function merge_tables(table1, table2) function find(list, value)
local table1copy = table.copy(table1) for index, other_value in pairs(list) do
for key, value in pairs(table2) do if value == other_value then
table1copy[key] = value return index
end
end end
return table1copy return false
end end
contains = find
function difference(table1, table2) function difference(table1, table2)
local result={} local result={}
for k, v in pairs(table2) do for k, v in pairs(table2) do
@ -105,6 +129,31 @@ function add_all(dst, new)
return dst return dst
end end
function complete(dst, new)
for key, value in pairs(new) do
if dst[key] == nil then
dst[key] = value
end
end
return dst
end
function merge_tables(table1, table2)
return add_all(copy(table1), table2)
end
union = merge_tables
function intersection(t1, t2)
local result = {}
for key, value in pairs(t1) do
if t2[key] then
result[key] = value
end
end
return result
end
function append(t1, t2) function append(t1, t2)
local l=#t1 local l=#t1
for k, v in ipairs(t2) do for k, v in ipairs(t2) do
@ -192,15 +241,42 @@ function max(table)
return best_value(table, function(v, m) return v > m end) return best_value(table, function(v, m) return v > m end)
end end
function binary_search(list, value) function default_comparator(a, b)
local min, size = 1, #list if a == b then
while size > 1 do return 0
local s_half = math.floor(size / 2)
local pivot = min + s_half
local element = list[pivot]
if value > element then
min = pivot
end
size = s_half
end end
if a > b then
return 1
end
return -1
end
function binary_search_comparator(comparator)
-- if found, returns index; if not found, returns -index for insertion
function binary_search(list, value)
local min, max = 1, #list
while min <= max do
local pivot = min + math.floor((max-min)/2)
local element = list[pivot]
local compared = comparator(value, element)
if compared == 0 then
return pivot
elseif compared > 0 then
min = pivot+1
else
max = pivot-1
end
end
return -min
end
end
binary_search = binary_search_comparator(default_comparator)
function reverse(list)
local len = #list
for i = 1, math.floor(#list/2) do
list[len-i+1], list[i] = list[i], list[len-i+1]
end
return list
end end

@ -1,4 +1,3 @@
-- TODO probably set string metatables ?
-- String helpers - split & trim at end & begin -- String helpers - split & trim at end & begin
function upper_first(str) function upper_first(str)
return str:sub(1,1):upper()..str:sub(2) return str:sub(1,1):upper()..str:sub(2)