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