FlightGear next
AIManager.hxx
Go to the documentation of this file.
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 - david.luff@nottingham.ac.uk
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
19class FGAIBase;
20class FGAIThermal;
21class FGAIAircraft;
22
23typedef SGSharedPtr<FGAIBase> FGAIBasePtr;
24
25class FGAIManager : public SGSubsystem
26{
27public:
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
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
74 FGAIBasePtr getObjectFromProperty(const SGPropertyNode* aProp) const;
75
76 typedef std::vector<FGAIBasePtr> ai_list_type;
78 {
79 return ai_list;
80 }
81
82 double calcRangeFt(const SGVec3d& aCartPos, const FGAIBase* aObject) const;
83
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
107private:
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};
SGSharedPtr< FGAIBase > FGAIBasePtr
Definition AIBase.hxx:371
SGSharedPtr< FGAIBase > FGAIBasePtr
Definition AIManager.hxx:23
#define p(x)
void reinit() override
static SGPropertyNode_ptr loadScenarioFile(const std::string &id, SGPath &outPath)
void unbind() override
bool isRadarEnabled() const
Definition AIManager.hxx:92
FGAIBasePtr getObjectFromProperty(const SGPropertyNode *aProp) const
given a reference to an /ai/models/<foo>[n] node, return the corresponding AIObject implementation,...
static SGPropertyNode_ptr registerScenarioFile(SGPropertyNode_ptr root, const SGPath &p)
void postinit() override
const ai_list_type & get_ai_list() const
Definition AIManager.hxx:77
bool loadScenario(const std::string &id)
std::vector< FGAIBasePtr > ai_list_type
Definition AIManager.hxx:76
double get_user_pitch() const
Definition AIManager.hxx:49
double get_user_agl() const
Definition AIManager.hxx:54
void updateLOD(SGPropertyNode *node)
update LOD settings of all AI/MP models
FGAIBasePtr addObject(const SGPropertyNode *definition)
double get_wind_from_east() const
Definition AIManager.hxx:51
void update(double dt) override
virtual ~FGAIManager()
void init() override
const FGAIBase * calcCollision(double alt, double lat, double lon, double fuse_range)
double get_user_speed() const
Definition AIManager.hxx:50
bool isVisible(const SGGeod &pos) const
static const char * staticSubsystemClassId()
Definition AIManager.hxx:41
bool enableRadarDebug() const
Definition AIManager.hxx:97
double get_wind_from_north() const
Definition AIManager.hxx:52
double radarRangeM() const
double calcRangeFt(const SGVec3d &aCartPos, const FGAIBase *aObject) const
double get_user_heading() const
Definition AIManager.hxx:48
friend class FGSubmodelMgr
double get_user_roll() const
Definition AIManager.hxx:53
void attach(const SGSharedPtr< FGAIBase > &model)
static void registerScenarios(SGPropertyNode_ptr root={})
Static helper to register scenarios.
void bind() override
FGAIAircraft * getUserAircraft() const
Retrieve the representation of the user's aircraft in the AI manager the position and velocity of thi...
void shutdown() override