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>();
|
||||
|
||||
public int Size {
|
||||
get {return flock.Count;}
|
||||
}
|
||||
|
||||
public void Initialise (int num, int xRange, int yRange, int zRange)
|
||||
{
|
||||
|
|
|
@ -57,6 +57,11 @@ namespace Flocking
|
|||
private uint m_frame = 0;
|
||||
private int m_frameUpdateRate = 1;
|
||||
|
||||
private int m_chatChannel = 118;
|
||||
private string m_boidPrim;
|
||||
|
||||
private UUID m_owner;
|
||||
|
||||
|
||||
#region IRegionModule Members
|
||||
|
||||
|
@ -64,22 +69,33 @@ namespace Flocking
|
|||
|
||||
public void Initialise (IConfigSource source)
|
||||
{
|
||||
//TODO: check if we are in the ini files
|
||||
//TODO: if so get some physical constants out of them and pass into the model
|
||||
//check if we are in the ini files
|
||||
//if so get some physical constants out of them and pass into the model
|
||||
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)
|
||||
{
|
||||
m_log.Info("ADDING FLOCKING");
|
||||
m_scene = scene;
|
||||
if (m_enabled) {
|
||||
m_scene.EventManager.OnFrame += FlockUpdate;
|
||||
//register commands
|
||||
RegisterCommands();
|
||||
|
||||
//register handlers
|
||||
m_scene.EventManager.OnFrame += FlockUpdate;
|
||||
m_scene.EventManager.OnChatFromClient += SimChatSent; //listen for commands sent from the client
|
||||
|
||||
// init module
|
||||
m_model = new FlockingModel();
|
||||
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) {
|
||||
// Generate initial flock values
|
||||
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
|
||||
m_ready = true;
|
||||
|
@ -99,6 +118,7 @@ namespace Flocking
|
|||
{
|
||||
if (m_enabled) {
|
||||
m_scene.EventManager.OnFrame -= FlockUpdate;
|
||||
m_scene.EventManager.OnChatFromClient -= SimChatSent;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -117,7 +137,7 @@ namespace Flocking
|
|||
|
||||
public void FlockUpdate()
|
||||
{
|
||||
if (((m_frame++ % m_frameUpdateRate) != 0) || !m_ready)
|
||||
if (((m_frame++ % m_frameUpdateRate) != 0) || !m_ready || !m_enabled)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
@ -130,8 +150,124 @@ namespace Flocking
|
|||
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
|
||||
|
||||
#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
|
||||
|
||||
|
||||
|
||||
|
|
|
@ -46,37 +46,45 @@ namespace Flocking
|
|||
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)
|
||||
{
|
||||
foreach (Boid boid in boids) {
|
||||
SceneObjectGroup sog = DrawBoid (boid);
|
||||
//sog.ScheduleGroupForTerseUpdate ();
|
||||
DrawBoid (boid);
|
||||
}
|
||||
}
|
||||
|
||||
private SceneObjectGroup DrawBoid (Boid boid)
|
||||
private void DrawBoid (Boid boid)
|
||||
{
|
||||
SceneObjectPart existing = m_scene.GetSceneObjectPart (boid.Id);
|
||||
|
||||
|
||||
SceneObjectGroup copy;
|
||||
SceneObjectGroup sog;
|
||||
if (existing == null) {
|
||||
SceneObjectGroup group = findByName ("boidPrim");
|
||||
copy = CopyPrim (group, boid.Id);
|
||||
m_sogMap [boid.Id] = copy;
|
||||
m_scene.AddNewSceneObject (copy, false);
|
||||
sog = CopyPrim (group, boid.Id);
|
||||
m_sogMap [boid.Id] = sog;
|
||||
m_scene.AddNewSceneObject (sog, false);
|
||||
} else {
|
||||
copy = existing.ParentGroup;
|
||||
sog = existing.ParentGroup;
|
||||
}
|
||||
|
||||
Quaternion rotation = CalcRotationToEndpoint (copy, copy.AbsolutePosition, boid.Location);
|
||||
copy.UpdateGroupRotationPR (boid.Location, rotation);
|
||||
return copy;
|
||||
Quaternion rotation = CalcRotationToEndpoint (sog, sog.AbsolutePosition, boid.Location);
|
||||
sog.UpdateGroupRotationPR (boid.Location, rotation);
|
||||
}
|
||||
|
||||
private static Quaternion CalcRotationToEndpoint (SceneObjectGroup copy, Vector3 sv, Vector3 ev)
|
||||
|
@ -138,6 +146,12 @@ namespace Flocking
|
|||
return prim;
|
||||
}
|
||||
|
||||
private void RemoveSOGFromScene(string sogName)
|
||||
{
|
||||
SceneObjectGroup sog = m_sogMap[sogName];
|
||||
m_scene.DeleteSceneObject(sog, false);
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue