diff --git a/Readme.md b/Readme.md index ebd1fc5..f6291b7 100644 --- a/Readme.md +++ b/Readme.md @@ -12,7 +12,9 @@ Mostly self-documenting code. Mod namespace is `modlib` or `_ml`, containing all ## Configuration -1. Configuration is loaded from `/config/.`, the following extensions are supported and will be searched for (in that order): +1. Configuration is loaded from `/config/.`, 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 \ No newline at end of file + 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 \ No newline at end of file diff --git a/conf.lua b/conf.lua index 6de8552..6660b7a 100644 --- a/conf.lua +++ b/conf.lua @@ -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) diff --git a/player.lua b/player.lua index 04e0abf..b86874d 100644 --- a/player.lua +++ b/player.lua @@ -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 \ No newline at end of file diff --git a/table.lua b/table.lua index 5059b37..3b0acfa 100644 --- a/table.lua +++ b/table.lua @@ -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 \ No newline at end of file diff --git a/vector.lua b/vector.lua new file mode 100644 index 0000000..a7ca3a5 --- /dev/null +++ b/vector.lua @@ -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 \ No newline at end of file