]> git.mxchange.org Git - flightgear.git/blob - src/AIModel/AIBase.cxx
baa5cc52f6c5f6298ac7f1b41ead2aafab699d87
[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
45 FGAIBase *FGAIBase::_self = NULL;
46
47 FGAIBase::FGAIBase() {
48     _self = this;
49 }
50
51 FGAIBase::~FGAIBase() {
52     _self = NULL;
53 }
54
55 void FGAIBase::update(double dt) {
56 }
57
58
59 void FGAIBase::Transform() {
60     aip.setPosition(pos.lon(), pos.lat(), pos.elev() * SG_METER_TO_FEET);
61     aip.setOrientation(roll, pitch, hdg);
62     aip.update( globals->get_scenery()->get_center() );    
63 }
64
65
66 bool FGAIBase::init() {
67
68    SGPropertyNode *root = globals->get_props()->getNode("ai/modeles", true);
69    vector<SGPropertyNode_ptr> p_vec = root->getChildren("model");
70    unsigned num = p_vec.size();
71    p_vec.clear();
72
73    props = root->getNode("model", num, true);
74    ssgBranch *model = sgLoad3DModel( globals->get_fg_root(),
75                                      model_path.c_str(),
76                                      props,
77                                      globals->get_sim_time_sec() );
78    if (model) {
79      aip.init( model );
80      aip.setVisible(true);
81      globals->get_scenery()->get_scene_graph()->addKid(aip.getSceneGraph());
82    } else {
83      SG_LOG(SG_INPUT, SG_WARN, "AIBase: Could not load aircraft model.");
84    } 
85
86    tgt_roll = tgt_pitch = tgt_yaw = tgt_vs = vs = roll = pitch = 0.0;
87    setDie(false);
88 }
89
90 void FGAIBase::bind() {
91    props->tie("velocities/airspeed-kt",  SGRawValuePointer<double>(&speed));
92    props->tie("velocities/vertical-speed-fps", SGRawValuePointer<double>(&vs));
93
94    props->tie("position/altitude-ft", SGRawValuePointer<double>(&altitude));
95    props->tie("position/latitude-deg",
96                SGRawValueFunctions<double>(FGAIBase::_getLatitude,
97                                            FGAIBase::_setLatitude));
98    props->tie("position/longitude-deg",
99                SGRawValueFunctions<double>(FGAIBase::_getLongitude,
100                                            FGAIBase::_setLongitude));
101
102    props->tie("orientation/pitch-deg", SGRawValuePointer<double>(&pitch));
103    props->tie("orientation/roll-deg", SGRawValuePointer<double>(&roll));
104    props->tie("orientation/heading-deg", SGRawValuePointer<double>(&hdg));
105 }
106
107 void FGAIBase::unbind() {
108     props->untie("velocities/airspeed-kt");
109     props->untie("velocities/vertical-speed-fps");
110
111     props->untie("position/altitude-ft");
112     props->untie("position/latitude-deg");
113     props->untie("position/longitude-deg");
114
115     props->untie("orientation/pitch-deg");
116     props->untie("orientation/roll-deg");
117     props->untie("orientation/heading-deg");
118 }
119
120
121 void FGAIBase::_setLongitude( double longitude ) {
122     _self->pos.setlon(longitude);
123 }
124
125 void FGAIBase::_setLatitude ( double latitude )  {
126     _self->pos.setlat(latitude);
127 }
128
129 double FGAIBase::_getLongitude() { return _self->pos.lon(); }
130
131 double FGAIBase::_getLatitude () { return _self->pos.lat(); }