]> git.mxchange.org Git - flightgear.git/blob - src/Systems/submodel.cxx
Tie the count into the property tree instead of using snprintf every frame. Add the...
[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     ai = (FGAIManager*)globals->get_subsystem("ai_model");
46 }
47
48 void
49 SubmodelSystem::bind ()
50 {
51 }
52
53 void
54 SubmodelSystem::unbind ()
55 {
56   submodel_iterator = submodels.begin();
57   while(submodel_iterator != submodels.end()) {
58     (*submodel_iterator)->prop->untie("count");
59     ++submodel_iterator;
60   }
61 }
62
63 void
64 SubmodelSystem::update (double dt)
65 {
66   if (!(_serviceable_node->getBoolValue())) return;
67   int i=-1;
68   submodel_iterator = submodels.begin();
69   while(submodel_iterator != submodels.end()) {
70     i++;
71     if ((*submodel_iterator)->trigger->getBoolValue()) {
72         if ((*submodel_iterator)->count != 0) {
73           release( (*submodel_iterator), dt);
74         } 
75     } 
76     ++submodel_iterator;
77   }
78    
79 }
80
81 bool
82 SubmodelSystem::release (submodel* sm, double dt)
83 {
84   sm->timer += dt;
85   if (sm->timer < sm->delay) return false;
86   sm->timer = 0.0;
87
88   transform(sm);  // calculate submodel's initial conditions in world-coordinates
89
90   //cout << "Creating a submodel." << endl; 
91   int rval = ai->createBallistic( sm->model, IC.lat, IC.lon, IC.alt, IC.azimuth,
92                                   IC.elevation, IC.speed, sm->drag_area, sm->life );
93   //cout << "Submodel created." << endl;
94   if (sm->count > 0) (sm->count)--; 
95
96   return true;                    
97 }
98
99 void
100 SubmodelSystem::load ()
101 {
102     int i;
103     SGPropertyNode *path = fgGetNode("/sim/systems/submodels/path");
104     SGPropertyNode root;
105
106     if (path) {
107       SGPath config( globals->get_fg_root() );
108       config.append( path->getStringValue() );
109
110       try {
111         readProperties(config.str(), &root);
112       } catch (const sg_exception &e) {
113         SG_LOG(SG_GENERAL, SG_ALERT,
114         "Unable to read submodels file: ");
115         cout << config.str() << endl;
116         return;
117       }
118     }
119
120    int count = root.nChildren();
121    for (i = 0; i < count; i++) { 
122      // cout << "Reading submodel " << i << endl;        
123      SGPropertyNode *prop;
124      submodel* sm = new submodel;
125      SGPropertyNode * entry_node = root.getChild(i);
126      sm->trigger        = fgGetNode(entry_node->getStringValue("trigger", "none"), true);
127      sm->name           = entry_node->getStringValue("name", "none_defined");
128      sm->model          = entry_node->getStringValue("model", "Models/Geometry/rocket.ac");
129      sm->speed          = entry_node->getDoubleValue("speed", 0.0);
130      sm->repeat         = entry_node->getBoolValue  ("repeat", false); 
131      sm->delay          = entry_node->getDoubleValue("delay", 0.25); 
132      sm->count          = entry_node->getIntValue   ("count", 1); 
133      sm->slaved         = entry_node->getBoolValue  ("slaved", false); 
134      sm->x_offset       = entry_node->getDoubleValue("x-offset", 0.0); 
135      sm->y_offset       = entry_node->getDoubleValue("y_offset", 0.0); 
136      sm->z_offset       = entry_node->getDoubleValue("z-offset", 0.0); 
137      sm->yaw_offset     = entry_node->getDoubleValue("yaw-offset", 0.0); 
138      sm->pitch_offset   = entry_node->getDoubleValue("pitch-offset", 0.0);
139      sm->drag_area      = entry_node->getDoubleValue("eda", 0.007);
140      sm->life           = entry_node->getDoubleValue("life", 900.0);
141
142      sm->trigger->setBoolValue(false);
143      sm->timer = sm->delay;
144
145      sm->prop = fgGetNode("/systems/submodels/submodel", i, true);
146      sm->prop->tie("count", SGRawValuePointer<int>(&(sm->count)));
147
148      submodels.push_back( sm );
149    }
150
151   submodel_iterator = submodels.begin();
152   // cout << submodels.size() << " submodels read." << endl;
153 }
154
155
156 void
157 SubmodelSystem::transform( submodel* sm) 
158 {
159   IC.lat =       _user_lat_node->getDoubleValue();
160   IC.lon =       _user_lon_node->getDoubleValue();
161   IC.alt =       _user_alt_node->getDoubleValue();
162   IC.azimuth =   _user_heading_node->getDoubleValue() + sm->yaw_offset;
163   IC.elevation = _user_pitch_node->getDoubleValue() + sm->pitch_offset;
164   IC.speed =     _user_speed_node->getDoubleValue() + sm->speed;
165 }
166
167
168 // end of submodel.cxx