VR-Engage  2.3
Loading...
Searching...
No Matches
loadingPlayerState.h
Go to the documentation of this file.
1/******************************************************************************
2** Copyright (c) 2025 MAK Technologies
3** All rights reserved.
4******************************************************************************/
5
6//! \file loadingPlayerState.h
7//! \brief Defines the DtLoadingPlayerState class for initializing the player station
8//!
9//! This file contains the DtLoadingPlayerState class which represents the application
10//! state between role selection and engaged gameplay. It orchestrates component
11//! initialization, readiness polling, and entity take-control in a deterministic
12//! sequence, replacing the ad-hoc deferred initialization that was previously
13//! scattered across component tick() methods and the engaged state entry.
14
15#pragma once
16
19
21
22#include <vlpi/entityIdentifier.h>
23
26
27#include <QString>
28
29namespace makVre
30{
32class DtPlayerStation;
34
35//! \brief Phases that the load-player state progresses through sequentially.
36//!
37//! Each phase updates the loading UI text before starting work.
38//! OwnershipAcquire is skipped when no ownership transfer is needed.
39enum class DtLoadPhase
40{
41 PrepareDisembark, //!< Release existing player-station roles before ownership acquisition
42 OwnershipAcquire, //!< Waiting for ownership transfer response (skipped if N/A)
43 CreateStation, //!< Create the player station via createPlayerStation()
44 CreateComponents, //!< Call initializeComponents() on the player station
45 PostInitComponents, //!< Call postInitializeComponents()
46 InitMenus, //!< Call initializeMenus()
47 FinishInit, //!< Call finishInit() (connectors, PlayerCreatedMessage)
48 ComponentReadiness, //!< Poll finalizeInitTick() on all components until ready
49 PostReadyDelay, //!< Artificial delay after all components are ready
50 EmbarkProbe, //!< Check if the loaded entity is already embarked
51 Complete //!< Transition to ENGAGED_STATE
52};
53
54//! \brief Application state that initializes the player station between role
55//! selection and engaged gameplay
56//!
57//! DtLoadingPlayerState replaces the monolithic startup that previously happened
58//! inside DtEngagedState::onEnter(). It shows a loading overlay, walks through
59//! initialization phases, polls component readiness via finalizeInitTick(), broadcasts
60//! PlayerStationCreationStatusMessage at each phase transition, and then
61//! transitions to ENGAGED_STATE. On failure or timeout it transitions back to
62//! CHOOSE_ROLE_STATE with an error notification.
63//!
64//! Take-control is no longer sent by this state. Instead, DtEntityControlLogic
65//! listens for the PlayerStationCreationStatusMessage and sends VrfTakeControlOf
66//! when it receives the ComponentsReady phase.
68{
69public:
70 //! \brief Constructor
71 //! \param app Reference to the player station application
73
74 //! \brief Virtual destructor
75 virtual ~DtLoadingPlayerState() override;
76
77 //! \brief Deleted copy constructor
79
80 //! \brief Deleted assignment operator
82
83 //! \brief Called when the state is entered
84 //! \param args Pointer to arguments from the previous state
85 //!
86 //! Starts the loading sequence. If ownership transfer is needed the state
87 //! enters the OwnershipAcquire phase; otherwise it advances directly to
88 //! CreateStation. The overall timeout timer starts here.
89 virtual void onEnter(const DtPlayerStationStateArgs* args) override;
90
91 //! \brief Called when the state is exited
92 //!
93 //! Removes any registered message handlers and hides the loading overlay.
94 virtual void onExit() override;
95
96 //! \brief Updates the state each frame
97 //! \param dt Time in seconds since the last update
98 //!
99 //! Drives the phase state machine. Each phase performs its work and either
100 //! advances to the next phase or stays in the current phase waiting for an
101 //! asynchronous result (ownership response or component readiness).
102 virtual void tick(double dt) override;
103
104protected:
105 //! \brief Advances to the next phase
106 //! \param nextPhase The phase to advance to
107 virtual void advanceToPhase(DtLoadPhase nextPhase);
108
109 //! \brief Handles failure by transitioning back to CHOOSE_ROLE_STATE
110 //! \param reason Human-readable description of the failure
111 virtual void fail(const std::string& reason);
112
113 //! \brief Handles the ownership transfer response message
114 //! \param message Pointer to the ownership transfer response message
115 //! \return Message handling result
117
118 //! \brief Consumes escape key presses during loading
119 //! \param message Pointer to the escape key message
120 //! \return EATEN to prevent the key from reaching other handlers
121 //!
122 //! Swallows escape key events while the load-player sequence is in progress,
123 //! preventing the user from navigating away during initialization.
125
126 //! \brief Sends an ownership acquire request for the current entity
128
129 //! \brief Updates the loading overlay status text
130 //! \param text The translatable status text to display
131 virtual void setLoadingText(const QString& text);
132
133 //! \brief Checks ownership requirements and advances to the correct initial phase
134 //!
135 //! Shared entry point for both initial loads (onEnter) and embarkation chains
136 //! (chainEmbarkationLoad). Evaluates ownership transfer settings for the current
137 //! entity/role, registers the response handler if needed, and advances to either
138 //! OwnershipAcquire or CreateStation.
140
141 //! \brief Resets the per-load-cycle phase tracking fields
142 //!
143 //! Used by both onEnter() (initial entry) and chainEmbarkationLoad()
144 //! (re-entry for a chained embarked-host load) to ensure the phase state machine
145 //! starts from a clean slate. Does NOT touch the overall timeout budget
146 //! (myTimeoutRemaining) — chained loads intentionally inherit the remaining
147 //! time so a stuck embarked-host load can't mask a stuck base load.
149
150 //! \brief Handles EmbarkationDetectedMessage from DtEngagedState
151 //!
152 //! DtEngagedState analyses SimulationStateMessage and forwards the
153 //! pre-resolved embarkation result here. This handler records the
154 //! host and role so the EmbarkProbe phase can chain a second load
155 //! for the embarkation host before popping.
157
158 //! \brief Restarts the loading pipeline for an embarked-host role
159 //!
160 //! Called from the EmbarkProbe phase when DtEngagedState detected that
161 //! the base entity is already embarked. Resets the phase machine back
162 //! to CreateStation (or OwnershipAcquire) so the embarked-host player station
163 //! is created on top of the base human.
164 virtual void chainEmbarkationLoad();
165
166protected:
167 //! \brief Current phase of the loading sequence
169
170 //! \brief Remaining time in seconds before the overall timeout fires
172
173 //! \brief Default timeout duration in seconds
174 static constexpr double theDefaultTimeout = 30.0;
175
176 //! \brief Request ID for the pending ownership acquire request
177 unsigned long long myPendingOwnershipRequestId;
178
179 //! \brief Whether the CreateStation phase has been executed this entry
181
182 //! \brief Pointer to the loading overlay page (not owned, stored in attribute store)
184
185 //! \brief Whether the fade-out animation has been started in the Complete phase
187
188 //! \brief Accumulated tick count in the PostReadyDelay phase
190
191 //! \brief Number of ticks spent in the current phase
193
194 //! \brief Whether the current phase has completed its one-time work
196
197 //! \brief Last status text set on the loading page, used to avoid redundant updates.
199
200 // --- Embarkation chain detection ---
201 //! DtEngagedState analyses SimulationStateMessage and forwards pre-resolved
202 //! results via EmbarkationDetectedMessage. These fields record that result
203 //! so the EmbarkProbe phase can chain a second load for the embarked-host role.
204
205 //! \brief Host entity detected via EmbarkationDetectedMessage during loading
206 DtEntityIdentifier myDetectedEmbarkHost;
207
208 //! \brief Resolved embarked-host role name detected during loading
210
211 //! \brief True after chainEmbarkationLoad() has run — prevents infinite loop
213
214 //! \brief Whether this load was triggered by an embarkation (set via LoadPlayerRequestMessage)
216
217 //! \brief Whether this load follows an authoritative embark or disembark update
218 //!
219 //! Such loads already have a known target role. The EmbarkProbe phase retains
220 //! its settle window but does not request a possibly stale simulation state.
222
223 //! \brief Whether the ownership transfer handler is currently registered
225
226 //! \brief Accumulated wall-clock seconds spent in the EmbarkProbe phase
228
229 // --- Targeted destruction ---
230 //! When set, the CreateStation phase destroys this specific player station
231 //! before creating the new one. This allows surgical cleanup (e.g., destroy
232 //! the previous embarked host during role switch) without nuking suspended stations
233 //! like the base human player.
234
235 //! \brief Entity of the player station to destroy before creating the new one
236 DtEntityIdentifier myDestroyEntityId;
237
238 //! \brief Station name of the player station to destroy before creating the new one
240
241 //! \brief Load policy read from the LoadPlayerRequestMessage on entry.
242 //!
243 //! When LoadPolicy_ReplaceExisting, the preparation phase calls
244 //! app().clearPlayers() before ownership acquisition to remove stale role
245 //! assignments before their simulations are restarted. The policy is then
246 //! reset to LoadPolicy_PreserveExisting so any chained embarkation loads
247 //! use targeted destruction instead.
249};
250} // namespace makVre
virtual void sendOwnershipAcquireRequest()
Sends an ownership acquire request for the current entity.
bool myFadeOutStarted
Whether the fade-out animation has been started in the Complete phase.
Definition loadingPlayerState.h:186
bool myOwnershipHandlerRegistered
Whether the ownership transfer handler is currently registered.
Definition loadingPlayerState.h:224
virtual void onEnter(const DtPlayerStationStateArgs *args) override
Called when the state is entered.
virtual DtVreMessageResult handleEmbarkationDetected(DtVreMessage *message)
Handles EmbarkationDetectedMessage from DtEngagedState.
DtLoadingPlayerState(const DtLoadingPlayerState &)=delete
Deleted copy constructor.
virtual void setLoadingText(const QString &text)
Updates the loading overlay status text.
bool myPhaseWorkDone
Whether the current phase has completed its one-time work.
Definition loadingPlayerState.h:195
virtual void fail(const std::string &reason)
Handles failure by transitioning back to CHOOSE_ROLE_STATE.
virtual DtVreMessageResult handleOwnershipTransferResponse(DtVreMessage *message)
Handles the ownership transfer response message.
DtVreLoadingPlayerPage * myLoadingPlayerPage
Pointer to the loading overlay page (not owned, stored in attribute store)
Definition loadingPlayerState.h:183
bool myStationCreated
Whether the CreateStation phase has been executed this entry.
Definition loadingPlayerState.h:180
double myTimeoutRemaining
Remaining time in seconds before the overall timeout fires.
Definition loadingPlayerState.h:171
int myPhaseTickCount
Number of ticks spent in the current phase.
Definition loadingPlayerState.h:192
bool myEmbarkChainCompleted
True after chainEmbarkationLoad() has run — prevents infinite loop.
Definition loadingPlayerState.h:212
static constexpr double theDefaultTimeout
Default timeout duration in seconds.
Definition loadingPlayerState.h:174
virtual void tick(double dt) override
Updates the state each frame.
void beginLoadCycle()
Checks ownership requirements and advances to the correct initial phase.
LoadPlayerRequestMessage::LoadPolicy myLoadPolicy
Load policy read from the LoadPlayerRequestMessage on entry.
Definition loadingPlayerState.h:248
DtEntityIdentifier myDetectedEmbarkHost
DtEngagedState analyses SimulationStateMessage and forwards pre-resolved results via EmbarkationDetec...
Definition loadingPlayerState.h:206
virtual ~DtLoadingPlayerState() override
Virtual destructor.
virtual void chainEmbarkationLoad()
Restarts the loading pipeline for an embarked-host role.
unsigned long long myPendingOwnershipRequestId
Request ID for the pending ownership acquire request.
Definition loadingPlayerState.h:177
std::string myDestroyStationName
Station name of the player station to destroy before creating the new one.
Definition loadingPlayerState.h:239
DtLoadPhase myCurrentPhase
Current phase of the loading sequence.
Definition loadingPlayerState.h:168
double myEmbarkProbeWaitSeconds
Accumulated wall-clock seconds spent in the EmbarkProbe phase.
Definition loadingPlayerState.h:227
QString myLastLoadingText
Last status text set on the loading page, used to avoid redundant updates.
Definition loadingPlayerState.h:198
DtLoadingPlayerState & operator=(const DtLoadingPlayerState &)=delete
Deleted assignment operator.
bool myIsEmbarkationLoad
Whether this load was triggered by an embarkation (set via LoadPlayerRequestMessage)
Definition loadingPlayerState.h:215
virtual void advanceToPhase(DtLoadPhase nextPhase)
Advances to the next phase.
int myPostReadyTicksElapsed
Accumulated tick count in the PostReadyDelay phase.
Definition loadingPlayerState.h:189
DtEntityIdentifier myDestroyEntityId
When set, the CreateStation phase destroys this specific player station before creating the new one....
Definition loadingPlayerState.h:236
std::string myDetectedEmbarkRole
Resolved embarked-host role name detected during loading.
Definition loadingPlayerState.h:209
virtual void onExit() override
Called when the state is exited.
DtLoadingPlayerState(DtPlayerStationApp &app)
Constructor.
virtual DtVreMessageResult handleEscKey(DtVreMessage *message)
Consumes escape key presses during loading.
bool myIsEmbarkationTransition
Whether this load follows an authoritative embark or disembark update.
Definition loadingPlayerState.h:221
void resetPhaseStateForNewCycle()
Resets the per-load-cycle phase tracking fields.
Top-level class representing the VR-Engage application.
Definition playerStationApp.h:155
Class for managing the user interface for engaged roles.
Definition playerStation.h:51
Class for passing arguments during state transitions.
Definition playerStationState.h:47
DtPlayerStationApp & app() const
Gets a reference to the player station application.
DtPlayerStationState(DtPlayerStationApp &app, std::string stateType)
Constructor.
Loading overlay shown during player station creation.
Definition vreLoadingPlayerPage.h:39
Abstract base class for all VREngage messages.
Definition vreMessage.h:50
LoadPolicy
LoadPolicy enumeration.
Definition loadPlayerRequest.h:38
Defines export macros for the VR-Engage Player Station library.
#define PLAYERSTATION_DLL
Definition export.h:24
!
Definition glsVreMessageUtil.h:50
DtLoadPhase
Phases that the load-player state progresses through sequentially.
Definition loadingPlayerState.h:40
@ OwnershipAcquire
Waiting for ownership transfer response (skipped if N/A)
Definition loadingPlayerState.h:42
@ PrepareDisembark
Release existing player-station roles before ownership acquisition.
Definition loadingPlayerState.h:41
@ FinishInit
Call finishInit() (connectors, PlayerCreatedMessage)
Definition loadingPlayerState.h:47
@ ComponentReadiness
Poll finalizeInitTick() on all components until ready.
Definition loadingPlayerState.h:48
@ CreateComponents
Call initializeComponents() on the player station.
Definition loadingPlayerState.h:44
@ EmbarkProbe
Check if the loaded entity is already embarked.
Definition loadingPlayerState.h:50
@ CreateStation
Create the player station via createPlayerStation()
Definition loadingPlayerState.h:43
@ PostInitComponents
Call postInitializeComponents()
Definition loadingPlayerState.h:45
@ Complete
Transition to ENGAGED_STATE.
Definition loadingPlayerState.h:51
@ InitMenus
Call initializeMenus()
Definition loadingPlayerState.h:46
@ PostReadyDelay
Artificial delay after all components are ready.
Definition loadingPlayerState.h:49
DtVreMessageResult
Enumeration of possible message handling results.
Definition vreMessage.h:33
Defines base classes for the VR-Engage state management system.
Defines the base class for all VREngage messages.