Advertisement
whaamp

Untitled

Jan 20th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.25 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 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_height,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(inventory_items[i].basic()))
  145. end
  146. return items_to_store
  147. end
  148.  
  149. function closest_order(item_1, item_2)
  150. return distance_away(item_1.position) > distance_away(item_2.position)
  151. end
  152.  
  153. function give_item_slots(items_to_store)
  154. for i = 1, #items_to_store do
  155. table.insert(items_to_store[i].slot, i)
  156. end
  157. end
  158.  
  159. function deposit_item(item)
  160. go_to(item.position)
  161. for _, slot_number in ipairs(item.slot) do
  162. local amount = inventory.pushItem(item.position.side, slot_number)
  163. item.quantity = item.quantity + amount
  164. end
  165. item.slot = {}
  166. save(items, "items")
  167. end
  168.  
  169. function store_items(items_to_store)
  170. give_item_slots(items_to_store)
  171. while #items_to_store > 0 do
  172. table.sort(items_to_store, closest_order)
  173. local closest_item = items_to_store[#items_to_store]
  174. deposit_item(closest_item)
  175. table.remove(items_to_store)
  176. end
  177. end
  178.  
  179. function store_inventory()
  180. show_title("Storing Inventory")
  181. inventory_items = condense_inventory()
  182. local items_to_store = check_inventory(inventory_items)
  183. store_items(items_to_store)
  184. end
  185.  
  186. function search(s1, s2)
  187. local s1 = string.lower(string.gsub(string.gsub(s1, " ", ""), "_", ""))
  188. local s2 = string.lower(string.gsub(string.gsub(s2, " ", ""), "_", ""))
  189. return string.find(s1, s2)
  190. end
  191.  
  192. function result_order(r1, r2)
  193. local alphabetical = string.lower(r1.display_name) < string.lower(r2.display_name)
  194. return alphabetical or r2.quantity == 0
  195. end
  196.  
  197. function clear_window()
  198. paintutils.drawFilledBox(1,2,screen_width,screen_height-1,colors.black)
  199. clear_search_bar()
  200. end
  201.  
  202. function side_number(n)
  203. term.setCursorPos(1, n+1)
  204. local spaces = {}
  205. n_string = tostring(n)
  206. for i=1, 2-#n_string do
  207. table.insert(spaces, " ")
  208. end
  209. term.setTextColor(colors.cyan)
  210. term.write(table.concat(spaces)..n_string)
  211. term.setTextColor(colors.white)
  212. end
  213.  
  214. function list_items(items_list)
  215. clear_window()
  216. term.setCursorPos(1,2)
  217. for i, item in pairs(slice(items_list, 1, screen_height-2)) do
  218. side_number(i)
  219. print(" "..item.display_name)
  220. term.setTextColor(colors.lightGray)
  221. term.write(" #"..item.quantity)
  222. end
  223. clear_search_bar()
  224. end
  225.  
  226. function select_item(search_term)
  227. show_title("Fetch: \""..search_term.."\"")
  228. local search_results = {}
  229. for _, item in ipairs(items) do
  230. local r = search(item.display_name, search_term)
  231. if r then
  232. table.insert(search_results, item)
  233. end
  234. end
  235. table.sort(search_results, result_order)
  236. list_items(search_results)
  237. local choice = io.read()
  238. while not tonumber(choice) and tonumber(choice) > #search_results do
  239. choice = io.read()
  240. end
  241. return search_results[tonumber(choice)]
  242. end
  243.  
  244. function amount_in_chest()
  245. store_chest.condenseItems()
  246. return #store_chest.getAllStacks()
  247. end
  248.  
  249. function get_enderchest(group)
  250. turtle.select(16)
  251. turtle.placeDown()
  252. turtle.select(1)
  253. end
  254.  
  255. function start_store()
  256. go_to({x=0, y=me.y, z=0})
  257. local at_home = turtle.detectDown()
  258. condense_inventory()
  259. if not at_home then
  260. get_enderchest("deposit")
  261. end
  262. items_left = amount_in_chest()
  263. while items_left > 0 do
  264. go_to({x=0, y=me.y, z=0})
  265. get_enderchest("deposit")
  266. while turtle.suckDown() do end
  267. if not at_home then
  268. if inventory.getStackInSlot(16) then
  269. inventory.pushItem("down", 16)
  270. end
  271. end
  272. items_left = amount_in_chest()
  273. if not at_home then
  274. turtle.select(16)
  275. turtle.digDown()
  276. end
  277. store_inventory()
  278. end
  279. end
  280.  
  281. function number_of_stacks(full_stack, amount)
  282. local stacks = math.floor(amount/full_stack)
  283. local remaining_items = amount % full_stack
  284. return stacks, remaining_items
  285. end
  286.  
  287. function fetch_item(item)
  288. go_to(item.position)
  289. local amount_to_fetch = item.amount_to_fetch
  290. while amount_to_fetch > 0 do
  291. local amount = inventory.pullItem(item.position.side, 2, amount_to_fetch)
  292. item.quantity = item.quantity - amount
  293. amount_to_fetch = amount_to_fetch - amount
  294. end
  295. if item.quantity == 0 then
  296. table.insert(open_positions, item.position)
  297. save(open_positions, "open_positions")
  298. item.position = nil
  299. end
  300. save(items, "items")
  301. end
  302.  
  303. function start_fetch()
  304. local items_to_fetch = queue.fetch
  305. while #items_to_fetch > 0 do
  306. table.sort(items_to_fetch, closest_order)
  307. fetch_item(items_to_fetch[#items_to_fetch])
  308. table.remove(items_to_fetch)
  309. end
  310. end
  311.  
  312. function do_queue()
  313. empty_inventory("down")
  314. store_inventory()
  315. if #queue.fetch > 0 then
  316. start_fetch()
  317. end
  318. if queue.store then
  319. start_store()
  320. end
  321. queue = {fetch = {}, store = false}
  322. go_to(home)
  323. end
  324.  
  325. function menu_fetch(search_term)
  326. local item_to_fetch = select_item(search_term)
  327. show_title("Fetch: "..item_to_fetch.display_name)
  328. local amount = io.read()
  329. while not tonumber(amount) do
  330. amount = io.read()
  331. end
  332. item_to_fetch.amount_to_fetch = amount
  333. table.insert(queue.fetch, item_to_fetch)
  334. end
  335.  
  336. function menu_store()
  337. queue.store = true
  338. end
  339.  
  340. function main_menu()
  341. local options = {
  342. ["store"] = menu_store,
  343. ["sort"] = menu_store,
  344. [">"] = menu_store,
  345. ["fetch"] = menu_fetch,
  346. ["get"] = menu_fetch,
  347. ["<"] = menu_fetch,
  348. ["start"] = do_queue,
  349. ["go"] = do_queue,
  350. [""] = do_queue
  351. }
  352. while true do
  353. show_title(os.getComputerLabel())
  354. local input = io.read()
  355. if options[input] then
  356. options[input](input)
  357. end
  358. end
  359. end
  360.  
  361. function start()
  362. screen_width, screen_height = term.getSize()
  363. queue = {to_store=false,to_fetch={}}
  364. home = {x=0,y=0,z=0}
  365. me = {x=home.x,y=home.y,z=home.z}
  366. store_chest = peripheral.wrap("bottom")
  367. fetch_chest = peripheral.wrap("right")
  368. inventory = peripheral.wrap("left")
  369. items = load("items")
  370. open_positions = load("open_positions")
  371. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement