VR-Engage  2.2
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
20#include <vrfmodel/joystickControllerInterface.h>
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
42 public DtJoystickControllerInterface
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 virtual void preFirstTickInit() override;
70
71 //! \brief Initializes the controller
72 //! \return True if initialization was successful, false otherwise
73 //!
74 //! Initializes the weapon status controller, adding a setRestore callback
75 //! for reinitializing state properties when the entity is restored.
76 //! Also registers message handlers for weapon configuration requests.
77 virtual bool init() override;
78
79 //! \brief Updates the controller state each simulation frame
80 //!
81 //! Updates weapon status properties each frame, tracking ammunition levels
82 //! and selected weapon state. Handles both player-controlled and non-player
83 //! controlled states, with different update behaviors for each.
84 virtual void tick() override;
85
86 //! \brief Gets the type identifier for this component
87 //! \return String identifying the component type (DtWeaponStatusControllerType)
88 //!
89 //! Implements the DtSimComponent::type() method to return the
90 //! type identifier for this component.
91 virtual const char* type() const override;
92
93 //! \brief Factory method to create a new instance of this component
94 //! \param name The name for the new component
95 //! \param owner The local object that will own this component
96 //! \param simManager The simulation services manager
97 //! \param desc Optional component descriptor
98 //! \param parentRegistry Optional parent registry for reader/writer functionality
99 //! \return Pointer to the newly created component
100 //!
101 //! Static factory method used by the component creation system to instantiate
102 //! new instances of this component type.
103 static DtSimComponent* creator(const DtString& name, DtLocalObject* owner, DtSimulationServices* simManager,
104 DtComponentDescriptor* desc = 0, DtReaderWriterRegistry* parentRegistry = 0);
105
106 //! \brief Processes joystick input for weapon cycling
107 //! \param functionGroup The function group identifier for the input
108 //! \param function The specific function name for the input
109 //! \param value The input value (typically in range [-1.0, 1.0])
110 //! \param repeat Flag indicating whether the function should repeat
111 //!
112 //! Handles the "CycleWeapon" joystick function by selecting the next
113 //! weapon that has ammunition, or "None" if no weapons with ammunition are available.
114 //! This allows players to quickly cycle through available weapons using joystick inputs.
116 const DtString& functionGroup, const DtString& function, double value, bool repeat) override;
117
118 //! \brief Gets the count of a specific munition resource
119 //! \param munitionName The name of the munition resource to count
120 //! \return The number of units of the specified munition available
121 //!
122 //! Retrieves the current count of a specific munition resource by name.
123 //! Returns 0 if the resource is not found or depleted.
124 virtual int munitionResourceCount(const DtEntityType& munitionName) const;
125
126protected:
127 //! \brief Initializes weapon configurations from entity's weapon systems
128 //! \return True if weapon configurations were successfully initialized, false otherwise
129 //!
130 //! Sets up the weapon configuration map from the entity's weapon systems.
131 //! Identifies and catalogs all weapon systems on the entity, setting up
132 //! the data structures needed to track their status.
133 virtual void initWeaponConfigs();
134
135 //! \brief Adds a weapon configuration for a specific weapon system
136 //! \param system The component system representing a weapon system
137 //! \param postfix The suffix to add to the weapon category name
138 //!
139 //! Adds a weapon configuration for the specified weapon system to the
140 //! weapon configuration map. The postfix parameter is used to distinguish
141 //! between multiple weapon systems of the same category.
142 virtual void addWeaponConfig(DtComponentSystem* system, const std::string& postfix);
143
144 virtual void updateAmmo();
145
146 //! \brief Sends a weapon configuration response message
147 //!
148 //! Constructs and sends a response message containing detailed information
149 //! about all weapon configurations on the entity. This includes weapon categories,
150 //! ammunition types, ranges, and other properties.
151 virtual void sendWeaponConfig();
152
153 virtual void sendAmmoUpdate(const std::string& weapon, const DtEntityType& munition, int count);
154
155 //! \brief Handles weapon configuration request messages
156 //! \param msg The message containing the weapon configuration request
157 //! \return Message processing result indicating success or failure
158 //!
159 //! Processes messages requesting information about the entity's weapon
160 //! configurations. Triggers the sending of a weapon configuration response.
162
163 //! \brief Handles weapon selection messages
164 //! \param msg The message containing the weapon selection request
165 //! \return Message processing result indicating success or failure
166 //!
167 //! Processes messages requesting the selection of a specific weapon category.
168 //! Updates the selected weapon state property and related weapon status information.
170
171 //! \brief Static callback function for set restore messages
172 //! \param msg The simulation message containing restore information
173 //! \param usrData User data pointer, typically pointing to an instance of this class
174 //!
175 //! Static function registered as a callback to handle set restore messages.
176 //! Forwards the message to the appropriate instance's processSetRestore method.
177 static void setRestoreCallback(DtSimMessage* msg, void* usrData);
178
179 //! \brief Reinitializes state properties during entity restoration
180 //! \param msg The simulation message containing restore information
181 //!
182 //! Reinitializes weapon status state properties when a set restore operation
183 //! is executed. Ensures that weapon configurations and status information
184 //! are properly restored after entity restoration.
185 virtual void processSetRestore(DtSimMessage* msg);
186
187 //! \brief Finds the next weapon system category to select
188 //! \return String containing the category name of the next weapon system
189 //!
190 //! Finds the category for the next weapon system to select, using the order
191 //! defined in the descriptor if it exists, or falling back on the order of
192 //! elements in the state property. Only considers weapon systems that have
193 //! ammunition available.
194 virtual bool selectNextWeapon();
195
196 virtual bool selectWeapon(const DtString& selection);
197
198 //! \brief Updates range information for the selected weapon system
199 //!
200 //! Clears the existing range items in state properties, then adds minimum,
201 //! optimal, and maximum range values for the selected weapon system.
202 //! This information is used by the UI to display weapon range indicators.
203 virtual void updateRangeItems();
204
205 //! \brief Handles joystick connection events
206 //!
207 //! Required by the DtJoystickControllerInterface but implemented as a no-op
208 //! since no special handling is needed when a joystick is connected.
209 virtual void joystickConnected() override;
210
211 //! \brief Handles joystick disconnection events
212 //!
213 //! Required by the DtJoystickControllerInterface but implemented as a no-op
214 //! since no special handling is needed when a joystick is disconnected.
215 virtual void joystickDisconnected() override;
216
217 DtRwPropertiesMap& getWeaponsStatusMap();
218
219 DtRwPropertyString& getCurrentWeapon();
220
221 //! \brief Replenishes fuel and ammunition resources
222 //!
223 //! Restores all fuel and ammunition resources to their maximum levels.
224 //! Used during entity restoration or for gameplay mechanics that
225 //! provide resource replenishment.
226 virtual void replenishFuelAndAmmo();
227
228protected:
229 //! \brief Flag tracking player control state
230 //!
231 //! Used to track when a player first takes control of the entity, in order
232 //! to trigger a full update of weapon status property values. Helps ensure
233 //! that the interface shows current values when a player first engages control.
235
236 //! \brief Flag indicating if entity has weapon systems
237 //!
238 //! True if the ownship entity has weapon systems that need to be updated.
239 //! Used to optimize performance by skipping weapon updates for entities
240 //! that don't have weapons.
242
243 //! \brief Ordered list of weapon categories for cycling
244 //!
245 //! Defines the order to cycle through weapon categories. Either specified in
246 //! the descriptor or determined by the order the weapon systems are defined
247 //! in the entity file. Used when cycling through weapons with joystick inputs.
248 std::vector<std::string> myWeaponCycleOrder;
249
250 std::vector<WeaponConfigResponseMessage::WeaponConfig> myWeaponConfigCache;
251 std::map<DtEntityType, unsigned int> myAmmoCache;
252
253private:
254 //! \brief Default constructor (not implemented)
255 //!
256 //! Default constructor is private and not implemented to prevent
257 //! creation of instances without proper initialization.
259
260 //! \brief Copy constructor (not implemented)
261 //!
262 //! Copy constructor is private and not implemented to prevent
263 //! copy construction.
265
266 //! \brief Assignment operator (not implemented)
267 //! \return Reference to this object
268 //!
269 //! Assignment operator is private and not implemented to prevent assignment.
271};
272
273} // namespace makVre
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:54
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:234
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:250
bool myHasWeaponSystems
Flag indicating if entity has weapon systems.
Definition weaponsStatusController.h:241
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:251
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)
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:248
Defines export macros for the vreVrfmodel library.
#define VREVRFMODEL_DLL
Definition export.h:22
Include export definitions for this library.
Definition glsVreMessageUtil.h:49
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
Base template class for VR-Engage simulation components.