]> git.mxchange.org Git - flightgear.git/blob - src/Airports/groundnetwork.hxx
Adding some more intelligence to the AI system step 2: Added a system to
[flightgear.git] / src / Airports / groundnetwork.hxx
1 // groundnet.hxx - A number of classes to handle taxiway
2 // assignments by the AI code
3 //
4 // Written by Durk Talsma, started June 2005.
5 //
6 // Copyright (C) 2004 Durk Talsma.
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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21 //
22 // $Id$
23
24 #ifndef _GROUNDNETWORK_HXX_
25 #define _GROUNDNETWORK_HXX_
26
27 #include <simgear/compiler.h>
28
29
30 #include STL_STRING
31 #include <vector>
32
33 SG_USING_STD(string);
34 SG_USING_STD(vector);
35
36
37 #include "parking.hxx"
38 //#include <AIModel/AIBase.hxx>
39
40
41
42 class FGTaxiSegment; // forward reference
43 class FGAIFlightPlan; // forward reference
44
45 typedef vector<FGTaxiSegment>  FGTaxiSegmentVector;
46 typedef vector<FGTaxiSegment*> FGTaxiSegmentPointerVector;
47 typedef vector<FGTaxiSegment>::iterator FGTaxiSegmentVectorIterator;
48 typedef vector<FGTaxiSegment*>::iterator FGTaxiSegmentPointerVectorIterator;
49
50 /**************************************************************************************
51  * class FGTaxiNode
52  *************************************************************************************/
53 class FGTaxiNode 
54 {
55 private:
56   double lat;
57   double lon;
58   int index;
59   FGTaxiSegmentPointerVector next; // a vector to all the segments leaving from this node
60   
61 public:
62   FGTaxiNode();
63   FGTaxiNode(double, double, int);
64
65   void setIndex(int idx)                  { index = idx;};
66   void setLatitude (double val)           { lat = val;};
67   void setLongitude(double val)           { lon = val;};
68   void setLatitude (const string& val)           { lat = processPosition(val);  };
69   void setLongitude(const string& val)           { lon = processPosition(val);  };
70   void addSegment(FGTaxiSegment *segment) { next.push_back(segment); };
71   
72   double getLatitude() { return lat;};
73   double getLongitude(){ return lon;};
74
75   int getIndex() { return index; };
76   FGTaxiNode *getAddress() { return this;};
77   FGTaxiSegmentPointerVectorIterator getBeginRoute() { return next.begin(); };
78   FGTaxiSegmentPointerVectorIterator getEndRoute()   { return next.end();   }; 
79   bool operator<(const FGTaxiNode &other) const { return index < other.index; };
80 };
81
82 typedef vector<FGTaxiNode> FGTaxiNodeVector;
83 typedef vector<FGTaxiNode>::iterator FGTaxiNodeVectorIterator;
84
85 /***************************************************************************************
86  * class FGTaxiSegment
87  **************************************************************************************/
88 class FGTaxiSegment
89 {
90 private:
91   int startNode;
92   int endNode;
93   double length;
94   FGTaxiNode *start;
95   FGTaxiNode *end;
96   int index;
97   FGTaxiSegment *oppositeDirection;
98
99 public:
100   FGTaxiSegment();
101   //FGTaxiSegment(FGTaxiNode *, FGTaxiNode *, int);
102
103   void setIndex        (int val) { index     = val; };
104   void setStartNodeRef (int val) { startNode = val; };
105   void setEndNodeRef   (int val) { endNode   = val; };
106
107   void setOpposite(FGTaxiSegment *opp) { oppositeDirection = opp; };
108
109   void setStart(FGTaxiNodeVector *nodes);
110   void setEnd  (FGTaxiNodeVector *nodes);
111   void setTrackDistance();
112
113   FGTaxiNode * getEnd() { return end;};
114   FGTaxiNode * getStart() { return start; };
115   double getLength() { return length; };
116   int getIndex() { return index; };
117
118  FGTaxiSegment *getAddress() { return this;};
119
120   bool operator<(const FGTaxiSegment &other) const { return index < other.index; };
121   FGTaxiSegment *opposite() { return oppositeDirection; };
122
123   
124 };
125
126
127 typedef vector<int> intVec;
128 typedef vector<int>::iterator intVecIterator;
129
130 /***************************************************************************************
131  * class FGTaxiRoute
132  **************************************************************************************/
133 class FGTaxiRoute
134 {
135 private:
136   intVec nodes;
137   intVec routes;
138   double distance;
139   intVecIterator currNode;
140   intVecIterator currRoute;
141
142 public:
143   FGTaxiRoute() { distance = 0; currNode = nodes.begin(); currRoute = routes.begin();};
144   FGTaxiRoute(intVec nds, intVec rts, double dist) { 
145     nodes = nds; 
146     routes = rts;
147     distance = dist; 
148     currNode = nodes.begin();
149   };
150   bool operator< (const FGTaxiRoute &other) const {return distance < other.distance; };
151   bool empty () { return nodes.begin() == nodes.end(); };
152   bool next(int *nde); 
153   bool next(int *nde, int *rte);
154   void rewind(int legNr);
155   
156   void first() { currNode = nodes.begin(); currRoute = routes.begin(); };
157   int size() { return nodes.size(); };
158 };
159
160 typedef vector<FGTaxiRoute> TaxiRouteVector;
161 typedef vector<FGTaxiRoute>::iterator TaxiRouteVectorIterator;
162
163 /**************************************************************************************
164  * class FGATCInstruction
165  * like class FGATC Controller, this class definition should go into its own file
166  * and or directory... For now, just testing this stuff out though...
167  *************************************************************************************/
168 class FGATCInstruction
169 {
170 private:
171   bool holdPattern;
172   bool holdPosition;
173   bool changeSpeed;
174   bool changeHeading;
175   bool changeAltitude;
176
177   double speed;
178   double heading;
179   double alt;
180 public:
181
182   FGATCInstruction();
183   bool hasInstruction   ();
184   bool getHoldPattern   () { return holdPattern;    };
185   bool getHoldPosition  () { return holdPosition;   };
186   bool getChangeSpeed   () { return changeSpeed;    };
187   bool getChangeHeading () { return changeHeading;  };
188   bool getChangeAltitude() { return changeAltitude; };
189
190   double getSpeed       () { return speed; };
191   double getHeading     () { return heading; };
192   double getAlt         () { return alt; };
193
194   void setHoldPattern   (bool val) { holdPattern    = val; };
195   void setHoldPosition  (bool val) { holdPosition   = val; };
196   void setChangeSpeed   (bool val) { changeSpeed    = val; };
197   void setChangeHeading (bool val) { changeHeading  = val; };
198   void setChangeAltitude(bool val) { changeAltitude = val; };
199
200   void setSpeed       (double val) { speed   = val; };
201   void setHeading     (double val) { heading = val; };
202   void setAlt         (double val) { alt     = val; };
203 };
204
205 class FGGroundNetwork;
206
207 /**************************************************************************************
208  * class FGTrafficRecord
209  *************************************************************************************/
210 class FGTrafficRecord
211 {
212 private:
213   int id, waitsForId;
214   int currentPos;
215   intVec intentions;
216   FGATCInstruction instruction;
217   double latitude, longitude, heading, speed, altitude, radius;
218   
219   
220 public:
221   FGTrafficRecord() {};
222   
223   void setId(int val)  { id = val; };
224   void setRadius(double rad) { radius = rad;};
225   void setPositionAndIntentions(int pos, FGAIFlightPlan *route);
226   int getId() { return id;};
227   FGATCInstruction getInstruction() { return instruction;};
228   bool hasInstruction() { return instruction.hasInstruction(); };
229   void setPositionAndHeading(double lat, double lon, double hdg, double spd, double alt);
230   bool checkPositionAndIntentions(FGTrafficRecord &other);
231   int  crosses                   (FGGroundNetwork *, FGTrafficRecord &other); 
232   bool isOpposing                (FGGroundNetwork *, FGTrafficRecord &other, int node);
233
234   bool getSpeedAdjustment() { return instruction.getChangeSpeed(); };
235   
236   double getLatitude () { return latitude ; };
237   double getLongitude() { return longitude; };
238   double getHeading  () { return heading  ; };
239   double getSpeed    () { return speed    ; };
240   double getAltitude () { return altitude ; };
241   double getRadius   () { return radius   ; };
242
243   int getWaitsForId  () { return waitsForId; };
244
245   void setSpeedAdjustment(double spd) { instruction.setChangeSpeed(true); 
246                                         instruction.setSpeed(spd); };
247   void setHeadingAdjustment(double heading) { instruction.setChangeHeading(true);
248                                               instruction.setHeading(heading); };
249   void clearSpeedAdjustment  () { instruction.setChangeSpeed  (false); };
250   void clearHeadingAdjustment() { instruction.setChangeHeading(false); };
251
252   bool hasHeadingAdjustment() { return instruction.getChangeHeading(); };
253   bool hasHoldPosition() { return instruction.getHoldPosition(); };
254   void setHoldPosition (bool inst) { instruction.setHoldPosition(inst); };
255
256   void setWaitsForId(int id) { waitsForId = id; };
257
258 };
259
260 typedef vector<FGTrafficRecord> TrafficVector;
261 typedef vector<FGTrafficRecord>::iterator TrafficVectorIterator;
262
263
264
265
266 /**************************************************************************************
267  * class FGATCController
268  * NOTE: this class serves as an abstraction layer for all sorts of ATC controller,
269  * Ground and air, so eventually it should move to its own file / directory. 
270  *************************************************************************************/
271 class FGATCController
272 {
273 private:
274   double dt_count;
275 public:
276   FGATCController() { dt_count = 0;};
277   virtual ~FGATCController() {};
278   virtual void announcePosition(int id, FGAIFlightPlan *intendedRoute, int currentRoute,
279                                 double lat, double lon,
280                                 double hdg, double spd, double alt, double radius) = 0;
281   virtual void             signOff(int id) = 0;
282   virtual void             update(int id, double lat, double lon, 
283                                   double heading, double speed, double alt, double dt) = 0;
284   virtual bool             hasInstruction(int id) = 0;
285   virtual FGATCInstruction getInstruction(int id) = 0;
286
287   double getDt() { return dt_count; };
288   void   setDt(double dt) { dt_count = dt;};
289 };
290
291
292
293 /**************************************************************************************
294  * class FGGroundNetWork
295  *************************************************************************************/
296 class FGGroundNetwork : public FGATCController
297 {
298 private:
299   bool hasNetwork;
300   FGTaxiNodeVector    nodes;
301   FGTaxiSegmentVector segments;
302   //intVec route;
303   intVec nodesStack;
304   intVec routesStack;
305   TaxiRouteVector routes;
306   TrafficVector activeTraffic;
307   TrafficVectorIterator currTraffic;
308   
309   bool foundRoute;
310   double totalDistance, maxDistance;
311
312   void printRoutingError(string);
313
314   void checkSpeedAdjustment(int id, double lat, double lon, 
315                             double heading, double speed, double alt);
316   void checkHoldPosition(int id, double lat, double lon, 
317                          double heading, double speed, double alt);
318   
319 public:
320   FGGroundNetwork();
321
322   void addNode   (const FGTaxiNode& node);
323   void addNodes  (FGParkingVec *parkings);
324   void addSegment(const FGTaxiSegment& seg); 
325
326   void init();
327   bool exists() { return hasNetwork; };
328   int findNearestNode(double lat, double lon);
329   FGTaxiNode *findNode(int idx);
330   FGTaxiSegment *findSegment(int idx);
331   FGTaxiRoute findShortestRoute(int start, int end);
332   void trace(FGTaxiNode *, int, int, double dist);
333
334   virtual void announcePosition(int id, FGAIFlightPlan *intendedRoute, int currentRoute, 
335                                 double lat, double lon, double hdg, double spd, double alt, double radius);
336   virtual void signOff(int id);
337   virtual void update(int id, double lat, double lon, double heading, double speed, double alt, double dt);
338   virtual bool hasInstruction(int id);
339   virtual FGATCInstruction getInstruction(int id);
340 };
341
342
343 #endif