Arma Reforger Explorer  1.1.0.42
Arma Reforger Code Explorer by Zeroy - Thanks to MisterOutofTime
SCR_WaveRespawnTimerComponent.c
Go to the documentation of this file.
1 [ComponentEditorProps(category: "GameScripted/GameMode/Components", description: "Handles respawn timers for players.")]
3 {
4 }
5 
6 #define RESPAWN_TIMER_COMPONENT_DEBUG
7 
9 // TODO@AS: Revisit after changes in timer comp
10 class 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 
47  WorldTimestamp timeNow = GetCurrentTime();
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 
96  WorldTimestamp timeNow = GetCurrentTime();
97  return m_aFactionRespawnTimers[factionIndex].GetRemainingTime(timeNow);
98  }
99 
100  //------------------------------------------------------------------------------------------------
101  override void OnPlayerSpawned(int playerId, IEntity controlledEntity)
102  {
103  super.OnPlayerSpawned(playerId, controlledEntity);
104 
105  int allowedIndex = m_aAllowedPlayers.Find(playerId);
106  if (allowedIndex != -1)
107  m_aAllowedPlayers.Remove( allowedIndex );
108 
109  int waitingIndex = m_aWaitingPlayers.Find(playerId);
110  if (waitingIndex != -1)
111  m_aWaitingPlayers.Remove( waitingIndex );
112 
113  Replication.BumpMe();
114  }
115 
116  //------------------------------------------------------------------------------------------------
122  override void OnPlayerKilled(int playerId, IEntity playerEntity, IEntity killerEntity, notnull Instigator killer)
123  {
124  super.OnPlayerKilled(playerId, playerEntity, killerEntity, killer);
125  if (!m_aWaitingPlayers.Contains(playerId))
126  {
127  m_aWaitingPlayers.Insert(playerId);
128  Replication.BumpMe();
129  }
130  }
131 
132  //------------------------------------------------------------------------------------------------
133  override void OnPlayerDeleted(int playerId, IEntity player)
134  {
135  OnPlayerKilled(playerId, player, null, Instigator.CreateInstigator(null));
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 }
ComponentEditorProps
SCR_FragmentEntityClass ComponentEditorProps
RpcDo_StartFactionTimer
void RpcDo_StartFactionTimer(int factionIndex, WorldTimestamp rplTime)
Definition: SCR_WaveRespawnTimerComponent.c:154
IsMaster
protected bool IsMaster()
Definition: SCR_DataCollectorComponent.c:190
GetGame
ArmaReforgerScripted GetGame()
Definition: game.c:1424
OnPlayerConnected
override void OnPlayerConnected(int playerId)
Definition: SCR_WaveRespawnTimerComponent.c:139
RplProp
SCR_WaveRespawnTimerComponentClass SCR_RespawnTimerComponentClass RplProp()] protected ref array< ref SCR_RespawnTimer > m_aFactionRespawnTimers
Must be attached to a GameMode.
RplRpc
SCR_AchievementsHandlerClass ScriptComponentClass RplRpc(RplChannel.Reliable, RplRcver.Owner)] void UnlockOnClient(AchievementId achievement)
Definition: SCR_AchievementsHandler.c:11
Instigator
Definition: Instigator.c:6
SCR_RespawnTimerComponentClass
Definition: SCR_RespawnTimerComponent.c:2
GetCurrentTime
protected WorldTimestamp GetCurrentTime()
Definition: SCR_RespawnTimerComponent.c:79
SCR_RespawnTimerComponent
void SCR_RespawnTimerComponent(IEntityComponentSource src, IEntity ent, IEntity parent)
Definition: SCR_RespawnTimerComponent.c:333
EOnFrame
override void EOnFrame(IEntity owner, float timeSlice)
Definition: SCR_WaveRespawnTimerComponent.c:161
OnPlayerKilled
override void OnPlayerKilled(int playerId, IEntity playerEntity, IEntity killerEntity, notnull Instigator killer)
Definition: SCR_WaveRespawnTimerComponent.c:122
OnPostInit
override void OnPostInit(IEntity owner)
Initialise this component.
Definition: SCR_WaveRespawnTimerComponent.c:219
SCR_WaveRespawnTimerComponentClass
Definition: SCR_WaveRespawnTimerComponent.c:2
OnPlayerSpawned
override void OnPlayerSpawned(int playerId, IEntity controlledEntity)
Definition: SCR_WaveRespawnTimerComponent.c:101
EOnInit
override void EOnInit(IEntity owner)
Initialise this component with data from FactionsManager.
Definition: SCR_WaveRespawnTimerComponent.c:231
Faction
Definition: Faction.c:12
m_aWaitingPlayers
protected ref array< int > m_aWaitingPlayers
Definition: SCR_WaveRespawnTimerComponent.c:20
m_RplComponent
protected RplComponent m_RplComponent
Definition: SCR_CampaignBuildingManagerComponent.c:42
index
SCR_DestructionSynchronizationComponentClass ScriptComponentClass int index
Definition: SCR_DestructionSynchronizationComponent.c:17
SCR_RespawnTimer
Definition: SCR_RespawnTimer.c:1
m_aAllowedPlayers
protected ref array< int > m_aAllowedPlayers
Definition: SCR_WaveRespawnTimerComponent.c:17
OnPlayerDeleted
override void OnPlayerDeleted(int playerId, IEntity player)
Definition: SCR_WaveRespawnTimerComponent.c:133
DrawDebugInfo
override void DrawDebugInfo()
Definition: SCR_WaveRespawnTimerComponent.c:24
SCR_FactionManager
void SCR_FactionManager(IEntitySource src, IEntity parent)
Definition: SCR_FactionManager.c:461
GetCanPlayerSpawn
override bool GetCanPlayerSpawn(int playerID, float additionalTime=0)
Definition: SCR_WaveRespawnTimerComponent.c:74
PlayerManager
Definition: PlayerManager.c:12
GetPlayerRemainingTime
override int GetPlayerRemainingTime(int playerID, float additionalTime=0)
Definition: SCR_WaveRespawnTimerComponent.c:80
category
params category
Definition: SCR_VehicleDamageManagerComponent.c:180