Advertisement
whaamp

Untitled

Jan 20th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.14 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=3,z=-5,side="down"}}, name)
  12. else
  13. save({}, name)
  14. end
  15. return load(name)
  16. else
  17. local data = file.readAll()
  18. file.close()
  19. return textutils.unserialize(data)
  20. end
  21. end
  22.  
  23. function go_to(target)
  24. if me.y ~= target.y then
  25. go_to({x = 0, y = me.y, z = 0})
  26. while me.y < target.y do
  27. if turtle.up() then me.y = me.y + 1 end
  28. end
  29. while me.y > target.y do
  30. if turtle.down() then me.y = me.y - 1 end
  31. end
  32. end
  33. if me.x < target.x then
  34. turtle.turnRight()
  35. while me.x < target.x do
  36. if turtle.forward() then me.x = me.x + 1 end
  37. end
  38. turtle.turnLeft()
  39. end
  40. if me.x > target.x then
  41. turtle.turnLeft()
  42. while me.x > target.x do
  43. if turtle.forward() then me.x = me.x - 1 end
  44. end
  45. turtle.turnRight()
  46. end
  47. while me.z < target.z do
  48. if turtle.forward() then me.z = me.z + 1 end
  49. end
  50. while me.z > target.z do
  51. if turtle.back() then me.z = me.z - 1 end
  52. end
  53. end
  54.  
  55. function next_position(position)
  56. local new_position = {x = position.x, y = position.y, z = position.z, side = position.side}
  57. if position.side == "down" then
  58. new_position.side = "up"
  59. else
  60. new_position.side = "down"
  61. if position.x == 5 then
  62. new_position.x = -5
  63. if position.z == 5 then
  64. new_position.z = -5
  65. new_position.y = position.y + 3
  66. else
  67. new_position.z = position.z + 1
  68. end
  69. else
  70. if position.x == -1 and position.z == 0 then
  71. new_position.x = position.x + 2
  72. else
  73. new_position.x = position.x + 1
  74. end
  75. end
  76. end
  77. return new_position
  78. end
  79.  
  80. function get_enderchest(group)
  81. turtle.select(16)
  82. turtle.placeDown()
  83. turtle.select(1)
  84. end
  85.  
  86. function safe_string(text)
  87. local new_text = {}
  88. for i = 1, #text do
  89. local val = string.byte(text, i)
  90. new_text[i] = (val > 31 and val < 127) and val or 32
  91. end
  92. return string.char(unpack(new_text))
  93. end
  94.  
  95. function earliest_order(position_1, position_2)
  96. if position_1.y > position_2.y then
  97. return true
  98. elseif position_1.y == position_2.y then
  99. if position_1.z > position_2.z then
  100. return true
  101. elseif position_1.z == position_2.z then
  102. if position_1.x > position_2.x then
  103. return true
  104. elseif position_1.x == position_2.x then
  105. if position_1.side == "up" then
  106. return true
  107. end
  108. end
  109. end
  110. end
  111. end
  112.  
  113. function next_open_position()
  114. if #open_positions > 1 then
  115. table.sort(open_positions, earliest_order)
  116. return table.remove(open_positions)
  117. else
  118. local position = table.remove(open_positions)
  119. table.insert(open_positions, next_position(position))
  120. return position
  121. end
  122. end
  123.  
  124. function register(stack)
  125. local item = nil
  126. stack.string_id = safe_string(stack.id..stack.dmg..stack.raw_name..stack.display_name)
  127. for _, existing_item in ipairs(items) do
  128. if existing_item.string_id == stack.string_id then
  129. item = existing_item
  130. break
  131. end
  132. end
  133. if not item then
  134. item = {}
  135. item.string_id = stack.string_id
  136. item.damage = stack.dmg
  137. item.max_damage = stack.max_dmg
  138. item.max_stack = stack.max_size
  139. item.recipes = {}
  140. item.quantity = 0
  141. item.position = next_open_position()
  142. item.slot = {}
  143. item.display_name = stack.display_name
  144. table.insert(items, item)
  145. end
  146. if item.position == nil then
  147. item.position = next_open_position()
  148. end
  149. save(open_positions, "open_positions")
  150. return item
  151. end
  152.  
  153. function condense_inventory()
  154. inventory.condenseItems()
  155. local inventory_items = inventory.getAllStacks()
  156. inventory.swapStacks(#inventory_items, 16)
  157. return inventory_items
  158. end
  159.  
  160. function check_inventory()
  161. local items_to_sort = {}
  162. local inventory_items = condense_inventory()
  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. local distance = math.abs(position.y - me.y)
  171. distance = distance + math.abs(me.x) + math.abs(me.z)
  172. distance = distance + math.abs(position.x) + math.abs(position.z)
  173. return distance
  174. end
  175.  
  176. function closest_order(item_1, item_2)
  177. return distance_away(item_1.position) > distance_away(item_2.position)
  178. end
  179.  
  180. function give_item_slots(items_to_sort)
  181. for i = 1, #items_to_sort do
  182. table.insert(items_to_sort[i].slot, i)
  183. end
  184. end
  185.  
  186. function deposit_item(item)
  187. go_to(item.position)
  188. for _, slot_number in ipairs(item.slot) do
  189. local amount = inventory.pushItem(item.position.side, slot_number)
  190. item.quantity = item.quantity + amount
  191. end
  192. item.slot = {}
  193. save(items, "items")
  194. end
  195.  
  196. function sort_items(items_to_sort)
  197. give_item_slots(items_to_sort)
  198. while #items_to_sort > 0 do
  199. table.sort(items_to_sort, closest_order)
  200. local closest_item = items_to_sort[#items_to_sort]
  201. deposit_item(closest_item)
  202. table.remove(items_to_sort)
  203. end
  204. end
  205.  
  206. function begin_depositing()
  207. go_to({x=0, y=me.y, z=0})
  208. local at_home = turtle.detectDown()
  209. condense_inventory()
  210. if not at_home then
  211. get_enderchest("deposit")
  212. end
  213. while turtle.suckDown() do end
  214. if not at_home then
  215. if inventory.getStackInSlot(16) then
  216. inventory.pushItem("down", 16)
  217. end
  218. end
  219. chest.condenseItems()
  220. if #chest.getAllStacks() > 0 then
  221. table.insert(queue, 2, {begin_depositing})
  222. end
  223. if not at_home then
  224. turtle.select(16)
  225. turtle.digDown()
  226. end
  227. local items_to_sort = check_inventory()
  228. sort_items(items_to_sort)
  229. end
  230.  
  231. function start()
  232. home = {x=0,y=0,z=0}
  233. me = {x=home.x,y=home.y,z=home.z}
  234. queue = {}
  235. chest = peripheral.wrap("bottom")
  236. while not chest do
  237. sleep(3)
  238. chest = peripheral.wrap("bottom")
  239. end
  240. inventory = peripheral.wrap("left")
  241. items = load("items")
  242. open_positions = load("open_positions")
  243. end
  244.  
  245. function spit()
  246. for i=1, 15 do
  247. turtle.select(i)
  248. turtle.drop()
  249. end
  250. end
  251.  
  252. function fetch(item, quantity)
  253. if quantity > item.quantity then
  254. print("Too many")
  255. else
  256. go_to(item.position)
  257. local amount = inventory.pullItem(item.position.side, 2, quantity)
  258. item.quantity = item.quantity - amount
  259. if item.quantity == 0 then
  260. table.insert(open_positions, item.position)
  261. save(open_positions, "open_positions")
  262. item.position = nil
  263. end
  264. save(items, "items")
  265. go_to(home)
  266. spit()
  267. end
  268. end
  269.  
  270. function do_queue()
  271. local i = 1
  272. while i <= #queue do
  273. queue[i][1](queue[i][2],queue[i][3])
  274. i = i + 1
  275. end
  276. queue = {}
  277. go_to(home)
  278. end
  279.  
  280. function search(s1, s2)
  281. local s1 = string.lower(string.gsub(string.gsub(s1, " ", ""), "_", ""))
  282. local s2 = string.lower(string.gsub(string.gsub(s2, " ", ""), "_", ""))
  283. return string.find(s1, s2)
  284. end
  285.  
  286. function slice(tbl, first, last)
  287. local sliced = {}
  288. for i = first, last do
  289. table.insert(sliced, tbl[i])
  290. end
  291. return sliced
  292. end
  293.  
  294. function show_results()
  295. term.clear()
  296. term.setCursorPos(1,1)
  297. for i, result in pairs(slice(results, 1, 12)) do
  298. print("#"..i.." "..result.display_name..": "..result.quantity)
  299. end
  300. term.setCursorPos(1,13)
  301. end
  302.  
  303. function start_search(search_term)
  304. results = {}
  305. for _, item in ipairs(items) do
  306. r = search(item.display_name, search_term)
  307. if r and item.quantity > 0 then
  308. table.insert(results, item)
  309. end
  310. end
  311. show_results()
  312. end
  313.  
  314. start()
  315.  
  316. while true do
  317. command = io.read()
  318. if command == ">" or command == "sort" then
  319. table.insert(queue, {begin_depositing})
  320. elseif command == "" then
  321. do_queue()
  322. elseif tonumber(command) and #results > 0 and tonumber(command) <= #results then
  323. n = tonumber(command)
  324. print(results[n].display_name..": "..results[n].quantity)
  325. print("How many?")
  326. amount = io.read()
  327. while not tonumber(amount) or amount == "x" do
  328. amount = io.read()
  329. end
  330. if amount == "x" then
  331. show_results()
  332. else
  333. table.insert(queue, {fetch, results[n], tonumber(amount)})
  334. end
  335. else
  336. start_search(command)
  337. end
  338. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement