added ability to change flock size

stable0711
Jon Cundill 2011-07-09 12:35:13 +01:00
parent 57717a3a79
commit 72db5e2cc8
3 changed files with 101 additions and 77 deletions

View File

@ -34,17 +34,39 @@ namespace Flocking
{ {
private List<Boid> flock = new List<Boid>(); private List<Boid> flock = new List<Boid>();
private int m_xRange = 200;
private int m_yRange = 200;
private int m_zRange = 200;
public int Size { public int Size {
get {return flock.Count;} get {return flock.Count;}
set {
if( value < flock.Count ) {
flock.RemoveRange( 0, flock.Count - value );
} else while( value > flock.Count ) {
AddBoid( "boid"+flock.Count);
} }
}
}
void AddBoid (string name)
{
Boid boid = new Boid (name, 3.0f, 0.05f);
boid.Location = new Vector3 (m_xRange / 2f, m_yRange / 2f, m_zRange / 2f);
flock.Add (boid);
}
public void Initialise (int num, int xRange, int yRange, int zRange) public void Initialise (int num, int xRange, int yRange, int zRange)
{ {
m_xRange = xRange;
m_yRange = yRange;
m_zRange = zRange;
//TODO: fill in the initial Flock array properly //TODO: fill in the initial Flock array properly
for (int i = 0; i < num; i++) { for (int i = 0; i < num; i++) {
Boid boid = new Boid ("boid"+i, 3.0f, 0.05f); AddBoid ("boid"+i );
boid.Location = new Vector3 (xRange / 2f, yRange / 2f, zRange / 2f);
flock.Add (boid);
} }
} }

View File

@ -37,32 +37,25 @@ using OpenSim.Region.Framework.Scenes;
using OpenSim.Framework; using OpenSim.Framework;
using OpenSim.Framework.Console; using OpenSim.Framework.Console;
namespace Flocking namespace Flocking
{ {
public class FlockingModule : INonSharedRegionModule public class FlockingModule : INonSharedRegionModule
{ {
private static readonly ILog m_log = LogManager.GetLogger (System.Reflection.MethodBase.GetCurrentMethod ().DeclaringType); private static readonly ILog m_log = LogManager.GetLogger (System.Reflection.MethodBase.GetCurrentMethod ().DeclaringType);
static object m_sync = new object();
private Scene m_scene; private Scene m_scene;
private FlockingModel m_model; private FlockingModel m_model;
private FlockingView m_view; private FlockingView m_view;
private bool m_enabled = false; private bool m_enabled = false;
private bool m_ready = false; private bool m_ready = false;
private uint m_frame = 0; private uint m_frame = 0;
private int m_frameUpdateRate = 1; private int m_frameUpdateRate = 1;
private int m_chatChannel = 118; private int m_chatChannel = 118;
private string m_boidPrim; private string m_boidPrim;
private UUID m_owner; private UUID m_owner;
#region IRegionModule Members #region IRegionModule Members
@ -71,10 +64,10 @@ namespace Flocking
{ {
//check if we are in the ini files //check if we are in the ini files
//if so get some physical constants out of them and pass into the model //if so get some physical constants out of them and pass into the model
IConfig config = source.Configs["Boids"]; IConfig config = source.Configs ["Boids"];
if (config != null) { if (config != null) {
m_chatChannel = config.GetInt( "chat-channel", 118 ); m_chatChannel = config.GetInt ("chat-channel", 118);
m_boidPrim = config.GetString( "boid-prim", "boidPrim" ); m_boidPrim = config.GetString ("boid-prim", "boidPrim");
// we're in the config - so turn on this module // we're in the config - so turn on this module
m_enabled = true; m_enabled = true;
@ -83,18 +76,18 @@ namespace Flocking
public void AddRegion (Scene scene) public void AddRegion (Scene scene)
{ {
m_log.Info("ADDING FLOCKING"); m_log.Info ("ADDING FLOCKING");
m_scene = scene; m_scene = scene;
if (m_enabled) { if (m_enabled) {
//register commands //register commands
RegisterCommands(); RegisterCommands ();
//register handlers //register handlers
m_scene.EventManager.OnFrame += FlockUpdate; m_scene.EventManager.OnFrame += FlockUpdate;
m_scene.EventManager.OnChatFromClient += SimChatSent; //listen for commands sent from the client m_scene.EventManager.OnChatFromClient += SimChatSent; //listen for commands sent from the client
// init module // init module
m_model = new FlockingModel(); m_model = new FlockingModel ();
m_view = new FlockingView (m_scene); m_view = new FlockingView (m_scene);
} }
} }
@ -103,11 +96,11 @@ namespace Flocking
{ {
if (m_enabled) { if (m_enabled) {
// Generate initial flock values // Generate initial flock values
m_model.Initialise( 200, 255, 255, 255); m_model.Initialise (200, 255, 255, 255);
// who is the owner for the flock in this region // who is the owner for the flock in this region
m_owner = m_scene.RegionInfo.EstateSettings.EstateOwner; m_owner = m_scene.RegionInfo.EstateSettings.EstateOwner;
m_view.PostInitialize(m_owner); m_view.PostInitialize (m_owner);
// Mark Module Ready for duty // Mark Module Ready for duty
m_ready = true; m_ready = true;
@ -122,7 +115,6 @@ namespace Flocking
} }
} }
public string Name { public string Name {
get { return "FlockingModule"; } get { return "FlockingModule"; }
} }
@ -135,10 +127,9 @@ namespace Flocking
#region EventHandlers #region EventHandlers
public void FlockUpdate() public void FlockUpdate ()
{
if (((m_frame++ % m_frameUpdateRate) != 0) || !m_ready || !m_enabled)
{ {
if (((m_frame++ % m_frameUpdateRate) != 0) || !m_ready || !m_enabled) {
return; return;
} }
@ -146,37 +137,39 @@ namespace Flocking
// work out where everyone has moved to // work out where everyone has moved to
// and tell the scene to render the new positions // and tell the scene to render the new positions
List<Boid> boids = m_model.UpdateFlockPos(); lock( m_sync ) {
m_view.Render(boids); List<Boid > boids = m_model.UpdateFlockPos ();
m_view.Render (boids);
}
} }
protected void SimChatSent (Object x, OSChatMessage msg) protected void SimChatSent (Object x, OSChatMessage msg)
{ {
m_log.Info("got msg"); m_log.Info ("got msg");
if (m_scene.ConsoleScene () != m_scene) if (m_scene.ConsoleScene () != m_scene)
return; // not for us return; // not for us
m_log.Info("got channel" + msg.Channel); m_log.Info ("got channel" + msg.Channel);
if (msg.Channel != m_chatChannel) if (msg.Channel != m_chatChannel)
return; // not for us return; // not for us
// try and parse a valid cmd from this msg // try and parse a valid cmd from this msg
string cmd = msg.Message.ToLower (); string cmd = msg.Message.ToLower ();
m_log.Info("got cmd " + cmd); m_log.Info ("got cmd " + cmd);
//stick ui in the args so we know to respond in world //stick ui in the args so we know to respond in world
string[] args = ("<ui> " + cmd).Split (" ".ToCharArray ()); string[] args = (cmd + " <ui>").Split (" ".ToCharArray ());
if (cmd.StartsWith ("stop")) { if (cmd.StartsWith ("stop")) {
HandleStopCmd("flock", args); HandleStopCmd ("flock", args);
} else if ( cmd.StartsWith( "start" ) ) { } else if (cmd.StartsWith ("start")) {
HandleStartCmd("flock", args); HandleStartCmd ("flock", args);
} else if ( cmd.StartsWith( "size" ) ) { } else if (cmd.StartsWith ("size")) {
HandleSetSizeCmd("flock", args); HandleSetSizeCmd ("flock", args);
} else if ( cmd.StartsWith ( "stats" ) ) { } else if (cmd.StartsWith ("stats")) {
HandleShowStatsCmd("flock", args ); HandleShowStatsCmd ("flock", args);
} else if ( cmd.StartsWith ( "prim" ) ) { } else if (cmd.StartsWith ("prim")) {
HandleSetPrimCmd("flock", args); HandleSetPrimCmd ("flock", args);
} }
} }
@ -188,14 +181,14 @@ namespace Flocking
private void AddCommand (string cmd, string args, string help, CommandDelegate fn) private void AddCommand (string cmd, string args, string help, CommandDelegate fn)
{ {
string argStr = ""; string argStr = "";
if( args.Trim().Length > 0 ) { if (args.Trim ().Length > 0) {
argStr = " <" + args + "> "; argStr = " <" + args + "> ";
} }
m_scene.AddCommand (this, "flock-" + cmd, "flock-" + cmd + argStr, help, fn); m_scene.AddCommand (this, "flock-" + cmd, "flock-" + cmd + argStr, help, fn);
} }
private void RegisterCommands ()
private void RegisterCommands() { {
AddCommand ("stop", "", "Stop all Flocking", HandleStopCmd); AddCommand ("stop", "", "Stop all Flocking", HandleStopCmd);
AddCommand ("start", "", "Start Flocking", HandleStartCmd); AddCommand ("start", "", "Start Flocking", HandleStartCmd);
AddCommand ("size", "num", "Adjust the size of the flock ", HandleSetSizeCmd); AddCommand ("size", "num", "Adjust the size of the flock ", HandleSetSizeCmd);
@ -203,24 +196,27 @@ namespace Flocking
AddCommand ("prim", "name", "set the prim used for each boid to that passed in", HandleSetPrimCmd); AddCommand ("prim", "name", "set the prim used for each boid to that passed in", HandleSetPrimCmd);
} }
private bool ShouldHandleCmd() { private bool ShouldHandleCmd ()
{
return m_scene.ConsoleScene () == m_scene; return m_scene.ConsoleScene () == m_scene;
} }
private bool IsInWorldCmd( ref string [] args ) { private bool IsInWorldCmd (ref string [] args)
{
bool retVal = false; bool retVal = false;
if( args.Length > 0 && args[0].Equals("<ui>") ) { if (args.Length > 0 && args [args.Length - 1].Equals ("<ui>")) {
retVal = true; retVal = true;
} }
return retVal; return retVal;
} }
private void ShowResponse(string response, bool inWorld) { private void ShowResponse (string response, bool inWorld)
if( inWorld ){ {
if (inWorld) {
IClientAPI ownerAPI = null; IClientAPI ownerAPI = null;
if( m_scene.TryGetClient( m_owner, out ownerAPI ) ) { if (m_scene.TryGetClient (m_owner, out ownerAPI)) {
ownerAPI.SendBlueBoxMessage( m_owner,"osboids", response ); ownerAPI.SendBlueBoxMessage (m_owner, "osboids", response);
} }
} else { } else {
MainConsole.Instance.Output (response); MainConsole.Instance.Output (response);
@ -229,44 +225,51 @@ namespace Flocking
public void HandleStopCmd (string module, string[] args) public void HandleStopCmd (string module, string[] args)
{ {
if( ShouldHandleCmd() ) { if (ShouldHandleCmd ()) {
m_log.Info("stop the flocking capability"); m_log.Info ("stop the flocking capability");
m_enabled = false; m_enabled = false;
m_view.Clear(); m_view.Clear ();
} }
} }
public void HandleStartCmd (string module, string[] args) public void HandleStartCmd (string module, string[] args)
{ {
if( ShouldHandleCmd() ) { if (ShouldHandleCmd ()) {
m_log.Info("start the flocking capability"); m_log.Info ("start the flocking capability");
m_enabled = true; m_enabled = true;
FlockUpdate(); FlockUpdate ();
} }
} }
public void HandleSetSizeCmd (string module, string[] args) public void HandleSetSizeCmd (string module, string[] args)
{ {
if( ShouldHandleCmd() ) { if (ShouldHandleCmd ()) {
m_log.Info("set size not implemented yet"); lock( m_sync ) {
//m_enabled = false;
//m_log.Info( args );
int newSize = Convert.ToInt32(args[1]);
m_model.Size = newSize;
m_view.Clear();
//m_enabled = true;
}
} }
} }
public void HandleShowStatsCmd (string module, string[] args) public void HandleShowStatsCmd (string module, string[] args)
{ {
if( ShouldHandleCmd() ) { if (ShouldHandleCmd ()) {
bool inWorld = IsInWorldCmd( ref args ); bool inWorld = IsInWorldCmd (ref args);
ShowResponse("Num Boids = " + m_model.Size, inWorld ); ShowResponse ("Num Boids = " + m_model.Size, inWorld);
} }
} }
public void HandleSetPrimCmd (string module, string[] args) public void HandleSetPrimCmd (string module, string[] args)
{ {
if( ShouldHandleCmd() ) { if (ShouldHandleCmd ()) {
m_log.Info("set prim not implemented yet"); m_log.Info ("set prim not implemented yet");
} }
} }
#endregion #endregion
@ -279,8 +282,6 @@ namespace Flocking
{ {
} }
public Type ReplaceableInterface { public Type ReplaceableInterface {
get { return null; } get { return null; }
} }

View File

@ -84,7 +84,8 @@ namespace Flocking
} }
Quaternion rotation = CalcRotationToEndpoint (sog, sog.AbsolutePosition, boid.Location); Quaternion rotation = CalcRotationToEndpoint (sog, sog.AbsolutePosition, boid.Location);
sog.UpdateGroupRotationPR (boid.Location, rotation); sog.UpdateGroupPosition(boid.Location);
sog.UpdateGroupRotationR (rotation);
} }
private static Quaternion CalcRotationToEndpoint (SceneObjectGroup copy, Vector3 sv, Vector3 ev) private static Quaternion CalcRotationToEndpoint (SceneObjectGroup copy, Vector3 sv, Vector3 ev)