Advertisement
Guest User

Untitled

a guest
Sep 26th, 2010
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 6.88 KB | None | 0 0
  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.  
  9. #if !defined function
  10.     #define function%0(%1) forward %0(%1); public %0(%1)
  11. #endif
  12. /*x---------------------------------Defining-------------------------------------x*/
  13. #define MAX_DISTANCE_UNIT   100.0   // maximum distance a player can shoot from
  14. //**BODY PARTS**//
  15. #define BODY_PART_HEAD  1
  16. #define BODY_PART_TORSO 2
  17. #define BODY_PART_LEGS  3
  18.  
  19. //#define SCRIPT_DEBUG
  20. /*x---------------------------------CallBacks-------------------------------------x*/
  21. /*/
  22.     native IsPlayerAimingBodyPart(playerid, bodypart);
  23.     native IsPlayerAimingTargetBodyPart(playerid, targetid, bodypart);
  24. */
  25. /*
  26.                                             ---[IsPlayerAimingBodyPart]---
  27.             »playerid: the player
  28.             »bodypart: the body part you want to check
  29.             *Return: 1 if true, 0 if false
  30.         *-------------------------------------------------------------------*
  31.         | Checks if the player is aiming at any player's certain body part. |
  32.         *-------------------------------------------------------------------*
  33. */
  34. #if defined SCRIPT_DEBUG
  35.     new shootpick[100];
  36. #endif
  37. function IsPlayerAimingBodyPart(playerid, bodypart)
  38. {
  39.     #if defined SCRIPT_DEBUG
  40.         new c;
  41.     #endif
  42.     // Get the camera's positions
  43.     new Float:x, Float:y, Float:z, Float:a;
  44.     new Float:vx, Float:vy, Float:vz;
  45.     new Float:cx, Float:cy, Float:cz;
  46.     new Float:offset;
  47.     new Float:radius;
  48.     GetPlayerCameraFrontVector(playerid, vx, vy, vz);
  49.     GetPlayerCameraPos(playerid, cx, cy, cz);
  50.     GetPlayerFacingAngle(playerid, a);
  51.     // Check if the player is aiming in a certain distance
  52.     for(new Float:d; d < MAX_DISTANCE_UNIT; d += 0.5)
  53.     {
  54.         switch (GetPlayerWeapon(playerid))
  55.         {
  56.             case 24, 29, 22, 23, 25, 26, 27, 28, 32: offset = 0.11;
  57.             case 30, 31: offset = 0.07;
  58.             case 33: offset = 0.045;
  59.             default: offset = 0.0;
  60.         }
  61.         switch (GetPlayerWeapon(playerid))
  62.         {
  63.             case 22, 26, 28, 32:
  64.             {
  65.                 // 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
  66.                 x = vx*d+cx;
  67.                 y = vy*d+cy;
  68.             }
  69.             default:
  70.             {
  71.                 // this is for weapons where your angle moves when you change your aim(deagle, sdpistol, m4, etc)
  72.                 x = cx + (d * floatsin(-a, degrees));
  73.                 y = cy + (d * floatcos(-a, degrees));
  74.             }
  75.         }
  76.  
  77.         z = (vz+offset)*d+cz;
  78.         switch (bodypart)
  79.         {
  80.             case BODY_PART_HEAD: z -= 0.0, radius = 0.3;  // the offsets are made for head shots
  81.             case BODY_PART_TORSO: z += 0.6, radius = 0.5;
  82.             case BODY_PART_LEGS: z += 1.2, radius = 0.4;
  83.         }
  84.  
  85.         #if defined SCRIPT_DEBUG
  86.             if (IsValidObject(shootpick[c])) DestroyObject(shootpick[c]);
  87.             shootpick[c] = CreateObject(1274, x, y, z, 0.0, 0.0, 0.0);
  88.             c++;
  89.         #endif
  90.        
  91.         for(new i, m = GetMaxPlayers(); i < m; i++)
  92.         {
  93.             if (!IsPlayerConnected(i)) continue;
  94.             if (playerid == i) continue;
  95.             if (GetPlayerSpecialAction(i) == SPECIAL_ACTION_DUCK)
  96.             {
  97.                 if (IsPlayerInRangeOfPoint(i, radius+0.2, x, y, z+1.2-1.3-(bodypart==BODY_PART_TORSO?0.42:0.0)))
  98.                 {
  99.                     return i;
  100.                 }
  101.             }
  102.             else if (IsPlayerInRangeOfPoint(i, radius, x, y, z-0.8))
  103.             {
  104.                 return i;
  105.             }
  106.         }
  107.     }
  108.     return INVALID_PLAYER_ID;
  109. }
  110.  
  111. /*
  112.                                             ---[IsPlayerAimingTargetBodyPart]---
  113.             »playerid: the player
  114.             »targetid: the target
  115.             »bodypart: the body part you want to check
  116.             *Return: 1 if true, 0 if false
  117.         *-------------------------------------------------------------------*
  118.         | Checks if the player is aiming at target's specific body part.    |
  119.         *-------------------------------------------------------------------*
  120. */
  121. function IsPlayerAimingTargetBodyPart(playerid, targetid, bodypart)
  122. {
  123.     #if defined SCRIPT_DEBUG
  124.         new c;
  125.     #endif
  126.     // Get the camera's positions
  127.     if(!IsPlayerLookingAtPlayer(playerid, targetid)) return 1;
  128.     new Float:x, Float:y, Float:z, Float:a;
  129.     new Float:vx, Float:vy, Float:vz;
  130.     new Float:cx, Float:cy, Float:cz;
  131.     new Float:offset;
  132.     new Float:radius;
  133.     GetPlayerCameraFrontVector(playerid, vx, vy, vz);
  134.     GetPlayerCameraPos(playerid, cx, cy, cz);
  135.     GetPlayerFacingAngle(playerid, a);
  136.     // Check if the player is aiming in a certain distance
  137.     for(new Float:d; d < MAX_DISTANCE_UNIT; d += 0.5)
  138.     {
  139.         switch (GetPlayerWeapon(playerid))
  140.         {
  141.             case 24, 29, 22, 23, 25, 26, 27, 28, 32: offset = 0.11;
  142.             case 30, 31: offset = 0.07;
  143.             case 33, 34: offset = 0.0;
  144.         }
  145.         switch (GetPlayerWeapon(playerid))
  146.         {
  147.             case 22, 26, 28, 32:
  148.             {
  149.                 // 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
  150.                 x = vx*d+cx;
  151.                 y = vy*d+cy;
  152.             }
  153.             default:
  154.             {
  155.                 // this is for weapons where your angle moves when you change your aim(deagle, sdpistol, m4, etc)
  156.                 x = cx + (d * floatsin(-a, degrees));
  157.                 y = cy + (d * floatcos(-a, degrees));
  158.             }
  159.         }
  160.  
  161.         z = (vz+offset)*d+cz;
  162.         switch (bodypart)
  163.         {
  164.             case BODY_PART_HEAD: z -= 0.0, radius = 0.3;  // the offsets are made for head shots
  165.             case BODY_PART_TORSO: z += 0.6, radius = 0.5;
  166.             case BODY_PART_LEGS: z += 1.2, radius = 0.4;
  167.         }
  168.  
  169.         #if defined SCRIPT_DEBUG
  170.             if (IsValidObject(shootpick[c])) DestroyObject(shootpick[c]);
  171.             shootpick[c] = CreateObject(1274, x, y, z, 0.0, 0.0, 0.0);
  172.             c++;
  173.         #endif
  174.  
  175.         if (GetPlayerSpecialAction(targetid) == SPECIAL_ACTION_DUCK)
  176.         {
  177.             if (IsPlayerInRangeOfPoint(targetid, radius+0.2, x, y, z+1.2-1.3-(bodypart==BODY_PART_TORSO?0.42:0.0)))
  178.             {
  179.                 return 1;
  180.             }
  181.         }
  182.         else if (IsPlayerInRangeOfPoint(targetid, radius, x, y, z-0.8))
  183.         {
  184.             return 1;
  185.         }
  186.     }
  187.     return 0;
  188. }
  189.  
  190. stock IsPlayerLookingAtPlayer(player1, player2) { // Simon edited by Carlton
  191.     if (!IsPlayerConnected(player1) || !IsPlayerConnected(player2)) return 0;
  192.     if(player1 == player2) return 0;
  193.     new
  194.         Float: distance,
  195.         Float: vectorX,
  196.         Float: vectorY,
  197.         Float: vectorZ,
  198.         Float: plyrPos[2][3],
  199.         Float: projPos[3];
  200.     GetPlayerCameraFrontVector(player1, vectorX, vectorY, vectorZ);
  201.     GetPlayerCameraPos(player1, plyrPos[0][0], plyrPos[0][1], plyrPos[0][2]);
  202.     GetPlayerPos(player2, plyrPos[1][0], plyrPos[1][1], plyrPos[1][2]);
  203.     #define SQUARE(%1)  ((%1)*(%1))
  204.     distance = floatsqroot(
  205.     SQUARE(plyrPos[1][0]-plyrPos[0][0]) + SQUARE(plyrPos[1][1]-plyrPos[0][1]) + SQUARE(plyrPos[1][2]-plyrPos[0][2]));
  206.     projPos[0] = plyrPos[0][0] + vectorX * distance;
  207.     projPos[1] = plyrPos[0][1] + vectorY * distance;
  208.     projPos[2] = plyrPos[0][2] + vectorZ * distance;
  209.     return ((SQUARE(plyrPos[1][0]-projPos[0]) + SQUARE(plyrPos[1][1]-projPos[1]) + SQUARE(plyrPos[1][2]-projPos[2])) <= SQUARE(distance / 6));
  210.     #undef SQUARE
  211. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement