Skip to content

Commit

Permalink
Add DoAfter handling to InteractWithOperator (space-wizards#30319)
Browse files Browse the repository at this point in the history
* commit used for the recording, mostly copypaste

* Remove recording artifacts, now contains content

* use switch expression instead of switch statement
Also `ExpectedDoAfter` to fail when doafter isn't raised
  • Loading branch information
osjarw authored Aug 2, 2024
1 parent 448f380 commit ccdbdee
Showing 1 changed file with 63 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,23 +1,74 @@
using Content.Server.Interaction;
using Content.Shared.CombatMode;
using Content.Shared.DoAfter;
using Content.Shared.Timing;

namespace Content.Server.NPC.HTN.PrimitiveTasks.Operators.Interactions;

public sealed partial class InteractWithOperator : HTNOperator
{
[Dependency] private readonly IEntityManager _entManager = default!;
private SharedDoAfterSystem _doAfterSystem = default!;

public override void Initialize(IEntitySystemManager sysManager)
{
base.Initialize(sysManager);
_doAfterSystem = sysManager.GetEntitySystem<SharedDoAfterSystem>();
}

/// <summary>
/// Key that contains the target entity.
/// </summary>
[DataField(required: true)]
public string TargetKey = default!;

/// <summary>
/// Exit with failure if doafter wasn't raised
/// </summary>
[DataField]
public bool ExpectDoAfter = false;

public string CurrentDoAfter = "CurrentInteractWithDoAfter";


// Ensure that CurrentDoAfter doesn't exist as we enter this operator,
// the code currently relies on the result of a TryGetValue
public override void Startup(NPCBlackboard blackboard)
{
blackboard.Remove<ushort>(CurrentDoAfter);

}

// Not really sure if we should clean it up, I guess some operator could use it
public override void TaskShutdown(NPCBlackboard blackboard, HTNOperatorStatus status)
{
blackboard.Remove<ushort>(CurrentDoAfter);
}

public override HTNOperatorStatus Update(NPCBlackboard blackboard, float frameTime)
{
var owner = blackboard.GetValue<EntityUid>(NPCBlackboard.Owner);

// Handle ongoing doAfter, and store the doAfter.nextId so we can detect if we started one
ushort nextId = 0;
if (_entManager.TryGetComponent<DoAfterComponent>(owner, out var doAfter))
{
// if CurrentDoAfter contains something, we have an active doAfter
if (blackboard.TryGetValue<ushort>(CurrentDoAfter, out var doAfterId, _entManager))
{
var status = _doAfterSystem.GetStatus(owner, doAfterId, null);
return status switch
{
DoAfterStatus.Running => HTNOperatorStatus.Continuing,
DoAfterStatus.Finished => HTNOperatorStatus.Finished,
_ => HTNOperatorStatus.Failed
};
}

nextId = doAfter.NextId;
}


if (_entManager.TryGetComponent<UseDelayComponent>(owner, out var useDelay) && _entManager.System<UseDelaySystem>().IsDelayed((owner, useDelay)) ||
!blackboard.TryGetValue<EntityUid>(TargetKey, out var moveTarget, _entManager) ||
!_entManager.TryGetComponent<TransformComponent>(moveTarget, out var targetXform))
Expand All @@ -31,6 +82,18 @@ public override HTNOperatorStatus Update(NPCBlackboard blackboard, float frameTi
}

_entManager.System<InteractionSystem>().UserInteraction(owner, targetXform.Coordinates, moveTarget);

// Detect doAfter, save it, and don't exit from this operator
if (doAfter != null && nextId != doAfter.NextId)
{
blackboard.SetValue(CurrentDoAfter, nextId);
return HTNOperatorStatus.Continuing;
}

// We shouldn't arrive here if we start a doafter, so fail if we expected a doafter
if(ExpectDoAfter)
return HTNOperatorStatus.Failed;

return HTNOperatorStatus.Finished;
}
}

0 comments on commit ccdbdee

Please sign in to comment.