]> git.mxchange.org Git - flightgear.git/blob - src/Aircraft/aircraft.cxx
9b667c8f7e842dd64d4e79fa5694486c5dc8bf9a
[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  - curt@infoplane.com
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., 675 Mass Ave, Cambridge, MA 02139, USA.
20 //
21 // $Id$
22
23
24 #include <stdio.h>
25 #include <string.h>             // strdup
26
27 #include <plib/ul.h>
28 #include <plib/ssg.h>
29
30 #include <simgear/constants.h>
31 #include <simgear/debug/logstream.hxx>
32 #include <simgear/misc/sg_path.hxx>
33 #include <simgear/misc/commands.hxx>
34 #include <simgear/misc/exception.hxx>
35
36 #include <Main/globals.hxx>
37 #include <Main/fg_props.hxx>
38 #include <Main/viewmgr.hxx>
39 #include <Sound/fg_fx.hxx>
40 #include <Sound/soundmgr.hxx>
41 #include <Cockpit/panel.hxx>
42 #include <Cockpit/hud.hxx>
43 #include <Cockpit/panel_io.hxx>
44 #include <Model/acmodel.hxx>
45 #include <Autopilot/newauto.hxx>
46 #include <Main/fgfs.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 #endif
131                 SGPropertyNode *aircraft =
132                                 aircraft_types->getChild(dire->d_name, 0, true);
133 #if 0
134
135                 aircraft->setStringValue(strdup(desc->getStringValue()));
136              }
137           }
138 #endif
139       }
140    }
141
142    ulCloseDir(dirp);
143
144    globals->get_commands()->addCommand("load-aircraft", fgLoadAircraft);
145 }
146
147 bool
148 fgLoadAircraft (const SGPropertyNode * arg)
149 {
150     static const SGPropertyNode *master_freeze
151         = fgGetNode("/sim/freeze/master");
152
153     bool freeze = master_freeze->getBoolValue();
154     if ( !freeze ) {
155         fgSetBool("/sim/freeze/master", true);
156     }
157
158     // TODO:
159     //    remove electrical system
160     cur_fdm_state->unbind();
161
162     // Save the selected aircraft model since restoreInitialState
163     // will obverwrite it.
164     //
165     string aircraft = fgGetString("/sim/aircraft", "");
166     globals->restoreInitialState();
167
168     fgSetString("/sim/aircraft", aircraft.c_str());
169     fgSetString("/sim/panel/path", "Aircraft/c172/Panels/c172-vfr-panel.xml");
170
171     if ( aircraft.size() > 0 ) {
172         SGPath aircraft_path(globals->get_fg_root());
173         aircraft_path.append("Aircraft");
174         aircraft_path.append(aircraft);
175         aircraft_path.concat("-set.xml");
176         SG_LOG(SG_INPUT, SG_INFO, "Reading default aircraft: " << aircraft
177                << " from " << aircraft_path.str());
178         try {
179             readProperties(aircraft_path.str(), globals->get_props());
180         } catch (const sg_exception &e) {
181             string message = "Error reading default aircraft: ";
182             message += e.getFormattedMessage();
183             SG_LOG(SG_INPUT, SG_ALERT, message);
184             exit(2);
185         }
186     } else {
187         SG_LOG(SG_INPUT, SG_ALERT, "No default aircraft specified");
188     }
189
190     // Initialize the (new) 2D panel.
191     //
192     string panel_path = fgGetString("/sim/panel/path",
193                                     "Aircraft/c172/Panels/c172-vfr-panel.xml");
194
195     FGPanel *panel = fgReadPanel(panel_path);
196     if (panel == 0) {
197         SG_LOG( SG_INPUT, SG_ALERT,
198                 "Error reading new panel from " << panel_path );
199     } else {
200         SG_LOG( SG_INPUT, SG_INFO, "Loaded new panel from " << panel_path );
201         globals->get_current_panel()->unbind();
202         delete globals->get_current_panel();
203         globals->set_current_panel( panel );
204         globals->get_current_panel()->init();
205         globals->get_current_panel()->bind();
206         globals->get_current_panel()->update(0);
207     }
208
209     // Load the new 3D model
210     //
211     globals->get_aircraft_model()->unbind();
212     delete globals->get_aircraft_model();
213     globals->set_aircraft_model(new FGAircraftModel);
214     globals->get_aircraft_model()->init();
215     globals->get_aircraft_model()->bind();
216
217     // TODO:
218     //    load new electrical system
219     //
220
221     // update our position based on current presets
222     fgInitPosition();
223
224     // Update the HUD
225     fgHUDInit(&current_aircraft);
226
227     SGTime *t = globals->get_time_params();
228     delete t;
229     t = fgInitTime();
230     globals->set_time_params( t );
231
232     // Reinitialize some subsystems
233     //
234     globals->get_viewmgr()->reinit();
235     globals->get_controls()->reset_all();
236     globals->get_autopilot()->reset();
237     globals->get_aircraft_model()->reinit();
238     globals->get_subsystem("fx")->reinit();
239
240     fgReInitSubsystems();
241
242     if ( !freeze ) {
243         fgSetBool("/sim/freeze/master", false);
244     }
245
246     return true;
247 }