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;
21 bool m_IsDrawKeyHeldDown;
23 bool m_IsToggleKeyPressed;
24 bool m_IsFireKeyPressed;
25 float m_TargetDistance;
27 vector m_AimTrailCyclic[BUFFER_SIZE];
28 vector m_AimTrailOrdered[BUFFER_SIZE];
36 Shape m_ShapeFireDirection1;
37 Shape m_ShapeFireDirection2;
40 Shape m_ShapeTrailLines;
41 Shape m_ShapeFireDirCamera;
46 Shape m_PermanentLine1;
47 Shape m_PermanentLine2;
49 Shape m_PermanentShape1;
50 Shape m_PermanentShape2;
56 if( player && player.GetItemInHands() )
Class.CastTo(weapon_in_hands, player.GetItemInHands());
58 return weapon_in_hands;
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");
70 RemoveAllShapes(
true);
74 void RemoveAllShapes(
bool is_exit =
false)
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);
90 Debug.RemoveShape(m_PermanentLine1);
91 Debug.RemoveShape(m_PermanentLine2);
92 Debug.RemoveShape(m_PermanentShape1);
93 Debug.RemoveShape(m_PermanentShape2);
98 void OnCommandHandlerUpdate()
101 m_IsLMBPressed =
false;
102 m_IsFireKeyPressed =
false;
106 if (!m_IsDrawKeyHeldDown)
110 m_IsDrawKeyHeldDown =
true;
114 if ( m_IsDrawKeyHeldDown )
118 m_IsDrawKeyHeldDown =
false;
129 m_IsFireKeyPressed =
true;
135 m_IsLMBPressed =
true;
146 void CycleDebugMode()
156 void OnPostFrameUpdate()
158 if ( GetWeaponInHands() )
161 Weapon weapon = GetWeaponInHands();
162 vector cameraDirection =
GetGame().GetCurrentCameraDirection();
163 vector cameraPosition =
GetGame().GetCurrentCameraPosition();
164 vector usti_hlavne_position = weapon.GetSelectionPositionMS(
"usti hlavne" );
165 vector konec_hlavne_position = weapon.GetSelectionPositionMS(
"konec hlavne" );
166 usti_hlavne_position = weapon.ModelToWorld(usti_hlavne_position);
167 konec_hlavne_position = weapon.ModelToWorld(konec_hlavne_position);
171 DrawLineOfFire(konec_hlavne_position,usti_hlavne_position );
176 DrawLineOfFireMuzzleToHit(usti_hlavne_position, cameraDirection, cameraPosition);
181 DrawLineOfFireCameraHybrid(usti_hlavne_position, cameraDirection, cameraPosition, konec_hlavne_position);
184 if (m_IsDrawKeyHeldDown)
186 AddPosToCyclicBuffer(usti_hlavne_position);
193 DrawEyePoint(weapon);
194 DrawBarrelMemoryPoints(konec_hlavne_position,usti_hlavne_position );
195 DisplayGeneralInfo();
206 void DrawBarrelMemoryPoints(
vector begin_point,
vector end_point)
208 if (!m_IsDrawKeyHeldDown)
216 void DrawLineOfFire(
vector begin_point,
vector end_point)
220 vector aim_point = end_point - begin_point;
221 int contact_component;
223 aim_point = aim_point.Normalized() * 100;
224 aim_point = aim_point + end_point;
226 m_ShapeFireDirection1 =
Debug.DrawLine(end_point, aim_point);
227 m_ShapeFireDirection2 =
Debug.DrawLine(begin_point, end_point,
ShapeFlags.NOZBUFFER );
229 if (
DayZPhysics.RaycastRV(end_point, aim_point, contact_point, contact_dir, contact_component,
null,
null,
null,
false,
false, ObjIntersectFire) )
234 if ( m_IsFireKeyPressed )
236 Debug.RemoveShape(m_PermanentLine1);
241 void AddPosToCyclicBuffer(
vector pos)
243 m_AimTrailCyclic[m_BufferIndex] = pos;
245 if (m_BufferIndex == BUFFER_SIZE)
251 void OrderTrailArray()
255 for (
int i = 0; i < BUFFER_SIZE; i++)
257 unordered_index = m_BufferIndex + i;
258 if ( unordered_index >= BUFFER_SIZE )
260 unordered_index = unordered_index - BUFFER_SIZE;
262 m_AimTrailOrdered[i] = m_AimTrailCyclic[unordered_index];
266 void DrawEyePoint(
Weapon weapon)
268 vector position = GetEyePointPosition(weapon);
274 string memory_point_name = weapon.ConfigGetString(
"memoryPointCamera");
275 ItemBase optics = weapon.GetAttachedOptics();
278 memory_point_name = optics.ConfigGetString(
"memoryPointCamera");
279 return optics.ModelToWorld(optics.GetSelectionPositionLS( memory_point_name ));
281 return weapon.ModelToWorld(weapon.GetSelectionPositionLS( memory_point_name ));
285 void DisplayGeneralInfo()
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");
299 void DisplayTargetInfo()
301 DbgUI.Begin(
"target distance", 50,200);
302 DbgUI.Text(
"value: " + m_TargetDistance.ToString());
307 void DrawLineOfFireMuzzleToHit(
vector begin_point,
vector camera_dir,
vector camera_pos)
309 vector contact_point_cam_trace;
310 vector contact_point_muzzle_trace;
312 vector contact_dir_muzzle;
313 vector contact_dir_camera;
318 int contact_component;
319 int contact_component_muzzle;
321 vector end_point = camera_pos + camera_dir * 1000;
322 Man player =
GetGame().GetPlayer();
324 Class.CastTo(player_o, player);
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) )
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);
332 if ( m_IsFireKeyPressed )
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 );
345 m_TargetDistance = -1;
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) )
350 m_HitShape3 =
Debug.DrawSphere(contact_point_muzzle_trace, 0.03,
COLOR_RED);
354 void DrawLineOfFireCameraHybrid(
vector usti_hlavne_position,
vector camera_dir,
vector camera_pos,
vector konec_hlavne_position)
357 vector contact_point_cam_trace;
358 vector contact_point_muzzle_trace;
362 vector contact_dir_muzzle;
367 int contact_component;
368 int contact_component_muzzle;
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();
376 Man player =
GetGame().GetPlayer();
378 Class.CastTo(player_o, player);
380 if (
DayZPhysics.RaycastRV(start_point, end_point, contact_point_cam_trace, contact_dir, contact_component,
null,
null, player_o ,
false,
false, ObjIntersectFire) )
382 m_TargetDistance =
vector.Distance(start_point, contact_point_cam_trace);
383 aim_at_position = contact_point_cam_trace;
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) )
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);
390 if ((collision_distance > 2 && muzzle_collision_distance < MAX_MUZZLE_DISTANCE_TOLERANCE) || m_TargetDistance < CAMERA_TRACE_MIN_DISTANCE_TOLERANCE)
393 aim_at_position = contact_point_muzzle_trace;
400 distance_to_aim_at =
vector.Distance(camera_pos, aim_at_position);
402 if ( m_IsFireKeyPressed )
404 Debug.RemoveShape(m_PermanentLine1);
405 Debug.RemoveShape(m_PermanentLine2);
406 Debug.RemoveShape(m_PermanentShape1);
408 vector contact_point_temp;
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 );
418 vector dir = contact_point_cam_trace - camera_pos;
421 float dst =
vector.Distance(camera_pos, contact_point_cam_trace);
422 float dist2 =
vector.Distance(camera_pos, contact_point_cam_trace);
425 m_PermanentLine1 =
Debug.DrawLine(start_point, contact_point_cam_trace + dir,
Colors.RED,
ShapeFlags.NOZBUFFER );
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);
434 Debug.RemoveShape(m_HitShape4);
435 Debug.RemoveShape(m_HitShape);
437 m_HitShape4 =
Debug.DrawSphere(aim_at_position, hit_sphere_size);