From fa4b157b42b5dc58a088df232a63d9ca4e985182 Mon Sep 17 00:00:00 2001 From: Chicken man Date: Sun, 4 May 2014 10:00:41 -0400 Subject: [PATCH] Added ConsoleCommand, and an example to go with it --- OpenRA.Game/Traits/Player/DeveloperMode.cs | 3 +- OpenRA.Mods.RA/ChatCommands.cs | 56 +++++++++++++ OpenRA.Mods.RA/DevCommands.cs | 83 +++++++++++++++++++ OpenRA.Mods.RA/HelpCommand.cs | 59 +++++++++++++ OpenRA.Mods.RA/OpenRA.Mods.RA.csproj | 3 + OpenRA.Mods.RA/TraitsInterfaces.cs | 1 + .../Widgets/Logic/IngameChatLogic.cs | 14 +++- mods/cnc/rules/world.yaml | 1 + 8 files changed, 217 insertions(+), 3 deletions(-) create mode 100644 OpenRA.Mods.RA/ChatCommands.cs create mode 100644 OpenRA.Mods.RA/DevCommands.cs create mode 100644 OpenRA.Mods.RA/HelpCommand.cs diff --git a/OpenRA.Game/Traits/Player/DeveloperMode.cs b/OpenRA.Game/Traits/Player/DeveloperMode.cs index 0ac7f34259..5f5ee0fd2a 100644 --- a/OpenRA.Game/Traits/Player/DeveloperMode.cs +++ b/OpenRA.Game/Traits/Player/DeveloperMode.cs @@ -77,7 +77,8 @@ namespace OpenRA.Traits } case "DevGiveCash": { - self.Trait().GiveCash(Info.Cash); + var amount = order.ExtraData != 0 ? (int)order.ExtraData : Info.Cash; + self.Trait().GiveCash(amount); break; } case "DevGrowResources": diff --git a/OpenRA.Mods.RA/ChatCommands.cs b/OpenRA.Mods.RA/ChatCommands.cs new file mode 100644 index 0000000000..c103a92a48 --- /dev/null +++ b/OpenRA.Mods.RA/ChatCommands.cs @@ -0,0 +1,56 @@ +#region Copyright & License Information +/* + * Copyright 2007-2014 The OpenRA Developers (see AUTHORS) + * This file is part of OpenRA, which is free software. It is made + * available to you under the terms of the GNU General Public License + * as published by the Free Software Foundation. For more information, + * see COPYING. + */ +#endregion + +using OpenRA.Traits; +using System.Collections.Generic; +using System.Linq; + +namespace OpenRA.Mods.RA +{ + public class ChatCommandsInfo : TraitInfo { } + + public class ChatCommands : INotifyChat + { + public Dictionary Commands { get; private set; } + + public ChatCommands() + { + Commands = new Dictionary(); + } + + public bool OnChat(string playername, string message) + { + if (message.StartsWith("/")) + { + var name = message.Substring(1).Split(' ')[0].ToLower(); + var command = Commands.FirstOrDefault(x => x.Key == name); + + if (command.Value != null) + command.Value.InvokeCommand(name.ToLower(), message.Substring(1 + name.Length)); + else + Game.Debug("{0} is not a valid command.", name); + + return false; + } + + return true; + } + + public void RegisterCommand(string name, IChatCommand command) + { + Commands.Add(name.ToLower(), command); + } + } + + public interface IChatCommand + { + void InvokeCommand(string command, string arg); + } +} diff --git a/OpenRA.Mods.RA/DevCommands.cs b/OpenRA.Mods.RA/DevCommands.cs new file mode 100644 index 0000000000..11532a5107 --- /dev/null +++ b/OpenRA.Mods.RA/DevCommands.cs @@ -0,0 +1,83 @@ +#region Copyright & License Information +/* + * Copyright 2007-2014 The OpenRA Developers (see AUTHORS) + * This file is part of OpenRA, which is free software. It is made + * available to you under the terms of the GNU General Public License + * as published by the Free Software Foundation. For more information, + * see COPYING. + */ +#endregion + +using System.Collections.Generic; +using System.Linq; +using OpenRA.Graphics; +using OpenRA.Traits; + +namespace OpenRA.Mods.RA +{ + public class DevCommandsInfo : TraitInfo { } + + public class DevCommands : IChatCommand, IWorldLoaded + { + World world; + + public DevCommands() { } + + public void WorldLoaded(World w, WorldRenderer wr) + { + world = w; + var console = world.WorldActor.Trait(); + var help = world.WorldActor.Trait(); + + console.RegisterCommand("shroud", this); + console.RegisterCommand("give", this); + console.RegisterCommand("instabuild", this); + console.RegisterCommand("buildrange", this); + console.RegisterCommand("power", this); + console.RegisterCommand("tech", this); + + help.RegisterHelp("shroud", "enables or disables shroud."); + help.RegisterHelp("give", "gives the default or specified amount of money."); + help.RegisterHelp("instabuild", "enables or disables instant building"); + help.RegisterHelp("buildrange", "allows or disallows you to build out of your build-range"); + help.RegisterHelp("power", "enables or disables infinite power"); + help.RegisterHelp("tech", "gives or takes the ability to build everything"); + } + + public void InvokeCommand(string name, string arg) + { + if (!world.LobbyInfo.GlobalSettings.AllowCheats) + { + Game.Debug("Cheats are disabled."); + return; + } + + switch (name) + { + case "give": + var order = new Order("DevGiveCash", world.LocalPlayer.PlayerActor, false); + var cash = 0; + + if (int.TryParse(arg, out cash)) + order.ExtraData = (uint)cash; + + Game.Debug("Giving {0} credits to player {1}.", (cash == 0 ? "cheat default" : cash.ToString()), world.LocalPlayer.PlayerName); + world.IssueOrder(order); + + break; + + case "shroud": IssueDevCommand(world, "DevShroudDisable"); break; + case "instabuild": IssueDevCommand(world, "DevFastBuild"); break; + case "buildrange": IssueDevCommand(world, "DevBuildAnywhere"); break; + case "power": IssueDevCommand(world, "DevUnlimitedPower"); break; + case "tech": IssueDevCommand(world, "DevEnableTech"); break; + case "support": IssueDevCommand(world, "DevFastCharge"); break; + } + } + + void IssueDevCommand(World world, string command) + { + world.IssueOrder(new Order(command, world.LocalPlayer.PlayerActor, false)); + } + } +} diff --git a/OpenRA.Mods.RA/HelpCommand.cs b/OpenRA.Mods.RA/HelpCommand.cs new file mode 100644 index 0000000000..62f8b35b90 --- /dev/null +++ b/OpenRA.Mods.RA/HelpCommand.cs @@ -0,0 +1,59 @@ +#region Copyright & License Information +/* + * Copyright 2007-2014 The OpenRA Developers (see AUTHORS) + * This file is part of OpenRA, which is free software. It is made + * available to you under the terms of the GNU General Public License + * as published by the Free Software Foundation. For more information, + * see COPYING. + */ +#endregion + +using System.Collections.Generic; +using System.Linq; +using OpenRA.Graphics; +using OpenRA.Traits; + +namespace OpenRA.Mods.RA +{ + public class HelpCommandInfo : TraitInfo { } + + public class HelpCommand : IChatCommand, IWorldLoaded + { + World world; + ChatCommands console; + Dictionary helpDescriptions; + + public HelpCommand() + { + helpDescriptions = new Dictionary(); + } + + public void WorldLoaded(World w, WorldRenderer wr) + { + world = w; + console = world.WorldActor.Trait(); + + console.RegisterCommand("help", this); + RegisterHelp("help", "provides useful info about various commands"); + } + + public void InvokeCommand(string name, string arg) + { + Game.Debug("Here are the available commands:"); + + foreach (var key in console.Commands.Keys) + { + var description = ""; + if (!helpDescriptions.TryGetValue(key, out description)) + description = "no description available."; + + Game.Debug("{0}: {1}", key, description); + } + } + + public void RegisterHelp(string name, string description) + { + helpDescriptions.Add(name, description); + } + } +} \ No newline at end of file diff --git a/OpenRA.Mods.RA/OpenRA.Mods.RA.csproj b/OpenRA.Mods.RA/OpenRA.Mods.RA.csproj index ece8d2c481..e967588427 100644 --- a/OpenRA.Mods.RA/OpenRA.Mods.RA.csproj +++ b/OpenRA.Mods.RA/OpenRA.Mods.RA.csproj @@ -521,6 +521,9 @@ + + + diff --git a/OpenRA.Mods.RA/TraitsInterfaces.cs b/OpenRA.Mods.RA/TraitsInterfaces.cs index 3a0db48bc9..2715cad444 100755 --- a/OpenRA.Mods.RA/TraitsInterfaces.cs +++ b/OpenRA.Mods.RA/TraitsInterfaces.cs @@ -48,4 +48,5 @@ namespace OpenRA.Mods.RA public interface INotifyTransform { void OnTransform(Actor self); } public interface INotifyTransformed { void OnTransformed(Actor toActor); } public interface INotifyAttack { void Attacking(Actor self, Target target, Armament a, Barrel barrel); } + public interface INotifyChat { bool OnChat(string from, string message); } } diff --git a/OpenRA.Mods.RA/Widgets/Logic/IngameChatLogic.cs b/OpenRA.Mods.RA/Widgets/Logic/IngameChatLogic.cs index 54734050bc..4fd0384908 100644 --- a/OpenRA.Mods.RA/Widgets/Logic/IngameChatLogic.cs +++ b/OpenRA.Mods.RA/Widgets/Logic/IngameChatLogic.cs @@ -1,6 +1,6 @@ #region Copyright & License Information /* - * Copyright 2013 The OpenRA Developers (see AUTHORS) + * Copyright 2007-2014 The OpenRA Developers (see AUTHORS) * This file is part of OpenRA, which is free software. It is made * available to you under the terms of the GNU General Public License * as published by the Free Software Foundation. For more information, @@ -8,10 +8,13 @@ */ #endregion +using System.Collections.Generic; using System.Drawing; using System.Linq; using OpenRA.Network; using OpenRA.Widgets; +using OpenRA.Traits; +using OpenRA.Graphics; namespace OpenRA.Mods.RA.Widgets.Logic { @@ -25,11 +28,15 @@ namespace OpenRA.Mods.RA.Widgets.Logic readonly ContainerWidget chatTemplate; readonly TextFieldWidget chatText; + readonly List chatTraits; + bool teamChat; [ObjectCreator.UseCtor] public IngameChatLogic(Widget widget, OrderManager orderManager, World world) { + chatTraits = world.WorldActor.TraitsImplementing().ToList(); + var players = world.Players.Where(p => p != world.LocalPlayer && !p.NonCombatant && !p.IsBot); var disableTeamChat = world.LocalPlayer == null || world.LobbyInfo.IsSinglePlayer || !players.Any(p => p.IsAlliedWith(world.LocalPlayer)); teamChat = !disableTeamChat; @@ -117,6 +124,9 @@ namespace OpenRA.Mods.RA.Widgets.Logic public void AddChatLine(Color c, string from, string text) { + if (chatTraits != null && !chatTraits.All(x => x.OnChat(from, text))) + return; + chatOverlayDisplay.AddLine(c, from, text); var template = chatTemplate.Clone(); @@ -154,4 +164,4 @@ namespace OpenRA.Mods.RA.Widgets.Logic Sound.PlayNotification(null, "Sounds", "ChatLine", null); } } -} \ No newline at end of file +} diff --git a/mods/cnc/rules/world.yaml b/mods/cnc/rules/world.yaml index b0441c6e26..555e62de85 100644 --- a/mods/cnc/rules/world.yaml +++ b/mods/cnc/rules/world.yaml @@ -1,4 +1,5 @@ World: + GameConsole: ScreenMap: ActorMap: LoadWidgetAtGameStart: