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