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