Advertisement
Guest User

[SAMP] GetPlayerAimedBodyPart v1.01 include by Dinnozor -fix

a guest
Jan 6th, 2013
514
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.05 KB | None | 0 0
  1. /*
  2.  
  3. GetPlayerAimedBodyPart include (v1.01) by Dinnozor
  4.  
  5. Detects if you shoot the head/arm/leg/torso of a player !
  6.  
  7. Feel free to share but please don't remove credits.
  8.  
  9. v1.01:
  10. -made it easier to shoot the arm, which was almost impossible in the 1.0
  11. -kind of found a solution about the widescreen on/off problem (thanks to Nanory on Samp Forums for reporting) : the script checks if the player has an "overcorrection" with the GetPlayerCameraFrontVector Z-coord (which was the case when the player used Widescreen). If so, it will put the player in a "Widescreen" mode.
  12. Also (since all the values were got experimentally), it will make the inverse check with angle correction (still about the GetPlayerCameraFrontVector inaccuracy with other weapons than sniper rifle) to check if the player lacks of correction. So basically, if the player is not in Widescreen but the script detected so (because of any bug or a wrong calculation), the script will automatically put the player back in non-Widescreen mode.
  13. Added some calculation to do that, and a PVar "Widescreen".
  14. Moreover, please note the script will be more accurate without Widescreen.
  15.  
  16. More info here : http://forum.sa-mp.com/showthread.php?t=403713
  17. Video here : http://www.youtube.com/watch?v=2Y4ngd_wpzY
  18.  
  19. */
  20.  
  21. #include <a_samp>
  22.  
  23. #define BP_HEAD 1
  24. #define BP_BODY 0
  25. #define BP_ARM 3
  26. #define BP_LEG 2
  27.  
  28. forward Float:Distance(Float:xA,Float:yA,Float:zA,Float:xB,Float:yB,Float:zB);
  29.  
  30. forward OnPlayerHeadshot(playerid,targetid,weaponid);
  31. forward OnPlayerLegshot(playerid,targetid,weaponid);
  32. forward OnPlayerArmshot(playerid,targetid,weaponid);
  33.  
  34. static
  35.     bool:GPABP_OPC,
  36.     bool:GPABP_OPTD;
  37.  
  38. public OnGameModeInit()
  39. {
  40.     GPABP_OPC=(funcidx("GPABP_OnPlayerConnect")!=-1);
  41.     GPABP_OPTD=(funcidx("GPABP_OnPlayerTakeDamage")!=-1);
  42.     return (funcidx("GPABP_OnGameModeInit")!=-1)?CallLocalFunction("GPABP_OnGameModeInit",""):1;
  43.  
  44. }
  45.  
  46. #if defined _ALS_OnGameModeInit
  47.     #undef OnGameModeInit
  48. #else
  49.     #define _ALS_OnGameModeInit
  50. #endif
  51. #define OnGameModeInit GPABP_OnGameModeInit
  52.  
  53. forward GPABP_OnGameModeInit();
  54.  
  55. public OnPlayerConnect(playerid)
  56. {
  57.         SetPVarInt(playerid,"Widescreen",0);//Assuming most of the people don't use Widescreen
  58.         return (GPABP_OPC)?CallLocalFunction("GTABP_OnPlayerConnect","i",playerid):1;
  59. }
  60.  
  61. #if defined _ALS_OnPlayerConnect
  62.     #undef OnPlayerConnect
  63. #else
  64.     #define _ALS_OnPlayerConnect
  65. #endif
  66. #define OnPlayerConnect GPABP_OnPlayerConnect
  67.  
  68. forward GPABP_OnPlayerConnect(playerid);
  69.  
  70. public Float:Distance(Float:xA,Float:yA,Float:zA,Float:xB,Float:yB,Float:zB)
  71. {
  72.         new Float:Dist=floatsqroot((xB-xA)*(xB-xA)+(yB-yA)*(yB-yA)+(zB-zA)*(zB-zA));
  73.         return Dist;
  74. }
  75.  
  76. stock GetPlayerAimedBodyPart(playerid, targetid, Float:range,weapon)
  77. {
  78.         new Float:AngleD, Float:xv,Float:yv,Float:zv,Float:xc,Float:yc,Float:zc,Float:xt,Float:yt,Float:zt,Float:X2,Float:Y2,Float:Z2;
  79.         new Float:AngleP,Float:Zdif,Float:xP,Float:yP,Float:zP,Float:zTest;
  80.         GetPlayerPos(targetid,X2,Y2,Z2);
  81.         GetPlayerPos(playerid,xP,yP,zP);
  82.         GetPlayerCameraPos(playerid,xc,yc,zc);
  83.         GetPlayerCameraFrontVector(playerid,xv,yv,zv);
  84.         GetPlayerFacingAngle(targetid,AngleP);
  85.         xt=range*xv+xc;
  86.         yt=range*yv+yc;
  87.         zt=range*zv+zc;
  88.         if(GetPVarInt(playerid,"Widescreen")==0)
  89.         {
  90.                 zTest=zv-(Z2-zP)/Distance(X2,Y2,Z2,xP,yP,zP);
  91.  
  92.                 if(GetPlayerSpecialAction(playerid)==1)
  93.                 {
  94.                         switch(weapon)
  95.  
  96.                         {
  97.                                 case 34:AngleD=0;
  98.                                 case 33:
  99.                                 {
  100.                                         AngleD=3;
  101.                                         Zdif=-0.033;
  102.  
  103.                                 }
  104.                                 case 30,31:
  105.                                 {
  106.                                         AngleD=4;
  107.                                         Zdif=-0.045;
  108.  
  109.                                 }
  110.                                 case 22..24:
  111.                                 {
  112.                                         AngleD=6;
  113.                                         Zdif=-0.1;
  114.  
  115.                                 }
  116.                                 case 25..27:
  117.                                 {
  118.                                         AngleD=4;
  119.                                         Zdif=-0.07;
  120.  
  121.                                 }
  122.                                 case 28,29,32:
  123.                                 {
  124.                                         Zdif=-0.07;
  125.                                         AngleD=6;
  126.  
  127.                                 }
  128.                                 default:
  129.                                 {
  130.                                         AngleD=2;
  131.                                         Zdif=-0.1;
  132.  
  133.                                 }
  134.  
  135.                         }
  136.  
  137.                 }
  138.                 else
  139.                 {
  140.                         switch(weapon)
  141.  
  142.                         {
  143.                                 case 34:AngleD=0;
  144.                                 case 33:
  145.                                 {
  146.                                         AngleD=3;
  147.                                         Zdif=-0.045;
  148.  
  149.                                 }
  150.                                 case 30,31:
  151.                                 {
  152.                                         AngleD=4;
  153.                                         Zdif=-0.065;
  154.  
  155.                                 }
  156.                                 case 22..24:
  157.                                 {
  158.                                         AngleD=6;
  159.                                         Zdif=-0.095;
  160.  
  161.                                 }
  162.                                 case 25..27:
  163.                                 {
  164.                                         AngleD=4;
  165.                                         Zdif=-0.07;
  166.  
  167.                                 }
  168.                                 case 28,29,32:
  169.                                 {
  170.                                         Zdif=-0.085;
  171.                                         AngleD=6;
  172.  
  173.                                 }
  174.                                 default:
  175.                                 {
  176.                                         AngleD=2;
  177.                                         Zdif=-0.1;
  178.  
  179.                                 }
  180.  
  181.                         }
  182.  
  183.  
  184.  
  185.                 }
  186.  
  187.                 if(zTest>Zdif&&weapon!=34)
  188.                 {
  189.                         SetPVarInt(playerid,"Widescreen",1);
  190.  
  191.                 }
  192.  
  193.         }
  194.         else
  195.         {
  196.                 zTest=asin(zt/range);
  197.                 switch(weapon)
  198.                 {
  199.                         case 34:
  200.                         {
  201.                                 AngleD=0;
  202.                                 Zdif=0;
  203.  
  204.                         }
  205.                         case 33:
  206.                         {
  207.                                 AngleD=2;
  208.                                 Zdif=1.5;
  209.  
  210.                         }
  211.                         case 30,31:
  212.                         {
  213.                                 AngleD=3;
  214.                                 Zdif=3;
  215.  
  216.                         }
  217.                         case 22..24:
  218.                         {
  219.                                 AngleD=4;
  220.                                 Zdif=4;
  221.  
  222.                         }
  223.                         case 25..27:
  224.                         {
  225.                                 AngleD=3;
  226.                                 Zdif=3;
  227.  
  228.                         }
  229.                         default:
  230.                         {
  231.                                 AngleD=4.5;
  232.                                 Zdif=3;
  233.  
  234.                         }
  235.  
  236.                 }
  237.                 if(zTest>Zdif&&weapon!=34)
  238.                 {
  239.                         SetPVarInt(playerid,"Widescreen",0);
  240.  
  241.                 }
  242.  
  243.         }
  244.  
  245.         if(GetPlayerSpecialAction(targetid)==1)
  246.         {
  247.                 if(Z2-0.1<=range*floattan(AngleD,degrees)+zt) return BP_HEAD;
  248.                 else if((Z2-0.55<=zt+range*floattan(AngleD,degrees))&&floatabs(0.5*floatcos(-AngleP,degrees)+X2-xt)<=0.5&&floatabs(-0.5*floatsin(-AngleP,degrees)+Y2-yt)<=0.5) return BP_ARM;
  249.  
  250.         }
  251.         else if(GetPlayerState(playerid)!=1)
  252.         {
  253.                 if(Z2+0.4<=range*floattan(AngleD,degrees)+zt) return BP_HEAD;
  254.  
  255.         }
  256.         else
  257.         {
  258.                 if((Z2-0.2<=zt+range*floattan(AngleD,degrees))&&(floatabs(0.27*floatcos(-AngleP,degrees)+X2-xt)<=0.15)&&(floatabs(0.27*floatsin(-AngleP,degrees)+Y2-yt)<=0.2)) return BP_ARM;
  259.                 else if(Z2+0.5<=range*floattan(AngleD,degrees)+zt) return BP_HEAD;
  260.                 else if(Z2-0.3>=zt+range*floattan(AngleD,degrees)) return BP_LEG;
  261.  
  262.         }
  263.         return BP_BODY;
  264. }
  265.  
  266. public OnPlayerTakeDamage(playerid, issuerid, Float:amount, weaponid)
  267. {
  268.         new Float:Xt,Float:Yt,Float:Zt;
  269.         GetPlayerPos(playerid,Xt,Yt,Zt);
  270.         if(issuerid!=INVALID_PLAYER_ID)
  271.  
  272.         {
  273.  
  274.                 new Float:xc,Float:yc,Float:zc,Float:DistC;
  275.                 GetPlayerCameraPos(issuerid,xc,yc,zc);
  276.                 DistC=Distance(xc,yc,zc,Xt,Yt,Zt);
  277.  
  278.                 switch(GetPlayerAimedBodyPart(issuerid,playerid,DistC,weaponid))
  279.  
  280.                 {
  281.                         case 1:CallLocalFunction("OnPlayerHeadshot", "ddd", issuerid,playerid,weaponid);
  282.                         case 2:CallLocalFunction("OnPlayerLegshot", "ddd", issuerid,playerid,weaponid);
  283.                         case 3:CallLocalFunction("OnPlayerArmshot", "ddd", issuerid,playerid,weaponid);
  284.                         //Create a call back for torso shots if you want : use default here
  285.  
  286.  
  287.                 }
  288.  
  289.  
  290.         }
  291.         return (GPABP_OPTD)?CallLocalFunction("GPABP_OnPlayerTakeDamage","iifi",playerid,issuerid,amount,weaponid):1;
  292. }
  293.  
  294. #if defined _ALS_OnPlayerTakeDamage
  295.     #undef OnPlayerTakeDamage
  296. #else
  297.     #define _ALS_OnPlayerTakeDamage
  298. #endif
  299. #define OnPlayerTakeDamage GPABP_OnPlayerTakeDamage
  300.  
  301. forward GPABP_OnPlayerTakeDamage(playerid,issuerid,Float:amount,weaponid);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement