]> git.mxchange.org Git - flightgear.git/blob - src/Systems/submodel.cxx
Add the missing buoyancy patch.
[flightgear.git] / src / Systems / submodel.cxx
1 // submodel.cxx - models a releasable submodel.
2 // Written by Dave Culp, started Aug 2004
3 //
4 // This file is in the Public Domain and comes with no warranty.
5
6 #include "submodel.hxx"
7
8 #include <simgear/structure/exception.hxx>
9 #include <simgear/misc/sg_path.hxx>
10
11 #include <Main/fg_props.hxx>
12 #include <Main/util.hxx>
13 #include <AIModel/AIManager.hxx>
14
15
16 SubmodelSystem::SubmodelSystem ()
17 {
18   x_offset = y_offset = 0.0;
19   z_offset = -4.0;
20   pitch_offset = 2.0;
21   yaw_offset = 0.0;
22 }
23
24 SubmodelSystem::~SubmodelSystem ()
25 {
26 }
27
28 void
29 SubmodelSystem::init ()
30 {
31     load();
32     _serviceable_node = fgGetNode("/sim/systems/submodels/serviceable", true);
33         
34     _user_lat_node = fgGetNode("/position/latitude-deg", true);
35     _user_lon_node = fgGetNode("/position/longitude-deg", true);
36     _user_alt_node = fgGetNode("/position/altitude-ft", true);
37
38     _user_heading_node = fgGetNode("/orientation/heading-deg", true);
39     _user_pitch_node =   fgGetNode("/orientation/pitch-deg", true);
40     _user_roll_node =    fgGetNode("/orientation/roll-deg", true);
41     _user_yaw_node =     fgGetNode("/orientation/yaw-deg", true);
42
43     _user_speed_node = fgGetNode("/velocities/uBody-fps", true);
44         
45
46     ai = (FGAIManager*)globals->get_subsystem("ai_model");
47 }
48
49 void
50 SubmodelSystem::bind ()
51 {
52 }
53
54 void
55 SubmodelSystem::unbind ()
56 {
57   submodel_iterator = submodels.begin();
58   while(submodel_iterator != submodels.end()) {
59     (*submodel_iterator)->prop->untie("count");
60     ++submodel_iterator;
61   }
62 }
63
64 void
65 SubmodelSystem::update (double dt)
66 {
67   if (!(_serviceable_node->getBoolValue())) return;
68   int i=-1;
69   submodel_iterator = submodels.begin();
70   while(submodel_iterator != submodels.end()) {
71     i++;
72     if ((*submodel_iterator)->trigger->getBoolValue()) {
73         if ((*submodel_iterator)->count != 0) {
74           release( (*submodel_iterator), dt);
75         } 
76     } 
77     ++submodel_iterator;
78   }
79    
80 }
81
82 bool
83 SubmodelSystem::release (submodel* sm, double dt)
84 {
85   sm->timer += dt;
86   if (sm->timer < sm->delay) return false;
87   sm->timer = 0.0;
88
89   transform(sm);  // calculate submodel's initial conditions in world-coordinates
90
91   //cout << "Creating a submodel." << endl; 
92   int rval = ai->createBallistic( sm->model, IC.lat, IC.lon, IC.alt, IC.azimuth,
93                                   IC.elevation, IC.speed, sm->drag_area, sm->life,
94                                   sm-> buoyancy );
95   //cout << "Submodel created." << endl;
96   if (sm->count > 0) (sm->count)--; 
97
98   return true;                    
99 }
100
101 void
102 SubmodelSystem::load ()
103 {
104     int i;
105     SGPropertyNode *path = fgGetNode("/sim/systems/submodels/path");
106     SGPropertyNode root;
107
108     if (path) {
109       SGPath config( globals->get_fg_root() );
110       config.append( path->getStringValue() );
111
112       try {
113         readProperties(config.str(), &root);
114       } catch (const sg_exception &e) {
115         SG_LOG(SG_GENERAL, SG_ALERT,
116         "Unable to read submodels file: ");
117         cout << config.str() << endl;
118         return;
119       }
120     }
121
122    int count = root.nChildren();
123    for (i = 0; i < count; i++) { 
124      // cout << "Reading submodel " << i << endl;        
125      SGPropertyNode *prop;
126      submodel* sm = new submodel;
127      SGPropertyNode * entry_node = root.getChild(i);
128      sm->trigger        = fgGetNode(entry_node->getStringValue("trigger", "none"), true);
129      sm->name           = entry_node->getStringValue("name", "none_defined");
130      sm->model          = entry_node->getStringValue("model", "Models/Geometry/rocket.ac");
131      sm->speed          = entry_node->getDoubleValue("speed", 0.0);
132      sm->repeat         = entry_node->getBoolValue  ("repeat", false); 
133      sm->delay          = entry_node->getDoubleValue("delay", 0.25); 
134      sm->count          = entry_node->getIntValue   ("count", 1); 
135      sm->slaved         = entry_node->getBoolValue  ("slaved", false); 
136      sm->x_offset       = entry_node->getDoubleValue("x-offset", 0.0); 
137      sm->y_offset       = entry_node->getDoubleValue("y_offset", 0.0); 
138      sm->z_offset       = entry_node->getDoubleValue("z-offset", 0.0); 
139      sm->yaw_offset     = entry_node->getDoubleValue("yaw-offset", 0.0); 
140      sm->pitch_offset   = entry_node->getDoubleValue("pitch-offset", 0.0);
141      sm->drag_area      = entry_node->getDoubleValue("eda", 0.007);
142      sm->life           = entry_node->getDoubleValue("life", 900.0);
143      sm->buoyancy       = entry_node->getDoubleValue("buoyancy", 0);
144
145      sm->trigger->setBoolValue(false);
146      sm->timer = sm->delay;
147
148      sm->prop = fgGetNode("/systems/submodels/submodel", i, true);
149      sm->prop->tie("count", SGRawValuePointer<int>(&(sm->count)));
150
151      submodels.push_back( sm );
152    }
153
154   submodel_iterator = submodels.begin();
155   // cout << submodels.size() << " submodels read." << endl;
156 }
157
158
159 void
160 SubmodelSystem::transform( submodel* sm) 
161 {
162   IC.lat =            _user_lat_node->getDoubleValue();
163   IC.lon =            _user_lon_node->getDoubleValue();
164   IC.alt =            _user_alt_node->getDoubleValue();
165   IC.azimuth =        _user_heading_node->getDoubleValue() + sm->yaw_offset;
166   IC.elevation =      _user_pitch_node->getDoubleValue() + sm->pitch_offset;
167   IC.speed =          _user_speed_node->getDoubleValue() + sm->speed;
168   
169 }
170
171
172 // end of submodel.cxx
173