]> 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
39 #include <Aircraft/controls.hxx>
40 #include <Airports/runways.hxx>
41 #include <ATCDCL/ATCmgr.hxx>
42 #include <Autopilot/route_mgr.hxx>
43 #include <Cockpit/panel.hxx>
44 #include <GUI/new_gui.hxx>
45 #include <Model/acmodel.hxx>
46 #include <Model/modelmgr.hxx>
47 #include <MultiPlayer/multiplaymgr.hxx>
48 #include <Scenery/scenery.hxx>
49 #include <Scenery/tilemgr.hxx>
50 #include <Navaids/navlist.hxx>
51
52 #include "globals.hxx"
53 #include "renderer.hxx"
54 #include "viewmgr.hxx"
55
56 #include "fg_props.hxx"
57 #include "fg_io.hxx"
58
59 \fclass AircraftResourceProvider : public simgear::ResourceProvider
60 {
61 public:
62   AircraftResourceProvider() :
63     simgear::ResourceProvider(simgear::ResourceManager::PRIORITY_HIGH)
64   {
65   }
66   
67   virtual SGPath resolve(const std::string& aResource, SGPath&) const
68   {
69     string_list pieces(sgPathBranchSplit(aResource));
70     if ((pieces.size() < 3) || (pieces.front() != "Aircraft")) {
71       return SGPath(); // not an Aircraft path
72     }
73     
74   // test against the aircraft-dir property
75     const char* aircraftDir = fgGetString("/sim/aircraft-dir");
76     string_list aircraftDirPieces(sgPathBranchSplit(aircraftDir));
77     if (aircraftDirPieces.empty() || (aircraftDirPieces.back() != pieces[1])) {
78       return SGPath(); // current aircraft-dir does not match resource aircraft
79     }
80     
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_INFO, "found path:" << aResource << " via /sim/aircraft-dir: " << r.str());
88       return r;
89     }
90   
91   // try each aircaft 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_INFO, "found path:" << aResource << " in aircraft dir: " << r.str());
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     warp( 0 ),
127     warp_delta( 0 ),
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 }
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     ai->unbind();
171     delete ai;
172     
173     subsystem_mgr->unbind();
174     delete subsystem_mgr;
175     
176     delete time_params;
177     delete mag;
178     delete matlib;
179     delete route_mgr;
180     delete current_panel;
181
182     delete ATC_mgr;
183     delete controls;
184
185     delete channel_options_list;
186     delete initial_waypoints;
187     delete scenery;
188     delete fontcache;
189
190     delete navlist;
191     delete loclist;
192     delete gslist;
193     delete dmelist;
194     delete tacanlist;
195     delete carrierlist;
196     delete channellist;
197
198     soundmgr->unbind();
199     delete soundmgr;
200 }
201
202
203 // set the fg_root path
204 void FGGlobals::set_fg_root (const string &root) {
205     fg_root = root;
206
207     // append /data to root if it exists
208     SGPath tmp( fg_root );
209     tmp.append( "data" );
210     tmp.append( "version" );
211     if ( ulFileExists( tmp.c_str() ) ) {
212         fgGetNode("BAD_FG_ROOT", true)->setStringValue(fg_root);
213         fg_root += "/data";
214         fgGetNode("GOOD_FG_ROOT", true)->setStringValue(fg_root);
215         SG_LOG(SG_GENERAL, SG_ALERT, "***\n***\n*** Warning: changing bad FG_ROOT/--fg-root to '"
216                 << fg_root << "'\n***\n***");
217     }
218
219     // remove /sim/fg-root before writing to prevent hijacking
220     SGPropertyNode *n = fgGetNode("/sim", true);
221     n->removeChild("fg-root", 0, false);
222     n = n->getChild("fg-root", 0, true);
223     n->setStringValue(fg_root.c_str());
224     n->setAttribute(SGPropertyNode::WRITE, false);
225     
226     simgear::ResourceManager::instance()->addBasePath(fg_root, 
227       simgear::ResourceManager::PRIORITY_DEFAULT);
228 }
229
230 void FGGlobals::set_fg_scenery (const string &scenery)
231 {
232     SGPath s;
233     if (scenery.empty()) {
234         s.set( fg_root );
235         s.append( "Scenery" );
236     } else
237         s.set( scenery );
238
239     string_list path_list = sgPathSplit( s.str() );
240     fg_scenery.clear();
241
242     for (unsigned i = 0; i < path_list.size(); i++) {
243         SGPath path(path_list[i]);
244         if (!path.exists()) {
245           SG_LOG(SG_GENERAL, SG_WARN, "scenery path not found:" << path.str());
246           continue;
247         }
248
249         simgear::Dir dir(path);
250         SGPath terrainDir(dir.file("Terrain"));
251         SGPath objectsDir(dir.file("Objects"));
252         
253       // this code used to add *either* the base dir, OR add the 
254       // Terrain and Objects subdirs, but the conditional logic was commented
255       // out, such that all three dirs are added. Unfortunately there's
256       // no information as to why the change was made.
257         fg_scenery.push_back(path.str());
258         
259         if (terrainDir.exists()) {
260           fg_scenery.push_back(terrainDir.str());
261         }
262         
263         if (objectsDir.exists()) {
264           fg_scenery.push_back(objectsDir.str());
265         }
266         
267         // insert a marker for FGTileEntry::load(), so that
268         // FG_SCENERY=A:B becomes list ["A/Terrain", "A/Objects", "",
269         // "B/Terrain", "B/Objects", ""]
270         fg_scenery.push_back("");
271     } // of path list iteration
272 }
273
274 void FGGlobals::append_aircraft_path(const std::string& path)
275 {
276   SGPath dirPath(path);
277   if (!dirPath.exists()) {
278     SG_LOG(SG_GENERAL, SG_WARN, "aircraft path not found:" << path);
279     return;
280   }
281   
282   unsigned int index = fg_aircraft_dirs.size();  
283   fg_aircraft_dirs.push_back(path);
284   
285 // make aircraft dirs available to Nasal
286   SGPropertyNode* sim = fgGetNode("/sim", true);
287   sim->removeChild("fg-aircraft", index, false);
288   SGPropertyNode* n = sim->getChild("fg-aircraft", index, true);
289   n->setStringValue(path);
290   n->setAttribute(SGPropertyNode::WRITE, false);
291 }
292
293 void FGGlobals::append_aircraft_paths(const std::string& path)
294 {
295   string_list paths = sgPathSplit(path);
296   for (unsigned int p = 0; p<paths.size(); ++p) {
297     append_aircraft_path(paths[p]);
298   }
299 }
300
301 SGPath FGGlobals::resolve_aircraft_path(const std::string& branch) const
302 {
303   return simgear::ResourceManager::instance()->findPath(branch);
304 }
305
306 SGPath FGGlobals::resolve_maybe_aircraft_path(const std::string& branch) const
307 {
308   return simgear::ResourceManager::instance()->findPath(branch);
309 }
310
311 FGRenderer *
312 FGGlobals::get_renderer () const
313 {
314    return renderer;
315 }
316
317 SGSubsystemMgr *
318 FGGlobals::get_subsystem_mgr () const
319 {
320     return subsystem_mgr;
321 }
322
323 SGSubsystem *
324 FGGlobals::get_subsystem (const char * name)
325 {
326     return subsystem_mgr->get_subsystem(name);
327 }
328
329 void
330 FGGlobals::add_subsystem (const char * name,
331                           SGSubsystem * subsystem,
332                           SGSubsystemMgr::GroupType type,
333                           double min_time_sec)
334 {
335     subsystem_mgr->add(name, subsystem, type, min_time_sec);
336 }
337
338 SGSoundMgr *
339 FGGlobals::get_soundmgr () const
340 {
341     return soundmgr;
342 }
343
344 SGEventMgr *
345 FGGlobals::get_event_mgr () const
346 {
347     return event_mgr;
348 }
349
350
351 // Save the current state as the initial state.
352 void
353 FGGlobals::saveInitialState ()
354 {
355   initial_state = new SGPropertyNode();
356
357   if (!copyProperties(props, initial_state))
358     SG_LOG(SG_GENERAL, SG_ALERT, "Error saving initial state");
359     
360   // delete various properties from the initial state, since we want to
361   // preserve their values even if doing a restore
362   
363   SGPropertyNode* sim = initial_state->getChild("sim");
364   sim->removeChild("presets");
365   SGPropertyNode* simStartup = sim->getChild("startup");
366   simStartup->removeChild("xsize");
367   simStartup->removeChild("ysize");
368   
369   SGPropertyNode* cameraGroupNode = sim->getNode("rendering/camera-group");
370   if (cameraGroupNode) {
371     cameraGroupNode->removeChild("camera");
372     cameraGroupNode->removeChild("gui");
373   }
374 }
375
376
377 // Restore the saved initial state, if any
378 void
379 FGGlobals::restoreInitialState ()
380 {
381     if ( initial_state == 0 ) {
382         SG_LOG(SG_GENERAL, SG_ALERT,
383                "No initial state available to restore!!!");
384         return;
385     }
386
387     if ( copyProperties(initial_state, props) ) {
388         SG_LOG( SG_GENERAL, SG_INFO, "Initial state restored successfully" );
389     } else {
390         SG_LOG( SG_GENERAL, SG_INFO,
391                 "Some errors restoring initial state (read-only props?)" );
392     }
393
394 }
395
396 FGViewer *
397 FGGlobals::get_current_view () const
398 {
399   return viewmgr->get_current_view();
400 }
401
402 // end of globals.cxx