| 1 | /* |
| 2 | * SPDX-FileName: AIManager.hxx |
| 3 | * SPDX-FileComment: a global management type for AI objects, based on David Luff's AIMgr |
| 4 | * SPDX-FileCopyrightText: Copyright (C) 2002 David C Luff - [email protected] |
| 5 | * SPDX-License-Identifier: GPL-2.0-or-later |
| 6 | */ |
| 7 | |
| 8 | #pragma once |
| 9 | |
| 10 | #include <list> |
| 11 | #include <map> |
| 12 | |
| 13 | #include <simgear/math/SGVec3.hxx> |
| 14 | #include <simgear/misc/sg_path.hxx> |
| 15 | #include <simgear/props/props.hxx> |
| 16 | #include <simgear/structure/SGSharedPtr.hxx> |
| 17 | #include <simgear/structure/subsystem_mgr.hxx> |
| 18 | |
| 19 | class FGAIBase; |
| 20 | class FGAIThermal; |
| 21 | class FGAIAircraft; |
| 22 | |
| 23 | typedef SGSharedPtr<FGAIBase> FGAIBasePtr; |
| 24 | |
| 25 | class FGAIManager : public SGSubsystem |
| 26 | { |
| 27 | public: |
| 28 | FGAIManager(); |
| 29 | virtual ~FGAIManager(); |
| 30 | |
| 31 | // Subsystem API. |
| 32 | void bind() override; |
| 33 | void init() override; |
| 34 | void postinit() override; |
| 35 | void reinit() override; |
| 36 | void shutdown() override; |
| 37 | void unbind() override; |
| 38 | void update(double dt) override; |
| 39 | |
| 40 | // Subsystem identification. |
| 41 | static const char* staticSubsystemClassId() { return "ai-model"; } |
| 42 | |
| 43 | void updateLOD(SGPropertyNode* node); |
| 44 | void attach(const SGSharedPtr<FGAIBase>& model); |
| 45 | |
| 46 | const FGAIBase* calcCollision(double alt, double lat, double lon, double fuse_range); |
| 47 | |
| 48 | inline double get_user_heading() const { return user_heading; } |
| 49 | inline double get_user_pitch() const { return user_pitch; } |
| 50 | inline double get_user_speed() const { return user_speed; } |
| 51 | inline double get_wind_from_east() const { return wind_from_east; } |
| 52 | inline double get_wind_from_north() const { return wind_from_north; } |
| 53 | inline double get_user_roll() const { return user_roll; } |
| 54 | inline double get_user_agl() const { return user_altitude_agl; } |
| 55 | |
| 56 | bool loadScenario(const std::string& id); |
| 57 | |
| 58 | /** |
| 59 | * Static helper to register scenarios. This has to happen very early because |
| 60 | * we need carrier scenarios to start the position-init process for a |
| 61 | * carrier start. |
| 62 | */ |
| 63 | static void registerScenarios(SGPropertyNode_ptr root = {}); |
| 64 | static SGPropertyNode_ptr registerScenarioFile(SGPropertyNode_ptr root, const SGPath& p); |
| 65 | static SGPropertyNode_ptr loadScenarioFile(const std::string& id, SGPath& outPath); |
| 66 | |
| 67 | FGAIBasePtr addObject(const SGPropertyNode* definition); |
| 68 | bool isVisible(const SGGeod& pos) const; |
| 69 | |
| 70 | /** |
| 71 | * @brief given a reference to an /ai/models/<foo>[n] node, return the |
| 72 | * corresponding AIObject implementation, or NULL. |
| 73 | */ |
| 74 | FGAIBasePtr getObjectFromProperty(const SGPropertyNode* aProp) const; |
| 75 | |
| 76 | typedef std::vector<FGAIBasePtr> ai_list_type; |
| 77 | const ai_list_type& get_ai_list() const |
| 78 | { |
| 79 | return ai_list; |
| 80 | } |
| 81 | |
| 82 | double calcRangeFt(const SGVec3d& aCartPos, const FGAIBase* aObject) const; |
| 83 | |
| 84 | /** |
| 85 | * @brief Retrieve the representation of the user's aircraft in the AI manager |
| 86 | * the position and velocity of this object are slaved to the user's aircraft, |
| 87 | * so that AI systems such as parking and ATC can see the user and process / |
| 88 | * avoid correctly. |
| 89 | */ |
| 90 | FGAIAircraft* getUserAircraft() const; |
| 91 | |
| 92 | bool isRadarEnabled() const |
| 93 | { |
| 94 | return _radarEnabled; |
| 95 | } |
| 96 | |
| 97 | bool enableRadarDebug() const |
| 98 | { |
| 99 | return _radarDebugMode; |
| 100 | } |
| 101 | |
| 102 | double radarRangeM() const |
| 103 | { |
| 104 | return _radarRangeM; |
| 105 | } |
| 106 | |
| 107 | private: |
| 108 | // FGSubmodelMgr is a friend for access to the AI_list |
| 109 | friend class FGSubmodelMgr; |
| 110 | |
| 111 | // A list of pointers to AI objects |
| 112 | typedef ai_list_type::iterator ai_list_iterator; |
| 113 | typedef ai_list_type::const_iterator ai_list_const_iterator; |
| 114 | |
| 115 | int getNumAiObjects() const; |
| 116 | |
| 117 | void removeDeadItem(FGAIBase* base); |
| 118 | |
| 119 | // Returns true on success, e.g. returns false if scenario is already loaded. |
| 120 | bool loadScenarioCommand(const SGPropertyNode* args, SGPropertyNode* root); |
| 121 | |
| 122 | bool unloadScenarioCommand(const SGPropertyNode* args, SGPropertyNode* root); |
| 123 | bool addObjectCommand(const SGPropertyNode* arg, const SGPropertyNode* root); |
| 124 | bool removeObjectCommand(const SGPropertyNode* arg, const SGPropertyNode* root); |
| 125 | |
| 126 | bool removeObject(const SGPropertyNode* args); |
| 127 | bool unloadScenario(const std::string& filename); |
| 128 | void unloadAllScenarios(); |
| 129 | |
| 130 | SGPropertyNode_ptr root; |
| 131 | SGPropertyNode_ptr enabled; |
| 132 | SGPropertyNode_ptr thermal_lift_node; |
| 133 | SGPropertyNode_ptr user_altitude_agl_node; |
| 134 | SGPropertyNode_ptr user_speed_node; |
| 135 | SGPropertyNode_ptr wind_from_east_node; |
| 136 | SGPropertyNode_ptr wind_from_north_node; |
| 137 | SGPropertyNode_ptr _environmentVisiblity; |
| 138 | SGPropertyNode_ptr _groundSpeedKts_node; |
| 139 | |
| 140 | ai_list_type ai_list; |
| 141 | |
| 142 | double user_altitude_agl = 0.0; |
| 143 | double user_heading = 0.0; |
| 144 | double user_pitch = 0.0; |
| 145 | double user_roll = 0.0; |
| 146 | double user_speed = 0.0; |
| 147 | double wind_from_east = 0.0; |
| 148 | double wind_from_north = 0.0; |
| 149 | |
| 150 | void fetchUserState(double dt); |
| 151 | |
| 152 | // used by thermals |
| 153 | double range_nearest = 0.0; |
| 154 | double strength = 0.0; |
| 155 | void processThermal(double dt, FGAIThermal* thermal); |
| 156 | |
| 157 | SGPropertyChangeCallback<FGAIManager> cb_ai_bare; |
| 158 | SGPropertyChangeCallback<FGAIManager> cb_ai_detailed; |
| 159 | SGPropertyChangeCallback<FGAIManager> cb_interior; |
| 160 | |
| 161 | class Scenario; |
| 162 | typedef std::map<std::string, Scenario*> ScenarioDict; |
| 163 | ScenarioDict _scenarios; |
| 164 | |
| 165 | SGSharedPtr<FGAIAircraft> _userAircraft; |
| 166 | |
| 167 | SGPropertyNode_ptr _simRadarControl, |
| 168 | _radarRangeNode, _radarDebugNode; |
| 169 | bool _radarEnabled = true, |
| 170 | _radarDebugMode = false; |
| 171 | double _radarRangeM = 0.0; |
| 172 | }; |