Refactor AI considerations (#1278)

Considerations are now instantiated under a manager and re-used between entities where they pass in their blackboard to get a score back.
Also makes the API a bit nicer to use.
Also some random cleanup.

Co-authored-by: Metal Gear Sloth <metalgearsloth@gmail.com>
This commit is contained in:
metalgearsloth
2020-07-08 09:37:35 +10:00
committed by GitHub
parent c25c1e1094
commit 5aefae184c
64 changed files with 625 additions and 396 deletions

View File

@@ -1,5 +1,5 @@
using Content.Server.AI.Utility.Curves;
using Content.Server.AI.WorldState;
using Content.Server.AI.WorldState.States.Clothing;
using Content.Server.AI.WorldState.States.Inventory;
using Content.Server.GameObjects;
using Content.Shared.GameObjects.Components.Inventory;
@@ -8,25 +8,27 @@ namespace Content.Server.AI.Utility.Considerations.Clothing
{
public sealed class ClothingInInventoryCon : Consideration
{
private readonly EquipmentSlotDefines.SlotFlags _slot;
public ClothingInInventoryCon(EquipmentSlotDefines.SlotFlags slotFlags, IResponseCurve curve) : base(curve)
public ClothingInInventoryCon Slot(EquipmentSlotDefines.SlotFlags slotFlags, Blackboard context)
{
_slot = slotFlags;
// Ideally we'd just use a variable but then if we were iterating through multiple AI at once it'd be
// Stuffed so we need to store it on the AI's context.
context.GetState<ClothingSlotFlagConState>().SetValue(slotFlags);
return this;
}
public override float GetScore(Blackboard context)
protected override float GetScore(Blackboard context)
{
var inventory = context.GetState<InventoryState>().GetValue();
var slots = context.GetState<ClothingSlotConState>().GetValue();
var slotFlags = EquipmentSlotDefines.SlotMasks[slots];
foreach (var entity in inventory)
foreach (var entity in context.GetState<EnumerableInventoryState>().GetValue())
{
if (!entity.TryGetComponent(out ClothingComponent clothingComponent))
{
continue;
}
if ((clothingComponent.SlotFlags & _slot) != 0)
if ((clothingComponent.SlotFlags & slotFlags) != 0)
{
return 1.0f;
}

View File

@@ -1,4 +1,3 @@
using Content.Server.AI.Utility.Curves;
using Content.Server.AI.WorldState;
using Content.Server.AI.WorldState.States.Clothing;
using Content.Shared.GameObjects.Components.Inventory;
@@ -7,18 +6,18 @@ namespace Content.Server.AI.Utility.Considerations.Clothing
{
public class ClothingInSlotCon : Consideration
{
private EquipmentSlotDefines.Slots _slot;
public ClothingInSlotCon(EquipmentSlotDefines.Slots slot, IResponseCurve curve) : base(curve)
public ClothingInSlotCon Slot(EquipmentSlotDefines.Slots slot, Blackboard context)
{
_slot = slot;
context.GetState<ClothingSlotConState>().SetValue(slot);
return this;
}
public override float GetScore(Blackboard context)
protected override float GetScore(Blackboard context)
{
var slot = context.GetState<ClothingSlotConState>().GetValue();
var inventory = context.GetState<EquippedClothingState>().GetValue();
return inventory.ContainsKey(_slot) ? 1.0f : 0.0f;
return inventory.ContainsKey(slot) ? 1.0f : 0.0f;
}
}
}