Advertisement
Guest User

dffd

a guest
Nov 17th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.64 KB | None | 0 0
  1. -- expected setup (from the side)
  2. -- G = cobble generator
  3. -- C = chest
  4. -- IT = item chest / crafty turtle facing I
  5. -- M = chest for storing minium stone (in first slot)
  6.  
  7. -- prerequisites:
  8. -- computercraft, OpenPeripherals, EE3 (for minium)
  9.  
  10. -- MANDATORY CONFIGURATION!!!!
  11. -- direction where turtle is located relative to the main item chest I
  12. -- TURTLE_DIRECTION() loaded from config file when first needed
  13. TURTLE_DIRECTION_CONFIG = nil
  14. function TURTLE_DIRECTION()
  15. if TURTLE_DIRECTION_CONFIG == nil then
  16. local h = fs.open("diacraft.config", "r")
  17. assert(h ~= nil, "Config file open failed. Write 'north', 'south', 'east' or 'west' into diacraft.config file")
  18. TURTLE_DIRECTION_CONFIG = h.readLine()
  19. h.close()
  20. assert(TURTLE_DIRECTION_CONFIG ~= nil, "Turtle direction not loaded")
  21. end
  22. return TURTLE_DIRECTION_CONFIG
  23. end
  24.  
  25. PROGNAME_UPDATE = "diacraft.update"
  26. PROGNAME_BACKUP = "diacraft.backup"
  27. PROGNAME = "diacraft"
  28. PASTEBIN_CODE = "uUSD1U4k"
  29.  
  30. --Algorithm:
  31. --4x Cobble => Flint
  32. --4x Flint => Clay
  33. --4x Clay => Clay Block
  34. --4x Clay Block => Iron ingot
  35. --8x Iron ingot => Gold
  36. --4x Gold => Diamond
  37.  
  38. -- define metatable for items - we want readable to string
  39. function item(id, meta)
  40. local i = {id = id,
  41. meta = meta,
  42. metaIgnore = false,
  43. minCount = 4,
  44. miniumPatternId = 4
  45. }
  46. return i
  47. end
  48.  
  49. -- Required input:
  50. ITEM = {}
  51. local itemmt = {__index = function(table, key) error("Unexisting key: "..key) end}
  52. setmetatable(ITEM,itemmt)
  53. ------------------------ Farm -----------------
  54. ITEM.COBBLE = item(4, 0)
  55. ITEM.FLINT = item(318, 0)
  56. ITEM.CLAY = item(337, 0)
  57. ITEM.CLAYBLOCK = item(82, 0)
  58.  
  59. ITEM.IRON_INGOT = item(265, 0)
  60. ITEM.IRON_INGOT.minCount = 8
  61. ITEM.IRON_INGOT.miniumPatternId = 8
  62.  
  63. ITEM.GOLD_INGOT = item(266, 0)
  64. ITEM.DIAMOND = item(264, 0)
  65.  
  66. ITEM.MINIUM_STONE = item(27002,0)
  67. ITEM.MINIUM_STONE.metaIgnore = true
  68.  
  69. local craftingOrder = {ITEM.COBBLE, ITEM.FLINT, ITEM.CLAY, ITEM.CLAYBLOCK, ITEM.IRON_INGOT, ITEM.GOLD_INGOT, ITEM.DIAMOND}
  70. for i=1,#craftingOrder - 1 do
  71. craftingOrder[i+1].prevOne = craftingOrder[i]
  72. craftingOrder[i].nextOne = craftingOrder[i+1]
  73. end
  74.  
  75. for _, item in pairs(ITEM) do
  76. item.registered = true
  77. end
  78.  
  79. function itemEquals(item, item2)
  80. --print ("Item1: id="..item.id..", meta="..item.meta)
  81. --print ("Item2: id="..item2.id..", meta="..item2.meta)
  82. -- if first item is not registered, then work with second item as registered
  83. -- if none is registered, it doesn't matter... but ignoreMeta won't work
  84. if not(item.registered) then
  85. item, item2 = item2, item
  86. end
  87.  
  88. if (item.id == item2.id and (item.ignoreMeta or (item.meta == item2.meta))) then
  89. return true
  90. else
  91. return false
  92. end
  93. end
  94.  
  95. function findItem(id, meta)
  96. assert(id ~= nil, "Id is nil")
  97. assert(meta ~= nil, "Meta is nil")
  98. --print("Looking for id="..id..", meta="..meta)
  99. local lookingFor = item(id, meta)
  100. for _,item in pairs(ITEM) do
  101. --print("Comparing ITEM id="..item.id..", meta="..item.meta)
  102. if itemEquals(item, lookingFor) then
  103. return item
  104. end
  105. end
  106. return nil
  107. end
  108.  
  109. local itemChest = peripheral.wrap("front")
  110. local cobbleChest = peripheral.wrap("top")
  111. local miniumChest = peripheral.wrap("bottom")
  112.  
  113. function craftWithMinium(index, craftCount, miniumPatternId)
  114.  
  115. if miniumPatternId == 4 then
  116. itemChest.pushIntoSlot(TURTLE_DIRECTION(), index,craftCount, 1)
  117. itemChest.pushIntoSlot(TURTLE_DIRECTION(), index,craftCount, 4)
  118. itemChest.pushIntoSlot(TURTLE_DIRECTION(), index,craftCount, 5)
  119. itemChest.pushIntoSlot(TURTLE_DIRECTION(), index,craftCount, 8)
  120. elseif miniumPatternId == 8 then
  121. itemChest.pushIntoSlot(TURTLE_DIRECTION(), index,craftCount, 1)
  122. itemChest.pushIntoSlot(TURTLE_DIRECTION(), index,craftCount, 2)
  123. itemChest.pushIntoSlot(TURTLE_DIRECTION(), index,craftCount, 4)
  124. itemChest.pushIntoSlot(TURTLE_DIRECTION(), index,craftCount, 5)
  125. itemChest.pushIntoSlot(TURTLE_DIRECTION(), index,craftCount, 6)
  126. itemChest.pushIntoSlot(TURTLE_DIRECTION(), index,craftCount, 8)
  127. itemChest.pushIntoSlot(TURTLE_DIRECTION(), index,craftCount, 9)
  128. itemChest.pushIntoSlot(TURTLE_DIRECTION(), index,craftCount, 10)
  129. else
  130. error("Unkonwn pattern ID:"..miniumPatternId)
  131. end
  132.  
  133. turtle.select(12)
  134. for i = 1,craftCount do
  135. turtle.craft()
  136. turtle.drop()
  137. end
  138. end
  139.  
  140. function craftSquare(index, craftCount)
  141. itemChest.pushIntoSlot(TURTLE_DIRECTION(), index,craftCount, 0)
  142. itemChest.pushIntoSlot(TURTLE_DIRECTION(), index,craftCount, 1)
  143. itemChest.pushIntoSlot(TURTLE_DIRECTION(), index,craftCount, 4)
  144. itemChest.pushIntoSlot(TURTLE_DIRECTION(), index,craftCount, 5)
  145.  
  146. turtle.select(12)
  147. for i = 1,craftCount do
  148. turtle.craft()
  149. turtle.drop()
  150. end
  151. end
  152.  
  153. function craftWithItem(index)
  154. local slot = itemChest.getStackInSlot(index)
  155. local originalItem
  156. originalItem = findItem(slot.id, slot.dmg)
  157.  
  158. local needMinium = not(itemEquals(ITEM.CLAY, item(slot.id, slot.dmg)))
  159.  
  160. if needMinium then
  161. assert(miniumChest.pushIntoSlot("up", 0, 1, 0) == 1, "minium grab failed")
  162. end
  163.  
  164. while(slot ~= nil and slot.qty >= originalItem.minCount and itemEquals(originalItem, item(slot.id, slot.dmg))) do
  165. local craftCount = math.floor(slot.qty / originalItem.minCount)
  166.  
  167. if needMinium then
  168. craftWithMinium(index, craftCount, originalItem.miniumPatternId)
  169. else
  170. craftSquare(index, craftCount)
  171. end
  172.  
  173. for turtleSlotNum = 2, 12 do
  174. turtle.select(turtleSlotNum)
  175. turtle.drop()
  176. end
  177. itemChest.condense()
  178. slot = itemChest.getStackInSlot(index)
  179. end
  180.  
  181. if needMinium then
  182. assert(miniumChest.pullIntoSlot("up", 0, 1, 0) == 1, "minium return failed")
  183. end
  184. end
  185.  
  186.  
  187. function iterateOverChest()
  188. -- OpenPeripherals index from 0
  189. for i = 0,itemChest.getSizeInventory()-1 do
  190. local slot = itemChest.getStackInSlot(i)
  191. if slot ~= nil then
  192. print("Slot "..i.." contains item: "..slot.name)
  193. local item = findItem(slot.id, slot.dmg)
  194. if (item ~= nil and item.nextOne ~= nil and slot.qty >= item.minCount) then
  195. craftWithItem(i)
  196. end
  197. end
  198. end
  199. end
  200.  
  201. function update()
  202. fs.delete(PROGNAME_UPDATE)
  203. shell.run("pastebin","get",PASTEBIN_CODE, PROGNAME_UPDATE)
  204. if fs.exists(PROGNAME_UPDATE) then
  205. fs.delete(PROGNAME_BACKUP)
  206. fs.move(PROGNAME,PROGNAME_BACKUP)
  207. fs.move(PROGNAME_UPDATE, PROGNAME)
  208. end
  209. end
  210.  
  211. local argv = {...}
  212. if #argv == 1 and argv[1] == "update" then
  213. update()
  214. error()
  215. end
  216.  
  217.  
  218. local slot = miniumChest.getStackInSlot(0)
  219. assert(slot ~= nil and itemEquals(ITEM.MINIUM_STONE, item(slot.id, slot.dmg)) == true, "Chest below us must contain minium stone")
  220.  
  221. while(true) do
  222. cobbleChest.condense()
  223. turtle.select(13)
  224. turtle.suckUp()
  225. turtle.drop()
  226. itemChest.condense()
  227. iterateOverChest()
  228. sleep(0.1)
  229. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement