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