Skip to content

Commit

Permalink
Use consistent and easy to read debug command names.
Browse files Browse the repository at this point in the history
If value parsing fails then abort the order.
Reorganize dev cheat command handling.
  • Loading branch information
Mailaender committed May 18, 2021
1 parent c64cfea commit 03b8971
Show file tree
Hide file tree
Showing 4 changed files with 179 additions and 120 deletions.
70 changes: 42 additions & 28 deletions OpenRA.Mods.Common/Commands/DebugVisualizationCommands.cs
Expand Up @@ -10,6 +10,7 @@
#endregion

using System;
using System.Collections.Generic;
using OpenRA.Graphics;
using OpenRA.Mods.Common.Traits;
using OpenRA.Traits;
Expand All @@ -22,6 +23,15 @@ public class DebugVisualizationCommandsInfo : TraitInfo<DebugVisualizationComman

public class DebugVisualizationCommands : IChatCommand, IWorldLoaded
{
readonly IDictionary<string, (string, Action<DebugVisualizations, DeveloperMode>)> commandHandlers = new Dictionary<string, (string, Action<DebugVisualizations, DeveloperMode>)>
{
{ "combat-geometry", ("toggles combat geometry overlay.", CombatGeometry) },
{ "render-geometry", ("toggles render geometry overlay.", RenderGeometry) },
{ "screen-map", ("toggles screen map overlay.", ScreenMap) },
{ "depth-buffer", ("toggles depth buffer overlay.", DepthBuffer) },
{ "actor-tags", ("toggles actor tags overlay.", ActorTags) },
};

DebugVisualizations debugVis;
DeveloperMode devMode;

Expand All @@ -45,38 +55,42 @@ public void WorldLoaded(World w, WorldRenderer wr)
help.RegisterHelp(name, helpText);
};

register("showcombatgeometry", "toggles combat geometry overlay.");
register("showrendergeometry", "toggles render geometry overlay.");
register("showscreenmap", "toggles screen map overlay.");
register("showdepthbuffer", "toggles depth buffer overlay.");
register("showactortags", "toggles actor tags overlay.");
foreach (var command in commandHandlers)
register(command.Key, command.Value.Item1);
}

static void CombatGeometry(DebugVisualizations debugVis, DeveloperMode devMode)
{
debugVis.CombatGeometry ^= true;
}

static void RenderGeometry(DebugVisualizations debugVis, DeveloperMode devMode)
{
debugVis.RenderGeometry ^= true;
}

static void ScreenMap(DebugVisualizations debugVis, DeveloperMode devMode)
{
if (devMode == null || devMode.Enabled)
debugVis.ScreenMap ^= true;
}

static void DepthBuffer(DebugVisualizations debugVis, DeveloperMode devMode)
{
debugVis.DepthBuffer ^= true;
}

static void ActorTags(DebugVisualizations debugVis, DeveloperMode devMode)
{
debugVis.ActorTags ^= true;
}

public void InvokeCommand(string name, string arg)
{
switch (name)
{
case "showcombatgeometry":
debugVis.CombatGeometry ^= true;
break;

case "showrendergeometry":
debugVis.RenderGeometry ^= true;
break;

case "showscreenmap":
if (devMode == null || devMode.Enabled)
debugVis.ScreenMap ^= true;
break;

case "showdepthbuffer":
debugVis.DepthBuffer ^= true;
break;

case "showactortags":
debugVis.ActorTags ^= true;
break;
}
if (!commandHandlers.TryGetValue(name, out var command))
return;

command.Item2(debugVis, devMode);
}
}
}
225 changes: 135 additions & 90 deletions OpenRA.Mods.Common/Commands/DevCommands.cs
Expand Up @@ -10,6 +10,7 @@
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using OpenRA.Graphics;
using OpenRA.Mods.Common.Traits;
Expand All @@ -23,6 +24,25 @@ public class DevCommandsInfo : TraitInfo<DevCommands> { }

