weapondebug.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. enum eDebugMode
  2. {
  3. NORMAL,
  4. MUZZLE_FIRE,
  5. CAMERA_MUZZLE_HYBRID,
  6. COUNT
  7. };
  8. class WeaponDebug
  9. {
  10. const int BUFFER_SIZE = 1000;
  11. const float COLLISIONS_DISTANCE_TOLERANCE = 0.01;
  12. const float MAX_MUZZLE_DISTANCE_TOLERANCE = 20;
  13. const float CAMERA_BULLET_ORIGIN_OFFSET = 1;
  14. const float CAMERA_TRACE_MIN_DISTANCE_TOLERANCE = 0.3;
  15. Weapon m_WeaponInHands;
  16. int m_BufferIndex;
  17. bool m_IsDrawKeyHeldDown;
  18. bool m_IsLMBPressed;
  19. bool m_IsToggleKeyPressed;
  20. bool m_IsFireKeyPressed;
  21. float m_TargetDistance;
  22. eDebugMode m_CurrentMode;
  23. vector m_AimTrailCyclic[BUFFER_SIZE];
  24. vector m_AimTrailOrdered[BUFFER_SIZE];
  25. ref map<int, string> m_DebugModesNames = new map<int, string>;
  26. //ref array<Selection> m_Selections = new array<Selection>();
  27. Shape m_Shape_usti;
  28. Shape m_Shape_konec;
  29. Shape m_ShapeFireDirection1;
  30. Shape m_ShapeFireDirection2;
  31. Shape m_HitShape;
  32. Shape m_ShapeEye;
  33. Shape m_ShapeTrailLines;
  34. Shape m_ShapeFireDirCamera;
  35. Shape m_HitShape2;
  36. Shape m_HitShape3;
  37. Shape m_HitShape4;
  38. //Shape temp_shape;
  39. Shape m_PermanentLine1;
  40. Shape m_PermanentLine2;
  41. Shape m_PermanentShape1;
  42. Shape m_PermanentShape2;
  43. Weapon GetWeaponInHands()
  44. {
  45. Weapon weapon_in_hands;
  46. PlayerBase player = PlayerBase.Cast(GetGame().GetPlayer());
  47. if( player && player.GetItemInHands() ) Class.CastTo(weapon_in_hands, player.GetItemInHands());
  48. return weapon_in_hands;
  49. }
  50. void WeaponDebug()
  51. {
  52. m_DebugModesNames.Insert(eDebugMode.NORMAL, "Mode "+eDebugMode.NORMAL.ToString()+": shoot along the weapon barrel");
  53. m_DebugModesNames.Insert(eDebugMode.MUZZLE_FIRE,"Mode "+eDebugMode.MUZZLE_FIRE.ToString()+": shoot from muzzle end to camera trace hit point");
  54. 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");
  55. }
  56. void ~WeaponDebug()
  57. {
  58. RemoveAllShapes(true);
  59. }
  60. void RemoveAllShapes(bool is_exit = false)
  61. {
  62. Debug.RemoveShape(m_Shape_usti);
  63. Debug.RemoveShape(m_Shape_konec);
  64. Debug.RemoveShape(m_ShapeFireDirection1);
  65. Debug.RemoveShape(m_ShapeFireDirection2);
  66. Debug.RemoveShape(m_ShapeEye);
  67. Debug.RemoveShape(m_ShapeTrailLines);
  68. Debug.RemoveShape(m_ShapeFireDirCamera);
  69. Debug.RemoveShape(m_HitShape);
  70. Debug.RemoveShape(m_HitShape2);
  71. Debug.RemoveShape(m_HitShape3);
  72. Debug.RemoveShape(m_HitShape4);
  73. if ( is_exit )
  74. {
  75. Debug.RemoveShape(m_PermanentLine1);
  76. Debug.RemoveShape(m_PermanentLine2);
  77. Debug.RemoveShape(m_PermanentShape1);
  78. Debug.RemoveShape(m_PermanentShape2);
  79. }
  80. }
  81. void OnCommandHandlerUpdate()
  82. {
  83. m_IsLMBPressed = false;
  84. m_IsFireKeyPressed = false;
  85. if (KeyState(KeyCode.KC_LWIN) == 1)
  86. {
  87. if (!m_IsDrawKeyHeldDown)
  88. {
  89. OnKeyDown(KeyCode.KC_LWIN);
  90. }
  91. m_IsDrawKeyHeldDown = true;
  92. }
  93. else
  94. {
  95. if ( m_IsDrawKeyHeldDown )
  96. {
  97. //OnKeyUp();
  98. }
  99. m_IsDrawKeyHeldDown = false;
  100. }
  101. if (KeyState(KeyCode.KC_Z) == 1)
  102. {
  103. ClearKey(KeyCode.KC_Z);
  104. CycleDebugMode();
  105. }
  106. if (KeyState(KeyCode.KC_X) == 1)
  107. {
  108. m_IsFireKeyPressed = true;
  109. ClearKey(KeyCode.KC_X);
  110. }
  111. if (GetMouseState(MouseState.LEFT) & MB_PRESSED_MASK)
  112. {
  113. m_IsLMBPressed = true;
  114. }
  115. }
  116. void OnKeyDown(KeyCode key)
  117. {
  118. if (key == KeyCode.KC_X)
  119. {
  120. }
  121. }
  122. void CycleDebugMode()
  123. {
  124. m_CurrentMode++;
  125. if ( m_CurrentMode == eDebugMode.COUNT )
  126. {
  127. m_CurrentMode = 0;
  128. }
  129. }
  130. void OnPostFrameUpdate()
  131. {
  132. if ( GetWeaponInHands() )
  133. {
  134. RemoveAllShapes();
  135. Weapon weapon = GetWeaponInHands();
  136. vector cameraDirection = GetGame().GetCurrentCameraDirection();
  137. vector cameraPosition = GetGame().GetCurrentCameraPosition();
  138. vector usti_hlavne_position = weapon.GetSelectionPositionMS( "usti hlavne" );//usti hlavne
  139. vector konec_hlavne_position = weapon.GetSelectionPositionMS( "konec hlavne" );//konec hlavne
  140. usti_hlavne_position = weapon.ModelToWorld(usti_hlavne_position);
  141. konec_hlavne_position = weapon.ModelToWorld(konec_hlavne_position);
  142. if ( m_CurrentMode == eDebugMode.NORMAL )
  143. {
  144. DrawLineOfFire(konec_hlavne_position,usti_hlavne_position );
  145. }
  146. if ( m_CurrentMode == eDebugMode.MUZZLE_FIRE )
  147. {
  148. DrawLineOfFireMuzzleToHit(usti_hlavne_position, cameraDirection, cameraPosition);
  149. }
  150. if ( m_CurrentMode == eDebugMode.CAMERA_MUZZLE_HYBRID )
  151. {
  152. DrawLineOfFireCameraHybrid(usti_hlavne_position, cameraDirection, cameraPosition, konec_hlavne_position);
  153. }
  154. if (m_IsDrawKeyHeldDown)
  155. {
  156. AddPosToCyclicBuffer(usti_hlavne_position);
  157. OrderTrailArray();
  158. }
  159. m_ShapeTrailLines = Debug.DrawLines( m_AimTrailOrdered,BUFFER_SIZE, COLOR_YELLOW, ShapeFlags.NOZBUFFER );
  160. DrawEyePoint(weapon);
  161. DrawBarrelMemoryPoints(konec_hlavne_position,usti_hlavne_position );
  162. DisplayGeneralInfo();
  163. DisplayTargetInfo();
  164. /*
  165. vector pos;
  166. weapon.GetProjectedCursorPos3d(pos);
  167. Debug.RemoveShape(temp_shape);
  168. temp_shape = Debug.DrawSphere(pos, 0.1, Colors.GREEN);
  169. */
  170. }
  171. }
  172. void DrawBarrelMemoryPoints(vector begin_point, vector end_point)
  173. {
  174. if (!m_IsDrawKeyHeldDown)
  175. {
  176. m_Shape_usti = Debug.DrawSphere(end_point, 0.011, Colors.GREEN, ShapeFlags.TRANSP|ShapeFlags.NOOUTLINE|ShapeFlags.NOZBUFFER);
  177. m_Shape_konec = Debug.DrawSphere(begin_point, 0.011, Colors.GREEN, ShapeFlags.TRANSP|ShapeFlags.NOOUTLINE|ShapeFlags.NOZBUFFER);
  178. }
  179. }
  180. void DrawLineOfFire(vector begin_point, vector end_point)
  181. {
  182. vector contact_point;
  183. vector contact_dir;
  184. vector aim_point = end_point - begin_point;
  185. int contact_component;
  186. aim_point = aim_point.Normalized() * 100;
  187. aim_point = aim_point + end_point;
  188. m_ShapeFireDirection1 = Debug.DrawLine(end_point, aim_point);
  189. m_ShapeFireDirection2 = Debug.DrawLine(begin_point, end_point, ShapeFlags.NOZBUFFER );
  190. if ( DayZPhysics.RaycastRV(end_point, aim_point, contact_point, contact_dir, contact_component, null, null, null, false, false, ObjIntersectFire) )
  191. {
  192. m_HitShape = Debug.DrawSphere(contact_point, 0.04, COLOR_RED);
  193. }
  194. if ( m_IsFireKeyPressed )
  195. {
  196. Debug.RemoveShape(m_PermanentLine1);
  197. m_PermanentLine1 = Debug.DrawLine(end_point, contact_point, Colors.RED, ShapeFlags.NOZBUFFER );
  198. }
  199. }
  200. void AddPosToCyclicBuffer(vector pos)
  201. {
  202. m_AimTrailCyclic[m_BufferIndex] = pos;
  203. m_BufferIndex++;
  204. if (m_BufferIndex == BUFFER_SIZE)
  205. {
  206. m_BufferIndex = 0;
  207. }
  208. }
  209. void OrderTrailArray()
  210. {
  211. int unordered_index;
  212. for (int i = 0; i < BUFFER_SIZE; i++)
  213. {
  214. unordered_index = m_BufferIndex + i;
  215. if ( unordered_index >= BUFFER_SIZE )
  216. {
  217. unordered_index = unordered_index - BUFFER_SIZE;
  218. }
  219. m_AimTrailOrdered[i] = m_AimTrailCyclic[unordered_index];
  220. }
  221. }
  222. void DrawEyePoint(Weapon weapon)
  223. {
  224. vector position = GetEyePointPosition(weapon);
  225. m_ShapeEye = Debug.DrawSphere(position, 0.009, COLOR_BLUE, ShapeFlags.TRANSP|ShapeFlags.NOOUTLINE|ShapeFlags.NOZBUFFER);
  226. }
  227. vector GetEyePointPosition(Weapon weapon)
  228. {
  229. string memory_point_name = weapon.ConfigGetString("memoryPointCamera");
  230. ItemBase optics = weapon.GetAttachedOptics();
  231. if (optics)
  232. {
  233. memory_point_name = optics.ConfigGetString("memoryPointCamera");
  234. return optics.ModelToWorld(optics.GetSelectionPositionLS( memory_point_name ));
  235. }
  236. return weapon.ModelToWorld(weapon.GetSelectionPositionLS( memory_point_name ));
  237. }
  238. void DisplayGeneralInfo()
  239. {
  240. DbgUI.Begin("sway weight", 50, 50);
  241. DayZPlayerImplement player = DayZPlayerImplement.Cast(GetGame().GetPlayer());
  242. float sway_weight = player.GetAimingModel().GetSwayWeight();
  243. DbgUI.Text("value: " + sway_weight.ToString());
  244. DbgUI.Text("Hold LWIN to draw debug line");
  245. DbgUI.Text("Press X to simulate fire");
  246. DbgUI.Text("Press Z to cycle debug modes");
  247. DbgUI.Text("Debug " +m_DebugModesNames.Get(m_CurrentMode) );
  248. DbgUI.Text("");
  249. DbgUI.End();
  250. }
  251. void DisplayTargetInfo()
  252. {
  253. DbgUI.Begin("target distance", 50,200);
  254. DbgUI.Text("value: " + m_TargetDistance.ToString());
  255. DbgUI.End();
  256. }
  257. void DrawLineOfFireMuzzleToHit(vector begin_point, vector camera_dir, vector camera_pos)
  258. {
  259. vector contact_point_cam_trace;
  260. vector contact_point_muzzle_trace;
  261. vector contact_dir_muzzle;
  262. vector contact_dir_camera;
  263. vector contact_point;
  264. vector contact_dir;
  265. int contact_component;
  266. int contact_component_muzzle;
  267. vector end_point = camera_pos + camera_dir * 1000;
  268. Man player = GetGame().GetPlayer();
  269. Object player_o;
  270. Class.CastTo(player_o, player);
  271. 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) )
  272. {
  273. m_ShapeFireDirCamera = Debug.DrawLine(begin_point, contact_point_cam_trace, Colors.RED, ShapeFlags.NOZBUFFER );
  274. m_HitShape2 = Debug.DrawSphere(contact_point_cam_trace, 0.03, Colors.GREEN);
  275. m_TargetDistance = vector.Distance( player.GetPosition(), contact_point_cam_trace);
  276. if ( m_IsFireKeyPressed )
  277. {
  278. Debug.RemoveShape(m_PermanentLine1);
  279. Debug.RemoveShape(m_PermanentLine2);
  280. m_PermanentLine1 = Debug.DrawLine(begin_point, contact_point_cam_trace, Colors.RED, ShapeFlags.NOZBUFFER );
  281. m_PermanentLine2 = Debug.DrawLine(camera_pos, contact_point_cam_trace, Colors.GREEN, ShapeFlags.NOZBUFFER );
  282. }
  283. }
  284. else
  285. {
  286. m_ShapeFireDirCamera = Debug.DrawLine(begin_point, end_point, Colors.GREEN, ShapeFlags.NOZBUFFER );
  287. m_TargetDistance = -1;
  288. }
  289. 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) )
  290. {
  291. m_HitShape3 = Debug.DrawSphere(contact_point_muzzle_trace, 0.03, COLOR_RED);
  292. }
  293. }
  294. void DrawLineOfFireCameraHybrid(vector usti_hlavne_position, vector camera_dir, vector camera_pos, vector konec_hlavne_position)
  295. {
  296. bool muzzle_shot;
  297. vector contact_point_cam_trace;
  298. vector contact_point_muzzle_trace;
  299. vector aim_at_position;
  300. vector contact_dir_muzzle;
  301. vector contact_dir;
  302. vector contact_point;
  303. int contact_component;
  304. int contact_component_muzzle;
  305. float distance_to_aim_at;
  306. vector end_point = camera_pos + camera_dir * 100;
  307. vector start_point = camera_pos + (CAMERA_BULLET_ORIGIN_OFFSET * camera_dir);
  308. vector weapon_aim_direction = usti_hlavne_position - konec_hlavne_position;
  309. weapon_aim_direction.Normalize();
  310. Man player = GetGame().GetPlayer();
  311. Object player_o;
  312. Class.CastTo(player_o, player);
  313. if ( DayZPhysics.RaycastRV(start_point, end_point, contact_point_cam_trace, contact_dir, contact_component,null, null, player_o , false, false, ObjIntersectFire) )
  314. {
  315. m_TargetDistance = vector.Distance(start_point, contact_point_cam_trace);
  316. aim_at_position = contact_point_cam_trace;
  317. 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) )
  318. {
  319. float collision_distance = vector.Distance(contact_point_cam_trace, contact_point_muzzle_trace);
  320. float muzzle_collision_distance = vector.Distance(usti_hlavne_position, contact_point_muzzle_trace);
  321. if ((collision_distance > 2 && muzzle_collision_distance < MAX_MUZZLE_DISTANCE_TOLERANCE) || m_TargetDistance < CAMERA_TRACE_MIN_DISTANCE_TOLERANCE)
  322. {
  323. muzzle_shot = true;
  324. aim_at_position = contact_point_muzzle_trace;
  325. }
  326. }
  327. }
  328. distance_to_aim_at = vector.Distance(camera_pos, aim_at_position);
  329. if ( m_IsFireKeyPressed )
  330. {
  331. Debug.RemoveShape(m_PermanentLine1);
  332. Debug.RemoveShape(m_PermanentLine2);
  333. Debug.RemoveShape(m_PermanentShape1);
  334. vector contact_point_temp;
  335. if (muzzle_shot)
  336. {
  337. m_PermanentLine1 = Debug.DrawLine(usti_hlavne_position, usti_hlavne_position + weapon_aim_direction * 5, Colors.RED, ShapeFlags.NOZBUFFER );
  338. m_PermanentLine2 = Debug.DrawLine(camera_pos, contact_point_cam_trace, Colors.GREEN, ShapeFlags.NOZBUFFER );
  339. PrintString("muzle shot");
  340. }
  341. else
  342. {
  343. vector dir = contact_point_cam_trace - camera_pos;
  344. dir.Normalize();
  345. dir = dir * 100;
  346. float dst = vector.Distance(camera_pos, contact_point_cam_trace);
  347. float dist2 = vector.Distance(camera_pos, contact_point_cam_trace);
  348. //m_PermanentShape1 = Debug.DrawSphere(start_point, 0.015,Colors.GREEN);
  349. m_PermanentLine1 = Debug.DrawLine(start_point, contact_point_cam_trace + dir, Colors.RED, ShapeFlags.NOZBUFFER );
  350. PrintString("camera shot");
  351. }
  352. }
  353. float clamped_distance = Math.Clamp(distance_to_aim_at, 0 , 100);
  354. float distance_normalized = Math.InverseLerp(0, 100, clamped_distance);
  355. float hit_sphere_size = Math.Lerp(0.025, 0.75, distance_normalized);
  356. Debug.RemoveShape(m_HitShape4);
  357. Debug.RemoveShape(m_HitShape);
  358. m_HitShape = Debug.DrawSphere(contact_point_cam_trace, 0.20,Colors.GREEN, ShapeFlags.TRANSP);
  359. m_HitShape4 = Debug.DrawSphere(aim_at_position, hit_sphere_size);
  360. }
  361. }