daily pastebin goal
47%
SHARE
TWEET

Untitled

whaamp Jan 16th, 2019 60 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.         if name == "open_positions" then
  11.             save({{x=-5,y=2,z=-5}}, 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. end
  84.  
  85. function safe_string(text)
  86.     local new_text = {}
  87.     for i = 1, #text do
  88.         local val = string.byte(text, i)
  89.         new_text[i] = (val > 31 and val < 127) and val or 32
  90.     end
  91.     return string.char(unpack(new_text))
  92. end
  93.  
  94. function earliest_order(position_1, position_2)
  95.     if position_1.y > position_2.y then
  96.         return true
  97.     elseif position_1.y == position_2.y then
  98.         if position_1.z > position_2.z then
  99.             return true
  100.         elseif position_1.z == position_2.z then
  101.             if position_1.x > position_2.x then
  102.                 return true
  103.             elseif position_1.x == position_2.x then
  104.                 if position_1.side == "up" then
  105.                     return true
  106.                 end
  107.             end
  108.         end
  109.     end
  110. end
  111.  
  112. function next_open_position()
  113.     if #open_positions > 1 then
  114.         table.sort(open_positions, earliest_order)
  115.         return table.remove(open_positions)
  116.     else
  117.         local position = table.remove(open_positions)
  118.         table.insert(next_position(position))
  119.         return position
  120.     end
  121. end
  122.  
  123. function register(stack)
  124.     local item = nil
  125.     stack.string_id = safe_string(stack.id..stack.dmg..stack.raw_name..stack.display_name)
  126.     for _, existing_item in ipairs(items) do
  127.         if existing_item.string_id == stack.string_id then
  128.             item = existing_item
  129.             break
  130.         end
  131.     end
  132.     if not item then
  133.         item = {}
  134.         item.string_id = stack.string_id
  135.         item.damage = stack.dmg
  136.         item.max_damage = stack.max_dmg
  137.         item.max_stack = stack.max_size
  138.         item.recipes = {}
  139.         item.quantity = 0
  140.         item.position = next_open_position()
  141.         item.slot = nil
  142.         table.insert(items, item)
  143.     end
  144.     if item.position == nil then
  145.         item.position = next_open_position()
  146.     end
  147.     save(open_positions, "open_positions")
  148. end
  149.  
  150. function check_inventory()
  151.     local items_to_sort = {}
  152.     local inventory_items = inventory.getAllStacks()
  153.     for i = 1, #inventory_items - 1 do
  154.         table.insert(items_to_sort, register(inventory_items[i].basic()))
  155.     end
  156.     return items_to_sort
  157. end
  158.  
  159. function distance_away(position)
  160.     local distance = math.abs(position.y - me.y)
  161.     distance = distance + math.abs(me.x) + math.abs(me.z)
  162.     distance = distance + math.abs(position.x) + math.abs(position.z)
  163.     return distance
  164. end
  165.  
  166. function closest_order(item_1, item_2)
  167.     return distance_away(item_1.position) > distance_away(item_2.position)
  168. end
  169.  
  170. function give_item_slots(items_to_sort)
  171.     for i = 1, #items_to_sort do
  172.         items_to_sort[i].slot = i
  173.     end
  174. end
  175.  
  176. function deposit_item(item)
  177.     go_to(item.position)
  178.     local amount = inventory.push(item.position.side, item.slot)
  179.     item.quantity = item.quantity + amount
  180.     save(items, "items")
  181. end
  182.  
  183. function sort_items(items_to_sort)
  184.     give_item_slots(items_to_sort)
  185.     while #items_to_sort > 0 do
  186.         local closest_item = table.sort(items_to_sort, closest_order)[#items_to_sort]
  187.         deposit_item(closest_item)  
  188.         table.remove(items_to_sort)
  189.     end
  190. end
  191.  
  192. function condense_inventory()
  193.     inventory.condenseItems()
  194.     inventory.swapStacks(#inventory.getAllStacks(), 16)
  195. end
  196.  
  197. function begin_depositing()
  198.     go_to({x=0, y=me.y, z=0})
  199.     local at_home = turtle.detectDown()
  200.     condense_inventory()
  201.     if not at_home then
  202.         get_enderchest("deposit")
  203.     end
  204.     turtle.select(1)
  205.     while turtle.suckDown() do end
  206.     chest.condenseItems()
  207.     if #chest.getAllStacks() > 0 then
  208.         table.insert(queue, 2, begin_depositing)
  209.     end
  210.     if not at_home then
  211.         turtle.digDown()
  212.     end
  213.     local items_to_sort = check_inventory()
  214.     sort_items(items_to_sort)
  215. end
  216.  
  217. function start()
  218.     me = {x=0,y=0,z=0}
  219.     queue = {}
  220.     while not chest do
  221.         chest = peripheral.wrap("bottom")
  222.         sleep(3)
  223.     end
  224.     inventory = peripheral.wrap("right")
  225.     items = load("items")
  226.     open_positions = load("open_positions")
  227. end
  228.  
  229. function do_queue()
  230.     local i = 1
  231.     while i <= #queue do
  232.         queue[i]()
  233.         i = i + 1
  234.     end
  235. end
  236.  
  237. start()
  238.  
  239. while true do
  240.     command = io.read()
  241.     if command == ">" then
  242.         table.insert(queue, begin_depositing)
  243.     elseif command == "" then
  244.         do_queue()
  245.     end
  246. 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