Advertisement
whaamp

Untitled

Jan 17th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.91 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,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. me = {x=0,y=0,z=0}
  233. queue = {}
  234. chest = peripheral.wrap("bottom")
  235. while not chest do
  236. sleep(3)
  237. chest = peripheral.wrap("bottom")
  238. end
  239. inventory = peripheral.wrap("left")
  240. items = load("items")
  241. open_positions = load("open_positions")
  242. end
  243.  
  244. function spit()
  245. for i=1, 15 do
  246. turtle.select(i)
  247. turtle.drop()
  248. end
  249. end
  250.  
  251. function fetch(item, quantity)
  252. if quantity > item.quantity then
  253. print("Too many")
  254. else
  255. go_to(item.position)
  256. turtle.suck(quantity)
  257. local n = contains(item)
  258. items[n].quantity = items[n].quantity - quantity
  259. save(items, "items")
  260. go_to({x=0,y=0,z=0})
  261. spit()
  262. end
  263. end
  264.  
  265. function do_queue()
  266. local i = 1
  267. while i <= #queue do
  268. queue[i][1](queue[i][2],queue[i][3])
  269. i = i + 1
  270. end
  271. queue = {}
  272. go_to({x=0,y=0,z=0})
  273. end
  274.  
  275. function search(s1, s2)
  276. s1 = string.lower(string.gsub(string.gsub(s1, " ", ""), "_", ""))
  277. s2 = string.lower(string.gsub(string.gsub(s2, " ", ""), "_", ""))
  278. return string.find(s1, s2)
  279. end
  280.  
  281. function slice(tbl, first, last)
  282. local sliced = {}
  283. for i = first, last do
  284. table.insert(sliced, tbl[i])
  285. end
  286. return sliced
  287. end
  288.  
  289. function show_results()
  290. term.clear()
  291. term.setCursorPos(1,1)
  292. for i, result in pairs(slice(results, 1, 12)) do
  293. print("#"..i.." "..result.display_name..": "..result.quantity)
  294. end
  295. term.setCursorPos(1,13)
  296. end
  297.  
  298. function start_search(search_term)
  299. results = {}
  300. for _, item in ipairs(items) do
  301. r = search(item.display_name, search_term)
  302. if r and item.quantity > 0 then
  303. table.insert(results, item)
  304. end
  305. end
  306. show_results()
  307. end
  308.  
  309. start()
  310.  
  311. while true do
  312. command = io.read()
  313. if command == ">" or command == "sort" then
  314. table.insert(queue, {begin_depositing})
  315. elseif command == "" then
  316. do_queue()
  317. elseif tonumber(command) and #results > 0 and tonumber(command) <= #results then
  318. n = tonumber(command)
  319. print(results[n].display_name..": "..results[n].quantity)
  320. print("How many?")
  321. amount = io.read()
  322. while not tonumber(amount) or amount == "x" do
  323. amount = io.read()
  324. end
  325. if amount == "x" then
  326. show_results()
  327. else
  328. table.insert(queue, {fetch, results[n], amount})
  329. end
  330. else
  331. start_search(command)
  332. end
  333. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement