VR-Engage  2.3
Loading...
Searching...
No Matches
vreSimComponent.h
Go to the documentation of this file.
1/*******************************************************************************
2** Copyright (c) 2025 MAK Technologies
3** All rights reserved.
4*******************************************************************************/
5
6//! \file vreSimComponent.h
7//! \ingroup vreVrfmodel
8//! \brief Base template class for VR-Engage simulation components
9//!
10//! This file defines the DtVreSimComponent template class which serves as the
11//! base class for VR-Engage simulation components. It extends VR-Forces base
12//! components with VR-Engage specific functionality such as player control detection
13//! and entity system name retrieval.
14
15#pragma once
16
17#include "vreVrfmodel/export.h"
18
19#pragma warning(push)
20#pragma warning(disable : 4100) // unused parameter
21#pragma warning(disable : 4244) // type conversion, possible loss of data
22
23#include <vrfobjcore/vrfObjectStateRepository.h>
24#include <vlutil/vlString.h>
25
26#pragma warning(pop)
27
28#include <readerWriter/rwProperties.h>
29#include <readerWriter/rwPropertiesMap.h>
30#include <readerWriter/propertyInterface.h>
31
33
34#include <string>
35#include <vector>
36
37class DtSimulationServices;
38class DtComponentDescriptor;
39class DtReaderWriterRegistry;
40
41namespace makVre
42{
43//! \brief Base template class for VR-Engage simulation components
44//! \tparam ComponentBase The base component class to extend with VR-Engage functionality
45//!
46//! DtVreSimComponent is a template class that serves as the base for VR-Engage
47//! simulation components. It extends VR-Forces base component classes with VR-Engage
48//! specific functionality such as player control detection and entity system name
49//! retrieval. All VR-Engage components should derive from this template class.
50template <typename ComponentBase>
51class VREVRFMODEL_DLL DtVreSimComponent : public ComponentBase
52{
53public:
54 //! \brief Constructor
55 //! \param name The name of this component
56 //! \param owner The local object that owns this component
57 //! \param simManager The simulation services manager
58 //! \param desc Component descriptor with configuration parameters
59 //! \param parentRegistry Optional parent registry for reader/writer functionality
60 //!
61 //! Creates a new VR-Engage simulation component with the specified parameters.
62 //! Forwards all parameters to the base component class constructor.
63 DtVreSimComponent(const DtString& name, DtLocalObject* owner, DtSimulationServices* simManager,
64 DtComponentDescriptor* desc = 0, DtReaderWriterRegistry* parentRegistry = 0)
65 : ComponentBase(name, owner, simManager, desc, parentRegistry)
66 {
67 }
68
69 //! \brief Virtual destructor
70 //!
71 //! Cleans up resources used by the VR-Engage simulation component.
72 //! Base component resources are cleaned up by the base class destructor.
73 virtual ~DtVreSimComponent() {}
74
75 //! \brief Checks if the entity is player controlled
76 //! \param role Optional role name to check for (empty string to check for any role)
77 //! \return True if the entity has the specified role, or any role if none is provided
78 //!
79 //! Determines whether the entity is under player control by inspecting the published
80 //! "Roles" state-property map. If a specific role is provided, returns true when that
81 //! role's station-entry list is non-empty. If no role is provided, returns true when
82 //! any role is occupied by at least one station.
83 bool isPlayerControlled(const std::string& role = "") const
84 {
85 DtRwProperties& props = const_cast<DtRwProperties&>(this->entity()->nextFrameStateProperties());
86 DtRwPropertiesMap* rolesMap = props.findProperty<DtRwPropertiesMap>(DtRolesStatePropertyName);
87 if (!rolesMap)
88 {
89 return false;
90 }
91
92 if (!role.empty())
93 {
94 DtPropertyInterface* item = rolesMap->findItem(role.c_str());
96 }
97
98 // No specific role: player controlled if any role has at least one controlling station.
99 for (const DtRwPropertiesMap::Key& key : rolesMap->allKeys())
100 {
101 DtPropertyInterface* item = rolesMap->findItem(key.string());
103 {
104 return true;
105 }
106 }
107 return false;
108 }
109
110 //! \brief Returns the identifiers of the stations controlling a given role
111 //! \param role The role name to query
112 //! \return The controlling station identifiers, or an empty vector if the role is unoccupied
113 //!
114 //! Reads the published "Roles" state-property map and returns the station identifiers
115 //! from the structured station list associated with \a role.
116 std::vector<std::string> controllingStations(const std::string& role) const
117 {
118 std::vector<std::string> stationsOut;
119
120 DtRwProperties& props = const_cast<DtRwProperties&>(this->entity()->nextFrameStateProperties());
121 DtRwPropertiesMap* rolesMap = props.findProperty<DtRwPropertiesMap>(DtRolesStatePropertyName);
122 if (!rolesMap)
123 {
124 return stationsOut;
125 }
126
127 DtPropertyInterface* item = rolesMap->findItem(role.c_str());
128 appendStatePropertyRoleStationIds(item, stationsOut);
129 return stationsOut;
130 }
131
132 //! \brief Returns the names of all roles occupied by at least one station
133 //! \return Role names whose controlling-station list is non-empty
134 //!
135 //! Reads the published "Roles" state-property map and returns each role name
136 //! (map key) that currently has one or more controlling stations.
137 std::vector<std::string> engagedRoles() const
138 {
139 std::vector<std::string> roles;
140
141 DtRwProperties& props = const_cast<DtRwProperties&>(this->entity()->nextFrameStateProperties());
142 DtRwPropertiesMap* rolesMap = props.findProperty<DtRwPropertiesMap>(DtRolesStatePropertyName);
143 if (!rolesMap)
144 {
145 return roles;
146 }
147
148 for (const DtRwPropertiesMap::Key& key : rolesMap->allKeys())
149 {
150 DtPropertyInterface* item = rolesMap->findItem(key.string());
152 {
153 roles.push_back(key.string().stdString());
154 }
155 }
156 return roles;
157 }
158
159 //! \brief Gets the entity's top-level system name
160 //! \return String containing the top-level system name from the entity definition
161 //!
162 //! Retrieves the name of the top-level system (direct child of base-system)
163 //! defined in the entity file. This is extracted from the full parent system name
164 //! by removing the "base-system." prefix and any suffixes after the first period.
165 //!
166 //! \note This implementation uses string manipulation since DtComponentSystem
167 //! does not provide a direct accessor for the top-level system name.
168 DtString entitySystemName() const
169 {
170 DtString name = this->parentSystem()->fullName();
171 if (name.findString("base-system") == 0)
172 {
173 name.snipFront('.');
174 }
175 name.snipBackToFirst('.');
176 return name;
177 }
178
179private:
180 //! \brief Default constructor (not implemented)
181 //!
182 //! Default constructor is private and not implemented to prevent
183 //! creation of instances without proper initialization.
185
186 //! \brief Copy constructor (not implemented)
187 //!
188 //! Copy constructor is private and not implemented to prevent
189 //! copy construction.
191
192 //! \brief Assignment operator (not implemented)
193 //! \return Reference to this object
194 //!
195 //! Assignment operator is private and not implemented to prevent assignment.
197};
198} // namespace makVre
std::vector< std::string > engagedRoles() const
Returns the names of all roles occupied by at least one station.
Definition vreSimComponent.h:137
DtVreSimComponent(const DtString &name, DtLocalObject *owner, DtSimulationServices *simManager, DtComponentDescriptor *desc=0, DtReaderWriterRegistry *parentRegistry=0)
Constructor.
Definition vreSimComponent.h:63
DtVreSimComponent()
Default constructor (not implemented)
DtVreSimComponent(const DtVreSimComponent &other)
Copy constructor (not implemented)
virtual ~DtVreSimComponent()
Virtual destructor.
Definition vreSimComponent.h:73
DtString entitySystemName() const
Gets the entity's top-level system name.
Definition vreSimComponent.h:168
bool isPlayerControlled(const std::string &role="") const
Checks if the entity is player controlled.
Definition vreSimComponent.h:83
DtVreSimComponent & operator=(const DtVreSimComponent &other)
Assignment operator (not implemented)
std::vector< std::string > controllingStations(const std::string &role) const
Returns the identifiers of the stations controlling a given role.
Definition vreSimComponent.h:116
Defines export macros for the vreVrfmodel library.
#define VREVRFMODEL_DLL
Definition export.h:22
!
Definition glsVreMessageUtil.h:50
UTIL_DLL bool statePropertyHasRoleStations(const DtPropertyInterface *property)
Determines whether a role-station list contains any station entries.
constexpr const char DtRolesStatePropertyName[]
State property carrying direct role assignments.
Definition vreStatePropertiesUtil.h:26
UTIL_DLL void appendStatePropertyRoleStationIds(const DtPropertyInterface *property, std::vector< std::string > &valuesOut)
Appends all station identifiers from a role-station list to an output vector.
Utility functions for VR-Engage runtime state properties.