1 // AIMgr.cxx - implementation of FGAIMgr
2 // - a global management class for FlightGear generated AI traffic
4 // Written by David Luff, started March 2002.
6 // Copyright (C) 2002 David C Luff - david.luff@nottingham.ac.uk
8 // This program is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU General Public License as
10 // published by the Free Software Foundation; either version 2 of the
11 // License, or (at your option) any later version.
13 // This program is distributed in the hope that it will be useful, but
14 // WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 // General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include <Airports/simple.hxx>
23 #include <Main/fgfs.hxx>
24 #include <Main/fg_props.hxx>
25 #include <Main/globals.hxx>
26 #include <Simgear/misc/sg_path.hxx>
27 #include <Simgear/bucket/newbucket.hxx>
34 # include <sys/types.h> // for directory reading
35 # include <dirent.h> // for directory reading
39 #include "AILocalTraffic.hxx"
40 #include "ATCutils.hxx"
46 ATC = globals->get_ATC_mgr();
52 void FGAIMgr::init() {
53 // go through the $FG_ROOT/ATC directory and find all *.taxi files
54 SGPath path(globals->get_fg_root());
56 string dir = path.dir();
61 // WARNING - I (DCL) haven't tested this on MSVC - this is simply cribbed from TerraGear
64 struct _finddata_t de;
67 path_str = dir + "\\*.*";
69 if ( ( hfile = _findfirst( path.c_str(), &de ) ) == -1 ) {
70 cout << "cannot open directory " << dir << "\n";
72 // load all .taxi files
76 ext = file.substr(pos + 1);
78 cout << "TAXI FILE FOUND!!!\n";
79 f_ident = file.substr(0, pos);
81 if(dclFindAirportID(f_ident, &a)) {
82 SGBucket sgb(a.longitude, a.latitude);
83 int idx = sgb.gen_index();
84 airports[idx] = f_ident;
85 cout << "Mapping " << f_ident << " to bucket " << idx << '\n';
88 } while ( _findnext( hfile, &de ) == 0 );
95 if ( (d = opendir( dir.c_str() )) == NULL ) {
96 cout << "cannot open directory " << dir << "\n";
98 cout << "Opened directory " << dir << " OK :-)\n";
99 cout << "Contents are:\n";
100 // load all .taxi files
101 while ( (de = readdir(d)) != NULL ) {
103 pos = file.find(".");
104 cout << file << '\n';
106 ext = file.substr(pos + 1);
108 cout << "TAXI FILE FOUND!!!\n";
109 f_ident = file.substr(0, pos);
111 if(dclFindAirportID(f_ident, &a)) {
112 SGBucket sgb(a.longitude, a.latitude);
113 int idx = sgb.gen_index();
114 airports[idx] = f_ident;
115 cout << "Mapping " << f_ident << " to bucket " << idx << '\n';
123 // Hard wire some local traffic for now.
124 // This is regardless of location and hence *very* ugly but it is a start.
125 ATC->AIRegisterAirport("KEMT");
126 FGAILocalTraffic* local_traffic = new FGAILocalTraffic;
127 //local_traffic->Init("KEMT", IN_PATTERN, TAKEOFF_ROLL);
128 local_traffic->Init("KEMT");
129 local_traffic->FlyCircuits(1, true); // Fly 2 circuits with touch & go in between
130 ai_list.push_back(local_traffic);
133 void FGAIMgr::bind() {
136 void FGAIMgr::unbind() {
139 void FGAIMgr::update(double dt) {
140 // Don't update any planes for first 50 runs through - this avoids some possible initialisation anomalies
147 // Traverse the list of active planes and run all their update methods
148 // TODO - spread the load - not all planes should need updating every frame.
149 // Note that this will require dt to be calculated for each plane though
150 // since they rely on it to calculate distance travelled.
151 ai_list_itr = ai_list.begin();
152 while(ai_list_itr != ai_list.end()) {
153 (*ai_list_itr)->Update(dt);