Arma Reforger Explorer  1.1.0.42
Arma Reforger Code Explorer by Zeroy - Thanks to MisterOutofTime
SCR_GameModeCleanSweep.c
Go to the documentation of this file.
1 //------------------------------------------------------------------------------------------------
3 {
4 };
5 
6 //------------------------------------------------------------------------------------------------
8 {
9  const int INVALID_AREA_INDEX = -1;
10  const string ENEMY_PRESENCE_ELIMINATED_TEXT = "Enemy presence eliminated!\nNew area is being selected.\nPlease wait.";
11 
12  [Attribute("6")]
13  int m_iMinAiGroups;
14 
15  [Attribute("10")]
16  int m_iMaxAiGroups;
17 
18  [Attribute("1.5")]
19  float m_fAiGroupsPerPlayer;
20 
21  [Attribute("", UIWidgets.ResourceNamePicker, "What group type should spawn")]
22  ResourceName m_GroupType;
23 
24  [Attribute(defvalue: "{EBD2A7DA0A3C6E17}UI/layouts/Menus/CleanSweep/CleanSweepAreaSelection.layout", desc: "Layout for area selection")]
25  ResourceName m_AreaSelectionLayout;
26 
27  [Attribute("0")]
28  bool m_bSwapSides;
29 
30  Widget m_wRoot;
31  Widget m_wAreaSelectionWidget;
32  TextWidget m_wText;
33 
34  ref array<IEntity> m_aEnemySoldiers = new array<IEntity>;
35  ref array<SCR_AIGroup> m_aGroups;
36  ref array<SCR_SpawnPoint> m_aEnemySpawnPoints;
37  ref array<SCR_SpawnPoint> m_aPlayerSpawnPoints;
38  AIWaypoint m_AttackWP;
39 
40  int m_iGameMasterID = -1;
41 
42  [RplProp(onRplName: "OnAreaChanged")]
43  int areaID = INVALID_AREA_INDEX;
44 
45  //------------------------------------------------------------------------------------------------
46  void OnAreaChanged()
47  {
48  if (m_wAreaSelectionWidget)
49  m_wAreaSelectionWidget.RemoveFromHierarchy();
50 
51  if (!m_aEnemySpawnPoints)
52  m_aEnemySpawnPoints = new array<SCR_SpawnPoint>();
53  else
54  m_aEnemySpawnPoints.Clear();
55 
56  if (!m_aPlayerSpawnPoints)
57  m_aPlayerSpawnPoints = new array<SCR_SpawnPoint>();
58  else
59  m_aPlayerSpawnPoints.Clear();
60 
61  if (m_RplComponent && m_RplComponent.IsProxy())
62  return;
63 
64  // Spawn setup:
65  // Select random spawn point
66 
67  ref array<SCR_CleanSweepArea> activeAreas = new array<SCR_CleanSweepArea>();
68 
69  int activeAreasCount = GetActiveAreas(activeAreas);
70 
71  if (activeAreasCount < 1)
72  {
73  Print("CleanSweeup has not enough active playable areas. GameMode will not function.", LogLevel.ERROR);
74  return;
75  }
76 
77  SCR_CleanSweepArea area = activeAreas[areaID];
78  if (!area)
79  return;
80 
81  // Move waypoint to the area
82  AIWaypoint wp = AIWaypoint.Cast(GetWorld().FindEntityByName("WP1"));
83  if (wp)
84  wp.SetOrigin(area.GetOrigin());
85 
86  vector centerPos = area.GetOrigin();
87  float rangeSq = area.m_Range * area.m_Range;
88  array<IEntity> entities = new array<IEntity>();
89  array<SCR_SpawnPoint> spawnPoints = SCR_SpawnPoint.GetSpawnPoints();
90  GetWorld().GetActiveEntities(entities);
91 
92  for (int i = spawnPoints.Count() - 1; i >= 0; i--)
93  {
94  SCR_SpawnPoint spawnPoint = spawnPoints[i];
95  if (vector.DistanceSqXZ(centerPos, spawnPoint.GetOrigin()) < rangeSq)
96  {
97  FactionKey faction = spawnPoint.GetFactionKey();
98  if (faction == "USSR")
99  {
100  if (!m_bSwapSides)
101  m_aEnemySpawnPoints.Insert(spawnPoint);
102  else
103  m_aPlayerSpawnPoints.Insert(spawnPoint);
104  }
105  if (faction == "US")
106  {
107  if (!m_bSwapSides)
108  m_aPlayerSpawnPoints.Insert(spawnPoint);
109  else
110  m_aEnemySpawnPoints.Insert(spawnPoint);
111  }
112  }
113  }
114 
115  for (int i = entities.Count() - 1; i >= 0; i--)
116  {
117  // Vehicle spawn component ??
118  SCR_VehicleSpawner vehicleSpawner = SCR_VehicleSpawner.Cast(entities[i].FindComponent(SCR_VehicleSpawner));
119  if (!vehicleSpawner)
120  continue;
121 
122  if (vector.DistanceSqXZ(centerPos, entities[i].GetOrigin()) < rangeSq)
123  vehicleSpawner.PerformSpawn();
124  }
125 
126  SpawnEnemies();
127  m_bAutoPlayerRespawn = true;
128 
129  RespawnPlayers();
130  }
131 
132  //------------------------------------------------------------------------------------------------
133  void RespawnPlayers()
134  {
135  array<int> players = new array<int>();
136  int count = GetGame().GetPlayerManager().GetPlayers(players);
137  for (int i = count - 1; i >= 0; i--)
138  {
139  PlayerController playerController = GetGame().GetPlayerManager().GetPlayerController(players[i]);
140 
141  if (!playerController)
142  continue;
143 
144  SCR_CleanSweepNetworkComponent networkComponent = SCR_CleanSweepNetworkComponent.Cast(playerController.FindComponent(SCR_CleanSweepNetworkComponent));
145  if (!networkComponent)
146  continue;
147 
148  networkComponent.CommitSuicide();
149  }
150  }
151 
152  //------------------------------------------------------------------------------------------------
153  void SetSwapSides(bool swapSides)
154  {
155  m_bSwapSides = swapSides;
156  }
157 
158  //------------------------------------------------------------------------------------------------
159  void SetArea(int targetAreaID /*= areaID this is error*/)
160  {
161  areaID = targetAreaID;
162  OnAreaChanged();
163  Replication.BumpMe();
164 
165  if (m_RplComponent && !m_RplComponent.IsProxy())
166  GetGame().GetCallqueue().CallLater(CheckActiveAreaState, 1, true);
167  }
168 
169  //------------------------------------------------------------------------------------------------
170  void CheckActiveAreaState()
171  {
172  ChimeraCharacter character;
173  DamageManagerComponent damageManager;
174  for (int i = m_aEnemySoldiers.Count() -1; i >= 0; i--)
175  {
176  if (!m_aEnemySoldiers[i])
177  m_aEnemySoldiers.Remove(i);
178  else
179  {
180  character = ChimeraCharacter.Cast(m_aEnemySoldiers[i]);
181  if (!character)
182  {
183  m_aEnemySoldiers.Remove(i);
184  continue;
185  }
186 
187  damageManager = character.GetDamageManager();
188  if (!damageManager)
189  return;
190 
191  if (damageManager.GetState() != EDamageState.DESTROYED)
192  return;
193  }
194  }
195 
196  if (m_RplComponent && !m_RplComponent.IsProxy())
197  {
198  ReplicatedShowHint(0, 5);
199 
200  if (RplSession.Mode() != RplMode.Dedicated)
201  ShowHint(0, 5);
202 
203  areaID = INVALID_AREA_INDEX;
204  Replication.BumpMe();
205  GetGame().GetCallqueue().Remove(CheckActiveAreaState);
206  ShowAreaSelectionToGameMaster();
207  }
208  }
209 
210  //------------------------------------------------------------------------------------------------
211  override void EOnFrame(IEntity owner, float timeSlice)
212  {
213  super.EOnFrame(owner, timeSlice);
214 
215  if (RplSession.Mode() != RplMode.Dedicated && areaID == INVALID_AREA_INDEX && m_wAreaSelectionWidget)
216  {
217  InputManager inputManager = GetGame().GetInputManager();
218  inputManager.ActivateContext("MenuContext");
219  }
220  }
221 
222  //------------------------------------------------------------------------------------------------
223  void ReplicatedShowHint(int hintID, float showTime)
224  {
225  Rpc(RPC_ShowHint, hintID, showTime);
226  }
227 
228  //------------------------------------------------------------------------------------------------
229  [RplRpc(RplChannel.Reliable, RplRcver.Broadcast)]
230  protected void RPC_ShowHint(int hintID, float time)
231  {
232  ShowHint(hintID, time);
233  }
234 
235  //------------------------------------------------------------------------------------------------
236  void ShowHint(int hintID, float showTime)
237  {
238  if (!m_wText || !m_wRoot)
239  return;
240 
241  switch (hintID)
242  {
243  case 0:
244  {
245  m_wText.SetText(ENEMY_PRESENCE_ELIMINATED_TEXT);
246  break;
247  }
248  }
249 
250  AnimateWidget.Opacity(m_wRoot, 1, 1);
251 
252  ScriptCallQueue queue = GetGame().GetCallqueue();
253  queue.CallLater(this.HideHint, showTime * 1000);
254  }
255 
256  //------------------------------------------------------------------------------------------------
257  void HideHint()
258  {
259  if (m_wRoot)
260  AnimateWidget.Opacity(m_wRoot, 0, 1);
261  }
262 
263  //------------------------------------------------------------------------------------------------
264  void SpawnEnemies()
265  {
266  if (m_RplComponent && m_RplComponent.IsProxy())
267  return;
268 
269  if (m_aEnemySpawnPoints.Count() == 0)
270  return;
271 
272  // Clean up groups
273  for (int i = m_aGroups.Count() - 1; i >= 0; i--)
274  {
275  delete m_aGroups[i];
276  }
277  m_aGroups.Clear();
278  m_aEnemySoldiers.Clear();
279 
280  int enemyGroupsCount = Math.ClampInt((Math.Ceil(GetGame().GetPlayerManager().GetPlayerCount() * m_fAiGroupsPerPlayer)), m_iMinAiGroups, m_iMaxAiGroups);
281  for (int i = 0; i < enemyGroupsCount; i++)
282  {
283  RandomGenerator generator = new RandomGenerator;
284  generator.SetSeed(Math.RandomInt(0,100));
285 
286  SCR_SpawnPoint spawnPoint = m_aEnemySpawnPoints.GetRandomElement();
287  if (!spawnPoint)
288  return;
289 
290  vector position = generator.GenerateRandomPointInRadius(0, 2, spawnPoint.GetOrigin());
291  position[1] = spawnPoint.GetOrigin()[1];
292  EntitySpawnParams params = EntitySpawnParams();
293  params.TransformMode = ETransformMode.WORLD;
294  params.Transform[3] = position;
295 
296  Resource res = Resource.Load(m_GroupType);
297  SCR_AIGroup newGrp = SCR_AIGroup.Cast(GetGame().SpawnEntityPrefab(res, null, params));
298  if (!newGrp)
299  continue;
300  m_aGroups.Insert(newGrp);
301 
302  array<AIAgent> agents = new array<AIAgent>;
303 
304  newGrp.GetAgents(agents);
305  foreach (AIAgent agent : agents)
306  {
307  if (agent)
308  m_aEnemySoldiers.Insert(agent.GetControlledEntity());
309  }
310 
311  if (m_AttackWP)
312  newGrp.AddWaypoint(m_AttackWP);
313  }
314  }
315 
316  //------------------------------------------------------------------------------------------------
324  protected override void OnPlayerKilled(int playerId, IEntity playerEntity, IEntity killerEntity, notnull Instigator killer)
325  {
326  super.OnPlayerKilled(playerId, playerEntity, killerEntity, killer);
327  m_aEnemySoldiers.RemoveItemOrdered(playerEntity);
328  }
329 
330  //------------------------------------------------------------------------------------------------
331  vector GetPlayersCenter()
332  {
333  array<int> players = new array<int>();
334  int count = GetGame().GetPlayerManager().GetPlayers(players);
335 
336  vector center = vector.Zero;
337  int countedEntities = 0;
338 
339  for (int i = count - 1; i >= 0; i--)
340  {
341  IEntity controlledEntity = SCR_PossessingManagerComponent.GetPlayerMainEntity(players[i]);
342  if (!controlledEntity)
343  continue;
344 
345  vector entityPosition = controlledEntity.GetOrigin();
346  center[0] = center[0] + entityPosition[0];
347  center[1] = center[1] + entityPosition[1];
348  center[2] = center[2] + entityPosition[2];
349 
350  countedEntities++;
351  }
352 
353  if (countedEntities != 0)
354  {
355  center[0] = center[0] / countedEntities;
356  center[1] = center[1] / countedEntities;
357  center[2] = center[2] / countedEntities;
358  }
359 
360  return center;
361  }
362 
363  //------------------------------------------------------------------------------------------------
364  int GetActiveAreas(array<SCR_CleanSweepArea> activeAreas)
365  {
366  int count = 0;
367 
368  array<SCR_CleanSweepArea> areas = SCR_CleanSweepArea.s_aInstances;
369  if (!areas)
370  return count;
371 
372  int areasCount = areas.Count();
373  for (int i = 0; i < areasCount; i++)
374  {
375  SCR_CleanSweepArea area = areas[i];
376  if (!area)
377  continue;
378 
379  if (!area.m_Active)
380  continue;
381 
382  activeAreas.Insert(area);
383  count++;
384  }
385 
386  return count;
387  }
388 
389  //------------------------------------------------------------------------------------------------
390  void InitializeServer()
391  {
392  m_AttackWP = AIWaypoint.Cast(GetWorld().FindEntityByName("WP1"));
393  m_aGroups = new array<SCR_AIGroup>;
394  m_aPlayerSpawnPoints = new array<SCR_SpawnPoint>;
395  m_aEnemySpawnPoints = new array<SCR_SpawnPoint>;
396  }
397 
398  //------------------------------------------------------------------------------------------------
399  PlayerController GetGameMaster()
400  {
401  PlayerController playerController = GetGame().GetPlayerManager().GetPlayerController(m_iGameMasterID);
402  if (!playerController)
403  return PickNewGameMaster();
404 
405  return playerController;
406  }
407 
408  //------------------------------------------------------------------------------------------------
409  PlayerController PickNewGameMaster(int exclude = -1)
410  {
411  PlayerController playerController;
412 
413  array<int> players = {};
414  int count = GetGame().GetPlayerManager().GetPlayers(players);
415  for (int i = 0; i < count; i++)
416  {
417  if (players[i] == exclude)
418  continue;
419 
420  playerController = GetGame().GetPlayerManager().GetPlayerController(players[i]);
421  if (playerController)
422  {
423  m_iGameMasterID = players[i];
424  break;
425  }
426  }
427 
428  return playerController;
429  }
430 
431  //------------------------------------------------------------------------------------------------
432  void ShowAreaSelectionToGameMaster()
433  {
434  PlayerController playerController = GetGameMaster();
435  if (!playerController)
436  return;
437 
438  SCR_CleanSweepNetworkComponent networkComponent = SCR_CleanSweepNetworkComponent.Cast(playerController.FindComponent(SCR_CleanSweepNetworkComponent));
439  if (!networkComponent)
440  return;
441 
442  networkComponent.ShowAreaSelectionScreen();
443  return;
444  }
445 
446  //------------------------------------------------------------------------------------------------
447  void ShowAreaSelectionUI()
448  {
449  if (areaID != INVALID_AREA_INDEX || m_AreaSelectionLayout.GetPath() == string.Empty)
450  return;
451 
452  if (!m_wAreaSelectionWidget)
453  m_wAreaSelectionWidget = GetGame().GetWorkspace().CreateWidgets(m_AreaSelectionLayout);
454 
455  XComboBoxWidget selectionBox = XComboBoxWidget.Cast(m_wAreaSelectionWidget.FindAnyWidget("SelectionBox"));
456  if (!selectionBox)
457  return;
458 
459  array<SCR_CleanSweepArea> activeAreas = {};
460 
461  int count = GetActiveAreas(activeAreas);
462 
463  for (int i = 0; i < count; i++)
464  {
465  selectionBox.AddItem(activeAreas[i].GetName());
466  }
467 
468  selectionBox.SetCurrentItem(0);
469  }
470 
471  //------------------------------------------------------------------------------------------------
472  override void OnPlayerRegistered(int playerId)
473  {
474  super.OnPlayerRegistered(playerId);
475  Replication.BumpMe();
476  if (areaID != INVALID_AREA_INDEX)
477  return;
478 
479  ShowAreaSelectionToGameMaster();
480  //GetGame().GetCallqueue().CallLater(ShowAreaSelectionToGameMaster, 10000, false);
481  }
482 
483  //------------------------------------------------------------------------------------------------
484  override void OnPlayerDisconnected(int playerId, KickCauseCode cause, int timeout)
485  {
486  super.OnPlayerDisconnected(playerId, cause, timeout);
487 
488  Replication.BumpMe();
489  if (areaID != INVALID_AREA_INDEX || m_iGameMasterID != playerId)
490  return;
491 
492  PickNewGameMaster(m_iGameMasterID);
493  ShowAreaSelectionToGameMaster();
494  }
495 
496  //------------------------------------------------------------------------------------------------
497  override void EOnInit(IEntity owner)
498  {
499  super.EOnInit(owner);
500  m_bAutoPlayerRespawn = false;
501 
502  if (m_RplComponent && !m_RplComponent.IsProxy())
503  {
505  if (gameMode)
506  gameMode.GetOnPlayerRegistered().Insert(OnPlayerRegistered);
507  }
508 
509  m_wRoot = GetGame().GetWorkspace().CreateWidgets("{DE9F713BE2C5D190}UI/layouts/HUD/HintFrame.layout");
510  m_wText = TextWidget.Cast(m_wRoot.FindAnyWidget("MessageText"));
511  TextWidget title = TextWidget.Cast(m_wRoot.FindAnyWidget("TitleText"));
512  if (title)
513  title.SetText("CLEAN SWEEP");
514 
515  m_wRoot.SetOpacity(0);
516 
517  if (m_RplComponent && !m_RplComponent.IsProxy())
518  InitializeServer();
519  }
520 };
SCR_BaseGameMode
Definition: SCR_BaseGameMode.c:137
GetName
string GetName()
Definition: SCR_ScenarioFrameworkLayerBase.c:85
m_wRoot
protected Widget m_wRoot
Definition: SCR_ScenarioFrameworkLayerTaskDefend.c:59
SCR_CleanSweepArea
Definition: SCR_CleanSweepArea.c:7
RplProp
SCR_RplTestEntityClass RplProp
Used for handling entity spawning requests for SCR_CatalogEntitySpawnerComponent and inherited classe...
GetGame
ArmaReforgerScripted GetGame()
Definition: game.c:1424
EDamageState
EDamageState
Definition: EDamageState.c:12
desc
UI Textures DeployMenu Briefing conflict_HintBanner_1_UI desc
Definition: SCR_RespawnBriefingComponent.c:17
RplRpc
SCR_AchievementsHandlerClass ScriptComponentClass RplRpc(RplChannel.Reliable, RplRcver.Owner)] void UnlockOnClient(AchievementId achievement)
Definition: SCR_AchievementsHandler.c:11
SCR_SpawnPoint
Spawn point entity defines positions on which players can possibly spawn.
Definition: SCR_SpawnPoint.c:27
FindEntityByName
IEntity FindEntityByName(string name)
Definition: SCR_ScenarioFrameworkActionsGetters.c:40
KickCauseCode
KickCauseCode
Definition: KickCauseCode.c:19
Instigator
Definition: Instigator.c:6
GetGameMode
SCR_BaseGameMode GetGameMode()
Definition: SCR_BaseGameModeComponent.c:15
Attribute
SCR_GameModeCleanSweepClass Attribute
Post-process effect of scripted camera.
SCR_VehicleSpawner
void SCR_VehicleSpawner(IEntityComponentSource src, IEntity ent, IEntity parent)
Definition: SCR_VehicleSpawner.c:86
SCR_BaseGameModeClass
Definition: SCR_BaseGameMode.c:12
m_RplComponent
protected RplComponent m_RplComponent
Definition: SCR_CampaignBuildingManagerComponent.c:42
SCR_GameModeCleanSweep
Definition: SCR_GameModeCleanSweep.c:7
SCR_AIGroup
Definition: SCR_AIGroup.c:68
SCR_GameModeCleanSweepClass
Definition: SCR_GameModeCleanSweep.c:2
params
Configs ServerBrowser KickDialogs params
Definition: SCR_NotificationSenderComponent.c:24
GetPlayerCount
int GetPlayerCount()
Definition: SCR_Faction.c:365
DamageManagerComponent
Definition: DamageManagerComponent.c:12
position
vector position
Definition: SCR_DestructibleTreeV2.c:30
SCR_CleanSweepNetworkComponent
void SCR_CleanSweepNetworkComponent(IEntityComponentSource src, IEntity ent, IEntity parent)
Definition: SCR_CleanSweepNetworkComponent.c:97