Kwarde

k_playerarea

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