2022-12-29 21:16:48 +03:00
|
|
|
---
|
|
|
|
--- Generated by EmmyLua(https://github.com/EmmyLua)
|
|
|
|
--- Created by michieal.
|
|
|
|
--- DateTime: 12/29/22 12:34 PM -- Restructure Date
|
2023-01-09 07:08:12 +03:00
|
|
|
--- Copyright (C) 2022 - 2023, Michieal. See License.txt
|
2022-12-29 21:16:48 +03:00
|
|
|
|
2023-01-06 08:41:26 +03:00
|
|
|
local DEBUG = false
|
2022-12-29 21:16:48 +03:00
|
|
|
|
2023-01-02 07:07:20 +03:00
|
|
|
local rand = math.random
|
2023-01-03 06:36:09 +03:00
|
|
|
math.randomseed((os.time() + 31) * 31415) -- try to make a valid seed
|
2023-01-03 09:38:12 +03: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 06:43:05 +03:00
|
|
|
local GROW_DOUBLE_CHANCE = 32
|
|
|
|
|
2022-12-29 21:16:48 +03: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 02:40:40 +03:00
|
|
|
function mcl_bamboo.is_dirt(node_name)
|
2023-01-11 15:16:12 +03: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 02:40:40 +03: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 15:16:12 +03:00
|
|
|
local index = table.indexof(mcl_bamboo.bamboo_index, node_name)
|
|
|
|
if index == -1 then
|
|
|
|
return false
|
|
|
|
else
|
|
|
|
return index
|
|
|
|
end
|
2023-01-04 02:40:40 +03:00
|
|
|
end
|
|
|
|
|
2022-12-29 21:16:48 +03: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-07 01:40:35 +03:00
|
|
|
local BAMBOO_ENDCAP_NAME = "mcl_bamboo:bamboo_endcap"
|
|
|
|
|
2023-02-09 05:48:19 +03:00
|
|
|
-- For when I learn more about the pistons...
|
|
|
|
function mcl_bamboo.break_orphaned(pos)
|
2023-02-09 07:03:17 +03:00
|
|
|
mcl_bamboo.mcl_log("Break_Orphaned called.")
|
2023-02-10 06:36:25 +03:00
|
|
|
local node_below = minetest.get_node(vector.offset(pos, 0, -1, 0))
|
|
|
|
local node_name = node_below.name
|
2023-02-09 05:48:19 +03:00
|
|
|
|
2023-02-10 06:36:25 +03:00
|
|
|
-- short circuit checks.
|
|
|
|
if 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
|
|
|
|
return
|
|
|
|
end
|
2023-02-09 05:48:19 +03:00
|
|
|
|
2023-02-10 06:36:25 +03: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 05:48:19 +03:00
|
|
|
|
2023-02-10 06:36:25 +03: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 05:48:19 +03:00
|
|
|
end
|
|
|
|
--]]
|
|
|
|
|
2023-01-05 06:43:05 +03:00
|
|
|
function mcl_bamboo.grow_bamboo(pos, bonemeal_applied)
|
|
|
|
local node_above = minetest.get_node(vector.offset(pos, 0, 1, 0))
|
2023-01-06 08:41:26 +03:00
|
|
|
mcl_bamboo.mcl_log("Grow bamboo called; bonemeal: " .. tostring(bonemeal_applied))
|
2022-12-29 21:16:48 +03:00
|
|
|
|
2023-01-11 15:16:12 +03:00
|
|
|
if not bonemeal_applied and mcl_bamboo.is_bamboo(node_above.name) ~= false then
|
2023-01-05 06:43:05 +03:00
|
|
|
return false -- short circuit this function if we're trying to grow (std) the bamboo and it's not the top shoot.
|
|
|
|
end
|
2022-12-29 21:16:48 +03:00
|
|
|
if minetest.get_node_light(pos) < 8 then
|
2023-01-05 06:43:05 +03:00
|
|
|
return false
|
2022-12-29 21:16:48 +03:00
|
|
|
end
|
2023-01-05 06:43:05 +03:00
|
|
|
|
|
|
|
-- variables used in more than one spot.
|
|
|
|
local first_shoot
|
|
|
|
local chk_pos
|
|
|
|
local soil_pos
|
|
|
|
local node_name = ""
|
|
|
|
local dist = 0
|
|
|
|
local node_below
|
|
|
|
-- -------------------
|
|
|
|
|
2023-01-06 08:41:26 +03:00
|
|
|
mcl_bamboo.mcl_log("Grow bamboo; checking for soil: ")
|
2023-01-11 15:16:12 +03:00
|
|
|
-- the soil node below the bamboo.
|
2022-12-29 21:16:48 +03:00
|
|
|
for py = -1, BAMBOO_SOIL_DIST, -1 do
|
|
|
|
chk_pos = vector.offset(pos, 0, py, 0)
|
2023-01-05 06:43:05 +03:00
|
|
|
node_name = minetest.get_node(chk_pos).name
|
|
|
|
if mcl_bamboo.is_dirt(node_name) then
|
2023-01-04 02:40:40 +03:00
|
|
|
soil_pos = chk_pos
|
2022-12-29 21:16:48 +03:00
|
|
|
break
|
2023-01-11 15:16:12 +03:00
|
|
|
end
|
|
|
|
if mcl_bamboo.is_bamboo(node_name) == false then
|
|
|
|
break
|
2022-12-29 21:16:48 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
-- requires knowing where the soil node is.
|
2023-01-05 06:43:05 +03:00
|
|
|
if soil_pos == nil then
|
|
|
|
return false -- returning false means don't use up the bonemeal.
|
2022-12-29 21:16:48 +03:00
|
|
|
end
|
2023-01-05 06:43:05 +03:00
|
|
|
|
2023-01-06 09:22:18 +03:00
|
|
|
mcl_bamboo.mcl_log("Grow bamboo; soil found. ")
|
2023-01-05 06:43:05 +03:00
|
|
|
local grow_amount = rand(1, GROW_DOUBLE_CHANCE)
|
|
|
|
grow_amount = rand(1, GROW_DOUBLE_CHANCE)
|
|
|
|
grow_amount = rand(1, GROW_DOUBLE_CHANCE) -- because yeah, not truly random, or even a good prng.
|
|
|
|
grow_amount = rand(1, GROW_DOUBLE_CHANCE)
|
2023-01-06 08:41:26 +03:00
|
|
|
local init_height = rand(BAM_MAX_HEIGHT_STPCHK + 1, BAM_MAX_HEIGHT_TOP + 1)
|
|
|
|
mcl_bamboo.mcl_log("Grow bamboo; random height: " .. init_height)
|
2023-01-05 06:43:05 +03:00
|
|
|
|
|
|
|
node_name = ""
|
|
|
|
|
|
|
|
-- update: add randomized max height to first node's meta data.
|
|
|
|
first_shoot = vector.offset(soil_pos, 0, 1, 0)
|
|
|
|
local meta = minetest.get_meta(first_shoot)
|
|
|
|
node_below = minetest.get_node(first_shoot).name
|
|
|
|
|
2023-01-06 08:41:26 +03:00
|
|
|
mcl_bamboo.mcl_log("Grow bamboo; checking height meta ")
|
2023-01-05 06:43:05 +03:00
|
|
|
-- check the meta data for the first node, to see how high to make the stalk.
|
|
|
|
if not meta then
|
|
|
|
-- if no metadata, set the metadata!!!
|
2023-01-06 08:41:26 +03:00
|
|
|
meta:set_int("height", init_height)
|
2023-01-05 06:43:05 +03:00
|
|
|
end
|
|
|
|
local height = meta:get_int("height", -1)
|
2023-01-06 08:41:26 +03:00
|
|
|
mcl_bamboo.mcl_log("Grow bamboo; meta-height: " .. height)
|
|
|
|
if height <= 10 then
|
2023-01-05 06:43:05 +03:00
|
|
|
height = init_height
|
2023-01-06 08:41:26 +03:00
|
|
|
meta:set_int("height", init_height)
|
2023-01-05 06:43:05 +03:00
|
|
|
end
|
|
|
|
|
2023-01-06 08:41:26 +03:00
|
|
|
mcl_bamboo.mcl_log("Grow bamboo; height: " .. height)
|
|
|
|
|
2022-12-29 21:16:48 +03:00
|
|
|
-- Bonemeal: Grows the bamboo by 1-2 stems. (per the minecraft wiki.)
|
2023-01-05 06:43:05 +03:00
|
|
|
if bonemeal_applied then
|
|
|
|
-- handle applying bonemeal.
|
|
|
|
for py = 1, BAM_MAX_HEIGHT_TOP do
|
|
|
|
-- find the top node of bamboo.
|
|
|
|
chk_pos = vector.offset(pos, 0, py, 0)
|
|
|
|
node_name = minetest.get_node(chk_pos).name
|
|
|
|
dist = vector.distance(soil_pos, chk_pos)
|
2023-01-07 01:40:35 +03:00
|
|
|
if mcl_bamboo.is_bamboo(node_name) == false or node_name == BAMBOO_ENDCAP_NAME then
|
2023-01-05 06:43:05 +03:00
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-01-06 08:41:26 +03:00
|
|
|
mcl_bamboo.mcl_log("Grow bamboo; dist: " .. dist)
|
|
|
|
|
2023-01-07 01:40:35 +03:00
|
|
|
if node_name == BAMBOO_ENDCAP_NAME then
|
2023-01-05 06:43:05 +03:00
|
|
|
-- prevent overgrowth
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
-- check to see if we have a full stalk of bamboo.
|
|
|
|
if dist >= height - 1 then
|
|
|
|
if dist == height - 1 then
|
|
|
|
-- equals top of the stalk before the cap
|
|
|
|
if node_name == "air" then
|
2023-01-06 08:41:26 +03:00
|
|
|
mcl_bamboo.mcl_log("Grow bamboo; Placing endcap")
|
2023-02-09 05:48:19 +03:00
|
|
|
minetest.set_node(vector.offset(chk_pos, 0, 1, 0), { name = BAMBOO_ENDCAP_NAME })
|
2023-01-05 06:43:05 +03:00
|
|
|
return true -- returning true means use up the bonemeal.
|
|
|
|
else
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
else
|
|
|
|
-- okay, we're higher than the end cap, fail out.
|
|
|
|
return false -- returning false means don't use up the bonemeal.
|
|
|
|
end
|
|
|
|
end
|
2022-12-29 21:16:48 +03:00
|
|
|
|
2023-01-05 06:43:05 +03:00
|
|
|
-- and now, the meat of the section... add bamboo to the stalk.
|
|
|
|
-- at this point, we should be lower than the generated maximum height. ~ about height -2 or lower.
|
|
|
|
if dist <= height - 2 then
|
|
|
|
if node_name == "air" then
|
|
|
|
-- here we can check to see if we can do up to 2 bamboo shoots onto the stalk
|
2023-01-06 08:41:26 +03:00
|
|
|
mcl_bamboo.mcl_log("Grow bamboo; Placing bamboo.")
|
2023-02-09 05:48:19 +03:00
|
|
|
minetest.set_node(chk_pos, { name = node_below })
|
2023-01-05 06:43:05 +03:00
|
|
|
-- handle growing a second node.
|
|
|
|
if grow_amount == 2 then
|
|
|
|
chk_pos = vector.offset(chk_pos, 0, 1, 0)
|
|
|
|
if minetest.get_node(chk_pos).name == "air" then
|
2023-01-06 08:41:26 +03:00
|
|
|
mcl_bamboo.mcl_log("Grow bamboo; OOOH! It's twofer day!")
|
2023-02-09 05:48:19 +03:00
|
|
|
minetest.set_node(chk_pos, { name = node_below })
|
2023-01-05 06:43:05 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
return true -- exit out with a success. We've added 1-2 nodes, per the wiki.
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Non-Bonemeal growth.
|
2022-12-29 21:16:48 +03:00
|
|
|
for py = 1, BAM_MAX_HEIGHT_TOP do
|
2023-01-05 06:43:05 +03:00
|
|
|
-- Find the topmost node above the stalk, and check it for "air"
|
2022-12-29 21:16:48 +03:00
|
|
|
chk_pos = vector.offset(pos, 0, py, 0)
|
2023-01-05 06:43:05 +03:00
|
|
|
node_below = minetest.get_node(pos).name
|
|
|
|
node_name = minetest.get_node(chk_pos).name
|
|
|
|
dist = vector.distance(soil_pos, chk_pos)
|
|
|
|
|
2023-01-11 10:19:19 +03:00
|
|
|
if node_name ~= "air" and mcl_bamboo.is_bamboo(node_name) == false then
|
|
|
|
break
|
|
|
|
end
|
|
|
|
|
2023-01-05 06:43:05 +03:00
|
|
|
-- stop growing check. ie, handle endcap placement.
|
|
|
|
if dist >= height - 1 then
|
2023-01-11 03:23:40 +03:00
|
|
|
local above_node_name = minetest.get_node(vector.offset(chk_pos, 0, 1, 0)).name
|
|
|
|
if node_name == "air" and above_node_name == "air" then
|
2023-01-05 06:43:05 +03:00
|
|
|
if height - 1 == dist then
|
2023-01-06 08:41:26 +03:00
|
|
|
mcl_bamboo.mcl_log("Grow bamboo; Placing endcap")
|
2023-02-09 05:48:19 +03:00
|
|
|
minetest.set_node(chk_pos, { name = BAMBOO_ENDCAP_NAME })
|
2022-12-29 21:16:48 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
break
|
|
|
|
end
|
2023-01-05 06:43:05 +03:00
|
|
|
|
|
|
|
-- handle regular node placement.
|
|
|
|
-- find the air node above the top shoot. place a node. And then, if short enough,
|
|
|
|
-- check for second node placement.
|
|
|
|
if node_name == "air" then
|
2023-01-06 08:41:26 +03:00
|
|
|
mcl_bamboo.mcl_log("Grow bamboo; dist: " .. dist)
|
|
|
|
mcl_bamboo.mcl_log("Grow bamboo; Placing bamboo.")
|
2023-02-09 05:48:19 +03:00
|
|
|
minetest.set_node(chk_pos, { name = node_below })
|
2023-01-05 06:43:05 +03:00
|
|
|
-- handle growing a second node. (1 in 32 chance.)
|
|
|
|
if grow_amount == 2 and dist <= height - 2 then
|
2022-12-29 21:16:48 +03:00
|
|
|
chk_pos = vector.offset(chk_pos, 0, 1, 0)
|
|
|
|
if minetest.get_node(chk_pos).name == "air" then
|
2023-01-06 08:41:26 +03:00
|
|
|
mcl_bamboo.mcl_log("Grow bamboo; OOOH! It's twofer day!")
|
2023-02-09 05:48:19 +03:00
|
|
|
minetest.set_node(chk_pos, { name = node_below })
|
2022-12-29 21:16:48 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Add Groups function, courtesy of Warr1024.
|
2023-01-03 06:36:09 +03:00
|
|
|
function mcl_bamboo.add_groups(name, ...)
|
2022-12-29 21:16:48 +03: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 06:36:09 +03:00
|
|
|
local function add_all(x, ...)
|
2022-12-29 21:16:48 +03:00
|
|
|
if not x then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
groups[x] = 1
|
2023-01-03 06:36:09 +03:00
|
|
|
return add_all(...)
|
2022-12-29 21:16:48 +03:00
|
|
|
end
|
|
|
|
addall(...)
|
2023-02-09 05:48:19 +03:00
|
|
|
return minetest.override_item(name, { groups = groups })
|
2022-12-29 21:16:48 +03:00
|
|
|
end
|
2023-01-03 09:38:12 +03:00
|
|
|
|
|
|
|
function mcl_bamboo.mcl_log(m, l)
|
2023-01-04 02:40:40 +03:00
|
|
|
if not m then
|
|
|
|
minetest.log("error", "expected string, received: " .. m)
|
|
|
|
return
|
|
|
|
end
|
2023-01-03 09:38:12 +03:00
|
|
|
if DEBUG then
|
|
|
|
if not l then
|
|
|
|
minetest.log("[mcl_bamboo]: " .. m)
|
2023-01-04 02:40:40 +03:00
|
|
|
else
|
|
|
|
minetest.log(l, "[mcl_bamboo]: " .. m)
|
2023-01-03 09:38:12 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|