FlightGear next
submodel.cxx
Go to the documentation of this file.
1
2// Written by Dave Culp, started Aug 2004
3// With major additions by Vivian Meaaza 2004 - 2007
4//
5// This file is in the Public Domain and comes with no warranty.
6
7#ifdef HAVE_CONFIG_H
8#include "config.h"
9#endif
10
11#include <algorithm>
12
13#include <simgear/math/sg_geodesy.hxx>
14#include <simgear/misc/sg_path.hxx>
15#include <simgear/props/props_io.hxx>
16#include <simgear/structure/exception.hxx>
17
18#include <Main/fg_props.hxx>
19#include <Main/util.hxx>
20
21#include "AIBallistic.hxx"
22#include "AIBase.hxx"
23#include "AIManager.hxx"
24
25#include "submodel.hxx"
26
27using std::cout;
28using std::endl;
29using std::string;
30using std::vector;
31
32
33const double FGSubmodelMgr::lbs_to_slugs = 0.031080950172;
34
36{
37 x_offset = y_offset = z_offset = 0.0;
38 pitch_offset = 0.0;
39 yaw_offset = 0.0;
40
41 //out[0] = out[1] = out[2] = 0;
42 //string contents_node;
43 contrail_altitude = 30000;
44 _count = 0;
45 _found_sub = true;
46}
47
48FGAIManager* FGSubmodelMgr::aiManager()
49{
50 return globals->get_subsystem<FGAIManager>();
51}
52
54{
55 index = 0;
56
57 _serviceable_node = fgGetNode("/sim/submodels/serviceable", true);
58 _serviceable_node->setBoolValue(true);
59
60 _user_lat_node = fgGetNode("/position/latitude-deg", true);
61 _user_lon_node = fgGetNode("/position/longitude-deg", true);
62 _user_alt_node = fgGetNode("/position/altitude-ft", true);
63
64 _user_heading_node = fgGetNode("/orientation/heading-deg", true);
65 _user_pitch_node = fgGetNode("/orientation/pitch-deg", true);
66 _user_roll_node = fgGetNode("/orientation/roll-deg", true);
67 _user_yaw_node = fgGetNode("/orientation/yaw-deg", true);
68 _user_alpha_node = fgGetNode("/orientation/alpha-deg", true);
69
70 _user_speed_node = fgGetNode("/velocities/uBody-fps", true);
71
72 _user_wind_from_east_node = fgGetNode("/environment/wind-from-east-fps", true);
73 _user_wind_from_north_node = fgGetNode("/environment/wind-from-north-fps", true);
74
75 _user_speed_down_fps_node = fgGetNode("/velocities/speed-down-fps", true);
76 _user_speed_east_fps_node = fgGetNode("/velocities/speed-east-fps", true);
77 _user_speed_north_fps_node = fgGetNode("/velocities/speed-north-fps", true);
78
79 _contrail_altitude_node = fgGetNode("/environment/params/contrail-altitude", true);
80 contrail_altitude = _contrail_altitude_node->getDoubleValue();
81 _contrail_trigger = fgGetNode("ai/submodels/contrails", true);
82 _contrail_trigger->setBoolValue(false);
83
84 load();
85}
86
88{
89 // postinit, so that the AI list is populated
90 loadAI();
91
92 while (_found_sub)
93 loadSubmodels();
94
95 // TODO: reload submodels if an MP ac joins
96
97 //_model_added_node = fgGetNode("ai/models/model-added", true);
98 //_model_added_node->addChangeListener(this, false);
99}
100
102{
103 std::for_each(submodels.begin(), submodels.end(), [](submodel* sm) { delete sm; });
104 submodels.clear();
105}
106
108{
109}
110
112{
113 submodel_iterator = submodels.begin();
114 while (submodel_iterator != submodels.end()) {
115 (*submodel_iterator)->prop->untie("count");
116 ++submodel_iterator;
117 }
118}
119
121{
122 if (!_serviceable_node->getBoolValue())
123 return;
124
125 _impact = false;
126 _hit = false;
127 _expiry = false;
128
129 // Check if the submodel hit an object or terrain
130 FGAIManager::ai_list_type sm_list(aiManager()->get_ai_list());
131 FGAIManager::ai_list_iterator sm_list_itr = sm_list.begin();
132 FGAIManager::ai_list_iterator end = sm_list.end();
133
134 for (; sm_list_itr != end; ++sm_list_itr) {
135 FGAIBase::object_type object_type = (*sm_list_itr)->getType();
136
137 // Continue if object is not ballistic
138 if (object_type != FGAIBase::object_type::otBallistic) {
139 continue;
140 }
141
142 int parent_subID = (*sm_list_itr)->_getSubID();
143 int id = (*sm_list_itr)->getID();
144
145 if (parent_subID == 0 || id == -1) // this entry in the list has no associated submodel
146 continue; // or is invalid so we can continue
147
148 //SG_LOG(SG_AI, SG_DEBUG, "Submodel: Impact " << _impact << " hit! "
149 // << _hit <<" parent_subID " << parent_subID);
150
151 _hit = (*sm_list_itr)->_getCollisionData();
152 _impact = (*sm_list_itr)->_getImpactData();
153 _expiry = (*sm_list_itr)->_getExpiryData();
154
155 //SG_LOG(SG_AI, SG_ALERT, "Submodel: " << (*sm_list_itr)->_getName()
156 // << " Impact " << _impact << " hit! " << _hit
157 // << " expiry :-( " << _expiry );
158
159 if (_impact || _hit || _expiry) {
160 // SG_LOG(SG_AI, SG_ALERT, "Submodel: Impact " << _impact << " hit! " << _hit
161 //<< " expiry :-( " << _expiry );
162
163 submodel_iterator = submodels.begin();
164
165 while (submodel_iterator != submodels.end()) {
166 int child_ID = (*submodel_iterator)->id;
167 //cout << "Impact: parent SubID " << parent_subID << " child_ID " << child_ID << endl;
168
169 if (parent_subID == child_ID) {
170 _parent_lat = (*sm_list_itr)->_getImpactLat();
171 _parent_lon = (*sm_list_itr)->_getImpactLon();
172 _parent_elev = (*sm_list_itr)->_getImpactElevFt();
173 _parent_hdg = (*sm_list_itr)->_getImpactHdg();
174 _parent_pitch = (*sm_list_itr)->_getImpactPitch();
175 _parent_roll = (*sm_list_itr)->_getImpactRoll();
176 _parent_speed = (*sm_list_itr)->_getImpactSpeed();
177 (*submodel_iterator)->first_time = true;
178 //cout << "Impact: parent SubID = child_ID elev " << _parent_elev << endl;
179
180 if (release(*submodel_iterator, dt)) {
181 (*sm_list_itr)->setDie(true);
182 //cout << "Impact: set die" << (*sm_list_itr)->_getName() << endl;
183 }
184 }
185
186 ++submodel_iterator;
187 }
188 }
189 }
190
191 _contrail_trigger->setBoolValue(_user_alt_node->getDoubleValue() > contrail_altitude);
192
193
194 for (auto sm : submodels) {
195 bool trigger = false;
196 if (sm->trigger_node) {
197 trigger = sm->trigger_node->getBoolValue();
198 }
199
200 if (trigger && sm->count != 0) {
201 //int id = (*submodel_iterator)->id;
202 //const string& name = (*submodel_iterator)->name;
203 SG_LOG(SG_AI, SG_DEBUG,
204 "Submodels release: " << sm->id
205 << " name " << sm->name
206 << " count " << sm->count
207 << " slaved " << sm->slaved);
208
209 release(sm, dt);
210 } else {
211 sm->first_time = true; // reset first-time flag
212 }
213 }
214}
215
216bool FGSubmodelMgr::release(submodel* sm, double dt)
217{
218 // Only run if first time or repeat is set to true
219 if (!sm->first_time && !sm->repeat) {
220 return false;
221 }
222
223 sm->timer += dt;
224
225 if (sm->timer < sm->delay) {
226 //cout << "not yet: timer " << sm->timer << " delay " << sm->delay << endl;
227 return false;
228 }
229
230 //cout << "released timer: " << sm->timer << " delay " << sm->delay << endl;
231
232 sm->timer = 0.0;
233
234 if (sm->first_time)
235 sm->first_time = false;
236
237 // Calculate submodel's initial conditions in world-coordinates
238 transform(sm);
239
240 FGAIBallistic* ballist = new FGAIBallistic;
241 ballist->setPath(sm->model.c_str());
242 ballist->setName(sm->name);
243 ballist->setSlaved(sm->slaved);
244 ballist->setRandom(sm->random);
245 ballist->setLifeRandomness(sm->life_randomness->get_value());
246 ballist->setLatitude(offsetpos.getLatitudeDeg());
247 ballist->setLongitude(offsetpos.getLongitudeDeg());
248 ballist->setAltitude(offsetpos.getElevationFt());
249 ballist->setAzimuthRandomError(sm->azimuth_error->get_value());
250 ballist->setAzimuth(IC.azimuth);
251 ballist->setElevationRandomError(sm->elevation_error->get_value());
252 ballist->setElevation(IC.elevation);
253 ballist->setRoll(IC.roll);
254 ballist->setSpeed(IC.speed / SG_KT_TO_FPS);
255 ballist->setWind_from_east(IC.wind_from_east);
256 ballist->setWind_from_north(IC.wind_from_north);
257 ballist->setMass(IC.mass);
258 ballist->setDragArea(sm->drag_area);
259 ballist->setLife(sm->life);
260 ballist->setBuoyancy(sm->buoyancy);
261 ballist->setWind(sm->wind);
262 ballist->setCdRandomness(sm->cd_randomness->get_value());
263 ballist->setCd(sm->cd);
264 ballist->setStabilisation(sm->aero_stabilised);
265 ballist->setNoRoll(sm->no_roll);
266 ballist->setCollision(sm->collision);
267 ballist->setExpiry(sm->expiry);
268 ballist->setImpact(sm->impact);
269 ballist->setImpactReportNode(sm->impact_report);
270 ballist->setFuseRange(sm->fuse_range);
271 ballist->setSubmodel(sm->submodel);
272 ballist->setSubID(sm->sub_id);
273 ballist->setForceStabilisation(sm->force_stabilised);
274 ballist->setExternalForce(sm->ext_force);
275 ballist->setForcePath(sm->force_path);
276 ballist->setXoffset(_x_offset);
277 ballist->setYoffset(_y_offset);
278 ballist->setZoffset(_z_offset);
279 ballist->setPitchoffset(sm->pitch_offset->get_value());
280 ballist->setYawoffset(sm->yaw_offset->get_value());
281 ballist->setParentNodes(_selected_ac);
282 ballist->setContentsNode(sm->contents_node);
283 ballist->setWeight(sm->weight);
284
285 aiManager()->attach(ballist);
286
287 if (sm->count > 0)
288 sm->count--;
289 return true;
290}
291
293{
294 SGPropertyNode_ptr path_node = fgGetNode("/sim/submodels/path");
295
296 if (path_node) {
297 const int id = 0;
298 const string& path = path_node->getStringValue();
299 bool serviceable = _serviceable_node->getBoolValue();
300 setData(id, path, serviceable, "/ai/submodels/submodel", submodels);
301 }
302}
303
304void FGSubmodelMgr::transform(submodel* sm)
305{
306 // Set initial conditions
307 if (sm->contents_node != 0 && !sm->slaved) {
308 // Get the weight of the contents (lbs) and convert to mass (slugs)
309 sm->contents = sm->contents_node->getChild("level-lbs", 0, 1)->getDoubleValue();
310 //cout << "transform: contents " << sm->contents << endl;
311 IC.mass = (sm->weight + sm->contents) * lbs_to_slugs;
312 //cout << "mass inc contents" << IC.mass << endl;
313
314 // Set contents to 0 in the parent
315 sm->contents_node->getChild("level-gal_us", 0, 1)->setDoubleValue(0);
316 /*cout << "contents " << sm->contents_node->getChild("level-gal_us")->getDoubleValue()
317 << " " << sm->contents_node->getChild("level-lbs",0,1)->getDoubleValue()
318 << endl;*/
319 } else {
320 IC.mass = sm->weight * lbs_to_slugs;
321 }
322
323 int id = sm->id;
324 //int sub_id = sm->sub_id;
325 //const string& name = sm->name;
326
327 if (sm->speed_node != 0)
328 sm->speed = sm->speed_node->getDoubleValue();
329
330 // set the Initial Conditions for the types of submodel parent
331
332 if (_impact || _hit || _expiry) {
333 _count++;
334
335 // Set the data for a submodel tied to a submodel
336 IC.lat = _parent_lat;
337 IC.lon = _parent_lon;
338 IC.alt = _parent_elev;
339 IC.roll = _parent_roll; // rotation about x axis
340 IC.elevation = _parent_pitch; // rotation about y axis
341 IC.azimuth = _parent_hdg; // rotation about z axis
342 IC.speed = _parent_speed;
343 IC.speed_down_fps = 0;
344 IC.speed_east_fps = 0;
345 IC.speed_north_fps = 0;
346 } else if (id == 0) {
347 // Set the data for a submodel tied to the main model
348 IC.lat = _user_lat_node->getDoubleValue();
349 IC.lon = _user_lon_node->getDoubleValue();
350 IC.alt = _user_alt_node->getDoubleValue();
351 IC.roll = _user_roll_node->getDoubleValue(); // rotation about x axis
352 IC.elevation = _user_pitch_node->getDoubleValue(); // rotation about y axis
353 IC.azimuth = _user_heading_node->getDoubleValue(); // rotation about z axis
354 IC.speed = _user_speed_node->getDoubleValue();
355 IC.speed_down_fps = _user_speed_down_fps_node->getDoubleValue();
356 IC.speed_east_fps = _user_speed_east_fps_node->getDoubleValue();
357 IC.speed_north_fps = _user_speed_north_fps_node->getDoubleValue();
358 } else {
359 // Set the data for a submodel tied to an AI Object
360 setParentNode(id);
361 }
362
363 // Set the Initial Conditions that are common to all types of parent
364 IC.wind_from_east = _user_wind_from_east_node->getDoubleValue();
365 IC.wind_from_north = _user_wind_from_north_node->getDoubleValue();
366
367 // For submodels affected by wind, convert ground speed to airspeed by subtracting wind.
368 // Since the wind frame is inverted (wind_*from*_{east,north}), the wind values are added.
369 if (sm->wind) {
370 IC.speed_east_fps += IC.wind_from_east;
371 IC.speed_north_fps += IC.wind_from_north;
372 }
373
374 userpos.setLatitudeDeg(IC.lat);
375 userpos.setLongitudeDeg(IC.lon);
376 userpos.setElevationFt(IC.alt);
377
378 if (sm->offsets_in_meter) {
379 _x_offset = -sm->x_offset->get_value() * SG_METER_TO_FEET;
380 _y_offset = sm->y_offset->get_value() * SG_METER_TO_FEET;
381 _z_offset = sm->z_offset->get_value() * SG_METER_TO_FEET;
382 } else {
383 _x_offset = sm->x_offset->get_value();
384 _y_offset = sm->y_offset->get_value();
385 _z_offset = sm->z_offset->get_value();
386 }
387
388 // this updates the 'offsetpos' member of us
389 setOffsetPos(sm);
390
391 // Compute initial orientation using yaw and pitch offsets and parent's orientation
392 const double l_yaw_offset = sm->yaw_offset->get_value();
393 const double l_pitch_offset = sm->pitch_offset->get_value();
394
395 SGQuatd ic_quat = SGQuatd::fromYawPitchRollDeg(IC.azimuth, IC.elevation, IC.roll);
396 ic_quat *= SGQuatd::fromYawPitchRollDeg(l_yaw_offset, l_pitch_offset, 0.0);
397
398 // Calculate total speed using speeds of submodel and parent
399 SGVec3d total_speed = SGVec3d(IC.speed_north_fps, IC.speed_east_fps, IC.speed_down_fps);
400 total_speed += ic_quat.rotate(SGVec3d(sm->speed, 0, 0));
401
402 IC.speed = length(total_speed);
403
404 // If speeds are low this calculation can become unreliable
405 if (IC.speed > 1) {
406 const double total_speed_north = total_speed.x();
407 const double total_speed_east = total_speed.y();
408 const double total_speed_down = total_speed.z();
409
410 IC.azimuth = atan2(total_speed_east, total_speed_north) * SG_RADIANS_TO_DEGREES;
411
412 // Rationalize the output
413 if (IC.azimuth < 0)
414 IC.azimuth += 360;
415 else if (IC.azimuth >= 360)
416 IC.azimuth -= 360;
417
418 IC.elevation = -atan(total_speed_down / sqrt(total_speed_north * total_speed_north + total_speed_east * total_speed_east)) * SG_RADIANS_TO_DEGREES;
419 } else {
420 double ic_roll;
421 ic_quat.getEulerDeg(IC.azimuth, IC.elevation, ic_roll);
422 }
423}
424
425void FGSubmodelMgr::loadAI()
426{
427 SG_LOG(SG_AI, SG_DEBUG, "Submodels: Loading AI submodels");
428
429 FGAIManager::ai_list_type sm_list(aiManager()->get_ai_list());
430
431 if (sm_list.empty()) {
432 SG_LOG(SG_AI, SG_ALERT, "Submodels: Unable to read AI submodel list");
433 return;
434 }
435
436 FGAIManager::ai_list_iterator sm_list_itr = sm_list.begin();
437 FGAIManager::ai_list_iterator end = sm_list.end();
438
439 while (sm_list_itr != end) {
440 string path = (*sm_list_itr)->_getSMPath();
441
442 if (path.empty()) {
443 ++sm_list_itr;
444 continue;
445 }
446
447 int id = (*sm_list_itr)->getID();
448 bool serviceable = (*sm_list_itr)->_getServiceable();
449
450 //string type = (*sm_list_itr)->getTypeString();
451 //cout << "loadAI: type " << type << " path "<< path << " serviceable " << serviceable << endl;
452
453 setData(id, path, serviceable, "/ai/submodels/submodel", submodels);
454 ++sm_list_itr;
455 }
456}
457
458void FGSubmodelMgr::setData(int id, const string& path, bool serviceable, const string& property_path, submodel_vector_type& models)
459{
460 SGPropertyNode root;
461
462 SGPath config = globals->resolve_aircraft_path(path);
463 if (!config.exists()) {
464 SG_LOG(SG_AI, SG_DEV_ALERT, "missing AI submodels file: " << config);
465 return;
466 }
467
468 try {
469 SG_LOG(SG_AI, SG_DEBUG,
470 "Submodels: Trying to read AI submodels file: " << config);
471 readProperties(config, &root);
472 } catch (const sg_exception&) {
473 SG_LOG(SG_AI, SG_ALERT,
474 "Submodels: Unable to read AI submodels file: " << config);
475 return;
476 }
477
478 vector<SGPropertyNode_ptr> children = root.getChildren("submodel");
479 vector<SGPropertyNode_ptr>::iterator it = children.begin();
480 vector<SGPropertyNode_ptr>::iterator end = children.end();
481
482 for (int i = 0; it != end; ++it, i++) {
483 //cout << "Reading AI submodel " << (*it)->getPath() << endl;
484 submodel* sm = new submodel;
485 SGPropertyNode_ptr entry_node = *it;
486 sm->name = entry_node->getStringValue("name", "none_defined");
487 sm->model = entry_node->getStringValue("model", "Models/Geometry/rocket.ac");
488 sm->speed = entry_node->getDoubleValue("speed", 2329.4);
489 sm->repeat = entry_node->getBoolValue("repeat", false);
490 sm->delay = entry_node->getDoubleValue("delay", 0.25);
491 sm->count = entry_node->getIntValue("count", 1);
492 sm->slaved = entry_node->getBoolValue("slaved", false);
493 sm->drag_area = entry_node->getDoubleValue("eda", 0.034);
494 sm->life = entry_node->getDoubleValue("life", 900.0);
495 sm->buoyancy = entry_node->getDoubleValue("buoyancy", 0);
496 sm->wind = entry_node->getBoolValue("wind", false);
497 sm->cd = entry_node->getDoubleValue("cd", 0.193);
498 sm->weight = entry_node->getDoubleValue("weight", 0.25);
499 sm->aero_stabilised = entry_node->getBoolValue("aero-stabilised", true);
500 sm->no_roll = entry_node->getBoolValue("no-roll", false);
501 sm->collision = entry_node->getBoolValue("collision", false);
502 sm->expiry = entry_node->getBoolValue("expiry", false);
503 sm->impact = entry_node->getBoolValue("impact", false);
504 sm->impact_report = entry_node->getStringValue("impact-reports");
505 sm->fuse_range = entry_node->getDoubleValue("fuse-range", 0.0);
506 sm->contents_node = fgGetNode(entry_node->getStringValue("contents", "none"), false);
507 sm->speed_node = fgGetNode(entry_node->getStringValue("speed-prop", "none"), false);
508 sm->submodel = entry_node->getStringValue("submodel-path", "");
509 sm->force_stabilised = entry_node->getBoolValue("force-stabilised", false);
510 sm->ext_force = entry_node->getBoolValue("external-force", false);
511 sm->force_path = entry_node->getStringValue("force-path", "");
512 sm->random = entry_node->getBoolValue("random", false);
513
514 SGPropertyNode_ptr prop_root = fgGetNode("/", true);
515 SGPropertyNode n;
516 SGPropertyNode_ptr a, b;
517
518 // Offsets
519 a = entry_node->getNode("offsets", false);
520 sm->offsets_in_meter = (a != 0);
521
522 if (a) {
523 b = a->getNode("x-m");
524 sm->x_offset = new simgear::Value(*prop_root, b ? *b : n);
525
526 b = a->getNode("y-m");
527 sm->y_offset = new simgear::Value(*prop_root, b ? *b : n);
528
529 b = a->getNode("z-m");
530 sm->z_offset = new simgear::Value(*prop_root, b ? *b : n);
531
532 b = a->getNode("heading-deg");
533 sm->yaw_offset = new simgear::Value(*prop_root, b ? *b : n);
534
535 b = a->getNode("pitch-deg");
536 sm->pitch_offset = new simgear::Value(*prop_root, b ? *b : n);
537 } else {
538 bool old = false;
539
540 b = entry_node->getNode("x-offset");
541 sm->x_offset = new simgear::Value(*prop_root, b ? *b : n);
542 if (b) old = true;
543
544 b = entry_node->getNode("y-offset");
545 sm->y_offset = new simgear::Value(*prop_root, b ? *b : n);
546 if (b) old = true;
547
548 b = entry_node->getNode("z-offset");
549 sm->z_offset = new simgear::Value(*prop_root, b ? *b : n);
550 if (b) old = true;
551
552 b = entry_node->getNode("yaw-offset");
553 sm->yaw_offset = new simgear::Value(*prop_root, b ? *b : n);
554 if (b) old = true;
555
556 b = entry_node->getNode("pitch-offset");
557 sm->pitch_offset = new simgear::Value(*prop_root, b ? *b : n);
558 if (b) old = true;
559
560 if (old) {
561 SG_LOG(SG_AI, SG_DEV_WARN, "Submodels: <*-offset> is deprecated. Use <offsets> instead");
562 }
563 }
564
565 // Randomness
566 a = entry_node->getNode("randomness", true);
567
568 // Maximum azimuth randomness error in degrees
569 b = a->getNode("azimuth");
570 sm->azimuth_error = new simgear::Value(*prop_root, b ? *b : n);
571
572 // Maximum elevation randomness error in degrees
573 b = a->getNode("elevation");
574 sm->elevation_error = new simgear::Value(*prop_root, b ? *b : n);
575
576 // Randomness of Cd (plus or minus)
577 b = a->getNode("cd");
578 if (!b) {
579 b = a->getNode("cd", true);
580 b->setDoubleValue(0.1);
581 }
582 sm->cd_randomness = new simgear::Value(*prop_root, *b);
583
584 // Randomness of life (plus or minus)
585 b = a->getNode("life");
586 if (!b) {
587 b = a->getNode("life", true);
588 b->setDoubleValue(0.5);
589 }
590 sm->life_randomness = new simgear::Value(*prop_root, *b);
591
592 if (sm->contents_node != 0)
593 sm->contents = sm->contents_node->getDoubleValue();
594
595 string trigger_path = entry_node->getStringValue("trigger", "");
596 if (!trigger_path.empty()) {
597 sm->trigger_node = fgGetNode(trigger_path, true);
598 sm->trigger_node->setBoolValue(sm->trigger_node->getBoolValue());
599 } else {
600 sm->trigger_node = 0;
601 }
602
603 if (sm->speed_node != 0)
604 sm->speed = sm->speed_node->getDoubleValue();
605
606 sm->timer = sm->delay;
607 sm->id = id;
608 sm->first_time = false;
609 sm->serviceable = serviceable;
610 sm->sub_id = 0;
611
612 sm->prop = fgGetNode(property_path, 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());
623
624 const string& submodel = sm->submodel;
625 sm->prop->setStringValue("submodel", submodel.c_str());
626
627 const string& force_path = sm->force_path;
628 sm->prop->setStringValue("force_path", force_path.c_str());
629
630 if (sm->contents_node != 0)
631 sm->prop->tie("contents-lbs", SGRawValuePointer<double>(&(sm->contents)));
632
633 index++;
634 models.push_back(sm);
635 }
636}
637
638void FGSubmodelMgr::loadSubmodels()
639{
640 SG_LOG(SG_AI, SG_DEBUG, "Submodels: Loading sub submodels");
641
642 _found_sub = false;
643
644 submodel_iterator = submodels.begin();
645
646 while (submodel_iterator != submodels.end()) {
647 const string& submodel = (*submodel_iterator)->submodel;
648 if (!submodel.empty()) {
649 //int id = (*submodel_iterator)->id;
650 SG_LOG(SG_AI, SG_DEBUG, "found path sub sub " << submodel << " index " << index << " name " << (*submodel_iterator)->name);
651
652 if ((*submodel_iterator)->sub_id == 0) {
653 (*submodel_iterator)->sub_id = index;
654 _found_sub = true;
655 bool serviceable = true;
656 setData(index, submodel, serviceable, "/ai/submodels/subsubmodel", subsubmodels);
657 }
658 }
659
660 ++submodel_iterator;
661 }
662
663 submodels.reserve(submodels.size() + subsubmodels.size());
664
665 // Add all elements from subsubmodels to submodels
666 subsubmodel_iterator = subsubmodels.begin();
667 while (subsubmodel_iterator != subsubmodels.end()) {
668 submodels.push_back(*subsubmodel_iterator);
669 ++subsubmodel_iterator;
670 }
671
672 subsubmodels.clear();
673}
674
675SGVec3d FGSubmodelMgr::getCartOffsetPos(submodel* sm) const
676{
677 // Transform to the right coordinate frame, configuration is done in
678 // either x-backward, y-right, z-up coordinates (meter),
679 // or (deprecated) x-forward, y-right, z-up coordinates (feet).
680 // computation in the simulation usual body x-forward, y-right, z-down coordinates (meters)
681 SGVec3d offset;
682 if (sm->offsets_in_meter) {
683 offset = SGVec3d(-sm->x_offset->get_value(),
684 sm->y_offset->get_value(),
685 -sm->z_offset->get_value());
686 } else {
687 offset = SGVec3d(sm->x_offset->get_value() * SG_FEET_TO_METER,
688 sm->y_offset->get_value() * SG_FEET_TO_METER,
689 -sm->z_offset->get_value() * SG_FEET_TO_METER);
690 }
691
692 // Transform the user position to the horizontal local coordinate system.
693 SGQuatd hlTrans = SGQuatd::fromLonLat(userpos);
694
695 // And postrotate the orientation of the user model wrt the horizontal
696 // local frame
697 hlTrans *= SGQuatd::fromYawPitchRollDeg(
698 IC.azimuth,
699 IC.elevation,
700 IC.roll);
701
702 // The offset converted to the usual body fixed coordinate system
703 // rotated to the earth-fixed coordinates axis
704 SGVec3d off = hlTrans.backTransform(offset);
705
706 // Add the position offset of the user model to get the geocentered position
707 return SGVec3d::fromGeod(userpos) + off;
708}
709
710void FGSubmodelMgr::setOffsetPos(submodel* sm)
711{
712 // Convert the offset geocentered position to geodetic
713 SGVec3d cartoffsetPos = getCartOffsetPos(sm);
714 SGGeodesy::SGCartToGeod(cartoffsetPos, offsetpos);
715}
716
717void FGSubmodelMgr::valueChanged(SGPropertyNode* prop)
718{
719 // REVIEW: This code has been dead for 10 years
720
721 // return; // this isn't working atm
722
723 // const char* _model_added = _model_added_node->getStringValue();
724
725 // std::basic_string <char>::size_type indexCh2b;
726
727 // string str2 = _model_added;
728 // const char *cstr2b = "multiplayer";
729 // indexCh2b = str2.find(cstr2b, 0);
730
731 // // Ignoring Ballistic Objects; there are potentially too many
732 // if (indexCh2b != string::npos ) {
733 // //cout << "Submodels: model added - " << str2 <<" read path "<< endl;
734 // //return;
735 // SGPropertyNode *a_node = fgGetNode(_model_added, true);
736 // SGPropertyNode *sub_node = a_node->getChild("sim", 0, true);
737 // SGPropertyNode_ptr path_node = sub_node->getChild("path", 0, true);
738 // SGPropertyNode_ptr callsign_node = a_node->getChild("callsign", 0, true);
739
740 // //const string& callsign = callsign_node->getStringValue();
741 // //cout << "Submodels: model added - " << callsign <<" read callsign "<< endl;
742 // }
743 // else {
744 // cout << "model added - " << str2 <<" returning " << endl;
745 // }
746}
747
748void FGSubmodelMgr::setParentNode(int id)
749{
750 const SGPropertyNode_ptr ai = fgGetNode("/ai/models", true);
751
752 for (int i = ai->nChildren() - 1; i >= -1; i--) {
753 SGPropertyNode_ptr model;
754
755 if (i < 0) {
756 // Last iteration: selected model
757 model = _selected_ac;
758 } else {
759 model = ai->getChild(i);
760 int parent_id = model->getIntValue("id");
761 if (!model->nChildren()) {
762 continue;
763 }
764 if (parent_id == id) {
765 // Save selected model for last iteration
766 _selected_ac = model;
767 break;
768 }
769 }
770 if (!model)
771 continue;
772 }
773
774 if (_selected_ac != 0) {
775 // const string name = _selected_ac->getStringValue("name");
776 IC.lat = _selected_ac->getDoubleValue("position/latitude-deg");
777 IC.lon = _selected_ac->getDoubleValue("position/longitude-deg");
778 IC.alt = _selected_ac->getDoubleValue("position/altitude-ft");
779 IC.roll = _selected_ac->getDoubleValue("orientation/roll-deg");
780 IC.elevation = _selected_ac->getDoubleValue("orientation/pitch-deg");
781 IC.azimuth = _selected_ac->getDoubleValue("orientation/true-heading-deg");
782 IC.speed = _selected_ac->getDoubleValue("velocities/true-airspeed-kt") * SG_KT_TO_FPS;
783 IC.speed_down_fps = -_selected_ac->getDoubleValue("velocities/vertical-speed-fps");
784 IC.speed_east_fps = _selected_ac->getDoubleValue("velocities/speed-east-fps");
785 IC.speed_north_fps = _selected_ac->getDoubleValue("velocities/speed-north-fps");
786 } else {
787 SG_LOG(SG_AI, SG_ALERT, "AISubmodel: parent node not found ");
788 }
789}
790
791
792// Register the subsystem.
793SGSubsystemMgr::Registrant<FGSubmodelMgr> registrantFGSubmodelMgr(
794 SGSubsystemMgr::POST_FDM);
#define i(x)
void setMass(double m)
void setSlaved(bool s)
void setImpactReportNode(const std::string &)
void setNoRoll(bool nr)
void setSubmodel(const std::string &)
void setParentNodes(const SGPropertyNode_ptr)
void setFuseRange(double f)
void setDragArea(double a)
void setExpiry(bool e)
void setStabilisation(bool val)
void setLifeRandomness(double randomness)
void setWind_from_east(double fps)
void setForcePath(const std::string &)
void setLife(double seconds)
void setWeight(double w)
void setSubID(int i)
void setContentsNode(const SGPropertyNode_ptr)
void setCollision(bool c)
void setRandom(bool r)
void setAzimuth(double az)
void setForceStabilisation(bool val)
void setElevation(double el)
void setCd(double cd)
void setCdRandomness(double randomness)
void setWind(bool val)
void setAzimuthRandomError(double error)
void setWind_from_north(double fps)
void setExternalForce(bool f)
void setImpact(bool i)
void setElevationRandomError(double error)
void setRoll(double rl)
void setBuoyancy(double fpss)
void setSpeed(double speed_KTAS)
Definition AIBase.hxx:404
void setName(const std::string &n)
Definition AIBase.hxx:491
void setLatitude(double latitude)
Definition AIBase.hxx:446
void setYoffset(double y_offset)
Definition AIBase.hxx:461
void setAltitude(double altitude_ft)
Definition AIBase.hxx:419
void setZoffset(double z_offset)
Definition AIBase.hxx:466
void setLongitude(double longitude)
Definition AIBase.hxx:441
void setPath(const char *model)
Definition AIBase.hxx:379
void setXoffset(double x_offset)
Definition AIBase.hxx:456
void setYawoffset(double z_offset)
Definition AIBase.hxx:481
void setPitchoffset(double x_offset)
Definition AIBase.hxx:471
std::vector< FGAIBasePtr > ai_list_type
Definition AIManager.hxx:76
SGPath resolve_aircraft_path(const std::string &branch) const
Given a path to an aircraft-related resource file, resolve it against the appropriate root.
Definition globals.cxx:555
void postinit() override
Definition submodel.cxx:87
void init() override
Definition submodel.cxx:53
void unbind() override
Definition submodel.cxx:111
void update(double dt) override
Definition submodel.cxx:120
void bind() override
Definition submodel.cxx:107
void shutdown() override
Definition submodel.cxx:101
const char * name
FGGlobals * globals
Definition globals.cxx:142
const double sm(5280 *foot)
SGPropertyNode * fgGetNode(const char *path, bool create)
Get a property node.
Definition proptest.cpp:27
SGSubsystemMgr::Registrant< FGSubmodelMgr > registrantFGSubmodelMgr(SGSubsystemMgr::POST_FDM)