Arma Reforger Explorer 1.7.0.54
Arma Reforger Code Explorer by Zeroy - Thanks to MisterOutofTime
Loading...
Searching...
No Matches
OnHitAimModifier.c
Go to the documentation of this file.
1#define SCRIPTED_AIM_MODIFIER_DEBUG
2//------------------------------------------------------------------------------------------------
4//------------------------------------------------------------------------------------------------
6{
7 private bool m_bIsSleeping = true;
8
9 // The maximum amount of damage this effect is scaled to.
10 const float m_fEffectScale = 80;
11
12 [Attribute("0.06", uiwidget: UIWidgets.Slider, params: "0 1 0.001")]
13 protected float m_fLinearScale;
14
15 [Attribute("25", uiwidget: UIWidgets.Slider, params: "0 100 0.1")]
16 protected float m_fAngularScale;
17
18 private vector m_vTarget;
19 private vector m_vCurrent;
20 private vector m_vVelocity;
21
22 [Attribute("2.65", uiwidget: UIWidgets.Slider, desc: "How long is the spring effect", params: "0 10 0.001")]
23 protected float m_fSpring;
24
25 [Attribute("1.67", uiwidget: UIWidgets.Slider, desc: "High damping will make the spring more bouncy", params: "0 10 0.001")]
26 protected float m_fDamping;
27
28 [Attribute("10", uiwidget: UIWidgets.Slider, desc: "Speed of interpolation for spring", params: "0 20 0.001")]
29 protected float m_fSpringSpeed;
30
31 [Attribute("0.1", uiwidget: UIWidgets.Slider, desc: "Rate of speed reduction when spring reaches the target value", params: "0 1 0.001")]
32 protected float m_fDecaySpeed;
33
34 [Attribute("1", uiwidget: UIWidgets.Slider, params: "0 5 0.001")]
35 protected float m_fRollRotationIntensity;
36
37 [Attribute("0.75", uiwidget: UIWidgets.Slider, params: "0 5 0.001")]
38 protected float m_fYawRotationIntensity;
39
40 [Attribute("1", uiwidget: UIWidgets.Slider, params: "0 5 0.001")]
42
43 protected IEntity m_pOwner;
44 #ifdef ENABLE_DIAG
46 #endif
47
48 //------------------------------------------------------------------------------------------------
49 protected override void OnInit(IEntity weaponEnt)
50 {
51 m_pOwner = weaponEnt;
52 #ifdef ENABLE_DIAG
53 DiagMenu.RegisterBool(SCR_DebugMenuID.DEBUGUI_CHARACTER_HITREACTION_AIMMODIFIER,"","AimModifier Debug", "Character");
54 DiagMenu.SetValue(SCR_DebugMenuID.DEBUGUI_CHARACTER_HITREACTION_AIMMODIFIER,0);
55 m_fDiagSpring = m_fSpring * m_fDiagMul;
56 m_fDiagDamping = m_fDamping * m_fDiagMul;
57 m_fDiagLinearScale = m_fLinearScale * m_fDiagMul;
58 m_fDiagAngularScale = m_fAngularScale * m_fDiagMul;
59 m_fDiagSpeed = m_fSpringSpeed * m_fDiagMul;
60 #endif
61 }
62 //------------------------------------------------------------------------------------------------
63 protected override void OnActivated(IEntity weaponOwner)
64 {
65 if (!weaponOwner)
66 return;
67 ChimeraCharacter parentCharacter = ChimeraCharacter.Cast(weaponOwner);
68 if (!parentCharacter)
69 return;
70
71 SCR_CharacterDamageManagerComponent dmgManager = SCR_CharacterDamageManagerComponent.Cast(parentCharacter.GetDamageManager());
72 if (dmgManager)
73 dmgManager.GetOnDamage().Insert(OnDamage);
74
75
76 #ifdef ENABLE_DIAG
77 m_pCharacter = parentCharacter;
78 #endif
79 }
80 //------------------------------------------------------------------------------------------------
81 protected override void OnDeactivated(IEntity weaponOwner)
82 {
83 if (!weaponOwner)
84 return;
85
86 ChimeraCharacter parentCharacter = ChimeraCharacter.Cast(weaponOwner);
87 if (!parentCharacter)
88 return;
89
90 SCR_CharacterDamageManagerComponent dmgManager = SCR_CharacterDamageManagerComponent.Cast(parentCharacter.GetDamageManager());
91 if (dmgManager)
92 dmgManager.GetOnDamage().Remove(OnDamage);
93
94
95 #ifdef ENABLE_DIAG
96 m_pCharacter = null;
97 #endif
98 }
99
100 //------------------------------------------------------------------------------------------------
101 void OnDamage(BaseDamageContext damageContext)
102 {
103 float div;
104 if (m_fEffectScale > 0.0)
105 div = 1.0 / m_fEffectScale;
106
107 float scaleFactor = damageContext.damageValue * div;
108 scaleFactor = Math.Clamp(scaleFactor, 0.0, 1.0);
109
110 if (scaleFactor > 0)
111 m_bIsSleeping = false;
112
113
114 vector direction = damageContext.hitDirection;
115
116 // Scale the input so it's slightly larger at lower values,
117 // and not linear, that would mean almost no visible effect at low damage values
118 const float POW = 1.8 / 2.0;
119 float finalPower = Math.Pow(scaleFactor, POW);
120 m_vTarget = direction * finalPower;
121 #ifdef ENABLE_DIAG
122 if (DiagMenu.GetBool(SCR_DebugMenuID.DEBUGUI_CHARACTER_HITREACTION_AIMMODIFIER))
123 Print("New target = " + m_vTarget);
124 #endif
125
126 }
127 //------------------------------------------------------------------------------------------------
128 // called every frame when aim offset is calculated
129 override void OnCalculate(IEntity owner, WeaponAimModifierContext context, float timeSlice, out vector translation, out vector rotation, out vector turnOffset)
130 {
131 #ifdef ENABLE_DIAG
132 if (DiagMenu.GetBool(SCR_DebugMenuID.DEBUGUI_CHARACTER_HITREACTION_AIMMODIFIER))
133 DrawDiagWindow(timeSlice);
134 #endif
135
136 if (!m_bIsSleeping)
137 {
138 const float threshold = 0.001;
139 if (m_vTarget.LengthSq() < threshold)
140 m_bIsSleeping = true;
141 }
142
143 if (m_bIsSleeping)
144 {
145 translation = vector.Zero;
146 rotation = vector.Zero;
147 turnOffset = vector.Zero;
148 return;
149 }
150
151 m_vCurrent = VectorSpring(m_vCurrent, m_vTarget, m_vVelocity, m_fSpring, m_fDamping, timeSlice * m_fSpringSpeed);
152 m_vTarget = vector.Lerp(m_vTarget, vector.Zero, m_fDecaySpeed);
153
154 translation = m_vCurrent * m_fLinearScale;
155 vector linToRot;
156 linToRot[1] = Math.AbsFloat(m_vCurrent[2]) * m_fPitchRotationIntensity;
157 linToRot[0] = m_vCurrent[0] * m_fYawRotationIntensity;
158 linToRot[2] = -linToRot[0] * m_fRollRotationIntensity;
159
160 rotation = linToRot * m_fAngularScale;
161 turnOffset = vector.Zero;
162 }
163
164 //------------------------------------------------------------------------------------------------
165 protected vector VectorSpring(vector current, vector target, inout vector velocity, float spring, float damping, float dt)
166 {
167 float vx = velocity[0];
168 float vy = velocity[1];
169 float vz = velocity[2];
170
171 current[0] = Math.SmoothSpring(current[0], target[0], vx, spring, damping, dt);
172 current[1] = Math.SmoothSpring(current[1], target[1], vy, spring, damping, dt);
173 current[2] = Math.SmoothSpring(current[2], target[2], vz, spring, damping, dt);
174
175 velocity = Vector(vx, vy, vz);
176 return current;
177 }
178
179 //------------------------------------------------------------------------------------------------
180 #ifdef ENABLE_DIAG
181 private void ResetEffect()
182 {
183 m_vCurrent = vector.Zero;
184 m_vVelocity = vector.Zero;
185 m_vTarget = vector.Zero;
186 }
187
188 int m_iDiagDamage = 50;
189 float m_fDiagSpring;
190 float m_fDiagDamping;
191 float m_fDiagLinearScale;
192 float m_fDiagAngularScale;
193 float m_fDiagSpeed;
194 const float m_fDiagMul = 100.0;
195
196 //------------------------------------------------------------------------------------------------
197 void DrawDiagWindow( float timeSlice )
198 {
199 DbgUI.Begin("On Hit Diag");
200 {
201 if (DbgUI.Button("Reset"))
202 ResetEffect();
203
204 vector _[3];
205 DbgUI.InputInt("Damage", m_iDiagDamage);
206 if (DbgUI.Button("Apply damage") || Debug.KeyState(KeyCode.KC_B))
207 {
208 BaseDamageContext damageContext = new BaseDamageContext();
209 damageContext.damageValue = m_iDiagDamage;
210 damageContext.damageType = EDamageType.KINETIC;
211 OnDamage(damageContext);
212 Debug.ClearKey(KeyCode.KC_B);
213 }
214
215 DbgUI.SliderFloat("Spring = " + m_fDiagSpring / m_fDiagMul, m_fDiagSpring, 0.0, 3.0 * m_fDiagMul);
216 DbgUI.SliderFloat("Damping = " + m_fDiagDamping / m_fDiagMul, m_fDiagDamping, 0.0, 3.0 * m_fDiagMul);
217 DbgUI.Spacer(16);
218 DbgUI.SliderFloat("Spring Speed = " + m_fDiagSpeed / m_fDiagMul, m_fDiagSpeed, 0.0, 10.0 * m_fDiagMul);
219 DbgUI.SliderFloat("Linear Scale = " + m_fDiagLinearScale / m_fDiagMul, m_fDiagLinearScale, 0.0, 1.0 * m_fDiagMul);
220 DbgUI.SliderFloat("Angular Scale = " + m_fDiagAngularScale / m_fDiagMul, m_fDiagAngularScale, 0.0, 90.0 * m_fDiagMul);
221 m_fSpring = m_fDiagSpring / m_fDiagMul;
222 m_fDamping = m_fDiagDamping / m_fDiagMul;
223 m_fLinearScale = m_fDiagLinearScale / m_fDiagMul;
224 m_fAngularScale = m_fDiagAngularScale / m_fDiagMul;
225 m_fSpringSpeed = m_fDiagSpeed / m_fDiagMul;
226
227
228 DbgUI.Text("Target = " + m_vTarget);
229
230 }
231 DbgUI.End();
232 }
233
234 #endif
235
236};
SCR_DebugMenuID
This enum contains all IDs for DiagMenu entries added in script.
Definition DebugMenuID.c:4
ChimeraCharacter m_pCharacter
SCR_CharacterBloodHitZone OnDamage
Resilience - incapacitation or death, depending on game mode settings.
vector direction
UI Textures DeployMenu Briefing conflict_HintBanner_1_UI desc
Diagnostic and developer menu system.
Definition DiagMenu.c:18
Definition Math.c:13
ONHIT AIM MODIFIER.
override void OnInit(IEntity weaponEnt)
override void OnCalculate(IEntity owner, WeaponAimModifierContext context, float timeSlice, out vector translation, out vector rotation, out vector turnOffset)
void OnDamage(BaseDamageContext damageContext)
override void OnActivated(IEntity weaponOwner)
override void OnDeactivated(IEntity weaponOwner)
vector VectorSpring(vector current, vector target, inout vector velocity, float spring, float damping, float dt)
proto void Print(void var, LogLevel level=LogLevel.NORMAL)
Prints content of variable to console/log.
SCR_FieldOfViewSettings Attribute
RespawnSystemComponentClass GameComponentClass vector vector rotation
EDamageType
Definition EDamageType.c:13
KeyCode
Definition KeyCode.c:13
proto native vector Vector(float x, float y, float z)
void Debug()
Definition Types.c:327