]> git.mxchange.org Git - flightgear.git/blob - src/Autopilot/autopilotgroup.cxx
Change default (non-set) MP hosts to be an empty string instead of '0'
[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
39
40 using simgear::PropertyList;
41
42 class FGXMLAutopilotGroupImplementation : public FGXMLAutopilotGroup
43 {
44 public:
45     void init();
46     void reinit();
47     void update( double dt );
48 private:
49     void initFrom( SGPropertyNode_ptr rootNode, const char * childName );
50     vector<string> _autopilotNames;
51
52 };
53
54 void FGXMLAutopilotGroupImplementation::update( double dt )
55 {
56     // update all configured autopilots
57     SGSubsystemGroup::update( dt );
58 }
59
60 void FGXMLAutopilotGroupImplementation::reinit()
61 {
62     SGSubsystemGroup::unbind();
63
64     for( vector<string>::size_type i = 0; i < _autopilotNames.size(); i++ ) {
65       FGXMLAutopilot::Autopilot * ap = (FGXMLAutopilot::Autopilot*)get_subsystem( _autopilotNames[i] );
66       if( ap == NULL ) continue; // ?
67       remove_subsystem( _autopilotNames[i] );
68       delete ap;
69     }
70     _autopilotNames.clear();
71     init();
72 }
73
74 void FGXMLAutopilotGroupImplementation::init()
75 {
76     static const char * nodeNames[] = {
77         "autopilot",
78         "property-rule"
79     };
80     for( unsigned i = 0; i < sizeof(nodeNames)/sizeof(nodeNames[0]); i++ )
81         initFrom( fgGetNode( "/sim/systems" ), nodeNames[i] );
82
83     SGSubsystemGroup::bind();
84     SGSubsystemGroup::init();
85 }
86
87 void FGXMLAutopilotGroupImplementation::initFrom( SGPropertyNode_ptr rootNode, const char * childName )
88 {
89     if( rootNode == NULL )
90         return;
91
92     PropertyList autopilotNodes = rootNode->getChildren(childName);
93     for( PropertyList::size_type i = 0; i < autopilotNodes.size(); i++ ) {
94         SGPropertyNode_ptr pathNode = autopilotNodes[i]->getNode( "path" );
95         if( pathNode == NULL ) {
96             SG_LOG( SG_ALL, SG_WARN, "No configuration file specified for this property-rule!");
97             continue;
98         }
99
100         string apName;
101         SGPropertyNode_ptr nameNode = autopilotNodes[i]->getNode( "name" );
102         if( nameNode != NULL ) {
103             apName = nameNode->getStringValue();
104         } else {
105           std::ostringstream buf;
106           buf <<  "unnamed_autopilot_" << i;
107           apName = buf.str();
108         }
109
110         {
111           // check for duplicate names
112           string name = apName;
113           for( unsigned i = 0; get_subsystem( apName.c_str() ) != NULL; i++ ) {
114               ostringstream buf;
115               buf <<  name << "_" << i;
116               apName = buf.str();
117           }
118           if( apName != name )
119             SG_LOG( SG_ALL, SG_WARN, "Duplicate property-rule configuration name " << name << ", renamed to " << apName );
120         }
121
122         SGPath config = globals->resolve_maybe_aircraft_path(pathNode->getStringValue());
123         if (config.isNull())
124         {
125             SG_LOG( SG_ALL, SG_ALERT, "Cannot find property-rule configuration file '" << pathNode->getStringValue() << "'." );
126         }
127         else
128         {
129             SG_LOG( SG_ALL, SG_INFO, "Reading property-rule configuration from " << config.str() );
130
131             try {
132                 SGPropertyNode_ptr root = new SGPropertyNode();
133                 readProperties( config.str(), root );
134
135                 SG_LOG( SG_AUTOPILOT, SG_INFO, "adding  property-rule subsystem " << apName );
136                 FGXMLAutopilot::Autopilot * ap = new FGXMLAutopilot::Autopilot( autopilotNodes[i], root );
137                 ap->set_name( apName );
138                 set_subsystem( apName, ap );
139                 _autopilotNames.push_back( apName );
140
141             } catch (const sg_exception& e) {
142                 SG_LOG( SG_AUTOPILOT, SG_ALERT, "Failed to load property-rule configuration: "
143                         << config.str() << ":" << e.getMessage() );
144                 continue;
145             }
146         }
147     }
148 }
149
150 FGXMLAutopilotGroup * FGXMLAutopilotGroup::createInstance()
151 {
152     return new FGXMLAutopilotGroupImplementation();
153 }