#include <simgear/sg_inlines.h>
#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>
/******************************************************************************
* the FGAISchedule class contains data members and code to maintain a
- * schedule of Flights for an articically controlled aircraft.
+ * schedule of Flights for an artificially controlled aircraft.
*****************************************************************************/
FGAISchedule::FGAISchedule()
{
FGAISchedule::~FGAISchedule()
{
+ // remove related object from AI manager
+ if (AIManagerRef)
+ {
+ FGAIManager* aimgr = (FGAIManager *) globals-> get_subsystem("ai-model");
+ if (aimgr)
+ aimgr->destroyObject(AIManagerRef);
+ AIManagerRef = 0;
+ }
+
/* for (FGScheduledFlightVecIterator flt = flights.begin(); flt != flights.end(); flt++)
{
delete (*flt);
time_t
totalTimeEnroute,
elapsedTimeEnroute,
- remainingTimeEnroute,
+ //remainingTimeEnroute,
deptime = 0;
if (!valid) {
return false;
firstRun = false;
}
- FGScheduledFlight* flight = flights.front();
+ FGScheduledFlight* flight = flights.front();
if (!deptime) {
deptime = flight->getDepartureTime();
//cerr << "Settiing departure time " << deptime << endl;
if (AIManagerRef) {
// Check if this aircraft has been released.
- FGTrafficManager *tmgr = (FGTrafficManager *) globals->get_subsystem("Traffic Manager");
+ FGTrafficManager *tmgr = (FGTrafficManager *) globals->get_subsystem("traffic-manager");
if (tmgr->isReleased(AIManagerRef)) {
AIManagerRef = 0;
} else {
// 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.
- flight->update();
+ flight->update();
flights.erase(flights.begin()); // pop_front(), effectively
- return true;
- }
+ return true;
+ }
FGAirport* dep = flight->getDepartureAirport();
FGAirport* arr = flight->getArrivalAirport();
totalTimeEnroute = flight->getArrivalTime() - flight->getDepartureTime();
if (flight->getDepartureTime() < now) {
elapsedTimeEnroute = now - flight->getDepartureTime();
- remainingTimeEnroute = totalTimeEnroute - elapsedTimeEnroute;
+ //remainingTimeEnroute = totalTimeEnroute - elapsedTimeEnroute;
double x = elapsedTimeEnroute / (double) totalTimeEnroute;
// current pos is based on great-circle course between departure/arrival,
speed = ((distanceM - coveredDistance) * SG_METER_TO_NM) / 3600.0;
} else {
// not departed yet
- remainingTimeEnroute = totalTimeEnroute;
+ //remainingTimeEnroute = totalTimeEnroute;
elapsedTimeEnroute = 0;
position = dep->geod();
SG_LOG (SG_GENERAL, SG_BULK, "Traffic Manager: Flight is pending, departure in "
}
} else {
// departure / arrival coincident
- remainingTimeEnroute = totalTimeEnroute = 0.0;
+ //remainingTimeEnroute = totalTimeEnroute = 0.0;
elapsedTimeEnroute = 0;
position = dep->geod();
}
return createAIAircraft(flight, speed, deptime);
}
+bool FGAISchedule::validModelPath(const std::string& modelPath)
+{
+ SGPath mp(globals->get_fg_root());
+ SGPath mp_ai = mp;
+
+ mp.append(modelPath);
+ mp_ai.append("AI");
+ mp_ai.append(modelPath);
+
+ return mp.exists() || mp_ai.exists();
+}
+
bool FGAISchedule::createAIAircraft(FGScheduledFlight* flight, double speedKnots, time_t deptime)
{
FGAirport* dep = flight->getDepartureAirport();
mp_ai.append(modelPath);
if (!mp.exists() && !mp_ai.exists()) {
- SG_LOG(SG_GENERAL, SG_WARN, "TrafficManager: Could not load model " << mp.str());
+ SG_LOG(SG_GENERAL, SG_WARN, "TrafficManager: Could not load model " << mp_ai.str());
return true;
}
aircraft->setLatitude(position.getLatitudeDeg());
aircraft->setLongitude(position.getLongitudeDeg());
aircraft->setAltitude(flight->getCruiseAlt()*100); // convert from FL to feet
- aircraft->setSpeed(speedKnots);
+ aircraft->setSpeed(0);
aircraft->setBank(0);
courseToDest = SGGeodesy::courseDeg(position, arr->geod());
- FGAIFlightPlan *fp = new FGAIFlightPlan(aircraft, flightPlanName, courseToDest, deptime,
+ FGAIFlightPlan *fp = new FGAIFlightPlan(aircraft, flightPlanName, courseToDest, deptime,
dep, arr, true, radius,
flight->getCruiseAlt()*100,
position.getLatitudeDeg(),
airline);
if (fp->isValidPlan()) {
aircraft->SetFlightPlan(fp);
- FGAIManager* aimgr = (FGAIManager *) globals-> get_subsystem("ai_model");
+ FGAIManager* aimgr = (FGAIManager *) globals-> get_subsystem("ai-model");
aimgr->attach(aircraft);
AIManagerRef = aircraft->getID();
return true;
if (!flights.empty()) {
return;
}
- string startingPort;
+ //string startingPort;
string userPort = fgGetString("/sim/presets/airport-id");
SG_LOG(SG_GENERAL, SG_BULK, "Scheduling Flights for : " << modelPath << " " << registration << " " << homePort);
FGScheduledFlight *flight = NULL;
do {
if (currentDestination.empty()) {
- //flight = findAvailableFlight(userPort, flightIdentifier, now, (now+1800));
+ flight = findAvailableFlight(userPort, flightIdentifier, now, (now+6400));
if (!flight)
flight = findAvailableFlight(currentDestination, flightIdentifier);
} else {
flight = findAvailableFlight(currentDestination, flightIdentifier);
}
-
if (!flight) {
break;
}
- if (startingPort.empty()) {
- startingPort = flight->getDepartureAirport()->getId();
- }
-
-
currentDestination = flight->getArrivalAirport()->getId();
//cerr << "Current destination " << currentDestination << endl;
if (!initialized) {
string departurePort = flight->getDepartureAirport()->getId();
- //cerr << "Scheduled " << registration << " " << score << " for Flight "
- // << flight-> getCallSign() << " from " << departurePort << " to " << currentDestination << endl;
if (userPort == departurePort) {
+ lastRun = 1;
hits++;
+ } else {
+ lastRun = 0;
}
//runCount++;
initialized = true;
<< " " << arrT << ":");
flights.push_back(flight);
- } while (1); //(currentDestination != startingPort);
+ } while (currentDestination != homePort);
SG_LOG(SG_GENERAL, SG_BULK, " Done ");
}
{
time_t now = time(NULL) + fgGetLong("/sim/time/warp");
- FGTrafficManager *tmgr = (FGTrafficManager *) globals->get_subsystem("Traffic Manager");
+ FGTrafficManager *tmgr = (FGTrafficManager *) globals->get_subsystem("traffic-manager");
FGScheduledFlightVecIterator fltBegin, fltEnd;
fltBegin = tmgr->getFirstFlight(req);
fltEnd = tmgr->getLastFlight(req);
}
if (flights.size()) {
time_t arrival = flights.back()->getArrivalTime();
- if ((*i)->getDepartureTime() < (arrival+(20*60)))
+ int groundTime = groundTimeFromRadius();
+ if ((*i)->getDepartureTime() < (arrival+(groundTime)))
continue;
}
if (min != 0) {
return NULL;
}
+int FGAISchedule::groundTimeFromRadius()
+{
+ if (radius < 10)
+ return 15 * 60;
+ else if (radius < 15)
+ return 20 * 60;
+ else if (radius < 20)
+ return 30 * 60;
+ else if (radius < 25)
+ return 50 * 60;
+ else if (radius < 30)
+ return 90 * 60;
+ else
+ return 120 * 60;
+}
+
+
double FGAISchedule::getSpeed()
{
FGScheduledFlightVecIterator i = flights.begin();