public class DevCommands : IChatCommand, IWorldLoaded
{
readonly IDictionary<string, (string, Action<string, World>)> commandHandlers = new Dictionary<string, (string, Action<string, World>)>
{
{ "visibility", ("toggles visibility checks and minimap.", Visibility) },
{ "give-cash", ("gives the default or specified amount of money.", GiveCash) },
{ "give-cash-all", ("gives the default or specified amount of money to all players and ai.", GiveCashAll) },
{ "instant-build", ("toggles instant building.", InstantBuild) },
{ "build-anywhere", ("toggles you the ability to build anywhere.", BuildAnywhere) },
{ "unlimited-power", ("toggles infinite power.", UnlimitedPower) },
{ "enable-tech", ("toggles the ability to build everything.", EnableTech) },
{ "instant-charge", ("toggles instant support power charging.", InstantCharge) },
{ "all", ("toggles all cheats and gives you some cash for your trouble.", All) },
{ "crash", ("crashes the game.", Crash) },
{ "levelup", ("adds a specified number of levels to the selected actors.", LevelUp) },
{ "player-experience", ("adds a specified amount of player experience to the owner(s) of selected actors.", PlayerExperience) },
{ "power-outage", ("causes owner(s) of selected actors to have a 5 second power outage.", PowerOutage) },
{ "kill", ("kills selected actors.", Kill) },
{ "dispose", ("disposes selected actors.", Dispose) }
};

World world;
DeveloperMode developerMode;

Expand All @@ -42,21 +62,8 @@ public void WorldLoaded(World w, WorldRenderer wr)
help.RegisterHelp(name, helpText);
};

register("visibility", "toggles visibility checks and minimap.");
register("givecash", "gives the default or specified amount of money.");
register("givecashall", "gives the default or specified amount of money to all players and ai.");
register("instantbuild", "toggles instant building.");
register("buildanywhere", "toggles you the ability to build anywhere.");
register("unlimitedpower", "toggles infinite power.");
register("enabletech", "toggles the ability to build everything.");
register("instantcharge", "toggles instant support power charging.");
register("all", "toggles all cheats and gives you some cash for your trouble.");
register("crash", "crashes the game.");
register("levelup", "adds a specified number of levels to the selected actors.");
register("playerexperience", "adds a specified amount of player experience to the owner(s) of selected actors.");
register("poweroutage", "causes owner(s) of selected actors to have a 5 second power outage.");
register("kill", "kills selected actors.");
register("dispose", "disposes selected actors.");
foreach (var command in commandHandlers)
register(command.Key, command.Value.Item1);
}

public void InvokeCommand(string name, string arg)
Expand All @@ -70,88 +77,126 @@ public void InvokeCommand(string name, string arg)
return;
}

switch (name)
if (!commandHandlers.TryGetValue(name, out var command))
return;

command.Item2(arg, world);
}

static void GiveCash(string arg, World world)
{
IssueCashDevCommand(world, "DevGiveCash", arg);
}

static void GiveCashAll(string arg, World world)
{
IssueCashDevCommand(world, "DevGiveCashAll", arg);
}

static void IssueCashDevCommand(World world, string command, string arg)
{
var giveCashOrder = new Order(command, world.LocalPlayer.PlayerActor, false);

if (int.TryParse(arg, out var cash))
{
giveCashOrder.ExtraData = (uint)cash;
world.IssueOrder(giveCashOrder);
}
}

static void Visibility(string arg, World world)
{
IssueDevCommand(world, "DevVisibility");
}

static void InstantBuild(string arg, World world)
{
IssueDevCommand(world, "DevFastBuild");
}

static void BuildAnywhere(string arg, World world)
{
IssueDevCommand(world, "DevBuildAnywhere");
}

static void UnlimitedPower(string arg, World world)
{
IssueDevCommand(world, "DevUnlimitedPower");
}

static void EnableTech(string arg, World world)
{
IssueDevCommand(world, "DevEnableTech");
}

static void InstantCharge(string arg, World world)
{
IssueDevCommand(world, "DevFastCharge");
}

static void All(string arg, World world)
{
IssueDevCommand(world, "DevAll");
}

static void Crash(string arg, World world)
{
throw new DevException();
}

static void LevelUp(string arg, World world)
{
if (!int.TryParse(arg, out var level))
return;

foreach (var actor in world.Selection.Actors)
{
case "givecash": IssueGiveCashDevCommand(false, arg); break;
case "givecashall": IssueGiveCashDevCommand(true, arg); break;
case "visibility": IssueDevCommand(world, "DevVisibility"); break;
case "instantbuild": IssueDevCommand(world, "DevFastBuild"); break;
case "buildanywhere": IssueDevCommand(world, "DevBuildAnywhere"); break;
case "unlimitedpower": IssueDevCommand(world, "DevUnlimitedPower"); break;
case "enabletech": IssueDevCommand(world, "DevEnableTech"); break;
case "instantcharge": IssueDevCommand(world, "DevFastCharge"); break;

case "all":
IssueDevCommand(world, "DevAll");
break;

case "crash":
throw new DevException();

case "levelup":
var level = 0;
int.TryParse(arg, out level);

foreach (var actor in world.Selection.Actors)
{
if (actor.IsDead)
continue;

var leveluporder = new Order("DevLevelUp", actor, false);
leveluporder.ExtraData = (uint)level;

if (actor.Info.HasTraitInfo<GainsExperienceInfo>())
world.IssueOrder(leveluporder);
}

break;

case "playerexperience":
var experience = 0;
int.TryParse(arg, out experience);

foreach (var player in world.Selection.Actors.Select(a => a.Owner.PlayerActor).Distinct())
world.IssueOrder(new Order("DevPlayerExperience", player, false) { ExtraData = (uint)experience });
break;

case "poweroutage":
foreach (var player in world.Selection.Actors.Select(a => a.Owner.PlayerActor).Distinct())
world.IssueOrder(new Order("PowerOutage", player, false) { ExtraData = 250 });
break;

case "kill":
foreach (var actor in world.Selection.Actors)
{
if (actor.IsDead)
continue;

world.IssueOrder(new Order("DevKill", world.LocalPlayer.PlayerActor, Target.FromActor(actor), false) { TargetString = arg });
}

break;

case "dispose":
foreach (var actor in world.Selection.Actors)
{
if (actor.Disposed)
continue;

world.IssueOrder(new Order("DevDispose", world.LocalPlayer.PlayerActor, Target.FromActor(actor), false));
}

break;
if (actor.IsDead)
continue;

var leveluporder = new Order("DevLevelUp", actor, false);
leveluporder.ExtraData = (uint)level;

if (actor.Info.HasTraitInfo<GainsExperienceInfo>())
world.IssueOrder(leveluporder);
}
}

void IssueGiveCashDevCommand(bool toAll, string arg)
static void PlayerExperience(string arg, World world)
{
var orderString = toAll ? "DevGiveCashAll" : "DevGiveCash";
var giveCashOrder = new Order(orderString, world.LocalPlayer.PlayerActor, false);
if (!int.TryParse(arg, out var experience))
return;

foreach (var player in world.Selection.Actors.Select(a => a.Owner.PlayerActor).Distinct())
world.IssueOrder(new Order("DevPlayerExperience", player, false) { ExtraData = (uint)experience });
}

static void PowerOutage(string arg, World world)
{
foreach (var player in world.Selection.Actors.Select(a => a.Owner.PlayerActor).Distinct())
world.IssueOrder(new Order("PowerOutage", player, false) { ExtraData = 250 });
}

int.TryParse(arg, out var cash);
giveCashOrder.ExtraData = (uint)cash;
static void Kill(string arg, World world)
{
foreach (var actor in world.Selection.Actors)
{
if (actor.IsDead)
continue;

world.IssueOrder(giveCashOrder);
world.IssueOrder(new Order("DevKill", world.LocalPlayer.PlayerActor, Target.FromActor(actor), false) { TargetString = arg });
}
}

static void Dispose(string arg, World world)
{
foreach (var actor in world.Selection.Actors)
{
if (actor.Disposed)
continue;

world.IssueOrder(new Order("DevDispose", world.LocalPlayer.PlayerActor, Target.FromActor(actor), false));
}
}

static void IssueDevCommand(World world, string command)
Expand Down
Expand Up @@ -28,7 +28,7 @@ class CustomTerrainDebugOverlayInfo : TraitInfo

class CustomTerrainDebugOverlay : IWorldLoaded, IChatCommand, IRenderAnnotations
{
const string CommandName = "debugcustomterrain";
const string CommandName = "custom-terrain";
const string CommandDesc = "toggles the custom terrain debug overlay.";

public bool Enabled;
Expand Down
2 changes: 1 addition & 1 deletion OpenRA.Mods.Common/Traits/World/TerrainGeometryOverlay.cs
Expand Up @@ -24,7 +24,7 @@ public class TerrainGeometryOverlayInfo : TraitInfo<TerrainGeometryOverlay> { }

public class TerrainGeometryOverlay : IRenderAnnotations, IWorldLoaded, IChatCommand
{
const string CommandName = "terrainoverlay";
const string CommandName = "terrain-geometry";
const string CommandDesc = "toggles the terrain geometry overlay.";

public bool Enabled;
Expand Down

0 comments on commit 03b8971

Please sign in to comment.