nSun

CC API pos

Apr 4th, 2014
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.75 KB | None | 0 0
  1. function correctfacing()
  2.     if (facingnum == 5) then
  3.         facingnum = 1
  4.     elseif (facingnum == 0) then
  5.         facingnum = 4
  6.     end
  7. end
  8.  
  9. function getDir()
  10.     local facing = ""
  11.     if (facingnum == 1) then
  12.         facing = "n"
  13.     elseif (facingnum == 2) then
  14.         facing = "e"
  15.     elseif (facingnum == 3) then
  16.         facing = "s"
  17.     elseif (facingnum == 4) then
  18.         facing = "w"
  19.     end
  20.     return facing
  21. end
  22.  
  23. function facedir(dir)
  24.     if (dir == "n") then
  25.         dirnum = 1
  26.     elseif (dir == "e") then
  27.         dirnum = 2
  28.     elseif (dir == "s") then
  29.         dirnum = 3
  30.     elseif (dir == "w") then
  31.         dirnum = 4
  32.     end
  33.     facingprocess = 1
  34.     while (facingprocess == 1) do
  35.         if (facingnum < dirnum) then
  36.             turtle.turnRight()
  37.             facingnum = (facingnum + 1)
  38.         elseif (facingnum > dirnum) then
  39.             turtle.turnLeft()
  40.             facingnum = (facingnum - 1)
  41.         elseif (facingnum == dirnum) then
  42.             facingprocess = 0
  43.         end
  44.         correctfacing()
  45.     end
  46. end
  47.  
  48. -- Creuse le passage
  49. function tdig()
  50.     while turtle.detect() do
  51.         if turtle.dig() or turtle.attack() then
  52.             sleep(0.5)
  53.         else break end
  54.     end
  55. end
  56.  
  57. function tdigUp()
  58.     while turtle.detectUp() do
  59.         if turtle.digUp() or turtle.attackUp() then
  60.             sleep(0.5)
  61.         else break end
  62.     end
  63. end
  64.  
  65. -- Mouvements
  66. function tforward()
  67.     tdigUp()
  68.     if turtle.forward() then
  69.         if (facingnum == 1) then
  70.             z = (z - 1)
  71.         elseif (facingnum == 2) then
  72.             x = (x + 1)
  73.         elseif (facingnum == 3) then
  74.             z = (z + 1)
  75.         elseif (facingnum == 4) then
  76.             x = (x - 1)
  77.         end
  78.         return true
  79.     else
  80.         return false
  81.     end
  82. end
  83.  
  84. function tup()
  85.     if turtle.up() then
  86.         y = (y+1)
  87.         return true
  88.     else
  89.         return false
  90.     end
  91. end
  92.  
  93. function tdown()
  94.     if turtle.down() then
  95.         y = (y-1)
  96.         return true
  97.     else
  98.         return false
  99.     end
  100. end
  101.  
  102. function tright()
  103.     turtle.turnRight()
  104.     facingnum = (facingnum + 1)
  105.     correctfacing()
  106. end
  107.  
  108. function tleft()
  109.     turtle.turnLeft()
  110.     facingnum = (facingnum - 1)
  111.     correctfacing()
  112. end
  113.  
  114. function flyto(tarx,tary,tarz)
  115.     tx = tonumber(tarx)
  116.     ty = tonumber(tary)
  117.     tz = tonumber(tarz)
  118.     fly = 1
  119.     flyingloop()
  120. end
  121.  
  122. function flyingloop()
  123.     while (fly == 1) do
  124.         if (tonumber(y) < tonumber(ty)) then
  125.             tup()
  126.         elseif (tonumber(z) > tonumber(tz)) then
  127.             facedir("n")
  128.             tforward()
  129.         elseif (tonumber(z) < tonumber(tz)) then
  130.             facedir("s")
  131.             tforward()
  132.         elseif (tonumber(x) < tonumber(tx)) then
  133.             facedir("e")
  134.             tforward()
  135.         elseif (tonumber(x) > tonumber(tx)) then
  136.             facedir("w")
  137.             tforward()
  138.         elseif (tonumber(y) > tonumber(ty)) then
  139.             tdown()
  140.         else
  141.             fly = 0
  142.         end
  143.     end
  144. end
  145.  
  146. function setcoords()
  147.     term.clear()
  148.     term.setCursorPos(1,1)
  149.     print("Please input target coordinates")
  150.     write("X: ")
  151.     targetx = read()
  152.     write("Y: ")
  153.     targety = read()
  154.     write("Z: ")
  155.     targetz = read()
  156.     print("Coordinates set.")
  157.     sleep(2)
  158. end
  159.  
  160. function status()
  161.     term.clear()
  162.     term.setCursorPos(1,1)
  163.     facing = getDir()
  164.     print("X: "..x.." Y: "..y.." Z: "..z.." Facing: "..facing)
  165.     local fuelLevel = tostring( turtle.getFuelLevel() )
  166.     print("Fuel level: "..fuelLevel)
  167.     sleep(0.5)
  168. end
  169.  
  170. function calibrate()
  171.     print("Please input turtles current X, Y and Z coordinates.")
  172.     write("X: ")
  173.     tempx = read()
  174.     x = tonumber(tempx)
  175.     write("Y: ")
  176.     tempy = read()
  177.     y = tonumber(tempy)
  178.     write("Z: ")
  179.     tempz = read()
  180.     z = tonumber(tempz)
  181.     print("")
  182.     print("Coordinates set.")
  183.     print("Please input the facing of the turtle, n / e / s / w")
  184.     write("Facing: ")
  185.     facingloop = 1
  186.     while (facingloop == 1) do
  187.         facing = read()
  188.         if (facing == "n") then
  189.             facingloop = 0
  190.             facingnum = 1
  191.         elseif (facing == "e") then
  192.             facingloop = 0
  193.             facingnum = 2
  194.         elseif (facing == "s") then
  195.             facingloop = 0
  196.             facingnum = 3
  197.         elseif (facing == "w") then
  198.             facingloop = 0
  199.             facingnum = 4
  200.         else
  201.             print("Invalid input")
  202.         end
  203.     end
  204.     print("Facing set.")
  205.     sleep(1)
  206. --  main()
  207. end
Advertisement
Add Comment
Please, Sign In to add comment