Advertisement
kreezxil

Dig - Tell your turtle to dig in a direction for Turtlecraft

Sep 3rd, 2013
314
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.58 KB | None | 0 0
  1. --[[
  2. ======================= A KreezCo Production =============================
  3. ==  Written by: Kreezxil                                                ==
  4. ==  Orignal Code by: M_I_Z_E                                            ==
  5. ==  Many Parts of this code was barrowed from the original turtle       ==
  6. ==    programs that come with computer craft.                           ==
  7. ==    http://www.computercraft.info/                                    ==
  8. ==  YouTube channel Name: Adventures in Casual Gaming                   ==
  9. ==  URL: http://www.youtube.com/user/kreezxil                           ==
  10. ==  Original URL: http://www.youtube.com/user/MIZ3craft                 ==
  11. ==  Please link to my channel and give proper credit if using my code.  ==
  12. ==  Thanks for watching!                                                ==
  13. ==========================================================================
  14. --]]
  15.  
  16. --[[ Begin Library
  17.      Libary by Kreezxil 7/29/2013
  18. --]]
  19.  
  20. function refuel()
  21.     --Check fuel and refuel if nessesary
  22.     --Code source: sethbling http://www.youtube.com/watch?v=DSsx4VSe-Uk&feature=share&list=SP2Qvl4gaBge02Eh4AqtDSWg3sojt3jeRO
  23.     if turtle.getFuelLevel() < 200 then
  24.         turtle.select(1)
  25.         turtle.refuel(1)
  26.     end
  27. end
  28.  
  29. function dig(direction, distance)
  30.     travel = 0
  31.     repeat
  32.         --[[ do we have fuel? --]]
  33.         refuel()
  34.         travel = travel + 1
  35.         if direction == "f" then
  36.             if turtle.dig() then
  37.                 turtle.suck()
  38.             end
  39.             if distance > 0 then
  40.                 turtle.forward()
  41.             end
  42.         elseif direction == "u" then
  43.             if turtle.digUp() then
  44.                 turtle.suckUp()
  45.             end
  46.             if distance > 0 then
  47.                 turtle.up()
  48.             end
  49.         elseif direction == "d" then
  50.             if turtle.digDown() then
  51.                 turtle.suckDown()
  52.             end
  53.             if distance > 0 then
  54.                 turtle.down()
  55.             end
  56.         elseif direction == "b" then
  57.             turtle.back()
  58.         end
  59.     until travel == distance
  60. end
  61.  
  62. --[[ End Library --]]
  63.  
  64. local tArgs = { ... }
  65. local dist = 1
  66. --[[ test how many args were passed, if none set dist to 0 and assume
  67. forward facing. --]]
  68. if #tArgs < 1 then
  69.     --[[ print usage --]]
  70.     print("Usage: dig [direction] <distance>")
  71.     print()
  72.     print("Place fuel in slot 1")
  73.     print("Give command to dig")
  74.     return
  75. else
  76.     --[[ dist = tonumber( tArgs[1] ) --]]
  77.     --[[ parses passed parameters and sets variables despite order of values --]]
  78.     print("Args Present: "..#tArgs)
  79.     for n=1,#tArgs do
  80.         if n == 3 then
  81.             --[[ in the future subsequent arguments will be accepted
  82.             as commandline mapping instructions
  83.             --]]
  84.             break
  85.         end
  86.         test = string.sub( tArgs[n], 1, 1)
  87.         test = string.lower( test )
  88.         if string.find("fdub",test) ~= nil then
  89.             direction = test
  90.         else
  91.             dist = tonumber(tArgs[n])
  92.         end
  93.     end
  94. end
  95.  
  96. --[[ Process command directives --]]
  97. paramError = 0
  98. if dist == nil or tonumber(dist) < 0 then
  99.     print("Distance must be a number from 0 on up.")
  100.     paramError = paramError + 1
  101. end
  102. if string.find("fdub",direction) == nil then
  103.     print("Direction must be one of: forward, down, up, back, f, d, u, b")
  104.     paramError = paramError + 1
  105. end
  106.  
  107. if paramError > 0 then
  108.     print("Number of parameter errors are " .. paramError)
  109.     print("Please think about your options.")
  110.     return
  111. end
  112.  
  113. dig(direction,dist)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement