Guest User

mansonh

a guest
Jan 7th, 2010
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 11.32 KB | None | 0 0
  1. /*
  2.     Hidden Checkpoints System
  3.     By: Mansonh
  4.     Dec 2009
  5. */
  6. #if defined _HiddenCPS_Included
  7.     #endinput
  8. #endif
  9. #define _HiddenCPS_Included
  10.  
  11. #define middleof(%1,%2) ((%1+%2)/2.0)
  12. #define MAX_HIDDEN_CPS (100+1)
  13. #define ENTER 0
  14. #define STOP 1
  15.  
  16. //CALLBACKS when player enters, exists, or stops, moves in a hidden cp
  17. forward OnPlayerEnterHiddenCheckPoint(playerid, hcpid);
  18. forward OnPlayerExitHiddenCheckPoint(playerid, hcpid);
  19. forward OnPlayerStopInHiddenCheckPoint(playerid, hcpid);
  20. forward OnPlayerMoveInHiddenCheckPoint(playerid, hcpid);
  21.  
  22. enum {
  23.     INVALID_HCP = 0,
  24.     SQUARE_HCP,
  25.     CUBE_HCP,
  26.     CIRCLE_HCP,
  27.     CYLINDER_HCP,
  28.     SPHERE_HCP
  29. }
  30. enum HCP_DATA{
  31.     hcp_type,
  32.     Float:hcp_X,
  33.     Float:hcp_Y,
  34.     Float:hcp_Z,
  35.     Float:hcp_X2_WR,
  36.     Float:hcp_Y2_L,
  37.     Float:hcp_Z2,
  38.     bool:multistop
  39. }
  40. new hiddenCPS[MAX_HIDDEN_CPS][HCP_DATA];
  41. new playerhcp[MAX_PLAYERS][2];
  42. new playerIgnoreHiddenCPS[MAX_PLAYERS][MAX_HIDDEN_CPS];
  43. new hcpsT = 1;
  44.  
  45. //CONSTRUCTORS
  46. stock CreateHiddenCPSquare(Float:X1, Float:Y1, Float:X2, Float:Y2, bool:mstop = false){
  47. /* The coordinates are set this way by force, if other corners are chosen it resets them
  48.           _________(X2,Y2)
  49.         /   x>    /
  50. (X1,Y1)/_________/y^      
  51. */
  52.     if(X1==X2 || Y1==Y2) return -1;
  53.     new hcpSlot;
  54.     hcpSlot = GetAvailableHCPSlot();
  55.     if(hcpSlot == -1) return -1;
  56.     hiddenCPS[hcpSlot][hcp_type] = SQUARE_HCP;
  57.     if(X1 < X2){hiddenCPS[hcpSlot][hcp_X] = X1; hiddenCPS[hcpSlot][hcp_X2_WR] = X2;}
  58.      else{hiddenCPS[hcpSlot][hcp_X] = X2; hiddenCPS[hcpSlot][hcp_X2_WR] = X1;}
  59.     if(Y1 < Y2){hiddenCPS[hcpSlot][hcp_Y] = Y1; hiddenCPS[hcpSlot][hcp_Y2_L] = Y2;}
  60.      else{hiddenCPS[hcpSlot][hcp_Y] = Y2; hiddenCPS[hcpSlot][hcp_Y2_L] = Y1;}
  61.     return hcpSlot;
  62. }
  63. stock CreateHiddenCPCube(Float:X1, Float:Y1, Float:Z1, Float:X2, Float:Y2, Float:Z2, bool:mstop = false){
  64. /* The coordinates are set this way by force, if other corners are chosen it resets them
  65.               _________(X2,Y2,Z2)
  66.             /|        /|
  67.            /________ / |z^
  68.           |  |_ _ _ |_ |
  69.           | /  x>   | /
  70. (X1,Y1,Z1)|/________|/y^
  71. */
  72.     if(X1==X2 || Y1==Y2 || Z1==Z2) return -1;
  73.     new hcpSlot;
  74.     hcpSlot = GetAvailableHCPSlot();
  75.     if(hcpSlot == -1) return -1;
  76.     hiddenCPS[hcpSlot][hcp_type] = CUBE_HCP;
  77.     if(X1 < X2){hiddenCPS[hcpSlot][hcp_X] = X1; hiddenCPS[hcpSlot][hcp_X2_WR] = X2;}
  78.      else{hiddenCPS[hcpSlot][hcp_X] = X2; hiddenCPS[hcpSlot][hcp_X2_WR] = X1;}
  79.     if(Y1 < Y2){hiddenCPS[hcpSlot][hcp_Y] = Y1; hiddenCPS[hcpSlot][hcp_Y2_L] = Y2;}
  80.      else{hiddenCPS[hcpSlot][hcp_Y] = Y2; hiddenCPS[hcpSlot][hcp_Y2_L] = Y1;}
  81.     if(Z1 < Z2){hiddenCPS[hcpSlot][hcp_Z] = Z1; hiddenCPS[hcpSlot][hcp_Z2] = Z2;}
  82.      else{hiddenCPS[hcpSlot][hcp_Z] = Z2; hiddenCPS[hcpSlot][hcp_Z2] = Z1;}
  83.     hiddenCPS[hcpSlot][multistop] = mstop;
  84.     return hcpSlot;
  85. }
  86. stock CreateHiddenCPCircle(Float:X, Float:Y, Float:R, bool:mstop = false){
  87. /*         _____
  88.          /       \
  89. (X1,Y1)-|--->+(R)-|
  90.          \ _____ /
  91. */
  92.     if(R==0.0) return -1;
  93.     new hcpSlot;
  94.     hcpSlot = GetAvailableHCPSlot();
  95.     if(hcpSlot == -1) return -1;
  96.     hiddenCPS[hcpSlot][hcp_type] = CIRCLE_HCP;
  97.     hiddenCPS[hcpSlot][hcp_X] = X1;
  98.     hiddenCPS[hcpSlot][hcp_Y] = Y1;
  99.     hiddenCPS[hcpSlot][hcp_X2_WR] = R;
  100.     return hcpSlot;
  101. }
  102. stock CreateHiddenCPCylinder(Float:X, Float:Y, Float:Z, Float:R, Float:H, bool:mstop = false){
  103. /*            _____
  104.             /       \  _
  105.            |    -(R)-|  \
  106.            |\_______/|   |
  107.            |  _____  |    >(H) z^
  108.            |/   x>  \|   |
  109. (X1,Y1,Z1)-|--->+ y^ | _/
  110.             \_______/
  111. */
  112.     if(R==0.0 || H==0.0) return -1;
  113.     new hcpSlot;
  114.     hcpSlot = GetAvailableHCPSlot();
  115.     if(hcpSlot == -1) return -1;
  116.     hiddenCPS[hcpSlot][hcp_type] = CYLINDER_HCP;
  117.     hiddenCPS[hcpSlot][hcp_X] = X1;
  118.     hiddenCPS[hcpSlot][hcp_Y] = Y1;
  119.     hiddenCPS[hcpSlot][hcp_Z] = Z1;
  120.     hiddenCPS[hcpSlot][hcp_X2_WR] = R;
  121.     hiddenCPS[hcpSlot][hcp_Z2] = Z1+H;
  122.     return hcpSlot;
  123. }
  124. stock CreateHiddenCPSphere(Float:X, Float:Y, Float:Z, Float:R, bool:mstop = false){
  125. /* A Sphere is the same as a circle its just how its used
  126.               .-----.
  127.             /    z^   \
  128. (X1,Y1,Z1)-|---->*.(R) |
  129.             \ x>  y^`./
  130.              `'-----'
  131. */
  132.     if(R==0.0) return -1;
  133.     new hcpSlot;
  134.     hcpSlot = GetAvailableHCPSlot();
  135.     if(hcpSlot == -1) return -1;
  136.     hiddenCPS[hcpSlot][hcp_type] = SPHERE_HCP;
  137.     hiddenCPS[hcpSlot][hcp_X] = X1;
  138.     hiddenCPS[hcpSlot][hcp_Y] = Y1;
  139.     hiddenCPS[hcpSlot][hcp_Z] = Z1;
  140.     hiddenCPS[hcpSlot][hpc_X2_R] = R;
  141.     return hcpSlot;
  142. }
  143. //DESTRUCTOR
  144. stock DestroyHiddenCheckPoint(hcpid){
  145.     hiddenCPS[hcpid][hcp_type] = INVALID_HCP;
  146.     while(hiddenCPS[hcpid][hcpsT-1] == INVALID_HCP) hcpsT--;
  147. }
  148. //HELPER FUNCTIONS
  149. stock IsValidHiddenCP(hcpid){
  150.     return (hiddenCPS[hcpid][hcp_type] != INVALID_HCP);
  151. }
  152. stock GetAvailableHCPSlot(){
  153.     new s;
  154.     for(s=1; s<hcpsT; s++)
  155.     {
  156.         if(hiddenCPS[s][hcp_type] == INVALID_HCP)
  157.         {
  158.             return s;
  159.         }
  160.     }
  161.     if(hcpsT == MAX_HIDDEN_CPS) return -1;
  162.     hcpsT++;
  163.     return s;
  164. }
  165. stock bool:PlayerStopped(playerid){
  166.     new Float:Xv, Float:Yv, Float:Zv;
  167.     new vehicleid = GetPlayerVehicleID(playerid);
  168.     if(vehicleid !=0)
  169.     {
  170.         GetVehicleVelocity(vehicleid, Xv, Yv, Zv);
  171.         return (floatsqroot(floatadd(floatadd(floatpower(Xv, 2), floatpower(Yv, 2)),  floatpower(Zv, 2))) == 0.0);
  172.     }else{
  173.         GetPlayerVelocity(playerid, Xv, Yv, Zv);
  174.         return (floatadd(floatadd(floatpower(Xv, 2), floatpower(Yv, 2)),  floatpower(Zv, 2)) == 0.0);
  175.     }
  176. }
  177. //PLAYER HCPS
  178. stock IsPlayerInHiddenCP(playerid, hcpid){
  179.     if(!IsValidHiddenCP(hcpid) || playerIgnoreHiddenCPS[playerid][INVALID_HCP] != 0 || playerIgnoreHiddenCPS[playerid][hcpid] != 0) return 0;
  180.     new Float:X, Float:Y, Float:Z;
  181.     GetPlayerPos(playerid, X, Y, Z);
  182.     switch (hiddenCPS[hcpid][hcp_type])
  183.     {
  184.         case SQUARE_HCP:
  185.         {
  186.             return (X > hiddenCPS[hcpid][hcp_X] && X < hiddenCPS[hcpid][hcp_X2_WR] && Y > hiddenCPS[hcpid][hcp_Y] && Y < hiddenCPS[hcpid][hcp_Y2_L]);
  187.         }
  188.         case CUBE_HCP:
  189.         {
  190.             return (X > hiddenCPS[hcpid][hcp_X] && X < hiddenCPS[hcpid][hcp_X2_WR] && Y > hiddenCPS[hcpid][hcp_Y] && Y < hiddenCPS[hcpid][hcp_Y2_L] && Z > hiddenCPS[hcpid][hcp_Z] && Z < hiddenCPS[hcpid][hcp_Z2]);
  191.         }
  192.         case CIRCLE_HCP:
  193.         {
  194.             return (IsPlayerInRangeOfPoint(playerid, hiddenCPS[hcpid][hcp_X2_WR], hiddenCPS[hcpid][hcp_X], hiddenCPS[hcpid][hcp_Y], Z));
  195.         }
  196.         case CYLINDER_HCP:
  197.         {
  198.             return (Z > hiddenCPS[hcpid][hcp_Z] && Z < hiddenCPS[hcpid][hcp_Z2] && IsPlayerInRangeOfPoint(playerid, hiddenCPS[hcpid][hcp_X2_WR], hiddenCPS[hcpid][hcp_X], hiddenCPS[hcpid][hcp_Y], Z));
  199.         }
  200.         case SPHERE_HCP:
  201.         {
  202.             return (IsPlayerInRangeOfPoint(playerid, hiddenCPS[hcpid][hcp_X2_WR], hiddenCPS[hcpid][hcp_X], hiddenCPS[hcpid][hcp_Y], Z));
  203.         }
  204.     }
  205.     return 0;
  206. }
  207. stock IsPlayerInAnyHiddenCP(playerid){
  208.     if (IsValidHiddenCP(playerhcp[playerid][ENTER]) && playerIgnoreHiddenCPS[playerid][INVALID_HCP] == 0 && playerIgnoreHiddenCPS[playerid][playerhcp[playerid][ENTER]] != 0) return playerhcp[playerid][ENTER];
  209.     return 0;
  210. }
  211. stock IsAnyPlayerInHiddenCP(hcpid){
  212.     for(new playerid=0; playerid<MAX_PLAYERS; playerid++)
  213.     {
  214.         if(IsPlayerInHiddenCP(playerid, hcpid) != 0) return 1;
  215.     }
  216.     return 0;
  217. }
  218. //IGNORE FLAGS
  219. //causese players to ignore hcps, 0 don't ignore -1 always ignore, 1,2,3,+ ignore once,twice,thrice etc
  220. stock PlayerIngoreHiddenCP(playerid, hcpid, ignorerate=-1){
  221.     playerIgnoreHiddenCPS[playerid][hcpid] = ignorerate;
  222. }
  223. stock PlayerIgnoreAllHiddenCPS(playerid, ignorerate=-1){
  224.     playerIgnoreHiddenCPS[playerid][INVALID_HCP] = ignorerate; //ignore all
  225. }
  226. stock PlayerIgnoreAllButHiddenCP(playerid, hcpid, ignorerate=-1){
  227.     for(new h=1; h < hcpsT; h++)
  228.     {
  229.         if(h == hcpid) continue;
  230.         playerIgnoreHiddenCPS[playerid][h] = ignorerate;
  231.     }
  232.     playerIgnoreHiddenCPS[playerid][INVALID_HCP] = 0; //don't ignore all
  233. }
  234. stock SetHiddenCPMultiStop(hcpid, bool:mstop=true){
  235.     hiddenCPS[hcpid][multistop] = mstop;
  236. }
  237. stock AllPlayersIgnoreHiddenCP(hcpid, ignorerate=-1){
  238.     for(new playerid=0; playerid < MAX_PLAYERS; playerid++)
  239.     {  
  240.         PlayerIgnoreHiddenCP(playerid, hcpid, ignorerate);
  241.     }
  242. }
  243. stock DisableHiddenCP(hcpid){  
  244.     AllPlayersIgnoreHiddenCP(hcpid);
  245. }
  246. //GET/SET
  247. stock GetCentreofHiddenCP(hcpid, &Float:X, &Float:Y, &Float:Z){
  248.     switch (hiddenCPS[hcpid][hcp_type])
  249.     {      
  250.         case INVALID_HCP: return 0;
  251.         case SQUARE_HCP:
  252.         {
  253.             X = middleof(hiddenCPS[hcpid][hcp_X], hiddenCPS[hcpid][hcp_X2_WR]);
  254.             Y = middleof(hiddenCPS[hcpid][hcp_Y], hiddenCPS[hcpid][hcp_Y2_L]);
  255.             Z = 0.0;
  256.         }
  257.         case CUBE_HCP:
  258.         {
  259.             X = middleof(hiddenCPS[hcpid][hcp_X], hiddenCPS[hcpid][hcp_X2_WR]);
  260.             Y = middleof(hiddenCPS[hcpid][hcp_Y], hiddenCPS[hcpid][hcp_Y2_L]);
  261.             Z = middleof(hiddenCPS[hcpid][hcp_Z], hiddenCPS[hcpid][hcp_Z2]);
  262.         }
  263.         case CIRCLE_HCP:
  264.         {
  265.             X = hcp_X,
  266.             Y = hcp_Y,
  267.             Z = 0.0;
  268.         }
  269.         case CYLINDER_HCP:
  270.         {
  271.             X = hcp_X,
  272.             Y = hcp_Y,
  273.             Z = middleof(hiddenCPS[hcpid][hcp_Z], hiddenCPS[hcpid][hcp_Z2]);
  274.         }
  275.         case SPHERE_HCP:
  276.         {
  277.             X = hiddenCPS[hcpid][hcp_X];
  278.             Y = hiddenCPS[hcpid][hcp_Y];
  279.             Z = hiddenCPS[hcpid][hcp_Z];
  280.         }
  281.     }
  282.     return 1;
  283. }
  284.  
  285. //TIMER
  286. forward HiddenCPTimer();
  287. public HiddenCPTimer(){
  288.     //SendClientMessageToAll(0xFF0000AA, "HiddenCPTimerTest");
  289.     new playerhcpid = INVALID_HCP, bool:callback, bool:isStopped;
  290.     for(new playerid=0; playerid<MAX_PLAYERS; playerid++)
  291.     {
  292.         if(!IsPlayerConnected(playerid)) continue;
  293.         new Float:X, Float:Y, Float:Z;
  294.         GetPlayerPos(playerid, X, Y, Z);
  295.         playerhcpid = playerhcp[playerid][ENTER];
  296.         if(playerhcpid != INVALID_HCP)
  297.         {
  298.             callback = (playerIgnoreHiddenCPS[playerid][INVALID_HCP] == 0 && playerIgnoreHiddenCPS[playerid][playerhcpid] == 0);
  299.             printf("current callback %d", callback);
  300.             if(IsPlayerInHiddenCP(playerid, playerhcpid))
  301.             {
  302.                 isStopped = IsPlayerStopped(playerid);
  303.                 if(playerhcp[playerid][STOP] == INVALID_HCP && isStopped)
  304.                 {
  305.                     playerhcp[playerid][STOP] = playerhcpid;
  306.                     if(callback) OnPlayerStopInHiddenCheckPoint(playerid, playerhcpid);
  307.                     SendClientMessage(playerid, 0xFF0000AA, "You have stopped in checkpoint");
  308.                 }else if(hiddenCPS[playerhcpid][multistop] && playerhcp[playerid][STOP] != INVALID_HCP && !isStopped)
  309.                 {
  310.                     playerhcp[playerid][STOP] = INVALID_HCP;
  311.                     SendClientMessage(playerid, 0xFF0000AA, "You have moved again in checkpoint ");
  312.                     if(callback) OnPlayerMoveInHiddenCheckPoint(playerid, playerhcpid);
  313.                 }
  314.                 return 1;//Player is still in cp
  315.             }else{
  316.                 if(callback) OnPlayerExitHiddenCheckPoint(playerid, playerhcpid);
  317.                 SendClientMessage(playerid, 0xFF0000AA, "You have left checkpoint");
  318.                 if(playerIgnoreHiddenCPS[playerid][INVALID_HCP] > 0) playerIgnoreHiddenCPS[playerid][INVALID_HCP]--;
  319.                 if(playerIgnoreHiddenCPS[playerid][playerhcpid] > 0) playerIgnoreHiddenCPS[playerid][playerhcpid]--;
  320.                 playerhcp[playerid][ENTER] = INVALID_HCP;
  321.                 playerhcp[playerid][STOP] = INVALID_HCP;
  322.             }
  323.             continue;
  324.         }
  325.         for(new hcpid=1; hcpid<hcpsT; hcpid++)
  326.         {
  327.             if(IsPlayerInHiddenCP(playerid,hcpid))
  328.             {
  329.                 callback = (playerIgnoreHiddenCPS[playerid][INVALID_HCP] == 0 && playerIgnoreHiddenCPS[playerid][hcpid] == 0);
  330.                 printf("current callback %d", callback);
  331.                 playerhcp[playerid][ENTER] = hcpid;
  332.                 if(callback) OnPlayerEnterHiddenCheckPoint(playerid, hcpid);
  333.                 SendClientMessage(playerid, 0xFF0000AA, "You have entered checkpoint hcpid");
  334.                 if(IsPlayerStopped(playerid))
  335.                 {
  336.                     playerhcp[playerid][STOP] = hcpid;
  337.                     if(callback) OnPlayerStopInHiddenCheckPoint(playerid, hcpid);
  338.                     SendClientMessage(playerid, 0xFF0000AA, "You have stopped in checkpoint hcpid");
  339.                 }
  340.             }
  341.         }
  342.     }
  343.     return 1;
  344. }
Advertisement
Add Comment
Please, Sign In to add comment