-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAutoDriveDijkstraLive.lua
356 lines (304 loc) · 11.9 KB
/
AutoDriveDijkstraLive.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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
function AutoDrive:dijkstraLiveLongLine(current_in, linked_in, target_id)
local current = current_in
local linked = linked_in
local newdist = 0
local distanceToAdd = 0;
local angle = 0;
local current_pre = 0
if
AutoDrive.mapWayPoints[linked].incoming ~= nil
and AutoDrive.mapWayPoints[linked].out ~= nil
and #AutoDrive.mapWayPoints[linked].incoming == 1
and #AutoDrive.mapWayPoints[linked].out == 1
then
if nil == AutoDrive.dijkstraCalc.distance[current] then
AutoDrive.dijkstraCalc.distance[current] = 10000000
end
newdist = AutoDrive.dijkstraCalc.distance[current]
while
#AutoDrive.mapWayPoints[linked].incoming == 1
and #AutoDrive.mapWayPoints[linked].out == 1
and not (linked == target_id)
do
distanceToAdd = 0;
angle = 0;
if nil == AutoDrive.dijkstraCalc.pre[current] then
AutoDrive.dijkstraCalc.pre[current] = -1
end
if AutoDrive.setting_useFastestRoute == true then
if AutoDrive.dijkstraCalc.pre[current] ~= nil and AutoDrive.dijkstraCalc.pre[current] ~= -1 then
distanceToAdd, angle = AutoDrive:getDriveTimeBetweenNodes(current, linked, AutoDrive.dijkstraCalc.pre[current],nil,false);
else
distanceToAdd, angle = AutoDrive:getDriveTimeBetweenNodes(current, linked, nil,nil,false);
end
else
distanceToAdd = AutoDrive:getDistanceBetweenNodes(current, linked);
if AutoDrive.dijkstraCalc.pre[current] ~= nil and AutoDrive.dijkstraCalc.pre[current] ~= -1 then
local wp_current = AutoDrive.mapWayPoints[current]
local wp_ahead = AutoDrive.mapWayPoints[linked]
local wp_ref = AutoDrive.mapWayPoints[AutoDrive.dijkstraCalc.pre[current]]
angle = AutoDrive.angleBetween({x = wp_ahead.x - wp_current.x, z = wp_ahead.z - wp_current.z}, {x = wp_current.x - wp_ref.x, z = wp_current.z - wp_ref.z})
angle = math.abs(angle)
else
angle = 0
end
end;
if math.abs(angle) > 90 then
newdist = 10000000;
end;
newdist = newdist + distanceToAdd
AutoDrive.dijkstraCalc.pre[linked] = current
current = linked
linked = AutoDrive.mapWayPoints[current].out[1]
if nil == AutoDrive.dijkstraCalc.pre[linked] then
AutoDrive.dijkstraCalc.pre[linked] = -1
end
current_pre = AutoDrive.dijkstraCalc.pre[linked]
end -- while...
distanceToAdd = 0;
angle = 0;
if nil == AutoDrive.dijkstraCalc.pre[current] then
AutoDrive.dijkstraCalc.pre[current] = -1
end
if AutoDrive.setting_useFastestRoute == true then
if AutoDrive.dijkstraCalc.pre[current] ~= nil and AutoDrive.dijkstraCalc.pre[current] ~= -1 then
distanceToAdd, angle = AutoDrive:getDriveTimeBetweenNodes(current, linked, AutoDrive.dijkstraCalc.pre[current],nil,false);
else
distanceToAdd, angle = AutoDrive:getDriveTimeBetweenNodes(current, linked, nil,nil,false);
end
else
distanceToAdd = AutoDrive:getDistanceBetweenNodes(current, linked);
if AutoDrive.dijkstraCalc.pre[current] ~= nil and AutoDrive.dijkstraCalc.pre[current] ~= -1 then
local wp_current = AutoDrive.mapWayPoints[current]
local wp_ahead = AutoDrive.mapWayPoints[linked]
local wp_ref = AutoDrive.mapWayPoints[AutoDrive.dijkstraCalc.pre[current]]
angle = AutoDrive.angleBetween({x = wp_ahead.x - wp_current.x, z = wp_ahead.z - wp_current.z}, {x = wp_current.x - wp_ref.x, z = wp_current.z - wp_ref.z})
angle = math.abs(angle)
else
angle = 0
end
end;
if math.abs(angle) > 90 then
newdist = 10000000;
end;
newdist = newdist + distanceToAdd
if nil == AutoDrive.dijkstraCalc.distance[linked] then
AutoDrive.dijkstraCalc.distance[linked] = 10000000
end
if nil == AutoDrive.dijkstraCalc.pre[linked] then
AutoDrive.dijkstraCalc.pre[linked] = -1
end
if newdist < AutoDrive.dijkstraCalc.distance[linked] then
AutoDrive.dijkstraCalc.distance[linked] = newdist;
AutoDrive.dijkstraCalc.pre[linked] = current;
if #AutoDrive.mapWayPoints[linked].out > 0 then
AutoDrive.dijkstraCalc.Q.dummy_id = AutoDrive.dijkstraCalc.Q.dummy_id + 1
table.insert(AutoDrive.dijkstraCalc.Q,1,AutoDrive.dijkstraCalc.Q.dummy_id)
AutoDrive.dijkstraCalc.Q[1] = linked
end
else
if current_pre ~= 0 then
AutoDrive.dijkstraCalc.pre[linked] = current_pre
end
end;
if linked == target_id then
return true, true;
end ;
return true, false;
else
return false, false;
end -- if...
end
function AutoDrive:dijkstraLive(start,target)
local distanceToAdd = 0;
local angle = 0;
local result = false
local target_found = false
if
start == nil or start == 0 or start == -1
or target == nil or target == 0 or target == -1
then
return false
end
AutoDrive:dijkstraLiveInit(start);
if nil ~= AutoDrive.getSetting("useFastestRoute") then
AutoDrive.setting_useFastestRoute = AutoDrive.getSetting("useFastestRoute")
end
while next(AutoDrive.dijkstraCalc.Q,nil) ~= nil do
local shortest = 10000000;
local shortest_id = -1;
for i, element_wp in ipairs(AutoDrive.dijkstraCalc.Q) do
if nil == AutoDrive.dijkstraCalc.distance[AutoDrive.dijkstraCalc.Q[i]] then
AutoDrive.dijkstraCalc.distance[AutoDrive.dijkstraCalc.Q[i]] = 10000000
end
if AutoDrive.dijkstraCalc.distance[AutoDrive.dijkstraCalc.Q[i]] < shortest then
shortest = AutoDrive.dijkstraCalc.distance[AutoDrive.dijkstraCalc.Q[i]];
shortest_id = AutoDrive.dijkstraCalc.Q[i];
shortest_index = i;
end;
if AutoDrive.dijkstraCalc.distance[AutoDrive.dijkstraCalc.Q[i]] >= 10000000 then
break;
end
end;
if shortest_id == target then
return true;
end
table.remove(AutoDrive.dijkstraCalc.Q,shortest_index)
if shortest_id == -1 then
AutoDrive.dijkstraCalc.Q = {};
else
if AutoDrive.dijkstraCalc.Q[shortest_index] ~= nil then
if #AutoDrive.mapWayPoints[shortest_id].out > 0 then
for i, linkedNodeId in pairs(AutoDrive.mapWayPoints[shortest_id].out) do
local wp = AutoDrive.mapWayPoints[linkedNodeId]
if wp ~= nil then
result = false
target_found = false
result, target_found = AutoDrive:dijkstraLiveLongLine(shortest_id, linkedNodeId, target, shortest_index)
if target_found == true then
return true
end
if result ~= true then
distanceToAdd = 0;
angle = 0;
if nil == AutoDrive.dijkstraCalc.pre[shortest_id] then
AutoDrive.dijkstraCalc.pre[shortest_id] = -1
end
if AutoDrive.setting_useFastestRoute == true then
if AutoDrive.dijkstraCalc.pre[shortest_id] ~= nil and AutoDrive.dijkstraCalc.pre[shortest_id] ~= -1 then
distanceToAdd, angle = AutoDrive:getDriveTimeBetweenNodes(shortest_id, linkedNodeId, AutoDrive.dijkstraCalc.pre[shortest_id],nil,false);
else
distanceToAdd, angle = AutoDrive:getDriveTimeBetweenNodes(shortest_id, linkedNodeId, nil,nil,false);
end
else
distanceToAdd = AutoDrive:getDistanceBetweenNodes(shortest_id, linkedNodeId);
if AutoDrive.dijkstraCalc.pre[shortest_id] ~= nil and AutoDrive.dijkstraCalc.pre[shortest_id] ~= -1 then
local wp_current = AutoDrive.mapWayPoints[shortest_id]
local wp_ahead = AutoDrive.mapWayPoints[linkedNodeId]
local wp_ref = AutoDrive.mapWayPoints[AutoDrive.dijkstraCalc.pre[shortest_id]]
angle = AutoDrive.angleBetween({x = wp_ahead.x - wp_current.x, z = wp_ahead.z - wp_current.z}, {x = wp_current.x - wp_ref.x, z = wp_current.z - wp_ref.z})
angle = math.abs(angle)
else
angle = 0
end
end;
local alternative = shortest + distanceToAdd;
if math.abs(angle) > 90 then
alternative = 10000000;
end;
if nil == AutoDrive.dijkstraCalc.distance[linkedNodeId] then
AutoDrive.dijkstraCalc.distance[linkedNodeId] = 10000000
end
if nil == AutoDrive.dijkstraCalc.pre[linkedNodeId] then
AutoDrive.dijkstraCalc.pre[linkedNodeId] = -1
end
if alternative < AutoDrive.dijkstraCalc.distance[linkedNodeId] then
AutoDrive.dijkstraCalc.distance[linkedNodeId] = alternative;
AutoDrive.dijkstraCalc.pre[linkedNodeId] = shortest_id;
AutoDrive.dijkstraCalc.Q.dummy_id = AutoDrive.dijkstraCalc.Q.dummy_id + 1
table.insert(AutoDrive.dijkstraCalc.Q,1,AutoDrive.dijkstraCalc.Q.dummy_id)
AutoDrive.dijkstraCalc.Q[1] = linkedNodeId
end;
end
end; -- if wp ~= nil then
if linkedNodeId == target then
return true
end ;
end; -- for i, linkedNodeId in pairs...
end -- if
end; -- if AutoDrive.dijkstraCalc.Q[shortest_id] ~= nil then
end;
end;
if next(AutoDrive.dijkstraCalc.Q,nil) == nil then
return true
end;
return false;
end;
function AutoDrive:dijkstraLiveInit(start)
if AutoDrive.setting_useFastestRoute == nil then
-- AutoDrive.setting_useFastestRoute = true
AutoDrive.setting_useFastestRoute = false
end;
if AutoDrive.dijkstraCalc == nil then
AutoDrive.dijkstraCalc = {};
end;
AutoDrive.dijkstraCalc.distance = {};
AutoDrive.dijkstraCalc.pre = {};
AutoDrive.dijkstraCalc.Q = {};
AutoDrive.dijkstraCalc.Q.dummy_id = 1000000
AutoDrive.dijkstraCalc.Q.dummy_id = AutoDrive.dijkstraCalc.Q.dummy_id + 1
table.insert(AutoDrive.dijkstraCalc.Q,1,AutoDrive.dijkstraCalc.Q.dummy_id)
AutoDrive.dijkstraCalc.distance[start] = 0;
if nil == AutoDrive.dijkstraCalc.pre[start] then
AutoDrive.dijkstraCalc.pre[start] = -1
end
table.insert(AutoDrive.dijkstraCalc.Q,1,start)
end;
--[[
Graph - AutoDrive.mapWayPoints
start_id - Waypoint ID of start point of the route
target_id_id - Waypoint ID of target point of the route
return values:
1. empty table {}, if
- start_id and / or target_id out of valid range (1..n), i.e. nil, 0, -1
- something with route calculation is not working
- route calculation from start_id not possible to target_id, i.e. track(s) is/are not connected inbetween start_id and target_id
- more than 50000 waypoints for a route, this is assumed as no practical use case
2. table with only 1 waypoint if start_id == target_id, same as in AutoDrive:FastShortestPath
3. table with waypoints from start_id to target_id including start_id and target_id
]]
function AutoDrive:dijkstraLiveShortestPath(Graph,start_id,target_id)
local ret = false
ret = AutoDrive:dijkstraLive(start_id,target_id)
if false == ret then
return {}; --something went wrong
end
local wp = {};
local count = 1;
local id = target_id;
while (AutoDrive.dijkstraCalc.pre[id] ~= -1 and id ~= nil) or id == start_id do
table.insert(wp, 1, Graph[id]);
count = count+1;
if id == start_id then
id = nil;
else
if AutoDrive.dijkstraCalc.pre[id] ~= nil and
AutoDrive.dijkstraCalc.pre[id] ~= -1 then
id = AutoDrive.dijkstraCalc.pre[id];
else -- invalid Route -> keep Vehicle at start point
-- print(string.format("Axel: AutoDrive:dijkstraLiveShortestPath ERROR invalid Route count = %d -> keep Vehicle at start point",count))
-- TODO: message to user route not calculateable
return {};
end;
end;
if count > 50000 then
print(string.format("Axel: AutoDrive:dijkstraLiveShortestPath ERROR count > 50000"))
return {}; --something went wrong. prevent overflow here
end;
end;
return wp;
end;
function AutoDrive:FastShortestPath(Graph,start,markerName, markerID)
local wp = {};
local start_id = start;
local target_id = 0;
if start_id == nil or start_id == 0 then
return wp
end
for i in pairs(AutoDrive.mapMarker) do
if AutoDrive.mapMarker[i].name == markerName then
target_id = AutoDrive.mapMarker[i].id
break;
end
end;
if target_id == 0 then
return wp
end
if target_id == start_id then
table.insert(wp, 1, Graph[target_id]);
return wp
end
wp = AutoDrive:dijkstraLiveShortestPath(Graph,start_id,target_id)
return wp
end;
print("FS19_AutoDriveLive V 0.0.0.6 by Axel")