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"
31 using FGXMLAutopilot::InputValue;
33 const double FGSubmodelMgr::lbs_to_slugs = 0.031080950172;
35 FGSubmodelMgr::FGSubmodelMgr()
37 x_offset = y_offset = z_offset = 0.0;
41 //out[0] = out[1] = out[2] = 0;
42 //string contents_node;
43 contrail_altitude = 30000;
48 FGSubmodelMgr::~FGSubmodelMgr()
52 FGAIManager* FGSubmodelMgr::aiManager()
54 return (FGAIManager*)globals->get_subsystem("ai-model");
57 void FGSubmodelMgr::init()
61 _serviceable_node = fgGetNode("/sim/submodels/serviceable", true);
62 _serviceable_node->setBoolValue(true);
64 _user_lat_node = fgGetNode("/position/latitude-deg", true);
65 _user_lon_node = fgGetNode("/position/longitude-deg", true);
66 _user_alt_node = fgGetNode("/position/altitude-ft", true);
68 _user_heading_node = fgGetNode("/orientation/heading-deg", true);
69 _user_pitch_node = fgGetNode("/orientation/pitch-deg", true);
70 _user_roll_node = fgGetNode("/orientation/roll-deg", true);
71 _user_yaw_node = fgGetNode("/orientation/yaw-deg", true);
72 _user_alpha_node = fgGetNode("/orientation/alpha-deg", true);
74 _user_speed_node = fgGetNode("/velocities/uBody-fps", true);
76 _user_wind_from_east_node = fgGetNode("/environment/wind-from-east-fps", true);
77 _user_wind_from_north_node = fgGetNode("/environment/wind-from-north-fps", true);
79 _user_speed_down_fps_node = fgGetNode("/velocities/speed-down-fps", true);
80 _user_speed_east_fps_node = fgGetNode("/velocities/speed-east-fps", true);
81 _user_speed_north_fps_node = fgGetNode("/velocities/speed-north-fps", true);
83 _contrail_altitude_node = fgGetNode("/environment/params/contrail-altitude", true);
84 contrail_altitude = _contrail_altitude_node->getDoubleValue();
85 _contrail_trigger = fgGetNode("ai/submodels/contrails", true);
86 _contrail_trigger->setBoolValue(false);
92 void FGSubmodelMgr::postinit() {
93 // postinit, so that the AI list is populated
100 //TODO reload submodels if an MP ac joins
102 //_model_added_node = fgGetNode("ai/models/model-added", true);
103 //_model_added_node->addChangeListener(this, false);
106 void FGSubmodelMgr::bind()
109 void FGSubmodelMgr::unbind()
111 submodel_iterator = submodels.begin();
112 while (submodel_iterator != submodels.end()) {
113 (*submodel_iterator)->prop->untie("count");
118 void FGSubmodelMgr::update(double dt)
120 if (!_serviceable_node->getBoolValue())
127 // check if the submodel hit an object or terrain
128 FGAIManager::ai_list_type sm_list(aiManager()->get_ai_list());
129 FGAIManager::ai_list_iterator sm_list_itr = sm_list.begin(),
132 for (; sm_list_itr != end; ++sm_list_itr) {
133 FGAIBase::object_type object_type =(*sm_list_itr)->getType();
135 if (object_type != FGAIBase::otBallistic){// only work on ballistic objects
136 continue; // so continue
139 int parent_subID = (*sm_list_itr)->_getSubID();
140 int id = (*sm_list_itr)->getID();
142 if ( parent_subID == 0 || id == -1) // this entry in the list has no associated submodel
143 continue; // or is invalid so we can continue
145 //SG_LOG(SG_AI, SG_DEBUG, "Submodel: Impact " << _impact << " hit! "
146 // << _hit <<" parent_subID " << parent_subID);
148 _hit = (*sm_list_itr)->_getCollisionData();
149 _impact = (*sm_list_itr)->_getImpactData();
150 _expiry = (*sm_list_itr)->_getExpiryData();
152 //SG_LOG(SG_AI, SG_ALERT, "Submodel: " << (*sm_list_itr)->_getName()
153 // << " Impact " << _impact << " hit! " << _hit
154 // << " exipiry :-( " << _expiry );
156 if (_impact || _hit || _expiry) {
157 // SG_LOG(SG_AI, SG_ALERT, "Submodel: Impact " << _impact << " hit! " << _hit
158 //<< " exipiry :-( " << _expiry );
160 submodel_iterator = submodels.begin();
162 while (submodel_iterator != submodels.end()) {
163 int child_ID = (*submodel_iterator)->id;
164 //cout << "Impact: parent SubID " << parent_subID << " child_ID " << child_ID << endl;
166 if ( parent_subID == child_ID ) {
167 _parent_lat = (*sm_list_itr)->_getImpactLat();
168 _parent_lon = (*sm_list_itr)->_getImpactLon();
169 _parent_elev = (*sm_list_itr)->_getImpactElevFt();
170 _parent_hdg = (*sm_list_itr)->_getImpactHdg();
171 _parent_pitch = (*sm_list_itr)->_getImpactPitch();
172 _parent_roll = (*sm_list_itr)->_getImpactRoll();
173 _parent_speed = (*sm_list_itr)->_getImpactSpeed();
174 (*submodel_iterator)->first_time = true;
175 //cout << "Impact: parent SubID = child_ID elev " << _parent_elev << endl;
177 if (release(*submodel_iterator, dt)){
178 (*sm_list_itr)->setDie(true);
179 //cout << "Impact: set die" << (*sm_list_itr)->_getName() << endl;
189 _contrail_trigger->setBoolValue(_user_alt_node->getDoubleValue() > contrail_altitude);
192 // bool in_range = true;
193 bool trigger = false;
196 submodel_iterator = submodels.begin();
197 while (submodel_iterator != submodels.end()) {
200 /*SG_LOG(SG_AI, SG_DEBUG,
201 "Submodels: " << (*submodel_iterator)->id
202 << " name " << (*submodel_iterator)->name
205 if ((*submodel_iterator)->trigger_node != 0) {
206 _trigger_node = (*submodel_iterator)->trigger_node;
207 trigger = _trigger_node->getBoolValue();
208 //cout << (*submodel_iterator)->name << "trigger node found " << trigger << endl;
211 //cout << (*submodel_iterator)->name << " trigger node not found " << trigger << endl;
214 if (trigger && (*submodel_iterator)->count != 0) {
216 //int id = (*submodel_iterator)->id;
217 //const string& name = (*submodel_iterator)->name;
219 SG_LOG(SG_AI, SG_DEBUG,
220 "Submodels release: " << (*submodel_iterator)->id
221 << " name " << (*submodel_iterator)->name
222 << " count " << (*submodel_iterator)->count
223 << " slaved " << (*submodel_iterator)->slaved
226 release(*submodel_iterator, dt);
228 (*submodel_iterator)->first_time = true;
234 bool FGSubmodelMgr::release(submodel *sm, double dt)
236 //cout << "release id " << sm->id
237 // << " name " << sm->name
238 // << " first time " << sm->first_time
239 // << " repeat " << sm->repeat
240 // << " slaved " << sm->slaved
243 // only run if first time or repeat is set to true
244 if (!sm->first_time && !sm->repeat) {
245 //cout<< "returning: "<< sm->name
246 // << " not first time " << sm->first_time
247 // << " repeat " << sm->repeat
248 // << " slaved " << sm->slaved
255 if (sm->timer < sm->delay) {
256 //cout << "not yet: timer " << sm->timer << " delay " << sm->delay << endl;
260 //cout << "released timer: " << sm->timer << " delay " << sm->delay << endl;
264 if (sm->first_time) {
266 sm->first_time = false;
269 double yaw_offset = 0.0;
270 double pitch_offset = 0.0;
272 if (sm->yaw_node != 0)
273 yaw_offset = sm->yaw_node->get_value();
274 if (sm->pitch_node != 0)
275 pitch_offset = sm->pitch_node->get_value();
277 transform(sm); // calculate submodel's initial conditions in world-coordinates
279 FGAIBallistic* ballist = new FGAIBallistic;
280 ballist->setPath(sm->model.c_str());
281 ballist->setName(sm->name);
282 ballist->setSlaved(sm->slaved);
283 ballist->setRandom(sm->random);
284 ballist->setRandomness(sm->randomness);
285 ballist->setLatitude(offsetpos.getLatitudeDeg());
286 ballist->setLongitude(offsetpos.getLongitudeDeg());
287 ballist->setAltitude(offsetpos.getElevationFt());
288 ballist->setAzimuth(IC.azimuth);
289 ballist->setElevation(IC.elevation);
290 ballist->setRoll(IC.roll);
291 ballist->setSpeed(IC.speed / SG_KT_TO_FPS);
292 ballist->setWind_from_east(IC.wind_from_east);
293 ballist->setWind_from_north(IC.wind_from_north);
294 ballist->setMass(IC.mass);
295 ballist->setDragArea(sm->drag_area);
296 ballist->setLife(sm->life);
297 ballist->setBuoyancy(sm->buoyancy);
298 ballist->setWind(sm->wind);
299 ballist->setCd(sm->cd);
300 ballist->setStabilisation(sm->aero_stabilised);
301 ballist->setNoRoll(sm->no_roll);
302 ballist->setCollision(sm->collision);
303 ballist->setExpiry(sm->expiry);
304 ballist->setImpact(sm->impact);
305 ballist->setImpactReportNode(sm->impact_report);
306 ballist->setFuseRange(sm->fuse_range);
307 ballist->setSubmodel(sm->submodel.c_str());
308 ballist->setSubID(sm->sub_id);
309 ballist->setForceStabilisation(sm->force_stabilised);
310 ballist->setExternalForce(sm->ext_force);
311 ballist->setForcePath(sm->force_path.c_str());
312 ballist->setXoffset(sm->x_offset);
313 ballist->setYoffset(sm->y_offset);
314 ballist->setZoffset(sm->z_offset);
315 ballist->setPitchoffset(pitch_offset);
316 ballist->setYawoffset(yaw_offset);
317 ballist->setParentNodes(_selected_ac);
318 ballist->setContentsNode(sm->contents_node);
319 ballist->setWeight(sm->weight);
321 aiManager()->attach(ballist);
328 void FGSubmodelMgr::load()
330 SGPropertyNode *path = fgGetNode("/sim/submodels/path");
334 const string& Path = path->getStringValue();
335 bool Seviceable =_serviceable_node->getBoolValue();
336 setData(id, Path, Seviceable);
340 void FGSubmodelMgr::transform(submodel *sm)
342 // set initial conditions
343 if (sm->contents_node != 0 && !sm->slaved) {
344 // get the weight of the contents (lbs) and convert to mass (slugs)
345 sm->contents = sm->contents_node->getChild("level-lbs",0,1)->getDoubleValue();
346 //cout << "transform: contents " << sm->contents << endl;
347 IC.mass = (sm->weight + sm->contents) * lbs_to_slugs;
348 //cout << "mass inc contents" << IC.mass << endl;
350 // set contents to 0 in the parent
351 sm->contents_node->getChild("level-gal_us",0,1)->setDoubleValue(0);
352 /*cout << "contents " << sm->contents_node->getChild("level-gal_us")->getDoubleValue()
353 << " " << sm->contents_node->getChild("level-lbs",0,1)->getDoubleValue()
356 IC.mass = sm->weight * lbs_to_slugs;
359 //int sub_id = sm->sub_id;
360 //const string& name = sm->name;
363 if (sm->speed_node != 0)
364 sm->speed = sm->speed_node->getDoubleValue();
366 double yaw_offset = 0.0;
367 double pitch_offset = 0.0;
369 if (sm->yaw_node != 0)
370 yaw_offset = sm->yaw_node->get_value();
371 if (sm->pitch_node != 0)
372 pitch_offset = sm->pitch_node->get_value();
374 //cout << " name " << name << " id " << id << " sub id" << sub_id << endl;
376 // set the Initial Conditions for the types of submodel parent
378 if (_impact || _hit || _expiry) {
379 // set the data for a submodel tied to a submodel
383 IC.lat = _parent_lat;
384 IC.lon = _parent_lon;
385 IC.alt = _parent_elev;
386 IC.roll = _parent_roll; // rotation about x axis
387 IC.elevation = _parent_pitch; // rotation about y axis
388 IC.azimuth = _parent_hdg; // rotation about z axis
389 IC.speed = _parent_speed;
390 IC.speed_down_fps = 0;
391 IC.speed_east_fps = 0;
392 IC.speed_north_fps = 0;
394 } else if (id == 0) {
395 //set the data for a submodel tied to the main model
397 IC.lat = _user_lat_node->getDoubleValue();
398 IC.lon = _user_lon_node->getDoubleValue();
399 IC.alt = _user_alt_node->getDoubleValue();
400 IC.roll = _user_roll_node->getDoubleValue(); // rotation about x axis
401 IC.elevation = _user_pitch_node->getDoubleValue(); // rotation about y axis
402 IC.azimuth = _user_heading_node->getDoubleValue(); // rotation about z axis
403 IC.speed = _user_speed_node->getDoubleValue();
404 IC.speed_down_fps = _user_speed_down_fps_node->getDoubleValue();
405 IC.speed_east_fps = _user_speed_east_fps_node->getDoubleValue();
406 IC.speed_north_fps = _user_speed_north_fps_node->getDoubleValue();
409 // set the data for a submodel tied to an AI Object
410 //cout << " set the data for a submodel tied to an AI Object " << id << endl;
414 //cout << "Submodel: setting IC "<< name << endl;
415 //cout << "heading " << IC.azimuth << endl ;
416 //cout << "speed down " << IC.speed_down_fps << endl ;
417 //cout << "speed east " << IC.speed_east_fps << endl ;
418 //cout << "speed north " << IC.speed_north_fps << endl ;
419 //cout << "parent speed fps in " << IC.speed << "sm speed in " << sm->speed << endl ;
420 //cout << "lat " << IC.lat;
421 //cout << "alt " << IC.alt << endl ;
424 // Set the Initial Conditions that are common to all types of parent
425 IC.wind_from_east = _user_wind_from_east_node->getDoubleValue();
426 IC.wind_from_north = _user_wind_from_north_node->getDoubleValue();
428 //cout << "wind e " << IC.wind_from_east << " n " << IC.wind_from_north << endl;
430 userpos.setLatitudeDeg(IC.lat);
431 userpos.setLongitudeDeg(IC.lon);
432 userpos.setElevationFt(IC.alt);
434 _x_offset = sm->x_offset;
435 _y_offset = sm->y_offset;
436 _z_offset = sm->z_offset;
440 //IC.elevation += sm->pitch_offset;
441 //IC.azimuth += sm->yaw_offset ;
443 // pre-process the trig functions
444 cosRx = cos(-IC.roll * SG_DEGREES_TO_RADIANS);
445 sinRx = sin(-IC.roll * SG_DEGREES_TO_RADIANS);
446 cosRy = cos(-IC.elevation * SG_DEGREES_TO_RADIANS);
447 sinRy = sin(-IC.elevation * SG_DEGREES_TO_RADIANS);
448 cosRz = cos(IC.azimuth * SG_DEGREES_TO_RADIANS);
449 sinRz = sin(IC.azimuth * SG_DEGREES_TO_RADIANS);
452 // Get submodel initial velocity vector angles in XZ and XY planes.
453 // This vector should be added to aircraft's vector.
454 IC.elevation += (yaw_offset * sinRx) + (pitch_offset * cosRx);
455 IC.azimuth += (yaw_offset * cosRx) - (pitch_offset * sinRx);
457 // calculate the total speed north
458 IC.total_speed_north = sm->speed * cos(IC.elevation * SG_DEGREES_TO_RADIANS)
459 * cos(IC.azimuth * SG_DEGREES_TO_RADIANS) + IC.speed_north_fps;
461 // calculate the total speed east
462 IC.total_speed_east = sm->speed * cos(IC.elevation * SG_DEGREES_TO_RADIANS)
463 * sin(IC.azimuth * SG_DEGREES_TO_RADIANS) + IC.speed_east_fps;
465 // calculate the total speed down
466 IC.total_speed_down = sm->speed * -sin(IC.elevation * SG_DEGREES_TO_RADIANS)
469 // re-calculate speed, elevation and azimuth
470 IC.speed = sqrt(IC.total_speed_north * IC.total_speed_north
471 + IC.total_speed_east * IC.total_speed_east
472 + IC.total_speed_down * IC.total_speed_down);
474 // if speeds are low this calculation can become unreliable
476 IC.azimuth = atan2(IC.total_speed_east, IC.total_speed_north) * SG_RADIANS_TO_DEGREES;
477 // cout << "azimuth1 " << IC.azimuth<<endl;
479 // rationalise the output
482 else if (IC.azimuth >= 360)
484 // cout << "azimuth2 " << IC.azimuth<<endl;
486 IC.elevation = -atan(IC.total_speed_down / sqrt(IC.total_speed_north
487 * IC.total_speed_north + IC.total_speed_east * IC.total_speed_east))
488 * SG_RADIANS_TO_DEGREES;
490 //cout << "IC.speed " << IC.speed / SG_KT_TO_FPS << endl;
493 void FGSubmodelMgr::updatelat(double lat)
495 ft_per_deg_latitude = 366468.96 - 3717.12 * cos(lat / SG_RADIANS_TO_DEGREES);
496 ft_per_deg_longitude = 365228.16 * cos(lat / SG_RADIANS_TO_DEGREES);
499 void FGSubmodelMgr::loadAI()
501 SG_LOG(SG_AI, SG_DEBUG, "Submodels: Loading AI submodels ");
503 FGAIManager::ai_list_type sm_list(aiManager()->get_ai_list());
505 if (sm_list.empty()) {
506 SG_LOG(SG_AI, SG_ALERT, "Submodels: Unable to read AI submodel list");
510 FGAIManager::ai_list_iterator sm_list_itr = sm_list.begin(),
513 while (sm_list_itr != end) {
514 string path = (*sm_list_itr)->_getSMPath();
521 int id = (*sm_list_itr)->getID();
522 bool serviceable = (*sm_list_itr)->_getServiceable();
524 //string type = (*sm_list_itr)->getTypeString();
525 //cout << "loadAI: type " << type << " path "<< path << " serviceable " << serviceable << endl;
527 setData(id, path, serviceable);
534 void FGSubmodelMgr::setData(int id, const string& path, bool serviceable)
538 SGPath config = globals->resolve_aircraft_path(path);
540 SG_LOG(SG_AI, SG_DEBUG,
541 "Submodels: Trying to read AI submodels file: " << config.str());
542 readProperties(config.str(), &root);
543 } catch (const sg_exception &) {
544 SG_LOG(SG_AI, SG_ALERT,
545 "Submodels: Unable to read AI submodels file: " << config.str());
549 vector<SGPropertyNode_ptr> children = root.getChildren("submodel");
550 vector<SGPropertyNode_ptr>::iterator it = children.begin();
551 vector<SGPropertyNode_ptr>::iterator end = children.end();
553 for (int i = 0; it != end; ++it, i++) {
554 //cout << "Reading AI submodel " << (*it)->getPath() << endl;
555 submodel* sm = new submodel;
556 SGPropertyNode * entry_node = *it;
557 sm->name = entry_node->getStringValue("name", "none_defined");
558 sm->model = entry_node->getStringValue("model", "Models/Geometry/rocket.ac");
559 sm->speed = entry_node->getDoubleValue("speed", 2329.4);
560 sm->repeat = entry_node->getBoolValue("repeat", false);
561 sm->delay = entry_node->getDoubleValue("delay", 0.25);
562 sm->count = entry_node->getIntValue("count", 1);
563 sm->slaved = entry_node->getBoolValue("slaved", false);
564 sm->x_offset = entry_node->getDoubleValue("x-offset", 0.0);
565 sm->y_offset = entry_node->getDoubleValue("y-offset", 0.0);
566 sm->z_offset = entry_node->getDoubleValue("z-offset", 0.0);
567 sm->drag_area = entry_node->getDoubleValue("eda", 0.034);
568 sm->life = entry_node->getDoubleValue("life", 900.0);
569 sm->buoyancy = entry_node->getDoubleValue("buoyancy", 0);
570 sm->wind = entry_node->getBoolValue("wind", false);
571 sm->cd = entry_node->getDoubleValue("cd", 0.193);
572 sm->weight = entry_node->getDoubleValue("weight", 0.25);
573 sm->aero_stabilised = entry_node->getBoolValue("aero-stabilised", true);
574 sm->no_roll = entry_node->getBoolValue("no-roll", false);
575 sm->collision = entry_node->getBoolValue("collision", false);
576 sm->expiry = entry_node->getBoolValue("expiry", false);
577 sm->impact = entry_node->getBoolValue("impact", false);
578 sm->impact_report = entry_node->getStringValue("impact-reports");
579 sm->fuse_range = entry_node->getDoubleValue("fuse-range", 0.0);
580 sm->contents_node = fgGetNode(entry_node->getStringValue("contents", "none"), false);
581 sm->speed_node = fgGetNode(entry_node->getStringValue("speed-prop", "none"), false);
582 sm->submodel = entry_node->getStringValue("submodel-path", "");
583 sm->force_stabilised= entry_node->getBoolValue("force-stabilised", false);
584 sm->ext_force = entry_node->getBoolValue("external-force", false);
585 sm->force_path = entry_node->getStringValue("force-path", "");
586 sm->random = entry_node->getBoolValue("random", false);
587 sm->randomness = entry_node->getDoubleValue("randomness", 0.5);
589 SGPropertyNode_ptr a = entry_node->getNode("yaw-offset");
590 SGPropertyNode_ptr b = entry_node->getNode("pitch-offset");
591 SGPropertyNode_ptr prop_root = fgGetNode("/", true);
595 sm->yaw_node = new InputValue(*prop_root, *a);
597 sm->pitch_node = new InputValue(*prop_root, *b);
599 if (sm->contents_node != 0)
600 sm->contents = sm->contents_node->getDoubleValue();
602 const char *trigger_path = entry_node->getStringValue("trigger", 0);
604 sm->trigger_node = fgGetNode(trigger_path, true);
605 sm->trigger_node->setBoolValue(sm->trigger_node->getBoolValue());
607 sm->trigger_node = 0;
610 if (sm->speed_node != 0)
611 sm->speed = sm->speed_node->getDoubleValue();
613 sm->timer = sm->delay;
615 sm->first_time = false;
616 sm->serviceable = serviceable;
619 sm->prop = fgGetNode("/ai/submodels/submodel", index, true);
620 sm->prop->tie("delay", SGRawValuePointer<double>(&(sm->delay)));
621 sm->prop->tie("count", SGRawValuePointer<int>(&(sm->count)));
622 sm->prop->tie("repeat", SGRawValuePointer<bool>(&(sm->repeat)));
623 sm->prop->tie("id", SGRawValuePointer<int>(&(sm->id)));
624 sm->prop->tie("sub-id", SGRawValuePointer<int>(&(sm->sub_id)));
625 sm->prop->tie("serviceable", SGRawValuePointer<bool>(&(sm->serviceable)));
626 sm->prop->tie("random", SGRawValuePointer<bool>(&(sm->random)));
627 sm->prop->tie("slaved", SGRawValuePointer<bool>(&(sm->slaved)));
628 const string& name = sm->name;
629 sm->prop->setStringValue("name", name.c_str());
631 const string& submodel = sm->submodel;
632 sm->prop->setStringValue("submodel", submodel.c_str());
634 const string& force_path = sm->force_path;
635 sm->prop->setStringValue("force_path", force_path.c_str());
636 //cout << "set force_path Sub " << force_path << endl;
638 if (sm->contents_node != 0)
639 sm->prop->tie("contents-lbs", SGRawValuePointer<double>(&(sm->contents)));
642 submodels.push_back(sm);
646 void FGSubmodelMgr::setSubData(int id, const string& path, bool serviceable)
649 SGPath config = globals->resolve_aircraft_path(path);
652 SG_LOG(SG_AI, SG_DEBUG,
653 "Submodels: Trying to read AI submodels file: " << config.str());
654 readProperties(config.str(), &root);
656 } catch (const sg_exception &) {
657 SG_LOG(SG_AI, SG_ALERT,
658 "Submodels: Unable to read AI submodels file: " << config.str());
662 vector<SGPropertyNode_ptr> children = root.getChildren("submodel");
663 vector<SGPropertyNode_ptr>::iterator it = children.begin();
664 vector<SGPropertyNode_ptr>::iterator end = children.end();
666 for (int i = 0; it != end; ++it, i++) {
667 //cout << "Reading AI submodel " << (*it)->getPath() << endl;
668 submodel* sm = new submodel;
669 SGPropertyNode * entry_node = *it;
670 sm->name = entry_node->getStringValue("name", "none_defined");
671 sm->model = entry_node->getStringValue("model", "Models/Geometry/rocket.ac");
672 sm->speed = entry_node->getDoubleValue("speed", 2329.4);
673 sm->repeat = entry_node->getBoolValue("repeat", false);
674 sm->delay = entry_node->getDoubleValue("delay", 0.25);
675 sm->count = entry_node->getIntValue("count", 1);
676 sm->slaved = entry_node->getBoolValue("slaved", false);
677 sm->x_offset = entry_node->getDoubleValue("x-offset", 0.0);
678 sm->y_offset = entry_node->getDoubleValue("y-offset", 0.0);
679 sm->z_offset = entry_node->getDoubleValue("z-offset", 0.0);
680 sm->drag_area = entry_node->getDoubleValue("eda", 0.034);
681 sm->life = entry_node->getDoubleValue("life", 900.0);
682 sm->buoyancy = entry_node->getDoubleValue("buoyancy", 0);
683 sm->wind = entry_node->getBoolValue("wind", false);
684 sm->cd = entry_node->getDoubleValue("cd", 0.193);
685 sm->weight = entry_node->getDoubleValue("weight", 0.25);
686 sm->aero_stabilised = entry_node->getBoolValue("aero-stabilised", true);
687 sm->no_roll = entry_node->getBoolValue("no-roll", false);
688 sm->collision = entry_node->getBoolValue("collision", false);
689 sm->expiry = entry_node->getBoolValue("expiry", false);
690 sm->impact = entry_node->getBoolValue("impact", false);
691 sm->impact_report = entry_node->getStringValue("impact-reports");
692 sm->fuse_range = entry_node->getDoubleValue("fuse-range", 0.0);
693 sm->contents_node = fgGetNode(entry_node->getStringValue("contents", "none"), false);
694 sm->speed_node = fgGetNode(entry_node->getStringValue("speed-prop", "none"), false);
695 sm->submodel = entry_node->getStringValue("submodel-path", "");
696 sm->force_stabilised= entry_node->getBoolValue("force-stabilised", false);
697 sm->ext_force = entry_node->getBoolValue("external-force", false);
698 sm->force_path = entry_node->getStringValue("force-path", "");
699 sm->random = entry_node->getBoolValue("random", false);
700 sm->randomness = entry_node->getDoubleValue("randomness", 0.5);
702 SGPropertyNode_ptr a = entry_node->getNode("yaw-offset");
703 SGPropertyNode_ptr b = entry_node->getNode("pitch-offset");
704 SGPropertyNode_ptr prop_root = fgGetNode("/", true);
708 sm->yaw_node = new InputValue(*prop_root, *a);
710 sm->pitch_node = new InputValue(*prop_root, *b);
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 sm->prop->tie("random", SGRawValuePointer<bool>(&(sm->random)));
739 sm->prop->tie("slaved", SGRawValuePointer<bool>(&(sm->slaved)));
741 const string& name = sm->name;
742 sm->prop->setStringValue("name", name.c_str());
744 const string& submodel = sm->submodel;
745 sm->prop->setStringValue("submodel-path", submodel.c_str());
746 // cout << " set submodel path AI" << submodel<< endl;
748 const string& force_path = sm->force_path;
749 sm->prop->setStringValue("force_path", force_path.c_str());
750 //cout << "set force_path AI" << force_path << endl;
752 if (sm->contents_node != 0)
753 sm->prop->tie("contents-lbs", SGRawValuePointer<double>(&(sm->contents)));
756 subsubmodels.push_back(sm);
760 void FGSubmodelMgr::loadSubmodels()
762 SG_LOG(SG_AI, SG_DEBUG, "Submodels: Loading sub submodels");
766 submodel_iterator = submodels.begin();
768 while (submodel_iterator != submodels.end()) {
769 const string& submodel = (*submodel_iterator)->submodel;
770 if (!submodel.empty()) {
771 //int id = (*submodel_iterator)->id;
772 bool serviceable = true;
773 SG_LOG(SG_AI, SG_DEBUG, "found path sub sub "
775 << " index " << index
776 << " name " << (*submodel_iterator)->name);
778 if ((*submodel_iterator)->sub_id == 0){
779 (*submodel_iterator)->sub_id = index;
781 setSubData(index, submodel, serviceable);
788 subsubmodel_iterator = subsubmodels.begin();
790 while (subsubmodel_iterator != subsubmodels.end()) {
792 submodels.push_back(*subsubmodel_iterator);
793 ++subsubmodel_iterator;
796 subsubmodels.clear();
798 //submodel_iterator = submodels.begin();
802 //while (submodel_iterator != submodels.end()) {
803 // int id = (*submodel_iterator)->id;
806 // SG_LOG(SG_AI, SG_ALERT,"after pushback "
807 // << " parent id " << id
808 // << " name " << (*submodel_iterator)->name
809 // << " sub id " << (*submodel_iterator)->sub_id
810 // << " subcount "<< subcount);
812 // ++submodel_iterator;
816 SGVec3d FGSubmodelMgr::getCartOffsetPos() const{
818 // convert geodetic positions to geocentered
819 SGVec3d cartuserPos = SGVec3d::fromGeod(userpos);
820 // Transform to the right coordinate frame, configuration is done in
821 // the x-forward, y-right, z-up coordinates (feet), computation
822 // in the simulation usual body x-forward, y-right, z-down coordinates
825 SGVec3d _off(_x_offset * SG_FEET_TO_METER,
826 _y_offset * SG_FEET_TO_METER,
827 -_z_offset * SG_FEET_TO_METER);
829 // Transform the user position to the horizontal local coordinate system.
830 SGQuatd hlTrans = SGQuatd::fromLonLat(userpos);
832 // and postrotate the orientation of the user model wrt the horizontal
834 hlTrans *= SGQuatd::fromYawPitchRollDeg(
839 // The offset converted to the usual body fixed coordinate system
840 // rotated to the earth-fixed coordinates axis
841 SGVec3d off = hlTrans.backTransform(_off);
843 // Add the position offset of the user model to get the geocentered position
844 SGVec3d offsetPos = cartuserPos + off;
848 void FGSubmodelMgr::setOffsetPos(){
849 // convert the offset geocentered position to geodetic
850 SGVec3d cartoffsetPos = getCartOffsetPos();
852 SGGeodesy::SGCartToGeod(cartoffsetPos, offsetpos);
854 //cout << "OFFSET POS" << offsetpos.getElevationFt();
858 void FGSubmodelMgr::valueChanged(SGPropertyNode *prop)
860 return; // this isn't working atm
862 const char* _model_added = _model_added_node->getStringValue();
864 std::basic_string <char>::size_type indexCh2b;
866 string str2 = _model_added;
867 const char *cstr2b = "multiplayer";
868 indexCh2b = str2.find( cstr2b, 0 );
870 if (indexCh2b != string::npos ){ // we will ignore Ballistic Objects - there are potentially too many
872 //cout << "Submodels: model added - " << str2 <<" read path "<< endl;
874 SGPropertyNode *a_node = fgGetNode(_model_added, true);
875 SGPropertyNode *sub_node = a_node->getChild("sim", 0, true);
876 SGPropertyNode_ptr path_node = sub_node->getChild("path", 0, true);
877 SGPropertyNode_ptr callsign_node = a_node->getChild("callsign", 0, true);
879 //const string& callsign = callsign_node->getStringValue();
880 //cout << "Submodels: model added - " << callsign <<" read callsign "<< endl;
884 cout << "model added - " << str2 <<" returning "<< endl;
890 void FGSubmodelMgr::setParentNode(int id) {
892 const SGPropertyNode_ptr ai = fgGetNode("/ai/models", true);
894 for (int i = ai->nChildren() - 1; i >= -1; i--) {
895 SGPropertyNode_ptr model;
897 if (i < 0) { // last iteration: selected model
898 model = _selected_ac;
900 model = ai->getChild(i);
901 //const string& path = ai->getPath();
902 //const string& name = model->getStringValue("name");
903 int parent_id = model->getIntValue("id");
904 if (!model->nChildren()){
907 if (parent_id == id) {
908 _selected_ac = model; // save selected model for last iteration
918 if (_selected_ac != 0){
920 //cout << " parent node found"<< endl;
922 const string name = _selected_ac->getStringValue("name");
923 IC.lat = _selected_ac->getDoubleValue("position/latitude-deg");
924 IC.lon = _selected_ac->getDoubleValue("position/longitude-deg");
925 IC.alt = _selected_ac->getDoubleValue("position/altitude-ft");
926 IC.roll = _selected_ac->getDoubleValue("orientation/roll-deg");
927 IC.elevation = _selected_ac->getDoubleValue("orientation/pitch-deg");
928 IC.azimuth = _selected_ac->getDoubleValue("orientation/true-heading-deg");
929 IC.speed = _selected_ac->getDoubleValue("velocities/true-airspeed-kt") * SG_KT_TO_FPS;
930 IC.speed_down_fps = -_selected_ac->getDoubleValue("velocities/vertical-speed-fps");
931 IC.speed_east_fps = _selected_ac->getDoubleValue("velocities/speed-east-fps");
932 IC.speed_north_fps = _selected_ac->getDoubleValue("velocities/speed-north-fps");
934 //cout << name << " IC.speed " << IC.speed << endl;
937 SG_LOG(SG_AI, SG_ALERT, "AISubmodel: parent node not found ");
941 // end of submodel.cxx