minecraft_storm

mine code

Feb 24th, 2021 (edited)
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.87 KB | None | 0 0
  1. --[[
  2. Known Issues:
  3.     Also, allow this to work with fuel, though this was originally
  4.     built with fuel disabled, if it runs in a world that requires fuel
  5.     it may get stuck if its not fueled properly.
  6.  
  7. ]]
  8.  
  9. local dn = 15 -- Distance notification, basically tells you how far the turtle has dug in increments
  10. local length, walk = ...
  11. local delay = 0.25 -- This is to accomodate Sand falling, or obsidian forming
  12. local sendaddress = 0
  13. local active = false
  14. modem = peripheral.wrap("right")
  15. modem.transmit(0,1,"new")
  16. modem.close(0)
  17. modem.open(1)
  18. msg = nil
  19. while msg ~= "send" do
  20.     event, side, recive, send, msg = os.pullEvent("modem_message")
  21.     print("waiting for reciveaddress")
  22. end
  23. local reciveaddress = send
  24. modem.transmit(sendaddress,reciveaddress,"reciveaddress"..reciveaddress)
  25. print("send address"..sendaddress.." : reciveaddress"..reciveaddress)
  26. modem.transmit(sendaddress,reciveaddress,"device ok")
  27. msg = nil
  28. while active == false do
  29.     print("waiting for commands")
  30.     os.queueEvent("run", 2, "nil")
  31.     local event, par1, par2, par3, par4, par5 = os.pullEvent()
  32.     if event == "modem_message" then
  33.      side = par1
  34.      recive = par2
  35.      send = parm3
  36.      msg = par4
  37.      dist = par5
  38.     end
  39.     if string.find(msg,",") ~= nil then
  40.      msg1 = string.sub(msg, 0, string.find(msg,","))
  41.      msg2 = string.sub(msg, string.find(msg,",")+1, msg:len())
  42.      print(msg1)
  43.      print(msg2)
  44.     end
  45. end
  46.  
  47. if walk == 'true' then
  48.     walk = true
  49. else
  50.     walk = false
  51. end
  52.  
  53.  
  54. --These variables keep track of the Turtles position and
  55. --orientation relative to the turtles Starting Position
  56. X = 0
  57. Y = 0
  58. Z = 0
  59. O = 0
  60.  
  61. --[[
  62. Orientation List
  63. Forward = 0
  64. Right = 1
  65. Back = 2
  66. Left = 3
  67. ]]--
  68.  
  69.  
  70. -- These functions are used to keep track of
  71. -- The Turtles Position Relative to its
  72. -- Starting Point
  73.  
  74. function left(q)
  75.     if q == nil then q = 1 end
  76.     for i=1,q do
  77.         turtle.turnLeft()
  78.         if O == 0 then
  79.             O = 3
  80.         else
  81.             O = O - 1
  82.         end
  83.     end
  84.     return true
  85. end
  86.  
  87. function right(q)
  88.     if q == nil then q = 1 end
  89.     for i=1,q do
  90.         turtle.turnRight()
  91.         if O == 3 then
  92.             O = 0
  93.         else
  94.             O = O + 1
  95.         end
  96.     end
  97.     return true
  98. end
  99.  
  100. function up(q)
  101.     if q == nil then q = 1 end
  102.     for i=1,q do
  103.         while not turtle.up() do
  104.             digUp()
  105.             turtle.up()
  106.             sleep(delay)
  107.         end
  108.         Y = Y + 1
  109.     end
  110.     return true
  111. end
  112.  
  113. function down(q)
  114.     if q == nil then q = 1 end
  115.     for i=1,q do
  116.         while not turtle.down() do
  117.             digDown()
  118.             turtle.attackDown()
  119.             sleep(delay)
  120.         end
  121.         Y = Y - 1
  122.     end
  123.     return true
  124. end
  125.  
  126. function forward(q)
  127.     if q == nil then q = 1 end
  128.     for i=1,q do
  129.         while not turtle.forward() do
  130.             turtle.dig()
  131.             turtle.attack()
  132.             sleep(delay)
  133.         end
  134.         if O == 0 then
  135.             X = X + 1
  136.         elseif O == 1 then
  137.             Z = Z + 1
  138.         elseif O == 2 then
  139.             X = X - 1
  140.         elseif O == 3 then
  141.             Z = Z - 1
  142.         end
  143.     end
  144.     return true
  145. end
  146.  
  147. function back()
  148.     right()
  149.     right()
  150.     forward()
  151.     left()
  152.     left()
  153.     return true
  154. end
  155.  
  156. function orient(arg1)
  157.     while arg1 ~= O do
  158.             right()
  159.     end
  160.     return true
  161. end
  162.  
  163. function dig()
  164.     while turtle.detect() do
  165.         turtle.dig()
  166.         sleep(delay)
  167.     end
  168.     return true
  169. end
  170.  
  171. function digUp()
  172.     while turtle.detectUp() do
  173.         turtle.digUp()
  174.         sleep(delay)
  175.     end
  176.     return true
  177. end
  178.  
  179. function digDown()
  180.     while turtle.detectDown() do
  181.         turtle.digDown()
  182.         sleep(delay)
  183.     end
  184.     return true
  185. end
  186.  
  187. --[[ Tells turtle where to go for longer movements,
  188.  such as going to and from the chest.
  189.  This may error if theres blocks in the way, but
  190.  Given the way we will have the turtle move in this
  191.  program, it will be fine. How ever if I were to
  192.  copy this function over to another program, I'd
  193.  probably fix this issue.]]
  194. function go(lx,ly,lz)
  195.     if X < lx then
  196.         orient(0)
  197.         for i=1,(lx-X) do
  198.             forward()
  199.         end
  200.     end
  201.    
  202.     if X > lx then
  203.         orient(2)
  204.         for i=1,X-lx do
  205.             forward()
  206.         end
  207.     end
  208.    
  209.     if Y < ly then
  210.         for i=1,ly-Y do
  211.             up()
  212.         end
  213.     end
  214.    
  215.     if Y > ly then
  216.         for i=1,Y-ly do
  217.             down()
  218.         end
  219.     end
  220.    
  221.     if Z < lz then
  222.         orient(1)
  223.         for i=1,lz-Z do
  224.             forward()
  225.         end
  226.     end
  227.    
  228.     if Z > lz then
  229.         orient(3)
  230.         for i=1,Z-lz do
  231.             forward()
  232.         end
  233.     end
  234. end
  235.  
  236.  
  237.  
  238. -- Deposits Items into a chest
  239. -- Warning! This may error if
  240. -- the chest is full, so make sure
  241. -- its emptied
  242. function deposit()
  243.     for i=1,16 do
  244.         turtle.select(i)
  245.         while turtle.drop() == false and turtle.getItemCount() ~= 0 do -- Waits until turtle can deposit item
  246.             sleep(1)
  247.         end
  248.         turtle.drop()
  249.     end
  250.     turtle.select(1)
  251.     return true
  252. end
  253.  
  254.  
  255.  
  256. --[[
  257. This function is what tells the
  258. turtle to mine and when to go back
  259. to empty its inventory
  260. ]]--
  261. function mine()
  262.     dig()
  263.     forward()
  264.     digUp()
  265.     digDown()
  266.     up()
  267.     left()
  268.     dig()
  269.     right(2)
  270.     dig()
  271.     down()
  272.     dig()
  273.     left(2)
  274.     dig()
  275.     down()
  276.     dig()
  277.     right(2)
  278.     dig()
  279.     left()
  280.     up()
  281. end
  282.  
  283.  
  284. -- Checks if turtle is full
  285. function check()
  286.     if turtle.getItemCount(15) == 0 then -- Checks second last slot. I'd like to leave 2 slots empty just to be safe
  287.         return false
  288.     else
  289.         return true
  290.     end
  291. end
  292.  
  293. function notify(include)
  294.     if X % dn == 0 and X ~= 0 then
  295.         print("The turtle has traveled "..X.."/"..length+include.." blocks so far")
  296.     end
  297.     return true
  298. end
  299.  
  300. local dni = 0 -- Blocks for distance notification to include in length, from the walk program
  301. if walk then
  302.     while turtle.detect() == false do
  303.         forward()
  304.         dni = dni + 1
  305.         notify(dni) -- Notifies Distance
  306.     end
  307. end
  308.  
  309. for i=1,length do
  310.     mine()
  311.     notify(dni) -- Notifies Distance
  312.     if turtle.getFuelLevel() ~= "unlimited" and turtle.getFuelLevel() < 100 then
  313.         for f=0, 16 do
  314.             turtle.select(f)
  315.             local valid = turtle.refuel(0)
  316.             if valid then
  317.                 turtle.refuel(10)
  318.             end
  319.         end
  320.     end
  321.     if check() then -- Checks if turtle should deposit items
  322.         local lx,ly,lz = X,Y,Z
  323.         go(0,0,0)
  324.         orient(2) -- Faces Chest
  325.         deposit()
  326.         orient(0)
  327.         go(lx,ly,lz)
  328.     end
  329. end
  330.  
  331. -- Returns to chest and deposits items before ending program
  332. go(0,0,0)
  333. orient(2) -- Faces Chest
  334. deposit()
  335. orient(0)
  336.  
Add Comment
Please, Sign In to add comment