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