Hello, I was using this code to learn more about LUA and minetest coding.
I made some improvements:
- Removed the "math.randomseed(os.time())" as it is already included in minetest (see Temperest's post)
- Added a per plant abm interval and chance
- Added a per planting surface light level support so plants don't grow in the dark and needed light level varies with surface.
-Corrected the odds to keep grow speed in the same range after the minimal_light implementation
I have also some other improvements planned:
- growing:isplant and growing:addsurface function to make this a default growing library for any other mod (kind of unified_growing)
-plant aging (so node "evolve")
-multi-node plants (ie: plants with trunk and leaves)
- forking other mods (ie: hydro) so they depend on [growing] to show possibilities
-adding aging plants (not growing:wheat,... and growing:hop,...) -- as part of a separated package to show off possibilities
I have some questions:
- What's the license of this mod?
-Why isn't it in a modding related section?
-Are people interested in maintainig this?
Here is the content of init.lua
Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
- Code: Select all
--[[
-- The growable table is to be used as following:
-- name describes the node that is to grow
-- interval is the interval of the grow abm
-- chance is the chance of the grow abm
-- surfaces is a table of:
-- the name of the node the said node is allowed to grow
-- the odds it has over 1000 to grow at each callback cycle
-- the minimal_light needed to grow on this surface
-- the maximal height of the plant on this surface
--]]
growable = {
{
name = "default:papyrus",
interval = 270,
chance = 2,
surfaces =
{
-- Papyrus will grow normally on dirt,
-- it has 100/1000 chances of growing on a cycle
{name = "default:dirt", odds = 100, minimal_light = 12, max_height = 4},
-- Papyrus won't grow very well on sand
{name = "default:sand", odds = 20, minimal_light = 14, max_height = 3}
}
},
{
name = "default:cactus",
interval = 270,
chance = 2,
surfaces =
{
{name = "default:desert_sand", odds = 100, minimal_light = 12, max_height = 4},
{name = "default:sand", odds = 80, minimal_light = 14, max_height = 3}
}
}
}
for _, e in ipairs(growable) do
minetest.register_abm(
{
nodenames = { e.name },
interval = e.interval,
chance = e.chance,
action = function(pos, node, active_object_count, active_object_count_wider)
-- First check if there is space above to grow
p_top = {x = pos.x, y = pos.y + 1, z = pos.z}
n_top = minetest.env:get_node(p_top)
if n_top.name == "air" then
--Get light level
local light = minetest.env:get_node_light(p_top, nil)
if light == nil then light = 0 end
-- Calc current height
cur_height = 1
p_next = {x = pos.x, y = pos.y - 1, z = pos.z}
n_next = minetest.env:get_node(p_next);
while true do
if n_next.name ~= node.name then
break
end
cur_height = cur_height + 1
p_next = {x = p_next.x, y = p_next.y - 1, z = p_next.z}
n_next = minetest.env:get_node(p_next)
end
--analyze surface properties
for _, s in ipairs(e.surfaces) do
if string.sub(n_next.name, 1, string.len(s.name)) == s.name and (math.random(1, 1000) > (1000 - s.odds)) then
if (cur_height < s.max_height) and (light >= s.minimal_light) then
minetest.env:add_node(p_top, {name = node.name})
end
end
end
end
end
})
end
[edit] sorry for my english [/edit]