]> git.mxchange.org Git - flightgear.git/blob - src/AIModel/AIManager.cxx
08c215aa7ed3642cbae26194ed9ca499d042eb84
[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_itr = ai_list.begin();
41   while(ai_list_itr != ai_list.end()) {
42     delete (*ai_list_itr);
43     ++ai_list_itr;
44   }
45   ai_list.clear();
46 }
47
48 void FGAIManager::init() {
49   SGPropertyNode * node = fgGetNode("sim/ai", true);
50   for (int i = 0; i < node->nChildren(); i++) {
51     const SGPropertyNode * entry = node->getChild(i);
52     if (!strcmp(entry->getName(), "entry")) {
53       if (!strcmp(entry->getStringValue("type", ""), "aircraft")) { 
54         FGAIAircraft* ai_plane = new FGAIAircraft;
55         ai_list.push_back(ai_plane);
56         if (!strcmp(entry->getStringValue("class", ""), "light")) {
57           PERF_STRUCT ps = {2.0, 2.0, 450.0, 1000.0, 70.0, 80.0, 100.0, 80.0, 60.0};
58           ai_plane->SetPerformance(ps);
59         } else if (!strcmp(entry->getStringValue("class", ""), "ww2_fighter")) {
60           PERF_STRUCT ps = {4.0, 2.0, 3000.0, 1500.0, 110.0, 180.0, 250.0, 200.0, 100.0};
61           ai_plane->SetPerformance(ps);
62         } else if (!strcmp(entry->getStringValue("class", ""), "jet_transport")) {
63           PERF_STRUCT ps = {5.0, 2.0, 3000.0, 1500.0, 140.0, 300.0, 430.0, 300.0, 130.0};
64           ai_plane->SetPerformance(ps);
65         } else if (!strcmp(entry->getStringValue("class", ""), "jet_fighter")) {
66           PERF_STRUCT ps = {7.0, 3.0, 4000.0, 2000.0, 150.0, 350.0, 500.0, 350.0, 150.0};
67           ai_plane->SetPerformance(ps);
68         }
69         ai_plane->setHeading(entry->getDoubleValue("heading"));
70         ai_plane->setSpeed(entry->getDoubleValue("speed-KTAS"));
71         ai_plane->setPath(entry->getStringValue("path"));
72         ai_plane->setAltitude(entry->getDoubleValue("altitude-ft"));
73         ai_plane->setLongitude(entry->getDoubleValue("longitude"));
74         ai_plane->setLatitude(entry->getDoubleValue("latitude"));
75         ai_plane->init();
76
77       } else if (!strcmp(entry->getStringValue("type", ""), "ship")) {
78         FGAIShip* ai_ship = new FGAIShip;
79         ai_list.push_back(ai_ship);
80         ai_ship->setHeading(entry->getDoubleValue("heading"));
81         ai_ship->setSpeed(entry->getDoubleValue("speed-KTAS"));
82         ai_ship->setPath(entry->getStringValue("path"));
83         ai_ship->setAltitude(entry->getDoubleValue("altitude-ft"));
84         ai_ship->setLongitude(entry->getDoubleValue("longitude"));
85         ai_ship->setLatitude(entry->getDoubleValue("latitude"));
86         ai_ship->init();
87
88       } else if (!strcmp(entry->getStringValue("type", ""), "ballistic")) {
89         FGAIBallistic* ai_ballistic = new FGAIBallistic;
90         ai_list.push_back(ai_ballistic);
91         ai_ballistic->setAzimuth(entry->getDoubleValue("azimuth"));
92         ai_ballistic->setElevation(entry->getDoubleValue("elevation"));
93         ai_ballistic->setSpeed(entry->getDoubleValue("speed-fps"));
94         ai_ballistic->setPath(entry->getStringValue("path"));
95         ai_ballistic->setAltitude(entry->getDoubleValue("altitude-ft"));
96         ai_ballistic->setLongitude(entry->getDoubleValue("longitude"));
97         ai_ballistic->setLatitude(entry->getDoubleValue("latitude"));
98         ai_ballistic->init();
99       } 
100      }
101   }
102
103   initDone = true;
104 }
105
106
107 void FGAIManager::bind() {
108 }
109
110
111 void FGAIManager::unbind() {
112 }
113
114
115 void FGAIManager::update(double dt) {
116 #if 0
117         if(!initDone) {
118           init();
119           SG_LOG(SG_ATC, SG_WARN, "Warning - AIManager::update(...) called before AIManager::init()");
120         }
121 #endif
122         
123         ai_list_itr = ai_list.begin();
124         while(ai_list_itr != ai_list.end()) {
125                 if ((*ai_list_itr)->getDie()) {
126                 // FIXME: delete object itself before removing it from the list.
127                 //   delete (*ai_list_itr);
128                    ai_list.erase(ai_list_itr, ai_list_itr);
129                 } else {
130                    (*ai_list_itr)->update(dt);
131                 }
132                 ++ai_list_itr;
133         }
134 }