From 252c2bd1d28744a80e1160bb7a4562f8068a1b20 Mon Sep 17 00:00:00 2001 From: Niklp09 <89982526+Niklp09@users.noreply.github.com> Date: Wed, 12 Apr 2023 17:00:18 +0200 Subject: [PATCH] add support for custom spawn functions --- bushes/init.lua | 6 ++--- plantlife_lib/init.lua | 59 ++++++++++++++++++++++++++++-------------- plantlife_lib/mod.conf | 3 ++- 3 files changed, 45 insertions(+), 23 deletions(-) diff --git a/bushes/init.lua b/bushes/init.lua index 7559cd1..ff5dfd6 100644 --- a/bushes/init.lua +++ b/bushes/init.lua @@ -255,8 +255,7 @@ abstract_bushes.grow_youngtree_node2 = function(pos, height) end end ---[[ -biome_lib.register_on_generate({ +pl.register_on_generate({ surface = { "default:dirt_with_grass", "stoneage:grass_with_silex", @@ -268,6 +267,7 @@ biome_lib.register_on_generate({ plantlife_limit = bushes_youngtrees_fertility, min_elevation = 1, -- above sea level }, + "bushes:youngtrees", + nil, abstract_bushes.grow_youngtree2 ) -]]-- \ No newline at end of file diff --git a/plantlife_lib/init.lua b/plantlife_lib/init.lua index 94fab02..47abddc 100644 --- a/plantlife_lib/init.lua +++ b/plantlife_lib/init.lua @@ -1,8 +1,15 @@ pl = {} -local deco_ids = {} -local spawn_funcs = {} +local deco = {} -function pl.register_on_generate(def, plantname, index, func) -- add spawnfunction support +function pl.get_def_from_id(id) + for i, _ in pairs(deco) do + if deco[i][1].id and deco[i][1].id == id then + return deco[i] + end + end +end + +function pl.register_on_generate(def, plantname, index, func) if not index then index = 1 end local deco_def = { name = plantname .. "_" .. index, @@ -16,36 +23,50 @@ function pl.register_on_generate(def, plantname, index, func) -- add spawnfuncti num_spawn_by = def.near_nodes_count, decoration = "air" } - deco_ids[#deco_ids+1] = plantname .. ("_" .. index or "_1") - spawn_funcs[#deco_ids+1] = func + local next = #deco + 1 + deco[next] = {} + deco[next][1] = deco_def + deco[next][2] = func or nil minetest.register_decoration(deco_def) + print(dump(deco)) end +local ids = {} minetest.register_on_mods_loaded(function() - print(dump(deco_ids)) - for k, v in pairs(deco_ids) do - deco_ids[k] = minetest.get_decoration_id(v) + print(dump(deco)) + for k, v in pairs(deco) do + local id = minetest.get_decoration_id(deco[k][1].name) + deco[k][1].id = id + table.insert(ids, id) end - minetest.set_gen_notify("decoration", deco_ids) - print(dump(deco_ids)) + print(dump2(ids)) + minetest.set_gen_notify("decoration", ids) + print(dump2(deco)) end) minetest.register_on_generated(function(minp, maxp, blockseed) local g = minetest.get_mapgen_object("gennotify") - print(dump(g)) local locations = {} - for _, id in pairs(deco_ids) do + for _, id in pairs(ids) do local deco_locations = g["decoration#" .. id] or {} - for _, pos in pairs(deco_locations) do - locations[#locations+1] = pos + print("dl: " .. dump2(deco_locations)) + for k, pos in pairs(deco_locations) do + print(id) + local next = #locations + 1 + locations[next] = {} + locations[next].pos = pos + locations[next].id = id + -- dbg() ^ - This must be ID! end end - print(dump(locations)) if #locations == 0 then return end - for _, pos in ipairs(locations) do - print("yay") - abstract_bushes.grow_bush(pos) + print("locations: " .. dump2(locations)) + for _, t in ipairs(locations) do + local def = pl.get_def_from_id(t.id) + local spawn_func = def[2] + spawn_func(t.pos) + abstract_bushes.grow_bush(t.pos) local player = minetest.get_player_by_name("Niklp") - player:set_pos(pos) + player:set_pos(t.pos) end end) \ No newline at end of file diff --git a/plantlife_lib/mod.conf b/plantlife_lib/mod.conf index 97a88bb..093359d 100644 --- a/plantlife_lib/mod.conf +++ b/plantlife_lib/mod.conf @@ -1 +1,2 @@ -name = plantlife_lib \ No newline at end of file +name = plantlife_lib +optional_depends = dbg \ No newline at end of file