Wiiplay123

Ultimate Program

May 3rd, 2022 (edited)
500
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 41.22 KB | None | 0 0
  1. --[[
  2.  
  3. INSTRUCTIONS:
  4.  
  5. Turtle must have pickaxe on right side, and crafting table on left side.
  6. Place single chests in front of and on top of turtle.
  7. Place a single hopper to the left of front chest.
  8. Place a furnace under the turtle with empty space on the left for the turtle to go under the furnace.
  9. Place a hopper behind furnace.
  10. Place a single redstone dust in front of the furnace.
  11. Place a single pressure plate in front of the redstone dust.
  12. Add to instructions as more requirements are added.
  13.  
  14.  
  15. VALUE DOCUMENTATION:
  16.  
  17. minFuel (number): Minimum fuel allowed in furnace before refuelling
  18. chests (table): External Chest Cache
  19. turtleInventory (table): Internal Inventory Cache
  20. inventorySize (number): Internal Inventory Size
  21. inv (number): Main Buffer Chest Size
  22. slots (table): List of crafting slots
  23. craftingRecipes (table): List of crafting recipes. Key is item name to be crafted, value is a table of either {itemCount,(1-9 itemName of items to place in crafting grid)} or {itemCount, {Item Names}, {Keys to previous item name table}}.
  24. furnaceRecipes (table): List of furnace recipes. Key is resulting item name, value is item name to be smelted/cooked.
  25. miners (number): Amount of active turtle miners
  26. maxMiners (number): Maximum amount of active turtle miners
  27. cautious (boolean): Whether or not to use clearCrafting() function.
  28. ironChests (boolean): Whether or not to craft chests from the Iron Chests mod.
  29.  
  30. FUNCTION DOCUMENTATION:
  31.  
  32. notCrafting(slot: number) (Returns boolean): Returns whether or not (slot) is a crafting slot.
  33. turnAround() (Returns nil): Does turtle.turnLeft() twice.
  34. updateInternalInventory([slot: number]) (Returns nil): Updates internal inventory cache table (turtleInventory) slot (slot), or if (slot) is absent, updates entire table.
  35. checkUp(chestIndex: number, [itemName: string, itemCount: number]) (Returns nil if itemName and itemCount exist, returns table containing chest contents otherwise) return: Updates chests[chestIndex] with the contents of the chest currently above the turtle. If itemName and itemCount are BOTH present, will only update for places that the items could possibly be placed in since the last cache update. (Used IMMEDIATELY after dropping anything into a chest)
  36. itemCount(itemName: string) (Returns number): Returns combined amount of (itemName) in both (turtleInventory) and (chests).
  37. checkIfEmptyInventory() (Returns boolean): Checks if turtle's internal inventory is empty. (not counting crafting slots)
  38. emptyBufferChest() (Returns boolean): Checks if buffer chest is empty.
  39. clearInventory([dontClearOne: boolean]) (Returns nil): Puts entire internal inventory (Unless dontClearOne is true, in which case everything except slot 1) into chests, and changes (turtleInventory) and (chests) contents to match.
  40. nearestBuffer([itemName: string]) (Returns number): Returns lowest internal inventory slot that can hold (itemName), or the lowest empty slot if itemName is not set.
  41. stockInventory([dontClearOne: boolean]) (Returns nil): Fills inventory with contents of buffer chest. If inventory is full, calls clearInventory([dontClearOne: boolean]) to clear inventory. Repeats until buffer chest is empty.
  42. pullItemFromStorage(itemName: string, itemCount: number) (Returns number): Pulls item from external chest inventory if item is not already in internal inventory. Returns and selects slot number item is in once item is in internal inventory.
  43. checkTotalInventoryCapacity() (Returns number): Returns amount of empty slots left in external chest inventory.
  44. refreshState() (Returns nil): Refreshes internal inventory, external inventory, and miner count values.
  45. canCraft(itemName: string, itemCount: number) (Returns boolean): Returns whether or not (itemCount) of (itemName) can be crafted using the contents of the internal and external inventories.
  46. minStack(coalAvailable: number, coalInFuelSlot: number) (Returns number): Returns the lowest of (coalAvailable) or (64 - coalInFuelSlot) (The amount of coal that can be placed into the furnace's fuel slot).
  47. mergeStack(itemName: string) (Returns nil): Merges all stacks of (itemName) in internal inventory to the lowest amount of stacks possible.
  48. notInList(list: table, string: string) (Returns boolean): Returns true if (string) is not in (list), false otherwise.
  49. mergeStacks() (Returns nil): Merges all stacks in internal inventory to the lowest amount of stacks possible using mergeStack(itemName: string).
  50. checkFurnaceFuel() (Returns nil): Refuels furnace below turtle if fuel is below (minFuel).
  51. clearCrafting() (Returns nil): Clears crafting slots.
  52. craft(itemName: string, itemCount: number) (Returns boolean): Puts (itemCount) of (itemName) into lowest available slot. If items are already available in internal inventory, will select slot. If not, will use pullItemFromStorage(itemName, itemCount) to pull more items in. If there are not enough items in external inventory, will craft the required items.
  53. pushItemToStorage(slot: number) (Returns nil): Puts items in (slot) into external inventory.
  54. updateAllChests()
  55. digAndUpdate(direction: string) (Returns nil): Direction can be nil, "up", or "down". Digs a block if it exists, and puts it in the inventory cache. Selects previously selected slot afterwards.
  56. makePickaxe(i: number) (Returns nil): Helper function for addNewChest(), crafts pickaxe if one isn't already available and then moves forward.
  57. addNewMiner() (Returns nil): Adds new miner and increments (miners) if successful
  58. addNewChest() (Returns nil): Adds new external chest to external chest inventory and updates (chests) to match.
  59. getItemName() (Returns (string, number) or (string) or (nil)): Returns "exit"
  60. canCraftInstance(itemRecipe: table, itemCount: number) (Returns boolean): Returns whether or not (itemCount) of item with (itemRecipe) recipe can be made. Helper function to canCraft() and craft().
  61. ]]
  62. if peripheral == nil then
  63.     error("Program is only designed for ComputerCraft.",0)
  64. end
  65. local cautious = true
  66. local makeMiners = false
  67. local maxMiners = 10
  68. local ironChests = true
  69. local checkingFuel = false
  70. local minFuel = 5
  71. local minTurtleFuel = 1600
  72. local inventorySize = 16
  73. --local turtleInventory = {}
  74. turtleInventory = {}
  75. --local chests = {}
  76. chests = {}
  77. local inv = peripheral.call("front","getInventorySize")
  78. if inv == nil then
  79.     repeat
  80.         turtle.turnLeft()
  81.         inv = peripheral.call("front","getInventorySize")
  82.     until inv
  83. end
  84. local slots = {1,2,3,5,6,7,9,10,11}
  85. local craftingRecipes = {
  86. ["minecraft:planks"] = {4,"minecraft:log"},
  87. ["minecraft:wooden_door"] = {1,{"minecraft:planks"},{1,1,0,1,1,0,1,1,0}},
  88. ["Natura:planks"] = {4,"Natura:tree"},
  89. ["minecraft:paper"] = {3,{"minecraft:reeds"},{1,1,1}},
  90. ["ComputerCraft:disk"] = {1,{"minecraft:redstone","minecraft:paper"},{1,0,0,2}},
  91. ["minecraft:stick"] = {4,"minecraft:planks","","","minecraft:planks"},
  92. ["IronChest:woodIronUpgrade"] = {{1,{"minecraft:iron_ingot","minecraft:planks"},{1,1,1,1,2,1,1,1,1}},{1,{"minecraft:iron_ingot","Natura:planks"},{1,1,1,1,2,1,1,1,1}}},
  93. ["minecraft:bread"] = {1,{"minecraft:wheat"},{0,0,0,1,1,1}},
  94. ["IronChest:ironGoldUpgrade"] = {1,{"minecraft:gold_ingot","minecraft:iron_ingot"},{1,1,1,1,2,1,1,1,1}},
  95. ["IronChest:goldDiamondUpgrade"] = {1,{"minecraft:glass","minecraft:diamond","minecraft:gold_ingot"},{1,1,1,2,3,2,1,1,1}},
  96. ["minecraft:glass_pane"] = {16,{"minecraft:glass"},{1,1,1,1,1,1}},
  97. ["ComputerCraft:CC-Computer"] = {1,{"minecraft:stone","minecraft:redstone","minecraft:glass_pane"},{1,1,1,1,2,1,1,3,1}},
  98. ["ComputerCraft:CC-Turtle"] = {1,{"minecraft:iron_ingot","ComputerCraft:CC-Computer","minecraft:chest"},{1,1,1,1,2,1,1,3,1}},
  99. ["ComputerCraft:CC-Peripheral"] = {1,{"minecraft:stone","minecraft:redstone"},{1,1,1,1,2,1,1,2,1}},
  100. ["computercraft:advanced_modem"] = {1,{"minecraft:gold_ingot","minecraft:ender_eye"}},
  101. ["minecraft:chest"] = {{1,{"Natura:planks"},{1,1,1,1,0,1,1,1,1}},{1,{"minecraft:planks"},{1,1,1,1,0,1,1,1,1}}},
  102. ["minecraft:furnace"] = {1,{"minecraft:cobblestone"},{1,1,1,1,0,1,1,1,1}},
  103. ["minecraft:enchanting_table"] = {1,{"minecraft:book","minecraft:diamond","minecraft:obsidian"},{0,1,0,2,3,2,3,3,3}},
  104. ["tile.chickenchunkloader|0"] = {1,{"minecraft:ender_pearl","minecraft:gold_ingot","minecraft:enchanting_table"},{0,1,0,2,2,2,2,3,2}},
  105. ["tile.chickenchunkloader|1"] = {10,{"minecraft:ender_pearl","tile.chickenchunkloader|0"},{1,1,1,1,2,1,1,1,1}},
  106. ["tile.computercraft:wireless_modem"] = {1,{"minecraft:stone","minecraft:ender_pearl"},{1,1,1,1,2,1,1,1,1}},
  107. ["PeripheralsPlusPlus:chunkLoaderUpgrade"] = {1,"tile.chickenchunkloader|1","tile.computercraft:wireless_modem"},
  108. ["minecraft:diamond_pickaxe"] = {1,{"minecraft:diamond","minecraft:stick"},{1,1,1,0,2,0,0,2}},
  109. ["minecraft:iron_pickaxe"] = {1,{"minecraft:iron_ingot","minecraft:stick"},{1,1,1,0,2,0,0,2}},
  110. --["ComputerCraft:CC-TurtleExpanded"] = {1,"minecraft:diamond_pickaxe","ComputerCraft:CC-Turtle","PeripheralsPlusPlus:chunkLoaderUpgrade"},
  111. ["ComputerCraft:CC-TurtleExpanded"] = {1,"minecraft:diamond_pickaxe","ComputerCraft:CC-Turtle"},
  112. --["usefulDNS:PStone"] = {1,{"minecraft:redstone","minecraft:glowstone_dust","usefulDNS:DecaNySodiumGem"},{1,2,1,2,3,2,1,2,1}},
  113. --["ProjectE:item.pe_philosophers_stone"] = {1,{"minecraft:redstone","minecraft:glowstone_dust","minecraft:diamond"},{1,2,1,2,3,2,1,2,1}},
  114. --["minecraft:ender_pearl"] = {1,{"usefulDNS:PStone","minecraft:iron_ingot"},{1,2,0,2,2,0,2}},
  115. --["minecraft:diamond"] = {1,{"usefulDNS:PStone","minecraft:gold_ingot"},{1,2,0,2,2,0,2}},
  116. --["minecraft:sand"] = {1,"usefulDNS:PStone","minecraft:cobblestone","","","minecraft:cobblestone"},
  117. ["minecraft:stone_slab"] = {6,{"minecraft:stone"},{1,1,1}},
  118. ["minecraft:hopper"] = {1,{"minecraft:iron_ingot","minecraft:chest"},{1,0,1,1,2,1,0,1,0}},
  119. ["ComputerCraft:pocketComputer"] = {1,{"minecraft:gold_ingot","minecraft:golden_apple","minecraft:glass_pane"},{1,1,1,1,2,1,1,3,1}},
  120. ["minecraft:golden_apple"] = {1,{"minecraft:gold_ingot","minecraft:apple"},{1,1,1,1,2,1,1,1,1}},
  121. ["OpenPeripheral:pim"] = {1,{"minecraft:obsidian","minecraft:chest","minecraft:redstone"},{1,1,1,2,3,2}}
  122. }
  123. local furnaceRecipes = {
  124.     ["minecraft:stone"] = "minecraft:cobblestone",
  125.     ["minecraft:iron_ingot"] = "minecraft:iron_ore",
  126.     ["minecraft:gold_ingot"] = "minecraft:gold_ore",
  127.     ["minecraft:glass"] = "minecraft:sand",
  128.     ["minecraft:cooked_chicken"] = "minecraft:chicken",
  129.     ["minecraft:cooked_porkchop"] = "minecraft:porkchop",
  130.     ["minecraft:cooked_beef"] = "minecraft:beef",
  131.     ["minecraft:cooked_fish"] = "minecraft:fish"
  132. }
  133.  
  134. function notCrafting(s)
  135.     return s > 11 or s == 4 or s == 8
  136. end
  137.  
  138. function turnAround()
  139.     turtle.turnLeft()
  140.     turtle.turnLeft()
  141. end
  142.  
  143. function checkFuel()
  144.     if not checkingFuel then
  145.         checkingFuel = true
  146.         local fuel = turtle.getFuelLevel()
  147.         local count = itemCount("minecraft:coal")
  148.         if fuel ~= "unlimited" and fuel < minTurtleFuel and count > 0 then
  149.             local slot = pullItemFromStorage("minecraft:coal",(count >= 64 and 64 or itemCount("minecraft:coal")))
  150.             if slot > 0 then
  151.                 if turtle.refuel(64) then
  152.                     turtleInventory[slot] = nil
  153.                 else
  154.                     updateInternalInventory()
  155.                 end
  156.             end
  157.         end
  158.         checkingFuel = false
  159.     end
  160. end
  161.  
  162. function updateInternalInventory(n)
  163.     if n then
  164.         turtleInventory[n] = turtle.getItemDetail(n)
  165.         if turtleInventory[n] then
  166.             turtleInventory[n].maxSize = turtle.getItemSpace(n) + turtleInventory[n].count
  167.         end
  168.     else
  169.         turtleInventory = {}
  170.         for i = 4, inventorySize do
  171.             if notCrafting(i) then
  172.                 turtleInventory[i] = turtle.getItemDetail(i)
  173.                 if turtleInventory[i] then
  174.                     turtleInventory[i].maxSize = turtle.getItemSpace(i) + turtleInventory[i].count
  175.                 end
  176.             end
  177.         end
  178.     end
  179. end
  180.  
  181. function checkUp(ix,id,count)
  182.     if chests and ix and ix <= #chests then
  183.         local oldSize = 0
  184.         if ix and id and chests[ix] and chests[ix]["stacks"] and count then
  185.             oldSize = chests[ix]["size"]
  186.             chests[ix].size = peripheral.call("top","getInventorySize")
  187.         end
  188.         if (oldSize == chests[ix]["size"]) and id and count then
  189.             local cacheCount = 0
  190.             for i = 1, chests[ix]["size"] do
  191.                 if not chests[ix]["stacks"][i] or chests[ix]["stacks"][i].id == id then
  192.                     local oldSlot = (chests[ix]["stacks"][i] or {id = "", qty = 0})
  193.                     chests[ix]["stacks"][i] = peripheral.call("top","getStackInSlot",i)
  194.                     if chests[ix]["stacks"][i] and (oldSlot.id == chests[ix]["stacks"][i].id or oldSlot.qty == 0) then
  195.                         cacheCount = cacheCount + chests[ix]["stacks"][i].qty - oldSlot.qty
  196.                         if cacheCount >= count then
  197.                             break
  198.                         end
  199.                     end
  200.                 end
  201.             end
  202.         else
  203.             chests[ix] = checkUp()
  204.         end
  205.     else
  206.         local stacks = {}
  207.         local size = peripheral.call("top","getInventorySize")
  208.         if size then
  209.             for i = 1, size do
  210.                 stacks[i] = peripheral.call("top","getStackInSlot",i)
  211.             end
  212.             return {["stacks"] = stacks,["size"] = size}
  213.         else
  214.             return false
  215.         end
  216.     end
  217. end
  218.  
  219. function itemCount(id)
  220.     local count = 0
  221.     for i = 4, inventorySize do
  222.         if notCrafting(i) then
  223.             local slot = turtleInventory[i]
  224.             if slot and slot.name == id then
  225.                 count = count + slot.count
  226.             end
  227.         end
  228.     end
  229.     for i = 1, #chests do
  230.         for o, v in pairs(chests[i]["stacks"]) do
  231.             if v.id == id then
  232.                 count = count + v.qty
  233.             end
  234.         end
  235.     end
  236.     return count
  237. end
  238.  
  239. function checkIfEmptyInventory()
  240.     for i = 1, inventorySize do
  241.         if turtle.getItemCount(i) > 0 then
  242.             return false
  243.         end
  244.     end
  245.     return true
  246. end
  247.  
  248. function emptyBufferChest()
  249.     for i = 1, inv do
  250.         if peripheral.call("front","getStackInSlot",i) then
  251.             return false
  252.         end
  253.     end
  254.     return true
  255. end
  256.  
  257. function clearInventory(dontClearOne)
  258.     printTitle("Ultimate Program Running","Clearing Inventory")
  259.     checkFuel()
  260.     local chestz = 0
  261.     local lastSlot = (dontClearOne and 2 or 1)
  262.     if #chests == 1 then
  263.         for i = (dontClearOne and 2 or 1), inventorySize do
  264.             local inspect = turtle.getItemDetail(i)
  265.             if inspect then
  266.                 turtle.select(i)
  267.                 if not turtle.dropUp() then
  268.                     break
  269.                 end
  270.                 checkUp(1,inspect.name,inspect.count)
  271.             end
  272.         end
  273.     else
  274.         for i = 1, #chests do
  275.             for p = lastSlot, inventorySize do
  276.                 local detail = turtle.getItemDetail(p)
  277.                 if detail then
  278.                     turtle.select(p)
  279.                     if not turtle.dropUp() then
  280.                         updateInternalInventory()
  281.                         lastSlot = p
  282.                         chests[i] = checkUp()
  283.                         break
  284.                     end
  285.                     checkUp(i,detail.name,detail.count)
  286.                     updateInternalInventory(p)
  287.                 end
  288.             end
  289.             if i < #chests and not ((lastSlot == inventorySize) or checkIfEmptyInventory()) then
  290.                 if i == 1 then
  291.                     turtle.turnRight()
  292.                 end
  293.                 forward()
  294.                 forward()
  295.                 chestz = chestz + 1
  296.             end
  297.         end
  298.         for i = 1, chestz do
  299.             chests[chestz + 2 - i] = checkUp()
  300.             repeat until back()
  301.             repeat until back()
  302.         end
  303.         if chestz > 0 then
  304.             turtle.turnLeft()
  305.         end
  306.     end
  307.     updateInternalInventory()
  308. end
  309.  
  310. function nearestBuffer(id)
  311.     for i = 4, inventorySize do
  312.         if notCrafting(i) and (not turtleInventory[i] or (turtleInventory[i].name == id and turtleInventory[i].count < turtleInventory[i].maxSize)) then
  313.             return i
  314.         end
  315.     end
  316.     return 0
  317. end
  318.  
  319. function stockInventory(dontClearOne)
  320.     updateInternalInventory()
  321.     local slot = turtle.getSelectedSlot()
  322.     local selecting = false
  323.     repeat
  324.         --printTitle("Ultimate Program Running","Checking front chest")
  325.         local breaking = false
  326.         local s = false
  327.         repeat
  328.             if not emptyBufferChest() then
  329.                 turtle.select(1)
  330.                 selecting = true
  331.             else
  332.                 break
  333.             end
  334.             if not turtle.suck() then
  335.                 break
  336.             else
  337.                 s = true
  338.             end
  339.         until emptyBufferChest()
  340.         updateInternalInventory()
  341.         if s then clearCrafting() end
  342.         if slotsLeft() < 4 or nearestBuffer() == 0 then
  343.             clearInventory(dontClearOne)
  344.             updateInternalInventory()
  345.         end
  346.     until emptyBufferChest()
  347.     clearInventory(dontClearOne)
  348.     updateInternalInventory()
  349.     if selecting then
  350.         turtle.select(slot)
  351.     end
  352. end
  353.  
  354. function pullItemFromStorage(id,count)
  355.     checkFuel()
  356.     local allTotal = 0
  357.     local intoSlot = 0
  358.     for i = 1, inventorySize do
  359.         if notCrafting(i) then
  360.             local tempSlot = turtleInventory[i]
  361.             if tempSlot and tempSlot.name == id then
  362.                 if intoSlot == 0 then
  363.                     allTotal = allTotal + turtleInventory[i].count
  364.                     intoSlot = i
  365.                     turtle.select(intoSlot)
  366.                 else
  367.                     turtle.select(i)
  368.                     allTotal = allTotal + turtleInventory[i].count
  369.                     turtle.transferTo(intoSlot)
  370.                     if turtleInventory[intoSlot].count + turtleInventory[i].count > turtleInventory[intoSlot].maxSize then
  371.                         turtleInventory[i].count = turtleInventory[i].count - (turtleInventory[intoSlot].maxSize - turtleInventory[intoSlot].count)
  372.                         turtleInventory[intoSlot].count = turtleInventory[intoSlot].maxSize
  373.                     else
  374.                         turtleInventory[intoSlot].count = turtleInventory[intoSlot].count + turtleInventory[i].count
  375.                         turtleInventory[i] = nil
  376.                     end
  377.                 end
  378.                 if allTotal >= count then
  379.                     turtle.select(intoSlot)
  380.                     return intoSlot
  381.                 end
  382.             end
  383.         end
  384.     end
  385.     if intoSlot == 0 then
  386.         repeat
  387.             intoSlot = nearestBuffer(id)
  388.             if intoSlot == 0 then
  389.                 stockInventory()
  390.             end
  391.         until intoSlot > 0
  392.     end
  393.     local chestTotal = {}
  394.     local moves = 0
  395.     local needsToMove = false
  396.     for i = 1, #chests do
  397.         chestTotal[i] = 0
  398.         for o, p in pairs(chests[i]["stacks"]) do
  399.             if p.id == id then
  400.                 allTotal = allTotal + p.qty
  401.                 chestTotal[i] = chestTotal[i] + p.qty
  402.                 if i > 1 then
  403.                     needsToMove = true
  404.                 end
  405.                 if allTotal >= count then
  406.                     break
  407.                 end
  408.             end
  409.         end
  410.     end
  411.     if needsToMove then
  412.         turtle.turnRight()
  413.     end
  414.     turtle.select(intoSlot)
  415.     local done = false
  416.     for i = 1, #chestTotal do
  417.         if chestTotal[i] > 0 then
  418.             for o, p in pairs(chests[i]["stacks"]) do
  419.                 local newCount = (turtleInventory[intoSlot] and turtleInventory[intoSlot].count or 0)
  420.                 if newCount >= count then
  421.                     done = true
  422.                     break
  423.                 end
  424.                 if p.id == id then
  425.                     peripheral.call("top","pushItemIntoSlot","down",o,count - newCount,intoSlot)
  426.                     updateInternalInventory(intoSlot)
  427.                     if p.qty <= count - newCount then
  428.                         chests[i] = checkUp()
  429.                         done = p.qty == count - newCount
  430.                         if done then break end
  431.                     else
  432.                         chests[i]["stacks"][o].qty = p.qty - count - newCount
  433.                         break
  434.                     end
  435.                 end
  436.             end
  437.         end
  438.         if done then
  439.             break
  440.         elseif needsToMove and i < #chestTotal and not done then
  441.             bulldoze(2)
  442.             moves = moves + 1
  443.         end
  444.     end
  445.     if needsToMove then
  446.         for i = 1, moves do
  447.             repeat until back()
  448.             repeat until back()
  449.         end
  450.         turtle.turnLeft()
  451.     end
  452.     return intoSlot
  453. end
  454.  
  455. function checkTotalInventoryCapacity()
  456.     local itemsLeft = 0
  457.     for i = 1, #chests do
  458.         itemsLeft = itemsLeft + chests[i]["size"] - #chests[i]["stacks"]
  459.     end
  460.     return itemsLeft
  461. end
  462.  
  463. function canCraftInstance(v,count)
  464.     local items = {}
  465.     if type(v[2]) == "table" then
  466.         for o = 1, #v[3] do
  467.             if v[3][o] > 0 then
  468.                 if items[v[2][v[3][o]]] == nil then
  469.                     items[v[2][v[3][o]]] = 0
  470.                 end
  471.                 items[v[2][v[3][o]]] = items[v[2][v[3][o]]] + (1/v[1])
  472.             end
  473.         end
  474.         for o, p in pairs(items) do
  475.             items[o] = items[o]*math.ceil(count/v[1])*v[1]
  476.         end
  477.     else
  478.         for o = 2, #v do
  479.             if v[o] ~= "" then
  480.                 if items[v[o]] == nil then
  481.                     items[v[o]] = 0
  482.                 end
  483.                 items[v[o]] = items[v[o]] + 1
  484.             end
  485.         end
  486.     end
  487.     for o, p in pairs(items) do
  488.         if not canCraft(o,p) then
  489.             return false
  490.         end
  491.     end
  492.     return true
  493. end
  494.  
  495. function canCraft(id,count)
  496.     local currentCount = 0
  497.     currentCount = currentCount + itemCount(id)
  498.     if currentCount >= count then
  499.         return true
  500.     end
  501.     if furnaceRecipes[id] then
  502.         currentCount = currentCount + itemCount(furnaceRecipes[id])
  503.         if currentCount >= count then
  504.             return true
  505.         end
  506.     end
  507.     if craftingRecipes[id] then
  508.         local v = craftingRecipes[id]
  509.         if type(v[1]) == "table" then
  510.             for first, second in pairs(craftingRecipes[id]) do
  511.                 local v = second
  512.                 if canCraftInstance(second,count) then
  513.                     return true
  514.                 else
  515.                     print("Could not craft "..count.." of "..id)
  516.                     --sleep(1)
  517.                 end
  518.             end
  519.         else
  520.             if canCraftInstance(v,count) then
  521.                 return true
  522.             else
  523.                 print("Could not craft "..count.." of "..id)
  524.                 --sleep(1)
  525.             end
  526.         end
  527.     end
  528.     return currentCount >= count
  529. end
  530. function minStack(id,qty)
  531.     if 64 - qty > id then
  532.         return id
  533.     else
  534.         return 64 - qty
  535.     end
  536. end
  537. function mergeStack(name)
  538.     local intoSlot = 0
  539.     for i = 1, inventorySize do
  540.         if turtleInventory[i] then
  541.             local v = turtleInventory[i]
  542.             if v.name == name then
  543.                 if intoSlot == 0 and v.count < v.maxSize then
  544.                     intoSlot = i
  545.                 else
  546.                     if intoSlot ~= 0 and v.count < v.maxSize then
  547.                         turtle.select(i)
  548.                         if turtle.transferTo(intoSlot) then
  549.                             if turtleInventory[intoSlot].count + turtleInventory[i].count > turtleInventory[intoSlot].maxSize then
  550.                                 turtleInventory[i].count = turtleInventory[i].count - (turtleInventory[intoSlot].maxSize - turtleInventory[intoSlot].count)
  551.                                 turtleInventory[intoSlot].count = turtleInventory[intoSlot].maxSize
  552.                                 intoSlot = i
  553.                             else
  554.                                 turtleInventory[intoSlot].count = turtleInventory[intoSlot].count + turtleInventory[i].count
  555.                                 turtleInventory[i] = nil
  556.                             end
  557.                         end
  558.                     end
  559.                 end
  560.             end
  561.         end
  562.     end
  563. end
  564. function notInList(list,str)
  565.     for i, v in pairs(list) do
  566.         if str == v then
  567.             return false
  568.         end
  569.     end
  570.     return true
  571. end
  572. function mergeStacks()
  573.     local names = {}
  574.     for i = 1, inventorySize do
  575.         if turtleInventory[i] then
  576.             if notInList(names,turtleInventory[i].name) then
  577.                 table.insert(names,turtleInventory[i].name)
  578.             end
  579.         end
  580.     end
  581.     for i = 1, #names do
  582.         mergeStack(names[i])
  583.     end
  584. end
  585.  
  586. function rf()
  587.     turtle.turnLeft()
  588.     turtle.turnRight()
  589. end
  590.  
  591. function checkFurnaceFuel()
  592.     printTitle("Ultimate Program Running","Checking furnace fuel")
  593.     sleep(0.1)
  594.     local originalSlot = turtle.getSelectedSlot()
  595.     rf()
  596.     local fuelSlot = peripheral.call("bottom","getStackInSlot",2)
  597.     if fuelSlot == nil or fuelSlot.qty < minFuel then
  598.         fuelSlot = (fuelSlot and fuelSlot or {["qty"] = 0})
  599.         local coal = itemCount("minecraft:coal")
  600.         if coal - fuelSlot.qty > 0 then
  601.             if minStack(coal,fuelSlot.qty) > 0 then
  602.                 local newSlot = pullItemFromStorage("minecraft:coal",minStack(coal,fuelSlot.qty))
  603.                 turnAround()
  604.                 turtle.drop()
  605.                 turnAround()
  606.                 turtleInventory[newSlot] = nil
  607.                 turtle.select(originalSlot)
  608.                 --updateInternalInventory(newSlot)
  609.                 for i = 1, 50 do
  610.                     rf()
  611.                     if peripheral.call("bottom","getStackInSlot",2).qty >= minFuel then break end
  612.                     sleep(0.1)
  613.                 end
  614.             end
  615.         end
  616.     end
  617. end
  618.  
  619. function clearCrafting()
  620.     if cautious then
  621.         for i = 1, #slots do
  622.             local slot = turtle.getItemDetail(slots[i])
  623.             if slot then
  624.                 local buffer = nearestBuffer(slot.name)
  625.                 if buffer > 0 then
  626.                     turtle.select(i)
  627.                     turtle.transferTo(buffer)
  628.                     if turtleInventory[buffer] ~= nil and slot.count > turtleInventory[buffer].maxSize - turtleInventory[buffer].count then
  629.                         stockInventory()
  630.                         clearCrafting()
  631.                         break
  632.                     else
  633.                         if turtleInventory[buffer] ~= nil then
  634.                             turtleInventory[buffer].count = turtleInventory[buffer].count + slot.count
  635.                         else
  636.                             updateInternalInventory(buffer)
  637.                         end
  638.                     end
  639.                 else
  640.                     stockInventory()
  641.                     clearCrafting()
  642.                     break
  643.                 end
  644.             end
  645.         end
  646.         updateInternalInventory()
  647.     end
  648. end
  649.  
  650. function clearFurnaceBufferChest()
  651.     for i = 1, 27 do
  652.         peripheral.call("front","pullItem","down",i)
  653.     end
  654. end
  655.  
  656. function craft(id,count)
  657.     printTitle("Ultimate Program Running","Making "..count.." "..id)
  658.     -- peripheral.call("top","getInventorySize")
  659.     local itc = itemCount(id)
  660.     local currentCount = itc
  661.     if itc > 0 then
  662.         local b = nearestBuffer(id)
  663.         local c = turtle.getItemCount(b)
  664.         if c >= count and count > 0 then
  665.             turtle.select(b)
  666.             return true
  667.         else
  668.             if itc >= count then
  669.                 pullItemFromStorage(id,count)
  670.                 return true
  671.             else
  672.                 pullItemFromStorage(id,itc)
  673.             end
  674.         end
  675.     end
  676.     if canCraft(id,count) then
  677.         local ready = false
  678.         local furnaceItem = ""
  679.         if furnaceRecipes[id] then
  680.             ready = true
  681.             furnaceItem = furnaceRecipes[id]
  682.             currentCount = currentCount + itemCount(furnaceRecipes[id])
  683.         end
  684.         if ready then
  685.             -- Furnace stuff happens here
  686.             for i = 1, 2 do
  687.                 if i == 2 then
  688.                     pullItemFromStorage(furnaceRecipes[id],count)
  689.                     turtle.dropDown()
  690.                 end
  691.                 repeat
  692.                     checkFurnaceFuel()
  693.                     rf()
  694.                 until not peripheral.call("bottom","getStackInSlot",1)
  695.                 rf()
  696.                 local stack = peripheral.call("bottom","getStackInSlot",3)
  697.                 if stack then
  698.                     if nearestBuffer(stack.id) == 0 then
  699.                         stockInventory()
  700.                     end
  701.                     local bfr = nearestBuffer(stack.id)
  702.                     turtle.turnRight()
  703.                     back()
  704.                     down()
  705.                     down()
  706.                     forward()
  707.                     turtle.suckUp()
  708.                     turtle.back()
  709.                     up()
  710.                     up()
  711.                     forward()
  712.                     turtle.turnLeft()
  713.                     --peripheral.call("bottom","pushItem",2,3,64,bfr)
  714.                     updateInternalInventory(bfr)
  715.                 end
  716.             end
  717.             if itemCount(id) >= count then
  718.                 return true
  719.             end
  720.         end
  721.         if craftingRecipes[id] then
  722.             local v = craftingRecipes[id]
  723.             local items = {}
  724.             if type(v[1]) == "table" then
  725.                 for first, second in pairs(craftingRecipes[id]) do
  726.                     if canCraftInstance(second,count) then
  727.                         v = second
  728.                     end
  729.                 end
  730.             end
  731.             if type(v[2]) == "table" then
  732.                     for o = 1, #v[3] do
  733.                         if v[3][o] > 0 then
  734.                             if items[v[2][v[3][o]]] == nil then
  735.                                 items[v[2][v[3][o]]] = 0
  736.                             end
  737.                             items[v[2][v[3][o]]] = items[v[2][v[3][o]]] + (1/v[1])
  738.                         end
  739.                     end
  740.                 else
  741.                     for o = 2, #v do
  742.                         if v[o] ~= "" then
  743.                             if items[v[o]] == nil then
  744.                                 items[v[o]] = 0
  745.                             end
  746.                             items[v[o]] = items[v[o]] + (1/v[1])
  747.                         end
  748.                     end
  749.                 end
  750.             local itemsOld = items
  751.             for o, p in pairs(items) do
  752.                 items[o] = items[o]*math.ceil(count/v[1])*v[1]
  753.             end
  754.             for o, p in pairs(items) do
  755.                 if not canCraft(o,p) then
  756.                     return false
  757.                 end
  758.             end
  759.             if not checkIfEmptyInventory() then
  760.                 clearInventory()
  761.                 clearCrafting()
  762.             end
  763.             for o, p in pairs(items) do
  764.                 if itemCount(o) < p then
  765.                     if canCraft(o,p) then
  766.                         craft(o,p) -- Something goes wrong here
  767.                         pushItemToStorage(4)
  768.                     else
  769.                         return false
  770.                     end
  771.                 end
  772.             end
  773.             if not checkIfEmptyInventory() then
  774.                 clearInventory()
  775.                 return craft(id,count)
  776.             end
  777.             for o, p in pairs(items) do
  778.                 local item = pullItemFromStorage(o,p)
  779.                 if type(v[2]) == "table" then
  780.                     for m = 1, #v[3] do
  781.                         if v[3][m] > 0 and v[2][v[3][m]] == o  then
  782.                             turtle.transferTo(slots[m],math.ceil((1/v[1])*count))
  783.                             updateInternalInventory(item)
  784.                             updateInternalInventory(slots[m])
  785.                         end
  786.                     end
  787.                 else
  788.                     for m = 2, #v do
  789.                         if v[m] == o then
  790.                             turtle.transferTo(slots[m - 1],math.ceil((1/v[1])*count))
  791.                             updateInternalInventory(item)
  792.                             updateInternalInventory(slots[m - 1])
  793.                         end
  794.                     end
  795.                 end
  796.             end
  797.             turtle.select(4)
  798.             local success = turtle.craft()
  799.             updateInternalInventory()
  800.             return success
  801.         end
  802.     else
  803.         return false
  804.     end
  805.     return true
  806. end
  807.  
  808. function pushItemToStorage(slotz)
  809.     local pushChest = 0
  810.     local originalSlot = turtle.getSelectedSlot()
  811.     local slot = (slotz and slotz or originalSlot)
  812.     local currentSlot = turtleInventory[slot]
  813.     if originalSlot ~= slot then
  814.         turtle.select(slot)
  815.     end
  816.     if #chests > 1 then
  817.         turtle.turnRight()
  818.     end
  819.     for i = 1, #chests do
  820.         local oldCount = turtle.getItemCount()
  821.         if turtle.dropUp() then
  822.             if turtle.getItemCount() == 0 then
  823.                 checkUp(i,currentSlot.name,oldCount)
  824.                 turtleInventory[slot] = nil
  825.                 break
  826.             else
  827.                 checkUp(i,currentSlot.name,oldCount)
  828.             end
  829.         end
  830.         if #chests > 1 and i < #chests then
  831.             forward()
  832.             forward()
  833.             pushChest = pushChest + 1
  834.         end
  835.     end
  836.     for i = 1, pushChest do
  837.         back()
  838.         back()
  839.     end
  840.     if #chests > 1 then
  841.         turtle.turnLeft()
  842.     end
  843.     turtle.select(originalSlot)
  844. end
  845.  
  846. function forward()
  847.     return turtle.forward()
  848. end
  849.  
  850. function back()
  851.     return turtle.back()
  852. end
  853.  
  854. function up()
  855.     return turtle.up()
  856. end
  857.  
  858. function down()
  859.     return turtle.down()
  860. end
  861.  
  862. function updateAllChests()
  863.     checkFuel()
  864.     if #chests > 1 then
  865.         turtle.turnRight()
  866.     end
  867.     for i = 1, #chests do
  868.         chests[i] = checkUp()
  869.         if #chests > 1 and i < #chests then
  870.             bullDoze(2)
  871.         end
  872.     end
  873.     turnAround()
  874.     if #chests > 1 then
  875.         bullDoze((#chests - 1)*2)
  876.     end
  877.     if #chests > 1 then
  878.         turtle.turnRight()
  879.     end
  880. end
  881.  
  882. function digAndUpdate(dir)
  883.     local slot = turtle.getSelectedSlot()
  884.     local there, inspect = false, {}
  885.     if dir == "up" then
  886.         there, inspect = turtle.inspectUp()
  887.     elseif dir == "down" then
  888.         there, inspect = turtle.inspectDown()
  889.     else
  890.         there, inspect = turtle.inspect()
  891.     end
  892.     if there then
  893.         local bf = 0
  894.         if inspect.name == "minecraft:dirt" then
  895.             bf = nearestBuffer(inspect.name)
  896.         elseif inspect.name == "minecraft:stone" then
  897.             bf = nearestBuffer("minecraft:cobblestone")
  898.         else
  899.             bf = nearestBuffer()
  900.         end
  901.         turtle.select(bf)
  902.         if dir == "up" then
  903.             turtle.digUp()
  904.         elseif dir == "down" then
  905.             turtle.digDown()
  906.         else
  907.             turtle.dig()
  908.         end
  909.         updateInternalInventory(bf)
  910.         mergeStacks()
  911.     end
  912.     turtle.select(slot)
  913. end
  914.  
  915. function makePickaxe(i)
  916.     if i == #chests then
  917.         if turtle.detect() then
  918.             digAndUpdate()
  919.         end
  920.     end
  921.     forward()
  922. end
  923.  
  924. function slotsLeft()
  925.     return inventorySize - #turtleInventory - 9
  926. end
  927.  
  928. local miners = 0
  929.  
  930. function reduceByOne(slot)
  931.     if turtleInventory[slot].count == 1 then
  932.         turtleInventory[slot] = nil
  933.     else
  934.         turtleInventory[slot].count = turtleInventory[slot].count - 1
  935.     end
  936. end
  937.  
  938. function countMiners()
  939.     printTitle("Ultimate Program Loading","Counting Miners")
  940.     turtle.turnLeft()
  941.     forward()
  942.     down()
  943.     turtle.turnRight()
  944.     digAndUpdate()
  945.     forward()
  946.     turtle.turnLeft()
  947.     digAndUpdate()
  948.     forward()
  949.     local minerz = 0
  950.     local stillMiners = true
  951.     while stillMiners do
  952.         stillMiners, hopper = turtle.inspectUp()
  953.         if stillMiners and hopper and hopper.name == "minecraft:hopper" then
  954.             minerz = minerz + 1
  955.             digAndUpdate()
  956.             forward()
  957.         end
  958.     end
  959.     for i = 1, minerz + 1 do
  960.         turtle.back()
  961.     end
  962.     turtle.turnLeft()
  963.     turtle.forward()
  964.     turtle.turnRight()
  965.     turtle.up()
  966.     turtle.back()
  967.     turtle.turnRight()
  968.     miners = minerz
  969. end
  970.  
  971. function addNewMiner() -- Needs fixing or testing!
  972.     stockInventory()
  973.     --[[
  974.     local list = {"minecraft:hopper","ComputerCraft:CC-TurtleExpanded","ComputerCraft:CC-Peripheral","ComputerCraft:disk"}
  975.     for i, v in pairs(list) do
  976.         print(v..": "..tostring(canCraft(v,1)))
  977.     end
  978. ]]
  979.     if canCraft("minecraft:hopper",1) and canCraft("ComputerCraft:CC-TurtleExpanded",1) and canCraft("ComputerCraft:CC-Peripheral",1) and canCraft("ComputerCraft:disk",1) and canCraft("minecraft:coal",16) then
  980.         printTitle("Ultimate Program Running","Adding New Miner")
  981.         craft("minecraft:hopper",1)
  982.         craft("ComputerCraft:CC-Peripheral",1)
  983.         craft("ComputerCraft:CC-TurtleExpanded",1)
  984.         craft("ComputerCraft:disk",1)
  985.         if slotsLeft() < 4 then
  986.             stockInventory()
  987.         end
  988.         pullItemFromStorage("minecraft:hopper",1)
  989.         pullItemFromStorage("ComputerCraft:CC-Peripheral",1)
  990.         pullItemFromStorage("ComputerCraft:CC-TurtleExpanded",1)
  991.         pullItemFromStorage("ComputerCraft:disk",1)
  992.         pullItemFromStorage("minecraft:coal",16)
  993.         local droneSlot = turtle.getSelectedSlot()
  994.         -- nearestBuffer("ComputerCraft:CC-Turtle")
  995.         turtle.turnLeft()
  996.         forward()
  997.         if not turtle.up() then
  998.             digAndUpdate("up")
  999.             turtle.up()
  1000.         end
  1001.         for i = 1, miners + 1 do
  1002.             if math.ceil(i/2) == miners + 1 then
  1003.                 if turtle.detect() then
  1004.                     digAndUpdate()
  1005.                 end
  1006.             end
  1007.             if not forward() then
  1008.                 digAndUpdate()
  1009.                 forward()
  1010.             end
  1011.         end
  1012.         if not down() then
  1013.             digAndUpdate("down")
  1014.             down()
  1015.         end
  1016.         pullItemFromStorage("ComputerCraft:CC-Peripheral",1)
  1017.         if turtle.placeDown() == false then
  1018.             digAndUpdate("down")
  1019.             pullItemFromStorage("ComputerCraft:CC-Peripheral",1)
  1020.             turtle.placeDown()
  1021.         end
  1022.         reduceByOne(turtle.getSelectedSlot())
  1023.         mergeStacks()
  1024.         local t = pullItemFromStorage("ComputerCraft:disk",1)
  1025.         turtle.dropDown()
  1026.         turtleInventory[t] = nil
  1027.         turtle.turnRight()
  1028.         repeat
  1029.             sleep(0.1)
  1030.         until fs.exists("disk")
  1031.         local f = fs.open("disk/minerindex","w")
  1032.         f.write(tostring(miners + 1))
  1033.         f.close()
  1034.         if fs.exists("auto") and not fs.exists("disk/startup") then
  1035.             fs.copy("auto","disk/startup")
  1036.             disk.setLabel("bottom","Automation Miner Startup Disk")
  1037.         end
  1038.         local it = pullItemFromStorage("minecraft:hopper",1)
  1039.         digAndUpdate()
  1040.         forward()
  1041.         turtle.turnLeft()
  1042.         digAndUpdate()
  1043.         forward()
  1044.         turnAround()
  1045.         turtle.select(it)
  1046.         turtle.place()
  1047.         reduceByOne(it)
  1048.         mergeStacks()
  1049.         turtle.turnRight()
  1050.         digAndUpdate()
  1051.         forward()
  1052.         turtle.turnLeft()
  1053.         digAndUpdate()
  1054.         forward()
  1055.         turtle.turnLeft()
  1056.         digAndUpdate("up")
  1057.         up()
  1058.         local tr = pullItemFromStorage("ComputerCraft:CC-TurtleExpanded",1)
  1059.         turtle.placeDown()
  1060.         local coalSlot = pullItemFromStorage("minecraft:coal",16)
  1061.         turtle.dropDown(16)
  1062.         turtle.turnLeft()
  1063.         reduceByOne(tr)
  1064.         peripheral.call("bottom","turnOn")
  1065.         miners = miners + 1
  1066.         for i = 1, miners do
  1067.             back()
  1068.         end
  1069.         down()
  1070.         back()
  1071.         turtle.turnRight()
  1072.         return true
  1073.     end
  1074.     return false
  1075. end
  1076.  
  1077. function bulldoze(i)
  1078.     for i = 1, i do
  1079.         if not forward() then repeat turtle.dig() until forward() end
  1080.     end
  1081. end
  1082.  
  1083. function addNewChest()
  1084.     stockInventory()
  1085.     if ironChests then
  1086.         local firstChest = 0
  1087.         for i = 1, #chests do
  1088.             if chests[i].size == 81 and canCraft("IronChest:goldDiamondUpgrade",1) then
  1089.                 firstChest = i
  1090.                 craft("IronChest:goldDiamondUpgrade",1)
  1091.                 break
  1092.             elseif chests[i].size == 54 and canCraft("IronChest:ironGoldUpgrade",1) then
  1093.                 firstChest = i
  1094.                 craft("IronChest:ironGoldUpgrade",1)
  1095.                 break
  1096.             elseif chests[i].size == 27 and canCraft("IronChest:woodIronUpgrade",1) then
  1097.                 firstChest = i
  1098.                 craft("IronChest:woodIronUpgrade",1)
  1099.                 break
  1100.             end
  1101.         end
  1102.         if firstChest ~= 0 then
  1103.             if firstChest > 1 then
  1104.                 turtle.turnRight()
  1105.                 bulldoze((firstChest - 1)*2)
  1106.             end
  1107.             turtle.placeUp()
  1108.             chests[firstChest] = checkUp()
  1109.             updateInternalInventory()
  1110.             if firstChest > 1 then
  1111.                 turnAround()
  1112.                 bulldoze((firstChest - 1)*2)
  1113.                 turtle.turnRight()
  1114.             end
  1115.             return true
  1116.         end
  1117.     else
  1118.         for i = 1, #chests do
  1119.             print(chests[i]["size"])
  1120.             sleep(1)
  1121.             if chests[i]["size"] == 27 then
  1122.                 if canCraft("minecraft:chest",1) then
  1123.                     craft("minecraft:chest",1)
  1124.                     local chest = pullItemFromStorage("minecraft:chest",1)
  1125.                     turtle.turnRight()
  1126.                     for o = 1, i - 1 do
  1127.                         makePickaxe(o)
  1128.                         makePickaxe(o)
  1129.                     end
  1130.                     if i == 1 then
  1131.                         repeat
  1132.                             digAndUpdate()
  1133.                         until forward()
  1134.                         turtle.turnRight()
  1135.                         repeat
  1136.                             digAndUpdate()
  1137.                         until turtle.forward()
  1138.                         turtle.turnRight()
  1139.                         repeat
  1140.                             digAndUpdate("up")
  1141.                         until up()
  1142.                         digAndUpdate()
  1143.                         turtle.place()
  1144.                         down()
  1145.                         turtle.turnLeft()
  1146.                         back()
  1147.                         turtle.turnLeft()
  1148.                         back()
  1149.                     else
  1150.                         turtle.turnRight()
  1151.                         repeat
  1152.                             digAndUpdate()
  1153.                         until turtle.forward()
  1154.                         digAndUpdate("up")
  1155.                         turtle.placeUp()
  1156.                         back()
  1157.                         turtle.turnLeft()
  1158.                     end
  1159.                     reduceByOne(chest)
  1160.                     checkUp(i)
  1161.                     for o = 1, i - 1 do
  1162.                         back()
  1163.                         back()
  1164.                     end
  1165.                     turtle.turnLeft()
  1166.                     return true
  1167.                 else
  1168.                     return false
  1169.                 end
  1170.             end
  1171.         end
  1172.     end
  1173.     if canCraft("minecraft:chest",(ironChests and 1 or 2)) then
  1174.         craft("minecraft:chest",(ironChests and 1 or 2))
  1175.         turtle.turnRight()
  1176.         for i = 1, #chests do
  1177.             makePickaxe(i)
  1178.             makePickaxe(i)
  1179.         end
  1180.         digAndUpdate("up")
  1181.         local chest = pullItemFromStorage("minecraft:chest",(ironChests and 1 or 2))
  1182.         turtle.placeUp()
  1183.         reduceByOne(chest)
  1184.         if not ironChests then
  1185.             turtle.turnRight()
  1186.             repeat
  1187.                 digAndUpdate()
  1188.             until turtle.forward()
  1189.             digAndUpdate("up")
  1190.             turtle.placeUp()
  1191.             chests[#chests + 1] = checkUp()
  1192.             turtle.back()
  1193.             turtle.turnLeft()
  1194.             reduceByOne(chest)
  1195.         end
  1196.         chests[#chests + 1] = checkUp()
  1197.         for i = 1, #chests * 2 do
  1198.             back()
  1199.         end
  1200.         turtle.turnLeft()
  1201.         return true
  1202.     end
  1203.     return false
  1204. end
  1205. --[[
  1206. for i, v in pairs(chests[1][1]) do
  1207.     if v["all"] then
  1208.         for o, p in pairs(v.all()) do
  1209.             textutils.slowPrint(o..": "..tostring(p))
  1210.         end
  1211.     end
  1212. end
  1213.  
  1214. id:
  1215. minecraft:log
  1216. minecraft:chest
  1217. minecraft:planks
  1218. minecraft:cobblestone
  1219. minecraft:gold_ingot
  1220. minecraft:furnace
  1221. minecraft:diamond
  1222. minecraft:stick
  1223. minecraft:diamond_pickaxe
  1224. minecraft:chest
  1225. minecraft:iron_ingot
  1226. minecraft:redstone
  1227. minecraft:sand
  1228. minecraft:glass
  1229. minecraft:glass_pane
  1230. minecraft:ender_pearl
  1231. IronChest:woodIronUpgrade
  1232. IronChest:ironGoldUpgrade
  1233. IronChest:goldDiamondUpgrade
  1234. raw_name:
  1235. tile.ironchest:diamond
  1236. tile.ironchest:iron
  1237. tile.ironchest:gold
  1238. tile.computercraft:advanced_computer
  1239. tile.computercraft:computer
  1240. tile.computercraft:turtle
  1241. ]]
  1242.  
  1243. function getItemName()
  1244.     write("Item Name: ")
  1245.     local itemName = ""
  1246.     itemName = read()
  1247.     if not itemName then
  1248.         return nil
  1249.     end
  1250.     if itemName == "exit" or itemName == "add miner" or itemName == "count" or itemName == "clear" or itemName == "dumpi"  or itemName == "dumpe" or itemName == "miners" or itemName == "refresh" then
  1251.         return itemName
  1252.     end
  1253.     if string.gsub(itemName," ","") == "" then
  1254.         return nil
  1255.     end
  1256.     write("Item Count: ")
  1257.     local itemCount2 = ""
  1258.     itemCount2 = tonumber(read())
  1259.     if not itemCount2 then
  1260.         return nil
  1261.     end
  1262.     return itemName, itemCount2
  1263. end
  1264. function printCenter(str,line)
  1265.     local x, y = term.getSize()
  1266.     term.setCursorPos((x/2) - (string.len(str)/2),line)
  1267.     term.clearLine()
  1268.     term.setCursorPos((x/2) - (string.len(str)/2),line)
  1269.     write(str)
  1270. end
  1271. function printLine()
  1272.     write(string.rep("-",({term.getSize()})[1]))
  1273. end
  1274. function printTitle(...)
  1275.     for i = 1, #({...}) do
  1276.         printCenter(({...})[i],i)
  1277.     end
  1278.     term.setCursorPos(1,#({...}) + 1)
  1279.     printLine()
  1280. end
  1281. function getInput()
  1282.     turtle.turnRight()
  1283.     term.clear()
  1284.     printTitle("Ultimate Program Interface", "Type \"exit\" to exit, enter to cancel.","\"add miner\",  \"clear\", \"count\".")
  1285.     local itemName, itemCount2 = getItemName()
  1286.     itemCount2 = tonumber(itemCount2)
  1287.     if itemName then
  1288.         if string.lower(itemName) == "exit" then
  1289.             turtle.turnLeft()
  1290.             return false
  1291.         elseif string.lower(itemName) == "add miner" then
  1292.             print("Attempting to add new miner...")
  1293.             turtle.turnLeft()
  1294.             addNewMiner()
  1295.             return true
  1296.         elseif string.lower(itemName) == "miners" then
  1297.             turtle.turnLeft()
  1298.             print("Miners: "..tostring(miners))
  1299.             read()
  1300.             return true
  1301.         elseif string.lower(itemName) == "clear" then
  1302.             turtle.turnLeft()
  1303.             print("Clearing inventory...")
  1304.             clearCrafting()
  1305.             clearInventory()
  1306.             return true
  1307.         elseif string.lower(itemName) == "count" then
  1308.             write("Item name: ")
  1309.             local itemName2 = read()
  1310.             print(itemCount2)
  1311.             print(itemName2)
  1312.             print("There are "..itemCount(itemName2).." of "..itemName2.." available.")
  1313.             sleep(2)
  1314.         elseif string.lower(itemName) == "dumpe" then
  1315.             textutils.pagedPrint(textutils.serialize(chests))
  1316.             read()
  1317.         elseif string.lower(itemName) == "dumpi" then
  1318.             textutils.pagedPrint(textutils.serialize(turtleInventory))
  1319.             read()
  1320.         elseif string.lower(itemName) == "refresh" then
  1321.             refreshState()
  1322.         else
  1323.             if canCraft(itemName,itemCount2) then
  1324.                 print("Processing...")
  1325.                 turtle.turnLeft()
  1326.                 for i = 1, math.ceil(itemCount2/64) do
  1327.                     craft(itemName,(i*64 > itemCount2 and itemCount2 % 64 or 64))
  1328.                     turtle.turnRight()
  1329.                     turtle.drop(i*64 > itemCount2 and itemCount2 % 64 or 64)
  1330.                     if i < math.ceil(itemCount2/64) then
  1331.                         turtle.turnLeft()
  1332.                     end
  1333.                     updateInternalInventory(turtle.getSelectedSlot())
  1334.                 end
  1335.             else
  1336.                 print("Cannot craft items.")
  1337.                 sleep(0.5)
  1338.             end
  1339.         end
  1340.     end
  1341.     turtle.turnLeft()
  1342.     return true
  1343. end
  1344. function checkRedstone()
  1345.     local e = {"nothing"}
  1346.     if not rs.getInput("bottom") then
  1347.         os.startTimer(0.1)
  1348.         e = {os.pullEvent()}
  1349.     end
  1350.     if rs.getInput("bottom") or e[1] == "key" or e[1] == "char" then
  1351.         if getInput() == false then
  1352.             term.clear()
  1353.             printTitle("Ultimate Program Closed")
  1354.             term.setCursorPos(1,3)
  1355.             return true
  1356.         else
  1357.             term.clear()
  1358.             printTitle("Ultimate Program Running")
  1359.         end
  1360.     end
  1361.     return false
  1362. end
  1363.  
  1364. function refreshState()
  1365.     term.clear()
  1366.     printTitle("Ultimate Program Loading","Initializing External Inventory")
  1367.     chests = {checkUp()}
  1368.     checkFuel()
  1369.     turtle.turnRight()
  1370.     local initChests = 0
  1371.     repeat
  1372.         local breakingForward = false
  1373.         repeat
  1374.             if turtle.detect() then
  1375.                 breakingForward = true
  1376.                 break
  1377.             end
  1378.         until forward()
  1379.         if breakingForward then break end
  1380.         repeat
  1381.             if turtle.detect() then
  1382.                 breakingForward = true
  1383.                 repeat until back()
  1384.                 break
  1385.             end
  1386.         until forward()
  1387.         if breakingForward then break end
  1388.         initChests = initChests + 1
  1389.         local check = checkUp()
  1390.         if check then chests[initChests + 1] = check else break end
  1391.     until not peripheral.call("top","getInventorySize")
  1392.     if initChests > 0 then
  1393.         for i = 1, initChests*2 do
  1394.             if not back() then
  1395.                 turnAround()
  1396.                 repeat until not turtle.dig()
  1397.                 turnAround()
  1398.                 back()
  1399.             end
  1400.         end
  1401.     end
  1402.     turtle.turnLeft()
  1403.     countMiners()
  1404.     printTitle("Ultimate Program Loading","Initializing Internal Inventory")
  1405.     updateInternalInventory()
  1406.     stockInventory()
  1407.     term.clear()
  1408. end
  1409.  
  1410. refreshState()
  1411. local stop = false
  1412. while true do
  1413.     mergeStacks()
  1414.     if checkTotalInventoryCapacity() < inventorySize*2 then
  1415.         repeat
  1416.             if checkRedstone() then stop = true; break end
  1417.             addNewChest()
  1418.         until checkTotalInventoryCapacity() >= inventorySize
  1419.         if stop then break end
  1420.     end
  1421.     stockInventory()
  1422.     printTitle("Ultimate Program Running","Idle")
  1423.     sleep(0.1)
  1424.     mergeStacks()
  1425.     if checkRedstone() then break end
  1426.     if makeMiners and miners < maxMiners then addNewMiner() end
  1427.     printTitle("Ultimate Program Running","Idle")
  1428.     sleep(0.1)
  1429.     mergeStacks()
  1430.     if checkRedstone() then break end
  1431. end
Add Comment
Please, Sign In to add comment