diff --git a/OpenRA.Mods.Common/OpenRA.Mods.Common.csproj b/OpenRA.Mods.Common/OpenRA.Mods.Common.csproj index 7a7a569837..c440f61bda 100644 --- a/OpenRA.Mods.Common/OpenRA.Mods.Common.csproj +++ b/OpenRA.Mods.Common/OpenRA.Mods.Common.csproj @@ -404,6 +404,7 @@ + diff --git a/OpenRA.Mods.Common/Traits/Render/RenderDebugState.cs b/OpenRA.Mods.Common/Traits/Render/RenderDebugState.cs new file mode 100644 index 0000000000..b39cd22f66 --- /dev/null +++ b/OpenRA.Mods.Common/Traits/Render/RenderDebugState.cs @@ -0,0 +1,101 @@ +#region Copyright & License Information +/* + * Copyright 2007-2015 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.Drawing; +using System.Linq; +using OpenRA.Graphics; +using OpenRA.Mods.Common.AI; +using OpenRA.Mods.Common.Graphics; +using OpenRA.Traits; + +namespace OpenRA.Mods.Common.Traits +{ + [Desc("Displays the actor's type and ID above the actor.")] + class RenderDebugStateInfo : ITraitInfo + { + public readonly string Font = "TinyBold"; + + public object Create(ActorInitializer init) { return new RenderDebugState(init.Self, this); } + } + + class RenderDebugState : INotifyAddedToWorld, INotifyOwnerChanged, IPostRenderSelection + { + readonly DeveloperMode devMode; + readonly SpriteFont font; + readonly Actor self; + readonly WVec offset; + readonly HackyAI ai; + + Color color; + string tagString; + + public RenderDebugState(Actor self, RenderDebugStateInfo info) + { + var buildingInfo = self.Info.TraitInfoOrDefault(); + var yOffset = buildingInfo == null ? 1 : buildingInfo.Dimensions.Y; + offset = new WVec(0, 512 * yOffset, 0); + + this.self = self; + color = GetColor(); + font = Game.Renderer.Fonts[info.Font]; + + var localPlayer = self.World.LocalPlayer; + devMode = localPlayer != null ? localPlayer.PlayerActor.Trait() : null; + ai = self.Owner.PlayerActor.TraitsImplementing().FirstOrDefault(x => x.IsEnabled); + } + + public void AddedToWorld(Actor self) + { + tagString = self.ToString(); + } + + public void OnOwnerChanged(Actor self, Player oldOwner, Player newOwner) + { + color = GetColor(); + } + + Color GetColor() + { + return self.EffectiveOwner != null ? self.EffectiveOwner.Owner.Color.RGB : self.Owner.Color.RGB; + } + + public IEnumerable RenderAfterWorld(WorldRenderer wr) + { + if (devMode == null || !devMode.ShowActorTags) + yield break; + + yield return new TextRenderable(font, self.CenterPosition - offset, 0, color, tagString); + + // Get the actor's activity. + var activity = self.GetCurrentActivity(); + if (activity != null) + { + var activityName = activity.GetType().ToString().Split('.').Last(); + yield return new TextRenderable(font, self.CenterPosition, 0, color, activityName); + } + + // Get the AI squad that this actor belongs to. + if (!self.Owner.IsBot) + yield break; + + if (ai == null) + yield break; + + var squads = ai.Squads; + var squad = squads.FirstOrDefault(x => x.Units.Contains(self)); + if (squad == null) + yield break; + + var aiSquadInfo = "{0}, {1}".F(squad.Type, squad.TargetActor); + yield return new TextRenderable(font, self.CenterPosition + offset, 0, color, aiSquadInfo); + } + } +} diff --git a/mods/cnc/rules/defaults.yaml b/mods/cnc/rules/defaults.yaml index 7648af383c..730b5e2e8c 100644 --- a/mods/cnc/rules/defaults.yaml +++ b/mods/cnc/rules/defaults.yaml @@ -6,6 +6,7 @@ ScriptTriggers: UpgradeManager: Huntable: + RenderDebugState: ^SpriteActor: BodyOrientation: diff --git a/mods/d2k/rules/defaults.yaml b/mods/d2k/rules/defaults.yaml index b27824fa6a..689e59978d 100644 --- a/mods/d2k/rules/defaults.yaml +++ b/mods/d2k/rules/defaults.yaml @@ -6,6 +6,7 @@ ScriptTriggers: UpgradeManager: Huntable: + RenderDebugState: ^SpriteActor: BodyOrientation: diff --git a/mods/ra/rules/defaults.yaml b/mods/ra/rules/defaults.yaml index 5f12fe231e..d7429a5afb 100644 --- a/mods/ra/rules/defaults.yaml +++ b/mods/ra/rules/defaults.yaml @@ -5,6 +5,7 @@ ScriptTriggers: UpgradeManager: Huntable: + RenderDebugState: ^SpriteActor: BodyOrientation: diff --git a/mods/ts/rules/defaults.yaml b/mods/ts/rules/defaults.yaml index 55383bd3e7..56f133871e 100644 --- a/mods/ts/rules/defaults.yaml +++ b/mods/ts/rules/defaults.yaml @@ -6,6 +6,7 @@ ScriptTriggers: UpgradeManager: Huntable: + RenderDebugState: ^SpriteActor: BodyOrientation: