]> git.mxchange.org Git - flightgear.git/blob - src/AIModel/AIBase.cxx
e59207a65d48c14dc8dfb39097e92c9c14d6a9b7
[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::_self = NULL;
47
48 FGAIBase::FGAIBase() {
49     _self = this;
50     _type_str = "model";
51     tgt_roll = roll = tgt_pitch = tgt_yaw = tgt_vs = vs = pitch = 0.0;
52     bearing = elevation = range = rdot = 0.0;
53     x_shift = y_shift = rotation = 0.0;
54     invisible = true;
55     no_roll = true;
56     model_path = "";
57     model = 0;
58     _otype = otNull;
59     index = 0;
60 }
61
62 FGAIBase::~FGAIBase() {
63     globals->get_scenery()->get_scene_graph()->removeKid(aip.getSceneGraph());
64     unbind();
65     SGPropertyNode *root = globals->get_props()->getNode("ai/models", true);
66     root->removeChild(_type_str.c_str(), index);
67     _self = NULL;
68 }
69
70 void FGAIBase::update(double dt) {
71 }
72
73
74 void FGAIBase::Transform() {
75     if (!invisible) {
76       aip.setPosition(pos.lon(), pos.lat(), pos.elev() * SG_METER_TO_FEET);
77       if (no_roll) {
78          aip.setOrientation(0.0, pitch, hdg);
79       } else {
80          aip.setOrientation(roll, pitch, hdg);
81       }
82       aip.update( globals->get_scenery()->get_center() );    
83     }
84 }
85
86
87 bool FGAIBase::init() {
88
89    SGPropertyNode *root = globals->get_props()->getNode("ai/models", true);
90    index = manager->getNum(_otype) - 1;
91    props = root->getNode(_type_str.c_str(), index, true);
92    if (model_path != "") {
93       model = sgLoad3DModel( globals->get_fg_root(),
94                              model_path.c_str(),
95                              props,
96                              globals->get_sim_time_sec() );
97    }
98    if (model) {
99      aip.init( model );
100      aip.setVisible(true);
101      invisible = false;
102      globals->get_scenery()->get_scene_graph()->addKid(aip.getSceneGraph());
103    } else {
104      if (model_path != "") { 
105        SG_LOG(SG_INPUT, SG_WARN, "AIBase: Could not load model.");
106      }
107    } 
108
109    setDie(false);
110
111    return true;
112 }
113
114 bool FGAIBase::isa( object_type otype ) {
115  if ( otype == _otype ) { return true; }
116  else { return false; } 
117 }
118
119
120 void FGAIBase::bind() {
121    props->tie("id", SGRawValuePointer<int>(&id));
122    props->tie("velocities/true-airspeed-kt",  SGRawValuePointer<double>(&speed));
123    props->tie("velocities/vertical-speed-fps",
124                SGRawValueFunctions<double>(FGAIBase::_getVS_fps,
125                                            FGAIBase::_setVS_fps));
126
127    props->tie("position/altitude-ft",
128                SGRawValueFunctions<double>(FGAIBase::_getAltitude,
129                                            FGAIBase::_setAltitude));
130    props->tie("position/latitude-deg",
131                SGRawValueFunctions<double>(FGAIBase::_getLatitude,
132                                            FGAIBase::_setLatitude));
133    props->tie("position/longitude-deg",
134                SGRawValueFunctions<double>(FGAIBase::_getLongitude,
135                                            FGAIBase::_setLongitude));
136
137    props->tie("orientation/pitch-deg",   SGRawValuePointer<double>(&pitch));
138    props->tie("orientation/roll-deg",    SGRawValuePointer<double>(&roll));
139    props->tie("orientation/true-heading-deg", SGRawValuePointer<double>(&hdg));
140
141    props->tie("radar/bearing-deg",   SGRawValueFunctions<double>(FGAIBase::_getBearing));
142    props->tie("radar/elevation-deg", SGRawValueFunctions<double>(FGAIBase::_getElevation));
143    props->tie("radar/range-nm",      SGRawValueFunctions<double>(FGAIBase::_getRange));
144    props->tie("radar/h-offset", SGRawValueFunctions<double>(FGAIBase::_getH_offset));
145    props->tie("radar/v-offset", SGRawValueFunctions<double>(FGAIBase::_getV_offset)); 
146    props->tie("radar/x-shift", SGRawValueFunctions<double>(FGAIBase::_getX_shift));
147    props->tie("radar/y-shift", SGRawValueFunctions<double>(FGAIBase::_getY_shift));
148    props->tie("radar/rotation", SGRawValueFunctions<double>(FGAIBase::_getRotation));
149
150    props->tie("controls/lighting/nav-lights",
151                SGRawValueFunctions<bool>(FGAIBase::_isNight));
152    props->setBoolValue("controls/lighting/beacon", true);
153    props->setBoolValue("controls/lighting/strobe", true);
154 }
155
156 void FGAIBase::unbind() {
157     props->untie("id");
158     props->untie("velocities/true-airspeed-kt");
159     props->untie("velocities/vertical-speed-fps");
160
161     props->untie("position/altitude-ft");
162     props->untie("position/latitude-deg");
163     props->untie("position/longitude-deg");
164
165     props->untie("orientation/pitch-deg");
166     props->untie("orientation/roll-deg");
167     props->untie("orientation/true-heading-deg");
168
169     props->untie("radar/bearing-deg");
170     props->untie("radar/elevation-deg");
171     props->untie("radar/range-nm");
172     props->untie("radar/h-offset");
173     props->untie("radar/v-offset");
174     props->untie("radar/x-shift");
175     props->untie("radar/y-shift");
176     props->untie("radar/rotation");
177
178     props->untie("controls/lighting/nav-lights");
179 }
180