1
0
mirror of https://github.com/mt-mods/plantlife_modpack.git synced 2024-11-15 23:00:38 +01:00
plantlife_modpack/plantlife_lib/init.lua

151 lines
3.9 KiB
Lua
Raw Normal View History

2023-04-12 14:12:51 +02:00
pl = {}
2023-04-12 17:00:18 +02:00
local deco = {}
2023-04-12 14:12:51 +02:00
2023-04-13 14:15:10 +02:00
dofile(minetest.get_modpath("plantlife_lib") .. DIR_DELIM .. "util.lua")
2023-04-12 17:00:18 +02:00
function pl.get_def_from_id(id)
2023-04-13 14:15:10 +02:00
for i, _ in ipairs(deco) do
2023-04-12 17:00:18 +02:00
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)
2023-04-12 17:52:29 +02:00
if not index then index = 1 end -- Do we need `index`?
2023-04-12 21:54:22 +02:00
local deco_def = {
2023-04-12 14:12:51 +02:00
name = plantname .. "_" .. index,
deco_type = "simple",
place_on = def.place_on or def.surface,
sidelen = 16,
2023-04-12 21:54:22 +02:00
fill_ratio = def.fill_ratio or 0.02,
noise_params = def.noise_params,
2023-04-12 14:12:51 +02:00
y_min = def.min_elevation,
y_max = def.max_elevation,
2023-04-12 21:54:22 +02:00
flags = def.flags,
2023-04-13 21:15:34 +02:00
decoration = "air", -- spawn the decoration later
2023-04-12 14:12:51 +02:00
}
2023-04-13 21:15:34 +02:00
-- handle avoid_nodes (no engine support :\)
if def.avoid_nodes then
deco_def.avoid_nodes = def.avoid_nodes
if def.avoid_radius then
deco_def.avoid_radius = def.avoid_radius
end
end
2023-04-13 14:15:10 +02:00
-- handle near_nodes (we can't use the engine function for that)
if def.near_nodes then
deco_def.near_nodes = def.near_nodes
if def.near_nodes_size then
deco_def.near_nodes_size = def.near_nodes_size
if def.near_nodes_vertical then
deco_def.near_nodes_vertical = def.near_nodes_vertical
end
end
deco_def.near_nodes_count = def.near_nodes_count or 1
end
-- save def
2023-04-12 17:00:18 +02:00
local next = #deco + 1
deco[next] = {}
deco[next][1] = deco_def
deco[next][2] = func or nil
2023-04-12 14:12:51 +02:00
minetest.register_decoration(deco_def)
2023-04-13 14:15:10 +02:00
-- print(dump(deco))
2023-04-12 14:12:51 +02:00
end
2023-04-12 17:00:18 +02:00
local ids = {}
2023-04-12 14:12:51 +02:00
minetest.register_on_mods_loaded(function()
2023-04-12 17:52:29 +02:00
-- print(dump(deco))
2023-04-13 14:15:10 +02:00
for k, v in ipairs(deco) do
2023-04-12 17:00:18 +02:00
local id = minetest.get_decoration_id(deco[k][1].name)
deco[k][1].id = id
table.insert(ids, id)
2023-04-12 14:12:51 +02:00
end
2023-04-12 17:52:29 +02:00
-- print(dump2(ids))
2023-04-12 17:00:18 +02:00
minetest.set_gen_notify("decoration", ids)
2023-04-12 17:52:29 +02:00
-- print(dump(deco))
2023-04-12 14:12:51 +02:00
end)
2023-04-12 21:54:22 +02:00
local function place_handler(t)
local def = pl.get_def_from_id(t.id)
2023-04-13 21:15:34 +02:00
local p_top = {x = pos.x, y = pos.y + 1, z = pos.z}
2023-04-12 21:54:22 +02:00
-- near nodes handler
2023-04-13 14:15:10 +02:00
if def.near_nodes and
2023-04-12 21:54:22 +02:00
#minetest.find_nodes_in_area(
{x = t.pos.x-def.near_nodes_size, y = t.pos.y-def.near_nodes_vertical, z = t.pos.z-def.near_nodes_size},
{x = t.pos.x+def.near_nodes_size, y = t.pos.y+def.near_nodes_vertical, z = t.pos.z+def.near_nodes_size},
def.near_nodes
) < def.near_nodes_count then
return -- Long distance neighbours do not match
end
2023-04-13 21:15:34 +02:00
-- avoid nodes handler
if def.avoid_nodes and def.avoid_radius then
if minetest.find_node_near(p_top, def.avoid_radius + math.random(-1.5,2), def.avoid_nodes) then
return
end
end
2023-04-12 21:54:22 +02:00
-- run spawn function
local spawn_func = def[2]
spawn_func(t.pos)
-- some fun
local player = minetest.get_player_by_name("Niklp")
-- player:set_pos(t.pos)
minetest.add_particle({
2023-04-13 21:15:34 +02:00
pos = p_top,
2023-04-12 21:54:22 +02:00
expirationtime = 15,
playername = player:get_player_name(),
glow = minetest.LIGHT_MAX,
texture = "default_mese_crystal.png",
size = 15,
})
end
2023-04-12 14:12:51 +02:00
minetest.register_on_generated(function(minp, maxp, blockseed)
2023-04-13 14:15:10 +02:00
local t0 = minetest.get_us_time()
2023-04-12 14:12:51 +02:00
local g = minetest.get_mapgen_object("gennotify")
local locations = {}
2023-04-13 14:15:10 +02:00
for _, id in ipairs(ids) do
2023-04-12 14:12:51 +02:00
local deco_locations = g["decoration#" .. id] or {}
2023-04-12 17:52:29 +02:00
-- print("dl: " .. dump2(deco_locations))
2023-04-12 17:00:18 +02:00
for k, pos in pairs(deco_locations) do
2023-04-12 17:52:29 +02:00
-- print(id)
2023-04-12 17:00:18 +02:00
local next = #locations + 1
locations[next] = {}
locations[next].pos = pos
locations[next].id = id
-- dbg() ^ - This must be ID!
2023-04-12 14:12:51 +02:00
end
end
if #locations == 0 then return end
2023-04-13 14:15:10 +02:00
-- print("locations: " .. dump2(locations))
2023-04-12 17:00:18 +02:00
for _, t in ipairs(locations) do
2023-04-12 21:54:22 +02:00
place_handler(t)
2023-04-12 14:12:51 +02:00
end
2023-04-13 14:15:10 +02:00
local t1 = minetest.get_us_time()
print((t1 - t0) / 1000 .. " ms")
2023-04-12 17:52:29 +02:00
end)
--[[ Example plant
{
{
y_min = 1,
decoration = "air",
deco_type = "simple",
id = 45,
name = "bushes:bushes_1",
place_on = {
"default:dirt_with_grass",
"stoneage:grass_with_silex",
"sumpf:peat",
"sumpf:sumpf"
},
sidelen = 16,
fill_ratio = 0.001
},
^ - decoration def; object ID
<function>
}, ^ - spawn function
]]--