2021-05-24 22:46:39 +00:00
|
|
|
local function get_falling_depth(self)
|
2018-02-05 18:58:31 +00:00
|
|
|
if not self._startpos then
|
|
|
|
-- Fallback
|
|
|
|
self._startpos = self.object:get_pos()
|
|
|
|
end
|
|
|
|
return self._startpos.y - vector.round(self.object:get_pos()).y
|
|
|
|
end
|
|
|
|
|
2021-05-24 22:46:39 +00:00
|
|
|
local function deal_falling_damage(self, dtime)
|
2018-02-05 18:20:14 +00:00
|
|
|
if minetest.get_item_group(self.node.name, "falling_node_damage") == 0 then
|
|
|
|
return
|
|
|
|
end
|
2021-03-26 16:38:25 +00:00
|
|
|
-- Cause damage to any entity it hits.
|
2018-02-05 18:01:54 +00:00
|
|
|
-- Algorithm based on MC anvils.
|
2018-02-05 16:18:28 +00:00
|
|
|
local pos = self.object:get_pos()
|
|
|
|
if not self._startpos then
|
2018-02-05 18:58:31 +00:00
|
|
|
-- Fallback
|
2018-02-05 16:18:28 +00:00
|
|
|
self._startpos = pos
|
|
|
|
end
|
2021-04-19 07:49:29 +00:00
|
|
|
self._hit = self._hit or {}
|
|
|
|
for _, obj in ipairs(minetest.get_objects_inside_radius(pos, 1)) do
|
|
|
|
if mcl_util.get_hp(obj) > 0 and not self._hit[obj] then
|
|
|
|
self._hit[obj] = true
|
|
|
|
local way = self._startpos.y - pos.y
|
|
|
|
local damage = (way - 1) * 2
|
|
|
|
damage = math.min(40, math.max(0, damage))
|
|
|
|
if damage >= 1 then
|
|
|
|
-- Reduce damage if wearing a helmet
|
|
|
|
local inv = mcl_util.get_inventory(obj)
|
|
|
|
if inv then
|
|
|
|
local helmet = inv:get_stack("armor", 2)
|
|
|
|
if minetest.get_item_group(helmet:get_name(), "combat_armor") > 0 then
|
|
|
|
damage = damage / 4 * 3
|
|
|
|
mcl_util.use_item_durability(helmet, 1)
|
|
|
|
inv:set_stack("armor", 2, helmet)
|
2021-03-26 16:38:25 +00:00
|
|
|
end
|
|
|
|
end
|
2021-05-24 22:46:39 +00:00
|
|
|
local dmg_type
|
2021-04-19 07:49:29 +00:00
|
|
|
if minetest.get_item_group(self.node.name, "anvil") ~= 0 then
|
2021-04-25 14:42:38 +00:00
|
|
|
dmg_type = "anvil"
|
2021-04-19 07:49:29 +00:00
|
|
|
else
|
2021-04-25 14:42:38 +00:00
|
|
|
dmg_type = "falling_node"
|
2021-03-26 16:38:25 +00:00
|
|
|
end
|
2021-04-19 07:49:29 +00:00
|
|
|
mcl_util.deal_damage(obj, damage, {type = dmg_type})
|
2021-03-26 16:38:25 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2018-02-05 16:18:28 +00:00
|
|
|
end
|
|
|
|
|
2017-11-26 14:05:45 +00:00
|
|
|
minetest.register_entity(":__builtin:falling_node", {
|
|
|
|
initial_properties = {
|
|
|
|
visual = "wielditem",
|
|
|
|
visual_size = {x = 0.667, y = 0.667},
|
|
|
|
textures = {},
|
|
|
|
physical = true,
|
|
|
|
is_visible = false,
|
|
|
|
collide_with_objects = false,
|
|
|
|
collisionbox = {-0.5, -0.5, -0.5, 0.5, 0.5, 0.5},
|
|
|
|
},
|
|
|
|
node = {},
|
|
|
|
meta = {},
|
|
|
|
set_node = function(self, node, meta)
|
2020-01-06 12:02:30 +00:00
|
|
|
local def = minetest.registered_nodes[node.name]
|
2019-12-17 20:24:57 +00:00
|
|
|
-- Change falling node if definition tells us to
|
|
|
|
if def and def._mcl_falling_node_alternative then
|
|
|
|
node.name = def._mcl_falling_node_alternative
|
|
|
|
end
|
2019-12-14 17:56:59 +00:00
|
|
|
local glow
|
2019-12-17 20:24:57 +00:00
|
|
|
self.node = node
|
|
|
|
self.meta = meta or {}
|
2018-01-31 18:10:14 +00:00
|
|
|
-- Set correct entity yaw
|
2018-02-02 21:12:45 +00:00
|
|
|
if def and node.param2 ~= 0 then
|
2018-01-31 18:10:14 +00:00
|
|
|
if (def.paramtype2 == "facedir" or def.paramtype2 == "colorfacedir") then
|
2020-01-06 12:02:30 +00:00
|
|
|
self.object:set_yaw(minetest.dir_to_yaw(minetest.facedir_to_dir(node.param2)))
|
2018-01-31 18:10:14 +00:00
|
|
|
elseif (def.paramtype2 == "wallmounted" or def.paramtype2 == "colorwallmounted") then
|
2020-01-06 12:02:30 +00:00
|
|
|
self.object:set_yaw(minetest.dir_to_yaw(minetest.wallmounted_to_dir(node.param2)))
|
2018-01-31 18:10:14 +00:00
|
|
|
end
|
2019-12-14 17:56:59 +00:00
|
|
|
if def.light_source then
|
|
|
|
glow = def.light_source
|
|
|
|
end
|
2018-01-31 18:10:14 +00:00
|
|
|
end
|
2019-12-14 17:56:59 +00:00
|
|
|
self.object:set_properties({
|
|
|
|
is_visible = true,
|
|
|
|
textures = {node.name},
|
|
|
|
glow = glow,
|
|
|
|
})
|
2017-11-26 14:05:45 +00:00
|
|
|
end,
|
|
|
|
get_staticdata = function(self)
|
2018-02-05 16:26:36 +00:00
|
|
|
local meta = self.meta
|
|
|
|
-- Workaround: Save inventory seperately from metadata.
|
|
|
|
-- Because Minetest crashes when a node with inventory gets deactivated
|
|
|
|
-- (GitHub issue #7020).
|
|
|
|
-- FIXME: Remove the _inv workaround when it is no longer needed
|
|
|
|
local inv
|
|
|
|
if meta then
|
|
|
|
inv = meta.inv
|
|
|
|
meta.inventory = nil
|
|
|
|
end
|
2017-11-26 14:05:45 +00:00
|
|
|
local ds = {
|
|
|
|
node = self.node,
|
|
|
|
meta = self.meta,
|
2018-02-05 16:26:36 +00:00
|
|
|
_inv = inv,
|
2018-02-05 16:18:28 +00:00
|
|
|
_startpos = self._startpos,
|
|
|
|
_hit_players = self._hit_players,
|
2017-11-26 14:05:45 +00:00
|
|
|
}
|
|
|
|
return minetest.serialize(ds)
|
|
|
|
end,
|
|
|
|
on_activate = function(self, staticdata)
|
|
|
|
self.object:set_armor_groups({immortal = 1})
|
2021-04-19 07:49:29 +00:00
|
|
|
|
2017-11-26 14:05:45 +00:00
|
|
|
local ds = minetest.deserialize(staticdata)
|
2018-02-05 16:18:28 +00:00
|
|
|
if ds then
|
|
|
|
self._startpos = ds._startpos
|
|
|
|
self._hit_players = ds._hit_players
|
|
|
|
if ds.node then
|
2018-02-05 16:26:36 +00:00
|
|
|
local meta = ds.meta
|
|
|
|
meta.inventory = ds._inv
|
|
|
|
self:set_node(ds.node, meta)
|
2018-02-05 16:18:28 +00:00
|
|
|
else
|
|
|
|
self:set_node(ds)
|
|
|
|
end
|
2017-11-26 14:05:45 +00:00
|
|
|
elseif staticdata ~= "" then
|
|
|
|
self:set_node({name = staticdata})
|
|
|
|
end
|
2018-02-05 18:58:31 +00:00
|
|
|
if not self._startpos then
|
|
|
|
self._startpos = self.object:get_pos()
|
|
|
|
end
|
|
|
|
self._startpos = vector.round(self._startpos)
|
2017-11-26 14:05:45 +00:00
|
|
|
end,
|
|
|
|
on_step = function(self, dtime)
|
|
|
|
-- Set gravity
|
2019-03-06 03:38:57 +00:00
|
|
|
local acceleration = self.object:get_acceleration()
|
2017-11-26 14:05:45 +00:00
|
|
|
if not vector.equals(acceleration, {x = 0, y = -10, z = 0}) then
|
2019-03-06 03:38:57 +00:00
|
|
|
self.object:set_acceleration({x = 0, y = -10, z = 0})
|
2017-11-26 14:05:45 +00:00
|
|
|
end
|
|
|
|
-- Turn to actual node when colliding with ground, or continue to move
|
2018-02-05 16:18:28 +00:00
|
|
|
local pos = self.object:get_pos()
|
2017-11-26 14:05:45 +00:00
|
|
|
|
|
|
|
-- Portal check
|
|
|
|
local np = {x = pos.x, y = pos.y + 0.3, z = pos.z}
|
|
|
|
local n2 = minetest.get_node(np)
|
|
|
|
if n2.name == "mcl_portals:portal_end" then
|
2021-04-19 07:49:29 +00:00
|
|
|
-- TODO: Teleport falling node.
|
2017-11-26 14:05:45 +00:00
|
|
|
self.object:remove()
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Position of bottom center point
|
|
|
|
local bcp = {x = pos.x, y = pos.y - 0.7, z = pos.z}
|
|
|
|
-- Avoid bugs caused by an unloaded node below
|
|
|
|
local bcn = minetest.get_node_or_nil(bcp)
|
|
|
|
local bcd = bcn and minetest.registered_nodes[bcn.name]
|
|
|
|
|
|
|
|
-- TODO: At this point, we did 2 get_nodes in 1 tick.
|
|
|
|
-- Figure out how to improve that (if it is a problem).
|
|
|
|
|
|
|
|
if bcn and (not bcd or bcd.walkable or
|
|
|
|
(minetest.get_item_group(self.node.name, "float") ~= 0 and
|
|
|
|
bcd.liquidtype ~= "none")) then
|
|
|
|
if bcd and bcd.leveled and
|
|
|
|
bcn.name == self.node.name then
|
|
|
|
local addlevel = self.node.level
|
|
|
|
if not addlevel or addlevel <= 0 then
|
|
|
|
addlevel = bcd.leveled
|
|
|
|
end
|
|
|
|
if minetest.add_node_level(bcp, addlevel) == 0 then
|
2018-02-05 18:58:31 +00:00
|
|
|
if minetest.registered_nodes[self.node.name]._mcl_after_falling then
|
|
|
|
minetest.registered_nodes[self.node.name]._mcl_after_falling(bcp, get_falling_depth(self))
|
|
|
|
end
|
2018-02-05 18:20:14 +00:00
|
|
|
deal_falling_damage(self, dtime)
|
2017-11-26 14:05:45 +00:00
|
|
|
self.object:remove()
|
|
|
|
return
|
|
|
|
end
|
|
|
|
elseif bcd and bcd.buildable_to and
|
|
|
|
(minetest.get_item_group(self.node.name, "float") == 0 or
|
|
|
|
bcd.liquidtype == "none") then
|
|
|
|
minetest.remove_node(bcp)
|
|
|
|
return
|
|
|
|
end
|
|
|
|
local nd = minetest.registered_nodes[n2.name]
|
2021-05-24 22:46:39 +00:00
|
|
|
--if n2.name == "mcl_portals:portal_end" then
|
|
|
|
-- TODO: Teleport falling node.
|
|
|
|
if (nd and nd.buildable_to == true) or minetest.get_item_group(self.node.name, "crush_after_fall") ~= 0 then
|
2017-11-26 14:05:45 +00:00
|
|
|
-- Replace destination node if it's buildable to
|
|
|
|
minetest.remove_node(np)
|
|
|
|
-- Run script hook
|
|
|
|
for _, callback in pairs(minetest.registered_on_dignodes) do
|
|
|
|
callback(np, n2)
|
|
|
|
end
|
2019-03-09 07:52:47 +00:00
|
|
|
local def = minetest.registered_nodes[self.node.name]
|
|
|
|
if def then
|
2017-11-26 14:05:45 +00:00
|
|
|
minetest.add_node(np, self.node)
|
2019-03-09 07:52:47 +00:00
|
|
|
if def._mcl_after_falling then
|
|
|
|
def._mcl_after_falling(np, get_falling_depth(self))
|
2018-02-05 18:58:31 +00:00
|
|
|
end
|
2017-11-26 14:05:45 +00:00
|
|
|
if self.meta then
|
|
|
|
local meta = minetest.get_meta(np)
|
|
|
|
meta:from_table(self.meta)
|
|
|
|
end
|
2019-03-09 07:52:47 +00:00
|
|
|
if def.sounds and def.sounds.place and def.sounds.place.name then
|
2020-04-06 22:55:45 +00:00
|
|
|
minetest.sound_play(def.sounds.place, {pos = np}, true)
|
2019-03-09 07:52:47 +00:00
|
|
|
end
|
2017-11-26 14:05:45 +00:00
|
|
|
end
|
|
|
|
else
|
|
|
|
-- Drop the *falling node* as an item if the destination node is NOT buildable to
|
|
|
|
local drops = minetest.get_node_drops(self.node.name, "")
|
|
|
|
for _, dropped_item in pairs(drops) do
|
|
|
|
minetest.add_item(np, dropped_item)
|
|
|
|
end
|
|
|
|
end
|
2018-02-05 18:20:14 +00:00
|
|
|
deal_falling_damage(self, dtime)
|
2017-11-26 14:05:45 +00:00
|
|
|
self.object:remove()
|
|
|
|
minetest.check_for_falling(np)
|
|
|
|
return
|
|
|
|
end
|
2019-03-06 03:38:57 +00:00
|
|
|
local vel = self.object:get_velocity()
|
2017-12-01 21:22:00 +00:00
|
|
|
-- Fix position if entity does not move
|
2017-11-26 14:05:45 +00:00
|
|
|
if vector.equals(vel, {x = 0, y = 0, z = 0}) then
|
2018-02-05 16:18:28 +00:00
|
|
|
local npos = vector.round(self.object:get_pos())
|
2017-12-01 21:22:00 +00:00
|
|
|
local npos2 = table.copy(npos)
|
|
|
|
npos2.y = npos2.y - 2
|
|
|
|
local lownode = minetest.get_node(npos2)
|
2018-01-29 02:18:02 +00:00
|
|
|
-- Special check required for fences and walls, because of their overhigh collision box.
|
|
|
|
if minetest.get_item_group(lownode.name, "fence") == 1 or minetest.get_item_group(lownode.name, "wall") == 1 then
|
|
|
|
-- Instantly stop the node if it is above a fence/wall. This is needed
|
|
|
|
-- because the falling node collides early with a fence/wall node.
|
2017-12-01 21:22:00 +00:00
|
|
|
-- Hacky, because the falling node will teleport a short distance, instead
|
|
|
|
-- of smoothly fall on the fence post.
|
|
|
|
local npos3 = table.copy(npos)
|
|
|
|
npos3.y = npos3.y - 1
|
|
|
|
minetest.add_node(npos3, self.node)
|
2019-03-09 07:52:47 +00:00
|
|
|
local def = minetest.registered_nodes[self.node.name]
|
|
|
|
if def then
|
|
|
|
if def._mcl_after_falling then
|
|
|
|
def._mcl_after_falling(npos3, get_falling_depth(self))
|
|
|
|
end
|
|
|
|
if def.sounds and def.sounds.place and def.sounds.place.name then
|
2020-04-06 22:55:45 +00:00
|
|
|
minetest.sound_play(def.sounds.place, {pos = np}, true)
|
2019-03-09 07:52:47 +00:00
|
|
|
end
|
2018-02-05 18:58:31 +00:00
|
|
|
end
|
2018-02-05 18:20:14 +00:00
|
|
|
deal_falling_damage(self, dtime)
|
2017-12-01 21:22:00 +00:00
|
|
|
self.object:remove()
|
|
|
|
minetest.check_for_falling(npos3)
|
|
|
|
return
|
|
|
|
else
|
|
|
|
-- Normal position fix (expected case)
|
2018-02-05 16:18:28 +00:00
|
|
|
self.object:set_pos(npos)
|
2017-12-01 21:22:00 +00:00
|
|
|
end
|
2017-11-26 14:05:45 +00:00
|
|
|
end
|
2018-02-05 18:20:14 +00:00
|
|
|
deal_falling_damage(self, dtime)
|
2017-11-26 14:05:45 +00:00
|
|
|
end
|
|
|
|
})
|