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