iJayce

Gang zones include by Jayce.

Feb 14th, 2016
453
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 8.66 KB | None | 0 0
  1. /////////////////////////////////////////////////////////
  2. // **   Gang Zones Include.                         ** //
  3. // **           By Jayce (PawnHunter).              ** //
  4. // **                                               ** //
  5. // **   Credits: -                                  ** //
  6. // **               Incognito - Streamer.           ** //
  7. // **               Y_Less - Foreach include.       ** //
  8. // **   File: gangzones.inc.                        ** //
  9. // **   Make sure you added both callbacks in your  ** //
  10. // **   scripts otherwise you'll get:               ** //
  11. // **   : error 004: function                       ** //
  12. // **   "OnPlayerEnterGangZone" is not implemented. ** //
  13. /////////////////////////////////////////////////////////
  14.  
  15. //#define GZ_USE_STREAMER
  16.  
  17. #if defined __PawnHunterGangZones__
  18.     #endinput
  19. #else
  20.     #define __PawnHunterGangZones__
  21. #endif
  22.  
  23. #if !defined _samp_included
  24.     #tryinclude <a_samp>
  25.     #if !defined _samp_included
  26.         #error "Could not find <a_samp.inc> include."
  27.     #endif
  28. #endif
  29.  
  30. #if !defined Iter_Count
  31.     #tryinclude <YSI\y_iterate>
  32.     #if !defined Iter_Count
  33.         #error "Could not find <foreach.inc> include."
  34.     #endif
  35. #endif
  36. #if defined GZ_USE_STREAMER
  37.     #tryinclude <streamer>
  38.     #if !defined INVALID_STREAMER_ID
  39.         #error "Could not find <streamer.inc> include."
  40.     #endif
  41. #endif
  42. #if !defined GZ_MAX_ZONES
  43.     #define GZ_MAX_ZONES 1024
  44. #endif
  45.  
  46. #if !defined GZ_TIMER_TICKRATE
  47.     #define GZ_TIMER_TICKRATE 700
  48. #endif
  49.  
  50. #define GangZonePlayersCount(%0) Iter_Count(GZ_ZonePlayers[%0])
  51.  
  52. forward OnPlayerEnterGangZone(playerid, gangzoneid);
  53. forward OnPlayerExitGangZone(playerid, gangzoneid);
  54. forward OnTimerTick();
  55.  
  56. enum GZ_ZONE_INFO {
  57.     #if defined GZ_USE_STREAMER
  58.         Z_dynamicID,
  59.     #endif
  60.     Float:Z_MinX,
  61.     Float:Z_MinY,
  62.     Float:Z_MaxX,
  63.     Float:Z_MaxY,
  64. };
  65. enum GZ_PLAYER_INFO {
  66.     P_ZoneID,
  67.     P_EnterTick,
  68.     P_ExitTick,
  69. }
  70. static
  71.     GZ_Zone[GZ_MAX_ZONES][GZ_ZONE_INFO],
  72.     bool:GZ_PlayerInZone[MAX_PLAYERS],
  73.     GZ_Player[MAX_PLAYERS][GZ_PLAYER_INFO],
  74.     Iterator:GZ_Zones<GZ_MAX_ZONES>
  75. ;
  76. #if !defined GZ_USE_STREAMER
  77.     static GZ_Timer = -1;
  78. #endif
  79. new stock
  80.     Iterator:GZ_ZonePlayers[GZ_MAX_ZONES]<MAX_PLAYERS>
  81. ;
  82. stock GZ_GangZoneCreate(Float:minx, Float:miny, Float:maxx, Float:maxy)
  83. {
  84.     new id = GangZoneCreate(minx, miny, maxx, maxy);
  85.     #if defined GZ_USE_STREAMER
  86.         GZ_Zone[id][Z_dynamicID] =  CreateDynamicRectangle(minx, miny, maxx, maxy);
  87.     #endif
  88.     GZ_Zone[id][Z_MinX] = minx;
  89.     GZ_Zone[id][Z_MinY] = miny;
  90.     GZ_Zone[id][Z_MaxX] = maxx;
  91.     GZ_Zone[id][Z_MaxY] = maxy;
  92.     Iter_Add(GZ_Zones, id);
  93.     return id;
  94. }
  95. stock GZ_GangZoneDestroy(zone)
  96. {
  97.     if (Iter_Contains(GZ_Zones, zone))
  98.     {
  99.         Iter_Remove(GZ_Zones, zone);
  100.         #if defined GZ_USE_STREAMER
  101.             DestroyDynamicArea(GZ_Zone[id][Z_dynamicID]);
  102.         #endif
  103.         GangZoneDestroy(zone);
  104.         return true;
  105.     }
  106.     else return false;
  107. }
  108. stock GetSecondsSinceEnter(playerid)
  109. {
  110.     if( !IsPlayerInAnyGangZone(playerid) || GZ_Player[playerid][P_EnterTick] == 0) return -1;
  111.     return gettime() - GZ_Player[playerid][P_EnterTick];
  112. }
  113. stock GetSecondsSinceExit(playerid)
  114. {
  115.     if( IsPlayerInAnyGangZone(playerid) || GZ_Player[playerid][P_ExitTick] == 0 ) return -1;
  116.     return gettime() - GZ_Player[playerid][P_ExitTick];
  117. }
  118. stock IsPlayerInGangZone(playerid, zoneid) return GZ_Player[playerid][P_ZoneID] == zoneid;
  119. stock IsPlayerInAnyGangZone(playerid) return GZ_PlayerInZone[playerid];
  120.  
  121. #if defined GZ_USE_STREAMER
  122. public OnPlayerEnterDynamicArea(playerid, areaid)
  123. {
  124.     foreach(new zone : GZ_Zones)
  125.     {
  126.         if( areaid == GZ_Zone[zone][Z_dynamicID]  )
  127.         {
  128.             GZ_Player[playerid][P_ZoneID] = zone;
  129.             GZ_Player[playerid][P_EnterTick] = gettime();
  130.             Iter_Add(GZ_ZonePlayers[zone], playerid);
  131.             GZ_PlayerInZone[playerid] = true;
  132.             // CallLocalFunction("OnPlayerEnterGangZone", "ii", playerid, zone);
  133.             OnPlayerEnterGangZone(playerid, zone);
  134.             break;
  135.         }
  136.     }
  137.     return true;
  138. }
  139. public OnPlayerLeaveDynamicArea(playerid, areaid)
  140. {
  141.     foreach(new zone : GZ_Zones)
  142.     {
  143.         if( areaid == GZ_Zone[zone][Z_dynamicID] )
  144.         {
  145.             GZ_Player[playerid][P_ZoneID] = -1;
  146.             GZ_Player[playerid][P_ExitTick] = gettime();
  147.             Iter_Remove(GZ_ZonePlayers[zone], playerid);
  148.             GZ_PlayerInZone[playerid] = false;
  149.             // CallLocalFunction("OnPlayerExitGangZone", "ii", playerid, zone);
  150.             OnPlayerExitGangZone(playerid, zone);
  151.             break;
  152.         }
  153.     }
  154.     return true;
  155. }
  156. #else
  157.     public OnTimerTick()
  158.     {
  159.         new Float:x, Float:y, Float:z;
  160.         foreach(new i : Player)
  161.         {
  162.             if(!IsPlayerInAnyGangZone(i))
  163.             {
  164.                 GetPlayerPos(i, x, y, z);
  165.                 foreach(new zone : GZ_Zones)
  166.                 {
  167.                     if(x < GZ_Zone[zone][Z_MaxX] && x > GZ_Zone[zone][Z_MinX] && y < GZ_Zone[zone][Z_MaxY] && y > GZ_Zone[zone][Z_MinY])
  168.                     {
  169.                         GZ_Player[i][P_ZoneID] = zone;
  170.                         GZ_Player[i][P_EnterTick] = gettime();
  171.                         Iter_Add(GZ_ZonePlayers[zone], i);
  172.                         GZ_PlayerInZone[i] = true;
  173.                         // CallLocalFunction("OnPlayerEnterGangZone", "ii", i, zone);
  174.                         OnPlayerEnterGangZone(i, zone);
  175.                         break;
  176.                     }
  177.                 }
  178.             }
  179.             else
  180.             {
  181.                 GetPlayerPos(i, x, y, z);
  182.                 foreach(new zone : GZ_Zones)
  183.                 {
  184.                     if(IsPlayerInGangZone(i, zone))
  185.                     {  
  186.                         if(x < GZ_Zone[zone][Z_MaxX] && x > GZ_Zone[zone][Z_MinX] && y < GZ_Zone[zone][Z_MaxY] && y > GZ_Zone[zone][Z_MinY]) continue;
  187.                         else
  188.                         {
  189.                             GZ_Player[i][P_ZoneID] = -1;
  190.                             GZ_Player[i][P_ExitTick] = gettime();
  191.                             Iter_Remove(GZ_ZonePlayers[zone], i);
  192.                             GZ_PlayerInZone[i] = false;
  193.                             // CallLocalFunction("OnPlayerExitGangZone", "ii", i, zone);
  194.                             OnPlayerExitGangZone(i, zone);
  195.                             break;
  196.                         }
  197.                     }
  198.                 }
  199.             }
  200.         }
  201.         return true;
  202.     }
  203. #endif
  204.  
  205. #if defined FILTERSCRIPT
  206. public OnFilterScriptInit()
  207. {
  208. #else
  209. public OnGameModeInit()
  210. {
  211. #endif
  212.  
  213.     #if !defined GZ_USE_STREAMER
  214.         GZ_Timer = SetTimer("OnTimerTick", GZ_TIMER_TICKRATE, true);
  215.     #endif
  216.     #if defined FILTERSCRIPT
  217.         #if defined GZ_OnFilterScriptInit
  218.             return GZ_OnFilterScriptInit();
  219.         #else
  220.             return true;
  221.         #endif
  222.     #else
  223.         #if defined GZ_OnGameModeInit
  224.             return GZ_OnGameModeInit();
  225.         #else
  226.             return true;
  227.         #endif
  228.     #endif
  229.  
  230. }
  231. #if defined FILTERSCRIPT
  232.     #if defined _ALS_OnFilterScriptInit
  233.         #undef OnFilterScriptInit
  234.     #else
  235.         #define _ALS_OnFilterScriptInit
  236.     #endif
  237.     #define OnFilterScriptInit GZ_OnFilterScriptInit
  238.     #if defined GZ_OnFilterScriptInit
  239.         forward GZ_OnFilterScriptInit();
  240.     #endif
  241. #else
  242.     #if defined _ALS_OnGameModeInit
  243.         #undef OnGameModeInit
  244.     #else
  245.         #define _ALS_OnGameModeInit
  246.     #endif
  247.     #define OnGameModeInit GZ_OnGameModeInit
  248.     #if defined GZ_OnGameModeInit
  249.         forward GZ_OnGameModeInit();
  250.     #endif
  251. #endif
  252.  
  253.  
  254. #if defined FILTERSCRIPT
  255. public OnFilterScriptExit()
  256. {
  257. #else
  258. public OnGameModeExit()
  259. {
  260. #endif
  261.  
  262.     #if !defined GZ_USE_STREAMER
  263.     if(GZ_Timer != -1) KillTimer(GZ_Timer);
  264.     #endif
  265.    
  266.     #if defined FILTERSCRIPT
  267.         #if defined GZ_OnFilterScriptExit
  268.             return GZ_OnFilterScriptExit();
  269.         #else
  270.             return true;
  271.         #endif
  272.     #else
  273.         #if defined GZ_OnGameModeExit
  274.             return GZ_OnGameModeExit();
  275.         #else
  276.             return true;
  277.         #endif
  278.     #endif
  279.  
  280. }
  281. #if defined FILTERSCRIPT
  282.     #if defined _ALS_OnFilterScriptExit
  283.         #undef OnFilterScriptExit
  284.     #else
  285.         #define _ALS_OnFilterScriptExit
  286.     #endif
  287.     #define OnFilterScriptExit GZ_OnFilterScriptExit
  288.     #if defined GZ_OnFilterScriptExit
  289.         forward GZ_OnFilterScriptExit();
  290.     #endif
  291. #else
  292.     #if defined _ALS_OnGameModeExit
  293.         #undef OnGameModeExit
  294.     #else
  295.         #define _ALS_OnGameModeExit
  296.     #endif
  297.     #define OnGameModeExit GZ_OnGameModeExit
  298.     #if defined GZ_OnGameModeExit
  299.         forward GZ_OnGameModeExit();
  300.     #endif
  301. #endif
  302.  
  303.  
  304. public OnPlayerConnect(playerid)
  305. {
  306.     GZ_Player[playerid][P_ZoneID] = -1;
  307.     GZ_Player[playerid][P_EnterTick] = 0;
  308.     GZ_Player[playerid][P_ExitTick] = 0;
  309.     GZ_PlayerInZone[playerid] = false;
  310.     #if defined GZ_OnPlayerConnect
  311.         return GZ_OnPlayerConnect(playerid);
  312.     #else
  313.         return true;
  314.     #endif
  315. }
  316.  
  317.  
  318. #if defined _ALS_OnPlayerConnect
  319.     #undef OnPlayerConnect
  320. #else
  321.     #define _ALS_OnPlayerConnect
  322. #endif
  323. #define OnPlayerConnect GZ_OnPlayerConnect
  324. #if defined GZ_OnPlayerConnect
  325.     forward GZ_OnPlayerConnect(playerid);
  326. #endif
  327. public OnPlayerDisconnect(playerid, reason)
  328. {
  329.     if(IsPlayerInAnyGangZone(playerid))
  330.     {  
  331.         new zone = GZ_Player[playerid][P_ZoneID];
  332.         Iter_Remove(GZ_ZonePlayers[zone], playerid);
  333.     }
  334.     #if defined GZ_OnPlayerDisconnect
  335.         return GZ_OnPlayerDisconnect(playerid, reason);
  336.     #else
  337.         return true;
  338.     #endif
  339. }
  340.  
  341.  
  342. #if defined _ALS_OnPlayerDisconnect
  343.     #undef OnPlayerDisconnect
  344. #else
  345.     #define _ALS_OnPlayerDisconnect
  346. #endif
  347. #define OnPlayerDisconnect GZ_OnPlayerDisconnect
  348. #if defined GZ_OnPlayerDisconnect
  349.     forward GZ_OnPlayerDisconnect(playerid, reason);
  350. #endif
  351.  
  352. #if defined _ALS_GangZoneDestroy
  353.     #undef GangZoneDestroy
  354. #else
  355.     #define _ALS_GangZoneDestroy
  356. #endif
  357. #define GangZoneDestroy GZ_GangZoneDestroy
  358.  
  359. #if defined _ALS_GangZoneCreate
  360.     #undef GangZoneCreate
  361. #else
  362.     #define _ALS_GangZoneCreate
  363. #endif
  364. #define GangZoneCreate GZ_GangZoneCreate
Advertisement
Add Comment
Please, Sign In to add comment