daily pastebin goal
77%
SHARE
TWEET

Untitled

whaamp Jan 16th, 2019 55 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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.         save({}, name)
  11.         return load(name)
  12.     else
  13.         local data = file.readAll()
  14.         file.close()
  15.         return textutils.unserialize(data)
  16.     end
  17. end
  18.  
  19. function go_to(target)
  20.     if me.y ~= target.y then
  21.         go_to({x = 0, y = me.y, z = 0})
  22.         while me.y < target.y do
  23.             if turtle.up() then me.y = me.y + 1 end
  24.         end
  25.         while me.y > target.y do
  26.             if turtle.down() then me.y = me.y - 1 end
  27.         end
  28.     end
  29.     if me.x < target.x then
  30.         turtle.turnRight()
  31.         while me.x < target.x do
  32.             if turtle.forward() then me.x = me.x + 1 end
  33.         end
  34.         turtle.turnLeft()
  35.     end
  36.     if me.x > target.x then
  37.         turtle.turnLeft()
  38.         while me.x > target.x do
  39.             if turtle.forward() then me.x = me.x - 1 end
  40.         end
  41.         turtle.turnRight()
  42.     end
  43.     while me.z < target.z do
  44.         if turtle.forward() then me.z = me.z + 1 end
  45.     end
  46.     while me.z > target.z do
  47.         if turtle.back() then me.z = me.z - 1 end
  48.     end
  49. end
  50.  
  51. function next_position(position)
  52.     local new_position = {x = position.x, y = position.y, z = position.z, side = position.side}
  53.     if position.x == 5 then
  54.         new_position.x = -5
  55.         if position.z == 5 then
  56.             new_position.z = -5
  57.             new_position.y = position.y + 3
  58.         else
  59.             new_position.z = position.z + 1
  60.         end
  61.     else
  62.         if position.x == -1 and position.z == 0 then
  63.             new_position.x = position.x + 2
  64.         else
  65.             new_position.x = position.x + 1
  66.         end
  67.     end
  68.     return new_position
  69. end
  70.  
  71. function get_enderchest(group)
  72.     turtle.select(16)
  73.     turtle.placeDown()
  74. end
  75.  
  76. function safe_string(text)
  77.     local new_text = {}
  78.     for i = 1, #text do
  79.         local val = string.byte(text, i)
  80.         new_text[i] = (val > 31 and val < 127) and val or 32
  81.     end
  82.     return string.char(unpack(new_text))
  83. end
  84.  
  85. function earliest_order(position_1, position_2)
  86.     if position_1.y > position_2.y then
  87.         return true
  88.     elseif position_1.y == position_2.y then
  89.         if position_1.z > position_2.z then
  90.             return true
  91.         elseif position_1.z == position_2.z then
  92.             if position_1.x > position_2.x then
  93.                 return true
  94.             elseif position_1.x == position_2.x then
  95.                 if position_1.side == "up" then
  96.                     return true
  97.                 end
  98.             end
  99.         end
  100.     end
  101. end
  102.  
  103. function next_open_position()
  104.     if #open_positions > 1 then
  105.         table.sort(open_positions, earliest_order)
  106.         return table.remove(open_positions)
  107.     else
  108.         local position = table.remove(open_positions)
  109.         table.insert(next_position(position))
  110.         return position
  111.     end
  112. end
  113.  
  114. function register(stack)
  115.     local item = nil
  116.     stack.string_id = safe_string(stack.id..stack.dmg..stack.raw_name..stack.display_name)
  117.     for _, existing_item in ipairs(items) do
  118.         if existing_item.string_id == stack.string_id then
  119.             item = existing_item
  120.             break
  121.         end
  122.     end
  123.     if not item then
  124.         item = {}
  125.         item.string_id = stack.string_id
  126.         item.damage = stack.dmg
  127.         item.max_damage = stack.max_dmg
  128.         item.max_stack = stack.max_size
  129.         item.recipes = {}
  130.         item.quantity = 0
  131.         item.position = next_open_position()
  132.         item.slot = nil
  133.         table.insert(items, item)
  134.     end
  135.     if item.position == nil then
  136.         item.position = next_open_position()
  137.     end
  138.     save(open_positions, "open_positions")
  139. end
  140.  
  141. function check_inventory()
  142.     local items_to_sort = {}
  143.     local inventory_items = inventory.getAllStacks()
  144.     for i = 1, #inventory_items - 1 do
  145.         table.insert(items_to_sort, register(inventory_items[i].basic()))
  146.     end
  147.     return items_to_sort
  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_sort)
  162.     for i = 1, #items_to_sort do
  163.         items_to_sort[i].slot = i
  164.     end
  165. end
  166.  
  167. function deposit_item(item)
  168.     go_to(item.position)
  169.     local amount = inventory.push(item.position.side, item.slot)
  170.     item.quantity = item.quantity + amount
  171.     save(items, "items")
  172. end
  173.  
  174. function sort_items(items_to_sort)
  175.     give_item_slots(items_to_sort)
  176.     while #items_to_sort > 0 do
  177.         local closest_item = table.sort(items_to_sort, closest_order)[#items_to_sort]
  178.         deposit_item(closest_item)  
  179.         table.remove(items_to_sort)
  180.     end
  181. end
  182.  
  183. function condense_inventory()
  184.     inventory.condenseItems()
  185.     inventory.swapStacks(#inventory.getAllStacks(), 16)
  186. end
  187.  
  188. function begin_depositing()
  189.     go_to({0, me.y, 0})
  190.     local at_home = turtle.detectDown()
  191.     condense_inventory()
  192.     if not at_home then
  193.         get_enderchest("deposit")
  194.     end
  195.     while turtle.suckDown() do end
  196.     chest.condenseItems()
  197.     if #chest.getAllStacks() > 0 then
  198.         table.insert(queue, 2, begin_depositing)
  199.     end
  200.     if not at_home then
  201.         turtle.digDown()
  202.     end
  203.     local items_to_sort = check_inventory()
  204.     sort_items(items_to_sort)
  205. end
  206.  
  207. function start()
  208.     me = {x=0,y=0,z=0}
  209.     queue = {}
  210.     while not chest do
  211.         chest = peripheral.wrap("bottom")
  212.         sleep(3)
  213.     end
  214.     inventory = peripheral.wrap("right")
  215.     items = load("items")
  216.     open_positions = load("open_positions")
  217. end
  218.  
  219. function do_queue()
  220.     local i = 1
  221.     while i <= #queue do
  222.         queue[i]()
  223.         i = i + 1
  224.     end
  225. end
  226.  
  227. while true do
  228.     command = io.read()
  229.     if command == ">" then
  230.         table.insert(queue, begin_depositing)
  231.     elseif command == "" then
  232.         do_queue()
  233.     end
  234. end
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top