]> git.mxchange.org Git - flightgear.git/blob - src/AIModel/AIFlightPlanCreatePushBack.cxx
Fix a GCC warning.
[flightgear.git] / src / AIModel / AIFlightPlanCreatePushBack.cxx
1 /****************************************************************************
2 * AIFlightPlanCreatePushBack.cxx
3 * Written by Durk Talsma, started August 1, 2007.
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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18 *
19 **************************************************************************/
20
21 #ifdef HAVE_CONFIG_H
22 #  include <config.h>
23 #endif
24
25 #include <cstdlib>
26 #include <cstdio>
27
28 #include <simgear/math/sg_geodesy.hxx>
29
30 #include <Airports/simple.hxx>
31 #include <Airports/runways.hxx>
32 #include <Airports/dynamics.hxx>
33
34 #include <Environment/environment_mgr.hxx>
35 #include <Environment/environment.hxx>
36
37 #include "AIFlightPlan.hxx"
38 #include "AIAircraft.hxx"
39 #include "performancedata.hxx"
40
41
42 // TODO: Use James Turner's createOnGround functions.
43 bool FGAIFlightPlan::createPushBack(FGAIAircraft *ac,
44                                     bool firstFlight, FGAirport *dep,
45                                     double radius,
46                                     const string& fltType,
47                                     const string& aircraftType,
48                                     const string& airline)
49 {
50     double vTaxi = ac->getPerformance()->vTaxi();
51     double vTaxiBackward = vTaxi * (-2.0/3.0);
52     double vTaxiReduced  = vTaxi * (2.0/3.0);
53     // Active runway can be conditionally set by ATC, so at the start of a new flight, this
54     // must be reset.
55     activeRunway.clear();
56
57     if (!(dep->getDynamics()->getGroundNetwork()->exists())) {
58         //cerr << "Push Back fallback" << endl;
59         createPushBackFallBack(ac, firstFlight, dep,
60                                radius, fltType, aircraftType, airline);
61       return true;
62     }
63   
64   // establish the parking position / gate if required
65     if (firstFlight) {
66       gate = dep->getDynamics()->getAvailableParking(radius, fltType,
67                                                        aircraftType, airline);
68       if (!gate.isValid()) {
69         SG_LOG(SG_AI, SG_WARN, "Warning: Could not find parking for a " <<
70                aircraftType <<
71                " of flight type " << fltType <<
72                " of airline     " << airline <<
73                " at airport     " << dep->getId());
74         return false;
75       }
76     }
77   
78     if (!gate.isValid()) {
79         createPushBackFallBack(ac, firstFlight, dep,
80                                radius, fltType, aircraftType, airline);
81         return true;
82
83     }
84   
85     FGGroundNetwork* groundNet = dep->getDynamics()->getGroundNetwork();
86     FGParking *parking = gate.parking();
87     if (parking && parking->getPushBackPoint() > 0) {
88         FGTaxiRoute route = groundNet->findShortestRoute(parking->guid(), parking->getPushBackPoint(), false);
89       
90         int size = route.size();
91         if (size < 2) {
92             SG_LOG(SG_AI, SG_ALERT, "Push back route from gate " << parking->ident() << " has only " << size << " nodes.");
93             SG_LOG(SG_AI, SG_ALERT, "Using  " << parking->getPushBackPoint());
94         }
95         
96         route.first();
97         PositionedID node, previous= 0;
98       
99         while (route.next(&node))
100         {
101             char buffer[10];
102             snprintf (buffer, 10, "%lld",  (long long int) node);
103             FGTaxiNode *tn = groundNet->findNode(node);
104             FGAIWaypoint *wpt = createOnGround(ac, string(buffer), tn->geod(), dep->getElevation(), vTaxiBackward);
105           
106             if (previous) {
107               FGTaxiSegment* segment = groundNet->findSegment(previous, node);
108               wpt->setRouteIndex(segment->getIndex());
109             } else {
110               // not on the route yet, make up a unique segment ID
111               int x = (int) tn->guid();
112               wpt->setRouteIndex(x);
113             }
114           
115             pushBackWaypoint(wpt);
116             previous = node;
117         }
118         // some special considerations for the last point:
119         waypoints.back()->setName(string("PushBackPoint"));
120         waypoints.back()->setSpeed(vTaxi);
121         ac->setTaxiClearanceRequest(true);
122     } else {  // In case of a push forward departure...
123         ac->setTaxiClearanceRequest(false);
124         double az2 = 0.0;
125
126       FGTaxiSegment* pushForwardSegment = dep->getDynamics()->getGroundNetwork()->findSegment(parking->guid(), 0);
127       // there aren't any routes for this parking.
128       if (!pushForwardSegment) {
129           SG_LOG(SG_AI, SG_ALERT, "Gate " << parking->ident() << "doesn't seem to have routes associated with it.");
130           return false;
131       }
132
133       lastNodeVisited = pushForwardSegment->getEnd()->getIndex();
134       double distance = pushForwardSegment->getLength();
135
136       double parkingHeading = parking->getHeading();
137     
138       for (int i = 1; i < 10; i++) {
139           SGGeod pushForwardPt;
140           SGGeodesy::direct(parking->geod(), parkingHeading,
141                             ((i / 10.0) * distance), pushForwardPt, az2);
142           char buffer[16];
143           snprintf(buffer, 16, "pushback-%02d", i);
144           FGAIWaypoint *wpt = createOnGround(ac, string(buffer), pushForwardPt, dep->getElevation(), vTaxiReduced);
145
146           wpt->setRouteIndex(pushForwardSegment->getIndex());
147           pushBackWaypoint(wpt);
148       }
149
150       waypoints.back()->setName(string("PushBackPoint"));
151       // cerr << "Done assinging new name" << endl;
152     }
153
154     return true;
155 }
156 /*******************************************************************
157 * createPushBackFallBack
158 * This is the backup function for airports that don't have a
159 * network yet.
160 ******************************************************************/
161 void FGAIFlightPlan::createPushBackFallBack(FGAIAircraft *ac, bool firstFlight, FGAirport *dep,
162         double radius,
163         const string& fltType,
164         const string& aircraftType,
165         const string& airline)
166 {
167     double az2 = 0.0;
168
169     double vTaxi = ac->getPerformance()->vTaxi();
170     double vTaxiBackward = vTaxi * (-2.0/3.0);
171     double vTaxiReduced  = vTaxi * (2.0/3.0);
172
173     double heading = 180.0; // this is a completely arbitrary heading!
174     FGAIWaypoint *wpt = createOnGround(ac, string("park"), dep->geod(), dep->getElevation(), vTaxiBackward);
175
176     pushBackWaypoint(wpt);
177
178     SGGeod coord;
179     SGGeodesy::direct(dep->geod(), heading, 10, coord, az2);
180     wpt = createOnGround(ac, string("park2"), coord, dep->getElevation(), vTaxiBackward);
181
182     pushBackWaypoint(wpt);
183   
184     SGGeodesy::direct(dep->geod(), heading, 2.2 * radius, coord, az2);
185     wpt = createOnGround(ac, string("taxiStart"), coord, dep->getElevation(), vTaxiReduced);
186     pushBackWaypoint(wpt);
187
188 }