]> git.mxchange.org Git - flightgear.git/blob - src/Main/globals.cxx
Merge commit 'refs/merge-requests/11' of git://gitorious.org/fg/flightgear into next
[flightgear.git] / src / Main / globals.cxx
1 // globals.cxx -- Global state that needs to be shared among the sim modules
2 //
3 // Written by Curtis Olson, started July 2000.
4 //
5 // Copyright (C) 2000  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 Foundation,
19 // 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 <simgear/structure/commands.hxx>
28 #include <simgear/misc/sg_path.hxx>
29 #include <simgear/misc/sg_dir.hxx>
30 #include <simgear/timing/sg_time.hxx>
31 #include <simgear/ephemeris/ephemeris.hxx>
32 #include <simgear/magvar/magvar.hxx>
33 #include <simgear/scene/material/matlib.hxx>
34 #include <simgear/structure/subsystem_mgr.hxx>
35 #include <simgear/structure/event_mgr.hxx>
36 #include <simgear/sound/soundmgr_openal.hxx>
37 #include <simgear/misc/ResourceManager.hxx>
38 #include <simgear/props/propertyObject.hxx>
39 #include <simgear/props/props_io.hxx>
40 #include <simgear/scene/model/placement.hxx>
41
42 #include <Aircraft/controls.hxx>
43 #include <Airports/runways.hxx>
44 #include <ATCDCL/ATCmgr.hxx>
45 #include <Autopilot/route_mgr.hxx>
46 #include <Cockpit/panel.hxx>
47 #include <GUI/new_gui.hxx>
48 #include <Model/acmodel.hxx>
49 #include <Model/modelmgr.hxx>
50 #include <MultiPlayer/multiplaymgr.hxx>
51 #include <Scenery/scenery.hxx>
52 #include <Scenery/tilemgr.hxx>
53 #include <Navaids/navlist.hxx>
54
55 #include "globals.hxx"
56 #include "renderer.hxx"
57 #include "viewmgr.hxx"
58
59 #include "fg_props.hxx"
60 #include "fg_io.hxx"
61
62 class AircraftResourceProvider : public simgear::ResourceProvider
63 {
64 public:
65   AircraftResourceProvider() :
66     simgear::ResourceProvider(simgear::ResourceManager::PRIORITY_HIGH)
67   {
68   }
69   
70   virtual SGPath resolve(const std::string& aResource, SGPath&) const
71   {
72     string_list pieces(sgPathBranchSplit(aResource));
73     if ((pieces.size() < 3) || (pieces.front() != "Aircraft")) {
74       return SGPath(); // not an Aircraft path
75     }
76     
77   // test against the aircraft-dir property
78     const char* aircraftDir = fgGetString("/sim/aircraft-dir");
79     string_list aircraftDirPieces(sgPathBranchSplit(aircraftDir));
80     if (!aircraftDirPieces.empty() && (aircraftDirPieces.back() == pieces[1])) {
81         // current aircraft-dir matches resource aircraft
82         SGPath r(aircraftDir);
83         for (unsigned int i=2; i<pieces.size(); ++i) {
84           r.append(pieces[i]);
85         }
86         
87         if (r.exists()) {
88           SG_LOG(SG_IO, SG_DEBUG, "found path:" << aResource << " via /sim/aircraft-dir: " << r.str());
89           return r;
90         }
91     }
92   
93   // try each aircraft dir in turn
94     std::string res(aResource, 9); // resource path with 'Aircraft/' removed
95     const string_list& dirs(globals->get_aircraft_paths());
96     string_list::const_iterator it = dirs.begin();
97     for (; it != dirs.end(); ++it) {
98       SGPath p(*it, res);
99       if (p.exists()) {
100         SG_LOG(SG_IO, SG_DEBUG, "found path:" << aResource << " in aircraft dir: " << *it);
101         return p;
102       }
103     } // of aircraft path iteration
104     
105     return SGPath(); // not found
106   }
107 };
108
109 ////////////////////////////////////////////////////////////////////////
110 // Implementation of FGGlobals.
111 ////////////////////////////////////////////////////////////////////////
112
113 // global global :-)
114 FGGlobals *globals;
115
116
117 // Constructor
118 FGGlobals::FGGlobals() :
119     props( new SGPropertyNode ),
120     initial_state( NULL ),
121     locale( NULL ),
122     renderer( new FGRenderer ),
123     subsystem_mgr( new SGSubsystemMgr ),
124     event_mgr( new SGEventMgr ),
125     soundmgr( new SGSoundMgr ),
126     sim_time_sec( 0.0 ),
127     fg_root( "" ),
128     time_params( NULL ),
129     ephem( NULL ),
130     mag( NULL ),
131     matlib( NULL ),
132     route_mgr( NULL ),
133     current_panel( NULL ),
134     ATC_mgr( NULL ),
135     controls( NULL ),
136     viewmgr( NULL ),
137     commands( SGCommandMgr::instance() ),
138     acmodel( NULL ),
139     model_mgr( NULL ),
140     channel_options_list( NULL ),
141     initial_waypoints( NULL ),
142     scenery( NULL ),
143     tile_mgr( NULL ),
144     fontcache ( new FGFontCache ),
145     navlist( NULL ),
146     loclist( NULL ),
147     gslist( NULL ),
148     dmelist( NULL ),
149     tacanlist( NULL ),
150     carrierlist( NULL ),
151     channellist( NULL )    
152 {
153   simgear::ResourceManager::instance()->addProvider(new AircraftResourceProvider());
154   simgear::PropertyObjectBase::setDefaultRoot(props);
155 }
156
157
158 // Destructor
159 FGGlobals::~FGGlobals() 
160 {
161     delete renderer;
162     renderer = NULL;
163     
164 // The AIModels manager performs a number of actions upon
165     // Shutdown that implicitly assume that other subsystems
166     // are still operational (Due to the dynamic allocation and
167     // deallocation of AIModel objects. To ensure we can safely
168     // shut down all subsystems, make sure we take down the 
169     // AIModels system first.
170     SGSubsystem* ai = subsystem_mgr->remove("ai_model");
171     if (ai) {
172         ai->unbind();
173         delete ai;
174     }
175     
176     subsystem_mgr->shutdown();
177     subsystem_mgr->unbind();
178     delete subsystem_mgr;
179     
180     delete time_params;
181     delete mag;
182     delete matlib;
183     delete route_mgr;
184     delete current_panel;
185
186     delete ATC_mgr;
187
188     if (controls)
189     {
190         controls->unbind();
191         delete controls;
192     }
193
194     delete channel_options_list;
195     delete initial_waypoints;
196     delete scenery;
197     delete fontcache;
198
199     delete navlist;
200     delete loclist;
201     delete gslist;
202     delete dmelist;
203     delete tacanlist;
204     delete carrierlist;
205     delete channellist;
206
207     soundmgr->unbind();
208     delete soundmgr;
209 }
210
211
212 // set the fg_root path
213 void FGGlobals::set_fg_root (const string &root) {
214     fg_root = root;
215
216     // append /data to root if it exists
217     SGPath tmp( fg_root );
218     tmp.append( "data" );
219     tmp.append( "version" );
220     if ( ulFileExists( tmp.c_str() ) ) {
221         fgGetNode("BAD_FG_ROOT", true)->setStringValue(fg_root);
222         fg_root += "/data";
223         fgGetNode("GOOD_FG_ROOT", true)->setStringValue(fg_root);
224         SG_LOG(SG_GENERAL, SG_ALERT, "***\n***\n*** Warning: changing bad FG_ROOT/--fg-root to '"
225                 << fg_root << "'\n***\n***");
226     }
227
228     // remove /sim/fg-root before writing to prevent hijacking
229     SGPropertyNode *n = fgGetNode("/sim", true);
230     n->removeChild("fg-root", 0, false);
231     n = n->getChild("fg-root", 0, true);
232     n->setStringValue(fg_root.c_str());
233     n->setAttribute(SGPropertyNode::WRITE, false);
234     
235     simgear::ResourceManager::instance()->addBasePath(fg_root, 
236       simgear::ResourceManager::PRIORITY_DEFAULT);
237 }
238
239 void FGGlobals::set_fg_scenery (const string &scenery)
240 {
241     SGPath s;
242     if (scenery.empty()) {
243         s.set( fg_root );
244         s.append( "Scenery" );
245     } else
246         s.set( scenery );
247
248     string_list path_list = sgPathSplit( s.str() );
249     fg_scenery.clear();
250     SGPropertyNode* sim = fgGetNode("/sim", true);
251     
252     for (unsigned i = 0; i < path_list.size(); i++) {
253         SGPath path(path_list[i]);
254         if (!path.exists()) {
255           SG_LOG(SG_GENERAL, SG_WARN, "scenery path not found:" << path.str());
256           continue;
257         }
258
259         simgear::Dir dir(path);
260         SGPath terrainDir(dir.file("Terrain"));
261         SGPath objectsDir(dir.file("Objects"));
262         
263       // this code used to add *either* the base dir, OR add the 
264       // Terrain and Objects subdirs, but the conditional logic was commented
265       // out, such that all three dirs are added. Unfortunately there's
266       // no information as to why the change was made.
267         fg_scenery.push_back(path.str());
268         
269         if (terrainDir.exists()) {
270           fg_scenery.push_back(terrainDir.str());
271         }
272         
273         if (objectsDir.exists()) {
274           fg_scenery.push_back(objectsDir.str());
275         }
276         
277         // insert a marker for FGTileEntry::load(), so that
278         // FG_SCENERY=A:B becomes list ["A/Terrain", "A/Objects", "",
279         // "B/Terrain", "B/Objects", ""]
280         fg_scenery.push_back("");
281         
282       // make scenery dirs available to Nasal
283         sim->removeChild("fg-scenery", i, false);
284         SGPropertyNode* n = sim->getChild("fg-scenery", i, true);
285         n->setStringValue(path.str());
286         n->setAttribute(SGPropertyNode::WRITE, false);
287     } // of path list iteration
288 }
289
290 void FGGlobals::append_aircraft_path(const std::string& path)
291 {
292   SGPath dirPath(path);
293   if (!dirPath.exists()) {
294     SG_LOG(SG_GENERAL, SG_WARN, "aircraft path not found:" << path);
295     return;
296   }
297   
298   unsigned int index = fg_aircraft_dirs.size();  
299   fg_aircraft_dirs.push_back(path);
300   
301 // make aircraft dirs available to Nasal
302   SGPropertyNode* sim = fgGetNode("/sim", true);
303   sim->removeChild("fg-aircraft", index, false);
304   SGPropertyNode* n = sim->getChild("fg-aircraft", index, true);
305   n->setStringValue(path);
306   n->setAttribute(SGPropertyNode::WRITE, false);
307 }
308
309 void FGGlobals::append_aircraft_paths(const std::string& path)
310 {
311   string_list paths = sgPathSplit(path);
312   for (unsigned int p = 0; p<paths.size(); ++p) {
313     append_aircraft_path(paths[p]);
314   }
315 }
316
317 SGPath FGGlobals::resolve_aircraft_path(const std::string& branch) const
318 {
319   return simgear::ResourceManager::instance()->findPath(branch);
320 }
321
322 SGPath FGGlobals::resolve_maybe_aircraft_path(const std::string& branch) const
323 {
324   return simgear::ResourceManager::instance()->findPath(branch);
325 }
326
327 FGRenderer *
328 FGGlobals::get_renderer () const
329 {
330    return renderer;
331 }
332
333 SGSubsystemMgr *
334 FGGlobals::get_subsystem_mgr () const
335 {
336     return subsystem_mgr;
337 }
338
339 SGSubsystem *
340 FGGlobals::get_subsystem (const char * name)
341 {
342     return subsystem_mgr->get_subsystem(name);
343 }
344
345 void
346 FGGlobals::add_subsystem (const char * name,
347                           SGSubsystem * subsystem,
348                           SGSubsystemMgr::GroupType type,
349                           double min_time_sec)
350 {
351     subsystem_mgr->add(name, subsystem, type, min_time_sec);
352 }
353
354 SGSoundMgr *
355 FGGlobals::get_soundmgr () const
356 {
357     return soundmgr;
358 }
359
360 SGEventMgr *
361 FGGlobals::get_event_mgr () const
362 {
363     return event_mgr;
364 }
365
366 const SGGeod &
367 FGGlobals::get_aircraft_position() const
368 {
369     if( acmodel != NULL ) {
370         SGModelPlacement * mp = acmodel->get3DModel();
371         if( mp != NULL )
372             return mp->getPosition();
373     }
374     throw sg_exception("Can't get aircraft position", "FGGlobals::get_aircraft_position()" );
375 }
376
377
378
379 // Save the current state as the initial state.
380 void
381 FGGlobals::saveInitialState ()
382 {
383   initial_state = new SGPropertyNode();
384
385   // copy properties which are READ/WRITEable - but not USERARCHIVEd or PRESERVEd
386   int checked  = SGPropertyNode::READ+SGPropertyNode::WRITE+
387                  SGPropertyNode::USERARCHIVE+SGPropertyNode::PRESERVE;
388   int expected = SGPropertyNode::READ+SGPropertyNode::WRITE;
389   if (!copyProperties(props, initial_state, expected, checked))
390     SG_LOG(SG_GENERAL, SG_ALERT, "Error saving initial state");
391     
392   // delete various properties from the initial state, since we want to
393   // preserve their values even if doing a restore
394   // => Properties should now use the PRESERVE flag to protect their values
395   // on sim-reset. Remove some specific properties for backward compatibility.
396   SGPropertyNode* sim = initial_state->getChild("sim");
397   SGPropertyNode* cameraGroupNode = sim->getNode("rendering/camera-group");
398   if (cameraGroupNode) {
399     cameraGroupNode->removeChild("camera");
400     cameraGroupNode->removeChild("gui");
401   }
402 }
403
404
405 // Restore the saved initial state, if any
406 void
407 FGGlobals::restoreInitialState ()
408 {
409     if ( initial_state == 0 ) {
410         SG_LOG(SG_GENERAL, SG_ALERT,
411                "No initial state available to restore!!!");
412         return;
413     }
414     // copy properties which are READ/WRITEable - but not USERARCHIVEd or PRESERVEd
415     int checked  = SGPropertyNode::READ+SGPropertyNode::WRITE+
416                    SGPropertyNode::USERARCHIVE+SGPropertyNode::PRESERVE;
417     int expected = SGPropertyNode::READ+SGPropertyNode::WRITE;
418     if ( copyProperties(initial_state, props, expected, checked)) {
419         SG_LOG( SG_GENERAL, SG_INFO, "Initial state restored successfully" );
420     } else {
421         SG_LOG( SG_GENERAL, SG_INFO,
422                 "Some errors restoring initial state (read-only props?)" );
423     }
424
425 }
426
427 FGViewer *
428 FGGlobals::get_current_view () const
429 {
430   return viewmgr->get_current_view();
431 }
432
433 long int FGGlobals::get_warp() const
434 {
435   return fgGetInt("/sim/time/warp");
436 }
437
438 void FGGlobals::set_warp( long int w )
439 {
440   fgSetInt("/sim/time/warp", w);
441 }
442
443 long int FGGlobals::get_warp_delta() const
444 {
445   return fgGetInt("/sim/time/warp-delta");
446 }
447
448 void FGGlobals::set_warp_delta( long int d )
449 {
450   fgSetInt("/sim/time/warp-delta", d);
451 }
452     
453 // end of globals.cxx