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