1 // aircraft.cxx -- various aircraft routines
3 // Written by Curtis Olson, started May 1997.
5 // Copyright (C) 1997 Curtis L. Olson - curt@infoplane.com
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.
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.
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.
25 #include <string.h> // strdup
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>
36 #include <Main/globals.hxx>
37 #include <Main/fg_props.hxx>
38 #include <Main/fgfs.hxx>
40 #include "aircraft.hxx"
42 extern void fgInitFDM(void);
45 // This is a record containing all the info for the aircraft currently
47 fgAIRCRAFT current_aircraft;
50 // Initialize an Aircraft structure
51 void fgAircraftInit( void ) {
52 SG_LOG( SG_AIRCRAFT, SG_INFO, "Initializing Aircraft structure" );
54 current_aircraft.fdm_state = cur_fdm_state;
55 current_aircraft.controls = globals->get_controls();
59 // Display various parameters to stdout
60 void fgAircraftOutputCurrent(fgAIRCRAFT *a) {
65 SG_LOG( SG_FLIGHT, SG_DEBUG,
67 << (f->get_Longitude() * 3600.0 * SGD_RADIANS_TO_DEGREES) << ","
68 << (f->get_Latitude() * 3600.0 * SGD_RADIANS_TO_DEGREES) << ","
70 << ") (Phi,Theta,Psi)=("
71 << f->get_Phi() << ","
72 << f->get_Theta() << ","
73 << f->get_Psi() << ")" );
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 ) );
84 // Show available aircraft types
85 void fgReadAircraft(void) {
87 SGPropertyNode *aircraft_types = fgGetNode("/sim/aircraft-types", true);
89 SGPath path( globals->get_fg_root() );
90 path.append("Aircraft");
95 dirp = ulOpenDir(path.c_str());
97 SG_LOG( SG_AIRCRAFT, SG_ALERT, "Unable to open aircraft directory." );
102 while ((dire = ulReadDir(dirp)) != NULL) {
105 if ((ptr = strstr(dire->d_name, "-set.xml")) && strlen(ptr) == 8) {
110 afile.append(dire->d_name);
114 readProperties(afile.str(), &root);
119 SGPropertyNode *node = root.getNode("sim");
121 SGPropertyNode *desc = node->getNode("description");
125 SGPropertyNode *aircraft =
126 aircraft_types->getChild(dire->d_name, 0, true);
129 aircraft->setStringValue(strdup(desc->getStringValue()));
138 globals->get_commands()->addCommand("load-aircraft", fgLoadAircraft);
142 fgLoadAircraft (const SGPropertyNode * arg)
144 static const SGPropertyNode *master_freeze
145 = fgGetNode("/sim/freeze/master");
147 bool freeze = master_freeze->getBoolValue();
149 fgSetBool("/sim/freeze/master", true);
152 cur_fdm_state->unbind();
154 string aircraft = fgGetString("/sim/aircraft", "");
155 globals->restoreInitialState();
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());
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);
173 SG_LOG(SG_INPUT, SG_ALERT, "No default aircraft specified");
178 fgSetString("/sim/aircraft", aircraft.c_str());
181 // update our position based on current presets
184 SGTime *t = globals->get_time_params();
187 globals->set_time_params( t );
189 fgReInitSubsystems();
192 fgSetBool("/sim/freeze/master", false);