]> git.mxchange.org Git - flightgear.git/blob - src/Aircraft/aircraft.cxx
Merge branch 'curt/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 <cstdio>
28 #include <cstring>              // 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 #include <simgear/sound/soundmgr_openal.hxx>
38
39 #include <Main/globals.hxx>
40 #include <Main/fg_init.hxx>
41 #include <Main/fg_props.hxx>
42 #include <Main/viewmgr.hxx>
43 #include <Cockpit/panel.hxx>
44 #include <Cockpit/hud.hxx>
45 #include <Cockpit/panel_io.hxx>
46 #include <Model/acmodel.hxx>
47
48 #include "aircraft.hxx"
49
50
51 // This is a record containing all the info for the aircraft currently
52 // being operated
53 fgAIRCRAFT current_aircraft;
54
55
56 // Initialize an Aircraft structure
57 void fgAircraftInit( void ) {
58     SG_LOG( SG_AIRCRAFT, SG_INFO, "Initializing Aircraft structure" );
59
60     current_aircraft.fdm_state   = cur_fdm_state;
61     current_aircraft.controls = globals->get_controls();
62 }
63
64
65 // Display various parameters to stdout
66 void fgAircraftOutputCurrent(fgAIRCRAFT *a) {
67     FGInterface *f;
68
69     f = a->fdm_state;
70
71     SG_LOG( SG_FLIGHT, SG_DEBUG,
72             "Pos = ("
73             << (f->get_Longitude() * 3600.0 * SGD_RADIANS_TO_DEGREES) << "," 
74             << (f->get_Latitude()  * 3600.0 * SGD_RADIANS_TO_DEGREES) << ","
75             << f->get_Altitude() 
76             << ")  (Phi,Theta,Psi)=("
77             << f->get_Phi() << "," 
78             << f->get_Theta() << "," 
79             << f->get_Psi() << ")" );
80
81     SG_LOG( SG_FLIGHT, SG_DEBUG,
82             "Kts = " << f->get_V_equiv_kts() 
83             << "  Elev = " << globals->get_controls()->get_elevator() 
84             << "  Aileron = " << globals->get_controls()->get_aileron() 
85             << "  Rudder = " << globals->get_controls()->get_rudder() 
86             << "  Power = " << globals->get_controls()->get_throttle( 0 ) );
87 }
88
89
90 // Show available aircraft types
91 void fgReadAircraft(void) {
92
93    // SGPropertyNode *aircraft_types = fgGetNode("/sim/aircraft-types", true);
94
95    SGPath path( globals->get_fg_root() );
96    path.append("Aircraft");
97
98    ulDirEnt* dire;
99    ulDir *dirp;
100
101    dirp = ulOpenDir(path.c_str());
102    if (dirp == NULL) {
103       SG_LOG( SG_AIRCRAFT, SG_ALERT, "Unable to open aircraft directory." );
104       ulCloseDir(dirp);
105       return;
106    }
107
108    while ((dire = ulReadDir(dirp)) != NULL) {
109       char *ptr;
110
111       if ((ptr = strstr(dire->d_name, "-set.xml")) && strlen(ptr) == 8) {
112
113           *ptr = '\0';
114 #if 0
115           SGPath afile = path;
116           afile.append(dire->d_name);
117
118           SGPropertyNode root;
119           try {
120              readProperties(afile.str(), &root);
121           } catch (...) {
122              continue;
123           }
124
125           SGPropertyNode *node = root.getNode("sim");
126           if (node) {
127              SGPropertyNode *desc = node->getNode("description");
128
129              if (desc) {
130                  SGPropertyNode *aircraft =
131                                 aircraft_types->getChild(dire->d_name, 0, true);
132
133                 aircraft->setStringValue(strdup(desc->getStringValue()));
134              }
135           }
136 #endif
137       }
138    }
139
140    ulCloseDir(dirp);
141
142    globals->get_commands()->addCommand("load-aircraft", fgLoadAircraft);
143 }
144
145 bool
146 fgLoadAircraft (const SGPropertyNode * arg)
147 {
148     static const SGPropertyNode *master_freeze
149         = fgGetNode("/sim/freeze/master");
150
151     bool freeze = master_freeze->getBoolValue();
152     if ( !freeze ) {
153         fgSetBool("/sim/freeze/master", true);
154     }
155
156     // TODO:
157     //    remove electrical system
158     cur_fdm_state->unbind();
159
160     // Save the selected aircraft model since restoreInitialState
161     // will obverwrite it.
162     //
163     string aircraft = fgGetString("/sim/aircraft", "");
164     globals->restoreInitialState();
165
166     fgSetString("/sim/aircraft", aircraft.c_str());
167     fgSetString("/sim/panel/path", "Aircraft/c172p/Panels/c172-vfr-panel.xml");
168
169     if ( aircraft.size() > 0 ) {
170         SGPath aircraft_path(globals->get_fg_root());
171         aircraft_path.append("Aircraft");
172         aircraft_path.append(aircraft);
173         aircraft_path.concat("-set.xml");
174         SG_LOG(SG_INPUT, SG_INFO, "Reading default aircraft: " << aircraft
175                << " from " << aircraft_path.str());
176         try {
177             readProperties(aircraft_path.str(), globals->get_props());
178         } catch (const sg_exception &e) {
179             string message = "Error reading default aircraft: ";
180             message += e.getFormattedMessage();
181             SG_LOG(SG_INPUT, SG_ALERT, message);
182             exit(2);
183         }
184     } else {
185         SG_LOG(SG_INPUT, SG_ALERT, "No default aircraft specified");
186     }
187
188     // Initialize the (new) 2D panel.
189     //
190     string panel_path = fgGetString("/sim/panel/path",
191                                     "Aircraft/c172p/Panels/c172-vfr-panel.xml");
192
193     FGPanel *panel = fgReadPanel(panel_path);
194     if (panel == 0) {
195         SG_LOG( SG_INPUT, SG_ALERT,
196                 "Error reading new panel from " << panel_path );
197     } else {
198         SG_LOG( SG_INPUT, SG_INFO, "Loaded new panel from " << panel_path );
199         globals->get_current_panel()->unbind();
200         delete globals->get_current_panel();
201         globals->set_current_panel( panel );
202         globals->get_current_panel()->init();
203         globals->get_current_panel()->bind();
204         globals->get_current_panel()->update(0);
205     }
206
207     // Load the new 3D model
208     //
209     globals->get_aircraft_model()->unbind();
210     delete globals->get_aircraft_model();
211     globals->set_aircraft_model(new FGAircraftModel);
212     globals->get_aircraft_model()->init();
213     globals->get_aircraft_model()->bind();
214
215     // TODO:
216     //    load new electrical system
217     //
218
219     // update our position based on current presets
220     fgInitPosition();
221
222     // Update the HUD
223     fgHUDInit(&current_aircraft);
224
225     SGTime *t = globals->get_time_params();
226     delete t;
227     t = fgInitTime();
228     globals->set_time_params( t );
229
230     globals->get_viewmgr()->reinit();
231     globals->get_controls()->reset_all();
232     globals->get_aircraft_model()->reinit();
233     globals->get_subsystem("xml-autopilot")->reinit();
234
235     fgReInitSubsystems();
236
237     if ( !freeze ) {
238         fgSetBool("/sim/freeze/master", false);
239     }
240
241     return true;
242 }