Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@ namespace CrashKonijn.Goap.Runtime
public class AgentTypeJobRunner : IAgentTypeJobRunner
{
private readonly IAgentType agentType;
private readonly IGraphResolver resolver;

private List<JobRunHandle> resolveHandles = new();
private readonly IExecutableBuilder executableBuilder;
private readonly IConditionBuilder conditionBuilder;
private readonly ICostBuilder costBuilder;
private readonly IEnabledBuilder enabledBuilder;
private readonly IExecutableBuilder executableBuilder;
private readonly IPositionBuilder positionBuilder;
private readonly ICostBuilder costBuilder;
private readonly IConditionBuilder conditionBuilder;
private readonly IGraphResolver resolver;

private readonly List<int> goalIndexes = new();

private List<int> goalIndexes = new();
private readonly List<JobRunHandle> resolveHandles = new();

public AgentTypeJobRunner(IAgentType agentType, IGraphResolver graphResolver)
{
Expand Down Expand Up @@ -51,6 +51,60 @@ public void Run(IMonoGoapActionProvider[] queue)
}
}

public void Complete()
{
foreach (var resolveHandle in this.resolveHandles)
{
var result = resolveHandle.Handle.Complete();

if (resolveHandle.ActionProvider.GoalRequest != resolveHandle.GoalRequest)
continue;

if (resolveHandle.ActionProvider.IsNull())
continue;

var goal = result.Goal;
if (goal == null)
{
resolveHandle.ActionProvider.Events.NoActionFound(resolveHandle.GoalRequest);
continue;
}

var action = result.Actions.FirstOrDefault() as IGoapAction;

if (action is null)
{
resolveHandle.ActionProvider.Events.NoActionFound(resolveHandle.GoalRequest);
continue;
}

if (action != resolveHandle.ActionProvider.Receiver.ActionState.Action)
resolveHandle.ActionProvider.SetAction(new GoalResult
{
Goal = goal,
Plan = result.Actions,
Action = action
});
}

this.resolveHandles.Clear();
}

public void Dispose()
{
foreach (var resolveHandle in this.resolveHandles)
{
resolveHandle.Handle.Complete();
}

this.resolver.Dispose();
}

public IGraph GetGraph()
{
return this.resolver.GetGraph();
}

private void Run(IMonoGoapActionProvider actionProvider)
{
if (actionProvider.IsNull())
Expand Down Expand Up @@ -101,8 +155,8 @@ private void Run(IMonoGoapActionProvider actionProvider)
Positions = new NativeArray<float3>(this.positionBuilder.Build(), Allocator.TempJob),
Costs = new NativeArray<float>(this.costBuilder.Build(), Allocator.TempJob),
ConditionsMet = new NativeArray<bool>(this.conditionBuilder.Build(), Allocator.TempJob),
DistanceMultiplier = actionProvider.DistanceMultiplier,
}),
DistanceMultiplier = actionProvider.DistanceMultiplier
})
});
}

Expand Down Expand Up @@ -144,8 +198,12 @@ private void FillBuilders(IMonoGoapActionProvider actionProvider)
var target = actionProvider.WorldData.GetTarget(node);

this.executableBuilder.SetExecutable(node, node.IsExecutable(actionProvider.Receiver, allMet));
this.enabledBuilder.SetEnabled(node, node.IsEnabled(actionProvider.Receiver));
this.costBuilder.SetCost(node, node.GetCost(actionProvider.Receiver, actionProvider.Receiver.Injector, target));

var isEnabled = node.IsEnabled(actionProvider.Receiver);
var cost = isEnabled ? node.GetCost(actionProvider.Receiver, actionProvider.Receiver.Injector, target) : 0f;

this.enabledBuilder.SetEnabled(node, isEnabled);
this.costBuilder.SetCost(node, cost);

this.positionBuilder.SetPosition(node, target?.GetValidPosition());
}
Expand Down Expand Up @@ -192,47 +250,6 @@ private bool MayResolve(IGoapActionProvider actionProvider)
return actionProvider.Receiver.ActionState.RunState.MayResolve(agent);
}

public void Complete()
{
foreach (var resolveHandle in this.resolveHandles)
{
var result = resolveHandle.Handle.Complete();

if (resolveHandle.ActionProvider.GoalRequest != resolveHandle.GoalRequest)
continue;

if (resolveHandle.ActionProvider.IsNull())
continue;

var goal = result.Goal;
if (goal == null)
{
resolveHandle.ActionProvider.Events.NoActionFound(resolveHandle.GoalRequest);
continue;
}

var action = result.Actions.FirstOrDefault() as IGoapAction;

if (action is null)
{
resolveHandle.ActionProvider.Events.NoActionFound(resolveHandle.GoalRequest);
continue;
}

if (action != resolveHandle.ActionProvider.Receiver.ActionState.Action)
{
resolveHandle.ActionProvider.SetAction(new GoalResult
{
Goal = goal,
Plan = result.Actions,
Action = action,
});
}
}

this.resolveHandles.Clear();
}

private void LogRequest(IGoapActionProvider actionProvider, IGoalRequest request)
{
#if UNITY_EDITOR
Expand All @@ -255,29 +272,17 @@ private void LogRequest(IGoapActionProvider actionProvider, IGoalRequest request
#endif
}

public void Dispose()
{
foreach (var resolveHandle in this.resolveHandles)
{
resolveHandle.Handle.Complete();
}

this.resolver.Dispose();
}

private class JobRunHandle
{
public IMonoGoapActionProvider ActionProvider { get; }
public IResolveHandle Handle { get; set; }
public IGoalRequest GoalRequest { get; set; }

public JobRunHandle(IMonoGoapActionProvider actionProvider, IGoalRequest goalRequest)
{
this.ActionProvider = actionProvider;
this.GoalRequest = goalRequest;
}
}

public IGraph GetGraph() => this.resolver.GetGraph();
public IMonoGoapActionProvider ActionProvider { get; }
public IResolveHandle Handle { get; set; }
public IGoalRequest GoalRequest { get; }
}
}
}
}
Loading