• API
• FAQ
• Tools
• Archive
SHARE
TWEET

Goto Oeed  Dec 18th, 2014 (edited) 172 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. local hx = 771
2. local hy = 65
3. local hz = -1345
4.
5. local tArgs = {...}
6.
7. if #tArgs ~= 3 then
8.   print('Usage: goto <x> <y> <z>')
9.   return
10. end
11.
12. function location()
13.   local x, y, z = gps.locate(4)
14.   if not x then
15.     error('Could not find location!')
16.   else
17.     return x, y, z
18.   end
19. end
20.
21. function distance(x, y, z)
22.   local _x, _y, _z = location()
23.   local dx = x - _x
24.   local dy = y - _y
25.   local dz = z - _z
26.   return dx, dy, dz
27. end
28.
29. local dir = {
30.   n = 1,
31.   e = 2,
32.   s = 3,
33.   w = 4
34. }
35.
36. function move(d)
37.   if d == 'up' then
38.     if not turtle.up() then
39.       turtle.digUp()
40.       turtle.up()
41.     end
42.   elseif d == 'down' then
43.     if not turtle.down() then
44.       turtle.digDown()
45.       turtle.down()
46.     end
47.   elseif d == 'foward' then
48.     if not turtle.forward() then
49.       turtle.dig()
50.       turtle.forward()
51.     end
52.   elseif d == 'back' then
53.     if not turtle.back() then
54.       turtle.turnLeft()
55.       turtle.turnLeft()
56.       turtle.dig()
57.       turtle.turnLeft()
58.       turtle.turnLeft()
59.       turtle.back()
60.     end
61.   end
62. end
63.
64. function direction()
65.   local x, y, z = location()
66.   move('forward')
67.   local _x, _y, _z = location()
68.   move('back')
69.   if _x > x then
70.     return dir.e
71.   elseif x > _x then
72.     return dir.w
73.   elseif _z > z then
74.     return dir.s
75.   elseif z > _z then
76.     return dir.n
77.   else
78.     error('Unknown direction.')
79.   end
80. end
81.
82. function turn(d)
83.   local turns = direction() - d
84.   if turns == 0 then
85.     return
86.   elseif turns > 0 then
87.     for i = 1, turns do
88.       turtle.turnLeft()
89.     end
90.   else
91.     for i = 1, math.abs(turns) do
92.       turtle.turnRight()
93.     end
94.   end
95. end
96.
97. function goto(x, y, z)
98.   local dx, dy, dz = distance(x, y, z)
99.   if dx > 0 then
100.     turn(dir.e)
101.   elseif dx < 0  then
102.     turn(dir.w)
103.   end
104.
105.   for i = 1, math.abs(dx) do
106.     move('forward')
107.   end
108.
109.   if dz > 0 then
110.     turn(dir.s)
111.   elseif dz < 0 then
112.     turn(dir.n)
113.   end
114.
115.   for i = 1, math.abs(dz) do
116.     move('forward')
117.   end
118.
119.   local f = 'up'
120.   if dy < 0 then
121.     f = 'down'
122.   end
123.
124.   for i = 1, math.abs(dy) do
125.     move(f)
126.   end
127. end
128.
129. print('Going to target!')
130. local tx = tonumber(tArgs)
131. local ty = tonumber(tArgs)
132. local tz = tonumber(tArgs)
133. local cx, cy, cz = location()
134. print('Climbing to cruise height')
135. goto(cx, 255, cz)
136. print('Transversing to target')
137. goto(tx, 255, tz)
138. print('Descending')
139. goto(tx, ty, tz)
140. print('Arrived. Press any key when done')