Fix cleanbots (#16922)

This commit is contained in:
metalgearsloth
2023-05-29 16:53:28 +10:00
committed by GitHub
parent c3e369cbdd
commit f308a1b31e
7 changed files with 75 additions and 125 deletions

View File

@@ -1,106 +0,0 @@
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Content.Server.Chemistry.EntitySystems;
using Content.Server.Fluids.EntitySystems;
using Content.Server.NPC.Pathfinding;
using Content.Shared.Fluids.Components;
using Robust.Shared.Map;
namespace Content.Server.NPC.HTN.PrimitiveTasks.Operators.Fluid;
/// <summary>
/// Picks a nearby evaporatable puddle.
/// </summary>
public sealed class PickPuddleOperator : HTNOperator
{
// This is similar to PickAccessibleComponent however I have an idea on generic utility queries
// that can also be re-used for melee that needs further fleshing out.
[Dependency] private readonly IComponentFactory _factory = default!;
[Dependency] private readonly IEntityManager _entManager = default!;
private PathfindingSystem _pathfinding = default!;
private EntityLookupSystem _lookup = default!;
[DataField("rangeKey", required: true)]
public string RangeKey = string.Empty;
[DataField("target")] public string Target = "Target";
[DataField("targetKey", required: true)]
public string TargetKey = string.Empty;
/// <summary>
/// Where the pathfinding result will be stored (if applicable). This gets removed after execution.
/// </summary>
[DataField("pathfindKey")]
public string PathfindKey = NPCBlackboard.PathfindKey;
public override void Initialize(IEntitySystemManager sysManager)
{
base.Initialize(sysManager);
_lookup = sysManager.GetEntitySystem<EntityLookupSystem>();
_pathfinding = sysManager.GetEntitySystem<PathfindingSystem>();
}
/// <inheritdoc/>
[Obsolete("Obsolete")]
public override async Task<(bool Valid, Dictionary<string, object>? Effects)> Plan(NPCBlackboard blackboard,
CancellationToken cancelToken)
{
var range = blackboard.GetValueOrDefault<float>(RangeKey, _entManager);
var owner = blackboard.GetValue<EntityUid>(NPCBlackboard.Owner);
if (!blackboard.TryGetValue<EntityCoordinates>(NPCBlackboard.OwnerCoordinates, out var coordinates, _entManager))
{
return (false, null);
}
var targets = new List<EntityUid>();
var puddleSystem = _entManager.System<PuddleSystem>();
var solSystem = _entManager.System<SolutionContainerSystem>();
foreach (var comp in _lookup.GetComponentsInRange<PuddleComponent>(coordinates, range))
{
if (comp.Owner == owner ||
!solSystem.TryGetSolution(comp.Owner, comp.SolutionName, out var puddleSolution) ||
puddleSystem.CanFullyEvaporate(puddleSolution))
{
continue;
}
targets.Add((comp.Owner));
}
if (targets.Count == 0)
{
return (false, null);
}
foreach (var target in targets)
{
var path = await _pathfinding.GetPath(
owner,
target,
1f,
cancelToken,
flags: _pathfinding.GetFlags(blackboard));
if (path.Result != PathResult.Path)
{
return (false, null);
}
var xform = _entManager.GetComponent<TransformComponent>(target);
return (true, new Dictionary<string, object>()
{
{ Target, target },
{ TargetKey, xform.Coordinates },
{ PathfindKey, path}
});
}
return (false, null);
}
}

View File

@@ -4,6 +4,7 @@ using Content.Server.Interaction;
using Content.Shared.Access.Systems;
using Content.Shared.ActionBlocker;
using Content.Shared.Interaction;
using JetBrains.Annotations;
using Robust.Shared.Utility;
namespace Content.Server.NPC;
@@ -60,6 +61,7 @@ public sealed class NPCBlackboard : IEnumerable<KeyValuePair<string, object>>
return dict;
}
[Pure]
public bool ContainsKey(string key)
{
return _blackboard.ContainsKey(key);
@@ -68,6 +70,7 @@ public sealed class NPCBlackboard : IEnumerable<KeyValuePair<string, object>>
/// <summary>
/// Get the blackboard data for a particular key.
/// </summary>
[Pure]
public T GetValue<T>(string key)
{
return (T) _blackboard[key];
@@ -76,6 +79,7 @@ public sealed class NPCBlackboard : IEnumerable<KeyValuePair<string, object>>
/// <summary>
/// Tries to get the blackboard data for a particular key. Returns default if not found
/// </summary>
[Pure]
public T? GetValueOrDefault<T>(string key, IEntityManager entManager)
{
if (_blackboard.TryGetValue(key, out var value))

View File

@@ -1,9 +0,0 @@
using Robust.Shared.Prototypes;
namespace Content.Server.NPC.Queries.Queries;
public sealed class NearbyComponentsQuery : UtilityQuery
{
[DataField("components")]
public ComponentRegistry Component = default!;
}

View File

@@ -0,0 +1,6 @@
namespace Content.Server.NPC.Queries.Queries;
public sealed class PuddleFilter : UtilityQueryFilter
{
}

View File

@@ -1,5 +1,7 @@
using System.Linq;
using Content.Server.Chemistry.EntitySystems;
using Content.Server.Examine;
using Content.Server.Fluids.EntitySystems;
using Content.Server.NPC.Queries;
using Content.Server.NPC.Queries.Considerations;
using Content.Server.NPC.Queries.Curves;
@@ -8,8 +10,10 @@ using Content.Server.Nutrition.Components;
using Content.Server.Nutrition.EntitySystems;
using Content.Server.Storage.Components;
using Content.Shared.Examine;
using Content.Shared.Fluids.Components;
using Content.Shared.Mobs.Systems;
using Robust.Server.Containers;
using Robust.Shared.Collections;
using Robust.Shared.Prototypes;
namespace Content.Server.NPC.Systems;
@@ -23,9 +27,11 @@ public sealed class NPCUtilitySystem : EntitySystem
[Dependency] private readonly ContainerSystem _container = default!;
[Dependency] private readonly EntityLookupSystem _lookup = default!;
[Dependency] private readonly FactionSystem _faction = default!;
[Dependency] private readonly MobStateSystem _mobState = default!;
[Dependency] private readonly SharedTransformSystem _transform = default!;
[Dependency] private readonly FoodSystem _food = default!;
[Dependency] private readonly MobStateSystem _mobState = default!;
[Dependency] private readonly PuddleSystem _puddle = default!;
[Dependency] private readonly SharedTransformSystem _transform = default!;
[Dependency] private readonly SolutionContainerSystem _solutions = default!;
/// <summary>
/// Runs the UtilityQueryPrototype and returns the best-matching entities.
@@ -269,10 +275,31 @@ public sealed class NPCUtilitySystem : EntitySystem
private void Filter(NPCBlackboard blackboard, HashSet<EntityUid> entities, UtilityQueryFilter filter)
{
var owner = blackboard.GetValue<EntityUid>(NPCBlackboard.Owner);
switch (filter)
{
case PuddleFilter:
{
var puddleQuery = GetEntityQuery<PuddleComponent>();
var toRemove = new ValueList<EntityUid>();
foreach (var ent in entities)
{
if (!puddleQuery.TryGetComponent(ent, out var puddleComp) ||
!_solutions.TryGetSolution(ent, puddleComp.SolutionName, out var sol) ||
_puddle.CanFullyEvaporate(sol))
{
toRemove.Add(ent);
}
}
foreach (var ent in toRemove)
{
entities.Remove(ent);
}
break;
}
default:
throw new NotImplementedException();
}

View File

@@ -12,18 +12,30 @@
branches:
- tasks:
- id: PickPuddlePrimitive
- id: MoveToAccessiblePrimitive
- id: InteractWithPrimitive
- id: MoveToCombatTargetPrimitive
- id: MopPrimitive
- type: htnPrimitive
id: PickPuddlePrimitive
operator: !type:PickPuddleOperator
rangeKey: BufferRange
targetKey: MovementTarget
component: Puddle
operator: !type:UtilityOperator
proto: NearbyPuddles
- type: htnPrimitive
id: SetIdleTimePrimitive
operator: !type:SetFloatOperator
targetKey: IdleTime
amount: 3
- type: htnPrimitive
id: MopPrimitive
preconditions:
- !type:TargetInRangePrecondition
targetKey: CombatTarget
rangeKey: InteractRange
operator: !type:InteractWithOperator
targetKey: CombatTarget
services:
- !type:UtilityService
id: PuddleService
proto: NearbyPuddles
key: CombatTarget

View File

@@ -35,6 +35,22 @@
- !type:TargetInLOSOrCurrentCon
curve: !type:BoolCurve
- type: utilityQuery
id: NearbyPuddles
query:
- !type:ComponentQuery
components:
- type: Puddle
- !type:PuddleFilter
considerations:
- !type:TargetDistanceCon
curve: !type:PresetCurve
preset: TargetDistance
- !type:TargetAccessibleCon
curve: !type:BoolCurve
- !type:TargetInLOSOrCurrentCon
curve: !type:BoolCurve
- type: utilityQuery
id: NearbyRangedTargets
query: