bodyparts.inc 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*
  2. Body Part Detection - Detect a player's body part
  3. By Seif
  4. */
  5. /*x---------------------------------Important-------------------------------------x*/
  6. //**INCLUDES**//
  7. #include <a_samp>
  8. #if !defined function
  9. #define function%0(%1) forward %0(%1); public %0(%1)
  10. #endif
  11. /*x---------------------------------Defining-------------------------------------x*/
  12. #define MAX_DISTANCE_UNIT 100.0 // maximum distance a player can shoot from
  13. //**BODY PARTS**//
  14. #define BODY_PART_HEAD 1
  15. #define BODY_PART_TORSO 2
  16. #define BODY_PART_LEGS 3
  17. //#define SCRIPT_DEBUG
  18. /*x---------------------------------CallBacks-------------------------------------x*/
  19. /*/
  20. native IsPlayerAimingBodyPart(playerid, bodypart);
  21. native IsPlayerAimingTargetBodyPart(playerid, targetid, bodypart);
  22. */
  23. /*
  24. ---[IsPlayerAimingBodyPart]---
  25. »playerid: the player
  26. »bodypart: the body part you want to check
  27. *Return: 1 if true, 0 if false
  28. *-------------------------------------------------------------------*
  29. | Checks if the player is aiming at any player's certain body part. |
  30. *-------------------------------------------------------------------*
  31. */
  32. #if defined SCRIPT_DEBUG
  33. new shootpick[100];
  34. #endif
  35. function IsPlayerAimingBodyPart(playerid, bodypart)
  36. {
  37. #if defined SCRIPT_DEBUG
  38. new c;
  39. #endif
  40. // Get the camera's positions
  41. new Float:x, Float:y, Float:z, Float:a;
  42. new Float:vx, Float:vy, Float:vz;
  43. new Float:cx, Float:cy, Float:cz;
  44. new Float:offset;
  45. new Float:radius;
  46. GetPlayerCameraFrontVector(playerid, vx, vy, vz);
  47. GetPlayerCameraPos(playerid, cx, cy, cz);
  48. GetPlayerFacingAngle(playerid, a);
  49. // Check if the player is aiming in a certain distance
  50. for(new Float:d; d < MAX_DISTANCE_UNIT; d += 0.5)
  51. {
  52. switch (GetPlayerWeapon(playerid))
  53. {
  54. case 24, 29, 22, 23, 25, 26, 27, 28, 32: offset = 0.11;
  55. case 30, 31: offset = 0.07;
  56. case 33: offset = 0.045;
  57. default: offset = 0.0;
  58. }
  59. switch (GetPlayerWeapon(playerid))
  60. {
  61. case 22, 26, 28, 32:
  62. {
  63. // duals, where you don't need to change your angle to change aim direction -- Not very accurate, but considering they spray, it should be good
  64. x = vx*d+cx;
  65. y = vy*d+cy;
  66. }
  67. default:
  68. {
  69. // this is for weapons where your angle moves when you change your aim(deagle, sdpistol, m4, etc)
  70. x = cx + (d * floatsin(-a, degrees));
  71. y = cy + (d * floatcos(-a, degrees));
  72. }
  73. }
  74. z = (vz+offset)*d+cz;
  75. switch (bodypart)
  76. {
  77. case BODY_PART_HEAD: z -= 0.0, radius = 0.3; // the offsets are made for head shots
  78. case BODY_PART_TORSO: z += 0.6, radius = 0.5;
  79. case BODY_PART_LEGS: z += 1.2, radius = 0.4;
  80. }
  81. #if defined SCRIPT_DEBUG
  82. if (IsValidObject(shootpick[c])) DestroyObject(shootpick[c]);
  83. shootpick[c] = CreateObject(1274, x, y, z, 0.0, 0.0, 0.0);
  84. c++;
  85. #endif
  86. for(new i, m = GetMaxPlayers(); i < m; i++)
  87. {
  88. if (!IsPlayerConnected(i)) continue;
  89. if (playerid == i) continue;
  90. if (GetPlayerSpecialAction(i) == SPECIAL_ACTION_DUCK)
  91. {
  92. if (IsPlayerInRangeOfPoint(i, radius+0.2, x, y, z+1.2-1.3-(bodypart==BODY_PART_TORSO?0.42:0.0)))
  93. {
  94. return i;
  95. }
  96. }
  97. else if (IsPlayerInRangeOfPoint(i, radius, x, y, z-0.8))
  98. {
  99. return i;
  100. }
  101. }
  102. }
  103. return INVALID_PLAYER_ID;
  104. }
  105. /*
  106. ---[IsPlayerAimingTargetBodyPart]---
  107. »playerid: the player
  108. »targetid: the target
  109. »bodypart: the body part you want to check
  110. *Return: 1 if true, 0 if false
  111. *-------------------------------------------------------------------*
  112. | Checks if the player is aiming at target's specific body part. |
  113. *-------------------------------------------------------------------*
  114. */
  115. function IsPlayerAimingTargetBodyPart(playerid, targetid, bodypart)
  116. {
  117. #if defined SCRIPT_DEBUG
  118. new c;
  119. #endif
  120. // Get the camera's positions
  121. new Float:x, Float:y, Float:z, Float:a;
  122. new Float:vx, Float:vy, Float:vz;
  123. new Float:cx, Float:cy, Float:cz;
  124. new Float:offset;
  125. new Float:radius;
  126. GetPlayerCameraFrontVector(playerid, vx, vy, vz);
  127. GetPlayerCameraPos(playerid, cx, cy, cz);
  128. GetPlayerFacingAngle(playerid, a);
  129. // Check if the player is aiming in a certain distance
  130. for(new Float:d; d < MAX_DISTANCE_UNIT; d += 0.5)
  131. {
  132. switch (GetPlayerWeapon(playerid))
  133. {
  134. case 24, 29, 22, 23, 25, 26, 27, 28, 32: offset = 0.11;
  135. case 30, 31: offset = 0.07;
  136. case 33, 34: offset = 0.0;
  137. }
  138. switch (GetPlayerWeapon(playerid))
  139. {
  140. case 22, 26, 28, 32:
  141. {
  142. // duals, where you don't need to change your angle to change aim direction -- Not very accurate, but considering they spray, it should be good
  143. x = vx*d+cx;
  144. y = vy*d+cy;
  145. }
  146. default:
  147. {
  148. // this is for weapons where your angle moves when you change your aim(deagle, sdpistol, m4, etc)
  149. x = cx + (d * floatsin(-a, degrees));
  150. y = cy + (d * floatcos(-a, degrees));
  151. }
  152. }
  153. z = (vz+offset)*d+cz;
  154. switch (bodypart)
  155. {
  156. case BODY_PART_HEAD: z -= 0.0, radius = 0.3; // the offsets are made for head shots
  157. case BODY_PART_TORSO: z += 0.6, radius = 0.5;
  158. case BODY_PART_LEGS: z += 1.2, radius = 0.4;
  159. }
  160. #if defined SCRIPT_DEBUG
  161. if (IsValidObject(shootpick[c])) DestroyObject(shootpick[c]);
  162. shootpick[c] = CreateObject(1274, x, y, z, 0.0, 0.0, 0.0);
  163. c++;
  164. #endif
  165. if (GetPlayerSpecialAction(targetid) == SPECIAL_ACTION_DUCK)
  166. {
  167. if (IsPlayerInRangeOfPoint(targetid, radius+0.2, x, y, z+1.2-1.3-(bodypart==BODY_PART_TORSO?0.42:0.0)))
  168. {
  169. return 1;
  170. }
  171. }
  172. else if (IsPlayerInRangeOfPoint(targetid, radius, x, y, z-0.8))
  173. {
  174. return 1;
  175. }
  176. }
  177. return 0;
  178. }