Advertisement
markuszeller

Refuel Turtle from Chest

May 19th, 2014 (edited)
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.14 KB | None | 0 0
  1. function refuelFromChest()
  2.   -- version 1.02
  3.   -- using pull items to test direction
  4.  
  5.   -- version 1.01
  6.   -- fixed empty chest causing stack size -1
  7.  
  8.   -- turtle must be be looking at chest
  9.   local directions = {"north", "south", "east", "west"}
  10.   local chestDirection = ""
  11.  
  12.   -- these item IDs can be taken as fuel
  13.   local fuel = {327, 4600, 7796, 263}
  14.  
  15.   -- connect to the chest
  16.   local chest = peripheral.wrap("front")
  17.   if not chest then
  18.     print("Failed to connect to chest at front side.")
  19.     return false
  20.   end
  21.  
  22.   -- get all stacks from chest
  23.   local chestStacks = chest.getAllStacks()
  24.   if not chestStacks then
  25.     print("Could not scan chest.")
  26.     return false
  27.   end
  28.  
  29.   -- check if there is a free slot. If not try to condense
  30.   if #chestStacks == chest.getInventorySize() then
  31.      chest.condenseItems();
  32.     -- space available now?
  33.     if #chestStacks == chest.getInventorySize() then
  34.        print("Chest is full. Please clean at least one slot. Preferebaly Slot 1.")
  35.        return false
  36.     end
  37.   end
  38.  
  39.   -- check if slot 1 is free. If not, try to swap with the next available
  40.   local swappedStack = 0
  41.   if chestStacks[1] then
  42.     for i = 2, chest.getInventorySize() do
  43.       if not chestStacks[i] then
  44.         chest.swapStacks(1, i)
  45.         chestStacks = chest.getAllStacks()
  46.         swappedStack = i
  47.         break
  48.       end
  49.     end
  50.   end
  51.  
  52.   -- take slot 1 from turtle into chest
  53.   turtle.select(1)
  54.  
  55.   -- probe test direction
  56.   for i = 1, #directions do
  57.     chestDirection = directions[i]
  58.     if chest.pullItem(chestDirection, 1) > 0 then
  59.       break
  60.     end
  61.   end
  62.  
  63.   -- take fuel from chest
  64.   local refueled = false
  65.   for k,v in pairs(chestStacks) do
  66.     local item = chestStacks[k].id
  67.     for i = 1, #fuel do
  68.       if item == fuel[i] then
  69.         chest.pushItem(chestDirection, k)
  70.         if turtle.refuel() then
  71.           refuel = true
  72.         end
  73.         turtle.select(1)
  74.         chest.pullItem(chestDirection, 1)
  75.       end
  76.     end
  77.   end
  78.  
  79.   -- give turtle back its first slot stack
  80.   turtle.select(1)
  81.   chest.pushItem(chestDirection, 1)
  82.  
  83.   -- when swapped then but item back to slot 1
  84.   if swappedStack > 1 then
  85.     chest.swapStacks(swappedStack, 1)
  86.   end
  87.  
  88.   return refueled
  89. end
  90.  
  91. refuelFromChest()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement