Add pCampbot "remove behaviour" console command for removing bot behaviours during operation.

Doesn't currently work very well as stopping physics, for instance, can leave bot travelling in old direction
0.7.6-extended
Justin Clark-Casey (justincc) 2013-09-03 19:05:54 +01:00
parent 7284cb76b6
commit 2a7b4c9db9
2 changed files with 60 additions and 5 deletions

View File

@ -184,6 +184,12 @@ namespace pCampBot
CreateLibOmvClient();
}
public bool TryGetBehaviour(string abbreviatedName, out IBehaviour behaviour)
{
lock (Behaviours)
return Behaviours.TryGetValue(abbreviatedName, out behaviour);
}
public bool AddBehaviour(IBehaviour behaviour)
{
lock (Behaviours)
@ -200,6 +206,12 @@ namespace pCampBot
return false;
}
public bool RemoveBehaviour(string abbreviatedName)
{
lock (Behaviours)
return Behaviours.Remove(abbreviatedName);
}
private void CreateLibOmvClient()
{
GridClient newClient = new GridClient();

View File

@ -200,11 +200,11 @@ namespace pCampBot
"Can be performed on connected or disconnected bots.",
HandleAddBehaviour);
// m_console.Commands.AddCommand(
// "bot", false, "remove behaviour", "remove behaviour <abbreviated-name> <bot-number>",
// "Remove a behaviour from a bot",
// "Can be performed on connected or disconnected bots.",
// HandleRemoveBehaviour);
m_console.Commands.AddCommand(
"bot", false, "remove behaviour", "remove behaviour <abbreviated-name> <bot-number>",
"Remove a behaviour from a bot",
"Can be performed on connected or disconnected bots.",
HandleRemoveBehaviour);
m_console.Commands.AddCommand(
"bot", false, "sit", "sit", "Sit all bots on the ground.",
@ -525,6 +525,49 @@ namespace pCampBot
string.Join(", ", behavioursAdded.ConvertAll<string>(b => b.Name).ToArray()), bot.Name);
}
private void HandleRemoveBehaviour(string module, string[] cmd)
{
if (cmd.Length != 4)
{
MainConsole.Instance.OutputFormat("Usage: remove behaviour <abbreviated-behaviour> <bot-number>");
return;
}
string rawBehaviours = cmd[2];
int botNumber;
if (!ConsoleUtil.TryParseConsoleNaturalInt(MainConsole.Instance, cmd[3], out botNumber))
return;
Bot bot = GetBotFromNumber(botNumber);
if (bot == null)
{
MainConsole.Instance.OutputFormat("Error: No bot found with number {0}", botNumber);
return;
}
HashSet<string> abbreviatedBehavioursToRemove = new HashSet<string>();
List<IBehaviour> behavioursRemoved = new List<IBehaviour>();
Array.ForEach<string>(rawBehaviours.Split(new char[] { ',' }), b => abbreviatedBehavioursToRemove.Add(b));
foreach (string b in abbreviatedBehavioursToRemove)
{
IBehaviour behaviour;
if (bot.TryGetBehaviour(b, out behaviour))
{
bot.RemoveBehaviour(b);
behavioursRemoved.Add(behaviour);
}
}
MainConsole.Instance.OutputFormat(
"Removed behaviours {0} to bot {1}",
string.Join(", ", behavioursRemoved.ConvertAll<string>(b => b.Name).ToArray()), bot.Name);
}
private void HandleDisconnect(string module, string[] cmd)
{
lock (m_bots)