t0x1c_adr1an

Drop

Apr 21st, 2021 (edited)
380
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.71 KB | None | 0 0
  1. --- CC
  2. --- Turtle sorter
  3. --- Input amount of rows and height of chests
  4.  
  5. -- User input
  6.  
  7. -- Initialized settings for the area the turtle while sort
  8. local chest_width = 5
  9. local chest_height = 2
  10.  
  11. -- If the turtles backs up to sort the chests ar to it's "chest_side"
  12. local chest_side = 'Left'
  13.  
  14. -- The way the face of the turtle is oriented
  15. -- N = 0; E = 1; S = 2; W = 3
  16. local turtle_face = 1
  17.  
  18. -- vector from 0 to 'home'.
  19. local home_cords = vector.new(89, 85, 172)
  20.  
  21. -- Script vars
  22.  
  23. -- Chest
  24. local chest = peripheral.wrap('front')
  25. local copy_side = chest_side
  26.  
  27. -- Positioning
  28. local pos = vector.new(gps.locate())
  29. local diff = pos:sub(home_cords)
  30.  
  31. -- accepted fuels
  32. local accepted_fuel = {
  33.     'minecraft:coal_block',
  34.     'minecraft:coal',
  35. }
  36.  
  37. -- Helper functions
  38. local function table_to_set(table)
  39.     --- Converts a table to a set where there are no double keys.
  40.  
  41.     -- Create the set.
  42.     local a_set = {}
  43.  
  44.     -- For each item in the table iterate trough it.
  45.     for i in ipairs(table) do
  46.         -- The key is the 'name' and the value, a place holder, is the last
  47.         -- 'count' value.
  48.         a_set[table[i]['name']] = table[i]['count']
  49.     end
  50.  
  51.     -- Print function remove.
  52.     for key, value in pairs(a_set) do
  53.         print( key, value)
  54.     end
  55.  
  56.     -- Return the set.
  57.     return a_set
  58. end
  59.  
  60. local function around()
  61.     turtle.turnLeft()
  62.     turtle.turnLeft()
  63.     if turtle_face + 2 == 4 then
  64.         turtle_face = 0
  65.     elseif turtle_face + 2 == 5 then
  66.         turtle_face = 1
  67.     else
  68.         turtle_face = turtle_face + 2
  69.     end
  70. end
  71.  
  72. local function Left()
  73.     turtle.turnLeft()
  74.     if turtle_face - 1 == -1 then
  75.         turtle_face = 3
  76.     else
  77.         turtle_face = turtle_face - 1
  78.     end
  79. end
  80.  
  81. local function Right()
  82.     turtle.turnRight()
  83.     if turtle_face + 1 == 4 then
  84.         turtle_face = 0
  85.     else
  86.         turtle_face = turtle_face + 1
  87.     end
  88. end
  89.  
  90. local function n_forward(n)
  91.     for x=1,n do
  92.         turtle.forward()
  93.     end
  94. end
  95.  
  96. local function pick_up()
  97.   for i in pairs(chest.list()) do
  98.     turtle.suck()
  99.   end
  100.   return true
  101. end
  102.  
  103. local function refuel()
  104.     local current_fuel = turtle.getFuelLevel()
  105.     if current_fuel <= 0 then
  106.         while current_fuel <= 0 do
  107.             turtle.select(1)
  108.             for x=1, #accepted_fuel do
  109.                 if turtle.getItemDetail().name == accepted_fuel[x] then
  110.                     turtle.refuel()
  111.                 end
  112.             end
  113.         end
  114.     end
  115. end
  116.  
  117. local function update(cords)
  118.     pos = vector.new(gps.locate())
  119.     diff = pos:sub(cords)
  120. end
  121.  
  122. local function go_to(cords)
  123.     --- Cords is a table, from the vector API, in {x='x', y='y', z='z'}
  124.     update(home_cords)
  125.     while diff.x ~= 0 or diff.y ~= 0 or diff.z ~= 0 do
  126.         pos = vector.new(gps.locate())
  127.  
  128.         -- For the x-field
  129.         if diff.x < 0 then
  130.             if turtle_face ~= 1 then
  131.                 while turtle_face ~= 1 do
  132.                     Right()
  133.                 end
  134.             end
  135.  
  136.             while not turtle.detect() and diff.x < 0 and turtle_face == 1 do
  137.                 turtle.forward()
  138.                 update(cords)
  139.             end
  140.  
  141.         elseif diff.x > 0 then
  142.             if turtle_face ~= 3 then
  143.                 while turtle_face ~= 3 do
  144.                     Right()
  145.                 end
  146.             end
  147.  
  148.             while not turtle.detect() and diff.x > 0 and turtle_face == 3 do
  149.                 turtle.forward()
  150.                 update(cords)
  151.             end
  152.         end
  153.  
  154.         -- For the y-field
  155.         if diff.y < 0 then
  156.             while not turtle.detectUp() and diff.y < 0 do
  157.                 turtle.up()
  158.                 update(cords)
  159.             end
  160.         elseif diff.y > 0 then
  161.             while not turtle.detectDown() and diff.y > 0 do
  162.                 turtle.down()
  163.                 update(cords)
  164.             end
  165.         end
  166.  
  167.         -- For the z-field
  168.         if diff.z < 0 then
  169.             if turtle_face ~= 0 then
  170.                 while turtle_face ~= 0 do
  171.                     Right()
  172.                 end
  173.             end
  174.  
  175.             while not turtle.detect() and diff.z < 0 and turtle_face == 0 do
  176.                 turtle.forward()
  177.                 update(cords)
  178.             end
  179.  
  180.         elseif diff.z > 0 then
  181.             if turtle_face ~= 2 then
  182.                 while turtle_face ~= 2 do
  183.                     Right()
  184.                 end
  185.             end
  186.  
  187.             while not turtle.detect() and diff.z > 0 and turtle_face == 2 do
  188.                 turtle.forward()
  189.                 update(cords)
  190.             end
  191.         end
  192.     end
  193. end
  194.  
  195. -- Main functions
  196.  
  197. -- Compare function
  198. local function drop(compare)
  199.     --- Compare/sort/drop function
  200.     --- To-do
  201.     ---     Use .getItemDetail(slot) / .size() instead of .list() for the
  202.     ---     display / support of nil slots
  203.  
  204.     -- If the turtle does not have to check if the current slot item is
  205.     -- in it's inventory.
  206.     if compare == false then
  207.         -- For all the items, except fuel, drop it in the chest infront of it.
  208.         for x=2,16 do
  209.             turtle.select(x)
  210.             turtle.drop()
  211.         end
  212.  
  213.     -- The turtle does need to compare.
  214.     else
  215.         -- for the amount of unique blocks in the chest.
  216.         if table_to_set(chest.list()) ~= nil then
  217.             for i in pairs(table_to_set(chest.list())) do
  218.                 -- For the inventorty of a turtle.
  219.                 for x=2,16 do
  220.                     -- Select the slot
  221.                     turtle.select(x)
  222.                     -- If the slot and the inventory items match, drop the item in
  223.                     -- the chest
  224.                     if i ~= nil and turtle.getItemDetail() ~= nil then
  225.                         if i ==
  226.                                 turtle.getItemDetail()['name'] then
  227.                             turtle.drop()
  228.                         end
  229.                     end
  230.                 end
  231.             end
  232.         end
  233.     end
  234.     turtle.select(1)
  235. end
  236.  
  237. local function move()
  238.     -- Move from 'home' to chests
  239.     around()
  240.     n_forward(2)
  241.  
  242.     -- If the chests are on it's right side, now they will be on the left.n_forward
  243.     if chest_side == 'Right' then
  244.         Left()
  245.         turtle.forward()
  246.     elseif chest_side == 'Left' then
  247.         Right()
  248.         turtle.forward()
  249.     end
  250.  
  251.     -- Place the items in the right chests
  252.     for x=1,chest_height do
  253.         for y=1,chest_width do
  254.             refuel()
  255.             if chest_side == 'Left' then
  256.                 Right()
  257.                 drop(true)
  258.                 Left()
  259.             elseif chest_side == 'Right' then
  260.                 Left()
  261.                 drop(true)
  262.                 Right()
  263.             end
  264.             turtle.forward()
  265.         end
  266.         turtle.up()
  267.         around()
  268.         if chest_side == 'Left' then
  269.             chest_side = 'Right'
  270.         else
  271.             chest_side = 'Left'
  272.         end
  273.     end
  274.     go_to(home_cords)
  275. end
  276.  
  277. local function main()
  278.     pos = vector.new(gps.locate())
  279.     diff = pos:sub(home_cords)
  280.     local inventory_full = true
  281.  
  282.     -- Check if the turtle is 'Home'
  283.     if diff.x == 0 and diff.y == 0 and diff.z == 0 then
  284.         -- Check fuel level and refuel if needed
  285.         refuel()
  286.  
  287.         -- Check if the turtle's inventory is full
  288.         for x=2,16 do
  289.             turtle.select(x)
  290.             if turtle.getItemDetail() == nil then
  291.                 inventory_full = false
  292.             end
  293.         end
  294.  
  295.         -- If there is space in the inventory don't sort but wait for a full
  296.         -- inventory.
  297.         while not inventory_full do
  298.             inventory_full = pick_up()
  299.         end
  300.         move()
  301.     end
  302.  
  303. end
  304.  
  305. -- Main loop
  306. while true do
  307.     main()
  308. end
Add Comment
Please, Sign In to add comment