]> git.mxchange.org Git - flightgear.git/blob - src/Airports/groundnetwork.hxx
Overhaul the ground-net / parking code.
[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 #include <string>
30 #include <vector>
31 #include <list>
32 #include <map>
33
34 #include "gnnode.hxx"
35 #include "parking.hxx"
36 #include <ATC/trafficcontrol.hxx>
37
38
39 class Block;
40 class FGRunway;
41 class FGTaxiSegment; // forward reference
42 class FGAIFlightPlan; // forward reference
43 class FGAirport;      // forward reference
44
45 typedef std::vector<FGTaxiSegment*>  FGTaxiSegmentVector;
46 typedef FGTaxiSegmentVector::iterator FGTaxiSegmentVectorIterator;
47
48 typedef std::map<int, FGTaxiNode_ptr> IndexTaxiNodeMap;
49
50 class Block
51 {
52 private:
53     int id;
54     time_t blocktime;
55     time_t touch;
56 public:
57     Block(int i, time_t bt, time_t curr) { id = i; blocktime= bt; touch = curr; };
58     ~Block() {};
59     int getId() { return id; };
60     void updateTimeStamps(time_t bt, time_t now) { blocktime = (bt < blocktime) ? bt : blocktime; touch = now; };
61     const time_t getBlockTime() const { return blocktime; };
62     time_t getTimeStamp() { return touch; };
63     bool operator< (const Block &other) const { return blocktime < other.blocktime; };
64 };
65
66 typedef std::vector<Block> BlockList;
67 typedef BlockList::iterator BlockListIterator;
68
69 /***************************************************************************************
70  * class FGTaxiSegment
71  **************************************************************************************/
72 class FGTaxiSegment
73 {
74 private:
75     const PositionedID startNode;
76     const PositionedID endNode;
77     
78     bool isActive;
79     BlockList blockTimes;
80
81     int index;
82     FGTaxiSegment *oppositeDirection;
83
84     friend class FGGroundNetwork;
85 public:
86     FGTaxiSegment(PositionedID start, PositionedID end);
87   
88     void setIndex        (int val) {
89         index     = val;
90     };
91   
92     void setDimensions(double elevation);
93     void block(int id, time_t blockTime, time_t now);
94     void unblock(time_t now); 
95     bool hasBlock(time_t now);
96
97     FGTaxiNode * getEnd() const;
98     FGTaxiNode * getStart() const;
99   
100     double getLength() const;
101   
102     // compute the center of the arc
103     SGGeod getCenter() const;
104   
105     double getHeading() const;
106     
107     int getIndex() {
108       return index;
109     };
110
111     int getPenalty(int nGates);
112
113     bool operator<(const FGTaxiSegment &other) const {
114         return index < other.index;
115     };
116
117     FGTaxiSegment *opposite() {
118         return oppositeDirection;
119     };
120 };
121
122
123
124
125 typedef std::vector<int> intVec;
126 typedef std::vector<int>::iterator intVecIterator;
127
128
129
130 /***************************************************************************************
131  * class FGTaxiRoute
132  **************************************************************************************/
133 class FGTaxiRoute
134 {
135 private:
136     PositionedIDVec nodes;
137     double distance;
138     PositionedIDVec::iterator currNode;
139
140 public:
141     FGTaxiRoute() {
142         distance = 0;
143         currNode = nodes.begin();
144     };
145   
146     FGTaxiRoute(const PositionedIDVec& nds, double dist, int dpth) {
147         nodes = nds;
148         distance = dist;
149         currNode = nodes.begin();
150     };
151
152     FGTaxiRoute& operator= (const FGTaxiRoute &other) {
153         nodes = other.nodes;
154         distance = other.distance;
155         currNode = nodes.begin();
156         return *this;
157     };
158
159     FGTaxiRoute(const FGTaxiRoute& copy) :
160             nodes(copy.nodes),
161             distance(copy.distance),
162             currNode(nodes.begin())
163     {};
164
165     bool operator< (const FGTaxiRoute &other) const {
166         return distance < other.distance;
167     };
168     bool empty () {
169         return nodes.empty();
170     };
171     bool next(PositionedID *nde);
172   
173     void first() {
174         currNode = nodes.begin();
175     };
176     int size() {
177         return nodes.size();
178     };
179     int nodesLeft() {
180         return nodes.end() - currNode;
181     };
182 };
183
184 typedef std::vector<FGTaxiRoute> TaxiRouteVector;
185 typedef std::vector<FGTaxiRoute>::iterator TaxiRouteVectorIterator;
186
187 /**************************************************************************************
188  * class FGGroundNetWork
189  *************************************************************************************/
190 class FGGroundNetwork : public FGATCController
191 {
192 private:
193     bool hasNetwork;
194     bool networkInitialized;
195     time_t nextSave;
196     //int maxDepth;
197     int count;
198     int version;
199   
200     FGTaxiSegmentVector segments;
201
202     TrafficVector activeTraffic;
203     TrafficVectorIterator currTraffic;
204
205     bool foundRoute;
206     double totalDistance, maxDistance;
207     FGTowerController *towerController;
208     FGAirport *parent;
209
210
211     //void printRoutingError(string);
212
213     void checkSpeedAdjustment(int id, double lat, double lon,
214                               double heading, double speed, double alt);
215     void checkHoldPosition(int id, double lat, double lon,
216                            double heading, double speed, double alt);
217
218
219     void parseCache();
220   
221     void loadSegments();
222 public:
223     FGGroundNetwork();
224     ~FGGroundNetwork();
225     
226     void setVersion (int v) { version = v;};
227     int getVersion() { return version; };
228
229     void init(FGAirport* pr);
230     bool exists() {
231         return hasNetwork;
232     };
233     void setTowerController(FGTowerController *twrCtrlr) {
234         towerController = twrCtrlr;
235     };
236
237     int findNearestNode(const SGGeod& aGeod) const;
238     int findNearestNodeOnRunway(const SGGeod& aGeod, FGRunway* aRunway = NULL) const;
239
240     FGTaxiNode *findNode(PositionedID idx) const;
241     FGTaxiSegment *findSegment(unsigned idx) const;
242   
243     /**
244      * Find the taxiway segment joining two (ground-net) nodes. Returns
245      * NULL if no such segment exists.
246      * It is permitted to pass 0 for the 'to' ID, indicating that any
247      * segment originating at 'from' is acceptable.
248      */
249     FGTaxiSegment* findSegment(PositionedID from, PositionedID to) const;
250   
251     FGTaxiRoute findShortestRoute(PositionedID start, PositionedID end, bool fullSearch=true);
252
253     virtual void announcePosition(int id, FGAIFlightPlan *intendedRoute, int currentRoute,
254                                   double lat, double lon, double hdg, double spd, double alt,
255                                   double radius, int leg, FGAIAircraft *aircraft);
256     virtual void signOff(int id);
257     virtual void updateAircraftInformation(int id, double lat, double lon, double heading, double speed, double alt, double dt);
258     virtual bool hasInstruction(int id);
259     virtual FGATCInstruction getInstruction(int id);
260
261     bool checkTransmissionState(int minState, int MaxState, TrafficVectorIterator i, time_t now, AtcMsgId msgId,
262                                 AtcMsgDir msgDir);
263     bool checkForCircularWaits(int id);
264     virtual void render(bool);
265     virtual std::string getName();
266     virtual void update(double dt);
267
268     void saveElevationCache();
269     void addVersion(int v) {version = v; };
270 };
271
272
273 #endif