Arma Reforger Explorer  1.1.0.42
Arma Reforger Code Explorer by Zeroy - Thanks to MisterOutofTime
SCR_AIAttackBehavior.c
Go to the documentation of this file.
2 {
3 #ifdef WORKBENCH
4  ref Shape m_Shape;
5 #endif
6 
7  // Be careful, here we want to store a ref to BaseTarget
8  ref SCR_BTParamRef<BaseTarget> m_Target = new SCR_BTParamRef<BaseTarget>(SCR_AIActionTask.TARGET_PORT);
9 
10  // Wait time before we start shooting
11  ref SCR_BTParam<float> m_fWaitTime = new SCR_BTParam<float>("WaitTime");
12 
13  SCR_AIWorld m_AIWorld;
14  SCR_AICombatComponent m_CombatComponent;
15 
16  bool m_bSelected = false;
17 
18  // This delay is executed before shooting starts
19  protected static const float WAIT_TIME_UNEXPECTED = 0.25;
20  protected static const float WAIT_TIME_OVERTHREATENED = 0.8;
21 
22  //----------------------------------------------------------------------------------
23  void InitParameters(BaseTarget target, float waitTime)
24  {
25  m_Target.Init(this, target);
26  m_fWaitTime.Init(this, waitTime);
27  }
28 
29  //----------------------------------------------------------------------------------
30  override void OnActionSelected()
31  {
32  super.OnActionSelected();
33  m_bSelected = true;
34  EnableFriendlyFireCheck(true);
35  }
36 
37  //----------------------------------------------------------------------------------
38  override void OnActionDeselected()
39  {
40  super.OnActionDeselected();
41  EnableFriendlyFireCheck(false);
42  }
43 
44  //----------------------------------------------------------------------------------
45  override void OnActionCompleted()
46  {
47  super.OnActionCompleted();
48  EnableFriendlyFireCheck(false);
49  }
50 
51  //----------------------------------------------------------------------------------
52  override void OnActionFailed()
53  {
54  super.OnActionFailed();
55  EnableFriendlyFireCheck(false);
56  }
57 
58  //----------------------------------------------------------------------------------
59  override string GetActionDebugInfo()
60  {
61  return this.ToString() + " attacking " + m_Target.ValueToString();
62  }
63 
64  //----------------------------------------------------------------------------------
65  void SCR_AIAttackBehavior(SCR_AIUtilityComponent utility, SCR_AIActivityBase groupActivity, BaseTarget target, BaseTarget prevTarget, float priorityLevel = PRIORITY_LEVEL_NORMAL)
66  {
67  InitParameters(target, 4);
68  if (!utility)
69  return;
70 
71  m_fPriorityLevel.m_Value = priorityLevel;
72  SetPriority(PRIORITY_BEHAVIOR_ATTACK_NOT_SELECTED);
73  m_fThreat = 1.01 * SCR_AIThreatSystem.VIGILANT_THRESHOLD;
74  m_sBehaviorTree = "AI/BehaviorTrees/Chimera/Soldier/Attack.bt";
75  m_bAllowLook = false;
76  m_bResetLook = true;
77  SetIsUniqueInActionQueue(false);
78  m_AIWorld = SCR_AIWorld.Cast(GetGame().GetAIWorld());
79  m_CombatComponent = utility.m_CombatComponent;
80 
81  // Init wait time, but only if we didn't have target before.
82  // Otherwise if we are switching between targets, it should be instant.
83  if (!prevTarget)
84  InitWaitTime(utility);
85  else
86  m_fWaitTime.m_Value = 0;
87  }
88 
89  //----------------------------------------------------------------------------------
90  override float CustomEvaluate()
91  {
92  BaseTarget baseTarget = m_Target.m_Value;
93 
94  // Return 0 if it's not current target selected by weapon&target selector
95  // Since weapon&target selector selects both weapon and target, if we chose to attack a different target,
96  // it might happen that we use a wrong weapon
97  if (baseTarget != m_CombatComponent.GetCurrentTarget())
98  return 0;
99 
100  // Update m_bUseCombatMove
101  m_bUseCombatMove = !m_Utility.m_AIInfo.HasUnitState(EUnitState.IN_TURRET);
102 
103  float targetScore = m_Utility.m_CombatComponent.m_WeaponTargetSelector.CalculateTargetScore(baseTarget);
104 
105  if (baseTarget.IsEndangering() || baseTarget.GetTimeSinceEndangered() < SCR_AICombatComponent.TARGET_ENDANGERED_TIMEOUT_S)
106  targetScore *= SCR_AICombatComponent.ENDANGERING_TARGET_SCORE_MULTIPLIER;
107 
108  if (targetScore >= SCR_AICombatComponent.TARGET_SCORE_HIGH_PRIORITY_ATTACK)
109  return PRIORITY_BEHAVIOR_ATTACK_HIGH_PRIORITY;
110 
111  if (m_bSelected)
112  return PRIORITY_BEHAVIOR_ATTACK_SELECTED;
113 
114  return PRIORITY_BEHAVIOR_ATTACK_NOT_SELECTED;
115  }
116 
117  // Sets the delay until shooting starts.
118  protected void InitWaitTime(SCR_AIUtilityComponent utility)
119  {
120  float threatMeasure = utility.m_ThreatSystem.GetThreatMeasure();
121 
122  // Delay depending on threat
123  float threatDelay;
124  if (threatMeasure < SCR_AIThreatSystem.ATTACK_DELAYED_THRESHOLD)
125  threatDelay = WAIT_TIME_UNEXPECTED;
126  else if (threatMeasure < SCR_AIThreatSystem.THREATENED_THRESHOLD)
127  threatDelay = 0;
128  else
129  threatDelay = WAIT_TIME_OVERTHREATENED;
130 
131  // Delay depending on distance
132  // 0m - 0ms
133  // 100m - 340ms
134  // 300m - 700ms
135  // 500m - 870ms
136  // 800m - 1018ms
137  float distance = vector.Distance(m_Utility.m_OwnerEntity.GetOrigin(), m_Target.m_Value.GetLastSeenPosition());
138  float distanceDelay = (1.4 * distance) / (300 + distance);
139 
140  m_fWaitTime.m_Value = threatDelay + distanceDelay;
141  }
142 
143  void EnableFriendlyFireCheck(bool enable)
144  {
145  if (m_Utility && m_Utility.m_PerceptionComponent)
146  m_Utility.m_PerceptionComponent.SetFriendlyFireCheck(enable);
147  }
148 };
GetGame
ArmaReforgerScripted GetGame()
Definition: game.c:1424
m_Target
class SCR_AIPolar m_Target
m_fPriorityLevel
float m_fPriorityLevel
Definition: SendGoalMessage.c:3
BaseTarget
Definition: BaseTarget.c:12
SCR_AIActivityBase
Definition: SCR_AIActivity.c:1
SCR_AIWorld
Definition: SCR_AIWorld.c:23
SCR_AIAttackBehavior
Definition: SCR_AIAttackBehavior.c:1
distance
float distance
Definition: SCR_DestructibleTreeV2.c:29
m_CombatComponent
SCR_AICombatComponent m_CombatComponent
Definition: SCR_AIUtilityComponent.c:12
m_bSelected
protected bool m_bSelected
Definition: SCR_InventoryHitZonePointUI.c:396
m_Shape
private ref Shape m_Shape
Definition: UnitDisplayComponent.c:11
SCR_AIActionTask
Definition: SCR_AIBehaviorTask.c:1
SCR_AIBehaviorBase
Definition: SCR_AIBehavior.c:1
SCR_AIThreatSystem
Definition: SCR_AIThreatSystem.c:17