]> git.mxchange.org Git - flightgear.git/blob - src/AIModel/submodel.cxx
- Added ultra-light traffic is now a separate traffic class that can have its
[flightgear.git] / src / AIModel / submodel.cxx
1 //// submodel.cxx - models a releasable submodel.
2 // Written by Dave Culp, started Aug 2004
3 // With major additions by Vivian Meaaza 2004 - 2007
4 //
5 // This file is in the Public Domain and comes with no warranty.
6
7 #ifdef HAVE_CONFIG_H
8 #  include "config.h"
9 #endif
10
11 #include "submodel.hxx"
12
13 #include <simgear/structure/exception.hxx>
14 #include <simgear/misc/sg_path.hxx>
15 #include <simgear/math/sg_geodesy.hxx>
16
17 #include <Main/fg_props.hxx>
18 #include <Main/util.hxx>
19
20
21 #include "AIBase.hxx"
22 #include "AIManager.hxx"
23 #include "AIBallistic.hxx"
24
25
26 const double FGSubmodelMgr::lbs_to_slugs = 0.031080950172;
27
28 FGSubmodelMgr::FGSubmodelMgr()
29 {
30     x_offset = y_offset = 0.0;
31     z_offset = -4.0;
32     pitch_offset = 2.0;
33     yaw_offset = 0.0;
34
35     out[0] = out[1] = out[2] = 0;
36     in[3] = out[3] = 1;
37     string contents_node;
38     contrail_altitude = 30000;
39     _count = 0;
40 }
41
42 FGSubmodelMgr::~FGSubmodelMgr()
43 {
44 }
45
46 void FGSubmodelMgr::init()
47 {
48     index = 0;
49
50     _serviceable_node = fgGetNode("/sim/submodels/serviceable", true);
51     _serviceable_node->setBoolValue(true);
52
53     _user_lat_node = fgGetNode("/position/latitude-deg", true);
54     _user_lon_node = fgGetNode("/position/longitude-deg", true);
55     _user_alt_node = fgGetNode("/position/altitude-ft", true);
56
57     _user_heading_node = fgGetNode("/orientation/heading-deg", true);
58     _user_pitch_node =   fgGetNode("/orientation/pitch-deg", true);
59     _user_roll_node =    fgGetNode("/orientation/roll-deg", true);
60     _user_yaw_node =     fgGetNode("/orientation/yaw-deg", true);
61     _user_alpha_node =   fgGetNode("/orientation/alpha-deg", true);
62
63     _user_speed_node = fgGetNode("/velocities/uBody-fps", true);
64
65     _user_wind_from_east_node  = fgGetNode("/environment/wind-from-east-fps", true);
66     _user_wind_from_north_node = fgGetNode("/environment/wind-from-north-fps", true);
67
68     _user_speed_down_fps_node   = fgGetNode("/velocities/speed-down-fps", true);
69     _user_speed_east_fps_node   = fgGetNode("/velocities/speed-east-fps", true);
70     _user_speed_north_fps_node  = fgGetNode("/velocities/speed-north-fps", true);
71
72     _contrail_altitude_node = fgGetNode("/environment/params/contrail-altitude", true);
73     contrail_altitude       = _contrail_altitude_node->getDoubleValue();
74     _contrail_trigger       = fgGetNode("ai/submodels/contrails", true);
75     _contrail_trigger->setBoolValue(false);
76
77     ai = (FGAIManager*)globals->get_subsystem("ai_model");
78
79     load();
80 }
81
82 void FGSubmodelMgr::postinit() {
83     // postinit, so that the AI list is populated
84     loadAI();
85     loadSubmodels();
86
87     //TODO reload submodels if an MP ac joins
88 }
89
90 void FGSubmodelMgr::bind()
91 {}
92
93 void FGSubmodelMgr::unbind()
94 {
95     submodel_iterator = submodels.begin();
96     while (submodel_iterator != submodels.end()) {
97         (*submodel_iterator)->prop->untie("count");
98         ++submodel_iterator;
99     }
100 }
101
102 void FGSubmodelMgr::update(double dt)
103 {
104     if (!_serviceable_node->getBoolValue())
105         return;
106
107     _impact = false;
108     _hit = false;
109
110     // check if the submodel hit an object or terrain
111     sm_list = ai->get_ai_list();
112     sm_list_iterator sm_list_itr = sm_list.begin();
113     sm_list_iterator end = sm_list.end();
114
115     for (; sm_list_itr != end; ++sm_list_itr) {
116         _impact = (*sm_list_itr)->_getImpactData();
117         _hit = (*sm_list_itr)->_getCollisionData();
118         int parent_subID = (*sm_list_itr)->_getSubID();
119         SG_LOG(SG_GENERAL, SG_DEBUG, "Submodel: Impact " << _impact << " hit! "
120                 << _hit <<" parent_subID " << parent_subID);
121         if ( parent_subID == 0) // this entry in the list has no associated submodel
122             continue;           // so we can continue
123
124         if (_impact || _hit) {
125             SG_LOG(SG_GENERAL, SG_DEBUG, "Submodel: Impact " << _impact << " hit! " << _hit );
126
127             submodel_iterator = submodels.begin();
128
129             while (submodel_iterator != submodels.end()) {
130                 int child_ID = (*submodel_iterator)->id;
131                 //cout << "Impact: parent SubID " << parent_subID << " child_ID " << child_ID << endl;
132
133                 if ( parent_subID == child_ID ) {
134                     _parent_lat = (*sm_list_itr)->_getImpactLat();
135                     _parent_lon = (*sm_list_itr)->_getImpactLon();
136                     _parent_elev = (*sm_list_itr)->_getImpactElevFt();
137                     _parent_hdg = (*sm_list_itr)->_getImpactHdg();
138                     _parent_pitch = (*sm_list_itr)->_getImpactPitch();
139                     _parent_roll = (*sm_list_itr)->_getImpactRoll();
140                     _parent_speed = (*sm_list_itr)->_getImpactSpeed();
141                     (*submodel_iterator)->first_time = true;
142
143                     if (release(*submodel_iterator, dt))
144                         (*sm_list_itr)->setDie(true);
145                 }
146
147                 ++submodel_iterator;
148             }
149         }
150     }
151
152     _contrail_trigger->setBoolValue(_user_alt_node->getDoubleValue() > contrail_altitude);
153
154
155     bool in_range = true;
156     bool trigger = false;
157     int i = -1;
158
159     submodel_iterator = submodels.begin();
160     while (submodel_iterator != submodels.end())  {
161         i++;
162         in_range = true;
163
164         SG_LOG(SG_GENERAL, SG_DEBUG,
165                 "Submodels:  " << (*submodel_iterator)->id
166                 << " name " << (*submodel_iterator)->name
167                 << " in range " << in_range);
168
169         if ((*submodel_iterator)->trigger_node != 0) {
170             _trigger_node = (*submodel_iterator)->trigger_node;
171             trigger = _trigger_node->getBoolValue();
172             //cout << "trigger node found " <<  trigger << endl;
173         } else {
174             trigger = true;
175             //cout << (*submodel_iterator)->name << "trigger node not found " << trigger << endl;
176         }
177
178         if (trigger) {
179             int id = (*submodel_iterator)->id;
180             string name = (*submodel_iterator)->name;
181             // don't release submodels from AI Objects if they are
182             // too far away to be seen. id 0 is not an AI model,
183             // so we can skip the whole process
184             sm_list_iterator sm_list_itr = sm_list.begin();
185             sm_list_iterator end = sm_list.end();
186
187             while (sm_list_itr != end) {
188
189                 if (id == 0) {
190                     SG_LOG(SG_GENERAL, SG_DEBUG,
191                             "Submodels: continuing: " << id << " name " << name );
192                     in_range = true;
193                     ++sm_list_itr;
194                     continue;
195                 }
196
197                 int parent_id = (*submodel_iterator)->id;
198
199                 if (parent_id == id) {
200                     double parent_lat = (*sm_list_itr)->_getLatitude();
201                     double parent_lon = (*sm_list_itr)->_getLongitude();
202                     double own_lat    = _user_lat_node->getDoubleValue();
203                     double own_lon    = _user_lon_node->getDoubleValue();
204                     double range_nm   = getRange(parent_lat, parent_lon, own_lat, own_lon);
205                     // cout << "parent " << parent_id << ", "<< parent_lat << ", " << parent_lon << endl;
206                     //cout << "own " << own_lat << ", " << own_lon << " range " << range_nm << endl;
207
208                     if (range_nm > 15) {
209                         SG_LOG(SG_GENERAL, SG_DEBUG,
210                                 "Submodels: skipping release: " << id);
211                         in_range = false;
212                     }
213                 }
214
215                 ++sm_list_itr;
216             } // end while
217
218             SG_LOG(SG_GENERAL, SG_DEBUG,
219                     "Submodels end:  " << (*submodel_iterator)->id
220                     << " name " << (*submodel_iterator)->name
221                     << " count " << (*submodel_iterator)->count
222                     << " in range " << in_range);
223
224             if ((*submodel_iterator)->count != 0 && in_range)
225                 release(*submodel_iterator, dt);
226
227         } else
228             (*submodel_iterator)->first_time = true;
229
230         ++submodel_iterator;
231     } // end while
232 }
233
234 bool FGSubmodelMgr::release(submodel *sm, double dt)
235 {
236     //cout << "release id " << sm->id << " name " << sm->name
237     //<< " first time " << sm->first_time  << " repeat " << sm->repeat  <<
238     //    endl;
239
240     // only run if first time or repeat is set to true
241     if (!sm->first_time && !sm->repeat) {
242         //cout<< "not first time " << sm->first_time<< " repeat " << sm->repeat <<endl;
243         return false;
244     }
245
246     sm->timer += dt;
247
248     if (sm->timer < sm->delay) {
249         //cout << "not yet: timer" << sm->timer << " delay " << sm->delay<< endl;
250         return false;
251     }
252
253     sm->timer = 0.0;
254
255     if (sm->first_time) {
256         dt = 0.0;
257         sm->first_time = false;
258     }
259
260     transform(sm);  // calculate submodel's initial conditions in world-coordinates
261
262     FGAIBallistic* ballist = new FGAIBallistic;
263     ballist->setPath(sm->model.c_str());
264     ballist->setLatitude(IC.lat);
265     ballist->setLongitude(IC.lon);
266     ballist->setAltitude(IC.alt);
267     ballist->setAzimuth(IC.azimuth);
268     ballist->setElevation(IC.elevation);
269     ballist->setRoll(IC.roll);
270     ballist->setSpeed(IC.speed / SG_KT_TO_FPS);
271     ballist->setWind_from_east(IC.wind_from_east);
272     ballist->setWind_from_north(IC.wind_from_north);
273     ballist->setMass(IC.mass);
274     ballist->setDragArea(sm->drag_area);
275     ballist->setLife(sm->life);
276     ballist->setBuoyancy(sm->buoyancy);
277     ballist->setWind(sm->wind);
278     ballist->setCd(sm->cd);
279     ballist->setStabilisation(sm->aero_stabilised);
280     ballist->setNoRoll(sm->no_roll);
281     ballist->setName(sm->name);
282     ballist->setCollision(sm->collision);
283     ballist->setImpact(sm->impact);
284     ballist->setImpactReportNode(sm->impact_report);
285     ballist->setFuseRange(sm->fuse_range);
286     ballist->setSubmodel(sm->submodel.c_str());
287     ballist->setSubID(sm->sub_id);
288     ai->attach(ballist);
289
290     if (sm->count > 0)
291         sm->count--;
292
293     return true;
294 }
295
296 void FGSubmodelMgr::load()
297 {
298     SGPropertyNode *path = fgGetNode("/sim/submodels/path");
299
300     if (path) {
301         const int id = 0;
302         string Path = path->getStringValue();
303         bool Seviceable =_serviceable_node->getBoolValue();
304         setData(id, Path, Seviceable);
305     }
306 }
307
308 void FGSubmodelMgr::transform(submodel *sm)
309 {
310     // set initial conditions
311     if (sm->contents_node != 0) {
312         // get the weight of the contents (lbs) and convert to mass (slugs)
313         sm->contents = sm->contents_node->getDoubleValue();
314         IC.mass = (sm->weight + sm->contents) * lbs_to_slugs;
315
316         // set contents to 0 in the parent
317         sm->contents_node->setDoubleValue(0);
318     } else {
319         IC.mass = sm->weight * lbs_to_slugs;
320     }
321
322     // cout << "mass "  << IC.mass << endl;
323
324     if (sm->speed_node != 0)
325         sm->speed = sm->speed_node->getDoubleValue();
326
327     int id = sm->id;
328     //int sub_id = (*submodel)->sub_id;
329     string name = sm->name;
330
331     //cout << " name " << name << " id " << id << " sub id" << sub_id << endl;
332
333     if (_impact || _hit) {
334         // set the data for a submodel tied to a submodel
335         _count++;
336         //cout << "Submodels: release sub sub " << _count<< endl;
337         //cout << " id " << sm->id
338         //    << " lat " << _parent_lat
339         //    << " lon " << _parent_lon
340         //    << " elev " << _parent_elev
341         //    << " name " << sm->name
342         //    << endl;
343
344         IC.lat             = _parent_lat;
345         IC.lon             = _parent_lon;
346         IC.alt             = _parent_elev;
347         IC.roll            = _parent_roll;    // rotation about x axis
348         IC.elevation       = _parent_pitch;   // rotation about y axis
349         IC.azimuth         = _parent_hdg;     // rotation about z axis
350         IC.speed           = _parent_speed;
351         IC.speed_down_fps  = 0;
352         IC.speed_east_fps  = 0;
353         IC.speed_north_fps = 0;
354
355     } else if (id == 0) {
356         //set the data for a submodel tied to the main model
357         /*cout << "Submodels: release main sub " << endl;
358         cout << " name " << sm->name
359         << " id" << sm->id
360         << endl;*/
361         IC.lat             = _user_lat_node->getDoubleValue();
362         IC.lon             = _user_lon_node->getDoubleValue();
363         IC.alt             = _user_alt_node->getDoubleValue();
364         IC.roll            = _user_roll_node->getDoubleValue();    // rotation about x axis
365         IC.elevation       = _user_pitch_node->getDoubleValue();   // rotation about y axis
366         IC.azimuth         = _user_heading_node->getDoubleValue(); // rotation about z axis
367         IC.speed           = _user_speed_node->getDoubleValue();
368         IC.speed_down_fps  = _user_speed_down_fps_node->getDoubleValue();
369         IC.speed_east_fps  = _user_speed_east_fps_node->getDoubleValue();
370         IC.speed_north_fps = _user_speed_north_fps_node->getDoubleValue();
371
372     } else {
373         // set the data for a submodel tied to an AI Object
374         sm_list_iterator sm_list_itr = sm_list.begin();
375         sm_list_iterator end = sm_list.end();
376
377         while (sm_list_itr != end) {
378             int parent_id = (*sm_list_itr)->getID();
379
380             if (id != parent_id) {
381                 ++sm_list_itr;
382                 continue;
383             }
384
385             //cout << "found id " << id << endl;
386             IC.lat             = (*sm_list_itr)->_getLatitude();
387             IC.lon             = (*sm_list_itr)->_getLongitude();
388             IC.alt             = (*sm_list_itr)->_getAltitude();
389             IC.roll            = (*sm_list_itr)->_getRoll();
390             IC.elevation       = (*sm_list_itr)->_getPitch();
391             IC.azimuth         = (*sm_list_itr)->_getHeading();
392             IC.alt             = (*sm_list_itr)->_getAltitude();
393             IC.speed           = (*sm_list_itr)->_getSpeed() * SG_KT_TO_FPS;
394             IC.speed_down_fps  = -(*sm_list_itr)->_getVS_fps();
395             IC.speed_east_fps  = (*sm_list_itr)->_get_speed_east_fps();
396             IC.speed_north_fps = (*sm_list_itr)->_get_speed_north_fps();
397
398             ++sm_list_itr;
399         }
400     }
401
402     /*cout << "heading " << IC.azimuth << endl ;
403     cout << "speed down " << IC.speed_down_fps << endl ;
404     cout << "speed east " << IC.speed_east_fps << endl ;
405     cout << "speed north " << IC.speed_north_fps << endl ;
406     cout << "parent speed fps in" << IC.speed << "sm speed in " << sm->speed << endl ;*/
407
408     IC.wind_from_east =  _user_wind_from_east_node->getDoubleValue();
409     IC.wind_from_north = _user_wind_from_north_node->getDoubleValue();
410
411     in[0] = sm->x_offset;
412     in[1] = sm->y_offset;
413     in[2] = sm->z_offset;
414
415     // pre-process the trig functions
416     cosRx = cos(-IC.roll * SG_DEGREES_TO_RADIANS);
417     sinRx = sin(-IC.roll * SG_DEGREES_TO_RADIANS);
418     cosRy = cos(-IC.elevation * SG_DEGREES_TO_RADIANS);
419     sinRy = sin(-IC.elevation * SG_DEGREES_TO_RADIANS);
420     cosRz = cos(IC.azimuth * SG_DEGREES_TO_RADIANS);
421     sinRz = sin(IC.azimuth * SG_DEGREES_TO_RADIANS);
422
423     // set up the transform matrix
424     trans[0][0] =  cosRy * cosRz;
425     trans[0][1] =  -1 * cosRx * sinRz + sinRx * sinRy * cosRz ;
426     trans[0][2] =  sinRx * sinRz + cosRx * sinRy * cosRz;
427
428     trans[1][0] =  cosRy * sinRz;
429     trans[1][1] =  cosRx * cosRz + sinRx * sinRy * sinRz;
430     trans[1][2] =  -1 * sinRx * cosRx + cosRx * sinRy * sinRz;
431
432     trans[2][0] =  -1 * sinRy;
433     trans[2][1] =  sinRx * cosRy;
434     trans[2][2] =  cosRx * cosRy;
435
436
437     // multiply the input and transform matrices
438     out[0] = in[0] * trans[0][0] + in[1] * trans[0][1] + in[2] * trans[0][2];
439     out[1] = in[0] * trans[1][0] + in[1] * trans[1][1] + in[2] * trans[1][2];
440     out[2] = in[0] * trans[2][0] + in[1] * trans[2][1] + in[2] * trans[2][2];
441
442     // convert ft to degrees of latitude
443     out[0] = out[0] / (366468.96 - 3717.12 * cos(IC.lat * SG_DEGREES_TO_RADIANS));
444
445     // convert ft to degrees of longitude
446     out[1] = out[1] / (365228.16 * cos(IC.lat * SG_DEGREES_TO_RADIANS));
447
448     // set submodel initial position
449     IC.lat += out[0];
450     IC.lon += out[1];
451     IC.alt += out[2];
452
453     // get aircraft velocity vector angles in XZ and XY planes
454     //double alpha = _user_alpha_node->getDoubleValue();
455     //double velXZ = IC.elevation - alpha * cosRx;
456     //double velXY = IC.azimuth - (IC.elevation - alpha * sinRx);
457
458     // Get submodel initial velocity vector angles in XZ and XY planes.
459     // This needs to be fixed. This vector should be added to aircraft's vector.
460     IC.elevation += (sm->yaw_offset * sinRx) + (sm->pitch_offset * cosRx);
461     IC.azimuth   += (sm->yaw_offset * cosRx) - (sm->pitch_offset * sinRx);
462
463     // calculate the total speed north
464     IC.total_speed_north = sm->speed * cos(IC.elevation * SG_DEGREES_TO_RADIANS)
465             * cos(IC.azimuth * SG_DEGREES_TO_RADIANS) + IC.speed_north_fps;
466
467     // calculate the total speed east
468     IC.total_speed_east = sm->speed * cos(IC.elevation * SG_DEGREES_TO_RADIANS)
469             * sin(IC.azimuth * SG_DEGREES_TO_RADIANS) + IC.speed_east_fps;
470
471     // calculate the total speed down
472     IC.total_speed_down = sm->speed * -sin(IC.elevation * SG_DEGREES_TO_RADIANS)
473             + IC.speed_down_fps;
474
475     // re-calculate speed, elevation and azimuth
476     IC.speed = sqrt(IC.total_speed_north * IC.total_speed_north
477             + IC.total_speed_east * IC.total_speed_east
478             + IC.total_speed_down * IC.total_speed_down);
479
480     // if speeds are low this calculation can become unreliable
481     if (IC.speed > 1) {
482         IC.azimuth = atan2(IC.total_speed_east , IC.total_speed_north) * SG_RADIANS_TO_DEGREES;
483         //        cout << "azimuth1 " << IC.azimuth<<endl;
484
485         // rationalise the output
486         if (IC.azimuth < 0)
487             IC.azimuth += 360;
488         else if (IC.azimuth >= 360)
489             IC.azimuth -= 360;
490     }
491
492     // cout << "azimuth2 " << IC.azimuth<<endl;
493
494     IC.elevation = -atan(IC.total_speed_down / sqrt(IC.total_speed_north
495             * IC.total_speed_north + IC.total_speed_east * IC.total_speed_east))
496             * SG_RADIANS_TO_DEGREES;
497 }
498
499 void FGSubmodelMgr::updatelat(double lat)
500 {
501     ft_per_deg_latitude = 366468.96 - 3717.12 * cos(lat / SG_RADIANS_TO_DEGREES);
502     ft_per_deg_longitude = 365228.16 * cos(lat / SG_RADIANS_TO_DEGREES);
503 }
504
505 void FGSubmodelMgr::loadAI()
506 {
507     SG_LOG(SG_GENERAL, SG_DEBUG, "Submodels: Loading AI submodels ");
508
509     sm_list = ai->get_ai_list();
510
511     if (sm_list.empty()) {
512         SG_LOG(SG_GENERAL, SG_DEBUG, "Submodels: Unable to read AI submodel list");
513         return;
514     }
515
516     sm_list_iterator sm_list_itr = sm_list.begin();
517     sm_list_iterator end = sm_list.end();
518
519     while (sm_list_itr != end) {
520         string path = (*sm_list_itr)->_getSMPath();
521
522         if (path.empty()) {
523             ++sm_list_itr;
524             continue;
525         }
526
527         int id = (*sm_list_itr)->getID();
528         bool serviceable = (*sm_list_itr)->_getServiceable();
529         setData(id, path, serviceable);
530         ++sm_list_itr;
531     }
532 }
533
534
535 double FGSubmodelMgr::getRange(double lat, double lon, double lat2, double lon2) const
536 {
537     double course, distance, az2;
538
539     //calculate the bearing and range of the second pos from the first
540     geo_inverse_wgs_84(lat, lon, lat2, lon2, &course, &az2, &distance);
541     distance *= SG_METER_TO_NM;
542     return distance;
543 }
544
545 void FGSubmodelMgr::setData(int id, string& path, bool serviceable)
546 {
547     SGPropertyNode root;
548
549     SGPath config(globals->get_fg_root());
550     config.append(path);
551     SG_LOG(SG_GENERAL, SG_DEBUG, "Submodels: path " << path);
552     try {
553         SG_LOG(SG_GENERAL, SG_DEBUG,
554                 "Submodels: Trying to read AI submodels file: " << config.str());
555         readProperties(config.str(), &root);
556     } catch (const sg_exception &e) {
557         SG_LOG(SG_GENERAL, SG_DEBUG,
558                 "Submodels: Unable to read AI submodels file: " << config.str());
559         return;
560     }
561
562     vector<SGPropertyNode_ptr> children = root.getChildren("submodel");
563     vector<SGPropertyNode_ptr>::iterator it = children.begin();
564     vector<SGPropertyNode_ptr>::iterator end = children.end();
565
566     for (int i = 0; it != end; ++it, i++) {
567         //cout << "Reading AI submodel " << (*it)->getPath() << endl;
568         submodel* sm = new submodel;
569         SGPropertyNode * entry_node = *it;
570         sm->name            = entry_node->getStringValue("name", "none_defined");
571         sm->model           = entry_node->getStringValue("model", "Models/Geometry/rocket.ac");
572         sm->speed           = entry_node->getDoubleValue("speed", 2329.4);
573         sm->repeat          = entry_node->getBoolValue("repeat", false);
574         sm->delay           = entry_node->getDoubleValue("delay", 0.25);
575         sm->count           = entry_node->getIntValue("count", 1);
576         sm->slaved          = entry_node->getBoolValue("slaved", false);
577         sm->x_offset        = entry_node->getDoubleValue("x-offset", 0.0);
578         sm->y_offset        = entry_node->getDoubleValue("y-offset", 0.0);
579         sm->z_offset        = entry_node->getDoubleValue("z-offset", 0.0);
580         sm->yaw_offset      = entry_node->getDoubleValue("yaw-offset", 0.0);
581         sm->pitch_offset    = entry_node->getDoubleValue("pitch-offset", 0.0);
582         sm->drag_area       = entry_node->getDoubleValue("eda", 0.034);
583         sm->life            = entry_node->getDoubleValue("life", 900.0);
584         sm->buoyancy        = entry_node->getDoubleValue("buoyancy", 0);
585         sm->wind            = entry_node->getBoolValue("wind", false);
586         sm->cd              = entry_node->getDoubleValue("cd", 0.193);
587         sm->weight          = entry_node->getDoubleValue("weight", 0.25);
588         sm->aero_stabilised = entry_node->getBoolValue("aero-stabilised", true);
589         sm->no_roll         = entry_node->getBoolValue("no-roll", false);
590         sm->collision       = entry_node->getBoolValue("collision", false);
591         sm->impact          = entry_node->getBoolValue("impact", false);
592         sm->impact_report   = entry_node->getStringValue("impact-reports");
593         sm->fuse_range      = entry_node->getDoubleValue("fuse-range", 0.0);
594         sm->contents_node   = fgGetNode(entry_node->getStringValue("contents", "none"), false);
595         sm->speed_node      = fgGetNode(entry_node->getStringValue("speed-node", "none"), false);
596         sm->submodel        = entry_node->getStringValue("submodel-path", "");
597
598         //cout <<  "sm->contents_node " << sm->contents_node << endl;
599         if (sm->contents_node != 0)
600             sm->contents = sm->contents_node->getDoubleValue();
601
602         const char *trigger_path = entry_node->getStringValue("trigger", 0);
603         if (trigger_path) {
604             sm->trigger_node = fgGetNode(trigger_path, true);
605             sm->trigger_node->setBoolValue(sm->trigger_node->getBoolValue());
606         } else {
607             sm->trigger_node = 0;
608         }
609
610         SG_LOG(SG_GENERAL, SG_DEBUG, "Submodels: trigger " << sm->trigger_node->getBoolValue() );
611         if (sm->speed_node != 0)
612             sm->speed = sm->speed_node->getDoubleValue();
613
614         sm->timer = sm->delay;
615         sm->id = id;
616         sm->first_time = false;
617         sm->serviceable = serviceable;
618         sm->sub_id = 0;
619
620         sm->prop = fgGetNode("/ai/submodels/submodel", index, true);
621         sm->prop->tie("count", SGRawValuePointer<int>(&(sm->count)));
622         sm->prop->tie("repeat", SGRawValuePointer<bool>(&(sm->repeat)));
623         sm->prop->tie("id", SGRawValuePointer<int>(&(sm->id)));
624         sm->prop->tie("sub-id", SGRawValuePointer<int>(&(sm->sub_id)));
625
626         sm->prop->tie("serviceable", SGRawValuePointer<bool>(&(sm->serviceable)));
627         string name = sm->name;
628         sm->prop->setStringValue("name", name.c_str());
629
630         string submodel = sm->submodel;
631         sm->prop->setStringValue("submodel", submodel.c_str());
632         //cout << " set submodel path " << submodel << endl;
633
634         if (sm->contents_node != 0)
635             sm->prop->tie("contents-lbs", SGRawValuePointer<double>(&(sm->contents)));
636
637         index++;
638         submodels.push_back(sm);
639     }
640 }
641
642 void FGSubmodelMgr::setSubData(int id, string& path, bool serviceable)
643 {
644     SGPropertyNode root;
645
646     SGPath config(globals->get_fg_root());
647     config.append(path);
648     SG_LOG(SG_GENERAL, SG_DEBUG,
649         "Submodels: path " << path);
650     try {
651         SG_LOG(SG_GENERAL, SG_DEBUG,
652                 "Submodels: Trying to read AI submodels file: " << config.str());
653         readProperties(config.str(), &root);
654
655     } catch (const sg_exception &e) {
656         SG_LOG(SG_GENERAL, SG_DEBUG,
657                 "Submodels: Unable to read AI submodels file: " << config.str());
658         return;
659     }
660
661     vector<SGPropertyNode_ptr> children = root.getChildren("submodel");
662     vector<SGPropertyNode_ptr>::iterator it = children.begin();
663     vector<SGPropertyNode_ptr>::iterator end = children.end();
664
665     for (int i = 0; it != end; ++it, i++) {
666         //cout << "Reading AI submodel " << (*it)->getPath() << endl;
667         submodel* sm = new submodel;
668         SGPropertyNode * entry_node = *it;
669         sm->name            = entry_node->getStringValue("name", "none_defined");
670         sm->model           = entry_node->getStringValue("model", "Models/Geometry/rocket.ac");
671         sm->speed           = entry_node->getDoubleValue("speed", 2329.4);
672         sm->repeat          = entry_node->getBoolValue("repeat", false);
673         sm->delay           = entry_node->getDoubleValue("delay", 0.25);
674         sm->count           = entry_node->getIntValue("count", 1);
675         sm->slaved          = entry_node->getBoolValue("slaved", false);
676         sm->x_offset        = entry_node->getDoubleValue("x-offset", 0.0);
677         sm->y_offset        = entry_node->getDoubleValue("y-offset", 0.0);
678         sm->z_offset        = entry_node->getDoubleValue("z-offset", 0.0);
679         sm->yaw_offset      = entry_node->getDoubleValue("yaw-offset", 0.0);
680         sm->pitch_offset    = entry_node->getDoubleValue("pitch-offset", 0.0);
681         sm->drag_area       = entry_node->getDoubleValue("eda", 0.034);
682         sm->life            = entry_node->getDoubleValue("life", 900.0);
683         sm->buoyancy        = entry_node->getDoubleValue("buoyancy", 0);
684         sm->wind            = entry_node->getBoolValue("wind", false);
685         sm->cd              = entry_node->getDoubleValue("cd", 0.193);
686         sm->weight          = entry_node->getDoubleValue("weight", 0.25);
687         sm->aero_stabilised = entry_node->getBoolValue("aero-stabilised", true);
688         sm->no_roll         = entry_node->getBoolValue("no-roll", false);
689         sm->collision       = entry_node->getBoolValue("collision", false);
690         sm->impact          = entry_node->getBoolValue("impact", false);
691         sm->impact_report   = entry_node->getStringValue("impact-reports");
692         sm->fuse_range      = entry_node->getDoubleValue("fuse-range", 0.0);
693         sm->contents_node   = fgGetNode(entry_node->getStringValue("contents", "none"), false);
694         sm->speed_node      = fgGetNode(entry_node->getStringValue("speed-node", "none"), false);
695         sm->submodel        = entry_node->getStringValue("submodel-path", "");
696
697         //cout <<  "sm->contents_node " << sm->contents_node << endl;
698         if (sm->contents_node != 0)
699             sm->contents = sm->contents_node->getDoubleValue();
700
701         const char *trigger_path = entry_node->getStringValue("trigger", 0);
702         if (trigger_path) {
703             sm->trigger_node = fgGetNode(trigger_path, true);
704             sm->trigger_node->setBoolValue(sm->trigger_node->getBoolValue());
705         } else {
706             sm->trigger_node = 0;
707         }
708
709         if (sm->speed_node != 0)
710             sm->speed = sm->speed_node->getDoubleValue();
711
712         sm->timer = sm->delay;
713         sm->id = index;
714         sm->first_time = false;
715         sm->serviceable = serviceable;
716         sm->sub_id = 0;
717
718         sm->prop = fgGetNode("/ai/submodels/subsubmodel", index, true);
719         sm->prop->tie("count", SGRawValuePointer<int>(&(sm->count)));
720         sm->prop->tie("repeat", SGRawValuePointer<bool>(&(sm->repeat)));
721         sm->prop->tie("id", SGRawValuePointer<int>(&(sm->id)));
722         sm->prop->tie("sub-id", SGRawValuePointer<int>(&(sm->sub_id)));
723         sm->prop->tie("serviceable", SGRawValuePointer<bool>(&(sm->serviceable)));
724         string name = sm->name;
725         sm->prop->setStringValue("name", name.c_str());
726
727         string submodel = sm->submodel;
728         sm->prop->setStringValue("submodel", submodel.c_str());
729         // cout << " set submodel path " << submodel<< endl;
730
731         if (sm->contents_node != 0)
732             sm->prop->tie("contents-lbs", SGRawValuePointer<double>(&(sm->contents)));
733
734         index++;
735         subsubmodels.push_back(sm);
736     }
737 }
738
739 void FGSubmodelMgr::loadSubmodels()
740 {
741     SG_LOG(SG_GENERAL, SG_DEBUG, "Submodels: Loading sub submodels");
742
743     submodel_iterator = submodels.begin();
744
745     while (submodel_iterator != submodels.end()) {
746         string submodel  = (*submodel_iterator)->submodel;
747         if (!submodel.empty()) {
748             //int id = (*submodel_iterator)->id;
749             bool serviceable = true;
750             SG_LOG(SG_GENERAL, SG_DEBUG, "found path sub sub "
751                     << submodel
752                     << " index " << index
753                     << "name " << (*submodel_iterator)->name);
754
755             (*submodel_iterator)->sub_id = index;
756             setSubData(index, submodel, serviceable);
757         }
758
759         ++submodel_iterator;
760     }
761
762     subsubmodel_iterator = subsubmodels.begin();
763
764     while (subsubmodel_iterator != subsubmodels.end()) {
765         submodels.push_back(*subsubmodel_iterator);
766         ++subsubmodel_iterator;
767     }
768
769     submodel_iterator = submodels.begin();
770
771     while (submodel_iterator != submodels.end()) {
772         int id = (*submodel_iterator)->id;
773         SG_LOG(SG_GENERAL, SG_DEBUG,"after pusback "
774                 << " id " << id
775                 << " name " << (*submodel_iterator)->name
776                 << " sub id " << (*submodel_iterator)->sub_id);
777
778         ++submodel_iterator;
779     }
780 }
781
782 // end of submodel.cxx