]> git.mxchange.org Git - flightgear.git/blob - src/AIModel/submodel.cxx
d08ceeafba57fa1e8de7c8042b1b987495a462b7
[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
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_ALERT, "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
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
216                 ++sm_list_itr;
217             } // end while
218
219             SG_LOG(SG_GENERAL, SG_DEBUG,
220                 "Submodels end:  " << (*submodel_iterator)->id
221                 << " name " << (*submodel_iterator)->name
222                 << " count " << (*submodel_iterator)->count
223                 << " in range " << in_range
224                 );
225
226             if ((*submodel_iterator)->count != 0 && in_range)
227                 release(*submodel_iterator, dt);
228
229         } else
230             (*submodel_iterator)->first_time = true;
231
232         ++submodel_iterator;
233     } // end while
234 }
235
236 bool FGSubmodelMgr::release(submodel *sm, double dt)
237 {
238     //cout << "release id " << sm->id << " name " << sm->name
239     //<< " first time " << sm->first_time  << " repeat " << (*sm)->repeat  <<
240     //    endl;
241
242     // only run if first time or repeat is set to true
243     if (!sm->first_time && !sm->repeat) {
244         //cout<< "not first time " << sm->first_time<< " repeat " << sm->repeat <<endl;
245         return false;
246     }
247
248     sm->timer += dt;
249
250     if (sm->timer < sm->delay) {
251         //cout << "not yet: timer" << sm->timer << " delay " << sm->delay<< endl;
252         return false;
253     }
254
255     sm->timer = 0.0;
256
257     if (sm->first_time) {
258         dt = 0.0;
259         sm->first_time = false;
260     }
261
262     transform(sm);  // calculate submodel's initial conditions in world-coordinates
263
264     FGAIBallistic* ballist = new FGAIBallistic;
265     ballist->setPath(sm->model.c_str());
266     ballist->setLatitude(IC.lat);
267     ballist->setLongitude(IC.lon);
268     ballist->setAltitude(IC.alt);
269     ballist->setAzimuth(IC.azimuth);
270     ballist->setElevation(IC.elevation);
271     ballist->setRoll(IC.roll);
272     ballist->setSpeed(IC.speed / SG_KT_TO_FPS);
273     ballist->setWind_from_east(IC.wind_from_east);
274     ballist->setWind_from_north(IC.wind_from_north);
275     ballist->setMass(IC.mass);
276     ballist->setDragArea(sm->drag_area);
277     ballist->setLife(sm->life);
278     ballist->setBuoyancy(sm->buoyancy);
279     ballist->setWind(sm->wind);
280     ballist->setCd(sm->cd);
281     ballist->setStabilisation(sm->aero_stabilised);
282     ballist->setNoRoll(sm->no_roll);
283     ballist->setName(sm->name);
284     ballist->setCollision(sm->collision);
285     ballist->setImpact(sm->impact);
286     ballist->setImpactReportNode(sm->impact_report);
287     ballist->setFuseRange(sm->fuse_range);
288     ballist->setSubmodel(sm->submodel.c_str());
289     ballist->setSubID(sm->sub_id);
290     ai->attach(ballist);
291
292     if (sm->count > 0)
293         sm->count--;
294
295     return true;
296 }
297
298 void FGSubmodelMgr::load()
299 {
300     SGPropertyNode *path = fgGetNode("/sim/submodels/path");
301
302     if (path) {
303         const int id = 0;
304         string Path = path->getStringValue();
305         bool Seviceable =_serviceable_node->getBoolValue();
306         setData(id, Path, Seviceable);
307     }
308 }
309
310 void FGSubmodelMgr::transform(submodel *sm)
311 {
312     // set initial conditions
313     if (sm->contents_node != 0) {
314         // get the weight of the contents (lbs) and convert to mass (slugs)
315         sm->contents = sm->contents_node->getDoubleValue();
316         IC.mass = (sm->weight + sm->contents) * lbs_to_slugs;
317
318         // set contents to 0 in the parent
319         sm->contents_node->setDoubleValue(0);
320     } else
321         IC.mass = sm->weight * lbs_to_slugs;
322
323     // cout << "mass "  << IC.mass << endl;
324
325     if (sm->speed_node != 0)
326         sm->speed = sm->speed_node->getDoubleValue();
327
328     int id = sm->id;
329     //int sub_id = (*submodel)->sub_id;
330     string name = sm->name;
331
332     //cout << " name " << name << " id " << id << " sub id" << sub_id << endl;
333
334     if (_impact || _hit) {
335         // set the data for a submodel tied to a submodel
336         _count++;
337         //cout << "Submodels: release sub sub " << _count<< endl;
338         //cout << " id " << sm->id
339         //    << " lat " << _parent_lat
340         //    << " lon " << _parent_lon
341         //    << " elev " << _parent_elev
342         //    << " name " << sm->name
343         //    << endl;
344
345         IC.lat             = _parent_lat;
346         IC.lon             = _parent_lon;
347         IC.alt             = _parent_elev;
348         IC.roll            = _parent_roll;    // rotation about x axis
349         IC.elevation       = _parent_pitch;   // rotation about y axis
350         IC.azimuth         = _parent_hdg;     // rotation about z axis
351         IC.speed           = _parent_speed;
352         IC.speed_down_fps  = 0;
353         IC.speed_east_fps  = 0;
354         IC.speed_north_fps = 0;
355
356     } else if (id == 0) {
357         //set the data for a submodel tied to the main model
358         /*cout << "Submodels: release main sub " << endl;
359         cout << " name " << sm->name
360         << " id" << sm->id
361         << endl;*/
362         IC.lat             = _user_lat_node->getDoubleValue();
363         IC.lon             = _user_lon_node->getDoubleValue();
364         IC.alt             = _user_alt_node->getDoubleValue();
365         IC.roll            = _user_roll_node->getDoubleValue();    // rotation about x axis
366         IC.elevation       = _user_pitch_node->getDoubleValue();   // rotation about y axis
367         IC.azimuth         = _user_heading_node->getDoubleValue(); // rotation about z axis
368         IC.speed           = _user_speed_node->getDoubleValue();
369         IC.speed_down_fps  = _user_speed_down_fps_node->getDoubleValue();
370         IC.speed_east_fps  = _user_speed_east_fps_node->getDoubleValue();
371         IC.speed_north_fps = _user_speed_north_fps_node->getDoubleValue();
372
373     } else {
374         // set the data for a submodel tied to an AI Object
375         sm_list_iterator sm_list_itr = sm_list.begin();
376         sm_list_iterator end = sm_list.end();
377
378         while (sm_list_itr != end) {
379             int parent_id = (*sm_list_itr)->getID();
380
381             if (id != parent_id) {
382                 ++sm_list_itr;
383                 continue;
384             }
385
386             //cout << "found id " << id << endl;
387             IC.lat             = (*sm_list_itr)->_getLatitude();
388             IC.lon             = (*sm_list_itr)->_getLongitude();
389             IC.alt             = (*sm_list_itr)->_getAltitude();
390             IC.roll            = (*sm_list_itr)->_getRoll();
391             IC.elevation       = (*sm_list_itr)->_getPitch();
392             IC.azimuth         = (*sm_list_itr)->_getHeading();
393             IC.alt             = (*sm_list_itr)->_getAltitude();
394             IC.speed           = (*sm_list_itr)->_getSpeed() * SG_KT_TO_FPS;
395             IC.speed_down_fps  = -(*sm_list_itr)->_getVS_fps();
396             IC.speed_east_fps  = (*sm_list_itr)->_get_speed_east_fps();
397             IC.speed_north_fps = (*sm_list_itr)->_get_speed_north_fps();
398
399             ++sm_list_itr;
400         }
401     }
402
403     /*cout << "heading " << IC.azimuth << endl ;
404     cout << "speed down " << IC.speed_down_fps << endl ;
405     cout << "speed east " << IC.speed_east_fps << endl ;
406     cout << "speed north " << IC.speed_north_fps << endl ;
407     cout << "parent speed fps in" << IC.speed << "sm speed in " << sm->speed << endl ;*/
408
409     IC.wind_from_east =  _user_wind_from_east_node->getDoubleValue();
410     IC.wind_from_north = _user_wind_from_north_node->getDoubleValue();
411
412     in[0] = sm->x_offset;
413     in[1] = sm->y_offset;
414     in[2] = sm->z_offset;
415
416     // pre-process the trig functions
417     cosRx = cos(-IC.roll * SG_DEGREES_TO_RADIANS);
418     sinRx = sin(-IC.roll * SG_DEGREES_TO_RADIANS);
419     cosRy = cos(-IC.elevation * SG_DEGREES_TO_RADIANS);
420     sinRy = sin(-IC.elevation * SG_DEGREES_TO_RADIANS);
421     cosRz = cos(IC.azimuth * SG_DEGREES_TO_RADIANS);
422     sinRz = sin(IC.azimuth * SG_DEGREES_TO_RADIANS);
423
424     // set up the transform matrix
425     trans[0][0] =  cosRy * cosRz;
426     trans[0][1] =  -1 * cosRx * sinRz + sinRx * sinRy * cosRz ;
427     trans[0][2] =  sinRx * sinRz + cosRx * sinRy * cosRz;
428
429     trans[1][0] =  cosRy * sinRz;
430     trans[1][1] =  cosRx * cosRz + sinRx * sinRy * sinRz;
431     trans[1][2] =  -1 * sinRx * cosRx + cosRx * sinRy * sinRz;
432
433     trans[2][0] =  -1 * sinRy;
434     trans[2][1] =  sinRx * cosRy;
435     trans[2][2] =  cosRx * cosRy;
436
437
438     // multiply the input and transform matrices
439     out[0] = in[0] * trans[0][0] + in[1] * trans[0][1] + in[2] * trans[0][2];
440     out[1] = in[0] * trans[1][0] + in[1] * trans[1][1] + in[2] * trans[1][2];
441     out[2] = in[0] * trans[2][0] + in[1] * trans[2][1] + in[2] * trans[2][2];
442
443     // convert ft to degrees of latitude
444     out[0] = out[0] / (366468.96 - 3717.12 * cos(IC.lat * SG_DEGREES_TO_RADIANS));
445
446     // convert ft to degrees of longitude
447     out[1] = out[1] / (365228.16 * cos(IC.lat * SG_DEGREES_TO_RADIANS));
448
449     // set submodel initial position
450     IC.lat += out[0];
451     IC.lon += out[1];
452     IC.alt += out[2];
453
454     // get aircraft velocity vector angles in XZ and XY planes
455     //double alpha = _user_alpha_node->getDoubleValue();
456     //double velXZ = IC.elevation - alpha * cosRx;
457     //double velXY = IC.azimuth - (IC.elevation - alpha * sinRx);
458
459     // Get submodel initial velocity vector angles in XZ and XY planes.
460     // This needs to be fixed. This vector should be added to aircraft's vector.
461     IC.elevation += (sm->yaw_offset * sinRx) + (sm->pitch_offset * cosRx);
462     IC.azimuth   += (sm->yaw_offset * cosRx) - (sm->pitch_offset * sinRx);
463
464     // calculate the total speed north
465     IC.total_speed_north = sm->speed * cos(IC.elevation * SG_DEGREES_TO_RADIANS)
466         * cos(IC.azimuth * SG_DEGREES_TO_RADIANS) + IC.speed_north_fps;
467
468     // calculate the total speed east
469     IC.total_speed_east = sm->speed * cos(IC.elevation * SG_DEGREES_TO_RADIANS)
470         * sin(IC.azimuth * SG_DEGREES_TO_RADIANS) + IC.speed_east_fps;
471
472     // calculate the total speed down
473     IC.total_speed_down = sm->speed * -sin(IC.elevation * SG_DEGREES_TO_RADIANS)
474         + IC.speed_down_fps;
475
476     // re-calculate speed, elevation and azimuth
477     IC.speed = sqrt(IC.total_speed_north * IC.total_speed_north
478         + IC.total_speed_east * IC.total_speed_east
479         + IC.total_speed_down * IC.total_speed_down);
480
481     // if speeds are low these calculations can become unreliable
482     if (IC.speed > 1) {
483         IC.azimuth = atan2(IC.total_speed_east , IC.total_speed_north) * SG_RADIANS_TO_DEGREES;
484         //        cout << "azimuth1 " << IC.azimuth<<endl;
485
486         // rationalise the output
487         if (IC.azimuth < 0)
488             IC.azimuth += 360;
489         else if (IC.azimuth >= 360)
490             IC.azimuth -= 360;
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
500 void FGSubmodelMgr::updatelat(double lat)
501 {
502     ft_per_deg_latitude = 366468.96 - 3717.12 * cos(lat / SG_RADIANS_TO_DEGREES);
503     ft_per_deg_longitude = 365228.16 * cos(lat / SG_RADIANS_TO_DEGREES);
504 }
505
506 void FGSubmodelMgr::loadAI()
507 {
508     SG_LOG(SG_GENERAL, SG_DEBUG, "Submodels: Loading AI submodels ");
509
510     sm_list = ai->get_ai_list();
511
512     if (sm_list.empty()) {
513         SG_LOG(SG_GENERAL, SG_DEBUG, "Submodels: Unable to read AI submodel list");
514         return;
515     }
516
517     sm_list_iterator sm_list_itr = sm_list.begin();
518     sm_list_iterator end = sm_list.end();
519
520     while (sm_list_itr != end) {
521         string path = (*sm_list_itr)->_getPath();
522
523         if (path.empty()) {
524             ++sm_list_itr;
525             continue;
526         }
527
528         int id = (*sm_list_itr)->getID();
529         bool serviceable = (*sm_list_itr)->_getServiceable();
530         setData(id, path, serviceable);
531         ++sm_list_itr;
532     }
533 }
534
535
536 double FGSubmodelMgr::getRange(double lat, double lon, double lat2, double lon2) const
537 {
538     double course, distance, az2;
539
540     //calculate the bearing and range of the second pos from the first
541     geo_inverse_wgs_84(lat, lon, lat2, lon2, &course, &az2, &distance);
542     distance *= SG_METER_TO_NM;
543     return distance;
544 }
545
546 void FGSubmodelMgr::setData(int id, string& path, bool serviceable)
547 {
548     SGPropertyNode root;
549
550     SGPath config(globals->get_fg_root());
551     config.append(path);
552     SG_LOG(SG_GENERAL, SG_DEBUG, "Submodels: path " << path);
553
554     try {
555         SG_LOG(SG_GENERAL, SG_DEBUG,
556             "Submodels: Trying to read AI submodels file: " << config.str());
557         readProperties(config.str(), &root);
558
559     } catch (const sg_exception &e) {
560         SG_LOG(SG_GENERAL, SG_DEBUG,
561             "Submodels: Unable to read AI submodels file: " << config.str());
562         return;
563     }
564
565     vector<SGPropertyNode_ptr> children = root.getChildren("submodel");
566     vector<SGPropertyNode_ptr>::iterator it = children.begin();
567     vector<SGPropertyNode_ptr>::iterator end = children.end();
568
569     for (int i = 0; it != end; ++it, i++) {
570         //cout << "Reading AI submodel " << (*it)->getPath() << endl;
571         submodel* sm = new submodel;
572         SGPropertyNode * entry_node = *it;
573         sm->name            = entry_node->getStringValue("name", "none_defined");
574         sm->model           = entry_node->getStringValue("model", "Models/Geometry/rocket.ac");
575         sm->speed           = entry_node->getDoubleValue("speed", 2329.4);
576         sm->repeat          = entry_node->getBoolValue("repeat", false);
577         sm->delay           = entry_node->getDoubleValue("delay", 0.25);
578         sm->count           = entry_node->getIntValue("count", 1);
579         sm->slaved          = entry_node->getBoolValue("slaved", false);
580         sm->x_offset        = entry_node->getDoubleValue("x-offset", 0.0);
581         sm->y_offset        = entry_node->getDoubleValue("y-offset", 0.0);
582         sm->z_offset        = entry_node->getDoubleValue("z-offset", 0.0);
583         sm->yaw_offset      = entry_node->getDoubleValue("yaw-offset", 0.0);
584         sm->pitch_offset    = entry_node->getDoubleValue("pitch-offset", 0.0);
585         sm->drag_area       = entry_node->getDoubleValue("eda", 0.034);
586         sm->life            = entry_node->getDoubleValue("life", 900.0);
587         sm->buoyancy        = entry_node->getDoubleValue("buoyancy", 0);
588         sm->wind            = entry_node->getBoolValue("wind", false);
589         sm->cd              = entry_node->getDoubleValue("cd", 0.193);
590         sm->weight          = entry_node->getDoubleValue("weight", 0.25);
591         sm->aero_stabilised = entry_node->getBoolValue("aero-stabilised", true);
592         sm->no_roll         = entry_node->getBoolValue("no-roll", false);
593         sm->collision       = entry_node->getBoolValue("collision", false);
594         sm->impact          = entry_node->getBoolValue("impact", false);
595         sm->impact_report   = entry_node->getStringValue("impact-reports");
596         sm->fuse_range      = entry_node->getDoubleValue("fuse-range", 0.0);
597         sm->contents_node   = fgGetNode(entry_node->getStringValue("contents", "none"), false);
598         sm->speed_node      = fgGetNode(entry_node->getStringValue("speed-node", "none"), false);
599         sm->submodel        = entry_node->getStringValue("submodel-path", "");
600
601         //cout <<  "sm->contents_node " << sm->contents_node << endl;
602         if (sm->contents_node != 0)
603             sm->contents = sm->contents_node->getDoubleValue();
604
605         const char *trigger_path = entry_node->getStringValue("trigger", 0);
606         if (trigger_path) {
607             sm->trigger_node = fgGetNode(trigger_path, true);
608             sm->trigger_node->setBoolValue(sm->trigger_node->getBoolValue());
609         } else {
610             sm->trigger_node = 0;
611         }
612
613         if (sm->speed_node != 0)
614             sm->speed = sm->speed_node->getDoubleValue();
615
616         sm->timer = sm->delay;
617         sm->id = id;
618         sm->first_time = false;
619         sm->serviceable = serviceable;
620         sm->sub_id = 0;
621
622         sm->prop = fgGetNode("/ai/submodels/submodel", index, true);
623         sm->prop->tie("count", SGRawValuePointer<int>(&(sm->count)));
624         sm->prop->tie("repeat", SGRawValuePointer<bool>(&(sm->repeat)));
625         sm->prop->tie("id", SGRawValuePointer<int>(&(sm->id)));
626         sm->prop->tie("sub-id", SGRawValuePointer<int>(&(sm->sub_id)));
627
628         sm->prop->tie("serviceable", SGRawValuePointer<bool>(&(sm->serviceable)));
629         string name = sm->name;
630         sm->prop->setStringValue("name", name.c_str());
631
632         string submodel = sm->submodel;
633         sm->prop->setStringValue("submodel", submodel.c_str());
634         //cout << " set submodel path " << submodel << endl;
635
636         if (sm->contents_node != 0)
637             sm->prop->tie("contents-lbs", SGRawValuePointer<double>(&(sm->contents)));
638
639         index++;
640         submodels.push_back(sm);
641     }
642 }
643
644 void FGSubmodelMgr::setSubData(int id, string& path, bool serviceable)
645 {
646     SGPropertyNode root;
647
648     SGPath config(globals->get_fg_root());
649     config.append(path);
650     SG_LOG(SG_GENERAL, SG_DEBUG,
651         "Submodels: path " << path);
652     try {
653         SG_LOG(SG_GENERAL, SG_DEBUG,
654             "Submodels: Trying to read AI submodels file: " << config.str());
655         readProperties(config.str(), &root);
656
657     } catch (const sg_exception &e) {
658         SG_LOG(SG_GENERAL, SG_DEBUG,
659             "Submodels: Unable to read AI submodels file: " << config.str());
660         return;
661     }
662
663     vector<SGPropertyNode_ptr> children = root.getChildren("submodel");
664     vector<SGPropertyNode_ptr>::iterator it = children.begin();
665     vector<SGPropertyNode_ptr>::iterator end = children.end();
666
667     for (int i = 0; it != end; ++it, i++) {
668         //cout << "Reading AI submodel " << (*it)->getPath() << endl;
669         submodel* sm = new submodel;
670         SGPropertyNode * entry_node = *it;
671         sm->name            = entry_node->getStringValue("name", "none_defined");
672         sm->model           = entry_node->getStringValue("model", "Models/Geometry/rocket.ac");
673         sm->speed           = entry_node->getDoubleValue("speed", 2329.4);
674         sm->repeat          = entry_node->getBoolValue("repeat", false);
675         sm->delay           = entry_node->getDoubleValue("delay", 0.25);
676         sm->count           = entry_node->getIntValue("count", 1);
677         sm->slaved          = entry_node->getBoolValue("slaved", false);
678         sm->x_offset        = entry_node->getDoubleValue("x-offset", 0.0);
679         sm->y_offset        = entry_node->getDoubleValue("y-offset", 0.0);
680         sm->z_offset        = entry_node->getDoubleValue("z-offset", 0.0);
681         sm->yaw_offset      = entry_node->getDoubleValue("yaw-offset", 0.0);
682         sm->pitch_offset    = entry_node->getDoubleValue("pitch-offset", 0.0);
683         sm->drag_area       = entry_node->getDoubleValue("eda", 0.034);
684         sm->life            = entry_node->getDoubleValue("life", 900.0);
685         sm->buoyancy        = entry_node->getDoubleValue("buoyancy", 0);
686         sm->wind            = entry_node->getBoolValue("wind", false);
687         sm->cd              = entry_node->getDoubleValue("cd", 0.193);
688         sm->weight          = entry_node->getDoubleValue("weight", 0.25);
689         sm->aero_stabilised = entry_node->getBoolValue("aero-stabilised", true);
690         sm->no_roll         = entry_node->getBoolValue("no-roll", false);
691         sm->collision       = entry_node->getBoolValue("collision", false);
692         sm->impact          = entry_node->getBoolValue("impact", false);
693         sm->impact_report   = entry_node->getStringValue("impact-reports");
694         sm->fuse_range      = entry_node->getDoubleValue("fuse-range", 0.0);
695         sm->contents_node   = fgGetNode(entry_node->getStringValue("contents", "none"), false);
696         sm->speed_node      = fgGetNode(entry_node->getStringValue("speed-node", "none"), false);
697         sm->submodel        = entry_node->getStringValue("submodel-path", "");
698
699         //cout <<  "sm->contents_node " << sm->contents_node << endl;
700         if (sm->contents_node != 0)
701             sm->contents = sm->contents_node->getDoubleValue();
702
703         const char *trigger_path = entry_node->getStringValue("trigger", 0);
704         if (trigger_path) {
705             sm->trigger_node = fgGetNode(trigger_path, true);
706             sm->trigger_node->setBoolValue(sm->trigger_node->getBoolValue());
707         } else {
708             sm->trigger_node = 0;
709         }
710
711         if (sm->speed_node != 0)
712             sm->speed = sm->speed_node->getDoubleValue();
713
714         sm->timer = sm->delay;
715         sm->id = index;
716         sm->first_time = false;
717         sm->serviceable = serviceable;
718         sm->sub_id = 0;
719
720         sm->prop = fgGetNode("/ai/submodels/subsubmodel", index, true);
721         sm->prop->tie("count", SGRawValuePointer<int>(&(sm->count)));
722         sm->prop->tie("repeat", SGRawValuePointer<bool>(&(sm->repeat)));
723         sm->prop->tie("id", SGRawValuePointer<int>(&(sm->id)));
724         sm->prop->tie("sub-id", SGRawValuePointer<int>(&(sm->sub_id)));
725         sm->prop->tie("serviceable", SGRawValuePointer<bool>(&(sm->serviceable)));
726         string name = sm->name;
727         sm->prop->setStringValue("name", name.c_str());
728
729         string submodel = sm->submodel;
730         sm->prop->setStringValue("submodel", submodel.c_str());
731         // cout << " set submodel path " << submodel<< endl;
732
733         if (sm->contents_node != 0)
734             sm->prop->tie("contents-lbs", SGRawValuePointer<double>(&(sm->contents)));
735
736         index++;
737         subsubmodels.push_back(sm);
738     }
739 }
740
741 void FGSubmodelMgr::loadSubmodels()
742 {
743     SG_LOG(SG_GENERAL, SG_DEBUG, "Submodels: Loading sub submodels");
744
745     submodel_iterator = submodels.begin();
746
747     while (submodel_iterator != submodels.end()) {
748         string submodel  = (*submodel_iterator)->submodel;
749         if (!submodel.empty()) {
750             //int id = (*submodel_iterator)->id;
751             bool serviceable = true;
752             SG_LOG(SG_GENERAL, SG_ALERT, "found path sub sub "
753                 << submodel
754                 << " index " << index
755                 << "name " << (*submodel_iterator)->name);
756
757             (*submodel_iterator)->sub_id = index;
758             setSubData(index, submodel, serviceable);
759         }
760
761         ++submodel_iterator;
762     }
763
764     subsubmodel_iterator = subsubmodels.begin();
765
766     while (subsubmodel_iterator != subsubmodels.end()) {
767         submodels.push_back(*subsubmodel_iterator);
768         ++subsubmodel_iterator;
769     }
770
771     submodel_iterator = submodels.begin();
772
773     while (submodel_iterator != submodels.end()) {
774         int id = (*submodel_iterator)->id;
775         SG_LOG(SG_GENERAL, SG_DEBUG,"after pusback "
776             << " id " << id
777             << " name " << (*submodel_iterator)->name
778             << " sub id " << (*submodel_iterator)->sub_id);
779
780         ++submodel_iterator;
781     }
782 }
783
784 // end of submodel.cxx