# Turtle Movement Engine

Mar 16th, 2023 (edited)
522
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1.
2. -- f, b, l, r, u, d
3.
4. -- 44, 64, -10
5.
6. -- 39, 64, -10
7.
8. -- localX 0
9. -- localY 5
10.
11. -- f f f f l f f f l f f r f f l f f
12.
13. -- r f f f f l l r
14.
15. -- 0, 4
16.
17. -- 0, -4
18.
19. --   A +x
20. --   l
21. -- <-o->
22. -- -yl +y
23. --   V -x
24.
25. -- Calculate f / r from where you stand. It's all relative to your direction. Important that we keep note of each turn.
26.
27. function travelLogCalc(log)
28.
29.     localX = 0
30.     localY = 0
31.     localZ = 0
32.
33.     -- px, py, mx, my
34.     -- north, east, south, west
35.     directions = {1, 1, -1, -1}
36.     dir = 0
37.     -- Calculate fasted route to travel
38.     for i = #log, 1, -1 do
39.         currentDir = (dir % 4) + 1
40.
41.         move = log[i]
42.
43.         if move == "f" then
44.             if currentDir == 1 or currentDir == 3 then
45.                 localX = localX - directions[(currentDir % 4) + 1]
46.             elseif currentDir == 2 or currentDir == 4 then
47.                 localY = localY - directions[(currentDir % 4) + 1]
48.             end
49.         elseif move == "b" then
50.             if currentDir == 1 or currentDir == 3 then
51.                 localX = localX + directions[(currentDir % 4) + 1]
52.             elseif currentDir == 2 or currentDir == 4 then
53.                 localY = localY + directions[(currentDir % 4) + 1]
54.             end
55.         elseif move == "r" then
56.             dir = dir + 1
57.         elseif move == "l" then
58.             dir = dir - 1
59.         elseif move == "u" then
60.             localZ = localZ - 1
61.         elseif move == "d" then
62.             localZ = localZ + 1
63.         end
64.     end
65.     return { localX, localY, localZ, dir }
66. end
67.
68. function travelEngine(args)
69.   log = {}
70.   localX = args[1]
71.   localY = args[2]
72.   localZ = args[3]
73.   -- we skip adding one since we already have turned to the right
74.   lookAtDir = (args[4] % 4) + 1
75.
76.   turtle.turnRight()
77.   table.insert(log, "r")
78.   if localY > 0 then
79.     for i = 1, localY do
80.       if turtle.forward() then
81.         table.insert(log, "f")
82.       end
83.     end
84.   elseif localY < 0 then
85.     -- invert localY
86.     localY = -localY
87.     for i = 1, localY do
88.       if turtle.back() then
89.         table.insert(log, "b")
90.       end
91.     end
92.   end
93.   turtle.turnLeft()
94.     table.insert(log, "l")
95.   if localX > 0 then
96.     for i = 1, localX do
97.       if turtle.forward() then
98.         table.insert(log, "f")
99.       end
100.     end
101.   elseif localX < 0 then
102.     -- invert localX
103.     localX = -localX
104.     for i = 1, localX do
105.       if turtle.back() then
106.         table.insert(log, "b")
107.       end
108.     end
109.   end
110.   if localZ > 0 then
111.     for i = 1, localZ do
112.       if turtle.up() then
113.         table.insert(log, "u")
114.       end
115.     end
116.   elseif localZ < 0 then
117.     localZ = -localZ
118.     for i = 1, localZ do
119.       if turtle.down() then
120.         table.insert(log, "d")
121.       end
122.     end
123.   end
124.   -- This is a little scuffed
125.   turtle.turnLeft()
126.   table.insert(log, "l")
127.   for i = 1, lookAtDir do
128.     turtle.turnRight()
129.     table.insert(log, "r")
130.   end
131.   return log
132. end
133.
134.
135. function createTravelLog()
136.     log = {}
137.     for i = 1, 3 do
138.         if turtle.forward() then
139.             table.insert(log, "f")
140.         end
141.     end
142.     turtle.turnRight()
143.     table.insert(log, "r")
144.     turtle.forward()
145.     table.insert(log, "f")
146.     turtle.turnRight()
147.     table.insert(log, "r")
148.     for i = 1, 2 do
149.         if turtle.forward() then
150.             table.insert(log, "f")
151.         end
152.     end
153.     --[[
154.     turtle.turnLeft()
155.     table.insert(log, "l")
156.     turtle.forward()
157.     table.insert(log, "f")
158.     turtle.turnLeft()
159.     table.insert(log, "l")
160.     for i = 1, 3 do
161.         if turtle.forward() then
162.             table.insert(log, "f")
163.         end
164.     end
165.     --]]
166.     return log
167. end
168.
169. -- travelLog = {"f", "f", "f", "f", "f", "f", "f", "r", "f","r", "f", "f", "f", "f", "f", "f", "f", "l", "f","l", "f", "f", "f", "f", "f", "f", "f"}
170.
171. travelLog = createTravelLog()
172. print(table.concat(travelLog,", "))
173. cords = travelLogCalc(travelLog)
174. --print(table.concat(cords,", "))
175. wayBack = travelEngine(cords)
176. cords2 = travelLogCalc(wayBack)
177. print(table.concat(wayBack, ", "))
178. print(table.concat(cords2, ", "))
179. travelEngine(cords2)