Guest User

SA:MP Basketballsystem

a guest
Nov 12th, 2025
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 12.88 KB | Gaming | 0 0
  1. /* Basketball System by Kaliber */
  2. #include <open.mp>
  3. /******************************************************************************/
  4. #define PRESSED(%0) (((newkeys & (%0)) == (%0)) && ((oldkeys & (%0)) != (%0)))
  5. #define PreloadAnim(%0,%1) ApplyAnimation(%0,%1,!"NULL",4.1,false,true,true,false,0)
  6. /******************************************************************************/
  7. #define sleep(%0)<%1>; SetTimer("@sleep_"#%1#,%0,false); return 1;}\
  8. @sleep_%1();@sleep_%1() {
  9. /******************************************************************************/
  10. #define pSleep(%0)<%1,%2,%3>; SetTimerEx("@pSleep_"#%1#,%0,false,%2,%3); return 1;}\
  11. @pSleep_%1(%3);@pSleep_%1(%3) {
  12. /******************************************************************************/
  13. const Float:DISTANCE_TO_GROUND=0.8,Float:DISTANCE_IN_FRONT=1.0,Float:flugSpeed=6.0,Float:wurfHeight=5.0,Float:wurfWeite=10.0,Float:passHeight=2.5,Float:passSpeed=8.0;
  14. new ball = -1, holder = -1, Float:ballSpeed = 4.75, korbCheck = -1, ballTrick = -1, bool:passWurf = false, bool:korbWurf = false, bool:ballLocked = false, wurfTimer = -1, endWurfTimer = -1, lastHolder = -1;
  15. new infoPickup, Text3D:infoLabel;
  16. /******************************************************************************/
  17. public OnFilterScriptExit()
  18. {
  19.     if(holder != -1)
  20.     {
  21.         DropBall(holder);
  22.     }
  23.     DestroyObject(ball);
  24.     DestroyPickup(infoPickup);
  25.     Delete3DTextLabel(infoLabel);
  26.     return 1;
  27. }
  28. public OnFilterScriptInit()
  29. {
  30.     ball = CreateObject(2114,2290.6697, -1528.4869, 26.8750-DISTANCE_TO_GROUND,0,0,0);
  31.    
  32.     infoPickup = CreatePickup(1239, 23, 2298.2263,-1501.6254,25.3047);
  33.    
  34.     infoLabel = Create3DTextLabel(!"<< Basketball Info >>\n\n{FF5C00}Tasten:\nC{FFFFFF} - Ball ann-/auf-/wegnehmen\n{FF5C00}LSTRG{FFFFFF} - Wurf\n{FF5C00}LALT{FFFFFF} - Trick\n{FF5C00}N{FFFFFF} - Passen\n{FF5C00}ENTER{FFFFFF} - Ablegen\n{FF5C00}Leertaste{FFFFFF} - Laufen/Sprinten", -1, 2298.2263,-1501.6254,25.3047+0.75, 15.0, 0,true);
  35.    
  36.     PreloadAnimsForAllPlayer();
  37.    
  38.     SetPlayerPos(0, 2290.6697, -1528.4869, 26.8750);
  39.    
  40.     sleep(1000)<clearChat>;
  41.     for(new i; i<100; i++) SendClientMessage(0,-1,!" ");
  42.     return 1;
  43. }
  44.  
  45. public OnPlayerDisconnect(playerid, reason)
  46. {
  47.     if(playerid == holder)
  48.     {
  49.         DropBall(playerid);
  50.     }
  51.     return 1;
  52. }
  53. public OnObjectMoved(objectid)
  54. {
  55.     if(objectid != ball) return 1;
  56.     if(holder != -1 && ballTrick == -1)
  57.     {
  58.         if(GetPlayerAnimationIndex(holder) == 1133)
  59.         {
  60.             SetCurrentMovement(holder);
  61.             return 1;
  62.         }
  63.         DripBall(holder);
  64.     }
  65.     return 1;
  66. }
  67. public OnPlayerEnterVehicle(playerid, vehicleid, ispassenger)
  68. {
  69.     if(HasBall(playerid))
  70.     {
  71.         DropBall(playerid);
  72.     }
  73.     return 1;
  74. }
  75.  
  76. public OnPlayerKeyStateChange(playerid,KEY:newkeys,KEY:oldkeys)
  77. {
  78.     if(HasBall(playerid))
  79.     {
  80.         if(PRESSED(KEY_SPRINT)) // Laufen/Rennen
  81.         {
  82.             ToggleMovement(playerid);
  83.         }
  84.         else if(PRESSED(KEY_FIRE)) // Korb Wurf
  85.         {
  86.             RemovePlayerBall(playerid);
  87.  
  88.             SetTimerEx(!"@korb_wurf",250,false,!"i",playerid);
  89.  
  90.             ApplyAnimation(playerid,!"BSKTBALL",!"BBALL_Jump_Shot",4.1,false,true,true,false,0);
  91.         }
  92.         else if(PRESSED(KEY_NO)) // Pass spiel werfen
  93.         {
  94.             RemovePlayerBall(playerid);
  95.  
  96.             SetTimerEx(!"@pass_game",250,false,!"ii",playerid,1);
  97.  
  98.             ApplyAnimation(playerid,!"BSKTBALL",!"BBALL_Jump_End",4.1,false,true,true,false,0);
  99.         }
  100.         else if(PRESSED(KEY_SECONDARY_ATTACK)) // Ball ablegen
  101.         {
  102.             DropBall(playerid);
  103.         }
  104.         else if(PRESSED(KEY_WALK)) // Ball Trick
  105.         {
  106.             if(ballTrick == -1)
  107.             {
  108.                 DestroyObject(ball),ball=-1;
  109.                 ApplyAnimation(playerid,!"BSKTBALL",!"BBALL_idle2",4.1,true,true,true,true,0);
  110.                 ballTrick = SetTimerEx(!"@RotateAttachedBall",50,true,!"i",playerid);
  111.             }
  112.             else
  113.             {
  114.                 SetCurrentMovement(playerid);
  115.             }
  116.            
  117.         }
  118.     }
  119.     else
  120.     {
  121.         if(PRESSED(KEY_CROUCH)) // Ball aufheben
  122.         {
  123.             if(passWurf && lastHolder != playerid && IsPlayerInRangeOfBall(playerid, 3.5)) // Pass Annehmen
  124.             {
  125.                 ApplyAnimation(playerid,!"BSKTBALL",!"BBALL_Dnk",4.1,false,true,true,false,0);
  126.                 GivePlayerBall(playerid);
  127.                 return 1;
  128.             }
  129.  
  130.             if(holder == -1 && IsPlayerInRangeOfBall(playerid, 2.0)) // Ball aufheben
  131.             {
  132.                 PickupBall(playerid);
  133.                 return 1;
  134.             }
  135.  
  136.             if(!ballLocked && IsPlayerInRangeOfBall(playerid, 1.0)) // Ball wegnehmen
  137.             {
  138.                 RemovePlayerBall(holder), GivePlayerBall(playerid);
  139.                 return 1;
  140.             }
  141.         }
  142.         else if(PRESSED(KEY_JUMP) && korbWurf && IsPlayerInRangeOfBall(playerid, 2.5) && lastHolder != playerid) // Wurf blocken
  143.         {
  144.            BlockBall(playerid);
  145.         }
  146.     }
  147.     return 1;
  148. }
  149. /******************************************************************************/
  150. stock SetCurrentMovement(playerid)
  151. {
  152.     return (ballSpeed < 8) ? Walk(playerid) : Run(playerid);
  153. }
  154. stock ToggleMovement(playerid)
  155. {
  156.     return (ballSpeed < 8) ? Run(playerid) : Walk(playerid);
  157. }
  158. stock Walk(playerid)
  159. {
  160.     DripBall(playerid);
  161.     ballSpeed = 4.75;
  162.     ApplyAnimation(playerid,!"BSKTBALL",!"BBALL_walk",4.1,true,true,true,true,1);
  163.     return 1;
  164. }
  165. stock Run(playerid)
  166. {
  167.     DripBall(playerid);
  168.     ballSpeed = 8.0;
  169.     ApplyAnimation(playerid,!"BSKTBALL",!"BBALL_run",4.1,true,true,true,true,1);
  170.     return 1;
  171. }
  172. /******************************************************************************/
  173. stock StopBallTrick(playerid)
  174. {
  175.     if(ballTrick == -1) return 0;
  176.    
  177.     new Float:x,Float:y,Float:z;
  178.     GetPosInFrontOfPlayer(playerid, DISTANCE_IN_FRONT, x,y,z, 20.0);
  179.     RemovePlayerAttachedObject(playerid, 0);
  180.     KillTimer(ballTrick), ballTrick=-1;
  181.     ball = CreateObject(2114, x,y,z,0,0,0);
  182.     return 1;
  183. }
  184. stock DripBall(playerid)
  185. {
  186.     StopBallTrick(playerid);
  187.  
  188.     new Float:x,Float:y,Float:z;
  189.     GetPosInFrontOfPlayer(playerid, DISTANCE_IN_FRONT, x,y,z, 20.0);
  190.  
  191.     static bool:s;
  192.     switch(s)
  193.     {
  194.         case false: MoveObject(ball,x,y,z,ballSpeed);
  195.         case true: MoveObject(ball,x,y,z-DISTANCE_TO_GROUND,ballSpeed);
  196.     }
  197.     s=!s;
  198. }
  199. stock EndKorbCheck()
  200. {
  201.     StopObject(ball);
  202.     if(korbCheck != -1)
  203.     {
  204.         KillTimer(korbCheck), korbCheck = -1;
  205.     }
  206.     if(wurfTimer != -1)
  207.     {
  208.         KillTimer(wurfTimer), wurfTimer = -1;
  209.     }
  210.     if(endWurfTimer != -1)
  211.     {
  212.         KillTimer(endWurfTimer), @end_wurf();
  213.     }
  214.     return 1;
  215. }
  216. stock RemovePlayerBall(playerid)
  217. {
  218.     StopBallTrick(playerid);
  219.  
  220.     ClearAnimations(playerid);
  221.     holder = -1;
  222.    
  223.     EndKorbCheck();
  224.  
  225.     new Float:x,Float:y,Float:z;
  226.     GetPosInFrontOfPlayer(playerid, 1.0, x,y,z);
  227.     SetObjectPos(ball,x,y,z);
  228.     return 1;
  229. }
  230. stock GivePlayerBall(playerid)
  231. {
  232.     StopBallTrick(playerid);
  233.  
  234.     ballLocked = true;
  235.     StopObject(ball);
  236.     lastHolder = playerid;
  237.     holder = playerid;
  238.     SetCurrentMovement(playerid);
  239.     sleep(1000)<unlock>;
  240.     ballLocked = false;
  241.     return 1;
  242. }
  243. stock DropBall(playerid)
  244. {
  245.     StopBallTrick(playerid);
  246.     holder = -1;
  247.     new Float:x,Float:y,Float:z;
  248.     GetPosInFrontOfPlayer(playerid, DISTANCE_IN_FRONT, x,y,z);
  249.     MoveObject(ball, x,y,z-DISTANCE_TO_GROUND, 1.0);
  250.     ApplyAnimation(playerid,!"BSKTBALL",!"BBALL_pickup",4.1,false,true,true,false,0);
  251.     return 1;
  252. }
  253. stock PickupBall(playerid)
  254. {
  255.     StopBallTrick(playerid);
  256.     ballLocked = true;
  257.     ApplyAnimation(playerid,!"BSKTBALL",!"BBALL_pickup",4.1,false,true,true,false,0);
  258.     pSleep(1000)<pickup, "i", playerid>;
  259.     GivePlayerBall(playerid);
  260.     return 1;
  261. }
  262. stock BlockBall(playerid)
  263. {
  264.     StopBallTrick(playerid);
  265.     new Float:x,Float:y,Float:z;
  266.     GetPlayerPos(playerid, x,y,z);
  267.     EndKorbCheck();
  268.     MoveObject(ball,x,y,z-DISTANCE_TO_GROUND,passSpeed);
  269.     ApplyAnimation(playerid,!"BSKTBALL",!"BBALL_def_jump_shot",4.1,false,true,true,false,0);
  270.     return 1;
  271. }
  272. stock ReactToGoal(bool:goal)
  273. {
  274.     if(lastHolder == -1) return 1;
  275.     ApplyAnimation(lastHolder,!"BSKTBALL",(goal ? (!"BBALL_react_score") : (!"BBALL_react_miss")),2.5,false,true,true,false,0);
  276.     PlayerPlaySound(lastHolder, (goal) ? 182 : 1136, 0.0, 0.0, 0.0);
  277.     new playerid = lastHolder;
  278.     lastHolder = -1;
  279.     pSleep(7000)<stopSound,"i",playerid>;
  280.     PlayerPlaySound(playerid, 0, 0.0, 0.0, 0.0);
  281.     return 1;
  282. }
  283. /******************************************************************************/
  284. // Helper Functions
  285. stock IsObjectInRangeOfPoint(objectid, Float:radi, Float:x1, Float:y1, Float:z1)
  286. {
  287.     new Float:x2,Float:y2,Float:z2;
  288.     GetObjectPos(objectid, x2, y2, z2);
  289.     return VectorSize(x1-x2, y1-y2, z1-z2) < radi;
  290. }
  291. stock SendRangeMessage(Float:x,Float:y,Float:z,Float:r,const string[])
  292. {
  293.     for(new i=MAX_PLAYERS; i!=-1; i--)
  294.     {
  295.         if(IsPlayerConnected(i) && !IsPlayerNPC(i) && IsPlayerInRangeOfPoint(i,r,x,y,z))
  296.         {
  297.             SendClientMessage(i,0xFF9900AA,string);
  298.         }
  299.     }
  300.     return 1;
  301. }
  302. stock GetPosInFrontOfPlayer(playerid, Float:distance, &Float:x, &Float:y, &Float:z, Float:spin=0.0)
  303. {
  304.     new Float:a;
  305.     GetPlayerPos(playerid, x, y, z), GetPlayerFacingAngle(playerid, a);
  306.     a -= spin;
  307.     x += (distance * floatsin(-a, degrees));
  308.     y += (distance * floatcos(-a, degrees));
  309. }
  310. stock HasBall(playerid) return playerid == holder;
  311. stock IsPlayerInRangeOfBall(playerid, Float:range)
  312. {
  313.     new Float:x,Float:y,Float:z;
  314.     GetObjectPos(ball, x,y,z);
  315.     return IsPlayerInRangeOfPoint(playerid, range,x,y,z);
  316. }
  317. stock PreloadAnimsForAllPlayer()
  318. {
  319.     for(new i=MAX_PLAYERS; i!=-1; i--)
  320.     {
  321.         if(IsPlayerConnected(i) && !IsPlayerNPC(i))
  322.         {
  323.             PreloadAnim(i, !"BSKTBALL");
  324.         }
  325.     }
  326.     return 1;
  327. }
  328. /******************************************************************************/
  329. // Timer
  330. static @pass_game(i);@pass_game(i)
  331. {
  332.     passWurf = true;
  333.  
  334.     new Float:x,Float:y,Float:z,Float:a;
  335.     GetPlayerFacingAngle(i, a);
  336.     GetPosInFrontOfPlayer(i, (wurfWeite/2), x,y,z);
  337.  
  338.     new t = MoveObject(ball,x,y,z+passHeight, passSpeed);
  339.     wurfTimer = SetTimerEx(!"@wurf_phase_two", t, false, !"ff",a,z,false);
  340.     return 1;
  341. }
  342. static @korb_wurf(i);@korb_wurf(i)
  343. {
  344.     korbWurf = true;
  345.  
  346.     new Float:x,Float:y,Float:z,Float:a;
  347.     GetPlayerFacingAngle(i, a);
  348.     GetPosInFrontOfPlayer(i, (wurfWeite/2), x,y,z);
  349.  
  350.     new t = MoveObject(ball,x,y,z+wurfHeight, flugSpeed);
  351.     wurfTimer = SetTimerEx(!"@wurf_phase_two", t, false, !"ff",a,z,true);
  352.     return 1;
  353. }
  354. static @wurf_phase_two(Float:a,Float:z);@wurf_phase_two(Float:a,Float:z)
  355. {
  356.     wurfTimer = -1;
  357.  
  358.     new Float:x,Float:y,Float:z2;
  359.     GetObjectPos(ball, x,y,z2);
  360.     x += (wurfWeite/2) * floatsin(-a, degrees);
  361.     y += (wurfWeite/2) * floatcos(-a, degrees);
  362.     new t = MoveObject(ball,x,y,z-DISTANCE_TO_GROUND,korbWurf ? flugSpeed : passSpeed);
  363.     if(korbWurf)
  364.     {
  365.         korbCheck = SetTimer(!"@checkKorb",100,true);
  366.     }
  367.     endWurfTimer = SetTimer(!"@end_wurf",t,false);
  368.     return 1;
  369. }
  370. static @end_wurf();@end_wurf()
  371. {
  372.     if(korbWurf)
  373.     {
  374.         ReactToGoal(false);
  375.     }
  376.     endWurfTimer = -1;
  377.     passWurf = korbWurf = false;
  378.    
  379.     if(korbCheck != -1)
  380.     {
  381.         KillTimer(korbCheck), korbCheck = -1;
  382.     }
  383.     return 1;
  384. }
  385. static @RotateAttachedBall(playerid);@RotateAttachedBall(playerid)
  386. {
  387.     static Float:rotZ;
  388.     rotZ += 10.0;
  389.     if(rotZ > 360.0) rotZ = 0.0;
  390.  
  391.     SetPlayerAttachedObject(playerid, 0, 2114, 6, 0.32, 0.15, 0, rotZ, 90.0, 0.0, 1.0, 1.0, 1.0);
  392.     return 1;
  393. }
  394. /******************************************************************************/
  395. enum eHoopData
  396. {
  397.     Float:hA[3],   // Hoop A
  398.     Float:hB[3],   // Hoop B
  399.     Float:hStart[3]// where the ball returns to
  400. };
  401. static stock const Float:hData[][eHoopData] = {
  402.     { // Hoop 1
  403.         {2290.5068, -1514.7594, 28.8521}, // Hoop A
  404.         {2290.7322, -1541.3042, 28.6934}, // Hoop B
  405.         {2290.6697, -1528.4869, 26.8750}  // ball reset pos
  406.     },
  407.     { // Hoop 2
  408.         {2316.8618,-1514.7913,27.0}, // Hoop A
  409.         {2316.9937,-1541.0542,27.0}, // Hoop B
  410.         {2316.9741,-1527.1895,25.3438}  // ball reset pos
  411.     },
  412.     { // Hoop 3
  413.         {2514.6929,1266.4944,12.9130}, // Hoop A
  414.         {2480.1257,1266.4353,12.5100}, // Hoop B
  415.         {2496.3042,1272.1831,10.8125}  // ball reset pos
  416.     },
  417.     { // Hoop 4
  418.         {2480.1272,1266.4354,12.5126}, // Hoop A
  419.         {2480.3235,1277.4980,12.5134}, // Hoop B
  420.         {2496.3042,1272.1831,10.8125}  // ball reset pos
  421.     },
  422.     { // Hoop 5
  423.         {2055.7283,-1694.6697,16.3749}, // Hoop A
  424.         {2055.7283,-1694.6697,16.3749}, // Hoop B
  425.         {2071.1609,-1694.6697,13.5469}  // ball reset pos
  426.     }
  427. };
  428. static @checkKorb();@checkKorb()
  429. {
  430.     for (new i; i < sizeof(hData); i++)
  431.     {
  432.         if (IsObjectInRangeOfPoint(ball, 0.5, hData[i][hA][0], hData[i][hA][1], hData[i][hA][2]) || IsObjectInRangeOfPoint(ball, 0.5, hData[i][hB][0], hData[i][hB][1], hData[i][hB][2]))
  433.         {
  434.             new string[80];
  435.             GetPlayerName(lastHolder,string,MAX_PLAYER_NAME);
  436.             format(string,sizeof(string),"* Spieler %s (ID: %d) hat einen Korb geworfen!",string,lastHolder);
  437.             SendRangeMessage(hData[i][hStart][0], hData[i][hStart][1], hData[i][hStart][2], 40.0, string);
  438.             SetObjectPos(ball,hData[i][hStart][0], hData[i][hStart][1], hData[i][hStart][2]-DISTANCE_TO_GROUND);
  439.             ReactToGoal(true);
  440.             EndKorbCheck();
  441.             break;
  442.         }
  443.     }
  444.     return 1;
  445. }
Advertisement
Add Comment
Please, Sign In to add comment