2004-06-03 17:59:14 +00:00
|
|
|
/******************************************************************************
|
|
|
|
* TrafficMGr.cxx
|
|
|
|
* Written by Durk Talsma, started May 5, 2004.
|
|
|
|
*
|
|
|
|
* 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 2 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, write to the Free Software
|
2006-02-21 01:16:04 +00:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
2004-06-03 17:59:14 +00:00
|
|
|
*
|
|
|
|
*
|
|
|
|
**************************************************************************/
|
|
|
|
|
|
|
|
/* This a prototype version of a top-level flight plan manager for Flightgear.
|
|
|
|
* It parses the fgtraffic.txt file and determine for a specific time/date,
|
|
|
|
* where each aircraft listed in this file is at the current time.
|
|
|
|
*
|
|
|
|
* I'm currently assuming the following simplifications:
|
|
|
|
* 1) The earth is a perfect sphere
|
|
|
|
* 2) Each aircraft flies a perfect great circle route.
|
|
|
|
* 3) Each aircraft flies at a constant speed (with infinite accelerations and
|
|
|
|
* decelerations)
|
|
|
|
* 4) Each aircraft leaves at exactly the departure time.
|
|
|
|
* 5) Each aircraft arrives at exactly the specified arrival time.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
*****************************************************************************/
|
2006-02-18 13:58:09 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2004-06-03 17:59:14 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <iostream>
|
|
|
|
#include <fstream>
|
|
|
|
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
2005-02-18 10:16:30 +00:00
|
|
|
#include <algorithm>
|
2004-06-03 17:59:14 +00:00
|
|
|
|
|
|
|
#include <plib/sg.h>
|
2006-12-27 11:53:54 +00:00
|
|
|
#include <plib/ul.h>
|
2004-06-03 17:59:14 +00:00
|
|
|
|
|
|
|
#include <simgear/compiler.h>
|
|
|
|
#include <simgear/math/polar3d.hxx>
|
|
|
|
#include <simgear/math/sg_geodesy.hxx>
|
2004-12-27 17:35:22 +00:00
|
|
|
#include <simgear/misc/sg_path.hxx>
|
2004-06-03 17:59:14 +00:00
|
|
|
#include <simgear/props/props.hxx>
|
|
|
|
#include <simgear/route/waypoint.hxx>
|
|
|
|
#include <simgear/structure/subsystem_mgr.hxx>
|
|
|
|
#include <simgear/xml/easyxml.hxx>
|
|
|
|
|
2005-02-10 09:01:51 +00:00
|
|
|
#include <AIModel/AIAircraft.hxx>
|
2004-06-03 17:59:14 +00:00
|
|
|
#include <AIModel/AIFlightPlan.hxx>
|
2004-09-07 09:53:23 +00:00
|
|
|
#include <AIModel/AIBase.hxx>
|
2004-06-03 17:59:14 +00:00
|
|
|
#include <Airports/simple.hxx>
|
2006-07-29 18:17:19 +00:00
|
|
|
#include <Main/fg_init.hxx>
|
2004-06-03 17:59:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include "TrafficMgr.hxx"
|
|
|
|
|
2005-02-18 10:16:30 +00:00
|
|
|
SG_USING_STD(sort);
|
2004-06-03 17:59:14 +00:00
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
* TrafficManager
|
|
|
|
*****************************************************************************/
|
|
|
|
FGTrafficManager::FGTrafficManager()
|
|
|
|
{
|
2006-07-29 18:17:19 +00:00
|
|
|
score = 0;
|
2006-11-11 10:52:05 +00:00
|
|
|
runCount = 0;
|
2004-06-03 17:59:14 +00:00
|
|
|
}
|
|
|
|
|
2006-10-06 17:36:31 +00:00
|
|
|
FGTrafficManager:: ~FGTrafficManager()
|
|
|
|
{
|
|
|
|
for (ScheduleVectorIterator sched = scheduledAircraft.begin(); sched != scheduledAircraft.end(); sched++)
|
|
|
|
{
|
|
|
|
delete (*sched);
|
|
|
|
}
|
|
|
|
scheduledAircraft.clear();
|
|
|
|
// for (FGScheduledFlightVecIterator flt = flights.begin(); flt != flights.end(); flt++)
|
|
|
|
// {
|
|
|
|
// delete (*flt);
|
|
|
|
// }
|
|
|
|
// flights.clear();
|
|
|
|
}
|
|
|
|
|
2004-06-03 17:59:14 +00:00
|
|
|
|
|
|
|
void FGTrafficManager::init()
|
2005-02-10 09:01:51 +00:00
|
|
|
{
|
|
|
|
//cerr << "Initializing Schedules" << endl;
|
2006-07-29 18:17:19 +00:00
|
|
|
//time_t now = time(NULL) + fgGetLong("/sim/time/warp");
|
|
|
|
//currAircraft = scheduledAircraft.begin();
|
|
|
|
//while (currAircraft != scheduledAircraft.end())
|
|
|
|
// {
|
|
|
|
// if (!(currAircraft->init()))
|
|
|
|
// {
|
|
|
|
// currAircraft=scheduledAircraft.erase(currAircraft);
|
|
|
|
// //cerr << "Erasing " << currAircraft->getRegistration() << endl;
|
|
|
|
// }
|
|
|
|
// else
|
|
|
|
// {
|
|
|
|
// currAircraft++;
|
|
|
|
// }
|
|
|
|
// }
|
2006-10-06 17:36:31 +00:00
|
|
|
// Sort by points: Aircraft with frequent visits to the
|
|
|
|
// startup airport will be processed first
|
2006-12-27 11:53:54 +00:00
|
|
|
ulDir* d, *d2;
|
|
|
|
ulDirEnt* dent, *dent2;
|
|
|
|
SGPath aircraftDir = globals->get_fg_root();
|
|
|
|
|
|
|
|
/* keep the following three lines (which mimicks the old "fixed path" behavior)
|
|
|
|
* until we have some AI models with traffic in the base package
|
|
|
|
*/
|
|
|
|
SGPath path = aircraftDir;
|
2007-05-10 14:28:17 +00:00
|
|
|
path.append("Traffic/fgtraffic.xml");
|
2006-12-27 11:53:54 +00:00
|
|
|
readXML(path.str(),*this);
|
|
|
|
|
2007-05-10 14:28:17 +00:00
|
|
|
aircraftDir.append("AI/Aircraft");
|
2006-12-27 11:53:54 +00:00
|
|
|
if (aircraftDir.exists())
|
|
|
|
{
|
|
|
|
if((d = ulOpenDir(aircraftDir.c_str())) == NULL)
|
|
|
|
return;
|
|
|
|
while((dent = ulReadDir(d)) != NULL) {
|
|
|
|
//cerr << "Scanning : " << dent->d_name << endl;
|
|
|
|
if (string(dent->d_name) != string(".") &&
|
|
|
|
string(dent->d_name) != string("..") &&
|
|
|
|
dent->d_isdir)
|
|
|
|
{
|
|
|
|
SGPath currACDir = aircraftDir;
|
|
|
|
currACDir.append(dent->d_name);
|
|
|
|
if ((d2 = ulOpenDir(currACDir.c_str())) == NULL)
|
|
|
|
return;
|
|
|
|
while ((dent2 = ulReadDir(d2)) != NULL) {
|
|
|
|
SGPath currFile = currACDir;
|
|
|
|
currFile.append(dent2->d_name);
|
|
|
|
if (currFile.extension() == string("xml"))
|
|
|
|
{
|
|
|
|
//cerr << "found " << dent2->d_name << " for parsing" << endl;
|
|
|
|
SGPath currFile = currACDir;
|
|
|
|
currFile.append(dent2->d_name);
|
|
|
|
SG_LOG(SG_GENERAL, SG_INFO, "Scanning " << currFile.str() << " for traffic");
|
|
|
|
readXML(currFile.str(),*this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ulCloseDir(d2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ulCloseDir(d);
|
|
|
|
}
|
|
|
|
// Sort by points: Aircraft with frequent visits to the
|
|
|
|
// startup airport will be processed first
|
2006-10-06 17:36:31 +00:00
|
|
|
sort(scheduledAircraft.begin(), scheduledAircraft.end(), compareSchedules);
|
2004-06-03 17:59:14 +00:00
|
|
|
currAircraft = scheduledAircraft.begin();
|
2005-02-10 09:01:51 +00:00
|
|
|
currAircraftClosest = scheduledAircraft.begin();
|
|
|
|
//cerr << "Done initializing schedules" << endl;
|
2004-06-03 17:59:14 +00:00
|
|
|
}
|
|
|
|
|
2007-05-10 14:28:17 +00:00
|
|
|
void FGTrafficManager::update(double /*dt*/)
|
2004-06-03 17:59:14 +00:00
|
|
|
{
|
2006-12-27 11:53:54 +00:00
|
|
|
//SG_LOG( SG_GENERAL, SG_INFO, "Running TrafficManager::Update() ");
|
2006-11-11 10:52:05 +00:00
|
|
|
if (runCount < 1000)
|
|
|
|
{
|
|
|
|
runCount++;
|
|
|
|
return;
|
|
|
|
}
|
2007-05-25 07:20:41 +00:00
|
|
|
//runCount = 0;
|
2004-06-03 17:59:14 +00:00
|
|
|
time_t now = time(NULL) + fgGetLong("/sim/time/warp");
|
2006-12-27 11:53:54 +00:00
|
|
|
if (scheduledAircraft.size() == 0) {
|
|
|
|
//SG_LOG( SG_GENERAL, SG_INFO, "Returned Running TrafficManager::Update() ");
|
2006-07-29 18:17:19 +00:00
|
|
|
return;
|
2006-12-27 11:53:54 +00:00
|
|
|
}
|
2005-02-10 09:01:51 +00:00
|
|
|
if(currAircraft == scheduledAircraft.end())
|
|
|
|
{
|
|
|
|
//cerr << "resetting schedule " << endl;
|
2004-06-03 17:59:14 +00:00
|
|
|
currAircraft = scheduledAircraft.begin();
|
2005-02-10 09:01:51 +00:00
|
|
|
}
|
2006-10-06 17:36:31 +00:00
|
|
|
if (!((*currAircraft)->update(now)))
|
2005-02-10 09:01:51 +00:00
|
|
|
{
|
|
|
|
// after proper initialization, we shouldnt get here.
|
|
|
|
// But let's make sure
|
2007-05-10 14:28:17 +00:00
|
|
|
SG_LOG( SG_GENERAL, SG_ALERT, "Failed to update aircraft schedule in traffic manager");
|
2005-02-10 09:01:51 +00:00
|
|
|
}
|
|
|
|
currAircraft++;
|
2006-12-27 11:53:54 +00:00
|
|
|
//SG_LOG( SG_GENERAL, SG_INFO, "Done Running TrafficManager::Update() ");
|
2004-06-03 17:59:14 +00:00
|
|
|
}
|
|
|
|
|
2005-10-15 14:55:51 +00:00
|
|
|
void FGTrafficManager::release(int id)
|
2004-11-29 09:41:43 +00:00
|
|
|
{
|
|
|
|
releaseList.push_back(id);
|
|
|
|
}
|
|
|
|
|
2005-10-15 14:55:51 +00:00
|
|
|
bool FGTrafficManager::isReleased(int id)
|
2004-11-29 09:41:43 +00:00
|
|
|
{
|
|
|
|
IdListIterator i = releaseList.begin();
|
|
|
|
while (i != releaseList.end())
|
|
|
|
{
|
|
|
|
if ((*i) == id)
|
|
|
|
{
|
|
|
|
releaseList.erase(i);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2004-06-03 17:59:14 +00:00
|
|
|
void FGTrafficManager::startXML () {
|
|
|
|
//cout << "Start XML" << endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FGTrafficManager::endXML () {
|
|
|
|
//cout << "End XML" << endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FGTrafficManager::startElement (const char * name, const XMLAttributes &atts) {
|
|
|
|
const char * attval;
|
|
|
|
//cout << "Start element " << name << endl;
|
|
|
|
//FGTrafficManager temp;
|
|
|
|
//for (int i = 0; i < atts.size(); i++)
|
|
|
|
// if (string(atts.getName(i)) == string("include"))
|
|
|
|
attval = atts.getValue("include");
|
|
|
|
if (attval != 0)
|
|
|
|
{
|
|
|
|
//cout << "including " << attval << endl;
|
2004-12-27 17:35:22 +00:00
|
|
|
SGPath path =
|
|
|
|
globals->get_fg_root();
|
|
|
|
path.append("/Traffic/");
|
|
|
|
path.append(attval);
|
|
|
|
readXML(path.str(), *this);
|
2004-06-03 17:59:14 +00:00
|
|
|
}
|
|
|
|
// cout << " " << atts.getName(i) << '=' << atts.getValue(i) << endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FGTrafficManager::endElement (const char * name) {
|
|
|
|
//cout << "End element " << name << endl;
|
|
|
|
string element(name);
|
|
|
|
if (element == string("model"))
|
|
|
|
mdl = value;
|
|
|
|
else if (element == string("livery"))
|
|
|
|
livery = value;
|
|
|
|
else if (element == string("registration"))
|
|
|
|
registration = value;
|
2005-02-10 09:01:51 +00:00
|
|
|
else if (element == string("airline"))
|
|
|
|
airline = value;
|
|
|
|
else if (element == string("actype"))
|
|
|
|
acType = value;
|
|
|
|
else if (element == string("flighttype"))
|
|
|
|
flighttype = value;
|
|
|
|
else if (element == string("radius"))
|
|
|
|
radius = atoi(value.c_str());
|
|
|
|
else if (element == string("offset"))
|
|
|
|
offset = atoi(value.c_str());
|
|
|
|
else if (element == string("performance-class"))
|
|
|
|
m_class = value;
|
2004-06-03 17:59:14 +00:00
|
|
|
else if (element == string("heavy"))
|
|
|
|
{
|
|
|
|
if(value == string("true"))
|
|
|
|
heavy = true;
|
|
|
|
else
|
|
|
|
heavy = false;
|
|
|
|
}
|
|
|
|
else if (element == string("callsign"))
|
|
|
|
callsign = value;
|
|
|
|
else if (element == string("fltrules"))
|
|
|
|
fltrules = value;
|
|
|
|
else if (element == string("port"))
|
|
|
|
port = value;
|
|
|
|
else if (element == string("time"))
|
|
|
|
timeString = value;
|
|
|
|
else if (element == string("departure"))
|
|
|
|
{
|
|
|
|
departurePort = port;
|
|
|
|
departureTime = timeString;
|
|
|
|
}
|
|
|
|
else if (element == string("cruise-alt"))
|
|
|
|
cruiseAlt = atoi(value.c_str());
|
|
|
|
else if (element == string("arrival"))
|
|
|
|
{
|
|
|
|
arrivalPort = port;
|
|
|
|
arrivalTime = timeString;
|
|
|
|
}
|
|
|
|
else if (element == string("repeat"))
|
|
|
|
repeat = value;
|
|
|
|
else if (element == string("flight"))
|
|
|
|
{
|
|
|
|
// We have loaded and parsed all the information belonging to this flight
|
|
|
|
// so we temporarily store it.
|
|
|
|
//cerr << "Pusing back flight " << callsign << endl;
|
|
|
|
//cerr << callsign << " " << fltrules << " "<< departurePort << " " << arrivalPort << " "
|
|
|
|
// << cruiseAlt << " " << departureTime<< " "<< arrivalTime << " " << repeat << endl;
|
2006-07-29 18:17:19 +00:00
|
|
|
|
|
|
|
//Prioritize aircraft
|
|
|
|
string apt = fgGetString("/sim/presets/airport-id");
|
|
|
|
//cerr << "Airport information: " << apt << " " << departurePort << " " << arrivalPort << endl;
|
|
|
|
if (departurePort == apt) score++;
|
2006-10-06 17:36:31 +00:00
|
|
|
flights.push_back(new FGScheduledFlight(callsign,
|
2004-06-03 17:59:14 +00:00
|
|
|
fltrules,
|
|
|
|
departurePort,
|
|
|
|
arrivalPort,
|
|
|
|
cruiseAlt,
|
|
|
|
departureTime,
|
|
|
|
arrivalTime,
|
|
|
|
repeat));
|
|
|
|
}
|
|
|
|
else if (element == string("aircraft"))
|
|
|
|
{
|
|
|
|
//cerr << "Pushing back aircraft " << registration << endl;
|
2006-10-06 17:36:31 +00:00
|
|
|
scheduledAircraft.push_back(new FGAISchedule(mdl,
|
2005-02-10 09:01:51 +00:00
|
|
|
livery,
|
|
|
|
registration,
|
|
|
|
heavy,
|
|
|
|
acType,
|
|
|
|
airline,
|
|
|
|
m_class,
|
|
|
|
flighttype,
|
|
|
|
radius,
|
|
|
|
offset,
|
2006-07-29 18:17:19 +00:00
|
|
|
score,
|
2005-02-10 09:01:51 +00:00
|
|
|
flights));
|
2006-10-06 17:36:31 +00:00
|
|
|
// while(flights.begin() != flights.end()) {
|
|
|
|
// flights.pop_back();
|
|
|
|
// }
|
|
|
|
for (FGScheduledFlightVecIterator flt = flights.begin(); flt != flights.end(); flt++)
|
|
|
|
{
|
|
|
|
delete (*flt);
|
|
|
|
}
|
|
|
|
flights.clear();
|
2006-07-29 18:17:19 +00:00
|
|
|
SG_LOG( SG_GENERAL, SG_BULK, "Reading aircraft : "
|
|
|
|
<< registration
|
|
|
|
<< " with prioritization score "
|
|
|
|
<< score);
|
|
|
|
score = 0;
|
2004-06-03 17:59:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void FGTrafficManager::data (const char * s, int len) {
|
|
|
|
string token = string(s,len);
|
|
|
|
//cout << "Character data " << string(s,len) << endl;
|
|
|
|
if ((token.find(" ") == string::npos && (token.find('\n')) == string::npos))
|
|
|
|
value += token;
|
|
|
|
else
|
|
|
|
value = string("");
|
|
|
|
}
|
|
|
|
|
|
|
|
void FGTrafficManager::pi (const char * target, const char * data) {
|
|
|
|
//cout << "Processing instruction " << target << ' ' << data << endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FGTrafficManager::warning (const char * message, int line, int column) {
|
2006-03-31 16:56:14 +00:00
|
|
|
SG_LOG(SG_IO, SG_WARN, "Warning: " << message << " (" << line << ',' << column << ')');
|
2004-06-03 17:59:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void FGTrafficManager::error (const char * message, int line, int column) {
|
2006-03-31 16:56:14 +00:00
|
|
|
SG_LOG(SG_IO, SG_ALERT, "Error: " << message << " (" << line << ',' << column << ')');
|
2004-06-03 17:59:14 +00:00
|
|
|
}
|