Fixed moving items master
authorGabriel Pérez-Cerezo <gabriel@gpcf.eu>
Thu, 29 Jun 2017 19:28:22 +0000 (21:28 +0200)
committerGabriel Pérez-Cerezo <gabriel@gpcf.eu>
Thu, 29 Jun 2017 19:28:22 +0000 (21:28 +0200)
init.lua

index 3453d43..c64f061 100644 (file)
--- a/init.lua
+++ b/init.lua
@@ -35,10 +35,10 @@ local function abs(x)
    end
 end
 local speed = 0.4
-local function generate_velocity_vector(vx, vz, node)
+local function generate_velocity_vector(dir, vx, vz, node)
    local vy=0
    local p = 1
-   local dir = vector.new(minetest.facedir_to_dir(node.param2))
+--   local dir = vector.new(minetest.facedir_to_dir(node.param2))
    if node.name=="walkway:slope_1" or node.name=="walkway:slope_2"  then
       p = 1
       vx=0
@@ -50,7 +50,7 @@ local function generate_velocity_vector(vx, vz, node)
         vy = -1
       end
    elseif node.name=="walkway:slope_1_down" or node.name=="walkway:slope_2_down" then
-      vx=0 -- dirty fix
+      vx=0 -- dirty fix, could not get this to work properly.
       vz=0
       p = -1
       if same_sign(dir.x/speed+vx,dir.x) or same_sign(dir.z/speed+vz,dir.z) then
