Advertisement
whaamp

Untitled

Jan 17th, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.58 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,side="down"}}, 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. turtle.select(1)
  89. end
  90.  
  91. function safe_string(text)
  92. print("safe_string(text)")
  93. local new_text = {}
  94. for i = 1, #text do
  95. local val = string.byte(text, i)
  96. new_text[i] = (val > 31 and val < 127) and val or 32
  97. end
  98. return string.char(unpack(new_text))
  99. end
  100.  
  101. function earliest_order(position_1, position_2)
  102. print("earliest_order(position_1, position_2)")
  103. if position_1.y > position_2.y then
  104. return true
  105. elseif position_1.y == position_2.y then
  106. if position_1.z > position_2.z then
  107. return true
  108. elseif position_1.z == position_2.z then
  109. if position_1.x > position_2.x then
  110. return true
  111. elseif position_1.x == position_2.x then
  112. if position_1.side == "up" then
  113. return true
  114. end
  115. end
  116. end
  117. end
  118. end
  119.  
  120. function next_open_position()
  121. print("next_open_position()")
  122. if #open_positions > 1 then
  123. table.sort(open_positions, earliest_order)
  124. return table.remove(open_positions)
  125. else
  126. local position = table.remove(open_positions)
  127. table.insert(open_positions, next_position(position))
  128. return position
  129. end
  130. end
  131.  
  132. function register(stack)
  133. print("register(stack)")
  134. local item = nil
  135. stack.string_id = safe_string(stack.id..stack.dmg..stack.raw_name..stack.display_name)
  136. for _, existing_item in ipairs(items) do
  137. if existing_item.string_id == stack.string_id then
  138. item = existing_item
  139. break
  140. end
  141. end
  142. if not item then
  143. item = {}
  144. item.string_id = stack.string_id
  145. item.damage = stack.dmg
  146. item.max_damage = stack.max_dmg
  147. item.max_stack = stack.max_size
  148. item.recipes = {}
  149. item.quantity = 0
  150. item.position = next_open_position()
  151. item.slot = {}
  152. table.insert(items, item)
  153. end
  154. if item.position == nil then
  155. item.position = next_open_position()
  156. end
  157. save(open_positions, "open_positions")
  158. return item
  159. end
  160.  
  161. function condense_inventory()
  162. print("condense_inventory()")
  163. inventory.condenseItems()
  164. local inventory_items = inventory.getAllStacks()
  165. inventory.swapStacks(#inventory_items, 16)
  166. return inventory_items
  167. end
  168.  
  169. function check_inventory()
  170. print("check_inventory()")
  171. local items_to_sort = {}
  172. local inventory_items = condense_inventory()
  173. for i = 1, #inventory_items - 1 do
  174. table.insert(items_to_sort, register(inventory_items[i].basic()))
  175. end
  176. return items_to_sort
  177. end
  178.  
  179. function distance_away(position)
  180. print("distance_away(position)")
  181. local distance = math.abs(position.y - me.y)
  182. distance = distance + math.abs(me.x) + math.abs(me.z)
  183. distance = distance + math.abs(position.x) + math.abs(position.z)
  184. return distance
  185. end
  186.  
  187. function closest_order(item_1, item_2)
  188. print("closest_order(item_1, item_2)")
  189. return distance_away(item_1.position) > distance_away(item_2.position)
  190. end
  191.  
  192. function give_item_slots(items_to_sort)
  193. print("give_item_slots(items_to_sort)")
  194. for i = 1, #items_to_sort do
  195. table.insert(items_to_sort[i].slot, i)
  196. end
  197. end
  198.  
  199. function deposit_item(item)
  200. print("deposit_item(item)")
  201. go_to(item.position)
  202. for _, slot_number in ipairs(item.slot) do
  203. local amount = inventory.pushItem(item.position.side, slot_number)
  204. item.quantity = item.quantity + amount
  205. end
  206. item.slot = {}
  207. save(items, "items")
  208. end
  209.  
  210. function sort_items(items_to_sort)
  211. print("sort_items(items_to_sort)")
  212. give_item_slots(items_to_sort)
  213. while #items_to_sort > 0 do
  214. table.sort(items_to_sort, closest_order)
  215. local closest_item = items_to_sort[#items_to_sort]
  216. deposit_item(closest_item)
  217. table.remove(items_to_sort)
  218. end
  219. end
  220.  
  221. function begin_depositing()
  222. print("begin_depositing()")
  223. go_to({x=0, y=me.y, z=0})
  224. local at_home = turtle.detectDown()
  225. condense_inventory()
  226. if not at_home then
  227. get_enderchest("deposit")
  228. end
  229. while turtle.suckDown() do end
  230. if not at_home then
  231. if inventory.getStackInSlot(16) then
  232. inventory.pushItem("down", 16)
  233. end
  234. end
  235. chest.condenseItems()
  236. if #chest.getAllStacks() > 0 then
  237. table.insert(queue, 2, begin_depositing)
  238. end
  239. if not at_home then
  240. turtle.select(16)
  241. turtle.digDown()
  242. end
  243. local items_to_sort = check_inventory()
  244. sort_items(items_to_sort)
  245. end
  246.  
  247. function start()
  248. print("start()")
  249. me = {x=0,y=0,z=0}
  250. queue = {}
  251. chest = peripheral.wrap("bottom")
  252. while not chest do
  253. sleep(3)
  254. chest = peripheral.wrap("bottom")
  255. end
  256. inventory = peripheral.wrap("left")
  257. items = load("items")
  258. open_positions = load("open_positions")
  259. end
  260.  
  261. function do_queue()
  262. print("do_queue()")
  263. local i = 1
  264. while i <= #queue do
  265. queue[i]()
  266. i = i + 1
  267. end
  268. queue = {}
  269. go_to({x=0,y=0,z=0})
  270. end
  271.  
  272. start()
  273.  
  274. while true do
  275. command = io.read()
  276. if command == ">" then
  277. table.insert(queue, begin_depositing)
  278. elseif command == "" then
  279. do_queue()
  280. end
  281. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement