Advertisement
Guest User

Rafelder

a guest
May 18th, 2008
662
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.58 KB | None | 0 0
  1. /*
  2. Checkpoint Control by Rafelder
  3. (c) by Rafelder
  4.  
  5. You're not allowed to edit the code without permission of me!
  6. */
  7.  
  8. /*
  9. native CreateCheckpoint(Float:X, Float:Y, Float:Z, Float:size = 2.0, Float:distance = 9999.9, name[MAX_NAME_CHAR] = " ", bool:enable = true)
  10. native DestroyCheckpoint(cpid)
  11. native IsValidCheckpoint(cpid)
  12.  
  13. native EnabledCheckpointForPlayer(playerid, cpid, bool:enable)      - cpid -1 = all
  14. native EnabledCheckpointForAll(cpid, bool:enable)           - cpid -1 = all
  15. native SetCheckpointPos(cpid, Float:X, Float:Y, Float:Z)        - cpid -1 = all
  16. native SetCheckpointSize(cpid, Float:size = 2.0)                    - cpid -1 = all
  17. native SetCheckpointSpawnDistance(cpid, Float:distance = 9999.9)        - cpid -1 = all
  18. native SetCheckpointName(cpid, name[] = " ")                            - cpid -1 = all
  19. native GetCheckpointPos(cpid, &Float:X, &Float:Y, &Float:Z)
  20. native GetCheckpointSize(cpid)
  21. native GetCheckpointSpawnDistance(cpid)
  22. native GetCheckpointName(cpid)
  23.  
  24. native IsCheckpointEnabledForPlayer(playerid, cpid)                     - cpid -1 = all
  25. native IsCheckpointEnabledForAll(cpid)                                  - cpid -1 = all
  26. native IsPlayerInAnyCheckpoint(playerid)                                - playerid -1 = all
  27. native IsPlayerInCheckpoint(playerid, cpid)                             - playerid -1 = all
  28. native GetPlayerCheckpoint(playerid)
  29.  
  30. native CPC_OnPlayerConnect(playerid)
  31. native CPC_OnPlayerEnterCheckpoint(playerid)
  32. native CPC_OnPlayerLeaveCheckpoint(playerid)
  33.  
  34. Callbacks:
  35.  
  36. native OnPlayerEnterCheckpointEx(playerid, cpid, name[])
  37. native OnPlayerLeaveCheckpointEx(playerid, cpid, name[])
  38. */
  39.  
  40. #define MAX_CHECKPOINTS 1024
  41. #define MAX_NAME_CHAR 64
  42.  
  43. enum CP_Info
  44. {
  45.     bool:Enabled,
  46.     Float:Xc,
  47.     Float:Yc,
  48.     Float:Zc,
  49.     Float:sizec,
  50.     Float:disc,
  51.     namec[MAX_NAME_CHAR]
  52. }
  53.  
  54. new Checkpoint[MAX_CHECKPOINTS][CP_Info];
  55.  
  56. enum PL_Info
  57. {
  58.     bool:Enabled
  59. }
  60.  
  61. new PlayerCPInfo[MAX_PLAYERS][MAX_CHECKPOINTS][PL_Info];
  62.  
  63. new LastCheckpointEnter[MAX_PLAYERS];
  64. new LastCheckpointLeave[MAX_PLAYERS];
  65. new CheckpointP[MAX_PLAYERS];
  66.  
  67. new Count[MAX_CHECKPOINTS];
  68. new CP, Timer_Enable;
  69. forward CheckpointUpdate();
  70. forward OnPlayerEnterCheckpointEx(playerid, cpid, name[]);
  71. forward OnPlayerLeaveCheckpointEx(playerid, cpid, name[]);
  72.  
  73. public CheckpointUpdate()
  74. {
  75.     new Checkp, Checkp2, Float:dist;
  76.     for(new playerid=0; playerid<1; playerid++)
  77.     {
  78.         if(!IsPlayerInAnyCheckpoint(playerid))
  79.         {
  80.         LastCheckpointEnter[playerid] = -1;
  81.         LastCheckpointLeave[playerid] = -1;
  82.         }
  83.         DisablePlayerCheckpoint(playerid);
  84.         if(GetEnabledCheckpoints(playerid) != 0)
  85.         {
  86.             Checkp = GetNearestCheckpoint(playerid, dist);
  87.             if(dist <= Checkpoint[Checkp][disc])
  88.             {
  89.                 SetPlayerCheckpoint(playerid, Checkpoint[Checkp][Xc], Checkpoint[Checkp][Yc], Checkpoint[Checkp][Zc], Checkpoint[Checkp][sizec]);
  90.                 CheckpointP[playerid] = Checkp;
  91.             }
  92.             else
  93.             {
  94.                 Checkp2 = GetNearestCheckpoint(playerid, dist, Checkp);
  95.                 if(dist <= Checkpoint[Checkp2][disc])
  96.                 {
  97.                     SetPlayerCheckpoint(playerid, Checkpoint[Checkp2][Xc], Checkpoint[Checkp2][Yc], Checkpoint[Checkp2][Zc], Checkpoint[Checkp2][sizec]);
  98.                     CheckpointP[playerid] = Checkp2;
  99.                 }
  100.             }
  101.         }
  102.     }
  103.     return 1;
  104. }
  105.  
  106. stock CreateCheckpoint(Float:X, Float:Y, Float:Z, Float:size = 2.0, Float:distance = 9999.9, name[MAX_NAME_CHAR] = " ", bool:enable = true)
  107. {
  108.     CP = GetFirstArraySize(Count, sizeof(Count), 0);
  109.     if(CP == MAX_CHECKPOINTS) return 0;
  110.     if(Timer_Enable == 0)
  111.     {
  112.         SetTimer("CheckpointUpdate", 200, 1);
  113.         Timer_Enable = 1;
  114.     }
  115.     Count[CP] = 1;
  116.     Checkpoint[CP][Enabled] = true;
  117.     Checkpoint[CP][Xc] = X;
  118.     Checkpoint[CP][Yc] = Y;
  119.     Checkpoint[CP][Zc] = Z;
  120.     Checkpoint[CP][sizec] = size;
  121.     Checkpoint[CP][disc] = distance;
  122.     format(Checkpoint[CP][namec], MAX_NAME_CHAR, "%s", name);
  123.     EnableCheckpointForAll(CP, enable);
  124.     return CP;
  125. }
  126.  
  127. stock DestroyCheckpoint(cpid)
  128. {
  129.     if(!IsValidCheckpoint(cpid)) return 0;
  130.     Count[cpid] = 0;
  131.     Checkpoint[CP][Enabled] = false;
  132.     Checkpoint[CP][Xc] = 0.0;
  133.     Checkpoint[CP][Yc] = 0.0;
  134.     Checkpoint[CP][Zc] = 0.0;
  135.     Checkpoint[CP][sizec] = 0.0;
  136.     Checkpoint[CP][disc] = 0.0;
  137.     format(Checkpoint[CP][namec], MAX_NAME_CHAR, "%s", " ");
  138.     Count[cpid] = 0;
  139.     return 1;
  140. }
  141.  
  142. stock IsValidCheckpoint(cpid)
  143. {
  144.     return Checkpoint[cpid][Enabled];
  145. }
  146.  
  147. stock SetCheckpointPos(cpid, Float:X, Float:Y, Float:Z)
  148. {
  149.     if(cpid != -1)
  150.     {
  151.         if(!IsValidCheckpoint(cpid)) return 0;
  152.         Checkpoint[cpid][Xc] = X;
  153.         Checkpoint[cpid][Yc] = Y;
  154.         Checkpoint[cpid][Zc] = Z;
  155.     }
  156.     else
  157.     {
  158.         for(new i=0; i<MAX_CHECKPOINTS; i++)
  159.         {
  160.             Checkpoint[i][Xc] = X;
  161.             Checkpoint[i][Yc] = Y;
  162.             Checkpoint[i][Zc] = Z;
  163.         }
  164.     }
  165.     return 1;
  166. }
  167.  
  168. stock GetCheckpointPos(cpid, &Float:X, &Float:Y, &Float:Z)
  169. {
  170.     X = Checkpoint[cpid][Xc];
  171.     Y = Checkpoint[cpid][Yc];
  172.     Z = Checkpoint[cpid][Zc];
  173. }
  174.  
  175. stock SetCheckpointSize(cpid, Float:size = 2.0)
  176. {
  177.     if(cpid != -1)
  178.     {
  179.         if(!IsValidCheckpoint(cpid)) return 0;
  180.         Checkpoint[cpid][sizec] = size;
  181.     }
  182.     else
  183.     {
  184.         for(new i=0; i<MAX_CHECKPOINTS; i++)
  185.         {
  186.             Checkpoint[i][sizec] = size;
  187.         }
  188.     }
  189.     return 1;
  190. }
  191.  
  192. stock GetCheckpointSize(cpid)
  193. {
  194.     return Checkpoint[cpid][sizec];
  195. }
  196.  
  197. stock SetCheckpointSpawnDistance(cpid, Float:distance = 9999.9)
  198. {
  199.     if(cpid != -1)
  200.     {
  201.         if(!IsValidCheckpoint(cpid)) return 0;
  202.         Checkpoint[cpid][disc] = distance;
  203.     }
  204.     else
  205.     {
  206.         for(new i=0; i<MAX_CHECKPOINTS; i++)
  207.         {
  208.             Checkpoint[i][disc] = distance;
  209.         }
  210.     }
  211.     return 1;
  212. }
  213.  
  214. stock GetCheckpointSpawnDistance(cpid)
  215. {
  216.     return Checkpoint[cpid][disc];
  217. }
  218.  
  219. stock SetCheckpointName(cpid, name[] = " ")
  220. {
  221.     if(cpid != -1)
  222.     {
  223.         if(!IsValidCheckpoint(cpid)) return 0;
  224.         format(Checkpoint[cpid][namec], MAX_NAME_CHAR, "%s", name);
  225.     }
  226.     else
  227.     {
  228.         for(new i=0; i<MAX_CHECKPOINTS; i++)
  229.         {
  230.             format(Checkpoint[i][namec], MAX_NAME_CHAR, "%s", name);
  231.         }
  232.     }
  233.     return 1;
  234. }
  235.  
  236. stock GetCheckpointName(cpid)
  237. {
  238.     return Checkpoint[cpid][name];
  239. }
  240.  
  241. stock GetPlayerCheckpoint(playerid)
  242. {
  243.     if(IsPlayerInCheckpoint(playerid)) return CheckpointP[playerid];
  244.     return -1;
  245. }
  246.  
  247. stock IsPlayerInCheckpointEx(playerid, cpid)
  248. {
  249.     if(!IsValidCheckpoint(cpid)) return 0;
  250.     if(playerid != -1)
  251.     {
  252.         if(IsPlayerInCheckpoint(playerid) && CheckpointP[playerid] == cpid) return 1;
  253.         return 0;
  254.     }
  255.     else
  256.     {
  257.         for(new i=0; i<MAX_PLAYERS; i++)
  258.         {
  259.             if(!IsPlayerInCheckpoint(i) || CheckpointP[i] != cpid) return 0;
  260.         }
  261.         return 1;
  262.     }
  263. }
  264.  
  265. stock IsPlayerInAnyCheckpoint(playerid)
  266. {
  267.     if(playerid != -1)
  268.     {
  269.         if(IsPlayerInCheckpoint(playerid)) return 1;
  270.         return 0;
  271.     }
  272.     else
  273.     {
  274.         for(new i=0; i<MAX_PLAYERS; i++)
  275.         {
  276.             if(!IsPlayerInCheckpoint(i)) return 0;
  277.         }
  278.         return 1;
  279.     }
  280. }
  281.  
  282. stock EnableCheckpointForPlayer(playerid, cpid, bool:enable)
  283. {
  284.     if(cpid != -1)
  285.     {
  286.         if(!IsValidCheckpoint(cpid)) return 0;
  287.         PlayerCPInfo[playerid][cpid][Enabled] = enable;
  288.     }
  289.     else
  290.     {
  291.         for(new i=0; i<MAX_CHECKPOINTS; i++)
  292.         {
  293.             PlayerCPInfo[playerid][i][Enabled] = enable;
  294.         }
  295.     }
  296.     return 1;
  297. }
  298.  
  299. stock EnableCheckpointForAll(cpid, bool:enable)
  300. {
  301.     if(cpid != -1)
  302.     {
  303.         if(!IsValidCheckpoint(cpid)) return 0;
  304.         for(new i=0; i<MAX_PLAYERS; i++)
  305.         {
  306.             PlayerCPInfo[i][cpid][Enabled] = enable;
  307.         }
  308.     }
  309.     else
  310.     {
  311.         for(new i=0; i<MAX_PLAYERS; i++)
  312.         {
  313.             for(new a=0; a<MAX_CHECKPOINTS; a++)
  314.             {
  315.                 PlayerCPInfo[i][a][Enabled] = enable;
  316.             }
  317.         }
  318.     }
  319.     return 1;
  320. }
  321.  
  322. stock IsCheckpointEnabledForPlayer(playerid, cpid)
  323. {
  324.     if(cpid != -1)
  325.     {
  326.         if(!IsValidCheckpoint(cpid)) return 0;
  327.         return PlayerCPInfo[playerid][cpid][Enabled];
  328.     }
  329.     else
  330.     {
  331.         if(GetEnabledCheckpoints(playerid) == GetValidCheckpoints()) return 1;
  332.     }
  333.     return 0;
  334. }
  335.  
  336. stock IsCheckpointEnabledForAll(cpid)
  337. {
  338.     if(cpid != -1)
  339.     {
  340.         if(!IsValidCheckpoint(cpid)) return 0;
  341.         for(new i=0; i<MAX_PLAYERS(); i++)
  342.         {
  343.             if(PlayerCPInfo[i][cpid][Enabled] == false) return 0;
  344.         }
  345.         return 1;
  346.     }
  347.     else
  348.     {
  349.         if(GetEnabledCheckpoints(-1) == (GetValidCheckpoints()*GetOnlinePlayers())) return 1;
  350.     }
  351.     return 0;
  352. }
  353.  
  354. stock CPC_OnPlayerEnterCheckpoint(playerid)
  355. {
  356.     if(CheckpointP[playerid] != LastCheckpointEnter[playerid])
  357.     {
  358.         LastCheckpointEnter[playerid] = CheckpointP[playerid];
  359.         OnPlayerEnterCheckpointEx(playerid, CheckpointP[playerid], Checkpoint[CheckpointP[playerid]][namec]);
  360.     }
  361. }
  362.  
  363. stock CPC_OnPlayerLeaveCheckpoint(playerid)
  364. {
  365.     if(CheckpointP[playerid] != LastCheckpointLeave[playerid])
  366.     {
  367.         LastCheckpointLeave[playerid] = CheckpointP[playerid];
  368.         OnPlayerLeaveCheckpointEx(playerid, CheckpointP[playerid], Checkpoint[CheckpointP[playerid]][namec]);
  369.     }
  370. }
  371.  
  372. stock CPC_OnPlayerConnect(playerid)
  373. {
  374.     LastCheckpointEnter[playerid] = -1;
  375.     LastCheckpointLeave[playerid] = -1;
  376.     EnableCheckpointForPlayer(playerid, -1, true);
  377.     CheckpointP[playerid] = -1;
  378. }
  379.  
  380. stock GetOnlinePlayers()
  381. {
  382.     new Counter;
  383.     for(new i=0; i<MAX_PLAYERS; i++)
  384.     {
  385.         if(IsPlayerConnected(i)) Counter++;
  386.     }
  387.     return Counter;
  388. }
  389.  
  390. stock GetValidCheckpoints()
  391. {
  392.     new Counter = 0;
  393.     for(new i=0; i<MAX_CHECKPOINTS; i++)
  394.     {
  395.         if(IsValidCheckpoint(i)) Counter++;
  396.     }
  397.     return Counter;
  398. }
  399.  
  400. stock GetEnabledCheckpoints(playerid)
  401. {
  402.     if(playerid != -1)
  403.     {
  404.         new Counter = 0;
  405.         for(new i=0; i<MAX_CHECKPOINTS; i++)
  406.         {
  407.             if(IsValidCheckpoint(i))
  408.             {
  409.                 if(PlayerCPInfo[playerid][i][Enabled] == true) Counter++;
  410.             }
  411.         }
  412.         return Counter;
  413.     }
  414.     else
  415.     {
  416.         new Counter;
  417.         for(new i=0; i<GetOnlinePlayers(); i++)
  418.         {
  419.             for(new a=0; a<MAX_CHECKPOINTS; a++)
  420.             {
  421.                 if(IsValidCheckpoint(i))
  422.                 {
  423.                     if(PlayerCPInfo[i][a][Enabled] == true) Counter++;
  424.                 }
  425.             }
  426.         }
  427.         return Counter;
  428.     }
  429. }
  430.  
  431. stock Float:GetDistanceToCheckpoint(playerid, cpid)
  432. {
  433.     new Float:x1,Float:y1,Float:z1,Float:x2,Float:y2,Float:z2;
  434.     GetPlayerPos(playerid, x1, y1, z1);
  435.     GetCheckpointPos(cpid, x2, y2, z2);
  436.     return floatsqroot(floatpower(floatabs(floatsub(x2,x1)),2)+floatpower(floatabs(floatsub(y2,y1)),2)+floatpower(floatabs(floatsub(z2,z1)),2));
  437. }
  438.  
  439. stock GetNearestCheckpoint(playerid, &Float:distance, notallowed = -1)
  440. {
  441.     new Float:dis = 9999999.9, Float:distancec, nearest;
  442.     for(new i=0; i<MAX_CHECKPOINTS; i++)
  443.     {
  444.         if(IsValidCheckpoint(i))
  445.         {
  446.             if(IsCheckpointEnabledForPlayer(playerid, i))
  447.             {
  448.                 if(i != notallowed)
  449.                 {
  450.                     distancec = GetDistanceToCheckpoint(playerid, i);
  451.                     if(distancec < dis) {dis = distancec, nearest = i;}
  452.                 }
  453.             }
  454.         }
  455.     }
  456.     distance = dis;
  457.     return nearest;
  458. }
  459.  
  460. stock GetFirstArraySize(array[], arraysize, size)
  461. {
  462.     for(new i=0; i<arraysize; i++)
  463.     {
  464.         if(array[i] == size) return i;
  465.     }
  466.     return -1;
  467. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement