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