1 //// submodel.cxx - models a releasable submodel.
2 // Written by Dave Culp, started Aug 2004
3 // With major additions by Vivian Meaaza 2004 - 2007
5 // This file is in the Public Domain and comes with no warranty.
11 #include "submodel.hxx"
13 #include <simgear/structure/exception.hxx>
14 #include <simgear/misc/sg_path.hxx>
15 #include <simgear/math/sg_geodesy.hxx>
17 #include <Main/fg_props.hxx>
18 #include <Main/util.hxx>
22 #include "AIManager.hxx"
23 #include "AIBallistic.hxx"
26 const double FGSubmodelMgr::lbs_to_slugs = 0.031080950172;
28 FGSubmodelMgr::FGSubmodelMgr()
30 x_offset = y_offset = 0.0;
35 out[0] = out[1] = out[2] = 0;
37 contrail_altitude = 30000;
41 FGSubmodelMgr::~FGSubmodelMgr()
45 void FGSubmodelMgr::init()
49 _serviceable_node = fgGetNode("/sim/submodels/serviceable", true);
50 _serviceable_node->setBoolValue(true);
52 _user_lat_node = fgGetNode("/position/latitude-deg", true);
53 _user_lon_node = fgGetNode("/position/longitude-deg", true);
54 _user_alt_node = fgGetNode("/position/altitude-ft", true);
56 _user_heading_node = fgGetNode("/orientation/heading-deg", true);
57 _user_pitch_node = fgGetNode("/orientation/pitch-deg", true);
58 _user_roll_node = fgGetNode("/orientation/roll-deg", true);
59 _user_yaw_node = fgGetNode("/orientation/yaw-deg", true);
60 _user_alpha_node = fgGetNode("/orientation/alpha-deg", true);
62 _user_speed_node = fgGetNode("/velocities/uBody-fps", true);
64 _user_wind_from_east_node = fgGetNode("/environment/wind-from-east-fps", true);
65 _user_wind_from_north_node = fgGetNode("/environment/wind-from-north-fps", true);
67 _user_speed_down_fps_node = fgGetNode("/velocities/speed-down-fps", true);
68 _user_speed_east_fps_node = fgGetNode("/velocities/speed-east-fps", true);
69 _user_speed_north_fps_node = fgGetNode("/velocities/speed-north-fps", true);
71 _contrail_altitude_node = fgGetNode("/environment/params/contrail-altitude", true);
72 contrail_altitude = _contrail_altitude_node->getDoubleValue();
73 _contrail_trigger = fgGetNode("ai/submodels/contrails", true);
74 _contrail_trigger->setBoolValue(false);
76 ai = (FGAIManager*)globals->get_subsystem("ai_model");
81 void FGSubmodelMgr::postinit() {
82 // postinit, so that the AI list is populated
86 //TODO reload submodels if an MP ac joins
89 void FGSubmodelMgr::bind()
92 void FGSubmodelMgr::unbind()
94 submodel_iterator = submodels.begin();
95 while (submodel_iterator != submodels.end()) {
96 (*submodel_iterator)->prop->untie("count");
101 void FGSubmodelMgr::update(double dt)
103 if (!_serviceable_node->getBoolValue())
109 // check if the submodel hit an object or terrain
110 sm_list = ai->get_ai_list();
111 sm_list_iterator sm_list_itr = sm_list.begin();
112 sm_list_iterator end = sm_list.end();
114 for (; sm_list_itr != end; ++sm_list_itr) {
115 _impact = (*sm_list_itr)->_getImpactData();
116 _hit = (*sm_list_itr)->_getCollisionData();
117 int parent_subID = (*sm_list_itr)->_getSubID();
118 SG_LOG(SG_GENERAL, SG_DEBUG, "Submodel: Impact " << _impact << " hit! "
119 << _hit <<" parent_subID " << parent_subID);
120 if ( parent_subID == 0) // this entry in the list has no associated submodel
121 continue; // so we can continue
123 if (_impact || _hit) {
124 SG_LOG(SG_GENERAL, SG_DEBUG, "Submodel: Impact " << _impact << " hit! " << _hit );
126 submodel_iterator = submodels.begin();
128 while (submodel_iterator != submodels.end()) {
129 int child_ID = (*submodel_iterator)->id;
130 //cout << "Impact: parent SubID " << parent_subID << " child_ID " << child_ID << endl;
132 if ( parent_subID == child_ID ) {
133 _parent_lat = (*sm_list_itr)->_getImpactLat();
134 _parent_lon = (*sm_list_itr)->_getImpactLon();
135 _parent_elev = (*sm_list_itr)->_getImpactElevFt();
136 _parent_hdg = (*sm_list_itr)->_getImpactHdg();
137 _parent_pitch = (*sm_list_itr)->_getImpactPitch();
138 _parent_roll = (*sm_list_itr)->_getImpactRoll();
139 _parent_speed = (*sm_list_itr)->_getImpactSpeed();
140 (*submodel_iterator)->first_time = true;
142 if (release(*submodel_iterator, dt))
143 (*sm_list_itr)->setDie(true);
151 _contrail_trigger->setBoolValue(_user_alt_node->getDoubleValue() > contrail_altitude);
154 bool in_range = true;
155 bool trigger = false;
158 submodel_iterator = submodels.begin();
159 while (submodel_iterator != submodels.end()) {
163 SG_LOG(SG_GENERAL, SG_DEBUG,
164 "Submodels: " << (*submodel_iterator)->id
165 << " name " << (*submodel_iterator)->name
166 << " in range " << in_range);
168 if ((*submodel_iterator)->trigger_node != 0) {
169 _trigger_node = (*submodel_iterator)->trigger_node;
170 trigger = _trigger_node->getBoolValue();
171 //cout << "trigger node found " << trigger << endl;
174 //cout << (*submodel_iterator)->name << "trigger node not found " << trigger << endl;
178 int id = (*submodel_iterator)->id;
179 string name = (*submodel_iterator)->name;
180 // don't release submodels from AI Objects if they are
181 // too far away to be seen. id 0 is not an AI model,
182 // so we can skip the whole process
183 sm_list_iterator sm_list_itr = sm_list.begin();
184 sm_list_iterator end = sm_list.end();
186 while (sm_list_itr != end) {
190 SG_LOG(SG_GENERAL, SG_DEBUG,
191 "Submodels: continuing: " << id << " name " << name );
196 int parent_id = (*submodel_iterator)->id;
198 if (parent_id == id) {
199 double parent_lat = (*sm_list_itr)->_getLatitude();
200 double parent_lon = (*sm_list_itr)->_getLongitude();
201 string parent_name = (*sm_list_itr)->_getName();
202 double own_lat = _user_lat_node->getDoubleValue();
203 double own_lon = _user_lon_node->getDoubleValue();
204 double range_nm = getRange(parent_lat, parent_lon, own_lat, own_lon);
205 //cout << "parent name " << parent_name << ", "<< parent_id << ", "<< parent_lat << ", " << parent_lon << endl;
206 //cout << "own name " << own_lat << ", " << own_lon << " range " << range_nm << endl;
209 SG_LOG(SG_GENERAL, SG_DEBUG,
210 "Submodels: skipping release, out of range: " << id);
218 SG_LOG(SG_GENERAL, SG_DEBUG,
219 "Submodels end: " << (*submodel_iterator)->id
220 << " name " << (*submodel_iterator)->name
221 << " count " << (*submodel_iterator)->count
222 << " in range " << in_range);
224 if ((*submodel_iterator)->count != 0 && in_range)
225 release(*submodel_iterator, dt);
228 (*submodel_iterator)->first_time = true;
234 bool FGSubmodelMgr::release(submodel *sm, double dt)
236 //cout << "release id " << sm->id << " name " << sm->name
237 //<< " first time " << sm->first_time << " repeat " << sm->repeat <<
240 // only run if first time or repeat is set to true
241 if (!sm->first_time && !sm->repeat) {
242 //cout<< "not first time " << sm->first_time<< " repeat " << sm->repeat <<endl;
248 if (sm->timer < sm->delay) {
249 //cout << "not yet: timer" << sm->timer << " delay " << sm->delay<< endl;
255 if (sm->first_time) {
257 sm->first_time = false;
260 transform(sm); // calculate submodel's initial conditions in world-coordinates
262 FGAIBallistic* ballist = new FGAIBallistic;
263 ballist->setPath(sm->model.c_str());
264 ballist->setLatitude(IC.lat);
265 ballist->setLongitude(IC.lon);
266 ballist->setAltitude(IC.alt);
267 ballist->setAzimuth(IC.azimuth);
268 ballist->setElevation(IC.elevation);
269 ballist->setRoll(IC.roll);
270 ballist->setSpeed(IC.speed / SG_KT_TO_FPS);
271 ballist->setWind_from_east(IC.wind_from_east);
272 ballist->setWind_from_north(IC.wind_from_north);
273 ballist->setMass(IC.mass);
274 ballist->setDragArea(sm->drag_area);
275 ballist->setLife(sm->life);
276 ballist->setBuoyancy(sm->buoyancy);
277 ballist->setWind(sm->wind);
278 ballist->setCd(sm->cd);
279 ballist->setStabilisation(sm->aero_stabilised);
280 ballist->setNoRoll(sm->no_roll);
281 ballist->setName(sm->name);
282 ballist->setCollision(sm->collision);
283 ballist->setImpact(sm->impact);
284 ballist->setImpactReportNode(sm->impact_report);
285 ballist->setFuseRange(sm->fuse_range);
286 ballist->setSubmodel(sm->submodel.c_str());
287 ballist->setSubID(sm->sub_id);
288 ballist->setExternalForce(sm->ext_force);
289 ballist->setForcePath(sm->force_path.c_str());
298 void FGSubmodelMgr::load()
300 SGPropertyNode *path = fgGetNode("/sim/submodels/path");
304 string Path = path->getStringValue();
305 bool Seviceable =_serviceable_node->getBoolValue();
306 setData(id, Path, Seviceable);
310 void FGSubmodelMgr::transform(submodel *sm)
312 // set initial conditions
313 if (sm->contents_node != 0) {
314 // get the weight of the contents (lbs) and convert to mass (slugs)
315 sm->contents = sm->contents_node->getChild("level-lbs",0,1)->getDoubleValue();
316 //cout << "transform: contents " << sm->contents << endl;
317 IC.mass = (sm->weight + sm->contents) * lbs_to_slugs;
318 //cout << "mass inc contents" << IC.mass << endl;
320 // set contents to 0 in the parent
321 sm->contents_node->getChild("level-gal_us",0,1)->setDoubleValue(0);
322 /*cout << "contents " << sm->contents_node->getChild("level-gal_us")->getDoubleValue()
323 << " " << sm->contents_node->getChild("level-lbs",0,1)->getDoubleValue()
326 IC.mass = sm->weight * lbs_to_slugs;
328 // cout << "mass " << IC.mass << endl;
330 if (sm->speed_node != 0)
331 sm->speed = sm->speed_node->getDoubleValue();
334 //int sub_id = (*submodel)->sub_id;
335 string name = sm->name;
337 //cout << " name " << name << " id " << id << " sub id" << sub_id << endl;
339 if (_impact || _hit) {
340 // set the data for a submodel tied to a submodel
342 //cout << "Submodels: release sub sub " << _count<< endl;
343 //cout << " id " << sm->id
344 // << " lat " << _parent_lat
345 // << " lon " << _parent_lon
346 // << " elev " << _parent_elev
347 // << " name " << sm->name
350 IC.lat = _parent_lat;
351 IC.lon = _parent_lon;
352 IC.alt = _parent_elev;
353 IC.roll = _parent_roll; // rotation about x axis
354 IC.elevation = _parent_pitch; // rotation about y axis
355 IC.azimuth = _parent_hdg; // rotation about z axis
356 IC.speed = _parent_speed;
357 IC.speed_down_fps = 0;
358 IC.speed_east_fps = 0;
359 IC.speed_north_fps = 0;
361 } else if (id == 0) {
362 //set the data for a submodel tied to the main model
363 /*cout << "Submodels: release main sub " << endl;
364 cout << " name " << sm->name
367 IC.lat = _user_lat_node->getDoubleValue();
368 IC.lon = _user_lon_node->getDoubleValue();
369 IC.alt = _user_alt_node->getDoubleValue();
370 IC.roll = _user_roll_node->getDoubleValue(); // rotation about x axis
371 IC.elevation = _user_pitch_node->getDoubleValue(); // rotation about y axis
372 IC.azimuth = _user_heading_node->getDoubleValue(); // rotation about z axis
373 IC.speed = _user_speed_node->getDoubleValue();
374 IC.speed_down_fps = _user_speed_down_fps_node->getDoubleValue();
375 IC.speed_east_fps = _user_speed_east_fps_node->getDoubleValue();
376 IC.speed_north_fps = _user_speed_north_fps_node->getDoubleValue();
379 // set the data for a submodel tied to an AI Object
380 sm_list_iterator sm_list_itr = sm_list.begin();
381 sm_list_iterator end = sm_list.end();
383 while (sm_list_itr != end) {
384 int parent_id = (*sm_list_itr)->getID();
386 if (id != parent_id) {
391 //cout << "found id " << id << endl;
392 IC.lat = (*sm_list_itr)->_getLatitude();
393 IC.lon = (*sm_list_itr)->_getLongitude();
394 IC.alt = (*sm_list_itr)->_getAltitude();
395 IC.roll = (*sm_list_itr)->_getRoll();
396 IC.elevation = (*sm_list_itr)->_getPitch();
397 IC.azimuth = (*sm_list_itr)->_getHeading();
398 IC.alt = (*sm_list_itr)->_getAltitude();
399 IC.speed = (*sm_list_itr)->_getSpeed() * SG_KT_TO_FPS;
400 IC.speed_down_fps = -(*sm_list_itr)->_getVS_fps();
401 IC.speed_east_fps = (*sm_list_itr)->_get_speed_east_fps();
402 IC.speed_north_fps = (*sm_list_itr)->_get_speed_north_fps();
408 /*cout << "heading " << IC.azimuth << endl ;
409 cout << "speed down " << IC.speed_down_fps << endl ;
410 cout << "speed east " << IC.speed_east_fps << endl ;
411 cout << "speed north " << IC.speed_north_fps << endl ;
412 cout << "parent speed fps in" << IC.speed << "sm speed in " << sm->speed << endl ;*/
414 IC.wind_from_east = _user_wind_from_east_node->getDoubleValue();
415 IC.wind_from_north = _user_wind_from_north_node->getDoubleValue();
417 in[0] = sm->x_offset;
418 in[1] = sm->y_offset;
419 in[2] = sm->z_offset;
421 // pre-process the trig functions
422 cosRx = cos(-IC.roll * SG_DEGREES_TO_RADIANS);
423 sinRx = sin(-IC.roll * SG_DEGREES_TO_RADIANS);
424 cosRy = cos(-IC.elevation * SG_DEGREES_TO_RADIANS);
425 sinRy = sin(-IC.elevation * SG_DEGREES_TO_RADIANS);
426 cosRz = cos(IC.azimuth * SG_DEGREES_TO_RADIANS);
427 sinRz = sin(IC.azimuth * SG_DEGREES_TO_RADIANS);
429 // set up the transform matrix
430 trans[0][0] = cosRy * cosRz;
431 trans[0][1] = -1 * cosRx * sinRz + sinRx * sinRy * cosRz ;
432 trans[0][2] = sinRx * sinRz + cosRx * sinRy * cosRz;
434 trans[1][0] = cosRy * sinRz;
435 trans[1][1] = cosRx * cosRz + sinRx * sinRy * sinRz;
436 trans[1][2] = -1 * sinRx * cosRx + cosRx * sinRy * sinRz;
438 trans[2][0] = -1 * sinRy;
439 trans[2][1] = sinRx * cosRy;
440 trans[2][2] = cosRx * cosRy;
443 // multiply the input and transform matrices
444 out[0] = in[0] * trans[0][0] + in[1] * trans[0][1] + in[2] * trans[0][2];
445 out[1] = in[0] * trans[1][0] + in[1] * trans[1][1] + in[2] * trans[1][2];
446 out[2] = in[0] * trans[2][0] + in[1] * trans[2][1] + in[2] * trans[2][2];
448 // convert ft to degrees of latitude
449 out[0] = out[0] / (366468.96 - 3717.12 * cos(IC.lat * SG_DEGREES_TO_RADIANS));
451 // convert ft to degrees of longitude
452 out[1] = out[1] / (365228.16 * cos(IC.lat * SG_DEGREES_TO_RADIANS));
454 // set submodel initial position
459 // get aircraft velocity vector angles in XZ and XY planes
460 //double alpha = _user_alpha_node->getDoubleValue();
461 //double velXZ = IC.elevation - alpha * cosRx;
462 //double velXY = IC.azimuth - (IC.elevation - alpha * sinRx);
464 // Get submodel initial velocity vector angles in XZ and XY planes.
465 // This needs to be fixed. This vector should be added to aircraft's vector.
466 IC.elevation += (sm->yaw_offset * sinRx) + (sm->pitch_offset * cosRx);
467 IC.azimuth += (sm->yaw_offset * cosRx) - (sm->pitch_offset * sinRx);
469 // calculate the total speed north
470 IC.total_speed_north = sm->speed * cos(IC.elevation * SG_DEGREES_TO_RADIANS)
471 * cos(IC.azimuth * SG_DEGREES_TO_RADIANS) + IC.speed_north_fps;
473 // calculate the total speed east
474 IC.total_speed_east = sm->speed * cos(IC.elevation * SG_DEGREES_TO_RADIANS)
475 * sin(IC.azimuth * SG_DEGREES_TO_RADIANS) + IC.speed_east_fps;
477 // calculate the total speed down
478 IC.total_speed_down = sm->speed * -sin(IC.elevation * SG_DEGREES_TO_RADIANS)
481 // re-calculate speed, elevation and azimuth
482 IC.speed = sqrt(IC.total_speed_north * IC.total_speed_north
483 + IC.total_speed_east * IC.total_speed_east
484 + IC.total_speed_down * IC.total_speed_down);
486 // if speeds are low this calculation can become unreliable
488 IC.azimuth = atan2(IC.total_speed_east , IC.total_speed_north) * SG_RADIANS_TO_DEGREES;
489 // cout << "azimuth1 " << IC.azimuth<<endl;
491 // rationalise the output
494 else if (IC.azimuth >= 360)
496 // cout << "azimuth2 " << IC.azimuth<<endl;
498 IC.elevation = -atan(IC.total_speed_down / sqrt(IC.total_speed_north
499 * IC.total_speed_north + IC.total_speed_east * IC.total_speed_east))
500 * SG_RADIANS_TO_DEGREES;
504 void FGSubmodelMgr::updatelat(double lat)
506 ft_per_deg_latitude = 366468.96 - 3717.12 * cos(lat / SG_RADIANS_TO_DEGREES);
507 ft_per_deg_longitude = 365228.16 * cos(lat / SG_RADIANS_TO_DEGREES);
510 void FGSubmodelMgr::loadAI()
512 SG_LOG(SG_GENERAL, SG_DEBUG, "Submodels: Loading AI submodels ");
514 sm_list = ai->get_ai_list();
516 if (sm_list.empty()) {
517 SG_LOG(SG_GENERAL, SG_DEBUG, "Submodels: Unable to read AI submodel list");
521 sm_list_iterator sm_list_itr = sm_list.begin();
522 sm_list_iterator end = sm_list.end();
524 while (sm_list_itr != end) {
525 string path = (*sm_list_itr)->_getSMPath();
532 int id = (*sm_list_itr)->getID();
533 bool serviceable = (*sm_list_itr)->_getServiceable();
534 setData(id, path, serviceable);
540 double FGSubmodelMgr::getRange(double lat, double lon, double lat2, double lon2) const
542 double course, distance, az2;
544 //calculate the bearing and range of the second pos from the first
545 geo_inverse_wgs_84(lat, lon, lat2, lon2, &course, &az2, &distance);
546 distance *= SG_METER_TO_NM;
550 void FGSubmodelMgr::setData(int id, string& path, bool serviceable)
554 SGPath config(globals->get_fg_root());
556 SG_LOG(SG_GENERAL, SG_DEBUG, "Submodels: path " << path);
558 SG_LOG(SG_GENERAL, SG_DEBUG,
559 "Submodels: Trying to read AI submodels file: " << config.str());
560 readProperties(config.str(), &root);
561 } catch (const sg_exception &e) {
562 SG_LOG(SG_GENERAL, SG_DEBUG,
563 "Submodels: Unable to read AI submodels file: " << config.str());
567 vector<SGPropertyNode_ptr> children = root.getChildren("submodel");
568 vector<SGPropertyNode_ptr>::iterator it = children.begin();
569 vector<SGPropertyNode_ptr>::iterator end = children.end();
571 for (int i = 0; it != end; ++it, i++) {
572 //cout << "Reading AI submodel " << (*it)->getPath() << endl;
573 submodel* sm = new submodel;
574 SGPropertyNode * entry_node = *it;
575 sm->name = entry_node->getStringValue("name", "none_defined");
576 sm->model = entry_node->getStringValue("model", "Models/Geometry/rocket.ac");
577 sm->speed = entry_node->getDoubleValue("speed", 2329.4);
578 sm->repeat = entry_node->getBoolValue("repeat", false);
579 sm->delay = entry_node->getDoubleValue("delay", 0.25);
580 sm->count = entry_node->getIntValue("count", 1);
581 sm->slaved = entry_node->getBoolValue("slaved", false);
582 sm->x_offset = entry_node->getDoubleValue("x-offset", 0.0);
583 sm->y_offset = entry_node->getDoubleValue("y-offset", 0.0);
584 sm->z_offset = entry_node->getDoubleValue("z-offset", 0.0);
585 sm->yaw_offset = entry_node->getDoubleValue("yaw-offset", 0.0);
586 sm->pitch_offset = entry_node->getDoubleValue("pitch-offset", 0.0);
587 sm->drag_area = entry_node->getDoubleValue("eda", 0.034);
588 sm->life = entry_node->getDoubleValue("life", 900.0);
589 sm->buoyancy = entry_node->getDoubleValue("buoyancy", 0);
590 sm->wind = entry_node->getBoolValue("wind", false);
591 sm->cd = entry_node->getDoubleValue("cd", 0.193);
592 sm->weight = entry_node->getDoubleValue("weight", 0.25);
593 sm->aero_stabilised = entry_node->getBoolValue("aero-stabilised", true);
594 sm->no_roll = entry_node->getBoolValue("no-roll", false);
595 sm->collision = entry_node->getBoolValue("collision", false);
596 sm->impact = entry_node->getBoolValue("impact", false);
597 sm->impact_report = entry_node->getStringValue("impact-reports");
598 sm->fuse_range = entry_node->getDoubleValue("fuse-range", 0.0);
599 sm->contents_node = fgGetNode(entry_node->getStringValue("contents", "none"), false);
600 sm->speed_node = fgGetNode(entry_node->getStringValue("speed-node", "none"), false);
601 sm->submodel = entry_node->getStringValue("submodel-path", "");
602 sm->ext_force = entry_node->getBoolValue("external-force", false);
603 sm->force_path = entry_node->getStringValue("force-path", "");
604 //cout << "sm->contents_node " << sm->contents_node << endl;
605 if (sm->contents_node != 0)
606 sm->contents = sm->contents_node->getDoubleValue();
608 const char *trigger_path = entry_node->getStringValue("trigger", 0);
610 sm->trigger_node = fgGetNode(trigger_path, true);
611 sm->trigger_node->setBoolValue(sm->trigger_node->getBoolValue());
613 sm->trigger_node = 0;
616 SG_LOG(SG_GENERAL, SG_DEBUG, "Submodels: trigger " << sm->trigger_node->getBoolValue() );
618 if (sm->speed_node != 0)
619 sm->speed = sm->speed_node->getDoubleValue();
621 sm->timer = sm->delay;
623 sm->first_time = false;
624 sm->serviceable = serviceable;
627 sm->prop = fgGetNode("/ai/submodels/submodel", index, true);
628 sm->prop->tie("count", SGRawValuePointer<int>(&(sm->count)));
629 sm->prop->tie("repeat", SGRawValuePointer<bool>(&(sm->repeat)));
630 sm->prop->tie("id", SGRawValuePointer<int>(&(sm->id)));
631 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());
637 string submodel = sm->submodel;
638 sm->prop->setStringValue("submodel", submodel.c_str());
639 //cout << " set submodel path " << submodel << endl;
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;
645 if (sm->contents_node != 0)
646 sm->prop->tie("contents-lbs", SGRawValuePointer<double>(&(sm->contents)));
649 submodels.push_back(sm);
653 void FGSubmodelMgr::setSubData(int id, string& path, bool serviceable)
657 SGPath config(globals->get_fg_root());
659 SG_LOG(SG_GENERAL, SG_DEBUG,
660 "Submodels: path " << path);
662 SG_LOG(SG_GENERAL, SG_DEBUG,
663 "Submodels: Trying to read AI submodels file: " << config.str());
664 readProperties(config.str(), &root);
666 } catch (const sg_exception &e) {
667 SG_LOG(SG_GENERAL, SG_DEBUG,
668 "Submodels: Unable to read AI submodels file: " << config.str());
672 vector<SGPropertyNode_ptr> children = root.getChildren("submodel");
673 vector<SGPropertyNode_ptr>::iterator it = children.begin();
674 vector<SGPropertyNode_ptr>::iterator end = children.end();
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", "");
710 //cout << "sm->contents_node " << sm->contents_node << endl;
711 if (sm->contents_node != 0)
712 sm->contents = sm->contents_node->getDoubleValue();
714 const char *trigger_path = entry_node->getStringValue("trigger", 0);
716 sm->trigger_node = fgGetNode(trigger_path, true);
717 sm->trigger_node->setBoolValue(sm->trigger_node->getBoolValue());
719 sm->trigger_node = 0;
722 if (sm->speed_node != 0)
723 sm->speed = sm->speed_node->getDoubleValue();
725 sm->timer = sm->delay;
727 sm->first_time = false;
728 sm->serviceable = serviceable;
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());
740 string submodel = sm->submodel;
741 sm->prop->setStringValue("submodel", submodel.c_str());
742 // cout << " set submodel path " << submodel<< endl;
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;
748 if (sm->contents_node != 0)
749 sm->prop->tie("contents-lbs", SGRawValuePointer<double>(&(sm->contents)));
752 subsubmodels.push_back(sm);
756 void FGSubmodelMgr::loadSubmodels()
758 SG_LOG(SG_GENERAL, SG_DEBUG, "Submodels: Loading sub submodels");
760 submodel_iterator = submodels.begin();
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 "
769 << " index " << index
770 << "name " << (*submodel_iterator)->name);
772 (*submodel_iterator)->sub_id = index;
773 setSubData(index, submodel, serviceable);
779 subsubmodel_iterator = subsubmodels.begin();
781 while (subsubmodel_iterator != subsubmodels.end()) {
782 submodels.push_back(*subsubmodel_iterator);
783 ++subsubmodel_iterator;
786 submodel_iterator = submodels.begin();
788 while (submodel_iterator != submodels.end()) {
789 int id = (*submodel_iterator)->id;
790 SG_LOG(SG_GENERAL, SG_DEBUG,"after pushback "
792 << " name " << (*submodel_iterator)->name
793 << " sub id " << (*submodel_iterator)->sub_id);
799 // end of submodel.cxx