1 // FGAIBase - abstract base class for AI objects
2 // Written by David Culp, started Nov 2003, based on
3 // David Luff's FGAIEntity class.
4 // - davidculp2@comcast.net
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License as
8 // published by the Free Software Foundation; either version 2 of the
9 // License, or (at your option) any later version.
11 // This program is distributed in the hope that it will be useful, but
12 // WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 #include <simgear/compiler.h>
32 #include <simgear/math/point3d.hxx>
33 #include <simgear/misc/sg_path.hxx>
34 #include <simgear/scene/model/location.hxx>
35 #include <simgear/scene/model/model.hxx>
36 #include <simgear/debug/logstream.hxx>
37 #include <simgear/props/props.hxx>
39 #include <Main/globals.hxx>
40 #include <Scenery/scenery.hxx>
45 FGAIBase *FGAIBase::_self = NULL;
47 FGAIBase::FGAIBase() {
52 FGAIBase::~FGAIBase() {
56 void FGAIBase::update(double dt) {
60 void FGAIBase::Transform() {
61 aip.setPosition(pos.lon(), pos.lat(), pos.elev() * SG_METER_TO_FEET);
62 aip.setOrientation(roll, pitch, hdg);
63 aip.update( globals->get_scenery()->get_center() );
67 bool FGAIBase::init() {
69 SGPropertyNode *root = globals->get_props()->getNode("ai/models", true);
70 vector<SGPropertyNode_ptr> p_vec = root->getChildren(_type_str);
71 unsigned num = p_vec.size();
74 props = root->getNode(_type_str, num, true);
75 ssgBranch *model = sgLoad3DModel( globals->get_fg_root(),
78 globals->get_sim_time_sec() );
82 globals->get_scenery()->get_scene_graph()->addKid(aip.getSceneGraph());
84 SG_LOG(SG_INPUT, SG_WARN, "AIBase: Could not load aircraft model.");
87 tgt_roll = tgt_pitch = tgt_yaw = tgt_vs = vs = roll = pitch = 0.0;
91 void FGAIBase::bind() {
92 props->tie("velocities/airspeed-kt", SGRawValuePointer<double>(&speed));
93 props->tie("velocities/vertical-speed-fps", SGRawValuePointer<double>(&vs));
95 props->tie("position/altitude-ft", SGRawValuePointer<double>(&altitude));
96 props->tie("position/latitude-deg",
97 SGRawValueFunctions<double>(FGAIBase::_getLatitude,
98 FGAIBase::_setLatitude));
99 props->tie("position/longitude-deg",
100 SGRawValueFunctions<double>(FGAIBase::_getLongitude,
101 FGAIBase::_setLongitude));
103 props->tie("orientation/pitch-deg", SGRawValuePointer<double>(&pitch));
104 props->tie("orientation/roll-deg", SGRawValuePointer<double>(&roll));
105 props->tie("orientation/heading-deg", SGRawValuePointer<double>(&hdg));
108 void FGAIBase::unbind() {
109 props->untie("velocities/airspeed-kt");
110 props->untie("velocities/vertical-speed-fps");
112 props->untie("position/altitude-ft");
113 props->untie("position/latitude-deg");
114 props->untie("position/longitude-deg");
116 props->untie("orientation/pitch-deg");
117 props->untie("orientation/roll-deg");
118 props->untie("orientation/heading-deg");
122 void FGAIBase::_setLongitude( double longitude ) {
123 _self->pos.setlon(longitude);
126 void FGAIBase::_setLatitude ( double latitude ) {
127 _self->pos.setlat(latitude);
130 double FGAIBase::_getLongitude() { return _self->pos.lon(); }
132 double FGAIBase::_getLatitude () { return _self->pos.lat(); }