# Internal tracking system for computercraft turtles BETA | test branch

Feb 27th, 2021 (edited)
1,086
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. local xPos, yPos, zPos = nil
2. face = nil
3. cal = false
4.
5. function setLocation() -- get gps using other computers
6.  xPos, yPos, zPos = gps.locate()
7.  if turtle.forward() then
8.  nxPos, nyPos, nzPos = gps.locate()
9.  end
10.  turtle.back()
11.  if zPos > nzPos then --if north
12.     face = 0
13.  end
14.  if xPos < nxPos then --if east
15.     face = 3
16.  end
17.  if zPos < nzPos then --if south
18.     face = 2
19.  end
20.  if xPos > nxPos then --if west
21.     face = 1
22.  end
23.  cal = true
24. end
25.
26. function manSetLocation(x, y, z, f) -- manually set location
27.  xPos = x
28.  yPos = y
29.  zPos = z
30.  face = f
31.  cal = true
32. end
33.
34. function getLocation() -- return the location
35.  if xPos ~= nil then
36.   return xPos, yPos, zPos, face
37.  else
38.   return nil
39.  end
40. end
41.
42. function turnLeft() -- turn left
43. if turtle.turnLeft() then
44.  if face == 0 then
45.   face = 1
46.     elseif face == 1 then
47.     face = 2
48.     elseif face == 2 then
49.     face = 3
50.     elseif face == 3 then
51.     face = 0
52.   end
53. end
54. end
55.
56. function turnRight() -- turn right
57. if turtle.turnRight() then
58.  if face == 0 then
59.   face = 3
60.     elseif face == 1 then
61.     face = 0
62.     elseif face == 2 then
63.     face = 1
64.     elseif face == 3 then
65.     face = 2
66.  end
67. end
68. end
69.
70. function forward() -- go forward
71.  if turtle.forward() then
72.    if cal == true then
73.    if face == 0 then
74.       zPos = zPos - 1
75.     elseif face == 1 then
76.       xPos = xPos - 1
77.     elseif face == 2 then
78.       zPos = zPos + 1
79.     elseif face == 3 then
80.       xPos = xPos + 1
81.    end
82.    else
83.       print("Not Calibrated.")
84.    end
85.    end
86.  end
87.
88. function back() -- go back
89.  if turtle.back() then
90.    if cal == true then
91.    if face == 0 then
92.       zPos = zPos + 1
93.    elseif face == 1 then
94.       xPos = xPos + 1
95.    elseif face == 2 then
96.       zPos = zPos - 1
97.    elseif face == 2 then
98.       xPos = xPos - 1
99.    end
100.    else
101.       print("Not Calibrated.")
102.    end
103.    end
104. end
105. function up() -- go up
106.  turtle.up()
107.  if cal == true then
108.   yPos = yPos + 1
109.  else
110.   print("Not Calibrated.")
111.  end
112. end
113.
114. function down() -- go down
115.  turtle.down()
116.  if cal == true then
117.   yPos = yPos - 1
118.  else
119.   print("Not Calibrated.")
120.  end
121. end
122.
123. function saveData(l)
124.    if not fs.isDir(l) then
125.       local f = fs.open(l,"a")
126.       if cal ~= false then
127.             f.writeLine("Current location: "..xPos..", "..yPos..", "..zPos..", Face:"..face)
128.       else
129.          if gps.locate() then
130.             x,y,z = gps.locate()
131.             f.writeLine("Current location: "..x..", "..y..", "..z..", Face: N/A")
132.          else
133.             error("location unknown")
134.          end
135.          f.close()
136.
137.       end
138.    else
139.       print("Dir With that name already exists")
140.    end
141. end
RAW Paste Data