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 = z_offset = 0.0;
35 //out[0] = out[1] = out[2] = 0;
37 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");
83 void FGSubmodelMgr::postinit() {
84 // postinit, so that the AI list is populated
91 //TODO reload submodels if an MP ac joins
93 //_model_added_node = fgGetNode("ai/models/model-added", true);
94 //_model_added_node->addChangeListener(this, false);
97 void FGSubmodelMgr::bind()
100 void FGSubmodelMgr::unbind()
102 submodel_iterator = submodels.begin();
103 while (submodel_iterator != submodels.end()) {
104 (*submodel_iterator)->prop->untie("count");
109 void FGSubmodelMgr::update(double dt)
111 if (!_serviceable_node->getBoolValue())
118 // check if the submodel hit an object or terrain
119 sm_list = ai->get_ai_list();
120 sm_list_iterator sm_list_itr = sm_list.begin();
121 sm_list_iterator end = sm_list.end();
123 for (; sm_list_itr != end; ++sm_list_itr) {
124 FGAIBase::object_type object_type =(*sm_list_itr)->getType();
126 if (object_type != FGAIBase::otBallistic){// only work on ballistic objects
127 continue; // so continue
130 int parent_subID = (*sm_list_itr)->_getSubID();
131 int id = (*sm_list_itr)->getID();
133 if ( parent_subID == 0 || id == -1) // this entry in the list has no associated submodel
134 continue; // or is invalid so we can continue
136 //SG_LOG(SG_GENERAL, SG_DEBUG, "Submodel: Impact " << _impact << " hit! "
137 // << _hit <<" parent_subID " << parent_subID);
139 _hit = (*sm_list_itr)->_getCollisionData();
140 _impact = (*sm_list_itr)->_getImpactData();
141 _expiry = (*sm_list_itr)->_getExpiryData();
143 //SG_LOG(SG_GENERAL, SG_ALERT, "Submodel: " << (*sm_list_itr)->_getName()
144 // << " Impact " << _impact << " hit! " << _hit
145 // << " exipiry :-( " << _expiry );
147 if (_impact || _hit || _expiry) {
148 // SG_LOG(SG_GENERAL, SG_ALERT, "Submodel: Impact " << _impact << " hit! " << _hit
149 //<< " exipiry :-( " << _expiry );
151 submodel_iterator = submodels.begin();
153 while (submodel_iterator != submodels.end()) {
154 int child_ID = (*submodel_iterator)->id;
155 //cout << "Impact: parent SubID " << parent_subID << " child_ID " << child_ID << endl;
157 if ( parent_subID == child_ID ) {
158 _parent_lat = (*sm_list_itr)->_getImpactLat();
159 _parent_lon = (*sm_list_itr)->_getImpactLon();
160 _parent_elev = (*sm_list_itr)->_getImpactElevFt();
161 _parent_hdg = (*sm_list_itr)->_getImpactHdg();
162 _parent_pitch = (*sm_list_itr)->_getImpactPitch();
163 _parent_roll = (*sm_list_itr)->_getImpactRoll();
164 _parent_speed = (*sm_list_itr)->_getImpactSpeed();
165 (*submodel_iterator)->first_time = true;
166 //cout << "Impact: parent SubID = child_ID elev " << _parent_elev << endl;
168 if (release(*submodel_iterator, dt)){
169 (*sm_list_itr)->setDie(true);
170 //cout << "Impact: set die" << (*sm_list_itr)->_getName() << endl;
180 _contrail_trigger->setBoolValue(_user_alt_node->getDoubleValue() > contrail_altitude);
183 // bool in_range = true;
184 bool trigger = false;
187 submodel_iterator = submodels.begin();
188 while (submodel_iterator != submodels.end()) {
191 /*SG_LOG(SG_GENERAL, SG_DEBUG,
192 "Submodels: " << (*submodel_iterator)->id
193 << " name " << (*submodel_iterator)->name
196 if ((*submodel_iterator)->trigger_node != 0) {
197 _trigger_node = (*submodel_iterator)->trigger_node;
198 trigger = _trigger_node->getBoolValue();
199 //cout << (*submodel_iterator)->name << "trigger node found " << trigger << endl;
202 //cout << (*submodel_iterator)->name << " trigger node not found " << trigger << endl;
205 if (trigger && (*submodel_iterator)->count != 0) {
207 int id = (*submodel_iterator)->id;
208 string name = (*submodel_iterator)->name;
210 SG_LOG(SG_GENERAL, SG_DEBUG,
211 "Submodels release: " << (*submodel_iterator)->id
212 << " name " << (*submodel_iterator)->name
213 << " count " << (*submodel_iterator)->count
214 << " slaved " << (*submodel_iterator)->slaved
217 release(*submodel_iterator, dt);
219 (*submodel_iterator)->first_time = true;
225 bool FGSubmodelMgr::release(submodel *sm, double dt)
227 //cout << "release id " << sm->id
228 // << " name " << sm->name
229 // << " first time " << sm->first_time
230 // << " repeat " << sm->repeat
231 // << " slaved " << sm->slaved
234 // only run if first time or repeat is set to true
235 if (!sm->first_time && !sm->repeat) {
236 //cout<< "returning: "<< sm->name
237 // << " not first time " << sm->first_time
238 // << " repeat " << sm->repeat
239 // << " slaved " << sm->slaved
246 if (sm->timer < sm->delay) {
247 //cout << "not yet: timer " << sm->timer << " delay " << sm->delay << endl;
251 //cout << "released 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->setName(sm->name);
265 ballist->setSlaved(sm->slaved);
266 ballist->setRandom(sm->random);
267 ballist->setRandomness(sm->randomness);
268 ballist->setLatitude(offsetpos.getLatitudeDeg());
269 ballist->setLongitude(offsetpos.getLongitudeDeg());
270 ballist->setAltitude(offsetpos.getElevationFt());
271 ballist->setAzimuth(IC.azimuth);
272 ballist->setElevation(IC.elevation);
273 ballist->setRoll(IC.roll);
274 ballist->setSpeed(IC.speed / SG_KT_TO_FPS);
275 ballist->setWind_from_east(IC.wind_from_east);
276 ballist->setWind_from_north(IC.wind_from_north);
277 ballist->setMass(IC.mass);
278 ballist->setDragArea(sm->drag_area);
279 ballist->setLife(sm->life);
280 ballist->setBuoyancy(sm->buoyancy);
281 ballist->setWind(sm->wind);
282 ballist->setCd(sm->cd);
283 ballist->setStabilisation(sm->aero_stabilised);
284 ballist->setNoRoll(sm->no_roll);
285 ballist->setCollision(sm->collision);
286 ballist->setExpiry(sm->expiry);
287 ballist->setImpact(sm->impact);
288 ballist->setImpactReportNode(sm->impact_report);
289 ballist->setFuseRange(sm->fuse_range);
290 ballist->setSubmodel(sm->submodel.c_str());
291 ballist->setSubID(sm->sub_id);
292 ballist->setForceStabilisation(sm->force_stabilised);
293 ballist->setExternalForce(sm->ext_force);
294 ballist->setForcePath(sm->force_path.c_str());
295 ballist->setXoffset(sm->x_offset);
296 ballist->setYoffset(sm->y_offset);
297 ballist->setZoffset(sm->z_offset);
298 ballist->setPitchoffset(sm->pitch_offset);
299 ballist->setYawoffset(sm->yaw_offset);
300 ballist->setParentNodes(_selected_ac);
301 ballist->setContentsNode(sm->contents_node);
302 ballist->setWeight(sm->weight);
310 void FGSubmodelMgr::load()
312 SGPropertyNode *path = fgGetNode("/sim/submodels/path");
316 string Path = path->getStringValue();
317 bool Seviceable =_serviceable_node->getBoolValue();
318 setData(id, Path, Seviceable);
322 void FGSubmodelMgr::transform(submodel *sm)
324 // set initial conditions
325 if (sm->contents_node != 0 && !sm->slaved) {
326 // get the weight of the contents (lbs) and convert to mass (slugs)
327 sm->contents = sm->contents_node->getChild("level-lbs",0,1)->getDoubleValue();
328 //cout << "transform: contents " << sm->contents << endl;
329 IC.mass = (sm->weight + sm->contents) * lbs_to_slugs;
330 //cout << "mass inc contents" << IC.mass << endl;
332 // set contents to 0 in the parent
333 sm->contents_node->getChild("level-gal_us",0,1)->setDoubleValue(0);
334 /*cout << "contents " << sm->contents_node->getChild("level-gal_us")->getDoubleValue()
335 << " " << sm->contents_node->getChild("level-lbs",0,1)->getDoubleValue()
338 IC.mass = sm->weight * lbs_to_slugs;
341 int sub_id = sm->sub_id;
342 string name = sm->name;
345 if (sm->speed_node != 0)
346 sm->speed = sm->speed_node->getDoubleValue();
349 //cout << " name " << name << " id " << id << " sub id" << sub_id << endl;
351 // set the Initial Conditions for the types of submodel parent
353 if (_impact || _hit || _expiry) {
354 // set the data for a submodel tied to a submodel
358 IC.lat = _parent_lat;
359 IC.lon = _parent_lon;
360 IC.alt = _parent_elev;
361 IC.roll = _parent_roll; // rotation about x axis
362 IC.elevation = _parent_pitch; // rotation about y axis
363 IC.azimuth = _parent_hdg; // rotation about z axis
364 IC.speed = _parent_speed;
365 IC.speed_down_fps = 0;
366 IC.speed_east_fps = 0;
367 IC.speed_north_fps = 0;
369 } else if (id == 0) {
370 //set the data for a submodel tied to the main model
372 IC.lat = _user_lat_node->getDoubleValue();
373 IC.lon = _user_lon_node->getDoubleValue();
374 IC.alt = _user_alt_node->getDoubleValue();
375 IC.roll = _user_roll_node->getDoubleValue(); // rotation about x axis
376 IC.elevation = _user_pitch_node->getDoubleValue(); // rotation about y axis
377 IC.azimuth = _user_heading_node->getDoubleValue(); // rotation about z axis
378 IC.speed = _user_speed_node->getDoubleValue();
379 IC.speed_down_fps = _user_speed_down_fps_node->getDoubleValue();
380 IC.speed_east_fps = _user_speed_east_fps_node->getDoubleValue();
381 IC.speed_north_fps = _user_speed_north_fps_node->getDoubleValue();
384 // set the data for a submodel tied to an AI Object
385 //cout << " set the data for a submodel tied to an AI Object " << id << endl;
386 sm_list_iterator sm_list_itr = sm_list.begin();
387 sm_list_iterator end = sm_list.end();
391 //cout << "Submodel: setting IC "<< name << endl;
392 //cout << "heading " << IC.azimuth << endl ;
393 //cout << "speed down " << IC.speed_down_fps << endl ;
394 //cout << "speed east " << IC.speed_east_fps << endl ;
395 //cout << "speed north " << IC.speed_north_fps << endl ;
396 //cout << "parent speed fps in " << IC.speed << "sm speed in " << sm->speed << endl ;
397 //cout << "lat " << IC.lat;
398 //cout << "alt " << IC.alt << endl ;
401 // Set the Initial Conditions that are common to all types of parent
402 IC.wind_from_east = _user_wind_from_east_node->getDoubleValue();
403 IC.wind_from_north = _user_wind_from_north_node->getDoubleValue();
405 //cout << "wind e " << IC.wind_from_east << " n " << IC.wind_from_north << endl;
407 userpos.setLatitudeDeg(IC.lat);
408 userpos.setLongitudeDeg(IC.lon);
409 userpos.setElevationFt(IC.alt);
411 _x_offset = sm->x_offset;
412 _y_offset = sm->y_offset;
413 _z_offset = sm->z_offset;
417 //IC.elevation += sm->pitch_offset;
418 //IC.azimuth += sm->yaw_offset ;
420 // pre-process the trig functions
421 cosRx = cos(-IC.roll * SG_DEGREES_TO_RADIANS);
422 sinRx = sin(-IC.roll * SG_DEGREES_TO_RADIANS);
423 cosRy = cos(-IC.elevation * SG_DEGREES_TO_RADIANS);
424 sinRy = sin(-IC.elevation * SG_DEGREES_TO_RADIANS);
425 cosRz = cos(IC.azimuth * SG_DEGREES_TO_RADIANS);
426 sinRz = sin(IC.azimuth * SG_DEGREES_TO_RADIANS);
429 // Get submodel initial velocity vector angles in XZ and XY planes.
430 // This vector should be added to aircraft's vector.
431 IC.elevation += (sm->yaw_offset * sinRx) + (sm->pitch_offset * cosRx);
432 IC.azimuth += (sm->yaw_offset * cosRx) - (sm->pitch_offset * sinRx);
434 // calculate the total speed north
435 IC.total_speed_north = sm->speed * cos(IC.elevation * SG_DEGREES_TO_RADIANS)
436 * cos(IC.azimuth * SG_DEGREES_TO_RADIANS) + IC.speed_north_fps;
438 // calculate the total speed east
439 IC.total_speed_east = sm->speed * cos(IC.elevation * SG_DEGREES_TO_RADIANS)
440 * sin(IC.azimuth * SG_DEGREES_TO_RADIANS) + IC.speed_east_fps;
442 // calculate the total speed down
443 IC.total_speed_down = sm->speed * -sin(IC.elevation * SG_DEGREES_TO_RADIANS)
446 // re-calculate speed, elevation and azimuth
447 IC.speed = sqrt(IC.total_speed_north * IC.total_speed_north
448 + IC.total_speed_east * IC.total_speed_east
449 + IC.total_speed_down * IC.total_speed_down);
451 // if speeds are low this calculation can become unreliable
453 IC.azimuth = atan2(IC.total_speed_east, IC.total_speed_north) * SG_RADIANS_TO_DEGREES;
454 // cout << "azimuth1 " << IC.azimuth<<endl;
456 // rationalise the output
459 else if (IC.azimuth >= 360)
461 // cout << "azimuth2 " << IC.azimuth<<endl;
463 IC.elevation = -atan(IC.total_speed_down / sqrt(IC.total_speed_north
464 * IC.total_speed_north + IC.total_speed_east * IC.total_speed_east))
465 * SG_RADIANS_TO_DEGREES;
467 //cout << "IC.speed " << IC.speed / SG_KT_TO_FPS << endl;
470 void FGSubmodelMgr::updatelat(double lat)
472 ft_per_deg_latitude = 366468.96 - 3717.12 * cos(lat / SG_RADIANS_TO_DEGREES);
473 ft_per_deg_longitude = 365228.16 * cos(lat / SG_RADIANS_TO_DEGREES);
476 void FGSubmodelMgr::loadAI()
478 SG_LOG(SG_GENERAL, SG_DEBUG, "Submodels: Loading AI submodels ");
480 sm_list = ai->get_ai_list();
482 if (sm_list.empty()) {
483 SG_LOG(SG_GENERAL, SG_ALERT, "Submodels: Unable to read AI submodel list");
487 sm_list_iterator sm_list_itr = sm_list.begin();
488 sm_list_iterator end = sm_list.end();
490 while (sm_list_itr != end) {
491 string path = (*sm_list_itr)->_getSMPath();
498 int id = (*sm_list_itr)->getID();
499 string type = (*sm_list_itr)->getTypeString();
500 bool serviceable = (*sm_list_itr)->_getServiceable();
502 //cout << "loadAI: type " << type << " path "<< path << " serviceable " << serviceable << endl;
504 setData(id, path, serviceable);
511 void FGSubmodelMgr::setData(int id, string& path, bool serviceable)
515 SGPath config = globals->resolve_aircraft_path(path);
517 SG_LOG(SG_GENERAL, SG_DEBUG,
518 "Submodels: Trying to read AI submodels file: " << config.str());
519 readProperties(config.str(), &root);
520 } catch (const sg_exception &) {
521 SG_LOG(SG_GENERAL, SG_ALERT,
522 "Submodels: Unable to read AI submodels file: " << config.str());
526 vector<SGPropertyNode_ptr> children = root.getChildren("submodel");
527 vector<SGPropertyNode_ptr>::iterator it = children.begin();
528 vector<SGPropertyNode_ptr>::iterator end = children.end();
530 for (int i = 0; it != end; ++it, i++) {
531 //cout << "Reading AI submodel " << (*it)->getPath() << endl;
532 submodel* sm = new submodel;
533 SGPropertyNode * entry_node = *it;
534 sm->name = entry_node->getStringValue("name", "none_defined");
535 sm->model = entry_node->getStringValue("model", "Models/Geometry/rocket.ac");
536 sm->speed = entry_node->getDoubleValue("speed", 2329.4);
537 sm->repeat = entry_node->getBoolValue("repeat", false);
538 sm->delay = entry_node->getDoubleValue("delay", 0.25);
539 sm->count = entry_node->getIntValue("count", 1);
540 sm->slaved = entry_node->getBoolValue("slaved", false);
541 sm->x_offset = entry_node->getDoubleValue("x-offset", 0.0);
542 sm->y_offset = entry_node->getDoubleValue("y-offset", 0.0);
543 sm->z_offset = entry_node->getDoubleValue("z-offset", 0.0);
544 sm->yaw_offset = entry_node->getDoubleValue("yaw-offset", 0.0);
545 sm->pitch_offset = entry_node->getDoubleValue("pitch-offset", 0.0);
546 sm->drag_area = entry_node->getDoubleValue("eda", 0.034);
547 sm->life = entry_node->getDoubleValue("life", 900.0);
548 sm->buoyancy = entry_node->getDoubleValue("buoyancy", 0);
549 sm->wind = entry_node->getBoolValue("wind", false);
550 sm->cd = entry_node->getDoubleValue("cd", 0.193);
551 sm->weight = entry_node->getDoubleValue("weight", 0.25);
552 sm->aero_stabilised = entry_node->getBoolValue("aero-stabilised", true);
553 sm->no_roll = entry_node->getBoolValue("no-roll", false);
554 sm->collision = entry_node->getBoolValue("collision", false);
555 sm->expiry = entry_node->getBoolValue("expiry", false);
556 sm->impact = entry_node->getBoolValue("impact", false);
557 sm->impact_report = entry_node->getStringValue("impact-reports");
558 sm->fuse_range = entry_node->getDoubleValue("fuse-range", 0.0);
559 sm->contents_node = fgGetNode(entry_node->getStringValue("contents", "none"), false);
560 sm->speed_node = fgGetNode(entry_node->getStringValue("speed-prop", "none"), false);
561 sm->submodel = entry_node->getStringValue("submodel-path", "");
562 sm->force_stabilised= entry_node->getBoolValue("force-stabilised", false);
563 sm->ext_force = entry_node->getBoolValue("external-force", false);
564 sm->force_path = entry_node->getStringValue("force-path", "");
565 sm->random = entry_node->getBoolValue("random", false);
566 sm->randomness = entry_node->getDoubleValue("randomness", 0.5);
568 if (sm->contents_node != 0)
569 sm->contents = sm->contents_node->getDoubleValue();
571 const char *trigger_path = entry_node->getStringValue("trigger", 0);
573 sm->trigger_node = fgGetNode(trigger_path, true);
574 sm->trigger_node->setBoolValue(sm->trigger_node->getBoolValue());
576 sm->trigger_node = 0;
579 if (sm->speed_node != 0)
580 sm->speed = sm->speed_node->getDoubleValue();
582 sm->timer = sm->delay;
584 sm->first_time = false;
585 sm->serviceable = serviceable;
588 sm->prop = fgGetNode("/ai/submodels/submodel", index, true);
589 sm->prop->tie("delay", SGRawValuePointer<double>(&(sm->delay)));
590 sm->prop->tie("count", SGRawValuePointer<int>(&(sm->count)));
591 sm->prop->tie("repeat", SGRawValuePointer<bool>(&(sm->repeat)));
592 sm->prop->tie("id", SGRawValuePointer<int>(&(sm->id)));
593 sm->prop->tie("sub-id", SGRawValuePointer<int>(&(sm->sub_id)));
594 sm->prop->tie("serviceable", SGRawValuePointer<bool>(&(sm->serviceable)));
595 sm->prop->tie("random", SGRawValuePointer<bool>(&(sm->random)));
596 sm->prop->tie("slaved", SGRawValuePointer<bool>(&(sm->slaved)));
597 string name = sm->name;
598 sm->prop->setStringValue("name", name.c_str());
600 string submodel = sm->submodel;
601 sm->prop->setStringValue("submodel", submodel.c_str());
603 string force_path = sm->force_path;
604 sm->prop->setStringValue("force_path", force_path.c_str());
605 //cout << "set force_path Sub " << force_path << endl;
607 if (sm->contents_node != 0)
608 sm->prop->tie("contents-lbs", SGRawValuePointer<double>(&(sm->contents)));
611 submodels.push_back(sm);
615 void FGSubmodelMgr::setSubData(int id, string& path, bool serviceable)
618 SGPath config = globals->resolve_aircraft_path(path);
621 SG_LOG(SG_GENERAL, SG_DEBUG,
622 "Submodels: Trying to read AI submodels file: " << config.str());
623 readProperties(config.str(), &root);
625 } catch (const sg_exception &) {
626 SG_LOG(SG_GENERAL, SG_ALERT,
627 "Submodels: Unable to read AI submodels file: " << config.str());
631 vector<SGPropertyNode_ptr> children = root.getChildren("submodel");
632 vector<SGPropertyNode_ptr>::iterator it = children.begin();
633 vector<SGPropertyNode_ptr>::iterator end = children.end();
635 for (int i = 0; it != end; ++it, i++) {
636 //cout << "Reading AI submodel " << (*it)->getPath() << endl;
637 submodel* sm = new submodel;
638 SGPropertyNode * entry_node = *it;
639 sm->name = entry_node->getStringValue("name", "none_defined");
640 sm->model = entry_node->getStringValue("model", "Models/Geometry/rocket.ac");
641 sm->speed = entry_node->getDoubleValue("speed", 2329.4);
642 sm->repeat = entry_node->getBoolValue("repeat", false);
643 sm->delay = entry_node->getDoubleValue("delay", 0.25);
644 sm->count = entry_node->getIntValue("count", 1);
645 sm->slaved = entry_node->getBoolValue("slaved", false);
646 sm->x_offset = entry_node->getDoubleValue("x-offset", 0.0);
647 sm->y_offset = entry_node->getDoubleValue("y-offset", 0.0);
648 sm->z_offset = entry_node->getDoubleValue("z-offset", 0.0);
649 sm->yaw_offset = entry_node->getDoubleValue("yaw-offset", 0.0);
650 sm->pitch_offset = entry_node->getDoubleValue("pitch-offset", 0.0);
651 sm->drag_area = entry_node->getDoubleValue("eda", 0.034);
652 sm->life = entry_node->getDoubleValue("life", 900.0);
653 sm->buoyancy = entry_node->getDoubleValue("buoyancy", 0);
654 sm->wind = entry_node->getBoolValue("wind", false);
655 sm->cd = entry_node->getDoubleValue("cd", 0.193);
656 sm->weight = entry_node->getDoubleValue("weight", 0.25);
657 sm->aero_stabilised = entry_node->getBoolValue("aero-stabilised", true);
658 sm->no_roll = entry_node->getBoolValue("no-roll", false);
659 sm->collision = entry_node->getBoolValue("collision", false);
660 sm->expiry = entry_node->getBoolValue("expiry", false);
661 sm->impact = entry_node->getBoolValue("impact", false);
662 sm->impact_report = entry_node->getStringValue("impact-reports");
663 sm->fuse_range = entry_node->getDoubleValue("fuse-range", 0.0);
664 sm->contents_node = fgGetNode(entry_node->getStringValue("contents", "none"), false);
665 sm->speed_node = fgGetNode(entry_node->getStringValue("speed-prop", "none"), false);
666 sm->submodel = entry_node->getStringValue("submodel-path", "");
667 sm->force_stabilised= entry_node->getBoolValue("force-stabilised", false);
668 sm->ext_force = entry_node->getBoolValue("external-force", false);
669 sm->force_path = entry_node->getStringValue("force-path", "");
670 sm->random = entry_node->getBoolValue("random", false);
671 sm->randomness = entry_node->getDoubleValue("randomness", 0.5);
673 if (sm->contents_node != 0)
674 sm->contents = sm->contents_node->getDoubleValue();
676 const char *trigger_path = entry_node->getStringValue("trigger", 0);
678 sm->trigger_node = fgGetNode(trigger_path, true);
679 sm->trigger_node->setBoolValue(sm->trigger_node->getBoolValue());
681 sm->trigger_node = 0;
684 if (sm->speed_node != 0)
685 sm->speed = sm->speed_node->getDoubleValue();
687 sm->timer = sm->delay;
689 sm->first_time = false;
690 sm->serviceable = serviceable;
693 sm->prop = fgGetNode("/ai/submodels/subsubmodel", index, true);
694 sm->prop->tie("count", SGRawValuePointer<int>(&(sm->count)));
695 sm->prop->tie("repeat", SGRawValuePointer<bool>(&(sm->repeat)));
696 sm->prop->tie("id", SGRawValuePointer<int>(&(sm->id)));
697 sm->prop->tie("sub-id", SGRawValuePointer<int>(&(sm->sub_id)));
698 sm->prop->tie("serviceable", SGRawValuePointer<bool>(&(sm->serviceable)));
699 sm->prop->tie("random", SGRawValuePointer<bool>(&(sm->random)));
700 sm->prop->tie("slaved", SGRawValuePointer<bool>(&(sm->slaved)));
702 string name = sm->name;
703 sm->prop->setStringValue("name", name.c_str());
705 string submodel = sm->submodel;
706 sm->prop->setStringValue("submodel-path", submodel.c_str());
707 // cout << " set submodel path AI" << submodel<< endl;
709 string force_path = sm->force_path;
710 sm->prop->setStringValue("force_path", force_path.c_str());
711 //cout << "set force_path AI" << force_path << endl;
713 if (sm->contents_node != 0)
714 sm->prop->tie("contents-lbs", SGRawValuePointer<double>(&(sm->contents)));
717 subsubmodels.push_back(sm);
721 void FGSubmodelMgr::loadSubmodels()
723 SG_LOG(SG_GENERAL, SG_DEBUG, "Submodels: Loading sub submodels");
727 submodel_iterator = submodels.begin();
729 while (submodel_iterator != submodels.end()) {
730 string submodel = (*submodel_iterator)->submodel;
731 if (!submodel.empty()) {
732 //int id = (*submodel_iterator)->id;
733 bool serviceable = true;
734 SG_LOG(SG_GENERAL, SG_DEBUG, "found path sub sub "
736 << " index " << index
737 << " name " << (*submodel_iterator)->name);
739 if ((*submodel_iterator)->sub_id == 0){
740 (*submodel_iterator)->sub_id = index;
742 setSubData(index, submodel, serviceable);
749 subsubmodel_iterator = subsubmodels.begin();
751 while (subsubmodel_iterator != subsubmodels.end()) {
753 submodels.push_back(*subsubmodel_iterator);
754 ++subsubmodel_iterator;
757 subsubmodels.clear();
759 //submodel_iterator = submodels.begin();
763 //while (submodel_iterator != submodels.end()) {
764 // int id = (*submodel_iterator)->id;
767 // SG_LOG(SG_GENERAL, SG_ALERT,"after pushback "
768 // << " parent id " << id
769 // << " name " << (*submodel_iterator)->name
770 // << " sub id " << (*submodel_iterator)->sub_id
771 // << " subcount "<< subcount);
773 // ++submodel_iterator;
777 SGVec3d FGSubmodelMgr::getCartOffsetPos() const{
779 // convert geodetic positions to geocentered
780 SGVec3d cartuserPos = SGVec3d::fromGeod(userpos);
781 // Transform to the right coordinate frame, configuration is done in
782 // the x-forward, y-right, z-up coordinates (feet), computation
783 // in the simulation usual body x-forward, y-right, z-down coordinates
786 SGVec3d _off(_x_offset * SG_FEET_TO_METER,
787 _y_offset * SG_FEET_TO_METER,
788 -_z_offset * SG_FEET_TO_METER);
790 // Transform the user position to the horizontal local coordinate system.
791 SGQuatd hlTrans = SGQuatd::fromLonLat(userpos);
793 // and postrotate the orientation of the user model wrt the horizontal
795 hlTrans *= SGQuatd::fromYawPitchRollDeg(
800 // The offset converted to the usual body fixed coordinate system
801 // rotated to the earth-fixed coordinates axis
802 SGVec3d off = hlTrans.backTransform(_off);
804 // Add the position offset of the user model to get the geocentered position
805 SGVec3d offsetPos = cartuserPos + off;
809 void FGSubmodelMgr::setOffsetPos(){
810 // convert the offset geocentered position to geodetic
811 SGVec3d cartoffsetPos = getCartOffsetPos();
813 SGGeodesy::SGCartToGeod(cartoffsetPos, offsetpos);
815 //cout << "OFFSET POS" << offsetpos.getElevationFt();
819 void FGSubmodelMgr::valueChanged(SGPropertyNode *prop)
821 return; // this isn't working atm
823 const char* _model_added = _model_added_node->getStringValue();
825 basic_string <char>::size_type indexCh2b;
827 string str2 = _model_added;
828 const char *cstr2b = "multiplayer";
829 indexCh2b = str2.find( cstr2b, 0 );
831 if (indexCh2b != string::npos ){ // we will ignore Ballistic Objects - there are potentially too many
833 //cout << "Submodels: model added - " << str2 <<" read path "<< endl;
835 SGPropertyNode *a_node = fgGetNode(_model_added, true);
836 SGPropertyNode *sub_node = a_node->getChild("sim", 0, true);
837 SGPropertyNode_ptr path_node = sub_node->getChild("path", 0, true);
838 SGPropertyNode_ptr callsign_node = a_node->getChild("callsign", 0, true);
840 string callsign = callsign_node->getStringValue();
842 //cout << "Submodels: model added - " << callsign <<" read callsign "<< endl;
846 cout << "model added - " << str2 <<" returning "<< endl;
852 void FGSubmodelMgr::setParentNode(int id) {
854 const SGPropertyNode_ptr ai = fgGetNode("/ai/models", true);
856 for (int i = ai->nChildren() - 1; i >= -1; i--) {
857 SGPropertyNode_ptr model;
859 if (i < 0) { // last iteration: selected model
860 model = _selected_ac;
862 model = ai->getChild(i);
863 string path = ai->getPath();
864 const string name = model->getStringValue("name");
865 int parent_id = model->getIntValue("id");
866 if (!model->nChildren()){
869 if (parent_id == id) {
870 _selected_ac = model; // save selected model for last iteration
880 if (_selected_ac != 0){
882 //cout << " parent node found"<< endl;
884 const string name = _selected_ac->getStringValue("name");
885 IC.lat = _selected_ac->getDoubleValue("position/latitude-deg");
886 IC.lon = _selected_ac->getDoubleValue("position/longitude-deg");
887 IC.alt = _selected_ac->getDoubleValue("position/altitude-ft");
888 IC.roll = _selected_ac->getDoubleValue("orientation/roll-deg");
889 IC.elevation = _selected_ac->getDoubleValue("orientation/pitch-deg");
890 IC.azimuth = _selected_ac->getDoubleValue("orientation/true-heading-deg");
891 IC.speed = _selected_ac->getDoubleValue("velocities/true-airspeed-kt") * SG_KT_TO_FPS;
892 IC.speed_down_fps = -_selected_ac->getDoubleValue("velocities/vertical-speed-fps");
893 IC.speed_east_fps = _selected_ac->getDoubleValue("velocities/speed-east-fps");
894 IC.speed_north_fps = _selected_ac->getDoubleValue("velocities/speed-north-fps");
896 //cout << name << " IC.speed " << IC.speed << endl;
899 SG_LOG(SG_GENERAL, SG_ALERT, "AISubmodel: parent node not found ");
903 // end of submodel.cxx