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/structure/commands.hxx>
34 #include <simgear/structure/exception.hxx>
36 #include <Main/globals.hxx>
37 #include <Main/fg_props.hxx>
38 #include <Main/viewmgr.hxx>
39 #include <Cockpit/panel.hxx>
40 #include <Cockpit/hud.hxx>
41 #include <Cockpit/panel_io.hxx>
42 #include <Model/acmodel.hxx>
43 #include <Autopilot/newauto.hxx>
45 #include "aircraft.hxx"
48 // This is a record containing all the info for the aircraft currently
50 fgAIRCRAFT current_aircraft;
53 // Initialize an Aircraft structure
54 void fgAircraftInit( void ) {
55 SG_LOG( SG_AIRCRAFT, SG_INFO, "Initializing Aircraft structure" );
57 current_aircraft.fdm_state = cur_fdm_state;
58 current_aircraft.controls = globals->get_controls();
62 // Display various parameters to stdout
63 void fgAircraftOutputCurrent(fgAIRCRAFT *a) {
68 SG_LOG( SG_FLIGHT, SG_DEBUG,
70 << (f->get_Longitude() * 3600.0 * SGD_RADIANS_TO_DEGREES) << ","
71 << (f->get_Latitude() * 3600.0 * SGD_RADIANS_TO_DEGREES) << ","
73 << ") (Phi,Theta,Psi)=("
74 << f->get_Phi() << ","
75 << f->get_Theta() << ","
76 << f->get_Psi() << ")" );
78 SG_LOG( SG_FLIGHT, SG_DEBUG,
79 "Kts = " << f->get_V_equiv_kts()
80 << " Elev = " << globals->get_controls()->get_elevator()
81 << " Aileron = " << globals->get_controls()->get_aileron()
82 << " Rudder = " << globals->get_controls()->get_rudder()
83 << " Power = " << globals->get_controls()->get_throttle( 0 ) );
87 // Show available aircraft types
88 void fgReadAircraft(void) {
90 // SGPropertyNode *aircraft_types = fgGetNode("/sim/aircraft-types", true);
92 SGPath path( globals->get_fg_root() );
93 path.append("Aircraft");
98 dirp = ulOpenDir(path.c_str());
100 SG_LOG( SG_AIRCRAFT, SG_ALERT, "Unable to open aircraft directory." );
105 while ((dire = ulReadDir(dirp)) != NULL) {
108 if ((ptr = strstr(dire->d_name, "-set.xml")) && strlen(ptr) == 8) {
113 afile.append(dire->d_name);
117 readProperties(afile.str(), &root);
122 SGPropertyNode *node = root.getNode("sim");
124 SGPropertyNode *desc = node->getNode("description");
127 SGPropertyNode *aircraft =
128 aircraft_types->getChild(dire->d_name, 0, true);
130 aircraft->setStringValue(strdup(desc->getStringValue()));
139 globals->get_commands()->addCommand("load-aircraft", fgLoadAircraft);
143 fgLoadAircraft (const SGPropertyNode * arg)
145 static const SGPropertyNode *master_freeze
146 = fgGetNode("/sim/freeze/master");
148 bool freeze = master_freeze->getBoolValue();
150 fgSetBool("/sim/freeze/master", true);
154 // remove electrical system
155 cur_fdm_state->unbind();
157 // Save the selected aircraft model since restoreInitialState
158 // will obverwrite it.
160 string aircraft = fgGetString("/sim/aircraft", "");
161 globals->restoreInitialState();
163 fgSetString("/sim/aircraft", aircraft.c_str());
164 fgSetString("/sim/panel/path", "Aircraft/c172/Panels/c172-vfr-panel.xml");
166 if ( aircraft.size() > 0 ) {
167 SGPath aircraft_path(globals->get_fg_root());
168 aircraft_path.append("Aircraft");
169 aircraft_path.append(aircraft);
170 aircraft_path.concat("-set.xml");
171 SG_LOG(SG_INPUT, SG_INFO, "Reading default aircraft: " << aircraft
172 << " from " << aircraft_path.str());
174 readProperties(aircraft_path.str(), globals->get_props());
175 } catch (const sg_exception &e) {
176 string message = "Error reading default aircraft: ";
177 message += e.getFormattedMessage();
178 SG_LOG(SG_INPUT, SG_ALERT, message);
182 SG_LOG(SG_INPUT, SG_ALERT, "No default aircraft specified");
185 // Initialize the (new) 2D panel.
187 string panel_path = fgGetString("/sim/panel/path",
188 "Aircraft/c172/Panels/c172-vfr-panel.xml");
190 FGPanel *panel = fgReadPanel(panel_path);
192 SG_LOG( SG_INPUT, SG_ALERT,
193 "Error reading new panel from " << panel_path );
195 SG_LOG( SG_INPUT, SG_INFO, "Loaded new panel from " << panel_path );
196 globals->get_current_panel()->unbind();
197 delete globals->get_current_panel();
198 globals->set_current_panel( panel );
199 globals->get_current_panel()->init();
200 globals->get_current_panel()->bind();
201 globals->get_current_panel()->update(0);
204 // Load the new 3D model
206 globals->get_aircraft_model()->unbind();
207 delete globals->get_aircraft_model();
208 globals->set_aircraft_model(new FGAircraftModel);
209 globals->get_aircraft_model()->init();
210 globals->get_aircraft_model()->bind();
213 // load new electrical system
216 // update our position based on current presets
220 fgHUDInit(¤t_aircraft);
222 SGTime *t = globals->get_time_params();
225 globals->set_time_params( t );
227 // Reinitialize some subsystems
229 globals->get_viewmgr()->reinit();
230 globals->get_controls()->reset_all();
231 globals->get_autopilot()->reset();
232 globals->get_aircraft_model()->reinit();
233 globals->get_subsystem("fx")->reinit();
235 fgReInitSubsystems();
238 fgSetBool("/sim/freeze/master", false);