Advertisement
gamer931215

gArea 1.1b

Jun 3rd, 2011
542
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 5.63 KB | None | 0 0
  1. /*
  2.     gArea by Gamer931215 V1.1b
  3.     Credits to Donya for the "OnPlayerLeaveArea idea"
  4.     Free to use (do NOT sell/mirror/clame it as your own/merge it with any other script)
  5.  
  6.     Native defines (for autofill functionality in pawno using my functions)
  7.         native CreateArea(Float:x,Float:y,Float:range)
  8.         native CreateAreaEx(Float:minx,Float:miny,Float:maxx,Float:maxy)
  9.         native OnPlayerEnterArea(playerid,areaid)
  10.         native ToggleArea(areaid,bool:toggle)
  11. */
  12.  
  13. /*===============================================
  14. |             Settings/Initialising             |
  15. ================================================*/
  16.  
  17. #if !defined MAX_AREAS
  18.     #define MAX_AREAS           10
  19. #endif
  20. #if !defined TIMER_TICKRATE
  21.     #define TIMER_TICKRATE      1000
  22. #endif
  23.  
  24. #if defined FILTERSCRIPT
  25.     public OnFilterScriptInit()
  26.     {
  27.         SetTimer("scan_players",TIMER_TICKRATE,true);
  28.         return CallLocalFunction("gArea_OnFilterScriptInit","");
  29.     }
  30.     #if defined _ALS_OnFilterScriptInit
  31.         #undef OnFilterScriptInit
  32.     #else
  33.         #define _ALS_OnFilterScriptInit
  34.     #endif
  35.     #define OnFilterScriptInit gArea_OnFilterScriptInit
  36.     forward gArea_OnFilterScriptInit();
  37. #else
  38.     public OnGameModeInit()
  39.     {
  40.         SetTimer("scan_players",TIMER_TICKRATE,true);
  41.         return CallLocalFunction("gArea_OnGameModeInit","");
  42.     }
  43.     #if defined _ALS_OnGameModeInit
  44.         #undef OnGameModeInit
  45.     #else
  46.         #define _ALS_OnGameModeInit
  47.     #endif
  48.     #define OnGameModeInit gArea_OnGameModeInit
  49.     forward gArea_OnGameModeInit();
  50. #endif
  51.  
  52. /*===============================================
  53. |               Internal Variables              |
  54. ================================================*/
  55. enum area
  56. {
  57.     Float:_minx,
  58.     Float:_miny,
  59.     Float:_maxx,
  60.     Float:_maxy,
  61.     bool:_used,
  62.     bool:_userangeofpoint,
  63.     bool:_active
  64. }
  65. new areas[MAX_AREAS][area];
  66. forward OnPlayerEnterArea(playerid,areaid);
  67. forward OnPlayerLeaveArea(playerid,areaid);
  68.  
  69. /*===============================================
  70. |               External functions              |
  71. ================================================*/
  72. stock CreateArea(Float:x,Float:y,Float:range)
  73. {
  74.     new id = GenerateID();
  75.     if(id == -1) return print("Error! gArea's maximum arealimit has been reached!");
  76.     areas[id][_minx] = x;
  77.     areas[id][_miny] = y;
  78.     areas[id][_maxx] = range;
  79.     areas[id][_userangeofpoint] = true;
  80.     areas[id][_used] = true;
  81.     areas[id][_active] = true;
  82.     return id;
  83. }
  84. stock CreateAreaEx(Float:minx,Float:miny,Float:maxx,Float:maxy)
  85. {
  86.     new id = GenerateID();
  87.     if(id == -1) return print("Error! gArea's maximum arealimit has been reached!");
  88.     areas[id][_minx] = minx;
  89.     areas[id][_miny] = miny;
  90.     areas[id][_maxx] = maxx;
  91.     areas[id][_maxy] = maxy;
  92.     areas[id][_used] = true;
  93.     areas[id][_userangeofpoint] = false;
  94.     areas[id][_active] = true;
  95.     return id;
  96. }
  97.  
  98. stock ToggleArea(areaid,bool:toggle)
  99. {
  100.     if(toggle == true)
  101.     {
  102.         areas[areaid][_active] = true;
  103.     } else {
  104.         areas[areaid][_active] = false;
  105.     }
  106.     return 1;
  107. }
  108.  
  109. /*===============================================
  110. |               Internal functions              |
  111. ================================================*/
  112.  
  113. public OnPlayerDisconnect(playerid, reason)
  114. {
  115.     return CallLocalFunction("gArea_OnPlayerDisconnect","ii",playerid,reason);
  116. }
  117. #if defined _ALS_OnPlayerDisconnect
  118.     #undef OnPlayerDisconnect
  119. #else
  120.     #define _ALS_OnPlayerDisconnect
  121. #endif
  122. #define gArea_OnPlayerDisconnect OnPlayerDisconnect
  123. forward gArea_OnPlayerDisconnect(playerid,reason);
  124.  
  125. stock GenerateID()
  126. {
  127.     for(new i = 0;i<MAX_AREAS;i++)
  128.     {
  129.         if(areas[i][_used] == false) return i;
  130.     }
  131.     return -1;
  132. }
  133.  
  134. stock IsPlayerInArea(playerid, Float:minx, Float:maxx, Float:miny, Float:maxy)
  135. {
  136.     new Float:x, Float:y, Float:z;GetPlayerPos(playerid, x, y, z);
  137.     if (x > minx && x < maxx && y > miny && y < maxy) return 1;
  138.     return 0;
  139. }
  140.  
  141. forward scan_players();
  142. public scan_players()
  143. {
  144.     for(new a = 0;a<MAX_PLAYERS;a++)
  145.     {
  146.         for(new b = 0;b<MAX_AREAS;b++)
  147.         {
  148.             if(areas[b][_active] == true)
  149.             {
  150.                 if(areas[b][_userangeofpoint] == false) //use with IsPlayerInRangeOfPoint
  151.                 {
  152.                     if(IsPlayerInArea(a,areas[b][_minx],areas[b][_maxx],areas[b][_miny],areas[b][_maxy]))
  153.                     {
  154.                         if((GetPVarInt(a,"gArea_entered") -1) != b)
  155.                         {
  156.                             SetPVarInt(a,"gArea_entered",(b +1));
  157.                             OnPlayerEnterArea(a,(b));
  158.                         }
  159.                     } else {
  160.                         if(GetPVarInt(a,"gArea_entered"))
  161.                         {
  162.                             if((GetPVarInt(a,"gArea_entered") -1) == b)
  163.                             {
  164.                                 OnPlayerLeaveArea(a,(GetPVarInt(a,"gArea_entered") -1));
  165.                                 DeletePVar(a,"gArea_entered");
  166.                             }
  167.                         }
  168.                     }
  169.                 } else { //use with IsPlayerInRangeOfPoint()
  170.                     new Float:z;GetPlayerPos(a,z,z,z);
  171.                     if(IsPlayerInRangeOfPoint(a,areas[b][_maxx],areas[b][_minx],areas[b][_miny],z))
  172.                     {
  173.                         if((GetPVarInt(a,"gArea_entered") -1) != b)
  174.                         {
  175.                             SetPVarInt(a,"gArea_entered",(b +1));
  176.                             OnPlayerEnterArea(a,(b));
  177.                         }
  178.                     } else {
  179.                         if(GetPVarInt(a,"gArea_entered"))
  180.                         {
  181.                             if((GetPVarInt(a,"gArea_entered") -1) == b)
  182.                             {
  183.                                 OnPlayerLeaveArea(a,(GetPVarInt(a,"gArea_entered") -1));
  184.                                 DeletePVar(a,"gArea_entered");
  185.                             }
  186.                         }
  187.                     }
  188.                 }
  189.             }
  190.         }
  191.     }
  192.     return 1;
  193. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement