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