]> git.mxchange.org Git - flightgear.git/blob - src/Traffic/TrafficMgr.cxx
9e6dded35b48ab3e2954a262b96507f373207f6a
[flightgear.git] / src / Traffic / TrafficMgr.cxx
1 /******************************************************************************
2  * TrafficMGr.cxx
3  * Written by Durk Talsma, started May 5, 2004.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License as
7  * published by the Free Software Foundation; either version 2 of the
8  * License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  *
19  *
20  **************************************************************************/
21  
22 /* This a prototype version of a top-level flight plan manager for Flightgear.
23  * It parses the fgtraffic.txt file and determine for a specific time/date, 
24  * where each aircraft listed in this file is at the current time. 
25  * 
26  * I'm currently assuming the following simplifications:
27  * 1) The earth is a perfect sphere
28  * 2) Each aircraft flies a perfect great circle route.
29  * 3) Each aircraft flies at a constant speed (with infinite accelerations and
30  *    decelerations) 
31  * 4) Each aircraft leaves at exactly the departure time. 
32  * 5) Each aircraft arrives at exactly the specified arrival time. 
33  *
34  *
35  *****************************************************************************/
36 #include <stdlib.h>
37 #include <time.h>
38 #include <iostream>
39 #include <fstream>
40
41
42 #include <string>
43 #include <vector>
44
45 #include <plib/sg.h>
46
47 #include <simgear/compiler.h>
48 #include <simgear/math/polar3d.hxx>
49 #include <simgear/math/sg_geodesy.hxx>
50 #include <simgear/misc/sg_path.hxx>
51 #include <simgear/props/props.hxx>
52 #include <simgear/route/waypoint.hxx>
53 #include <simgear/structure/subsystem_mgr.hxx>
54 #include <simgear/xml/easyxml.hxx>
55
56 #include <AIModel/AIFlightPlan.hxx>
57 #include <AIModel/AIBase.hxx>
58 #include <Airports/simple.hxx>
59 #include <Main/fg_init.hxx>   // That's pretty ugly, but I need fgFindAirportID
60
61
62
63 #include "TrafficMgr.hxx"
64
65  
66 /******************************************************************************
67  * TrafficManager
68  *****************************************************************************/
69 FGTrafficManager::FGTrafficManager()
70 {
71 }
72
73
74 void FGTrafficManager::init()
75 {
76   currAircraft = scheduledAircraft.begin();
77 }
78
79 void FGTrafficManager::update(double something)
80 {
81
82   //static const SGPropertyNode *warp = globals->get_props()->getNode("/sim/time/warp");
83   
84   //time_t now = time(NULL) + globals->get_warp();
85   time_t now = time(NULL) + fgGetLong("/sim/time/warp");
86   //  cerr << "TrafficManager update" << globals->get_warp() << endl;
87     if(currAircraft == scheduledAircraft.end())
88       currAircraft = scheduledAircraft.begin();
89     currAircraft->update(now);
90     currAircraft++;
91 }
92
93 void FGTrafficManager::release(void *id)
94 {
95   releaseList.push_back(id);
96 }
97
98 bool FGTrafficManager::isReleased(void *id)
99 {
100   IdListIterator i = releaseList.begin();
101   while (i != releaseList.end())
102     {
103       if ((*i) == id)
104         {
105           releaseList.erase(i);
106           return true;
107         }
108       i++;
109     }
110   return false;
111 }
112
113 void  FGTrafficManager::startXML () {
114   //cout << "Start XML" << endl;
115 }
116
117 void  FGTrafficManager::endXML () {
118   //cout << "End XML" << endl;
119 }
120
121 void  FGTrafficManager::startElement (const char * name, const XMLAttributes &atts) {
122   const char * attval;
123   //cout << "Start element " << name << endl;
124   //FGTrafficManager temp;
125   //for (int i = 0; i < atts.size(); i++)
126   //  if (string(atts.getName(i)) == string("include"))
127   attval = atts.getValue("include");
128   if (attval != 0)
129       {
130         //cout << "including " << attval << endl;
131         SGPath path = 
132           globals->get_fg_root();
133         path.append("/Traffic/");
134         path.append(attval);
135         readXML(path.str(), *this);
136       }
137   //  cout << "  " << atts.getName(i) << '=' << atts.getValue(i) << endl; 
138 }
139
140 void  FGTrafficManager::endElement (const char * name) {
141   //cout << "End element " << name << endl;
142   string element(name);
143   if (element == string("model"))
144     mdl = value;
145   else if (element == string("livery"))
146     livery = value;
147   else if (element == string("registration"))
148     registration = value;
149   else if (element == string("heavy"))
150     {
151       if(value == string("true"))
152         heavy = true;
153       else
154         heavy = false;
155     }
156   else if (element == string("callsign"))
157     callsign = value;
158   else if (element == string("fltrules"))
159     fltrules = value;
160   else if (element == string("port"))
161     port = value;
162   else if (element == string("time"))
163     timeString = value;
164   else if (element == string("departure"))
165     {
166       departurePort = port;
167       departureTime = timeString;
168     }
169   else if (element == string("cruise-alt"))
170     cruiseAlt = atoi(value.c_str());
171   else if (element == string("arrival"))
172     {
173       arrivalPort = port;
174       arrivalTime = timeString;
175     }
176   else if (element == string("repeat"))
177     repeat = value;
178   else if (element == string("flight"))
179     {
180       // We have loaded and parsed all the information belonging to this flight
181       // so we temporarily store it. 
182       //cerr << "Pusing back flight " << callsign << endl;
183       //cerr << callsign  <<  " " << fltrules     << " "<< departurePort << " " <<  arrivalPort << " "
184       //   << cruiseAlt <<  " " << departureTime<< " "<< arrivalTime   << " " << repeat << endl;
185       flights.push_back(FGScheduledFlight(callsign,
186                                           fltrules,
187                                           departurePort,
188                                           arrivalPort,
189                                           cruiseAlt,
190                                           departureTime,
191                                           arrivalTime,
192                                           repeat));
193     }
194   else if (element == string("aircraft"))
195     {
196       //cerr << "Pushing back aircraft " << registration << endl;
197       scheduledAircraft.push_back(FGAISchedule(mdl, 
198                                                 livery, 
199                                                 registration, 
200                                                 heavy, 
201                                                 flights));
202       while(flights.begin() != flights.end())
203         flights.pop_back();
204     }
205 }
206
207 void  FGTrafficManager::data (const char * s, int len) {
208   string token = string(s,len);
209   //cout << "Character data " << string(s,len) << endl;
210   if ((token.find(" ") == string::npos && (token.find('\n')) == string::npos))
211       value += token;
212   else
213     value = string("");
214 }
215
216 void  FGTrafficManager::pi (const char * target, const char * data) {
217   //cout << "Processing instruction " << target << ' ' << data << endl;
218 }
219
220 void  FGTrafficManager::warning (const char * message, int line, int column) {
221   cout << "Warning: " << message << " (" << line << ',' << column << ')'   
222        << endl;
223 }
224
225 void  FGTrafficManager::error (const char * message, int line, int column) {
226   cout << "Error: " << message << " (" << line << ',' << column << ')'
227        << endl;
228 }