MineClone2/mods/ITEMS/mcl_bamboo/globals.lua
Michieal ef7fb0d2e3 Cleaned up the code. Fixed side placement of bamboo against bamboo.
Dinked with the random number generator some more.

Condensed some more of the code duplication (WIP).

Added in MCL_Log function.

Finally settled on a decent looking Bamboo top.
2023-01-03 01:38:12 -05:00

144 lines
3.8 KiB
Lua

---
--- Generated by EmmyLua(https://github.com/EmmyLua)
--- Created by michieal.
--- DateTime: 12/29/22 12:34 PM -- Restructure Date
---
local modname = minetest.get_current_modname()
local S = minetest.get_translator(modname)
local bamboo = "mcl_bamboo:bamboo"
local DEBUG = false
local strlen = string.len
local substr = string.sub
local rand = math.random
math.randomseed((os.time() + 31) * 31415) -- try to make a valid seed
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
--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",
}
--- 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
function mcl_bamboo.grow_bamboo(pos, _)
local chk_pos
local soil_pos
if minetest.get_node_light(pos) < 8 then
return
end
local found = false -- used for the soil check
local mboo = ""
for py = -1, BAMBOO_SOIL_DIST, -1 do
chk_pos = vector.offset(pos, 0, py, 0)
local name = minetest.get_node(chk_pos).name
for i = 1, #mcl_bamboo.bamboo_dirt_nodes do
if name == mcl_bamboo.bamboo_dirt_nodes[i] then
found = true
soil_pos = chk_pos
break
end
end
if found then
break
else
mboo = substr(name, strlen(name) - 3, strlen(name))
if mboo ~= "mboo" and mboo ~= "oo_1" and mboo ~= "oo_2" and mboo ~= "oo_3" then
break
end
end
end
-- requires knowing where the soil node is.
if not found then
return
end
local grow_amount = rand(1, 32)
grow_amount = rand(1, 32)
grow_amount = rand(1, 32)
grow_amount = rand(1, 32) -- because yeah, not truly random, or even a good prng.
-- Bonemeal: Grows the bamboo by 1-2 stems. (per the minecraft wiki.)
for py = 1, BAM_MAX_HEIGHT_TOP do
chk_pos = vector.offset(pos, 0, py, 0)
local node_below = minetest.get_node(pos).name
local name = minetest.get_node(chk_pos).name
local dist = vector.distance(soil_pos, chk_pos)
if dist >= BAM_MAX_HEIGHT_STPCHK then
-- stop growing check.
if name == "air" then
local height = rand(BAM_MAX_HEIGHT_STPCHK, BAM_MAX_HEIGHT_TOP)
if height == dist then
minetest.set_node(chk_pos, {name = "mcl_bamboo:bamboo_endcap"})
end
end
break
end
mboo = substr(name, strlen(name) - 3, strlen(name))
if name == "air" then
minetest.set_node(chk_pos, {name = node_below})
-- 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
minetest.set_node(chk_pos, {name = node_below})
end
end
break
elseif mboo ~= "mboo" and mboo ~= "oo_1" and mboo ~= "oo_2" and mboo ~= "oo_3" then
break
end
end
end
-- Add Groups function, courtesy of Warr1024.
function mcl_bamboo.add_groups(name, ...)
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
local function add_all(x, ...)
if not x then
return
end
groups[x] = 1
return add_all(...)
end
addall(...)
return minetest.override_item(name, {groups = groups})
end
function mcl_bamboo.mcl_log(m, l)
if DEBUG then
if not l then
minetest.log("[mcl_bamboo]: " .. m)
end
minetest.log(l, "[mcl_bamboo]: " .. m)
end
end