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