From 3c1a7174fb13a25ebbaee9b6ac6e6630316bfef3 Mon Sep 17 00:00:00 2001 From: ehofman Date: Sat, 29 May 2004 11:39:10 +0000 Subject: [PATCH] David Culp: Here's some new AI stuff. 1) AI objects must now be defined in a scenario file, not in preferences.xml or a *-set file. (Of course this doesn't prevent objects from being created dynamically, as with Durk's traffic manager). 2) A new demo_scenario file is attached. It creates 3 aircraft, a sailboat, and a thunderstorm. 3) Objects without flightplans live forever. 4) FGAIShip::ProcessFlightplan() is not yet implemented. 5) preferences.xml should now define only and --- src/AIModel/AIAircraft.cxx | 1 - src/AIModel/AIAircraft.hxx | 3 - src/AIModel/AIBase.cxx | 1 + src/AIModel/AIBase.hxx | 2 + src/AIModel/AIManager.cxx | 116 ++++++++++++++++--------------------- src/AIModel/AIManager.hxx | 5 +- src/AIModel/AIScenario.cxx | 11 ++++ src/AIModel/AIScenario.hxx | 11 ++++ src/AIModel/AIShip.cxx | 11 ++++ src/AIModel/AIShip.hxx | 2 + 10 files changed, 90 insertions(+), 73 deletions(-) diff --git a/src/AIModel/AIAircraft.cxx b/src/AIModel/AIAircraft.cxx index e18217a7c..9ec2c8be7 100644 --- a/src/AIModel/AIAircraft.cxx +++ b/src/AIModel/AIAircraft.cxx @@ -64,7 +64,6 @@ FGAIAircraft::FGAIAircraft(FGAIManager* mgr) { FGAIAircraft::~FGAIAircraft() { - if (fp) delete fp; } diff --git a/src/AIModel/AIAircraft.hxx b/src/AIModel/AIAircraft.hxx index 0b8739fda..97227ab1b 100644 --- a/src/AIModel/AIAircraft.hxx +++ b/src/AIModel/AIAircraft.hxx @@ -23,7 +23,6 @@ #include "AIManager.hxx" #include "AIBase.hxx" -#include "AIFlightPlan.hxx" #include SG_USING_STD(string); @@ -67,13 +66,11 @@ public: void ClimbTo(double altitude); void TurnTo(double heading); void ProcessFlightPlan( double dt ); - //double getHeading(double lat1, double lon1, double lat2, double lon2); private: bool hdg_lock; bool alt_lock; - FGAIFlightPlan *fp; double dt_count; double dt; diff --git a/src/AIModel/AIBase.cxx b/src/AIModel/AIBase.cxx index d1f0ca336..f27067ad6 100644 --- a/src/AIModel/AIBase.cxx +++ b/src/AIModel/AIBase.cxx @@ -61,6 +61,7 @@ FGAIBase::~FGAIBase() { // unbind(); SGPropertyNode *root = globals->get_props()->getNode("ai/models", true); root->removeChild(_type_str.c_str(), index); + if (fp) delete fp; } void FGAIBase::update(double dt) { diff --git a/src/AIModel/AIBase.hxx b/src/AIModel/AIBase.hxx index 990760433..b9db98c6c 100644 --- a/src/AIModel/AIBase.hxx +++ b/src/AIModel/AIBase.hxx @@ -27,6 +27,7 @@ #include #include
+#include "AIFlightPlan.hxx" SG_USING_STD(string); @@ -103,6 +104,7 @@ protected: int id; bool invisible; bool no_roll; + FGAIFlightPlan *fp; void Transform(); diff --git a/src/AIModel/AIManager.cxx b/src/AIModel/AIManager.cxx index b53ac439b..1aed8f732 100644 --- a/src/AIModel/AIManager.cxx +++ b/src/AIModel/AIManager.cxx @@ -54,75 +54,15 @@ FGAIManager::~FGAIManager() { void FGAIManager::init() { - int rval; root = fgGetNode("sim/ai", true); enabled = root->getNode("enabled", true)->getBoolValue(); if (!enabled) return; - wind_from_down = fgGetNode("/environment/wind-from-down-fps", true); - - for (int i = 0; i < root->nChildren(); i++) { - const SGPropertyNode * entry = root->getChild(i); - - if (!strcmp(entry->getName(), "scenario")){ - scenario_filename = entry->getStringValue(); - } - - if (!strcmp(entry->getName(), "entry")) { - if (!strcmp(entry->getStringValue("type", ""), "aircraft")) { - - rval = createAircraft( entry->getStringValue("class", ""), - entry->getStringValue("path"), - entry->getDoubleValue("latitude"), - entry->getDoubleValue("longitude"), - entry->getDoubleValue("altitude-ft"), - entry->getDoubleValue("heading"), - entry->getDoubleValue("speed-KTAS"), - 0.0, - entry->getDoubleValue("bank") ); - - } else if (!strcmp(entry->getStringValue("type", ""), "ship")) { - - rval = createShip( entry->getStringValue("path"), - entry->getDoubleValue("latitude"), - entry->getDoubleValue("longitude"), - entry->getDoubleValue("altitude-ft"), - entry->getDoubleValue("heading"), - entry->getDoubleValue("speed-KTAS"), - entry->getDoubleValue("rudder") ); - - } else if (!strcmp(entry->getStringValue("type", ""), "ballistic")) { - - rval = createBallistic( entry->getStringValue("path"), - entry->getDoubleValue("latitude"), - entry->getDoubleValue("longitude"), - entry->getDoubleValue("altitude-ft"), - entry->getDoubleValue("azimuth"), - entry->getDoubleValue("elevation"), - entry->getDoubleValue("speed") ); - - } else if (!strcmp(entry->getStringValue("type", ""), "storm")) { - - rval = createStorm( entry->getStringValue("path"), - entry->getDoubleValue("latitude"), - entry->getDoubleValue("longitude"), - entry->getDoubleValue("altitude-ft"), - entry->getDoubleValue("heading"), - entry->getDoubleValue("speed-KTAS") ); - - } else if (!strcmp(entry->getStringValue("type", ""), "thermal")) { - - rval = createThermal( entry->getDoubleValue("latitude"), - entry->getDoubleValue("longitude"), - entry->getDoubleValue("strength-fps"), - entry->getDoubleValue("diameter-ft") ); - - } - } - } + + scenario_filename = root->getNode("scenario", true)->getStringValue(); if (scenario_filename != "") processScenario( scenario_filename ); initDone = true; @@ -214,7 +154,7 @@ void FGAIManager::freeID( int ID ) { int FGAIManager::createAircraft( string model_class, string path, double latitude, double longitude, double altitude, - double heading, double speed, double pitch, double roll ) { + double heading, double speed, double roll ) { FGAIAircraft* ai_plane = new FGAIAircraft(this); ai_list.push_back(ai_plane); @@ -290,6 +230,19 @@ int FGAIManager::createShip( string path, double latitude, double longitude, return ai_ship->getID(); } +int FGAIManager::createShip( string path, FGAIFlightPlan* flightplan ) { + + FGAIShip* ai_ship = new FGAIShip(this); + ai_list.push_back(ai_ship); + ai_ship->setID( assignID() ); + ++numObjects; + ai_ship->setPath(path.c_str()); + ai_ship->setFlightPlan(flightplan); + ai_ship->init(); + ai_ship->bind(); + return ai_ship->getID(); +} + int FGAIManager::createBallistic( string path, double latitude, double longitude, double altitude, double azimuth, double elevation, @@ -387,17 +340,46 @@ void FGAIManager::processThermal( FGAIThermal* thermal ) { void FGAIManager::processScenario( string filename ) { - //cout << "AIManager: creating a scenario." << endl; FGAIScenario* s = new FGAIScenario( filename ); + FGAIFlightPlan* f; + for (int i=0;inEntries();i++) { FGAIScenario::entry* en = s->getNextEntry(); + f = 0; if (en) { - FGAIFlightPlan* f = new FGAIFlightPlan( en->flightplan ); + if (en->flightplan != ""){ + f = new FGAIFlightPlan( en->flightplan ); + } if (en->aitype == "aircraft"){ - createAircraft( en->aircraft_class, en->model_path, f); - } + if (f){ + createAircraft( en->aircraft_class, en->model_path, f ); + } else { + createAircraft( en->aircraft_class, en->model_path, en->latitude, + en->longitude, en->altitude, en->heading, + en->speed, en->roll ); + } + } else if (en->aitype == "ship"){ + if (f){ + createShip( en->model_path, f ); + } else { + createShip( en->model_path, en->latitude, + en->longitude, en->altitude, en->heading, + en->speed, en->rudder ); + } + + } else if (en->aitype == "storm"){ + createStorm( en->model_path, en->latitude, en->longitude, + en->altitude, en->heading, en->speed ); + } else if (en->aitype == "thermal"){ + createThermal( en->latitude, en->longitude, en->strength, + en->diameter ); + } else if (en->aitype == "ballistic"){ + createBallistic( en->model_path, en->latitude, en->longitude, + en->altitude, en->azimuth, en->elevation, en->speed ); + } } } + delete s; } diff --git a/src/AIModel/AIManager.hxx b/src/AIModel/AIManager.hxx index 2e154fd6a..1766c8a8c 100644 --- a/src/AIModel/AIManager.hxx +++ b/src/AIModel/AIManager.hxx @@ -74,7 +74,6 @@ public: double altitude, // in feet double heading, // true heading in degrees double speed, // in knots true airspeed (KTAS) - double pitch = 0, // in degrees double roll = 0 ); // in degrees int createAircraft( string model_class, // see FGAIAircraft.hxx for possible classes @@ -87,8 +86,10 @@ public: double altitude, // in feet (ex. for a lake!) double heading, // true heading in degrees double speed, // in knots true - double rudder ); // in degrees (between 0 and 5 works best) + double rudder ); // in degrees (right is positive)(0 to 5 works best) + int createShip( string path, // path to exterior model + FGAIFlightPlan *flightplan ); int createBallistic( string path, // path to exterior model double latitude, // in degrees -90 to 90 diff --git a/src/AIModel/AIScenario.cxx b/src/AIModel/AIScenario.cxx index 07013c9af..dcd87360f 100644 --- a/src/AIModel/AIScenario.cxx +++ b/src/AIModel/AIScenario.cxx @@ -58,6 +58,17 @@ FGAIScenario::FGAIScenario(string filename) en->model_path = entry_node->getStringValue("model", "Models/Geometry/glider.ac"); en->flightplan = entry_node->getStringValue("flightplan", ""); en->repeat = entry_node->getDoubleValue("repeat", 0.0); + en->latitude = entry_node->getDoubleValue("latitude", 0.0); + en->longitude = entry_node->getDoubleValue("longitude", 0.0); + en->altitude = entry_node->getDoubleValue("altitude", 0.0); + en->speed = entry_node->getDoubleValue("speed", 0.0); + en->heading = entry_node->getDoubleValue("heading", 0.0); + en->roll = entry_node->getDoubleValue("roll", 0.0); + en->azimuth = entry_node->getDoubleValue("azimuth", 0.0); + en->elevation = entry_node->getDoubleValue("elevation", 0.0); + en->rudder = entry_node->getDoubleValue("rudder", 0.0); + en->strength = entry_node->getDoubleValue("strength", 0.0); + en->diameter = entry_node->getDoubleValue("diameter", 0.0); } entry_iterator = entries.begin(); diff --git a/src/AIModel/AIScenario.hxx b/src/AIModel/AIScenario.hxx index 949c72768..39a8644cb 100644 --- a/src/AIModel/AIScenario.hxx +++ b/src/AIModel/AIScenario.hxx @@ -37,6 +37,17 @@ public: string model_path; string flightplan; double repeat; // in seconds + double latitude; // used if no flightplan defined + double longitude; // used if no flightplan defined + double altitude; // used if no flightplan defined + double speed; // used if no flightplan defined + double heading; // used if no flightplan defined + double roll; // used if no flightplan defined + double azimuth; // used by ballistic objects + double elevation; // used by ballistic objects + double rudder; // used by ship objects + double strength; // used by thermal objects + double diameter; // used by thermal objects } entry; FGAIScenario(string filename); diff --git a/src/AIModel/AIShip.cxx b/src/AIModel/AIShip.cxx index c1d4ce809..890ab0b49 100644 --- a/src/AIModel/AIShip.cxx +++ b/src/AIModel/AIShip.cxx @@ -66,6 +66,8 @@ void FGAIShip::update(double dt) { void FGAIShip::Run(double dt) { + if (fp) ProcessFlightPlan(dt); + double turn_radius_ft; double turn_circum_ft; double speed_north_deg_sec; @@ -167,3 +169,12 @@ double FGAIShip::sign(double x) { if ( x < 0.0 ) { return -1.0; } else { return 1.0; } } + +void FGAIShip::setFlightPlan(FGAIFlightPlan* f) { + fp = f; +} + +void FGAIShip::ProcessFlightPlan(double dt) { + // not implemented yet +} + diff --git a/src/AIModel/AIShip.hxx b/src/AIModel/AIShip.hxx index 1fc583ca0..25d8b4495 100644 --- a/src/AIModel/AIShip.hxx +++ b/src/AIModel/AIShip.hxx @@ -35,6 +35,8 @@ public: virtual void bind(); virtual void unbind(); void update(double dt); + void setFlightPlan(FGAIFlightPlan* f); + void ProcessFlightPlan( double dt ); void AccelTo(double speed); void PitchTo(double angle); -- 2.39.5