Advertisement
RuiGy

dzones.inc - Dynamic Zones

Aug 2nd, 2012
319
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 16.09 KB | None | 0 0
  1. /*----------------------------------------------------------------------------*\
  2.                             Dynamic Zones - Include
  3. Description:
  4.     This include contains functions to manage gangzones between teams
  5.     dynamically.
  6. Version:
  7.     1.0
  8. Changelog:
  9.     02/08/2012:
  10.         * First version.
  11. Extra Data:
  12.     lines: 405 - length: 16477
  13. Author:
  14.     RuiGy.
  15. Thanks to:
  16.     Y_Less,
  17.     SA-MP Team.
  18. \*----------------------------------------------------------------------------*/
  19.  
  20. #include    <a_samp>
  21.  
  22. #if defined _dzones_included
  23.     #endinput
  24. #endif
  25.  
  26. #define _dzones_included
  27.  
  28. /*
  29. native GetDynamicZoneArea(zoneid, &Float:minx, &Float:miny, &Float:maxx, &Float:maxy);
  30. native SetDynamicZoneArea(zoneid, Float:minx, Float:miny, Float:maxx, Float:maxy);
  31. native CreateDynamicZone(Float:minx, Float:miny, Float:maxx, Float:maxy, color);
  32. native FlashDynamicZoneForPlayer(playerid, zoneid, color);
  33. native IsDynamicZoneFlashingForPlayer(playerid, zoneid);
  34. native IsDynamicZoneVisibleForPlayer(playerid, zoneid);
  35. native StopFlashDynamicZoneForPlayer(playerid, zoneid);
  36. native ShowDynamicZoneForPlayer(playerid, zoneid);
  37. native HideDynamicZoneForPlayer(playerid, zoneid);
  38. native IsPlayerInDynamicZone(playerid, zoneid);
  39. native FlashDynamicZoneForAll(zoneid, color);
  40. native SetDynamicZoneColor(zoneid, color);
  41. native StopFlashDynamicZoneForAll(zoneid);
  42. native ShowDynamicZoneForAll(zoneid);
  43. native HideDynamicZoneForAll(zoneid);
  44. native GetDynamicZoneColor(zoneid);
  45. native DestroyDynamicZone(zoneid);
  46. native zexist(zoneid);
  47. */
  48.  
  49. /*----------------------------------------------------------------------------*\
  50.                 Global variables of Project.
  51. \*----------------------------------------------------------------------------*/
  52. enum g_GANG_ZONE {
  53.     g_GANG_ZONE_ID,
  54.     g_GANG_ZONE_COLOR,
  55.     Float:g_GANG_ZONE_MINX,
  56.     Float:g_GANG_ZONE_MINY,
  57.     Float:g_GANG_ZONE_MAXX,
  58.     Float:g_GANG_ZONE_MAXY
  59. }
  60. new
  61.     gZoneData[ MAX_GANG_ZONES ][ g_GANG_ZONE ];
  62.  
  63. /*----------------------------------------------------------------------------*\
  64.                 Forwards of Project.
  65. \*----------------------------------------------------------------------------*/
  66. forward dzones_Check();
  67.  
  68. /*----------------------------------------------------------------------------*\
  69.                 This function creates a dynamic zone.
  70. \*----------------------------------------------------------------------------*/
  71. stock CreateDynamicZone(Float:minx, Float:miny, Float:maxx, Float:maxy, color)
  72. {
  73.     new int_zone_id = SearchZones();
  74.     if (int_zone_id == INVALID_GANG_ZONE) return print("ERROR: Maximum limit of zones exceeded. (dzones.inc)"), 0;
  75.     gZoneData[ int_zone_id ][ g_GANG_ZONE_ID ] = GangZoneCreate(minx, miny, maxx, maxy);
  76.     gZoneData[ int_zone_id ][ g_GANG_ZONE_MINX ] = minx;
  77.     gZoneData[ int_zone_id ][ g_GANG_ZONE_MINY ] = miny;
  78.     gZoneData[ int_zone_id ][ g_GANG_ZONE_MAXX ] = maxx;
  79.     gZoneData[ int_zone_id ][ g_GANG_ZONE_MAXY ] = maxy;
  80.     gZoneData[ int_zone_id ][ g_GANG_ZONE_COLOR ] = color;
  81.     return int_zone_id;
  82. }
  83.  
  84. /*----------------------------------------------------------------------------*\
  85.                 This function destroys a dynamic zone.
  86. \*----------------------------------------------------------------------------*/
  87. stock DestroyDynamicZone(int_zone_id)
  88. {
  89.     if (gZoneData[ int_zone_id ][ g_GANG_ZONE_ID ] == INVALID_GANG_ZONE) return 0;
  90.     GangZoneDestroy(gZoneData[ int_zone_id ][ g_GANG_ZONE_ID ]);
  91.     gZoneData[ int_zone_id ][ g_GANG_ZONE_ID ] = INVALID_GANG_ZONE;
  92.     return int_zone_id;
  93. }
  94.  
  95. /*----------------------------------------------------------------------------*\
  96.                 This function change the zone position.
  97. \*----------------------------------------------------------------------------*/
  98. stock SetDynamicZoneArea(int_zone_id, Float:minx, Float:miny, Float:maxx, Float:maxy)
  99. {
  100.     if (gZoneData[ int_zone_id ][ g_GANG_ZONE_ID ] == INVALID_GANG_ZONE) return 0;
  101.     GangZoneDestroy(gZoneData[ int_zone_id ][ g_GANG_ZONE_ID ]);
  102.     gZoneData[ int_zone_id ][ g_GANG_ZONE_MINX ] = minx;
  103.     gZoneData[ int_zone_id ][ g_GANG_ZONE_MINY ] = miny;
  104.     gZoneData[ int_zone_id ][ g_GANG_ZONE_MAXX ] = maxx;
  105.     gZoneData[ int_zone_id ][ g_GANG_ZONE_MAXY ] = maxy;
  106.     gZoneData[ int_zone_id ][ g_GANG_ZONE_ID ] = GangZoneCreate(minx, miny, maxx, maxy);
  107.    
  108.     for (new i; i < MAX_PLAYERS; i++) {
  109.         new str_zone_id[14];
  110.         format(str_zone_id, sizeof str_zone_id, "vszone_%i", int_zone_id);
  111.         if (GetPVarInt(i, str_zone_id) == 1) {
  112.             GangZoneHideForPlayer(i, gZoneData[ int_zone_id ][ g_GANG_ZONE_ID ]);
  113.             GangZoneShowForPlayer(i, gZoneData[ int_zone_id ][ g_GANG_ZONE_ID ], gZoneData[ int_zone_id ][ g_GANG_ZONE_COLOR ]);
  114.         }
  115.     }
  116.    
  117.     return int_zone_id;
  118. }
  119.  
  120. /*----------------------------------------------------------------------------*\
  121.         This function Store the zone position, passed by reference.
  122. \*----------------------------------------------------------------------------*/
  123. stock GetDynamicZoneArea(int_zone_id, &Float:minx, &Float:miny, &Float:maxx, &Float:maxy)
  124. {
  125.     if (gZoneData[ int_zone_id ][ g_GANG_ZONE_ID ] == INVALID_GANG_ZONE) return 0;
  126.     minx = gZoneData[ int_zone_id ][ g_GANG_ZONE_MINX ];
  127.     miny = gZoneData[ int_zone_id ][ g_GANG_ZONE_MINY ];
  128.     maxx = gZoneData[ int_zone_id ][ g_GANG_ZONE_MAXX ];
  129.     maxy = gZoneData[ int_zone_id ][ g_GANG_ZONE_MAXY ];
  130.     return int_zone_id;
  131. }
  132.  
  133. /*----------------------------------------------------------------------------*\
  134.                 This function show a dynamic zone for a player.
  135. \*----------------------------------------------------------------------------*/
  136. stock ShowDynamicZoneForPlayer(playerid, int_zone_id)
  137. {
  138.     if (gZoneData[ int_zone_id ][ g_GANG_ZONE_ID ] == INVALID_GANG_ZONE) return 0;
  139.     GangZoneShowForPlayer(playerid, gZoneData[ int_zone_id ][ g_GANG_ZONE_ID ], gZoneData[ int_zone_id ][ g_GANG_ZONE_COLOR ]);
  140.  
  141.     new str_zone_id[14];
  142.     format(str_zone_id, sizeof str_zone_id, "vszone_%i", int_zone_id);
  143.     SetPVarInt(playerid, str_zone_id, 1);
  144.  
  145.     return int_zone_id;
  146. }
  147.  
  148. /*----------------------------------------------------------------------------*\
  149.                 This function hides a dynamic zone for a player.
  150. \*----------------------------------------------------------------------------*/
  151. stock HideDynamicZoneForPlayer(playerid, int_zone_id)
  152. {
  153.     if (gZoneData[ int_zone_id ][ g_GANG_ZONE_ID ] == INVALID_GANG_ZONE) return 0;
  154.     GangZoneHideForPlayer(playerid, gZoneData[ int_zone_id ][ g_GANG_ZONE_ID ]);
  155.  
  156.     new str_zone_id[14];
  157.     format(str_zone_id, sizeof str_zone_id, "vszone_%i", int_zone_id);
  158.     DeletePVar(playerid, str_zone_id);
  159.  
  160.     return int_zone_id;
  161. }
  162.  
  163. /*----------------------------------------------------------------------------*\
  164.                 This function show a dynamic zone for all.
  165. \*----------------------------------------------------------------------------*/
  166. stock ShowDynamicZoneForAll(int_zone_id)
  167. {
  168.     if (gZoneData[ int_zone_id ][ g_GANG_ZONE_ID ] == INVALID_GANG_ZONE) return 0;
  169.     for (new i; i < MAX_PLAYERS; i++) {
  170.         GangZoneShowForPlayer(i, gZoneData[ int_zone_id ][ g_GANG_ZONE_ID ], gZoneData[ int_zone_id ][ g_GANG_ZONE_COLOR ]);
  171.  
  172.         new str_zone_id[14];
  173.         format(str_zone_id, sizeof str_zone_id, "vszone_%i", int_zone_id);
  174.         SetPVarInt(i, str_zone_id, 1);
  175.     }
  176.     return int_zone_id;
  177. }
  178.  
  179. /*----------------------------------------------------------------------------*\
  180.                 This function hides a dynamic zone for all.
  181. \*----------------------------------------------------------------------------*/
  182. stock HideDynamicZoneForAll(int_zone_id)
  183. {
  184.     if (gZoneData[ int_zone_id ][ g_GANG_ZONE_ID ] == INVALID_GANG_ZONE) return 0;
  185.     for (new i; i < MAX_PLAYERS; i++) {
  186.         GangZoneHideForPlayer(i, gZoneData[ int_zone_id ][ g_GANG_ZONE_ID ]);
  187.  
  188.         new str_zone_id[14];
  189.         format(str_zone_id, sizeof str_zone_id, "vszone_%i", int_zone_id);
  190.         DeletePVar(i, str_zone_id);
  191.     }
  192.     return int_zone_id;
  193. }
  194.  
  195. /*----------------------------------------------------------------------------*\
  196.                 This function flashs a dynamic zone for all.
  197. \*----------------------------------------------------------------------------*/
  198. stock FlashDynamicZoneForAll(int_zone_id, color)
  199. {
  200.     if (gZoneData[ int_zone_id ][ g_GANG_ZONE_ID ] == INVALID_GANG_ZONE) return 0;
  201.     for (new i; i < MAX_PLAYERS; i++) {
  202.         new str_zone_id[14];
  203.         format(str_zone_id, sizeof str_zone_id, "vszone_%i", int_zone_id);
  204.         if (GetPVarInt(i, str_zone_id) == 1) {
  205.             GangZoneFlashForPlayer(i, gZoneData[ int_zone_id ][ g_GANG_ZONE_ID ], color);
  206.  
  207.             new str_zone_id_2[14];
  208.             format(str_zone_id_2, sizeof str_zone_id_2, "fszone_%i", int_zone_id);
  209.             SetPVarInt(i, str_zone_id_2, 1);
  210.         }
  211.     }
  212.     return int_zone_id;
  213. }
  214.  
  215. /*----------------------------------------------------------------------------*\
  216.             This function stop flashing a dynamic zone for all.
  217. \*----------------------------------------------------------------------------*/
  218. stock StopFlashDynamicZoneForAll(int_zone_id)
  219. {
  220.     if (gZoneData[ int_zone_id ][ g_GANG_ZONE_ID ] == INVALID_GANG_ZONE) return 0;
  221.     for (new i; i < MAX_PLAYERS; i++) {
  222.         new str_zone_id[14];
  223.         format(str_zone_id, sizeof str_zone_id, "vszone_%i", int_zone_id);
  224.         if (GetPVarInt(i, str_zone_id) == 1) {
  225.             GangZoneStopFlashForPlayer(i, gZoneData[ int_zone_id ][ g_GANG_ZONE_ID ]);
  226.  
  227.             new str_zone_id_2[14];
  228.             format(str_zone_id_2, sizeof str_zone_id_2, "fszone_%i", int_zone_id);
  229.             DeletePVar(i, str_zone_id_2);
  230.         }
  231.     }
  232.     return int_zone_id;
  233. }
  234.  
  235. /*----------------------------------------------------------------------------*\
  236.                 This function flashs a dynamic zone for a player.
  237. \*----------------------------------------------------------------------------*/
  238. stock FlashDynamicZoneForPlayer(playerid, int_zone_id, color)
  239. {
  240.     if (gZoneData[ int_zone_id ][ g_GANG_ZONE_ID ] == INVALID_GANG_ZONE) return 0;
  241.     new str_zone_id[14];
  242.     format(str_zone_id, sizeof str_zone_id, "vszone_%i", int_zone_id);
  243.     if (GetPVarInt(playerid, str_zone_id) == 1) {
  244.         GangZoneFlashForPlayer(playerid, gZoneData[ int_zone_id ][ g_GANG_ZONE_ID ], color);
  245.         new str_zone_id_2[14];
  246.         format(str_zone_id_2, sizeof str_zone_id_2, "fszone_%i", int_zone_id);
  247.         SetPVarInt(playerid, str_zone_id_2, 1);
  248.     }
  249.     return int_zone_id;
  250. }
  251.  
  252. /*----------------------------------------------------------------------------*\
  253.             This function stop flashing a dynamic zone for a player.
  254. \*----------------------------------------------------------------------------*/
  255. stock StopFlashDynamicZoneForPlayer(playerid, int_zone_id)
  256. {
  257.     if (gZoneData[ int_zone_id ][ g_GANG_ZONE_ID ] == INVALID_GANG_ZONE) return 0;
  258.     new str_zone_id[14];
  259.     format(str_zone_id, sizeof str_zone_id, "vszone_%i", int_zone_id);
  260.     if (GetPVarInt(playerid, str_zone_id) == 1) {
  261.         GangZoneStopFlashForPlayer(playerid, gZoneData[ int_zone_id ][ g_GANG_ZONE_ID ]);
  262.         new str_zone_id_2[14];
  263.         format(str_zone_id_2, sizeof str_zone_id_2, "fszone_%i", int_zone_id);
  264.         DeletePVar(playerid, str_zone_id_2);
  265.     }
  266.     return int_zone_id;
  267. }
  268.  
  269. /*----------------------------------------------------------------------------*\
  270.                 This function sets a dynamic gang zone color.
  271. \*----------------------------------------------------------------------------*/
  272. stock SetDynamicZoneColor(int_zone_id, color)
  273. {
  274.     if (gZoneData[ int_zone_id ][ g_GANG_ZONE_ID ] == INVALID_GANG_ZONE) return 0;
  275.     gZoneData[ int_zone_id ][ g_GANG_ZONE_COLOR ] = color;
  276.     for (new i; i < MAX_PLAYERS; i++) {
  277.         new str_zone_id[14];
  278.         format(str_zone_id, sizeof str_zone_id, "vszone_%i", int_zone_id);
  279.         if (GetPVarInt(i, str_zone_id) == 1) {
  280.             GangZoneHideForPlayer(i, gZoneData[ int_zone_id ][ g_GANG_ZONE_ID ]);
  281.             GangZoneShowForPlayer(i, gZoneData[ int_zone_id ][ g_GANG_ZONE_ID ], gZoneData[ int_zone_id ][ g_GANG_ZONE_COLOR ]);
  282.         }
  283.     }
  284.     return int_zone_id;
  285. }
  286.  
  287. /*----------------------------------------------------------------------------*\
  288.                 This function gets a dynamic gang zone color.
  289. \*----------------------------------------------------------------------------*/
  290. stock GetDynamicZoneColor(int_zone_id)
  291. {
  292.     if (gZoneData[ int_zone_id ][ g_GANG_ZONE_ID ] == INVALID_GANG_ZONE) return 0;
  293.     return gZoneData[ int_zone_id ][ g_GANG_ZONE_COLOR ];
  294. }
  295.  
  296. /*----------------------------------------------------------------------------*\
  297.         This function checks if a dynamic zone is visible for a player.
  298. \*----------------------------------------------------------------------------*/
  299. stock IsDynamicZoneVisibleForPlayer(playerid, int_zone_id)
  300. {
  301.     new str_zone_id[14];
  302.     format(str_zone_id, sizeof str_zone_id, "vszone_%i", int_zone_id);
  303.     return (!GetPVarInt(playerid, str_zone_id)) ? 0 : 1;
  304. }
  305.  
  306. /*----------------------------------------------------------------------------*\
  307.         This function checks if a dynamic zone is flashing for a player.
  308. \*----------------------------------------------------------------------------*/
  309. stock IsDynamicZoneFlashingForPlayer(playerid, int_zone_id)
  310. {
  311.     new str_zone_id[14];
  312.     format(str_zone_id, sizeof str_zone_id, "fszone_%i", int_zone_id);
  313.     return (!GetPVarInt(playerid, str_zone_id)) ? 0 : 1;
  314. }
  315.  
  316. /*----------------------------------------------------------------------------*\
  317.         This function checks if a player is inside a dynamic zone.
  318. \*----------------------------------------------------------------------------*/
  319. stock IsPlayerInDynamicZone(playerid, int_zone_id)
  320. {
  321.     new
  322.         Float:x,
  323.         Float:y,
  324.         Float:z;
  325.  
  326.     GetPlayerPos(playerid, x, y, z);
  327.     if (x > gZoneData[ int_zone_id ][ g_GANG_ZONE_MINX ] && x < gZoneData[ int_zone_id ][ g_GANG_ZONE_MAXX ] && y > gZoneData[ int_zone_id ][ g_GANG_ZONE_MINY ] && y < gZoneData[ int_zone_id ][ g_GANG_ZONE_MAXY ]) return 1;
  328.     return 0;
  329. }
  330.  
  331. /*----------------------------------------------------------------------------*\
  332.                 This function checks if a zone exist.
  333. \*----------------------------------------------------------------------------*/
  334. stock zexist(int_zone_id)
  335. {
  336.     return (!gZoneData[ int_zone_id ][ g_GANG_ZONE_ID ]) ? 0 : 1;
  337. }
  338.  
  339. /*----------------------------------------------------------------------------*\
  340.                 This function searches for existing zones.
  341. \*----------------------------------------------------------------------------*/
  342. stock SearchZones()
  343. {
  344.     for (new z = 0; z < MAX_GANG_ZONES; z++) if (gZoneData[ z ][ g_GANG_ZONE_ID ] == INVALID_GANG_ZONE) return z;
  345.     return INVALID_GANG_ZONE;
  346. }
  347.  
  348. /*----------------------------------------------------------------------------*\
  349.                 This callback is called one time each second.
  350. \*----------------------------------------------------------------------------*/
  351. public dzones_Check()
  352. {
  353.     for (new i = 0; i < MAX_PLAYERS; i++) {
  354.         for (new z = 0; z < MAX_GANG_ZONES; z++) {
  355.             new str_zone_id[14];
  356.             format(str_zone_id, sizeof str_zone_id, "etzone_%i", z);
  357.             if (IsPlayerInDynamicZone(i, z) && GetPVarInt(i, str_zone_id) == 0) {
  358.                 CallLocalFunction("OnPlayerEnterDynamicZone", "ii", i, str_zone_id);
  359.                 SetPVarInt(i, str_zone_id, 1);
  360.             }
  361.             else if (!IsPlayerInDynamicZone(i, z) && GetPVarInt(i, str_zone_id) == 1) {
  362.                 CallLocalFunction("OnPlayerExitDynamicZone", "ii", i, str_zone_id);
  363.                 DeletePVar(i, str_zone_id);
  364.             }
  365.         }
  366.     }
  367.     return 1;
  368. }
  369.  
  370. /*----------------------------------------------------------------------------*\
  371.             Hooking OnGameMode/FilterScriptInit, Thanks Y_Less.
  372. \*----------------------------------------------------------------------------*/
  373. #if defined FILTERSCRIPT
  374.     public OnFilterScriptInit()
  375.     {
  376.         for (new z = 0; z < MAX_GANG_ZONES; z++) { gZoneData[ z ][ g_GANG_ZONE_ID ] = INVALID_GANG_ZONE; }
  377.         SetTimer("dzones_Check", 1000, true);
  378.         return CallLocalFunction("dzones_OnFilterScriptInit", "");
  379.     }
  380.     #if defined _ALS_OnFilterScriptInit
  381.         #undef OnFilterScriptInit
  382.     #else
  383.         #define _ALS_OnFilterScriptInit
  384.     #endif
  385.     #define OnFilterScriptInit dzones_OnFilterScriptInit
  386.     forward dzones_OnFilterScriptInit();
  387.     forward OnPlayerEnterDynamicZone(playerid, zoneid);
  388.     forward OnPlayerExitDynamicZone(playerid, zoneid);
  389. #else
  390.     public OnGameModeInit()
  391.     {
  392.         for (new z = 0; z < MAX_GANG_ZONES; z++) { gZoneData[ z ][ g_GANG_ZONE_ID ] = INVALID_GANG_ZONE; }
  393.         SetTimer("dzones_Check", 1000, true);
  394.         return CallLocalFunction("dzones_OnGameModeInit", "");
  395.     }
  396.     #if defined _ALS_OnGameModeInit
  397.         #undef OnGameModeInit
  398.     #else
  399.         #define _ALS_OnGameModeInit
  400.     #endif
  401.     #define OnGameModeInit dzones_OnGameModeInit
  402.     forward dzones_OnGameModeInit();
  403.     forward OnPlayerEnterDynamicZone(playerid, zoneid);
  404.     forward OnPlayerExitDynamicZone(playerid, zoneid);
  405. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement