Advertisement
VADemon

crafting Turtle

Oct 21st, 2014
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.23 KB | None | 0 0
  1. -- how many items to craft at once?
  2. targetItemCount = 16
  3. -- sleep time between inv checks
  4. sleepTimeInventory = 2
  5. -- which item from /craftingGrid/ to craft?
  6. targetItemType = "compressedSawdust"
  7.  
  8. craftingGrid = {
  9.     compressedSawdust = {
  10.         true,    true,  true,   false,
  11.         true,    false, true,   false,
  12.         true,    true,  true,   false,
  13.         false,   false, false,  false
  14.     },
  15.    
  16.     compressedCobblestone = {
  17.         true,    true,  true,   false,
  18.         true,    true, true,    false,
  19.         true,    true,  true,   false,
  20.         false,   false, false,  false
  21.     }
  22. }
  23.  
  24. craftingGrid_meta = {
  25.     -- itemType = {
  26.     --      resultSlot = number,
  27.     -- }
  28. }
  29.  
  30. function initCraftingGrid()
  31.     -- Main function
  32.     for gridKey, gridValue in pairs(craftingGrid) do
  33.        
  34.         if craftingGrid_meta[ gridKey ] == nil then
  35.             craftingGrid_meta[ gridKey ] = {}
  36.         end
  37.        
  38.         local requiredCraftingSlots = 0
  39.         -- that's the first slot where the items would go to
  40.         local craftResultSlot
  41.        
  42.         for i = 1, #gridValue do
  43.             local slotValue = gridValue[i]
  44.            
  45.             if slotValue then
  46.                 requiredCraftingSlots = requiredCraftingSlots + 1
  47.             else
  48.                 if not craftResultSlot then
  49.                     craftResultSlot = i
  50.                 end
  51.                
  52.                 gridValue[ i ] = nil    -- erase value from table
  53.             end
  54.         end
  55.        
  56.         if requiredCraftingSlots <= 9 then
  57.             craftingGrid_meta[ gridKey ].requiredCraftingSlots = requiredCraftingSlots
  58.         else
  59.             print("Error: craftingGrid for [" .. gridKey .. "] requires more than 9 crafting slots!")
  60.             print("Double-check this")
  61.             error()
  62.         end
  63.        
  64.        
  65.         craftingGrid_meta[ gridKey ].resultSlot = craftResultSlot
  66.        
  67.         craftingGrid[ gridKey ] = gridValue -- apply changes to the global table from above
  68.     end
  69. end
  70.  
  71.  
  72. function craftingGridSort( targetItem )
  73.     local readyForCrafting = false
  74.    
  75.     for i, v in pairs( craftingGrid[ targetItem ] ) do
  76.        
  77.         -- item in slot /i/?
  78.         if v then
  79.        
  80.             local currentSlotItems = turtle.getItemCount(i)
  81.            
  82.             -- return next slot or, if at the end, the first slot:
  83.             local nextSlot = ( next(craftingGrid[ targetItem ], i) or next(craftingGrid[ targetItem ]) )
  84.             print( type(nextSlot), "    ", nextSlot)
  85.            
  86.             if currentSlotItems > targetItemCount then
  87.                 turtle.select(i)
  88.                 print("Transfer to: ", nextSlot, "   ", (currentSlotItems - targetItemCount))
  89.                 turtle.transferTo(nextSlot, (currentSlotItems - targetItemCount))
  90.             elseif nextSlot ~= 1 then
  91.                 return false
  92.             end
  93.            
  94.             if nextSlot == 1 then
  95.                 readyForCrafting = true
  96.             end
  97.         end
  98.     end
  99.    
  100.     return readyForCrafting
  101. end
  102.  
  103. function inputCraftingItems()
  104.     -- fetch from craftingGrid table
  105.     return turtle.suckUp()
  106. end
  107.  
  108. function outputCraftedItems( targetItem )
  109.     -- same as above
  110.     turtle.select( craftingGrid_meta[ targetItem ].resultSlot )
  111.    
  112.     return turtle.dropDown()
  113. end
  114.  
  115. -- runtime
  116. initCraftingGrid()
  117.  
  118. while true do
  119.     print("Iteration")
  120.     turtle.select(1)
  121.    
  122.     while inputCraftingItems( targetItemType ) == false do
  123.         -- no new input items
  124.         sleep( sleepTimeInventory )
  125.     end
  126.    
  127.     -- ready to craft?
  128.     if craftingGridSort( targetItemType ) then
  129.         turtle.select( craftingGrid_meta[ targetItemType ].resultSlot )
  130.         turtle.craft( targetItemCount )
  131.        
  132.         while outputCraftedItems( targetItemType ) == false do
  133.             print("Can't output crafted Items!")
  134.             sleep(5)
  135.         end
  136.     end
  137.    
  138.     sleep( sleepTimeInventory )
  139. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement