Advertisement
whaamp

Untitled

Jan 20th, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.39 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 clear_search_bar()
  24. term.setTextColor(colors.cyan)
  25. paintutils.drawFilledBox(1,screen_height,screen_width,screen_height,colors.gray)
  26. term.setCursorPos(1,screen_height)
  27. end
  28.  
  29. function show_title(title)
  30. paintutils.drawLine(1,1,screen_width,1,colors.cyan)
  31. term.setCursorPos(1,1)
  32. term.write(title)
  33. clear_search_bar()
  34. end
  35.  
  36. function empty_inventory(side)
  37. for i=1, 15 do
  38. inventory.pushItem(side, i)
  39. end
  40. end
  41.  
  42. function condense_inventory()
  43. inventory.condenseItems()
  44. local inventory_items = inventory.getAllStacks()
  45. inventory.swapStacks(#inventory_items, 16)
  46. return inventory_items
  47. end
  48.  
  49. function next_position(position)
  50. local new_position = {x = position.x, y = position.y, z = position.z, side = position.side}
  51. if position.side == "down" then
  52. new_position.side = "up"
  53. else
  54. new_position.side = "down"
  55. if position.x == 5 then
  56. new_position.x = -5
  57. if position.z == 5 then
  58. new_position.z = -5
  59. new_position.y = position.y + 3
  60. else
  61. new_position.z = position.z + 1
  62. end
  63. else
  64. if position.x == -1 and position.z == 0 then
  65. new_position.x = position.x + 2
  66. else
  67. new_position.x = position.x + 1
  68. end
  69. end
  70. end
  71. return new_position
  72. end
  73.  
  74. function earliest_order(position_1, position_2)
  75. if position_1.y > position_2.y then
  76. return true
  77. elseif position_1.y == position_2.y then
  78. if position_1.z > position_2.z then
  79. return true
  80. elseif position_1.z == position_2.z then
  81. if position_1.x > position_2.x then
  82. return true
  83. elseif position_1.x == position_2.x then
  84. if position_1.side == "up" then
  85. return true
  86. end
  87. end
  88. end
  89. end
  90. end
  91.  
  92. function next_open_position()
  93. if #open_positions > 1 then
  94. table.sort(open_positions, earliest_order)
  95. return table.remove(open_positions)
  96. else
  97. local position = table.remove(open_positions)
  98. table.insert(open_positions, next_position(position))
  99. return position
  100. end
  101. end
  102.  
  103. function safe_string(text)
  104. local new_text = {}
  105. for i = 1, #text do
  106. local val = string.byte(text, i)
  107. new_text[i] = (val > 31 and val < 127) and val or 32
  108. end
  109. return string.char(unpack(new_text))
  110. end
  111.  
  112. function register_item(stack)
  113. local string_id = stack.id..stack.dmg..stack.raw_name..stack.display_name
  114. stack.string_id = safe_string(string_id)
  115. for _, existing_item in ipairs(items) do
  116. if existing_item.string_id == stack.string_id then
  117. local item = existing_item
  118. break
  119. end
  120. end
  121. if not item then
  122. item = {}
  123. item.string_id = stack.string_id
  124. item.damage = stack.dmg
  125. item.max_damage = stack.max_dmg
  126. item.max_stack = stack.max_size
  127. item.recipes = {}
  128. item.quantity = 0
  129. item.position = next_open_position()
  130. item.slot = {}
  131. item.display_name = stack.display_name
  132. item.mod_name = stack.mod
  133. table.insert(items, item)
  134. end
  135. if item.position == nil then
  136. item.position = next_open_position()
  137. end
  138. return item
  139. end
  140.  
  141. function check_inventory(inventory_items)
  142. local items_to_store = {}
  143. for i = 1, #inventory_items - 1 do
  144. table.insert(items_to_store, register_item(inventory_items[i].basic()))
  145. end
  146. return items_to_store
  147. end
  148.  
  149. function distance_away(position)
  150. local distance = math.abs(position.y - me.y)
  151. distance = distance + math.abs(me.x) + math.abs(me.z)
  152. distance = distance + math.abs(position.x) + math.abs(position.z)
  153. return distance
  154. end
  155.  
  156. function closest_order(item_1, item_2)
  157. return distance_away(item_1.position) > distance_away(item_2.position)
  158. end
  159.  
  160. function give_item_slots(items_to_store)
  161. for i = 1, #items_to_store do
  162. table.insert(items_to_store[i].slot, i)
  163. end
  164. end
  165.  
  166. function go_to(target)
  167. if me.y ~= target.y then
  168. go_to({x = 0, y = me.y, z = 0})
  169. while me.y < target.y do
  170. if turtle.up() then me.y = me.y + 1 end
  171. end
  172. while me.y > target.y do
  173. if turtle.down() then me.y = me.y - 1 end
  174. end
  175. end
  176. if me.x < target.x then
  177. turtle.turnRight()
  178. while me.x < target.x do
  179. if turtle.forward() then me.x = me.x + 1 end
  180. end
  181. turtle.turnLeft()
  182. end
  183. if me.x > target.x then
  184. turtle.turnLeft()
  185. while me.x > target.x do
  186. if turtle.forward() then me.x = me.x - 1 end
  187. end
  188. turtle.turnRight()
  189. end
  190. while me.z < target.z do
  191. if turtle.forward() then me.z = me.z + 1 end
  192. end
  193. while me.z > target.z do
  194. if turtle.back() then me.z = me.z - 1 end
  195. end
  196. end
  197.  
  198. function deposit_item(item)
  199. go_to(item.position)
  200. for _, slot_number in ipairs(item.slot) do
  201. local amount = inventory.pushItem(item.position.side, slot_number)
  202. item.quantity = item.quantity + amount
  203. end
  204. item.slot = {}
  205. save(items, "items")
  206. end
  207.  
  208. function store_items(items_to_store)
  209. give_item_slots(items_to_store)
  210. while #items_to_store > 0 do
  211. table.sort(items_to_store, closest_order)
  212. local closest_item = items_to_store[#items_to_store]
  213. deposit_item(closest_item)
  214. table.remove(items_to_store)
  215. end
  216. end
  217.  
  218. function store_inventory()
  219. show_title("Storing Inventory")
  220. inventory_items = condense_inventory()
  221. local items_to_store = check_inventory(inventory_items)
  222. store_items(items_to_store)
  223. end
  224.  
  225. function search(s1, s2)
  226. local s1 = string.lower(string.gsub(string.gsub(s1, " ", ""), "_", ""))
  227. local s2 = string.lower(string.gsub(string.gsub(s2, " ", ""), "_", ""))
  228. return string.find(s1, s2)
  229. end
  230.  
  231. function result_order(r1, r2)
  232. local alphabetical = string.lower(r1.display_name) < string.lower(r2.display_name)
  233. return alphabetical or r2.quantity == 0
  234. end
  235.  
  236. function clear_window()
  237. paintutils.drawFilledBox(1,2,screen_width,screen_height-1,colors.black)
  238. clear_search_bar()
  239. end
  240.  
  241. function side_number(n)
  242. term.setCursorPos(1, n+1)
  243. local spaces = {}
  244. n_string = tostring(n)
  245. for i=1, 2-#n_string do
  246. table.insert(spaces, " ")
  247. end
  248. term.setTextColor(colors.cyan)
  249. term.write(table.concat(spaces)..n_string)
  250. term.setTextColor(colors.white)
  251. end
  252.  
  253. function list_items(items_list)
  254. clear_window()
  255. term.setCursorPos(1,2)
  256. for i, item in pairs(slice(items_list, 1, screen_height-2)) do
  257. side_number(i)
  258. print(" "..item.display_name)
  259. term.setTextColor(colors.lightGray)
  260. term.write(" #"..item.quantity)
  261. end
  262. clear_search_bar()
  263. end
  264.  
  265. function select_item(search_term)
  266. show_title("Fetch: \""..search_term.."\"")
  267. local search_results = {}
  268. for _, item in ipairs(items) do
  269. local r = search(item.display_name, search_term)
  270. if r then
  271. table.insert(search_results, item)
  272. end
  273. end
  274. table.sort(search_results, result_order)
  275. list_items(search_results)
  276. local choice = io.read()
  277. while not tonumber(choice) and tonumber(choice) > #search_results do
  278. choice = io.read()
  279. end
  280. return search_results[tonumber(choice)]
  281. end
  282.  
  283. function amount_in_chest()
  284. store_chest.condenseItems()
  285. return #store_chest.getAllStacks()
  286. end
  287.  
  288. function get_enderchest(group)
  289. turtle.select(16)
  290. turtle.placeDown()
  291. turtle.select(1)
  292. end
  293.  
  294. function start_store()
  295. go_to({x=0, y=me.y, z=0})
  296. local at_home = turtle.detectDown()
  297. condense_inventory()
  298. if not at_home then
  299. get_enderchest("deposit")
  300. end
  301. items_left = amount_in_chest()
  302. while items_left > 0 do
  303. go_to({x=0, y=me.y, z=0})
  304. get_enderchest("deposit")
  305. while turtle.suckDown() do end
  306. if not at_home then
  307. if inventory.getStackInSlot(16) then
  308. inventory.pushItem("down", 16)
  309. end
  310. end
  311. items_left = amount_in_chest()
  312. if not at_home then
  313. turtle.select(16)
  314. turtle.digDown()
  315. end
  316. store_inventory()
  317. end
  318. end
  319.  
  320. function number_of_stacks(full_stack, amount)
  321. local stacks = math.floor(amount/full_stack)
  322. local remaining_items = amount % full_stack
  323. return stacks, remaining_items
  324. end
  325.  
  326. function fetch_item(item)
  327. go_to(item.position)
  328. local amount_to_fetch = item.amount_to_fetch
  329. while amount_to_fetch > 0 do
  330. local amount = inventory.pullItem(item.position.side, 2, amount_to_fetch)
  331. item.quantity = item.quantity - amount
  332. amount_to_fetch = amount_to_fetch - amount
  333. end
  334. if item.quantity == 0 then
  335. table.insert(open_positions, item.position)
  336. save(open_positions, "open_positions")
  337. item.position = nil
  338. end
  339. save(items, "items")
  340. end
  341.  
  342. function start_fetch()
  343. local items_to_fetch = queue.fetch
  344. while #items_to_fetch > 0 do
  345. table.sort(items_to_fetch, closest_order)
  346. fetch_item(items_to_fetch[#items_to_fetch])
  347. table.remove(items_to_fetch)
  348. end
  349. end
  350.  
  351. function do_queue()
  352. empty_inventory("down")
  353. store_inventory()
  354. if #queue.fetch > 0 then
  355. start_fetch()
  356. end
  357. if queue.store then
  358. start_store()
  359. end
  360. queue = {fetch = {}, store = false}
  361. go_to(home)
  362. end
  363.  
  364. function menu_fetch(search_term)
  365. local item_to_fetch = select_item(search_term)
  366. show_title("Fetch: "..item_to_fetch.display_name)
  367. local amount = io.read()
  368. while not tonumber(amount) do
  369. amount = io.read()
  370. end
  371. item_to_fetch.amount_to_fetch = amount
  372. table.insert(queue.fetch, item_to_fetch)
  373. end
  374.  
  375. function menu_store()
  376. queue.store = true
  377. end
  378.  
  379. function main_menu()
  380. local options = {
  381. ["store"] = menu_store,
  382. ["sort"] = menu_store,
  383. [">"] = menu_store,
  384. ["fetch"] = menu_fetch,
  385. ["get"] = menu_fetch,
  386. ["<"] = menu_fetch,
  387. ["start"] = do_queue,
  388. ["go"] = do_queue,
  389. [""] = do_queue
  390. }
  391. while true do
  392. show_title(os.getComputerLabel())
  393. local input = io.read()
  394. if options[input] then
  395. options[input](input)
  396. end
  397. end
  398. end
  399.  
  400. function start()
  401. screen_width, screen_height = term.getSize()
  402. queue = {to_store=false,fetch={}}
  403. home = {x=0,y=0,z=0}
  404. me = {x=home.x,y=home.y,z=home.z}
  405. store_chest = peripheral.wrap("bottom")
  406. fetch_chest = peripheral.wrap("right")
  407. inventory = peripheral.wrap("left")
  408. items = load("items")
  409. open_positions = load("open_positions")
  410. main_menu()
  411. end
  412.  
  413. start()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement