1
0
Fork 0

Replace activeTraffic iterations with a standard function

This commit is contained in:
legoboyvdlp R 2019-08-30 11:25:14 +01:00 committed by James Turner
parent 3e1dc4683b
commit 18c0613762
4 changed files with 90 additions and 245 deletions

View file

@ -20,14 +20,13 @@
//
// $Id$
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <config.h>
#include <cmath>
#include <algorithm>
#include <fstream>
#include <map>
#include <algorithm>
#include <osg/Geode>
#include <osg/Geometry>
@ -105,17 +104,10 @@ bool compare_trafficrecords(FGTrafficRecord a, FGTrafficRecord b)
*/
TrafficVectorIterator FGGroundController::searchActiveTraffic(int id)
{
// Possible optimization - investigate using map instead of vector
TrafficVectorIterator i = activeTraffic.begin();
if (!activeTraffic.empty()) {
while (i != activeTraffic.end()) {
if (i->getId() == id) {
break;
}
i++;
}
}
return i;
return std::find_if(activeTraffic.begin(), activeTraffic.end(),
[id] (const FGTrafficRecord& rec)
{ return rec.getId() == id; }
);
}
void FGGroundController::announcePosition(int id,
@ -226,20 +218,10 @@ void FGGroundController::updateAircraftInformation(int id, double lat, double lo
// Transmit confirmation ...
// Probably use a status mechanism similar to the Engine start procedure in the startup controller.
// leave this here until I figure out what current, closest is all about
TrafficVectorIterator i = activeTraffic.begin();
// Search search if the current id has an entry
// This might be faster using a map instead of a vector, but let's start by taking a safe route
// Search the activeTraffic vector to find a traffic vector with our id
TrafficVectorIterator i = searchActiveTraffic(id);
TrafficVectorIterator current, closest;
if (activeTraffic.size()) {
//while ((i->getId() != id) && i != activeTraffic.end()) {
while (i != activeTraffic.end()) {
if (i->getId() == id) {
break;
}
i++;
}
}
// update position of the current aircraft
if (i == activeTraffic.end() || (activeTraffic.size() == 0)) {
SG_LOG(SG_GENERAL, SG_ALERT,
@ -313,20 +295,12 @@ void FGGroundController::checkSpeedAdjustment(int id, double lat,
{
TrafficVectorIterator current, closest, closestOnNetwork;
TrafficVectorIterator i = activeTraffic.begin();
bool otherReasonToSlowDown = false;
// bool previousInstruction;
if (activeTraffic.size()) {
//while ((i->getId() != id) && (i != activeTraffic.end()))
while (i != activeTraffic.end()) {
if (i->getId() == id) {
break;
}
i++;
}
} else {
TrafficVectorIterator i = searchActiveTraffic(id);
if (!activeTraffic.size()) {
return;
}
}
if (i == activeTraffic.end() || (activeTraffic.size() == 0)) {
SG_LOG(SG_GENERAL, SG_ALERT,
"AI error: Trying to access non-existing aircraft in FGGroundNetwork::checkSpeedAdjustment at " << SG_ORIGIN);
@ -409,10 +383,10 @@ void FGGroundController::checkSpeedAdjustment(int id, double lat,
}
*/
// Clear any active speed adjustment, check if the aircraft needs to brake
// Clear any active speed adjustment, check if the aircraft needs to brake
current->clearSpeedAdjustment();
bool needBraking = false;
if (current->checkPositionAndIntentions(*closest)
|| otherReasonToSlowDown) {
double maxAllowableDistance =
@ -423,7 +397,7 @@ void FGGroundController::checkSpeedAdjustment(int id, double lat,
return;
else
current->setWaitsForId(closest->getId());
if (closest->getId() != current->getId()) {
current->setSpeedAdjustment(closest->getSpeed() *
(mindist / 100));
@ -438,7 +412,7 @@ void FGGroundController::checkSpeedAdjustment(int id, double lat,
} else {
current->setSpeedAdjustment(0); // This can only happen when the user aircraft is the one closest
}
if (mindist < maxAllowableDistance) {
//double newSpeed = (maxAllowableDistance-mindist);
//current->setSpeedAdjustment(newSpeed);
@ -449,7 +423,7 @@ void FGGroundController::checkSpeedAdjustment(int id, double lat,
}
}
}
if ((closest->getId() == closestOnNetwork->getId()) && (current->getPriority() < closest->getPriority()) && needBraking) {
swap(current, closest);
}
@ -1073,10 +1047,11 @@ bool FGGroundController::updateActiveTraffic(TrafficVectorIterator i,
FGTaxiSegment* segment = network->findSegment(pos);
length = segment->getLength();
if (segment->hasBlock(now)) {
//SG_LOG(SG_GENERAL, SG_ALERT, "Taxiway incursion for AI aircraft" << i->getAircraft()->getCallSign());
SG_LOG(SG_ATC, SG_ALERT, "Taxiway incursion for AI aircraft" << i->getAircraft()->getCallSign());
}
}
intVecIterator ivi;
for (ivi = i->getIntentions().begin(); ivi != i->getIntentions().end(); ivi++) {
int segIndex = (*ivi);
@ -1087,6 +1062,7 @@ bool FGGroundController::updateActiveTraffic(TrafficVectorIterator i,
}
}
}
//after this, ivi points just behind the last valid unblocked taxi segment.
for (intVecIterator j = i->getIntentions().begin(); j != ivi; j++) {
int pos = (*j);

View file

@ -20,9 +20,7 @@
//
// $Id$
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <config.h>
#include <algorithm>
#include <cstdio>
@ -66,6 +64,12 @@ void clearTrafficControllers(TrafficVector& vec)
}
}
TrafficVectorIterator searchActiveTraffic(TrafficVector& vec, int id)
{
return std::find_if(vec.begin(), vec.end(), [id] (const FGTrafficRecord& rec)
{ return rec.getId() == id; });
}
} // of anonymous namespace
/***************************************************************************
@ -245,14 +249,13 @@ FGTrafficRecord::~FGTrafficRecord()
void FGTrafficRecord::setPositionAndIntentions(int pos,
FGAIFlightPlan * route)
{
SG_LOG(SG_ATC, SG_DEBUG, "Position: " << pos);
currentPos = pos;
if (! intentions.empty()) {
if (!intentions.empty()) {
intVecIterator i = intentions.begin();
if ((*i) != currentPos) {
SG_LOG(SG_ATC, SG_ALERT,
"Error in FGTrafficRecord::setPositionAndIntentions at " << SG_ORIGIN);
"Error in FGTrafficRecord::setPositionAndIntentions at " << SG_ORIGIN << ", " << (*i));
}
intentions.erase(i);
} else {
@ -638,15 +641,15 @@ void FGATCController::transmit(FGTrafficRecord * rec, FGAirportDynamics *parent,
getName() + "-Tower";
break;
}
// Swap sender and receiver value in case of a ground to air transmission
if (msgDir == ATC_GROUND_TO_AIR) {
string tmp = sender;
sender = receiver;
receiver = tmp;
ground_to_air=1;
ground_to_air = 1;
}
switch (msgId) {
case MSG_ANNOUNCE_ENGINE_START:
text = sender + ". Ready to Start up.";
@ -800,7 +803,7 @@ void FGATCController::transmit(FGTrafficRecord * rec, FGAirportDynamics *parent,
text = text + sender + ". Transmitting unknown Message.";
break;
}
if (audible) {
double onBoardRadioFreq0 =
fgGetDouble("/instrumentation/comm[0]/frequencies/selected-mhz");
@ -917,18 +920,10 @@ void FGTowerController::announcePosition(int id,
FGAIAircraft * ref)
{
init();
TrafficVectorIterator i = activeTraffic.begin();
// Search whether the current id alread has an entry
// This might be faster using a map instead of a vector, but let's start by taking a safe route
if (! activeTraffic.empty()) {
//while ((i->getId() != id) && i != activeTraffic.end()) {
while (i != activeTraffic.end()) {
if (i->getId() == id) {
break;
}
i++;
}
}
// Search activeTraffic for a record matching our id
TrafficVectorIterator i = searchActiveTraffic(activeTraffic, id);
// Add a new TrafficRecord if no one exsists for this aircraft.
if (i == activeTraffic.end() || (activeTraffic.empty())) {
FGTrafficRecord rec;
@ -970,20 +965,9 @@ void FGTowerController::updateAircraftInformation(int id, double lat, double lon
double heading, double speed, double alt,
double dt)
{
TrafficVectorIterator i = activeTraffic.begin();
// Search whether the current id has an entry
// This might be faster using a map instead of a vector, but let's start by taking a safe route
TrafficVectorIterator closest;
if (! activeTraffic.empty()) {
//while ((i->getId() != id) && i != activeTraffic.end()) {
while (i != activeTraffic.end()) {
if (i->getId() == id) {
break;
}
i++;
}
}
// Search activeTraffic for a record matching our id
TrafficVectorIterator i = searchActiveTraffic(activeTraffic, id);
setDt(getDt() + dt);
if (i == activeTraffic.end() || (activeTraffic.empty())) {
@ -1060,18 +1044,9 @@ void FGTowerController::updateAircraftInformation(int id, double lat, double lon
void FGTowerController::signOff(int id)
{
TrafficVectorIterator i = activeTraffic.begin();
// Search search if the current id alread has an entry
// This might be faster using a map instead of a vector, but let's start by taking a safe route
if (! activeTraffic.empty()) {
//while ((i->getId() != id) && i != activeTraffic.end()) {
while (i != activeTraffic.end()) {
if (i->getId() == id) {
break;
}
i++;
}
}
// Search activeTraffic for a record matching our id
TrafficVectorIterator i = searchActiveTraffic(activeTraffic, id);
// If this aircraft has left the runway, we can clear the departure record for this runway
ActiveRunwayVecIterator rwy = activeRunways.begin();
if (! activeRunways.empty()) {
@ -1108,18 +1083,9 @@ void FGTowerController::signOff(int id)
// Note that this function is probably obsolete
bool FGTowerController::hasInstruction(int id)
{
TrafficVectorIterator i = activeTraffic.begin();
// Search search if the current id has an entry
// This might be faster using a map instead of a vector, but let's start by taking a safe route
if (! activeTraffic.empty()) {
//while ((i->getId() != id) && i != activeTraffic.end()) {
while (i != activeTraffic.end()) {
if (i->getId() == id) {
break;
}
i++;
}
}
// Search activeTraffic for a record matching our id
TrafficVectorIterator i = searchActiveTraffic(activeTraffic, id);
if (i == activeTraffic.end() || activeTraffic.empty()) {
SG_LOG(SG_ATC, SG_ALERT,
"AI error: checking ATC instruction for aircraft without traffic record at " << SG_ORIGIN);
@ -1132,18 +1098,9 @@ bool FGTowerController::hasInstruction(int id)
FGATCInstruction FGTowerController::getInstruction(int id)
{
TrafficVectorIterator i = activeTraffic.begin();
// Search search if the current id has an entry
// This might be faster using a map instead of a vector, but let's start by taking a safe route
if (! activeTraffic.empty()) {
//while ((i->getId() != id) && i != activeTraffic.end()) {
while (i != activeTraffic.end()) {
if (i->getId() == id) {
break;
}
i++;
}
}
// Search activeTraffic for a record matching our id
TrafficVectorIterator i = searchActiveTraffic(activeTraffic, id);
if (i == activeTraffic.end() || activeTraffic.empty()) {
SG_LOG(SG_ATC, SG_ALERT,
"AI error: requesting ATC instruction for aircraft without traffic record at " << SG_ORIGIN);
@ -1193,18 +1150,9 @@ void FGStartupController::announcePosition(int id,
FGAIAircraft * ref)
{
init();
TrafficVectorIterator i = activeTraffic.begin();
// Search whether the current id alread has an entry
// This might be faster using a map instead of a vector, but let's start by taking a safe route
if (! activeTraffic.empty()) {
//while ((i->getId() != id) && i != activeTraffic.end()) {
while (i != activeTraffic.end()) {
if (i->getId() == id) {
break;
}
i++;
}
}
// Search activeTraffic for a record matching our id
TrafficVectorIterator i = searchActiveTraffic(activeTraffic, id);
// Add a new TrafficRecord if no one exsists for this aircraft.
if (i == activeTraffic.end() || activeTraffic.empty()) {
FGTrafficRecord rec;
@ -1233,18 +1181,9 @@ void FGStartupController::announcePosition(int id,
// Note that this function is probably obsolete
bool FGStartupController::hasInstruction(int id)
{
TrafficVectorIterator i = activeTraffic.begin();
// Search search if the current id has an entry
// This might be faster using a map instead of a vector, but let's start by taking a safe route
if (! activeTraffic.empty()) {
//while ((i->getId() != id) && i != activeTraffic.end()) {
while (i != activeTraffic.end()) {
if (i->getId() == id) {
break;
}
i++;
}
}
// Search activeTraffic for a record matching our id
TrafficVectorIterator i = searchActiveTraffic(activeTraffic, id);
if (i == activeTraffic.end() || activeTraffic.empty()) {
SG_LOG(SG_ATC, SG_ALERT,
"AI error: checking ATC instruction for aircraft without traffic record at " << SG_ORIGIN);
@ -1257,18 +1196,9 @@ bool FGStartupController::hasInstruction(int id)
FGATCInstruction FGStartupController::getInstruction(int id)
{
TrafficVectorIterator i = activeTraffic.begin();
// Search search if the current id has an entry
// This might be faster using a map instead of a vector, but let's start by taking a safe route
if (! activeTraffic.empty()) {
//while ((i->getId() != id) && i != activeTraffic.end()) {
while (i != activeTraffic.end()) {
if (i->getId() == id) {
break;
}
i++;
}
}
// Search activeTraffic for a record matching our id
TrafficVectorIterator i = searchActiveTraffic(activeTraffic, id);
if (i == activeTraffic.end() || activeTraffic.empty()) {
SG_LOG(SG_ATC, SG_ALERT,
"AI error: requesting ATC instruction for aircraft without traffic record at " << SG_ORIGIN);
@ -1280,18 +1210,9 @@ FGATCInstruction FGStartupController::getInstruction(int id)
void FGStartupController::signOff(int id)
{
TrafficVectorIterator i = activeTraffic.begin();
// Search search if the current id alread has an entry
// This might be faster using a map instead of a vector, but let's start by taking a safe route
if (! activeTraffic.empty()) {
//while ((i->getId() != id) && i != activeTraffic.end()) {
while (i != activeTraffic.end()) {
if (i->getId() == id) {
break;
}
i++;
}
}
// Search activeTraffic for a record matching our id
TrafficVectorIterator i = searchActiveTraffic(activeTraffic, id);
if (i == activeTraffic.end() || activeTraffic.empty()) {
SG_LOG(SG_ATC, SG_ALERT,
"AI error: Aircraft without traffic record is signing off from tower at " << SG_ORIGIN);
@ -1338,21 +1259,10 @@ void FGStartupController::updateAircraftInformation(int id, double lat, double l
double heading, double speed, double alt,
double dt)
{
TrafficVectorIterator i = activeTraffic.begin();
// Search search if the current id has an entry
// This might be faster using a map instead of a vector, but let's start by taking a safe route
TrafficVectorIterator current, closest;
if (! activeTraffic.empty()) {
//while ((i->getId() != id) && i != activeTraffic.end()) {
while (i != activeTraffic.end()) {
if (i->getId() == id) {
break;
}
i++;
}
}
// // update position of the current aircraft
// Search activeTraffic for a record matching our id
TrafficVectorIterator i = searchActiveTraffic(activeTraffic, id);
TrafficVectorIterator current, closest;
if (i == activeTraffic.end() || (activeTraffic.size() == 0)) {
SG_LOG(SG_ATC, SG_ALERT,
"AI error: updating aircraft without traffic record at " << SG_ORIGIN);
@ -1650,7 +1560,7 @@ FGApproachController::~FGApproachController()
clearTrafficControllers(activeTraffic);
}
//
void FGApproachController::announcePosition(int id,
FGAIFlightPlan * intendedRoute,
int currentPosition,
@ -1660,18 +1570,10 @@ void FGApproachController::announcePosition(int id,
int leg, FGAIAircraft * ref)
{
init();
TrafficVectorIterator i = activeTraffic.begin();
// Search whether the current id alread has an entry
// This might be faster using a map instead of a vector, but let's start by taking a safe route
if (! activeTraffic.empty()) {
//while ((i->getId() != id) && i != activeTraffic.end()) {
while (i != activeTraffic.end()) {
if (i->getId() == id) {
break;
}
i++;
}
}
// Search activeTraffic for a record matching our id
TrafficVectorIterator i = searchActiveTraffic(activeTraffic, id);
// Add a new TrafficRecord if no one exsists for this aircraft.
if (i == activeTraffic.end() || activeTraffic.empty()) {
FGTrafficRecord rec;
@ -1692,20 +1594,11 @@ void FGApproachController::updateAircraftInformation(int id, double lat, double
double heading, double speed, double alt,
double dt)
{
TrafficVectorIterator i = activeTraffic.begin();
// Search search if the current id has an entry
// This might be faster using a map instead of a vector, but let's start by taking a safe route
TrafficVectorIterator current, closest;
if (! activeTraffic.empty()) {
//while ((i->getId() != id) && i != activeTraffic.end()) {
while (i != activeTraffic.end()) {
if (i->getId() == id) {
break;
}
i++;
}
}
// // update position of the current aircraft
// Search activeTraffic for a record matching our id
TrafficVectorIterator i = searchActiveTraffic(activeTraffic, id);
TrafficVectorIterator current;
// update position of the current aircraft
if (i == activeTraffic.end() || activeTraffic.empty()) {
SG_LOG(SG_ATC, SG_ALERT,
"AI error: updating aircraft without traffic record at " << SG_ORIGIN);
@ -1740,20 +1633,12 @@ void FGApproachController::updateAircraftInformation(int id, double lat, double
setDt(getDt() + dt);
}
/* Search for and erase traffic record with a specific id */
void FGApproachController::signOff(int id)
{
TrafficVectorIterator i = activeTraffic.begin();
// Search search if the current id alread has an entry
// This might be faster using a map instead of a vector, but let's start by taking a safe route
if (! activeTraffic.empty()) {
//while ((i->getId() != id) && i != activeTraffic.end()) {
while (i != activeTraffic.end()) {
if (i->getId() == id) {
break;
}
i++;
}
}
// Search activeTraffic for a record matching our id
TrafficVectorIterator i = searchActiveTraffic(activeTraffic, id);
if (i == activeTraffic.end() || activeTraffic.empty()) {
SG_LOG(SG_ATC, SG_ALERT,
"AI error: Aircraft without traffic record is signing off from approach at " << SG_ORIGIN);
@ -1762,6 +1647,7 @@ void FGApproachController::signOff(int id)
}
}
/* Periodically check for and remove dead traffic records */
void FGApproachController::update(double dt)
{
eraseDeadTraffic(activeTraffic);
@ -1769,18 +1655,9 @@ void FGApproachController::update(double dt)
bool FGApproachController::hasInstruction(int id)
{
TrafficVectorIterator i = activeTraffic.begin();
// Search search if the current id has an entry
// This might be faster using a map instead of a vector, but let's start by taking a safe route
if (! activeTraffic.empty()) {
//while ((i->getId() != id) && i != activeTraffic.end()) {
while (i != activeTraffic.end()) {
if (i->getId() == id) {
break;
}
i++;
}
}
// Search activeTraffic for a record matching our id
TrafficVectorIterator i = searchActiveTraffic(activeTraffic, id);
if (i == activeTraffic.end() || activeTraffic.empty()) {
SG_LOG(SG_ATC, SG_ALERT,
"AI error: checking ATC instruction for aircraft without traffic record at " << SG_ORIGIN);
@ -1793,18 +1670,9 @@ bool FGApproachController::hasInstruction(int id)
FGATCInstruction FGApproachController::getInstruction(int id)
{
TrafficVectorIterator i = activeTraffic.begin();
// Search search if the current id has an entry
// This might be faster using a map instead of a vector, but let's start by taking a safe route
if (activeTraffic.size()) {
//while ((i->getId() != id) && i != activeTraffic.end()) {
while (i != activeTraffic.end()) {
if (i->getId() == id) {
break;
}
i++;
}
}
// Search activeTraffic for a record matching our id
TrafficVectorIterator i = searchActiveTraffic(activeTraffic, id);
if (i == activeTraffic.end() || (activeTraffic.size() == 0)) {
SG_LOG(SG_ATC, SG_ALERT,
"AI error: requesting ATC instruction for aircraft without traffic record at " << SG_ORIGIN);

View file

@ -179,16 +179,16 @@ public:
void setLeg(int lg) {
leg = lg;
};
int getId() {
int getId() const {
return id;
};
int getState() {
int getState() const {
return state;
};
void setState(int s) {
state = s;
}
FGATCInstruction getInstruction() {
FGATCInstruction getInstruction() const {
return instruction;
};
bool hasInstruction() {

View file

@ -249,6 +249,7 @@ FGAirport *FGScheduledFlight::getDepartureAirport()
else
return 0;
}
FGAirport * FGScheduledFlight::getArrivalAirport ()
{
if (!(initialized))