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 const double FGSubmodelMgr::lbs_to_slugs = 0.031080950172;
33 FGSubmodelMgr::FGSubmodelMgr()
35 x_offset = y_offset = z_offset = 0.0;
39 //out[0] = out[1] = out[2] = 0;
40 //string contents_node;
41 contrail_altitude = 30000;
46 FGSubmodelMgr::~FGSubmodelMgr()
50 FGAIManager* FGSubmodelMgr::aiManager()
52 return (FGAIManager*)globals->get_subsystem("ai-model");
55 void FGSubmodelMgr::init()
59 _serviceable_node = fgGetNode("/sim/submodels/serviceable", true);
60 _serviceable_node->setBoolValue(true);
62 _user_lat_node = fgGetNode("/position/latitude-deg", true);
63 _user_lon_node = fgGetNode("/position/longitude-deg", true);
64 _user_alt_node = fgGetNode("/position/altitude-ft", true);
66 _user_heading_node = fgGetNode("/orientation/heading-deg", true);
67 _user_pitch_node = fgGetNode("/orientation/pitch-deg", true);
68 _user_roll_node = fgGetNode("/orientation/roll-deg", true);
69 _user_yaw_node = fgGetNode("/orientation/yaw-deg", true);
70 _user_alpha_node = fgGetNode("/orientation/alpha-deg", true);
72 _user_speed_node = fgGetNode("/velocities/uBody-fps", true);
74 _user_wind_from_east_node = fgGetNode("/environment/wind-from-east-fps", true);
75 _user_wind_from_north_node = fgGetNode("/environment/wind-from-north-fps", true);
77 _user_speed_down_fps_node = fgGetNode("/velocities/speed-down-fps", true);
78 _user_speed_east_fps_node = fgGetNode("/velocities/speed-east-fps", true);
79 _user_speed_north_fps_node = fgGetNode("/velocities/speed-north-fps", true);
81 _contrail_altitude_node = fgGetNode("/environment/params/contrail-altitude", true);
82 contrail_altitude = _contrail_altitude_node->getDoubleValue();
83 _contrail_trigger = fgGetNode("ai/submodels/contrails", true);
84 _contrail_trigger->setBoolValue(false);
90 void FGSubmodelMgr::postinit() {
91 // postinit, so that the AI list is populated
98 //TODO reload submodels if an MP ac joins
100 //_model_added_node = fgGetNode("ai/models/model-added", true);
101 //_model_added_node->addChangeListener(this, false);
104 void FGSubmodelMgr::bind()
107 void FGSubmodelMgr::unbind()
109 submodel_iterator = submodels.begin();
110 while (submodel_iterator != submodels.end()) {
111 (*submodel_iterator)->prop->untie("count");
116 void FGSubmodelMgr::update(double dt)
118 if (!_serviceable_node->getBoolValue())
125 // check if the submodel hit an object or terrain
126 FGAIManager::ai_list_type sm_list(aiManager()->get_ai_list());
127 FGAIManager::ai_list_iterator sm_list_itr = sm_list.begin(),
130 for (; sm_list_itr != end; ++sm_list_itr) {
131 FGAIBase::object_type object_type =(*sm_list_itr)->getType();
133 if (object_type != FGAIBase::otBallistic){// only work on ballistic objects
134 continue; // so continue
137 int parent_subID = (*sm_list_itr)->_getSubID();
138 int id = (*sm_list_itr)->getID();
140 if ( parent_subID == 0 || id == -1) // this entry in the list has no associated submodel
141 continue; // or is invalid so we can continue
143 //SG_LOG(SG_AI, SG_DEBUG, "Submodel: Impact " << _impact << " hit! "
144 // << _hit <<" parent_subID " << parent_subID);
146 _hit = (*sm_list_itr)->_getCollisionData();
147 _impact = (*sm_list_itr)->_getImpactData();
148 _expiry = (*sm_list_itr)->_getExpiryData();
150 //SG_LOG(SG_AI, SG_ALERT, "Submodel: " << (*sm_list_itr)->_getName()
151 // << " Impact " << _impact << " hit! " << _hit
152 // << " exipiry :-( " << _expiry );
154 if (_impact || _hit || _expiry) {
155 // SG_LOG(SG_AI, SG_ALERT, "Submodel: Impact " << _impact << " hit! " << _hit
156 //<< " exipiry :-( " << _expiry );
158 submodel_iterator = submodels.begin();
160 while (submodel_iterator != submodels.end()) {
161 int child_ID = (*submodel_iterator)->id;
162 //cout << "Impact: parent SubID " << parent_subID << " child_ID " << child_ID << endl;
164 if ( parent_subID == child_ID ) {
165 _parent_lat = (*sm_list_itr)->_getImpactLat();
166 _parent_lon = (*sm_list_itr)->_getImpactLon();
167 _parent_elev = (*sm_list_itr)->_getImpactElevFt();
168 _parent_hdg = (*sm_list_itr)->_getImpactHdg();
169 _parent_pitch = (*sm_list_itr)->_getImpactPitch();
170 _parent_roll = (*sm_list_itr)->_getImpactRoll();
171 _parent_speed = (*sm_list_itr)->_getImpactSpeed();
172 (*submodel_iterator)->first_time = true;
173 //cout << "Impact: parent SubID = child_ID elev " << _parent_elev << endl;
175 if (release(*submodel_iterator, dt)){
176 (*sm_list_itr)->setDie(true);
177 //cout << "Impact: set die" << (*sm_list_itr)->_getName() << endl;
187 _contrail_trigger->setBoolValue(_user_alt_node->getDoubleValue() > contrail_altitude);
190 // bool in_range = true;
191 bool trigger = false;
194 submodel_iterator = submodels.begin();
195 while (submodel_iterator != submodels.end()) {
198 /*SG_LOG(SG_AI, SG_DEBUG,
199 "Submodels: " << (*submodel_iterator)->id
200 << " name " << (*submodel_iterator)->name
203 if ((*submodel_iterator)->trigger_node != 0) {
204 _trigger_node = (*submodel_iterator)->trigger_node;
205 trigger = _trigger_node->getBoolValue();
206 //cout << (*submodel_iterator)->name << "trigger node found " << trigger << endl;
209 //cout << (*submodel_iterator)->name << " trigger node not found " << trigger << endl;
212 if (trigger && (*submodel_iterator)->count != 0) {
214 //int id = (*submodel_iterator)->id;
215 //const string& name = (*submodel_iterator)->name;
217 SG_LOG(SG_AI, SG_DEBUG,
218 "Submodels release: " << (*submodel_iterator)->id
219 << " name " << (*submodel_iterator)->name
220 << " count " << (*submodel_iterator)->count
221 << " slaved " << (*submodel_iterator)->slaved
224 release(*submodel_iterator, dt);
226 (*submodel_iterator)->first_time = true;
232 bool FGSubmodelMgr::release(submodel *sm, double dt)
234 //cout << "release id " << sm->id
235 // << " name " << sm->name
236 // << " first time " << sm->first_time
237 // << " repeat " << sm->repeat
238 // << " slaved " << sm->slaved
241 // only run if first time or repeat is set to true
242 if (!sm->first_time && !sm->repeat) {
243 //cout<< "returning: "<< sm->name
244 // << " not first time " << sm->first_time
245 // << " repeat " << sm->repeat
246 // << " slaved " << sm->slaved
253 if (sm->timer < sm->delay) {
254 //cout << "not yet: timer " << sm->timer << " delay " << sm->delay << endl;
258 //cout << "released timer: " << sm->timer << " delay " << sm->delay << endl;
262 if (sm->first_time) {
264 sm->first_time = false;
267 double yaw_offset = 0.0;
268 double pitch_offset = 0.0;
270 yaw_offset = sm->yaw_offset->get_value();
271 pitch_offset = sm->pitch_offset->get_value();
273 transform(sm); // calculate submodel's initial conditions in world-coordinates
275 FGAIBallistic* ballist = new FGAIBallistic;
276 ballist->setPath(sm->model.c_str());
277 ballist->setName(sm->name);
278 ballist->setSlaved(sm->slaved);
279 ballist->setRandom(sm->random);
280 ballist->setRandomness(sm->randomness);
281 ballist->setLatitude(offsetpos.getLatitudeDeg());
282 ballist->setLongitude(offsetpos.getLongitudeDeg());
283 ballist->setAltitude(offsetpos.getElevationFt());
284 ballist->setAzimuth(IC.azimuth);
285 ballist->setElevation(IC.elevation);
286 ballist->setRoll(IC.roll);
287 ballist->setSpeed(IC.speed / SG_KT_TO_FPS);
288 ballist->setWind_from_east(IC.wind_from_east);
289 ballist->setWind_from_north(IC.wind_from_north);
290 ballist->setMass(IC.mass);
291 ballist->setDragArea(sm->drag_area);
292 ballist->setLife(sm->life);
293 ballist->setBuoyancy(sm->buoyancy);
294 ballist->setWind(sm->wind);
295 ballist->setCd(sm->cd);
296 ballist->setStabilisation(sm->aero_stabilised);
297 ballist->setNoRoll(sm->no_roll);
298 ballist->setCollision(sm->collision);
299 ballist->setExpiry(sm->expiry);
300 ballist->setImpact(sm->impact);
301 ballist->setImpactReportNode(sm->impact_report);
302 ballist->setFuseRange(sm->fuse_range);
303 ballist->setSubmodel(sm->submodel.c_str());
304 ballist->setSubID(sm->sub_id);
305 ballist->setForceStabilisation(sm->force_stabilised);
306 ballist->setExternalForce(sm->ext_force);
307 ballist->setForcePath(sm->force_path.c_str());
308 ballist->setXoffset(sm->x_offset);
309 ballist->setYoffset(sm->y_offset);
310 ballist->setZoffset(sm->z_offset);
311 ballist->setPitchoffset(pitch_offset);
312 ballist->setYawoffset(yaw_offset);
313 ballist->setParentNodes(_selected_ac);
314 ballist->setContentsNode(sm->contents_node);
315 ballist->setWeight(sm->weight);
317 aiManager()->attach(ballist);
324 void FGSubmodelMgr::load()
326 SGPropertyNode *path = fgGetNode("/sim/submodels/path");
330 const string& Path = path->getStringValue();
331 bool Seviceable =_serviceable_node->getBoolValue();
332 setData(id, Path, Seviceable);
336 void FGSubmodelMgr::transform(submodel *sm)
338 // set initial conditions
339 if (sm->contents_node != 0 && !sm->slaved) {
340 // get the weight of the contents (lbs) and convert to mass (slugs)
341 sm->contents = sm->contents_node->getChild("level-lbs",0,1)->getDoubleValue();
342 //cout << "transform: contents " << sm->contents << endl;
343 IC.mass = (sm->weight + sm->contents) * lbs_to_slugs;
344 //cout << "mass inc contents" << IC.mass << endl;
346 // set contents to 0 in the parent
347 sm->contents_node->getChild("level-gal_us",0,1)->setDoubleValue(0);
348 /*cout << "contents " << sm->contents_node->getChild("level-gal_us")->getDoubleValue()
349 << " " << sm->contents_node->getChild("level-lbs",0,1)->getDoubleValue()
352 IC.mass = sm->weight * lbs_to_slugs;
355 //int sub_id = sm->sub_id;
356 //const string& name = sm->name;
359 if (sm->speed_node != 0)
360 sm->speed = sm->speed_node->getDoubleValue();
362 double yaw_offset = 0.0;
363 double pitch_offset = 0.0;
365 yaw_offset = sm->yaw_offset->get_value();
366 pitch_offset = sm->pitch_offset->get_value();
368 //cout << " name " << name << " id " << id << " sub id" << sub_id << endl;
370 // set the Initial Conditions for the types of submodel parent
372 if (_impact || _hit || _expiry) {
373 // set the data for a submodel tied to a submodel
377 IC.lat = _parent_lat;
378 IC.lon = _parent_lon;
379 IC.alt = _parent_elev;
380 IC.roll = _parent_roll; // rotation about x axis
381 IC.elevation = _parent_pitch; // rotation about y axis
382 IC.azimuth = _parent_hdg; // rotation about z axis
383 IC.speed = _parent_speed;
384 IC.speed_down_fps = 0;
385 IC.speed_east_fps = 0;
386 IC.speed_north_fps = 0;
388 } else if (id == 0) {
389 //set the data for a submodel tied to the main model
391 IC.lat = _user_lat_node->getDoubleValue();
392 IC.lon = _user_lon_node->getDoubleValue();
393 IC.alt = _user_alt_node->getDoubleValue();
394 IC.roll = _user_roll_node->getDoubleValue(); // rotation about x axis
395 IC.elevation = _user_pitch_node->getDoubleValue(); // rotation about y axis
396 IC.azimuth = _user_heading_node->getDoubleValue(); // rotation about z axis
397 IC.speed = _user_speed_node->getDoubleValue();
398 IC.speed_down_fps = _user_speed_down_fps_node->getDoubleValue();
399 IC.speed_east_fps = _user_speed_east_fps_node->getDoubleValue();
400 IC.speed_north_fps = _user_speed_north_fps_node->getDoubleValue();
403 // set the data for a submodel tied to an AI Object
404 //cout << " set the data for a submodel tied to an AI Object " << id << endl;
408 //cout << "Submodel: setting IC "<< name << endl;
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 ;
414 //cout << "lat " << IC.lat;
415 //cout << "alt " << IC.alt << endl ;
418 // Set the Initial Conditions that are common to all types of parent
419 IC.wind_from_east = _user_wind_from_east_node->getDoubleValue();
420 IC.wind_from_north = _user_wind_from_north_node->getDoubleValue();
422 //cout << "wind e " << IC.wind_from_east << " n " << IC.wind_from_north << endl;
424 userpos.setLatitudeDeg(IC.lat);
425 userpos.setLongitudeDeg(IC.lon);
426 userpos.setElevationFt(IC.alt);
428 _x_offset = sm->x_offset;
429 _y_offset = sm->y_offset;
430 _z_offset = sm->z_offset;
434 //IC.elevation += sm->pitch_offset;
435 //IC.azimuth += sm->yaw_offset ;
437 // pre-process the trig functions
438 cosRx = cos(-IC.roll * SG_DEGREES_TO_RADIANS);
439 sinRx = sin(-IC.roll * SG_DEGREES_TO_RADIANS);
440 cosRy = cos(-IC.elevation * SG_DEGREES_TO_RADIANS);
441 sinRy = sin(-IC.elevation * SG_DEGREES_TO_RADIANS);
442 cosRz = cos(IC.azimuth * SG_DEGREES_TO_RADIANS);
443 sinRz = sin(IC.azimuth * SG_DEGREES_TO_RADIANS);
446 // Get submodel initial velocity vector angles in XZ and XY planes.
447 // This vector should be added to aircraft's vector.
448 IC.elevation += (yaw_offset * sinRx) + (pitch_offset * cosRx);
449 IC.azimuth += (yaw_offset * cosRx) - (pitch_offset * sinRx);
451 // calculate the total speed north
452 IC.total_speed_north = sm->speed * cos(IC.elevation * SG_DEGREES_TO_RADIANS)
453 * cos(IC.azimuth * SG_DEGREES_TO_RADIANS) + IC.speed_north_fps;
455 // calculate the total speed east
456 IC.total_speed_east = sm->speed * cos(IC.elevation * SG_DEGREES_TO_RADIANS)
457 * sin(IC.azimuth * SG_DEGREES_TO_RADIANS) + IC.speed_east_fps;
459 // calculate the total speed down
460 IC.total_speed_down = sm->speed * -sin(IC.elevation * SG_DEGREES_TO_RADIANS)
463 // re-calculate speed, elevation and azimuth
464 IC.speed = sqrt(IC.total_speed_north * IC.total_speed_north
465 + IC.total_speed_east * IC.total_speed_east
466 + IC.total_speed_down * IC.total_speed_down);
468 // if speeds are low this calculation can become unreliable
470 IC.azimuth = atan2(IC.total_speed_east, IC.total_speed_north) * SG_RADIANS_TO_DEGREES;
471 // cout << "azimuth1 " << IC.azimuth<<endl;
473 // rationalise the output
476 else if (IC.azimuth >= 360)
478 // cout << "azimuth2 " << IC.azimuth<<endl;
480 IC.elevation = -atan(IC.total_speed_down / sqrt(IC.total_speed_north
481 * IC.total_speed_north + IC.total_speed_east * IC.total_speed_east))
482 * SG_RADIANS_TO_DEGREES;
484 //cout << "IC.speed " << IC.speed / SG_KT_TO_FPS << endl;
487 void FGSubmodelMgr::updatelat(double lat)
489 ft_per_deg_latitude = 366468.96 - 3717.12 * cos(lat / SG_RADIANS_TO_DEGREES);
490 ft_per_deg_longitude = 365228.16 * cos(lat / SG_RADIANS_TO_DEGREES);
493 void FGSubmodelMgr::loadAI()
495 SG_LOG(SG_AI, SG_DEBUG, "Submodels: Loading AI submodels ");
497 FGAIManager::ai_list_type sm_list(aiManager()->get_ai_list());
499 if (sm_list.empty()) {
500 SG_LOG(SG_AI, SG_ALERT, "Submodels: Unable to read AI submodel list");
504 FGAIManager::ai_list_iterator sm_list_itr = sm_list.begin(),
507 while (sm_list_itr != end) {
508 string path = (*sm_list_itr)->_getSMPath();
515 int id = (*sm_list_itr)->getID();
516 bool serviceable = (*sm_list_itr)->_getServiceable();
518 //string type = (*sm_list_itr)->getTypeString();
519 //cout << "loadAI: type " << type << " path "<< path << " serviceable " << serviceable << endl;
521 setData(id, path, serviceable);
528 void FGSubmodelMgr::setData(int id, const string& path, bool serviceable)
532 SGPath config = globals->resolve_aircraft_path(path);
534 SG_LOG(SG_AI, SG_DEBUG,
535 "Submodels: Trying to read AI submodels file: " << config.str());
536 readProperties(config.str(), &root);
537 } catch (const sg_exception &) {
538 SG_LOG(SG_AI, SG_ALERT,
539 "Submodels: Unable to read AI submodels file: " << config.str());
543 vector<SGPropertyNode_ptr> children = root.getChildren("submodel");
544 vector<SGPropertyNode_ptr>::iterator it = children.begin();
545 vector<SGPropertyNode_ptr>::iterator end = children.end();
547 for (int i = 0; it != end; ++it, i++) {
548 //cout << "Reading AI submodel " << (*it)->getPath() << endl;
549 submodel* sm = new submodel;
550 SGPropertyNode * entry_node = *it;
551 sm->name = entry_node->getStringValue("name", "none_defined");
552 sm->model = entry_node->getStringValue("model", "Models/Geometry/rocket.ac");
553 sm->speed = entry_node->getDoubleValue("speed", 2329.4);
554 sm->repeat = entry_node->getBoolValue("repeat", false);
555 sm->delay = entry_node->getDoubleValue("delay", 0.25);
556 sm->count = entry_node->getIntValue("count", 1);
557 sm->slaved = entry_node->getBoolValue("slaved", false);
558 sm->x_offset = entry_node->getDoubleValue("x-offset", 0.0);
559 sm->y_offset = entry_node->getDoubleValue("y-offset", 0.0);
560 sm->z_offset = entry_node->getDoubleValue("z-offset", 0.0);
561 sm->drag_area = entry_node->getDoubleValue("eda", 0.034);
562 sm->life = entry_node->getDoubleValue("life", 900.0);
563 sm->buoyancy = entry_node->getDoubleValue("buoyancy", 0);
564 sm->wind = entry_node->getBoolValue("wind", false);
565 sm->cd = entry_node->getDoubleValue("cd", 0.193);
566 sm->weight = entry_node->getDoubleValue("weight", 0.25);
567 sm->aero_stabilised = entry_node->getBoolValue("aero-stabilised", true);
568 sm->no_roll = entry_node->getBoolValue("no-roll", false);
569 sm->collision = entry_node->getBoolValue("collision", false);
570 sm->expiry = entry_node->getBoolValue("expiry", false);
571 sm->impact = entry_node->getBoolValue("impact", false);
572 sm->impact_report = entry_node->getStringValue("impact-reports");
573 sm->fuse_range = entry_node->getDoubleValue("fuse-range", 0.0);
574 sm->contents_node = fgGetNode(entry_node->getStringValue("contents", "none"), false);
575 sm->speed_node = fgGetNode(entry_node->getStringValue("speed-prop", "none"), false);
576 sm->submodel = entry_node->getStringValue("submodel-path", "");
577 sm->force_stabilised= entry_node->getBoolValue("force-stabilised", false);
578 sm->ext_force = entry_node->getBoolValue("external-force", false);
579 sm->force_path = entry_node->getStringValue("force-path", "");
580 sm->random = entry_node->getBoolValue("random", false);
581 sm->randomness = entry_node->getDoubleValue("randomness", 0.5);
583 SGPropertyNode_ptr prop_root = fgGetNode("/", true);
586 SGPropertyNode_ptr a = entry_node->getNode("yaw-offset");
587 sm->yaw_offset = new FGXMLAutopilot::InputValue(*prop_root, a ? *a : n );
589 a = entry_node->getNode("pitch-offset");
590 sm->pitch_offset = new FGXMLAutopilot::InputValue(*prop_root, a ? *a : n );
592 if (sm->contents_node != 0)
593 sm->contents = sm->contents_node->getDoubleValue();
595 const char *trigger_path = entry_node->getStringValue("trigger", 0);
597 sm->trigger_node = fgGetNode(trigger_path, true);
598 sm->trigger_node->setBoolValue(sm->trigger_node->getBoolValue());
600 sm->trigger_node = 0;
603 if (sm->speed_node != 0)
604 sm->speed = sm->speed_node->getDoubleValue();
606 sm->timer = sm->delay;
608 sm->first_time = false;
609 sm->serviceable = serviceable;
612 sm->prop = fgGetNode("/ai/submodels/submodel", index, true);
613 sm->prop->tie("delay", SGRawValuePointer<double>(&(sm->delay)));
614 sm->prop->tie("count", SGRawValuePointer<int>(&(sm->count)));
615 sm->prop->tie("repeat", SGRawValuePointer<bool>(&(sm->repeat)));
616 sm->prop->tie("id", SGRawValuePointer<int>(&(sm->id)));
617 sm->prop->tie("sub-id", SGRawValuePointer<int>(&(sm->sub_id)));
618 sm->prop->tie("serviceable", SGRawValuePointer<bool>(&(sm->serviceable)));
619 sm->prop->tie("random", SGRawValuePointer<bool>(&(sm->random)));
620 sm->prop->tie("slaved", SGRawValuePointer<bool>(&(sm->slaved)));
621 const string& name = sm->name;
622 sm->prop->setStringValue("name", name.c_str());
624 const string& submodel = sm->submodel;
625 sm->prop->setStringValue("submodel", submodel.c_str());
627 const string& force_path = sm->force_path;
628 sm->prop->setStringValue("force_path", force_path.c_str());
629 //cout << "set force_path Sub " << force_path << endl;
631 if (sm->contents_node != 0)
632 sm->prop->tie("contents-lbs", SGRawValuePointer<double>(&(sm->contents)));
635 submodels.push_back(sm);
639 void FGSubmodelMgr::setSubData(int id, const string& path, bool serviceable)
642 SGPath config = globals->resolve_aircraft_path(path);
645 SG_LOG(SG_AI, SG_DEBUG,
646 "Submodels: Trying to read AI submodels file: " << config.str());
647 readProperties(config.str(), &root);
649 } catch (const sg_exception &) {
650 SG_LOG(SG_AI, SG_ALERT,
651 "Submodels: Unable to read AI submodels file: " << config.str());
655 vector<SGPropertyNode_ptr> children = root.getChildren("submodel");
656 vector<SGPropertyNode_ptr>::iterator it = children.begin();
657 vector<SGPropertyNode_ptr>::iterator end = children.end();
659 for (int i = 0; it != end; ++it, i++) {
660 //cout << "Reading AI submodel " << (*it)->getPath() << endl;
661 submodel* sm = new submodel;
662 SGPropertyNode * entry_node = *it;
663 sm->name = entry_node->getStringValue("name", "none_defined");
664 sm->model = entry_node->getStringValue("model", "Models/Geometry/rocket.ac");
665 sm->speed = entry_node->getDoubleValue("speed", 2329.4);
666 sm->repeat = entry_node->getBoolValue("repeat", false);
667 sm->delay = entry_node->getDoubleValue("delay", 0.25);
668 sm->count = entry_node->getIntValue("count", 1);
669 sm->slaved = entry_node->getBoolValue("slaved", false);
670 sm->x_offset = entry_node->getDoubleValue("x-offset", 0.0);
671 sm->y_offset = entry_node->getDoubleValue("y-offset", 0.0);
672 sm->z_offset = entry_node->getDoubleValue("z-offset", 0.0);
673 sm->drag_area = entry_node->getDoubleValue("eda", 0.034);
674 sm->life = entry_node->getDoubleValue("life", 900.0);
675 sm->buoyancy = entry_node->getDoubleValue("buoyancy", 0);
676 sm->wind = entry_node->getBoolValue("wind", false);
677 sm->cd = entry_node->getDoubleValue("cd", 0.193);
678 sm->weight = entry_node->getDoubleValue("weight", 0.25);
679 sm->aero_stabilised = entry_node->getBoolValue("aero-stabilised", true);
680 sm->no_roll = entry_node->getBoolValue("no-roll", false);
681 sm->collision = entry_node->getBoolValue("collision", false);
682 sm->expiry = entry_node->getBoolValue("expiry", false);
683 sm->impact = entry_node->getBoolValue("impact", false);
684 sm->impact_report = entry_node->getStringValue("impact-reports");
685 sm->fuse_range = entry_node->getDoubleValue("fuse-range", 0.0);
686 sm->contents_node = fgGetNode(entry_node->getStringValue("contents", "none"), false);
687 sm->speed_node = fgGetNode(entry_node->getStringValue("speed-prop", "none"), false);
688 sm->submodel = entry_node->getStringValue("submodel-path", "");
689 sm->force_stabilised= entry_node->getBoolValue("force-stabilised", false);
690 sm->ext_force = entry_node->getBoolValue("external-force", false);
691 sm->force_path = entry_node->getStringValue("force-path", "");
692 sm->random = entry_node->getBoolValue("random", false);
693 sm->randomness = entry_node->getDoubleValue("randomness", 0.5);
695 SGPropertyNode_ptr prop_root = fgGetNode("/", true);
698 SGPropertyNode_ptr a = entry_node->getNode("yaw-offset");
699 sm->yaw_offset = new FGXMLAutopilot::InputValue(*prop_root, a ? *a : n );
701 a = entry_node->getNode("pitch-offset");
702 sm->pitch_offset = new FGXMLAutopilot::InputValue(*prop_root, a ? *a : n );
704 if (sm->contents_node != 0)
705 sm->contents = sm->contents_node->getDoubleValue();
707 const char *trigger_path = entry_node->getStringValue("trigger", 0);
709 sm->trigger_node = fgGetNode(trigger_path, true);
710 sm->trigger_node->setBoolValue(sm->trigger_node->getBoolValue());
712 sm->trigger_node = 0;
715 if (sm->speed_node != 0)
716 sm->speed = sm->speed_node->getDoubleValue();
718 sm->timer = sm->delay;
720 sm->first_time = false;
721 sm->serviceable = serviceable;
724 sm->prop = fgGetNode("/ai/submodels/subsubmodel", index, true);
725 sm->prop->tie("count", SGRawValuePointer<int>(&(sm->count)));
726 sm->prop->tie("repeat", SGRawValuePointer<bool>(&(sm->repeat)));
727 sm->prop->tie("id", SGRawValuePointer<int>(&(sm->id)));
728 sm->prop->tie("sub-id", SGRawValuePointer<int>(&(sm->sub_id)));
729 sm->prop->tie("serviceable", SGRawValuePointer<bool>(&(sm->serviceable)));
730 sm->prop->tie("random", SGRawValuePointer<bool>(&(sm->random)));
731 sm->prop->tie("slaved", SGRawValuePointer<bool>(&(sm->slaved)));
733 const string& name = sm->name;
734 sm->prop->setStringValue("name", name.c_str());
736 const string& submodel = sm->submodel;
737 sm->prop->setStringValue("submodel-path", submodel.c_str());
738 // cout << " set submodel path AI" << submodel<< endl;
740 const string& force_path = sm->force_path;
741 sm->prop->setStringValue("force_path", force_path.c_str());
742 //cout << "set force_path AI" << force_path << endl;
744 if (sm->contents_node != 0)
745 sm->prop->tie("contents-lbs", SGRawValuePointer<double>(&(sm->contents)));
748 subsubmodels.push_back(sm);
752 void FGSubmodelMgr::loadSubmodels()
754 SG_LOG(SG_AI, SG_DEBUG, "Submodels: Loading sub submodels");
758 submodel_iterator = submodels.begin();
760 while (submodel_iterator != submodels.end()) {
761 const string& submodel = (*submodel_iterator)->submodel;
762 if (!submodel.empty()) {
763 //int id = (*submodel_iterator)->id;
764 bool serviceable = true;
765 SG_LOG(SG_AI, SG_DEBUG, "found path sub sub "
767 << " index " << index
768 << " name " << (*submodel_iterator)->name);
770 if ((*submodel_iterator)->sub_id == 0){
771 (*submodel_iterator)->sub_id = index;
773 setSubData(index, submodel, serviceable);
780 subsubmodel_iterator = subsubmodels.begin();
782 while (subsubmodel_iterator != subsubmodels.end()) {
784 submodels.push_back(*subsubmodel_iterator);
785 ++subsubmodel_iterator;
788 subsubmodels.clear();
790 //submodel_iterator = submodels.begin();
794 //while (submodel_iterator != submodels.end()) {
795 // int id = (*submodel_iterator)->id;
798 // SG_LOG(SG_AI, SG_ALERT,"after pushback "
799 // << " parent id " << id
800 // << " name " << (*submodel_iterator)->name
801 // << " sub id " << (*submodel_iterator)->sub_id
802 // << " subcount "<< subcount);
804 // ++submodel_iterator;
808 SGVec3d FGSubmodelMgr::getCartOffsetPos() const{
810 // convert geodetic positions to geocentered
811 SGVec3d cartuserPos = SGVec3d::fromGeod(userpos);
812 // Transform to the right coordinate frame, configuration is done in
813 // the x-forward, y-right, z-up coordinates (feet), computation
814 // in the simulation usual body x-forward, y-right, z-down coordinates
817 SGVec3d _off(_x_offset * SG_FEET_TO_METER,
818 _y_offset * SG_FEET_TO_METER,
819 -_z_offset * SG_FEET_TO_METER);
821 // Transform the user position to the horizontal local coordinate system.
822 SGQuatd hlTrans = SGQuatd::fromLonLat(userpos);
824 // and postrotate the orientation of the user model wrt the horizontal
826 hlTrans *= SGQuatd::fromYawPitchRollDeg(
831 // The offset converted to the usual body fixed coordinate system
832 // rotated to the earth-fixed coordinates axis
833 SGVec3d off = hlTrans.backTransform(_off);
835 // Add the position offset of the user model to get the geocentered position
836 SGVec3d offsetPos = cartuserPos + off;
840 void FGSubmodelMgr::setOffsetPos(){
841 // convert the offset geocentered position to geodetic
842 SGVec3d cartoffsetPos = getCartOffsetPos();
844 SGGeodesy::SGCartToGeod(cartoffsetPos, offsetpos);
846 //cout << "OFFSET POS" << offsetpos.getElevationFt();
850 void FGSubmodelMgr::valueChanged(SGPropertyNode *prop)
852 return; // this isn't working atm
854 const char* _model_added = _model_added_node->getStringValue();
856 std::basic_string <char>::size_type indexCh2b;
858 string str2 = _model_added;
859 const char *cstr2b = "multiplayer";
860 indexCh2b = str2.find( cstr2b, 0 );
862 if (indexCh2b != string::npos ){ // we will ignore Ballistic Objects - there are potentially too many
864 //cout << "Submodels: model added - " << str2 <<" read path "<< endl;
866 SGPropertyNode *a_node = fgGetNode(_model_added, true);
867 SGPropertyNode *sub_node = a_node->getChild("sim", 0, true);
868 SGPropertyNode_ptr path_node = sub_node->getChild("path", 0, true);
869 SGPropertyNode_ptr callsign_node = a_node->getChild("callsign", 0, true);
871 //const string& callsign = callsign_node->getStringValue();
872 //cout << "Submodels: model added - " << callsign <<" read callsign "<< endl;
876 cout << "model added - " << str2 <<" returning "<< endl;
882 void FGSubmodelMgr::setParentNode(int id) {
884 const SGPropertyNode_ptr ai = fgGetNode("/ai/models", true);
886 for (int i = ai->nChildren() - 1; i >= -1; i--) {
887 SGPropertyNode_ptr model;
889 if (i < 0) { // last iteration: selected model
890 model = _selected_ac;
892 model = ai->getChild(i);
893 //const string& path = ai->getPath();
894 //const string& name = model->getStringValue("name");
895 int parent_id = model->getIntValue("id");
896 if (!model->nChildren()){
899 if (parent_id == id) {
900 _selected_ac = model; // save selected model for last iteration
910 if (_selected_ac != 0){
912 //cout << " parent node found"<< endl;
914 const string name = _selected_ac->getStringValue("name");
915 IC.lat = _selected_ac->getDoubleValue("position/latitude-deg");
916 IC.lon = _selected_ac->getDoubleValue("position/longitude-deg");
917 IC.alt = _selected_ac->getDoubleValue("position/altitude-ft");
918 IC.roll = _selected_ac->getDoubleValue("orientation/roll-deg");
919 IC.elevation = _selected_ac->getDoubleValue("orientation/pitch-deg");
920 IC.azimuth = _selected_ac->getDoubleValue("orientation/true-heading-deg");
921 IC.speed = _selected_ac->getDoubleValue("velocities/true-airspeed-kt") * SG_KT_TO_FPS;
922 IC.speed_down_fps = -_selected_ac->getDoubleValue("velocities/vertical-speed-fps");
923 IC.speed_east_fps = _selected_ac->getDoubleValue("velocities/speed-east-fps");
924 IC.speed_north_fps = _selected_ac->getDoubleValue("velocities/speed-north-fps");
926 //cout << name << " IC.speed " << IC.speed << endl;
929 SG_LOG(SG_AI, SG_ALERT, "AISubmodel: parent node not found ");
933 // end of submodel.cxx