This repository was archived by the owner on Dec 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlocation
More file actions
194 lines (179 loc) · 3.38 KB
/
Copy pathlocation
File metadata and controls
194 lines (179 loc) · 3.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
-- location api/object for keeping track of x,y,z,&o without
-- the use of gps
-- include with os.loadAPI("location")
moves = 0
x = 0
y = 0
z = 0
o = 0
place = {}
function init()
term.clear()
--print("Set location? (y/n)")
local setOption = 'n' --io.read()
if(setOption == 'y' or setOption == 'Y') then
print("Initializing turtle location")
print("x: ")
x = tonumber(io.read())
print("y: ")
y = tonumber(io.read())
print("z: ")
z = tonumber(io.read())
print("orientation (n,s,e,w): ")
oStr = io.read()
o = getOrientationFromStr(oStr)
else
x = 0
y = 0
z = 0
o = 2 -- North
end
place["home"] = {x=x,y=y,z=z,o=o}
place["store"] = {x=-167,y=10,z=320,o=1}
moves = 0
print("Great, the turtle is ready for action")
end
function f(n)
for i = 1, n do
if(turtle.forward()) then
if o == 0 then
z = z + 1
elseif o == 1 then
x = x - 1
elseif o == 2 then
z = z - 1
elseif o == 3 then
x = x + 1
end
else
return false
end
end
moves = moves + 1
return true
end
function b(n)
for i = 1, n do
if(turtle.back()) then
if o == 0 then
z = z - 1
elseif o == 1 then
x = x + 1
elseif o == 2 then
z = z + 1
elseif o == 3 then
x = x - 1
end
else
return false
end
end
moves = moves + 1
return true
end
function u(n)
for i = 1, n do
if(turtle.up()) then
y = y + 1
else
return false
end
end
moves = moves + 1
return true
end
function d(n)
for i = 1, n do
if(turtle.down()) then
y = y - 1
else
return false
end
end
moves = moves + 1
return true
end
function r()
o = (o + 1) % 4
turtle.turnRight()
end
function l()
o = (o + 4 - 1) % 4
turtle.turnLeft()
end
function t()
o = (o + 2) % 4
turtle.turnRight()
turtle.turnRight()
end
-- according to http://minecraft.gamepedia.com/Tutorials/Navigation
function getOrientationFromStr(str)
if(oStr == 'n' or oStr == 'N') then
return 2
elseif (oStr == 'e' or oStr == 'E') then
return 3
elseif (oStr == 's' or oStr == 'S') then
return 0
else
return 1
end
end
function p(oTarget)
local turn = (o + 4 - oTarget) % 4
if turn == 0 then
-- already there
elseif turn == 1 then
l()
elseif turn == 2 then
t()
else
r()
end
end
function where()
print("I'm at " .. x .. ", " .. y .. ", " .. z .. " " .. o)
return {x=x,y=y,z=z,o=o}
end
function goto(placeWrapper)
local target
if(type(placeWrapper) == "string") then
target = place[placeWrapper]
else
target = placeWrapper
end
local xTarget = target.x
local yTarget = target.y
local zTarget = target.z
local oTarget = target.o
while (x ~= xTarget or y ~= yTarget or z ~= zTarget) do
-- x axis W-E
if x < xTarget then
p(3)
f(1)
elseif x > xTarget then
p(1)
f(1)
end
-- z axis N-S
if z < zTarget then
p(0)
f(1)
elseif z > zTarget then
p(2)
f(1)
end
-- y axis D-U
if y < yTarget then
u(1)
elseif y > yTarget then
d(1)
end
end
p(oTarget)
print("Arrived after goto")
where()
end
function goHome()
goto("home")
end
init()