Advertisement
akihex

starMaker

Aug 23rd, 2014
279
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.47 KB | None | 0 0
  1. local args = {...}
  2.  
  3. local dumpDir = "up"
  4. local chestDir = "east"
  5. local mainLoopDelay = 10
  6.  
  7. local mon = peripheral.wrap("top")
  8. local narc = peripheral.find("turtle")
  9. local lp = peripheral.find("LogisticsPipes:Normal")
  10. local chest = peripheral.find("deep_storage_unit")
  11. local dump = peripheral.find("diamond")
  12.  
  13. if not narc or not lp or not chest or not dump then mon.setTextColor(colors.red) mon.clear() mon.setCursorPos(1,1) mon.write("Peripheral Error:") mon.setCursorPos(1,2) mon.write("narc: "..(narc and 1 or 0).." lp: "..(lp and 1 or 0).." chest: "..(chest and 1 or 0).." dump: "..(dump and 1 or 0)) return 0 end
  14.  
  15. mon.setTextColor(colors.white)
  16. mon.setBackgroundColor(colors.black)
  17. mon.clear()
  18. mon.setCursorPos(1,1)
  19. --mon.write("beep")
  20.  
  21. local ALKAHEST = 27480
  22. local DIAMOND = 264
  23. local WITHER = 397
  24. local POTION = 27484
  25. local STAR = 399
  26.  
  27. local ingredients = {[ALKAHEST]=1,[DIAMOND]=1,[STAR]=1,[WITHER]=3,[POTION]=3}
  28.  
  29. local function moveItemToInventory(inventory,dir,slot,number)
  30.   while number > 0 and turtle.getItemCount(slot) > 0 do
  31.     number = number - inventory.pullItem(dumpDir,slot,number)
  32.     if number > 0 then sleep(0.5) end
  33.   end
  34. end
  35.  
  36. local function dumpItem(slot,number)
  37.   moveItemToInventory(dump,dumpDir,slot,number)
  38. end
  39.  
  40. local next = next
  41.  
  42. local function checkInventory()
  43. --  narc.condenseItems()
  44.   local items = narc.getAllStacks()
  45.   -- shallow copy
  46.   local currentRecipe = {}
  47.   for i,k in pairs(ingredients) do currentRecipe[i] = k end
  48.   --for i,k in pairs(currentRecipe) do print(i," ",k) end
  49.   for n,item in pairs(items) do
  50.     if currentRecipe[item.id] then
  51.       currentRecipe[item.id] = currentRecipe[item.id]-item.qty ~= 0 and currentRecipe[item.id]-item.qty or nil
  52.       if currentRecipe[item.id] and currentRecipe[item.id] < 0 then dumpItem(n,-currentRecipe[item.id]) currentRecipe[item.id] = nil end
  53.     else
  54.       dumpItem(n,64)
  55.     end
  56.   end
  57.   --for i,k in pairs(currentRecipe) do print(i," ",k) end
  58.   return next(currentRecipe) == nil and true or false
  59. end
  60.  
  61. local recipe = {ALKAHEST,DIAMOND,STAR,nil,WITHER,WITHER,WITHER,nil,POTION,POTION,POTION,nil,nil,nil,nil,nil}
  62. local reverseRecipe = {}
  63. for i,k in pairs(recipe) do reverseRecipe[k] = i end
  64.  
  65. local function getEmptySlotsTurtle()
  66.   local slots = {}
  67.   for i=1,16 do
  68.     if turtle.getItemCount(i) == 0 then slots[i] = true end
  69.   end
  70.   return slots
  71. end
  72.  
  73. local function getEmptySlots(itemList)
  74.   local slots = {}
  75.   --{4,8,12,13,14,15,16,11,10,9,7,6,5,3,2,1}
  76.   for _,i in ipairs({1,2,3,5,6,7,9,10,11,16,15,14,13,12,8,4}) do
  77.     if not itemList[i] then slots[#slots+1] = i end
  78.   end
  79.   return slots
  80. end
  81.  
  82. local function move(slot,qty,emptySlots)
  83.   turtle.select(slot)
  84.   while not turtle.transferTo(emptySlots[#emptySlots],qty) do sleep(0.5) end
  85.   emptySlots[#emptySlots] = nil
  86. end
  87.  
  88. local condenseRecipe = {ALKAHEST,DIAMOND,STAR,nil,WITHER,nil,nil,nil,POTION}
  89. local reverseCondenseRecipe = {}
  90. for i,k in pairs(condenseRecipe) do reverseCondenseRecipe[k] = i end
  91.  
  92. local distribution = {}
  93. local reverseDistribution = {}
  94.  
  95. local function swapItems(src,dst)
  96. --,distr,reverseDistr)
  97.   narc.swapStacks(src,dst)
  98.   local tmpId = reverseDistribution[dst]
  99.   local tmpSlot = distribution[dst]
  100.   distribution[dst] = distribution[src]
  101.   distribution[src] = tmpId
  102.   reverseDistribution[dst] = reverseDistribution[src]
  103.   reverseDistribution[src] = tmpSlot
  104. end
  105.  
  106. local function prepareCraft()
  107.   narc.condenseItems()
  108.   local items = narc.getAllStacks()
  109.   --local emptySlots = getEmptySlots(items)
  110.   for n,item in pairs(items) do
  111.     distribution[item.id] = n
  112.     reverseDistribution[n] = item.id
  113.   end
  114.   for _,slot in ipairs({1,2,3,5,6,7,9,10,11}) do
  115.     if reverseDistribution[slot] ~= condenseRecipe[slot] then
  116.       --print(slot," ",condenseRecipe[slot]," ",distribution[condenseRecipe[slot]])
  117.       swapItems(slot,distribution[condenseRecipe[slot]],distribution,reverseDistribution)
  118.     end
  119.   end
  120.   turtle.select(5)
  121.   turtle.transferTo(6,1) turtle.transferTo(7,1)
  122.   turtle.select(9)
  123.   turtle.transferTo(10,1) turtle.transferTo(11,1)
  124.   turtle.select(3)
  125. end
  126.  
  127. --for i,k in pairs(recipe) do print(i," ",k) end
  128.  
  129. local slot3
  130.  
  131. while true do
  132.   if not checkInventory() then os.pullEvent("turtle_inventory") end
  133.   while not checkInventory() do
  134.     mon.write("waiting for items")
  135.     sleep(0.5)
  136.   end
  137.   prepareCraft()
  138.   turtle.craft()
  139.   slot3=narc.getStackInSlot(3)
  140.   if slot3.id == STAR and slot3.qty >= 2 then chest.pullItem(chestDir,3,slot3.qty-1) end
  141.   --sleep(mainLoopDelay)
  142. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement