SHOW:
|
|
- or go back to the newest paste.
1 | --navigation api to keep track of location | |
2 | dirTable = {} | |
3 | dirTable[0], dirTable[1], dirTable[2], dirTable[3] = 'front', 'right', 'back', 'left' | |
4 | pos = {x = 0, y = 0, z = 0, dir = 0} | |
5 | ||
6 | if not fs.exists('position.txt') then | |
7 | local position = fs.open('position.txt', 'w') | |
8 | pos.dir = pos.dir % 4 | |
9 | position.write(textutils.serialise(pos)) | |
10 | position.close() | |
11 | else | |
12 | local position = fs.open('position.txt', 'r') | |
13 | pos = textutils.unserialise(position.readAll()) | |
14 | position.close() | |
15 | end | |
16 | ||
17 | function updatePos() | |
18 | pos.dir = pos.dir % 4 | |
19 | local position = fs.open('position.txt', 'w') | |
20 | position.write(textutils.serialise(pos)) | |
21 | position.close() | |
22 | end | |
23 | ||
24 | function returnPos() | |
25 | return pos.x, pos.y, pos.z, pos.dir | |
26 | end | |
27 | ||
28 | function calibrate() | |
29 | pos.x, pos.y, pos.z, pos.dir = 0, 0, 0, 0 | |
30 | updatePos() | |
31 | end | |
32 | ||
33 | function direction(isForward) | |
34 | local xDir = 0 | |
35 | local yDir = 0 | |
36 | pos.dir = pos.dir % 4 | |
37 | if pos.dir == 0 then | |
38 | xDir = 1 | |
39 | yDir = 0 | |
40 | elseif pos.dir == 1 then | |
41 | xDir = 0 | |
42 | yDir = 1 | |
43 | elseif pos.dir == 2 then | |
44 | xDir = -1 | |
45 | yDir = 0 | |
46 | elseif pos.dir == 3 then | |
47 | xDir = 0 | |
48 | yDir = -1 | |
49 | end | |
50 | if isForward == true then | |
51 | pos.x = pos.x + xDir | |
52 | pos.y = pos.y + yDir | |
53 | else | |
54 | pos.x = pos.x - xDir | |
55 | pos.y = pos.y - yDir | |
56 | end | |
57 | updatePos() | |
58 | end | |
59 | ||
60 | function forward(x) | |
61 | for i = 1, x do | |
62 | while not turtle.forward() do | |
63 | turtle.dig() | |
64 | end | |
65 | direction(true) | |
66 | end | |
67 | updatePos() | |
68 | end | |
69 | ||
70 | function back(x) | |
71 | for i = 1, x do | |
72 | repeat until turtle.back() | |
73 | direction(false) | |
74 | end | |
75 | updatePos() | |
76 | end | |
77 | ||
78 | ||
79 | function up(x) | |
80 | for i = 1, x do | |
81 | while not turtle.up() do | |
82 | turtle.digUp() | |
83 | end | |
84 | pos.z = pos.z + 1 | |
85 | end | |
86 | updatePos() | |
87 | end | |
88 | ||
89 | function down(x) | |
90 | for i = 1, x do | |
91 | while not turtle.down() do | |
92 | turtle.digDown() | |
93 | end | |
94 | pos.z = pos.z - 1 | |
95 | end | |
96 | updatePos() | |
97 | end | |
98 | ||
99 | function turnRight(x) | |
100 | for i = 1, x do | |
101 | turtle.turnRight() | |
102 | pos.dir = pos.dir + 1 | |
103 | end | |
104 | updatePos() | |
105 | end | |
106 | ||
107 | function turnLeft(x) | |
108 | for i = 1, x do | |
109 | turtle.turnLeft() | |
110 | pos.dir = pos.dir + 3 | |
111 | end | |
112 | updatePos() | |
113 | end | |
114 | ||
115 | function turnAround() | |
116 | turnLeft(2) | |
117 | end | |
118 | ||
119 | function goDir(dir) | |
120 | while pos.dir % 4 ~= dir do | |
121 | turnLeft() | |
122 | end | |
123 | updatePos() | |
124 | end |