VR-Engage  2.3
Loading...
Searching...
No Matches
weaponsStatusController.h
Go to the documentation of this file.
1/*******************************************************************************
2** Copyright (c) 2025 MAK Technologies
3** All rights reserved.
4*******************************************************************************/
5
6//! \file weaponsStatusController.h
7//! \ingroup vreVrfmodel
8//! \brief Controller for managing weapon systems status and selection
9//!
10//! This file defines the DtWeaponsStatusController class which manages
11//! weapon systems status information, weapon selection, and handling of
12//! weapon-related commands. It maintains state properties for weapon status
13//! and provides interfaces for cycling through available weapons.
14
15#pragma once
16
17#include "vreVrfmodel/export.h"
19
22
23namespace makVre
24{
25//! \brief Type identifier for the weapon status controller component
26const char* DtWeaponStatusControllerType = "weapon-status-controller";
27
28//! \brief Controller for managing weapon systems status and selection
29//!
30//! DtWeaponsStatusController monitors and updates weapon systems status information
31//! for an entity. It manages the SelectedWeapon and WeaponsStatus state properties,
32//! and handles "CycleWeapon" commands to change the currently selected weapon system.
33//! The controller maintains a map of weapon configurations, tracks ammunition levels,
34//! and publishes this information for use by the VR-Engage interface.
35//!
36//! This component is responsible for determining valid weapon selections based on
37//! the availability of ammunition and updating state properties to reflect current
38//! weapon status for the entity.
39//!
40//! Uses Descriptor Type: DtWeaponsStatusControllerDescriptor
43{
44public:
45 //! \brief Constructor
46 //! \param name The name of this component
47 //! \param owner The local object that owns this component
48 //! \param simManager The simulation services manager
49 //! \param desc Component descriptor with configuration parameters
50 //! \param parentRegistry Optional parent registry for reader/writer functionality
51 //!
52 //! Creates a new weapon status controller component with the specified parameters.
53 //! Initializes the weapon status tracking system.
54 DtWeaponsStatusController(const DtString& name, DtLocalObject* owner, DtSimulationServices* simManager,
55 DtComponentDescriptor* desc = 0, DtReaderWriterRegistry* parentRegistry = 0);
56
57 //! \brief Virtual destructor
58 //!
59 //! Cleans up resources used by the weapon status controller component.
60 virtual ~DtWeaponsStatusController() override;
61
62 //! \brief Initializes the controller before the first tick
63 //! \return None
64 //!
65 //! Sets up state properties for weapon status tracking. This initialization
66 //! relies on other systems already being initialized, so it can't be done
67 //! in the init() method itself. Initializes weapon configurations and
68 //! establishes initial weapon selection state.
69 //! Restores incoming joystick state after weapon configuration initialization.
70 virtual void preFirstTickInit() override;
71
72 //! \brief Initializes the controller
73 //! \return True if initialization was successful, false otherwise
74 //!
75 //! Initializes the weapon status controller, adding a setRestore callback
76 //! for reinitializing state properties when the entity is restored.
77 //! Also registers message handlers for weapon configuration requests.
78 virtual bool init() override;
79
80 //! \brief Updates the controller state each simulation frame
81 //!
82 //! Updates weapon status properties each frame, tracking ammunition levels
83 //! and selected weapon state. Handles both player-controlled and non-player
84 //! controlled states, with different update behaviors for each.
85 virtual void tick() override;
86
87 //! \brief Gets the type identifier for this component
88 //! \return String identifying the component type (DtWeaponStatusControllerType)
89 //!
90 //! Implements the DtSimComponent::type() method to return the
91 //! type identifier for this component.
92 virtual const char* type() const override;
93
94 //! \brief Factory method to create a new instance of this component
95 //! \param name The name for the new component
96 //! \param owner The local object that will own this component
97 //! \param simManager The simulation services manager
98 //! \param desc Optional component descriptor
99 //! \param parentRegistry Optional parent registry for reader/writer functionality
100 //! \return Pointer to the newly created component
101 //!
102 //! Static factory method used by the component creation system to instantiate
103 //! new instances of this component type.
104 static DtSimComponent* creator(const DtString& name, DtLocalObject* owner, DtSimulationServices* simManager,
105 DtComponentDescriptor* desc = 0, DtReaderWriterRegistry* parentRegistry = 0);
106
107 //! \brief Processes joystick input for weapon cycling
108 //! \param functionGroup The function group identifier for the input
109 //! \param function The specific function name for the input
110 //! \param value The input value (typically in range [-1.0, 1.0])
111 //! \param repeat Flag indicating whether the function should repeat
112 //!
113 //! Handles the "CycleWeapon" joystick function by selecting the next
114 //! weapon that has ammunition, or "None" if no weapons with ammunition are available.
115 //! This allows players to quickly cycle through available weapons using joystick inputs.
117 const DtString& functionGroup, const DtString& function, double value, bool repeat) override;
118
119 //! \brief Gets the count of a specific munition resource
120 //! \param munitionName The name of the munition resource to count
121 //! \return The number of units of the specified munition available
122 //!
123 //! Retrieves the current count of a specific munition resource by name.
124 //! Returns 0 if the resource is not found or depleted.
125 virtual int munitionResourceCount(const DtEntityType& munitionName) const;
126
127protected:
128 //! \brief Initializes weapon configurations from entity's weapon systems
129 //! \return True if weapon configurations were successfully initialized, false otherwise
130 //!
131 //! Sets up the weapon configuration map from the entity's weapon systems.
132 //! Identifies and catalogs all weapon systems on the entity, setting up
133 //! the data structures needed to track their status.
134 virtual void initWeaponConfigs();
135
136 //! \brief Adds a weapon configuration for a specific weapon system
137 //!
138 //! \param system The component system representing a weapon system.
139 //! \param postfix Suffix used to select indexed weapon-system attributes.
140 //!
141 //! Adds a weapon configuration for the specified weapon system to the
142 //! weapon configuration map. When the weapon interface has no display name,
143 //! resolves a unique VR-Forces weapon ID for the system and munition type to
144 //! deconflict configurations. Duplicate weapon categories are reported with
145 //! the required category-name override and automatically assigned a numeric
146 //! suffix so that all configurations remain selectable.
147 virtual void addWeaponConfig(DtComponentSystem* system, const std::string& postfix);
148
149 //! \brief Finds a unique VR-Forces weapon ID for a component system.
150 //!
151 //! Returns the system's sole weapon ID when it owns exactly one. When a
152 //! system owns multiple weapon IDs, uses the munition type to identify one.
153 //!
154 //! \param system Component system that scopes the search.
155 //! \param munitionType Munition type used to distinguish multiple weapon IDs.
156 //! \param weaponIdOut Receives the matching weapon ID when the result is true.
157 //! \return True when a unique weapon ID is found; otherwise false.
159 DtComponentSystem* system, const DtEntityType& munitionType, DtString& weaponIdOut) const;
160
161 virtual void updateAmmo();
162
163 //! \brief Sends a weapon configuration response message
164 //!
165 //! Constructs and sends a response message containing detailed information
166 //! about all weapon configurations on the entity. This includes weapon categories,
167 //! ammunition types, ranges, and other properties.
168 virtual void sendWeaponConfig();
169
170 virtual void sendAmmoUpdate(const std::string& weapon, const DtEntityType& munition, int count);
171
172 //! \brief Handles weapon configuration request messages
173 //! \param msg The message containing the weapon configuration request
174 //! \return Message processing result indicating success or failure
175 //!
176 //! Processes messages requesting information about the entity's weapon
177 //! configurations. Triggers the sending of a weapon configuration response.
179
180 //! \brief Handles weapon selection messages
181 //! \param msg The message containing the weapon selection request
182 //! \return Message processing result indicating success or failure
183 //!
184 //! Processes messages requesting the selection of a specific weapon category.
185 //! Updates the selected weapon state property and related weapon status information.
187
188 //! \brief Static callback function for set restore messages
189 //! \param msg The simulation message containing restore information
190 //! \param usrData User data pointer, typically pointing to an instance of this class
191 //!
192 //! Static function registered as a callback to handle set restore messages.
193 //! Forwards the message to the appropriate instance's processSetRestore method.
194 static void setRestoreCallback(DtSimMessage* msg, void* usrData);
195
196 //! \brief Reinitializes state properties during entity restoration
197 //! \param msg The simulation message containing restore information
198 //!
199 //! Reinitializes weapon status state properties when a set restore operation
200 //! is executed. Ensures that weapon configurations and status information
201 //! are properly restored after entity restoration.
202 virtual void processSetRestore(DtSimMessage* msg);
203
204 //! \brief Finds the next weapon system category to select
205 //! \return String containing the category name of the next weapon system
206 //!
207 //! Finds the category for the next weapon system to select, using the order
208 //! defined in the descriptor if it exists, or falling back on the order of
209 //! elements in the state property. Only considers weapon systems that have
210 //! ammunition available.
211 virtual bool selectNextWeapon();
212
213 virtual bool selectWeapon(const DtString& selection);
214
215 //! \brief Updates range information for the selected weapon system
216 //!
217 //! Clears the existing range items in state properties, then adds minimum,
218 //! optimal, and maximum range values for the selected weapon system.
219 //! This information is used by the UI to display weapon range indicators.
220 virtual void updateRangeItems();
221
222 //! \brief Handles joystick connection events
223 //!
224 //! Required by the DtJoystickControllerInterface but implemented as a no-op
225 //! since no special handling is needed when a joystick is connected.
226 virtual void joystickConnected() override;
227
228 //! \brief Handles joystick disconnection events
229 //!
230 //! Required by the DtJoystickControllerInterface but implemented as a no-op
231 //! since no special handling is needed when a joystick is disconnected.
232 virtual void joystickDisconnected() override;
233
234 DtRwPropertiesMap& getWeaponsStatusMap();
235
236 DtRwPropertyString& getCurrentWeapon();
237
238 //! \brief Replenishes fuel and ammunition resources
239 //!
240 //! Restores all fuel and ammunition resources to their maximum levels.
241 //! Used during entity restoration or for gameplay mechanics that
242 //! provide resource replenishment.
243 virtual void replenishFuelAndAmmo();
244
245protected:
246 //! \brief Flag tracking player control state
247 //!
248 //! Used to track when a player first takes control of the entity, in order
249 //! to trigger a full update of weapon status property values. Helps ensure
250 //! that the interface shows current values when a player first engages control.
252
253 //! \brief Flag indicating if entity has weapon systems
254 //!
255 //! True if the ownship entity has weapon systems that need to be updated.
256 //! Used to optimize performance by skipping weapon updates for entities
257 //! that don't have weapons.
259
260 //! \brief Ordered list of weapon categories for cycling
261 //!
262 //! Defines the order to cycle through weapon categories. Either specified in
263 //! the descriptor or determined by the order the weapon systems are defined
264 //! in the entity file. Used when cycling through weapons with joystick inputs.
265 std::vector<std::string> myWeaponCycleOrder;
266
267 std::vector<WeaponConfigResponseMessage::WeaponConfig> myWeaponConfigCache;
268 std::map<DtEntityType, unsigned int> myAmmoCache;
269
270private:
271 //! \brief Default constructor (not implemented)
272 //!
273 //! Default constructor is private and not implemented to prevent
274 //! creation of instances without proper initialization.
276
277 //! \brief Copy constructor (not implemented)
278 //!
279 //! Copy constructor is private and not implemented to prevent
280 //! copy construction.
282
283 //! \brief Assignment operator (not implemented)
284 //! \return Reference to this object
285 //!
286 //! Assignment operator is private and not implemented to prevent assignment.
288};
289
290} // namespace makVre
DtVreJoystickControllerInterface(DtSimComponent *component, DtLocalObject *owner, DtSimulationServices *simManager, DtComponentDescriptor *desc)
Constructs an interface alongside an existing component base.
Abstract base class for all VREngage messages.
Definition vreMessage.h:50
DtVreSimComponent(const DtString &name, DtLocalObject *owner, DtSimulationServices *simManager, DtComponentDescriptor *desc=0, DtReaderWriterRegistry *parentRegistry=0)
Definition vreSimComponent.h:63
virtual bool init() override
Initializes the controller.
virtual void initWeaponConfigs()
Initializes weapon configurations from entity's weapon systems.
virtual DtVreMessageResult handleWeaponConfigSelectWeapon(DtVreMessage *msg)
Handles weapon selection messages.
virtual void calcAndSetPortValues(const DtString &functionGroup, const DtString &function, double value, bool repeat) override
Processes joystick input for weapon cycling.
virtual bool selectNextWeapon()
Finds the next weapon system category to select.
virtual void addWeaponConfig(DtComponentSystem *system, const std::string &postfix)
Adds a weapon configuration for a specific weapon system.
virtual bool selectWeapon(const DtString &selection)
virtual void sendWeaponConfig()
Sends a weapon configuration response message.
bool myWasPlayerControlled
Flag tracking player control state.
Definition weaponsStatusController.h:251
DtRwPropertyString & getCurrentWeapon()
virtual void joystickConnected() override
Handles joystick connection events.
virtual void processSetRestore(DtSimMessage *msg)
Reinitializes state properties during entity restoration.
virtual void preFirstTickInit() override
Initializes the controller before the first tick.
DtRwPropertiesMap & getWeaponsStatusMap()
std::vector< WeaponConfigResponseMessage::WeaponConfig > myWeaponConfigCache
Definition weaponsStatusController.h:267
bool myHasWeaponSystems
Flag indicating if entity has weapon systems.
Definition weaponsStatusController.h:258
DtWeaponsStatusController()
Default constructor (not implemented)
virtual void replenishFuelAndAmmo()
Replenishes fuel and ammunition resources.
virtual ~DtWeaponsStatusController() override
Virtual destructor.
static void setRestoreCallback(DtSimMessage *msg, void *usrData)
Static callback function for set restore messages.
virtual void joystickDisconnected() override
Handles joystick disconnection events.
DtWeaponsStatusController(const DtWeaponsStatusController &orig)
Copy constructor (not implemented)
std::map< DtEntityType, unsigned int > myAmmoCache
Definition weaponsStatusController.h:268
DtWeaponsStatusController(const DtString &name, DtLocalObject *owner, DtSimulationServices *simManager, DtComponentDescriptor *desc=0, DtReaderWriterRegistry *parentRegistry=0)
Constructor.
static DtSimComponent * creator(const DtString &name, DtLocalObject *owner, DtSimulationServices *simManager, DtComponentDescriptor *desc=0, DtReaderWriterRegistry *parentRegistry=0)
Factory method to create a new instance of this component.
virtual void updateRangeItems()
Updates range information for the selected weapon system.
virtual void sendAmmoUpdate(const std::string &weapon, const DtEntityType &munition, int count)
bool findWeaponId(DtComponentSystem *system, const DtEntityType &munitionType, DtString &weaponIdOut) const
Finds a unique VR-Forces weapon ID for a component system.
const DtWeaponsStatusController & operator=(const DtWeaponsStatusController &orig)
Assignment operator (not implemented)
virtual const char * type() const override
Gets the type identifier for this component.
virtual DtVreMessageResult handleWeaponConfigRequestMessage(DtVreMessage *msg)
Handles weapon configuration request messages.
virtual int munitionResourceCount(const DtEntityType &munitionName) const
Gets the count of a specific munition resource.
virtual void tick() override
Updates the controller state each simulation frame.
std::vector< std::string > myWeaponCycleOrder
Ordered list of weapon categories for cycling.
Definition weaponsStatusController.h:265
Defines export macros for the vreVrfmodel library.
#define VREVRFMODEL_DLL
Definition export.h:22
!
Definition glsVreMessageUtil.h:50
const char * DtWeaponStatusControllerType
Type identifier for the weapon status controller component.
Definition weaponsStatusController.h:26
DtVreMessageResult
Enumeration of possible message handling results.
Definition vreMessage.h:33
Internal-state persistence for VRE joystick controllers.
Base template class for VR-Engage simulation components.