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 <boost/foreach.hpp>
30 #include <simgear/structure/commands.hxx>
31 #include <simgear/misc/sg_path.hxx>
32 #include <simgear/misc/sg_dir.hxx>
33 #include <simgear/timing/sg_time.hxx>
34 #include <simgear/ephemeris/ephemeris.hxx>
35 #include <simgear/magvar/magvar.hxx>
36 #include <simgear/scene/material/matlib.hxx>
37 #include <simgear/structure/subsystem_mgr.hxx>
38 #include <simgear/structure/event_mgr.hxx>
39 #include <simgear/sound/soundmgr_openal.hxx>
40 #include <simgear/misc/ResourceManager.hxx>
41 #include <simgear/props/propertyObject.hxx>
42 #include <simgear/props/props_io.hxx>
43 #include <simgear/scene/model/placement.hxx>
45 #include <Aircraft/controls.hxx>
46 #include <Airports/runways.hxx>
47 #include <ATCDCL/ATCmgr.hxx>
48 #include <Autopilot/route_mgr.hxx>
49 #include <Cockpit/panel.hxx>
50 #include <GUI/FGFontCache.hxx>
51 #include <Model/acmodel.hxx>
52 #include <Model/modelmgr.hxx>
53 #include <MultiPlayer/multiplaymgr.hxx>
54 #include <Scenery/scenery.hxx>
55 #include <Scenery/tilemgr.hxx>
56 #include <Navaids/navlist.hxx>
58 #include "globals.hxx"
59 #include "renderer.hxx"
60 #include "viewmgr.hxx"
62 #include "fg_props.hxx"
65 class AircraftResourceProvider : public simgear::ResourceProvider
68 AircraftResourceProvider() :
69 simgear::ResourceProvider(simgear::ResourceManager::PRIORITY_HIGH)
73 virtual SGPath resolve(const std::string& aResource, SGPath&) const
75 string_list pieces(sgPathBranchSplit(aResource));
76 if ((pieces.size() < 3) || (pieces.front() != "Aircraft")) {
77 return SGPath(); // not an Aircraft path
80 // test against the aircraft-dir property
81 const char* aircraftDir = fgGetString("/sim/aircraft-dir");
82 string_list aircraftDirPieces(sgPathBranchSplit(aircraftDir));
83 if (!aircraftDirPieces.empty() && (aircraftDirPieces.back() == pieces[1])) {
84 // current aircraft-dir matches resource aircraft
85 SGPath r(aircraftDir);
86 for (unsigned int i=2; i<pieces.size(); ++i) {
91 SG_LOG(SG_IO, SG_DEBUG, "found path:" << aResource << " via /sim/aircraft-dir: " << r.str());
96 // try each aircraft dir in turn
97 std::string res(aResource, 9); // resource path with 'Aircraft/' removed
98 const string_list& dirs(globals->get_aircraft_paths());
99 string_list::const_iterator it = dirs.begin();
100 for (; it != dirs.end(); ++it) {
103 SG_LOG(SG_IO, SG_DEBUG, "found path:" << aResource << " in aircraft dir: " << *it);
106 } // of aircraft path iteration
108 return SGPath(); // not found
112 ////////////////////////////////////////////////////////////////////////
113 // Implementation of FGGlobals.
114 ////////////////////////////////////////////////////////////////////////
121 FGGlobals::FGGlobals() :
122 props( new SGPropertyNode ),
123 initial_state( NULL ),
125 renderer( new FGRenderer ),
126 subsystem_mgr( new SGSubsystemMgr ),
127 event_mgr( new SGEventMgr ),
135 current_panel( NULL ),
139 commands( SGCommandMgr::instance() ),
142 channel_options_list( NULL ),
143 initial_waypoints( NULL ),
146 fontcache ( new FGFontCache ),
155 simgear::ResourceManager::instance()->addProvider(new AircraftResourceProvider());
156 simgear::PropertyObjectBase::setDefaultRoot(props);
161 FGGlobals::~FGGlobals()
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");
174 SGSubsystem* sound = subsystem_mgr->remove("sound");
176 subsystem_mgr->shutdown();
177 subsystem_mgr->unbind();
178 delete subsystem_mgr;
187 delete current_panel;
197 delete channel_options_list;
198 delete initial_waypoints;
213 // set the fg_root path
214 void FGGlobals::set_fg_root (const string &root) {
217 // append /data to root if it exists
218 SGPath tmp( fg_root );
219 tmp.append( "data" );
220 tmp.append( "version" );
221 if ( tmp.exists() ) {
222 fgGetNode("BAD_FG_ROOT", true)->setStringValue(fg_root);
224 fgGetNode("GOOD_FG_ROOT", true)->setStringValue(fg_root);
225 SG_LOG(SG_GENERAL, SG_ALERT, "***\n***\n*** Warning: changing bad FG_ROOT/--fg-root to '"
226 << fg_root << "'\n***\n***");
229 // remove /sim/fg-root before writing to prevent hijacking
230 SGPropertyNode *n = fgGetNode("/sim", true);
231 n->removeChild("fg-root", 0, false);
232 n = n->getChild("fg-root", 0, true);
233 n->setStringValue(fg_root.c_str());
234 n->setAttribute(SGPropertyNode::WRITE, false);
236 simgear::ResourceManager::instance()->addBasePath(fg_root,
237 simgear::ResourceManager::PRIORITY_DEFAULT);
240 void FGGlobals::append_fg_scenery (const string &paths)
242 // fg_scenery.clear();
243 SGPropertyNode* sim = fgGetNode("/sim", true);
245 // find first unused fg-scenery property in /sim
247 while (sim->getChild("fg-scenery", propIndex) != NULL) {
251 BOOST_FOREACH(const SGPath& path, sgPathSplit( paths )) {
252 if (!path.exists()) {
253 SG_LOG(SG_GENERAL, SG_WARN, "scenery path not found:" << path.str());
257 // check for duplicates
258 string_list::const_iterator ex = std::find(fg_scenery.begin(), fg_scenery.end(), path.str());
259 if (ex != fg_scenery.end()) {
260 SG_LOG(SG_GENERAL, SG_INFO, "skipping duplicate add of scenery path:" << path.str());
264 simgear::Dir dir(path);
265 SGPath terrainDir(dir.file("Terrain"));
266 SGPath objectsDir(dir.file("Objects"));
268 // this code used to add *either* the base dir, OR add the
269 // Terrain and Objects subdirs, but the conditional logic was commented
270 // out, such that all three dirs are added. Unfortunately there's
271 // no information as to why the change was made.
272 fg_scenery.push_back(path.str());
274 if (terrainDir.exists()) {
275 fg_scenery.push_back(terrainDir.str());
278 if (objectsDir.exists()) {
279 fg_scenery.push_back(objectsDir.str());
282 // insert a marker for FGTileEntry::load(), so that
283 // FG_SCENERY=A:B becomes list ["A/Terrain", "A/Objects", "",
284 // "B/Terrain", "B/Objects", ""]
285 fg_scenery.push_back("");
287 // make scenery dirs available to Nasal
288 SGPropertyNode* n = sim->getChild("fg-scenery", propIndex++, true);
289 n->setStringValue(path.str());
290 n->setAttribute(SGPropertyNode::WRITE, false);
291 } // of path list iteration
294 void FGGlobals::append_aircraft_path(const std::string& path)
296 SGPath dirPath(path);
297 if (!dirPath.exists()) {
298 SG_LOG(SG_GENERAL, SG_WARN, "aircraft path not found:" << path);
302 unsigned int index = fg_aircraft_dirs.size();
303 fg_aircraft_dirs.push_back(path);
305 // make aircraft dirs available to Nasal
306 SGPropertyNode* sim = fgGetNode("/sim", true);
307 sim->removeChild("fg-aircraft", index, false);
308 SGPropertyNode* n = sim->getChild("fg-aircraft", index, true);
309 n->setStringValue(path);
310 n->setAttribute(SGPropertyNode::WRITE, false);
313 void FGGlobals::append_aircraft_paths(const std::string& path)
315 string_list paths = sgPathSplit(path);
316 for (unsigned int p = 0; p<paths.size(); ++p) {
317 append_aircraft_path(paths[p]);
321 SGPath FGGlobals::resolve_aircraft_path(const std::string& branch) const
323 return simgear::ResourceManager::instance()->findPath(branch);
326 SGPath FGGlobals::resolve_maybe_aircraft_path(const std::string& branch) const
328 return simgear::ResourceManager::instance()->findPath(branch);
332 FGGlobals::get_renderer () const
338 FGGlobals::get_subsystem_mgr () const
340 return subsystem_mgr;
344 FGGlobals::get_subsystem (const char * name)
346 return subsystem_mgr->get_subsystem(name);
350 FGGlobals::add_subsystem (const char * name,
351 SGSubsystem * subsystem,
352 SGSubsystemMgr::GroupType type,
355 subsystem_mgr->add(name, subsystem, type, min_time_sec);
359 FGGlobals::get_soundmgr () const
362 return (SGSoundMgr*) subsystem_mgr->get_subsystem("sound");
368 FGGlobals::get_event_mgr () const
374 FGGlobals::get_aircraft_position() const
376 if( acmodel != NULL ) {
377 SGModelPlacement * mp = acmodel->get3DModel();
379 return mp->getPosition();
381 throw sg_exception("Can't get aircraft position", "FGGlobals::get_aircraft_position()" );
385 FGGlobals::get_aircraft_positon_cart() const
387 return SGVec3d::fromGeod(get_aircraft_position());
391 // Save the current state as the initial state.
393 FGGlobals::saveInitialState ()
395 initial_state = new SGPropertyNode();
397 // copy properties which are READ/WRITEable - but not USERARCHIVEd or PRESERVEd
398 int checked = SGPropertyNode::READ+SGPropertyNode::WRITE+
399 SGPropertyNode::USERARCHIVE+SGPropertyNode::PRESERVE;
400 int expected = SGPropertyNode::READ+SGPropertyNode::WRITE;
401 if (!copyProperties(props, initial_state, expected, checked))
402 SG_LOG(SG_GENERAL, SG_ALERT, "Error saving initial state");
404 // delete various properties from the initial state, since we want to
405 // preserve their values even if doing a restore
406 // => Properties should now use the PRESERVE flag to protect their values
407 // on sim-reset. Remove some specific properties for backward compatibility.
408 SGPropertyNode* sim = initial_state->getChild("sim");
409 SGPropertyNode* cameraGroupNode = sim->getNode("rendering/camera-group");
410 if (cameraGroupNode) {
411 cameraGroupNode->removeChild("camera");
412 cameraGroupNode->removeChild("gui");
417 // Restore the saved initial state, if any
419 FGGlobals::restoreInitialState ()
421 if ( initial_state == 0 ) {
422 SG_LOG(SG_GENERAL, SG_ALERT,
423 "No initial state available to restore!!!");
426 // copy properties which are READ/WRITEable - but not USERARCHIVEd or PRESERVEd
427 int checked = SGPropertyNode::READ+SGPropertyNode::WRITE+
428 SGPropertyNode::USERARCHIVE+SGPropertyNode::PRESERVE;
429 int expected = SGPropertyNode::READ+SGPropertyNode::WRITE;
430 if ( copyProperties(initial_state, props, expected, checked)) {
431 SG_LOG( SG_GENERAL, SG_INFO, "Initial state restored successfully" );
433 SG_LOG( SG_GENERAL, SG_INFO,
434 "Some errors restoring initial state (read-only props?)" );
440 FGGlobals::get_current_view () const
442 return viewmgr->get_current_view();
445 long int FGGlobals::get_warp() const
447 return fgGetInt("/sim/time/warp");
450 void FGGlobals::set_warp( long int w )
452 fgSetInt("/sim/time/warp", w);
455 long int FGGlobals::get_warp_delta() const
457 return fgGetInt("/sim/time/warp-delta");
460 void FGGlobals::set_warp_delta( long int d )
462 fgSetInt("/sim/time/warp-delta", d);
465 // end of globals.cxx