Advertisement
markman4897

MRP - Move and Refuel Program

Apr 17th, 2017
82
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 11.00 KB | None
  1. -- MRP library - Move and Refuel Program
  2. -- Version: 2.0.0
  3. -- by @markman4897
  4.  
  5. -- Place an output (ender)chest in the first slot.
  6. --   (the turtle will dump all its contents there)
  7. --
  8. -- Place the fuel (ender)chest in the second slot.
  9. --   (the turtle will take lava buckets, refill
  10. --   and place empty buckets back into that chest)
  11. --
  12. -- Write the coordinates of the turtle into the
  13. -- variables below (x_coord, z_coord and y_coord).
  14. --
  15. -- Write the orientation of the turtle into the
  16. -- variable below (dir). 1 means north, 2 means
  17. -- east, 3 means south and 4 means west.
  18.  
  19. -- Functions of the program:
  20. -- - Movement tracking by coordinates
  21. -- - Auto refuel with (ender)chest in slot 2 with
  22. --   lava bucket.
  23. -- - Dump all items into (ender)chest in slot 1.
  24.  
  25. -- Enjoy basic library.
  26.  
  27. -- TODO:
  28. -- - add option to set dir in string or int
  29. --
  30. -- - change length for times in dig function
  31. --
  32. -- - revise function set (recursive calling)
  33. -- - revise function get (maybe change return)
  34. --
  35. -- - make move function use native api so it can
  36. --   handle interrupts
  37. --
  38. -- - make mapper turtle (that sends the map via
  39. --   rednet to central map computer for later and
  40. --   massive use)
  41. --
  42. -- - change the dig and move function from while
  43. --   to repeat
  44. -- - drop the recursion in dig and move functions
  45. -- - move attack from dig to move function
  46. --
  47. -- - tutorial on how to merge programs with the
  48. --   library if people don't want two files
  49. --   (replace MPR.get-s first and then delete all
  50. --   "MPR."... i think, and then just paste the
  51. --   program at the end of this library)
  52. --
  53. -- - add alternative method for refueling (fuel
  54. --   instead of a chest)
  55. --
  56. -- - add alternative method for invDump (go to
  57. --   dump chest when full)
  58. -- - add exceptions in inv check for when you need
  59. --   to go to dump chest (one more slot free)
  60. --
  61. -- - switch fuel chest slot and dump chest slot
  62.  
  63.  
  64. -- =========
  65. -- Variables
  66. -- =========
  67.  
  68. -- coordinate values
  69. x_coord = 0
  70. z_coord = 0
  71. y_coord = 0
  72.  
  73. dir = 1
  74. directions = {"north", "east", "south", "west"}
  75.  
  76. -- other variables
  77. dumpSlot = 1
  78. fuelSlot = 2
  79.  
  80.  
  81. -- =========
  82. -- Functions
  83. -- =========
  84.  
  85. -- Set and get function to change variables here
  86.  
  87. function set(a,b,c,d)
  88.     c = c or 0
  89.     d = d or 0
  90.  
  91.     if a == "x" then
  92.         x_coord = b
  93.  
  94.     elseif a == "z" then
  95.         z_coord = b
  96.  
  97.     elseif a == "y" then
  98.         y_coord = b
  99.  
  100.     elseif a == "dir" then
  101.         dir = b
  102.  
  103.     elseif c ~= 0 and d == 0 then
  104.     -- could do this with calling this function 4 times...
  105.     -- is it better tho? maybe faster or lighter??
  106.         x_coord = a
  107.         z_coord = b
  108.         y_coord = c
  109.  
  110.     elseif c ~= 0 and d ~= 0 then
  111.     -- could do the same here...
  112.         x_coord = a
  113.         z_coord = b
  114.         y_coord = c
  115.         dir = d
  116.  
  117.     else
  118.         return false
  119.  
  120.     end
  121.  
  122.     return true
  123.  
  124. end
  125.  
  126. function get(a)
  127.     if a == "x" then
  128.         b = x_coord
  129.  
  130.     elseif a == "z" then
  131.         b = z_coord
  132.  
  133.     elseif a == "y" then
  134.         b = y_coord
  135.  
  136.     elseif a == "dir" then
  137.         b = dir
  138.  
  139.     else
  140.         return {x_coord,z_coord,y_coord,dir} -- TEST THIS!!!
  141.         -- maybe just return 4 variables instead of list?
  142.  
  143.     end
  144.  
  145.     return b
  146.    
  147. end
  148.  
  149. -- Refuel function (with fuel chest full of lava buckets)
  150.  
  151. function refuel()
  152.     print("Starting refueling.")
  153.     turtle.select(fuelSlot)
  154.  
  155.     if turtle.back() then
  156.         print("-moving back")
  157.         turtle.place()
  158.         turtle.suck()
  159.         turtle.refuel()
  160.         turtle.drop()
  161.         turtle.dig()
  162.         turtle.forward()
  163.     elseif turtle.up() then
  164.         print("-moving up")
  165.         turtle.placeDown()
  166.         turtle.suckDown()
  167.         turtle.refuel()
  168.         turtle.dropDown()
  169.         turtle.digDown()
  170.         turtle.down()
  171.     elseif turtle.down() then
  172.         print("-moving down")
  173.         turtle.placeUp()
  174.         turtle.suckUp()
  175.         turtle.refuel()
  176.         turtle.dropUp()
  177.         turtle.digUp()
  178.         turtle.up()
  179.     else
  180.         print("Couldn't refill! (rogue turn?)")
  181.     end
  182.  
  183.     turtle.select(dumpSlot)
  184.     print("-Done refueling")
  185.  
  186.     return true
  187.  
  188. end
  189.  
  190. -- Check how many slots are not empty
  191.  
  192. function checkInv()
  193.     out = 0
  194.     for i=3,16,1 do
  195.         if turtle.getItemCount(i) ~= 0 then
  196.             out = out+1
  197.         end
  198.     end
  199.  
  200.     return out
  201. end
  202.  
  203. -- Movement function
  204.  
  205. function move(direction, length, force)
  206.     -- check fuel and refill if nececary
  207.     if turtle.getFuelLevel() < 10 then
  208.         refuel()
  209.     end
  210.  
  211.     length = length or 1
  212.     force = force or false
  213.     force = not(force)
  214.  
  215.     if direction == "up" then
  216.         while not(turtle.up()) do
  217.             print("Trying to move up")
  218.             if force then
  219.                 print("-couldn't move.")
  220.                 return false
  221.             end
  222.         end
  223.         y_coord = y_coord+1
  224.  
  225.     elseif direction == "down" then
  226.         while not(turtle.down()) do
  227.             print("Trying to move down")
  228.             if force then
  229.                 print("-couldn't move.")
  230.                 return false
  231.             end
  232.         end
  233.         y_coord = y_coord-1
  234.  
  235.     elseif direction == "forw" then
  236.         while not(turtle.forward()) do
  237.             print("Trying to move forward")
  238.             if force then
  239.                 print("-couldn't move.")
  240.                 return false
  241.             end
  242.         end
  243.         if dir == 1 then
  244.             z_coord = z_coord-1
  245.         elseif dir == 2 then
  246.             x_coord = x_coord+1
  247.         elseif dir == 3 then
  248.             z_coord = z_coord+1
  249.         elseif dir == 4 then
  250.             x_coord = x_coord-1
  251.         end
  252.  
  253.     elseif direction == "back" then
  254.         while not(turtle.back()) do
  255.             print("Trying to move back")
  256.             if force then
  257.                 print("-couldn't move.")
  258.                 return false
  259.             end
  260.         end
  261.         if dir == 1 then
  262.             z_coord = z_coord+1
  263.         elseif dir == 2 then
  264.             x_coord = x_coord-1
  265.         elseif dir == 3 then
  266.             z_coord = z_coord-1
  267.         elseif dir == 4 then
  268.             x_coord = x_coord+1
  269.         end
  270.  
  271.     end
  272.  
  273.     print("-XYZ:",x_coord,",",z_coord,",",y_coord)
  274.  
  275.     length = length - 1
  276.     if length>0 then
  277.         move(direction, length)
  278.     end
  279.  
  280.     return true
  281.  
  282. end
  283.  
  284. -- Digging function
  285.  
  286. function dig(direction, length, attack)
  287.     length = length or 1
  288.     attack = attack or false
  289.     i=length
  290.  
  291.     while i>0 do
  292.         -- check if inventory is almost full
  293.         -- do that every 64*x
  294.         if (length%64)==0 then
  295.             print("Checking inventory")
  296.             if checkInv()>12 then
  297.                 emptyInv()
  298.             end
  299.         end
  300.  
  301.         if direction == "up" then
  302.             a,b=nil
  303.             a,b = turtle.digUp()
  304.             while not(a) do
  305.                 print("Trying to dig up")
  306.                 if b ~= nil then
  307.                     print("-there's nothing to dig.")
  308.                     if attack then
  309.                         print("-punching!")
  310.                         for i=1,8,1 do
  311.                             turtle.attackUp()
  312.                         end
  313.                     end
  314.                     break
  315.                 end
  316.             end
  317.  
  318.         elseif direction == "down" then
  319.             a,b=nil
  320.             a,b = turtle.digDown()
  321.             while not(a) do
  322.                 print("Trying to dig down")
  323.                 if b ~= nil then
  324.                     print("-there's nothing to dig.")
  325.                     if attack then
  326.                         print("-punching!")
  327.                         for i=1,8,1 do
  328.                             turtle.attackDown()
  329.                         end
  330.                     end
  331.                     break
  332.                 end
  333.             end
  334.  
  335.         elseif direction == "forw" then
  336.             a,b=nil
  337.             a,b = turtle.dig()
  338.             while not(a) do
  339.                 print("Trying to dig")
  340.                 if b ~= nil then
  341.                     print("-there's nothing to dig.")
  342.                     if attack then
  343.                         print("-punching!")
  344.                         for i=1,8,1 do
  345.                             turtle.attack()
  346.                         end
  347.                     end
  348.                     break
  349.                 end
  350.             end
  351.  
  352.         end
  353.  
  354.         i = i - 1
  355.     end
  356.  
  357.     return true
  358.  
  359. end
  360.  
  361. -- Dig + move function = mine function
  362.  
  363. function mine(direction, length, attack)
  364.     length = length or 1
  365.     attack = attack or nil
  366.  
  367.     -- shouldn't be needed but just in case
  368.     -- could just make it turn 2 times
  369.     if direction == "back" then
  370.         print("Invalid direction.")
  371.         return false
  372.     end
  373.  
  374.     for i=1,length,1 do
  375.         -- check if inventory is almost full
  376.         -- do that every 12*x and first time
  377.         if i==1 or (i%12)==0 then
  378.             print("Checking inventory")
  379.             if checkInv()>12 then
  380.                 emptyInv()
  381.             end
  382.         end
  383.  
  384.         dig(direction,1,attack)
  385.         move(direction)
  386.     end
  387.  
  388.     return true
  389.  
  390. end
  391.  
  392. -- Turn functions
  393.  
  394. function turn(direction, length)
  395.     length = length or 1
  396.  
  397.     if direction == "left" then
  398.         print("Turning left")
  399.         turtle.turnLeft()
  400.         dir = ((dir-2)%4)+1
  401.         print("-direction:",directions[dir])
  402.  
  403.     elseif direction == "right" then
  404.         print("Turning right")
  405.         turtle.turnRight()
  406.         dir = ((dir)%4)+1
  407.         print("-direction:",directions[dir])
  408.  
  409.     elseif direction == "north" then
  410.         print("Turning to north")
  411.         if dir == 2 then
  412.             turn("left")
  413.         elseif dir == 3 then
  414.             turn("left",2)
  415.         elseif dir == 4 then
  416.             turn("right")
  417.         end
  418.  
  419.     elseif direction == "east" then
  420.         print("Turning to east")
  421.         if dir == 3 then
  422.             turn("left")
  423.         elseif dir == 4 then
  424.             turn("left",2)
  425.         elseif dir == 1 then
  426.             turn("right")
  427.         end
  428.  
  429.     elseif direction == "south" then
  430.         print("Turning to south")
  431.         if dir == 4 then
  432.             turn("left")
  433.         elseif dir == 1 then
  434.             turn("left",2)
  435.         elseif dir == 2 then
  436.             turn("right")
  437.         end
  438.  
  439.     elseif direction == "west" then
  440.         print("Turning to west")
  441.         if dir == 1 then
  442.             turn("left")
  443.         elseif dir == 2 then
  444.             turn("left",2)
  445.         elseif dir == 3 then
  446.             turn("right")
  447.         end
  448.  
  449.     end
  450.  
  451.     length = length - 1
  452.     if length>0 then
  453.         turn(direction, length)
  454.     end
  455.  
  456.     return true
  457.  
  458. end
  459.  
  460. -- Empty inventory function
  461.  
  462. function emptyInv()
  463.     print("Starting inventory dump.")
  464.     turtle.select(dumpSlot)
  465.  
  466.     if turtle.back() then
  467.         print("-moving back")
  468.         turtle.place()
  469.         for i=3,16 do
  470.             turtle.select(i)
  471.             turtle.drop()
  472.         end
  473.         turtle.select(dumpSlot)
  474.         turtle.dig()
  475.         turtle.forward()
  476.     elseif turtle.up() then
  477.         print("-moving up")
  478.         turtle.placeDown()
  479.         for i=3,16 do
  480.             turtle.select(i)
  481.             turtle.dropDown()
  482.         end
  483.         turtle.select(dumpSlot)
  484.         turtle.digDown()
  485.         turtle.down()
  486.     elseif turtle.down() then
  487.         print("-moving down")
  488.         turtle.placeUp()
  489.         for i=3,16 do
  490.             turtle.select(i)
  491.             turtle.dropUp()
  492.         end
  493.         turtle.select(dumpSlot)
  494.         turtle.digUp()
  495.         turtle.up()
  496.     else
  497.         print("Couldn't dump inventory! (rogue turn?)")
  498.     end
  499.  
  500.     turtle.select(dumpSlot)
  501.     print("-Done dumping")
  502.  
  503.     return true
  504.  
  505. end
  506.  
  507.  
  508. -- ============
  509. -- Main program
  510. -- ============
  511.  
  512. -- Check if there is anything in slot 1 and 2
  513.  
  514. if turtle.getItemCount(1) == 0 then
  515.     print("WARNING: There is no dump chest!")
  516. end
  517.  
  518. if turtle.getItemCount(2) == 0 then
  519.     print("WARNING: There is no fuel chest!")
  520. end
  521.  
  522. -- /
  523.  
  524. -- === === === === ===
  525. --   END OF LIBRARY
  526. -- === === === === ===
Advertisement
RAW Paste Data Copied
Advertisement