Advertisement
whaamp

Untitled

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