DayZ Scripts
v1.21.156300 ยท Jun 20, 2023
 
Loading...
Searching...
No Matches
WeaponDebug.c
Go to the documentation of this file.
2{
7};
8
9
10
12{
13 const int BUFFER_SIZE = 1000;
18
29
31
32 //ref array<Selection> m_Selections = new array<Selection>();
33
45 //Shape temp_shape;
48
51
53 {
54 Weapon weapon_in_hands;
55 PlayerBase player = PlayerBase.Cast(GetGame().GetPlayer());
56 if( player && player.GetItemInHands() ) Class.CastTo(weapon_in_hands, player.GetItemInHands());
57
58 return weapon_in_hands;
59 }
60
62 {
63 m_DebugModesNames.Insert(eDebugMode.NORMAL, "Mode "+eDebugMode.NORMAL.ToString()+": shoot along the weapon barrel");
64 m_DebugModesNames.Insert(eDebugMode.MUZZLE_FIRE,"Mode "+eDebugMode.MUZZLE_FIRE.ToString()+": shoot from muzzle end to camera trace hit point");
65 m_DebugModesNames.Insert(eDebugMode.CAMERA_MUZZLE_HYBRID, "Mode "+eDebugMode.CAMERA_MUZZLE_HYBRID.ToString()+": shoot either from camera lens or from muzzle \n depending on the situation");
66 }
67
69 {
70 RemoveAllShapes(true);
71
72 }
73
74 void RemoveAllShapes(bool is_exit = false)
75 {
87
88 if ( is_exit )
89 {
94 }
95 }
96
97
99 {
100
101 m_IsLMBPressed = false;
102 m_IsFireKeyPressed = false;
103
104 if (KeyState(KeyCode.KC_LWIN) == 1)
105 {
107 {
108 OnKeyDown(KeyCode.KC_LWIN);
109 }
110 m_IsDrawKeyHeldDown = true;
111 }
112 else
113 {
115 {
116 //OnKeyUp();
117 }
118 m_IsDrawKeyHeldDown = false;
119 }
120
121 if (KeyState(KeyCode.KC_Z) == 1)
122 {
123 ClearKey(KeyCode.KC_Z);
125 }
126
127 if (KeyState(KeyCode.KC_X) == 1)
128 {
129 m_IsFireKeyPressed = true;
130 ClearKey(KeyCode.KC_X);
131 }
132
133 if (GetMouseState(MouseState.LEFT) & MB_PRESSED_MASK)
134 {
135 m_IsLMBPressed = true;
136 }
137 }
138
140 {
141 if (key == KeyCode.KC_X)
142 {
143 }
144 }
145
147 {
149 if ( m_CurrentMode == eDebugMode.COUNT )
150 {
151 m_CurrentMode = 0;
152 }
153
154 }
155
157 {
158 if ( GetWeaponInHands() )
159 {
161 Weapon weapon = GetWeaponInHands();
162 vector cameraDirection = GetGame().GetCurrentCameraDirection();
163 vector cameraPosition = GetGame().GetCurrentCameraPosition();
164 vector usti_hlavne_position = weapon.GetSelectionPositionMS( "usti hlavne" );//usti hlavne
165 vector konec_hlavne_position = weapon.GetSelectionPositionMS( "konec hlavne" );//konec hlavne
166 usti_hlavne_position = weapon.ModelToWorld(usti_hlavne_position);
167 konec_hlavne_position = weapon.ModelToWorld(konec_hlavne_position);
168
169 if ( m_CurrentMode == eDebugMode.NORMAL )
170 {
171 DrawLineOfFire(konec_hlavne_position,usti_hlavne_position );
172 }
173
174 if ( m_CurrentMode == eDebugMode.MUZZLE_FIRE )
175 {
176 DrawLineOfFireMuzzleToHit(usti_hlavne_position, cameraDirection, cameraPosition);
177 }
178
179 if ( m_CurrentMode == eDebugMode.CAMERA_MUZZLE_HYBRID )
180 {
181 DrawLineOfFireCameraHybrid(usti_hlavne_position, cameraDirection, cameraPosition, konec_hlavne_position);
182 }
183
185 {
186 AddPosToCyclicBuffer(usti_hlavne_position);
188 }
189
191
192
193 DrawEyePoint(weapon);
194 DrawBarrelMemoryPoints(konec_hlavne_position,usti_hlavne_position );
197 /*
198 vector pos;
199 weapon.GetProjectedCursorPos3d(pos);
200 Debug.RemoveShape(temp_shape);
201 temp_shape = Debug.DrawSphere(pos, 0.1, Colors.GREEN);
202 */
203 }
204 }
205
206 void DrawBarrelMemoryPoints(vector begin_point, vector end_point)
207 {
209 {
210 m_Shape_usti = Debug.DrawSphere(end_point, 0.011, Colors.GREEN, ShapeFlags.TRANSP|ShapeFlags.NOOUTLINE|ShapeFlags.NOZBUFFER);
211 m_Shape_konec = Debug.DrawSphere(begin_point, 0.011, Colors.GREEN, ShapeFlags.TRANSP|ShapeFlags.NOOUTLINE|ShapeFlags.NOZBUFFER);
212 }
213 }
214
215
216 void DrawLineOfFire(vector begin_point, vector end_point)
217 {
218 vector contact_point;
219 vector contact_dir;
220 vector aim_point = end_point - begin_point;
221 int contact_component;
222
223 aim_point = aim_point.Normalized() * 100;
224 aim_point = aim_point + end_point;
225
226 m_ShapeFireDirection1 = Debug.DrawLine(end_point, aim_point);
227 m_ShapeFireDirection2 = Debug.DrawLine(begin_point, end_point, ShapeFlags.NOZBUFFER );
228
229 if ( DayZPhysics.RaycastRV(end_point, aim_point, contact_point, contact_dir, contact_component, null, null, null, false, false, ObjIntersectFire) )
230 {
231 m_HitShape = Debug.DrawSphere(contact_point, 0.04, COLOR_RED);
232 }
233
234 if ( m_IsFireKeyPressed )
235 {
237 m_PermanentLine1 = Debug.DrawLine(end_point, contact_point, Colors.RED, ShapeFlags.NOZBUFFER );
238 }
239 }
240
242 {
246 {
247 m_BufferIndex = 0;
248 }
249 }
250
252 {
253 int unordered_index;
254
255 for (int i = 0; i < BUFFER_SIZE; i++)
256 {
257 unordered_index = m_BufferIndex + i;
258 if ( unordered_index >= BUFFER_SIZE )
259 {
260 unordered_index = unordered_index - BUFFER_SIZE;
261 }
262 m_AimTrailOrdered[i] = m_AimTrailCyclic[unordered_index];
263 }
264 }
265
266 void DrawEyePoint(Weapon weapon)
267 {
268 vector position = GetEyePointPosition(weapon);
269 m_ShapeEye = Debug.DrawSphere(position, 0.009, COLOR_BLUE, ShapeFlags.TRANSP|ShapeFlags.NOOUTLINE|ShapeFlags.NOZBUFFER);
270 }
271
273 {
274 string memory_point_name = weapon.ConfigGetString("memoryPointCamera");
275 ItemBase optics = weapon.GetAttachedOptics();
276 if (optics)
277 {
278 memory_point_name = optics.ConfigGetString("memoryPointCamera");
279 return optics.ModelToWorld(optics.GetSelectionPositionLS( memory_point_name ));
280 }
281 return weapon.ModelToWorld(weapon.GetSelectionPositionLS( memory_point_name ));
282
283 }
284
286 {
287 DbgUI.Begin("sway weight", 50, 50);
289 float sway_weight = player.GetAimingModel().GetSwayWeight();
290 DbgUI.Text("value: " + sway_weight.ToString());
291 DbgUI.Text("Hold LWIN to draw debug line");
292 DbgUI.Text("Press X to simulate fire");
293 DbgUI.Text("Press Z to cycle debug modes");
294 DbgUI.Text("Debug " +m_DebugModesNames.Get(m_CurrentMode) );
295 DbgUI.Text("");
296 DbgUI.End();
297 }
298
300 {
301 DbgUI.Begin("target distance", 50,200);
302 DbgUI.Text("value: " + m_TargetDistance.ToString());
303 DbgUI.End();
304 }
305
306
307 void DrawLineOfFireMuzzleToHit(vector begin_point, vector camera_dir, vector camera_pos)
308 {
309 vector contact_point_cam_trace;
310 vector contact_point_muzzle_trace;
311
312 vector contact_dir_muzzle;
313 vector contact_dir_camera;
314
315 vector contact_point;
316 vector contact_dir;
317
318 int contact_component;
319 int contact_component_muzzle;
320
321 vector end_point = camera_pos + camera_dir * 1000;
322 Man player = GetGame().GetPlayer();
323 Object player_o;
324 Class.CastTo(player_o, player);
325
326 if ( DayZPhysics.RaycastRV(camera_pos, end_point, contact_point_cam_trace, contact_dir_camera, contact_component,null, null, player_o , false, false, ObjIntersectFire, 0.1) )
327 {
328 m_ShapeFireDirCamera = Debug.DrawLine(begin_point, contact_point_cam_trace, Colors.RED, ShapeFlags.NOZBUFFER );
329 m_HitShape2 = Debug.DrawSphere(contact_point_cam_trace, 0.03, Colors.GREEN);
330 m_TargetDistance = vector.Distance( player.GetPosition(), contact_point_cam_trace);
331
332 if ( m_IsFireKeyPressed )
333 {
336 m_PermanentLine1 = Debug.DrawLine(begin_point, contact_point_cam_trace, Colors.RED, ShapeFlags.NOZBUFFER );
337 m_PermanentLine2 = Debug.DrawLine(camera_pos, contact_point_cam_trace, Colors.GREEN, ShapeFlags.NOZBUFFER );
338 }
339
340 }
341
342 else
343 {
344 m_ShapeFireDirCamera = Debug.DrawLine(begin_point, end_point, Colors.GREEN, ShapeFlags.NOZBUFFER );
345 m_TargetDistance = -1;
346 }
347
348 if ( DayZPhysics.RaycastRV(begin_point, contact_point_cam_trace, contact_point_muzzle_trace, contact_dir_muzzle, contact_component_muzzle, null, null, null, false, false, ObjIntersectFire, 0.0) )
349 {
350 m_HitShape3 = Debug.DrawSphere(contact_point_muzzle_trace, 0.03, COLOR_RED);
351 }
352 }
353
354 void DrawLineOfFireCameraHybrid(vector usti_hlavne_position, vector camera_dir, vector camera_pos, vector konec_hlavne_position)
355 {
356 bool muzzle_shot;
357 vector contact_point_cam_trace;
358 vector contact_point_muzzle_trace;
359
360 vector aim_at_position;
361
362 vector contact_dir_muzzle;
363 vector contact_dir;
364
365 vector contact_point;
366
367 int contact_component;
368 int contact_component_muzzle;
369
370 float distance_to_aim_at;
371 vector end_point = camera_pos + camera_dir * 100;
372 vector start_point = camera_pos + (CAMERA_BULLET_ORIGIN_OFFSET * camera_dir);
373 vector weapon_aim_direction = usti_hlavne_position - konec_hlavne_position;
374 weapon_aim_direction.Normalize();
375
376 Man player = GetGame().GetPlayer();
377 Object player_o;
378 Class.CastTo(player_o, player);
379
380 if ( DayZPhysics.RaycastRV(start_point, end_point, contact_point_cam_trace, contact_dir, contact_component,null, null, player_o , false, false, ObjIntersectFire) )
381 {
382 m_TargetDistance = vector.Distance(start_point, contact_point_cam_trace);
383 aim_at_position = contact_point_cam_trace;
384
385 if ( DayZPhysics.RaycastRV(usti_hlavne_position, contact_point_cam_trace, contact_point_muzzle_trace, contact_dir, contact_component,null, null, player_o , false, false, ObjIntersectFire, 0.05) )
386 {
387 float collision_distance = vector.Distance(contact_point_cam_trace, contact_point_muzzle_trace);
388 float muzzle_collision_distance = vector.Distance(usti_hlavne_position, contact_point_muzzle_trace);
389
390 if ((collision_distance > 2 && muzzle_collision_distance < MAX_MUZZLE_DISTANCE_TOLERANCE) || m_TargetDistance < CAMERA_TRACE_MIN_DISTANCE_TOLERANCE)
391 {
392 muzzle_shot = true;
393 aim_at_position = contact_point_muzzle_trace;
394 }
395 }
396 }
397
398
399
400 distance_to_aim_at = vector.Distance(camera_pos, aim_at_position);
401
402 if ( m_IsFireKeyPressed )
403 {
407
408 vector contact_point_temp;
409
410 if (muzzle_shot)
411 {
412 m_PermanentLine1 = Debug.DrawLine(usti_hlavne_position, usti_hlavne_position + weapon_aim_direction * 5, Colors.RED, ShapeFlags.NOZBUFFER );
413 m_PermanentLine2 = Debug.DrawLine(camera_pos, contact_point_cam_trace, Colors.GREEN, ShapeFlags.NOZBUFFER );
414 PrintString("muzle shot");
415 }
416 else
417 {
418 vector dir = contact_point_cam_trace - camera_pos;
419 dir.Normalize();
420 dir = dir * 100;
421 float dst = vector.Distance(camera_pos, contact_point_cam_trace);
422 float dist2 = vector.Distance(camera_pos, contact_point_cam_trace);
423
424 //m_PermanentShape1 = Debug.DrawSphere(start_point, 0.015,Colors.GREEN);
425 m_PermanentLine1 = Debug.DrawLine(start_point, contact_point_cam_trace + dir, Colors.RED, ShapeFlags.NOZBUFFER );
426 PrintString("camera shot");
427 }
428 }
429
430 float clamped_distance = Math.Clamp(distance_to_aim_at, 0 , 100);
431 float distance_normalized = Math.InverseLerp(0, 100, clamped_distance);
432 float hit_sphere_size = Math.Lerp(0.025, 0.75, distance_normalized);
433
436 m_HitShape = Debug.DrawSphere(contact_point_cam_trace, 0.20,Colors.GREEN, ShapeFlags.TRANSP);
437 m_HitShape4 = Debug.DrawSphere(aim_at_position, hit_sphere_size);
438
439 }
440
441}
PlayerBase GetPlayer()
eDebugMode
Definition WeaponDebug.c:2
@ CAMERA_MUZZLE_HYBRID
Definition WeaponDebug.c:5
@ COUNT
Definition WeaponDebug.c:6
@ MUZZLE_FIRE
Definition WeaponDebug.c:4
@ NORMAL
Definition WeaponDebug.c:3
proto native vector GetCurrentCameraDirection()
proto native DayZPlayer GetPlayer()
proto native vector GetCurrentCameraPosition()
Super root of all classes in Enforce script.
Definition EnScript.c:11
Definition Colors.c:4
const int GREEN
Definition Colors.c:8
const int RED
Definition Colors.c:5
static proto bool RaycastRV(vector begPos, vector endPos, out vector contactPos, out vector contactDir, out int contactComponent, set< Object > results=NULL, Object with=NULL, Object ignore=NULL, bool sorted=false, bool ground_only=false, int iType=ObjIntersectView, float radius=0.0, CollisionFlags flags=CollisionFlags.NEARESTCONTACT)
Raycasts world by given parameters.
Definition DbgUI.c:60
Definition Debug.c:14
static Shape DrawLines(vector[] positions, int count, int color=0xFFFFFFFF, int flags=0)
Definition Debug.c:374
static void RemoveShape(out Shape shape)
Definition Debug.c:107
static Shape DrawLine(vector from, vector to, int color=0xFFFFFFFF, int flags=0)
Definition Debug.c:361
static Shape DrawSphere(vector pos, float size=1, int color=0x1fff7f7f, ShapeFlags flags=ShapeFlags.TRANSP|ShapeFlags.NOOUTLINE)
Definition Debug.c:306
Definition EnMath.c:7
Shape m_Shape_usti
Definition WeaponDebug.c:34
void DrawLineOfFireCameraHybrid(vector usti_hlavne_position, vector camera_dir, vector camera_pos, vector konec_hlavne_position)
void OnCommandHandlerUpdate()
Definition WeaponDebug.c:98
Shape m_Shape_konec
Definition WeaponDebug.c:35
int m_BufferIndex
Definition WeaponDebug.c:20
void OrderTrailArray()
Shape m_ShapeFireDirection1
Definition WeaponDebug.c:36
void DisplayGeneralInfo()
const float CAMERA_TRACE_MIN_DISTANCE_TOLERANCE
Definition WeaponDebug.c:17
Shape m_ShapeFireDirection2
Definition WeaponDebug.c:37
eDebugMode m_CurrentMode
Definition WeaponDebug.c:26
Shape m_PermanentShape1
Definition WeaponDebug.c:49
void ~WeaponDebug()
Definition WeaponDebug.c:68
vector GetEyePointPosition(Weapon weapon)
const float CAMERA_BULLET_ORIGIN_OFFSET
Definition WeaponDebug.c:16
Weapon GetWeaponInHands()
Definition WeaponDebug.c:52
vector m_AimTrailCyclic[BUFFER_SIZE]
Definition WeaponDebug.c:27
const float COLLISIONS_DISTANCE_TOLERANCE
Definition WeaponDebug.c:14
bool m_IsFireKeyPressed
Definition WeaponDebug.c:24
Weapon m_WeaponInHands
Definition WeaponDebug.c:19
bool m_IsLMBPressed
Definition WeaponDebug.c:22
ref map< int, string > m_DebugModesNames
Definition WeaponDebug.c:30
void DrawBarrelMemoryPoints(vector begin_point, vector end_point)
Shape m_ShapeTrailLines
Definition WeaponDebug.c:40
void DrawEyePoint(Weapon weapon)
const int BUFFER_SIZE
Definition WeaponDebug.c:13
float m_TargetDistance
Definition WeaponDebug.c:25
bool m_IsToggleKeyPressed
Definition WeaponDebug.c:23
bool m_IsDrawKeyHeldDown
Definition WeaponDebug.c:21
Shape m_PermanentShape2
Definition WeaponDebug.c:50
Shape m_HitShape4
Definition WeaponDebug.c:44
void OnPostFrameUpdate()
Shape m_HitShape2
Definition WeaponDebug.c:42
void DisplayTargetInfo()
void CycleDebugMode()
void WeaponDebug()
Definition WeaponDebug.c:61
vector m_AimTrailOrdered[BUFFER_SIZE]
Definition WeaponDebug.c:28
Shape m_HitShape3
Definition WeaponDebug.c:43
Shape m_ShapeFireDirCamera
Definition WeaponDebug.c:41
Shape m_ShapeEye
Definition WeaponDebug.c:39
const float MAX_MUZZLE_DISTANCE_TOLERANCE
Definition WeaponDebug.c:15
Shape m_PermanentLine1
Definition WeaponDebug.c:46
Shape m_HitShape
Definition WeaponDebug.c:38
void OnKeyDown(KeyCode key)
Shape m_PermanentLine2
Definition WeaponDebug.c:47
void RemoveAllShapes(bool is_exit=false)
Definition WeaponDebug.c:74
void AddPosToCyclicBuffer(vector pos)
void DrawLineOfFireMuzzleToHit(vector begin_point, vector camera_dir, vector camera_pos)
void DrawLineOfFire(vector begin_point, vector end_point)
script counterpart to engine's class Weapon
proto string ToString()
proto vector Normalized()
return normalized vector (keeps orginal vector untouched)
static proto native float Distance(vector v1, vector v2)
Returns the distance between tips of two 3D vectors.
proto float Normalize()
Normalizes vector. Returns length.
proto native CGame GetGame()
const int COLOR_BLUE
Definition constants.c:66
const int COLOR_RED
Definition constants.c:64
const int COLOR_YELLOW
Definition constants.c:67
ShapeFlags
Definition EnDebug.c:126
static proto native void Begin(string windowTitle, float x=0, float y=0)
static proto native void Text(string label)
static proto native void End()
class DiagMenu Shape
don't call destructor directly. Use Destroy() instead
static proto bool CastTo(out Class to, Class from)
Try to safely down-cast base class to child class.
void PrintString(string s)
Helper for printing out string expression. Example: PrintString("Hello " + var);.
Definition EnScript.c:345
proto native void ClearKey(KeyCode key)
KeyCode
Definition EnSystem.c:157
proto native int KeyState(KeyCode key)
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'.
MouseState
Definition EnSystem.c:311
proto native int GetMouseState(MouseState index)