]> git.mxchange.org Git - flightgear.git/blob - src/AIModel/AIManager.cxx
52a1c0fd18c58f84a7fc9c6d783f91fb2c466e6d
[flightgear.git] / src / AIModel / AIManager.cxx
1 // AIManager.cxx  Based on David Luff's AIMgr:
2 // - a global management class for AI objects
3 //
4 // Written by David Culp, started October 2003.
5 // - davidculp2@comcast.net
6 //
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.
11 //
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.
16 //
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., 675 Mass Ave, Cambridge, MA 02139, USA.
20
21 #include <simgear/misc/sg_path.hxx>
22 #include <Main/fg_props.hxx>
23 #include <Main/globals.hxx>
24
25 #include <list>
26
27 #include "AIManager.hxx"
28 #include "AIAircraft.hxx"
29 #include "AIShip.hxx"
30 #include "AIBallistic.hxx"
31
32 SG_USING_STD(list);
33
34
35 FGAIManager::FGAIManager() {
36   initDone = false;
37 }
38
39 FGAIManager::~FGAIManager() {
40   ai_list.clear();
41 }
42
43 void FGAIManager::init() {
44   SGPropertyNode * node = fgGetNode("sim/ai", true);
45
46   for (int i = 0; i < node->nChildren(); i++) {
47     const SGPropertyNode * entry = node->getChild(i);
48
49     if (!strcmp(entry->getName(), "entry")) {
50       if (!strcmp(entry->getStringValue("type", ""), "aircraft")) { 
51         FGAIAircraft* ai_plane = new FGAIAircraft;
52         ai_list.push_back(ai_plane);
53
54         string model_class = entry->getStringValue("class", "");
55         if (model_class == "light") {
56           ai_plane->SetPerformance(&FGAIAircraft::settings[FGAIAircraft::LIGHT]);
57
58         } else if (model_class == "ww2_fighter") {
59           ai_plane->SetPerformance(&FGAIAircraft::settings[FGAIAircraft::WW2_FIGHTER]);
60
61         } else if (model_class ==  "jet_transport") {
62           ai_plane->SetPerformance(&FGAIAircraft::settings[FGAIAircraft::JET_TRANSPORT]);
63
64         } else if (model_class == "jet_fighter") {
65           ai_plane->SetPerformance(&FGAIAircraft::settings[FGAIAircraft::JET_FIGHTER]);
66         }
67
68         ai_plane->setHeading(entry->getDoubleValue("heading"));
69         ai_plane->setSpeed(entry->getDoubleValue("speed-KTAS"));
70         ai_plane->setPath(entry->getStringValue("path"));
71         ai_plane->setAltitude(entry->getDoubleValue("altitude-ft"));
72         ai_plane->setLongitude(entry->getDoubleValue("longitude"));
73         ai_plane->setLatitude(entry->getDoubleValue("latitude"));
74         ai_plane->setBank(entry->getDoubleValue("bank"));
75         ai_plane->init();
76         ai_plane->bind();
77
78       } else if (!strcmp(entry->getStringValue("type", ""), "ship")) {
79         FGAIShip* ai_ship = new FGAIShip;
80         ai_list.push_back(ai_ship);
81         ai_ship->setHeading(entry->getDoubleValue("heading"));
82         ai_ship->setSpeed(entry->getDoubleValue("speed-KTAS"));
83         ai_ship->setPath(entry->getStringValue("path"));
84         ai_ship->setAltitude(entry->getDoubleValue("altitude-ft"));
85         ai_ship->setLongitude(entry->getDoubleValue("longitude"));
86         ai_ship->setLatitude(entry->getDoubleValue("latitude"));
87         ai_ship->setBank(entry->getDoubleValue("rudder"));
88         ai_ship->init();
89         ai_ship->bind();
90
91       } else if (!strcmp(entry->getStringValue("type", ""), "ballistic")) {
92         FGAIBallistic* ai_ballistic = new FGAIBallistic;
93         ai_list.push_back(ai_ballistic);
94         ai_ballistic->setAzimuth(entry->getDoubleValue("azimuth"));
95         ai_ballistic->setElevation(entry->getDoubleValue("elevation"));
96         ai_ballistic->setSpeed(entry->getDoubleValue("speed-fps"));
97         ai_ballistic->setPath(entry->getStringValue("path"));
98         ai_ballistic->setAltitude(entry->getDoubleValue("altitude-ft"));
99         ai_ballistic->setLongitude(entry->getDoubleValue("longitude"));
100         ai_ballistic->setLatitude(entry->getDoubleValue("latitude"));
101         ai_ballistic->init();
102         ai_ballistic->bind();
103       } 
104     }
105   }
106
107   initDone = true;
108 }
109
110
111 void FGAIManager::bind() {
112 }
113
114
115 void FGAIManager::unbind() {
116     ai_list_itr = ai_list.begin();
117     while(ai_list_itr != ai_list.end()) {
118         (*ai_list_itr)->unbind();
119         ++ai_list_itr;
120     }
121 }
122
123
124 void FGAIManager::update(double dt) {
125 #if 0
126         if(!initDone) {
127           init();
128           SG_LOG(SG_ATC, SG_WARN, "Warning - AIManager::update(...) called before AIManager::init()");
129         }
130 #endif
131         
132         ai_list_itr = ai_list.begin();
133         while(ai_list_itr != ai_list.end()) {
134                 if ((*ai_list_itr)->getDie()) {
135                    ai_list.erase(ai_list_itr, ai_list_itr);
136                 } else {
137                    (*ai_list_itr)->update(dt);
138                 }
139                 ++ai_list_itr;
140         }
141 }