VR-Engage  2.3
Loading...
Searching...
No Matches
vreSetController.h
Go to the documentation of this file.
1/*******************************************************************************
2** Copyright (c) 2026 MAK Technologies
3** All rights reserved.
4*******************************************************************************/
5
6//! \file vreSetController.h
7//! \ingroup vreVrfmodel
8//! \brief Template controller for handling VR-Engage entity control assignments
9//!
10//! This file defines the DtVreSetController template class which extends base set
11//! controllers to handle VR-Engage specific control requests. It manages role-based
12//! control assignments with multi-station support and provides resource information
13//! publishing for player-controlled entities.
14
15#pragma once
16
17#include "vreVrfmodel/export.h"
19
20#include <vrfobjcore/setDataManager.h>
21#include <vrfobjcore/localObject.h>
22#include <vrftasks/setDataRequest.h>
23
24#include <vlutil/vlString.h>
25#include <readerWriter/propertyInterface.h>
26
27#include <list>
28#include <string>
29#include <vector>
30
31class DtSimulationServices;
32class DtComponentDescriptor;
33class DtReaderWriterRegistry;
34
35namespace makVre
36{
38
39//! \brief Template controller for handling VR-Engage entity control assignments
40//! \tparam SetControllerBase The base set controller class to extend with VR-Engage functionality
41//!
42//! DtVreSetController is a template class that extends base set controllers with
43//! VR-Engage specific functionality for handling DtSetVreControlledRequest messages.
44//! These messages assign control of entity roles to specific VR-Engage player stations,
45//! supporting multi-station control of a single entity with different roles.
46//!
47//! The controller also handles the publishing of resource information for player-controlled
48//! entities and provides special handling for altitude setting commands.
49//!
50//! All VR-Engage specific set controllers should derive from this template class.
51//!
52//! Uses Descriptor Type: DtComponentDescriptor
53template <typename SetControllerBase>
54class DtVreSetController : public DtVreSimComponent<SetControllerBase>
55{
56public:
57 //! \brief Queued role-control change extracted from a set-data request.
59 {
60 //! \brief Whether the entity should be controlled (true) or released (false).
62
63 //! \brief Station identifier associated with the queued change.
64 DtString myStationId;
65
66 //! \brief Role-type keys affected by the queued change.
67 std::vector<std::string> myRoles;
68
69 //! \brief Entity-role name stored in each affected station entry.
70 std::string myRoleName;
71 };
72
73 //! \brief Constructor
74 //!
75 //! Creates the controller, initializes its internal state, and connects the
76 //! ownership-transfer and save hooks used by its role-state bookkeeping.
77 //!
78 //! \param name The name of this component.
79 //! \param owner The local object that owns this component.
80 //! \param simManager The simulation services manager.
81 //! \param desc Component descriptor with configuration parameters.
82 //! \param parentRegistry Optional parent registry for reader/writer functionality.
83 //!
84 DtVreSetController(const DtString& name,
85 DtLocalObject* owner,
86 DtSimulationServices* simManager,
87 DtComponentDescriptor* desc = nullptr,
88 DtReaderWriterRegistry* parentRegistry = nullptr);
89
90 //! \brief Virtual destructor
91 //!
92 //! Disconnects the registered signal handlers, unregisters set-data callbacks,
93 //! and releases any temporarily detached role-state property.
94 virtual ~DtVreSetController() override;
95
96 //! \brief Default constructor (not implemented)
97 //!
98 //! Default constructor is private and not implemented to prevent
99 //! creation of instances without proper initialization. Here because
100 //! the compiler would generate an unprotected one otherwise.
102
103 //! \brief Copy constructor (not implemented)
104 //!
105 //! Copy constructor is private and not implemented to prevent
106 //! copy construction.
107 //!
108 //! \param orig The controller instance that would have been copied.
110
111 //! \brief Assignment operator (not implemented)
112 //!
113 //! Assignment operator is private and not implemented to prevent assignment.
114 //!
115 //! \param orig The controller instance that would have been assigned from.
116 //! \return A reference to this object. This operator is declared but not implemented.
117 const DtVreSetController& operator=(const DtVreSetController& orig) = delete;
118
119 //! \brief Sets the simulation radio and registers callbacks
120 //!
121 //! Overrides the base method, then registers callbacks for VR-Engage role-control
122 //! and ownership-transfer requests.
123 //!
124 //! \param radio The simulation radio to use for communication.
125 virtual void setRadio(DtSimRadio* radio);
126
127 //! \brief Static callback function for player control messages
128 //!
129 //! Static function registered as a callback to handle VR-Engage control messages.
130 //! Forwards the message to the appropriate instance's processSetPlayerControlled method.
131 //!
132 //! \param msg The simulation message containing control-assignment information.
133 //! \param usrData User data pointer, typically pointing to an instance of this class.
134 static void setPlayerControlledCallback(DtSimMessage* msg, void* usrData);
135
136 //! \brief Static callback function for ownership-transfer disable messages.
137 //!
138 //! Static function registered as a callback to handle scripted ownership-transfer
139 //! control messages. Forwards the message to the appropriate instance's
140 //! processSetDisableOwnershipTransfer method.
141 //!
142 //! \param msg The simulation message containing the disable-ownership-transfer request.
143 //! \param usrData User data pointer, typically pointing to an instance of this class.
144 static void setDisableOwnershipTransferCallback(DtSimMessage* msg, void* usrData);
145
146 //! \brief Processes a VR-Engage control assignment message
147 //!
148 //! Extracts the requested station, roles, role metadata, and control flag from a
149 //! DtSetVreControlledRequest and queues the change so it can be applied
150 //! later during tick().
151 //!
152 //! \param msg The simulation message containing control-assignment information.
153 virtual void processSetPlayerControlled(DtSimMessage* msg);
154
155 //! \brief Processes a disable-ownership-transfer request.
156 //!
157 //! Reads the scripted-task variable named "value" and mirrors it into the
158 //! next-frame "DisableOwnershipTransfer" state property when present.
159 //!
160 //! \param msg The simulation message containing the disable-ownership-transfer request.
161 virtual void processSetDisableOwnershipTransfer(DtSimMessage* msg);
162
163 //! \brief Restores live ownership-transfer and role-occupancy state after an entity reset.
164 //!
165 //! First delegates to the VR-Forces handler to restore entity state, then restores
166 //! the live LastVrfSim value and copies the current-frame "Roles" state-property
167 //! map into the next frame. This preserves the ownership return target and player
168 //! control through a reset or reset-in-place operation.
169 //!
170 //! \param msg The simulation message containing the restore request.
171 virtual void processSetRestore(DtSimMessage* msg);
172
173protected:
174 //! \brief Creates or updates the next-frame LastVrfSim state property.
175 //! \param simAddress The VR-Forces simulation address to store.
176 void setLastVrfSimStateProperty(const DtString& simAddress);
177
178 //! \brief Removes the top-level "Roles" state property before eligible saves.
179 //!
180 //! This is currently limited to checkpoint saves so role occupancy is not
181 //! serialized into the saved data while the surrounding checkpoint is written.
182 //!
183 //! \param saveType The type of save operation currently in progress.
184 virtual void processPreSave(DtLocalObject::SaveType saveType);
185
186 //! \brief Restores the top-level "Roles" state property after eligible saves.
187 //!
188 //! Reattaches the property previously detached by processPreSave() once the
189 //! checkpoint save has completed.
190 //!
191 //! \param saveType The type of save operation that has just completed.
192 virtual void processPostSave(DtLocalObject::SaveType saveType);
193
194 //! \brief Sends one frontend role-assignment replay message.
195 //!
196 //! Broadcasts a DtIfAssignRolePlayer message for a single station/role pair
197 //! so the matching frontend station can restore its assignment state. Marks
198 //! the replay so the frontend can apply its reengagement policy.
199 //!
200 //! \param stationId The station identifier that should receive the replayed assignment.
201 //! \param roleName The role name to reassign to the station.
202 virtual void sendAssignRolePlayerMessage(const DtString& stationId, const DtString& roleName);
203
204 //! \brief Determines whether a role assignment on this entity is an embarked human role.
205 //!
206 //! Checks directly embarked human objects for a matching controlling station and
207 //! engaged embarked-role marker, allowing the backend to tag host-role occupancy
208 //! entries without relying on frontend-provided embarkation flags.
209 //!
210 //! \param stationId The controlling station identifier stored in the role entry.
211 //! \param roleType The top-level role-type key being updated.
212 //! \param roleName The entity-role name stored in the structured role entry.
213 //! \return True if the assignment represents an embarked human controlling a host role.
215 const DtString& stationId, const std::string& roleType, const std::string& roleName) const;
216
217 //! \brief Determines whether a restored station entry should emit a replay message.
218 //!
219 //! The default implementation suppresses replay for embarked host-role entries
220 //! because restoring the base human assignment is sufficient to re-chain the
221 //! embarkation load on the frontend.
222 //!
223 //! \param roleName The top-level role-type key for the restored entry.
224 //! \param stationItem The restored station entry under consideration.
225 //! \return True if a DtIfAssignRolePlayer message should be emitted for this entry.
227 const DtString& roleName, const DtStatePropertyRoleStationItem& stationItem);
228
229 //! \brief Replays and clears restored role assignments from the "Roles" state property.
230 //!
231 //! Scans the current-frame state properties first, then the next-frame state
232 //! properties, and emits one replay message for each restored unique station/role
233 //! pair that should be replayed to the frontend. After at least one message is
234 //! sent, removes the restored role entries from the next-frame map so the
235 //! re-engaging frontend repopulates them through its normal take-control path.
237
238public:
239 //! \brief Applies one queued role-occupancy change to the entity state.
240 //!
241 //! Updates the next-frame "Roles" state-property map by adding or removing the
242 //! requesting station from each role entry in the change payload.
243 //!
244 //! \param change The queued control change to apply.
246
247 //! \brief Initializes the controller before the first tick
248 //!
249 //! Performs initialization that must happen before the first tick but after the
250 //! component is fully constructed. Creates or refreshes the LastVrfSim state property
251 //! on full-capability simulators and calls the base class preFirstTickInit().
252 //! Preserves existing role assignments during ownership handoff. Otherwise,
253 //! replays restored role assignments to the frontend and clears those entries
254 //! so successful re-engagement republishes them through the normal
255 //! control-acquisition path.
256 virtual void preFirstTickInit();
257
258 //! \brief Updates the controller state each simulation frame
259 //!
260 //! Applies at most one queued role-control change per frame. If the entity is
261 //! not player controlled, the base controller tick continues to run normally;
262 //! otherwise the base tick is skipped so CGF control logic does not take over.
263 virtual void tick();
264
265 //! \brief Processes altitude setting messages with enhanced terrain handling
266 //! \param msg The simulation message containing altitude setting information
267 //!
268 //! Overrides the base method to correctly handle setting altitude of 0 m above terrain.
269 //! Provides special handling for altitude values of 0, which are interpreted as
270 //! "place the entity directly on the terrain" rather than "place at 0 MSL".
271 //! Also handles terrain clamping appropriately.
272 virtual void processSetAltitude(DtSimMessage* msg);
273
274 //! \brief Factory method to create a new instance of this component
275 //! \param name The name for the new component
276 //! \param owner The local object that will own this component
277 //! \param simManager The simulation services manager
278 //! \param desc Optional component descriptor
279 //! \param parentRegistry Optional parent registry for reader/writer functionality
280 //! \return A pointer to the newly created component. Ownership is transferred
281 //! to the caller or component-creation framework that requested it.
282 //!
283 //! Static factory method used by the component creation system to instantiate
284 //! new instances of this component type.
285 static DtSimComponent* creator(const DtString& name,
286 DtLocalObject* owner,
287 DtSimulationServices* simManager,
288 DtComponentDescriptor* desc = nullptr,
289 DtReaderWriterRegistry* parentRegistry = nullptr);
290
291protected:
292 //! \brief Flag indicating if a resource warning has been logged
293 //!
294 //! Tracks whether a warning about exceeding the maximum resource string size
295 //! has already been logged, to prevent spamming the log with the same warning.
297
298 //! \brief True once the entity has completed a convert-to-local handoff.
300
301 //! \brief Temporarily detached top-level "Roles" state property during checkpoint save.
302 DtPropertyInterface* myDetachedRolesStateProperty = nullptr;
303
304 //! \brief Connection for ownership-transfer notifications from the local object.
305 boost::signals2::connection myOwnershipTransferConn;
306
307 //! \brief Connection for pre-save notifications from the local object.
308 boost::signals2::connection myPreSaveConn;
309
310 //! \brief Connection for post-save notifications from the local object.
311 boost::signals2::connection myPostSaveConn;
312
313 //! \brief FIFO queue of pending role-control changes to apply during tick().
314 std::list<SetVreControlledContent> myPendingControlChanges;
315};
316} // namespace makVre
317
318#include "vreVrfModel/vreSetController.inl"
std::list< SetVreControlledContent > myPendingControlChanges
FIFO queue of pending role-control changes to apply during tick().
Definition vreSetController.h:314
virtual void setRadio(DtSimRadio *radio)
Sets the simulation radio and registers callbacks.
virtual void replayRestoredRoleAssignments()
Replays and clears restored role assignments from the "Roles" state property.
DtPropertyInterface * myDetachedRolesStateProperty
Temporarily detached top-level "Roles" state property during checkpoint save.
Definition vreSetController.h:302
static void setDisableOwnershipTransferCallback(DtSimMessage *msg, void *usrData)
Static callback function for ownership-transfer disable messages.
virtual ~DtVreSetController() override
Virtual destructor.
virtual void processPreSave(DtLocalObject::SaveType saveType)
Removes the top-level "Roles" state property before eligible saves.
virtual void processSetPlayerControlled(DtSimMessage *msg)
Processes a VR-Engage control assignment message.
virtual void sendAssignRolePlayerMessage(const DtString &stationId, const DtString &roleName)
Sends one frontend role-assignment replay message.
static DtSimComponent * creator(const DtString &name, DtLocalObject *owner, DtSimulationServices *simManager, DtComponentDescriptor *desc=nullptr, DtReaderWriterRegistry *parentRegistry=nullptr)
Factory method to create a new instance of this component.
virtual void updatePlayerControlled(const SetVreControlledContent &change)
Applies one queued role-occupancy change to the entity state.
virtual void processSetRestore(DtSimMessage *msg)
Restores live ownership-transfer and role-occupancy state after an entity reset.
bool myWarnedAboutResource
Flag indicating if a resource warning has been logged.
Definition vreSetController.h:296
virtual void processSetAltitude(DtSimMessage *msg)
Processes altitude setting messages with enhanced terrain handling.
bool myObjectWasTransferred
True once the entity has completed a convert-to-local handoff.
Definition vreSetController.h:299
virtual void processPostSave(DtLocalObject::SaveType saveType)
Restores the top-level "Roles" state property after eligible saves.
virtual void tick()
Updates the controller state each simulation frame.
virtual void processSetDisableOwnershipTransfer(DtSimMessage *msg)
Processes a disable-ownership-transfer request.
static void setPlayerControlledCallback(DtSimMessage *msg, void *usrData)
Static callback function for player control messages.
DtVreSetController(const DtString &name, DtLocalObject *owner, DtSimulationServices *simManager, DtComponentDescriptor *desc=nullptr, DtReaderWriterRegistry *parentRegistry=nullptr)
Constructor.
bool isEmbarkedRoleAssignment(const DtString &stationId, const std::string &roleType, const std::string &roleName) const
Determines whether a role assignment on this entity is an embarked human role.
DtVreSetController()=delete
Default constructor (not implemented)
boost::signals2::connection myPreSaveConn
Connection for pre-save notifications from the local object.
Definition vreSetController.h:308
virtual bool shouldReplayRestoredRoleAssignment(const DtString &roleName, const DtStatePropertyRoleStationItem &stationItem)
Determines whether a restored station entry should emit a replay message.
const DtVreSetController & operator=(const DtVreSetController &orig)=delete
Assignment operator (not implemented)
boost::signals2::connection myOwnershipTransferConn
Connection for ownership-transfer notifications from the local object.
Definition vreSetController.h:305
virtual void preFirstTickInit()
Initializes the controller before the first tick.
boost::signals2::connection myPostSaveConn
Connection for post-save notifications from the local object.
Definition vreSetController.h:311
DtVreSetController(const DtVreSetController &orig)=delete
Copy constructor (not implemented)
void setLastVrfSimStateProperty(const DtString &simAddress)
Creates or updates the next-frame LastVrfSim state property.
DtVreSimComponent(const DtString &name, DtLocalObject *owner, DtSimulationServices *simManager, DtComponentDescriptor *desc=0, DtReaderWriterRegistry *parentRegistry=0)
Definition vreSimComponent.h:63
Defines export macros for the vreVrfmodel library.
!
Definition glsVreMessageUtil.h:50
One station entry from the published "Roles" state property.
Definition vreStatePropertiesUtil.h:33
Queued role-control change extracted from a set-data request.
Definition vreSetController.h:59
std::vector< std::string > myRoles
Role-type keys affected by the queued change.
Definition vreSetController.h:67
std::string myRoleName
Entity-role name stored in each affected station entry.
Definition vreSetController.h:70
DtString myStationId
Station identifier associated with the queued change.
Definition vreSetController.h:64
bool myControlled
Whether the entity should be controlled (true) or released (false).
Definition vreSetController.h:61
Base template class for VR-Engage simulation components.