Guest User

Recovered autocrafting cobblestone-diamond script

a guest
Feb 1st, 2014
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.18 KB | None | 0 0
  1. os.loadAPI("logging.lua")
  2. log = Logger.new()
  3. log.SetTraceLevel()
  4.  
  5. -- expected setup (from the side)
  6. --   G    = cobble generator
  7. --   C    = chest
  8. --  IT    = item chest / crafty turtle facing I
  9. --   M    = chest for storing minium stone (in first slot)
  10.  
  11. -- prerequisites:
  12. -- computercraft, OpenPeripherals, EE3 (for minium)
  13.  
  14. -- MANDATORY CONFIGURATION!!!!
  15. -- direction where turtle is located relative to the main item chest I
  16. -- TURTLE_DIRECTION() loaded from config file when first needed
  17. TURTLE_DIRECTION_CONFIG = nil
  18. function TURTLE_DIRECTION()
  19.     if TURTLE_DIRECTION_CONFIG == nil then
  20.         local h = fs.open("diacraft.config", "r")
  21.         assert(h ~= nil, "Config file open failed. Write 'north', 'south', 'east' or 'west' into diacraft.config file")
  22.         TURTLE_DIRECTION_CONFIG = h.readLine()
  23.         h.close()
  24.         assert(TURTLE_DIRECTION_CONFIG ~= nil, "Turtle direction not loaded")
  25.     end
  26.     return TURTLE_DIRECTION_CONFIG
  27. end
  28.  
  29. PROGNAME_UPDATE = "diacraft.update"
  30. PROGNAME_BACKUP = "diacraft.backup"
  31. PROGNAME = "diacraft"
  32. PASTEBIN_CODE = "uUSD1U4k"
  33.  
  34. --Algorithm:
  35. --4x Cobble => Flint
  36. --4x Flint => Clay
  37. --4x Clay => Clay Block
  38. --4x Clay Block => Iron ingot
  39. --8x Iron ingot => Gold
  40. --4x Gold => Diamond
  41.  
  42. -- define metatable for items - we want readable to string
  43. function item(id, meta)
  44.     local i = {id = id,
  45.         meta = meta,
  46.         metaIgnore = false,
  47.         minCount = 4,
  48.         miniumPatternId = 4
  49.     }
  50.     return i
  51. end
  52.  
  53. -- Required input:
  54. ITEM = {}
  55. local itemmt = {__index = function(table, key) error("Unexisting key: "..key) end}
  56. setmetatable(ITEM,itemmt)
  57. ------------------------ Farm -----------------
  58. ITEM.COBBLE = item(4, 0)
  59. ITEM.FLINT = item(318, 0)
  60. ITEM.CLAY = item(337, 0)
  61. ITEM.CLAYBLOCK = item(82, 0)
  62.  
  63. ITEM.IRON_INGOT = item(265, 0)
  64. ITEM.IRON_INGOT.minCount = 8
  65. ITEM.IRON_INGOT.miniumPatternId = 8
  66.  
  67. ITEM.GOLD_INGOT = item(266, 0)
  68. ITEM.DIAMOND = item(264, 0)
  69.  
  70. ITEM.MINIUM_STONE = item(27002,0)
  71. ITEM.MINIUM_STONE.metaIgnore = true
  72.  
  73. local craftingOrder = {ITEM.COBBLE, ITEM.FLINT, ITEM.CLAY, ITEM.CLAYBLOCK, ITEM.IRON_INGOT, ITEM.GOLD_INGOT, ITEM.DIAMOND}
  74. for i=1,#craftingOrder - 1 do
  75.     craftingOrder[i+1].prevOne = craftingOrder[i]
  76.     craftingOrder[i].nextOne = craftingOrder[i+1]
  77. end
  78.  
  79. for _, item in pairs(ITEM) do
  80.     item.registered = true
  81. end
  82.  
  83. function itemEquals(item, item2)
  84.     --print ("Item1: id="..item.id..", meta="..item.meta)
  85.     --print ("Item2: id="..item2.id..", meta="..item2.meta)
  86.     -- if first item is not registered, then work with second item as registered
  87.     -- if none is registered, it doesn't matter... but ignoreMeta won't work
  88.     if not(item.registered) then
  89.         item, item2 = item2, item
  90.     end
  91.  
  92.     if (item.id == item2.id and (item.ignoreMeta or (item.meta == item2.meta))) then
  93.         return true
  94.     else
  95.         return false
  96.     end
  97. end
  98.  
  99. function findItem(id, meta)
  100.     assert(id ~= nil, "Id is nil")
  101.     assert(meta ~= nil, "Meta is nil")
  102.     -- log.Trace("Looking for id="..id..", meta="..meta)
  103.     local lookingFor = item(id, meta)
  104.     for _,item in pairs(ITEM) do
  105.         -- log.Trace("Comparing ITEM id="..item.id..", meta="..item.meta)
  106.         if itemEquals(item, lookingFor) then
  107.             return item
  108.         end
  109.     end
  110.     return nil
  111. end
  112.  
  113. local errors = false
  114.  
  115. if not peripheral.isPresent("front") then
  116.     print "Missing front chest.  This is used for items."
  117.     errors = true
  118. end
  119.  
  120. if not peripheral.isPresent("top") then
  121.     print "Missing top chest.  This is used for cobblestone"
  122.     errors = true
  123. end
  124.  
  125. if not peripheral.isPresent("bottom") then
  126.     print "Missing bottom chest.  This stores the Minium Stone."
  127.     errors = true
  128. end
  129.  
  130. local itemChest = peripheral.wrap("front")
  131. local cobbleChest = peripheral.wrap("top")
  132. local miniumChest = peripheral.wrap("bottom")
  133.  
  134. if not cobbleChest then
  135.     print "Cobble chest was nil."
  136.     errors = true
  137. end
  138.  
  139. if not itemChest then
  140.     print "Item chest was nil."
  141.     errors = true
  142. end
  143.  
  144. if not miniumChest then
  145.     print "Minium chest was nil."
  146.     errors = true
  147. end
  148.  
  149. if errors == true then
  150.     print "Exiting due to errors."
  151.     return
  152. end
  153.  
  154.  
  155. function craftWithMinium(index, craftCount, miniumPatternId)
  156.  
  157.     if miniumPatternId == 4 then
  158.         itemChest.pushItemIntoSlot(TURTLE_DIRECTION(), index,craftCount, 2)
  159.         itemChest.pushItemIntoSlot(TURTLE_DIRECTION(), index,craftCount, 5)
  160.         itemChest.pushItemIntoSlot(TURTLE_DIRECTION(), index,craftCount, 6)
  161.         itemChest.pushItemIntoSlot(TURTLE_DIRECTION(), index,craftCount, 9)
  162.     elseif miniumPatternId == 8 then
  163.         itemChest.pushItemIntoSlot(TURTLE_DIRECTION(), index,craftCount, 2)
  164.         itemChest.pushItemIntoSlot(TURTLE_DIRECTION(), index,craftCount, 3)
  165.         itemChest.pushItemIntoSlot(TURTLE_DIRECTION(), index,craftCount, 5)
  166.         itemChest.pushItemIntoSlot(TURTLE_DIRECTION(), index,craftCount, 6)
  167.         itemChest.pushItemIntoSlot(TURTLE_DIRECTION(), index,craftCount, 7)
  168.         itemChest.pushItemIntoSlot(TURTLE_DIRECTION(), index,craftCount, 9)
  169.         itemChest.pushItemIntoSlot(TURTLE_DIRECTION(), index,craftCount, 10)
  170.         itemChest.pushItemIntoSlot(TURTLE_DIRECTION(), index,craftCount, 11)
  171.     else
  172.         error("Unkonwn pattern ID:"..miniumPatternId)
  173.     end
  174.  
  175.     turtle.select(12)
  176.     for i = 1,craftCount do
  177.         turtle.craft()
  178.         turtle.drop()
  179.     end
  180. end
  181.  
  182. function craftSquare(index, craftCount)
  183.     itemChest.pushItemIntoSlot(TURTLE_DIRECTION(), index,craftCount, 1)
  184.     itemChest.pushItemIntoSlot(TURTLE_DIRECTION(), index,craftCount, 2)
  185.     itemChest.pushItemIntoSlot(TURTLE_DIRECTION(), index,craftCount, 5)
  186.     itemChest.pushItemIntoSlot(TURTLE_DIRECTION(), index,craftCount, 6)
  187.  
  188.     turtle.select(12)
  189.     for i = 1,craftCount do
  190.         turtle.craft()
  191.         turtle.drop()
  192.     end
  193. end
  194.  
  195. function craftWithItem(index)
  196.     log.Debug("Pulling items from item chest index " .. tostring(index))
  197.     local slot = itemChest.getStackInSlot(index)
  198.     local originalItem
  199.  
  200.     log.Trace("findItem(" .. tostring(slot.id) .. ", " .. tostring(slot.dmg) .. ")")
  201.     originalItem = findItem(slot.id, slot.dmg)
  202.  
  203.     log.Trace("needMinium check.")
  204.     local needMinium = not(itemEquals(ITEM.CLAY, item(slot.id, slot.dmg)))
  205.  
  206.     if needMinium then
  207.         assert(miniumChest.pushItemIntoSlot("up", 1, 1, 1) == 1, "minium grab failed")
  208.     end
  209.  
  210.     while(slot ~= nil and slot.qty >= originalItem.minCount and itemEquals(originalItem, item(slot.id, slot.dmg))) do
  211.         local craftCount = math.floor(slot.qty / originalItem.minCount)
  212.  
  213.         if needMinium then
  214.             log.Trace("craftWithMinium(" .. tostring(index) .. tostring(craftCount) .. tostring(originalItem.miniumPatternId) .. ")")
  215.             craftWithMinium(index, craftCount, originalItem.miniumPatternId)
  216.         else
  217.             log.Trace("craftSquare(" .. tostring(index) .. tostring(craftCount) .. ")")
  218.             craftSquare(index, craftCount)
  219.         end
  220.  
  221.         for turtleSlotNum = 2, 12 do
  222.             log.Trace("turtle.select(" .. tostring(turtleSlotNum) .. ")")
  223.             turtle.select(turtleSlotNum)
  224.             turtle.drop()
  225.         end
  226.         log.Trace("Condensing items again")
  227.         itemChest.condenseItems()
  228.         log.Trace("Getting slot.")
  229.         slot = itemChest.getStackInSlot(index)
  230.     end
  231.  
  232.     if needMinium then
  233.         assert(miniumChest.pullItemIntoSlot("up", 1, 1, 1) == 1, "minium return failed")
  234.     end
  235. end
  236.  
  237.  
  238. function iterateOverChest()
  239.     -- OpenPeripherals index from 0
  240.     for i = 1,itemChest.getInventorySize()-1 do
  241.         local slot = itemChest.getStackInSlot(i)
  242.         if slot ~= nil then
  243.             log.Debug("Slot "..i.." contains item: "..slot.name)
  244.             local item = findItem(slot.id, slot.dmg)
  245.             if (item ~= nil and item.nextOne ~= nil and slot.qty >= item.minCount) then
  246.                 craftWithItem(i)
  247.             end
  248.         end
  249.     end
  250. end
  251.  
  252. function update()
  253.     fs.delete(PROGNAME_UPDATE)
  254.     shell.run("pastebin","get",PASTEBIN_CODE, PROGNAME_UPDATE)
  255.     if fs.exists(PROGNAME_UPDATE) then
  256.         fs.delete(PROGNAME_BACKUP)
  257.         fs.move(PROGNAME,PROGNAME_BACKUP)
  258.         fs.move(PROGNAME_UPDATE, PROGNAME)
  259.     end
  260. end
  261.  
  262. local argv = {...}
  263. if #argv == 1 and argv[1] == "update" then
  264.     update()
  265.     error()
  266. end
  267.  
  268. --some common recovery
  269. --- Minium in my inventory
  270. if turtle.getItemCount(1) == 1 then
  271.     log.Info("Minium stone probably in our inventory, fixing")
  272.     assert(miniumChest.getStackInSlot(1) == nil, "Slot in chest for minium is not empty")
  273.     assert(miniumChest.pullItemIntoSlot("up", 1,1,1) == 1, "Minium removal failed")
  274. end
  275.  
  276. local slot = miniumChest.getStackInSlot(1)
  277. assert(slot ~= nil and itemEquals(ITEM.MINIUM_STONE, item(slot.id, slot.dmg)) == true, "Chest below us must contain minium stone")
  278.  
  279. while(true) do
  280.     log.Debug("Condensing cobble chest")
  281.     cobbleChest.condenseItems()
  282.     log.Debug("Selecting 13")
  283.     turtle.select(13)
  284.     log.Debug("Sucking up items")
  285.     turtle.suckUp()
  286.     log.Debug("Dropping items")
  287.     turtle.drop()
  288.     log.Debug("Condensing item chest")
  289.     itemChest.condenseItems()
  290.     log.Debug("Iterating over chest")
  291.     iterateOverChest()
  292.     sleep(0.1)
  293. end
Advertisement
Add Comment
Please, Sign In to add comment