Minor AI system fixes (#1292)

* Add test to check all LogicNames in prototypes
* Change CreateProcessor to AiLogicProcessor (I thought I'd already done this as I remember PJB telling me to do this but apparently I'm an idiot)
* Temporarily remove invalid AiControllers

Co-authored-by: Metal Gear Sloth <metalgearsloth@gmail.com>
This commit is contained in:
metalgearsloth
2020-07-08 02:56:38 +10:00
committed by GitHub
parent cddfa62980
commit 0ecaba1727
3 changed files with 72 additions and 14 deletions

View File

@@ -0,0 +1,67 @@
using NUnit.Framework;
using Robust.Shared.GameObjects;
using Robust.Shared.Interfaces.GameObjects;
using Robust.Shared.Interfaces.Map;
using Robust.Shared.Map;
using Robust.Shared.Prototypes;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Content.Server.GameObjects.Components.Movement;
using Content.Shared.VendingMachines;
using Robust.Server.AI;
using Robust.Shared.Log;
using Robust.Server.Interfaces.Maps;
using Robust.Server.Interfaces.Timing;
using Robust.Shared.Interfaces.Reflection;
using Robust.Shared.IoC;
using Robust.Shared.Maths;
namespace Content.IntegrationTests.Tests
{
[TestFixture]
[TestOf(typeof(AiControllerTest))]
public class AiControllerTest : ContentIntegrationTest
{
[Test]
public async Task AiProcessorNamesValidTest()
{
var server = StartServerDummyTicker();
server.Assert(() =>
{
var entityManager = IoCManager.Resolve<IEntityManager>();
var prototypeManager = IoCManager.Resolve<IPrototypeManager>();
var reflectionManager = IoCManager.Resolve<IReflectionManager>();
var mapManager = IoCManager.Resolve<IMapManager>();
mapManager.CreateMap(new MapId(1));
var grid = mapManager.CreateGrid(new MapId(1));
var processorNames = new List<string>();
// Verify they all have the required attribute
foreach (var processor in reflectionManager.GetAllChildren(typeof(AiLogicProcessor)))
{
var attrib = (AiLogicProcessorAttribute) Attribute.GetCustomAttribute(processor, typeof(AiLogicProcessorAttribute));
Assert.That(attrib != null, $"No AiLogicProcessorAttribute found on {processor.Name}");
processorNames.Add(attrib.SerializeName);
}
foreach (var entity in prototypeManager.EnumeratePrototypes<EntityPrototype>())
{
var comps = entity.Components;
if (!comps.ContainsKey("AiController")) continue;
var aiEntity = entityManager.SpawnEntity(entity.ID, new GridCoordinates(new Vector2(0, 0), grid.Index));
var aiController = aiEntity.GetComponent<AiControllerComponent>();
Assert.That(processorNames.Contains(aiController.LogicName), $"Could not find valid processor named {aiController.LogicName} on entity {entity.ID}");
}
});
await server.WaitIdleAsync();
}
}
}

View File

@@ -40,17 +40,15 @@ namespace Content.Server.GameObjects.EntitySystems.AI
foreach (var processor in processors)
{
var att = (AiLogicProcessorAttribute)Attribute.GetCustomAttribute(processor, typeof(AiLogicProcessorAttribute));
if (att != null)
{
_processorTypes.Add(att.SerializeName, processor);
}
// Tests should pick this up
DebugTools.AssertNotNull(att);
_processorTypes.Add(att.SerializeName, processor);
}
}
/// <inheritdoc />
public override void Update(float frameTime)
{
var entities = EntityManager.GetEntities(EntityQuery);
foreach (var entity in entities)
{
@@ -77,15 +75,14 @@ namespace Content.Server.GameObjects.EntitySystems.AI
if (controller.Processor != null) return;
controller.Processor = CreateProcessor(controller.LogicName);
controller.Processor.SelfEntity = controller.Owner;
controller.Processor.VisionRadius = controller.VisionRadius;
controller.Processor.Setup();
}
private UtilityAi CreateProcessor(string name)
private AiLogicProcessor CreateProcessor(string name)
{
if (_processorTypes.TryGetValue(name, out var type))
{
return (UtilityAi)_typeFactory.CreateInstance(type);
return (AiLogicProcessor)_typeFactory.CreateInstance(type);
}
// processor needs to inherit AiLogicProcessor, and needs an AiLogicProcessorAttribute to define the YAML name

View File

@@ -19,9 +19,6 @@
drawdepth: WallMountedItems
texture: Buildings/TurrTop.png
directional: false
- type: AiController
logic: AimShootLife
vision: 6.0
- type: entity
id: TurretTopLight
@@ -34,9 +31,6 @@
drawdepth: WallMountedItems
texture: Buildings/TurrLamp.png
directional: false
- type: AiController
logic: AimShootLife
vision: 6.0
- type: PointLight
radius: 512
mask: flashlight_mask