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