nodecore-cd2025/mods/nc_tree/grow_active.lua

206 lines
5.9 KiB
Lua
Raw Normal View History

2018-11-03 11:26:15 -04:00
-- LUALOCALS < ---------------------------------------------------------
local math, minetest, nodecore, pairs
= math, minetest, nodecore, pairs
2020-01-15 22:38:02 -05:00
local math_random
= math.random
2018-11-03 11:26:15 -04:00
-- LUALOCALS > ---------------------------------------------------------
local modname = minetest.get_current_modname()
local function growparticles(pos, rate, width)
local zero = {x = 0, y = 0, z = 0}
nodecore.digparticles(minetest.registered_items[modname .. ":leaves_bud"],
{
amount = rate,
time = 10,
minpos = {
x = pos.x - width,
y = pos.y + 33/64,
z = pos.z - width
},
maxpos = {
x = pos.x + width,
y = pos.y + 33/64,
z= pos.z + width
},
minvel = zero,
maxvel = zero,
minexptime = 0.25,
maxexptime = 1,
minsize = 3 * width,
maxsize = 9 * width,
})
end
local sproutcost = 2000
nodecore.register_soaking_abm({
label = "EggCorn Growing",
fieldname = "eggcorn",
nodenames = {modname .. ":eggcorn_planted"},
interval = 10,
chance = 1,
limited_max = 100,
limited_alert = 1000,
soakrate = nodecore.tree_growth_rate,
soakcheck = function(data, pos)
if data.total >= sproutcost then
nodecore.node_sound(pos, "dig")
nodecore.set_loud(pos, {name = modname .. ":root"})
local apos = {x = pos.x, y = pos.y + 1, z = pos.z}
2020-01-21 20:49:31 -05:00
nodecore.witness(apos, "tree growth")
nodecore.set_loud(apos,
2020-01-15 22:38:02 -05:00
{name = modname .. ":tree_bud", param2 = 1})
return nodecore.soaking_abm_push(apos,
"treegrow", data.total - sproutcost)
2020-01-15 22:38:02 -05:00
end
return growparticles(pos, data.rate, 0.2)
end
})
2020-01-15 22:38:02 -05:00
local function leafbud(pos, dx, dy, dz, param2, surplus, rate)
2020-01-15 22:38:02 -05:00
local npos = {x = pos.x + dx, y = pos.y + dy, z = pos.z + dz}
if not nodecore.buildable_to(npos) then
local node = minetest.get_node(npos)
if minetest.get_item_group(node.name, "canopy") == 0 or param2 < node.param2
then return end
2020-01-15 22:38:02 -05:00
end
if param2 <= 1 then
if 240 < math_random(0, 255) then return end
return nodecore.set_loud(npos, nodecore.calc_leaves(npos))
end
nodecore.set_loud(npos, {name = modname .. ":leaves_bud", param2 = param2})
minetest.get_meta(npos):set_float("growrate", rate)
return nodecore.soaking_abm_push(npos, "leafgrow", surplus)
2020-01-15 22:38:02 -05:00
end
2020-01-15 23:10:18 -05:00
local trunkcost = 500
2020-01-15 22:38:02 -05:00
nodecore.register_soaking_abm({
label = "Tree Trunk Growth",
fieldname = "treegrow",
nodenames = {modname .. ":tree_bud"},
interval = 10,
2020-01-15 23:36:05 -05:00
chance = 1,
2020-01-15 22:38:02 -05:00
limited_max = 100,
limited_alert = 1000,
soakrate = nodecore.tree_trunk_growth_rate,
2020-01-15 22:38:02 -05:00
soakcheck = function(data, pos, node)
if data.total < trunkcost then
return growparticles(pos, data.rate, 0.45)
end
2020-01-15 22:38:02 -05:00
local tp = nodecore.tree_params[node.param2]
if not tp then return minetest.remove_node(pos) end
minetest.set_node(pos, {name = modname .. ":tree"})
local apos = {x = pos.x, y = pos.y + 1, z = pos.z}
if not nodecore.buildable_to(apos)
and minetest.get_item_group(minetest.get_node(apos).name, "canopy") == 0
then return end
2020-01-15 22:38:02 -05:00
local param2 = node.param2 + 1
tp = nodecore.tree_params[param2]
if not tp then return minetest.remove_node(pos) end
while tp.prob and (tp.prob < math_random(0, 255)) do
param2 = param2 + 1
tp = nodecore.tree_params[param2]
if not tp then return minetest.remove_node(pos) end
end
local surplus = data.total - trunkcost
2020-01-15 23:10:18 -05:00
if tp.leaves then
leafbud(apos, 1, 0, 0, tp.leaves + 1, surplus, data.rate)
leafbud(apos, -1, 0, 0, tp.leaves + 1, surplus, data.rate)
leafbud(apos, 0, 0, 1, tp.leaves, surplus, data.rate)
leafbud(apos, 0, 0, -1, tp.leaves, surplus, data.rate)
2020-01-15 23:10:18 -05:00
end
2020-01-15 22:38:02 -05:00
if tp.notrunk then
leafbud(apos, 0, 0, 0, tp.leaves, surplus, data.rate)
2020-01-15 22:38:02 -05:00
else
2020-01-21 20:49:31 -05:00
nodecore.witness(apos, "tree growth")
nodecore.set_loud(apos, {
2020-01-15 22:38:02 -05:00
name = modname .. ":tree_bud",
param2 = param2
})
nodecore.soaking_abm_push(apos,
"treegrow", surplus)
2020-01-15 22:38:02 -05:00
end
end
})
local leafcost = trunkcost
nodecore.register_soaking_abm({
2020-01-15 22:38:02 -05:00
label = "Tree Leaves Growth",
nodenames = {modname .. ":leaves_bud"},
fieldname = "leafgrow",
2020-01-15 22:38:02 -05:00
interval = 10,
chance = 1,
2020-01-15 22:38:02 -05:00
limited_max = 100,
limited_alert = 1000,
soakrate = function(pos)
local rate = minetest.get_meta(pos):get_float("growrate") or 0
return rate and rate ~= 0 and rate or 10
end,
soakcheck = function(data, pos, node)
if data.total < leafcost then return end
nodecore.set_loud(pos, nodecore.calc_leaves(pos))
local surplus = data.total - leafcost
2020-01-15 22:38:02 -05:00
if node.param2 <= 1 then
return
elseif node.param2 == 2 then
leafbud(pos, 1, 0, 0, 1, surplus, data.rate)
leafbud(pos, -1, 0, 0, 1, surplus, data.rate)
2020-01-15 22:38:02 -05:00
elseif node.param2 == 3 then
leafbud(pos, 0, 0, 1, 1, surplus, data.rate)
leafbud(pos, 0, 0, -1, 1, surplus, data.rate)
2020-01-15 22:38:02 -05:00
else
leafbud(pos, 1, 0, 0, 3, surplus, data.rate)
leafbud(pos, -1, 0, 0, 3, surplus, data.rate)
leafbud(pos, 0, 0, 1, 2, surplus, data.rate)
leafbud(pos, 0, 0, -1, 2, surplus, data.rate)
2020-01-15 22:38:02 -05:00
if node.param2 >= 6 then
leafbud(pos, 0, 1, 0, node.param2 - 4, surplus, data.rate)
2020-01-15 22:38:02 -05:00
end
end
end
})
local growtreedata = {
[modname .. ":eggcorn_planted"] = {
r = nodecore.tree_growth_rate,
f = "eggcorn"
},
[modname .. ":tree_bud"] = {
r = nodecore.tree_trunk_growth_rate,
f = "treegrow"
2020-01-20 07:05:00 -05:00
},
[modname .. ":leaves_bud"] = {
r = function() return 1 end,
f = "leafgrow"
}
}
minetest.register_chatcommand("growtrees", {
description = "Accelerate growth of nearby trees",
privs = {["debug"] = true},
func = function(pname)
local player = minetest.get_player_by_name(pname)
if not player then return end
local pos = player:get_pos()
local spec = {}
for k in pairs(growtreedata) do spec[#spec + 1] = k end
for _, p in pairs(nodecore.find_nodes_around(pos, spec, 5)) do
local nn = minetest.get_node(p).name
local data = growtreedata[nn]
local r = data.r(p)
if r and r > 0 then
nodecore.soaking_abm_push(p, data.f, 100000)
minetest.chat_send_player(pname, "boosted "
2020-01-21 21:37:08 -05:00
.. nn .. " at " .. minetest.pos_to_string(p))
end
end
2020-01-21 21:37:08 -05:00
end
})