Advertisement
whaamp

Untitled

Jan 17th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.49 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. 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(open_positions, 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 = {}
  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. return item
  158. end
  159.  
  160. function condense_inventory()
  161. print("condense_inventory()")
  162. inventory.condenseItems()
  163. local inventory_items = inventory.getAllStacks()
  164. inventory.swapStacks(#inventory_items, 16)
  165. return inventory_items
  166. end
  167.  
  168. function check_inventory()
  169. print("check_inventory()")
  170. local items_to_sort = {}
  171. local inventory_items = condense_inventory()
  172. for i = 1, #inventory_items - 1 do
  173. table.insert(items_to_sort, register(inventory_items[i].basic()))
  174. end
  175. return items_to_sort
  176. end
  177.  
  178. function distance_away(position)
  179. print("distance_away(position)")
  180. local distance = math.abs(position.y - me.y)
  181. distance = distance + math.abs(me.x) + math.abs(me.z)
  182. distance = distance + math.abs(position.x) + math.abs(position.z)
  183. return distance
  184. end
  185.  
  186. function closest_order(item_1, item_2)
  187. print("closest_order(item_1, item_2)")
  188. return distance_away(item_1.position) > distance_away(item_2.position)
  189. end
  190.  
  191. function give_item_slots(items_to_sort)
  192. print("give_item_slots(items_to_sort)")
  193. for i = 1, #items_to_sort do
  194. table.insert(items_to_sort[i].slot, i)
  195. end
  196. end
  197.  
  198. function deposit_item(item)
  199. print("deposit_item(item)")
  200. go_to(item.position)
  201. for _, slot_number in ipairs(item.slot) do
  202. local amount = inventory.pushItem(item.position.side, slot_number)
  203. item.quantity = item.quantity + amount
  204. end
  205. item.slot = {}
  206. save(items, "items")
  207. end
  208.  
  209. function sort_items(items_to_sort)
  210. print("sort_items(items_to_sort)")
  211. give_item_slots(items_to_sort)
  212. while #items_to_sort > 0 do
  213. table.sort(items_to_sort, closest_order)
  214. local closest_item = items_to_sort[#items_to_sort]
  215. deposit_item(closest_item)
  216. table.remove(items_to_sort)
  217. end
  218. end
  219.  
  220. function begin_depositing()
  221. print("begin_depositing()")
  222. go_to({x=0, y=me.y, z=0})
  223. local at_home = turtle.detectDown()
  224. condense_inventory()
  225. if not at_home then
  226. get_enderchest("deposit")
  227. end
  228. turtle.select(1)
  229. while turtle.suckDown() do end
  230. if not at_home then
  231. if #condense_inventory == 16 then
  232. turtle.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.digDown()
  241. end
  242. local items_to_sort = check_inventory()
  243. sort_items(items_to_sort)
  244. end
  245.  
  246. function start()
  247. print("start()")
  248. me = {x=0,y=0,z=0}
  249. queue = {}
  250. while not chest do
  251. chest = peripheral.wrap("bottom")
  252. sleep(3)
  253. end
  254. inventory = peripheral.wrap("left")
  255. items = load("items")
  256. open_positions = load("open_positions")
  257. end
  258.  
  259. function do_queue()
  260. print("do_queue()")
  261. local i = 1
  262. while i <= #queue do
  263. queue[i]()
  264. i = i + 1
  265. end
  266. queue = {}
  267. end
  268.  
  269. start()
  270.  
  271. while true do
  272. command = io.read()
  273. if command == ">" then
  274. table.insert(queue, begin_depositing)
  275. elseif command == "" then
  276. do_queue()
  277. end
  278. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement