| 1 | /* |
| 2 | * SPDX-FileName: AIBase.hxx |
| 3 | * SPDX-FileComment: abstract base class for AI objects, based on David Luff's FGAIEntity class. |
| 4 | * SPDX-FileCopyrightText: Written by David Culp, started Nov 2003 - [email protected] |
| 5 | * SPDX-License-Identifier: GPL-2.0-or-later |
| 6 | */ |
| 7 | |
| 8 | #pragma once |
| 9 | |
| 10 | #include <string> |
| 11 | #include <string_view> |
| 12 | |
| 13 | #include <osg/ref_ptr> |
| 14 | |
| 15 | #include <simgear/constants.h> |
| 16 | #include <simgear/math/sg_geodesy.hxx> |
| 17 | #include <simgear/misc/sg_path.hxx> |
| 18 | #include <simgear/props/tiedpropertylist.hxx> |
| 19 | #include <simgear/scene/model/placement.hxx> |
| 20 | #include <simgear/sg_inlines.h> |
| 21 | #include <simgear/structure/SGReferenced.hxx> |
| 22 | #include <simgear/structure/SGSharedPtr.hxx> |
| 23 | |
| 24 | |
| 25 | namespace osg { |
| 26 | class PagedLOD; |
| 27 | } |
| 28 | namespace simgear { |
| 29 | class BVHMaterial; |
| 30 | } |
| 31 | |
| 32 | class FGAIManager; |
| 33 | class FGAIFlightPlan; |
| 34 | class FGFX; |
| 35 | class FGAIModelData; // defined below |
| 36 | |
| 37 | |
| 38 | class FGAIBase : public SGReferenced |
| 39 | { |
| 40 | public: |
| 41 | enum class object_type { |
| 42 | otNull = 0, |
| 43 | otAircraft, |
| 44 | otShip, |
| 45 | otCarrier, |
| 46 | otBallistic, |
| 47 | otRocket, |
| 48 | otStorm, |
| 49 | otThermal, |
| 50 | otStatic, |
| 51 | otWingman, |
| 52 | otGroundVehicle, |
| 53 | otEscort, |
| 54 | otMultiplayer, |
| 55 | MAX_OBJECTS // Needs to be last!!! |
| 56 | }; |
| 57 | |
| 58 | FGAIBase(object_type ot, bool enableHot); |
| 59 | virtual ~FGAIBase(); |
| 60 | |
| 61 | // these depend on the order in which the models are loaded. OSG is a little vague about this, but |
| 62 | // from experimentation it seems to work best if the LODs are in the range list in terms of detail |
| 63 | // from lowest to highest |
| 64 | const int modelLowDetailIndex = 0; |
| 65 | const int modelHighDetailIndex = 1; |
| 66 | |
| 67 | virtual void readFromScenario(SGPropertyNode* scFileNode); |
| 68 | |
| 69 | enum class ModelSearchOrder { |
| 70 | DATA_ONLY, // don't search AI/ prefix at all |
| 71 | PREFER_AI, // search AI first, override other paths |
| 72 | PREFER_DATA // search data first but fall back to AI |
| 73 | }; |
| 74 | |
| 75 | virtual bool init(ModelSearchOrder searchOrder); |
| 76 | virtual void initModel(); |
| 77 | virtual void update(double dt); |
| 78 | virtual void bind(); |
| 79 | virtual void unbind(); |
| 80 | virtual void reinit() {} |
| 81 | |
| 82 | // default model radius for LOD. |
| 83 | virtual double getDefaultModelRadius() { return 20.0; } |
| 84 | |
| 85 | void updateLOD(); |
| 86 | void updateInterior(); |
| 87 | |
| 88 | void setManager(FGAIManager* mgr, SGPropertyNode* p); |
| 89 | |
| 90 | void setPath(const char* model); |
| 91 | void setPathLowres(std::string model); |
| 92 | |
| 93 | void setFallbackModelIndex(const int i); |
| 94 | void setSMPath(const std::string& p); |
| 95 | void setCallSign(const std::string&); |
| 96 | |
| 97 | void setSpeed(double speed_KTAS); |
| 98 | void setMaxSpeed(double kts); |
| 99 | |
| 100 | void setAltitude(double altitude_ft); |
| 101 | void setAltitudeAGL(double altitude_agl_ft); |
| 102 | void setHeading(double heading); |
| 103 | void setLatitude(double latitude); |
| 104 | void setLongitude(double longitude); |
| 105 | |
| 106 | void setBank(double bank); |
| 107 | void setPitch(double newpitch); |
| 108 | void setRadius(double radius); |
| 109 | |
| 110 | void setXoffset(double x_offset); |
| 111 | void setYoffset(double y_offset); |
| 112 | void setZoffset(double z_offset); |
| 113 | |
| 114 | void setPitchoffset(double x_offset); |
| 115 | void setRolloffset(double y_offset); |
| 116 | void setYawoffset(double z_offset); |
| 117 | |
| 118 | void setServiceable(bool serviceable); |
| 119 | |
| 120 | bool getDie(); |
| 121 | void setDie(bool die); |
| 122 | bool isValid() const; |
| 123 | |
| 124 | void setCollisionData(bool i, double lat, double lon, double elev); |
| 125 | void setImpactData(bool d); |
| 126 | void setImpactLat(double lat); |
| 127 | void setImpactLon(double lon); |
| 128 | void setImpactElev(double e); |
| 129 | |
| 130 | void setName(const std::string& n); |
| 131 | bool setParentNode(); |
| 132 | void setParentName(const std::string& p); |
| 133 | |
| 134 | void setCollisionLength(int range); |
| 135 | void setCollisionHeight(int height); |
| 136 | |
| 137 | void calcRangeBearing(double lat, double lon, double lat2, double lon2, |
| 138 | double& range, double& bearing) const; |
| 139 | double calcRelBearingDeg(double bearing, double heading); |
| 140 | double calcTrueBearingDeg(double bearing, double heading); |
| 141 | double calcRecipBearingDeg(double bearing); |
| 142 | |
| 143 | int getID() const; |
| 144 | int _getSubID() const; |
| 145 | |
| 146 | void setFlightPlan(std::unique_ptr<FGAIFlightPlan> f); |
| 147 | |
| 148 | SGGeod getGeodPos() const; |
| 149 | void setGeodPos(const SGGeod& pos); |
| 150 | SGVec3d getCartPosAt(const SGVec3d& off) const; |
| 151 | SGVec3d getCartPos() const; |
| 152 | |
| 153 | bool getGroundElevationM(const SGGeod& pos, double& elev, |
| 154 | const simgear::BVHMaterial** material) const; |
| 155 | |
| 156 | SGPropertyNode* getPositionFromNode(SGPropertyNode* scFileNode, const std::string& key, SGVec3d& position); |
| 157 | |
| 158 | double getTrueHeadingDeg() const { return hdg; } |
| 159 | |
| 160 | double _getCartPosX() const; |
| 161 | double _getCartPosY() const; |
| 162 | double _getCartPosZ() const; |
| 163 | |
| 164 | osg::LOD* getSceneBranch() const; |
| 165 | |
| 166 | virtual int getCollisionHeight() const; |
| 167 | virtual int getCollisionLength() const; |
| 168 | |
| 169 | /** |
| 170 | * |
| 171 | * @return true if at least one model (either low_res or high_res) is loaded |
| 172 | */ |
| 173 | bool modelLoaded() const; |
| 174 | |
| 175 | void setScenarioPath(const std::string& scenarioPath); |
| 176 | |
| 177 | protected: |
| 178 | double _elevation_m = 0.0; |
| 179 | |
| 180 | double _maxRangeInterior = 50.0; |
| 181 | |
| 182 | double _x_offset; |
| 183 | double _y_offset; |
| 184 | double _z_offset; |
| 185 | |
| 186 | double _pitch_offset; |
| 187 | double _roll_offset; |
| 188 | double _yaw_offset; |
| 189 | |
| 190 | double _max_speed = 300.0; |
| 191 | |
| 192 | int collisionHeight = 0; |
| 193 | int collisionLength = 0; |
| 194 | |
| 195 | std::string _path; |
| 196 | std::string _callsign; |
| 197 | std::string _submodel; |
| 198 | std::string _name; |
| 199 | std::string _parent; |
| 200 | std::string _scenarioPath; |
| 201 | |
| 202 | /** |
| 203 | * Tied-properties helper, record nodes which are tied for easy un-tie-ing |
| 204 | */ |
| 205 | template <typename T> |
| 206 | void tie(const char* aRelPath, const SGRawValue<T>& aRawValue) |
| 207 | { |
| 208 | _tiedProperties.Tie(props->getNode(aRelPath, true), aRawValue); |
| 209 | } |
| 210 | |
| 211 | simgear::TiedPropertyList _tiedProperties; |
| 212 | SGPropertyNode_ptr _selected_ac; |
| 213 | SGPropertyNode_ptr props; |
| 214 | SGPropertyNode_ptr trigger_node; |
| 215 | SGPropertyNode_ptr replay_time; |
| 216 | SGPropertyNode_ptr model_removed; // where to report model removal |
| 217 | FGAIManager* manager = nullptr; |
| 218 | |
| 219 | // these describe the model's actual state |
| 220 | SGGeod pos; // WGS84 lat & lon in degrees, elev above sea-level in meters |
| 221 | double hdg; // True heading in degrees |
| 222 | double roll; // degrees, left is negative |
| 223 | double pitch; // degrees, nose-down is negative |
| 224 | double speed; // knots true airspeed |
| 225 | double speed_fps = 0.0; // fps true airspeed |
| 226 | double altitude_ft; // feet above sea level |
| 227 | double vs_fps; // vertical speed |
| 228 | double speed_north_deg_sec; |
| 229 | double speed_east_deg_sec; |
| 230 | double turn_radius_ft; // turn radius ft at 15 kts rudder angle 15 degrees |
| 231 | double altitude_agl_ft = 0.0; |
| 232 | |
| 233 | double ft_per_deg_lon; |
| 234 | double ft_per_deg_lat; |
| 235 | |
| 236 | // these describe the model's desired state |
| 237 | double tgt_heading; // target heading, degrees true |
| 238 | double tgt_altitude_ft; // target altitude, *feet* above sea level |
| 239 | double tgt_speed; // target speed, KTAS |
| 240 | double tgt_roll; |
| 241 | double tgt_pitch; |
| 242 | double tgt_yaw; |
| 243 | double tgt_vs; |
| 244 | |
| 245 | // these describe radar information for the user |
| 246 | bool in_range; // true if in range of the radar, otherwise false |
| 247 | double bearing; // true bearing from user to this model |
| 248 | double elevation; // elevation in degrees from user to this model |
| 249 | double range; // range from user to this model, nm |
| 250 | double rdot; // range rate, in knots |
| 251 | double horiz_offset; // look left/right from user to me, deg |
| 252 | double vert_offset; // look up/down from user to me, deg |
| 253 | double x_shift; // value used by radar display instrument |
| 254 | double y_shift; // value used by radar display instrument |
| 255 | double rotation; // value used by radar display instrument |
| 256 | double ht_diff; // value used by radar display instrument |
| 257 | |
| 258 | std::string model_path; // Path to the 3D model |
| 259 | std::string model_path_lowres; // Path to optional low res 3D model |
| 260 | int _fallback_model_index = 0; // Index into /sim/multiplay/fallback-models[] |
| 261 | SGModelPlacement aip; |
| 262 | |
| 263 | bool delete_me; |
| 264 | bool invisible = false; |
| 265 | bool no_roll; |
| 266 | bool serviceable; |
| 267 | bool _installed = false; |
| 268 | int _subID = 0; |
| 269 | |
| 270 | double life; |
| 271 | |
| 272 | std::unique_ptr<FGAIFlightPlan> fp; |
| 273 | |
| 274 | bool _impact_reported; |
| 275 | bool _collision_reported; |
| 276 | bool _expiry_reported; |
| 277 | |
| 278 | double _impact_lat; |
| 279 | double _impact_lon; |
| 280 | double _impact_elev; |
| 281 | double _impact_hdg; |
| 282 | double _impact_pitch; |
| 283 | double _impact_roll; |
| 284 | double _impact_speed; |
| 285 | |
| 286 | ModelSearchOrder _searchOrder = ModelSearchOrder::DATA_ONLY; |
| 287 | void Transform(); |
| 288 | double UpdateRadar(FGAIManager* manager); |
| 289 | |
| 290 | void removeModel(); |
| 291 | void removeSoundFx(); |
| 292 | |
| 293 | static int _newAIModelID(); |
| 294 | |
| 295 | private: |
| 296 | int _refID; |
| 297 | object_type _otype; |
| 298 | bool _initialized = false; |
| 299 | osg::ref_ptr<osg::LOD> _model; |
| 300 | osg::ref_ptr<osg::PagedLOD> _low_res; |
| 301 | osg::ref_ptr<osg::PagedLOD> _high_res; |
| 302 | osg::ref_ptr<osg::Group> _group; |
| 303 | osg::ref_ptr<osg::PagedLOD> _interior; |
| 304 | osg::ref_ptr<FGAIModelData> _modeldata; |
| 305 | |
| 306 | SGSharedPtr<FGFX> _fx; |
| 307 | |
| 308 | std::vector<std::string> resolveModelPath(ModelSearchOrder searchOrder); |
| 309 | |
| 310 | public: |
| 311 | object_type getType(); |
| 312 | |
| 313 | virtual std::string_view getTypeString(void) const { return "null"; } |
| 314 | |
| 315 | bool isa(object_type otype); |
| 316 | |
| 317 | void _setVS_fps(double _vs); |
| 318 | void _setAltitude(double _alt); |
| 319 | void _setLongitude(double longitude); |
| 320 | void _setLatitude(double latitude); |
| 321 | void _setSubID(int s); |
| 322 | |
| 323 | double _getAltitudeAGL(SGGeod inpos, double start); |
| 324 | |
| 325 | double _getVS_fps() const; |
| 326 | double _getAltitude() const; |
| 327 | double _getLongitude() const; |
| 328 | double _getLatitude() const; |
| 329 | double _getElevationFt() const; |
| 330 | double _getRdot() const; |
| 331 | double _getH_offset() const; |
| 332 | double _getV_offset() const; |
| 333 | double _getX_shift() const; |
| 334 | double _getY_shift() const; |
| 335 | double _getRotation() const; |
| 336 | double _getSpeed() const; |
| 337 | double _getRoll() const; |
| 338 | double _getPitch() const; |
| 339 | double _getHeading() const; |
| 340 | double _get_speed_east_fps() const; |
| 341 | double _get_speed_north_fps() const; |
| 342 | double _get_SubPath() const; |
| 343 | double _getImpactLat() const; |
| 344 | double _getImpactLon() const; |
| 345 | double _getImpactElevFt() const; |
| 346 | double _getImpactHdg() const; |
| 347 | double _getImpactPitch() const; |
| 348 | double _getImpactRoll() const; |
| 349 | double _getImpactSpeed() const; |
| 350 | double _getXOffset() const; |
| 351 | double _getYOffset() const; |
| 352 | double _getZOffset() const; |
| 353 | |
| 354 | bool _getServiceable() const; |
| 355 | bool _getFirstTime() const; |
| 356 | bool _getImpact(); |
| 357 | bool _getImpactData(); |
| 358 | bool _getCollisionData(); |
| 359 | bool _getExpiryData(); |
| 360 | |
| 361 | SGPropertyNode* _getProps() const; |
| 362 | |
| 363 | const char* _getPath() const; |
| 364 | const char* _getSMPath() const; |
| 365 | const char* _getCallsign() const; |
| 366 | const char* _getTriggerNode() const; |
| 367 | const char* _getName() const; |
| 368 | const char* _getSubmodel() const; |
| 369 | int _getFallbackModelIndex() const; |
| 370 | |
| 371 | static const double e; |
| 372 | static const double lbs_to_slugs; |
| 373 | |
| 374 | double _getRange() const { return range; } |
| 375 | double _getBearing() const { return bearing; } |
| 376 | |
| 377 | static bool _isNight(); |
| 378 | |
| 379 | const std::string& getCallSign() const { return _callsign; } |
| 380 | ModelSearchOrder getSearchOrder() const { return _searchOrder; } |
| 381 | }; |
| 382 | |
| 383 | typedef SGSharedPtr<FGAIBase> FGAIBasePtr; |
| 384 | |
| 385 | inline void FGAIBase::setManager(FGAIManager* mgr, SGPropertyNode* p) |
| 386 | { |
| 387 | manager = mgr; |
| 388 | props = p; |
| 389 | } |
| 390 | |
| 391 | inline void FGAIBase::setPath(const char* model) |
| 392 | { |
| 393 | model_path.append(model); |
| 394 | } |
| 395 | |
| 396 | inline void FGAIBase::setPathLowres(std::string model) |
| 397 | { |
| 398 | model_path_lowres.append(model); |
| 399 | } |
| 400 | |
| 401 | inline void FGAIBase::setFallbackModelIndex(const int i) |
| 402 | { |
| 403 | _fallback_model_index = i; |
| 404 | } |
| 405 | |
| 406 | inline void FGAIBase::setSMPath(const std::string& p) |
| 407 | { |
| 408 | _path = p; |
| 409 | } |
| 410 | |
| 411 | inline void FGAIBase::setServiceable(bool s) |
| 412 | { |
| 413 | serviceable = s; |
| 414 | } |
| 415 | |
| 416 | inline void FGAIBase::setSpeed(double speed_KTAS) |
| 417 | { |
| 418 | speed = tgt_speed = speed_KTAS; |
| 419 | } |
| 420 | |
| 421 | inline void FGAIBase::setRadius(double radius) |
| 422 | { |
| 423 | turn_radius_ft = radius; |
| 424 | } |
| 425 | |
| 426 | inline void FGAIBase::setHeading(double heading) |
| 427 | { |
| 428 | hdg = tgt_heading = heading; |
| 429 | } |
| 430 | |
| 431 | inline void FGAIBase::setAltitude(double alt_ft) |
| 432 | { |
| 433 | altitude_ft = tgt_altitude_ft = alt_ft; |
| 434 | pos.setElevationFt(altitude_ft); |
| 435 | } |
| 436 | |
| 437 | inline void FGAIBase::setAltitudeAGL(double alt_ft) |
| 438 | { |
| 439 | altitude_agl_ft = alt_ft; |
| 440 | } |
| 441 | |
| 442 | inline void FGAIBase::setBank(double bank) |
| 443 | { |
| 444 | roll = tgt_roll = bank; |
| 445 | no_roll = false; |
| 446 | } |
| 447 | |
| 448 | inline void FGAIBase::setPitch(double newpitch) |
| 449 | { |
| 450 | pitch = tgt_pitch = newpitch; |
| 451 | } |
| 452 | |
| 453 | inline void FGAIBase::setLongitude(double longitude) |
| 454 | { |
| 455 | pos.setLongitudeDeg(longitude); |
| 456 | } |
| 457 | |
| 458 | inline void FGAIBase::setLatitude(double latitude) |
| 459 | { |
| 460 | pos.setLatitudeDeg(latitude); |
| 461 | } |
| 462 | |
| 463 | inline void FGAIBase::setCallSign(const std::string& s) |
| 464 | { |
| 465 | _callsign = s; |
| 466 | } |
| 467 | |
| 468 | inline void FGAIBase::setXoffset(double x) |
| 469 | { |
| 470 | _x_offset = x; |
| 471 | } |
| 472 | |
| 473 | inline void FGAIBase::setYoffset(double y) |
| 474 | { |
| 475 | _y_offset = y; |
| 476 | } |
| 477 | |
| 478 | inline void FGAIBase::setZoffset(double z) |
| 479 | { |
| 480 | _z_offset = z; |
| 481 | } |
| 482 | |
| 483 | inline void FGAIBase::setPitchoffset(double p) |
| 484 | { |
| 485 | _pitch_offset = p; |
| 486 | } |
| 487 | |
| 488 | inline void FGAIBase::setRolloffset(double r) |
| 489 | { |
| 490 | _roll_offset = r; |
| 491 | } |
| 492 | |
| 493 | inline void FGAIBase::setYawoffset(double y) |
| 494 | { |
| 495 | _yaw_offset = y; |
| 496 | } |
| 497 | |
| 498 | inline void FGAIBase::setParentName(const std::string& p) |
| 499 | { |
| 500 | _parent = p; |
| 501 | } |
| 502 | |
| 503 | inline void FGAIBase::setName(const std::string& n) |
| 504 | { |
| 505 | _name = n; |
| 506 | } |
| 507 | |
| 508 | inline void FGAIBase::setCollisionLength(int length) |
| 509 | { |
| 510 | collisionLength = length; |
| 511 | } |
| 512 | |
| 513 | inline void FGAIBase::setCollisionHeight(int height) |
| 514 | { |
| 515 | collisionHeight = height; |
| 516 | } |
| 517 | |
| 518 | inline void FGAIBase::setDie(bool die) { delete_me = die; } |
| 519 | |
| 520 | inline bool FGAIBase::getDie() { return delete_me; } |
| 521 | |
| 522 | inline FGAIBase::object_type FGAIBase::getType() { return _otype; } |
| 523 | |
| 524 | inline void FGAIBase::calcRangeBearing(double lat, double lon, double lat2, double lon2, |
| 525 | double& range, double& bearing) const |
| 526 | { |
| 527 | // calculate the bearing and range of the second pos from the first |
| 528 | double az2, distance; |
| 529 | geo_inverse_wgs_84(lat, lon, lat2, lon2, &bearing, &az2, &distance); |
| 530 | range = distance * SG_METER_TO_NM; |
| 531 | } |
| 532 | |
| 533 | inline double FGAIBase::calcRelBearingDeg(double bearing, double heading) |
| 534 | { |
| 535 | double angle = bearing - heading; |
| 536 | SG_NORMALIZE_RANGE(angle, -180.0, 180.0); |
| 537 | return angle; |
| 538 | } |
| 539 | |
| 540 | inline double FGAIBase::calcTrueBearingDeg(double bearing, double heading) |
| 541 | { |
| 542 | double angle = bearing + heading; |
| 543 | SG_NORMALIZE_RANGE(angle, 0.0, 360.0); |
| 544 | return angle; |
| 545 | } |
| 546 | |
| 547 | inline double FGAIBase::calcRecipBearingDeg(double bearing) |
| 548 | { |
| 549 | double angle = bearing - 180; |
| 550 | SG_NORMALIZE_RANGE(angle, 0.0, 360.0); |
| 551 | return angle; |
| 552 | } |
| 553 | |
| 554 | inline void FGAIBase::setMaxSpeed(double m) |
| 555 | { |
| 556 | _max_speed = m; |
| 557 | } |
| 558 | |
| 559 | /* |
| 560 | * Default height and lengths for AI submodel collision detection. |
| 561 | * The difference in height is used first and then the range must be within |
| 562 | * the value specifed in the length field. This effective chops the top and |
| 563 | * bottom off the circle - but does not take into account the orientation of the |
| 564 | * AI model; so this algorithm is fast but fairly inaccurate. |
| 565 | * |
| 566 | * Default values: |
| 567 | * +---------------+-------------+------------+ |
| 568 | * | Type | Height(m) | Length(m) | |
| 569 | * +---------------+-------------+------------+ |
| 570 | * | Null | 0 | 0 | |
| 571 | * | Aircraft | 50 | 100 | |
| 572 | * | Ship | 100 | 200 | |
| 573 | * | Carrier | 250 | 750 | |
| 574 | * | Ballistic | 0 | 0 | |
| 575 | * | Rocket | 100 | 50 | |
| 576 | * | Storm | 0 | 0 | |
| 577 | * | Thermal | 0 | 0 | |
| 578 | * | Static | 50 | 200 | |
| 579 | * | Wingman | 50 | 100 | |
| 580 | * | GroundVehicle | 20 | 40 | |
| 581 | * | Escort | 100 | 200 | |
| 582 | * | Multiplayer | 50 | 100 | |
| 583 | * +---------------+-------------+------------+ |
| 584 | */ |
| 585 | const static double tgt_ht[] = {0, 50, 100, 250, 0, 100, 0, 0, 50, 50, 20, 100, 50}; |
| 586 | const static double tgt_length[] = {0, 100, 200, 750, 0, 50, 0, 0, 200, 100, 40, 200, 100}; |
| 587 | |
| 588 | inline int FGAIBase::getCollisionHeight() const |
| 589 | { |
| 590 | if (collisionHeight == 0) |
| 591 | return tgt_ht[static_cast<int>(_otype)]; |
| 592 | |
| 593 | return collisionHeight; |
| 594 | } |
| 595 | inline int FGAIBase::getCollisionLength() const |
| 596 | { |
| 597 | if (collisionLength == 0) |
| 598 | return tgt_length[static_cast<int>(_otype)]; |
| 599 | |
| 600 | return collisionLength; |
| 601 | } |