Advertisement
whaamp

Untitled

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