]> git.mxchange.org Git - flightgear.git/blob - src/AIModel/AIFlightPlanCreatePushBack.cxx
Clean up dynamics/parking handing code.
[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     FGTaxiRoute *pushBackRoute;
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       gateId = dep->getDynamics()->getAvailableParking(radius, fltType,
67                                                        aircraftType, airline);
68       if (gateId < 0) {
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     } else {
77       dep->getDynamics()->getParking(gateId);
78     }
79   
80     if (gateId < 0) {
81         createPushBackFallBack(ac, firstFlight, dep,
82                                radius, fltType, aircraftType, airline);
83         return true;
84
85     }
86
87     FGParking *parking = dep->getDynamics()->getParking(gateId);
88     int pushBackNode = parking->getPushBackPoint();
89
90     pushBackRoute = parking->getPushBackRoute();
91     if ((pushBackNode > 0) && (pushBackRoute == 0)) {  // Load the already established route for this gate
92         int node, rte;
93         FGTaxiRoute route;
94         //cerr << "Creating push-back for " << gateId << " (" << parking->getName() << ") using push-back point " << pushBackNode << endl;
95         route = dep->getDynamics()->getGroundNetwork()->findShortestRoute(gateId, pushBackNode, false);
96         parking->setPushBackRoute(new FGTaxiRoute(route));
97
98
99         pushBackRoute = parking->getPushBackRoute();
100         int size = pushBackRoute->size();
101         if (size < 2) {
102             SG_LOG(SG_AI, SG_ALERT, "Push back route from gate " << gateId << " has only " << size << " nodes.");
103             SG_LOG(SG_AI, SG_ALERT, "Using  " << pushBackNode);
104         }
105         pushBackRoute->first();
106         while (pushBackRoute->next(&node, &rte))
107         {
108             //FGTaxiNode *tn = apt->getDynamics()->getGroundNetwork()->findSegment(node)->getEnd();
109             char buffer[10];
110             snprintf (buffer, 10, "%d", node);
111             FGTaxiNode *tn = dep->getDynamics()->getGroundNetwork()->findNode(node);
112             //ids.pop_back();
113             //wpt = new waypoint;
114             FGAIWaypoint *wpt = createOnGround(ac, string(buffer), tn->getGeod(), dep->getElevation(), vTaxiBackward);
115
116             wpt->setRouteIndex(rte);
117             pushBackWaypoint(wpt);
118         }
119         // some special considerations for the last point:
120         waypoints.back()->setName(string("PushBackPoint"));
121         waypoints.back()->setSpeed(vTaxi);
122         ac->setTaxiClearanceRequest(true);
123     } else {  // In case of a push forward departure...
124         ac->setTaxiClearanceRequest(false);
125         double az2 = 0.0;
126
127         //cerr << "Creating final push forward point for gate " << gateId << endl;
128         FGTaxiNode *tn = dep->getDynamics()->getGroundNetwork()->findNode(gateId);
129         FGTaxiSegmentVectorIterator ts = tn->getBeginRoute();
130         FGTaxiSegmentVectorIterator te = tn->getEndRoute();
131         // if the starting node equals the ending node, then there aren't any routes for this parking.
132         // in cases like these we should flag the gate as being inoperative and return false
133         if (ts == te) {
134             SG_LOG(SG_AI, SG_ALERT, "Gate " << gateId << "doesn't seem to have routes associated with it.");
135             parking->setAvailable(false);
136             return false;
137         }
138         tn = (*ts)->getEnd();
139         lastNodeVisited = tn->getIndex();
140         if (tn == NULL) {
141             SG_LOG(SG_AI, SG_ALERT, "No valid taxinode found");
142             exit(1);
143         }
144         double distance = (*ts)->getLength();
145
146         double parkingHeading = parking->getHeading();
147       
148         for (int i = 1; i < 10; i++) {
149             SGGeod pushForwardPt;
150             SGGeodesy::direct(parking->getGeod(), parkingHeading,
151                               ((i / 10.0) * distance), pushForwardPt, az2);
152             char buffer[16];
153             snprintf(buffer, 16, "pushback-%02d", i);
154             FGAIWaypoint *wpt = createOnGround(ac, string(buffer), pushForwardPt, dep->getElevation(), vTaxiReduced);
155
156             wpt->setRouteIndex((*ts)->getIndex());
157             pushBackWaypoint(wpt);
158         }
159         // cerr << "Done " << endl;
160         waypoints.back()->setName(string("PushBackPoint"));
161         // cerr << "Done assinging new name" << endl;
162     }
163
164     return true;
165 }
166 /*******************************************************************
167 * createPushBackFallBack
168 * This is the backup function for airports that don't have a
169 * network yet.
170 ******************************************************************/
171 void FGAIFlightPlan::createPushBackFallBack(FGAIAircraft *ac, bool firstFlight, FGAirport *dep,
172         double radius,
173         const string& fltType,
174         const string& aircraftType,
175         const string& airline)
176 {
177     double az2 = 0.0;
178
179     double vTaxi = ac->getPerformance()->vTaxi();
180     double vTaxiBackward = vTaxi * (-2.0/3.0);
181     double vTaxiReduced  = vTaxi * (2.0/3.0);
182
183     double heading = 180.0; // this is a completely arbitrary heading!
184     FGAIWaypoint *wpt = createOnGround(ac, string("park"), dep->geod(), dep->getElevation(), vTaxiBackward);
185
186     pushBackWaypoint(wpt);
187
188     SGGeod coord;
189     SGGeodesy::direct(dep->geod(), heading, 10, coord, az2);
190     wpt = createOnGround(ac, string("park2"), coord, dep->getElevation(), vTaxiBackward);
191
192     pushBackWaypoint(wpt);
193   
194     SGGeodesy::direct(dep->geod(), heading, 2.2 * radius, coord, az2);
195     wpt = createOnGround(ac, string("taxiStart"), coord, dep->getElevation(), vTaxiReduced);
196     pushBackWaypoint(wpt);
197
198 }