Refactored nature classic global functions

This commit is contained in:
neko259 2014-10-31 22:09:32 +02:00
parent 7a513e85b1
commit b16a5f0a68

View File

@ -1,16 +1,21 @@
local NODE_YOUNG = "young" local NODE_YOUNG = "young"
local SETTING_TRUE = "true"
local SETTING_FALSE = "false"
local YOUTH_DELAY = 5
nature = {} nature = {}
local function set_young_node(pos) local function set_young_node(pos)
local meta = minetest.get_meta(pos) local meta = minetest.get_meta(pos)
meta:set_string(NODE_YOUNG, "true") meta:set_string(NODE_YOUNG, SETTING_TRUE)
minetest.after(5, minetest.after(YOUTH_DELAY,
function(pos) function(pos)
local meta = minetest.get_meta(pos) local meta = minetest.get_meta(pos)
meta:set_string(NODE_YOUNG, "false") meta:set_string(NODE_YOUNG, SETTING_FALSE)
end, end,
pos) pos)
end end
@ -18,26 +23,26 @@ local function is_not_young(pos)
local meta = minetest.get_meta(pos) local meta = minetest.get_meta(pos)
local young = meta:get_string(NODE_YOUNG) local young = meta:get_string(NODE_YOUNG)
return young ~= "true" return young ~= SETTING_TRUE
end end
function nature:grow_node(pos, nodename) function nature:grow_node(pos, nodename)
if pos ~= nil then if pos ~= nil then
local light_enough = minetest.get_node_light(pos, nil) local light_enough = minetest.get_node_light(pos, nil)
>= MINIMUM_GROWTH_LIGHT >= MINIMUM_GROWTH_LIGHT
if is_not_young(pos) and light_enough then if is_not_young(pos) and light_enough then
minetest.remove_node(pos) minetest.remove_node(pos)
minetest.set_node(pos, { name = nodename }) minetest.set_node(pos, { name = nodename })
set_young_node(pos) set_young_node(pos)
minetest.log("info", nodename .. " has grown at " .. pos.x .. "," minetest.log("info", nodename .. " has grown at " .. pos.x .. ","
.. pos.y .. "," .. pos.z) .. pos.y .. "," .. pos.z)
end end
end end
end end
function nature:is_near_water(pos) function nature:is_near_water(pos)
return minetest.find_node_near(pos, DISTANCE_FROM_WATER, return DISTANCE_FROM_WATER == -1 or minetest.find_node_near(pos, DISTANCE_FROM_WATER,
{ "default:water_source" }) ~= nil or DISTANCE_FROM_WATER == -1 { "default:water_source" }) ~= nil
end end