1 // aircraft.cxx -- various aircraft routines
3 // Written by Curtis Olson, started May 1997.
5 // Copyright (C) 1997 Curtis L. Olson - http://www.flightgear.org/~curt
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
29 #include <simgear/constants.h>
30 #include <simgear/debug/logstream.hxx>
31 #include <simgear/misc/sg_path.hxx>
32 #include <simgear/structure/commands.hxx>
33 #include <simgear/structure/exception.hxx>
35 #include <Main/globals.hxx>
36 #include <Main/fg_props.hxx>
37 #include <Main/viewmgr.hxx>
38 #include <Cockpit/panel.hxx>
39 #include <Cockpit/hud.hxx>
40 #include <Cockpit/panel_io.hxx>
41 #include <Model/acmodel.hxx>
43 #include "aircraft.hxx"
46 // This is a record containing all the info for the aircraft currently
48 fgAIRCRAFT current_aircraft;
51 // Initialize an Aircraft structure
52 void fgAircraftInit( void ) {
53 SG_LOG( SG_AIRCRAFT, SG_INFO, "Initializing Aircraft structure" );
55 current_aircraft.fdm_state = cur_fdm_state;
56 current_aircraft.controls = globals->get_controls();
60 // Display various parameters to stdout
61 void fgAircraftOutputCurrent(fgAIRCRAFT *a) {
66 SG_LOG( SG_FLIGHT, SG_DEBUG,
68 << (f->get_Longitude() * 3600.0 * SGD_RADIANS_TO_DEGREES) << ","
69 << (f->get_Latitude() * 3600.0 * SGD_RADIANS_TO_DEGREES) << ","
71 << ") (Phi,Theta,Psi)=("
72 << f->get_Phi() << ","
73 << f->get_Theta() << ","
74 << f->get_Psi() << ")" );
76 SG_LOG( SG_FLIGHT, SG_DEBUG,
77 "Kts = " << f->get_V_equiv_kts()
78 << " Elev = " << globals->get_controls()->get_elevator()
79 << " Aileron = " << globals->get_controls()->get_aileron()
80 << " Rudder = " << globals->get_controls()->get_rudder()
81 << " Power = " << globals->get_controls()->get_throttle( 0 ) );
85 // Show available aircraft types
86 void fgReadAircraft(void) {
88 // SGPropertyNode *aircraft_types = fgGetNode("/sim/aircraft-types", true);
90 SGPath path( globals->get_fg_root() );
91 path.append("Aircraft");
96 dirp = ulOpenDir(path.c_str());
98 SG_LOG( SG_AIRCRAFT, SG_ALERT, "Unable to open aircraft directory." );
103 while ((dire = ulReadDir(dirp)) != NULL) {
106 if ((ptr = strstr(dire->d_name, "-set.xml")) && strlen(ptr) == 8) {
111 afile.append(dire->d_name);
115 readProperties(afile.str(), &root);
120 SGPropertyNode *node = root.getNode("sim");
122 SGPropertyNode *desc = node->getNode("description");
125 SGPropertyNode *aircraft =
126 aircraft_types->getChild(dire->d_name, 0, true);
128 aircraft->setStringValue(strdup(desc->getStringValue()));
137 globals->get_commands()->addCommand("load-aircraft", fgLoadAircraft);
141 fgLoadAircraft (const SGPropertyNode * arg)
143 static const SGPropertyNode *master_freeze
144 = fgGetNode("/sim/freeze/master");
146 bool freeze = master_freeze->getBoolValue();
148 fgSetBool("/sim/freeze/master", true);
152 // remove electrical system
153 cur_fdm_state->unbind();
155 // Save the selected aircraft model since restoreInitialState
156 // will obverwrite it.
158 string aircraft = fgGetString("/sim/aircraft", "");
159 globals->restoreInitialState();
161 fgSetString("/sim/aircraft", aircraft.c_str());
162 fgSetString("/sim/panel/path", "Aircraft/c172/Panels/c172-vfr-panel.xml");
164 if ( aircraft.size() > 0 ) {
165 SGPath aircraft_path(globals->get_fg_root());
166 aircraft_path.append("Aircraft");
167 aircraft_path.append(aircraft);
168 aircraft_path.concat("-set.xml");
169 SG_LOG(SG_INPUT, SG_INFO, "Reading default aircraft: " << aircraft
170 << " from " << aircraft_path.str());
172 readProperties(aircraft_path.str(), globals->get_props());
173 } catch (const sg_exception &e) {
174 string message = "Error reading default aircraft: ";
175 message += e.getFormattedMessage();
176 SG_LOG(SG_INPUT, SG_ALERT, message);
180 SG_LOG(SG_INPUT, SG_ALERT, "No default aircraft specified");
183 // Initialize the (new) 2D panel.
185 string panel_path = fgGetString("/sim/panel/path",
186 "Aircraft/c172/Panels/c172-vfr-panel.xml");
188 FGPanel *panel = fgReadPanel(panel_path);
190 SG_LOG( SG_INPUT, SG_ALERT,
191 "Error reading new panel from " << panel_path );
193 SG_LOG( SG_INPUT, SG_INFO, "Loaded new panel from " << panel_path );
194 globals->get_current_panel()->unbind();
195 delete globals->get_current_panel();
196 globals->set_current_panel( panel );
197 globals->get_current_panel()->init();
198 globals->get_current_panel()->bind();
199 globals->get_current_panel()->update(0);
202 // Load the new 3D model
204 globals->get_aircraft_model()->unbind();
205 delete globals->get_aircraft_model();
206 globals->set_aircraft_model(new FGAircraftModel);
207 globals->get_aircraft_model()->init();
208 globals->get_aircraft_model()->bind();
211 // load new electrical system
214 // update our position based on current presets
218 fgHUDInit(¤t_aircraft);
220 SGTime *t = globals->get_time_params();
223 globals->set_time_params( t );
225 // Reinitialize some subsystems
227 globals->get_viewmgr()->reinit();
228 globals->get_controls()->reset_all();
229 globals->get_aircraft_model()->reinit();
230 globals->get_subsystem("fx")->reinit();
231 globals->get_subsystem("xml-autopilot")->reinit();
233 fgReInitSubsystems();
236 fgSetBool("/sim/freeze/master", false);