basic controller structure added
parent
7f38d0537e
commit
57717a3a79
Binary file not shown.
|
@ -34,6 +34,9 @@ namespace Flocking
|
||||||
{
|
{
|
||||||
private List<Boid> flock = new List<Boid>();
|
private List<Boid> flock = new List<Boid>();
|
||||||
|
|
||||||
|
public int Size {
|
||||||
|
get {return flock.Count;}
|
||||||
|
}
|
||||||
|
|
||||||
public void Initialise (int num, int xRange, int yRange, int zRange)
|
public void Initialise (int num, int xRange, int yRange, int zRange)
|
||||||
{
|
{
|
||||||
|
|
|
@ -56,6 +56,11 @@ namespace Flocking
|
||||||
|
|
||||||
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 string m_boidPrim;
|
||||||
|
|
||||||
|
private UUID m_owner;
|
||||||
|
|
||||||
|
|
||||||
#region IRegionModule Members
|
#region IRegionModule Members
|
||||||
|
@ -64,9 +69,16 @@ namespace Flocking
|
||||||
|
|
||||||
public void Initialise (IConfigSource source)
|
public void Initialise (IConfigSource source)
|
||||||
{
|
{
|
||||||
//TODO: check if we are in the ini files
|
//check if we are in the ini files
|
||||||
//TODO: 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
|
||||||
m_enabled = true;
|
IConfig config = source.Configs["Boids"];
|
||||||
|
if (config != null) {
|
||||||
|
m_chatChannel = config.GetInt( "chat-channel", 118 );
|
||||||
|
m_boidPrim = config.GetString( "boid-prim", "boidPrim" );
|
||||||
|
|
||||||
|
// we're in the config - so turn on this module
|
||||||
|
m_enabled = true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void AddRegion (Scene scene)
|
public void AddRegion (Scene scene)
|
||||||
|
@ -74,12 +86,16 @@ namespace Flocking
|
||||||
m_log.Info("ADDING FLOCKING");
|
m_log.Info("ADDING FLOCKING");
|
||||||
m_scene = scene;
|
m_scene = scene;
|
||||||
if (m_enabled) {
|
if (m_enabled) {
|
||||||
|
//register commands
|
||||||
|
RegisterCommands();
|
||||||
|
|
||||||
|
//register handlers
|
||||||
m_scene.EventManager.OnFrame += FlockUpdate;
|
m_scene.EventManager.OnFrame += FlockUpdate;
|
||||||
|
m_scene.EventManager.OnChatFromClient += SimChatSent; //listen for commands sent from the client
|
||||||
|
|
||||||
|
// init module
|
||||||
m_model = new FlockingModel();
|
m_model = new FlockingModel();
|
||||||
m_view = new FlockingView (m_scene);
|
m_view = new FlockingView (m_scene);
|
||||||
|
|
||||||
m_scene.AddCommand (this, "flocking", "I haz got a Flocking Module", "wotever" , null);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -88,7 +104,10 @@ 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);
|
||||||
m_view.PostInitialize();
|
|
||||||
|
// who is the owner for the flock in this region
|
||||||
|
m_owner = m_scene.RegionInfo.EstateSettings.EstateOwner;
|
||||||
|
m_view.PostInitialize(m_owner);
|
||||||
|
|
||||||
// Mark Module Ready for duty
|
// Mark Module Ready for duty
|
||||||
m_ready = true;
|
m_ready = true;
|
||||||
|
@ -99,6 +118,7 @@ namespace Flocking
|
||||||
{
|
{
|
||||||
if (m_enabled) {
|
if (m_enabled) {
|
||||||
m_scene.EventManager.OnFrame -= FlockUpdate;
|
m_scene.EventManager.OnFrame -= FlockUpdate;
|
||||||
|
m_scene.EventManager.OnChatFromClient -= SimChatSent;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -117,7 +137,7 @@ namespace Flocking
|
||||||
|
|
||||||
public void FlockUpdate()
|
public void FlockUpdate()
|
||||||
{
|
{
|
||||||
if (((m_frame++ % m_frameUpdateRate) != 0) || !m_ready)
|
if (((m_frame++ % m_frameUpdateRate) != 0) || !m_ready || !m_enabled)
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -129,10 +149,126 @@ namespace Flocking
|
||||||
List<Boid> boids = m_model.UpdateFlockPos();
|
List<Boid> boids = m_model.UpdateFlockPos();
|
||||||
m_view.Render(boids);
|
m_view.Render(boids);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected void SimChatSent (Object x, OSChatMessage msg)
|
||||||
|
{
|
||||||
|
m_log.Info("got msg");
|
||||||
|
if (m_scene.ConsoleScene () != m_scene)
|
||||||
|
return; // not for us
|
||||||
|
|
||||||
|
m_log.Info("got channel" + msg.Channel);
|
||||||
|
if (msg.Channel != m_chatChannel)
|
||||||
|
return; // not for us
|
||||||
|
|
||||||
|
// try and parse a valid cmd from this msg
|
||||||
|
string cmd = msg.Message.ToLower ();
|
||||||
|
m_log.Info("got cmd " + cmd);
|
||||||
|
|
||||||
|
//stick ui in the args so we know to respond in world
|
||||||
|
string[] args = ("<ui> " + cmd).Split (" ".ToCharArray ());
|
||||||
|
|
||||||
|
if (cmd.StartsWith ("stop")) {
|
||||||
|
HandleStopCmd("flock", args);
|
||||||
|
} else if ( cmd.StartsWith( "start" ) ) {
|
||||||
|
HandleStartCmd("flock", args);
|
||||||
|
} else if ( cmd.StartsWith( "size" ) ) {
|
||||||
|
HandleSetSizeCmd("flock", args);
|
||||||
|
} else if ( cmd.StartsWith ( "stats" ) ) {
|
||||||
|
HandleShowStatsCmd("flock", args );
|
||||||
|
} else if ( cmd.StartsWith ( "prim" ) ) {
|
||||||
|
HandleSetPrimCmd("flock", args);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
#region Command Handling
|
||||||
|
|
||||||
|
private void AddCommand (string cmd, string args, string help, CommandDelegate fn)
|
||||||
|
{
|
||||||
|
string argStr = "";
|
||||||
|
if( args.Trim().Length > 0 ) {
|
||||||
|
argStr = " <" + args + "> ";
|
||||||
|
}
|
||||||
|
m_scene.AddCommand (this, "flock-" + cmd, "flock-" + cmd + argStr, help, fn);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private void RegisterCommands() {
|
||||||
|
AddCommand ("stop", "", "Stop all Flocking", HandleStopCmd);
|
||||||
|
AddCommand ("start", "", "Start Flocking", HandleStartCmd);
|
||||||
|
AddCommand ("size", "num", "Adjust the size of the flock ", HandleSetSizeCmd);
|
||||||
|
AddCommand ("stats", "", "show flocking stats", HandleShowStatsCmd);
|
||||||
|
AddCommand ("prim", "name", "set the prim used for each boid to that passed in", HandleSetPrimCmd);
|
||||||
|
}
|
||||||
|
|
||||||
|
private bool ShouldHandleCmd() {
|
||||||
|
return m_scene.ConsoleScene () == m_scene;
|
||||||
|
}
|
||||||
|
|
||||||
|
private bool IsInWorldCmd( ref string [] args ) {
|
||||||
|
bool retVal = false;
|
||||||
|
|
||||||
|
if( args.Length > 0 && args[0].Equals("<ui>") ) {
|
||||||
|
retVal = true;
|
||||||
|
}
|
||||||
|
return retVal;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void ShowResponse(string response, bool inWorld) {
|
||||||
|
if( inWorld ){
|
||||||
|
IClientAPI ownerAPI = null;
|
||||||
|
if( m_scene.TryGetClient( m_owner, out ownerAPI ) ) {
|
||||||
|
ownerAPI.SendBlueBoxMessage( m_owner,"osboids", response );
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
MainConsole.Instance.Output (response);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void HandleStopCmd (string module, string[] args)
|
||||||
|
{
|
||||||
|
if( ShouldHandleCmd() ) {
|
||||||
|
m_log.Info("stop the flocking capability");
|
||||||
|
m_enabled = false;
|
||||||
|
m_view.Clear();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void HandleStartCmd (string module, string[] args)
|
||||||
|
{
|
||||||
|
if( ShouldHandleCmd() ) {
|
||||||
|
m_log.Info("start the flocking capability");
|
||||||
|
m_enabled = true;
|
||||||
|
FlockUpdate();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void HandleSetSizeCmd (string module, string[] args)
|
||||||
|
{
|
||||||
|
if( ShouldHandleCmd() ) {
|
||||||
|
m_log.Info("set size not implemented yet");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void HandleShowStatsCmd (string module, string[] args)
|
||||||
|
{
|
||||||
|
if( ShouldHandleCmd() ) {
|
||||||
|
bool inWorld = IsInWorldCmd( ref args );
|
||||||
|
ShowResponse("Num Boids = " + m_model.Size, inWorld );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public void HandleSetPrimCmd (string module, string[] args)
|
||||||
|
{
|
||||||
|
if( ShouldHandleCmd() ) {
|
||||||
|
m_log.Info("set prim not implemented yet");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#region IRegionModuleBase Members
|
#region IRegionModuleBase Members
|
||||||
|
|
|
@ -40,43 +40,51 @@ namespace Flocking
|
||||||
private UUID m_owner;
|
private UUID m_owner;
|
||||||
|
|
||||||
private Dictionary<string, SceneObjectGroup> m_sogMap = new Dictionary<string, SceneObjectGroup> ();
|
private Dictionary<string, SceneObjectGroup> m_sogMap = new Dictionary<string, SceneObjectGroup> ();
|
||||||
|
|
||||||
public FlockingView (Scene scene)
|
public FlockingView (Scene scene)
|
||||||
{
|
{
|
||||||
m_scene = scene;
|
m_scene = scene;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void PostInitialize ()
|
public void PostInitialize (UUID owner)
|
||||||
{
|
{
|
||||||
m_owner = m_scene.RegionInfo.EstateSettings.EstateOwner;
|
m_owner = owner;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void Clear ()
|
||||||
|
{
|
||||||
|
//trash everything we have
|
||||||
|
foreach (string name in m_sogMap.Keys)
|
||||||
|
{
|
||||||
|
RemoveSOGFromScene(name);
|
||||||
|
}
|
||||||
|
m_sogMap.Clear();
|
||||||
|
}
|
||||||
|
|
||||||
public void Render (List<Boid> boids)
|
public void Render (List<Boid> boids)
|
||||||
{
|
{
|
||||||
foreach (Boid boid in boids) {
|
foreach (Boid boid in boids) {
|
||||||
SceneObjectGroup sog = DrawBoid (boid);
|
DrawBoid (boid);
|
||||||
//sog.ScheduleGroupForTerseUpdate ();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private SceneObjectGroup DrawBoid (Boid boid)
|
private void DrawBoid (Boid boid)
|
||||||
{
|
{
|
||||||
SceneObjectPart existing = m_scene.GetSceneObjectPart (boid.Id);
|
SceneObjectPart existing = m_scene.GetSceneObjectPart (boid.Id);
|
||||||
|
|
||||||
|
|
||||||
SceneObjectGroup copy;
|
SceneObjectGroup sog;
|
||||||
if (existing == null) {
|
if (existing == null) {
|
||||||
SceneObjectGroup group = findByName ("boidPrim");
|
SceneObjectGroup group = findByName ("boidPrim");
|
||||||
copy = CopyPrim (group, boid.Id);
|
sog = CopyPrim (group, boid.Id);
|
||||||
m_sogMap [boid.Id] = copy;
|
m_sogMap [boid.Id] = sog;
|
||||||
m_scene.AddNewSceneObject (copy, false);
|
m_scene.AddNewSceneObject (sog, false);
|
||||||
} else {
|
} else {
|
||||||
copy = existing.ParentGroup;
|
sog = existing.ParentGroup;
|
||||||
}
|
}
|
||||||
|
|
||||||
Quaternion rotation = CalcRotationToEndpoint (copy, copy.AbsolutePosition, boid.Location);
|
Quaternion rotation = CalcRotationToEndpoint (sog, sog.AbsolutePosition, boid.Location);
|
||||||
copy.UpdateGroupRotationPR (boid.Location, rotation);
|
sog.UpdateGroupRotationPR (boid.Location, rotation);
|
||||||
return copy;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Quaternion CalcRotationToEndpoint (SceneObjectGroup copy, Vector3 sv, Vector3 ev)
|
private static Quaternion CalcRotationToEndpoint (SceneObjectGroup copy, Vector3 sv, Vector3 ev)
|
||||||
|
@ -138,6 +146,12 @@ namespace Flocking
|
||||||
return prim;
|
return prim;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void RemoveSOGFromScene(string sogName)
|
||||||
|
{
|
||||||
|
SceneObjectGroup sog = m_sogMap[sogName];
|
||||||
|
m_scene.DeleteSceneObject(sog, false);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue