index targets by scriptID

master
UbitUmarov 2020-02-26 04:50:55 +00:00
parent 6dd5ea0c96
commit 771381558e
1 changed files with 74 additions and 57 deletions

View File

@ -361,6 +361,7 @@ namespace OpenSim.Region.Framework.Scenes
private SortedDictionary<int, scriptPosTarget> m_targets = new SortedDictionary<int, scriptPosTarget>(); private SortedDictionary<int, scriptPosTarget> m_targets = new SortedDictionary<int, scriptPosTarget>();
private SortedDictionary<int, scriptRotTarget> m_rotTargets = new SortedDictionary<int, scriptRotTarget>(); private SortedDictionary<int, scriptRotTarget> m_rotTargets = new SortedDictionary<int, scriptRotTarget>();
private Dictionary<UUID, List<int>> m_targetsByScript = new Dictionary<UUID, List<int>>();
public SortedDictionary<int, scriptPosTarget> AtTargets public SortedDictionary<int, scriptPosTarget> AtTargets
{ {
@ -4810,10 +4811,22 @@ namespace OpenSim.Region.Framework.Scenes
waypoint.scriptID = scriptID; waypoint.scriptID = scriptID;
int handle = m_scene.AllocateIntId(); int handle = m_scene.AllocateIntId();
waypoint.handle = handle; waypoint.handle = handle;
lock (m_rotTargets) lock (m_rotTargets)
{ {
if (m_rotTargets.Count >= 8) if(m_targetsByScript.TryGetValue(scriptID, out List<int> handles))
m_rotTargets.Remove(m_rotTargets.ElementAt(0).Key); {
if (handles.Count >= 8)
{
int todel = handles[0];
handles.RemoveAt(0);
m_rotTargets.Remove(todel);
}
handles.Add(handle);
}
else
m_targetsByScript[scriptID] = new List<int>(){handle};
m_rotTargets.Add(handle, waypoint); m_rotTargets.Add(handle, waypoint);
} }
m_scene.AddGroupTarget(this); m_scene.AddGroupTarget(this);
@ -4824,26 +4837,45 @@ namespace OpenSim.Region.Framework.Scenes
{ {
lock (m_targets) lock (m_targets)
{ {
if(m_rotTargets.TryGetValue(handle, out scriptRotTarget waypoint))
{
if(m_targetsByScript.TryGetValue(waypoint.scriptID, out List<int>handles))
{
handles.Remove(handle);
if(handles.Count() == 0)
m_targetsByScript.Remove(waypoint.scriptID);
}
m_rotTargets.Remove(handle); m_rotTargets.Remove(handle);
if (m_targets.Count == 0) }
if (m_targets.Count == 0 && m_rotTargets.Count == 0)
m_scene.RemoveGroupTarget(this); m_scene.RemoveGroupTarget(this);
} }
} }
public int registerTargetWaypoint(UUID scriptID, Vector3 target, float tolerance) public int registerTargetWaypoint(UUID scriptID, Vector3 target, float tolerance)
{ {
int handle = m_scene.AllocateIntId();
scriptPosTarget waypoint = new scriptPosTarget(); scriptPosTarget waypoint = new scriptPosTarget();
waypoint.targetPos = target; waypoint.targetPos = target;
waypoint.tolerance = tolerance * tolerance; waypoint.tolerance = tolerance * tolerance;
waypoint.scriptID = scriptID; waypoint.scriptID = scriptID;
int handle = m_scene.AllocateIntId();
waypoint.handle = handle; waypoint.handle = handle;
lock (m_targets) lock (m_targets)
{ {
if (m_targets.Count >= 8) if (m_targetsByScript.TryGetValue(scriptID, out List<int> handles))
m_targets.Remove(m_targets.ElementAt(0).Key); {
if (handles.Count >= 8)
{
int todel = handles[0];
handles.RemoveAt(0);
m_rotTargets.Remove(todel);
}
handles.Add(handle);
}
else
m_targetsByScript[scriptID] = new List<int>() { handle };
m_targets.Add(handle, waypoint); m_targets.Add(handle, waypoint);
} }
m_scene.AddGroupTarget(this); m_scene.AddGroupTarget(this);
@ -4854,43 +4886,39 @@ namespace OpenSim.Region.Framework.Scenes
{ {
lock (m_targets) lock (m_targets)
{ {
if (m_targets.TryGetValue(handle, out scriptPosTarget waypoint))
{
if (m_targetsByScript.TryGetValue(waypoint.scriptID, out List<int> handles))
{
handles.Remove(handle);
if (handles.Count() == 0)
m_targetsByScript.Remove(waypoint.scriptID);
}
m_targets.Remove(handle); m_targets.Remove(handle);
if (m_targets.Count == 0) }
if (m_targets.Count == 0 && m_rotTargets.Count == 0)
m_scene.RemoveGroupTarget(this); m_scene.RemoveGroupTarget(this);
} }
} }
public void RemoveScriptTargets(UUID scriptID) public void RemoveScriptTargets(UUID scriptID)
{ {
List<int> toremove = new List<int>();
lock (m_targets) lock (m_targets)
{ {
foreach (KeyValuePair<int, scriptPosTarget> kvp in m_targets) if(m_targetsByScript.TryGetValue(scriptID, out List<int> toremove))
{ {
if (kvp.Value.scriptID == scriptID)
toremove.Add(kvp.Key);
}
if (toremove.Count > 0) if (toremove.Count > 0)
{ {
for (int i = 0; i < toremove.Count; ++i) for (int i = 0; i < toremove.Count; ++i)
{
m_targets.Remove(toremove[i]); m_targets.Remove(toremove[i]);
toremove.Clear(); m_rotTargets.Remove(toremove[i]);
} }
} }
m_targetsByScript.Remove(scriptID);
lock (m_rotTargets)
{
foreach (KeyValuePair<int, scriptRotTarget> kvp in m_rotTargets)
{
if (kvp.Value.scriptID == scriptID)
toremove.Add(kvp.Key);
} }
if (toremove.Count > 0) if (m_targets.Count == 0 && m_rotTargets.Count == 0)
{ m_scene.RemoveGroupTarget(this);
for (int i = 0; i < toremove.Count; ++i)
m_targets.Remove(toremove[i]);
}
} }
} }
@ -4900,7 +4928,7 @@ namespace OpenSim.Region.Framework.Scenes
if (targetsCount > 0 && (m_scriptListens_atTarget || m_scriptListens_notAtTarget)) if (targetsCount > 0 && (m_scriptListens_atTarget || m_scriptListens_notAtTarget))
{ {
List<scriptPosTarget> atTargets = new List<scriptPosTarget>(); List<scriptPosTarget> atTargets = new List<scriptPosTarget>();
List<scriptPosTarget> notatTargets = new List<scriptPosTarget>(targetsCount); HashSet<UUID> notatTargets = new HashSet<UUID>();
Vector3 pos = m_rootPart.GroupPosition; Vector3 pos = m_rootPart.GroupPosition;
lock (m_targets) lock (m_targets)
{ {
@ -4910,34 +4938,29 @@ namespace OpenSim.Region.Framework.Scenes
{ {
if (m_scriptListens_atTarget) if (m_scriptListens_atTarget)
atTargets.Add(target); atTargets.Add(target);
notatTargets.Remove(target.scriptID);
} }
else else
{ {
if (m_scriptListens_notAtTarget) if (m_scriptListens_notAtTarget)
notatTargets.Add(target); notatTargets.Add(target.scriptID);
} }
} }
} }
bool hasnot = notatTargets.Count > 0;
HashSet<UUID> excludes = new HashSet<UUID>();
if (atTargets.Count > 0) if (atTargets.Count > 0)
{ {
for (int target = 0; target < atTargets.Count; ++target) for (int target = 0; target < atTargets.Count; ++target)
{ {
scriptPosTarget att = atTargets[target]; scriptPosTarget att = atTargets[target];
m_scene.EventManager.TriggerAtTargetEvent(att.scriptID, (uint)att.handle, att.targetPos, pos); m_scene.EventManager.TriggerAtTargetEvent(att.scriptID, (uint)att.handle, att.targetPos, pos);
if(hasnot)
excludes.Add(att.scriptID);
} }
} }
if (hasnot) if (notatTargets.Count > 0)
{ {
for (int target = 0; target < notatTargets.Count; ++target) foreach (UUID id in notatTargets)
{ {
UUID id = notatTargets[target].scriptID;
if(!excludes.Contains(id))
m_scene.EventManager.TriggerNotAtTargetEvent(id); m_scene.EventManager.TriggerNotAtTargetEvent(id);
} }
} }
@ -4947,7 +4970,7 @@ namespace OpenSim.Region.Framework.Scenes
if (targetsCount > 0 && (m_scriptListens_atRotTarget || m_scriptListens_notAtRotTarget)) if (targetsCount > 0 && (m_scriptListens_atRotTarget || m_scriptListens_notAtRotTarget))
{ {
List<scriptRotTarget> atRotTargets = new List<scriptRotTarget>(targetsCount); List<scriptRotTarget> atRotTargets = new List<scriptRotTarget>(targetsCount);
List<scriptRotTarget> notatRotTargets = new List<scriptRotTarget>(targetsCount); HashSet<UUID> notatRotTargets = new HashSet<UUID>();
Quaternion rot = m_rootPart.RotationOffset; Quaternion rot = m_rootPart.RotationOffset;
lock (m_rotTargets) lock (m_rotTargets)
{ {
@ -4962,35 +4985,29 @@ namespace OpenSim.Region.Framework.Scenes
{ {
if (m_scriptListens_atRotTarget) if (m_scriptListens_atRotTarget)
atRotTargets.Add(target); atRotTargets.Add(target);
notatRotTargets.Remove(target.scriptID);
} }
else else
{ {
if (m_scriptListens_notAtRotTarget) if (m_scriptListens_notAtRotTarget)
notatRotTargets.Add(target); notatRotTargets.Add(target.scriptID);
} }
} }
} }
bool hasnot = notatRotTargets.Count > 0;
HashSet<UUID> excludes = new HashSet<UUID>();
if (atRotTargets.Count > 0) if (atRotTargets.Count > 0)
{ {
for (int target = 0; target < atRotTargets.Count; ++target) for (int target = 0; target < atRotTargets.Count; ++target)
{ {
scriptRotTarget att = atRotTargets[target]; scriptRotTarget att = atRotTargets[target];
m_scene.EventManager.TriggerAtRotTargetEvent(att.scriptID, (uint)att.handle, att.targetRot, rot); m_scene.EventManager.TriggerAtRotTargetEvent(att.scriptID, (uint)att.handle, att.targetRot, rot);
if(hasnot)
excludes.Add(att.scriptID);
} }
} }
if (notatRotTargets.Count > 0) if (notatRotTargets.Count > 0)
{ {
for (int target = 0; target < notatRotTargets.Count; ++target) foreach (UUID id in notatRotTargets)
{ {
UUID id = notatRotTargets[target].scriptID;
if(!excludes.Contains(id))
m_scene.EventManager.TriggerNotAtRotTargetEvent(id); m_scene.EventManager.TriggerNotAtRotTargetEvent(id);
} }
} }