/*
* Copyright (c) Contributors, http://opensimulator.org/
* See CONTRIBUTORS.TXT for a full list of copyright holders.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the OpenSimulator Project nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using OpenMetaverse;
using OpenSim.Framework;
//using log4net;
using OpenSim.Region.Framework.Interfaces;
using OpenSim.Region.Framework.Scenes;
namespace OpenSim.Region.ScriptEngine.Shared.Api.Plugins
{
public class SensorRepeat
{
//private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
///
/// Used by one-off and repeated sensors
///
public class SensorInfo
{
public uint localID;
public UUID itemID;
public double interval;
public DateTime next;
public string name;
public UUID keyID;
public int type;
public float range;
public float arc;
public SceneObjectPart host;
public SensorInfo Clone()
{
return (SensorInfo)MemberwiseClone();
}
}
public AsyncCommandManager m_CmdManager;
///
/// Number of sensors active.
///
public int SensorsCount
{
get
{
return SenseRepeaters.Count;
}
}
public SensorRepeat(AsyncCommandManager CmdManager)
{
m_CmdManager = CmdManager;
maximumRange = CmdManager.m_ScriptEngine.Config.GetFloat("SensorMaxRange", 96.0f);
maximumToReturn = CmdManager.m_ScriptEngine.Config.GetInt("SensorMaxResults", 16);
m_npcModule = m_CmdManager.m_ScriptEngine.World.RequestModuleInterface();
}
private readonly INPCModule m_npcModule;
private readonly object SenseLock = new();
private const int AGENT = 1;
private const int AGENT_BY_USERNAME = 0x10;
private const int NPC = 0x20;
private const int ACTIVE = 2;
private const int PASSIVE = 4;
private const int SCRIPTED = 8;
private readonly float maximumRange = 96.0f;
private readonly int maximumToReturn = 16;
//
// Sensed entity
//
private class SensedEntity : IComparable
{
public SensedEntity(float detectedDistance, UUID detectedID)
{
distance = detectedDistance;
itemID = detectedID;
}
public int CompareTo(object obj)
{
if (obj is not SensedEntity ent)
throw new InvalidOperationException();
if (ent.distance < distance) return 1;
if (ent.distance > distance) return -1;
return 0;
}
public UUID itemID;
public float distance;
}
///
/// Sensors to process.
///
///
/// Always lock SenseRepeatListLock when updating this list.
///
private List SenseRepeaters = new();
private readonly object SenseRepeatListLock = new();
public void SetSenseRepeatEvent(uint m_localID, UUID m_itemID,
string name, UUID keyID, int type, double range,
double arc, double sec, SceneObjectPart host)
{
// Always remove first, in case this is a re-set
UnSetSenseRepeaterEvents(m_localID, m_itemID);
if (sec == 0) // Disabling timer
return;
float frange = (float)range;
// Add to timer
SensorInfo ts = new()
{
localID = m_localID,
itemID = m_itemID,
interval = sec,
next = DateTime.UtcNow.AddSeconds(sec),
name = name,
keyID = keyID,
type = type,
range = frange >= maximumRange ? maximumRange : frange,
arc = (float)arc,
host = host
};
AddSenseRepeater(ts);
}
private void AddSenseRepeater(SensorInfo senseRepeater)
{
lock (SenseRepeatListLock)
SenseRepeaters.Add(senseRepeater);
}
public void UnSetSenseRepeaterEvents(uint m_localID, UUID m_itemID)
{
// Remove from timer
lock (SenseRepeatListLock)
{
List newSenseRepeaters = new(SenseRepeaters.Count);
foreach (SensorInfo ts in SenseRepeaters)
{
if (ts.localID != m_localID || ts.itemID.NotEqual(m_itemID))
{
newSenseRepeaters.Add(ts);
}
}
SenseRepeaters = newSenseRepeaters;
}
}
public void CheckSenseRepeaterEvents()
{
// Go through all timers
List curSensors;
lock(SenseRepeatListLock)
curSensors = SenseRepeaters;
DateTime now = DateTime.UtcNow;
foreach (SensorInfo ts in curSensors)
{
if(ts.interval == 0)
{
SensorSweep(ts);
lock (SenseRepeatListLock)
SenseRepeaters.Remove(ts);
}
// Time has passed?
else if (ts.next < now)
{
SensorSweep(ts);
// set next interval
ts.next = now.AddSeconds(ts.interval);
}
}
}
public void SenseOnce(uint m_localID, UUID m_itemID,
string name, UUID keyID, int type,
double range, double arc, SceneObjectPart host)
{
float frange = (float)range;
// Add to timer
SensorInfo ts = new()
{
localID = m_localID,
itemID = m_itemID,
interval = 0,
name = name,
keyID = keyID,
type = type,
range = frange >= maximumRange ? maximumRange : frange,
arc = (float)arc,
host = host
};
AddSenseRepeater(ts);
}
private void SensorSweep(SensorInfo ts)
{
if (ts.host is null)
return;
List sensedEntities = new();
// Is the sensor type is AGENT and not SCRIPTED then include agents
if ((ts.type & (AGENT | AGENT_BY_USERNAME | NPC)) != 0 && (ts.type & SCRIPTED) == 0)
{
sensedEntities.AddRange(doAgentSensor(ts));
}
// If SCRIPTED or PASSIVE or ACTIVE check objects
if ((ts.type & SCRIPTED) != 0 || (ts.type & PASSIVE) != 0 || (ts.type & ACTIVE) != 0)
{
sensedEntities.AddRange(doObjectSensor(ts));
}
lock (SenseLock)
{
if (sensedEntities.Count == 0)
{
// send a "no_sensor"
// Add it to queue
m_CmdManager.m_ScriptEngine.PostScriptEvent(ts.itemID,
new EventParams("no_sensor", Array.Empty