- local is_stuck = false
- local snode = minetest.get_node_or_nil(pos)
- if snode then
- local sdef = minetest.registered_nodes[snode.name] or {}
- is_stuck = (sdef.walkable == nil or sdef.walkable == true)
- and (sdef.collision_box == nil or sdef.collision_box.type == "regular")
- and (sdef.node_box == nil or sdef.node_box.type == "regular")
- end
-
- -- Push item out when stuck inside solid node
- if is_stuck then
- local shootdir
- local order = {
- {x=1, y=0, z=0}, {x=-1, y=0, z= 0},
- {x=0, y=0, z=1}, {x= 0, y=0, z=-1},
- }
-
- -- Check which one of the 4 sides is free
- for o = 1, #order do
- local cnode = minetest.get_node(vector.add(pos, order[o])).name
- local cdef = minetest.registered_nodes[cnode] or {}
- if cnode ~= "ignore" and cdef.walkable == false then
- shootdir = order[o]
- break
- end
- end
- -- If none of the 4 sides is free, check upwards
- if not shootdir then
- shootdir = {x=0, y=1, z=0}
- local cnode = minetest.get_node(vector.add(pos, shootdir)).name
- if cnode == "ignore" then
- shootdir = nil -- Do not push into ignore
- end
- end
-
- if shootdir then
- -- Set new item moving speed accordingly
- local newv = vector.multiply(shootdir, 3)
- self:disable_physics()
- self.object:set_velocity(newv)
-
- self.force_out = newv
- self.force_out_start = vector.round(pos)
- return
- end
- elseif self.force_out then
- -- This code runs after the entity got a push from the above code.
- -- It makes sure the entity is entirely outside the solid node
- local c = self.object:get_properties().collisionbox
- local s = self.force_out_start
- local f = self.force_out
- local ok = (f.x > 0 and pos.x + c[1] > s.x + 0.5) or
- (f.y > 0 and pos.y + c[2] > s.y + 0.5) or
- (f.z > 0 and pos.z + c[3] > s.z + 0.5) or
- (f.x < 0 and pos.x + c[4] < s.x - 0.5) or
- (f.z < 0 and pos.z + c[6] < s.z - 0.5)
- if ok then
- -- Item was successfully forced out
- self.force_out = nil
- self:enable_physics()
- end
- end
-