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