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
141 changes: 54 additions & 87 deletions Runtime/UnitaskFbtNodes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using System.Threading;
using Cysharp.Threading.Tasks;

namespace Baltin.UnitaskFBT
namespace Baltin.UFBT
{
/// <summary>
/// Async Functional Behavior Tree pattern
Expand All @@ -15,19 +15,6 @@ namespace Baltin.UnitaskFBT
/// <typeparam name="T">Type of 'blackboard' that represents the controlled object.</typeparam>
public static class UnitaskFbtNodes
{
/// <summary>
/// Classic inverter node
/// </summary>
/// <param name="board">Blackboard object</param>
/// <param name="func">Delegate receiving T and returning Status</param>
/// <returns></returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static async UniTask<bool> Inverter<T>(
this T board,
CancellationToken ct,
Func<T, CancellationToken, UniTask<bool>> func)
=> !await func.Invoke(board, ct);

/// <summary>
/// Execute the given func delegate if the given condition is true
/// </summary>
Expand All @@ -39,10 +26,9 @@ public static async UniTask<bool> Inverter<T>(
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static async UniTask<bool> If<T>(
this T board,
CancellationToken ct,
Func<T, bool> condition,
Func<T, CancellationToken, UniTask<bool>> func)
=> condition.Invoke(board) && await func.Invoke(board, ct);
Func<T, UniTask<bool>> func)
=> condition(board) && await func(board);

/// <summary>
/// Classic selector node
Expand All @@ -59,28 +45,29 @@ public static async UniTask<bool> If<T>(
/// <param name="token"></param>
/// <returns></returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static async UniTask<bool> Selector<T>(this T board, CancellationToken token,
Func<T, CancellationToken, UniTask<bool>> f1,
Func<T, CancellationToken, UniTask<bool>> f2,
Func<T, CancellationToken, UniTask<bool>> f3 = null,
Func<T, CancellationToken, UniTask<bool>> f4 = null,
Func<T, CancellationToken, UniTask<bool>> f5 = null,
Func<T, CancellationToken, UniTask<bool>> f6 = null,
Func<T, CancellationToken, UniTask<bool>> f7 = null,
Func<T, CancellationToken, UniTask<bool>> f8 = null)
public static async UniTask<bool> Selector<T>(this T board,
Func<T, UniTask<bool>> f1,
Func<T, UniTask<bool>> f2,
Func<T, UniTask<bool>> f3 = null,
Func<T, UniTask<bool>> f4 = null,
Func<T, UniTask<bool>> f5 = null,
Func<T, UniTask<bool>> f6 = null,
Func<T, UniTask<bool>> f7 = null,
Func<T, UniTask<bool>> f8 = null)
{
var s = f1 is not null && await f1.Invoke(board, token); if(s) return true;
s = f2 is not null && await f2.Invoke(board, token); if(s) return true;
s = f3 is not null && await f3.Invoke(board, token); if(s) return true;
s = f4 is not null && await f4.Invoke(board, token); if(s) return true;
s = f5 is not null && await f5.Invoke(board, token); if(s) return true;
s = f6 is not null && await f6.Invoke(board, token); if(s) return true;
s = f7 is not null && await f7.Invoke(board, token); if(s) return true;
s = f8 is not null && await f8.Invoke(board, token); if(s) return true;
var s = await f1(board); if(s) return true;
s = await f2(board); if(s) return true;
s = f3 is not null && await f3(board); if(s) return true;
s = f4 is not null && await f4(board); if(s) return true;
s = f5 is not null && await f5(board); if(s) return true;
s = f6 is not null && await f6(board); if(s) return true;
s = f7 is not null && await f7(board); if(s) return true;
s = f8 is not null && await f8(board); if(s) return true;

return false;
}

// ReSharper disable Unity.PerformanceAnalysis
/// <summary>
/// Classic sequencer node
/// </summary>
Expand All @@ -95,24 +82,24 @@ public static async UniTask<bool> Selector<T>(this T board, CancellationToken to
/// <param name="f8">Optional delegate receiving T and returning Status</param>
/// <returns></returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static async UniTask<bool> Sequencer<T>(this T board, CancellationToken token,
Func<T, CancellationToken, UniTask<bool>> f1,
Func<T, CancellationToken, UniTask<bool>> f2,
Func<T, CancellationToken, UniTask<bool>> f3 = null,
Func<T, CancellationToken, UniTask<bool>> f4 = null,
Func<T, CancellationToken, UniTask<bool>> f5 = null,
Func<T, CancellationToken, UniTask<bool>> f6 = null,
Func<T, CancellationToken, UniTask<bool>> f7 = null,
Func<T, CancellationToken, UniTask<bool>> f8 = null)
public static async UniTask<bool> Sequencer<T>(this T board,
Func<T, UniTask<bool>> f1,
Func<T, UniTask<bool>> f2,
Func<T, UniTask<bool>> f3 = null,
Func<T, UniTask<bool>> f4 = null,
Func<T, UniTask<bool>> f5 = null,
Func<T, UniTask<bool>> f6 = null,
Func<T, UniTask<bool>> f7 = null,
Func<T, UniTask<bool>> f8 = null)
{
var s = f1 is not null && await f1.Invoke(board, token); if(!s) return false;
s = f2 is not null && await f2.Invoke(board, token); if(!s) return false;
s = f3 is not null && await f3.Invoke(board, token); if(!s) return false;
s = f4 is not null && await f4.Invoke(board, token); if(!s) return false;
s = f5 is not null && await f5.Invoke(board, token); if(!s) return false;
s = f6 is not null && await f6.Invoke(board, token); if(!s) return false;
s = f7 is not null && await f7.Invoke(board, token); if(!s) return false;
s = f8 is not null && await f8.Invoke(board, token); if(!s) return false;
var s = await f1(board); if(!s) return false;
s = await f2(board); if(!s) return false;
s = f3 is not null && await f3(board); if(!s) return false;
s = f4 is not null && await f4(board); if(!s) return false;
s = f5 is not null && await f5(board); if(!s) return false;
s = f6 is not null && await f6(board); if(!s) return false;
s = f7 is not null && await f7(board); if(!s) return false;
s = f8 is not null && await f8(board); if(!s) return false;

return true;
}
Expand All @@ -121,25 +108,6 @@ public static async UniTask<bool> Sequencer<T>(this T board, CancellationToken t
//Every conditional node can be replaced by two nodes the first of them is an Action node containing the condition and wrapping the second mains node
//But usually is more convenient to use one conditional node instead

/// <summary>
/// Check condition, execute given action and then return its inverted result
/// </summary>
/// <param name="board">Blackboard object</param>
/// <param name="condition">Condition given as a delegate returning bool</param>
/// <param name="func">Action returning Status</param>
/// <param name="elseFunc"></param>
/// <returns>If the condition is false return Failure. Else return inverted value of the func</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static async UniTask<bool> ConditionalInverter<T>(
this T board,
CancellationToken token,
Func<T, bool> condition,
Func<T, CancellationToken, UniTask<bool>> func,
Func<T, CancellationToken, UniTask<bool>> elseFunc = null)
=> condition.Invoke(board)
? await board.Inverter(token, func)
: elseFunc != null && await board.Inverter(token, elseFunc);

/// <summary>
/// Check condition before Selector
/// Returns Failure if the condition is false
Expand All @@ -154,17 +122,16 @@ public static async UniTask<bool> ConditionalInverter<T>(
/// <param name="f6">Optional delegate receiving T and returning Status</param>
/// <returns>If the condition is false return Failure. Else return the result of Selector</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static async UniTask<bool> ConditionalSelector<T>(
public static async UniTask<bool> IfSelector<T>(
this T board,
CancellationToken token,
Func<T, bool> condition,
Func<T, CancellationToken, UniTask<bool>> f1,
Func<T, CancellationToken, UniTask<bool>> f2,
Func<T, CancellationToken, UniTask<bool>> f3 = null,
Func<T, CancellationToken, UniTask<bool>> f4 = null,
Func<T, CancellationToken, UniTask<bool>> f5 = null,
Func<T, CancellationToken, UniTask<bool>> f6 = null)
=> condition.Invoke(board) && await board.Selector(token, f1, f2, f3, f4, f5, f6);
Func<T, UniTask<bool>> f1,
Func<T, UniTask<bool>> f2,
Func<T, UniTask<bool>> f3 = null,
Func<T, UniTask<bool>> f4 = null,
Func<T, UniTask<bool>> f5 = null,
Func<T, UniTask<bool>> f6 = null)
=> condition(board) && await board.Selector(f1, f2, f3, f4, f5, f6);

/// <summary>
/// Check condition before Sequencer
Expand All @@ -179,14 +146,14 @@ public static async UniTask<bool> ConditionalSelector<T>(
/// <param name="f6">Optional delegate receiving T and returning Status</param>
/// <returns>If the condition is false return Failure. Else return the result of Sequencer</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static async UniTask<bool> ConditionalSequencer<T>(this T board, CancellationToken token,
public static async UniTask<bool> IfSequencer<T>(this T board,
Func<T, bool> condition,
Func<T, CancellationToken, UniTask<bool>> f1,
Func<T, CancellationToken, UniTask<bool>> f2,
Func<T, CancellationToken, UniTask<bool>> f3 = null,
Func<T, CancellationToken, UniTask<bool>> f4 = null,
Func<T, CancellationToken, UniTask<bool>> f5 = null,
Func<T, CancellationToken, UniTask<bool>> f6 = null)
=> condition.Invoke(board) && await board.Sequencer(token, f1, f2, f3, f4, f5, f6);
Func<T, UniTask<bool>> f1,
Func<T, UniTask<bool>> f2,
Func<T, UniTask<bool>> f3 = null,
Func<T, UniTask<bool>> f4 = null,
Func<T, UniTask<bool>> f5 = null,
Func<T, UniTask<bool>> f6 = null)
=> condition(board) && await board.Sequencer(f1, f2, f3, f4, f5, f6);
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "com.baltin.ufbt",
"version": "0.5.4",
"version": "0.6.0",
"displayName": "UniTaskFBT",
"description": "Async Functional Behavior Tree implementation based on UniTask",
"unity": "2021.2",
Expand Down