]> git.mxchange.org Git - flightgear.git/blob - src/Aircraft/aircraft.cxx
Forward port from CVS/PLIB: Use a more memory allocation efficient replay
[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  - http://www.flightgear.org/~curt
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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20 //
21 // $Id$
22
23 #ifdef HAVE_CONFIG_H
24 #  include "config.h"
25 #endif
26
27 #include <stdio.h>
28 #include <string.h>             // strdup
29
30 #include <plib/ul.h>
31
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>
37
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>
45
46 #include "aircraft.hxx"
47
48
49 // This is a record containing all the info for the aircraft currently
50 // being operated
51 fgAIRCRAFT current_aircraft;
52
53
54 // Initialize an Aircraft structure
55 void fgAircraftInit( void ) {
56     SG_LOG( SG_AIRCRAFT, SG_INFO, "Initializing Aircraft structure" );
57
58     current_aircraft.fdm_state   = cur_fdm_state;
59     current_aircraft.controls = globals->get_controls();
60 }
61
62
63 // Display various parameters to stdout
64 void fgAircraftOutputCurrent(fgAIRCRAFT *a) {
65     FGInterface *f;
66
67     f = a->fdm_state;
68
69     SG_LOG( SG_FLIGHT, SG_DEBUG,
70             "Pos = ("
71             << (f->get_Longitude() * 3600.0 * SGD_RADIANS_TO_DEGREES) << "," 
72             << (f->get_Latitude()  * 3600.0 * SGD_RADIANS_TO_DEGREES) << ","
73             << f->get_Altitude() 
74             << ")  (Phi,Theta,Psi)=("
75             << f->get_Phi() << "," 
76             << f->get_Theta() << "," 
77             << f->get_Psi() << ")" );
78
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 ) );
85 }
86
87
88 // Show available aircraft types
89 void fgReadAircraft(void) {
90
91    // SGPropertyNode *aircraft_types = fgGetNode("/sim/aircraft-types", true);
92
93    SGPath path( globals->get_fg_root() );
94    path.append("Aircraft");
95
96    ulDirEnt* dire;
97    ulDir *dirp;
98
99    dirp = ulOpenDir(path.c_str());
100    if (dirp == NULL) {
101       SG_LOG( SG_AIRCRAFT, SG_ALERT, "Unable to open aircraft directory." );
102       ulCloseDir(dirp);
103       return;
104    }
105
106    while ((dire = ulReadDir(dirp)) != NULL) {
107       char *ptr;
108
109       if ((ptr = strstr(dire->d_name, "-set.xml")) && strlen(ptr) == 8) {
110
111           *ptr = '\0';
112 #if 0
113           SGPath afile = path;
114           afile.append(dire->d_name);
115
116           SGPropertyNode root;
117           try {
118              readProperties(afile.str(), &root);
119           } catch (...) {
120              continue;
121           }
122
123           SGPropertyNode *node = root.getNode("sim");
124           if (node) {
125              SGPropertyNode *desc = node->getNode("description");
126
127              if (desc) {
128                  SGPropertyNode *aircraft =
129                                 aircraft_types->getChild(dire->d_name, 0, true);
130
131                 aircraft->setStringValue(strdup(desc->getStringValue()));
132              }
133           }
134 #endif
135       }
136    }
137
138    ulCloseDir(dirp);
139
140    globals->get_commands()->addCommand("load-aircraft", fgLoadAircraft);
141 }
142
143 bool
144 fgLoadAircraft (const SGPropertyNode * arg)
145 {
146     static const SGPropertyNode *master_freeze
147         = fgGetNode("/sim/freeze/master");
148
149     bool freeze = master_freeze->getBoolValue();
150     if ( !freeze ) {
151         fgSetBool("/sim/freeze/master", true);
152     }
153
154     // TODO:
155     //    remove electrical system
156     cur_fdm_state->unbind();
157
158     // Save the selected aircraft model since restoreInitialState
159     // will obverwrite it.
160     //
161     string aircraft = fgGetString("/sim/aircraft", "");
162     globals->restoreInitialState();
163
164     fgSetString("/sim/aircraft", aircraft.c_str());
165     fgSetString("/sim/panel/path", "Aircraft/c172p/Panels/c172-vfr-panel.xml");
166
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());
174         try {
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);
180             exit(2);
181         }
182     } else {
183         SG_LOG(SG_INPUT, SG_ALERT, "No default aircraft specified");
184     }
185
186     // Initialize the (new) 2D panel.
187     //
188     string panel_path = fgGetString("/sim/panel/path",
189                                     "Aircraft/c172p/Panels/c172-vfr-panel.xml");
190
191     FGPanel *panel = fgReadPanel(panel_path);
192     if (panel == 0) {
193         SG_LOG( SG_INPUT, SG_ALERT,
194                 "Error reading new panel from " << panel_path );
195     } else {
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);
203     }
204
205     // Load the new 3D model
206     //
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();
212
213     // TODO:
214     //    load new electrical system
215     //
216
217     // update our position based on current presets
218     fgInitPosition();
219
220     // Update the HUD
221     fgHUDInit(&current_aircraft);
222
223     SGTime *t = globals->get_time_params();
224     delete t;
225     t = fgInitTime();
226     globals->set_time_params( t );
227
228     // Reinitialize some subsystems
229     //
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();
235
236     fgReInitSubsystems();
237
238     if ( !freeze ) {
239         fgSetBool("/sim/freeze/master", false);
240     }
241
242     return true;
243 }