]> git.mxchange.org Git - flightgear.git/blob - src/Autopilot/autopilotgroup.cxx
MapWidget: make use of the new POI system and display cities on the map.
[flightgear.git] / src / Autopilot / autopilotgroup.cxx
1 // autopilotgroup.cxx - an even more flexible, generic way to build autopilots
2 //
3 // Written by Torsten Dreyer
4 // Based heavily on work created by Curtis Olson, started January 2004.
5 //
6 // Copyright (C) 2004  Curtis L. Olson  - http://www.flightgear.org/~curt
7 // Copyright (C) 2010  Torsten Dreyer - Torsten (at) t3r (dot) de
8 //
9 // This program is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU General Public License as
11 // published by the Free Software Foundation; either version 2 of the
12 // License, or (at your option) any later version.
13 //
14 // This program is distributed in the hope that it will be useful, but
15 // WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17 // General Public License for more details.
18 //
19 // You should have received a copy of the GNU General Public License
20 // along with this program; if not, write to the Free Software
21 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
22 //
23
24 #ifdef HAVE_CONFIG_H
25 #  include <config.h>
26 #endif
27
28 #include "autopilot.hxx"
29 #include "autopilotgroup.hxx"
30
31 #include <string>
32 #include <vector>
33
34 #include <simgear/props/props_io.hxx>
35 #include <simgear/structure/subsystem_mgr.hxx>
36 #include <simgear/structure/exception.hxx>
37 #include <Main/fg_props.hxx>
38 #include <boost/foreach.hpp>
39
40 using std::vector;
41 using std::string;
42 using simgear::PropertyList;
43
44 class FGXMLAutopilotGroupImplementation : public FGXMLAutopilotGroup
45 {
46 public:
47     FGXMLAutopilotGroupImplementation(const std::string& nodeName) :
48         FGXMLAutopilotGroup(), _nodeName(nodeName) {}
49     virtual void addAutopilot( const std::string & name, SGPropertyNode_ptr apNode, SGPropertyNode_ptr config );
50     virtual void removeAutopilot( const std::string & name );
51     void init();
52     InitStatus incrementalInit();
53     void reinit();
54     void update( double dt );
55 private:
56     void initFrom( SGPropertyNode_ptr rootNode, const char * childName );
57     vector<string> _autopilotNames;
58     std::string _nodeName;
59
60 };
61
62 void FGXMLAutopilotGroupImplementation::addAutopilot( const std::string & name, SGPropertyNode_ptr apNode, SGPropertyNode_ptr config )
63 {
64     BOOST_FOREACH( std::string & n, _autopilotNames ) {
65         if( n == name ) {
66             SG_LOG(SG_ALL, SG_ALERT, "NOT adding duplicate property rule name " << name );
67             return;
68         }
69     }
70     FGXMLAutopilot::Autopilot * ap = new FGXMLAutopilot::Autopilot( apNode, config );
71     ap->set_name( name );
72
73     double updateInterval = config->getDoubleValue( "update-interval-secs", 0.0 );
74     set_subsystem( name, ap, updateInterval );
75     _autopilotNames.push_back( name );
76 }
77
78 void FGXMLAutopilotGroupImplementation::removeAutopilot( const std::string & name )
79 {
80     FGXMLAutopilot::Autopilot * ap = (FGXMLAutopilot::Autopilot*)get_subsystem( name );
81     if( ap == NULL ) return; // ?
82     remove_subsystem( name );
83     delete ap;
84 }
85
86
87
88 void FGXMLAutopilotGroupImplementation::update( double dt )
89 {
90     // update all configured autopilots
91     SGSubsystemGroup::update( dt );
92 }
93
94 void FGXMLAutopilotGroupImplementation::reinit()
95 {
96     SGSubsystemGroup::unbind();
97
98     for( vector<string>::size_type i = 0; i < _autopilotNames.size(); i++ ) {
99       removeAutopilot( _autopilotNames[i] );
100     }
101     _autopilotNames.clear();
102     init();
103 }
104
105 SGSubsystem::InitStatus FGXMLAutopilotGroupImplementation::incrementalInit()
106 {
107   init();
108   return INIT_DONE;
109 }
110
111 void FGXMLAutopilotGroupImplementation::init()
112 {
113     initFrom( fgGetNode( "/sim/systems" ), _nodeName.c_str() );
114
115     SGSubsystemGroup::bind();
116     SGSubsystemGroup::init();
117 }
118
119 void FGXMLAutopilotGroupImplementation::initFrom( SGPropertyNode_ptr rootNode, const char * childName )
120 {
121     if( rootNode == NULL )
122         return;
123
124     BOOST_FOREACH( SGPropertyNode_ptr autopilotNode, rootNode->getChildren(childName) ) {
125         SGPropertyNode_ptr pathNode = autopilotNode->getNode( "path" );
126         if( pathNode == NULL ) {
127             SG_LOG( SG_ALL, SG_WARN, "No configuration file specified for this property-rule!");
128             continue;
129         }
130
131         string apName;
132         SGPropertyNode_ptr nameNode = autopilotNode->getNode( "name" );
133         if( nameNode != NULL ) {
134             apName = nameNode->getStringValue();
135         } else {
136           std::ostringstream buf;
137           buf <<  "unnamed_autopilot_" << autopilotNode->getIndex();
138           apName = buf.str();
139         }
140
141         {
142           // check for duplicate names
143           string name = apName;
144           for( unsigned i = 0; get_subsystem( apName.c_str() ) != NULL; i++ ) {
145               std::ostringstream buf;
146               buf <<  name << "_" << i;
147               apName = buf.str();
148           }
149           if( apName != name )
150             SG_LOG( SG_ALL, SG_WARN, "Duplicate property-rule configuration name " << name << ", renamed to " << apName );
151         }
152
153         addAutopilotFromFile( apName, autopilotNode, pathNode->getStringValue() );
154     }
155 }
156
157 void FGXMLAutopilotGroup::addAutopilotFromFile( const std::string & name, SGPropertyNode_ptr apNode, const char * path )
158 {
159     SGPath config = globals->resolve_maybe_aircraft_path(path);
160     if (config.isNull())
161     {
162         SG_LOG( SG_ALL, SG_ALERT, "Cannot find property-rule configuration file '" << path << "'." );
163         return;
164     }
165     SG_LOG( SG_ALL, SG_INFO, "Reading property-rule configuration from " << config.str() );
166
167     try {
168         SGPropertyNode_ptr configNode = new SGPropertyNode();
169         readProperties( config.str(), configNode );
170
171         SG_LOG( SG_AUTOPILOT, SG_INFO, "adding  property-rule subsystem " << name );
172         addAutopilot( name, apNode, configNode );
173
174     } catch (const sg_exception& e) {
175         SG_LOG( SG_AUTOPILOT, SG_ALERT, "Failed to load property-rule configuration: "
176             << config.str() << ":" << e.getMessage() );
177         return;
178     }
179 }
180
181 FGXMLAutopilotGroup * FGXMLAutopilotGroup::createInstance(const std::string& nodeName)
182 {
183     return new FGXMLAutopilotGroupImplementation(nodeName);
184 }