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>
16 #include <simgear/props/props_io.hxx>
18 #include <Main/fg_props.hxx>
19 #include <Main/util.hxx>
23 #include "AIManager.hxx"
24 #include "AIBallistic.hxx"
27 const double FGSubmodelMgr::lbs_to_slugs = 0.031080950172;
29 FGSubmodelMgr::FGSubmodelMgr()
31 x_offset = y_offset = 0.0;
36 out[0] = out[1] = out[2] = 0;
38 contrail_altitude = 30000;
42 FGSubmodelMgr::~FGSubmodelMgr()
46 void FGSubmodelMgr::init()
50 _serviceable_node = fgGetNode("/sim/submodels/serviceable", true);
51 _serviceable_node->setBoolValue(true);
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);
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);
63 _user_speed_node = fgGetNode("/velocities/uBody-fps", true);
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);
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);
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);
77 ai = (FGAIManager*)globals->get_subsystem("ai_model");
82 void FGSubmodelMgr::postinit() {
83 // postinit, so that the AI list is populated
87 //TODO reload submodels if an MP ac joins
90 void FGSubmodelMgr::bind()
93 void FGSubmodelMgr::unbind()
95 submodel_iterator = submodels.begin();
96 while (submodel_iterator != submodels.end()) {
97 (*submodel_iterator)->prop->untie("count");
102 void FGSubmodelMgr::update(double dt)
104 if (!_serviceable_node->getBoolValue())
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();
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
124 if (_impact || _hit) {
125 SG_LOG(SG_GENERAL, SG_DEBUG, "Submodel: Impact " << _impact << " hit! " << _hit );
127 submodel_iterator = submodels.begin();
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;
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;
143 if (release(*submodel_iterator, dt))
144 (*sm_list_itr)->setDie(true);
152 _contrail_trigger->setBoolValue(_user_alt_node->getDoubleValue() > contrail_altitude);
155 bool in_range = true;
156 bool trigger = false;
159 submodel_iterator = submodels.begin();
160 while (submodel_iterator != submodels.end()) {
164 SG_LOG(SG_GENERAL, SG_DEBUG,
165 "Submodels: " << (*submodel_iterator)->id
166 << " name " << (*submodel_iterator)->name
167 << " in range " << in_range);
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;
175 //cout << (*submodel_iterator)->name << "trigger node not found " << trigger << endl;
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();
187 while (sm_list_itr != end) {
191 SG_LOG(SG_GENERAL, SG_DEBUG,
192 "Submodels: continuing: " << id << " name " << name );
197 int parent_id = (*submodel_iterator)->id;
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;
210 SG_LOG(SG_GENERAL, SG_DEBUG,
211 "Submodels: skipping release, out of range: " << id);
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);
225 if ((*submodel_iterator)->count != 0 && in_range)
226 release(*submodel_iterator, dt);
229 (*submodel_iterator)->first_time = true;
235 bool FGSubmodelMgr::release(submodel *sm, double dt)
237 //cout << "release id " << sm->id << " name " << sm->name
238 //<< " first time " << sm->first_time << " repeat " << sm->repeat <<
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;
249 if (sm->timer < sm->delay) {
250 //cout << "not yet: timer" << sm->timer << " delay " << sm->delay<< endl;
256 if (sm->first_time) {
258 sm->first_time = false;
261 transform(sm); // calculate submodel's initial conditions in world-coordinates
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->setExternalForce(sm->ext_force);
290 ballist->setForcePath(sm->force_path.c_str());
299 void FGSubmodelMgr::load()
301 SGPropertyNode *path = fgGetNode("/sim/submodels/path");
305 string Path = path->getStringValue();
306 bool Seviceable =_serviceable_node->getBoolValue();
307 setData(id, Path, Seviceable);
311 void FGSubmodelMgr::transform(submodel *sm)
313 // set initial conditions
314 if (sm->contents_node != 0) {
315 // get the weight of the contents (lbs) and convert to mass (slugs)
316 sm->contents = sm->contents_node->getChild("level-lbs",0,1)->getDoubleValue();
317 //cout << "transform: contents " << sm->contents << endl;
318 IC.mass = (sm->weight + sm->contents) * lbs_to_slugs;
319 //cout << "mass inc contents" << IC.mass << endl;
321 // set contents to 0 in the parent
322 sm->contents_node->getChild("level-gal_us",0,1)->setDoubleValue(0);
323 /*cout << "contents " << sm->contents_node->getChild("level-gal_us")->getDoubleValue()
324 << " " << sm->contents_node->getChild("level-lbs",0,1)->getDoubleValue()
327 IC.mass = sm->weight * lbs_to_slugs;
329 // cout << "mass " << IC.mass << endl;
331 if (sm->speed_node != 0)
332 sm->speed = sm->speed_node->getDoubleValue();
335 //int sub_id = (*submodel)->sub_id;
336 string name = sm->name;
338 //cout << " name " << name << " id " << id << " sub id" << sub_id << endl;
340 if (_impact || _hit) {
341 // set the data for a submodel tied to a submodel
343 //cout << "Submodels: release sub sub " << _count<< endl;
344 //cout << " id " << sm->id
345 // << " lat " << _parent_lat
346 // << " lon " << _parent_lon
347 // << " elev " << _parent_elev
348 // << " name " << sm->name
351 IC.lat = _parent_lat;
352 IC.lon = _parent_lon;
353 IC.alt = _parent_elev;
354 IC.roll = _parent_roll; // rotation about x axis
355 IC.elevation = _parent_pitch; // rotation about y axis
356 IC.azimuth = _parent_hdg; // rotation about z axis
357 IC.speed = _parent_speed;
358 IC.speed_down_fps = 0;
359 IC.speed_east_fps = 0;
360 IC.speed_north_fps = 0;
362 } else if (id == 0) {
363 //set the data for a submodel tied to the main model
364 /*cout << "Submodels: release main sub " << endl;
365 cout << " name " << sm->name
368 IC.lat = _user_lat_node->getDoubleValue();
369 IC.lon = _user_lon_node->getDoubleValue();
370 IC.alt = _user_alt_node->getDoubleValue();
371 IC.roll = _user_roll_node->getDoubleValue(); // rotation about x axis
372 IC.elevation = _user_pitch_node->getDoubleValue(); // rotation about y axis
373 IC.azimuth = _user_heading_node->getDoubleValue(); // rotation about z axis
374 IC.speed = _user_speed_node->getDoubleValue();
375 IC.speed_down_fps = _user_speed_down_fps_node->getDoubleValue();
376 IC.speed_east_fps = _user_speed_east_fps_node->getDoubleValue();
377 IC.speed_north_fps = _user_speed_north_fps_node->getDoubleValue();
380 // set the data for a submodel tied to an AI Object
381 sm_list_iterator sm_list_itr = sm_list.begin();
382 sm_list_iterator end = sm_list.end();
384 while (sm_list_itr != end) {
385 int parent_id = (*sm_list_itr)->getID();
387 if (id != parent_id) {
392 //cout << "found id " << id << endl;
393 IC.lat = (*sm_list_itr)->_getLatitude();
394 IC.lon = (*sm_list_itr)->_getLongitude();
395 IC.alt = (*sm_list_itr)->_getAltitude();
396 IC.roll = (*sm_list_itr)->_getRoll();
397 IC.elevation = (*sm_list_itr)->_getPitch();
398 IC.azimuth = (*sm_list_itr)->_getHeading();
399 IC.alt = (*sm_list_itr)->_getAltitude();
400 IC.speed = (*sm_list_itr)->_getSpeed() * SG_KT_TO_FPS;
401 IC.speed_down_fps = -(*sm_list_itr)->_getVS_fps();
402 IC.speed_east_fps = (*sm_list_itr)->_get_speed_east_fps();
403 IC.speed_north_fps = (*sm_list_itr)->_get_speed_north_fps();
409 /*cout << "heading " << IC.azimuth << endl ;
410 cout << "speed down " << IC.speed_down_fps << endl ;
411 cout << "speed east " << IC.speed_east_fps << endl ;
412 cout << "speed north " << IC.speed_north_fps << endl ;
413 cout << "parent speed fps in" << IC.speed << "sm speed in " << sm->speed << endl ;*/
415 IC.wind_from_east = _user_wind_from_east_node->getDoubleValue();
416 IC.wind_from_north = _user_wind_from_north_node->getDoubleValue();
418 in[0] = sm->x_offset;
419 in[1] = sm->y_offset;
420 in[2] = sm->z_offset;
422 // pre-process the trig functions
423 cosRx = cos(-IC.roll * SG_DEGREES_TO_RADIANS);
424 sinRx = sin(-IC.roll * SG_DEGREES_TO_RADIANS);
425 cosRy = cos(-IC.elevation * SG_DEGREES_TO_RADIANS);
426 sinRy = sin(-IC.elevation * SG_DEGREES_TO_RADIANS);
427 cosRz = cos(IC.azimuth * SG_DEGREES_TO_RADIANS);
428 sinRz = sin(IC.azimuth * SG_DEGREES_TO_RADIANS);
430 // set up the transform matrix
431 trans[0][0] = cosRy * cosRz;
432 trans[0][1] = -1 * cosRx * sinRz + sinRx * sinRy * cosRz ;
433 trans[0][2] = sinRx * sinRz + cosRx * sinRy * cosRz;
435 trans[1][0] = cosRy * sinRz;
436 trans[1][1] = cosRx * cosRz + sinRx * sinRy * sinRz;
437 trans[1][2] = -1 * sinRx * cosRx + cosRx * sinRy * sinRz;
439 trans[2][0] = -1 * sinRy;
440 trans[2][1] = sinRx * cosRy;
441 trans[2][2] = cosRx * cosRy;
444 // multiply the input and transform matrices
445 out[0] = in[0] * trans[0][0] + in[1] * trans[0][1] + in[2] * trans[0][2];
446 out[1] = in[0] * trans[1][0] + in[1] * trans[1][1] + in[2] * trans[1][2];
447 out[2] = in[0] * trans[2][0] + in[1] * trans[2][1] + in[2] * trans[2][2];
449 // convert ft to degrees of latitude
450 out[0] = out[0] / (366468.96 - 3717.12 * cos(IC.lat * SG_DEGREES_TO_RADIANS));
452 // convert ft to degrees of longitude
453 out[1] = out[1] / (365228.16 * cos(IC.lat * SG_DEGREES_TO_RADIANS));
455 // set submodel initial position
460 // get aircraft velocity vector angles in XZ and XY planes
461 //double alpha = _user_alpha_node->getDoubleValue();
462 //double velXZ = IC.elevation - alpha * cosRx;
463 //double velXY = IC.azimuth - (IC.elevation - alpha * sinRx);
465 // Get submodel initial velocity vector angles in XZ and XY planes.
466 // This needs to be fixed. This vector should be added to aircraft's vector.
467 IC.elevation += (sm->yaw_offset * sinRx) + (sm->pitch_offset * cosRx);
468 IC.azimuth += (sm->yaw_offset * cosRx) - (sm->pitch_offset * sinRx);
470 // calculate the total speed north
471 IC.total_speed_north = sm->speed * cos(IC.elevation * SG_DEGREES_TO_RADIANS)
472 * cos(IC.azimuth * SG_DEGREES_TO_RADIANS) + IC.speed_north_fps;
474 // calculate the total speed east
475 IC.total_speed_east = sm->speed * cos(IC.elevation * SG_DEGREES_TO_RADIANS)
476 * sin(IC.azimuth * SG_DEGREES_TO_RADIANS) + IC.speed_east_fps;
478 // calculate the total speed down
479 IC.total_speed_down = sm->speed * -sin(IC.elevation * SG_DEGREES_TO_RADIANS)
482 // re-calculate speed, elevation and azimuth
483 IC.speed = sqrt(IC.total_speed_north * IC.total_speed_north
484 + IC.total_speed_east * IC.total_speed_east
485 + IC.total_speed_down * IC.total_speed_down);
487 // if speeds are low this calculation can become unreliable
489 IC.azimuth = atan2(IC.total_speed_east , IC.total_speed_north) * SG_RADIANS_TO_DEGREES;
490 // cout << "azimuth1 " << IC.azimuth<<endl;
492 // rationalise the output
495 else if (IC.azimuth >= 360)
497 // cout << "azimuth2 " << IC.azimuth<<endl;
499 IC.elevation = -atan(IC.total_speed_down / sqrt(IC.total_speed_north
500 * IC.total_speed_north + IC.total_speed_east * IC.total_speed_east))
501 * SG_RADIANS_TO_DEGREES;
505 void FGSubmodelMgr::updatelat(double lat)
507 ft_per_deg_latitude = 366468.96 - 3717.12 * cos(lat / SG_RADIANS_TO_DEGREES);
508 ft_per_deg_longitude = 365228.16 * cos(lat / SG_RADIANS_TO_DEGREES);
511 void FGSubmodelMgr::loadAI()
513 SG_LOG(SG_GENERAL, SG_DEBUG, "Submodels: Loading AI submodels ");
515 sm_list = ai->get_ai_list();
517 if (sm_list.empty()) {
518 SG_LOG(SG_GENERAL, SG_DEBUG, "Submodels: Unable to read AI submodel list");
522 sm_list_iterator sm_list_itr = sm_list.begin();
523 sm_list_iterator end = sm_list.end();
525 while (sm_list_itr != end) {
526 string path = (*sm_list_itr)->_getSMPath();
533 int id = (*sm_list_itr)->getID();
534 bool serviceable = (*sm_list_itr)->_getServiceable();
535 setData(id, path, serviceable);
541 double FGSubmodelMgr::getRange(double lat, double lon, double lat2, double lon2) const
543 double course, distance, az2;
545 //calculate the bearing and range of the second pos from the first
546 geo_inverse_wgs_84(lat, lon, lat2, lon2, &course, &az2, &distance);
547 distance *= SG_METER_TO_NM;
551 void FGSubmodelMgr::setData(int id, string& path, bool serviceable)
555 SGPath config(globals->get_fg_root());
557 SG_LOG(SG_GENERAL, SG_DEBUG, "Submodels: path " << path);
559 SG_LOG(SG_GENERAL, SG_DEBUG,
560 "Submodels: Trying to read AI submodels file: " << config.str());
561 readProperties(config.str(), &root);
562 } catch (const sg_exception &) {
563 SG_LOG(SG_GENERAL, SG_DEBUG,
564 "Submodels: Unable to read AI submodels file: " << config.str());
568 vector<SGPropertyNode_ptr> children = root.getChildren("submodel");
569 vector<SGPropertyNode_ptr>::iterator it = children.begin();
570 vector<SGPropertyNode_ptr>::iterator end = children.end();
572 for (int i = 0; it != end; ++it, i++) {
573 //cout << "Reading AI submodel " << (*it)->getPath() << endl;
574 submodel* sm = new submodel;
575 SGPropertyNode * entry_node = *it;
576 sm->name = entry_node->getStringValue("name", "none_defined");
577 sm->model = entry_node->getStringValue("model", "Models/Geometry/rocket.ac");
578 sm->speed = entry_node->getDoubleValue("speed", 2329.4);
579 sm->repeat = entry_node->getBoolValue("repeat", false);
580 sm->delay = entry_node->getDoubleValue("delay", 0.25);
581 sm->count = entry_node->getIntValue("count", 1);
582 sm->slaved = entry_node->getBoolValue("slaved", false);
583 sm->x_offset = entry_node->getDoubleValue("x-offset", 0.0);
584 sm->y_offset = entry_node->getDoubleValue("y-offset", 0.0);
585 sm->z_offset = entry_node->getDoubleValue("z-offset", 0.0);
586 sm->yaw_offset = entry_node->getDoubleValue("yaw-offset", 0.0);
587 sm->pitch_offset = entry_node->getDoubleValue("pitch-offset", 0.0);
588 sm->drag_area = entry_node->getDoubleValue("eda", 0.034);
589 sm->life = entry_node->getDoubleValue("life", 900.0);
590 sm->buoyancy = entry_node->getDoubleValue("buoyancy", 0);
591 sm->wind = entry_node->getBoolValue("wind", false);
592 sm->cd = entry_node->getDoubleValue("cd", 0.193);
593 sm->weight = entry_node->getDoubleValue("weight", 0.25);
594 sm->aero_stabilised = entry_node->getBoolValue("aero-stabilised", true);
595 sm->no_roll = entry_node->getBoolValue("no-roll", false);
596 sm->collision = entry_node->getBoolValue("collision", false);
597 sm->impact = entry_node->getBoolValue("impact", false);
598 sm->impact_report = entry_node->getStringValue("impact-reports");
599 sm->fuse_range = entry_node->getDoubleValue("fuse-range", 0.0);
600 sm->contents_node = fgGetNode(entry_node->getStringValue("contents", "none"), false);
601 sm->speed_node = fgGetNode(entry_node->getStringValue("speed-node", "none"), false);
602 sm->submodel = entry_node->getStringValue("submodel-path", "");
603 sm->ext_force = entry_node->getBoolValue("external-force", false);
604 sm->force_path = entry_node->getStringValue("force-path", "");
605 //cout << "sm->contents_node " << sm->contents_node << endl;
606 if (sm->contents_node != 0)
607 sm->contents = sm->contents_node->getDoubleValue();
609 const char *trigger_path = entry_node->getStringValue("trigger", 0);
611 sm->trigger_node = fgGetNode(trigger_path, true);
612 sm->trigger_node->setBoolValue(sm->trigger_node->getBoolValue());
614 sm->trigger_node = 0;
617 SG_LOG(SG_GENERAL, SG_DEBUG, "Submodels: trigger " << sm->trigger_node->getBoolValue() );
619 if (sm->speed_node != 0)
620 sm->speed = sm->speed_node->getDoubleValue();
622 sm->timer = sm->delay;
624 sm->first_time = false;
625 sm->serviceable = serviceable;
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)));
634 sm->prop->tie("serviceable", SGRawValuePointer<bool>(&(sm->serviceable)));
635 string name = sm->name;
636 sm->prop->setStringValue("name", name.c_str());
638 string submodel = sm->submodel;
639 sm->prop->setStringValue("submodel", submodel.c_str());
640 //cout << " set submodel path " << submodel << endl;
642 string force_path = sm->force_path;
643 sm->prop->setStringValue("force_path", force_path.c_str());
644 //cout << "set force_path " << force_path << endl;
646 if (sm->contents_node != 0)
647 sm->prop->tie("contents-lbs", SGRawValuePointer<double>(&(sm->contents)));
650 submodels.push_back(sm);
654 void FGSubmodelMgr::setSubData(int id, string& path, bool serviceable)
658 SGPath config(globals->get_fg_root());
660 SG_LOG(SG_GENERAL, SG_DEBUG,
661 "Submodels: path " << path);
663 SG_LOG(SG_GENERAL, SG_DEBUG,
664 "Submodels: Trying to read AI submodels file: " << config.str());
665 readProperties(config.str(), &root);
667 } catch (const sg_exception &) {
668 SG_LOG(SG_GENERAL, SG_DEBUG,
669 "Submodels: Unable to read AI submodels file: " << config.str());
673 vector<SGPropertyNode_ptr> children = root.getChildren("submodel");
674 vector<SGPropertyNode_ptr>::iterator it = children.begin();
675 vector<SGPropertyNode_ptr>::iterator end = children.end();
677 for (int i = 0; it != end; ++it, i++) {
678 //cout << "Reading AI submodel " << (*it)->getPath() << endl;
679 submodel* sm = new submodel;
680 SGPropertyNode * entry_node = *it;
681 sm->name = entry_node->getStringValue("name", "none_defined");
682 sm->model = entry_node->getStringValue("model", "Models/Geometry/rocket.ac");
683 sm->speed = entry_node->getDoubleValue("speed", 2329.4);
684 sm->repeat = entry_node->getBoolValue("repeat", false);
685 sm->delay = entry_node->getDoubleValue("delay", 0.25);
686 sm->count = entry_node->getIntValue("count", 1);
687 sm->slaved = entry_node->getBoolValue("slaved", false);
688 sm->x_offset = entry_node->getDoubleValue("x-offset", 0.0);
689 sm->y_offset = entry_node->getDoubleValue("y-offset", 0.0);
690 sm->z_offset = entry_node->getDoubleValue("z-offset", 0.0);
691 sm->yaw_offset = entry_node->getDoubleValue("yaw-offset", 0.0);
692 sm->pitch_offset = entry_node->getDoubleValue("pitch-offset", 0.0);
693 sm->drag_area = entry_node->getDoubleValue("eda", 0.034);
694 sm->life = entry_node->getDoubleValue("life", 900.0);
695 sm->buoyancy = entry_node->getDoubleValue("buoyancy", 0);
696 sm->wind = entry_node->getBoolValue("wind", false);
697 sm->cd = entry_node->getDoubleValue("cd", 0.193);
698 sm->weight = entry_node->getDoubleValue("weight", 0.25);
699 sm->aero_stabilised = entry_node->getBoolValue("aero-stabilised", true);
700 sm->no_roll = entry_node->getBoolValue("no-roll", false);
701 sm->collision = entry_node->getBoolValue("collision", false);
702 sm->impact = entry_node->getBoolValue("impact", false);
703 sm->impact_report = entry_node->getStringValue("impact-reports");
704 sm->fuse_range = entry_node->getDoubleValue("fuse-range", 0.0);
705 sm->contents_node = fgGetNode(entry_node->getStringValue("contents", "none"), false);
706 sm->speed_node = fgGetNode(entry_node->getStringValue("speed-node", "none"), false);
707 sm->submodel = entry_node->getStringValue("submodel-path", "");
708 sm->ext_force = entry_node->getBoolValue("external-force", false);
709 sm->force_path = entry_node->getStringValue("force-path", "");
711 //cout << "sm->contents_node " << sm->contents_node << endl;
712 if (sm->contents_node != 0)
713 sm->contents = sm->contents_node->getDoubleValue();
715 const char *trigger_path = entry_node->getStringValue("trigger", 0);
717 sm->trigger_node = fgGetNode(trigger_path, true);
718 sm->trigger_node->setBoolValue(sm->trigger_node->getBoolValue());
720 sm->trigger_node = 0;
723 if (sm->speed_node != 0)
724 sm->speed = sm->speed_node->getDoubleValue();
726 sm->timer = sm->delay;
728 sm->first_time = false;
729 sm->serviceable = serviceable;
732 sm->prop = fgGetNode("/ai/submodels/subsubmodel", index, true);
733 sm->prop->tie("count", SGRawValuePointer<int>(&(sm->count)));
734 sm->prop->tie("repeat", SGRawValuePointer<bool>(&(sm->repeat)));
735 sm->prop->tie("id", SGRawValuePointer<int>(&(sm->id)));
736 sm->prop->tie("sub-id", SGRawValuePointer<int>(&(sm->sub_id)));
737 sm->prop->tie("serviceable", SGRawValuePointer<bool>(&(sm->serviceable)));
738 string name = sm->name;
739 sm->prop->setStringValue("name", name.c_str());
741 string submodel = sm->submodel;
742 sm->prop->setStringValue("submodel", submodel.c_str());
743 // cout << " set submodel path " << submodel<< endl;
745 string force_path = sm->force_path;
746 sm->prop->setStringValue("force_path", force_path.c_str());
747 //cout << "set force_path " << force_path << endl;
749 if (sm->contents_node != 0)
750 sm->prop->tie("contents-lbs", SGRawValuePointer<double>(&(sm->contents)));
753 subsubmodels.push_back(sm);
757 void FGSubmodelMgr::loadSubmodels()
759 SG_LOG(SG_GENERAL, SG_DEBUG, "Submodels: Loading sub submodels");
761 submodel_iterator = submodels.begin();
763 while (submodel_iterator != submodels.end()) {
764 string submodel = (*submodel_iterator)->submodel;
765 if (!submodel.empty()) {
766 //int id = (*submodel_iterator)->id;
767 bool serviceable = true;
768 SG_LOG(SG_GENERAL, SG_DEBUG, "found path sub sub "
770 << " index " << index
771 << "name " << (*submodel_iterator)->name);
773 (*submodel_iterator)->sub_id = index;
774 setSubData(index, submodel, serviceable);
780 subsubmodel_iterator = subsubmodels.begin();
782 while (subsubmodel_iterator != subsubmodels.end()) {
783 submodels.push_back(*subsubmodel_iterator);
784 ++subsubmodel_iterator;
787 submodel_iterator = submodels.begin();
789 while (submodel_iterator != submodels.end()) {
790 int id = (*submodel_iterator)->id;
791 SG_LOG(SG_GENERAL, SG_DEBUG,"after pushback "
793 << " name " << (*submodel_iterator)->name
794 << " sub id " << (*submodel_iterator)->sub_id);
800 // end of submodel.cxx