NPC refactor (#10122)

Co-authored-by: metalgearsloth <metalgearsloth@gmail.com>
This commit is contained in:
metalgearsloth
2022-09-06 00:28:23 +10:00
committed by GitHub
parent 138e328c04
commit 0286b88388
290 changed files with 13842 additions and 5939 deletions

View File

@@ -0,0 +1,46 @@
using Content.Server.Administration;
using Content.Server.NPC.HTN;
using Content.Shared.Administration;
using Robust.Shared.Console;
namespace Content.Server.NPC.Commands
{
[AdminCommand(AdminFlags.Fun)]
public sealed class AddNPCCommand : IConsoleCommand
{
[Dependency] private readonly IEntityManager _entities = default!;
public string Command => "addnpc";
public string Description => "Add a HTN NPC component with a given root task";
public string Help => "Usage: addnpc <entityId> <rootTask>"
+ "\n entityID: Uid of entity to add the AiControllerComponent to. Open its VV menu to find this."
+ "\n rootTask: Name of a behaviorset to add to the component on initialize.";
public void Execute(IConsoleShell shell, string argStr, string[] args)
{
if (args.Length != 2)
{
shell.WriteError("Wrong number of args.");
return;
}
var entId = new EntityUid(int.Parse(args[0]));
if (!_entities.EntityExists(entId))
{
shell.WriteError($"Unable to find entity with uid {entId}");
return;
}
if (_entities.HasComponent<HTNComponent>(entId))
{
shell.WriteError("Entity already has an NPC component.");
return;
}
var comp = _entities.AddComponent<HTNComponent>(entId);
comp.RootTask = args[1];
shell.WriteLine("AI component added.");
}
}
}

View File

@@ -0,0 +1,89 @@
using System.Text;
using Content.Server.Administration;
using Content.Server.NPC.Systems;
using Content.Shared.Administration;
using Robust.Shared.Console;
namespace Content.Server.NPC.Commands
{
[AdminCommand(AdminFlags.Fun)]
public sealed class FactionCommand : IConsoleCommand
{
public string Command => "factions";
public string Description => Loc.GetString("faction-command-description");
public string Help => Loc.GetString("faction-command-help-text");
public void Execute(IConsoleShell shell, string argStr, string[] args)
{
if (args.Length == 0)
{
var result = new StringBuilder();
foreach (Faction value in Enum.GetValues(typeof(Faction)))
{
if (value == Faction.None)
continue;
result.Append(value + "\n");
}
shell.WriteLine(result.ToString());
return;
}
if (args.Length < 2)
{
shell.WriteLine(Loc.GetString("shell-wrong-arguments-number"));
return;
}
if (!Enum.TryParse(args[0], true, out Faction faction))
{
shell.WriteLine(Loc.GetString("faction-command-invalid-faction-error"));
return;
}
Faction targetFaction;
switch (args[1])
{
case "friendly":
if (args.Length < 3)
{
shell.WriteLine(Loc.GetString("faction-command-no-target-faction-error"));
return;
}
if (!Enum.TryParse(args[2], true, out targetFaction))
{
shell.WriteLine(Loc.GetString("faction-command-invalid-target-faction-error"));
return;
}
EntitySystem.Get<AiFactionTagSystem>().MakeFriendly(faction, targetFaction);
shell.WriteLine(Loc.GetString("shell-command-success"));
break;
case "hostile":
if (args.Length < 3)
{
shell.WriteLine(Loc.GetString("faction-command-no-target-faction-error"));
return;
}
if (!Enum.TryParse(args[2], true, out targetFaction))
{
shell.WriteLine(Loc.GetString("faction-command-invalid-target-faction-error"));
return;
}
EntitySystem.Get<AiFactionTagSystem>().MakeHostile(faction, targetFaction);
shell.WriteLine(Loc.GetString("shell-command-success"));
break;
case "list":
shell.WriteLine(EntitySystem.Get<AiFactionTagSystem>().GetHostileFactions(faction).ToString());
break;
default:
shell.WriteLine(Loc.GetString("faction-command-unknown-faction-argument-error"));
break;
}
}
}
}

View File

@@ -0,0 +1,26 @@
using Content.Server.Administration;
using Content.Server.EUI;
using Content.Server.NPC.UI;
using Content.Shared.Administration;
using Robust.Server.Player;
using Robust.Shared.Console;
namespace Content.Server.NPC.Commands;
[AdminCommand(AdminFlags.Debug)]
public sealed class NPCCommand : IConsoleCommand
{
public string Command => "npc";
public string Description => "Opens the debug window for NPCs";
public string Help => $"{Command}";
public void Execute(IConsoleShell shell, string argStr, string[] args)
{
if (shell.Player is not IPlayerSession playerSession)
{
return;
}
var euiManager = IoCManager.Resolve<EuiManager>();
euiManager.OpenEui(new NPCEui(), playerSession);
}
}

View File

@@ -0,0 +1,52 @@
using System.Linq;
using Content.Server.Administration;
using Content.Server.NPC.HTN;
using Content.Shared.Administration;
using Robust.Shared.Console;
using Robust.Shared.Prototypes;
namespace Content.Server.NPC.Commands;
/// <summary>
/// Lists out the domain of a particular HTN compound task.
/// </summary>
[AdminCommand(AdminFlags.Debug)]
public sealed class NPCDomainCommand : IConsoleCommand
{
public string Command => "npcdomain";
public string Description => "Lists the domain of a particular HTN compound task";
public string Help => $"{Command} <htncompoundtask>";
public void Execute(IConsoleShell shell, string argStr, string[] args)
{
if (args.Length != 1)
{
shell.WriteError("shell-need-exactly-one-argument");
return;
}
var protoManager = IoCManager.Resolve<IPrototypeManager>();
if (!protoManager.TryIndex<HTNCompoundTask>(args[0], out var compound))
{
shell.WriteError($"Unable to find HTN compound task for '{args[0]}'");
return;
}
var htnSystem = IoCManager.Resolve<IEntitySystemManager>().GetEntitySystem<HTNSystem>();
foreach (var line in htnSystem.GetDomain(compound).Split("\n"))
{
shell.WriteLine(line);
}
}
public CompletionResult GetCompletion(IConsoleShell shell, string[] args)
{
var protoManager = IoCManager.Resolve<IPrototypeManager>();
if (args.Length > 1)
return CompletionResult.Empty;
return CompletionResult.FromHintOptions(protoManager.EnumeratePrototypes<HTNCompoundTask>().Select(o => o.ID), "compound task");
}
}