forked from Courseplay/courseplay
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TrafficController.lua
315 lines (288 loc) · 11.3 KB
/
TrafficController.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
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
--[[
This file is part of Courseplay (https://github.com/Courseplay/courseplay)
Copyright (C) 2018 Peter Vajko
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
]]
--- A reservation we put in the reservation table.
Reservation = CpObject()
function Reservation:init(vehicleId, timeStamp)
self.vehicleId = vehicleId
self.timeStamp = timeStamp
end
--- TrafficController provides a cooperative collision avoidance facility for all Courseplay driven vehicles.
--
-- The TrafficController is a singleton object and should be initialized once after CP is loaded and
-- then call update() to update its clock (the clock is needed to remove stale reservations)
--
-- Vehicles should call reserve() when they reach a waypoint to reserve the next section of their path and to make sure
-- their path is not in conflict with another vehicle's future path.
--
-- Reservations are per tile in a grid representing the map. When a vehicle asks for a reservation, TrafficController
-- reserves the tiles under the future path of the vehicle (based on the course it is driving).
--
-- TrafficController looks into the future for lookaheadTimeSeconds (30 by default) only. So when a vehicle calls
-- reserve() with a waypoint index, only the part of the course lying within lookaheadTimeSeconds from that waypoint
-- is actually reserved.
--
-- The calculation is based on the speed stored in the course, or if that does not exist, the speed passed in to
-- reserve() or if none, it defaults to 10 km/h.
--
-- When reserve() is called, TrafficController also frees all tiles reserved for the waypoints behind the passed
-- in waypoint index.
--
-- When the course of the vehicle is updated or multiple waypoints are skipped, the vehicle should call cancel()
-- to cancel all existing reservations and then reserve() again from the current waypoint index.
--
-- TrafficController also periodically cleans up all stale reservations based on the timestamp recorded at
-- the time of the reservation and on the internal clock value. This is to make sure that forgotten reservations
-- don't block other vehicles forever.
--
-- Usage:
---------
-- After init() is called once to initialize the TrafficController singleton, call update()
-- periodically to update the internal clock and trigger the cleanup when necessary.
--
-- Vehicles should be calling reserve() once start moving with the current waypoint index and
-- check the return value.
-- If reserve returns false it means it could not reserve all the tiles for the next lookaheadTimeSeconds
-- and the vehicle should stop as its path is conflicting with another vehicles's path. The stopped
-- vehicle keeps calling reserve() until it returns true, at that point the path should be clear.
--
TrafficController = CpObject()
function TrafficController:init()
self.prevTimeString = getDate(TrafficController.dateFormatString)
self.clock = 0
-- this is our window of traffic awareness, we only plan for the next 30 seconds
self.lookaheadTimeSeconds = 30
-- the reservation table grid size in meters. This should be less than the maximum waypoint distance
self.gridSpacing = 2.5
-- every so often we clean up stale reservations
self.cleanUpIntervalSeconds = 30
self.staleReservationTimeoutSeconds = 3 * self.lookaheadTimeSeconds
-- this holds all the reservations
self.reservations = {}
self.dateFormatString = '%H%M%S'
end
--- Update our clock and take care of stale entries
-- This should be called once in an update cycle (globally, not vehicle specific)
function TrafficController:update(dt)
-- The Giants engine does not seem to provide a clock, so implement our own.
local currentTimeString = getDate(TrafficController.dateFormatString)
if self.prevTimeString ~= currentTimeString then
self.prevTimeString = currentTimeString
self.clock = self.clock + 1
end
if self.clock % self.cleanUpIntervalSeconds == 0 then
self:cleanUp()
end
end
--- Make a reservation for the next lookaheadTimeSeconds interval
-- @param vehicleId unique ID of the reserving vehicle
-- @param course vehicle course
-- @param fromIx index of the course waypoint where we start the reservation
-- @param speed expected speed of the vehicle in km/h. If not given will use the speed in the course.
-- @return true if successfully reserved _all_ tiles. When returning false it may
-- reserve some of the tiles though.
function TrafficController:reserve(vehicleId, course, fromIx, speed)
self:freePreviousTiles(vehicleId, course, fromIx, speed)
local ok = self:reserveNextTiles(vehicleId, course, fromIx, speed)
return ok
end
--- Free waypoints already passed
-- use the link to the previous tile to walk back until the oldest one is reached.
function TrafficController:freePreviousTiles(vehicleId, course, fromIx, speed)
local tiles = self:getGridPointsUnderCourse(course, self:backwardIterator(fromIx), speed)
for i = 1, #tiles do
self:freeGridPoint(tiles[i], vehicleId)
end
end
function TrafficController:reserveNextTiles(vehicleId, course, fromIx, speed)
local ok = true
local gridPoints = self:getGridPointsUnderCourse(course, self:forwardIterator(fromIx, course:getNumberOfWaypoints() - 1), speed)
for i = 1, #gridPoints do
ok = ok and self:reserveGridPoint(gridPoints[i], Reservation(vehicleId, self.clock))
end
return ok
end
--- Get the list of tiles the segment of the course defined by the iterator is passing through, using the
-- speed in the course or the one supplied here. Will find the tiles reached in lookaheadTimeSeconds only
-- (based on the speed and the waypoint distance)
function TrafficController:getGridPointsUnderCourse(course, iterator, speed)
local tiles = {}
local travelTimeSeconds = 0
for i in iterator() do
local v = speed or course.waypoints[i].speed or 10
local s = course:getDistanceToNextWaypoint(i)
local x, z = self:getGridCoordinates(course.waypoints[i])
table.insert(tiles, Point(x, z))
-- if waypoints are further apart than our grid spacing then we need to add points
-- in between to not miss a tile
if s > self.gridSpacing then
local ips = self:getIntermediatePoints(course.waypoints[i], course.waypoints[i + 1])
for _, wp in ipairs(ips) do
x, z = self:getGridCoordinates(wp)
table.insert(tiles, Point(x, z))
end
end
travelTimeSeconds = travelTimeSeconds + s / (v / 3.6)
if travelTimeSeconds > self.lookaheadTimeSeconds then
return tiles
end
end
-- if we ended up here then we went all the way to the waypoint before the last, so
-- add the last one here
local x, z = self:getGridCoordinates(course.waypoints[course:getNumberOfWaypoints()])
table.insert(tiles, Point(x, z))
return tiles
end
--- If waypoint a and b a farther apart than the grid spacing then we need to
-- add points in between so wo don't miss a tile
function TrafficController:getIntermediatePoints(a, b)
local dx, dz = b.x - a.x, b.z - a.z
local d = math.sqrt(dx * dx + dz * dz)
local nx, nz = dx / d, dz / d
local nPoints = math.floor((d - 0.001) / self.gridSpacing) -- 0.001 makes sure we have only one wp even if a and b are exactly on the grid
local x, z = a.x, a.z
local intermediatePoints = {}
for i = 1, nPoints do
x, z = x + self.gridSpacing * nx, z + self.gridSpacing * nz
table.insert(intermediatePoints, {x = x, z = z})
end
return intermediatePoints
end
--- Add tiles around x, z to the list of tiles.
function TrafficController:getTilesAroundPoint(point)
return {
point,
Point(point.x - 1, point.z),
Point(point.x + 1, point.z),
Point(point.x, point.z - 1),
Point(point.x, point.z + 1)
}
end
--- Reserve a grid point. This will reserve the tile the point is on and the adjacent tiles (above, below, left and right,
-- but not diagonally) as well to make sure the vehicle has enough clearance from all sides.
function TrafficController:reserveGridPoint(point, reservation)
-- reserve tiles around point
for _, tile in ipairs(self:getTilesAroundPoint(point)) do
if not self:reserveTile(tile, reservation) then
return false
end
end
return true
end
function TrafficController:freeGridPoint(point, vehicleId)
-- free tiles around point
for _, tile in ipairs(self:getTilesAroundPoint(point)) do
self:freeTile(tile, vehicleId)
end
end
function TrafficController:freeTile(point, vehicleId)
if not self.reservations[point.x] then
return
end
if not self.reservations[point.x][point.z] then
return
end
if self.reservations[point.x][point.z].vehicleId == vehicleId then
-- no more reservations left, remove entry
self.reservations[point.x][point.z] = nil
end
end
function TrafficController:reserveTile(point, reservation)
if not self.reservations[point.x] then
self.reservations[point.x] = {}
end
if self.reservations[point.x][point.z] then
if self.reservations[point.x][point.z].vehicleId == reservation.vehicleId then
-- already reserved for this vehicle
return true
else
-- reserved for another vehicle
return false
end
end
self.reservations[point.x][point.z] = reservation
return true
end
function TrafficController:getGridCoordinates(wp)
local gridX = math.floor(wp.x / self.gridSpacing)
local gridZ = math.floor(wp.z / self.gridSpacing)
return gridX, gridZ
end
--- Cancel all reservations for a vehicle
function TrafficController:cancel(vehicleId)
for row in pairs(self.reservations) do
for col in pairs(self.reservations[row]) do
local reservation = self.reservations[row][col]
if reservation and reservation.vehicleId == vehicleId then
self.reservations[row][col] = nil
end
end
end
end
--- Clean up all stale reservations
function TrafficController:cleanUp(vehicleId)
local nTotalReservedTiles = 0
local nFreedTiles = 0
for row in pairs(self.reservations) do
for col in pairs(self.reservations[row]) do
local reservation = self.reservations[row][col]
if reservation then
nTotalReservedTiles = nTotalReservedTiles + 1
if reservation.timeStamp <= (self.clock - self.staleReservationTimeoutSeconds) then
self.reservations[row][col] = nil
nFreedTiles = nFreedTiles + 1
nTotalReservedTiles = nTotalReservedTiles - 1
end
end
end
end
self:debug('Clean up: freed %d tiles, total %d reserved tiles remaining.', nFreedTiles, nTotalReservedTiles)
end
function TrafficController:forwardIterator(from, to)
return function()
local i, n = from - 1, to
return function()
i = i + 1
if i <= n then return i end
end
end
end
function TrafficController:backwardIterator(from)
return function()
local i = from
return function()
i = i - 1
if i >= 1 then return i end
end
end
end
--- Cancel all reservations for a vehicle
function TrafficController:__tostring()
local result = ''
for row = 0, 9 do
for col = 0, 9 do
local reservation = self.reservations[row] and self.reservations[row][col]
if reservation then
result = result .. reservation.vehicleId
else
result = result .. '.'
end
end
result = result .. '\n'
end
return result
end
function TrafficController:debug(...)
courseplay:debug(string.format(...), 12)
end