VR-Engage  2.2
Loading...
Searching...
No Matches
weaponResourceLogic.h
Go to the documentation of this file.
1/*******************************************************************************
2** Copyright (c) 2025 MAK Technologies
3** All rights reserved.
4*******************************************************************************/
5
6//! \file weaponResourceLogic.h
7//! \brief Component for managing weapon resources and related state attributes
8//! \ingroup vreCommonComponents
9
10#pragma once
11
13
15
17
18namespace makVre
19{
20
22{
23//! \ingroup RoleConfigParams
24//! \defgroup WeaponResourceLogicRoleParams Weapon Resource Logic Role Parameters
25//! \roleInherits{PlayerComponentRoleParams}
26//! Configuration Options for weaponResourceLogic role.
27//! @{
28
29//! \vreRoleParam{resourceMappings,table,{},weaponResourceLogic,Configuration table mapping resource names to display names and properties.}
30constexpr const char* resourceMappings = "resourceMappings";
31
32//! @}
33} // namespace WeaponResourceLogicConfig
34
35//! \brief Type identifier for DtWeaponResourceLogic component
36constexpr const char* DtWeaponResourceLogicType = "DtWeaponResourceLogic";
37
38//! \brief Component for managing weapon resources, equipment, and related state attributes
39//!
40//! DtWeaponResourceLogic tracks available weapon systems and munitions, processes
41//! resource list updates from simulation state messages, and maintains weapon-related
42//! state attributes. It maps between internal system names and human-readable names,
43//! handles resource dependencies, and enables weapons based on equipment availability.
45{
46public:
47 //! \brief Constructor for DtWeaponResourceLogic
48 //!
49 //! Initializes the component with default settings
51
52 //! \brief Virtual destructor for DtWeaponResourceLogic
53 virtual ~DtWeaponResourceLogic() override;
54
55 //! \brief Initializes the weapon resource logic component
56 //! \param player Pointer to the player station this component belongs to
57 //! \param config Configuration table containing resource mappings
58 //! \return True if initialization succeeded, false otherwise
59 //!
60 //! Reads resource mappings configuration, initializes state attributes,
61 //! and sets up message handlers for simulation state updates
63 //! \brief Performs post-initialization after all components are initialized
64 //! \return True if post-initialization succeeded, false otherwise
65 //!
66 //! Updates the weapon list based on the current resource list
67 virtual bool postInitialize() override;
68
69 //! \brief Called every frame to update component state
70 //! \param dt Delta time since the last frame in seconds
71 //!
72 //! Current implementation is empty as updates occur through message handlers
73 virtual void tick(double dt) override;
74 //! \brief Shuts down the weapon resource logic component
75 //!
76 //! Removes message handlers and attribute change callbacks
77 virtual void shutdown() override;
78
79 //! \brief Returns the type identifier for this component
80 //! \return The type string for DtWeaponResourceLogic
81 virtual const char* type() const override;
82
83 //! \brief Gets a list of all resource mappings keys
84 //! \return Vector of strings containing all resource mapping keys
85 //!
86 //! Returns the list of all keys in the resource mappings table
87 virtual std::vector<std::string> resourceMappingKeys();
88
89 //! \brief Looks up the human-readable short name for a resource
90 //! \param vrfResourceName Resource name in VRF format (possibly with system prefix)
91 //! \return Short name for the resource or the original name if not found
92 //!
93 //! Retrieves the human-readable short name for a given resource
94 virtual std::string lookUpShortName(const std::string& vrfResourceName);
95
96 //! \brief Looks up the diguy hand item for a resource
97 //! \param vrfResourceName Resource name in VRF format
98 //! \return Hand item name or "default" if not found
99 //!
100 //! Retrieves the diguy hand item corresponding to a resource
101 virtual std::string lookUpDiguyHandItem(const std::string& vrfResourceName);
102
103 //! \brief Finds a resource name by matching against an entity type
104 //! \param equipmentType Entity type to match against patterns
105 //! \return Resource name if a match is found, empty string otherwise
106 //!
107 //! Finds the resource name (table key) by checking a deployed equipment type against the
108 //! patterns in the "deployedEquipmentTypes" table. The pattern represents the external entity
109 //! the ownship entity is currently using (such as a tripod-mounted launcher).
110 virtual std::string lookUpResourceByDeployedEquipment(const DtEntityType& equipmentType);
111
112 //! \brief Checks if a hand item matches a resource
113 //! \param vrfResourceName Resource name in VRF format
114 //! \param handItem Hand item to check for a match
115 //! \return True if the hand item matches this resource, false otherwise
116 //!
117 //! Determines if a hand item matches either the primary diguy hand item
118 //! or one of the alternative items in the itemMatches list
119 virtual bool doesHandItemMatch(const std::string& vrfResourceName, const std::string& handItem);
120
121 //! \brief Looks up a field value for a resource in the resource mappings table
122 //! \tparam T Type of the field value to retrieve
123 //! \param resource Resource name in VRF format (possibly with system prefix)
124 //! \param field Name of the field to look up
125 //! \param defaultValue Default value to return if the field is not found
126 //! \return Value of the field or defaultValue if not found
127 //!
128 //! Retrieves a field value from the resource mappings table, handling cases where
129 //! the resource name may include system prefixes or contain special characters
130 template <typename T>
131 T lookUpField(std::string resource, const std::string& field, T defaultValue);
132
133protected:
134 //! \brief Handles resource update
135 //! \param msg Resource update
136 //! \return Message handling result indicating if the message was handled
137 //!
138 //! Processes resource updates to update resource and equipment information
140
141 virtual void resourceChanged(const DtAttributeHandle& changed);
142
143 virtual void addWeaponToList(const std::string& resource);
144
145 virtual void removeWeaponFromList(const std::string& resource);
146
147 //! \brief Configuration table containing resource mappings
149
151
153};
154} // namespace makVre
155
156
157template <typename T>
158T makVre::DtWeaponResourceLogic::lookUpField(std::string resource, const std::string& field, T defaultValue)
159{
160 if (myResourceMappings.isValid())
161 {
162 // Note the use of "false" when looking for the resource config item in the resourceMappings table.
163 // This is necessary because the keys in resourceMappings are munition names, and munition names
164 // can have dots in them, which would by default be interpreted as a lua sub-object delimiter, and
165 // the lookup would fail. The optional "false" parameter skips the dot resolution and treats the
166 // entire string as a single lua object name.
167
168 // Find the config table for the given resource/munition
169 if (myResourceMappings.hasField(resource, false))
170 {
171 DtInitTable resourceEntry = myResourceMappings.findData<DtInitTable>(resource, false);
172
173 // And return the requested field for this resource
174 if (resourceEntry.hasField(field))
175 {
176 return resourceEntry.findData<T>(field);
177 }
178 }
179 }
180
181 LOG_DEBUG("Resource monitor") << "Could not find " << field << " for VRF resource: " << resource << std::endl;
182
183 return defaultValue;
184}
Scoped attribute change callback wrapper.
Definition attributeCallback.h:86
Handle class for safe access to attributes.
Definition attributeHandle.h:30
Type-specific handle class for attributes.
Definition attributeHandle.h:181
Table-based access to Lua state for configuration data.
Definition initializer.h:38
RT findData(const std::string &name, bool resolveDots=true)
Definition initializer.h:175
virtual bool hasField(const std::string &name, bool resolveDots=true)
Checks if the table has a field with the given name.
virtual DtPlayerStation & player()
Gets the player station.
DtPlayerComponent()
Constructor.
Class for managing the user interface for engaged roles.
Definition playerStation.h:51
Abstract base class for all VREngage messages.
Definition vreMessage.h:50
T lookUpField(std::string resource, const std::string &field, T defaultValue)
Looks up a field value for a resource in the resource mappings table.
Definition weaponResourceLogic.h:158
virtual void tick(double dt) override
Called every frame to update component state.
virtual ~DtWeaponResourceLogic() override
Virtual destructor for DtWeaponResourceLogic.
virtual makVre::DtVreMessageResult handleResourceUpdate(makVre::DtVreMessage *msg)
Handles resource update.
DtAttributeHandleType< std::vector< std::string > > myResourceStore
Definition weaponResourceLogic.h:150
DtAttributeChangeCallback myResourceCallback
Definition weaponResourceLogic.h:152
virtual const char * type() const override
Returns the type identifier for this component.
virtual bool doesHandItemMatch(const std::string &vrfResourceName, const std::string &handItem)
Checks if a hand item matches a resource.
virtual void addWeaponToList(const std::string &resource)
DtInitTable myResourceMappings
Configuration table containing resource mappings.
Definition weaponResourceLogic.h:148
DtWeaponResourceLogic()
Constructor for DtWeaponResourceLogic.
virtual void removeWeaponFromList(const std::string &resource)
virtual std::string lookUpDiguyHandItem(const std::string &vrfResourceName)
Looks up the diguy hand item for a resource.
virtual std::string lookUpResourceByDeployedEquipment(const DtEntityType &equipmentType)
Finds a resource name by matching against an entity type.
virtual bool postInitialize() override
Performs post-initialization after all components are initialized.
virtual bool initialize(makVre::DtPlayerStation *player, makVre::DtInitTable &config) override
Initializes the weapon resource logic component.
virtual std::string lookUpShortName(const std::string &vrfResourceName)
Looks up the human-readable short name for a resource.
virtual void resourceChanged(const DtAttributeHandle &changed)
virtual void shutdown() override
Shuts down the weapon resource logic component.
virtual std::vector< std::string > resourceMappingKeys()
Gets a list of all resource mappings keys.
constexpr const char * resourceMappings
Definition weaponResourceLogic.h:30
Defines export/import macros for the vreCommonComponents library.
#define VRECOMMONCOMPONENTS_DLL
DLL export/import macro for the vreCommonComponents library.
Definition export.h:28
#define LOG_DEBUG(channel)
Macro to log a debug message to log files.
Definition logger.h:84
Definition weaponResourceLogic.h:22
Include export definitions for this library.
Definition glsVreMessageUtil.h:49
constexpr const char * DtWeaponResourceLogicType
Type identifier for DtWeaponResourceLogic component.
Definition weaponResourceLogic.h:36
DtVreMessageResult
Enumeration of possible message handling results.
Definition vreMessage.h:33
Defines the DtPlayerComponent base class for VR-Engage role components.
Defines the base class for all VREngage messages.