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>
39 #include <simgear/props/props_io.hxx>
40 #include <simgear/scene/model/placement.hxx>
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>
55 #include "globals.hxx"
56 #include "renderer.hxx"
57 #include "viewmgr.hxx"
59 #include "fg_props.hxx"
62 class AircraftResourceProvider : public simgear::ResourceProvider
65 AircraftResourceProvider() :
66 simgear::ResourceProvider(simgear::ResourceManager::PRIORITY_HIGH)
70 virtual SGPath resolve(const std::string& aResource, SGPath&) const
72 string_list pieces(sgPathBranchSplit(aResource));
73 if ((pieces.size() < 3) || (pieces.front() != "Aircraft")) {
74 return SGPath(); // not an Aircraft path
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) {
88 SG_LOG(SG_IO, SG_DEBUG, "found path:" << aResource << " via /sim/aircraft-dir: " << r.str());
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) {
100 SG_LOG(SG_IO, SG_DEBUG, "found path:" << aResource << " in aircraft dir: " << *it);
103 } // of aircraft path iteration
105 return SGPath(); // not found
109 ////////////////////////////////////////////////////////////////////////
110 // Implementation of FGGlobals.
111 ////////////////////////////////////////////////////////////////////////
118 FGGlobals::FGGlobals() :
119 props( new SGPropertyNode ),
120 initial_state( NULL ),
122 renderer( new FGRenderer ),
123 subsystem_mgr( new SGSubsystemMgr ),
124 event_mgr( new SGEventMgr ),
125 soundmgr( new SGSoundMgr ),
133 current_panel( NULL ),
137 commands( SGCommandMgr::instance() ),
140 channel_options_list( NULL ),
141 initial_waypoints( NULL ),
144 fontcache ( new FGFontCache ),
153 simgear::ResourceManager::instance()->addProvider(new AircraftResourceProvider());
154 simgear::PropertyObjectBase::setDefaultRoot(props);
159 FGGlobals::~FGGlobals()
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");
176 subsystem_mgr->shutdown();
177 subsystem_mgr->unbind();
178 delete subsystem_mgr;
184 delete current_panel;
194 delete channel_options_list;
195 delete initial_waypoints;
212 // set the fg_root path
213 void FGGlobals::set_fg_root (const string &root) {
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);
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***");
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);
235 simgear::ResourceManager::instance()->addBasePath(fg_root,
236 simgear::ResourceManager::PRIORITY_DEFAULT);
239 void FGGlobals::set_fg_scenery (const string &scenery)
242 if (scenery.empty()) {
244 s.append( "Scenery" );
248 string_list path_list = sgPathSplit( s.str() );
250 SGPropertyNode* sim = fgGetNode("/sim", true);
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());
259 simgear::Dir dir(path);
260 SGPath terrainDir(dir.file("Terrain"));
261 SGPath objectsDir(dir.file("Objects"));
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());
269 if (terrainDir.exists()) {
270 fg_scenery.push_back(terrainDir.str());
273 if (objectsDir.exists()) {
274 fg_scenery.push_back(objectsDir.str());
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("");
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
290 void FGGlobals::append_aircraft_path(const std::string& path)
292 SGPath dirPath(path);
293 if (!dirPath.exists()) {
294 SG_LOG(SG_GENERAL, SG_WARN, "aircraft path not found:" << path);
298 unsigned int index = fg_aircraft_dirs.size();
299 fg_aircraft_dirs.push_back(path);
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);
309 void FGGlobals::append_aircraft_paths(const std::string& path)
311 string_list paths = sgPathSplit(path);
312 for (unsigned int p = 0; p<paths.size(); ++p) {
313 append_aircraft_path(paths[p]);
317 SGPath FGGlobals::resolve_aircraft_path(const std::string& branch) const
319 return simgear::ResourceManager::instance()->findPath(branch);
322 SGPath FGGlobals::resolve_maybe_aircraft_path(const std::string& branch) const
324 return simgear::ResourceManager::instance()->findPath(branch);
328 FGGlobals::get_renderer () const
334 FGGlobals::get_subsystem_mgr () const
336 return subsystem_mgr;
340 FGGlobals::get_subsystem (const char * name)
342 return subsystem_mgr->get_subsystem(name);
346 FGGlobals::add_subsystem (const char * name,
347 SGSubsystem * subsystem,
348 SGSubsystemMgr::GroupType type,
351 subsystem_mgr->add(name, subsystem, type, min_time_sec);
355 FGGlobals::get_soundmgr () const
361 FGGlobals::get_event_mgr () const
367 FGGlobals::get_aircraft_position() const
369 if( acmodel != NULL ) {
370 SGModelPlacement * mp = acmodel->get3DModel();
372 return mp->getPosition();
374 throw sg_exception("Can't get aircraft position", "FGGlobals::get_aircraft_position()" );
379 // Save the current state as the initial state.
381 FGGlobals::saveInitialState ()
383 initial_state = new SGPropertyNode();
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");
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");
405 // Restore the saved initial state, if any
407 FGGlobals::restoreInitialState ()
409 if ( initial_state == 0 ) {
410 SG_LOG(SG_GENERAL, SG_ALERT,
411 "No initial state available to restore!!!");
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" );
421 SG_LOG( SG_GENERAL, SG_INFO,
422 "Some errors restoring initial state (read-only props?)" );
428 FGGlobals::get_current_view () const
430 return viewmgr->get_current_view();
433 long int FGGlobals::get_warp() const
435 return fgGetInt("/sim/time/warp");
438 void FGGlobals::set_warp( long int w )
440 fgSetInt("/sim/time/warp", w);
443 long int FGGlobals::get_warp_delta() const
445 return fgGetInt("/sim/time/warp-delta");
448 void FGGlobals::set_warp_delta( long int d )
450 fgSetInt("/sim/time/warp-delta", d);
453 // end of globals.cxx