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