Advertisement
Guest User

weather.c file from dayz

a guest
Dec 17th, 2018
3,799
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.44 KB | None | 0 0
  1. /*!
  2. All classes related to game weather
  3. */
  4.  
  5.  
  6. //-----------------------------------------------------------------------------
  7. /*!
  8. Weather phenomenon type
  9. */
  10. enum EWeatherPhenomenon
  11. {
  12. OVERCAST,
  13. FOG,
  14. RAIN
  15. };
  16.  
  17.  
  18. //-----------------------------------------------------------------------------
  19. /*!
  20. Weather phenomenon
  21. */
  22. class WeatherPhenomenon
  23. {
  24. //! Returns type of this phenomenon.
  25. proto native EWeatherPhenomenon GetType();
  26.  
  27. //! Returns actual value of phenomenon in range <0, 1>.
  28. proto native float GetActual();
  29.  
  30. //! Returns a forecast value the phenomenon is heading towards.
  31. proto native float GetForecast();
  32.  
  33. /*!
  34. \brief Sets the forecast.
  35. \param forecast Desired forecast value that should be met in given time.
  36. \param time A time of the next change (how long it takes in seconds to interpolate to given value).
  37. \param minDuration A minimal time in seconds the change will last.
  38. */
  39. proto native void Set( float forecast, float time = 0, float minDuration = 0 );
  40.  
  41. //! Reads the time in seconds when the next forecast will be computed.
  42. proto native float GetNextChange();
  43. //! Sets the time in seconds when the next forecast will be computed.
  44. proto native void SetNextChange( float time );
  45.  
  46. /*!
  47. \brief Reads limits of this phenomenon.
  48. \param fnMin Function minimum (in range <0, 1>).
  49. \param fnMax Function maximum (in range <0, 1>).
  50. */
  51. proto external void GetLimits( out float fnMin, out float fnMax );
  52. /*!
  53. \brief Sets limits of this phenomenon.
  54.  
  55. Actual value of this phenomenon will be always held in range <fnMin, fnMax>.
  56.  
  57. Default values are:
  58. fnMin = 0
  59. fnMax = 1
  60.  
  61. \param fnMin Function minimum (in range <0, 1>).
  62. \param fnMax Function maximum (in range <0, 1>).
  63. */
  64. proto native void SetLimits( float fnMin, float fnMax );
  65.  
  66. /*!
  67. \brief Reads limits of change when forecast is computed.
  68. \param fcMin Forecast change minimum (in range <0, 1>).
  69. \param fcMax Forecast change maximum (in range <0, 1>).
  70. */
  71. proto external void GetForecastChangeLimits( out float fcMin, out float fcMax );
  72. /*!
  73. \brief Sets limits of change when forecast is computed.
  74.  
  75. These limits govern how much the forecast value can change
  76. when it is computed by weather controller.
  77.  
  78. Default values are:
  79. fcMin = 0
  80. fcMax = 1
  81.  
  82. \param fcMin Forecast change minimum (in range <0, 1>).
  83. \param fcMax Forecast change maximum (in range <0, 1>).
  84. */
  85. proto native void SetForecastChangeLimits( float fcMin, float fcMax );
  86.  
  87. /*!
  88. \brief Reads time range in which next forecast can be computed.
  89. \param ftMin Minimal number of seconds.
  90. \param ftMax Maximal number of seconds.
  91. */
  92. proto external void GetForecastTimeLimits( out float ftMin, out float ftMax );
  93. /*!
  94. \brief Sets time range in which next forecast can be computed.
  95.  
  96. Default values are:
  97. ftMin = 300 (5 minutes)
  98. ftMax = 3600 (1 hour)
  99.  
  100. \param ftMin Minimal number of seconds.
  101. \param ftMax Maximal number of seconds.
  102. */
  103. proto native void SetForecastTimeLimits( float ftMin, float ftMax );
  104.  
  105. /*!
  106. \brief Is called every time the Phenomenon computes new forecast.
  107.  
  108. \note Called on server only.
  109.  
  110. \param type Type of this phenomenon.
  111. \param change Computed change of forecast value.
  112. \param time Seconds when the next forecast will be computed.
  113. \return True when script modifies state of this phenomenon false otherwise.
  114. */
  115. bool OnBeforeChange( EWeatherPhenomenon type, float change, float time )
  116. {
  117. Weather weather = g_Game.GetWeather();
  118.  
  119. if ( weather.GetMissionWeather() )
  120. return false;
  121.  
  122. float rainMin;
  123. float rainMax;
  124. float rainTime;
  125. float rainTimeDur;
  126.  
  127. float rainActual = weather.GetRain().GetActual();
  128.  
  129. float overcastActual = weather.GetOvercast().GetActual();
  130. float overcastForecast = weather.GetOvercast().GetForecast();
  131. float overcastNextChange = weather.GetOvercast().GetNextChange();
  132.  
  133. float overcastInverted = 1.0 - overcastActual;
  134.  
  135. weather.SetRainThresholds( 0.75, 1.0, 30 );
  136. weather.SetWindMaximumSpeed( 30 );
  137.  
  138. switch( type )
  139. {
  140. case EWeatherPhenomenon.OVERCAST:
  141. Print( "COMPUTING NEW OVERCAST" );
  142.  
  143. float rangeMin = 0.15;
  144. float rangeHigh = 0.85;
  145.  
  146. float timeChangeLow = 1800.0;
  147. float timeChangeMid = 1.5 * timeChangeLow;
  148. float timeChangeHigh = 2 * timeChangeLow;
  149.  
  150. float timeRange = timeChangeLow;
  151.  
  152. float overcastMid = 0.5;
  153. float overcastRange = 0.1;
  154.  
  155. float rand = Math.RandomFloatInclusive( 0.0, 1.0 );
  156.  
  157. if ( rand > rangeMin && rand < rangeHigh )
  158. {
  159. overcastMid = 0.5;
  160. overcastRange = 0.2;
  161.  
  162. weather.SetWindFunctionParams( 0.1, 0.4, 30 );
  163.  
  164. //m_clrCounter += 0.1;
  165. //m_badCounter -= 0.1;
  166. }
  167. else if ( rand <= rangeMin )
  168. {
  169. overcastMid = 0.25;
  170. overcastRange = 0.12;
  171.  
  172. weather.SetWindFunctionParams( 0.02, 0.2, 30 );
  173.  
  174. if ( GetActual() > rangeHigh )
  175. {
  176. timeRange = timeChangeHigh;
  177. weather.SetWindFunctionParams( 0.2, 0.6, 40 );
  178. }
  179. else if ( GetActual() > rangeMin )
  180. {
  181. timeRange = timeChangeMid;
  182. weather.SetWindFunctionParams( 0.1, 0.4, 30 );
  183. }
  184. else
  185. {
  186. timeRange = timeChangeLow;
  187. weather.SetWindFunctionParams( 0.02, 0.1, 20 );
  188. }
  189.  
  190. //m_clrCounter -= 0.1;
  191. //m_badCounter -= 0.1;
  192. }
  193. else
  194. {
  195. overcastMid = 0.8;
  196. overcastRange = 0.2;
  197.  
  198. if ( GetActual() < rangeMin )
  199. {
  200. timeRange = timeChangeHigh;
  201. weather.SetWindFunctionParams( 0.2, 0.8, 50 );
  202. }
  203. else if ( GetActual() > rangeHigh )
  204. {
  205. timeRange = timeChangeLow;
  206. float maxWind = Math.RandomFloatInclusive( 0.7, 1.0 );
  207. weather.SetWindFunctionParams( 0.7, maxWind, 50 );
  208. }
  209. else
  210. {
  211. timeRange = timeChangeMid;
  212. weather.SetWindFunctionParams( 0.4, 0.8, 50 );
  213. }
  214.  
  215. //m_clrCounter += 0.1;
  216. //m_badCounter += 0.1;
  217. }
  218.  
  219. float nextOvercast = Math.RandomFloatInclusive( overcastMid - overcastRange, overcastMid + overcastRange );
  220. Set( nextOvercast, timeRange, 0 );
  221. //SetNextChange( timeRange );
  222.  
  223. Print( "Actual " + "( " + g_Game.GetDayTime() + " )" + " overcast: " + GetActual() );
  224. Print( "Actual " + "( " + g_Game.GetDayTime() + " )" + " rain: " + rainActual );
  225. Print( "-----------------------------------------------------------" );
  226.  
  227. return true;
  228.  
  229. case EWeatherPhenomenon.FOG:
  230. Print( "COMPUTING NEW FOG" );
  231. Print( "Actual " + "( " + g_Game.GetDayTime() + " )" + " fog: " + GetActual() );
  232. Print( "-----------------------------------------------------------" );
  233.  
  234. float fogMin = 0.0;
  235. float fogMax = 0.15;
  236. float fogTime = 1800.0;
  237.  
  238. float fogyMorning = Math.RandomFloatInclusive( 0.0, 1.0 );
  239.  
  240. if ( fogyMorning > 0.85 )
  241. {
  242. if ( (g_Game.GetDayTime() > 4 && g_Game.GetDayTime() < 7 ) )
  243. {
  244. fogMin = 0.10;
  245. fogMax = 0.35;
  246. fogTime = 300;
  247. }
  248. }
  249.  
  250. if ( overcastActual < 0.3 )
  251. {
  252. fogMin = 0.0;
  253. fogMax = 0.08;
  254. fogTime = 900.0;
  255. }
  256.  
  257. Set( Math.RandomFloatInclusive( fogMin, fogMax ), fogTime, 0);
  258. return true;
  259.  
  260. case EWeatherPhenomenon.RAIN:
  261. Print( "COMPUTING NEW RAIN" );
  262.  
  263. if ( GetActual() > 0.1 && overcastActual <= 0.95 )
  264. {
  265. rainMin = 0.0;
  266. rainMax = 0.05;
  267. rainTime = Math.RandomFloatInclusive( overcastActual*20, overcastActual*60 ); // *10 converting to seconds *2-12 muting rain
  268. rainTimeDur = Math.RandomFloatInclusive( overcastInverted*1200, overcastInverted*1800 );
  269. }
  270. else if ( overcastActual > 0.8 )
  271. {
  272. rainMin = 0.1;
  273. rainMax = 0.4;
  274. rainTime = Math.RandomFloatInclusive( 120, 240 );
  275. rainTimeDur = Math.RandomFloatInclusive( 30, 60 );
  276. }
  277. else if ( overcastActual > 0.9 )
  278. {
  279. rainMin = 0.4;
  280. rainMax = 0.7;
  281. rainTime = Math.RandomFloatInclusive( 60, 120 );
  282. rainTimeDur = Math.RandomFloatInclusive( 60, 120 );
  283. }
  284. else if ( overcastActual > 0.95 )
  285. {
  286. rainMin = 0.5;
  287. rainMax = 0.8;
  288. rainTime = Math.RandomFloatInclusive( 10, 60 );
  289. rainTimeDur = Math.RandomFloatInclusive( 120, 240 );
  290. }
  291. else
  292. {
  293. rainMin = 0.0;
  294. rainMax = 0.3;
  295. rainTime = Math.RandomFloatInclusive( 240, 300 );
  296. rainTimeDur = Math.RandomFloatInclusive( 90, 120 );
  297. }
  298.  
  299. SetLimits(rainMin, rainMax);
  300. Set( Math.RandomFloatInclusive(rainMin, rainMax), rainTime, rainTimeDur );
  301. /*
  302. if ( overcastNextChange < rainTime + rainTimeDur )
  303. SetNextChange( overcastNextChange );
  304. else
  305. */
  306. //SetNextChange(rainTime + rainTimeDur);
  307.  
  308.  
  309. Print( "Actual " + "( " + g_Game.GetDayTime() + " )" + " rain: " + GetActual() );
  310. Print( "-----------------------------------------------------------" );
  311.  
  312. return true;
  313. }
  314.  
  315. return false;
  316. }
  317. };
  318.  
  319.  
  320. typedef WeatherPhenomenon Overcast;
  321. typedef WeatherPhenomenon Fog;
  322. typedef WeatherPhenomenon Rain;
  323.  
  324.  
  325.  
  326.  
  327. //-----------------------------------------------------------------------------
  328. /*!
  329. Weather controller
  330. */
  331. class Weather
  332. {
  333. protected bool m_missionWeather;
  334.  
  335. void Weather()
  336. {
  337. m_missionWeather = false;
  338. }
  339.  
  340. //! Returns actual time from start of a server (how many seconds elapsed from server start).
  341. proto native float GetTime();
  342.  
  343. //! Returns an overcast phenomenon object.
  344. proto native Overcast GetOvercast();
  345.  
  346. //! Returns a fog phenomenon object.
  347. proto native Fog GetFog();
  348.  
  349. //! Returns a rain phenomenon object.
  350. proto native Rain GetRain();
  351.  
  352. /*!
  353. \brief Sets the thunderstorm properties.
  354. \param density A value in <0, 1> range where 0 means no thunderstorms at all
  355. and 1 means thunderstorm every time it gets cloudy.
  356. \param threshold The overcast value that must be exceeded so that lightning can appear.
  357. \param timeOut A minimal time in seconds between lightning during thunderstorm.
  358. */
  359. proto native void SetStorm( float density, float threshold, float timeOut );
  360.  
  361. //! Returns wind vector (direction and speed as length of the vector).
  362. proto native vector GetWind();
  363. //! Sets the wind vector (direction and speed as length of the vector).
  364. proto native void SetWind( vector wind );
  365. /*!
  366. \brief Returns actual wind speed in metre per second.
  367. \note Wind is changing continuously in time, so the returned value may not stand for too long.
  368. */
  369. proto native float GetWindSpeed();
  370. /*!
  371. \brief Sets the actual wind speed in metre per second.
  372. \note Wind is changing continuously in time, so the returned value may not stand for too long.
  373. Minimum speed for wind is 0.1 m/s.
  374. */
  375. proto native void SetWindSpeed( float speed );
  376. /*!
  377. \brief Returns maximal wind speed in metre per second.
  378. \note By default this value is 10 m/s but it can be overridden in script on mission initialization.
  379. */
  380. proto native float GetWindMaximumSpeed();
  381. //! Sets the maximal wind speed in metre per second.
  382. proto native void SetWindMaximumSpeed( float maxSpeed );
  383. /*!
  384. \brief Reads function parameters that controls the wind behaviour (change in time).
  385. \param fnMin Function relative minimum (in range <0, 1>).
  386. \param fnMax Function relative maximum (in range <0, 1>).
  387. \param fnSpeed Controls speed of change of function value.
  388. */
  389. proto external void GetWindFunctionParams( out float fnMin, out float fnMax, out float fnSpeed );
  390. /*!
  391. \brief Sets function parameters that controls the wind behaviour (change in time).
  392. \param fnMin Function relative minimum (in range <0, 1>).
  393. \param fnMax Function relative maximum (in range <0, 1>).
  394. \param fnSpeed Controls speed of change of function value.
  395. */
  396. proto native void SetWindFunctionParams( float fnMin, float fnMax, float fnSpeed );
  397.  
  398. /*!
  399. \brief Sets overcast threshold values for rain phenomena.
  400.  
  401. Rain can start only if actual overcast value is in given range of <tMin, tMax>.
  402. If it's already raining and actual overcast value gets out of given range
  403. then rain will stop in given tTime seconds.
  404.  
  405. Default values are:
  406. tMin = 0.6
  407. tMax = 1
  408. tTime = 30
  409.  
  410. \param tMin Minimal overcast value (in range <0, 1>).
  411. \param tMax Maximal overcast value (in range <0, 1>).
  412. \param tTime Time in seconds when it stops raining.
  413. */
  414. proto native void SetRainThresholds( float tMin, float tMax, float tTime );
  415.  
  416. void MissionWeather( bool use )
  417. {
  418. m_missionWeather = use;
  419. }
  420.  
  421. bool GetMissionWeather()
  422. {
  423. return m_missionWeather;
  424. }
  425. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement