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