Advertisement
whaamp

Untitled

Jan 16th, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.49 KB | None | 0 0
  1. function save(tbl, name)
  2. local file = fs.open(name, "w")
  3. file.write(textutils.serialize(tbl))
  4. file.close()
  5. end
  6.  
  7. function load(name)
  8. local file = fs.open(name, "r")
  9. if not file then
  10. if name == "open_positions" then
  11. save({{x=-5,y=2,z=-5}}, name)
  12. else
  13. save({}, name)
  14. return load(name)
  15. else
  16. local data = file.readAll()
  17. file.close()
  18. return textutils.unserialize(data)
  19. end
  20. end
  21.  
  22. function go_to(target)
  23. if me.y ~= target.y then
  24. go_to({x = 0, y = me.y, z = 0})
  25. while me.y < target.y do
  26. if turtle.up() then me.y = me.y + 1 end
  27. end
  28. while me.y > target.y do
  29. if turtle.down() then me.y = me.y - 1 end
  30. end
  31. end
  32. if me.x < target.x then
  33. turtle.turnRight()
  34. while me.x < target.x do
  35. if turtle.forward() then me.x = me.x + 1 end
  36. end
  37. turtle.turnLeft()
  38. end
  39. if me.x > target.x then
  40. turtle.turnLeft()
  41. while me.x > target.x do
  42. if turtle.forward() then me.x = me.x - 1 end
  43. end
  44. turtle.turnRight()
  45. end
  46. while me.z < target.z do
  47. if turtle.forward() then me.z = me.z + 1 end
  48. end
  49. while me.z > target.z do
  50. if turtle.back() then me.z = me.z - 1 end
  51. end
  52. end
  53.  
  54. function next_position(position)
  55. local new_position = {x = position.x, y = position.y, z = position.z, side = position.side}
  56. if position.side == "down" then
  57. new_position.side == "up"
  58. else
  59. new_position.side == "down"
  60. if position.x == 5 then
  61. new_position.x = -5
  62. if position.z == 5 then
  63. new_position.z = -5
  64. new_position.y = position.y + 3
  65. else
  66. new_position.z = position.z + 1
  67. end
  68. else
  69. if position.x == -1 and position.z == 0 then
  70. new_position.x = position.x + 2
  71. else
  72. new_position.x = position.x + 1
  73. end
  74. end
  75. end
  76. return new_position
  77. end
  78.  
  79. function get_enderchest(group)
  80. turtle.select(16)
  81. turtle.placeDown()
  82. end
  83.  
  84. function safe_string(text)
  85. local new_text = {}
  86. for i = 1, #text do
  87. local val = string.byte(text, i)
  88. new_text[i] = (val > 31 and val < 127) and val or 32
  89. end
  90. return string.char(unpack(new_text))
  91. end
  92.  
  93. function earliest_order(position_1, position_2)
  94. if position_1.y > position_2.y then
  95. return true
  96. elseif position_1.y == position_2.y then
  97. if position_1.z > position_2.z then
  98. return true
  99. elseif position_1.z == position_2.z then
  100. if position_1.x > position_2.x then
  101. return true
  102. elseif position_1.x == position_2.x then
  103. if position_1.side == "up" then
  104. return true
  105. end
  106. end
  107. end
  108. end
  109. end
  110.  
  111. function next_open_position()
  112. if #open_positions > 1 then
  113. table.sort(open_positions, earliest_order)
  114. return table.remove(open_positions)
  115. else
  116. local position = table.remove(open_positions)
  117. table.insert(next_position(position))
  118. return position
  119. end
  120. end
  121.  
  122. function register(stack)
  123. local item = nil
  124. stack.string_id = safe_string(stack.id..stack.dmg..stack.raw_name..stack.display_name)
  125. for _, existing_item in ipairs(items) do
  126. if existing_item.string_id == stack.string_id then
  127. item = existing_item
  128. break
  129. end
  130. end
  131. if not item then
  132. item = {}
  133. item.string_id = stack.string_id
  134. item.damage = stack.dmg
  135. item.max_damage = stack.max_dmg
  136. item.max_stack = stack.max_size
  137. item.recipes = {}
  138. item.quantity = 0
  139. item.position = next_open_position()
  140. item.slot = nil
  141. table.insert(items, item)
  142. end
  143. if item.position == nil then
  144. item.position = next_open_position()
  145. end
  146. save(open_positions, "open_positions")
  147. end
  148.  
  149. function check_inventory()
  150. local items_to_sort = {}
  151. local inventory_items = inventory.getAllStacks()
  152. for i = 1, #inventory_items - 1 do
  153. table.insert(items_to_sort, register(inventory_items[i].basic()))
  154. end
  155. return items_to_sort
  156. end
  157.  
  158. function distance_away(position)
  159. local distance = math.abs(position.y - me.y)
  160. distance = distance + math.abs(me.x) + math.abs(me.z)
  161. distance = distance + math.abs(position.x) + math.abs(position.z)
  162. return distance
  163. end
  164.  
  165. function closest_order(item_1, item_2)
  166. return distance_away(item_1.position) > distance_away(item_2.position)
  167. end
  168.  
  169. function give_item_slots(items_to_sort)
  170. for i = 1, #items_to_sort do
  171. items_to_sort[i].slot = i
  172. end
  173. end
  174.  
  175. function deposit_item(item)
  176. go_to(item.position)
  177. local amount = inventory.push(item.position.side, item.slot)
  178. item.quantity = item.quantity + amount
  179. save(items, "items")
  180. end
  181.  
  182. function sort_items(items_to_sort)
  183. give_item_slots(items_to_sort)
  184. while #items_to_sort > 0 do
  185. local closest_item = table.sort(items_to_sort, closest_order)[#items_to_sort]
  186. deposit_item(closest_item)
  187. table.remove(items_to_sort)
  188. end
  189. end
  190.  
  191. function condense_inventory()
  192. inventory.condenseItems()
  193. inventory.swapStacks(#inventory.getAllStacks(), 16)
  194. end
  195.  
  196. function begin_depositing()
  197. go_to({x=0, y=me.y, z=0})
  198. local at_home = turtle.detectDown()
  199. condense_inventory()
  200. if not at_home then
  201. get_enderchest("deposit")
  202. end
  203. while turtle.suckDown() do end
  204. chest.condenseItems()
  205. if #chest.getAllStacks() > 0 then
  206. table.insert(queue, 2, begin_depositing)
  207. end
  208. if not at_home then
  209. turtle.digDown()
  210. end
  211. local items_to_sort = check_inventory()
  212. sort_items(items_to_sort)
  213. end
  214.  
  215. function start()
  216. me = {x=0,y=0,z=0}
  217. queue = {}
  218. while not chest do
  219. chest = peripheral.wrap("bottom")
  220. sleep(3)
  221. end
  222. inventory = peripheral.wrap("right")
  223. items = load("items")
  224. open_positions = load("open_positions")
  225. end
  226.  
  227. function do_queue()
  228. local i = 1
  229. while i <= #queue do
  230. queue[i]()
  231. i = i + 1
  232. end
  233. end
  234.  
  235. start()
  236.  
  237. while true do
  238. command = io.read()
  239. if command == ">" then
  240. table.insert(queue, begin_depositing)
  241. elseif command == "" then
  242. do_queue()
  243. end
  244. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement