]> git.mxchange.org Git - flightgear.git/blob - src/Aircraft/aircraft.cxx
6679d6486c8f3e578ed1c470807a07c91f9a8049
[flightgear.git] / src / Aircraft / aircraft.cxx
1 // aircraft.cxx -- various aircraft routines
2 //
3 // Written by Curtis Olson, started May 1997.
4 //
5 // Copyright (C) 1997  Curtis L. Olson  - curt@infoplane.com
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 // General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 //
21 // $Id$
22
23
24 #include <stdio.h>
25 #include <string.h>             // strdup
26
27 #include <plib/ul.h>
28 #include <plib/ssg.h>
29
30 #include <simgear/constants.h>
31 #include <simgear/debug/logstream.hxx>
32 #include <simgear/misc/sg_path.hxx>
33 #include <simgear/misc/commands.hxx>
34 #include <simgear/misc/exception.hxx>
35
36 #include <Main/globals.hxx>
37 #include <Main/fg_props.hxx>
38 #include <Main/fgfs.hxx>
39
40 #include "aircraft.hxx"
41
42 extern void fgInitFDM(void);
43 class FGFX;
44
45 // This is a record containing all the info for the aircraft currently
46 // being operated
47 fgAIRCRAFT current_aircraft;
48
49
50 // Initialize an Aircraft structure
51 void fgAircraftInit( void ) {
52     SG_LOG( SG_AIRCRAFT, SG_INFO, "Initializing Aircraft structure" );
53
54     current_aircraft.fdm_state   = cur_fdm_state;
55     current_aircraft.controls = globals->get_controls();
56 }
57
58
59 // Display various parameters to stdout
60 void fgAircraftOutputCurrent(fgAIRCRAFT *a) {
61     FGInterface *f;
62
63     f = a->fdm_state;
64
65     SG_LOG( SG_FLIGHT, SG_DEBUG,
66             "Pos = ("
67             << (f->get_Longitude() * 3600.0 * SGD_RADIANS_TO_DEGREES) << "," 
68             << (f->get_Latitude()  * 3600.0 * SGD_RADIANS_TO_DEGREES) << ","
69             << f->get_Altitude() 
70             << ")  (Phi,Theta,Psi)=("
71             << f->get_Phi() << "," 
72             << f->get_Theta() << "," 
73             << f->get_Psi() << ")" );
74
75     SG_LOG( SG_FLIGHT, SG_DEBUG,
76             "Kts = " << f->get_V_equiv_kts() 
77             << "  Elev = " << globals->get_controls()->get_elevator() 
78             << "  Aileron = " << globals->get_controls()->get_aileron() 
79             << "  Rudder = " << globals->get_controls()->get_rudder() 
80             << "  Power = " << globals->get_controls()->get_throttle( 0 ) );
81 }
82
83
84 // Show available aircraft types
85 void fgReadAircraft(void) {
86
87    SGPropertyNode *aircraft_types = fgGetNode("/sim/aircraft-types", true);
88
89    SGPath path( globals->get_fg_root() );
90    path.append("Aircraft");
91
92    ulDirEnt* dire;
93    ulDir *dirp;
94
95    dirp = ulOpenDir(path.c_str());
96    if (dirp == NULL) {
97       SG_LOG( SG_AIRCRAFT, SG_ALERT, "Unable to open aircraft directory." );
98       ulCloseDir(dirp);
99       return;
100    }
101
102    while ((dire = ulReadDir(dirp)) != NULL) {
103       char *ptr;
104
105       if ((ptr = strstr(dire->d_name, "-set.xml")) && strlen(ptr) == 8) {
106
107           *ptr = '\0';
108 #if 0
109           SGPath afile = path;
110           afile.append(dire->d_name);
111
112           SGPropertyNode root;
113           try {
114              readProperties(afile.str(), &root);
115           } catch (...) {
116              continue;
117           }
118
119           SGPropertyNode *node = root.getNode("sim");
120           if (node) {
121              SGPropertyNode *desc = node->getNode("description");
122
123              if (desc) {
124 #endif
125                 SGPropertyNode *aircraft =
126                                 aircraft_types->getChild(dire->d_name, 0, true);
127 #if 0
128
129                 aircraft->setStringValue(strdup(desc->getStringValue()));
130              }
131           }
132 #endif
133       }
134    }
135
136    ulCloseDir(dirp);
137
138    globals->get_commands()->addCommand("load-aircraft", fgLoadAircraft);
139 }
140
141 static inline bool
142 fgLoadAircraft (const SGPropertyNode * arg)
143 {
144     static const SGPropertyNode *master_freeze
145         = fgGetNode("/sim/freeze/master");
146
147     bool freeze = master_freeze->getBoolValue();
148     if ( !freeze ) {
149         fgSetBool("/sim/freeze/master", true);
150     }
151
152     cur_fdm_state->unbind();
153
154     string aircraft = fgGetString("/sim/aircraft", "");
155     globals->restoreInitialState();
156
157     if ( aircraft.size() > 0 ) {
158         SGPath aircraft_path(globals->get_fg_root());
159         aircraft_path.append("Aircraft");
160         aircraft_path.append(aircraft);
161         aircraft_path.concat("-set.xml");
162         SG_LOG(SG_INPUT, SG_INFO, "Reading default aircraft: " << aircraft
163                << " from " << aircraft_path.str());
164         try {
165             readProperties(aircraft_path.str(), globals->get_props());
166         } catch (const sg_exception &e) {
167             string message = "Error reading default aircraft: ";
168             message += e.getFormattedMessage();
169             SG_LOG(SG_INPUT, SG_ALERT, message);
170             exit(2);
171         }
172     } else {
173         SG_LOG(SG_INPUT, SG_ALERT, "No default aircraft specified");
174     }
175
176     // Update the FDM
177     //
178     fgSetString("/sim/aircraft", aircraft.c_str());
179     fgInitFDM();
180
181     // update our position based on current presets
182     fgInitPosition();
183
184     SGTime *t = globals->get_time_params();
185     delete t;
186     t = fgInitTime();
187     globals->set_time_params( t );
188
189     fgReInitSubsystems();
190
191     if ( !freeze ) {
192         fgSetBool("/sim/freeze/master", false);
193     }
194
195     return true;
196 }