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