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