1 // AIManager.cxx Based on David Luff's AIMgr:
2 // - a global management type for AI objects
4 // Written by David Culp, started October 2003.
5 // - davidculp2@comcast.net
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 // General Public License for more details.
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 #include <Main/globals.hxx>
23 #include <Airports/simple.hxx>
24 #include <Traffic/TrafficMgr.hxx>
26 #include "AIManager.hxx"
27 #include "AIAircraft.hxx"
29 #include "AIBallistic.hxx"
30 #include "AIStorm.hxx"
31 #include "AIThermal.hxx"
32 #include "AICarrier.hxx"
33 #include "AIStatic.hxx"
34 #include "AIMultiplayer.hxx"
36 FGAIManager::FGAIManager() {
39 for (unsigned i = 0; i < FGAIBase::MAX_OBJECTS; ++i)
40 mNumAiTypeModels[i] = 0;
43 FGAIManager::~FGAIManager() {
44 ai_list_iterator ai_list_itr = ai_list.begin();
45 while(ai_list_itr != ai_list.end()) {
46 (*ai_list_itr)->unbind();
52 void FGAIManager::init() {
53 root = fgGetNode("sim/ai", true);
55 enabled = root->getNode("enabled", true)->getBoolValue();
59 wind_from_down_node = fgGetNode("/environment/wind-from-down-fps", true);
60 wind_from_east_node = fgGetNode("/environment/wind-from-east-fps",true);
61 wind_from_north_node = fgGetNode("/environment/wind-from-north-fps",true);
63 user_latitude_node = fgGetNode("/position/latitude-deg", true);
64 user_longitude_node = fgGetNode("/position/longitude-deg", true);
65 user_altitude_node = fgGetNode("/position/altitude-ft", true);
66 user_heading_node = fgGetNode("/orientation/heading-deg", true);
67 user_pitch_node = fgGetNode("/orientation/pitch-deg", true);
68 user_yaw_node = fgGetNode("/orientation/side-slip-deg", true);
69 user_speed_node = fgGetNode("/velocities/uBody-fps", true);
72 /// Move that into the constructor
73 for(int i = 0 ; i < root->nChildren() ; i++) {
74 SGPropertyNode *aiEntry = root->getChild( i );
75 if( !strcmp( aiEntry->getName(), "scenario" ) ) {
76 scenario_filename = aiEntry->getStringValue();
77 if (!scenario_filename.empty())
78 processScenario( scenario_filename );
84 void FGAIManager::reinit() {
89 void FGAIManager::bind() {
90 root = globals->get_props()->getNode("ai/models", true);
91 root->tie("count", SGRawValueMethods<FGAIManager, int>(*this,
92 &FGAIManager::getNumAiObjects));
96 void FGAIManager::unbind() {
101 void FGAIManager::update(double dt) {
102 // initialize these for finding nearest thermals
103 range_nearest = 10000.0;
108 FGTrafficManager *tmgr = (FGTrafficManager*) globals->get_subsystem("Traffic Manager");
111 ai_list_iterator ai_list_itr = ai_list.begin();
112 while(ai_list_itr != ai_list.end()) {
113 if ((*ai_list_itr)->getDie()) {
114 tmgr->release((*ai_list_itr)->getID());
116 --(mNumAiTypeModels[(*ai_list_itr)->getType()]);
117 (*ai_list_itr)->unbind();
118 ai_list_itr = ai_list.erase(ai_list_itr);
121 if ((*ai_list_itr)->isa(FGAIBase::otThermal)) {
122 FGAIBase *base = *ai_list_itr;
123 processThermal((FGAIThermal*)base);
125 (*ai_list_itr)->update(_dt);
130 wind_from_down_node->setDoubleValue( strength ); // for thermals
134 FGAIManager::attach(SGSharedPtr<FGAIBase> model)
136 unsigned idx = mNumAiTypeModels[model->getType()];
137 const char* typeString = model->getTypeString();
138 SGPropertyNode* root = globals->get_props()->getNode("ai/models", true);
139 SGPropertyNode* p = root->getNode(typeString, idx, true);
140 model->setManager(this, p);
141 ai_list.push_back(model);
143 ++(mNumAiTypeModels[model->getType()]);
149 void FGAIManager::destroyObject( int ID ) {
150 ai_list_iterator ai_list_itr = ai_list.begin();
151 while(ai_list_itr != ai_list.end()) {
152 if ((*ai_list_itr)->getID() == ID) {
154 --(mNumAiTypeModels[(*ai_list_itr)->getType()]);
155 (*ai_list_itr)->unbind();
156 ai_list_itr = ai_list.erase(ai_list_itr);
163 FGAIManager::getNumAiObjects(void) const
168 void FGAIManager::fetchUserState( void ) {
169 user_latitude = user_latitude_node->getDoubleValue();
170 user_longitude = user_longitude_node->getDoubleValue();
171 user_altitude = user_altitude_node->getDoubleValue();
172 user_heading = user_heading_node->getDoubleValue();
173 user_pitch = user_pitch_node->getDoubleValue();
174 user_yaw = user_yaw_node->getDoubleValue();
175 user_speed = user_speed_node->getDoubleValue() * 0.592484;
176 wind_from_east = wind_from_east_node->getDoubleValue();
177 wind_from_north = wind_from_north_node->getDoubleValue();
181 // only keep the results from the nearest thermal
182 void FGAIManager::processThermal( FGAIThermal* thermal ) {
183 thermal->update(_dt);
184 if ( thermal->_getRange() < range_nearest ) {
185 range_nearest = thermal->_getRange();
186 strength = thermal->getStrength();
191 void FGAIManager::processScenario( const string &filename ) {
193 SGPropertyNode_ptr scenarioTop = loadScenarioFile(filename);
196 SGPropertyNode* scenarios = scenarioTop->getChild("scenario");
200 for (int i = 0; i < scenarios->nChildren(); i++) {
201 SGPropertyNode* scEntry = scenarios->getChild(i);
202 std::string type = scEntry->getStringValue("type", "aircraft");
204 if (type == "aircraft") {
205 FGAIAircraft* aircraft = new FGAIAircraft;
206 aircraft->readFromScenario(scEntry);
209 } else if (type == "ship") {
210 FGAIShip* ship = new FGAIShip;
211 ship->readFromScenario(scEntry);
214 } else if (type == "carrier") {
215 FGAICarrier* carrier = new FGAICarrier;
216 carrier->readFromScenario(scEntry);
219 } else if (type == "thunderstorm") {
220 FGAIStorm* storm = new FGAIStorm;
221 storm->readFromScenario(scEntry);
224 } else if (type == "thermal") {
225 FGAIThermal* thermal = new FGAIThermal;
226 thermal->readFromScenario(scEntry);
229 } else if (type == "ballistic") {
230 FGAIBallistic* ballistic = new FGAIBallistic;
231 ballistic->readFromScenario(scEntry);
234 } else if (type == "static") {
235 FGAIStatic* aistatic = new FGAIStatic;
236 aistatic->readFromScenario(scEntry);
244 FGAIManager::loadScenarioFile(const std::string& filename)
246 SGPath path(globals->get_fg_root());
247 path.append("AI/" + filename + ".xml");
249 SGPropertyNode_ptr root = new SGPropertyNode;
250 readProperties(path.str(), root);
252 } catch (const sg_exception &e) {
253 SG_LOG(SG_GENERAL, SG_ALERT, "Incorrect path specified for AI "
254 "scenario: \"" << path.str() << "\"");
259 // This code keeps track of models that have already been loaded
260 // Eventually we'd prbably need to find a way to keep track of models
261 // that are unloaded again
262 ssgBranch * FGAIManager::getModel(const string& path) const
264 ModelVecIterator i = loadedModels.begin();
265 while (i != loadedModels.end())
267 if (i->getPath() == path)
268 return i->getModelId();
274 void FGAIManager::setModel(const string& path, ssgBranch *model)
276 loadedModels.push_back(FGModelID(path,model));
279 bool FGAIManager::getStartPosition(const string& id, const string& pid,
280 SGGeod& geodPos, double& hdng, SGVec3d& uvw)
283 SGPropertyNode* root = fgGetNode("sim/ai", true);
284 if (!root->getNode("enabled", true)->getBoolValue())
287 for(int i = 0 ; (!found) && i < root->nChildren() ; i++) {
288 SGPropertyNode *aiEntry = root->getChild( i );
289 if( !strcmp( aiEntry->getName(), "scenario" ) ) {
290 string filename = aiEntry->getStringValue();
291 SGPropertyNode_ptr scenarioTop = loadScenarioFile(filename);
293 SGPropertyNode* scenarios = scenarioTop->getChild("scenario");
295 for (int i = 0; i < scenarios->nChildren(); i++) {
296 SGPropertyNode* scEntry = scenarios->getChild(i);
297 std::string type = scEntry->getStringValue("type");
298 std::string pnumber = scEntry->getStringValue("pennant-number");
299 std::string name = scEntry->getStringValue("name");
300 if (type == "carrier" && (pnumber == id || name == id)) {
301 SGSharedPtr<FGAICarrier> carrier = new FGAICarrier;
302 carrier->readFromScenario(scEntry);
304 if (carrier->getParkPosition(pid, geodPos, hdng, uvw)) {