-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathflightplan.cpp
More file actions
407 lines (347 loc) · 13.4 KB
/
flightplan.cpp
File metadata and controls
407 lines (347 loc) · 13.4 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
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
#include "pch.h"
#include "flightplan.h"
#include "utils.h"
#include "messageHandler.h"
#include "constants.h"
std::vector<std::string> vsid::fplnhelper::clean(const EuroScopePlugIn::CFlightPlan &FlightPlan, std::string filedSidWpt)
{
EuroScopePlugIn::CFlightPlanData fplnData = FlightPlan.GetFlightPlanData();
std::string callsign = FlightPlan.GetCallsign();
std::string origin = fplnData.GetOrigin();
std::vector<std::string> filedRoute = vsid::utils::split(fplnData.GetRoute(), ' ');
std::pair<std::string, std::string> atcBlock = getAtcBlock(FlightPlan);
if (filedRoute.size() > 0)
{
try
{
if (filedRoute.at(0).find('/') != std::string::npos && filedRoute.at(0).find("/N") == std::string::npos)
{
filedRoute.erase(filedRoute.begin());
}
}
catch (std::out_of_range)
{
messageHandler->writeMessage("ERROR", "[" + callsign + "] Error during cleaning of route at first entry. ADEP: " + origin +
" with route \"" + vsid::utils::join(filedRoute) + "\". Code: " + ERROR_FPLN_CLNFIRST);
}
}
/* stop cleaning if flightplan is VFR */
if (std::string(fplnData.GetPlanType()) == "V") return filedRoute;
/* if a possible SID block was found check the entire route until the sid waypoint is found*/
if (filedRoute.size() > 0 && filedSidWpt != "")
{
for (std::vector<std::string>::iterator it = filedRoute.begin(); it != filedRoute.end();)
{
try
{
*it = vsid::utils::split(*it, '/').at(0); // to fetch wrong speed/level groups
}
catch (std::out_of_range)
{
messageHandler->writeMessage("ERROR", "[" + callsign + "] Error during cleaning of route. Cleaning was continued after false entry. ADEP: " + origin +
" with route \"" + vsid::utils::join(filedRoute) + "\". Code: " + ERROR_FPLN_CLNSPDLVL);
}
if (*it == filedSidWpt) break;
it = filedRoute.erase(it);
}
}
/* if the route has no sid waypoint clean up until the probably first waypoint*/
else if (filedRoute.size() > 0 && filedSidWpt == "")
{
for (std::vector<std::string>::iterator it = filedRoute.begin(); it != filedRoute.end();)
{
try
{
*it = vsid::utils::split(*it, '/').at(0); // to fetch wrong speed/level groups
}
catch (std::out_of_range)
{
messageHandler->writeMessage("ERROR", "[" + callsign + "] Error during cleaning of route. Cleaning was continued after false entry. ADEP: " + origin +
" with route \"" + vsid::utils::join(filedRoute) + "\". Code: " + ERROR_FPLN_CLNSPDLVL);
}
if (*it != origin) break;
it = filedRoute.erase(it);
}
}
if (filedRoute.size() == 0 && vsid::utils::split(fplnData.GetRoute(), ' ').size() != 0)
{
messageHandler->writeMessage("WARNING", "[" + callsign +
"] did not clean route as cleaning resulted in an empty route (possible error in the filed route). Returning original route.");
return vsid::utils::split(fplnData.GetRoute(), ' ');
}
return filedRoute;
}
// #evaluate - do we still need filedSidWpt or should we used the stored value?
std::string vsid::fplnhelper::getTransition(const EuroScopePlugIn::CFlightPlan& FlightPlan, const std::map<std::string, vsid::Transition>& transition,
const std::string& filedSidWpt)
{
if (transition.empty()) return "";
std::vector<std::string> route = vsid::fplnhelper::clean(FlightPlan, filedSidWpt);
for (auto& [base, trans] : transition)
{
if (std::find(route.begin(), route.end(), base) == route.end()) continue;
return trans.base + trans.number + trans.designator;
}
messageHandler->writeMessage("DEBUG", "[" + std::string(FlightPlan.GetCallsign()) +
"] no matching transition wpt found", vsid::MessageHandler::DebugArea::Sid);
return ""; // fallback if no transition could be matched
}
std::pair<std::string, std::string> vsid::fplnhelper::splitTransition(std::string atcSid) // #refactor to string_view
{
if (atcSid.size() < 2) return { "", "" };
atcSid = vsid::utils::trim(atcSid);
std::size_t pos = 0;
std::pair<std::string, std::string> fb = { atcSid, "" };
while ((pos = atcSid.find_first_of("xX", pos)) != std::string::npos)
{
if (pos == 0 || pos >= atcSid.size() - 1) { ++pos; continue; }
const std::string sid = atcSid.substr(0, pos);
const std::string trans = atcSid.substr(pos + 1);
if (sid.size() >= 3 && trans.size() >= 3 && vsid::utils::containsDigit(sid))
{
if (trans.front() != 'x' && trans.front() != 'X') return { sid, trans };
if (fb.second.empty()) fb = { sid, trans }; // save trans starting with x as fallback
}
++pos;
}
if (!fb.second.empty()) return fb;
return { atcSid, ""};
}
std::pair<std::string, std::string> vsid::fplnhelper::getAtcBlock(const EuroScopePlugIn::CFlightPlan &FlightPlan)
{
std::vector<std::string> filedRoute = vsid::utils::split(FlightPlan.GetFlightPlanData().GetRoute(), ' ');
std::string origin = FlightPlan.GetFlightPlanData().GetOrigin();
std::string callsign = FlightPlan.GetCallsign();
std::string atcRwy = "";
std::string atcSid = "";
if (filedRoute.size() > 0)
{
try
{
if (filedRoute.at(0).find('/') != std::string::npos && filedRoute.at(0).find("/N") == std::string::npos)
{
std::vector<std::string> sidBlock = vsid::utils::split(filedRoute.at(0), '/');
if (sidBlock.at(0).find_first_of("0123456789RV") != std::string::npos || sidBlock.at(0) == origin)
{
atcSid = sidBlock.front();
atcRwy = sidBlock.back();
}
}
messageHandler->removeFplnError(callsign, ERROR_FPLN_ATCBLOCK);
}
catch (std::out_of_range)
{
if (!messageHandler->getFplnErrors(callsign).contains(ERROR_FPLN_ATCBLOCK))
{
messageHandler->writeMessage("ERROR", "[" + callsign + "] Failed to get ATC block. First route entry: " +
filedRoute.at(0) + ". Code: " + ERROR_FPLN_ATCBLOCK);
messageHandler->addFplnError(callsign, ERROR_FPLN_ATCBLOCK);
}
}
}
return { atcSid, atcRwy };
}
bool vsid::fplnhelper::findRemarks(const EuroScopePlugIn::CFlightPlan& FlightPlan, const std::string(& searchStr))
{
if (!FlightPlan.IsValid()) return false;
EuroScopePlugIn::CFlightPlanData fplnData = FlightPlan.GetFlightPlanData();
return std::string(fplnData.GetRemarks()).find(searchStr) != std::string::npos;
}
bool vsid::fplnhelper::removeRemark(EuroScopePlugIn::CFlightPlan& FlightPlan, const std::string(&toRemove))
{
if (!FlightPlan.IsValid()) return false;
EuroScopePlugIn::CFlightPlanData fplnData = FlightPlan.GetFlightPlanData();
std::vector<std::string> remarks = vsid::utils::split(fplnData.GetRemarks(), ' ');
for (std::vector<std::string>::iterator it = remarks.begin(); it != remarks.end();)
{
if (*it == toRemove)
{
it = remarks.erase(it);
}
else if (it != remarks.end()) ++it;
}
return fplnData.SetRemarks(vsid::utils::join(remarks).c_str());
}
bool vsid::fplnhelper::addRemark(EuroScopePlugIn::CFlightPlan& FlightPlan, const std::string(&toAdd))
{
if (!FlightPlan.IsValid()) return false;
EuroScopePlugIn::CFlightPlanData fplnData = FlightPlan.GetFlightPlanData();
std::vector<std::string> remarks = vsid::utils::split(fplnData.GetRemarks(), ' ');
remarks.push_back(toAdd);
return fplnData.SetRemarks(vsid::utils::join(remarks).c_str());
}
bool vsid::fplnhelper::findScratchPad(const EuroScopePlugIn::CFlightPlan& FlightPlan, const std::string& toSearch)
{
if (!FlightPlan.IsValid()) return false;
EuroScopePlugIn::CFlightPlanControllerAssignedData cad = FlightPlan.GetControllerAssignedData();
return std::string(cad.GetScratchPadString()).find(vsid::utils::toupper(toSearch)); // #refactor - string_view
}
std::string vsid::fplnhelper::getEquip(const EuroScopePlugIn::CFlightPlan& FlightPlan, const std::set<std::string>& rnav)
{
std::string equip = FlightPlan.GetFlightPlanData().GetAircraftInfo();
std::string callsign = FlightPlan.GetCallsign();
char cap = FlightPlan.GetFlightPlanData().GetCapibilities();
std::vector<std::string> vecEquip = {};
if (equip.find("-") != std::string::npos)
{
vecEquip = vsid::utils::split(equip, '-');
}
else vecEquip.clear(); // equipment not present
/* default state - if an aircraft is known rnav capable skip checks */
if (rnav.contains(FlightPlan.GetFlightPlanData().GetAircraftFPType()))
{
messageHandler->writeMessage("DEBUG", "[" + callsign +
"] found in RNAV list. Returning \"SDE2E3FGIJ1RWY\"", vsid::MessageHandler::DebugArea::Sid);
return "SDE2E3FGIJ1RWY";
}
else if (vecEquip.size() >= 2)
{
vecEquip = vsid::utils::split(vecEquip.at(1), '/');
try
{
return vecEquip.at(0);
}
catch (std::out_of_range)
{
messageHandler->writeMessage("DEBUG", "[" + callsign +
"] failed to get equipment, Nothing will be returned.", vsid::MessageHandler::DebugArea::Sid);
return "";
}
}
else if (cap != ' ')
{
messageHandler->writeMessage("DEBUG", "[" + callsign +
"] failed to get equipment, falling back to capability checking. Reported equipment \"" +
equip + "\". Reported capability \"" + cap + "\"", vsid::MessageHandler::DebugArea::Sid);
std::map<char, std::string> faaToIcao = {
// X disabled due to too many occurences with fplns that are RNAV capable
//{'X', "SF"},
// T and U also disabled and will default to L
// {'T', "SF"}, {'U', "SF"},
{'D', "SDF"}, {'B', "SDF"}, {'A', "SDF"},
{'M', "DFILTUV"}, {'N', "DFILTUV"}, {'P', "DFILTUV"},
{'Y', "SDFIRY"}, {'C', "SDFIRY"}, {'I', "SDFIRY"},
{'V', "SDFGRY"}, {'S', "SDFGRY"}, {'G', "SDFGRY"},
{'W', "SDFWY"},
{'Z', "SDE2E3FIJ1RWY"},
{'L', "SDE2E3FGIJ1RWY"}
};
if (faaToIcao.contains(cap)) return faaToIcao[cap];
else return faaToIcao['L'];
}
else
{
messageHandler->writeMessage("DEBUG", "[" + callsign + "] failed to get equipment or capabilities. Returning empty equipment",
vsid::MessageHandler::DebugArea::Sid);
return "";
}
}
std::string vsid::fplnhelper::getPbn(const EuroScopePlugIn::CFlightPlan& FlightPlan)
{
if (vsid::fplnhelper::findRemarks(FlightPlan, "PBN/"))
{
std::string pbn;
std::vector<std::string> vecPbn = vsid::utils::split(FlightPlan.GetFlightPlanData().GetRemarks(), ' ');
for (std::string &rem : vecPbn)
{
if (rem.find("PBN/") != std::string::npos)
{
try
{
return vsid::utils::split(rem, '/').at(1);
}
catch (std::out_of_range)
{
return "";
}
}
}
}
return "";
}
void vsid::fplnhelper::saveFplnInfo(const std::string& callsign, vsid::Fpln fplnInfo, std::map<std::string, vsid::Fpln>& savedFplnInfo)
{
messageHandler->writeMessage("DEBUG", "[" + callsign + "] saving flight plan info for possible reconnection.", vsid::MessageHandler::DebugArea::Fpln);
savedFplnInfo[callsign] = std::move(fplnInfo);
savedFplnInfo[callsign].sid = {};
savedFplnInfo[callsign].customSid = {};
savedFplnInfo[callsign].sidWpt = "";
savedFplnInfo[callsign].transition = "";
savedFplnInfo[callsign].validEquip = true;
}
bool vsid::fplnhelper::restoreFplnInfo(const std::string& callsign, std::map<std::string, vsid::Fpln>& processed, std::map<std::string, vsid::Fpln>& savedFplnInfo)
{
if (!savedFplnInfo.contains(callsign)) return false;
processed[callsign] = std::move(savedFplnInfo[callsign]);
savedFplnInfo.erase(callsign);
if (processed.contains(callsign))
{
messageHandler->writeMessage("DEBUG", "[" + callsign + "] successfully restored.", vsid::MessageHandler::DebugArea::Fpln);
return true;
}
else
{
messageHandler->writeMessage("DEBUG", "[" + callsign + "] failed to restore from saved info.", vsid::MessageHandler::DebugArea::Fpln);
return false;
}
}
bool vsid::fplnhelper::restoreIC(const vsid::Fpln& fplnInfo, EuroScopePlugIn::CFlightPlan FlightPlan, EuroScopePlugIn::CController atcMyself)
{
if (!FlightPlan.IsValid()) return false;
auto [blockSid, blockRwy] = vsid::fplnhelper::getAtcBlock(FlightPlan);
std::string callsign = FlightPlan.GetCallsign();
EuroScopePlugIn::CFlightPlanControllerAssignedData cad = FlightPlan.GetControllerAssignedData();
EuroScopePlugIn::CFlightPlanData fplnData = FlightPlan.GetFlightPlanData();
if (std::find(blockSid.begin(), blockSid.end(), 'x') != blockSid.end() || // #refactor - remove checks for xX
std::find(blockSid.begin(), blockSid.end(), 'X') != blockSid.end())
{
blockSid = vsid::fplnhelper::splitTransition(blockSid).first;
}
if (cad.GetClearedAltitude() == 0 &&
blockSid != "" &&
atcMyself.IsController() &&
(blockSid == fplnInfo.sid.name() ||
blockSid == fplnInfo.customSid.name())
)
{
if (!fplnInfo.customSid.empty())
{
int initialClimb = (fplnInfo.customSid.initialClimb > fplnData.GetFinalAltitude()) ?
fplnData.GetFinalAltitude() : fplnInfo.customSid.initialClimb;
if (!cad.SetClearedAltitude(initialClimb))
{
if (!messageHandler->getFplnErrors(callsign).contains(ERROR_FPLN_SETALT_RESET))
{
messageHandler->writeMessage("ERROR", "[" + callsign + "] - failed to set altitude. Code: " + ERROR_FPLN_SETALT_RESET);
messageHandler->addFplnError(callsign, ERROR_FPLN_SETALT_RESET);
}
return false;
}
else
{
messageHandler->removeFplnError(callsign, ERROR_FPLN_SETALT_RESET);
return true;
}
}
else if (!fplnInfo.sid.empty())
{
int initialClimb = (fplnInfo.sid.initialClimb > fplnData.GetFinalAltitude()) ?
fplnData.GetFinalAltitude() : fplnInfo.sid.initialClimb;
if (!cad.SetClearedAltitude(initialClimb))
{
if (!messageHandler->getFplnErrors(callsign).contains(ERROR_FPLN_SETALT_RESET))
{
messageHandler->writeMessage("ERROR", "[" + callsign + "] - failed to set altitude. Code: " + ERROR_FPLN_SETALT_RESET);
messageHandler->addFplnError(callsign, ERROR_FPLN_SETALT_RESET);
}
return false;
}
else
{
messageHandler->removeFplnError(callsign, ERROR_FPLN_SETALT_RESET);
return true;
}
}
}
return false;
}