OpenSim.Modules.PathFinding/src/BasicPathFinding.cs

218 lines
7.0 KiB
C#

using log4net;
using Mono.Addins;
using Nini.Config;
using OpenMetaverse;
using OpenSim.Region.Framework.Interfaces;
using OpenSim.Region.Framework.Scenes;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Security.Cryptography;
using System.Text;
using System.Threading;
using System.Timers;
[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 string m_dataDirectory = "./PathFindingData";
private IScriptModuleComms m_scriptModule;
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_dataDirectory = m_config.GetString("PathFindingDataDirectory", m_dataDirectory);
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, "osCreateNewPathFindingScene");
//m_scriptModule.RegisterScriptInvocation(this, "osAddObjectToPathFindingScene");
//m_scriptModule.RegisterScriptInvocation(this, "osGetPath");
}
catch (Exception e)
{
m_log.WarnFormat("[" + Name + "]: script method registration failed; {0}", e.Message);
m_enabled = false;
}
}
}
public void RemoveRegion(Scene scene)
{
}
#endregion
#region Script Funktions
private bool drawAllBlocketPositions(ref Bitmap _map)
{
List<SceneObjectGroup> _objects = m_scene.GetSceneObjectGroups().FindAll(X => X.IsPhantom != true);
foreach(SceneObjectGroup _object in _objects)
{
SceneObjectPart[] _parts = _object.Parts;
foreach (SceneObjectPart _part in _parts)
{
if(!_part.Description.Contains("ignorepathfinding"))
{
Vector2 _start = new Vector2(_part.AbsolutePosition.X - ((int)_part.Scale.X / 2), _part.AbsolutePosition.Y - ((int)_part.Scale.Y / 2));
Vector2 _end = new Vector2(_part.AbsolutePosition.X + ((int)_part.Scale.X / 2), _part.AbsolutePosition.Y + ((int)_part.Scale.Y / 2));
fillArea(ref _map, _start, _end, Color.Red);
}
}
}
return false;
}
private bool drawAllAllowedPaths(ref Bitmap _map)
{
foreach (SceneObjectGroup _object in m_scene.GetSceneObjectGroups())
{
SceneObjectPart[] _parts = _object.Parts;
foreach (SceneObjectPart _part in _parts)
{
if (_part.Description.Contains("allowedpath") || _part.Description.Contains("pathfinding_allow"))
{
Vector2 _start = new Vector2(_part.AbsolutePosition.X - ((int)_part.Scale.X / 2), _part.AbsolutePosition.Y - ((int)_part.Scale.Y / 2));
Vector2 _end = new Vector2(_part.AbsolutePosition.X + ((int)_part.Scale.X / 2), _part.AbsolutePosition.Y + ((int)_part.Scale.Y / 2));
fillArea(ref _map, _start, _end, Color.Green);
}
}
}
return false;
}
private void fillArea(ref Bitmap _map, Vector2 _start, Vector2 _end, Color _color)
{
for (int X = (int)_start.X; X < (int)_end.X; X++)
{
for (int Y = (int)_start.Y; Y < (int)_end.Y; Y++)
{
_map.SetPixel(X, Y, _color);
}
}
}
private void createPathFindingScene(String imageName)
{
Bitmap _map = new Bitmap((int)m_scene.RegionInfo.RegionSizeX, (int)m_scene.RegionInfo.RegionSizeY);
for (int X = 0; X < m_scene.RegionInfo.RegionSizeX; X++)
{
for (int Y = 0; Y < m_scene.RegionInfo.RegionSizeY; Y++)
{
float baseheight = (float)m_scene.Heightmap[X, Y];
if (baseheight <= m_scene.RegionInfo.RegionSettings.WaterHeight)
_map.SetPixel(Y, X, Color.Red);
if (baseheight > m_scene.RegionInfo.RegionSettings.WaterHeight)
_map.SetPixel(Y, X, Color.Yellow);
drawAllBlocketPositions(ref _map);
drawAllAllowedPaths(ref _map);
}
}
_map.Save(imageName + ".png", ImageFormat.Png);
}
[ScriptInvocation]
public string osCreateNewPathFindingScene(UUID hostID, UUID scriptID)
{
String imageName = UUID.Random().ToString();
(new Thread(delegate () { createPathFindingScene(imageName); })).Start();
return imageName;
}
#endregion
}
}