]> git.mxchange.org Git - flightgear.git/blob - src/ATC/AIMgr.cxx
More stuff to make the AI/ATC system less hardwired and more generic. Most of the...
[flightgear.git] / src / ATC / AIMgr.cxx
1 // AIMgr.cxx - implementation of FGAIMgr 
2 // - a global management class for FlightGear generated AI traffic
3 //
4 // Written by David Luff, started March 2002.
5 //
6 // Copyright (C) 2002  David C Luff - david.luff@nottingham.ac.uk
7 //
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.
12 //
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.
17 //
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.
21
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>
28
29 #include <list>
30
31 #ifdef _MSC_VER
32 #  include <io.h>
33 #else
34 #  include <sys/types.h>        // for directory reading
35 #  include <dirent.h>           // for directory reading
36 #endif
37
38 #include "AIMgr.hxx"
39 #include "AILocalTraffic.hxx"
40 #include "ATCutils.hxx"
41
42 SG_USING_STD(list);
43
44
45 FGAIMgr::FGAIMgr() {
46         ATC = globals->get_ATC_mgr();
47 }
48
49 FGAIMgr::~FGAIMgr() {
50 }
51
52 void FGAIMgr::init() {
53         // go through the $FG_ROOT/ATC directory and find all *.taxi files
54         SGPath path(globals->get_fg_root());
55         path.append("ATC/");
56         string dir = path.dir();
57     string ext;
58     string file, f_ident;
59         int pos;
60         
61         // WARNING - I (DCL) haven't tested this on MSVC - this is simply cribbed from TerraGear
62 #ifdef _MSC_VER 
63         long hfile;
64         struct _finddata_t de;
65         string path_str;
66         
67         path_str = dir + "\\*.*";
68         
69         if ( ( hfile = _findfirst( path.c_str(), &de ) ) == -1 ) {
70                 cout << "cannot open directory " << dir << "\n";
71         } else {                
72                 // load all .taxi files
73                 do {
74                         file = de.name;
75                         pos = file.find(".");
76                         ext = file.substr(pos + 1);
77                         if(ext == "taxi") {
78                                 cout << "TAXI FILE FOUND!!!\n";
79                                 f_ident = file.substr(0, pos);
80                                 FGAirport a;
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'; 
86                                 }
87                         }
88                 } while ( _findnext( hfile, &de ) == 0 );
89         }
90 #else
91
92     DIR *d;
93     struct dirent *de;
94
95     if ( (d = opendir( dir.c_str() )) == NULL ) {
96                 cout << "cannot open directory " << dir << "\n";
97         } else {
98                 cout << "Opened directory " << dir << " OK :-)\n";
99                 cout << "Contents are:\n";
100                 // load all .taxi files
101                 while ( (de = readdir(d)) != NULL ) {
102                         file = de->d_name;
103                         pos = file.find(".");
104                         cout << file << '\n';
105
106                         ext = file.substr(pos + 1);
107                         if(ext == "taxi") {
108                                 cout << "TAXI FILE FOUND!!!\n";
109                                 f_ident = file.substr(0, pos);
110                                 FGAirport a;
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'; 
116                                 }
117                         }
118                 }               
119                 closedir(d);
120         }
121 #endif
122         
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);
131 }
132
133 void FGAIMgr::bind() {
134 }
135
136 void FGAIMgr::unbind() {
137 }
138
139 void FGAIMgr::update(double dt) {
140         // Don't update any planes for first 50 runs through - this avoids some possible initialisation anomalies
141         static int i = 0;
142         if(i < 50) {
143                 i++;
144                 return;
145         }
146         
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);
154                 ++ai_list_itr;
155         }
156 }