2011-07-08 17:57:07 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) Contributors, https://github.com/jonc/osboids
|
2014-12-24 16:29:19 +00:00
|
|
|
* https://github.com/JakDaniels/OpenSimBirds
|
2011-07-08 17:57:07 +00:00
|
|
|
* 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 OpenMetaverse;
|
|
|
|
using OpenSim.Framework;
|
2014-12-31 18:50:48 +00:00
|
|
|
using OpenSim.Region.Framework.Scenes;
|
|
|
|
using log4net;
|
2011-07-08 17:57:07 +00:00
|
|
|
|
|
|
|
namespace Flocking
|
|
|
|
{
|
|
|
|
public class FlockingView
|
2014-12-31 18:50:48 +00:00
|
|
|
{
|
|
|
|
private static readonly ILog m_log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
|
|
|
|
private Scene m_scene;
|
|
|
|
private UUID m_owner;
|
|
|
|
private String m_name;
|
2014-12-24 16:29:19 +00:00
|
|
|
private String m_birdPrim;
|
2011-07-08 17:57:07 +00:00
|
|
|
|
|
|
|
private Dictionary<string, SceneObjectGroup> m_sogMap = new Dictionary<string, SceneObjectGroup> ();
|
2011-07-09 10:11:19 +00:00
|
|
|
|
2014-12-31 18:50:48 +00:00
|
|
|
public FlockingView (String moduleName, Scene scene)
|
|
|
|
{
|
|
|
|
m_name = moduleName;
|
|
|
|
m_scene = scene;
|
2011-07-08 17:57:07 +00:00
|
|
|
}
|
|
|
|
|
2011-07-09 10:11:19 +00:00
|
|
|
public void PostInitialize (UUID owner)
|
2011-07-08 17:57:07 +00:00
|
|
|
{
|
2011-07-09 10:11:19 +00:00
|
|
|
m_owner = owner;
|
2011-07-08 17:57:07 +00:00
|
|
|
}
|
2011-07-09 12:27:58 +00:00
|
|
|
|
2014-12-24 16:29:19 +00:00
|
|
|
public String BirdPrim {
|
|
|
|
set{ m_birdPrim = value;}
|
2011-07-09 12:27:58 +00:00
|
|
|
}
|
2011-07-08 17:57:07 +00:00
|
|
|
|
2011-07-09 10:11:19 +00:00
|
|
|
public void Clear ()
|
|
|
|
{
|
|
|
|
//trash everything we have
|
|
|
|
foreach (string name in m_sogMap.Keys)
|
2014-12-31 18:50:48 +00:00
|
|
|
{
|
|
|
|
m_log.InfoFormat("[{0}]: Removing prim {1} from region {2}", m_name, name, m_scene.RegionInfo.RegionName);
|
2011-07-09 10:11:19 +00:00
|
|
|
RemoveSOGFromScene(name);
|
|
|
|
}
|
|
|
|
m_sogMap.Clear();
|
|
|
|
}
|
|
|
|
|
2014-12-24 16:29:19 +00:00
|
|
|
public void Render (List<Bird> birds)
|
2011-07-08 17:57:07 +00:00
|
|
|
{
|
2014-12-24 16:29:19 +00:00
|
|
|
foreach (Bird bird in birds) {
|
|
|
|
DrawBird (bird);
|
2011-07-08 17:57:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-24 16:29:19 +00:00
|
|
|
private void DrawBird (Bird bird)
|
2011-07-08 17:57:07 +00:00
|
|
|
{
|
2014-12-24 16:29:19 +00:00
|
|
|
SceneObjectPart existing = m_scene.GetSceneObjectPart (bird.Id);
|
2011-07-08 17:57:07 +00:00
|
|
|
|
|
|
|
|
2014-12-31 18:50:48 +00:00
|
|
|
SceneObjectGroup sog;
|
|
|
|
SceneObjectPart rootPart;
|
|
|
|
|
2011-07-08 17:57:07 +00:00
|
|
|
if (existing == null) {
|
2014-12-24 16:29:19 +00:00
|
|
|
SceneObjectGroup group = findByName (m_birdPrim);
|
2014-12-31 18:50:48 +00:00
|
|
|
sog = CopyPrim (group, bird.Id);
|
|
|
|
rootPart = sog.RootPart;
|
|
|
|
//set prim to phantom
|
|
|
|
sog.UpdatePrimFlags(rootPart.LocalId, false, false, true, false);
|
|
|
|
m_sogMap [bird.Id] = sog;
|
|
|
|
m_log.InfoFormat("[{0}]: Adding prim {1} from region {2}", m_name, bird.Id, m_scene.RegionInfo.RegionName);
|
2011-07-09 10:11:19 +00:00
|
|
|
m_scene.AddNewSceneObject (sog, false);
|
2011-07-08 17:57:07 +00:00
|
|
|
} else {
|
2014-12-31 18:50:48 +00:00
|
|
|
sog = existing.ParentGroup;
|
|
|
|
rootPart = sog.RootPart;
|
|
|
|
//set prim to phantom
|
|
|
|
sog.UpdatePrimFlags(rootPart.LocalId, false, false, true, false);
|
2011-07-08 17:57:07 +00:00
|
|
|
}
|
|
|
|
|
2014-12-24 16:29:19 +00:00
|
|
|
Quaternion rotation = CalcRotationToEndpoint (sog, sog.AbsolutePosition, bird.Location);
|
2014-12-31 18:50:48 +00:00
|
|
|
sog.UpdateGroupRotationPR( bird.Location, rotation);
|
|
|
|
|
|
|
|
// Fire script on_rez
|
|
|
|
sog.CreateScriptInstances(0, true, m_scene.DefaultScriptEngine, 1);
|
|
|
|
rootPart.ParentGroup.ResumeScripts();
|
|
|
|
rootPart.ScheduleFullUpdate();
|
2011-07-08 17:57:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private static Quaternion CalcRotationToEndpoint (SceneObjectGroup copy, Vector3 sv, Vector3 ev)
|
|
|
|
{
|
2011-07-09 16:39:29 +00:00
|
|
|
//llSetRot(llRotBetween(<1,0,0>,llVecNorm(targetPosition - llGetPos())));
|
2014-12-24 16:29:19 +00:00
|
|
|
// bird wil fly x forwards and Z up
|
2011-07-09 16:39:29 +00:00
|
|
|
|
2011-07-08 17:57:07 +00:00
|
|
|
Vector3 currDirVec = Vector3.UnitX;
|
|
|
|
Vector3 desiredDirVec = Vector3.Subtract (ev, sv);
|
|
|
|
desiredDirVec.Normalize ();
|
|
|
|
|
|
|
|
Quaternion rot = Vector3.RotationBetween (currDirVec, desiredDirVec);
|
|
|
|
return rot;
|
|
|
|
}
|
|
|
|
|
|
|
|
private SceneObjectGroup CopyPrim (SceneObjectGroup prim, string name)
|
|
|
|
{
|
2014-12-31 18:50:48 +00:00
|
|
|
SceneObjectGroup copy = prim.Copy (true);
|
2011-07-08 17:57:07 +00:00
|
|
|
copy.Name = name;
|
|
|
|
copy.DetachFromBackup ();
|
|
|
|
return copy;
|
|
|
|
}
|
|
|
|
|
|
|
|
private SceneObjectGroup findByName (string name)
|
|
|
|
{
|
|
|
|
SceneObjectGroup retVal = null;
|
|
|
|
foreach (EntityBase e in m_scene.GetEntities()) {
|
|
|
|
if (e.Name == name) {
|
|
|
|
retVal = (SceneObjectGroup)e;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// can't find it so make a default one
|
|
|
|
if (retVal == null) {
|
|
|
|
retVal = MakeDefaultPrim (name);
|
|
|
|
}
|
|
|
|
|
|
|
|
return retVal;
|
|
|
|
}
|
|
|
|
|
|
|
|
private SceneObjectGroup MakeDefaultPrim (string name)
|
|
|
|
{
|
|
|
|
PrimitiveBaseShape shape = PrimitiveBaseShape.CreateSphere ();
|
2014-12-31 18:50:48 +00:00
|
|
|
shape.Scale = new Vector3 (0.5f, 0.5f, 0.5f);
|
2014-12-24 16:29:19 +00:00
|
|
|
|
2014-12-31 18:50:48 +00:00
|
|
|
SceneObjectGroup prim = new SceneObjectGroup(m_owner, new Vector3((float)m_scene.RegionInfo.RegionSizeX / 2, (float)m_scene.RegionInfo.RegionSizeY / 2, 25f), shape);
|
2011-07-08 17:57:07 +00:00
|
|
|
prim.Name = name;
|
|
|
|
prim.DetachFromBackup ();
|
|
|
|
m_scene.AddNewSceneObject (prim, false);
|
|
|
|
|
|
|
|
return prim;
|
|
|
|
}
|
|
|
|
|
2011-07-09 10:11:19 +00:00
|
|
|
private void RemoveSOGFromScene(string sogName)
|
|
|
|
{
|
|
|
|
SceneObjectGroup sog = m_sogMap[sogName];
|
|
|
|
m_scene.DeleteSceneObject(sog, false);
|
|
|
|
|
|
|
|
}
|
2011-07-08 17:57:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|