2004-06-03 17:59:14 +00:00
|
|
|
/******************************************************************************
|
|
|
|
* Schedule.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
|
|
|
*
|
|
|
|
*
|
|
|
|
****************************************************************************
|
|
|
|
*
|
|
|
|
*****************************************************************************/
|
2006-02-18 13:58:09 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2006-07-29 18:17:19 +00:00
|
|
|
#define BOGUS 0xFFFF
|
|
|
|
|
2004-06-03 17:59:14 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <iostream>
|
|
|
|
#include <fstream>
|
|
|
|
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
2004-06-06 08:35:09 +00:00
|
|
|
#include <algorithm>
|
2004-06-03 17:59:14 +00:00
|
|
|
|
|
|
|
#include <simgear/compiler.h>
|
2009-01-04 17:11:25 +00:00
|
|
|
#include <simgear/sg_inlines.h>
|
2004-06-03 17:59:14 +00:00
|
|
|
#include <simgear/math/sg_geodesy.hxx>
|
|
|
|
#include <simgear/props/props.hxx>
|
|
|
|
#include <simgear/route/waypoint.hxx>
|
|
|
|
#include <simgear/structure/subsystem_mgr.hxx>
|
|
|
|
#include <simgear/xml/easyxml.hxx>
|
|
|
|
|
|
|
|
#include <AIModel/AIFlightPlan.hxx>
|
|
|
|
#include <AIModel/AIManager.hxx>
|
2006-02-11 13:16:56 +00:00
|
|
|
#include <AIModel/AIAircraft.hxx>
|
2004-06-03 17:59:14 +00:00
|
|
|
#include <Airports/simple.hxx>
|
|
|
|
#include <Main/fg_init.hxx> // That's pretty ugly, but I need fgFindAirportID
|
|
|
|
|
|
|
|
|
|
|
|
#include "SchedFlight.hxx"
|
|
|
|
#include "TrafficMgr.hxx"
|
|
|
|
|
2008-07-29 08:27:48 +00:00
|
|
|
using std::sort;
|
2004-06-03 17:59:14 +00:00
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
* the FGAISchedule class contains data members and code to maintain a
|
|
|
|
* schedule of Flights for an articically controlled aircraft.
|
|
|
|
*****************************************************************************/
|
|
|
|
FGAISchedule::FGAISchedule()
|
|
|
|
{
|
|
|
|
firstRun = true;
|
|
|
|
AIManagerRef = 0;
|
2005-12-06 18:32:07 +00:00
|
|
|
|
|
|
|
heavy = false;
|
|
|
|
lat = 0;
|
|
|
|
lon = 0;
|
|
|
|
radius = 0;
|
|
|
|
groundOffset = 0;
|
|
|
|
distanceToUser = 0;
|
2008-11-16 13:45:24 +00:00
|
|
|
//score = 0;
|
2004-06-03 17:59:14 +00:00
|
|
|
}
|
|
|
|
|
2008-11-16 13:45:24 +00:00
|
|
|
/*
|
2004-06-03 17:59:14 +00:00
|
|
|
FGAISchedule::FGAISchedule(string mdl,
|
|
|
|
string liv,
|
|
|
|
string reg,
|
|
|
|
bool hvy,
|
2005-02-10 09:01:51 +00:00
|
|
|
string act,
|
|
|
|
string arln,
|
|
|
|
string mclass,
|
|
|
|
string fltpe,
|
|
|
|
double rad,
|
|
|
|
double grnd,
|
2006-07-29 18:17:19 +00:00
|
|
|
int scre,
|
2008-11-16 13:45:24 +00:00
|
|
|
FGScheduledFlightVec flt)*/
|
|
|
|
FGAISchedule::FGAISchedule(string model,
|
|
|
|
string lvry,
|
|
|
|
string port,
|
|
|
|
string reg,
|
|
|
|
string flightId,
|
|
|
|
bool hvy,
|
|
|
|
string act,
|
|
|
|
string arln,
|
|
|
|
string mclass,
|
|
|
|
string fltpe,
|
|
|
|
double rad,
|
|
|
|
double grnd)
|
2004-06-03 17:59:14 +00:00
|
|
|
{
|
2008-11-16 13:45:24 +00:00
|
|
|
modelPath = model;
|
|
|
|
livery = lvry;
|
|
|
|
homePort = port;
|
|
|
|
registration = reg;
|
|
|
|
flightIdentifier = flightId;
|
|
|
|
acType = act;
|
|
|
|
airline = arln;
|
|
|
|
m_class = mclass;
|
|
|
|
flightType = fltpe;
|
|
|
|
lat = 0;
|
|
|
|
lon = 0;
|
|
|
|
radius = rad;
|
|
|
|
groundOffset = grnd;
|
|
|
|
distanceToUser = 0;
|
|
|
|
heavy = hvy;
|
|
|
|
/*for (FGScheduledFlightVecIterator i = flt.begin();
|
2004-06-03 17:59:14 +00:00
|
|
|
i != flt.end();
|
|
|
|
i++)
|
2008-11-16 13:45:24 +00:00
|
|
|
flights.push_back(new FGScheduledFlight((*(*i))));*/
|
|
|
|
AIManagerRef = 0;
|
|
|
|
//score = scre;
|
|
|
|
firstRun = true;
|
2004-06-03 17:59:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
FGAISchedule::FGAISchedule(const FGAISchedule &other)
|
|
|
|
{
|
2008-11-16 13:45:24 +00:00
|
|
|
modelPath = other.modelPath;
|
|
|
|
homePort = other.homePort;
|
|
|
|
livery = other.livery;
|
|
|
|
registration = other.registration;
|
|
|
|
heavy = other.heavy;
|
|
|
|
flightIdentifier = other.flightIdentifier;
|
|
|
|
flights = other.flights;
|
|
|
|
lat = other.lat;
|
|
|
|
lon = other.lon;
|
|
|
|
AIManagerRef = other.AIManagerRef;
|
|
|
|
acType = other.acType;
|
|
|
|
airline = other.airline;
|
|
|
|
m_class = other.m_class;
|
|
|
|
firstRun = other.firstRun;
|
|
|
|
radius = other.radius;
|
|
|
|
groundOffset = other.groundOffset;
|
|
|
|
flightType = other.flightType;
|
|
|
|
//score = other.score;
|
|
|
|
distanceToUser = other.distanceToUser;
|
|
|
|
currentDestination = other.currentDestination;
|
|
|
|
firstRun = other.firstRun;
|
2004-06-03 17:59:14 +00:00
|
|
|
}
|
|
|
|
|
2005-02-10 09:01:51 +00:00
|
|
|
|
2004-06-03 17:59:14 +00:00
|
|
|
FGAISchedule::~FGAISchedule()
|
|
|
|
{
|
2008-11-16 13:45:24 +00:00
|
|
|
/* for (FGScheduledFlightVecIterator flt = flights.begin(); flt != flights.end(); flt++)
|
2006-10-06 17:36:31 +00:00
|
|
|
{
|
|
|
|
delete (*flt);
|
|
|
|
}
|
2008-11-16 13:45:24 +00:00
|
|
|
flights.clear();*/
|
2004-06-03 17:59:14 +00:00
|
|
|
}
|
|
|
|
|
2005-02-10 09:01:51 +00:00
|
|
|
bool FGAISchedule::init()
|
|
|
|
{
|
|
|
|
//tm targetTimeDate;
|
|
|
|
//SGTime* currTimeDate = globals->get_time_params();
|
|
|
|
|
|
|
|
//tm *temp = currTimeDate->getGmt();
|
|
|
|
//char buffer[512];
|
|
|
|
//sgTimeFormatTime(&targetTimeDate, buffer);
|
|
|
|
//cout << "Scheduled Time " << buffer << endl;
|
|
|
|
//cout << "Time :" << time(NULL) << " SGTime : " << sgTimeGetGMT(temp) << endl;
|
2008-11-16 13:45:24 +00:00
|
|
|
/*for (FGScheduledFlightVecIterator i = flights.begin();
|
2005-02-10 09:01:51 +00:00
|
|
|
i != flights.end();
|
|
|
|
i++)
|
|
|
|
{
|
|
|
|
//i->adjustTime(now);
|
2006-10-06 17:36:31 +00:00
|
|
|
if (!((*i)->initializeAirports()))
|
2005-02-10 09:01:51 +00:00
|
|
|
return false;
|
2008-11-16 13:45:24 +00:00
|
|
|
} */
|
2005-02-10 09:01:51 +00:00
|
|
|
//sort(flights.begin(), flights.end());
|
|
|
|
// Since time isn't initialized yet when this function is called,
|
|
|
|
// Find the closest possible airport.
|
|
|
|
// This should give a reasonable initialization order.
|
2006-07-29 18:17:19 +00:00
|
|
|
//setClosestDistanceToUser();
|
2005-02-10 09:01:51 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FGAISchedule::update(time_t now)
|
2008-11-16 13:45:24 +00:00
|
|
|
{
|
2004-06-03 17:59:14 +00:00
|
|
|
FGAirport *dep;
|
|
|
|
FGAirport *arr;
|
|
|
|
double angle;
|
|
|
|
|
|
|
|
FGAIManager *aimgr;
|
|
|
|
string airport;
|
|
|
|
|
2009-02-15 15:29:56 +00:00
|
|
|
double courseToUser;
|
2005-02-10 09:01:51 +00:00
|
|
|
double distanceToDest;
|
2004-06-03 17:59:14 +00:00
|
|
|
double speed;
|
|
|
|
|
|
|
|
time_t
|
|
|
|
totalTimeEnroute,
|
|
|
|
elapsedTimeEnroute,
|
2006-08-26 07:22:20 +00:00
|
|
|
remainingTimeEnroute, deptime = 0;
|
2004-06-03 17:59:14 +00:00
|
|
|
double
|
|
|
|
userLatitude,
|
|
|
|
userLongitude;
|
|
|
|
|
2004-07-22 18:50:29 +00:00
|
|
|
if (fgGetBool("/sim/traffic-manager/enabled") == false)
|
2005-02-10 09:01:51 +00:00
|
|
|
return true;
|
2004-06-03 17:59:14 +00:00
|
|
|
|
|
|
|
aimgr = (FGAIManager *) globals-> get_subsystem("ai_model");
|
2008-11-16 13:45:24 +00:00
|
|
|
// Out-of-work aircraft seeks employment. Willing to work irregular hours ...
|
|
|
|
//cerr << "About to find a flight " << endl;
|
|
|
|
if (flights.empty()) {
|
|
|
|
//execute this loop at least once.
|
|
|
|
SG_LOG(SG_GENERAL, SG_INFO, "Scheduling for : " << modelPath << " " << registration << " " << homePort);
|
|
|
|
FGScheduledFlight *flight = 0;
|
|
|
|
do {
|
|
|
|
flight = findAvailableFlight(currentDestination, flightIdentifier);
|
|
|
|
if (flight) {
|
|
|
|
currentDestination = flight->getArrivalAirport()->getId();
|
|
|
|
time_t arr, dep;
|
|
|
|
dep = flight->getDepartureTime();
|
|
|
|
arr = flight->getArrivalTime();
|
|
|
|
string depT = asctime(gmtime(&dep));
|
|
|
|
string arrT = asctime(gmtime(&arr));
|
|
|
|
|
|
|
|
depT = depT.substr(0,24);
|
|
|
|
arrT = arrT.substr(0,24);
|
|
|
|
SG_LOG(SG_GENERAL, SG_INFO, " " << flight->getCallSign() << ":"
|
|
|
|
<< " " << flight->getDepartureAirport()->getId() << ":"
|
|
|
|
<< " " << depT << ":"
|
|
|
|
<< " \"" << flight->getArrivalAirport()->getId() << "\"" << ":"
|
|
|
|
<< " " << arrT << ":");
|
|
|
|
flights.push_back(flight);
|
|
|
|
}
|
|
|
|
} while ((currentDestination != homePort) && (flight != 0));
|
|
|
|
SG_LOG(SG_GENERAL, SG_INFO, cerr << " Done " << endl);
|
2004-06-03 17:59:14 +00:00
|
|
|
}
|
2008-11-16 13:45:24 +00:00
|
|
|
//cerr << " Done " << endl;
|
|
|
|
// No flights available for this aircraft
|
|
|
|
if (flights.size() == 0) {
|
|
|
|
return false;
|
|
|
|
}
|
2004-06-03 17:59:14 +00:00
|
|
|
// Sort all the scheduled flights according to scheduled departure time.
|
|
|
|
// Because this is done at every update, we only need to check the status
|
|
|
|
// of the first listed flight.
|
2008-11-16 13:45:24 +00:00
|
|
|
//sort(flights.begin(), flights.end(), compareScheduledFlights);
|
|
|
|
if (firstRun) {
|
|
|
|
if (fgGetBool("/sim/traffic-manager/instantaneous-action") == true) {
|
|
|
|
deptime = now + rand() % 300; // Wait up to 5 minutes until traffic starts moving to prevent too many aircraft
|
|
|
|
// from cluttering the gate areas.
|
|
|
|
cerr << "Scheduling " << registration << " for instantaneous action flight " << endl;
|
|
|
|
}
|
|
|
|
firstRun = false;
|
|
|
|
}
|
2006-08-26 07:22:20 +00:00
|
|
|
if (!deptime)
|
2006-10-06 17:36:31 +00:00
|
|
|
deptime = (*flights.begin())->getDepartureTime();
|
2004-06-03 17:59:14 +00:00
|
|
|
FGScheduledFlightVecIterator i = flights.begin();
|
2008-05-08 06:11:43 +00:00
|
|
|
SG_LOG (SG_GENERAL, SG_DEBUG,"Traffic Manager: Processing registration " << registration << " with callsign " << (*i)->getCallSign());
|
2004-11-29 09:41:43 +00:00
|
|
|
if (AIManagerRef)
|
|
|
|
{
|
|
|
|
// Check if this aircraft has been released.
|
|
|
|
FGTrafficManager *tmgr = (FGTrafficManager *) globals->get_subsystem("Traffic Manager");
|
|
|
|
if (tmgr->isReleased(AIManagerRef))
|
|
|
|
AIManagerRef = 0;
|
|
|
|
}
|
|
|
|
|
2004-06-03 17:59:14 +00:00
|
|
|
if (!AIManagerRef)
|
|
|
|
{
|
|
|
|
userLatitude = fgGetDouble("/position/latitude-deg");
|
|
|
|
userLongitude = fgGetDouble("/position/longitude-deg");
|
|
|
|
|
2005-02-10 09:01:51 +00:00
|
|
|
//cerr << "Estimated minimum distance to user: " << distanceToUser << endl;
|
2004-06-03 17:59:14 +00:00
|
|
|
// This flight entry is entirely in the past, do we need to
|
|
|
|
// push it forward in time to the next scheduled departure.
|
2006-10-06 17:36:31 +00:00
|
|
|
if (((*i)->getDepartureTime() < now) && ((*i)->getArrivalTime() < now))
|
2004-06-03 17:59:14 +00:00
|
|
|
{
|
2008-05-08 06:11:43 +00:00
|
|
|
SG_LOG (SG_GENERAL, SG_DEBUG, "Traffic Manager: Flight is in the Past");
|
2008-11-16 13:45:24 +00:00
|
|
|
//cerr << modelPath << " " << registration << ": Flights from the past belong to the past :-)" << endl;
|
|
|
|
//exit(1);
|
|
|
|
// Don't just update: check whether we need to load a new leg. etc.
|
|
|
|
// This update occurs for distant aircraft, so we can update the current leg
|
|
|
|
// and detach it from the current list of aircraft.
|
2006-10-06 17:36:31 +00:00
|
|
|
(*i)->update();
|
2008-11-16 13:45:24 +00:00
|
|
|
i = flights.erase(i);
|
2005-02-10 09:01:51 +00:00
|
|
|
return true;
|
2004-06-03 17:59:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Departure time in the past and arrival time in the future.
|
|
|
|
// This flight is in progress, so we need to calculate it's
|
|
|
|
// approximate position and -if in range- create an AIAircraft
|
|
|
|
// object for it.
|
2004-11-29 09:41:43 +00:00
|
|
|
//if ((i->getDepartureTime() < now) && (i->getArrivalTime() > now))
|
|
|
|
|
|
|
|
// Part of this flight is in the future.
|
2006-10-06 17:36:31 +00:00
|
|
|
if ((*i)->getArrivalTime() > now)
|
2004-06-03 17:59:14 +00:00
|
|
|
{
|
2008-05-08 06:11:43 +00:00
|
|
|
|
2006-10-06 17:36:31 +00:00
|
|
|
dep = (*i)->getDepartureAirport();
|
|
|
|
arr = (*i)->getArrivalAirport ();
|
2005-02-10 09:01:51 +00:00
|
|
|
if (!(dep && arr))
|
|
|
|
return false;
|
2004-06-03 17:59:14 +00:00
|
|
|
|
2007-07-08 08:46:29 +00:00
|
|
|
SGVec3d a = SGVec3d::fromGeoc(SGGeoc::fromDegM(dep->getLongitude(),
|
|
|
|
dep->getLatitude(), 1));
|
|
|
|
SGVec3d b = SGVec3d::fromGeoc(SGGeoc::fromDegM(arr->getLongitude(),
|
|
|
|
arr->getLatitude(), 1));
|
|
|
|
SGVec3d _cross = cross(b, a);
|
2004-06-03 17:59:14 +00:00
|
|
|
|
2007-07-08 08:46:29 +00:00
|
|
|
angle = sgACos(dot(a, b));
|
2004-06-03 17:59:14 +00:00
|
|
|
|
|
|
|
// Okay, at this point we have the angle between departure and
|
|
|
|
// arrival airport, in degrees. From here we can interpolate the
|
|
|
|
// position of the aircraft by calculating the ratio between
|
|
|
|
// total time enroute and elapsed time enroute.
|
2004-11-29 09:41:43 +00:00
|
|
|
|
2006-10-06 17:36:31 +00:00
|
|
|
totalTimeEnroute = (*i)->getArrivalTime() - (*i)->getDepartureTime();
|
|
|
|
if (now > (*i)->getDepartureTime())
|
2005-02-10 09:01:51 +00:00
|
|
|
{
|
|
|
|
//err << "Lat = " << lat << ", lon = " << lon << endl;
|
|
|
|
//cerr << "Time diff: " << now-i->getDepartureTime() << endl;
|
2006-10-06 17:36:31 +00:00
|
|
|
elapsedTimeEnroute = now - (*i)->getDepartureTime();
|
|
|
|
remainingTimeEnroute = (*i)->getArrivalTime() - now;
|
2008-05-08 06:11:43 +00:00
|
|
|
SG_LOG (SG_GENERAL, SG_DEBUG, "Traffic Manager: Flight is in progress.");
|
2005-02-10 09:01:51 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
lat = dep->getLatitude();
|
|
|
|
lon = dep->getLongitude();
|
|
|
|
elapsedTimeEnroute = 0;
|
|
|
|
remainingTimeEnroute = totalTimeEnroute;
|
2008-05-08 06:11:43 +00:00
|
|
|
SG_LOG (SG_GENERAL, SG_DEBUG, "Traffic Manager: Flight is pending.");
|
2005-02-10 09:01:51 +00:00
|
|
|
}
|
|
|
|
|
2004-06-03 17:59:14 +00:00
|
|
|
angle *= ( (double) elapsedTimeEnroute/ (double) totalTimeEnroute);
|
|
|
|
|
|
|
|
|
|
|
|
//cout << "a = " << a[0] << " " << a[1] << " " << a[2]
|
|
|
|
// << "b = " << b[0] << " " << b[1] << " " << b[2] << endl;
|
2007-07-08 08:46:29 +00:00
|
|
|
sgdMat4 matrix;
|
|
|
|
sgdMakeRotMat4(matrix, angle, _cross.sg());
|
|
|
|
SGVec3d newPos(0, 0, 0);
|
2004-06-03 17:59:14 +00:00
|
|
|
for(int j = 0; j < 3; j++)
|
|
|
|
{
|
|
|
|
for (int k = 0; k<3; k++)
|
|
|
|
{
|
|
|
|
newPos[j] += matrix[j][k]*a[k];
|
|
|
|
}
|
|
|
|
}
|
2005-02-10 09:01:51 +00:00
|
|
|
|
2006-10-06 17:36:31 +00:00
|
|
|
if (now > (*i)->getDepartureTime())
|
2004-11-29 09:41:43 +00:00
|
|
|
{
|
2007-07-27 19:31:44 +00:00
|
|
|
SGGeoc geoc = SGGeoc::fromCart(newPos);
|
|
|
|
lat = geoc.getLatitudeDeg();
|
|
|
|
lon = geoc.getLongitudeDeg();
|
2004-11-29 09:41:43 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2005-02-10 09:01:51 +00:00
|
|
|
lat = dep->getLatitude();
|
|
|
|
lon = dep->getLongitude();
|
2004-11-29 09:41:43 +00:00
|
|
|
}
|
2004-06-03 17:59:14 +00:00
|
|
|
|
2005-02-10 09:01:51 +00:00
|
|
|
|
2004-06-03 17:59:14 +00:00
|
|
|
SGWayPoint current (lon,
|
|
|
|
lat,
|
2006-12-27 10:02:13 +00:00
|
|
|
(*i)->getCruiseAlt(),
|
|
|
|
SGWayPoint::SPHERICAL);
|
2004-06-03 17:59:14 +00:00
|
|
|
SGWayPoint user ( userLongitude,
|
|
|
|
userLatitude,
|
2006-12-27 10:02:13 +00:00
|
|
|
(*i)->getCruiseAlt(),
|
|
|
|
SGWayPoint::SPHERICAL);
|
2005-02-10 09:01:51 +00:00
|
|
|
SGWayPoint dest ( arr->getLongitude(),
|
|
|
|
arr->getLatitude(),
|
2006-12-27 10:02:13 +00:00
|
|
|
(*i)->getCruiseAlt(),
|
|
|
|
SGWayPoint::SPHERICAL);
|
2004-06-03 17:59:14 +00:00
|
|
|
// We really only need distance to user
|
|
|
|
// and course to destination
|
2004-11-29 09:41:43 +00:00
|
|
|
user.CourseAndDistance(current, &courseToUser, &distanceToUser);
|
|
|
|
dest.CourseAndDistance(current, &courseToDest, &distanceToDest);
|
2004-06-03 17:59:14 +00:00
|
|
|
speed = (distanceToDest*SG_METER_TO_NM) /
|
|
|
|
((double) remainingTimeEnroute/3600.0);
|
|
|
|
|
|
|
|
|
|
|
|
// If distance between user and simulated aircaft is less
|
|
|
|
// then 500nm, create this flight. At jet speeds 500 nm is roughly
|
|
|
|
// one hour flight time, so that would be a good approximate point
|
|
|
|
// to start a more detailed simulation of this aircraft.
|
2008-05-08 06:11:43 +00:00
|
|
|
SG_LOG (SG_GENERAL, SG_DEBUG, "Traffic manager: " << registration << " is scheduled for a flight from "
|
|
|
|
<< dep->getId() << " to " << arr->getId() << ". Current distance to user: "
|
|
|
|
<< distanceToUser*SG_METER_TO_NM);
|
2008-11-16 13:45:24 +00:00
|
|
|
if ((distanceToUser*SG_METER_TO_NM) < TRAFFICTOAIDISTTOSTART)
|
2004-06-03 17:59:14 +00:00
|
|
|
{
|
2005-02-10 09:01:51 +00:00
|
|
|
string flightPlanName = dep->getId() + string("-") + arr->getId() +
|
2004-07-22 18:50:29 +00:00
|
|
|
string(".xml");
|
2008-05-08 06:11:43 +00:00
|
|
|
SG_LOG (SG_GENERAL, SG_DEBUG, "Traffic manager: Creating AIModel");
|
2006-12-17 18:40:55 +00:00
|
|
|
//int alt;
|
2004-11-29 09:41:43 +00:00
|
|
|
//if ((i->getDepartureTime() < now))
|
|
|
|
//{
|
|
|
|
// alt = i->getCruiseAlt() *100;
|
|
|
|
// }
|
|
|
|
//else
|
|
|
|
//{
|
2004-12-22 23:57:07 +00:00
|
|
|
// alt = dep->_elevation+19;
|
2004-11-29 09:41:43 +00:00
|
|
|
// }
|
2004-06-03 17:59:14 +00:00
|
|
|
|
2006-12-17 08:01:56 +00:00
|
|
|
// Only allow traffic to be created when the model path (or the AI version of mp) exists
|
2006-03-11 18:14:48 +00:00
|
|
|
SGPath mp(globals->get_fg_root());
|
2006-12-17 08:01:56 +00:00
|
|
|
SGPath mp_ai = mp;
|
|
|
|
|
2006-03-11 18:14:48 +00:00
|
|
|
mp.append(modelPath);
|
2006-12-17 08:01:56 +00:00
|
|
|
mp_ai.append("AI");
|
|
|
|
mp_ai.append(modelPath);
|
|
|
|
|
|
|
|
if (mp.exists() || mp_ai.exists())
|
2006-03-11 18:14:48 +00:00
|
|
|
{
|
|
|
|
FGAIAircraft *aircraft = new FGAIAircraft(this);
|
|
|
|
aircraft->setPerformance(m_class); //"jet_transport";
|
|
|
|
aircraft->setCompany(airline); //i->getAirline();
|
|
|
|
aircraft->setAcType(acType); //i->getAcType();
|
|
|
|
aircraft->setPath(modelPath.c_str());
|
2006-03-29 18:36:21 +00:00
|
|
|
//aircraft->setFlightPlan(flightPlanName);
|
2006-03-11 18:14:48 +00:00
|
|
|
aircraft->setLatitude(lat);
|
|
|
|
aircraft->setLongitude(lon);
|
2006-10-06 17:36:31 +00:00
|
|
|
aircraft->setAltitude((*i)->getCruiseAlt()*100); // convert from FL to feet
|
2006-03-11 18:14:48 +00:00
|
|
|
aircraft->setSpeed(speed);
|
|
|
|
aircraft->setBank(0);
|
2009-01-30 18:48:44 +00:00
|
|
|
aircraft->SetFlightPlan(new FGAIFlightPlan(aircraft, flightPlanName, courseToDest, deptime,
|
2006-08-26 07:22:20 +00:00
|
|
|
dep, arr,true, radius,
|
2006-10-06 17:36:31 +00:00
|
|
|
(*i)->getCruiseAlt()*100,
|
2006-08-26 07:22:20 +00:00
|
|
|
lat, lon, speed, flightType, acType,
|
|
|
|
airline));
|
2006-03-11 18:14:48 +00:00
|
|
|
aimgr->attach(aircraft);
|
|
|
|
|
|
|
|
|
|
|
|
AIManagerRef = aircraft->getID();
|
|
|
|
//cerr << "Class: " << m_class << ". acType: " << acType << ". Airline: " << airline << ". Speed = " << speed << ". From " << dep->getId() << " to " << arr->getId() << ". Time Fraction = " << (remainingTimeEnroute/(double) totalTimeEnroute) << endl;
|
|
|
|
//cerr << "Latitude : " << lat << ". Longitude : " << lon << endl;
|
|
|
|
//cerr << "Dep : " << dep->getLatitude()<< ", "<< dep->getLongitude() << endl;
|
|
|
|
//cerr << "Arr : " << arr->getLatitude()<< ", "<< arr->getLongitude() << endl;
|
|
|
|
//cerr << "Time remaining = " << (remainingTimeEnroute/3600.0) << endl;
|
|
|
|
//cerr << "Total time = " << (totalTimeEnroute/3600.0) << endl;
|
|
|
|
//cerr << "Distance remaining = " << distanceToDest*SG_METER_TO_NM << endl;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
SG_LOG(SG_INPUT, SG_WARN, "TrafficManager: Could not load model " << mp.str());
|
|
|
|
}
|
2004-06-03 17:59:14 +00:00
|
|
|
}
|
2005-02-10 09:01:51 +00:00
|
|
|
return true;
|
|
|
|
}
|
2004-07-22 18:50:29 +00:00
|
|
|
|
2004-06-03 17:59:14 +00:00
|
|
|
// Both departure and arrival time are in the future, so this
|
|
|
|
// the aircraft is parked at the departure airport.
|
2004-11-29 09:41:43 +00:00
|
|
|
// Currently this status is mostly ignored, but in future
|
2004-06-03 17:59:14 +00:00
|
|
|
// versions, code should go here that -if within user range-
|
|
|
|
// positions these aircraft at parking locations at the airport.
|
2006-10-06 17:36:31 +00:00
|
|
|
if (((*i)->getDepartureTime() > now) && ((*i)->getArrivalTime() > now))
|
2004-06-03 17:59:14 +00:00
|
|
|
{
|
2006-10-06 17:36:31 +00:00
|
|
|
dep = (*i)->getDepartureAirport();
|
2005-02-10 09:01:51 +00:00
|
|
|
return true;
|
2004-06-03 17:59:14 +00:00
|
|
|
}
|
|
|
|
}
|
2005-10-18 18:44:37 +00:00
|
|
|
//cerr << "Traffic schedule got to beyond last clause" << endl;
|
2005-04-19 12:52:26 +00:00
|
|
|
// EMH: prevent a warning, should this be 'true' instead?
|
2005-10-18 18:44:37 +00:00
|
|
|
// DT: YES. Originally, this code couldn't be reached, but
|
|
|
|
// when the "if(!(AIManagerManager))" clause is false we
|
|
|
|
// fall through right to the end. This is a valid flow.
|
|
|
|
// the actual value is pretty innocent, only it triggers
|
|
|
|
// warning in TrafficManager::update().
|
|
|
|
// (which was added as a sanity check for myself in the first place. :-)
|
|
|
|
return true;
|
2004-06-03 17:59:14 +00:00
|
|
|
}
|
2004-11-29 09:41:43 +00:00
|
|
|
|
|
|
|
|
2008-11-16 13:45:24 +00:00
|
|
|
bool FGAISchedule::next()
|
|
|
|
{
|
|
|
|
FGScheduledFlightVecIterator i = flights.begin();
|
|
|
|
(*i)->release();
|
|
|
|
//FIXME: remove first entry,
|
|
|
|
// load new flights until back at home airport
|
|
|
|
// Lock loaded flights
|
|
|
|
//sort(flights.begin(), flights.end(), compareScheduledFlights);
|
|
|
|
// until that time
|
|
|
|
i = flights.erase(i);
|
|
|
|
//cerr << "Next: scheduling for : " << modelPath << " " << registration << endl;
|
|
|
|
FGScheduledFlight *flight = findAvailableFlight(currentDestination, flightIdentifier);
|
|
|
|
if (flight) {
|
|
|
|
currentDestination = flight->getArrivalAirport()->getId();
|
|
|
|
time_t arr, dep;
|
|
|
|
dep = flight->getDepartureTime();
|
|
|
|
arr = flight->getArrivalTime();
|
|
|
|
string depT = asctime(gmtime(&dep));
|
|
|
|
string arrT = asctime(gmtime(&arr));
|
|
|
|
|
|
|
|
depT = depT.substr(0,24);
|
|
|
|
arrT = arrT.substr(0,24);
|
|
|
|
//cerr << " " << flight->getCallSign() << ":"
|
|
|
|
// << " " << flight->getDepartureAirport()->getId() << ":"
|
|
|
|
// << " " << depT << ":"
|
|
|
|
// << " \"" << flight->getArrivalAirport()->getId() << "\"" << ":"
|
|
|
|
// << " " << arrT << ":" << endl;
|
|
|
|
|
|
|
|
flights.push_back(flight);
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
//cerr << "FGAISchedule :: next needs updating" << endl;
|
|
|
|
//exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
FGScheduledFlight* FGAISchedule::findAvailableFlight (const string ¤tDestination,
|
|
|
|
const string &req)
|
2004-11-29 09:41:43 +00:00
|
|
|
{
|
2008-11-16 13:45:24 +00:00
|
|
|
time_t now = time(NULL) + fgGetLong("/sim/time/warp");
|
|
|
|
|
|
|
|
FGTrafficManager *tmgr = (FGTrafficManager *) globals->get_subsystem("Traffic Manager");
|
|
|
|
FGScheduledFlightVecIterator fltBegin, fltEnd;
|
|
|
|
fltBegin = tmgr->getFirstFlight(req);
|
|
|
|
fltEnd = tmgr->getLastFlight(req);
|
|
|
|
|
|
|
|
|
|
|
|
//cerr << "Finding available flight " << endl;
|
|
|
|
// For Now:
|
|
|
|
// Traverse every registered flight
|
|
|
|
if (fltBegin == fltEnd) {
|
|
|
|
//cerr << "No Flights Scheduled for " << req << endl;
|
|
|
|
}
|
|
|
|
int counter = 0;
|
|
|
|
for (FGScheduledFlightVecIterator i = fltBegin; i != fltEnd; i++) {
|
|
|
|
(*i)->adjustTime(now);
|
|
|
|
//sort(fltBegin, fltEnd, compareScheduledFlights);
|
|
|
|
//cerr << counter++ << endl;
|
|
|
|
}
|
|
|
|
sort(fltBegin, fltEnd, compareScheduledFlights);
|
|
|
|
for (FGScheduledFlightVecIterator i = fltBegin; i != fltEnd; i++) {
|
|
|
|
//bool valid = true;
|
|
|
|
counter++;
|
|
|
|
if (!(*i)->isAvailable()) {
|
|
|
|
//cerr << (*i)->getCallSign() << "is no longer available" << endl;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!((*i)->getRequirement() == req)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!(((*i)->getArrivalAirport()) && ((*i)->getDepartureAirport()))) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!(currentDestination.empty())) {
|
|
|
|
if (currentDestination != (*i)->getDepartureAirport()->getId()) {
|
|
|
|
//cerr << (*i)->getCallSign() << "Doesn't match destination" << endl;
|
|
|
|
//cerr << "Current Destination " << currentDestination << "Doesnt match flight's " <<
|
|
|
|
// (*i)->getArrivalAirport()->getId() << endl;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//TODO: check time
|
|
|
|
// So, if we actually get here, we have a winner
|
|
|
|
//cerr << "found flight: " << req << " : " << currentDestination << " : " <<
|
|
|
|
// (*i)->getArrivalAirport()->getId() << endl;
|
|
|
|
(*i)->lock();
|
|
|
|
return (*i);
|
|
|
|
}
|
|
|
|
// matches req?
|
|
|
|
// if currentDestination has a value, does it match departure of next flight?
|
|
|
|
// is departure time later than planned arrival?
|
|
|
|
// is departure port valid?
|
|
|
|
// is arrival port valid?
|
|
|
|
//cerr << "Ack no flight found: " << endl;
|
|
|
|
return 0;
|
2004-11-29 09:41:43 +00:00
|
|
|
}
|
|
|
|
|
2005-02-10 09:01:51 +00:00
|
|
|
double FGAISchedule::getSpeed()
|
|
|
|
{
|
|
|
|
FGScheduledFlightVecIterator i = flights.begin();
|
|
|
|
|
2009-01-04 17:11:25 +00:00
|
|
|
FGAirport* dep = (*i)->getDepartureAirport(),
|
|
|
|
*arr = (*i)->getArrivalAirport();
|
|
|
|
double dist = SGGeodesy::distanceNm(dep->geod(), arr->geod());
|
|
|
|
double remainingTimeEnroute = (*i)->getArrivalTime() - (*i)->getDepartureTime();
|
|
|
|
|
|
|
|
double speed = dist / (remainingTimeEnroute/3600.0);
|
|
|
|
SG_CLAMP_RANGE(speed, 300.0, 500.0);
|
2005-02-10 09:01:51 +00:00
|
|
|
return speed;
|
|
|
|
}
|
2008-11-16 14:17:52 +00:00
|
|
|
/*
|
2006-10-06 17:36:31 +00:00
|
|
|
bool compareSchedules(FGAISchedule*a, FGAISchedule*b)
|
|
|
|
{
|
2008-11-16 13:45:24 +00:00
|
|
|
//return (*a) < (*b);
|
2006-10-06 17:36:31 +00:00
|
|
|
}
|
2008-11-16 14:17:52 +00:00
|
|
|
*/
|
2005-02-10 09:01:51 +00:00
|
|
|
|
2006-07-29 18:17:19 +00:00
|
|
|
// void FGAISchedule::setClosestDistanceToUser()
|
|
|
|
// {
|
2005-02-10 09:01:51 +00:00
|
|
|
|
|
|
|
|
2006-07-29 18:17:19 +00:00
|
|
|
// double course;
|
|
|
|
// double dist;
|
2005-02-10 09:01:51 +00:00
|
|
|
|
2006-07-29 18:17:19 +00:00
|
|
|
// Point3D temp;
|
|
|
|
// time_t
|
|
|
|
// totalTimeEnroute,
|
|
|
|
// elapsedTimeEnroute;
|
2005-02-10 09:01:51 +00:00
|
|
|
|
2006-07-29 18:17:19 +00:00
|
|
|
// double userLatitude = fgGetDouble("/position/latitude-deg");
|
|
|
|
// double userLongitude = fgGetDouble("/position/longitude-deg");
|
2005-02-10 09:01:51 +00:00
|
|
|
|
2006-07-29 18:17:19 +00:00
|
|
|
// FGAirport *dep;
|
2005-02-10 09:01:51 +00:00
|
|
|
|
2006-07-29 18:17:19 +00:00
|
|
|
// #if defined( __CYGWIN__) || defined( __MINGW32__)
|
|
|
|
// #define HUGE HUGE_VAL
|
|
|
|
// #endif
|
|
|
|
// distanceToUser = HUGE;
|
|
|
|
// FGScheduledFlightVecIterator i = flights.begin();
|
|
|
|
// while (i != flights.end())
|
|
|
|
// {
|
|
|
|
// dep = i->getDepartureAirport();
|
|
|
|
// //if (!(dep))
|
|
|
|
// //return HUGE;
|
2005-02-10 09:01:51 +00:00
|
|
|
|
2006-07-29 18:17:19 +00:00
|
|
|
// SGWayPoint user ( userLongitude,
|
|
|
|
// userLatitude,
|
|
|
|
// i->getCruiseAlt());
|
|
|
|
// SGWayPoint current (dep->getLongitude(),
|
|
|
|
// dep->getLatitude(),
|
|
|
|
// 0);
|
|
|
|
// user.CourseAndDistance(current, &course, &dist);
|
|
|
|
// if (dist < distanceToUser)
|
|
|
|
// {
|
|
|
|
// distanceToUser = dist;
|
|
|
|
// //cerr << "Found closest distance to user for " << registration << " to be " << distanceToUser << " at airport " << dep->getId() << endl;
|
|
|
|
// }
|
|
|
|
// i++;
|
|
|
|
// }
|
|
|
|
// //return distToUser;
|
|
|
|
// }
|
2006-10-06 17:36:31 +00:00
|
|
|
|