Zefram
2014-08-13 b001a679799bdc7d08eabcd1300271d0e1357b0a
technic/machines/register/common.lua
@@ -1,4 +1,6 @@
local S = technic.getter
function technic.handle_machine_upgrades(meta)
   -- Get the names of the upgrades
   local inv = meta:get_inventory()
@@ -35,23 +37,27 @@
end
function technic.send_items(pos, x_velocity, z_velocity)
function technic.send_items(pos, x_velocity, z_velocity, output_name)
   -- Send items on their way in the pipe system.
   if output_name == nil then
      output_name = "dst"
   end
   local meta = minetest.get_meta(pos) 
   local inv = meta:get_inventory()
   local i = 0
   for _, stack in ipairs(inv:get_list("dst")) do
   for _, stack in ipairs(inv:get_list(output_name)) do
      i = i + 1
      if stack then
         local item0 = stack:to_table()
         if item0 then 
            item0["count"] = "1"
            local item1 = tube_item({x=pos.x, y=pos.y, z=pos.z}, item0)
            local item1 = pipeworks.tube_item({x=pos.x, y=pos.y, z=pos.z}, item0)
            item1:get_luaentity().start_pos = {x=pos.x, y=pos.y, z=pos.z}
            item1:setvelocity({x=x_velocity, y=0, z=z_velocity})
            item1:setacceleration({x=0, y=0, z=0})
            stack:take_item(1)
            inv:set_stack("dst", i, stack)
            inv:set_stack(output_name, i, stack)
            return
         end
      end
@@ -65,21 +71,25 @@
   if meta:get_int("cook_time") < result.time / speed then
      return
   end
   local result = minetest.get_craft_result({method = "cooking", width = 1, items = inv:get_list("src")})
   local result
   local afterfuel
   result, afterfuel = minetest.get_craft_result({method = "cooking", width = 1, items = inv:get_list("src")})
   if result and result.item then
      meta:set_int("cook_time", 0)
      -- check if there's room for output in "dst" list
      if inv:room_for_item("dst", result) then
         srcstack = inv:get_stack("src", 1)
         srcstack:take_item()
         inv:set_stack("src", 1, srcstack)
      if inv:room_for_item("dst", result.item) then
         inv:set_stack("src", 1, afterfuel.items[1])
         inv:add_item("dst", result.item)
      end
   end
end
function technic.handle_machine_pipeworks(pos, tube_upgrade)
function technic.handle_machine_pipeworks(pos, tube_upgrade, send_function)
   if send_function == nil then
      send_function = technic.send_items
   end
   local node = minetest.get_node(pos)
   local meta = minetest.get_meta(pos)
   local inv = meta:get_inventory()
@@ -100,12 +110,48 @@
   end
   tube_time = meta:get_int("tube_time")
   tube_time = tube_time + tube_upgrade
   if tube_time > 3 then
   if tube_time >= 2 then
      tube_time = 0
      if output_tube_connected then
         technic.send_items(pos, x_velocity, z_velocity)
         send_function(pos, x_velocity, z_velocity)
      end
   end
   meta:set_int("tube_time", tube_time)
end
function technic.machine_can_dig(pos, player)
   local meta = minetest.get_meta(pos)
   local inv = meta:get_inventory()
   if not inv:is_empty("src") or not inv:is_empty("dst") or
      not inv:is_empty("upgrade1") or not inv:is_empty("upgrade2") then
      minetest.chat_send_player(player:get_player_name(),
         S("Machine cannot be removed because it is not empty"))
      return false
   else
      return true
   end
end
local function inv_change(pos, player, count)
   if minetest.is_protected(pos, player:get_player_name()) then
      minetest.chat_send_player(player:get_player_name(),
         S("Inventory move disallowed due to protection"))
      return 0
   end
   return count
end
function technic.machine_inventory_put(pos, listname, index, stack, player)
   return inv_change(pos, player, stack:get_count())
end
function technic.machine_inventory_take(pos, listname, index, stack, player)
   return inv_change(pos, player, stack:get_count())
end
function technic.machine_inventory_move(pos, from_list, from_index,
      to_list, to_index, count, player)
   return inv_change(pos, player, count)
end