]> git.mxchange.org Git - flightgear.git/blob - src/Autopilot/route_mgr.hxx
Fix various route-manager issues reported by Hyde.
[flightgear.git] / src / Autopilot / route_mgr.hxx
1 // route_mgr.hxx - manage a route (i.e. a collection of waypoints)
2 //
3 // Written by Curtis Olson, started January 2004.
4 //
5 // Copyright (C) 2004  Curtis L. Olson  - http://www.flightgear.org/~curt
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 // General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20 //
21 // $Id$
22
23
24 #ifndef _ROUTE_MGR_HXX
25 #define _ROUTE_MGR_HXX 1
26
27 #include <simgear/props/props.hxx>
28 #include <simgear/route/waypoint.hxx>
29 #include <simgear/structure/subsystem_mgr.hxx>
30
31 #include <Navaids/route.hxx>
32
33 // forward decls
34 class SGPath;
35 class PropertyWatcher;
36
37 class FGAirport;
38 class FGRunway;
39
40 typedef SGSharedPtr<FGAirport> FGAirportRef;
41
42 /**
43  * Top level route manager class
44  * 
45  */
46
47 class FGRouteMgr : public SGSubsystem, 
48                    public flightgear::FlightPlan::Delegate
49 {
50 public:
51   FGRouteMgr();
52   ~FGRouteMgr();
53
54   void init ();
55   void postinit ();
56   void bind ();
57   void unbind ();
58   void update (double dt);
59   
60   typedef enum {
61     ROUTE_HIGH_AIRWAYS, ///< high-level airways routing
62     ROUTE_LOW_AIRWAYS, ///< low-level airways routing
63     ROUTE_VOR ///< VOR-VOR routing
64   } RouteType;
65   
66   /**
67    * Insert waypoints from index-1 to index. In practice this means you can
68    * 'fill in the gaps' between defined waypoints. If index=0, the departure
69    * airport is used as index-1; if index is -1, the destination airport is
70    * used as the final waypoint.
71    */
72   bool routeToIndex(int index, RouteType aRouteType);
73         
74   bool isRouteActive() const;
75          
76   int currentIndex() const;
77   
78   void setFlightPlan(flightgear::FlightPlan* plan);
79   flightgear::FlightPlan* flightPlan() const;
80   
81   void clearRoute();
82   
83   flightgear::Waypt* currentWaypt() const;
84   
85   int numLegs() const;
86   
87 // deprecated
88   int numWaypts() const
89   { return numLegs(); }
90   
91 // deprecated
92   flightgear::Waypt* wayptAtIndex(int index) const;
93   
94   SGPropertyNode_ptr wayptNodeAtIndex(int index) const;
95   
96   void removeLegAtIndex(int aIndex);
97   
98   /**
99    * Activate a built route. This checks for various mandatory pieces of
100    * data, such as departure and destination airports, and creates waypoints
101    * for them on the route structure.
102    *
103    * returns true if the route was activated successfully, or false if the
104    * route could not be activated for some reason
105    */
106   bool activate();
107
108   /**
109    * Step to the next waypoint on the active route
110    */
111   void sequence();
112   
113   /**
114    * Set the current waypoint to the specified index.
115    */
116   void jumpToIndex(int index);
117   
118   bool saveRoute(const SGPath& p);
119   bool loadRoute(const SGPath& p);
120   
121   flightgear::WayptRef waypointFromString(const std::string& target);
122   
123   /**
124    * Helper command to setup current airport/runway if necessary
125    */
126   void initAtPosition();
127
128 private:
129     flightgear::FlightPlan* _plan;
130   
131     time_t _takeoffTime;
132     time_t _touchdownTime;
133
134     // automatic inputs
135     SGPropertyNode_ptr magvar;
136     
137     // automatic outputs    
138     SGPropertyNode_ptr departure; ///< departure airport information
139     SGPropertyNode_ptr destination; ///< destination airport information
140     SGPropertyNode_ptr alternate; ///< alternate airport information
141     SGPropertyNode_ptr cruise; ///< cruise information
142     
143     SGPropertyNode_ptr totalDistance;
144     SGPropertyNode_ptr distanceToGo;
145     SGPropertyNode_ptr ete;
146     SGPropertyNode_ptr elapsedFlightTime;
147     
148     SGPropertyNode_ptr active;
149     SGPropertyNode_ptr airborne;
150     
151     SGPropertyNode_ptr wp0;
152     SGPropertyNode_ptr wp1;
153     SGPropertyNode_ptr wpn;
154     
155     
156     SGPropertyNode_ptr _pathNode;
157     SGPropertyNode_ptr _currentWpt;
158     
159     /// integer property corresponding to the RouteType enum
160     SGPropertyNode_ptr _routingType;
161     
162     /** 
163      * Signal property to notify people that the route was edited
164      */
165     SGPropertyNode_ptr _edited;
166     
167     /**
168      * Signal property to notify when the last waypoint is reached
169      */
170     SGPropertyNode_ptr _finished;
171     
172     SGPropertyNode_ptr _flightplanChanged;
173   
174     void setETAPropertyFromDistance(SGPropertyNode_ptr aProp, double aDistance);
175     
176     /**
177      * retrieve the cached path distance along a leg
178      */
179     double cachedLegPathDistanceM(int index) const;
180     double cachedWaypointPathTotalDistance(int index) const;
181   
182     class InputListener : public SGPropertyChangeListener {
183     public:
184         InputListener(FGRouteMgr *m) : mgr(m) {}
185         virtual void valueChanged (SGPropertyNode * prop);
186     private:
187         FGRouteMgr *mgr;
188     };
189
190     SGPropertyNode_ptr input;
191     SGPropertyNode_ptr weightOnWheels;
192     SGPropertyNode_ptr groundSpeed;
193   
194     InputListener *listener;
195     SGPropertyNode_ptr mirror;    
196   
197     virtual void departureChanged();
198     void buildDeparture(flightgear::WayptRef enroute, flightgear::WayptVec& wps);
199     
200     virtual void arrivalChanged();
201     void buildArrival(flightgear::WayptRef enroute, flightgear::WayptVec& wps);
202     
203     /**
204      * Helper to keep various pieces of state in sync when the route is
205      * modified (waypoints added, inserted, removed). Notably, this fires the
206      * 'edited' signal.
207      */
208     virtual void waypointsChanged();
209     
210     void update_mirror();
211     
212     virtual void currentWaypointChanged();
213     
214     /**
215      * Check if we've reached the final waypoint. 
216      * Returns true if we have.
217      */
218     bool checkFinished();
219     
220     /**
221      * Predicate for helping the UI - test if at least one waypoint was
222      * entered by the user (as opposed to being generated by the route-manager)
223      */
224     bool haveUserWaypoints() const;
225     
226 // tied getters and setters
227     const char* getDepartureICAO() const;
228     const char* getDepartureName() const;
229     void setDepartureICAO(const char* aIdent);
230     
231     const char* getDepartureRunway() const;
232     void setDepartureRunway(const char* aIdent);
233   
234     const char* getSID() const;
235     void setSID(const char* aIdent);
236   
237     const char* getDestinationICAO() const;
238     const char* getDestinationName() const;
239     void setDestinationICAO(const char* aIdent);
240
241     const char* getDestinationRunway() const;
242     void setDestinationRunway(const char* aIdent);
243   
244     const char* getApproach() const;
245     void setApproach(const char* aIdent);
246   
247     const char* getSTAR() const;
248     void setSTAR(const char* aIdent);
249   
250     double getDepartureFieldElevation() const;  
251     double getDestinationFieldElevation() const;  
252 };
253
254
255 #endif // _ROUTE_MGR_HXX