local fdir_table = {
   {  1,  0 },
   {  0, -1 },
   { -1,  0 },
   {  0,  1 },
   {  1,  0 },
   {  0, -1 },
   { -1,  0 },
   {  0,  1 },
}

local laser_gate = {
   type = 'fixed',
      fixed = {
         {-.5, -.3, -.2, .5, .3, .2},},}

minetest.register_node('ship:laser_gate', {
   description = 'Laser Gate',
   drawtype = 'mesh',
   mesh = 'ship_laser_gate.obj',
   paramtype = 'light',
   paramtype2 = 'facedir',
   light_source = 10,
   tiles = {'ship_laser_gate.png'},
   groups = {breakable=1},
   selection_box = laser_gate,
   collision_box = laser_gate,
   after_place_node = function(pos, placer, itemstack)
      local node = minetest.get_node(pos)
      local fdir = node.param2
      local length = 0
      if fdir == 1 then -- negative z
         local pos1 = {x=pos.x, y=pos.y, z=pos.z-1}
         local pos1_name = minetest.get_node(pos1).name
         while pos1_name == 'air' and length <= 10 do
            length = length + 1
            pos1 = {x=pos1.x, y=pos1.y, z=pos1.z-1}
            pos1_name = minetest.get_node(pos1).name
         end
         if length >= 1 then
            minetest.set_node(pos, {name = 'ship:laser_gate_l', param2 = fdir})
            for i = 1, (length-1) do
               local place_pos = {x=pos.x, y=pos.y, z=pos.z-i}
               minetest.set_node(place_pos, {name = 'ship:laser_gate_m', param2 = fdir})
            end
            local end_pos = {x=pos.x, y=pos.y, z=pos.z-length}
            minetest.set_node(end_pos, {name = 'ship:laser_gate_r', param2 = fdir})
         end
      elseif fdir == 2 then -- negative x
         local pos1 = {x=pos.x-1, y=pos.y, z=pos.z}
         local pos1_name = minetest.get_node(pos1).name
         while pos1_name == 'air' and length <= 10 do
            length = length + 1
            pos1 = {x=pos1.x-1, y=pos1.y, z=pos1.z}
            pos1_name = minetest.get_node(pos1).name
         end
         if length >= 1 then
            minetest.set_node(pos, {name = 'ship:laser_gate_l', param2 = fdir})
            for i = 1, (length-1) do
               local place_pos = {x=pos.x-i, y=pos.y, z=pos.z}
               minetest.set_node(place_pos, {name = 'ship:laser_gate_m', param2 = fdir})
            end
            local end_pos = {x=pos.x-length, y=pos.y, z=pos.z}
            minetest.set_node(end_pos, {name = 'ship:laser_gate_r', param2 = fdir})
         end
      elseif fdir == 3 then -- positive z
         local pos1 = {x=pos.x, y=pos.y, z=pos.z+1}
         local pos1_name = minetest.get_node(pos1).name
         while pos1_name == 'air' and length <= 10 do
            length = length + 1
            pos1 = {x=pos1.x, y=pos1.y, z=pos1.z+1}
            pos1_name = minetest.get_node(pos1).name
         end
         if length >= 1 then
            minetest.set_node(pos, {name = 'ship:laser_gate_l', param2 = fdir})
            for i = 1, (length-1) do
               local place_pos = {x=pos.x, y=pos.y, z=pos.z+i}
               minetest.set_node(place_pos, {name = 'ship:laser_gate_m', param2 = fdir})
            end
            local end_pos = {x=pos.x, y=pos.y, z=pos.z+length}
            minetest.set_node(end_pos, {name = 'ship:laser_gate_r', param2 = fdir})
         end
      elseif fdir == 0 then -- positive x
         local pos1 = {x=pos.x+1, y=pos.y, z=pos.z}
         local pos1_name = minetest.get_node(pos1).name
         while pos1_name == 'air' and length <= 10 do
            length = length + 1
            pos1 = {x=pos1.x+1, y=pos1.y, z=pos1.z}
            pos1_name = minetest.get_node(pos1).name
         end
         if length >= 1 then
            minetest.set_node(pos, {name = 'ship:laser_gate_l', param2 = fdir})
            for i = 1, (length-1) do
               local place_pos = {x=pos.x+i, y=pos.y, z=pos.z}
               minetest.set_node(place_pos, {name = 'ship:laser_gate_m', param2 = fdir})
            end
            local end_pos = {x=pos.x+length, y=pos.y, z=pos.z}
            minetest.set_node(end_pos, {name = 'ship:laser_gate_r', param2 = fdir})
         end
      end
   end,
})

minetest.register_node('ship:laser_gate_l', {
   drawtype = 'mesh',
   mesh = 'ship_laser_gate_L.obj',
   paramtype = 'light',
   paramtype2 = 'facedir',
   light_source = 10,
   tiles = {'ship_laser_gate.png'},
   groups = {breakable=1, not_in_creative_inventory=1},
   drop = 'ship:laser_gate',
   selection_box = laser_gate,
   collision_box = laser_gate,
})

minetest.register_node('ship:laser_gate_r', {
   drawtype = 'mesh',
   mesh = 'ship_laser_gate_R.obj',
   paramtype = 'light',
   paramtype2 = 'facedir',
   light_source = 10,
   tiles = {'ship_laser_gate.png'},
   groups = {breakable=1, not_in_creative_inventory=1},
   drop = 'ship:laser_gate',
   selection_box = laser_gate,
   collision_box = laser_gate,
})

minetest.register_node('ship:laser_gate_m', {
   drawtype = 'mesh',
   mesh = 'ship_laser_gate_M.obj',
   paramtype = 'light',
   paramtype2 = 'facedir',
   light_source = 10,
   tiles = {'ship_laser_gate.png'},
   groups = {breakable=1, not_in_creative_inventory=1},
   drop = 'ship:laser_gate',
   selection_box = laser_gate,
   collision_box = laser_gate,
})
