1 // globals.cxx -- Global state that needs to be shared among the sim modules
3 // Written by Curtis Olson, started July 2000.
5 // Copyright (C) 2000 Curtis L. Olson - http://www.flightgear.org/~curt
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.
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.
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.
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>
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>
53 #include "globals.hxx"
54 #include "renderer.hxx"
55 #include "viewmgr.hxx"
57 #include "fg_props.hxx"
60 class AircraftResourceProvider : public simgear::ResourceProvider
63 AircraftResourceProvider() :
64 simgear::ResourceProvider(simgear::ResourceManager::PRIORITY_HIGH)
68 virtual SGPath resolve(const std::string& aResource, SGPath&) const
70 string_list pieces(sgPathBranchSplit(aResource));
71 if ((pieces.size() < 3) || (pieces.front() != "Aircraft")) {
72 return SGPath(); // not an Aircraft path
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) {
86 SG_LOG(SG_IO, SG_DEBUG, "found path:" << aResource << " via /sim/aircraft-dir: " << r.str());
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) {
98 SG_LOG(SG_IO, SG_DEBUG, "found path:" << aResource << " in aircraft dir: " << *it);
101 } // of aircraft path iteration
103 return SGPath(); // not found
107 ////////////////////////////////////////////////////////////////////////
108 // Implementation of FGGlobals.
109 ////////////////////////////////////////////////////////////////////////
116 FGGlobals::FGGlobals() :
117 props( new SGPropertyNode ),
118 initial_state( NULL ),
120 renderer( new FGRenderer ),
121 subsystem_mgr( new SGSubsystemMgr ),
122 event_mgr( new SGEventMgr ),
123 soundmgr( new SGSoundMgr ),
131 current_panel( NULL ),
135 commands( SGCommandMgr::instance() ),
138 channel_options_list( NULL ),
139 initial_waypoints( NULL ),
142 fontcache ( new FGFontCache ),
151 simgear::ResourceManager::instance()->addProvider(new AircraftResourceProvider());
152 simgear::PropertyObjectBase::setDefaultRoot(props);
157 FGGlobals::~FGGlobals()
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");
174 subsystem_mgr->shutdown();
175 subsystem_mgr->unbind();
176 delete subsystem_mgr;
182 delete current_panel;
188 delete channel_options_list;
189 delete initial_waypoints;
206 // set the fg_root path
207 void FGGlobals::set_fg_root (const string &root) {
210 // append /data to root if it exists
211 SGPath tmp( fg_root );
212 tmp.append( "data" );
213 tmp.append( "version" );
214 if ( ulFileExists( tmp.c_str() ) ) {
215 fgGetNode("BAD_FG_ROOT", true)->setStringValue(fg_root);
217 fgGetNode("GOOD_FG_ROOT", true)->setStringValue(fg_root);
218 SG_LOG(SG_GENERAL, SG_ALERT, "***\n***\n*** Warning: changing bad FG_ROOT/--fg-root to '"
219 << fg_root << "'\n***\n***");
222 // remove /sim/fg-root before writing to prevent hijacking
223 SGPropertyNode *n = fgGetNode("/sim", true);
224 n->removeChild("fg-root", 0, false);
225 n = n->getChild("fg-root", 0, true);
226 n->setStringValue(fg_root.c_str());
227 n->setAttribute(SGPropertyNode::WRITE, false);
229 simgear::ResourceManager::instance()->addBasePath(fg_root,
230 simgear::ResourceManager::PRIORITY_DEFAULT);
233 void FGGlobals::set_fg_scenery (const string &scenery)
236 if (scenery.empty()) {
238 s.append( "Scenery" );
242 string_list path_list = sgPathSplit( s.str() );
244 SGPropertyNode* sim = fgGetNode("/sim", true);
246 for (unsigned i = 0; i < path_list.size(); i++) {
247 SGPath path(path_list[i]);
248 if (!path.exists()) {
249 SG_LOG(SG_GENERAL, SG_WARN, "scenery path not found:" << path.str());
253 simgear::Dir dir(path);
254 SGPath terrainDir(dir.file("Terrain"));
255 SGPath objectsDir(dir.file("Objects"));
257 // this code used to add *either* the base dir, OR add the
258 // Terrain and Objects subdirs, but the conditional logic was commented
259 // out, such that all three dirs are added. Unfortunately there's
260 // no information as to why the change was made.
261 fg_scenery.push_back(path.str());
263 if (terrainDir.exists()) {
264 fg_scenery.push_back(terrainDir.str());
267 if (objectsDir.exists()) {
268 fg_scenery.push_back(objectsDir.str());
271 // insert a marker for FGTileEntry::load(), so that
272 // FG_SCENERY=A:B becomes list ["A/Terrain", "A/Objects", "",
273 // "B/Terrain", "B/Objects", ""]
274 fg_scenery.push_back("");
276 // make scenery dirs available to Nasal
277 sim->removeChild("fg-scenery", i, false);
278 SGPropertyNode* n = sim->getChild("fg-scenery", i, true);
279 n->setStringValue(path.str());
280 n->setAttribute(SGPropertyNode::WRITE, false);
281 } // of path list iteration
284 void FGGlobals::append_aircraft_path(const std::string& path)
286 SGPath dirPath(path);
287 if (!dirPath.exists()) {
288 SG_LOG(SG_GENERAL, SG_WARN, "aircraft path not found:" << path);
292 unsigned int index = fg_aircraft_dirs.size();
293 fg_aircraft_dirs.push_back(path);
295 // make aircraft dirs available to Nasal
296 SGPropertyNode* sim = fgGetNode("/sim", true);
297 sim->removeChild("fg-aircraft", index, false);
298 SGPropertyNode* n = sim->getChild("fg-aircraft", index, true);
299 n->setStringValue(path);
300 n->setAttribute(SGPropertyNode::WRITE, false);
303 void FGGlobals::append_aircraft_paths(const std::string& path)
305 string_list paths = sgPathSplit(path);
306 for (unsigned int p = 0; p<paths.size(); ++p) {
307 append_aircraft_path(paths[p]);
311 SGPath FGGlobals::resolve_aircraft_path(const std::string& branch) const
313 return simgear::ResourceManager::instance()->findPath(branch);
316 SGPath FGGlobals::resolve_maybe_aircraft_path(const std::string& branch) const
318 return simgear::ResourceManager::instance()->findPath(branch);
322 FGGlobals::get_renderer () const
328 FGGlobals::get_subsystem_mgr () const
330 return subsystem_mgr;
334 FGGlobals::get_subsystem (const char * name)
336 return subsystem_mgr->get_subsystem(name);
340 FGGlobals::add_subsystem (const char * name,
341 SGSubsystem * subsystem,
342 SGSubsystemMgr::GroupType type,
345 subsystem_mgr->add(name, subsystem, type, min_time_sec);
349 FGGlobals::get_soundmgr () const
355 FGGlobals::get_event_mgr () const
361 // Save the current state as the initial state.
363 FGGlobals::saveInitialState ()
365 initial_state = new SGPropertyNode();
367 // copy properties which are READ/WRITEable - but not USERARCHIVEd or PRESERVEd
368 int checked = SGPropertyNode::READ+SGPropertyNode::WRITE+
369 SGPropertyNode::USERARCHIVE+SGPropertyNode::PRESERVE;
370 int expected = SGPropertyNode::READ+SGPropertyNode::WRITE;
371 if (!copyProperties(props, initial_state, expected, checked))
372 SG_LOG(SG_GENERAL, SG_ALERT, "Error saving initial state");
374 // delete various properties from the initial state, since we want to
375 // preserve their values even if doing a restore
376 // => Properties should now use the PRESERVE flag to protect their values
377 // on sim-reset. Remove some specific properties for backward compatibility.
378 SGPropertyNode* sim = initial_state->getChild("sim");
379 SGPropertyNode* cameraGroupNode = sim->getNode("rendering/camera-group");
380 if (cameraGroupNode) {
381 cameraGroupNode->removeChild("camera");
382 cameraGroupNode->removeChild("gui");
387 // Restore the saved initial state, if any
389 FGGlobals::restoreInitialState ()
391 if ( initial_state == 0 ) {
392 SG_LOG(SG_GENERAL, SG_ALERT,
393 "No initial state available to restore!!!");
396 // copy properties which are READ/WRITEable - but not USERARCHIVEd or PRESERVEd
397 int checked = SGPropertyNode::READ+SGPropertyNode::WRITE+
398 SGPropertyNode::USERARCHIVE+SGPropertyNode::PRESERVE;
399 int expected = SGPropertyNode::READ+SGPropertyNode::WRITE;
400 if ( copyProperties(initial_state, props, expected, checked)) {
401 SG_LOG( SG_GENERAL, SG_INFO, "Initial state restored successfully" );
403 SG_LOG( SG_GENERAL, SG_INFO,
404 "Some errors restoring initial state (read-only props?)" );
410 FGGlobals::get_current_view () const
412 return viewmgr->get_current_view();
415 long int FGGlobals::get_warp() const
417 return fgGetInt("/sim/time/warp");
420 void FGGlobals::set_warp( long int w )
422 fgSetInt("/sim/time/warp", w);
425 long int FGGlobals::get_warp_delta() const
427 return fgGetInt("/sim/time/warp-delta");
430 void FGGlobals::set_warp_delta( long int d )
432 fgSetInt("/sim/time/warp-delta", d);
435 // end of globals.cxx