Computercraft thingies
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

mturtle.lua 2.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. local pos = {x = 0, y = 0, z = 0, face = 0}
  2. local function addToPos(n)
  3. if pos.face == Faces.FORWARD then
  4. pos.z = pos.z + n
  5. elseif pos.face == Faces.BACK then
  6. pos.z = pos.z - n
  7. elseif pos.face == Faces.RIGHT then
  8. pos.x = pos.x + n
  9. elseif pos.face == Faces.LEFT then
  10. pos.x = pos.x - n
  11. end
  12. end
  13. local function addToFace(n)
  14. pos.face = (pos.face + n) % 4
  15. if pos.face < 0 then
  16. pos.face = 4 + pos.face
  17. end
  18. end
  19. Faces = {
  20. FORWARD = 0;
  21. RIGHT = 1;
  22. BACK = 2;
  23. LEFT = 3;
  24. };
  25. function getPos()
  26. return {
  27. x = pos.x;
  28. y = pos.y;
  29. z = pos.z;
  30. face = pos.face;
  31. }
  32. end
  33. function dig()
  34. while turtle.detect() do turtle.dig() end
  35. end
  36. function digUp()
  37. while turtle.detectUp() do turtle.digUp() end
  38. end
  39. function digDown()
  40. while turtle.detectDown() do turtle.digDown() end
  41. end
  42. function right()
  43. turtle.turnRight()
  44. addToFace(1)
  45. end
  46. function left()
  47. turtle.turnLeft()
  48. addToFace(-1)
  49. end
  50. function forward()
  51. while not turtle.forward() do end
  52. addToPos(1)
  53. end
  54. function back()
  55. while not turtle.back() do end
  56. addToPos(-1)
  57. end
  58. function up()
  59. while not turtle.up() do end
  60. pos.y = pos.y + 1
  61. end
  62. function down()
  63. while not turtle.down() do end
  64. pos.y = pos.y - 1
  65. end
  66. function digForwards()
  67. dig()
  68. forward()
  69. digUp()
  70. digDown()
  71. end
  72. function alterturn(i)
  73. if i % 2 == 1 then
  74. right()
  75. else
  76. left()
  77. end
  78. end
  79. function selectItem()
  80. if turtle.getItemCount(turtle.getSelectedSlot()) > 0 then
  81. return
  82. end
  83. for i = 1, 16 do
  84. if turtle.getItemCount(i) > 0 then
  85. turtle.select(i)
  86. return true
  87. end
  88. end
  89. turtle.select(1)
  90. return false
  91. end
  92. function turnTo(face)
  93. if pos.face == 0 and face == 3 or
  94. pos.face == 1 and face == 0 or
  95. pos.face == 2 and face == 1 or
  96. pos.face == 3 and face == 2 then
  97. left()
  98. else
  99. while pos.face ~= face do
  100. right()
  101. end
  102. end
  103. end
  104. function goTo(dest)
  105. -- Correct X value
  106. if dest.x > pos.x then
  107. turnTo(Faces.RIGHT)
  108. elseif dest.x < pos.x then
  109. turnTo(Faces.LEFT)
  110. end
  111. for i = 1, math.abs(dest.x - pos.x) do
  112. forward()
  113. end
  114. -- Correct Z value
  115. if dest.z > pos.z then
  116. turnTo(Faces.FORWARD)
  117. elseif dest.z < pos.z then
  118. turnTo(Faces.BACK)
  119. end
  120. for i = 1, math.abs(dest.z - pos.z) do
  121. forward()
  122. end
  123. -- Correct Y value
  124. while dest.y > pos.y do up() end
  125. while dest.y < pos.y do down() end
  126. -- Correct facing
  127. turnTo(dest.face)
  128. end