separated flock size from flocking parameters and exposed parameters to command handlers

stable0711
Jon Cundill 2011-07-17 13:43:24 +01:00
parent bf2061a391
commit 9aa909ba74
4 changed files with 84 additions and 18 deletions

View File

@ -25,7 +25,7 @@ namespace Flocking
m_name = name;
if (args.Trim ().Length > 0) {
NumParams = args.Split (", ".ToCharArray ()).Length;
NumParams = args.Split (",".ToCharArray ()).Length;
} else {
NumParams = 0;
}

View File

@ -1,10 +1,11 @@
using System;
using System.Collections.Generic;
namespace Flocking
{
public struct FlockParameters
public class FlockParameters
{
public int flockSize;
// public int flockSize;
public float maxSpeed;
public float maxForce;
public float neighbourDistance;
@ -14,6 +15,67 @@ namespace Flocking
public float alignmentWeighting;
public float cohesionWeighting;
public float lookaheadDistance;
private Dictionary<string, string> m_paramDescriptions = new Dictionary<string, string> ();
public FlockParameters() {
m_paramDescriptions.Add("max-speed", "max distance boid will travel per frame");
m_paramDescriptions.Add("max-force", "max acceleration od decelleration boid can exert");
m_paramDescriptions.Add("neighbour-distance", "boid will consider other boids within this distance as part of local flock");
m_paramDescriptions.Add("desired-separation", "closest distance to other boids that our boid would like to get");
m_paramDescriptions.Add("tolerance", "how close to the edges of objects or the scene should our boid get");
m_paramDescriptions.Add("separation-weighting", "factor by which closeness to other boids should be favoured when updating position in flock");
m_paramDescriptions.Add("alignment-weighting", "factor by which alignment with other boids should be favoured when updating position in flock");
m_paramDescriptions.Add("cohesion-weighting", "factor by which keeping within the local flock should be favoured when updating position in flock");
m_paramDescriptions.Add("lookahead-distance", "how far in front should the boid look for edges and boundaries");
}
public bool IsValidParameter (string name)
{
return m_paramDescriptions.ContainsKey (name);
}
public void SetParameter (string name, string newVal)
{
switch (name) {
case "max-speed":
maxSpeed = Convert.ToSingle(newVal);
break;
case "max-force":
maxForce = Convert.ToSingle(newVal);
break;
case "neighbour-distance":
neighbourDistance = Convert.ToSingle(newVal);
break;
case "desired-separation":
desiredSeparation = Convert.ToSingle(newVal);
break;
case "tolerance":
tolerance = Convert.ToSingle(newVal);
break;
case "separation-weighting":
separationWeighting = Convert.ToSingle(newVal);
break;
case "alignment-weighting":
alignmentWeighting = Convert.ToSingle(newVal);
break;
case "cohesion-weighting":
cohesionWeighting = Convert.ToSingle(newVal);
break;
case "lookahead-distance":
lookaheadDistance = Convert.ToSingle(newVal);
break;
}
}
public string GetList ()
{
string retVal = Environment.NewLine;
foreach (string name in m_paramDescriptions.Keys) {
retVal += name + " - " + m_paramDescriptions [name] + Environment.NewLine;
}
return retVal;
}
}
}

View File

@ -37,9 +37,10 @@ namespace Flocking
private FlowField m_flowField;
private FlockParameters m_parameters;
private Random m_rnd = new Random(Environment.TickCount);
private int m_size;
public int Size {
get {return m_flock.Count;}
get {return m_size;}
set {
if( value < m_flock.Count ) {
m_flock.RemoveRange( 0, m_flock.Count - value );
@ -89,29 +90,26 @@ namespace Flocking
public float SeparationWeighting {
get{ return m_parameters.separationWeighting; }
set{ m_parameters.separationWeighting = value;}
}
public float AlignmentWeighting {
get{ return m_parameters.alignmentWeighting; }
set{ m_parameters.alignmentWeighting = value;}
}
public float CohesionWeighting {
get{ return m_parameters.cohesionWeighting; }
set{ m_parameters.cohesionWeighting = value;}
}
public float LookaheadDistance {
get { return m_parameters.lookaheadDistance; }
set { m_parameters.lookaheadDistance = value;}
}
public void Initialise (FlowField flowField)
public void Initialise (int size, FlowField flowField)
{
m_flowField = flowField;
for (int i = 0; i < m_parameters.flockSize; i++) {
m_flowField = flowField;
m_size = size;
for (int i = 0; i < m_size; i++) {
AddBoid ("boid"+i );
}
}

View File

@ -56,6 +56,7 @@ namespace Flocking
private string m_boidPrim;
private ChatCommandParser m_chatCommandParser;
private FlockParameters m_parameters;
private int m_flockSize = 100;
private UUID m_owner;
@ -73,7 +74,7 @@ namespace Flocking
m_boidPrim = config.GetString ("boid-prim", "boidPrim");
m_parameters = new FlockParameters();
m_parameters.flockSize = config.GetInt ("flock-size", 100);
m_flockSize = config.GetInt ("flock-size", 100);
m_parameters.maxSpeed = config.GetFloat("max-speed", 3f);
m_parameters.maxForce = config.GetFloat("max-force", 0.25f);
m_parameters.neighbourDistance = config.GetFloat("neighbour-dist", 25f);
@ -110,11 +111,6 @@ namespace Flocking
}
}
void chatCom (object sender, OSChatMessage chat)
{
}
public void RegionLoaded (Scene scene)
{
if (m_enabled) {
@ -123,7 +119,7 @@ namespace Flocking
FlowField field = new FlowField(scene, new Vector3(128f, 128f, 128f), 200, 200, 200);
// Generate initial flock values
m_model.Initialise (field);
m_model.Initialise (m_flockSize, field);
// who is the owner for the flock in this region
m_owner = m_scene.RegionInfo.EstateSettings.EstateOwner;
@ -220,6 +216,16 @@ namespace Flocking
public void HandleSetParameterCmd(string module, string[] args)
{
if (ShouldHandleCmd ()) {
string name = args[1];
string newVal = args[2];
if( m_parameters.IsValidParameter( name ) ) {
m_parameters.SetParameter(name, newVal);
} else {
bool inWorld = IsInWorldCmd( ref args);
ShowResponse( name + "is not a valid flock parameter", inWorld );
ShowResponse( "valid parameters are: " + m_parameters.GetList(), inWorld);
}
}
}