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

@@ -1,62 +0,0 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Content.Server.AI.Utility;
using Content.Server.AI.Utility.Actions;
using Content.Server.AI.Utility.AiLogic;
using NUnit.Framework;
using Robust.Shared.Prototypes;
using Robust.Shared.Reflection;
namespace Content.IntegrationTests.Tests.AI
{
[TestFixture]
[TestOf(typeof(BehaviorSetPrototype))]
public sealed class BehaviorSetsTest
{
[Test]
public async Task TestBehaviorSets()
{
await using var pairTracker = await PoolManager.GetServerClient(new PoolSettings{NoClient = true});
var server = pairTracker.Pair.Server;
var protoManager = server.ResolveDependency<IPrototypeManager>();
var reflectionManager = server.ResolveDependency<IReflectionManager>();
Dictionary<string, List<string>> behaviorSets = new();
// Test that all BehaviorSet actions exist.
await server.WaitAssertion(() =>
{
foreach (var proto in protoManager.EnumeratePrototypes<BehaviorSetPrototype>())
{
behaviorSets[proto.ID] = proto.Actions.ToList();
foreach (var action in proto.Actions)
{
if (!reflectionManager.TryLooseGetType(action, out var actionType) ||
!typeof(IAiUtility).IsAssignableFrom(actionType))
{
Assert.Fail($"Action {action} is not valid within BehaviorSet {proto.ID}");
}
}
}
});
// Test that all BehaviorSets on NPCs exist.
await server.WaitAssertion(() =>
{
foreach (var entity in protoManager.EnumeratePrototypes<EntityPrototype>())
{
if (!entity.TryGetComponent<UtilityNPCComponent>("UtilityAI", out var npcNode)) continue;
foreach (var entry in npcNode.BehaviorSets)
{
Assert.That(behaviorSets.ContainsKey(entry), $"BehaviorSet {entry} in entity {entity.ID} not found");
}
}
});
await pairTracker.CleanReturnAsync();
}
}
}

View File

@@ -0,0 +1,56 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Content.Server.NPC.HTN;
using NUnit.Framework;
using Robust.Shared.Prototypes;
using Robust.Shared.Utility;
namespace Content.IntegrationTests.Tests.NPC;
[TestFixture]
public sealed class NPCTest
{
[Test]
public async Task CompoundRecursion()
{
var pool = await PoolManager.GetServerClient(new PoolSettings() { NoClient = true });
var server = pool.Pair.Server;
await server.WaitIdleAsync();
var protoManager = server.ResolveDependency<IPrototypeManager>();
await server.WaitAssertion(() =>
{
var counts = new Dictionary<string, int>();
foreach (var compound in protoManager.EnumeratePrototypes<HTNCompoundTask>())
{
Count(compound, counts);
counts.Clear();
}
});
await pool.CleanReturnAsync();
}
private static void Count(HTNCompoundTask compound, Dictionary<string, int> counts)
{
foreach (var branch in compound.Branches)
{
foreach (var task in branch.Tasks)
{
if (task is HTNCompoundTask compoundTask)
{
var count = counts.GetOrNew(compound.ID);
count++;
Assert.That(count, Is.LessThan(50));
counts[compound.ID] = count;
Count(compoundTask, counts);
}
}
}
}
}