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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
28 #include <string.h> // strdup
32 #include <simgear/constants.h>
33 #include <simgear/debug/logstream.hxx>
34 #include <simgear/misc/sg_path.hxx>
35 #include <simgear/structure/commands.hxx>
36 #include <simgear/structure/exception.hxx>
38 #include <Main/globals.hxx>
39 #include <Main/fg_props.hxx>
40 #include <Main/viewmgr.hxx>
41 #include <Cockpit/panel.hxx>
42 #include <Cockpit/hud.hxx>
43 #include <Cockpit/panel_io.hxx>
44 #include <Model/acmodel.hxx>
46 #include "aircraft.hxx"
49 // This is a record containing all the info for the aircraft currently
51 fgAIRCRAFT current_aircraft;
54 // Initialize an Aircraft structure
55 void fgAircraftInit( void ) {
56 SG_LOG( SG_AIRCRAFT, SG_INFO, "Initializing Aircraft structure" );
58 current_aircraft.fdm_state = cur_fdm_state;
59 current_aircraft.controls = globals->get_controls();
63 // Display various parameters to stdout
64 void fgAircraftOutputCurrent(fgAIRCRAFT *a) {
69 SG_LOG( SG_FLIGHT, SG_DEBUG,
71 << (f->get_Longitude() * 3600.0 * SGD_RADIANS_TO_DEGREES) << ","
72 << (f->get_Latitude() * 3600.0 * SGD_RADIANS_TO_DEGREES) << ","
74 << ") (Phi,Theta,Psi)=("
75 << f->get_Phi() << ","
76 << f->get_Theta() << ","
77 << f->get_Psi() << ")" );
79 SG_LOG( SG_FLIGHT, SG_DEBUG,
80 "Kts = " << f->get_V_equiv_kts()
81 << " Elev = " << globals->get_controls()->get_elevator()
82 << " Aileron = " << globals->get_controls()->get_aileron()
83 << " Rudder = " << globals->get_controls()->get_rudder()
84 << " Power = " << globals->get_controls()->get_throttle( 0 ) );
88 // Show available aircraft types
89 void fgReadAircraft(void) {
91 // SGPropertyNode *aircraft_types = fgGetNode("/sim/aircraft-types", true);
93 SGPath path( globals->get_fg_root() );
94 path.append("Aircraft");
99 dirp = ulOpenDir(path.c_str());
101 SG_LOG( SG_AIRCRAFT, SG_ALERT, "Unable to open aircraft directory." );
106 while ((dire = ulReadDir(dirp)) != NULL) {
109 if ((ptr = strstr(dire->d_name, "-set.xml")) && strlen(ptr) == 8) {
114 afile.append(dire->d_name);
118 readProperties(afile.str(), &root);
123 SGPropertyNode *node = root.getNode("sim");
125 SGPropertyNode *desc = node->getNode("description");
128 SGPropertyNode *aircraft =
129 aircraft_types->getChild(dire->d_name, 0, true);
131 aircraft->setStringValue(strdup(desc->getStringValue()));
140 globals->get_commands()->addCommand("load-aircraft", fgLoadAircraft);
144 fgLoadAircraft (const SGPropertyNode * arg)
146 static const SGPropertyNode *master_freeze
147 = fgGetNode("/sim/freeze/master");
149 bool freeze = master_freeze->getBoolValue();
151 fgSetBool("/sim/freeze/master", true);
155 // remove electrical system
156 cur_fdm_state->unbind();
158 // Save the selected aircraft model since restoreInitialState
159 // will obverwrite it.
161 string aircraft = fgGetString("/sim/aircraft", "");
162 globals->restoreInitialState();
164 fgSetString("/sim/aircraft", aircraft.c_str());
165 fgSetString("/sim/panel/path", "Aircraft/c172p/Panels/c172-vfr-panel.xml");
167 if ( aircraft.size() > 0 ) {
168 SGPath aircraft_path(globals->get_fg_root());
169 aircraft_path.append("Aircraft");
170 aircraft_path.append(aircraft);
171 aircraft_path.concat("-set.xml");
172 SG_LOG(SG_INPUT, SG_INFO, "Reading default aircraft: " << aircraft
173 << " from " << aircraft_path.str());
175 readProperties(aircraft_path.str(), globals->get_props());
176 } catch (const sg_exception &e) {
177 string message = "Error reading default aircraft: ";
178 message += e.getFormattedMessage();
179 SG_LOG(SG_INPUT, SG_ALERT, message);
183 SG_LOG(SG_INPUT, SG_ALERT, "No default aircraft specified");
186 // Initialize the (new) 2D panel.
188 string panel_path = fgGetString("/sim/panel/path",
189 "Aircraft/c172p/Panels/c172-vfr-panel.xml");
191 FGPanel *panel = fgReadPanel(panel_path);
193 SG_LOG( SG_INPUT, SG_ALERT,
194 "Error reading new panel from " << panel_path );
196 SG_LOG( SG_INPUT, SG_INFO, "Loaded new panel from " << panel_path );
197 globals->get_current_panel()->unbind();
198 delete globals->get_current_panel();
199 globals->set_current_panel( panel );
200 globals->get_current_panel()->init();
201 globals->get_current_panel()->bind();
202 globals->get_current_panel()->update(0);
205 // Load the new 3D model
207 globals->get_aircraft_model()->unbind();
208 delete globals->get_aircraft_model();
209 globals->set_aircraft_model(new FGAircraftModel);
210 globals->get_aircraft_model()->init();
211 globals->get_aircraft_model()->bind();
214 // load new electrical system
217 // update our position based on current presets
221 fgHUDInit(¤t_aircraft);
223 SGTime *t = globals->get_time_params();
226 globals->set_time_params( t );
228 // Reinitialize some subsystems
230 globals->get_viewmgr()->reinit();
231 globals->get_controls()->reset_all();
232 globals->get_aircraft_model()->reinit();
233 globals->get_subsystem("fx")->reinit();
234 globals->get_subsystem("xml-autopilot")->reinit();
236 fgReInitSubsystems();
239 fgSetBool("/sim/freeze/master", false);