From 540d4c2111fa0d1d6d846e6c98e3968dc36b16b5 Mon Sep 17 00:00:00 2001 From: James Turner Date: Tue, 5 Jan 2016 22:00:12 -0600 Subject: [PATCH] Use SGTime instead of time() in traffic/AI code - avoids manual handling of /sim/time/warp in many places --- src/AIModel/AIAircraft.cxx | 2 +- src/AIModel/AIFlightPlan.cxx | 3 ++- src/AIModel/AIFlightPlanCreate.cxx | 4 +++- src/ATC/GroundController.cxx | 12 ++++++++---- src/ATC/trafficcontrol.cxx | 14 ++++++++++---- src/Traffic/Schedule.cxx | 3 ++- src/Traffic/TrafficMgr.cxx | 7 +++++-- 7 files changed, 31 insertions(+), 14 deletions(-) diff --git a/src/AIModel/AIAircraft.cxx b/src/AIModel/AIAircraft.cxx index 836550afb..3cb90a51c 100644 --- a/src/AIModel/AIAircraft.cxx +++ b/src/AIModel/AIAircraft.cxx @@ -1397,7 +1397,7 @@ time_t FGAIAircraft::checkForArrivalTime(const string& wptName) { } else { return 0; } - time_t now = time(NULL) + fgGetLong("/sim/time/warp"); + time_t now = globals->get_time_params()->get_cur_time(); time_t arrivalTime = fp->getArrivalTime(); time_t ete = tracklength / ((speed * SG_NM_TO_METER) / 3600.0); diff --git a/src/AIModel/AIFlightPlan.cxx b/src/AIModel/AIFlightPlan.cxx index dbfd06ee9..d99172274 100644 --- a/src/AIModel/AIFlightPlan.cxx +++ b/src/AIModel/AIFlightPlan.cxx @@ -29,6 +29,7 @@ #include #include #include +#include #include
#include
@@ -192,7 +193,7 @@ void FGAIFlightPlan::createWaypoints(FGAIAircraft *ac, const string& acType, const string& airline) { - time_t now = time(NULL) + fgGetLong("/sim/time/warp"); + time_t now = globals->get_time_params()->get_cur_time(); time_t timeDiff = now-start; leg = 1; diff --git a/src/AIModel/AIFlightPlanCreate.cxx b/src/AIModel/AIFlightPlanCreate.cxx index ae369d57b..b7a24ab1a 100644 --- a/src/AIModel/AIFlightPlanCreate.cxx +++ b/src/AIModel/AIFlightPlanCreate.cxx @@ -29,6 +29,7 @@ #include #include #include +#include #include #include @@ -694,7 +695,8 @@ bool FGAIFlightPlan::createDescent(FGAIAircraft * ac, FGAirport * apt, double turnDistance = (2 * M_PI * initialTurnRadius) * (side / 360.0); time_t remaining = (turnDistance + distance) / ((vDescent * SG_NM_TO_METER) / 3600.0); - time_t now = time(NULL) + fgGetLong("/sim/time/warp"); + time_t now = globals->get_time_params()->get_cur_time(); + //if (ac->getTrafficRef()->getCallSign() == fgGetString("/ai/track-callsign")) { // cerr << " Arrival time estimation: turn angle " << side << ". Turn distance " << turnDistance << ". Linear distance " << distance << ". Time to go " << remaining << endl; // //exit(1); diff --git a/src/ATC/GroundController.cxx b/src/ATC/GroundController.cxx index b3c11741d..d4b71f3db 100644 --- a/src/ATC/GroundController.cxx +++ b/src/ATC/GroundController.cxx @@ -42,6 +42,7 @@ #include #include #include +#include #include #include @@ -263,7 +264,8 @@ void FGGroundController::updateAircraftInformation(int id, double lat, double lo } else { current->setHoldPosition(true); int state = current->getState(); - time_t now = time(NULL) + fgGetLong("/sim/time/warp"); + time_t now = globals->get_time_params()->get_cur_time(); + if ((now - lastTransmission) > 15) { available = true; } @@ -467,7 +469,8 @@ void FGGroundController::checkHoldPosition(int id, double lat, } else { return; } - time_t now = time(NULL) + fgGetLong("/sim/time/warp"); + + time_t now = globals->get_time_params()->get_cur_time(); if (i == activeTraffic.end() || (activeTraffic.size() == 0)) { SG_LOG(SG_GENERAL, SG_ALERT, "AI error: Trying to access non-existing aircraft in FGGroundNetwork::checkHoldPosition at " << SG_ORIGIN); @@ -778,7 +781,8 @@ void FGGroundController::render(bool visible) FGScenery * local_scenery = globals->get_scenery(); // double elevation_meters = 0.0; // double elevation_feet = 0.0; - time_t now = time(NULL) + fgGetLong("/sim/time/warp"); + time_t now = globals->get_time_params()->get_cur_time(); + //for ( FGTaxiSegmentVectorIterator i = segments.begin(); i != segments.end(); i++) { //double dx = 0; @@ -951,7 +955,7 @@ string FGGroundController::getName() { void FGGroundController::update(double dt) { - time_t now = time(NULL) + fgGetLong("/sim/time/warp"); + time_t now = globals->get_time_params()->get_cur_time(); FGGroundNetwork* network = dynamics->getGroundNetwork(); network->unblockAllSegments(now); int priority = 1; diff --git a/src/ATC/trafficcontrol.cxx b/src/ATC/trafficcontrol.cxx index d2fc499e9..0da90a6a8 100644 --- a/src/ATC/trafficcontrol.cxx +++ b/src/ATC/trafficcontrol.cxx @@ -36,6 +36,8 @@ #include #include #include +#include + #include #include "trafficcontrol.hxx" @@ -132,7 +134,9 @@ time_t ActiveRunway::requestTimeSlot(time_t eta) estimatedArrivalTimes.push_back(newEta); sort(estimatedArrivalTimes.begin(), estimatedArrivalTimes.end()); // do some housekeeping : remove any timestamps that are past - time_t now = time(NULL) + fgGetLong("/sim/time/warp"); + + time_t now = globals->get_time_params()->get_cur_time(); + TimeVectorIterator i = estimatedArrivalTimes.begin(); while (i != estimatedArrivalTimes.end()) { if ((*i) < now) { @@ -431,7 +435,7 @@ bool FGTrafficRecord::isOpposing(FGGroundNetwork * net, bool FGTrafficRecord::isActive(int margin) const { - time_t now = time(NULL) + fgGetLong("/sim/time/warp"); + time_t now = globals->get_time_params()->get_cur_time(); time_t deptime = aircraft->getTrafficRef()->getDepartureTime(); return ((now + margin) > deptime); } @@ -1269,7 +1273,8 @@ void FGStartupController::updateAircraftInformation(int id, double lat, double l // The user controlled aircraft should have crased here, because it doesn't have a traffic reference. // NOTE: if we create a traffic schedule for the user aircraft, we can use this to plan a flight. time_t startTime = i->getAircraft()->getTrafficRef()->getDepartureTime(); - time_t now = time(NULL) + fgGetLong("/sim/time/warp"); + time_t now = globals->get_time_params()->get_cur_time(); + //cerr << i->getAircraft()->getTrafficRef()->getCallSign() // << " is scheduled to depart in " << startTime-now << " seconds. Available = " << available // << " at parking " << getGateName(i->getAircraft()) << endl; @@ -1352,7 +1357,8 @@ void FGStartupController::render(bool visible) //for ( FGTaxiSegmentVectorIterator i = segments.begin(); i != segments.end(); i++) { double dx = 0; - time_t now = time(NULL) + fgGetLong("/sim/time/warp"); + time_t now = globals->get_time_params()->get_cur_time(); + for (TrafficVectorIterator i = activeTraffic.begin(); i != activeTraffic.end(); i++) { if (i->isActive(300)) { // Handle start point diff --git a/src/Traffic/Schedule.cxx b/src/Traffic/Schedule.cxx index 01878ea26..09efa2dec 100644 --- a/src/Traffic/Schedule.cxx +++ b/src/Traffic/Schedule.cxx @@ -44,6 +44,7 @@ #include #include #include +#include #include #include @@ -557,7 +558,7 @@ FGScheduledFlight* FGAISchedule::findAvailableFlight (const string ¤tDesti const string &req, time_t min, time_t max) { - time_t now = time(NULL) + fgGetLong("/sim/time/warp"); + time_t now = globals->get_time_params()->get_cur_time(); FGTrafficManager *tmgr = (FGTrafficManager *) globals->get_subsystem("traffic-manager"); FGScheduledFlightVecIterator fltBegin, fltEnd; diff --git a/src/Traffic/TrafficMgr.cxx b/src/Traffic/TrafficMgr.cxx index 62a2de4e3..eeda08ac6 100644 --- a/src/Traffic/TrafficMgr.cxx +++ b/src/Traffic/TrafficMgr.cxx @@ -58,6 +58,7 @@ #include #include #include +#include #include #include @@ -719,17 +720,19 @@ void FGTrafficManager::update(double dt) finishInit(); } - time_t now = time(NULL) + fgGetLong("/sim/time/warp"); + if (scheduledAircraft.empty()) { return; } SGVec3d userCart = globals->get_aircraft_position_cart(); - + if (currAircraft == scheduledAircraft.end()) { currAircraft = scheduledAircraft.begin(); } + time_t now = globals->get_time_params()->get_cur_time(); + //cerr << "Processing << " << (*currAircraft)->getRegistration() << " with score " << (*currAircraft)->getScore() << endl; if ((*currAircraft)->update(now, userCart)) { // schedule is done - process another aircraft in next iteration -- 2.39.5