DayZ Scripts
v1.21.156300 ยท Jun 20, 2023
 
Loading...
Searching...
No Matches
DayZPlayerImplementAiming.c
Go to the documentation of this file.
1/*
2DayZPlayerImplement
3
4this file is implemenation of dayzPlayer in script
5- logic of movement
6- camera switching logic
7
8*/
9
11{
16 MAX
18
19class PlayerSwayConstants
20{
21 static const float SWAY_MULTIPLIER_DEFAULT = 1.0;
22 static const float SWAY_MULTIPLIER_STABLE = 0.05;
23 static const float SWAY_MULTIPLIER_EXHAUSTED = 0.6;
24 static const float SWAY_TIME_IN = 1.5;
25 static const float SWAY_TIME_STABLE = 3.0;
26 static const float SWAY_TIME_EXHAUSTED = 1.5;
27 static const float SWAY_TIME_OUT = 0.5;
28 static const float SWAY_ROLL = 3;
29}
30
32{
33
34 //-------------------------------------------------------------
38
39 protected const float SWAY_WEIGHT_SCALER = 1;//use this to scale the sway effect up/down
40 protected float m_HorizontalNoise;
41 protected float m_HorizontalTargetValue;
42 protected float m_HorizontalNoiseVelocity[1] = {0};
45 protected float m_TotalTime;
46 protected float m_ReferenceTime = 0;
47 protected float m_SwayStateStartTime;
48 //protected float m_SwayStateStartTime[eSwayStates.MAX];
49 protected float m_LastSwayMultiplier = PlayerSwayConstants.SWAY_MULTIPLIER_DEFAULT;
52 protected float m_BreathingXAxisOffset;
53 protected float m_BreathingYAxisOffset;
54 protected bool m_HoldingBreathSet;
55 protected bool m_AimNoiseAllowed = true;
56 protected bool m_ProceduralRecoilEnabled = true;
58 protected int m_ShakeCount;
59 protected float m_SwayWeight;
60 protected float m_MaxVelocity;
61 protected ref KuruShake m_KuruShake;
62 protected float m_CamShakeX;
63 protected float m_CamShakeY;
64 protected vector m_SwayModifier = "1 1 1";//"max_speed_noise radius_noise overall_speed"
65 protected int m_SwayState = -1;
66
67 protected float m_StealthAimY_Last;
68 protected float m_FilterVelocityStealthAimY[1] = {0};
69
70 protected static float m_AimXClampRanges[] = { -180, -20, 90, 0, -50, 90, 180, -20, 90 };
71
73 {
74 m_PlayerDpi = player;
75 Class.CastTo(m_PlayerPb, player);
77 }
78
79 void SetRecoil( Weapon_Base weapon )
80 {
82 {
84 }
85 }
86
87 void RequestKuruShake(float amount)
88 {
89 if(!m_KuruShake)
90 m_KuruShake = new KuruShake(m_PlayerPb, amount);
91 }
92
94 {
95 Weapon_Base weapon = Weapon_Base.Cast(player.GetHumanInventory().GetEntityInHands());
96 if(weapon)
97 {
98 m_SwayModifier = weapon.GetPropertyModifierObject().m_SwayModifiers;
99 }
100 }
101
102 void OnFinisherBegin(float currentAimY)
103 {
104 m_StealthAimY_Last = currentAimY;
106 }
107
108 void OnSwayStateChange(int state)
109 {
110 switch (state)
111 {
112 case eSwayStates.HOLDBREATH_EXHAUSTED:
113 m_PlayerPb.OnHoldBreathExhausted();
114 break;
115
116 default:
117 break;
118 }
119 }
120
122 {
123 return m_SwayWeight;
124 }
125
126 void SetAimNoiseAllowed(bool state)
127 {
128 m_AimNoiseAllowed = state;
129 }
130
132 {
133 return m_AimNoiseAllowed;
134 }
135
137 {
139 }
140
142 {
144 }
145
146 void SetCamShakeValues(float x_axis, float y_axis)
147 {
148 m_CamShakeX = x_axis;
149 m_CamShakeY = y_axis;
150 }
151
153 {
156 return true;
157 }
158
159 bool ProcessAimFilters(float pDt, SDayZPlayerAimingModel pModel, int stance_index)
160 {
161 float breathing_offset_x;
162 float breathing_offset_y;
163
164 float noise_offset_x;
165 float noise_offset_y;
166
167 float shake_offset_x;
168 float shake_offset_y;
169
170 float recoil_offset_mouse_x;
171 float recoil_offset_mouse_y;
172
173 float recoil_offset_hands_x;
174 float recoil_offset_hands_y;
175
176 float kuru_offset_x;
177 float kuru_offset_y;
178
179 //float player_stamina = m_PlayerPb.GetStaminaHandler().GetStaminaNormalized();
180 float player_stamina = m_PlayerPb.GetStaminaHandler().GetSyncedStaminaNormalized();
181
182 #ifdef DEVELOPER
183 DbgPrintAimingImplement("Player: " + m_PlayerPb + " | ProcessAimFilters | timestamp: " + m_PlayerPb.GetSimulationTimeStamp());
184 #endif
185
186 //negates stamina effect during hold breath
187 if ( m_PlayerPb.IsHoldingBreath() )
188 {
189 player_stamina = 1;
190 }
191 float speed = (((1.0 - player_stamina) * 3.0) + 1.0) * m_SwayModifier[2];
192 m_TotalTime += pDt * speed;
193
194 if (m_PlayerPb.IsHoldingBreath() && !m_HoldingBreathSet)
195 {
197 }
198 else if (!m_PlayerPb.IsHoldingBreath() && m_HoldingBreathSet)
199 {
201 }
202
203 float adjusted_sway_multiplier = CalculateSwayMultiplier();
204 m_LastSwayMultiplier = adjusted_sway_multiplier;
205
206 m_SwayWeight = CalculateWeight( stance_index, player_stamina, 0.5/*m_PlayerPb.m_CameraSwayModifier*/, m_PlayerPb.IsHoldingBreath() ) * adjusted_sway_multiplier;
207
209 ApplyBreathingPattern(breathing_offset_x, breathing_offset_y, 3.0, m_TotalTime, m_SwayWeight);
210 ApplyHorizontalNoise(noise_offset_x, noise_offset_y, 0.2, 0.5, 3.0 * m_SwayModifier[0], speed, 3 * m_SwayModifier[1], m_SwayWeight, pDt);
211
212 int shake_level = m_PlayerPb.GetShakeLevel();
213 if (shake_level != 0)
214 {
215 ApplyShakes(shake_offset_x, shake_offset_y, shake_level);
216 }
217
219 if( m_CurrentRecoil )
220 {
221 m_CurrentRecoil.Update(pModel, recoil_offset_mouse_x, recoil_offset_mouse_y, recoil_offset_hands_x, recoil_offset_hands_y, pDt);
222 }
223
224 if( m_KuruShake )
225 {
226 m_KuruShake.Update(pDt, kuru_offset_x, kuru_offset_y);
227 }
228
230 pModel.m_fAimXHandsOffset = breathing_offset_x + noise_offset_x + recoil_offset_hands_x + shake_offset_x + kuru_offset_x;
231 pModel.m_fAimYHandsOffset = breathing_offset_y + noise_offset_y + recoil_offset_hands_y + shake_offset_y + kuru_offset_y;
232
233 #ifdef DEVELOPER
234 DbgPrintAimingImplement("breathing_offset_y: " + breathing_offset_y);
235 DbgPrintAimingImplement("noise_offset_y: " + noise_offset_y);
236 DbgPrintAimingImplement("recoil_offset_hands_y: " + recoil_offset_hands_y);
237 DbgPrintAimingImplement("shake_offset_y: " + shake_offset_y);
238 DbgPrintAimingImplement("kuru_offset_y: " + kuru_offset_y);
239 DbgPrintAimingImplement("pModel.m_fAimYHandsOffset: " + pModel.m_fAimYHandsOffset);
240 #endif
242 pModel.m_fAimXCamOffset = -shake_offset_x - recoil_offset_hands_x - kuru_offset_x + m_CamShakeX;
243 pModel.m_fAimYCamOffset = -shake_offset_y - recoil_offset_hands_y - kuru_offset_y + m_CamShakeY;
244
245
246 #ifdef DEVELOPER
247 DbgPrintAimingImplement("m_CamShakeY: " + m_CamShakeY);
248 DbgPrintAimingImplement("pModel.m_fAimYCamOffset: " + pModel.m_fAimYCamOffset);
249 #endif
250
252 if (stance_index == DayZPlayerConstants.STANCEIDX_RAISEDPRONE)
253 {
254 float newVal = DayZPlayerUtils.LinearRangeClamp(pModel.m_fCurrentAimX, pModel.m_fCurrentAimY, m_AimXClampRanges);
255 pModel.m_fAimYHandsOffset += newVal - pModel.m_fCurrentAimY;
256 }
257
258 if( m_PlayerDpi.IsInOptics() && m_KuruShake )
259 {
260 //TODO - do not offset mouse
261 }
263 pModel.m_fAimXMouseShift = recoil_offset_mouse_x -kuru_offset_x / 10;
264 pModel.m_fAimYMouseShift = recoil_offset_mouse_y + kuru_offset_y / 10;
265
266 #ifdef DEVELOPER
267 DbgPrintAimingImplement("recoil_offset_mouse_y: " + recoil_offset_mouse_y);
268 DbgPrintAimingImplement("pModel.m_fAimYMouseShift: " + pModel.m_fAimYMouseShift);
269 #endif
270
271 if ( m_PlayerPb.IsHoldingBreath() && !m_HoldingBreathSet)
272 {
273 m_HoldingBreathSet = true;
274 m_HorizontalNoiseXAxisOffset = noise_offset_x;
275 m_BreathingXAxisOffset = breathing_offset_x;
276 m_BreathingYAxisOffset = breathing_offset_y;
277 }
278 else if(!m_PlayerPb.IsHoldingBreath() && m_HoldingBreathSet)
279 {
280 m_HoldingBreathSet = false;
281 }
282
283 if(!m_PlayerPb.IsHoldingBreath() && m_LastSwayMultiplier == PlayerSwayConstants.SWAY_MULTIPLIER_DEFAULT && m_HorizontalNoiseXAxisOffset != 0)
284 {
288 }
289 #ifdef DEVELOPER
290 DbgPrintAimingImplement("----------------------------");
291 #endif
292 return true;
293 }
294
295 protected float CalculateSwayMultiplier()
296 {
297 float max;
298 float time;
299 float time_clamped;
300 float ret;
301
302 if (m_PlayerPb.IsHoldingBreath())
303 {
305
306 if (time < PlayerSwayConstants.SWAY_TIME_IN)
307 {
308 UpdateSwayState(eSwayStates.HOLDBREATH_IN);
309 max = PlayerSwayConstants.SWAY_TIME_IN;
310 time_clamped = Math.Clamp((m_TotalTime - m_SwayStateStartTime),0,max);
311 ret = Math.Lerp(m_LastSwayMultiplier,PlayerSwayConstants.SWAY_MULTIPLIER_STABLE,time_clamped/max);
312 }
313 else if (time >= PlayerSwayConstants.SWAY_TIME_IN && time < (PlayerSwayConstants.SWAY_TIME_IN + PlayerSwayConstants.SWAY_TIME_STABLE))
314 {
315 UpdateSwayState(eSwayStates.HOLDBREATH_STABLE);
316 ret = PlayerSwayConstants.SWAY_MULTIPLIER_STABLE;
317 }
318 else
319 {
320 UpdateSwayState(eSwayStates.HOLDBREATH_EXHAUSTED);
321 max = PlayerSwayConstants.SWAY_TIME_EXHAUSTED;
322 time_clamped = Math.Clamp((m_TotalTime - m_SwayStateStartTime),0,max);
323 ret = Math.Lerp(PlayerSwayConstants.SWAY_MULTIPLIER_STABLE,PlayerSwayConstants.SWAY_MULTIPLIER_EXHAUSTED,(time_clamped/max));
324 }
325 }
326 else
327 {
329 max = PlayerSwayConstants.SWAY_TIME_OUT;
330 time_clamped = Math.Clamp((m_TotalTime - m_SwayStateStartTime),0,max);
331 ret = Math.Lerp(m_LastSwayMultiplier,1,time_clamped/max);
332 }
333 return ret;
334 }
335
336 protected bool UpdateSwayState(int state)
337 {
338 if (state != m_SwayState)
339 {
340 m_SwayState = state;
343 OnSwayStateChange(state);
344 return true;
345 }
346
347 return false;
348 }
349
351 {
352 return m_SwayState;
353 }
354
355 protected void ApplyBreathingPattern(out float x_axis, out float y_axis, float pAmplitude, float pTotalTime, float weight)
356 {
357
358 float multiplier = Math.Lerp(PlayerSwayConstants.SWAY_MULTIPLIER_DEFAULT,0,m_LastSwayMultiplier); //TODO revise
359 #ifdef DEVELOPER
360 DbgPrintAimingImplement("m_LastSwayMultiplier: " + m_LastSwayMultiplier);
361 DbgPrintAimingImplement("pAmplitude: " + pAmplitude);
362 DbgPrintAimingImplement("pTotalTime: " + pTotalTime);
363 DbgPrintAimingImplement("weight: " + weight);
364 DbgPrintAimingImplement("multiplier: " + multiplier);
365 #endif
366
367 x_axis = (Math.Sin(pTotalTime) * pAmplitude / 4) * weight;
368 y_axis = (Math.Sin((pTotalTime) * 0.8 + 0.6 ) * pAmplitude) * weight;
369 #ifdef DEVELOPER
370 DbgPrintAimingImplement("y_axis_midproduct: " + y_axis);
371 #endif
372 x_axis += m_BreathingXAxisOffset * multiplier;
373 y_axis += m_BreathingYAxisOffset * multiplier;
374 }
375
376 protected void ApplyHorizontalNoise(out float x_axis, out float y_axis, float smooth_time,float max_velocity_low, float max_velocity_high, float velocity_modifier, float max_distance, float weight, float pDt)
377 {
379 {
380 //acquire new target
381 m_MaxVelocity = m_PlayerPb.GetRandomGeneratorSyncManager().GetRandomInRange(RandomGeneratorSyncUsage.RGSAimingModel, max_velocity_low, max_velocity_high);
382
383 float r = m_PlayerPb.GetRandomGeneratorSyncManager().GetRandomInRange(RandomGeneratorSyncUsage.RGSAimingModel, 0, 1);
384 m_HorizontalTargetValue = (r - 0.5) * 2 * max_distance;
386 }
387
389 x_axis = m_HorizontalNoise * weight;
390 float multiplier = Math.Lerp(PlayerSwayConstants.SWAY_MULTIPLIER_DEFAULT,0,m_LastSwayMultiplier); //TODO revise
391 x_axis += m_HorizontalNoiseXAxisOffset * multiplier;
392 }
393
394 protected void ApplyShakes(out float x_axis, out float y_axis, int level)
395 {
396 float weight = level / PlayerBase.SHAKE_LEVEL_MAX;
397 m_ShakeCount++;
398 int shakes_threshold = Math.Round(m_PlayerPb.GetRandomGeneratorSyncManager().GetRandomInRange(RandomGeneratorSyncUsage.RGSAimingModel, 2, 4));
399 if (m_ShakeCount > shakes_threshold)
400 {
401 m_ShakeCount = 0;
402
403 float modifier = m_PlayerPb.GetRandomGeneratorSyncManager().GetRandomInRange(RandomGeneratorSyncUsage.RGSAimingModel, 0.45, 0.9);
404 x_axis = modifier * weight * m_PlayerPb.GetRandomGeneratorSyncManager().GetRandomInRange(RandomGeneratorSyncUsage.RGSAimingModel, 0, 1);
405 y_axis = modifier * weight * m_PlayerPb.GetRandomGeneratorSyncManager().GetRandomInRange(RandomGeneratorSyncUsage.RGSAimingModel, 0, 1);
406 }
407 }
408
409 protected float CalculateWeight(int stance_index, float current_stamina, float camera_sway_modifier, bool holding_breath)
410 {
411 if (m_PlayerDpi.GetCommand_Move() && m_PlayerDpi.GetCommand_Move().IsInRoll())//when the player is rolling, set a constant and disregard everything else
412 {
413 return PlayerSwayConstants.SWAY_ROLL;
414 }
415 float stance_modifier;
416 //float scale = SWAY_WEIGHT_SCALER;
417 switch ( stance_index )
418 {
419 case DayZPlayerConstants.STANCEIDX_RAISEDERECT:
420 stance_modifier = 0.5;
421 break;
422 case DayZPlayerConstants.STANCEIDX_RAISEDCROUCH:
423 stance_modifier = 0.75;
424 break;
425 case DayZPlayerConstants.STANCEIDX_RAISEDPRONE:
426 stance_modifier = 0.9;
427 break;
428 default:
429 stance_modifier = 0.75;
430 //Debug.LogError("stance mask out of definition");
431 break;
432 }
433
434 #ifdef DEVELOPER
435 DbgPrintAimingImplement("current_stamina: " + current_stamina);
436 DbgPrintAimingImplement("camera_sway_modifier: " + camera_sway_modifier);
437 DbgPrintAimingImplement("holding_breath: " + holding_breath);
438 #endif
439
440 return (1 - stance_modifier) * m_AimNoiseAllowed * camera_sway_modifier * SWAY_WEIGHT_SCALER;
441 }
442
443 void DbgPrintAimingImplement(string val)
444 {
445 #ifdef DEVELOPER
446 if (GetDayZGame().IsAimLogEnabled())
447 Print("DayZPlayerImplementAiming | " + val);
448 #endif
449 }
450}
451
DayZGame GetDayZGame()
Definition DayZGame.c:3656
static const float SWAY_TIME_OUT
enum eSwayStates SWAY_MULTIPLIER_DEFAULT
static const float SWAY_TIME_EXHAUSTED
static const float SWAY_MULTIPLIER_STABLE
static const float SWAY_ROLL
static const float SWAY_TIME_IN
static const float SWAY_MULTIPLIER_EXHAUSTED
static const float SWAY_TIME_STABLE
private void DayZPlayerUtils()
cannot be instantiated
Super root of all classes in Enforce script.
Definition EnScript.c:11
protected void ApplyShakes(out float x_axis, out float y_axis, int level)
void SetRecoil(Weapon_Base weapon)
protected bool UpdateSwayState(int state)
void OnRaiseBegin(DayZPlayerImplement player)
protected float m_FilterVelocityStealthAimY[1]
void DayZPlayerImplementAiming(DayZPlayerImplement player)
protected void ApplyHorizontalNoise(out float x_axis, out float y_axis, float smooth_time, float max_velocity_low, float max_velocity_high, float velocity_modifier, float max_distance, float weight, float pDt)
bool ProcessAimFilters(float pDt, SDayZPlayerAimingModel pModel, int stance_index)
bool ProcessStealthFilters(float pDt, SDayZPlayerAimingModel pModel)
protected DayZPlayerImplement m_PlayerDpi
protected ref KuruShake m_KuruShake
void OnFinisherBegin(float currentAimY)
protected ref RecoilBase m_CurrentRecoil
protected float CalculateWeight(int stance_index, float current_stamina, float camera_sway_modifier, bool holding_breath)
protected float m_HorizontalNoiseVelocity[1]
void SetCamShakeValues(float x_axis, float y_axis)
protected void ApplyBreathingPattern(out float x_axis, out float y_axis, float pAmplitude, float pTotalTime, float weight)
static protected float m_AimXClampRanges[]
protected const float SWAY_WEIGHT_SCALER
void Update(float delta_time, out float pos_x, out float pos_y)
Definition KuruShake.c:48
Definition EnMath.c:7
void Update(SDayZPlayerAimingModel pModel, out float axis_mouse_x, out float axis_mouse_y, out float axis_hands_x, out float axis_hands_y, float pDt)
Definition RecoilBase.c:68
override RecoilBase SpawnRecoilObject()
Definition Magnum.c:68
DayZPlayerConstants
defined in C++
Definition dayzplayer.c:602
proto void Print(void var)
Prints content of variable to console/log.
static proto bool CastTo(out Class to, Class from)
Try to safely down-cast base class to child class.
static proto float Round(float f)
Returns mathematical round of value.
static proto float Clamp(float value, float min, float max)
Clamps 'value' to 'min' if it is lower than 'min', or to 'max' if it is higher than 'max'.
static proto float SmoothCD(float val, float target, inout float velocity[], float smoothTime, float maxVelocity, float dt)
Does the CD smoothing function - easy in | easy out / S shaped smoothing.
static proto float Lerp(float a, float b, float time)
Linearly interpolates between 'a' and 'b' given 'time'.
static proto float AbsFloat(float f)
Returns absolute value.
static proto float Sin(float angle)
Returns sinus of angle in radians.