Arma Reforger Explorer  1.1.0.42
Arma Reforger Code Explorer by Zeroy - Thanks to MisterOutofTime
SCR_FiringRangeTarget.c
Go to the documentation of this file.
1 [EntityEditorProps(category: "GameScripted/FiringRange", description: "Handeles behaviour of target. Evaluates a hit.", color: "0 0 255 255")]
3 {
4 
5 };
6 
8 {
9  TARGET_UP = 0,
11 };
12 
14 {
15  TRAINING = 1,
17 };
18 
19 class SCR_FiringRangeTarget : BaseBuilding
20 {
21 
23  protected int m_iDesiredState;
24 
26  [Attribute("3", UIWidgets.Slider, "Target in stays in folded position for... seconds", "1 100 3")]
27  protected float m_fTimeInFoldedPosition;
28 
30  [Attribute("1", UIWidgets.Slider, "How far is the target from shooting line.", "1 9999 50")]
31  protected int m_iTargetDistance;
32 
34  [Attribute("3", UIWidgets.Slider, "Target stays in erected position for... seconds", "0.5 10 0.5")]
35  protected float m_fTimeInErectedPosition;
36 
38  [Attribute( defvalue: "0", uiwidget: UIWidgets.CheckBox, desc: "Set if the target has to be used as hit indicator." )]
39  protected bool m_bIsIndicator;
40 
41  // If true, target is reset to standing position after set time
42  [Attribute( defvalue: "1", uiwidget: UIWidgets.CheckBox, desc: "Set if the target should be reset to erected position." ), RplProp()]
43  private bool m_bAutoResetTarget;
44 
45  //Internal value for keeping target down for a while
46  [RplProp()]
47  protected bool m_bWaitInPosition = false;
48 
49  // Signal manager to pass signals into proc anim
50  private SignalsManagerComponent m_SignalManager;
51  private int m_iTargetHitSignal;
52 
53  // Firing range controller
54  private SCR_FiringRangeController m_LineController;
55  private SCR_FiringRangeManager m_FiringRangeManager;
56 
57  // Proc anim component
58  private ProcAnimComponent m_ProcAnimComponent;
59 
60  // RPL component
61  private RplComponent m_RplComponent;
62 
63  // time for how long the target should stay folded
64  protected float m_fFallenTargetTimer;
65 
66  // Variable preventing multiple hit evaluation
67  protected bool m_bTargetHit = false;
68 
69  // Owner of the target (parent entity)
70  private IEntity m_OwnerEntity = null;
71 
72  // Target owenr (player)
73  private int m_iTargetOwner = -1;
74 
75  //--- Target modes
76  private int m_iTargetMode = -1;
77 
78  // value of hit (score)
79  private int m_iHitValue;
80 
81  // state of the target for JIP
82  private int m_iTargetState;
83 
84  private IEntity m_FiringLineController;
85 
86  ref ScriptInvoker Event_TargetChangeState = new ScriptInvoker;
87 
88  // target scoring points
89  const float SCORE_CENTER = 10;
90  const float SCORE_CIRCLE_1 = 5;
91  const float SCORE_CIRCLE_2 = 3;
92  const float SCORE_CIRCLE_3 = 1;
93 
94  // definition of areas on target for scoring
95  // 10 points area
96  const float AREA_10_W_LEFT = 0.03;
97  const float AREA_10_W_RIGHT = -0.03;
98  const float AREA_10_H_UP = 1.15;
99  const float AREA_10_H_DOWN = 1.03;
100 
101  // 5 points area
102  const float AREA_5_W_LEFT = 0.08;
103  const float AREA_5_W_RIGHT = -0.08;
104  const float AREA_5_H_UP = 1.2;
105  const float AREA_5_H_DOWN = 0.98;
106 
107  // 3 points area
108  const float AREA_3_W_LEFT = 0.13;
109  const float AREA_3_W_RIGHT = -0.13;
110  const float AREA_3_H_UP = 1.32;
111  const float AREA_3_H_DOWN = 0.84;
112 
113  // 1 point area
114  const float AREA_1_W_LEFT = 0.28;
115  const float AREA_1_W_RIGHT = -0.28;
116  const float AREA_1_H_UP = 1.77;
117  const float AREA_1_H_DOWN = 0.5;
118 
119  const float AREA_BOTTOM_VALID = 0.40;
120 
121  //------------------------------------------------------------------------------------------------
122  override void OnDamage(float damage,
124  IEntity pHitEntity,
125  inout vector outMat[3], // Hit position/direction/normal
126  IEntity damageSource,
127  notnull Instigator instigator,
128  int colliderID,
129  float speed)
130  {
131 
132  if (IsProxy())
133  return;
134 
135  // don't continue if projectile hit the stand of the target.
136  if (!IsHitValid(CoordToLocal(outMat[0])))
137  return;
138 
139  super.OnDamage(damage,type,pHitEntity,outMat,damageSource,instigator,colliderID,speed);
140 
141  // check if target is in erected state
142  if (GetState() != ETargetState.TARGET_UP)
143  return;
144 
145  // target was already hit. Multiple execution check
146  if (m_bTargetHit)
147  return;
148  m_bTargetHit = true;
149 
150  SetState(ETargetState.TARGET_DOWN);
151 
152  // activate entity EOnFrame
153  SetEventMask(EntityEvent.FRAME);
154 
155  // get lobby
156  // get ID of the player who hits the target
157  int playerID = instigator.GetInstigatorPlayerID();
158 
159  // check if the player is also the owner of the target or the target is in training mode
160  if (m_iTargetOwner == playerID || m_iTargetMode == ETargetMode.TRAINING)
161  {
162  // Check if the Firing range manager exists
163  if (!m_FiringRangeManager)
164  return;
165 
166  if (GetValidityOfHit(CoordToLocal(outMat[0])))
167  m_FiringRangeManager.AddIndicator(CoordToLocal(outMat[0]),VectorToLocal(outMat[2] * -0.1), pHitEntity);
168  }
169 
170  // only if owner of the target is the player, count score
171  if (m_iTargetOwner == playerID && m_iTargetMode == ETargetMode.COMPETITION)
172  {
173  GetHitValue(CoordToLocal(outMat[0]),playerID);
174  }
175  }
176 
177  //------------------------------------------------------------------------------------------------
179  void SetState(int state)
180  {
181  Rpc(SetStateMP, state);
182  SetStateMP(state);
183  Replication.BumpMe();
184  }
185 
186  [RplRpc(RplChannel.Reliable, RplRcver.Broadcast)]
187  void SetStateMP(int state)
188  {
189  m_iDesiredState = state;
190  Event_TargetChangeState.Invoke(state, this);
191  if (m_iDesiredState == ETargetState.TARGET_DOWN)
192  {
193  // Reset time for how long the target should lie down.
194  m_fFallenTargetTimer = 0;
195  // Start timer which will erect target.
196  m_bWaitInPosition = true;
197  // Animate target
198  m_SignalManager.SetSignalValue(m_iTargetHitSignal, ETargetState.TARGET_DOWN);
199  // Play sound
200  SCR_SoundManagerEntity soundManagerEntity = GetGame().GetSoundManagerEntity();
201  if (soundManagerEntity)
202  {
203  soundManagerEntity.CreateAndPlayAudioSource(this, SCR_SoundEvent.SOUND_TARGET_DOWN);
204  }
205  // Count the targets
206  if (m_LineController)
207  m_LineController.CountPopUpTargets();
208  Replication.BumpMe();
209  }
210  else
211  {
212  m_fFallenTargetTimer = 0;
213  m_bTargetHit = false;
214  m_bWaitInPosition = false;
215  // Animate target
216  m_SignalManager.SetSignalValue(m_iTargetHitSignal, ETargetState.TARGET_UP);
217  // Play sound
218  SCR_SoundManagerEntity soundManagerEntity = GetGame().GetSoundManagerEntity();
219  if (soundManagerEntity)
220  {
221  soundManagerEntity.CreateAndPlayAudioSource(this, SCR_SoundEvent.SOUND_TARGET_UP);
222  }
223  Replication.BumpMe();
224  }
225  }
226 
227  //------------------------------------------------------------------------------------------------
229  bool IsProxy()
230  {
231  return (m_RplComponent && m_RplComponent.IsProxy());
232  }
233 
234  //------------------------------------------------------------------------------------------------
236  int GetState()
237  {
238  return m_SignalManager.GetSignalValue(m_SignalManager.FindSignal("target_hit"));
239  }
240 
241  //------------------------------------------------------------------------------------------------
243  int GetSetDistance()
244  {
245  return m_iTargetDistance;
246  }
247 
248  //------------------------------------------------------------------------------------------------
250  int GetTargetOwner()
251  {
252  return m_iTargetOwner;
253  }
254 
255  //------------------------------------------------------------------------------------------------
257  void SetTargetOwner(int playerID)
258  {
259  m_iTargetOwner = playerID;
260  }
261 
262  //------------------------------------------------------------------------------------------------
264  void SetTargetMode(int targetMode)
265  {
266  m_iTargetMode = targetMode;
267  }
268 
269  //------------------------------------------------------------------------------------------------
271  int GetErectedDuration()
272  {
273  //recalculate seconds to miliseconds
274  return m_fTimeInErectedPosition * 1000;
275  }
276 
277  //------------------------------------------------------------------------------------------------
279  bool GetValidityOfHit(vector coordOfHit)
280  {
281  float HitY = coordOfHit[1];
282  if (HitY > AREA_1_H_DOWN)
283  return true;
284 
285  return false;
286  }
287 
288  //------------------------------------------------------------------------------------------------
290  void SetAutoResetTarget(bool state)
291  {
292  m_bAutoResetTarget = state;
293  }
294 
295  //------------------------------------------------------------------------------------------------
297  bool IsHitValid(vector coordOfHit)
298  {
299  float HitX = coordOfHit[0];
300  float HitY = coordOfHit[1];
301 
302  if (HitX < AREA_1_W_LEFT && HitX > AREA_1_W_RIGHT && HitY < AREA_1_H_UP && HitY > AREA_BOTTOM_VALID)
303  return true;
304 
305  return false;
306  }
307 
308  //------------------------------------------------------------------------------------------------
310  bool IsIndicator()
311  {
312  return m_bIsIndicator;
313  }
314 
315  //------------------------------------------------------------------------------------------------
317  void GetHitValue(vector coordOfHit, int playerID)
318  {
319  PlayerController playerController = GetGame().GetPlayerManager().GetPlayerController(playerID);
320  if (!playerController)
321  return;
322 
323  float HitX = coordOfHit[0];
324  float HitY = coordOfHit[1];
325 
326  if (HitX < AREA_10_W_LEFT && HitX > AREA_10_W_RIGHT && HitY < AREA_10_H_UP && HitY > AREA_10_H_DOWN)
327  m_iHitValue = SCORE_CENTER;
328 
329  else if (HitX < AREA_5_W_LEFT && HitX > AREA_5_W_RIGHT && HitY < AREA_5_H_UP && HitY > AREA_5_H_DOWN)
330  m_iHitValue = SCORE_CIRCLE_1;
331 
332  else if (HitX < AREA_3_W_LEFT && HitX > AREA_3_W_RIGHT && HitY < AREA_3_H_UP && HitY > AREA_3_H_DOWN)
333  m_iHitValue = SCORE_CIRCLE_2;
334 
335  else if (HitX < AREA_1_W_LEFT && HitX > AREA_1_W_RIGHT && HitY < AREA_1_H_UP && HitY > AREA_1_H_DOWN)
336  m_iHitValue = SCORE_CIRCLE_3;
337 
338  m_FiringRangeManager.CountPlayerScore(m_iTargetOwner, m_iHitValue);
339  }
340 
341  //------------------------------------------------------------------------------------------------
342  override void EOnFrame(IEntity owner, float timeSlice)
343  {
344  // This counter runs, if the target was animated down after hit and at the same time m_bAutoResetTarget is true (that means target is in default mode, not in competition one)
345  if (m_bWaitInPosition && m_bAutoResetTarget)
346  {
347  if (m_fFallenTargetTimer < m_fTimeInFoldedPosition)
348  {
349  m_fFallenTargetTimer+= timeSlice;
350  }
351  else
352  {
353  // Flip target back to standing position
354  SetState(ETargetState.TARGET_UP);
355  // deactivate entity EOnFrame
356  ClearEventMask(EntityEvent.FRAME);
357  }
358  }
359  }
360 
361  //------------------------------------------------------------------------------------------------
362  override bool RplSave(ScriptBitWriter writer)
363  {
364  // Save targets state.
365  m_iTargetState = GetState();
366  writer.Write(m_iTargetState, 1);
367  return true;
368  }
369 
370  //------------------------------------------------------------------------------------------------
371  override bool RplLoad(ScriptBitReader reader)
372  {
373  // Load targets state.
374  if (!reader.Read(m_iTargetState, 1)) return false;
375  SetState(m_iTargetState);
376  return true;
377  }
378 
379  //------------------------------------------------------------------------------------------------
380  override void EOnInit(IEntity owner)
381  {
382  m_SignalManager = SignalsManagerComponent.Cast(FindComponent(SignalsManagerComponent));
383  m_ProcAnimComponent = ProcAnimComponent.Cast(FindComponent(ProcAnimComponent));
384 
385  if (!m_SignalManager)
386  return;
387 
388  // Get the signal index
389  m_iTargetHitSignal = m_SignalManager.FindSignal("target_hit");
390  SetTargetMode(ETargetMode.TRAINING);
391 
392  m_OwnerEntity = owner.GetParent();
393  if (!m_OwnerEntity)
394  return;
395 
396  m_LineController = SCR_FiringRangeController.Cast(m_OwnerEntity);
397  if (!m_LineController)
398  return;
399 
400  if (m_bIsIndicator)
401  m_LineController.SetIndicator(owner);
402 
403  m_FiringRangeManager = SCR_FiringRangeManager.Cast(m_OwnerEntity.GetParent());
404  if (!m_FiringRangeManager)
405  return;
406 
407  m_RplComponent = RplComponent.Cast(m_FiringRangeManager.FindComponent(RplComponent));
408  }
409 
410  //------------------------------------------------------------------------------------------------
411  void SCR_FiringRangeTarget(IEntitySource src, IEntity parent)
412  {
413  SetEventMask(EntityEvent.INIT);
414  m_FiringLineController = parent;
415  }
416 
417  //------------------------------------------------------------------------------------------------
418  void ~SCR_FiringRangeTarget()
419  {
420  }
421 };
EntityEditorProps
enum EQueryType EntityEditorProps(category:"GameScripted/Sound", description:"THIS IS THE SCRIPT DESCRIPTION.", color:"0 0 255 255")
Definition: SCR_AmbientSoundsComponent.c:12
SCR_FiringRangeTargetClass
Definition: SCR_FiringRangeTarget.c:2
SCR_FiringRangeTarget
Definition: SCR_FiringRangeTarget.c:19
RplProp
SCR_RplTestEntityClass RplProp
Used for handling entity spawning requests for SCR_CatalogEntitySpawnerComponent and inherited classe...
GetGame
ArmaReforgerScripted GetGame()
Definition: game.c:1424
SCR_SoundEvent
Definition: SCR_SoundEvent.c:1
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_SoundManagerEntity
Definition: SCR_SoundManagerEntity.c:17
Instigator
Definition: Instigator.c:6
ETargetState
ETargetState
Definition: SCR_FiringRangeTarget.c:7
COMPETITION
@ COMPETITION
Definition: SCR_FiringRangeTarget.c:16
m_SignalManager
protected SignalsManagerComponent m_SignalManager
Definition: SCR_WristwatchComponent.c:55
TRAINING
@ TRAINING
Definition: SCR_FiringRangeTarget.c:15
SCR_FiringRangeManager
Definition: SCR_FiringRangeManager.c:7
Attribute
typedef Attribute
Post-process effect of scripted camera.
TARGET_DOWN
@ TARGET_DOWN
Definition: SCR_FiringRangeTarget.c:10
BaseBuildingClass
Definition: BaseBuilding.c:12
m_RplComponent
protected RplComponent m_RplComponent
Definition: SCR_CampaignBuildingManagerComponent.c:42
TARGET_UP
@ TARGET_UP
Definition: SCR_FiringRangeTarget.c:9
type
EDamageType type
Definition: SCR_DestructibleTreeV2.c:32
EDamageType
EDamageType
Definition: EDamageType.c:12
SCR_FiringRangeController
Definition: SCR_FiringRangeController.c:9
ETargetMode
ETargetMode
Definition: SCR_FiringRangeTarget.c:13
m_OwnerEntity
SCR_AIUtilityComponentClass m_OwnerEntity
category
params category
Definition: SCR_VehicleDamageManagerComponent.c:180