ConfigurableWind.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSimulator Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using System;
  28. using System.Collections.Generic;
  29. using System.Reflection;
  30. using log4net;
  31. using OpenMetaverse;
  32. using Mono.Addins;
  33. using OpenSim.Region.Framework.Interfaces;
  34. using OpenSim.Region.CoreModules.World.Wind;
  35. namespace OpenSim.Region.CoreModules.World.Wind.Plugins
  36. {
  37. [Extension(Path = "/OpenSim/WindModule", NodeName = "WindModel", Id = "ConfigurableWind")]
  38. class ConfigurableWind : Mono.Addins.TypeExtensionNode, IWindModelPlugin
  39. {
  40. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  41. private Vector2[] m_windSpeeds = new Vector2[16 * 16];
  42. //private Random m_rndnums = new Random(Environment.TickCount);
  43. private float m_avgStrength = 5.0f; // Average magnitude of the wind vector
  44. private float m_avgDirection = 0.0f; // Average direction of the wind in degrees
  45. private float m_varStrength = 5.0f; // Max Strength Variance
  46. private float m_varDirection = 30.0f;// Max Direction Variance
  47. private float m_rateChange = 1.0f; //
  48. private Vector2 m_curPredominateWind = new Vector2();
  49. #region IPlugin Members
  50. public string Version
  51. {
  52. get { return "1.0.0.0"; }
  53. }
  54. public string Name
  55. {
  56. get { return "ConfigurableWind"; }
  57. }
  58. public void Initialise()
  59. {
  60. }
  61. #endregion
  62. #region IDisposable Members
  63. public void Dispose()
  64. {
  65. m_windSpeeds = null;
  66. }
  67. #endregion
  68. #region IWindModelPlugin Members
  69. public void WindConfig(OpenSim.Region.Framework.Scenes.Scene scene, Nini.Config.IConfig windConfig)
  70. {
  71. if (windConfig != null)
  72. {
  73. // Uses strength value if avg_strength not specified
  74. m_avgStrength = windConfig.GetFloat("strength", 5.0F);
  75. m_avgStrength = windConfig.GetFloat("avg_strength", 5.0F);
  76. m_avgDirection = windConfig.GetFloat("avg_direction", 0.0F);
  77. m_varStrength = windConfig.GetFloat("var_strength", 5.0F);
  78. m_varDirection = windConfig.GetFloat("var_direction", 30.0F);
  79. m_rateChange = windConfig.GetFloat("rate_change", 1.0F);
  80. LogSettings();
  81. }
  82. }
  83. public void WindUpdate(uint frame)
  84. {
  85. double avgAng = m_avgDirection * (Math.PI/180.0f);
  86. double varDir = m_varDirection * (Math.PI/180.0f);
  87. // Prevailing wind algorithm
  88. // Inspired by Kanker Greenacre
  89. // TODO:
  90. // * This should probably be based on in-world time.
  91. // * should probably move all these local variables to class members and constants
  92. double time = DateTime.Now.TimeOfDay.Seconds / 86400.0f;
  93. double theta = time * (2 * Math.PI) * m_rateChange;
  94. double offset = Math.Sin(theta) * Math.Sin(theta*2) * Math.Sin(theta*9) * Math.Cos(theta*4);
  95. double windDir = avgAng + (varDir * offset);
  96. offset = Math.Sin(theta) * Math.Sin(theta*4) + (Math.Sin(theta*13) / 3);
  97. double windSpeed = m_avgStrength + (m_varStrength * offset);
  98. if (windSpeed<0)
  99. windSpeed=0;
  100. m_curPredominateWind.X = (float)Math.Cos(windDir);
  101. m_curPredominateWind.Y = (float)Math.Sin(windDir);
  102. m_curPredominateWind.Normalize();
  103. m_curPredominateWind.X *= (float)windSpeed;
  104. m_curPredominateWind.Y *= (float)windSpeed;
  105. for (int y = 0; y < 16; y++)
  106. {
  107. for (int x = 0; x < 16; x++)
  108. {
  109. m_windSpeeds[y * 16 + x] = m_curPredominateWind;
  110. }
  111. }
  112. }
  113. public Vector3 WindSpeed(float fX, float fY, float fZ)
  114. {
  115. return new Vector3(m_curPredominateWind, 0.0f);
  116. }
  117. public Vector2[] WindLLClientArray()
  118. {
  119. return m_windSpeeds;
  120. }
  121. public string Description
  122. {
  123. get
  124. {
  125. return "Provides a predominate wind direction that can change within configured variances for direction and speed.";
  126. }
  127. }
  128. public System.Collections.Generic.Dictionary<string, string> WindParams()
  129. {
  130. Dictionary<string, string> Params = new Dictionary<string, string>();
  131. Params.Add("avgStrength", "average wind strength");
  132. Params.Add("avgDirection", "average wind direction in degrees");
  133. Params.Add("varStrength", "allowable variance in wind strength");
  134. Params.Add("varDirection", "allowable variance in wind direction in +/- degrees");
  135. Params.Add("rateChange", "rate of change");
  136. return Params;
  137. }
  138. public void WindParamSet(string param, float value)
  139. {
  140. switch (param)
  141. {
  142. case "avgStrength":
  143. m_avgStrength = value;
  144. break;
  145. case "avgDirection":
  146. m_avgDirection = value;
  147. break;
  148. case "varStrength":
  149. m_varStrength = value;
  150. break;
  151. case "varDirection":
  152. m_varDirection = value;
  153. break;
  154. case "rateChange":
  155. m_rateChange = value;
  156. break;
  157. }
  158. }
  159. public float WindParamGet(string param)
  160. {
  161. switch (param)
  162. {
  163. case "avgStrength":
  164. return m_avgStrength;
  165. case "avgDirection":
  166. return m_avgDirection;
  167. case "varStrength":
  168. return m_varStrength;
  169. case "varDirection":
  170. return m_varDirection;
  171. case "rateChange":
  172. return m_rateChange;
  173. default:
  174. throw new Exception(String.Format("Unknown {0} parameter {1}", this.Name, param));
  175. }
  176. }
  177. #endregion
  178. private void LogSettings()
  179. {
  180. m_log.InfoFormat("[ConfigurableWind] Average Strength : {0}", m_avgStrength);
  181. m_log.InfoFormat("[ConfigurableWind] Average Direction : {0}", m_avgDirection);
  182. m_log.InfoFormat("[ConfigurableWind] Varience Strength : {0}", m_varStrength);
  183. m_log.InfoFormat("[ConfigurableWind] Varience Direction : {0}", m_varDirection);
  184. m_log.InfoFormat("[ConfigurableWind] Rate Change : {0}", m_rateChange);
  185. }
  186. #region IWindModelPlugin Members
  187. #endregion
  188. }
  189. }