327 lines
11 KiB
C#
327 lines
11 KiB
C#
using log4net;
|
|
using Mono.Addins;
|
|
using Nini.Config;
|
|
using OpenMetaverse;
|
|
using OpenSim.Region.CoreModules.World.LegacyMap;
|
|
using OpenSim.Region.Framework.Interfaces;
|
|
using OpenSim.Region.Framework.Scenes;
|
|
using OpenSim.Region.ScriptEngine.Interfaces;
|
|
using OpenSim.Region.ScriptEngine.Shared.Api;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Drawing;
|
|
using System.Drawing.Imaging;
|
|
using System.Reflection;
|
|
using System.Threading;
|
|
|
|
using LSL_Float = OpenSim.Region.ScriptEngine.Shared.LSL_Types.LSLFloat;
|
|
using LSL_Integer = OpenSim.Region.ScriptEngine.Shared.LSL_Types.LSLInteger;
|
|
using LSL_Key = OpenSim.Region.ScriptEngine.Shared.LSL_Types.LSLString;
|
|
using LSL_List = OpenSim.Region.ScriptEngine.Shared.LSL_Types.list;
|
|
using LSL_Rotation = OpenSim.Region.ScriptEngine.Shared.LSL_Types.Quaternion;
|
|
using LSL_String = OpenSim.Region.ScriptEngine.Shared.LSL_Types.LSLString;
|
|
using LSL_Vector = OpenSim.Region.ScriptEngine.Shared.LSL_Types.Vector3;
|
|
|
|
[assembly: Addin("BasicPathFindingModule", "0.1")]
|
|
[assembly: AddinDependency("OpenSim.Region.Framework", OpenSim.VersionInfo.VersionNumber)]
|
|
namespace OpenSim.Modules.PathFinding
|
|
{
|
|
[Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule", Id = "BasicPathFindingModule")]
|
|
|
|
public class BasicPathFindingModule : INonSharedRegionModule
|
|
{
|
|
#region Region Module
|
|
private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
|
|
|
|
private Scene m_scene = null;
|
|
private IConfig m_config = null;
|
|
private bool m_enabled = true;
|
|
private IScriptModuleComms m_scriptModule;
|
|
|
|
private List<Environment> m_environments = new List<Environment>();
|
|
|
|
public string Name
|
|
{
|
|
get { return "BasicPathFindingModule"; }
|
|
}
|
|
|
|
public Type ReplaceableInterface
|
|
{
|
|
get { return null; }
|
|
}
|
|
|
|
public void AddRegion(Scene scene)
|
|
{
|
|
|
|
}
|
|
|
|
public void Close()
|
|
{
|
|
|
|
}
|
|
|
|
public void Initialise(IConfigSource source)
|
|
{
|
|
try
|
|
{
|
|
m_config = source.Configs["XEngine"];
|
|
|
|
if (m_config != null)
|
|
{
|
|
m_enabled = m_config.GetBoolean("EnablePathFinding", m_enabled);
|
|
}
|
|
else
|
|
{
|
|
m_log.Error("[" + Name + "]: Cant find config.");
|
|
}
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
m_log.ErrorFormat("[" + Name + "]: initialization error: {0}", e.Message);
|
|
return;
|
|
}
|
|
|
|
if (m_enabled)
|
|
{
|
|
m_log.Info("[" + Name + "]: module is enabled");
|
|
}
|
|
else
|
|
{
|
|
m_log.Info("[" + Name + "]: module is disabled");
|
|
}
|
|
}
|
|
|
|
public void RegionLoaded(Scene scene)
|
|
{
|
|
if (m_enabled)
|
|
{
|
|
m_log.Info("[" + Name + "]: Load region " + scene.Name);
|
|
|
|
m_scene = scene;
|
|
m_scriptModule = m_scene.RequestModuleInterface<IScriptModuleComms>();
|
|
if (m_scriptModule == null)
|
|
{
|
|
m_log.ErrorFormat("[" + Name + "]: Failed to load IScriptModuleComms!");
|
|
m_enabled = false;
|
|
return;
|
|
}
|
|
|
|
try
|
|
{
|
|
m_scriptModule.RegisterScriptInvocation(this, "osGeneratePathEnv");
|
|
m_scriptModule.RegisterScriptInvocation(this, "osKeepAlivePathEnv");
|
|
m_scriptModule.RegisterScriptInvocation(this, "osSetPositionData");
|
|
m_scriptModule.RegisterScriptInvocation(this, "osGeneratePath");
|
|
|
|
m_scriptModule.RegisterScriptInvocation(this, "osGetObjectsList");
|
|
|
|
m_scriptModule.RegisterScriptInvocation(this, "osGenerateDebugImage");
|
|
|
|
m_scriptModule.RegisterConstant("PATH_ENV_SUCCESSFUL", 19850);
|
|
m_scriptModule.RegisterConstant("PATH_ENV_NOT_FOUND", 19851);
|
|
m_scriptModule.RegisterConstant("PATH_ENV_OUT_OF_RANGE", 19852);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
m_log.WarnFormat("[" + Name + "]: script method registration failed; {0}", e.Message);
|
|
m_enabled = false;
|
|
}
|
|
|
|
m_log.Info("[" + Name + "]: Region loading done!");
|
|
}
|
|
}
|
|
|
|
public void RemoveRegion(Scene scene)
|
|
{
|
|
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Asyn Funktions
|
|
|
|
private void generatePathEnvironment(ScriptRequestData requestData)
|
|
{
|
|
UUID _envID = UUID.Random();
|
|
Environment _newEnv = new Environment(_envID.ToString(), (int)m_scene.RegionInfo.RegionSizeX);
|
|
|
|
lock(m_environments)
|
|
{
|
|
m_environments.Add(_newEnv);
|
|
}
|
|
|
|
m_scriptModule.DispatchReply(requestData.ScriptID, 19850, _envID.ToString(), requestData.RequestID.ToString());
|
|
}
|
|
|
|
private void keepAlivePathEnv(ScriptRequestData requestData)
|
|
{
|
|
Environment _env = m_environments.Find(X => X.ID == requestData.EnvironmentID);
|
|
|
|
if(_env != null)
|
|
{
|
|
_env.LastTimeUsed = (Int32)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds;
|
|
m_scriptModule.DispatchReply(requestData.ScriptID, 19850, "", requestData.RequestID.ToString());
|
|
return;
|
|
}
|
|
|
|
m_scriptModule.DispatchReply(requestData.ScriptID, 19851, "", requestData.RequestID.ToString());
|
|
}
|
|
|
|
private void removeAllTargets(ScriptRequestData requestData)
|
|
{
|
|
Environment _env = m_environments.Find(X => X.ID == requestData.EnvironmentID);
|
|
|
|
if (_env != null)
|
|
{
|
|
List<PathNode> _nodes = _env.Nodes.FindAll(X => X.Target == true);
|
|
|
|
foreach(PathNode thisNode in _nodes)
|
|
{
|
|
thisNode.Target = false;
|
|
}
|
|
}
|
|
}
|
|
|
|
private void setPositionData(ScriptRequestData requestData, Vector3 position, int walkable, int isTarget)
|
|
{
|
|
Environment _env = m_environments.Find(X => X.ID == requestData.EnvironmentID);
|
|
|
|
if (_env != null)
|
|
{
|
|
if (isTarget == 1)
|
|
removeAllTargets(requestData);
|
|
|
|
PathNode _node = _env.Nodes.Find(X => X.PositionX == (int)position.X && X.PositionY == (int)position.Y);
|
|
|
|
if (_node == null)
|
|
{
|
|
_node = new PathNode((int)position.X, (int)position.Y, false, false);
|
|
_env.Nodes.Add(_node);
|
|
}
|
|
|
|
if (walkable == 1)
|
|
_node.Walkable = true;
|
|
|
|
if (walkable == 0)
|
|
_node.Walkable = false;
|
|
|
|
if (isTarget == 1)
|
|
_node.Target = true;
|
|
|
|
if (isTarget == 0)
|
|
_node.Target = false;
|
|
|
|
return;
|
|
}
|
|
|
|
m_scriptModule.DispatchReply(requestData.ScriptID, 19851, "", requestData.RequestID.ToString());
|
|
}
|
|
|
|
private void generatePath(ScriptRequestData requestData)
|
|
{
|
|
|
|
}
|
|
|
|
private void generateDebugImage(ScriptRequestData requestData)
|
|
{
|
|
Environment _env = m_environments.Find(X => X.ID == requestData.EnvironmentID);
|
|
|
|
if (_env != null)
|
|
{
|
|
Bitmap _bitmap = new Bitmap(_env.Size, _env.Size);
|
|
|
|
foreach(PathNode thisNode in _env.Nodes)
|
|
{
|
|
if (thisNode.Walkable)
|
|
_bitmap.SetPixel(thisNode.PositionX, thisNode.PositionY, Color.Green);
|
|
|
|
if (!thisNode.Walkable)
|
|
_bitmap.SetPixel(thisNode.PositionX, thisNode.PositionY, Color.Black);
|
|
|
|
if (thisNode.Target)
|
|
_bitmap.SetPixel(thisNode.PositionX, thisNode.PositionY, Color.Blue);
|
|
}
|
|
|
|
_bitmap.Save(requestData.EnvironmentID + ".png");
|
|
m_scriptModule.DispatchReply(requestData.ScriptID, 19850, "", requestData.RequestID.ToString());
|
|
return;
|
|
}
|
|
|
|
m_scriptModule.DispatchReply(requestData.ScriptID, 19851, "", requestData.RequestID.ToString());
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Script Funktions
|
|
|
|
[ScriptInvocation]
|
|
public string osGeneratePathEnv(UUID hostID, UUID scriptID, int size)
|
|
{
|
|
UUID requestKey = UUID.Random();
|
|
|
|
SceneObjectGroup _host = m_scene.GetSceneObjectGroup(hostID);
|
|
(new Thread(delegate () { generatePathEnvironment(new ScriptRequestData(hostID, scriptID, null, requestKey)); })).Start();
|
|
|
|
return requestKey.ToString();
|
|
}
|
|
|
|
[ScriptInvocation]
|
|
public string osKeepAlivePathEnv(UUID hostID, UUID scriptID, String environmentID)
|
|
{
|
|
UUID requestKey = UUID.Random();
|
|
|
|
SceneObjectGroup _host = m_scene.GetSceneObjectGroup(hostID);
|
|
(new Thread(delegate () { keepAlivePathEnv(new ScriptRequestData(hostID, scriptID, environmentID, requestKey)); })).Start();
|
|
|
|
return requestKey.ToString();
|
|
}
|
|
|
|
[ScriptInvocation]
|
|
public void osSetPositionData(UUID hostID, UUID scriptID, String environmentID, Vector3 position, int walkable, int isTarget)
|
|
{
|
|
SceneObjectGroup _host = m_scene.GetSceneObjectGroup(hostID);
|
|
(new Thread(delegate () { setPositionData(new ScriptRequestData(hostID, scriptID, environmentID), position, walkable, isTarget); })).Start();
|
|
}
|
|
|
|
[ScriptInvocation]
|
|
public string osGeneratePath(UUID hostID, UUID scriptID, String environmentID)
|
|
{
|
|
UUID requestKey = UUID.Random();
|
|
|
|
SceneObjectGroup _host = m_scene.GetSceneObjectGroup(hostID);
|
|
(new Thread(delegate () { generatePath(new ScriptRequestData(hostID, scriptID, environmentID, requestKey)); })).Start();
|
|
|
|
return requestKey.ToString();
|
|
}
|
|
|
|
[ScriptInvocation]
|
|
public string osGenerateDebugImage(UUID hostID, UUID scriptID, String environmentID)
|
|
{
|
|
UUID requestKey = UUID.Random();
|
|
|
|
SceneObjectGroup _host = m_scene.GetSceneObjectGroup(hostID);
|
|
(new Thread(delegate () { generateDebugImage(new ScriptRequestData(hostID, scriptID, environmentID, requestKey)); })).Start();
|
|
|
|
return requestKey.ToString();
|
|
}
|
|
|
|
[ScriptInvocation]
|
|
public LSL_List osGetSearchableObjectList(UUID hostID, UUID scriptID, String searchString)
|
|
{
|
|
LSL_List returnList = new LSL_List();
|
|
|
|
foreach (SceneObjectGroup thisGroup in m_scene.GetSceneObjectGroups())
|
|
{
|
|
if(thisGroup.Name == searchString)
|
|
returnList.Add(thisGroup.GroupID);
|
|
}
|
|
|
|
return returnList;
|
|
}
|
|
|
|
|
|
#endregion
|
|
|
|
}
|
|
}
|