]> git.mxchange.org Git - flightgear.git/blob - src/AIModel/AIBase.cxx
8759b3a662be25cb771abcde00715a7940a6a469
[flightgear.git] / src / AIModel / AIBase.cxx
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
5 //
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.
10 //
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.
15 //
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.
19
20
21 #ifdef HAVE_CONFIG_H
22 #  include <config.h>
23 #endif
24
25 #include <simgear/compiler.h>
26
27 #include STL_STRING
28
29 #include <plib/sg.h>
30 #include <plib/ssg.h>
31
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>
38
39 #include <Main/globals.hxx>
40 #include <Scenery/scenery.hxx>
41
42
43 #include "AIBase.hxx"
44 #include "AIManager.hxx"
45
46 FGAIBase::FGAIBase() {
47     _type_str = "model";
48     tgt_roll = roll = tgt_pitch = tgt_yaw = tgt_vs = vs = pitch = 0.0;
49     bearing = elevation = range = rdot = 0.0;
50     x_shift = y_shift = rotation = 0.0;
51     invisible = true;
52     no_roll = true;
53     model_path = "";
54     model = 0;
55     _otype = otNull;
56     index = 0;
57 }
58
59 FGAIBase::~FGAIBase() {
60     globals->get_scenery()->get_scene_graph()->removeKid(aip.getSceneGraph());
61     // unbind();
62     SGPropertyNode *root = globals->get_props()->getNode("ai/models", true);
63     root->removeChild(_type_str.c_str(), index);
64 }
65
66 void FGAIBase::update(double dt) {
67 }
68
69
70 void FGAIBase::Transform() {
71     if (!invisible) {
72       aip.setPosition(pos.lon(), pos.lat(), pos.elev() * SG_METER_TO_FEET);
73       if (no_roll) {
74          aip.setOrientation(0.0, pitch, hdg);
75       } else {
76          aip.setOrientation(roll, pitch, hdg);
77       }
78       aip.update( globals->get_scenery()->get_center() );    
79     }
80 }
81
82
83 bool FGAIBase::init() {
84
85    SGPropertyNode *root = globals->get_props()->getNode("ai/models", true);
86    index = manager->getNum(_otype) - 1;
87    props = root->getNode(_type_str.c_str(), index, true);
88    if (model_path != "") {
89       model = sgLoad3DModel( globals->get_fg_root(),
90                              model_path.c_str(),
91                              props,
92                              globals->get_sim_time_sec() );
93    }
94    if (model) {
95      aip.init( model );
96      aip.setVisible(true);
97      invisible = false;
98      globals->get_scenery()->get_scene_graph()->addKid(aip.getSceneGraph());
99    } else {
100      if (model_path != "") { 
101        SG_LOG(SG_INPUT, SG_WARN, "AIBase: Could not load model.");
102      }
103    } 
104
105    setDie(false);
106
107    return true;
108 }
109
110 bool FGAIBase::isa( object_type otype ) {
111  if ( otype == _otype ) { return true; }
112  else { return false; } 
113 }
114
115
116 void FGAIBase::bind() {
117    props->tie("id", SGRawValuePointer<int>(&id));
118    props->tie("velocities/true-airspeed-kt",  SGRawValuePointer<double>(&speed));
119    props->tie("velocities/vertical-speed-fps",
120                SGRawValueFunctions<double>(FGAIBase::_getVS_fps,
121                                            FGAIBase::_setVS_fps, this));
122
123    props->tie("position/altitude-ft",
124                SGRawValueFunctions<double>(FGAIBase::_getAltitude,
125                                            FGAIBase::_setAltitude, this));
126    props->tie("position/latitude-deg",
127                SGRawValueFunctions<double>(FGAIBase::_getLatitude,
128                                            FGAIBase::_setLatitude, this));
129    props->tie("position/longitude-deg",
130                SGRawValueFunctions<double>(FGAIBase::_getLongitude,
131                                            FGAIBase::_setLongitude, this));
132
133    props->tie("orientation/pitch-deg",   SGRawValuePointer<double>(&pitch));
134    props->tie("orientation/roll-deg",    SGRawValuePointer<double>(&roll));
135    props->tie("orientation/true-heading-deg", SGRawValuePointer<double>(&hdg));
136
137    props->tie("radar/bearing-deg",   SGRawValuePointer<double>(&bearing));
138    props->tie("radar/elevation-deg", SGRawValuePointer<double>(&elevation));
139    props->tie("radar/range-nm", SGRawValuePointer<double>(&range));
140    props->tie("radar/h-offset", SGRawValuePointer<double>(&horiz_offset));
141    props->tie("radar/v-offset", SGRawValuePointer<double>(&vert_offset)); 
142    props->tie("radar/x-shift", SGRawValuePointer<double>(&x_shift));
143    props->tie("radar/y-shift", SGRawValuePointer<double>(&y_shift));
144    props->tie("radar/rotation", SGRawValuePointer<double>(&rotation));
145
146    props->tie("controls/lighting/nav-lights",
147                SGRawValueFunctions<bool>(FGAIBase::_isNight));
148    props->setBoolValue("controls/lighting/beacon", true);
149    props->setBoolValue("controls/lighting/strobe", true);
150 }
151
152 void FGAIBase::unbind() {
153     props->untie("id");
154     props->untie("velocities/true-airspeed-kt");
155     props->untie("velocities/vertical-speed-fps");
156
157     props->untie("position/altitude-ft");
158     props->untie("position/latitude-deg");
159     props->untie("position/longitude-deg");
160
161     props->untie("orientation/pitch-deg");
162     props->untie("orientation/roll-deg");
163     props->untie("orientation/true-heading-deg");
164
165     props->untie("radar/bearing-deg");
166     props->untie("radar/elevation-deg");
167     props->untie("radar/range-nm");
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");
173
174     props->untie("controls/lighting/nav-lights");
175 }
176
177
178 /*
179  * getters and Setters
180  */
181 void FGAIBase::_setLongitude( double longitude, void *p ) {
182     FGAIBase *self = (FGAIBase *)p;
183     self->pos.setlon(longitude);
184 }
185 void FGAIBase::_setLatitude ( double latitude, void *p )  {
186     FGAIBase *self = (FGAIBase *)p;
187     self->pos.setlat(latitude);
188 }
189
190 double FGAIBase::_getLongitude(void *p) {
191     FGAIBase *self = (FGAIBase *)p;
192     return self->pos.lon();
193 }
194 double FGAIBase::_getLatitude (void *p) {
195     FGAIBase *self = (FGAIBase *)p;
196     return self->pos.lat();
197 }
198 double FGAIBase::_getRdot(void *p)      {
199     FGAIBase *self = (FGAIBase *)p;
200     return self->rdot;
201 }
202 double FGAIBase::_getVS_fps(void *p) {
203     FGAIBase *self = (FGAIBase *)p;
204     return self->vs*60.0;
205 }
206 void FGAIBase::_setVS_fps( double _vs, void *p ) {
207     FGAIBase *self = (FGAIBase *)p;
208     self->vs = _vs/60.0;
209 }
210
211 double FGAIBase::_getAltitude(void *p) {
212     FGAIBase *self = (FGAIBase *)p;
213     return self->altitude;
214 }
215 void FGAIBase::_setAltitude( double _alt, void *p ) {
216     FGAIBase *self = (FGAIBase *)p;
217     self->setAltitude( _alt );
218 }
219
220 bool FGAIBase::_isNight() {
221     return (fgGetFloat("/sim/time/sun-angle-rad") > 1.57);
222 }
223