camarajohnny

2EnderChest_and_a_turtle_Sorter_V2

Feb 5th, 2016
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.82 KB | None | 0 0
  1. function pickEnder(enderNumber,enderChest) --enderNumber is between 0 and 4096, enderChest is the wrapped chest
  2. b1 = math.floor(enderNumber/ 256) -- find first colour code
  3. b2 = math.floor((enderNumber - (256 *b1)) / 16)
  4. b3 = enderNumber % 16
  5. error = enderChest.setColors(2^b1,2^b2,2^b3)
  6. return error
  7. end -- ends pickEnder()
  8.  
  9. function readLookUp(fileName)
  10. if fs.exists(fileName) then
  11.  
  12. local file = fs.open(fileName,"r")
  13. local data = file.readAll()
  14. file.close()
  15. return textutils.unserialize(data)
  16. else
  17.  
  18. local data = {}
  19. return data
  20. end
  21. end
  22.  
  23. function saveLookUp(data,fileName)
  24. local file = fs.open(fileName, "w")
  25. file.write(textutils.serialize(data))
  26. file.close()
  27. end
  28.  
  29. function inToOut(inSort, outSort, inv, amount)
  30. inSort.pushItem("west",inv, amount)
  31. outSort.pullItem("east",1,amount)
  32. end
  33. function findHome(inSort, outSort, inv, amount, outBox)-- this function figures out where to send items and deals with them. outBox is a table with a list of possible outputs
  34. local done = false --- this is triggered to true when the item finds a home
  35. local countLoop = 0
  36. local itemTable = {}
  37. while (done == false) do
  38. countLoop = countLoop + 1
  39. if countLoop > #outBox then
  40. print("Things are backed up")
  41. sleep(5)
  42. countLoop = 1
  43. end
  44. if outBox[countLoop] == -1 then
  45. inSort.destroyStack(inv)
  46. done = true
  47. else
  48. pickEnder(outBox[countLoop],outSort)
  49. outSort.condenseItems()
  50. itemTable = outSort.getAllStacks()
  51. if #itemTable < outSort.getInventorySize() then -- if the box isn't full
  52. inToOut(inSort, outSort, inv, amount)
  53. done = true
  54. end
  55. end
  56.  
  57. end
  58. end
  59. -- function assumes garbage is north
  60. function addToScrap(ScrapAddSort,data,outSort) --fixed
  61. local itemsMoved = 0
  62. local ana
  63. local addItem
  64. pickEnder(1365,ScrapAddSort) -- input from garbage can is 1365
  65. pickEnder(1364,outSort)
  66.  
  67.  
  68. for inv = 1, ScrapAddSort.getInventorySize() do
  69. ana = ScrapAddSort.getStackInSlot(inv)
  70. addItem = 1
  71. if ana == nil then
  72. addItem = 0
  73. else
  74.  
  75. for i = 1, #data, 1 do
  76. if ana.id == data[i].id then
  77. addItem = 2
  78. end
  79. end
  80. if addItem == 1 then
  81. -- inserts entry into table
  82. table.insert(data,{raw_name = ana.raw_name, id = ana.id,name = ana.name, outBox = {1364,-1}})
  83. saveLookUp(data,"GarbageList")
  84. elseif addItem > 1 then
  85. findHome(ScrapAddSort, outSort, inv, ana.qty, data[#data].outBox)
  86. --inToOut(ScrapAddSort, outSort, inv, ana.qty)
  87. itemsMoved = itemsMoved + 1
  88. end
  89. end
  90.  
  91. end
  92. return itemsMoved, data
  93. end
  94.  
  95. -- function for furnace
  96. function addToFurnace(fAddSort,oldData) --fixed
  97. local itemsMoved = 0
  98. local ana
  99. local addItem
  100. local fdata ={}
  101. local inChestNumber = 3822 -- red red red for ender chest input to furnace
  102. local outChestNumber = 3809 -- red red orange for ender chest output to furnace
  103. local outSecondChestNumber = 4091 -- second output if furnace is backed up
  104. pickEnder(inChestNumber,fAddSort)
  105. for inv = 1, fAddSort.getInventorySize() do
  106. ana = fAddSort.getStackInSlot(inv)
  107. addItem = 1
  108. if ana == nil then
  109. addItem = 0
  110. else
  111. if #oldData ~= nil then
  112. for i = 1, #oldData, 1 do
  113. if ana.id == oldData[i].id then
  114. addItem = 2
  115. end
  116. end
  117. end
  118. if addItem > 0 then
  119. -- inserts entry into table
  120. table.insert(oldData,{raw_name = ana.raw_name, id = ana.id,name = ana.name, outBox = {outChestNumber,outSecondChestNumber}})
  121. if addItem == 1 then
  122. itemsMoved = itemsMoved + 1
  123. end
  124. end
  125. end
  126.  
  127. end
  128. return itemsMoved, oldData
  129. end
  130.  
  131. -- this function assumes garbage is "west" furnace is east and master sorter is north
  132. function comp(compSort,outSort, data, inputNumbers ) --fixed to work with chest as peripheral -- compSort is input, data is a table with all the list, outSort is the output chest, inputNumbers is a table with a list of input chest to check
  133. local itemsMoved = 0
  134. local ana
  135. local notGarbage
  136. local j, k = 1 -- used to count
  137. local defOut = {4091} --default output chest number
  138. for i = 1, #inputNumbers do --- do this for every input chest
  139. pickEnder(inputNumbers[i],compSort) -- sets the input ender chest
  140.  
  141. for inv = 1, compSort.getInventorySize() do -- scans through the chest on slot at a time
  142. ana = compSort.getStackInSlot(inv)
  143.  
  144. if ana ~= nil then -- if the slot is not empty
  145. notGarbage = 2
  146. j = 1
  147. while (notGarbage == 2) do --j = 1, #data do -- scan through each list one at a time
  148. k = 1
  149. notGarbage = 1
  150. while (notGarbage == 1) do --k = 1, #data[j] -- scan through each item in that list
  151. if data[j][k] ~= nil then
  152. if (data[j][k].raw_name ~= nil) then
  153. if (data[j][k].raw_name == ana.raw_name) then -- if the list has a raw name use that first other wise use id
  154. findHome(compSort, outSort, inv, ana.qty, data[j][k].outBox)
  155. itemsMoved = itemsMoved + 1
  156. notGarbage = 0
  157. end
  158. elseif (ana.id == data[j][k].id) and (data[j][k].raw_name == nil) then
  159. findHome(compSort, outSort, inv, ana.qty, data[j][k].outBox)
  160. itemsMoved = itemsMoved + 1
  161. notGarbage = 0
  162. end
  163. end
  164. k = k + 1
  165. if (k > #data[j]) then notGarbage = 2 end
  166. end
  167. j = j + 1
  168. if (j > #data) then notGarbage = 3 end
  169. end
  170. if notGarbage == 3 then -- this should only happen if item is not on list
  171. findHome(compSort, outSort, inv, ana.qty, defOut)
  172. itemsMoved = itemsMoved + 1
  173. end
  174. end
  175. end
  176. end
  177. return itemsMoved
  178. end
  179. --globals
  180.  
  181. local inSort = peripheral.wrap("left")
  182. local outSort = peripheral.wrap("right")
  183. local sortKey = {} --- this is a multi dimensional table that holds a number of list to be used to compare items too
  184. local inputChestNumbers = {4095,4093}
  185. local isPresent = 1
  186. local temp = 1
  187. sortKey[1] = readLookUp("GarbageList") -- first item in the list is garbage
  188. sortKey[2] = {}
  189. sortKey[3] = readLookUp("seedList")
  190. -- main program
  191. while (true) do
  192. while isPresent ~= 0 do
  193. temp, sortKey[2] = addToFurnace(inSort, sortKey[2]) --- second list is things to go to furnace
  194. isPresent = 0
  195. temp = comp(inSort, outSort, sortKey, inputChestNumbers) --
  196. isPresent = isPresent + temp
  197. temp = addToScrap(inSort,sortKey[1], outSort)
  198. isPresent = isPresent + temp
  199. sleep(0.4)
  200. end
  201. isPresent = 1
  202. end
  203. print("done")
Add Comment
Please, Sign In to add comment