Advertisement
Mauzen

Real Weather Include, updated link

Nov 3rd, 2011
2,201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 12.99 KB | None | 0 0
  1. /*
  2.     MWeather Include - Improved weather functions
  3.     (c) 2010 Mauzen
  4.     Do not remove this header
  5.     You are allowed to use this include in your script.
  6.     You are allowed to change the code for private use.
  7.     You are not allowed to re-release this script without my permission
  8.    
  9.     If you use this include, it would be nice to give me some credits
  10.     in any way. But you do not have to, because I cannot check it
  11.     anyways.
  12. */
  13.  
  14. #if defined _mweather_included
  15.   #endinput
  16. #endif
  17.  
  18. #define _mweather_included
  19. #pragma library mweather
  20.  
  21. #define WEATHER_UNKNOWN             0
  22. #define WEATHER_SUNNY               1
  23. #define WEATHER_MOSTLY_SUNNY        2
  24. #define WEATHER_PARTLY_CLOUDY       3
  25. #define WEATHER_MOSTLY_CLOUDY       4
  26. #define WEATHER_CLOUDY              5
  27. #define WEATHER_LIGHT_RAIN          6
  28. #define WEATHER_SHOWERS             7
  29. #define WEATHER_RAIN                8
  30. #define WEATHER_LIGHT_THUNDERSTORM  9
  31. #define WEATHER_THUNDERSTORM        10
  32. #define WEATHER_MIST                11
  33. #define WEATHER_SNOWRAIN            12
  34. #define WEATHER_LIGHT_SNOW          13
  35.  
  36. #include <a_http>
  37.  
  38. // Callbacks
  39. forward OnPlayerWeatherZoneChange(playerid, oldzone, newzone);
  40. forward OnWeatherZoneMoved(zoneid);
  41. forward OnRealWeatherUpdate(observerid, oldstate, newstate);
  42.  
  43. enum EnumRealWeather {
  44.     bool:ValidWeather,
  45.     UpdateTimer,
  46.     WUrl[192],
  47.     LastWState
  48. }
  49.  
  50. enum EnumWeatherZone {
  51.     bool:ValidWeatherZone,
  52.     Float:W_midx,
  53.     Float:W_midy,
  54.     Float:W_midz,
  55.     Float:W_length,
  56.     Float:W_width,
  57.     Float:W_height,
  58.     Float:W_xt,
  59.     Float:W_yt,
  60.     Float:W_zt,
  61.     Float:W_movespeed,
  62.     W_WeatherID,
  63.     bool:W_observer,
  64.     W_timer
  65. }
  66.  
  67. new RealWeather[16][EnumRealWeather];
  68. new WeatherZones[16][EnumWeatherZone];
  69.  
  70. new GlobalWeather = -1;
  71. new PlayerWeather[MAX_PLAYERS];
  72. new GlobalNormalWeather = -1;
  73. new PlayerNormalWeather[MAX_PLAYERS];
  74. new PlayerZone[MAX_PLAYERS];
  75.  
  76. MWeather_OnGameModeInit()
  77. {
  78.     for(new i = 0; i < MAX_PLAYERS; i ++)
  79.     {
  80.         PlayerWeather[i] = -1;
  81.         PlayerNormalWeather[i] = -1;
  82.         PlayerZone[i] = -1;
  83.     }
  84. }
  85.  
  86. // -----------------------------------------------------------------------------
  87. // -------------------------- Weather zones ------------------------------------
  88.  
  89. stock AddWeatherZone(Float:x, Float:y, Float:z, Float:length, Float:width, Float:height, weatherid, bool:observer)
  90. {
  91.     new wid = GetFreeWeatherZone();
  92.     if(wid == -1) return -1;
  93.     if(W_observer && !RealWeather[weatherid][ValidWeather]) return -1;
  94.     WeatherZones[wid][W_midx] = x;
  95.     WeatherZones[wid][W_midy] = y;
  96.     WeatherZones[wid][W_midz] = z;
  97.     WeatherZones[wid][W_length] = length;
  98.     WeatherZones[wid][W_width] = width;
  99.     WeatherZones[wid][W_height] = height;
  100.     WeatherZones[wid][W_WeatherID] = weatherid;
  101.     WeatherZones[wid][W_observer] = observer;
  102.     WeatherZones[wid][W_timer] = SetTimerEx("WeatherZoneUpdater", 2000, 1, "i", wid);
  103.     WeatherZones[wid][ValidWeatherZone] = true;
  104.     return wid;
  105. }
  106.  
  107. stock MoveWeatherZone(zoneid, Float:tox, Float:toy, Float:toz, Float:speed)
  108. {
  109.     if(!WeatherZones[zoneid][ValidWeatherZone]) return 0;
  110.     WeatherZones[zoneid][W_xt] = tox;
  111.     WeatherZones[zoneid][W_yt] = toy;
  112.     WeatherZones[zoneid][W_zt] = toz;
  113.     WeatherZones[zoneid][W_movespeed] = speed;
  114.     return 1;
  115. }
  116. stock StopWeatherZone(zoneid)
  117. {
  118.     if(!WeatherZones[zoneid][ValidWeatherZone]) return 0;
  119.     WeatherZones[zoneid][W_movespeed] = 0.0;
  120.     return 1;
  121. }
  122.  
  123.  
  124. // -----------------------------------------------------------------------------
  125. // --------------------------- RealWeather -------------------------------------
  126.  
  127. stock AddWeatherObserver(url[], updatetime)
  128. {
  129.     new wid = GetFreeRealWeather();
  130.     if(wid == -1) return -1;
  131.     format(RealWeather[wid][WUrl], 192, "www.mauzen.net/utils/a.php?URL=%s", url);
  132.     RealWeather[wid][UpdateTimer] = SetTimerEx("WeatherUpdater", updatetime, 1, "i", wid);
  133.     RealWeather[wid][LastWState] = WEATHER_UNKNOWN;
  134.     RealWeather[wid][ValidWeather] = true;
  135.     WeatherUpdater(wid);
  136.     return wid;
  137. }
  138. stock RemoveWeatherObserver(observerid)
  139. {
  140.     if(!RealWeather[observerid][ValidWeather]) return false;
  141.     RealWeather[observerid][ValidWeather] = false;
  142.     KillTimer(RealWeather[observerid][UpdateTimer]);
  143.     return true;
  144. }
  145.  
  146. // -----------------------------------------------------------------------------
  147. // ------------------------- Sets and Gets -------------------------------------
  148.  
  149. stock SetRealWeatherEx(observerid)
  150. {
  151.     if(!RealWeather[observerid][ValidWeather]) return;
  152.     GlobalWeather = observerid;
  153.     GlobalNormalWeather = -1;
  154.     UpdateWeather();
  155. }
  156. stock GetRealWeatherEx()
  157. {
  158.     return GlobalWeather;
  159. }
  160. stock SetWeatherEx(weatherid)
  161. {
  162.     GlobalNormalWeather = weatherid;
  163.     GlobalWeather = -1;
  164.     UpdateWeather();
  165. }
  166. stock GetWeatherEx()
  167. {
  168.     return GlobalNormalWeather;
  169. }
  170. stock SetPlayerRealWeatherEx(playerid, observerid)
  171. {
  172.     if(observerid != -1 && !RealWeather[observerid][ValidWeather]) return;
  173.     PlayerWeather[playerid] = observerid;
  174.     PlayerNormalWeather[playerid] = -1;
  175.     UpdateWeather();
  176. }
  177. stock GetRealPlayerWeatherEx(playerid)
  178. {
  179.     return PlayerWeather[playerid];
  180. }
  181. stock SetPlayerWeatherEx(playerid, weatherid)
  182. {
  183.     PlayerNormalWeather[playerid] = weatherid;
  184.     PlayerWeather[playerid] = -1;
  185.     UpdateWeather();
  186. }
  187. stock GetPlayerWeatherEx(playerid)
  188. {
  189.     return PlayerNormalWeather[playerid];
  190. }
  191.  
  192. // -----------------------------------------------------------------------------
  193. // ------------------------ Internally used ------------------------------------
  194.  
  195. //     ----------------------- RealWeather ---------------------------------
  196.  
  197. stock GetFreeRealWeather()
  198. {
  199.     for(new i = 0; i < sizeof (RealWeather); i ++)
  200.     {
  201.         if(!RealWeather[i][ValidWeather]) return i;
  202.     }
  203.     return -1;
  204. }
  205.  
  206. forward WeatherUpdater(observerid);
  207. public WeatherUpdater(observerid)
  208. {
  209.     if(!RealWeather[observerid][ValidWeather])
  210.     {
  211.         KillTimer(RealWeather[observerid][UpdateTimer]);
  212.         return;
  213.     }
  214.     HTTP(observerid + 950, HTTP_GET, RealWeather[observerid][WUrl], "", "ObserverUpdate");
  215. }
  216.  
  217. forward ObserverUpdate(index, response_code, data[]);
  218. public ObserverUpdate(index, response_code, data[])
  219. {
  220.     new oldstate = RealWeather[index - 950][LastWState];
  221.     if(response_code != 200)
  222.     {
  223.         RealWeather[index - 950][LastWState] = WEATHER_UNKNOWN;
  224.     } else
  225.     {
  226.        
  227.         if(!strcmp(data, "Sunny")) RealWeather[index - 950][LastWState] = WEATHER_SUNNY;
  228.         else if(!strcmp(data, "Fair") || !strcmp(data, "Mostly Sunny")) RealWeather[index - 950][LastWState] = WEATHER_MOSTLY_SUNNY;
  229.         else if(!strcmp(data, "Partly Cloudy")) RealWeather[index - 950][LastWState] = WEATHER_PARTLY_CLOUDY;
  230.         else if(!strcmp(data, "Mostly Cloudy")) RealWeather[index - 950][LastWState] = WEATHER_MOSTLY_CLOUDY;
  231.         else if(!strcmp(data, "Cloudy")) RealWeather[index - 950][LastWState] = WEATHER_CLOUDY;
  232.         else if(!strcmp(data, "Light Rain")) RealWeather[index - 950][LastWState] = WEATHER_LIGHT_RAIN;
  233.         else if(!strcmp(data, "Showers") || !strcmp(data, "Scattered Showers")) RealWeather[index - 950][LastWState] = WEATHER_SHOWERS;
  234.         else if(!strcmp(data, "Rain")) RealWeather[index - 950][LastWState] = WEATHER_RAIN;
  235.         else if(!strcmp(data, "Mist") || !strcmp(data, "Fog") || !strcmp(data, "Haze")) RealWeather[index - 950][LastWState] = WEATHER_MIST;
  236.         else if(!strcmp(data, "Rain/Snow") || !strcmp(data, "Rain/Snow Showers")) RealWeather[index - 950][LastWState] = WEATHER_SNOWRAIN;
  237.         else if(!strcmp(data, "Light Snow")) RealWeather[index - 950][LastWState] = WEATHER_LIGHT_SNOW;
  238.         else if(!strcmp(data, "T-Rain") || !strcmp(data, "Scattered T-Storm")) RealWeather[index - 950][LastWState] = WEATHER_LIGHT_THUNDERSTORM;
  239.         else if(!strcmp(data, "T-Storm") || !strcmp(data, "Isolated T-Storm")) RealWeather[index - 950][LastWState] = WEATHER_THUNDERSTORM;
  240.         //printf("Catched weather: %d", RealWeather[index - 950][LastWState]);
  241.         UpdateWeather();
  242.     }
  243.     CallLocalFunction("OnRealWeatherUpdate", "iii", index - 950, oldstate, RealWeather[index - 950][LastWState]);
  244.     return;
  245.    
  246. }
  247.  
  248. UpdateWeather()
  249. {
  250.     if(GlobalWeather != -1)
  251.     {
  252.         SetWeather(GetWeatherIDFromState(RealWeather[GlobalWeather][LastWState]));
  253.     }
  254.     if(GlobalNormalWeather != -1)
  255.     {
  256.         SetWeather(GlobalNormalWeather);
  257.     }
  258.     for(new i = 0; i < MAX_PLAYERS; i ++)
  259.     {
  260.         if(PlayerWeather[i] != -1) SetPlayerWeather(i, GetWeatherIDFromState(RealWeather[PlayerWeather[i]][LastWState]));
  261.         if(PlayerNormalWeather[i] != -1) SetPlayerWeather(i, PlayerNormalWeather[i]);
  262.     }
  263. }
  264.  
  265. stock GetWeatherIDFromState(wstate)
  266. {
  267.     switch(wstate)
  268.     {
  269.         case WEATHER_UNKNOWN: return 1;
  270.         case WEATHER_SUNNY: return 1;
  271.         case WEATHER_PARTLY_CLOUDY: return 10;
  272.         case WEATHER_MOSTLY_CLOUDY: return 4;
  273.         case WEATHER_CLOUDY: return 7;
  274.         case WEATHER_LIGHT_RAIN: return 16;
  275.         case WEATHER_SHOWERS: return 16;
  276.         case WEATHER_LIGHT_THUNDERSTORM: return 16;
  277.         case WEATHER_SNOWRAIN: return 16;
  278.         case WEATHER_LIGHT_SNOW: return 16;
  279.         case WEATHER_RAIN: return 8;
  280.         case WEATHER_THUNDERSTORM: return 8;
  281.         case WEATHER_MIST: return 9;
  282.     }
  283.     return 1;
  284. }
  285.  
  286. //     ------------------------ Weather zones ------------------------------
  287.  
  288. stock GetFreeWeatherZone()
  289. {
  290.     for(new i = 0; i < sizeof (WeatherZones); i ++)
  291.     {
  292.         if(!WeatherZones[i][ValidWeatherZone]) return i;
  293.     }
  294.     return -1;
  295. }
  296.  
  297. new tez;
  298.  
  299. forward WeatherZoneUpdater(zoneid);
  300. public WeatherZoneUpdater(zoneid)
  301. {
  302.     if(!WeatherZones[zoneid][ValidWeatherZone])
  303.     {
  304.         KillTimer(WeatherZones[zoneid][W_timer]);
  305.         return;
  306.     }
  307.     if(WeatherZones[zoneid][W_movespeed] > 0.0)
  308.     {
  309.         new Float:xd, Float:yd, Float:zd;
  310.         new Float:vx, Float:vy, Float:vz;
  311.         xd = WeatherZones[zoneid][W_midx] - WeatherZones[zoneid][W_xt];
  312.         yd = WeatherZones[zoneid][W_midy] - WeatherZones[zoneid][W_yt];
  313.         zd = WeatherZones[zoneid][W_midz] - WeatherZones[zoneid][W_zt];
  314.        
  315.         /*vx = floatsqroot( (WeatherZones[zoneid][W_movespeed] * WeatherZones[zoneid][W_movespeed]) / (((yd * yd + zd * zd) / xd * xd) + 1) );
  316.         if(xd > 0) vx *= -1;
  317.         vy = (yd / xd) * vx;
  318.         vz = (zd / xd) * vx;*/
  319.        
  320.         vx = WeatherZones[zoneid][W_movespeed] * (xd / (xd + yd + zd)) * 2.0;
  321.         vy = WeatherZones[zoneid][W_movespeed] * (yd / (xd + yd + zd)) * 2.0;
  322.         vz = WeatherZones[zoneid][W_movespeed] * (zd / (xd + yd + zd)) * 2.0;
  323.         if(xd > 0.0) vx *= -1;
  324.         if(yd > 0.0) vy *= -1;
  325.         if(zd > 0.0) vz *= -1;
  326.  
  327.         if((vx > 0 && WeatherZones[zoneid][W_midx] + vx >= WeatherZones[zoneid][W_xt]) || (vx < 0 && WeatherZones[zoneid][W_midx] + vx <= WeatherZones[zoneid][W_xt]))
  328.         {
  329.             WeatherZones[zoneid][W_midx] = WeatherZones[zoneid][W_xt];
  330.             WeatherZones[zoneid][W_midy] = WeatherZones[zoneid][W_yt];
  331.             WeatherZones[zoneid][W_midz] = WeatherZones[zoneid][W_zt];
  332.             WeatherZones[zoneid][W_movespeed] = 0.0;
  333.             CallLocalFunction("OnWeatherZoneMoved", "i", zoneid);
  334.         } else {
  335.             WeatherZones[zoneid][W_midx] = WeatherZones[zoneid][W_midx] + vx;
  336.             WeatherZones[zoneid][W_midy] = WeatherZones[zoneid][W_midy] + vy;
  337.             WeatherZones[zoneid][W_midz] = WeatherZones[zoneid][W_midz] + vz;
  338.         }
  339.        
  340.         //printf("Zone pos: %.1f %.1f %.1f / %.2f %.2f %.2f", WeatherZones[zoneid][W_midx], WeatherZones[zoneid][W_midy], WeatherZones[zoneid][W_midz], vx, vy, vz);
  341.  
  342.         GangZoneDestroy(tez);
  343.         tez = GangZoneCreate(WeatherZones[zoneid][W_midx] - WeatherZones[zoneid][W_length] / 2, WeatherZones[zoneid][W_midy] - WeatherZones[zoneid][W_width] / 2, (WeatherZones[zoneid][W_midx] - WeatherZones[zoneid][W_length] / 2) + WeatherZones[zoneid][W_length], (WeatherZones[zoneid][W_midy] - WeatherZones[zoneid][W_width] / 2) + WeatherZones[zoneid][W_width]);
  344.         GangZoneShowForAll(tez, 0xFF000099);
  345.        
  346.     }
  347.     for(new i = 0; i < MAX_PLAYERS; i ++)
  348.     {
  349.         if(!IsPlayerConnected(i)) continue;
  350.         if((WeatherZones[zoneid][W_length] < 0.0 && IsPlayerInRangeOfPoint(i, WeatherZones[zoneid][W_length] * -1, WeatherZones[zoneid][W_midx], WeatherZones[zoneid][W_midy], WeatherZones[zoneid][W_midz])) ||
  351.             (WeatherZones[zoneid][W_length] > 0.0 && MIsPlayerInArea(i, WeatherZones[zoneid][W_midx], WeatherZones[zoneid][W_midy], WeatherZones[zoneid][W_midz],
  352.             WeatherZones[zoneid][W_length], WeatherZones[zoneid][W_width],  WeatherZones[zoneid][W_height])))
  353.         {
  354.             if(WeatherZones[zoneid][W_observer]) SetPlayerRealWeatherEx(i, WeatherZones[zoneid][W_WeatherID]);
  355.                 else SetPlayerWeatherEx(i, WeatherZones[zoneid][W_WeatherID]);
  356.             if(zoneid != PlayerZone[i])
  357.             {
  358.                 CallLocalFunction("OnPlayerWeatherZoneChange", "iii", i, PlayerZone[i], zoneid);
  359.                 PlayerZone[i] = zoneid;
  360.             }
  361.         } else
  362.         {
  363.             if(PlayerZone[i] != -1) {
  364.                 CallLocalFunction("OnPlayerWeatherZoneChange", "iii", i, PlayerZone[i], -1);
  365.                 PlayerZone[i] = -1;
  366.                 SetPlayerWeatherEx(i, -1);
  367.                 SetPlayerRealWeatherEx(i, -1);
  368.             }
  369.         }
  370.        
  371.     }
  372.     UpdateWeather();
  373. }
  374.  
  375. //     -------------------- General stocks ---------------------------------
  376.  
  377. stock MIsPlayerInArea(playerid, Float:x, Float:y, Float:z, Float:l, Float:w, Float:h)
  378. {
  379.     new Float:px, Float:py, Float:pz;
  380.     GetPlayerPos(playerid, px, py, pz);
  381.     x -= l / 2;
  382.     y -= w / 2;
  383.     z -= h / 2;
  384.     if(px >= x && px <= x + l)
  385.         if(py >= y && py <= y + w)
  386.             if(pz >= z && pz <= z + h)
  387.                 return true;
  388.     return false;
  389. }
  390.  
  391.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement