Axinite tree groth now uses lbm

This commit is contained in:
Peppy 2019-11-19 08:45:22 +01:00
parent 9246127cd6
commit 2995d49803
2 changed files with 150 additions and 74 deletions

View File

@ -146,77 +146,6 @@ function grow_crystal(pos, node)
minetest.swap_node( consumepos, {name="air"} )
end
-- Axinite tree
local add_tree = function (pos, ofx, ofy, ofz, schem)
-- check for schematic
if not schem then
print ("Schematic not found")
return
end
-- remove sapling and place schematic
minetest.swap_node(pos, {name = "air"})
minetest.place_schematic(
{x = pos.x - ofx, y = pos.y - ofy, z = pos.z - ofz},
schem, 0, nil, false)
end
function grow_axinitium_tree(pos)
add_tree(pos, 2, 1, 2, path .. "/schematics/tree.mts")
end
-- check if sapling has enough height room to grow
local function enough_height(pos, height)
local nod = minetest.line_of_sight(
{x = pos.x, y = pos.y + 1, z = pos.z},
{x = pos.x, y = pos.y + height, z = pos.z})
if not nod then
return false -- obstructed
else
return true -- can grow
end
end
local grow_sapling = function (pos, node)
local under = minetest.get_node({
x = pos.x,
y = pos.y - 1,
z = pos.z
}).name
if not minetest.registered_nodes[node.name] then
return
end
local height = minetest.registered_nodes[node.name].grown_height
-- do we have enough height to grow sapling into tree?
if not height or not enough_height(pos, height) then
return
end
-- Check if Axinitium Sapling is growing on correct substrate
if node.name == "axinitium:sapling"
and under == "axinitium:dirt" then
grow_axinitium_tree(pos)
end
end
-- Grow saplings
minetest.register_abm({
label = "Axinitium grow sapling",
nodenames = {"axinitium:sapling"},
interval = 40,
chance = 8,
catch_up = false,
action = function(pos, node)
local light_level = minetest.get_node_light(pos)
if not light_level or light_level < 13 then
return
end
grow_sapling(pos, node)
end,
})
-- Grow axinitium crystal
minetest.register_abm({
nodenames = {"axinitium:crystal_ore1", "axinitium:crystal_ore2",

153
tree.lua
View File

@ -1,24 +1,171 @@
local S = axinitium.intllib
-- Required wrapper to allow customization of axinitium.grow_sapling
local function grow_sapling(...)
return axinitium.grow_sapling(...)
end
function axinitium.can_grow(pos)
local node_under = minetest.get_node_or_nil({x = pos.x, y = pos.y - 1, z = pos.z})
if not node_under then
return false
end
local name_under = node_under.name
if not name_under == "axinitium:dirt" then
return false
end
local light_level = minetest.get_node_light(pos)
if not light_level or light_level < 13 then
return false
end
return true
end
function axinitium.grow_sapling(pos)
if not axinitium.can_grow(pos) then
-- try again 5 min later
minetest.get_node_timer(pos):start(300)
return
end
minetest.log("action", "An axinitium tree sapling grows into a tree at "..
minetest.pos_to_string(pos))
axinitium.grow_axinitium_tree(pos)
end
minetest.register_lbm({
name = "axinitium:convert_axinitium_tree_saplings_to_node_timer",
nodenames = {"axinitium:sapling"},
action = function(pos)
minetest.get_node_timer(pos):start(math.random(300, 1500))
end
})
function axinitium.grow_axinitium_tree(pos)
local path = minetest.get_modpath("axinitium") ..
"/schematics/tree.mts"
minetest.swap_node(pos, {name = "air"})
minetest.place_schematic({x = pos.x - 2, y = pos.y, z = pos.z - 2},
path, "random", nil, false)
end
--
-- Sapling 'on place' function to check protection of node and resulting tree volume
--
function axinitium.sapling_on_place(itemstack, placer, pointed_thing,
sapling_name, minp_relative, maxp_relative, interval)
-- Position of sapling
local pos = pointed_thing.under
local node = minetest.get_node_or_nil(pos)
local pdef = node and minetest.registered_nodes[node.name]
if pdef and pdef.on_rightclick and
not (placer and placer:is_player() and
placer:get_player_control().sneak) then
return pdef.on_rightclick(pos, node, placer, itemstack, pointed_thing)
end
if not pdef or not pdef.buildable_to then
pos = pointed_thing.above
node = minetest.get_node_or_nil(pos)
pdef = node and minetest.registered_nodes[node.name]
if not pdef or not pdef.buildable_to then
return itemstack
end
end
local player_name = placer and placer:get_player_name() or ""
-- Check sapling position for protection
if minetest.is_protected(pos, player_name) then
minetest.record_protection_violation(pos, player_name)
return itemstack
end
-- Check tree volume for protection
if minetest.is_area_protected(
vector.add(pos, minp_relative),
vector.add(pos, maxp_relative),
player_name,
interval) then
minetest.record_protection_violation(pos, player_name)
-- Print extra information to explain
-- minetest.chat_send_player(player_name,
-- itemstack:get_definition().description .. " will intersect protection " ..
-- "on growth")
minetest.chat_send_player(player_name,
S("@1 will intersect protection on growth.",
itemstack:get_definition().description))
return itemstack
end
minetest.log("action", player_name .. " places node "
.. sapling_name .. " at " .. minetest.pos_to_string(pos))
local take_item = not (creative and creative.is_enabled_for
and creative.is_enabled_for(player_name))
local newnode = {name = sapling_name}
local ndef = minetest.registered_nodes[sapling_name]
minetest.set_node(pos, newnode)
-- Run callback
if ndef and ndef.after_place_node then
-- Deepcopy place_to and pointed_thing because callback can modify it
if ndef.after_place_node(table.copy(pos), placer,
itemstack, table.copy(pointed_thing)) then
take_item = false
end
end
-- Run script hook
for _, callback in ipairs(minetest.registered_on_placenodes) do
-- Deepcopy pos, node and pointed_thing because callback can modify them
if callback(table.copy(pos), table.copy(newnode),
placer, table.copy(node or {}),
itemstack, table.copy(pointed_thing)) then
take_item = false
end
end
if take_item then
itemstack:take_item()
end
return itemstack
end
-- Axinite tree
minetest.register_node("axinitium:sapling", {
description = S("Axinitium Tree Sapling"),
drawtype = "plantlike",
visual_scale = 1.0,
tiles = {"sapling.png"},
inventory_image = "sapling.png",
wield_image = "sapling.png",
paramtype = "light",
sunlight_propagates = true,
walkable = false,
on_timer = grow_sapling,
selection_box = {
type = "fixed",
fixed = {-0.3, -0.5, -0.3, 0.3, 0.35, 0.3}
},
groups = {snappy = 2, dig_immediate = 3, flammable = 2,
attached_node = 1, sapling = 1, axinite_sapling = 1},
attached_node = 1, sapling = 1},
sounds = default.node_sound_leaves_defaults(),
grown_height = 8,
on_construct = function(pos)
minetest.get_node_timer(pos):start(math.random(300, 1500))
end,
on_place = function(itemstack, placer, pointed_thing)
itemstack = default.sapling_on_place(itemstack, placer, pointed_thing,
"axinitium:sapling",
-- minp, maxp to be checked, relative to sapling pos
-- minp_relative.y = 1 because sapling pos has been checked
{x = -2, y = 1, z = -2},
{x = 2, y = 7, z = 2},
-- maximum interval of interior volume check
4)
return itemstack
end,
})
minetest.register_node("axinitium:tree", {