2022-12-29 19:16:48 +01:00
|
|
|
---
|
|
|
|
--- Generated by EmmyLua(https://github.com/EmmyLua)
|
|
|
|
--- Created by michieal.
|
|
|
|
--- DateTime: 12/29/22 12:34 PM -- Restructure Date
|
2023-01-09 05:08:12 +01:00
|
|
|
--- Copyright (C) 2022 - 2023, Michieal. See License.txt
|
2022-12-29 19:16:48 +01:00
|
|
|
|
2023-01-06 06:41:26 +01:00
|
|
|
local DEBUG = false
|
2022-12-29 19:16:48 +01:00
|
|
|
|
2023-01-02 05:07:20 +01:00
|
|
|
local rand = math.random
|
2023-01-03 04:36:09 +01:00
|
|
|
math.randomseed((os.time() + 31) * 31415) -- try to make a valid seed
|
2023-01-03 07:38:12 +01:00
|
|
|
local BAMBOO_MAX_HEIGHT = 16 -- base height check.
|
|
|
|
|
|
|
|
local BAMBOO_SOIL_DIST = BAMBOO_MAX_HEIGHT * -1
|
|
|
|
local BAM_MAX_HEIGHT_STPCHK = BAMBOO_MAX_HEIGHT - 5
|
|
|
|
local BAM_MAX_HEIGHT_TOP = BAMBOO_MAX_HEIGHT - 1
|
2023-01-05 04:43:05 +01:00
|
|
|
local GROW_DOUBLE_CHANCE = 32
|
|
|
|
|
2022-12-29 19:16:48 +01:00
|
|
|
--Bamboo can be planted on moss blocks, grass blocks, dirt, coarse dirt, rooted dirt, gravel, mycelium, podzol, sand, red sand, or mud
|
|
|
|
mcl_bamboo.bamboo_dirt_nodes = {
|
|
|
|
"mcl_core:redsand",
|
|
|
|
"mcl_core:sand",
|
|
|
|
"mcl_core:dirt",
|
|
|
|
"mcl_core:coarse_dirt",
|
|
|
|
"mcl_core:dirt_with_grass",
|
|
|
|
"mcl_core:podzol",
|
|
|
|
"mcl_core:mycelium",
|
|
|
|
"mcl_lush_caves:rooted_dirt",
|
|
|
|
"mcl_lush_caves:moss",
|
|
|
|
"mcl_mud:mud",
|
|
|
|
}
|
|
|
|
|
2023-01-04 00:40:40 +01:00
|
|
|
function mcl_bamboo.is_dirt(node_name)
|
2023-01-11 13:16:12 +01:00
|
|
|
local index = table.indexof(mcl_bamboo.bamboo_dirt_nodes, node_name)
|
|
|
|
if index == -1 then
|
|
|
|
return false
|
|
|
|
else
|
|
|
|
return true
|
|
|
|
end
|
2023-01-04 00:40:40 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
mcl_bamboo.bamboo_index = {
|
|
|
|
"mcl_bamboo:bamboo",
|
|
|
|
"mcl_bamboo:bamboo_1",
|
|
|
|
"mcl_bamboo:bamboo_2",
|
|
|
|
"mcl_bamboo:bamboo_3",
|
|
|
|
}
|
|
|
|
|
|
|
|
function mcl_bamboo.is_bamboo(node_name)
|
2023-01-11 13:16:12 +01:00
|
|
|
local index = table.indexof(mcl_bamboo.bamboo_index, node_name)
|
|
|
|
if index == -1 then
|
|
|
|
return false
|
|
|
|
else
|
|
|
|
return index
|
|
|
|
end
|
2023-01-04 00:40:40 +01:00
|
|
|
end
|
|
|
|
|
2022-12-29 19:16:48 +01:00
|
|
|
--- pos: node position; placer: ObjectRef that is placing the item
|
|
|
|
--- returns: true if protected, otherwise false.
|
|
|
|
function mcl_bamboo.is_protected(pos, placer)
|
|
|
|
local name = placer:get_player_name()
|
|
|
|
if minetest.is_protected(pos, name) then
|
|
|
|
minetest.record_protection_violation(pos, name)
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
2023-01-06 23:40:35 +01:00
|
|
|
local BAMBOO_ENDCAP_NAME = "mcl_bamboo:bamboo_endcap"
|
|
|
|
|
2023-06-18 21:14:43 +02:00
|
|
|
-- check if supporting block is broken. pistons now break the bamboo plant.
|
2023-02-09 03:48:19 +01:00
|
|
|
function mcl_bamboo.break_orphaned(pos)
|
2023-02-09 05:03:17 +01:00
|
|
|
mcl_bamboo.mcl_log("Break_Orphaned called.")
|
2023-02-10 04:36:25 +01:00
|
|
|
local node_below = minetest.get_node(vector.offset(pos, 0, -1, 0))
|
|
|
|
local node_name = node_below.name
|
2023-02-09 03:48:19 +01:00
|
|
|
|
2023-02-10 04:36:25 +01:00
|
|
|
-- short circuit checks.
|
2024-09-29 13:57:52 +02:00
|
|
|
if node_name == "ignore" or mcl_bamboo.is_dirt(node_name) or mcl_bamboo.is_bamboo(node_name) or mcl_bamboo.is_bamboo(minetest.get_node(pos).name) == false then
|
2023-02-10 04:36:25 +01:00
|
|
|
return
|
|
|
|
end
|
2023-02-09 03:48:19 +01:00
|
|
|
|
2023-02-10 04:36:25 +01:00
|
|
|
-- dig the node.
|
|
|
|
minetest.remove_node(pos) -- if that fails, remove the node
|
|
|
|
local istack = ItemStack("mcl_bamboo:bamboo")
|
|
|
|
local sound_params = {
|
|
|
|
pos = pos,
|
|
|
|
gain = 1.0, -- default
|
|
|
|
max_hear_distance = 10, -- default, uses a Euclidean metric
|
|
|
|
}
|
2023-02-09 03:48:19 +01:00
|
|
|
|
2023-02-10 04:36:25 +01:00
|
|
|
minetest.remove_node(pos)
|
|
|
|
minetest.sound_play(mcl_sounds.node_sound_wood_defaults().dug, sound_params, true)
|
|
|
|
minetest.add_item(pos, istack)
|
2023-02-09 03:48:19 +01:00
|
|
|
end
|
|
|
|
--]]
|
|
|
|
|
2023-01-05 04:43:05 +01:00
|
|
|
function mcl_bamboo.grow_bamboo(pos, bonemeal_applied)
|
2024-08-22 13:58:39 +02:00
|
|
|
local log = mcl_bamboo.mcl_log
|
2023-01-05 04:43:05 +01:00
|
|
|
local node_above = minetest.get_node(vector.offset(pos, 0, 1, 0))
|
2024-08-22 13:58:39 +02:00
|
|
|
log("Grow bamboo called; bonemeal: " .. tostring(bonemeal_applied))
|
2022-12-29 19:16:48 +01:00
|
|
|
|
2024-08-22 13:58:39 +02:00
|
|
|
if not bonemeal_applied then
|
|
|
|
-- Only allow natural growth at the top of the bamboo
|
|
|
|
if mcl_bamboo.is_bamboo(node_above.name) ~= false then return false end
|
|
|
|
|
|
|
|
-- Don't perform natual growth in low light
|
|
|
|
if minetest.get_node_light(pos) < 8 then return false end
|
2022-12-29 19:16:48 +01:00
|
|
|
end
|
2023-01-05 04:43:05 +01:00
|
|
|
|
2024-08-22 13:58:39 +02:00
|
|
|
-- Determine the location of soil
|
2023-01-05 04:43:05 +01:00
|
|
|
local soil_pos
|
2024-08-22 13:58:39 +02:00
|
|
|
soil_pos,a,b = mcl_util.trace_nodes(pos, -1, mcl_bamboo.bamboo_index, BAMBOO_MAX_HEIGHT - 1)
|
2023-01-05 04:43:05 +01:00
|
|
|
|
2024-08-22 13:58:39 +02:00
|
|
|
-- No soil found, return false so that bonemeal isn't used
|
|
|
|
if not soil_pos then return false end
|
|
|
|
log("Grow bamboo; soil found. ")
|
2023-01-05 04:43:05 +01:00
|
|
|
|
2024-08-22 13:58:39 +02:00
|
|
|
-- Find the first bamboo shoot and retrieve data about it
|
|
|
|
local first_shoot = vector.offset(soil_pos, 0, 1, 0)
|
|
|
|
local first_shoot_meta = minetest.get_meta(first_shoot)
|
2023-01-05 04:43:05 +01:00
|
|
|
|
2024-08-22 13:58:39 +02:00
|
|
|
-- Get or initialize bamboo height
|
|
|
|
local height = (first_shoot_meta and first_shoot_meta:get_int("height", -1)) or -1
|
|
|
|
if height == -1 then
|
|
|
|
height = rand(BAM_MAX_HEIGHT_STPCHK + 1, BAM_MAX_HEIGHT_TOP + 1)
|
|
|
|
first_shoot_meta:set_int("height", height)
|
|
|
|
end
|
|
|
|
log("Grow bamboo; height: " .. height)
|
2023-01-05 04:43:05 +01:00
|
|
|
|
2024-08-22 13:58:39 +02:00
|
|
|
-- Locate the bamboo tip
|
|
|
|
local bamboo_tip,actual_height,bamboo_tip_node = mcl_util.trace_nodes(first_shoot, 1, mcl_bamboo.bamboo_index, height - 1)
|
|
|
|
log("Current height: "..tostring(actual_height))
|
2023-01-05 04:43:05 +01:00
|
|
|
|
2024-08-22 13:58:39 +02:00
|
|
|
-- Short circuit growth if the bamboo is already finished growing
|
|
|
|
if not bamboo_tip or not actual_height or actual_height >= height then
|
|
|
|
log("Bamboo is already as large as it can grow")
|
|
|
|
return false
|
2023-01-05 04:43:05 +01:00
|
|
|
end
|
|
|
|
|
2024-08-22 13:58:39 +02:00
|
|
|
-- Now that we are actually going to add nodes, initialize some more information
|
|
|
|
local first_shoot_node_name = minetest.get_node(first_shoot).name
|
2023-01-06 06:41:26 +01:00
|
|
|
|
2024-08-22 13:58:39 +02:00
|
|
|
-- If applying bonemeal, randomly grow two segments instead of one
|
|
|
|
local grow_amount = 1
|
2023-01-05 04:43:05 +01:00
|
|
|
if bonemeal_applied then
|
2024-08-22 13:58:39 +02:00
|
|
|
local rng = PcgRandom(minetest.hash_node_position(pos) + minetest.get_us_time())
|
|
|
|
if rng:next(1, GROW_DOUBLE_CHANGE) == 1 then
|
|
|
|
grow_amount = 2
|
2023-01-05 04:43:05 +01:00
|
|
|
end
|
|
|
|
end
|
2024-08-22 13:58:39 +02:00
|
|
|
log("Growing up to "..grow_amount.." segments")
|
2023-01-05 04:43:05 +01:00
|
|
|
|
2024-08-22 13:58:39 +02:00
|
|
|
-- Perform bamboo growth
|
|
|
|
for i = 1,grow_amount do
|
|
|
|
-- Check for air to grow into
|
|
|
|
local bamboo_tip_node = minetest.get_node(bamboo_tip)
|
|
|
|
if not bamboo_tip_node or bamboo_tip_node.name ~= "air" then
|
|
|
|
-- Something is blocking growth, stop and signal that use bonemeal has been used if at least on segment has grown
|
|
|
|
return i ~= 1
|
2023-01-11 08:19:19 +01:00
|
|
|
end
|
|
|
|
|
2024-08-22 13:58:39 +02:00
|
|
|
if actual_height + 1 == height then
|
|
|
|
-- This is the end cap
|
|
|
|
minetest.set_node(bamboo_tip, { name = BAMBOO_ENDCAP_NAME })
|
|
|
|
return true
|
|
|
|
else
|
|
|
|
-- This isn't the end cap, add a bamboo segment
|
|
|
|
minetest.set_node(bamboo_tip, { name = first_shoot_node_name })
|
|
|
|
actual_height = actual_height + 1
|
2022-12-29 19:16:48 +01:00
|
|
|
end
|
2023-01-05 04:43:05 +01:00
|
|
|
|
2024-08-22 13:58:39 +02:00
|
|
|
bamboo_tip = vector.offset(bamboo_tip, 0, 1, 0)
|
2022-12-29 19:16:48 +01:00
|
|
|
end
|
2024-08-22 13:58:39 +02:00
|
|
|
|
|
|
|
return true
|
2022-12-29 19:16:48 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
-- Add Groups function, courtesy of Warr1024.
|
2023-01-03 04:36:09 +01:00
|
|
|
function mcl_bamboo.add_groups(name, ...)
|
2022-12-29 19:16:48 +01:00
|
|
|
local def = minetest.registered_items[name] or error(name .. " not found")
|
|
|
|
local groups = {}
|
|
|
|
for k, v in pairs(def.groups) do
|
|
|
|
groups[k] = v
|
|
|
|
end
|
2023-01-03 04:36:09 +01:00
|
|
|
local function add_all(x, ...)
|
2022-12-29 19:16:48 +01:00
|
|
|
if not x then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
groups[x] = 1
|
2023-01-03 04:36:09 +01:00
|
|
|
return add_all(...)
|
2022-12-29 19:16:48 +01:00
|
|
|
end
|
|
|
|
addall(...)
|
2023-02-09 03:48:19 +01:00
|
|
|
return minetest.override_item(name, { groups = groups })
|
2022-12-29 19:16:48 +01:00
|
|
|
end
|
2023-01-03 07:38:12 +01:00
|
|
|
|
|
|
|
function mcl_bamboo.mcl_log(m, l)
|
2023-01-04 00:40:40 +01:00
|
|
|
if not m then
|
|
|
|
minetest.log("error", "expected string, received: " .. m)
|
|
|
|
return
|
|
|
|
end
|
2023-01-03 07:38:12 +01:00
|
|
|
if DEBUG then
|
|
|
|
if not l then
|
|
|
|
minetest.log("[mcl_bamboo]: " .. m)
|
2023-01-04 00:40:40 +01:00
|
|
|
else
|
|
|
|
minetest.log(l, "[mcl_bamboo]: " .. m)
|
2023-01-03 07:38:12 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|