DayZ Scripts
v1.21.156300 ยท Jun 20, 2023
 
Loading...
Searching...
No Matches
RecoilBase.c
Go to the documentation of this file.
2{
4
7 protected bool m_DeleteRequested;
8 protected float m_Time;//how much time has elapsed since first update
9 protected float m_ReloadTime;//reload time config parameter of the weapon
11 protected bool m_IsClient;
12 // for mouse offset
13 float m_MouseOffsetRangeMin;//in degrees min
14 float m_MouseOffsetRangeMax;//in degrees max
15 float m_MouseOffsetRelativeTime = 1;//[0..1] a time it takes to move the mouse the required distance relative to the reload time of the weapon(firing mode)
16 float m_HandsOffsetRelativeTime = 1;//[0..1] a time it takes to move the hands the required distance given by the curve relative to the reload time of the weapon(firing mode)
17 float m_CamOffsetRelativeTime = 1;//[0..1] a time it takes to move the camera the required distance relative to the reload time of the weapon(firing mode)
18 float m_CamOffsetDistance = 0.05;//how far the camera will travel along the z-axis in cm
19 float m_MouseOffsetDistance;//how far should the mouse travel
21 //protected float m_MouseOffsetResult;//in degrees max
22 protected vector m_MouseOffsetTarget;//move the mouse towards this point
23 protected vector m_MouseOffsetTargetAccum;//the overall mouse offset so far(all deltas accumulated)
24 protected float m_Angle;//result between the min and max
25 // mouse end
26
28
30 {
31 m_Weapon = weapon;
32 //m_DebugMode = false;
33 m_DebugMode = GetDayZGame().IsAimLogEnabled();
34 m_Player = PlayerBase.Cast(weapon.GetHierarchyRootPlayer());
36 Init();
37 PostInit(weapon);
38 }
39
40 void Init();
41
43 {
44 return m_Weapon;
45 }
46
47 void PostInit(Weapon_Base weapon)
48 {
49 int muzzleIndex = weapon.GetCurrentMuzzle();
50 m_Angle = m_Player.GetRandomGeneratorSyncManager().GetRandomInRange(RandomGeneratorSyncUsage.RGSRecoil, m_MouseOffsetRangeMin, m_MouseOffsetRangeMax);
53
54 m_ReloadTime = weapon.GetReloadTime(muzzleIndex);
59 }
60
62 void Destroy()
63 {
64 m_DeleteRequested = true;
65 }
66
67 // called externally per update, not to be overriden in children
68 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 )
69 {
71 {
72 delete this;
73 }
74
77
78 ApplyMouseOffset(pDt, axis_mouse_x, axis_mouse_y);
79 ApplyHandsOffset(pDt, axis_hands_x, axis_hands_y);
80 if(m_IsClient)
81 ApplyCamOffset(pModel);
82 #ifdef DEVELOPER
83 if(m_DebugMode)
84 PrintString("RecoilBase | BEFORE | axis_mouse_y: " + axis_mouse_y.ToString());
85 #endif
86 axis_mouse_x = axis_mouse_x * m_RecoilModifier[0];
87 axis_mouse_y = axis_mouse_y * m_RecoilModifier[1];
88
89 axis_hands_x = axis_hands_x * m_RecoilModifier[0];
90 axis_hands_y = axis_hands_y * m_RecoilModifier[1];
91
92 #ifdef DEVELOPER
93 if(m_DebugMode)
94 {
95 PrintString("RecoilBase | AFTER | axis_mouse_y: " + axis_mouse_y.ToString());
96 }
97 #endif
98 m_Time += pDt;
99
100 if( m_Time >= m_ReloadTime )
101 {
102 Destroy();
103 }
104 }
105
107 {
109 float offset = 0;
110 float time = Easing.EaseOutBack(time_rel);
111 if(time == 1)
112 {
113 offset = 0;
114 }
115 else
116 {
117 offset = Math.Lerp(0,m_CamOffsetDistance,time);
118 }
119
120 pModel.m_fCamPosOffsetZ = offset;
121
122 m_Player.GetCurrentCamera().SendRecoilOffsetZ(offset);
123 }
124
125 void ApplyHandsOffset(float pDt, out float pRecResultX, out float pRecResultY)
126 {
127 float relative_time = m_TimeNormalized / Math.Clamp(m_HandsOffsetRelativeTime, 0.001,1);
128 vector pos_on_curve = GetPositionOnCurve(m_HandsCurvePoints, relative_time);
129
130 /*if(m_DebugMode)
131 {
132 PrintString("pos_on_curve: " + pos_on_curve.ToString());
133 PrintString("normalized time: " + m_TimeNormalized.ToString());
134 PrintString("elapsed time: " + m_Time.ToString());
135 PrintString("curve pos x: " + pos_on_curve[0].ToString());
136 PrintString("curve pos y: " + pos_on_curve[1].ToString());
137 PrintString("relative_time: " + relative_time.ToString());
138 }*/
139
140 pRecResultX = pos_on_curve[0];
141 pRecResultY = pos_on_curve[1];
142 }
143
144 void ApplyMouseOffset(float pDt, out float pRecResultX, out float pRecResultY)
145 {
146 #ifdef DEVELOPER
147 if(m_DebugMode)
148 {
150 PrintString( "RecoilBase | ApplyMouseOffset processing: " + b1 );
151 PrintString( "RecoilBase | m_MouseOffsetTargetAccum : " + m_MouseOffsetTargetAccum.ToString() );
152 PrintString( "RecoilBase | m_MouseOffsetTarget : " + m_MouseOffsetTarget.ToString() );
153 }
154 #endif
155
157 {
158 float relative_delta = pDt / m_ReloadTime / Math.Clamp(m_MouseOffsetRelativeTime, 0.001,1);
159
160 float delta_mouse_offset_x = m_MouseOffsetTarget[0] * relative_delta;
161 float delta_mouse_offset_y = m_MouseOffsetTarget[1] * relative_delta;
162
163 if( ( m_MouseOffsetTargetAccum[1] + delta_mouse_offset_y) > m_MouseOffsetTarget[1] )
164 {
165 delta_mouse_offset_x = m_MouseOffsetTarget[0] - m_MouseOffsetTargetAccum[0];
166 delta_mouse_offset_y = m_MouseOffsetTarget[1] - m_MouseOffsetTargetAccum[1];
167 }
168
169 m_MouseOffsetTargetAccum[0] = m_MouseOffsetTargetAccum[0] + delta_mouse_offset_x;
170 m_MouseOffsetTargetAccum[1] = m_MouseOffsetTargetAccum[1] + delta_mouse_offset_y;
171
172 pRecResultX = delta_mouse_offset_x;
173 pRecResultY = delta_mouse_offset_y;
174
175 /*if(m_DebugMode)
176 {
177 PrintString( "delta x: "+ delta_mouse_offset_x );
178 PrintString( "delta y: "+ delta_mouse_offset_y );
179 PrintString( "target x: "+ m_MouseOffsetTarget[0] );
180 PrintString( "target y: "+ m_MouseOffsetTarget[1] );
181 PrintString( "accum x: "+ m_MouseOffsetTargetAccum[0] );
182 PrintString( "accum y: "+ m_MouseOffsetTargetAccum[1] );
183 }*/
184 }
185 #ifdef DEVELOPER
186 if(m_DebugMode)
187 {
188 PrintString( "RecoilBase | pRecResultY: " + pRecResultY );
189 }
190 #endif
191 }
192
194 {
195 if( weapon )
196 {
197 return weapon.GetPropertyModifierObject().m_RecoilModifiers;
198 }
199 else return "1 1 1";
200 }
201
203 {
204 return Math3D.Curve(ECurveType.CatmullRom, time, points);
205 }
206}
DayZGame GetDayZGame()
Definition DayZGame.c:3656
proto native bool IsDedicatedServer()
Robust check which is preferred than the above, as it is valid much sooner.
Input value between 0 and 1, returns value adjusted by easing, no automatic clamping of input(do your...
Definition Easing.c:3
static float EaseOutBack(float t, float magnitude=1.70158)
Definition Easing.c:173
Definition EnMath.c:7
float m_TimeNormalized
Definition RecoilBase.c:20
Weapon_Base GetWeapon()
Definition RecoilBase.c:42
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
void Init()
protected float m_Time
Definition RecoilBase.c:8
vector GetRecoilModifier(Weapon_Base weapon)
Definition RecoilBase.c:193
bool m_DebugMode
Definition RecoilBase.c:3
Weapon_Base m_Weapon
Definition RecoilBase.c:5
float m_HandsOffsetRelativeTime
Definition RecoilBase.c:16
protected vector m_RecoilModifier
Definition RecoilBase.c:10
vector GetPositionOnCurve(array< vector > points, float time)
Definition RecoilBase.c:202
PlayerBase m_Player
Definition RecoilBase.c:6
protected vector m_MouseOffsetTargetAccum
Definition RecoilBase.c:23
float m_MouseOffsetRangeMin
Definition RecoilBase.c:13
float m_MouseOffsetRelativeTime
Definition RecoilBase.c:15
protected float m_Angle
Definition RecoilBase.c:24
void ApplyCamOffset(SDayZPlayerAimingModel pModel)
Definition RecoilBase.c:106
void RecoilBase(Weapon_Base weapon)
Definition RecoilBase.c:29
float m_CamOffsetRelativeTime
Definition RecoilBase.c:17
protected bool m_IsClient
Definition RecoilBase.c:11
void ApplyMouseOffset(float pDt, out float pRecResultX, out float pRecResultY)
Definition RecoilBase.c:144
void PostInit(Weapon_Base weapon)
Definition RecoilBase.c:47
protected float m_ReloadTime
Definition RecoilBase.c:9
void ApplyHandsOffset(float pDt, out float pRecResultX, out float pRecResultY)
Definition RecoilBase.c:125
void Destroy()
Destroys this object next update tick.
Definition RecoilBase.c:62
float m_CamOffsetDistance
Definition RecoilBase.c:18
protected bool m_DeleteRequested
Definition RecoilBase.c:7
float m_MouseOffsetDistance
Definition RecoilBase.c:19
protected ref array< vector > m_HandsCurvePoints
Definition RecoilBase.c:27
protected vector m_MouseOffsetTarget
Definition RecoilBase.c:22
float m_MouseOffsetRangeMax
Definition RecoilBase.c:14
Result for an object found in CGame.IsBoxCollidingGeometryProxy.
proto string ToString(bool beautify=true)
Vector to string.
proto static native vector YawToVector(float yaw)
Returns vector of yaw.
proto native CGame GetGame()
proto void Print(void var)
Prints content of variable to console/log.
void PrintString(string s)
Helper for printing out string expression. Example: PrintString("Hello " + var);.
Definition EnScript.c:345
ECurveType
Definition EnMath3D.c:21
static proto native vector Curve(ECurveType type, float param, notnull array< vector > points)
Computes curve.
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 InverseLerp(float a, float b, float value)
Calculates the linear value that produces the interpolant value within the range [a,...
static proto float Lerp(float a, float b, float time)
Linearly interpolates between 'a' and 'b' given 'time'.