Additional configuration formats, experimental vector library, other utilities

master
Lars Mueller 2020-06-28 22:42:24 +02:00
parent e5266d9862
commit 68b5c4013f
5 changed files with 91 additions and 4 deletions

View File

@ -12,7 +12,9 @@ Mostly self-documenting code. Mod namespace is `modlib` or `_ml`, containing all
## Configuration
1. Configuration is loaded from `<worldpath>/config/<modname>.<extension>`, the following extensions are supported and will be searched for (in that order):
1. Configuration is loaded from `<worldpath>/config/<modname>.<extension>`, the following extensions are supported and loaded (in the given order), with loaded configurations overriding properties of previous ones:
1. [`json`](https://json.org)
2. [`conf`](https://github.com/minetest/minetest/blob/master/doc/lua_api.txt)
2. Settings are loaded and override configuration values
2. [`lua`](https://lua.org)
3. [`luon`](https://github.com/appgurueu/luon), Lua but without the `return`
4. [`conf`](https://github.com/minetest/minetest/blob/master/doc/lua_api.txt)
2. Settings are loaded from `minetest.conf` and override configuration values

View File

@ -89,14 +89,27 @@ function load_or_create(filename, replacement_file, constraints)
end
function import(modname, constraints, no_settingtypes)
local default_config = modlib.mod.get_resource(modname, "default_config.json")
local default_conf = minetest.parse_json(modlib.file.read(default_config))
local config = load_or_create(get_path(modname)..".json", default_config, constraints)
local formats = {
{ extension = ".lua", load = minetest.deserialize },
{ extension = ".luon", load = function(text) minetest.deserialize("return "..text) end },
{ extension = ".conf", load = function(text) return fix_types(build_tree(read_conf(text)), constraints) end }
}
for _, format in ipairs(formats) do
local conf = modlib.file.read(get_path(modname)..format.extension)
if conf then
config = merge_config(config, format.load(conf))
end
end
if not no_settingtypes then
constraints.name = modname
local settingtypes = generate_settingtypes(minetest.parse_json(modlib.file.read(default_config)), constraints)
local settingtypes = generate_settingtypes(default_conf, constraints)
modlib.file.write(modlib.mod.get_resource(modname, "settingtypes.txt"), settingtypes)
end
local additional_settings = modlib.conf.settings[modname] or {}
additional_settings = fix_types(additional_settings, constraints)
-- TODO implement merge_config_legal(default_conf, ...)
config = merge_config(config, additional_settings)
if constraints then
check_config_constraints(config, constraints, function(message)

View File

@ -83,3 +83,14 @@ minetest.register_on_leaveplayer(
delete_player_data(playername)
end
)
function datatable(table, default)
table = table or {}
default = default or {}
minetest.register_on_joinplayer(function(player)
local name = player:get_player_name()
table[name] = table[name] or default
end)
minetest.register_on_leaveplayer(function(player) table[player:get_player_name()] = nil end)
return table
end

View File

@ -304,4 +304,12 @@ function reverse(list)
list[len-i+1], list[i] = list[i], list[len-i+1]
end
return list
end
function repetition(value, count)
local table = {}
for i = 1, count do
table[i] = value
end
return table
end

53
vector.lua Normal file
View File

@ -0,0 +1,53 @@
local mt_vector = vector
local vector = getfenv(1)
function new(v)
return setmetatable(v, vector)
end
function from_xyzw(v)
return new{v.x, v.y, v.z, v.w}
end
function to_xyzw(v)
return {x = v[1], y = v[2], z = v[3], w = v[4]}
end
function to_minetest(v)
return mt_vector.new(unpack(v))
end
function combine(v1, v2, f)
local v = {}
for k, c in pairs(v1) do
v[k] = f(c, v2[k])
end
return new(v)
end
function apply(v, s, f)
for i, c in pairs(v) do
v[i] = f(c, s)
end
end
function combinator(f)
return function(v1, v2)
return combine(v1, v2, f)
end, function(v, s)
return apply(v, s, f)
end
end
add, add_scalar = combinator(function(a, b) return a + b end)
subtract, subtract_scalar = combinator(function(a, b) return a - b end)
multiply, multiply_scalar = combinator(function(a, b) return a * b end)
divide, divide_scalar = combinator(function(a, b) return a / b end)
function length(v)
local sum = 0
for _, c in pairs(v) do
sum = sum + c*c
end
return math.sqrt(sum)
end