forked from xyqyear/auto-crossbreeding
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathgps.lua
120 lines (102 loc) · 2.18 KB
/
gps.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
local robot = require("robot")
local nowFacing = 1
local nowPos = {0, 0}
local savedPos = {}
local function getFacing()
return nowFacing
end
local function getPos()
return nowPos
end
local function safeForward()
local forwardSuccess
repeat
forwardSuccess = robot.forward()
until forwardSuccess
end
local function turnTo(facing)
local delta = (facing - nowFacing) % 4
nowFacing = facing
if delta <= 2 then
for _=1, delta do
robot.turnRight()
end
else
for _= 1, 4 - delta do
robot.turnLeft()
end
end
end
local function turningDelta(facing)
local delta = (facing - nowFacing) % 4
if delta <= 2 then
return delta
else
return 4-delta
end
end
local function go(pos)
if nowPos[1] == pos[1] and nowPos[2] == pos[2] then
return
end
-- find path
local posDelta = {pos[1]-nowPos[1], pos[2]-nowPos[2]}
local path = {}
if posDelta[1] > 0 then
path[#path+1] = {2, posDelta[1]}
elseif posDelta[1] < 0 then
path[#path+1] = {4, -posDelta[1]}
end
if posDelta[2] > 0 then
path[#path+1] = {1, posDelta[2]}
elseif posDelta[2] < 0 then
path[#path+1] = {3, -posDelta[2]}
end
-- optimal first turn
if #path == 2 and turningDelta(path[2][1]) < turningDelta(path[1][1]) then
path[1], path[2] = path[2], path[1]
end
for i=1, #path do
turnTo(path[i][1])
for _=1, path[i][2] do
safeForward()
end
end
nowPos = pos
end
local function down(distance)
if distance == nil then
distance = 1
end
for _=1, distance do
robot.down()
end
end
local function up(distance)
if distance == nil then
distance = 1
end
for _=1, distance do
robot.up()
end
end
local function save()
savedPos[#savedPos+1] = nowPos
end
local function resume()
if #savedPos == 0 then
return
end
go(savedPos[#savedPos])
savedPos[#savedPos] = nil
end
return {
getFacing = getFacing,
getPos = getPos,
turnTo = turnTo,
go = go,
save = save,
resume = resume,
down = down,
up = up
}