@@ -122,7 +122,7 @@ minetest.register_entity("walkway:moving_dummy",{
                                  -- detach on jump
                                  local veldir = self.object:getvelocity();
                                  self.player:set_detach()
-                                 self.player:setpos({x = pos.x + veldir.x / 3, y = pos.y, z = pos.z + veldir.z / 3})
+                                 self.player:setpos({x = pos.x + veldir.x / 3, y = pos.y+1, z = pos.z + veldir.z / 3})
                                  self.object:remove()
                                  default.player_attached[name] = false
                                  return
@@ -158,7 +158,7 @@ minetest.register_entity("walkway:moving_dummy",{
                               elseif dir.z == 0 then
                                  dir.z = (math.floor(pos.z + 0.5) - pos.z) * 2
                               end
-                              self.object:setvelocity(generate_velocity_vector(vx, vz, napos))
+                              self.object:setvelocity(generate_velocity_vector(dir, vx, vz, napos))
                            else
                               if self.player then
                                  local veldir = self.object:getvelocity();
@@ -174,7 +174,14 @@ minetest.register_entity("walkway:moving_dummy",{
                            end
                         end
 })
+nodedef = {
+   groups = {cracky=1},
+   paramtype = "light",
+   paramtype2 = "facedir",
+   is_ground_content = true,
+   legacy_facedir_simple = true,
 
+}
 
 minetest.register_node("walkway:belt", {
        description = "Moving Walkway",
@@ -278,23 +285,23 @@ minetest.register_node("walkway:slope_2_down",
 --             },
 -- })
 
-minetest.register_abm({
-       nodenames = {"walkway:belt", "walkway:belt_straight"},
-       neighbors = nil,
-       interval = 1,
-       chance = 1,
-       action = function(pos, node, active_object_count, active_object_count_wider)
-               local all_objects = minetest.get_objects_inside_radius(pos, 0.75)
-               local _,obj
-               for _,obj in ipairs(all_objects) do
-                       if not obj:is_player() and obj:get_luaentity() and obj:get_luaentity().name == "__builtin:item" then
-                               walkway.do_moving_item({x = pos.x, y = pos.y + 0.15, z = pos.z}, obj:get_luaentity().itemstring)
-                               obj:get_luaentity().itemstring = ""
-                               obj:remove()
-                       end
-               end
-       end,
-})
+-- minetest.register_abm({
+--     nodenames = {"walkway:belt", "walkway:belt_straight"},
+--     neighbors = nil,
+--     interval = 1,
+--     chance = 1,
+--     action = function(pos, node, active_object_count, active_object_count_wider)
+--             local all_objects = minetest.get_objects_inside_radius(pos, 0.75)
+--             local _,obj
+--             for _,obj in ipairs(all_objects) do
+--                     if not obj:is_player() and obj:get_luaentity() and obj:get_luaentity().name == "__builtin:item" then
+--                             walkway.do_moving_item({x = pos.x, y = pos.y + 0.15, z = pos.z}, obj:get_luaentity().itemstring)
+--                             obj:get_luaentity().itemstring = ""
+--                             obj:remove()
+--                     end
+--             end
+--     end,
+-- })
 
 minetest.register_abm({
        nodenames = {"walkway:belt", "walkway:belt_straight", "walkway:slope_1", "walkway:slope_2", "walkway:slope_1_down", "walkway:slope_2_down"},
@@ -302,23 +309,26 @@ minetest.register_abm({
        interval = 1,
        chance = 1,
        action = function(pos, node, active_object_count, active_object_count_wider)
-               local all_objects = minetest.get_objects_inside_radius(pos, 1)
+               local all_objects = minetest.get_objects_inside_radius(pos, 0.75)
                local _,obj
---             minetest.chat_send_all(node.name)
                for _,obj in ipairs(all_objects) do
-
                   if obj:is_player() and not obj:get_attach() then
-                     dum = walkway.do_moving_dummy({x = pos.x, y = pos.y + 0.15, z = pos.z}, obj):get_luaentity()
+--                   dum = walkway.do_moving_dummy():get_luaentity()
+                     local dum = minetest.add_entity({x = pos.x, y = pos.y + 0.15, z = pos.z}, "walkway:moving_dummy"):get_luaentity()
                      dum.player = obj
                      obj:set_attach(dum.object, "", {x=0,y=9,z=0}, {x=0,y=0,z=0})
                      local name = obj:get_player_name()
                      default.player_attached[name] = true
                   elseif obj:get_luaentity() and string.sub(obj:get_luaentity().name,1,string.len("mobs_animal"))  == "mobs_animal" then
-                     local napos = minetest.get_node(pos) 
-                     obj:setvelocity(generate_velocity_vector(0, 0, node))
+                     local dir = vector.new(minetest.facedir_to_dir(node.param2))
+                     obj:setvelocity(generate_velocity_vector(dir,0, 0, node))
 --                   dum = walkway.do_moving_dummy({x = pos.x, y = pos.y + 0.15, z = pos.z}, obj):get_luaentity()
 --                   dum.player = obj
 --                   obj:set_attach(dum.object, "", {x=0,y=-1,z=0}, {x=0,y=0,z=0})
+                  elseif obj:get_luaentity() and obj:get_luaentity().name == "__builtin:item" then
+                     walkway.do_moving_item({x = pos.x, y = pos.y + 0.15, z = pos.z}, obj:get_luaentity().itemstring)
+                     obj:get_luaentity().itemstring = ""
+                     obj:remove()
                   end
                end
        end,
@@ -336,13 +346,6 @@ function walkway.do_moving_item(pos, item)
        return obj
 end
 
-function walkway.do_moving_dummy(pos, player)
-        local stack = ItemStack(item)
-       local obj = minetest.add_entity(pos, "walkway:moving_dummy")
-       --      obj:attach(player)
-       return obj
-end
-
 
 minetest.register_entity("walkway:moving_item", {
        initial_properties = {
@@ -415,15 +418,23 @@ minetest.register_entity("walkway:moving_item", {
                local pos = self.object:getpos()
                local napos = minetest.get_node(pos) 
                local dir = vector.new(minetest.facedir_to_dir(napos.param2)) -- a copy of the facedir so we don't overwrite the facedir table
+               if napos.name == "air" then
+                  napos = minetest.get_node({x = pos.x, y = pos.y -1, z = pos.z})
+                  dir = vector.new(minetest.facedir_to_dir(napos.param2))
+               elseif not string.sub(napos.name,1,string.len("walkway:")) == "walkway:" then
+                  napos = minetest.get_node({x = pos.x, y = pos.y + 1, z = pos.z})
+                  dir = vector.new(minetest.facedir_to_dir(napos.param2))
+               end
+
                if napos.name == "walkway:belt_straight" then
                        self.object:setvelocity({x = dir.x / speed, y = 0, z = dir.z / speed})
-               elseif napos.name == "walkway:belt" then
+               elseif string.sub(napos.name,1,string.len("walkway:")) == "walkway:" then
                        if dir.x == 0 then
                                dir.x = (math.floor(pos.x + 0.5) - pos.x) * 2
                        elseif dir.z == 0 then
                                dir.z = (math.floor(pos.z + 0.5) - pos.z) * 2
                        end
-                       self.object:setvelocity({x = dir.x / speed, y = 0, z = dir.z / speed})
+                       self.object:setvelocity(generate_velocity_vector(dir, 0, 0, napos))
                else
                        local stack = ItemStack(self.itemstring)
                        local veldir = self.object:getvelocity();