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