-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpathfinder.lua
162 lines (134 loc) · 3.48 KB
/
pathfinder.lua
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
directions = {
north = 0, --Negativez
east = 1, --PostiveX
south = 2, --PostiveZ
west = 3 --NegativeX
}
local function turnTo(targetDirection)
targetDirection = targetDirection % 4
local startDirection = turtle.getLocation().direction
--0 for nothing, -1 for left, 1 for right
local rotating = 0
local diff = targetDirection - startDirection
if diff == 0 then
rotating = 0 --Dont rotate
elseif(diff > 2 or diff < 0) then
rotating = -1
elseif(diff <=2) then
rotating = 1
end
if rotating > 0 then
while(startDirection ~= targetDirection) do
turtle.turnRight()
startDirection = (startDirection + 1) % 4
end
elseif rotating < 0 then
while(startDirection ~= targetDirection) do
turtle.turnLeft()
startDirection = (startDirection - 1) % 4
end
end
end
local function moveForward(steps)
for step=1, math.abs(steps) do
while not turtle.forward() do
end
end
end
local function moveBackward(steps)
for step=1, math.abs(steps) do
while not turtle.back() do
end
end
end
--Moving relative
function moveX(xSteps, backwards)
if( backwards == nil) then
backwards = false
end
local gotoDirection = 0
if(xSteps > 0) then
gotoDirection = directions.east
elseif(xSteps < 0) then
gotoDirection = directions.west
end
if backwards then
gotoDirection = gotoDirection + 2
end
if(backwards) then print("moving backwards") else print("moving forward") end
turnTo(gotoDirection)
if(backwards) then
moveBackward(xSteps)
else
moveForward(xSteps)
end
end
function moveZ(zSteps, backwards)
if( backwards == nil) then
backwards = false
end
local gotoDirection = 0
if(zSteps > 0) then
gotoDirection = directions.south
elseif(zSteps < 0) then
gotoDirection = directions.north
end
if backwards then
gotoDirection = gotoDirection + 2
end
turnTo(gotoDirection)
if(backwards) then
moveBackward(zSteps)
else
moveForward(zSteps)
end
end
function moveY(ySteps)
if(ySteps > 0) then
for step = 1, math.abs(ySteps) do
while not turtle.up() do end
end
end
if(ySteps < 0) then
for step = 1, math.abs(ySteps) do
while not turtle.down() do end
end
end
end
--Moving to absolute
function gotoX(xCoord)
local currentX = turtle.getLocation().x
local diff = xCoord - currentX
moveX(diff)
end
function gotoZ(zCoord)
local currentZ = turtle.getLocation().z
local diff = zCoord - currentZ
moveZ(diff)
end
function gotoY(yCoord)
local currentY = turtle.getLocation().y
local diff = yCoord - currentY
moveY(diff)
end
function goto(x, y, z, order, direction)
if not x or not y or not z or not order then
error("missing parameters, use : <x> <y> <z> <order> [direction]")
end
if #order ~= 3 then
print("order paramter shoul container only x, y, or z, each one")
end
for i=1,#order do
local direction = order:sub(i, i)
if(direction == "x") then
gotoX(x)
elseif(direction == "y") then
gotoY(y)
elseif(direction == "z") then
gotoZ(z)
else
error("unknown order type, only x, y, or z allowed")
end
end
if direction then turnTo(direction) end
end