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() {
50 tgt_roll = roll = tgt_pitch = tgt_yaw = tgt_vs = vs = pitch = 0.0;
51 bearing = elevation = range = rdot = 0.0;
52 x_shift = y_shift = rotation = 0.0;
58 FGAIBase::~FGAIBase() {
59 globals->get_scenery()->get_scene_graph()->removeKid(aip.getSceneGraph());
64 void FGAIBase::update(double dt) {
68 void FGAIBase::Transform() {
70 aip.setPosition(pos.lon(), pos.lat(), pos.elev() * SG_METER_TO_FEET);
71 aip.setOrientation(roll, pitch, hdg);
72 aip.update( globals->get_scenery()->get_center() );
77 bool FGAIBase::init() {
79 SGPropertyNode *root = globals->get_props()->getNode("ai/models", true);
80 vector<SGPropertyNode_ptr> p_vec = root->getChildren(_type_str);
81 unsigned num = p_vec.size();
84 props = root->getNode(_type_str, num, true);
86 if (model_path != "") {
87 model = sgLoad3DModel( globals->get_fg_root(),
90 globals->get_sim_time_sec() );
96 globals->get_scenery()->get_scene_graph()->addKid(aip.getSceneGraph());
98 if (model_path != "") {
99 SG_LOG(SG_INPUT, SG_WARN, "AIBase: Could not load model.");
108 bool FGAIBase::isa( object_type otype ) {
109 if ( otype == _otype ) { return true; }
110 else { return false; }
114 void FGAIBase::bind() {
115 props->tie("id", SGRawValuePointer<int>(&id));
116 props->tie("velocities/true-airspeed-kt", SGRawValuePointer<double>(&speed));
117 props->tie("velocities/vertical-speed-fps",
118 SGRawValueFunctions<double>(FGAIBase::_getVS_fps,
119 FGAIBase::_setVS_fps));
121 props->tie("position/altitude-ft",
122 SGRawValueFunctions<double>(FGAIBase::_getAltitude,
123 FGAIBase::_setAltitude));
124 props->tie("position/latitude-deg",
125 SGRawValueFunctions<double>(FGAIBase::_getLatitude,
126 FGAIBase::_setLatitude));
127 props->tie("position/longitude-deg",
128 SGRawValueFunctions<double>(FGAIBase::_getLongitude,
129 FGAIBase::_setLongitude));
131 props->tie("orientation/pitch-deg", SGRawValuePointer<double>(&pitch));
132 props->tie("orientation/roll-deg", SGRawValuePointer<double>(&roll));
133 props->tie("orientation/true-heading-deg", SGRawValuePointer<double>(&hdg));
135 props->tie("radar/bearing-deg", SGRawValueFunctions<double>(FGAIBase::_getBearing));
136 props->tie("radar/elevation-deg", SGRawValueFunctions<double>(FGAIBase::_getElevation));
137 props->tie("radar/range-nm", SGRawValueFunctions<double>(FGAIBase::_getRange));
138 // props->tie("radar/rdot-kts", SGRawValueFunctions<double>(FGAIBase::_getRdot));
139 props->tie("radar/h-offset", SGRawValueFunctions<double>(FGAIBase::_getH_offset));
140 props->tie("radar/v-offset", SGRawValueFunctions<double>(FGAIBase::_getV_offset));
141 props->tie("radar/x-shift", SGRawValueFunctions<double>(FGAIBase::_getX_shift));
142 props->tie("radar/y-shift", SGRawValueFunctions<double>(FGAIBase::_getY_shift));
143 props->tie("radar/rotation", SGRawValueFunctions<double>(FGAIBase::_getRotation));
145 props->tie("controls/lighting/nav-lights",
146 SGRawValueFunctions<bool>(FGAIBase::_isNight));
147 props->setBoolValue("controls/lighting/beacon", true);
148 props->setBoolValue("controls/lighting/strobe", true);
151 void FGAIBase::unbind() {
153 props->untie("velocities/true-airspeed-kt");
154 props->untie("velocities/vertical-speed-fps");
156 props->untie("position/altitude-ft");
157 props->untie("position/latitude-deg");
158 props->untie("position/longitude-deg");
160 props->untie("orientation/pitch-deg");
161 props->untie("orientation/roll-deg");
162 props->untie("orientation/true-heading-deg");
164 props->untie("radar/bearing-deg");
165 props->untie("radar/elevation-deg");
166 props->untie("radar/range-nm");
167 // props->untie("radar/rdot-kts");
168 props->untie("radar/h-offset");
169 props->untie("radar/v-offset");
170 props->untie("radar/x-shift");
171 props->untie("radar/y-shift");
172 props->untie("radar/rotation");
174 props->untie("controls/controls/lighting/nav-lights");