29 using System.Collections.Generic;
30 using System.Reflection;
35 using OpenSim.Framework;
36 using OpenSim.Region.Framework.Interfaces;
37 using OpenSim.Region.Framework.Scenes;
39 using OpenSim.Region.CoreModules.World.Wind;
41 namespace OpenSim.
Region.CoreModules
43 [Extension(Path =
"/OpenSim/RegionModules", NodeName =
"RegionModule", Id =
"WindModule")]
46 private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
48 private uint m_frame = 0;
49 private uint m_frameLastUpdateClientArray = 0;
50 private int m_frameUpdateRate = 150;
52 private Scene m_scene = null;
53 private bool m_ready =
false;
55 private bool m_enabled =
false;
56 private IConfig m_windConfig;
58 private string m_dWindPluginName =
"SimpleRandomWind";
59 private Dictionary<string, IWindModelPlugin> m_availableWindPlugins =
new Dictionary<string, IWindModelPlugin>();
62 private Vector2[] windSpeeds =
new Vector2[16 * 16];
64 #region INonSharedRegionModule Methods
68 m_windConfig = config.Configs[
"Wind"];
71 if (m_windConfig != null)
73 m_enabled = m_windConfig.GetBoolean(
"enabled",
true);
75 m_frameUpdateRate = m_windConfig.GetInt(
"wind_update_rate", 150);
78 if (m_windConfig.Contains(
"wind_plugin"))
80 m_dWindPluginName = m_windConfig.GetString(
"wind_plugin", m_dWindPluginName);
86 m_log.InfoFormat(
"[WIND] Enabled with an update rate of {0} frames.", m_frameUpdateRate);
101 foreach (
IWindModelPlugin windPlugin
in AddinManager.GetExtensionObjects(
"/OpenSim/WindModule",
false))
103 m_log.InfoFormat(
"[WIND] Found Plugin: {0}", windPlugin.Name);
104 m_availableWindPlugins.Add(windPlugin.Name, windPlugin);
108 if (m_availableWindPlugins.ContainsKey(m_dWindPluginName))
110 m_activeWindPlugin = m_availableWindPlugins[m_dWindPluginName];
112 m_log.InfoFormat(
"[WIND] {0} plugin found, initializing.", m_dWindPluginName);
114 if (m_windConfig != null)
116 m_activeWindPlugin.Initialise();
117 m_activeWindPlugin.WindConfig(m_scene, m_windConfig);
123 if (m_activeWindPlugin == null)
125 m_log.ErrorFormat(
"[WIND] Could not find specified wind plug-in: {0}", m_dWindPluginName);
126 m_log.ErrorFormat(
"[WIND] Defaulting to no wind.");
142 "wind base wind_update_rate",
143 "wind base wind_update_rate [<value>]",
144 "Get or set the wind update rate.",
146 HandleConsoleBaseCommand);
148 foreach (KeyValuePair<string, string> kvp
in windPlugin.
WindParams())
150 string windCommand = String.Format(
"wind {0} {1}", windPlugin.Name, kvp.Key);
151 m_scene.AddCommand(
"Regions",
this, windCommand, string.Format(
"{0} [<value>]", windCommand), kvp.Value,
"", HandleConsoleParamCommand);
156 m_scene.EventManager.OnFrame += WindUpdate;
157 m_scene.EventManager.OnMakeRootAgent += OnAgentEnteredRegion;
160 m_scene.RegisterModuleInterface<
IWindModule>(
this);
177 m_activeWindPlugin = null;
180 windPlugin.Dispose();
183 m_availableWindPlugins.Clear();
186 m_scene.EventManager.OnFrame -= WindUpdate;
187 m_scene.EventManager.OnMakeRootAgent -= OnAgentEnteredRegion;
197 get {
return "WindModule"; }
200 public Type ReplaceableInterface
211 #region Console Commands
212 private void ValidateConsole()
214 if (m_scene.ConsoleScene() == null)
219 MainConsole.Instance.Output(
"Please change to a specific region in order to set Sun parameters.");
223 if (m_scene.ConsoleScene() != m_scene)
225 MainConsole.Instance.Output(
"Console Scene is not my scene.");
233 private void HandleConsoleCommand(
string module,
string[] cmdparams)
237 MainConsole.Instance.Output(
238 "The wind command can be used to change the currently active wind model plugin and update the parameters for wind plugins.");
244 private void HandleConsoleBaseCommand(
string module,
string[] cmdparams)
248 if ((cmdparams.Length != 4)
249 || !cmdparams[1].Equals(
"base"))
251 MainConsole.Instance.Output(
252 "Invalid parameters to change parameters for Wind module base, usage: wind base <parameter> <value>");
257 switch (cmdparams[2])
259 case "wind_update_rate":
262 if (
int.TryParse(cmdparams[3], out newRate))
264 m_frameUpdateRate = newRate;
268 MainConsole.Instance.OutputFormat(
269 "Invalid value {0} specified for {1}", cmdparams[3], cmdparams[2]);
276 string desiredPlugin = cmdparams[3];
278 if (desiredPlugin.Equals(m_activeWindPlugin.Name))
280 MainConsole.Instance.OutputFormat(
"Wind model plugin {0} is already active", cmdparams[3]);
285 if (m_availableWindPlugins.ContainsKey(desiredPlugin))
287 m_activeWindPlugin = m_availableWindPlugins[cmdparams[3]];
289 MainConsole.Instance.OutputFormat(
"{0} wind model plugin now active", m_activeWindPlugin.Name);
293 MainConsole.Instance.OutputFormat(
"Could not find wind model plugin {0}", desiredPlugin);
302 private void HandleConsoleParamCommand(
string module,
string[] cmdparams)
307 if ((cmdparams.Length != 4)
308 && (cmdparams.Length != 3))
310 MainConsole.Instance.Output(
"Usage: wind <plugin> <param> [value]");
314 string plugin = cmdparams[1];
315 string param = cmdparams[2];
317 if (cmdparams.Length == 4)
319 if (!
float.TryParse(cmdparams[3], out value))
321 MainConsole.Instance.OutputFormat(
"Invalid value {0}", cmdparams[3]);
326 WindParamSet(plugin, param, value);
327 MainConsole.Instance.OutputFormat(
"{0} set to {1}", param, value);
331 MainConsole.Instance.OutputFormat(
"{0}", e.Message);
338 value = WindParamGet(plugin, param);
339 MainConsole.Instance.OutputFormat(
"{0} : {1}", param, value);
343 MainConsole.Instance.OutputFormat(
"{0}", e.Message);
351 #region IWindModule Methods
361 if (m_activeWindPlugin != null)
363 return m_activeWindPlugin.WindSpeed(x, y, z);
367 return new Vector3(0.0f, 0.0f, 0.0f);
373 if (m_availableWindPlugins.ContainsKey(plugin))
376 windPlugin.WindParamSet(param, value);
380 throw new Exception(
String.Format(
"Could not find plugin {0}", plugin));
386 if (m_availableWindPlugins.ContainsKey(plugin))
389 return windPlugin.WindParamGet(param);
393 throw new Exception(
String.Format(
"Could not find plugin {0}", plugin));
397 public string WindActiveModelPluginName
401 if (m_activeWindPlugin != null)
403 return m_activeWindPlugin.Name;
419 if (((m_frame++ % m_frameUpdateRate) != 0) || !m_ready)
426 SendWindAllClients();
433 if (m_activeWindPlugin != null)
437 if (m_frame >= (m_frameLastUpdateClientArray + m_frameUpdateRate))
439 windSpeeds = m_activeWindPlugin.WindLLClientArray();
440 m_frameLastUpdateClientArray = m_frame;
444 avatar.ControllingClient.SendWindData(windSpeeds);
448 private void SendWindAllClients()
452 if (m_scene.GetRootAgentCount() > 0)
456 if (m_frame >= (m_frameLastUpdateClientArray + m_frameUpdateRate))
458 windSpeeds = m_activeWindPlugin.WindLLClientArray();
459 m_frameLastUpdateClientArray = m_frame;
462 m_scene.ForEachRootClient(delegate(
IClientAPI client)
464 client.SendWindData(windSpeeds);
473 private void GenWindPos()
475 if (m_activeWindPlugin != null)
478 m_activeWindPlugin.WindUpdate(m_frame);
void AddRegion(Scene scene)
This is called whenever a Scene is added. For shared modules, this can happen several times...
void WindParamSet(string plugin, string param, float value)
Set Wind Plugin Parameter
void WindUpdate()
Called on each frame update. Updates the wind model and clients as necessary.
void RemoveRegion(Scene scene)
This is called whenever a Scene is removed. For shared modules, this can happen several times...
void Initialise(IConfigSource config)
This is called to initialize the region module. For shared modules, this is called exactly once...
Dictionary< string, string > WindParams()
Retrieve a list of parameter/description pairs.
void RegionLoaded(Scene scene)
This will be called once for every scene loaded. In a shared module this will be multiple times in on...
float WindParamGet(string plugin, string param)
Get Wind Plugin Parameter
void Close()
This is the inverse to Initialise. After a Close(), this instance won't be usable anymore...
Vector3 WindSpeed(int x, int y, int z)
Retrieve the wind speed at the given region coordinate. This implimentation ignores Z...
void OnAgentEnteredRegion(ScenePresence avatar)