nodecore-cd2025/mods/nc_tree/cultivation.lua

258 lines
7.3 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 ldname = "nc_terrain:dirt_loose"
local epname = modname .. ":eggcorn_planted"
2018-11-03 11:26:15 -04:00
minetest.register_node(modname .. ":eggcorn", {
description = "Eggcorn",
2018-11-03 11:26:15 -04:00
drawtype = "plantlike",
paramtype = "light",
visual_scale = 0.5,
2019-03-23 20:05:56 -04:00
wield_scale = {x = 0.75, y = 0.75, z = 1.5},
2018-11-03 11:26:15 -04:00
collision_box = nodecore.fixedbox(-3/16, -0.5, -3/16, 3/16, 0, 3/16),
selection_box = nodecore.fixedbox(-3/16, -0.5, -3/16, 3/16, 0, 3/16),
inventory_image = "[combine:24x24:4,4=" .. modname .. "_eggcorn.png",
tiles = {modname .. "_eggcorn.png"},
2018-11-03 11:26:15 -04:00
groups = {
snappy = 1,
flammable = 3,
attached_node = 1,
},
node_placement_prediction = "",
place_as_item = true,
2019-03-25 00:15:05 -04:00
sounds = nodecore.sounds("nc_tree_corny"),
stack_rightclick = function(pos, _, whom, stack)
if nodecore.stack_get(pos):get_count() ~= 1 then return end
local def = minetest.registered_items[stack:get_name()]
if (not def) or (not def.groups) or (not def.groups.dirt_loose) then return end
nodecore.set_loud(pos, {name = epname})
if nodecore.player_stat_add then
nodecore.player_stat_add(1, whom, "craft", "eggcorn planting")
end
minetest.log((whom and whom:get_player_name() or "unknown")
.. " planted an eggcorn at " .. minetest.pos_to_string(pos))
stack:set_count(stack:get_count() - 1)
return stack
end
2018-11-03 11:26:15 -04:00
})
nodecore.register_limited_abm({
interval = 1,
chance = 1,
nodenames = {modname .. ":eggcorn"},
action = function(pos)
minetest.remove_node(pos)
return nodecore.place_stack(pos, modname .. ":eggcorn")
end
})
nodecore.register_leaf_drops(function(_, node, list)
2018-11-03 11:26:15 -04:00
list[#list + 1] = {
name = "air",
item = modname .. ":eggcorn",
prob = 0.05 * (node.param2 + 1)}
2018-11-03 11:26:15 -04:00
end)
local epdef = nodecore.underride({
drop = ldname,
no_self_repack = true
}, minetest.registered_items[ldname] or {})
epdef.groups.soil = nil
minetest.register_node(epname, epdef)
local sproutcost = 2000
nodecore.register_soaking_abm({
label = "EggCorn Growing",
fieldname = "eggcorn",
nodenames = {epname},
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}
nodecore.witness(apos, "grow tree")
nodecore.set_loud(apos,
2020-01-15 22:38:02 -05:00
{name = modname .. ":tree_bud", param2 = 1})
local sub = minetest.get_meta(apos)
sub:set_float("treegrowqty", data.total - sproutcost)
sub:set_float("treegrowtime", nodecore.gametime)
2020-01-15 22:38:02 -05:00
return
end
local zero = {x = 0, y = 0, z = 0}
nodecore.digparticles(minetest.registered_items[modname .. ":leaves"],
{
amount = data.rate,
time = 10,
minpos = {
x = pos.x - 0.3,
y = pos.y + 33/64,
z = pos.z - 0.3
},
maxpos = {
x = pos.x + 0.3,
y = pos.y + 33/64,
z= pos.z + 0.3
},
minvel = zero,
maxvel = zero,
minexptime = 0.1,
maxexptime = 0.5,
minsize = 1,
maxsize = 3,
})
end
})
2020-01-15 22:38:02 -05:00
local function leafbud(pos, dx, dy, dz, param2)
2020-01-15 23:01:11 -05:00
if param2 <= 1 then
if 240 < math_random(0, 255) then return end
local npos = {x = pos.x + dx, y = pos.y + dy, z = pos.z + dz}
if nodecore.buildable_to(npos) then
return nodecore.set_loud(npos, nodecore.calc_leaves(npos))
2020-01-15 23:01:11 -05:00
end
end
2020-01-15 22:38:02 -05:00
local npos = {x = pos.x + dx, y = pos.y + dy, z = pos.z + dz}
if nodecore.buildable_to(npos) then
return nodecore.set_loud(npos, {name = modname .. ":leaves_bud", param2 = param2})
2020-01-15 22:38:02 -05:00
end
local node = minetest.get_node(npos)
if minetest.get_item_group(node.name, "canopy") ~= 0 and param2 >= node.param2 then
return nodecore.set_loud(npos, {name = modname .. ":leaves_bud", param2 = param2})
end
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)
2020-01-15 23:10:18 -05:00
if data.total < trunkcost then return 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
2020-01-15 23:10:18 -05:00
if tp.leaves then
leafbud(apos, 1, 0, 0, tp.leaves + 1)
leafbud(apos, -1, 0, 0, tp.leaves + 1)
leafbud(apos, 0, 0, 1, tp.leaves)
leafbud(apos, 0, 0, -1, tp.leaves)
end
2020-01-15 22:38:02 -05:00
if tp.notrunk then
nodecore.set_loud(apos, {
2020-01-15 22:38:02 -05:00
name = modname .. ":leaves_bud",
param2 = tp.leaves
})
else
nodecore.witness(apos, "grow tree")
nodecore.set_loud(apos, {
2020-01-15 22:38:02 -05:00
name = modname .. ":tree_bud",
param2 = param2
})
2020-01-15 23:10:18 -05:00
local sub = minetest.get_meta(apos)
sub:set_float("treegrowqty", data.total - trunkcost)
sub:set_float("treegrowtime", nodecore.gametime)
2020-01-15 23:10:18 -05:00
return false
2020-01-15 22:38:02 -05:00
end
end
})
nodecore.register_limited_abm({
label = "Tree Leaves Growth",
nodenames = {modname .. ":leaves_bud"},
interval = 10,
2020-01-15 23:36:05 -05:00
chance = 10,
2020-01-15 22:38:02 -05:00
limited_max = 100,
limited_alert = 1000,
action = function(pos, node)
nodecore.set_loud(pos, nodecore.calc_leaves(pos))
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)
leafbud(pos, -1, 0, 0, 1)
elseif node.param2 == 3 then
leafbud(pos, 0, 0, 1, 1)
leafbud(pos, 0, 0, -1, 1)
else
leafbud(pos, 1, 0, 0, 3)
leafbud(pos, -1, 0, 0, 3)
leafbud(pos, 0, 0, 1, 2)
leafbud(pos, 0, 0, -1, 2)
if node.param2 >= 6 then
2020-01-15 23:01:11 -05:00
leafbud(pos, 0, 1, 0, node.param2 - 4)
2020-01-15 22:38:02 -05:00
end
end
end
})
local growtreedata = {
[epname] = {
r = nodecore.tree_growth_rate,
f = "eggcornqty"
},
[modname .. ":tree_bud"] = {
r = nodecore.tree_trunk_growth_rate,
f = "treegrowqty"
}
}
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
local meta = minetest.get_meta(p)
meta:set_float(data.f, 10000)
minetest.chat_send_player(pname, "boosted "
.. nn .. " at " .. minetest.pos_to_string(p))
end
end
end
})