From 9c3c9b7f5f62a7ed892f691180b765a9190cbbcc Mon Sep 17 00:00:00 2001 From: Justin Clark-Casey (justincc) Date: Tue, 3 Sep 2013 19:57:34 +0100 Subject: Make pCampbot "add behaviour" and "remove behaviour" console commands work for all bots if no bot number is given --- OpenSim/Tools/pCampBot/BotManager.cs | 123 ++++++++++++++++++++++------------- 1 file changed, 78 insertions(+), 45 deletions(-) (limited to 'OpenSim/Tools') diff --git a/OpenSim/Tools/pCampBot/BotManager.cs b/OpenSim/Tools/pCampBot/BotManager.cs index 6433c2e..3c1b11e 100644 --- a/OpenSim/Tools/pCampBot/BotManager.cs +++ b/OpenSim/Tools/pCampBot/BotManager.cs @@ -195,15 +195,17 @@ namespace pCampBot HandleDisconnect); m_console.Commands.AddCommand( - "bot", false, "add behaviour", "add behaviour ", + "bot", false, "add behaviour", "add behaviour []", "Add a behaviour to a bot", - "Can be performed on connected or disconnected bots.", + "If no bot number is specified then behaviour is added to all bots.\n" + + "Can be performed on connected or disconnected bots.", HandleAddBehaviour); m_console.Commands.AddCommand( - "bot", false, "remove behaviour", "remove behaviour ", + "bot", false, "remove behaviour", "remove behaviour []", "Remove a behaviour from a bot", - "Can be performed on connected or disconnected bots.", + "If no bot number is specified then behaviour is added to all bots.\n" + + "Can be performed on connected or disconnected bots.", HandleRemoveBehaviour); m_console.Commands.AddCommand( @@ -224,7 +226,7 @@ namespace pCampBot "bot", false, "show bots", "show bots", "Shows the status of all bots", HandleShowBotsStatus); m_console.Commands.AddCommand( - "bot", false, "show bot", "show bot ", + "bot", false, "show bot", "show bot ", "Shows the detailed status and settings of a particular bot.", HandleShowBotStatus); m_bots = new List(); @@ -489,83 +491,114 @@ namespace pCampBot private void HandleAddBehaviour(string module, string[] cmd) { - if (cmd.Length != 4) + if (cmd.Length < 3 || cmd.Length > 4) { - MainConsole.Instance.OutputFormat("Usage: add behaviour "); + MainConsole.Instance.OutputFormat("Usage: add behaviour []"); return; } string rawBehaviours = cmd[2]; - int botNumber; - - if (!ConsoleUtil.TryParseConsoleNaturalInt(MainConsole.Instance, cmd[3], out botNumber)) - return; - Bot bot = GetBotFromNumber(botNumber); + List botsToEffect = new List(); - if (bot == null) + if (cmd.Length == 3) { - MainConsole.Instance.OutputFormat("Error: No bot found with number {0}", botNumber); - return; + lock (m_bots) + botsToEffect.AddRange(m_bots); } + else + { + 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; + } + + botsToEffect.Add(bot); + } + HashSet rawAbbreviatedSwitchesToAdd = new HashSet(); Array.ForEach(rawBehaviours.Split(new char[] { ',' }), b => rawAbbreviatedSwitchesToAdd.Add(b)); - List behavioursAdded = new List(); - - foreach (IBehaviour behaviour in CreateBehavioursFromAbbreviatedNames(rawAbbreviatedSwitchesToAdd)) + foreach (Bot bot in botsToEffect) { - if (bot.AddBehaviour(behaviour)) - behavioursAdded.Add(behaviour); - } + List behavioursAdded = new List(); - MainConsole.Instance.OutputFormat( - "Added behaviours {0} to bot {1}", - string.Join(", ", behavioursAdded.ConvertAll(b => b.Name).ToArray()), bot.Name); + foreach (IBehaviour behaviour in CreateBehavioursFromAbbreviatedNames(rawAbbreviatedSwitchesToAdd)) + { + if (bot.AddBehaviour(behaviour)) + behavioursAdded.Add(behaviour); + } + + MainConsole.Instance.OutputFormat( + "Added behaviours {0} to bot {1}", + string.Join(", ", behavioursAdded.ConvertAll(b => b.Name).ToArray()), bot.Name); + } } private void HandleRemoveBehaviour(string module, string[] cmd) { - if (cmd.Length != 4) + if (cmd.Length < 3 || cmd.Length > 4) { - MainConsole.Instance.OutputFormat("Usage: remove behaviour "); + MainConsole.Instance.OutputFormat("Usage: remove behaviour []"); return; } string rawBehaviours = cmd[2]; - int botNumber; - - if (!ConsoleUtil.TryParseConsoleNaturalInt(MainConsole.Instance, cmd[3], out botNumber)) - return; - Bot bot = GetBotFromNumber(botNumber); + List botsToEffect = new List(); - if (bot == null) + if (cmd.Length == 3) { - MainConsole.Instance.OutputFormat("Error: No bot found with number {0}", botNumber); - return; + lock (m_bots) + botsToEffect.AddRange(m_bots); } + else + { + int botNumber; + if (!ConsoleUtil.TryParseConsoleNaturalInt(MainConsole.Instance, cmd[3], out botNumber)) + return; - HashSet abbreviatedBehavioursToRemove = new HashSet(); - List behavioursRemoved = new List(); + Bot bot = GetBotFromNumber(botNumber); + + if (bot == null) + { + MainConsole.Instance.OutputFormat("Error: No bot found with number {0}", botNumber); + return; + } + botsToEffect.Add(bot); + } + + HashSet abbreviatedBehavioursToRemove = new HashSet(); Array.ForEach(rawBehaviours.Split(new char[] { ',' }), b => abbreviatedBehavioursToRemove.Add(b)); - foreach (string b in abbreviatedBehavioursToRemove) + foreach (Bot bot in botsToEffect) { - IBehaviour behaviour; + List behavioursRemoved = new List(); - if (bot.TryGetBehaviour(b, out behaviour)) + foreach (string b in abbreviatedBehavioursToRemove) { - bot.RemoveBehaviour(b); - behavioursRemoved.Add(behaviour); + 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(b => b.Name).ToArray()), bot.Name); + MainConsole.Instance.OutputFormat( + "Removed behaviours {0} to bot {1}", + string.Join(", ", behavioursRemoved.ConvertAll(b => b.Name).ToArray()), bot.Name); + } } private void HandleDisconnect(string module, string[] cmd) -- cgit v1.1