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 transform(sm); // calculate submodel's initial conditions in world-coordinates
269 FGAIBallistic* ballist = new FGAIBallistic;
270 ballist->setPath(sm->model.c_str());
271 ballist->setName(sm->name);
272 ballist->setSlaved(sm->slaved);
273 ballist->setRandom(sm->random);
274 ballist->setRandomness(sm->randomness);
275 ballist->setLatitude(offsetpos.getLatitudeDeg());
276 ballist->setLongitude(offsetpos.getLongitudeDeg());
277 ballist->setAltitude(offsetpos.getElevationFt());
278 ballist->setAzimuth(IC.azimuth);
279 ballist->setElevation(IC.elevation);
280 ballist->setRoll(IC.roll);
281 ballist->setSpeed(IC.speed / SG_KT_TO_FPS);
282 ballist->setWind_from_east(IC.wind_from_east);
283 ballist->setWind_from_north(IC.wind_from_north);
284 ballist->setMass(IC.mass);
285 ballist->setDragArea(sm->drag_area);
286 ballist->setLife(sm->life);
287 ballist->setBuoyancy(sm->buoyancy);
288 ballist->setWind(sm->wind);
289 ballist->setCd(sm->cd);
290 ballist->setStabilisation(sm->aero_stabilised);
291 ballist->setNoRoll(sm->no_roll);
292 ballist->setCollision(sm->collision);
293 ballist->setExpiry(sm->expiry);
294 ballist->setImpact(sm->impact);
295 ballist->setImpactReportNode(sm->impact_report);
296 ballist->setFuseRange(sm->fuse_range);
297 ballist->setSubmodel(sm->submodel.c_str());
298 ballist->setSubID(sm->sub_id);
299 ballist->setForceStabilisation(sm->force_stabilised);
300 ballist->setExternalForce(sm->ext_force);
301 ballist->setForcePath(sm->force_path.c_str());
302 ballist->setXoffset(sm->x_offset);
303 ballist->setYoffset(sm->y_offset);
304 ballist->setZoffset(sm->z_offset);
305 ballist->setPitchoffset(sm->pitch_offset);
306 ballist->setYawoffset(sm->yaw_offset);
307 ballist->setParentNodes(_selected_ac);
308 ballist->setContentsNode(sm->contents_node);
309 ballist->setWeight(sm->weight);
311 aiManager()->attach(ballist);
318 void FGSubmodelMgr::load()
320 SGPropertyNode *path = fgGetNode("/sim/submodels/path");
324 const string& Path = path->getStringValue();
325 bool Seviceable =_serviceable_node->getBoolValue();
326 setData(id, Path, Seviceable);
330 void FGSubmodelMgr::transform(submodel *sm)
332 // set initial conditions
333 if (sm->contents_node != 0 && !sm->slaved) {
334 // get the weight of the contents (lbs) and convert to mass (slugs)
335 sm->contents = sm->contents_node->getChild("level-lbs",0,1)->getDoubleValue();
336 //cout << "transform: contents " << sm->contents << endl;
337 IC.mass = (sm->weight + sm->contents) * lbs_to_slugs;
338 //cout << "mass inc contents" << IC.mass << endl;
340 // set contents to 0 in the parent
341 sm->contents_node->getChild("level-gal_us",0,1)->setDoubleValue(0);
342 /*cout << "contents " << sm->contents_node->getChild("level-gal_us")->getDoubleValue()
343 << " " << sm->contents_node->getChild("level-lbs",0,1)->getDoubleValue()
346 IC.mass = sm->weight * lbs_to_slugs;
349 //int sub_id = sm->sub_id;
350 //const string& name = sm->name;
353 if (sm->speed_node != 0)
354 sm->speed = sm->speed_node->getDoubleValue();
357 //cout << " name " << name << " id " << id << " sub id" << sub_id << endl;
359 // set the Initial Conditions for the types of submodel parent
361 if (_impact || _hit || _expiry) {
362 // set the data for a submodel tied to a submodel
366 IC.lat = _parent_lat;
367 IC.lon = _parent_lon;
368 IC.alt = _parent_elev;
369 IC.roll = _parent_roll; // rotation about x axis
370 IC.elevation = _parent_pitch; // rotation about y axis
371 IC.azimuth = _parent_hdg; // rotation about z axis
372 IC.speed = _parent_speed;
373 IC.speed_down_fps = 0;
374 IC.speed_east_fps = 0;
375 IC.speed_north_fps = 0;
377 } else if (id == 0) {
378 //set the data for a submodel tied to the main model
380 IC.lat = _user_lat_node->getDoubleValue();
381 IC.lon = _user_lon_node->getDoubleValue();
382 IC.alt = _user_alt_node->getDoubleValue();
383 IC.roll = _user_roll_node->getDoubleValue(); // rotation about x axis
384 IC.elevation = _user_pitch_node->getDoubleValue(); // rotation about y axis
385 IC.azimuth = _user_heading_node->getDoubleValue(); // rotation about z axis
386 IC.speed = _user_speed_node->getDoubleValue();
387 IC.speed_down_fps = _user_speed_down_fps_node->getDoubleValue();
388 IC.speed_east_fps = _user_speed_east_fps_node->getDoubleValue();
389 IC.speed_north_fps = _user_speed_north_fps_node->getDoubleValue();
392 // set the data for a submodel tied to an AI Object
393 //cout << " set the data for a submodel tied to an AI Object " << id << endl;
397 //cout << "Submodel: setting IC "<< name << endl;
398 //cout << "heading " << IC.azimuth << endl ;
399 //cout << "speed down " << IC.speed_down_fps << endl ;
400 //cout << "speed east " << IC.speed_east_fps << endl ;
401 //cout << "speed north " << IC.speed_north_fps << endl ;
402 //cout << "parent speed fps in " << IC.speed << "sm speed in " << sm->speed << endl ;
403 //cout << "lat " << IC.lat;
404 //cout << "alt " << IC.alt << endl ;
407 // Set the Initial Conditions that are common to all types of parent
408 IC.wind_from_east = _user_wind_from_east_node->getDoubleValue();
409 IC.wind_from_north = _user_wind_from_north_node->getDoubleValue();
411 //cout << "wind e " << IC.wind_from_east << " n " << IC.wind_from_north << endl;
413 userpos.setLatitudeDeg(IC.lat);
414 userpos.setLongitudeDeg(IC.lon);
415 userpos.setElevationFt(IC.alt);
417 _x_offset = sm->x_offset;
418 _y_offset = sm->y_offset;
419 _z_offset = sm->z_offset;
423 //IC.elevation += sm->pitch_offset;
424 //IC.azimuth += sm->yaw_offset ;
426 // pre-process the trig functions
427 cosRx = cos(-IC.roll * SG_DEGREES_TO_RADIANS);
428 sinRx = sin(-IC.roll * SG_DEGREES_TO_RADIANS);
429 cosRy = cos(-IC.elevation * SG_DEGREES_TO_RADIANS);
430 sinRy = sin(-IC.elevation * SG_DEGREES_TO_RADIANS);
431 cosRz = cos(IC.azimuth * SG_DEGREES_TO_RADIANS);
432 sinRz = sin(IC.azimuth * SG_DEGREES_TO_RADIANS);
435 // Get submodel initial velocity vector angles in XZ and XY planes.
436 // This vector should be added to aircraft's vector.
437 IC.elevation += (sm->yaw_offset * sinRx) + (sm->pitch_offset * cosRx);
438 IC.azimuth += (sm->yaw_offset * cosRx) - (sm->pitch_offset * sinRx);
440 // calculate the total speed north
441 IC.total_speed_north = sm->speed * cos(IC.elevation * SG_DEGREES_TO_RADIANS)
442 * cos(IC.azimuth * SG_DEGREES_TO_RADIANS) + IC.speed_north_fps;
444 // calculate the total speed east
445 IC.total_speed_east = sm->speed * cos(IC.elevation * SG_DEGREES_TO_RADIANS)
446 * sin(IC.azimuth * SG_DEGREES_TO_RADIANS) + IC.speed_east_fps;
448 // calculate the total speed down
449 IC.total_speed_down = sm->speed * -sin(IC.elevation * SG_DEGREES_TO_RADIANS)
452 // re-calculate speed, elevation and azimuth
453 IC.speed = sqrt(IC.total_speed_north * IC.total_speed_north
454 + IC.total_speed_east * IC.total_speed_east
455 + IC.total_speed_down * IC.total_speed_down);
457 // if speeds are low this calculation can become unreliable
459 IC.azimuth = atan2(IC.total_speed_east, IC.total_speed_north) * SG_RADIANS_TO_DEGREES;
460 // cout << "azimuth1 " << IC.azimuth<<endl;
462 // rationalise the output
465 else if (IC.azimuth >= 360)
467 // cout << "azimuth2 " << IC.azimuth<<endl;
469 IC.elevation = -atan(IC.total_speed_down / sqrt(IC.total_speed_north
470 * IC.total_speed_north + IC.total_speed_east * IC.total_speed_east))
471 * SG_RADIANS_TO_DEGREES;
473 //cout << "IC.speed " << IC.speed / SG_KT_TO_FPS << endl;
476 void FGSubmodelMgr::updatelat(double lat)
478 ft_per_deg_latitude = 366468.96 - 3717.12 * cos(lat / SG_RADIANS_TO_DEGREES);
479 ft_per_deg_longitude = 365228.16 * cos(lat / SG_RADIANS_TO_DEGREES);
482 void FGSubmodelMgr::loadAI()
484 SG_LOG(SG_AI, SG_DEBUG, "Submodels: Loading AI submodels ");
486 FGAIManager::ai_list_type sm_list(aiManager()->get_ai_list());
488 if (sm_list.empty()) {
489 SG_LOG(SG_AI, SG_ALERT, "Submodels: Unable to read AI submodel list");
493 FGAIManager::ai_list_iterator sm_list_itr = sm_list.begin(),
496 while (sm_list_itr != end) {
497 string path = (*sm_list_itr)->_getSMPath();
504 int id = (*sm_list_itr)->getID();
505 bool serviceable = (*sm_list_itr)->_getServiceable();
507 //string type = (*sm_list_itr)->getTypeString();
508 //cout << "loadAI: type " << type << " path "<< path << " serviceable " << serviceable << endl;
510 setData(id, path, serviceable);
517 void FGSubmodelMgr::setData(int id, const string& path, bool serviceable)
521 SGPath config = globals->resolve_aircraft_path(path);
523 SG_LOG(SG_AI, SG_DEBUG,
524 "Submodels: Trying to read AI submodels file: " << config.str());
525 readProperties(config.str(), &root);
526 } catch (const sg_exception &) {
527 SG_LOG(SG_AI, SG_ALERT,
528 "Submodels: Unable to read AI submodels file: " << config.str());
532 vector<SGPropertyNode_ptr> children = root.getChildren("submodel");
533 vector<SGPropertyNode_ptr>::iterator it = children.begin();
534 vector<SGPropertyNode_ptr>::iterator end = children.end();
536 for (int i = 0; it != end; ++it, i++) {
537 //cout << "Reading AI submodel " << (*it)->getPath() << endl;
538 submodel* sm = new submodel;
539 SGPropertyNode * entry_node = *it;
540 sm->name = entry_node->getStringValue("name", "none_defined");
541 sm->model = entry_node->getStringValue("model", "Models/Geometry/rocket.ac");
542 sm->speed = entry_node->getDoubleValue("speed", 2329.4);
543 sm->repeat = entry_node->getBoolValue("repeat", false);
544 sm->delay = entry_node->getDoubleValue("delay", 0.25);
545 sm->count = entry_node->getIntValue("count", 1);
546 sm->slaved = entry_node->getBoolValue("slaved", false);
547 sm->x_offset = entry_node->getDoubleValue("x-offset", 0.0);
548 sm->y_offset = entry_node->getDoubleValue("y-offset", 0.0);
549 sm->z_offset = entry_node->getDoubleValue("z-offset", 0.0);
550 sm->yaw_offset = entry_node->getDoubleValue("yaw-offset", 0.0);
551 sm->pitch_offset = entry_node->getDoubleValue("pitch-offset", 0.0);
552 sm->drag_area = entry_node->getDoubleValue("eda", 0.034);
553 sm->life = entry_node->getDoubleValue("life", 900.0);
554 sm->buoyancy = entry_node->getDoubleValue("buoyancy", 0);
555 sm->wind = entry_node->getBoolValue("wind", false);
556 sm->cd = entry_node->getDoubleValue("cd", 0.193);
557 sm->weight = entry_node->getDoubleValue("weight", 0.25);
558 sm->aero_stabilised = entry_node->getBoolValue("aero-stabilised", true);
559 sm->no_roll = entry_node->getBoolValue("no-roll", false);
560 sm->collision = entry_node->getBoolValue("collision", false);
561 sm->expiry = entry_node->getBoolValue("expiry", false);
562 sm->impact = entry_node->getBoolValue("impact", false);
563 sm->impact_report = entry_node->getStringValue("impact-reports");
564 sm->fuse_range = entry_node->getDoubleValue("fuse-range", 0.0);
565 sm->contents_node = fgGetNode(entry_node->getStringValue("contents", "none"), false);
566 sm->speed_node = fgGetNode(entry_node->getStringValue("speed-prop", "none"), false);
567 sm->submodel = entry_node->getStringValue("submodel-path", "");
568 sm->force_stabilised= entry_node->getBoolValue("force-stabilised", false);
569 sm->ext_force = entry_node->getBoolValue("external-force", false);
570 sm->force_path = entry_node->getStringValue("force-path", "");
571 sm->random = entry_node->getBoolValue("random", false);
572 sm->randomness = entry_node->getDoubleValue("randomness", 0.5);
574 if (sm->contents_node != 0)
575 sm->contents = sm->contents_node->getDoubleValue();
577 const char *trigger_path = entry_node->getStringValue("trigger", 0);
579 sm->trigger_node = fgGetNode(trigger_path, true);
580 sm->trigger_node->setBoolValue(sm->trigger_node->getBoolValue());
582 sm->trigger_node = 0;
585 if (sm->speed_node != 0)
586 sm->speed = sm->speed_node->getDoubleValue();
588 sm->timer = sm->delay;
590 sm->first_time = false;
591 sm->serviceable = serviceable;
594 sm->prop = fgGetNode("/ai/submodels/submodel", index, true);
595 sm->prop->tie("delay", SGRawValuePointer<double>(&(sm->delay)));
596 sm->prop->tie("count", SGRawValuePointer<int>(&(sm->count)));
597 sm->prop->tie("repeat", SGRawValuePointer<bool>(&(sm->repeat)));
598 sm->prop->tie("id", SGRawValuePointer<int>(&(sm->id)));
599 sm->prop->tie("sub-id", SGRawValuePointer<int>(&(sm->sub_id)));
600 sm->prop->tie("serviceable", SGRawValuePointer<bool>(&(sm->serviceable)));
601 sm->prop->tie("random", SGRawValuePointer<bool>(&(sm->random)));
602 sm->prop->tie("slaved", SGRawValuePointer<bool>(&(sm->slaved)));
603 const string& name = sm->name;
604 sm->prop->setStringValue("name", name.c_str());
606 const string& submodel = sm->submodel;
607 sm->prop->setStringValue("submodel", submodel.c_str());
609 const string& force_path = sm->force_path;
610 sm->prop->setStringValue("force_path", force_path.c_str());
611 //cout << "set force_path Sub " << force_path << endl;
613 if (sm->contents_node != 0)
614 sm->prop->tie("contents-lbs", SGRawValuePointer<double>(&(sm->contents)));
617 submodels.push_back(sm);
621 void FGSubmodelMgr::setSubData(int id, const string& path, bool serviceable)
624 SGPath config = globals->resolve_aircraft_path(path);
627 SG_LOG(SG_AI, SG_DEBUG,
628 "Submodels: Trying to read AI submodels file: " << config.str());
629 readProperties(config.str(), &root);
631 } catch (const sg_exception &) {
632 SG_LOG(SG_AI, SG_ALERT,
633 "Submodels: Unable to read AI submodels file: " << config.str());
637 vector<SGPropertyNode_ptr> children = root.getChildren("submodel");
638 vector<SGPropertyNode_ptr>::iterator it = children.begin();
639 vector<SGPropertyNode_ptr>::iterator end = children.end();
641 for (int i = 0; it != end; ++it, i++) {
642 //cout << "Reading AI submodel " << (*it)->getPath() << endl;
643 submodel* sm = new submodel;
644 SGPropertyNode * entry_node = *it;
645 sm->name = entry_node->getStringValue("name", "none_defined");
646 sm->model = entry_node->getStringValue("model", "Models/Geometry/rocket.ac");
647 sm->speed = entry_node->getDoubleValue("speed", 2329.4);
648 sm->repeat = entry_node->getBoolValue("repeat", false);
649 sm->delay = entry_node->getDoubleValue("delay", 0.25);
650 sm->count = entry_node->getIntValue("count", 1);
651 sm->slaved = entry_node->getBoolValue("slaved", false);
652 sm->x_offset = entry_node->getDoubleValue("x-offset", 0.0);
653 sm->y_offset = entry_node->getDoubleValue("y-offset", 0.0);
654 sm->z_offset = entry_node->getDoubleValue("z-offset", 0.0);
655 sm->yaw_offset = entry_node->getDoubleValue("yaw-offset", 0.0);
656 sm->pitch_offset = entry_node->getDoubleValue("pitch-offset", 0.0);
657 sm->drag_area = entry_node->getDoubleValue("eda", 0.034);
658 sm->life = entry_node->getDoubleValue("life", 900.0);
659 sm->buoyancy = entry_node->getDoubleValue("buoyancy", 0);
660 sm->wind = entry_node->getBoolValue("wind", false);
661 sm->cd = entry_node->getDoubleValue("cd", 0.193);
662 sm->weight = entry_node->getDoubleValue("weight", 0.25);
663 sm->aero_stabilised = entry_node->getBoolValue("aero-stabilised", true);
664 sm->no_roll = entry_node->getBoolValue("no-roll", false);
665 sm->collision = entry_node->getBoolValue("collision", false);
666 sm->expiry = entry_node->getBoolValue("expiry", false);
667 sm->impact = entry_node->getBoolValue("impact", false);
668 sm->impact_report = entry_node->getStringValue("impact-reports");
669 sm->fuse_range = entry_node->getDoubleValue("fuse-range", 0.0);
670 sm->contents_node = fgGetNode(entry_node->getStringValue("contents", "none"), false);
671 sm->speed_node = fgGetNode(entry_node->getStringValue("speed-prop", "none"), false);
672 sm->submodel = entry_node->getStringValue("submodel-path", "");
673 sm->force_stabilised= entry_node->getBoolValue("force-stabilised", false);
674 sm->ext_force = entry_node->getBoolValue("external-force", false);
675 sm->force_path = entry_node->getStringValue("force-path", "");
676 sm->random = entry_node->getBoolValue("random", false);
677 sm->randomness = entry_node->getDoubleValue("randomness", 0.5);
679 if (sm->contents_node != 0)
680 sm->contents = sm->contents_node->getDoubleValue();
682 const char *trigger_path = entry_node->getStringValue("trigger", 0);
684 sm->trigger_node = fgGetNode(trigger_path, true);
685 sm->trigger_node->setBoolValue(sm->trigger_node->getBoolValue());
687 sm->trigger_node = 0;
690 if (sm->speed_node != 0)
691 sm->speed = sm->speed_node->getDoubleValue();
693 sm->timer = sm->delay;
695 sm->first_time = false;
696 sm->serviceable = serviceable;
699 sm->prop = fgGetNode("/ai/submodels/subsubmodel", index, true);
700 sm->prop->tie("count", SGRawValuePointer<int>(&(sm->count)));
701 sm->prop->tie("repeat", SGRawValuePointer<bool>(&(sm->repeat)));
702 sm->prop->tie("id", SGRawValuePointer<int>(&(sm->id)));
703 sm->prop->tie("sub-id", SGRawValuePointer<int>(&(sm->sub_id)));
704 sm->prop->tie("serviceable", SGRawValuePointer<bool>(&(sm->serviceable)));
705 sm->prop->tie("random", SGRawValuePointer<bool>(&(sm->random)));
706 sm->prop->tie("slaved", SGRawValuePointer<bool>(&(sm->slaved)));
708 const string& name = sm->name;
709 sm->prop->setStringValue("name", name.c_str());
711 const string& submodel = sm->submodel;
712 sm->prop->setStringValue("submodel-path", submodel.c_str());
713 // cout << " set submodel path AI" << submodel<< endl;
715 const string& force_path = sm->force_path;
716 sm->prop->setStringValue("force_path", force_path.c_str());
717 //cout << "set force_path AI" << force_path << endl;
719 if (sm->contents_node != 0)
720 sm->prop->tie("contents-lbs", SGRawValuePointer<double>(&(sm->contents)));
723 subsubmodels.push_back(sm);
727 void FGSubmodelMgr::loadSubmodels()
729 SG_LOG(SG_AI, SG_DEBUG, "Submodels: Loading sub submodels");
733 submodel_iterator = submodels.begin();
735 while (submodel_iterator != submodels.end()) {
736 const string& submodel = (*submodel_iterator)->submodel;
737 if (!submodel.empty()) {
738 //int id = (*submodel_iterator)->id;
739 bool serviceable = true;
740 SG_LOG(SG_AI, SG_DEBUG, "found path sub sub "
742 << " index " << index
743 << " name " << (*submodel_iterator)->name);
745 if ((*submodel_iterator)->sub_id == 0){
746 (*submodel_iterator)->sub_id = index;
748 setSubData(index, submodel, serviceable);
755 subsubmodel_iterator = subsubmodels.begin();
757 while (subsubmodel_iterator != subsubmodels.end()) {
759 submodels.push_back(*subsubmodel_iterator);
760 ++subsubmodel_iterator;
763 subsubmodels.clear();
765 //submodel_iterator = submodels.begin();
769 //while (submodel_iterator != submodels.end()) {
770 // int id = (*submodel_iterator)->id;
773 // SG_LOG(SG_AI, SG_ALERT,"after pushback "
774 // << " parent id " << id
775 // << " name " << (*submodel_iterator)->name
776 // << " sub id " << (*submodel_iterator)->sub_id
777 // << " subcount "<< subcount);
779 // ++submodel_iterator;
783 SGVec3d FGSubmodelMgr::getCartOffsetPos() const{
785 // convert geodetic positions to geocentered
786 SGVec3d cartuserPos = SGVec3d::fromGeod(userpos);
787 // Transform to the right coordinate frame, configuration is done in
788 // the x-forward, y-right, z-up coordinates (feet), computation
789 // in the simulation usual body x-forward, y-right, z-down coordinates
792 SGVec3d _off(_x_offset * SG_FEET_TO_METER,
793 _y_offset * SG_FEET_TO_METER,
794 -_z_offset * SG_FEET_TO_METER);
796 // Transform the user position to the horizontal local coordinate system.
797 SGQuatd hlTrans = SGQuatd::fromLonLat(userpos);
799 // and postrotate the orientation of the user model wrt the horizontal
801 hlTrans *= SGQuatd::fromYawPitchRollDeg(
806 // The offset converted to the usual body fixed coordinate system
807 // rotated to the earth-fixed coordinates axis
808 SGVec3d off = hlTrans.backTransform(_off);
810 // Add the position offset of the user model to get the geocentered position
811 SGVec3d offsetPos = cartuserPos + off;
815 void FGSubmodelMgr::setOffsetPos(){
816 // convert the offset geocentered position to geodetic
817 SGVec3d cartoffsetPos = getCartOffsetPos();
819 SGGeodesy::SGCartToGeod(cartoffsetPos, offsetpos);
821 //cout << "OFFSET POS" << offsetpos.getElevationFt();
825 void FGSubmodelMgr::valueChanged(SGPropertyNode *prop)
827 return; // this isn't working atm
829 const char* _model_added = _model_added_node->getStringValue();
831 std::basic_string <char>::size_type indexCh2b;
833 string str2 = _model_added;
834 const char *cstr2b = "multiplayer";
835 indexCh2b = str2.find( cstr2b, 0 );
837 if (indexCh2b != string::npos ){ // we will ignore Ballistic Objects - there are potentially too many
839 //cout << "Submodels: model added - " << str2 <<" read path "<< endl;
841 SGPropertyNode *a_node = fgGetNode(_model_added, true);
842 SGPropertyNode *sub_node = a_node->getChild("sim", 0, true);
843 SGPropertyNode_ptr path_node = sub_node->getChild("path", 0, true);
844 SGPropertyNode_ptr callsign_node = a_node->getChild("callsign", 0, true);
846 //const string& callsign = callsign_node->getStringValue();
847 //cout << "Submodels: model added - " << callsign <<" read callsign "<< endl;
851 cout << "model added - " << str2 <<" returning "<< endl;
857 void FGSubmodelMgr::setParentNode(int id) {
859 const SGPropertyNode_ptr ai = fgGetNode("/ai/models", true);
861 for (int i = ai->nChildren() - 1; i >= -1; i--) {
862 SGPropertyNode_ptr model;
864 if (i < 0) { // last iteration: selected model
865 model = _selected_ac;
867 model = ai->getChild(i);
868 //const string& path = ai->getPath();
869 //const string& name = model->getStringValue("name");
870 int parent_id = model->getIntValue("id");
871 if (!model->nChildren()){
874 if (parent_id == id) {
875 _selected_ac = model; // save selected model for last iteration
885 if (_selected_ac != 0){
887 //cout << " parent node found"<< endl;
889 const string name = _selected_ac->getStringValue("name");
890 IC.lat = _selected_ac->getDoubleValue("position/latitude-deg");
891 IC.lon = _selected_ac->getDoubleValue("position/longitude-deg");
892 IC.alt = _selected_ac->getDoubleValue("position/altitude-ft");
893 IC.roll = _selected_ac->getDoubleValue("orientation/roll-deg");
894 IC.elevation = _selected_ac->getDoubleValue("orientation/pitch-deg");
895 IC.azimuth = _selected_ac->getDoubleValue("orientation/true-heading-deg");
896 IC.speed = _selected_ac->getDoubleValue("velocities/true-airspeed-kt") * SG_KT_TO_FPS;
897 IC.speed_down_fps = -_selected_ac->getDoubleValue("velocities/vertical-speed-fps");
898 IC.speed_east_fps = _selected_ac->getDoubleValue("velocities/speed-east-fps");
899 IC.speed_north_fps = _selected_ac->getDoubleValue("velocities/speed-north-fps");
901 //cout << name << " IC.speed " << IC.speed << endl;
904 SG_LOG(SG_AI, SG_ALERT, "AISubmodel: parent node not found ");
908 // end of submodel.cxx