Generalize the auto-planting mechanism
parent
9556808b82
commit
dced2d6488
|
@ -1,13 +1,8 @@
|
||||||
-- peoplecantplant - Plant saplings when touching the ground
|
-- peoplecantplant - Plant saplings when they touch the ground
|
||||||
-- Contains code fragments from builtin/game/item_entity.lua
|
|
||||||
|
|
||||||
|
|
||||||
-- Function that gets called when the builtin item stops
|
-- Function that gets called when the builtin item stops
|
||||||
function builtin_item_stopped(self, pos_below, node)
|
function builtin_item_stopped(self)
|
||||||
if minetest.get_item_group(node.name, "soil") == 0 then
|
|
||||||
return -- Not soil
|
|
||||||
end
|
|
||||||
|
|
||||||
local stack = ItemStack(self.itemstring)
|
local stack = ItemStack(self.itemstring)
|
||||||
if stack:get_count() > 1 then
|
if stack:get_count() > 1 then
|
||||||
return -- Obviously dropped by someone (more than 1 item on the stack)
|
return -- Obviously dropped by someone (more than 1 item on the stack)
|
||||||
|
@ -18,82 +13,54 @@ function builtin_item_stopped(self, pos_below, node)
|
||||||
return -- It is not a sapling
|
return -- It is not a sapling
|
||||||
end
|
end
|
||||||
|
|
||||||
if minetest.is_protected(pos_below, ":nobody") then
|
local pos = vector.round(self.object:get_pos())
|
||||||
|
local pos_below = vector.new(pos)
|
||||||
|
pos_below.y = pos_below.y - 1
|
||||||
|
|
||||||
|
if minetest.is_protected(pos, ":nobody") then
|
||||||
return -- The area is protected by someone
|
return -- The area is protected by someone
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local node_below = minetest.get_node(pos_below)
|
||||||
|
if minetest.get_item_group(node_below.name, "soil") == 0 then
|
||||||
|
return -- Not soil
|
||||||
|
end
|
||||||
|
|
||||||
if minetest.find_node_near(pos_below, 3, {"group:sapling", "group:tree"}) then
|
if minetest.find_node_near(pos_below, 3, {"group:sapling", "group:tree"}) then
|
||||||
return -- There's another tree around, prevent jungle
|
return -- There's another tree around, prevent jungle
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Using 'pos = pos_below' would not allow us to modify the tables seperately,
|
local node = minetest.get_node(pos).name
|
||||||
-- thus make a copy of it with vector.new
|
-- Get node definition to decide whether to replace or not
|
||||||
local pos = vector.new(pos_below)
|
-- fallback to empty table when it's an unknown node
|
||||||
pos.y = pos.y + 1
|
local nodedef = minetest.registered_nodes[node] or {}
|
||||||
|
if not nodedef.buildable_to then
|
||||||
if minetest.get_node(pos).name ~= "air" then
|
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
minetest.set_node(pos, {name = item})
|
minetest.set_node(pos, {name = item})
|
||||||
self.itemstring = ""
|
self.itemstring = ""
|
||||||
self.object:remove()
|
self.object:remove()
|
||||||
return true -- Success!
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
-- Lifespan of an entity, default it to 900 on fail
|
|
||||||
local time_to_live = tonumber(minetest.setting_get("item_entity_ttl")) or 900
|
|
||||||
|
|
||||||
-- Overwrite "on_step" in the entity that's used for dropped items
|
-- Overwrite "on_step" in the entity that's used for dropped items
|
||||||
minetest.registered_entities["__builtin:item"].on_step = function(self, dtime)
|
local entity_def = minetest.registered_entities["__builtin:item"]
|
||||||
self.age = self.age + dtime
|
local old_step = entity_def.on_step
|
||||||
if time_to_live > 0 and self.age > time_to_live then
|
entity_def.on_step = function(self, dtime)
|
||||||
-- Item expired, remove it
|
local old_acc = self.object:get_acceleration()
|
||||||
self.itemstring = ""
|
old_step(self, dtime)
|
||||||
self.object:remove()
|
|
||||||
return
|
if self.itemstring == "" then
|
||||||
|
return -- Item removed
|
||||||
|
end
|
||||||
|
if vector.equals(old_acc, {x=0, y=0, z=0}) then
|
||||||
|
return -- No motion change
|
||||||
end
|
end
|
||||||
|
|
||||||
local p = self.object:getpos()
|
-- Acceleration defines in this case whether it's moving or not
|
||||||
p.y = p.y - 0.5
|
local new_acc = self.object:get_acceleration()
|
||||||
local node = minetest.get_node(p)
|
if vector.equals(new_acc, {x=0, y=0, z=0}) then
|
||||||
local def = minetest.registered_nodes[node.name]
|
-- Not moving: Try to place on node below
|
||||||
-- Ignore is walkable, so let it stop until the stuff below loaded
|
builtin_item_stopped(self)
|
||||||
local entity_fall = (def and not def.walkable)
|
|
||||||
|
|
||||||
if self.physical_state == entity_fall then
|
|
||||||
return -- State didn't change, don't do anything
|
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Different to previous state - resetting the velocity doesn't hurt anything
|
|
||||||
self.object:setvelocity({x=0, y=0, z=0})
|
|
||||||
if entity_fall then
|
|
||||||
-- Entity is falling: downwards acceleration of earth
|
|
||||||
self.object:setacceleration({x=0, y=-9.81, z=0})
|
|
||||||
else
|
|
||||||
-- Entity stopped, call our magic planting function
|
|
||||||
local success = builtin_item_stopped(self, vector.round(p), node)
|
|
||||||
if success then
|
|
||||||
return -- The entity doesn't exist anymore when our function was successful
|
|
||||||
end
|
|
||||||
|
|
||||||
-- Code from the original __builtin:item, get surrounding objects
|
|
||||||
local own_stack = ItemStack(self.itemstring)
|
|
||||||
for _, object in ipairs(minetest.get_objects_inside_radius(p, 0.8)) do
|
|
||||||
local obj = object:get_luaentity()
|
|
||||||
if obj and obj.name == "__builtin:item"
|
|
||||||
and obj.physical_state == false then
|
|
||||||
-- Try to merge the items around with this one
|
|
||||||
if self:try_merge_with(own_stack, object, obj) then
|
|
||||||
return -- Item was removed/replaced
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
self.object:setacceleration({x=0, y=0, z=0})
|
|
||||||
end
|
|
||||||
|
|
||||||
self.physical_state = entity_fall
|
|
||||||
self.object:set_properties({
|
|
||||||
physical = entity_fall
|
|
||||||
})
|
|
||||||
end
|
end
|
Loading…
Reference in New Issue