Guest User

nav

a guest
Jun 22nd, 2014
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.72 KB | None | 0 0
  1. --checks for file position and creates it if not
  2. if not fs.exists("nav.position") then
  3. local pos={0,0,0}
  4. print("Creating position file")
  5. file.create("nav.position",pos) end
  6. pos=file.read("nav.position")
  7. x=pos[1]
  8. y=pos[2]
  9. z=pos[3]
  10. --print("true")
  11. print("I am at |x:",x,"|y:",y,"|z:",z)
  12. facing={"north","south","east","west"}
  13. --#checks fuel
  14. if turtle.getFuelLevel() <= 100 then turtle.refuel(1) end
  15. --#Checks for file direction and creates it if not
  16. if not fs.exists("nav.direction") then
  17. file.create("nav.direction","north") end
  18. way=file.read("nav.direction")
  19.  
  20. --#determines cardinal direction
  21. function turn(direction,count)
  22. counter=0
  23. repeat
  24. direction1=direction
  25. --print(direction1)
  26. counter=counter+1
  27. --print(counter)
  28. if way == "north" and direction1 == "right" then
  29. way="east" direction1=0
  30. turtle.turnRight()
  31. end
  32. if way == "north" and direction1 == "left" then
  33. way="west" direction1=0
  34. turtle.turnLeft()
  35. end
  36. if way == "south" and direction1 == "right" then
  37. way="west" direction1=0
  38. turtle.turnRight()
  39. end
  40. if way == "south" and direction1 == "left" then
  41. way="east" direction1=0
  42. turtle.turnLeft()
  43. end
  44. if way == "east" and direction1 == "right" then
  45. way="south" direction1=0
  46. turtle.turnRight()
  47. end
  48. if way == "east" and direction1 == "left" then
  49. way="north" direction1=0
  50. turtle.turnLeft()
  51. end
  52. if way == "west" and direction1 == "right" then
  53. way="north" direction1=0
  54. turtle.turnRight()
  55. end
  56. if way == "west" and direction1 == "left" then
  57. way="south" direction1=0
  58. turtle.turnLeft()
  59. end
  60. --sleep()
  61. until counter==count
  62. file.create("nav.direction",way)
  63. end
  64.  
  65. function getCount()
  66. CD=file.read("nav.direction")
  67. if CD == "north" then xcv=0 zcv=1 end
  68. if CD == "south" then xcv=0 zcv=-1 end
  69. if CD == "east" then xcv=1 zcv=0 end
  70. if CD == "west" then xcv=-1 zcv=0 end
  71. return xcv,zcv
  72. end
  73.  
  74. function forward(count)
  75. dx,dz=getCount()
  76. for i=1,count do
  77. --print(i)
  78. if not turtle.forward() then print("I need fuel") break end
  79. x=x+dx
  80. z=z+dz
  81.  pos={x,y,z}
  82.  file.create("nav.position",pos)
  83.  --sleep()
  84.  end
  85. end
  86. --#goes up and updates y
  87. function up(count)
  88. getCount()
  89. for i=1,count do
  90. turtle.up()
  91. y=y+1
  92. pos={x,y,z}
  93. file.create("nav.position",pos)
  94.  end
  95. end
  96. --# goes down and updates Y
  97. function down(count)
  98. getCount()
  99. for i=1,count do
  100. turtle.down()
  101. y=y-1
  102. pos={x,y,z}
  103. file.create("nav.position",pos)
  104.  end
  105. end
  106. --#calculates new position then navigates to
  107. function goto(nx,ny,nz)
  108. pos=file.read("nav.position")
  109. x=pos[1]
  110. y=pos[2]
  111. z=pos[3]
  112. dx=nx-x
  113. dy=ny-y
  114. dz=nz-z
  115. dx=tostring(dx)
  116. dy=tostring(dy)
  117. dz=tostring(dz)
  118. positive="%d+"
  119. negative="-%d+"
  120. print(dx)
  121. if dx == positive then print("true") turn("right",1) forward(dx) end
  122. end
  123. goto(6,0,0)
  124. print("I am now at |x:",x,"|y:",y,"|z:",z," I am facing: ",CD)
Advertisement
Add Comment
Please, Sign In to add comment