OpenSim
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros
ConfigurableWind.cs
Go to the documentation of this file.
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 
28 using System;
29 using System.Collections.Generic;
30 using System.Reflection;
31 
32 using log4net;
33 using OpenMetaverse;
34 using Mono.Addins;
35 
36 using OpenSim.Region.Framework.Interfaces;
37 using OpenSim.Region.CoreModules.World.Wind;
38 
39 namespace OpenSim.Region.CoreModules.World.Wind.Plugins
40 {
41  [Extension(Path = "/OpenSim/WindModule", NodeName = "WindModel", Id = "ConfigurableWind")]
42  class ConfigurableWind : Mono.Addins.TypeExtensionNode, IWindModelPlugin
43  {
44  private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
45 
46  private Vector2[] m_windSpeeds = new Vector2[16 * 16];
47  //private Random m_rndnums = new Random(Environment.TickCount);
48 
49  private float m_avgStrength = 5.0f; // Average magnitude of the wind vector
50  private float m_avgDirection = 0.0f; // Average direction of the wind in degrees
51  private float m_varStrength = 5.0f; // Max Strength Variance
52  private float m_varDirection = 30.0f;// Max Direction Variance
53  private float m_rateChange = 1.0f; //
54 
55  private Vector2 m_curPredominateWind = new Vector2();
56 
57 
58 
59  #region IPlugin Members
60 
61  public string Version
62  {
63  get { return "1.0.0.0"; }
64  }
65 
66  public string Name
67  {
68  get { return "ConfigurableWind"; }
69  }
70 
71  public void Initialise()
72  {
73 
74  }
75 
76  #endregion
77 
78  #region IDisposable Members
79 
80  public void Dispose()
81  {
82  m_windSpeeds = null;
83  }
84 
85  #endregion
86 
87  #region IWindModelPlugin Members
88 
89  public void WindConfig(OpenSim.Region.Framework.Scenes.Scene scene, Nini.Config.IConfig windConfig)
90  {
91  if (windConfig != null)
92  {
93  // Uses strength value if avg_strength not specified
94  m_avgStrength = windConfig.GetFloat("strength", 5.0F);
95  m_avgStrength = windConfig.GetFloat("avg_strength", 5.0F);
96 
97  m_avgDirection = windConfig.GetFloat("avg_direction", 0.0F);
98  m_varStrength = windConfig.GetFloat("var_strength", 5.0F);
99  m_varDirection = windConfig.GetFloat("var_direction", 30.0F);
100  m_rateChange = windConfig.GetFloat("rate_change", 1.0F);
101 
102  LogSettings();
103  }
104  }
105 
106  public void WindUpdate(uint frame)
107  {
108  double avgAng = m_avgDirection * (Math.PI/180.0f);
109  double varDir = m_varDirection * (Math.PI/180.0f);
110 
111  // Prevailing wind algorithm
112  // Inspired by Kanker Greenacre
113 
114  // TODO:
115  // * This should probably be based on in-world time.
116  // * should probably move all these local variables to class members and constants
117  double time = DateTime.Now.TimeOfDay.Seconds / 86400.0f;
118 
119  double theta = time * (2 * Math.PI) * m_rateChange;
120 
121  double offset = Math.Sin(theta) * Math.Sin(theta*2) * Math.Sin(theta*9) * Math.Cos(theta*4);
122 
123  double windDir = avgAng + (varDir * offset);
124 
125  offset = Math.Sin(theta) * Math.Sin(theta*4) + (Math.Sin(theta*13) / 3);
126  double windSpeed = m_avgStrength + (m_varStrength * offset);
127 
128  if (windSpeed<0)
129  windSpeed=0;
130 
131 
132 
133  m_curPredominateWind.X = (float)Math.Cos(windDir);
134  m_curPredominateWind.Y = (float)Math.Sin(windDir);
135 
136  m_curPredominateWind.Normalize();
137  m_curPredominateWind.X *= (float)windSpeed;
138  m_curPredominateWind.Y *= (float)windSpeed;
139 
140  for (int y = 0; y < 16; y++)
141  {
142  for (int x = 0; x < 16; x++)
143  {
144  m_windSpeeds[y * 16 + x] = m_curPredominateWind;
145  }
146  }
147  }
148 
149  public Vector3 WindSpeed(float fX, float fY, float fZ)
150  {
151  return new Vector3(m_curPredominateWind, 0.0f);
152  }
153 
154  public Vector2[] WindLLClientArray()
155  {
156  return m_windSpeeds;
157  }
158 
159  public string Description
160  {
161  get
162  {
163  return "Provides a predominate wind direction that can change within configured variances for direction and speed.";
164  }
165  }
166 
167  public System.Collections.Generic.Dictionary<string, string> WindParams()
168  {
169  Dictionary<string, string> Params = new Dictionary<string, string>();
170 
171  Params.Add("avgStrength", "average wind strength");
172  Params.Add("avgDirection", "average wind direction in degrees");
173  Params.Add("varStrength", "allowable variance in wind strength");
174  Params.Add("varDirection", "allowable variance in wind direction in +/- degrees");
175  Params.Add("rateChange", "rate of change");
176 
177  return Params;
178  }
179 
180  public void WindParamSet(string param, float value)
181  {
182  switch (param)
183  {
184  case "avgStrength":
185  m_avgStrength = value;
186  break;
187  case "avgDirection":
188  m_avgDirection = value;
189  break;
190  case "varStrength":
191  m_varStrength = value;
192  break;
193  case "varDirection":
194  m_varDirection = value;
195  break;
196  case "rateChange":
197  m_rateChange = value;
198  break;
199  }
200  }
201 
202  public float WindParamGet(string param)
203  {
204  switch (param)
205  {
206  case "avgStrength":
207  return m_avgStrength;
208  case "avgDirection":
209  return m_avgDirection;
210  case "varStrength":
211  return m_varStrength;
212  case "varDirection":
213  return m_varDirection;
214  case "rateChange":
215  return m_rateChange;
216  default:
217  throw new Exception(String.Format("Unknown {0} parameter {1}", this.Name, param));
218 
219  }
220  }
221 
222 
223 
224  #endregion
225 
226 
227  private void LogSettings()
228  {
229  m_log.InfoFormat("[ConfigurableWind] Average Strength : {0}", m_avgStrength);
230  m_log.InfoFormat("[ConfigurableWind] Average Direction : {0}", m_avgDirection);
231  m_log.InfoFormat("[ConfigurableWind] Varience Strength : {0}", m_varStrength);
232  m_log.InfoFormat("[ConfigurableWind] Varience Direction : {0}", m_varDirection);
233  m_log.InfoFormat("[ConfigurableWind] Rate Change : {0}", m_rateChange);
234  }
235 
236  #region IWindModelPlugin Members
237 
238 
239  #endregion
240  }
241 }
Vector3 WindSpeed(float fX, float fY, float fZ)
Returns the wind vector at the given local region coordinates.
float WindParamGet(string param)
Get the specified parameter
void WindConfig(OpenSim.Region.Framework.Scenes.Scene scene, Nini.Config.IConfig windConfig)
void WindParamSet(string param, float value)
Set the specified parameter
Interactive OpenSim region server
Definition: OpenSim.cs:55
Vector2[] WindLLClientArray()
Generate a 16 x 16 Vector2 array of wind speeds for LL* based viewers
System.Collections.Generic.Dictionary< string, string > WindParams()
Retrieve a list of parameter/description pairs.