Arma Reforger Explorer 1.7.0.54
Arma Reforger Code Explorer by Zeroy - Thanks to MisterOutofTime
Loading...
Searching...
No Matches
SCR_WaveRespawnTimerComponent.c
Go to the documentation of this file.
1[ComponentEditorProps(category: "GameScripted/GameMode/Components", description: "Handles respawn timers for players.")]
5
6#define RESPAWN_TIMER_COMPONENT_DEBUG
7
9// TODO@AS: Revisit after changes in timer comp
10class SCR_WaveRespawnTimerComponent : SCR_RespawnTimerComponent
11{
13 [RplProp()]
14 protected ref array<ref SCR_RespawnTimer> m_aFactionRespawnTimers = {};
15
16 [RplProp()]
17 protected ref array<int> m_aAllowedPlayers = {};
18
19 [RplProp()]
20 protected ref array<int> m_aWaitingPlayers = {};
21
22 #ifdef RESPAWN_TIMER_COMPONENT_DEBUG
23 //------------------------------------------------------------------------------------------------
24 override void DrawDebugInfo()
25 {
26 super.DrawDebugInfo();
27
28 DbgUI.Begin("Wave Respawn Diag");
29 {
30 // Players ready to respawn
31 DbgUI.Text("Allowed Players:");
32 PlayerManager playerManager = GetGame().GetPlayerManager();
33 foreach (int playerId : m_aAllowedPlayers)
34 {
35 string playerText = string.Format("%1: %2", playerId, playerManager.GetPlayerName(playerId));
36 DbgUI.Text(playerText);
37 }
38
39 // Players waiting in wave
40 DbgUI.Text("Waiting Players:");
41 foreach (int playerId : m_aWaitingPlayers)
42 {
43 string playerText = string.Format("%1: %2", playerId, playerManager.GetPlayerName(playerId));
44 DbgUI.Text(playerText);
45 }
46
48 SCR_FactionManager factionManager = SCR_FactionManager.Cast(GetGame().GetFactionManager());
49
50 if (factionManager)
51 {
52 // Respawn timers
53 DbgUI.Text("Faction Timers:");
54 int index = 0;
55 foreach (SCR_RespawnTimer respawnTimer : m_aFactionRespawnTimers)
56 {
57 Faction affiliatedFaction = factionManager.GetFactionByIndex(index);
58 if (affiliatedFaction)
59 {
60 string factionText = string.Format("%1: %2s (%3)", index, respawnTimer.GetRemainingTime(timeNow), affiliatedFaction.GetFactionName());
61 DbgUI.Text(factionText);
62 }
63
64 index++;
65 }
66 }
67
68 }
69 DbgUI.End();
70 }
71 #endif
72
73 //------------------------------------------------------------------------------------------------
74 override bool GetCanPlayerSpawn(int playerID, float additionalTime = 0)
75 {
76 return m_aAllowedPlayers.Contains(playerID);
77 }
78
79 //------------------------------------------------------------------------------------------------
80 override int GetPlayerRemainingTime(int playerID, float additionalTime = 0)
81 {
82 // Ready to respawn
83 if (m_aAllowedPlayers.Contains(playerID))
84 return 0;
85
86 SCR_FactionManager factionManager = SCR_FactionManager.Cast(GetGame().GetFactionManager());
87 if (!factionManager)
88 return 0;
89
90 // Waiting in next wave
91 Faction faction = factionManager.GetPlayerFaction(playerID);
92 int factionIndex = factionManager.GetFactionIndex(faction);
93 if (factionIndex < 0)
94 return 0;
95
97 return m_aFactionRespawnTimers[factionIndex].GetRemainingTime(timeNow);
98 }
99
100 //------------------------------------------------------------------------------------------------
101 override void OnPlayerSpawnFinalize_S(SCR_SpawnRequestComponent requestComponent, SCR_SpawnHandlerComponent handlerComponent, SCR_SpawnData data, IEntity entity)
102 {
103 super.OnPlayerSpawnFinalize_S(requestComponent, handlerComponent, data, entity);
104
105 if (!requestComponent)
106 return;
107
108 int playerId = requestComponent.GetPlayerId();
109
110 int allowedIndex = m_aAllowedPlayers.Find(playerId);
111 if (allowedIndex != -1)
112 m_aAllowedPlayers.Remove( allowedIndex );
113
114 int waitingIndex = m_aWaitingPlayers.Find(playerId);
115 if (waitingIndex != -1)
116 m_aWaitingPlayers.Remove( waitingIndex );
117
118 Replication.BumpMe();
119 }
120
121 //------------------------------------------------------------------------------------------------
122 override void OnPlayerKilled(notnull SCR_InstigatorContextData instigatorContextData)
123 {
124 super.OnPlayerKilled(instigatorContextData);
125 if (!m_aWaitingPlayers.Contains(instigatorContextData.GetVictimPlayerID()))
126 {
127 m_aWaitingPlayers.Insert(instigatorContextData.GetVictimPlayerID());
128 Replication.BumpMe();
129 }
130 }
131
132 //------------------------------------------------------------------------------------------------
133 override void OnPlayerDeleted(int playerId, IEntity player)
134 {
135 OnPlayerKilled(new SCR_InstigatorContextData(playerId, player, null, Instigator.CreateInstigator(null), true));
136 }
137
138 //------------------------------------------------------------------------------------------------
139 override void OnPlayerConnected(int playerId)
140 {
141 if (!m_aWaitingPlayers.Contains(playerId))
142 {
143 m_aWaitingPlayers.Insert(playerId);
144 Replication.BumpMe();
145 }
146 }
147
148 //------------------------------------------------------------------------------------------------
153 [RplRpc(RplChannel.Reliable, RplRcver.Broadcast)]
154 void RpcDo_StartFactionTimer(int factionIndex, WorldTimestamp rplTime)
155 {
156 SCR_RespawnTimer timer = m_aFactionRespawnTimers[factionIndex];
157 timer.Start(rplTime);
158 }
159
160 //------------------------------------------------------------------------------------------------
161 override void EOnFrame(IEntity owner, float timeSlice)
162 {
163 if (!IsMaster())
164 return;
165
166 bool shouldBump;
167 WorldTimestamp timeNow = GetCurrentTime();
168 // Update timers
169 int timersCount = m_aFactionRespawnTimers.Count();
170 SCR_FactionManager factionManager = SCR_FactionManager.Cast(GetGame().GetFactionManager());
171 if (!factionManager)
172 return;
173
174 Faction playerFaction;
175 for (int i = 0; i < timersCount; i++)
176 {
177 SCR_RespawnTimer timer = m_aFactionRespawnTimers[i];
178 if (timer)
179 {
180 // If timer is finished, allow spawn for current wave of players
181 if (timer.IsFinished(timeNow))
182 {
183 // Add each waiting player of given faction into the "allow players" list
184 for (int playerIndex = m_aWaitingPlayers.Count() -1; playerIndex >= 0; playerIndex--)
185 {
186 int playerId = m_aWaitingPlayers[playerIndex];
187 playerFaction = factionManager.GetPlayerFaction(playerId);
188 if (playerFaction)
189 {
190 int factionIndex = factionManager.GetFactionIndex(playerFaction);
191 if (factionIndex == i)
192 {
193 // It would be nicer to use set<T>, but rpl codec is missing for that object
194 if (!m_aAllowedPlayers.Contains(playerId))
195 m_aAllowedPlayers.Insert(playerId);
196
197 // Remove from waiting list, we are in ready list already
198 m_aWaitingPlayers.Remove(playerIndex);
199
200 // Make sure to propagate changes
201 shouldBump = true;
202 }
203 }
204 }
205
206 // Start timer
207 RpcDo_StartFactionTimer(i, timeNow);
208 Rpc(RpcDo_StartFactionTimer, i, timeNow);
209 }
210 }
211 }
212
213 if (shouldBump)
214 Replication.BumpMe();
215 }
216
217 //------------------------------------------------------------------------------------------------
219 override void OnPostInit(IEntity owner)
220 {
221 super.OnPostInit(owner);
222
223 SetEventMask(owner, EntityEvent.INIT | EntityEvent.FRAME);
224
225 // Fetch necessary components
226 m_RplComponent = RplComponent.Cast(owner.FindComponent(RplComponent));
227 }
228
229 //------------------------------------------------------------------------------------------------
231 override void EOnInit(IEntity owner)
232 {
233 // Fill list of respawns with default data
234 int factionsCount;
235 array<Faction> factions = {};
236 SCR_FactionManager factionManager = SCR_FactionManager.Cast(GetGame().GetFactionManager());
237 if (factionManager)
238 factionsCount = factionManager.GetFactionsList(factions);
239
240 // Prefill list with data
241 SCR_RespawnTimer timer;
242 WorldTimestamp timeNow = GetCurrentTime();
243 for (int i = 0; i < factionsCount; i++)
244 {
245 timer = new SCR_RespawnTimer();
246 timer.SetDuration(m_fRespawnTime);
247 timer.Start(timeNow.PlusSeconds(-m_fRespawnTime));
248 m_aFactionRespawnTimers.Insert(timer);
249 }
250
251 Replication.BumpMe();
252 }
253}
ArmaReforgerScripted GetGame()
Definition game.c:1398
enum EAIGroupCombatMode ComponentEditorProps(category:"GameScripted/AI", description:"Component for utility AI system for groups")
SCR_CacheNoteComponentClass ScriptComponentClass RplProp()] protected ref array< string > m_aLines
RplComponent m_RplComponent
void OnPlayerKilled(notnull SCR_InstigatorContextData instigatorContextData)
SCR_DestructionSynchronizationComponentClass ScriptComponentClass int index
Get all prefabs that have the spawner data
void SCR_FactionManager(IEntitySource src, IEntity parent)
override void EOnFrame(IEntity owner, float timeSlice)
void OnPlayerDeleted(int playerId, IEntity player)
WorldTimestamp GetCurrentTime()
void SCR_RespawnTimerComponent(IEntityComponentSource src, IEntity ent, IEntity parent)
void DrawDebugInfo()
bool GetCanPlayerSpawn(int playerID, float additionalTime=0)
int GetPlayerRemainingTime(int playerID, float additionalTime=0)
override void OnPlayerSpawnFinalize_S(SCR_SpawnRequestComponent requestComponent, SCR_SpawnHandlerComponent handlerComponent, SCR_SpawnData data, IEntity entity)
ref array< int > m_aWaitingPlayers
void RpcDo_StartFactionTimer(int factionIndex, WorldTimestamp rplTime)
ref array< int > m_aAllowedPlayers
Definition DbgUI.c:66
proto external Managed FindComponent(typename typeName)
Main replication API.
Definition Replication.c:14
void Start(WorldTimestamp timeNow)
void SetDuration(float duration)
bool IsFinished(WorldTimestamp timeNow, float additionalTime=0)
override void EOnInit(IEntity owner)
EntityEvent
Various entity events.
Definition EntityEvent.c:14
void OnPlayerConnected(int playerId)
void RplRpc(RplChannel channel, RplRcver rcver, RplCondition condition=RplCondition.None, string customConditionName="")
Definition EnNetwork.c:95
RplRcver
Definition RplRcver.c:59
RplChannel
Communication channel. Reliable is guaranteed to be delivered. Unreliable not.
Definition RplChannel.c:14