NPCTest.cs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. using System.Collections.Generic;
  2. using Content.Server.NPC.HTN;
  3. using Robust.Shared.GameObjects;
  4. using Robust.Shared.Prototypes;
  5. using Robust.Shared.Utility;
  6. namespace Content.IntegrationTests.Tests.NPC;
  7. [TestFixture]
  8. public sealed class NPCTest
  9. {
  10. [Test]
  11. public async Task CompoundRecursion()
  12. {
  13. var pool = await PoolManager.GetServerClient();
  14. var server = pool.Server;
  15. await server.WaitIdleAsync();
  16. var htnSystem = server.ResolveDependency<IEntitySystemManager>().GetEntitySystem<HTNSystem>();
  17. var protoManager = server.ResolveDependency<IPrototypeManager>();
  18. await server.WaitAssertion(() =>
  19. {
  20. var counts = new Dictionary<string, int>();
  21. foreach (var compound in protoManager.EnumeratePrototypes<HTNCompoundPrototype>())
  22. {
  23. Count(compound, counts, htnSystem, protoManager);
  24. counts.Clear();
  25. }
  26. });
  27. await pool.CleanReturnAsync();
  28. }
  29. private static void Count(HTNCompoundPrototype compound, Dictionary<string, int> counts, HTNSystem htnSystem, IPrototypeManager protoManager)
  30. {
  31. foreach (var branch in compound.Branches)
  32. {
  33. foreach (var task in branch.Tasks)
  34. {
  35. if (task is HTNCompoundTask compoundTask)
  36. {
  37. var count = counts.GetOrNew(compound.ID);
  38. count++;
  39. // Compound tasks marked with AllowRecursion are only evaluated once
  40. if (counts.ContainsKey(compound.ID) && compound.AllowRecursion)
  41. continue;
  42. Assert.That(count, Is.LessThan(50));
  43. counts[compound.ID] = count;
  44. Count(protoManager.Index<HTNCompoundPrototype>(compoundTask.Task), counts, htnSystem, protoManager);
  45. }
  46. }
  47. }
  48. }
  49. }