Dayz Explorer  1.24.157551 (v105080)
Dayz Code Explorer by Zeroy
weapondebug.c
Go to the documentation of this file.
2 {
7 };
8 
9 
10 
12 {
13  const int BUFFER_SIZE = 1000;
14  const float COLLISIONS_DISTANCE_TOLERANCE = 0.01;
15  const float MAX_MUZZLE_DISTANCE_TOLERANCE = 20;
16  const float CAMERA_BULLET_ORIGIN_OFFSET = 1;
17  const float CAMERA_TRACE_MIN_DISTANCE_TOLERANCE = 0.3;
18 
19  Weapon m_WeaponInHands;
20  int m_BufferIndex;
21  bool m_IsDrawKeyHeldDown;
22  bool m_IsLMBPressed;
23  bool m_IsToggleKeyPressed;
24  bool m_IsFireKeyPressed;
25  float m_TargetDistance;
26  eDebugMode m_CurrentMode;
27  vector m_AimTrailCyclic[BUFFER_SIZE];
28  vector m_AimTrailOrdered[BUFFER_SIZE];
29 
30  ref map<int, string> m_DebugModesNames = new map<int, string>;
31 
32  //ref array<Selection> m_Selections = new array<Selection>();
33 
34  Shape m_Shape_usti;
35  Shape m_Shape_konec;
36  Shape m_ShapeFireDirection1;
37  Shape m_ShapeFireDirection2;
38  Shape m_HitShape;
39  Shape m_ShapeEye;
40  Shape m_ShapeTrailLines;
41  Shape m_ShapeFireDirCamera;
42  Shape m_HitShape2;
43  Shape m_HitShape3;
44  Shape m_HitShape4;
45  //Shape temp_shape;
46  Shape m_PermanentLine1;
47  Shape m_PermanentLine2;
48 
49  Shape m_PermanentShape1;
50  Shape m_PermanentShape2;
51 
52  Weapon GetWeaponInHands()
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 
61  void WeaponDebug()
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 
68  void ~WeaponDebug()
69  {
70  RemoveAllShapes(true);
71 
72  }
73 
74  void RemoveAllShapes(bool is_exit = false)
75  {
76  Debug.RemoveShape(m_Shape_usti);
77  Debug.RemoveShape(m_Shape_konec);
78  Debug.RemoveShape(m_ShapeFireDirection1);
79  Debug.RemoveShape(m_ShapeFireDirection2);
80  Debug.RemoveShape(m_ShapeEye);
81  Debug.RemoveShape(m_ShapeTrailLines);
82  Debug.RemoveShape(m_ShapeFireDirCamera);
83  Debug.RemoveShape(m_HitShape);
84  Debug.RemoveShape(m_HitShape2);
85  Debug.RemoveShape(m_HitShape3);
86  Debug.RemoveShape(m_HitShape4);
87 
88  if ( is_exit )
89  {
90  Debug.RemoveShape(m_PermanentLine1);
91  Debug.RemoveShape(m_PermanentLine2);
92  Debug.RemoveShape(m_PermanentShape1);
93  Debug.RemoveShape(m_PermanentShape2);
94  }
95  }
96 
97 
98  void OnCommandHandlerUpdate()
99  {
100 
101  m_IsLMBPressed = false;
102  m_IsFireKeyPressed = false;
103 
104  if (KeyState(KeyCode.KC_LWIN) == 1)
105  {
106  if (!m_IsDrawKeyHeldDown)
107  {
108  OnKeyDown(KeyCode.KC_LWIN);
109  }
110  m_IsDrawKeyHeldDown = true;
111  }
112  else
113  {
114  if ( m_IsDrawKeyHeldDown )
115  {
116  //OnKeyUp();
117  }
118  m_IsDrawKeyHeldDown = false;
119  }
120 
121  if (KeyState(KeyCode.KC_Z) == 1)
122  {
123  ClearKey(KeyCode.KC_Z);
124  CycleDebugMode();
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 
139  void OnKeyDown(KeyCode key)
140  {
141  if (key == KeyCode.KC_X)
142  {
143  }
144  }
145 
146  void CycleDebugMode()
147  {
148  m_CurrentMode++;
149  if ( m_CurrentMode == eDebugMode.COUNT )
150  {
151  m_CurrentMode = 0;
152  }
153 
154  }
155 
156  void OnPostFrameUpdate()
157  {
158  if ( GetWeaponInHands() )
159  {
160  RemoveAllShapes();
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 
184  if (m_IsDrawKeyHeldDown)
185  {
186  AddPosToCyclicBuffer(usti_hlavne_position);
187  OrderTrailArray();
188  }
189 
190  m_ShapeTrailLines = Debug.DrawLines( m_AimTrailOrdered,BUFFER_SIZE, COLOR_YELLOW, ShapeFlags.NOZBUFFER );
191 
192 
193  DrawEyePoint(weapon);
194  DrawBarrelMemoryPoints(konec_hlavne_position,usti_hlavne_position );
195  DisplayGeneralInfo();
196  DisplayTargetInfo();
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  {
208  if (!m_IsDrawKeyHeldDown)
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  {
236  Debug.RemoveShape(m_PermanentLine1);
237  m_PermanentLine1 = Debug.DrawLine(end_point, contact_point, Colors.RED, ShapeFlags.NOZBUFFER );
238  }
239  }
240 
241  void AddPosToCyclicBuffer(vector pos)
242  {
243  m_AimTrailCyclic[m_BufferIndex] = pos;
244  m_BufferIndex++;
245  if (m_BufferIndex == BUFFER_SIZE)
246  {
247  m_BufferIndex = 0;
248  }
249  }
250 
251  void OrderTrailArray()
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 
272  vector GetEyePointPosition(Weapon weapon)
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 
285  void DisplayGeneralInfo()
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 
299  void DisplayTargetInfo()
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  {
334  Debug.RemoveShape(m_PermanentLine1);
335  Debug.RemoveShape(m_PermanentLine2);
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  {
404  Debug.RemoveShape(m_PermanentLine1);
405  Debug.RemoveShape(m_PermanentLine2);
406  Debug.RemoveShape(m_PermanentShape1);
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 
434  Debug.RemoveShape(m_HitShape4);
435  Debug.RemoveShape(m_HitShape);
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 }
ItemBase
Definition: inventoryitem.c:730
GetGame
proto native CGame GetGame()
ClearKey
proto native void ClearKey(KeyCode key)
DbgUI
Definition: dbgui.c:59
KeyCode
KeyCode
Definition: ensystem.c:156
Weapon
script counterpart to engine's class Weapon
Definition: inventoryitem.c:48
COLOR_YELLOW
const int COLOR_YELLOW
Definition: constants.c:67
CAMERA_MUZZLE_HYBRID
@ CAMERA_MUZZLE_HYBRID
Definition: weapondebug.c:5
WeaponDebug
Definition: weapondebug.c:11
MUZZLE_FIRE
@ MUZZLE_FIRE
Definition: weapondebug.c:4
Colors
Definition: colors.c:3
eDebugMode
eDebugMode
Definition: weapondebug.c:1
PlayerBase
Definition: playerbaseclient.c:1
map
map
Definition: controlsxboxnew.c:3
vector
Definition: enconvert.c:105
KeyState
proto native int KeyState(KeyCode key)
ShapeFlags
ShapeFlags
Definition: endebug.c:125
Object
Definition: objecttyped.c:1
PrintString
void PrintString(string s)
Helper for printing out string expression. Example: PrintString("Hello " + var);.
Definition: enscript.c:345
m_CurrentMode
int m_CurrentMode
Definition: plugindayzplayerdebug.c:384
DayZPlayerImplement
Definition: manbase.c:1
GetMouseState
proto native int GetMouseState(MouseState index)
COLOR_RED
const int COLOR_RED
Definition: constants.c:64
MouseState
MouseState
Definition: ensystem.c:310
GetPlayer
protected void GetPlayer()
Definition: crosshairselector.c:127
Debug
Definition: debug.c:13
DayZPhysics
Definition: dayzphysics.c:123
Math
Definition: enmath.c:6
NORMAL
@ NORMAL
Definition: weapondebug.c:3
Class
Super root of all classes in Enforce script.
Definition: enscript.c:10
COLOR_BLUE
const int COLOR_BLUE
Definition: constants.c:66
COUNT
@ COUNT
Definition: weapondebug.c:6
Shape
class DiagMenu Shape
don't call destructor directly. Use Destroy() instead