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