Skip to content

Commit

Permalink
added state checkers for programmatically checking state before actio…
Browse files Browse the repository at this point in the history
…n evaluation or execution
caesuric committed Jan 3, 2024
1 parent f215167 commit cb450fe
Showing 4 changed files with 28 additions and 4 deletions.
15 changes: 13 additions & 2 deletions MountainGoap/Action.cs
Original file line number Diff line number Diff line change
@@ -61,8 +61,16 @@ public class Action {
/// </summary>
private readonly Dictionary<string, string> parameterPostconditions = new();

/// <summary>
/// State mutator for modifying state programmatically after action execution or evaluation.
/// </summary>
private readonly StateMutatorCallback? stateMutator;

/// <summary>
/// State checker for checking state programmatically before action execution or evaluation.
/// </summary>
private readonly StateCheckerCallback? stateChecker;

/// <summary>
/// Parameters to be passed to the action.
/// </summary>
@@ -82,7 +90,8 @@ public class Action {
/// <param name="arithmeticPostconditions">Arithmetic postconditions added to state after the action is successfully executed.</param>
/// <param name="parameterPostconditions">Parameter postconditions copied to state after the action is successfully executed.</param>
/// <param name="stateMutator">Callback for modifying state after action execution or evaluation.</param>
public Action(string? name = null, Dictionary<string, PermutationSelectorCallback>? permutationSelectors = null, ExecutorCallback? executor = null, float cost = 1f, CostCallback? costCallback = null, Dictionary<string, object?>? preconditions = null, Dictionary<string, ComparisonValuePair>? comparativePreconditions = null, Dictionary<string, object?>? postconditions = null, Dictionary<string, object>? arithmeticPostconditions = null, Dictionary<string, string>? parameterPostconditions = null, StateMutatorCallback? stateMutator = null) {
/// <param name="stateChecker">Callback for checking state before action execution or evaluation.</param>
public Action(string? name = null, Dictionary<string, PermutationSelectorCallback>? permutationSelectors = null, ExecutorCallback? executor = null, float cost = 1f, CostCallback? costCallback = null, Dictionary<string, object?>? preconditions = null, Dictionary<string, ComparisonValuePair>? comparativePreconditions = null, Dictionary<string, object?>? postconditions = null, Dictionary<string, object>? arithmeticPostconditions = null, Dictionary<string, string>? parameterPostconditions = null, StateMutatorCallback? stateMutator = null, StateCheckerCallback? stateChecker = null) {
if (permutationSelectors == null) this.permutationSelectors = new();
else this.permutationSelectors = permutationSelectors;
if (executor == null) this.executor = DefaultExecutorCallback;
@@ -96,6 +105,7 @@ public Action(string? name = null, Dictionary<string, PermutationSelectorCallbac
if (arithmeticPostconditions != null) this.arithmeticPostconditions = arithmeticPostconditions;
if (parameterPostconditions != null) this.parameterPostconditions = parameterPostconditions;
if (stateMutator != null) this.stateMutator = stateMutator;
if (stateChecker != null) this.stateChecker = stateChecker;
}

/// <summary>
@@ -118,7 +128,7 @@ public Action(string? name = null, Dictionary<string, PermutationSelectorCallbac
/// </summary>
/// <returns>A copy of the action.</returns>
public Action Copy() {
var newAction = new Action(Name, permutationSelectors, executor, cost, costCallback, preconditions.Copy(), comparativePreconditions.Copy(), postconditions.Copy(), arithmeticPostconditions.CopyNonNullable(), parameterPostconditions.Copy(), stateMutator) {
var newAction = new Action(Name, permutationSelectors, executor, cost, costCallback, preconditions.Copy(), comparativePreconditions.Copy(), postconditions.Copy(), arithmeticPostconditions.CopyNonNullable(), parameterPostconditions.Copy(), stateMutator, stateChecker) {
parameters = parameters.Copy()
};
return newAction;
@@ -200,6 +210,7 @@ internal bool IsPossible(Dictionary<string, object?> state) {
}
else return false;
}
if (stateChecker?.Invoke(this, state) == false) return false;
return true;
}

13 changes: 13 additions & 0 deletions MountainGoap/CallbackDelegates/StateCheckerCallback.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// <copyright file="StateCheckerCallback.cs" company="Chris Muller">
// Copyright (c) Chris Muller. All rights reserved.
// </copyright>

namespace MountainGoap {
/// <summary>
/// Delegate type for a callback that checks state before action execution or evaluation (the latter during planning).
/// </summary>
/// <param name="action">Action being executed or evaluated.</param>
/// <param name="currentState">State as it will be when the action is executed or evaluated.</param>
/// <returns>True if the state is okay for executing the action, otherwise false.</returns>
public delegate bool StateCheckerCallback(Action action, Dictionary<string, object?> currentState);
}
2 changes: 1 addition & 1 deletion MountainGoap/CallbackDelegates/StateMutatorCallback.cs
Original file line number Diff line number Diff line change
@@ -6,7 +6,7 @@ namespace MountainGoap {
/// <summary>
/// Delegate type for a callback that mutates state following action execution or evaluation (the latter during planning).
/// </summary>
/// <param name="action">Action being executed.</param>
/// <param name="action">Action being executed or evaluated.</param>
/// <param name="currentState">State as it will be when the action is executed or evaluated.</param>
public delegate void StateMutatorCallback(Action action, Dictionary<string, object?> currentState);
}
2 changes: 1 addition & 1 deletion MountainGoap/MountainGoap.csproj
Original file line number Diff line number Diff line change
@@ -6,7 +6,7 @@
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<GenerateDocumentationFile>True</GenerateDocumentationFile>
<VersionPrefix>0.12.0</VersionPrefix>
<VersionPrefix>0.13.0</VersionPrefix>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<Description>A simple GOAP (Goal Oriented Action Planning) library.</Description>
<Copyright>MIT licensed 2022</Copyright>

0 comments on commit cb450fe

Please sign in to comment.