Kwarde

k_playerarea

Feb 21st, 2011
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 5.29 KB | None | 0 0
  1. /*
  2.     PlayerArea's - By Kwarde.
  3.     Current version: 1.0.1
  4. */
  5.  
  6. #if defined _playerarea_included
  7.     #endinput
  8. #endif
  9. #define _playerarea_included
  10.  
  11. #include <a_samp>
  12.  
  13. #define MAX_AREAS       100
  14. #define ERROR_NOAREA    11
  15. #define ERROR_LIMIT     12
  16.  
  17. #if defined IsPlayerInArea
  18.     #define IsPlayerInAreaEx IsPlayerInKRPAArea
  19. #else
  20.     #define IsPlayerInArea IsPlayerInKRPAArea
  21. #endif
  22.  
  23. enum aInfo
  24. {
  25.     aID,
  26.     Float:aPosMinX,
  27.     Float:aPosMinY,
  28.     Float:aPosMinZ,
  29.     Float:aPosMaxX,
  30.     Float:aPosMaxY,
  31.     Float:aPosMaxZ,
  32.     bool:aZUsed,
  33.     bool:aCreated
  34. };
  35. new AreaInfo[MAX_AREAS][aInfo], AreaCount = -1;
  36. new pIsInArea[MAX_PLAYERS], pTimer[MAX_PLAYERS];
  37.  
  38. forward OnPlayerEnterArea(playerid, areaid);
  39. forward OnPlayerExitArea(playerid, areaid);
  40.  
  41. forward _KRPA_OnPlayerConnect(playerid);
  42. forward _KRPA_OnPlayerDisconnect(playerid, reason);
  43. forward CheckPlayerPos(playerid);
  44.  
  45. public OnPlayerConnect(playerid)
  46. {
  47.     pIsInArea[playerid] = (-1);
  48.     pTimer[playerid] = SetTimerEx("CheckPlayerPos", 500, true, "i", playerid);
  49. }
  50. #if defined _ALS_OnPlayerConnect
  51.     #undef OnPlayerConnect
  52. #else
  53.     #define _ALS_OnPlayerConnect
  54. #endif
  55. #define OnPlayerConnect _KRPA_OnPlayerConnect
  56.  
  57. public OnPlayerDisconnect(playerid)
  58. {
  59.     KillTimer(pTimer[playerid]);
  60.     pTimer[playerid] = (-1);
  61.     pIsInArea[playerid] = (-1);
  62. }
  63. #if defined _ALS_OnPlayerDisconnect
  64.     #undef OnPlayerDisconnect
  65. #else
  66.     #define _ALS_OnPlayerDisconnect
  67. #endif
  68. #define OnPlayerDisconnect _KRPA_OnPlayerDisconnect
  69.  
  70. public CheckPlayerPos(playerid)
  71. {
  72.     for(new i = 0; i < MAX_AREAS; i++){
  73.         if(!IsAreaCreated(i)) continue;
  74.         if(IsPlayerInArea(playerid, i) && pIsInArea[playerid] != i){
  75.             CallLocalFunction("OnPlayerEnterArea", "dd", playerid, i);
  76.             pIsInArea[playerid] = i;
  77.         }
  78.         else if(!IsPlayerInArea(playerid, i) && pIsInArea[playerid] == i){
  79.             CallLocalFunction("OnPlayerExitArea", "dd", playerid, i);
  80.             pIsInArea[playerid] = (-1);
  81.         }
  82.     }
  83.     return 1;
  84. }
  85.  
  86. stock IsPlayerInKRPAArea(playerid, areaid)
  87. {
  88.     if(!IsAreaCreated(areaid)) return ERROR_NOAREA;
  89.     new
  90.         Float:pX,
  91.         Float:pY,
  92.         Float:pZ
  93.     ;
  94.     GetPlayerPos(playerid, pX, pY, pZ);
  95.     if(pX >= AreaInfo[areaid][aPosMinX] && pX <= AreaInfo[areaid][aPosMaxX] && pY >= AreaInfo[areaid][aPosMinY] && pY <= AreaInfo[areaid][aPosMaxY]){
  96.         if(AreaInfo[areaid][aZUsed]){
  97.             if(pZ >= AreaInfo[areaid][aPosMinZ] && pZ <= AreaInfo[areaid][aPosMaxZ])
  98.                 return true;
  99.         }
  100.         else return true;
  101.     }
  102.     return false;
  103. }
  104.  
  105. stock IsAreaCreated(areaid)
  106.     return AreaInfo[areaid][aCreated];
  107.    
  108. stock GetLowestFreeAreaID()
  109. {
  110.     for(new i = 0; i < MAX_AREAS; i++)
  111.     {
  112.         if(IsAreaCreated(i) || i == AreaCount) //I had a bug.
  113.             continue;
  114.         else
  115.             return i;
  116.     }
  117.     return ERROR_NOAREA;
  118. }
  119.    
  120. stock CreateArea(Float:MinX, Float:MaxX, Float:MinY, Float:MaxY, Float:MinZ = -1.0, Float:MaxZ = -1.0)
  121. {
  122.     new id;
  123.     AreaCount++;
  124.     if(AreaCount == MAX_AREAS-1) return ERROR_LIMIT; //Script language counts from 0 untill the end - 1. (Because the '0' was there). With 'for(new i = 0; i < 5; i++)' it'll loop untill 4
  125.     id = GetLowestFreeAreaID();
  126.     AreaInfo[id][aPosMinX] = MinX;
  127.     AreaInfo[id][aPosMaxX] = MaxX;
  128.     AreaInfo[id][aPosMinY] = MinY;
  129.     AreaInfo[id][aPosMaxY] = MaxY;
  130.     AreaInfo[id][aPosMinZ] = MinZ;
  131.     AreaInfo[id][aPosMaxZ] = MaxZ;
  132.     if(AreaInfo[id][aPosMinZ] == -1.0 && AreaInfo[id][aPosMaxZ] == -1.0) AreaInfo[id][aZUsed] = false;
  133.     AreaInfo[id][aCreated] = true;
  134.     return id;
  135. }
  136.  
  137. stock CreateArea2(Float:MinX, Float:MinY, Float:MaxX, Float:MaxY, Float:MinZ = -1.0, Float:MaxZ = -1.0) //Gangzones
  138.     return CreateArea(MinX, MaxX, MinY, MaxY, MinZ, MaxZ);
  139.  
  140. stock CreateArea3(Float:MaxX, Float:MinX, Float:MaxY, Float:MinY, Float:MinZ = -1.0, Float:MaxZ = -1.0) //World Bounds
  141.     return CreateArea(MinX, MaxX, MinY, MaxY, MinZ, MaxZ);
  142.  
  143. stock DeleteArea(areaid)
  144. {
  145.     if(!IsAreaCreated(areaid)) return ERROR_NOAREA;
  146.     AreaCount--;
  147.     AreaInfo[areaid][aPosMinX] = 0.0;
  148.     AreaInfo[areaid][aPosMaxX] = 0.0;
  149.     AreaInfo[areaid][aPosMinY] = 0.0;
  150.     AreaInfo[areaid][aPosMaxY] = 0.0;
  151.     AreaInfo[areaid][aPosMinZ] = 0.0;
  152.     AreaInfo[areaid][aPosMaxZ] = 0.0;
  153.     AreaInfo[areaid][aZUsed] = false;
  154.     AreaInfo[areaid][aCreated] = false;
  155. }
  156.  
  157. stock UpdateArea(areaid, Float:MinX, Float:MaxX, Float:MinY, Float:MaxY, Float:MinZ = -1.0, Float:MaxZ = -1.0)
  158. {
  159.     new id = areaid;
  160.     if(!IsAreaCreated(id)) return ERROR_NOAREA;
  161.     AreaInfo[id][aPosMinX] = MinX;
  162.     AreaInfo[id][aPosMaxX] = MaxX;
  163.     AreaInfo[id][aPosMinY] = MinY;
  164.     AreaInfo[id][aPosMaxY] = MaxY;
  165.     AreaInfo[id][aPosMinZ] = MinZ;
  166.     AreaInfo[id][aPosMaxZ] = MaxZ;
  167.     if(AreaInfo[id][aPosMinZ] == -1.0 && AreaInfo[id][aPosMaxZ] == -1.0) AreaInfo[id][aZUsed] = false;
  168. }
  169.  
  170. stock UpdateArea2(areaid, Float:MinX, Float:MinY, Float:MaxX, Float:MaxY, Float:MinZ = -1.0, Float:MaxZ = -1.0)
  171.     return UpdateArea(areaid, MinX, MaxX, MinY, MaxY, MinZ, MaxZ);
  172.    
  173. stock UpdateArea3(areaid, Float:MaxX, Float:MinX, Float:MaxY, Float:MinY, Float:MinZ = -1.0, Float:MaxZ = -1.0)
  174.     return UpdateArea(areaid, MinX, MaxX, MinY, MaxY, MinZ, MaxZ);
  175.  
  176. stock GetAreaInfo(areaid, &Float:MinX, &Float:MaxX, &Float:MinY, &Float:MaxY, &Float:MinZ, &Float:MaxZ)
  177. {
  178.     if(!IsAreaCreated(areaid)) return ERROR_NOAREA;
  179.     MinX = AreaInfo[areaid][aPosMinX];
  180.     MaxX = AreaInfo[areaid][aPosMaxX];
  181.     MinY = AreaInfo[areaid][aPosMinY];
  182.     MaxY = AreaInfo[areaid][aPosMaxY];
  183.     MaxZ = AreaInfo[areaid][aPosMaxZ];
  184.     return 1;
  185. }
Add Comment
Please, Sign In to add comment