Arma Reforger Explorer  1.1.0.42
Arma Reforger Code Explorer by Zeroy - Thanks to MisterOutofTime
SCR_DataCollectorShootingModule.c
Go to the documentation of this file.
3 {
4  //TODO: Remove this map
5  protected ref map<int, IEntity> m_mTrackedPossibleShooters = new map<int, IEntity>();
6 
7  //------------------------------------------------------------------------------------------------
8  protected override void AddInvokers(IEntity player)
9  {
10  super.AddInvokers(player);
11  if (!player)
12  return;
13 
14  SCR_CompartmentAccessComponent compartmentAccessComponent = SCR_CompartmentAccessComponent.Cast(player.FindComponent(SCR_CompartmentAccessComponent));
15 
16  if (!compartmentAccessComponent)
17  return;
18 
19  compartmentAccessComponent.GetOnCompartmentEntered().Insert(OnCompartmentEntered);
20  compartmentAccessComponent.GetOnCompartmentLeft().Insert(OnCompartmentLeft);
21 
22  EventHandlerManagerComponent eventHandlerManager = EventHandlerManagerComponent.Cast(player.FindComponent(EventHandlerManagerComponent));
23  if (!eventHandlerManager)
24  return;
25 
26  eventHandlerManager.RegisterScriptHandler("OnProjectileShot", this, OnWeaponFired);
27  eventHandlerManager.RegisterScriptHandler("OnGrenadeThrown", this, OnGrenadeThrown);
28  }
29 
30  //------------------------------------------------------------------------------------------------
31  protected void OnWeaponFired(int playerID, BaseWeaponComponent weapon, IEntity entity)
32  {
33  SCR_PlayerData playerData = GetGame().GetDataCollector().GetPlayerData(playerID);
34 
35  //In the future we will use Weapon.GetWeaponType() and Weapon.GetWeaponSubtype() to determine the weapon shot and add it to the player's profile
36  //For now, simply count a shot
37  playerData.AddStat(SCR_EDataStats.SHOTS, 1);
38  }
39 
40  //------------------------------------------------------------------------------------------------
41  protected void OnGrenadeThrown(int playerID, BaseWeaponComponent weapon, IEntity entity)
42  {
43  if (!weapon)
44  return;
45 
46  SCR_PlayerData playerData = GetGame().GetDataCollector().GetPlayerData(playerID);
47 
48  //Not counting smoke grenade 'cause it deals no damage. TO DO: Count it as a different specialization operation
49  if (weapon.GetWeaponType() == EWeaponType.WT_SMOKEGRENADE)
50  return;
51 
52  playerData.AddStat(SCR_EDataStats.GRENADES_THROWN, 1);
53  }
54 
55  //------------------------------------------------------------------------------------------------
56  protected override void RemoveInvokers(IEntity player)
57  {
58  super.RemoveInvokers(player);
59  if (!player)
60  return;
61 
62  SCR_CompartmentAccessComponent compartmentAccessComponent = SCR_CompartmentAccessComponent.Cast(player.FindComponent(SCR_CompartmentAccessComponent));
63 
64  if (!compartmentAccessComponent)
65  return;
66 
67  compartmentAccessComponent.GetOnCompartmentEntered().Remove(OnCompartmentEntered);
68  compartmentAccessComponent.GetOnCompartmentLeft().Remove(OnCompartmentLeft);
69 
70  EventHandlerManagerComponent eventHandlerManager = EventHandlerManagerComponent.Cast(player.FindComponent(EventHandlerManagerComponent));
71  if (!eventHandlerManager)
72  return;
73 
74  eventHandlerManager.RemoveScriptHandler("OnProjectileShot", this, OnWeaponFired);
75  eventHandlerManager.RemoveScriptHandler("OnGrenadeThrown", this, OnGrenadeThrown);
76  }
77 
78  //Players who enter a vehicle do not need to be tracked as possible shooters, unless using a turret
79  //------------------------------------------------------------------------------------------------
80  protected void OnCompartmentEntered(IEntity targetEntity, BaseCompartmentManagerComponent manager, int mgrID, int slotID, bool move)
81  {
82  BaseCompartmentSlot compartment = manager.FindCompartment(slotID, mgrID);
83 
84  //Turrets can shoot
85  if (!compartment || !compartment.GetOccupant() || compartment.GetType() == ECompartmentType.Turret)
86  return;
87 
88  PlayerManager playerManager = GetGame().GetPlayerManager();
89 
90  int playerID = playerManager.GetPlayerIdFromControlledEntity(compartment.GetOccupant());
91 
92  m_mTrackedPossibleShooters.Remove(playerID);
93  }
94 
95  //Players who leave a vehicle can be shooters
96  //------------------------------------------------------------------------------------------------
97  protected void OnCompartmentLeft(IEntity targetEntity, BaseCompartmentManagerComponent manager, int mgrID, int slotID, bool move)
98  {
99  BaseCompartmentSlot compartment = manager.FindCompartment(slotID, mgrID);
100  if (!compartment)
101  return;
102 
103  if (!compartment.GetOccupant())
104  return;
105 
106  PlayerManager playerManager = GetGame().GetPlayerManager();
107 
108  int playerID = playerManager.GetPlayerIdFromControlledEntity(compartment.GetOccupant());
109  if (playerID == 0) // Non-player character
110  return;
111 
112  m_mTrackedPossibleShooters.Insert(playerID, compartment.GetOccupant());
113  }
114 
115  //------------------------------------------------------------------------------------------------
116  override void OnPlayerDisconnected(int playerID, IEntity controlledEntity = null)
117  {
118  controlledEntity = m_mTrackedPossibleShooters.Get(playerID);
119  super.OnPlayerDisconnected(playerID, controlledEntity);
120 
121  m_mTrackedPossibleShooters.Remove(playerID);
122  }
123 
124  //------------------------------------------------------------------------------------------------
125  override void OnPlayerSpawned(int playerID, IEntity controlledEntity)
126  {
127  super.OnPlayerSpawned(playerID, controlledEntity);
128  m_mTrackedPossibleShooters.Insert(playerID, controlledEntity);
129  }
130 
131 #ifdef ENABLE_DIAG
132  //------------------------------------------------------------------------------------------------
133  override void OnControlledEntityChanged(IEntity from, IEntity to)
134  {
135  super.OnControlledEntityChanged(from, to);
136 
137  if (to)
138  {
139  int playerID = GetGame().GetPlayerManager().GetPlayerIdFromControlledEntity(to);
140  m_mTrackedPossibleShooters.Insert(playerID, to);
141  }
142  else if (from)
143  {
144  int playerID = GetGame().GetPlayerManager().GetPlayerIdFromControlledEntity(from);
145  m_mTrackedPossibleShooters.Remove(playerID);
146  }
147  }
148 #endif
149 
150  //------------------------------------------------------------------------------------------------
151  override void OnAIKilled(IEntity AIEntity, IEntity killerEntity, notnull Instigator killer)
152  {
153  super.OnAIKilled(AIEntity, killerEntity, killer);
154 
155  //This method adds a kill no matter the mean by which the AI was killed.
156  //The name of the module is a little bit misleading
157  if (killer.GetInstigatorType() != InstigatorType.INSTIGATOR_PLAYER)
158  return;
159 
160  SCR_ChimeraCharacter AIEntityChar = SCR_ChimeraCharacter.Cast(AIEntity);
161  if (!AIEntityChar)
162  return;
163 
164  int killerId = killer.GetInstigatorPlayerID();
165 
166  SCR_FactionManager factionManager = SCR_FactionManager.Cast(GetGame().GetFactionManager());
167  if (!factionManager)
168  return;
169 
170  Faction factionKiller = Faction.Cast(factionManager.GetPlayerFaction(killerId));
171  if (!factionKiller)
172  return;
173 
174  SCR_PlayerData killerData = GetGame().GetDataCollector().GetPlayerData(killerId);
175 
176  //Add an AI kill. Find if friendly or unfriendly
177  if (factionKiller.IsFactionFriendly(AIEntityChar.GetFaction()))
178  killerData.AddStat(SCR_EDataStats.FRIENDLY_AI_KILLS);
179  else
180  killerData.AddStat(SCR_EDataStats.AI_KILLS);
181  }
182 
183  //------------------------------------------------------------------------------------------------
184  override void OnPlayerKilled(int playerId, IEntity playerEntity, IEntity killerEntity, notnull Instigator killer)
185  {
186  super.OnPlayerKilled(playerId, playerEntity, killerEntity, killer);
187  m_mTrackedPossibleShooters.Remove(playerId);
188 
189  SCR_PlayerData playerData = GetGame().GetDataCollector().GetPlayerData(playerId);
190  playerData.AddStat(SCR_EDataStats.DEATHS);
191 
192  if (killer.GetInstigatorType() != InstigatorType.INSTIGATOR_PLAYER)
193  return;
194 
195  SCR_ChimeraCharacter playerEntityChar = SCR_ChimeraCharacter.Cast(playerEntity);
196  if (!playerEntityChar)
197  return;
198 
199  int killerId = killer.GetInstigatorPlayerID();
200 
201  // Suicide?
202  if (playerId == killerId)
203  return;
204 
205  SCR_PlayerData killerData = GetGame().GetDataCollector().GetPlayerData(killerId);
206 
207  SCR_FactionManager factionManager = SCR_FactionManager.Cast(GetGame().GetFactionManager());
208  if (!factionManager)
209  return;
210 
211  Faction factionKiller = Faction.Cast(factionManager.GetPlayerFaction(killerId));
212  if (!factionKiller)
213  return;
214 
215  //Add a kill. Find if friendly or unfriendly
216  if (factionKiller.IsFactionFriendly(playerEntityChar.GetFaction()))
217  killerData.AddStat(SCR_EDataStats.FRIENDLY_KILLS);
218  else
219  killerData.AddStat(SCR_EDataStats.KILLS);
220  }
221 
222  //------------------------------------------------------------------------------------------------
223  override void Update(float timeTick)
224  {
225  //If there's no data collector, do nothing
226  if (!GetGame().GetDataCollector())
227  return;
228 
229  m_fTimeSinceUpdate += timeTick;
230 
231  if (m_fTimeSinceUpdate < m_fUpdatePeriod)
232  return;
233 
234  SCR_PlayerData playerData;
235  int playerId;
236 
237  for (int i = m_mTrackedPossibleShooters.Count() - 1; i >= 0; i--)
238  {
239 
240  playerId = m_mTrackedPossibleShooters.GetKey(i);
241  playerData = GetGame().GetDataCollector().GetPlayerData(playerId);
242 
243  //DEBUG display
244 #ifdef ENABLE_DIAG
245  if (m_StatsVisualization)
246  {
247  m_StatsVisualization.Get(SCR_EShootingModuleStats.DEATHS).SetText((playerData.GetStat(SCR_EDataStats.DEATHS) - playerData.GetStat(SCR_EDataStats.DEATHS, false)).ToString());
248  m_StatsVisualization.Get(SCR_EShootingModuleStats.PLAYERKILLS).SetText((playerData.GetStat(SCR_EDataStats.KILLS) - playerData.GetStat(SCR_EDataStats.KILLS, false)).ToString());
249  m_StatsVisualization.Get(SCR_EShootingModuleStats.AIKILLS).SetText((playerData.GetStat(SCR_EDataStats.AI_KILLS) - playerData.GetStat(SCR_EDataStats.AI_KILLS, false)).ToString());
250  m_StatsVisualization.Get(SCR_EShootingModuleStats.FRIENDLYPLAYERKILLS).SetText((playerData.GetStat(SCR_EDataStats.FRIENDLY_KILLS) - playerData.GetStat(SCR_EDataStats.FRIENDLY_KILLS, false)).ToString());
251  m_StatsVisualization.Get(SCR_EShootingModuleStats.FRIENDLYAIKILLS).SetText((playerData.GetStat(SCR_EDataStats.FRIENDLY_AI_KILLS) - playerData.GetStat(SCR_EDataStats.FRIENDLY_AI_KILLS, false)).ToString());
252  m_StatsVisualization.Get(SCR_EShootingModuleStats.BULLETSSHOT).SetText((playerData.GetStat(SCR_EDataStats.SHOTS) - playerData.GetStat(SCR_EDataStats.SHOTS, false)).ToString());
253  m_StatsVisualization.Get(SCR_EShootingModuleStats.GRENADESTHROWN).SetText((playerData.GetStat(SCR_EDataStats.GRENADES_THROWN) - playerData.GetStat(SCR_EDataStats.GRENADES_THROWN, false)).ToString());
254  }
255 #endif
256  }
257  m_fTimeSinceUpdate = 0;
258  }
259 
260 #ifdef ENABLE_DIAG
261  //------------------------------------------------------------------------------------------------
262  override void CreateVisualization()
263  {
264  super.CreateVisualization();
265  if (!m_StatsVisualization)
266  return;
267 
268  CreateEntry("Deaths: ", 0, SCR_EShootingModuleStats.DEATHS);
269  CreateEntry("Player Kills: ", 0, SCR_EShootingModuleStats.PLAYERKILLS);
270  CreateEntry("AI Kills: ", 0, SCR_EShootingModuleStats.AIKILLS);
271  CreateEntry("Friendly Player Kills: ", 0, SCR_EShootingModuleStats.FRIENDLYPLAYERKILLS);
272  CreateEntry("Friendly AI Kills: ", 0, SCR_EShootingModuleStats.FRIENDLYAIKILLS);
273  CreateEntry("Bullets Shot: ", 0, SCR_EShootingModuleStats.BULLETSSHOT);
274  CreateEntry("Grenades Thrown: ", 0, SCR_EShootingModuleStats.GRENADESTHROWN);
275  }
276 #endif
277 };
278 
279 #ifdef ENABLE_DIAG
280 enum SCR_EShootingModuleStats
281 {
282  DEATHS,
283  PLAYERKILLS,
284  AIKILLS,
285  FRIENDLYPLAYERKILLS,
286  FRIENDLYAIKILLS,
287  BULLETSSHOT,
288  GRENADESTHROWN
289 };
290 #endif
InstigatorType
InstigatorType
Definition: InstigatorType.c:12
SCR_CompartmentAccessComponent
Definition: SCR_CompartmentAccessComponent.c:15
m_fTimeSinceUpdate
protected float m_fTimeSinceUpdate
Definition: SCR_RestrictedDeployableSpawnPointComponent.c:89
OnControlledEntityChanged
protected void OnControlledEntityChanged(IEntity from, IEntity to)
Runs every time the controlled entity has been changed.
Definition: SCR_ItemPlacementComponent.c:113
GetGame
ArmaReforgerScripted GetGame()
Definition: game.c:1424
ECompartmentType
ECompartmentType
Definition: ECompartmentType.c:7
SCR_PlayerData
Definition: SCR_PlayerData.c:2
Instigator
Definition: Instigator.c:6
BaseWeaponComponent
Definition: BaseWeaponComponent.c:12
DEATHS
@ DEATHS
Deaths.
Definition: SCR_PlayerData.c:966
Faction
Definition: Faction.c:12
SCR_EDataStats
SCR_EDataStats
Definition: SCR_PlayerData.c:950
SCR_DataCollectorShootingModule
Definition: SCR_DataCollectorShootingModule.c:2
EWeaponType
EWeaponType
Definition: EWeaponType.c:12
GetDataCollector
SCR_DataCollectorComponent GetDataCollector()
Definition: game.c:90
SCR_FactionManager
void SCR_FactionManager(IEntitySource src, IEntity parent)
Definition: SCR_FactionManager.c:461
PlayerManager
Definition: PlayerManager.c:12
SCR_DataCollectorModule
Definition: SCR_DataCollectorModule.c:2
BaseContainerProps
SCR_AIGoalReaction_Follow BaseContainerProps
Handles insects that are supposed to be spawned around selected prefabs defined in prefab names array...
Definition: SCR_AIGoalReaction.c:468