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