From d1afad3b37d058b1c3d52ed9b02e26fb76181c8b Mon Sep 17 00:00:00 2001 From: NetGnome Date: Thu, 3 Oct 2013 14:09:07 -0400 Subject: [PATCH 01/27] Refactored Selector/Sequence to PartialSelector/Sequence Refactored ParallelSequence/Selector to Selector/Sequence Added StatefulSelector/Sequence (keeps state during running) Added Initial Test cases. Uses CSTester library --- BehaviorLibrary.sln | 49 +++-- BehaviorLibrary.userprefs | 31 +++ BehaviorLibrary/BehaviorLibrary.csproj | 8 +- .../Components/Composites/ParallelSequence.cs | 66 ------- ...ParallelSelector.cs => PartialSelector.cs} | 178 ++++++++---------- .../Components/Composites/PartialSequence.cs | 76 ++++++++ .../Components/Composites/RootSelector.cs | 2 +- .../Components/Composites/Selector.cs | 143 +++++++------- .../Components/Composites/Sequence.cs | 142 +++++++------- .../Components/Composites/StatefulSelector.cs | 59 ++++++ .../Components/Composites/StatefulSequence.cs | 66 +++++++ Tests/Program.cs | 45 +++++ Tests/Properties/AssemblyInfo.cs | 22 +++ Tests/TestCases.cs | 121 ++++++++++++ Tests/Tests.csproj | 52 +++++ 15 files changed, 716 insertions(+), 344 deletions(-) create mode 100644 BehaviorLibrary.userprefs delete mode 100644 BehaviorLibrary/Components/Composites/ParallelSequence.cs rename BehaviorLibrary/Components/Composites/{ParallelSelector.cs => PartialSelector.cs} (50%) create mode 100644 BehaviorLibrary/Components/Composites/PartialSequence.cs create mode 100644 BehaviorLibrary/Components/Composites/StatefulSelector.cs create mode 100644 BehaviorLibrary/Components/Composites/StatefulSequence.cs create mode 100644 Tests/Program.cs create mode 100644 Tests/Properties/AssemblyInfo.cs create mode 100644 Tests/TestCases.cs create mode 100644 Tests/Tests.csproj diff --git a/BehaviorLibrary.sln b/BehaviorLibrary.sln index 9eed5dd..92a0054 100644 --- a/BehaviorLibrary.sln +++ b/BehaviorLibrary.sln @@ -1,20 +1,29 @@ - -Microsoft Visual Studio Solution File, Format Version 11.00 -# Visual Studio 2010 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BehaviorLibrary", "BehaviorLibrary\BehaviorLibrary.csproj", "{CC824B6F-6145-485F-9604-FB94F0ECACA7}" -EndProject -Global - GlobalSection(SolutionConfigurationPlatforms) = preSolution - Debug|Any CPU = Debug|Any CPU - Release|Any CPU = Release|Any CPU - EndGlobalSection - GlobalSection(ProjectConfigurationPlatforms) = postSolution - {CC824B6F-6145-485F-9604-FB94F0ECACA7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {CC824B6F-6145-485F-9604-FB94F0ECACA7}.Debug|Any CPU.Build.0 = Debug|Any CPU - {CC824B6F-6145-485F-9604-FB94F0ECACA7}.Release|Any CPU.ActiveCfg = Release|Any CPU - {CC824B6F-6145-485F-9604-FB94F0ECACA7}.Release|Any CPU.Build.0 = Release|Any CPU - EndGlobalSection - GlobalSection(SolutionProperties) = preSolution - HideSolutionNode = FALSE - EndGlobalSection -EndGlobal + +Microsoft Visual Studio Solution File, Format Version 11.00 +# Visual Studio 2010 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BehaviorLibrary", "BehaviorLibrary\BehaviorLibrary.csproj", "{CC824B6F-6145-485F-9604-FB94F0ECACA7}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tests", "Tests\Tests.csproj", "{A7D9F051-A51E-42FF-AD2B-D94DC8A06650}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {A7D9F051-A51E-42FF-AD2B-D94DC8A06650}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {A7D9F051-A51E-42FF-AD2B-D94DC8A06650}.Debug|Any CPU.Build.0 = Debug|Any CPU + {A7D9F051-A51E-42FF-AD2B-D94DC8A06650}.Release|Any CPU.ActiveCfg = Release|Any CPU + {A7D9F051-A51E-42FF-AD2B-D94DC8A06650}.Release|Any CPU.Build.0 = Release|Any CPU + {CC824B6F-6145-485F-9604-FB94F0ECACA7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {CC824B6F-6145-485F-9604-FB94F0ECACA7}.Debug|Any CPU.Build.0 = Debug|Any CPU + {CC824B6F-6145-485F-9604-FB94F0ECACA7}.Release|Any CPU.ActiveCfg = Release|Any CPU + {CC824B6F-6145-485F-9604-FB94F0ECACA7}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(MonoDevelopProperties) = preSolution + StartupItem = BehaviorLibrary\BehaviorLibrary.csproj + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/BehaviorLibrary.userprefs b/BehaviorLibrary.userprefs new file mode 100644 index 0000000..a70d3d6 --- /dev/null +++ b/BehaviorLibrary.userprefs @@ -0,0 +1,31 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/BehaviorLibrary/BehaviorLibrary.csproj b/BehaviorLibrary/BehaviorLibrary.csproj index f351907..92e478b 100644 --- a/BehaviorLibrary/BehaviorLibrary.csproj +++ b/BehaviorLibrary/BehaviorLibrary.csproj @@ -44,18 +44,20 @@ - - - + + + + + diff --git a/BehaviorLibrary/Components/Composites/ParallelSequence.cs b/BehaviorLibrary/Components/Composites/ParallelSequence.cs deleted file mode 100644 index e08dd1f..0000000 --- a/BehaviorLibrary/Components/Composites/ParallelSequence.cs +++ /dev/null @@ -1,66 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; - -namespace BehaviorLibrary.Components.Composites -{ - public class ParallelSequence : BehaviorComponent - { - - private BehaviorComponent[] p_Behaviors; - - /// - /// attempts to run the behaviors all in one cycle - /// -Returns Success when all are successful - /// -Returns Failure if one behavior fails or an error occurs - /// -Does not Return Running - /// - /// - public ParallelSequence(params BehaviorComponent[] behaviors) - { - p_Behaviors = behaviors; - } - - /// - /// performs the given behavior - /// - /// the behaviors return code - public override BehaviorReturnCode Behave() - { - - for(int i = 0; i < p_Behaviors.Length;i++) - { - try - { - switch (p_Behaviors[i].Behave()) - { - case BehaviorReturnCode.Failure: - ReturnCode = BehaviorReturnCode.Failure; - return ReturnCode; - case BehaviorReturnCode.Success: - continue; - case BehaviorReturnCode.Running: - continue; - default: - ReturnCode = BehaviorReturnCode.Success; - return ReturnCode; - } - } - catch (Exception e) - { -#if DEBUG - Console.Error.WriteLine(e.ToString()); -#endif - ReturnCode = BehaviorReturnCode.Failure; - return ReturnCode; - } - } - - ReturnCode = BehaviorReturnCode.Success; - return ReturnCode; - } - - - } -} diff --git a/BehaviorLibrary/Components/Composites/ParallelSelector.cs b/BehaviorLibrary/Components/Composites/PartialSelector.cs similarity index 50% rename from BehaviorLibrary/Components/Composites/ParallelSelector.cs rename to BehaviorLibrary/Components/Composites/PartialSelector.cs index 70f1ef0..3391c47 100644 --- a/BehaviorLibrary/Components/Composites/ParallelSelector.cs +++ b/BehaviorLibrary/Components/Composites/PartialSelector.cs @@ -1,100 +1,78 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; - -namespace BehaviorLibrary.Components.Composites -{ - public class ParallelSelector : BehaviorComponent - { - - protected BehaviorComponent[] p_Behaviors; - - private short p_Selections = 0; - - private short p_SelLength = 0; - - /// - /// Selects among the given behavior components - /// Performs an OR-Like behavior and will "fail-over" to each successive component until Success is reached or Failure is certain - /// -Returns Success if a behavior component returns Success - /// -Returns Running if a behavior component returns Running - /// -Returns Failure if all behavior components returned Failure - /// - /// one to many behavior components - public ParallelSelector(params BehaviorComponent[] behaviors) - { - p_Behaviors = behaviors; - p_SelLength = (short)p_Behaviors.Length; - } - - /// - /// performs the given behavior - /// - /// the behaviors return code - public override BehaviorReturnCode Behave() - { - - for (int i = 0; i < p_SelLength; i++) - { - try - { - switch (p_Behaviors[i].Behave()) - { - case BehaviorReturnCode.Failure: - continue; - case BehaviorReturnCode.Success: - ReturnCode = BehaviorReturnCode.Success; - return ReturnCode; - case BehaviorReturnCode.Running: - ReturnCode = BehaviorReturnCode.Running; - return ReturnCode; - default: - continue; - } - } - catch (Exception e) - { -#if DEBUG - Console.Error.WriteLine(e.ToString()); -#endif - continue; - } - } - - - /* - while (p_Selections < p_SelLength) - { - try - { - switch (p_Behaviors[p_Selections].Behave()) - { - case BehaviorReturnCode.Failure: - p_Selections++; - continue; - case BehaviorReturnCode.Success: - p_Selections = 0; - ReturnCode = BehaviorReturnCode.Success; - return ReturnCode; - case BehaviorReturnCode.Running: - ReturnCode = BehaviorReturnCode.Running; - return ReturnCode; - default: - p_Selections++; - continue; - } - } - catch (Exception) - { - p_Selections++; - continue; - } - }*/ - - p_Selections = 0; - ReturnCode = BehaviorReturnCode.Failure; - return ReturnCode; - } - } -} +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace BehaviorLibrary.Components.Composites +{ + public class PartialSelector : BehaviorComponent + { + + protected BehaviorComponent[] s_Behaviors; + + private short selections = 0; + + private short selLength = 0; + + /// + /// Selects among the given behavior components (one evaluation per Behave call) + /// Performs an OR-Like behavior and will "fail-over" to each successive component until Success is reached or Failure is certain + /// -Returns Success if a behavior component returns Success + /// -Returns Running if a behavior component returns Failure or Running + /// -Returns Failure if all behavior components returned Failure or an error has occured + /// + /// one to many behavior components + public PartialSelector(params BehaviorComponent[] behaviors) + { + s_Behaviors = behaviors; + selLength = (short)s_Behaviors.Length; + } + + /// + /// performs the given behavior + /// + /// the behaviors return code + public override BehaviorReturnCode Behave() + { + while (selections < selLength) + { + try + { + switch (s_Behaviors[selections].Behave()) + { + case BehaviorReturnCode.Failure: + selections++; + ReturnCode = BehaviorReturnCode.Running; + return ReturnCode; + case BehaviorReturnCode.Success: + selections = 0; + ReturnCode = BehaviorReturnCode.Success; + return ReturnCode; + case BehaviorReturnCode.Running: + ReturnCode = BehaviorReturnCode.Running; + return ReturnCode; + default: + selections++; + ReturnCode = BehaviorReturnCode.Failure; + return ReturnCode; + } + } + catch (Exception e) + { +#if DEBUG + Console.Error.WriteLine(e.ToString()); +#endif + selections++; + ReturnCode = BehaviorReturnCode.Failure; + return ReturnCode; + } + } + + selections = 0; + ReturnCode = BehaviorReturnCode.Failure; + return ReturnCode; + } + + + } +} diff --git a/BehaviorLibrary/Components/Composites/PartialSequence.cs b/BehaviorLibrary/Components/Composites/PartialSequence.cs new file mode 100644 index 0000000..04b36d2 --- /dev/null +++ b/BehaviorLibrary/Components/Composites/PartialSequence.cs @@ -0,0 +1,76 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace BehaviorLibrary.Components.Composites +{ + public class PartialSequence : BehaviorComponent + { + + protected BehaviorComponent[] s_Behaviors; + + private short sequence = 0; + + private short seqLength = 0; + + /// + /// Performs the given behavior components sequentially (one evaluation per Behave call) + /// Performs an AND-Like behavior and will perform each successive component + /// -Returns Success if all behavior components return Success + /// -Returns Running if an individual behavior component returns Success or Running + /// -Returns Failure if a behavior components returns Failure or an error is encountered + /// + /// one to many behavior components + public PartialSequence(params BehaviorComponent[] behaviors) + { + s_Behaviors = behaviors; + seqLength = (short) s_Behaviors.Length; + } + + /// + /// performs the given behavior + /// + /// the behaviors return code + public override BehaviorReturnCode Behave() + { + //while you can go through them, do so + while (sequence < seqLength) + { + try + { + switch (s_Behaviors[sequence].Behave()) + { + case BehaviorReturnCode.Failure: + sequence = 0; + ReturnCode = BehaviorReturnCode.Failure; + return ReturnCode; + case BehaviorReturnCode.Success: + sequence++; + ReturnCode = BehaviorReturnCode.Running; + return ReturnCode; + case BehaviorReturnCode.Running: + ReturnCode = BehaviorReturnCode.Running; + return ReturnCode; + } + } + catch (Exception e) + { +#if DEBUG + Console.Error.WriteLine(e.ToString()); +#endif + sequence = 0; + ReturnCode = BehaviorReturnCode.Failure; + return ReturnCode; + } + + } + + sequence = 0; + ReturnCode = BehaviorReturnCode.Success; + return ReturnCode; + + } + + } +} diff --git a/BehaviorLibrary/Components/Composites/RootSelector.cs b/BehaviorLibrary/Components/Composites/RootSelector.cs index 5c41c4e..53c1156 100644 --- a/BehaviorLibrary/Components/Composites/RootSelector.cs +++ b/BehaviorLibrary/Components/Composites/RootSelector.cs @@ -5,7 +5,7 @@ namespace BehaviorLibrary.Components.Composites { - public class RootSelector : Selector + public class RootSelector : PartialSelector { private BehaviorComponent[] rs_Behaviors; diff --git a/BehaviorLibrary/Components/Composites/Selector.cs b/BehaviorLibrary/Components/Composites/Selector.cs index 7f95035..200a507 100644 --- a/BehaviorLibrary/Components/Composites/Selector.cs +++ b/BehaviorLibrary/Components/Composites/Selector.cs @@ -1,78 +1,65 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; - -namespace BehaviorLibrary.Components.Composites -{ - public class Selector : BehaviorComponent - { - - protected BehaviorComponent[] s_Behaviors; - - private short selections = 0; - - private short selLength = 0; - - /// - /// Selects among the given behavior components - /// Performs an OR-Like behavior and will "fail-over" to each successive component until Success is reached or Failure is certain - /// -Returns Success if a behavior component returns Success - /// -Returns Running if a behavior component returns Failure or Running - /// -Returns Failure if all behavior components returned Failure or an error has occured - /// - /// one to many behavior components - public Selector(params BehaviorComponent[] behaviors) - { - s_Behaviors = behaviors; - selLength = (short)s_Behaviors.Length; - } - - /// - /// performs the given behavior - /// - /// the behaviors return code - public override BehaviorReturnCode Behave() - { - while (selections < selLength) - { - try - { - switch (s_Behaviors[selections].Behave()) - { - case BehaviorReturnCode.Failure: - selections++; - ReturnCode = BehaviorReturnCode.Running; - return ReturnCode; - case BehaviorReturnCode.Success: - selections = 0; - ReturnCode = BehaviorReturnCode.Success; - return ReturnCode; - case BehaviorReturnCode.Running: - ReturnCode = BehaviorReturnCode.Running; - return ReturnCode; - default: - selections++; - ReturnCode = BehaviorReturnCode.Failure; - return ReturnCode; - } - } - catch (Exception e) - { -#if DEBUG - Console.Error.WriteLine(e.ToString()); -#endif - selections++; - ReturnCode = BehaviorReturnCode.Failure; - return ReturnCode; - } - } - - selections = 0; - ReturnCode = BehaviorReturnCode.Failure; - return ReturnCode; - } - - - } -} +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace BehaviorLibrary.Components.Composites +{ + public class Selector : BehaviorComponent + { + + protected BehaviorComponent[] s_Behaviors; + + + /// + /// Selects among the given behavior components + /// Performs an OR-Like behavior and will "fail-over" to each successive component until Success is reached or Failure is certain + /// -Returns Success if a behavior component returns Success + /// -Returns Running if a behavior component returns Running + /// -Returns Failure if all behavior components returned Failure + /// + /// one to many behavior components + public Selector(params BehaviorComponent[] behaviors) + { + s_Behaviors = behaviors; + } + + /// + /// performs the given behavior + /// + /// the behaviors return code + public override BehaviorReturnCode Behave() + { + + for (int i = 0; i < s_Behaviors.Length; i++) + { + try + { + switch (s_Behaviors[i].Behave()) + { + case BehaviorReturnCode.Failure: + continue; + case BehaviorReturnCode.Success: + ReturnCode = BehaviorReturnCode.Success; + return ReturnCode; + case BehaviorReturnCode.Running: + ReturnCode = BehaviorReturnCode.Running; + return ReturnCode; + default: + continue; + } + } + catch (Exception e) + { +#if DEBUG + Console.Error.WriteLine(e.ToString()); +#endif + continue; + } + } + + ReturnCode = BehaviorReturnCode.Failure; + return ReturnCode; + } + } +} diff --git a/BehaviorLibrary/Components/Composites/Sequence.cs b/BehaviorLibrary/Components/Composites/Sequence.cs index 9f1de9d..3e2a96d 100644 --- a/BehaviorLibrary/Components/Composites/Sequence.cs +++ b/BehaviorLibrary/Components/Composites/Sequence.cs @@ -1,76 +1,66 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; - -namespace BehaviorLibrary.Components.Composites -{ - public class Sequence : BehaviorComponent - { - - protected BehaviorComponent[] s_Behaviors; - - private short sequence = 0; - - private short seqLength = 0; - - /// - /// Performs the given behavior components sequentially - /// Performs an AND-Like behavior and will perform each successive component - /// -Returns Success if all behavior components return Success - /// -Returns Running if an individual behavior component returns Success or Running - /// -Returns Failure if a behavior components returns Failure or an error is encountered - /// - /// one to many behavior components - public Sequence(params BehaviorComponent[] behaviors) - { - s_Behaviors = behaviors; - seqLength = (short) s_Behaviors.Length; - } - - /// - /// performs the given behavior - /// - /// the behaviors return code - public override BehaviorReturnCode Behave() - { - //while you can go through them, do so - while (sequence < seqLength) - { - try - { - switch (s_Behaviors[sequence].Behave()) - { - case BehaviorReturnCode.Failure: - sequence = 0; - ReturnCode = BehaviorReturnCode.Failure; - return ReturnCode; - case BehaviorReturnCode.Success: - sequence++; - ReturnCode = BehaviorReturnCode.Running; - return ReturnCode; - case BehaviorReturnCode.Running: - ReturnCode = BehaviorReturnCode.Running; - return ReturnCode; - } - } - catch (Exception e) - { -#if DEBUG - Console.Error.WriteLine(e.ToString()); -#endif - sequence = 0; - ReturnCode = BehaviorReturnCode.Failure; - return ReturnCode; - } - - } - - sequence = 0; - ReturnCode = BehaviorReturnCode.Success; - return ReturnCode; - - } - - } -} +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace BehaviorLibrary.Components.Composites +{ + public class Sequence : BehaviorComponent + { + + private BehaviorComponent[] s_Behaviors; + + /// + /// attempts to run the behaviors all in one cycle + /// -Returns Success when all are successful + /// -Returns Failure if one behavior fails or an error occurs + /// -Does not Return Running + /// + /// + public Sequence(params BehaviorComponent[] behaviors) + { + s_Behaviors = behaviors; + } + + /// + /// performs the given behavior + /// + /// the behaviors return code + public override BehaviorReturnCode Behave() + { + + for(int i = 0; i < s_Behaviors.Length;i++) + { + try + { + switch (s_Behaviors[i].Behave()) + { + case BehaviorReturnCode.Failure: + ReturnCode = BehaviorReturnCode.Failure; + return ReturnCode; + case BehaviorReturnCode.Success: + continue; + case BehaviorReturnCode.Running: + continue; + default: + ReturnCode = BehaviorReturnCode.Success; + return ReturnCode; + } + } + catch (Exception e) + { +#if DEBUG + Console.Error.WriteLine(e.ToString()); +#endif + ReturnCode = BehaviorReturnCode.Failure; + return ReturnCode; + } + } + + ReturnCode = BehaviorReturnCode.Success; + return ReturnCode; + } + + + } +} diff --git a/BehaviorLibrary/Components/Composites/StatefulSelector.cs b/BehaviorLibrary/Components/Composites/StatefulSelector.cs new file mode 100644 index 0000000..e66bf69 --- /dev/null +++ b/BehaviorLibrary/Components/Composites/StatefulSelector.cs @@ -0,0 +1,59 @@ +using System; +using BehaviorLibrary.Components; + +namespace BehaviorLibrary +{ + public class StatefulSelector : BehaviorComponent + { + private BehaviorComponent[] s_Behaviors; + + private int s_LastBehavior = 0; + + /// + /// Selects among the given behavior components (stateful on running) + /// Performs an OR-Like behavior and will "fail-over" to each successive component until Success is reached or Failure is certain + /// -Returns Success if a behavior component returns Success + /// -Returns Running if a behavior component returns Running + /// -Returns Failure if all behavior components returned Failure + /// + /// one to many behavior components + public StatefulSelector(params BehaviorComponent[] behaviors){ + this.s_Behaviors = behaviors; + } + + /// + /// performs the given behavior + /// + /// the behaviors return code + public override BehaviorReturnCode Behave(){ + + for(; s_LastBehavior < s_Behaviors.Length; s_LastBehavior++){ + try{ + switch (s_Behaviors[s_LastBehavior].Behave()){ + case BehaviorReturnCode.Failure: + continue; + case BehaviorReturnCode.Success: + s_LastBehavior = 0; + ReturnCode = BehaviorReturnCode.Success; + return ReturnCode; + case BehaviorReturnCode.Running: + ReturnCode = BehaviorReturnCode.Running; + return ReturnCode; + default: + continue; + } + } + catch (Exception e){ +#if DEBUG + Console.Error.WriteLine(e.ToString()); +#endif + continue; + } + } + + s_LastBehavior = 0; + ReturnCode = BehaviorReturnCode.Failure; + return ReturnCode; + } + } +} diff --git a/BehaviorLibrary/Components/Composites/StatefulSequence.cs b/BehaviorLibrary/Components/Composites/StatefulSequence.cs new file mode 100644 index 0000000..5c3ddc7 --- /dev/null +++ b/BehaviorLibrary/Components/Composites/StatefulSequence.cs @@ -0,0 +1,66 @@ +using System; +using BehaviorLibrary.Components; + +namespace BehaviorLibrary +{ + public class StatefulSequence : BehaviorComponent + { + private BehaviorComponent[] s_Behaviors; + + private int s_LastBehavior = 0; + + /// + /// attempts to run the behaviors all in one cycle (stateful on running) + /// -Returns Success when all are successful + /// -Returns Failure if one behavior fails or an error occurs + /// -Does not Return Running + /// + /// + public StatefulSequence (params BehaviorComponent[] behaviors){ + this.s_Behaviors = behaviors; + } + + /// + /// performs the given behavior + /// + /// the behaviors return code + public override BehaviorReturnCode Behave(){ + + //start from last remembered position + for(; s_LastBehavior < s_Behaviors.Length;s_LastBehavior++){ + try{ + switch (s_Behaviors[s_LastBehavior].Behave()){ + case BehaviorReturnCode.Failure: + s_LastBehavior = 0; + ReturnCode = BehaviorReturnCode.Failure; + return ReturnCode; + case BehaviorReturnCode.Success: + continue; + case BehaviorReturnCode.Running: + ReturnCode = BehaviorReturnCode.Running; + return ReturnCode; + default: + s_LastBehavior = 0; + ReturnCode = BehaviorReturnCode.Success; + return ReturnCode; + } + } + catch (Exception e){ +#if DEBUG + Console.Error.WriteLine(e.ToString()); +#endif + s_LastBehavior = 0; + ReturnCode = BehaviorReturnCode.Failure; + return ReturnCode; + } + } + + s_LastBehavior = 0; + ReturnCode = BehaviorReturnCode.Success; + return ReturnCode; + } + + + } +} + diff --git a/Tests/Program.cs b/Tests/Program.cs new file mode 100644 index 0000000..0ae1742 --- /dev/null +++ b/Tests/Program.cs @@ -0,0 +1,45 @@ +// +// Program.cs +// +// Author: +// Thomas H. Jonell <@Net_Gnome> +// +// Copyright (c) 2013 Thomas H. Jonell +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Lesser General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with this program. If not, see . +using System; +using CSTester; +using CSLogging; + +namespace Tests +{ + class MainClass + { + public static void Main (string[] args) + { + try + { + Tester tester = new Tester(); + tester.registerTestCase(new TestCases()); + tester.initialize(); + Console.Write(tester.getResults()); + } + catch (Exception e) + { + Console.Write(e.ToString()); + } + } + + } +} diff --git a/Tests/Properties/AssemblyInfo.cs b/Tests/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..d1009d6 --- /dev/null +++ b/Tests/Properties/AssemblyInfo.cs @@ -0,0 +1,22 @@ +using System.Reflection; +using System.Runtime.CompilerServices; + +// Information about this assembly is defined by the following attributes. +// Change them to the values specific to your project. +[assembly: AssemblyTitle ("Tests")] +[assembly: AssemblyDescription ("")] +[assembly: AssemblyConfiguration ("")] +[assembly: AssemblyCompany ("")] +[assembly: AssemblyProduct ("")] +[assembly: AssemblyCopyright ("tom")] +[assembly: AssemblyTrademark ("")] +[assembly: AssemblyCulture ("")] +// The assembly version has the format "{Major}.{Minor}.{Build}.{Revision}". +// The form "{Major}.{Minor}.*" will automatically update the build and revision, +// and "{Major}.{Minor}.{Build}.*" will update just the revision. +[assembly: AssemblyVersion ("1.0.*")] +// The following attributes are used to specify the signing key for the assembly, +// if desired. See the Mono documentation for more information about signing. +//[assembly: AssemblyDelaySign(false)] +//[assembly: AssemblyKeyFile("")] + diff --git a/Tests/TestCases.cs b/Tests/TestCases.cs new file mode 100644 index 0000000..5ed4908 --- /dev/null +++ b/Tests/TestCases.cs @@ -0,0 +1,121 @@ +// +// TestCases.cs +// +// Author: +// Thomas H. Jonell <@Net_Gnome> +// +// Copyright (c) 2013 Thomas H. Jonell +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Lesser General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with this program. If not, see . +using System; +using CSTester; +using CSLogging; +using BehaviorLibrary; +using BehaviorLibrary.Components; +using BehaviorLibrary.Components.Actions; + +namespace Tests +{ + public class TestCases + { + public TestCases (){} + + private CSLogger log = CSLogger.Instance; + + [BuildUp] + public void buildup(){ + log.setEnableLogging (true); + log.setEnableDebug (true); + log.setEnableError (true); + log.setEnableMessage (true); + log.loadLog("","behaviorLibrary.log"); + log.enterScope ("TestCases"); + log.logMessage ("----------------- STARTING BEHAVIOR LIBRARY TESTS -----------------"); + } + + [TearDown] + public void teardown(){ + log.enterScope("teardown"); + log.exitScope (); + log.logMessage ("----------------- ENDING BEHAVIOR LIBRARY TESTS -----------------"); + log.exitScope (); + log.closeLog (); + } + + [Test] + public void testStatefulSeq(){ + log.enterScope("testStatefulSeq"); + + bool first = true; + + var foo = new StatefulSequence (new BehaviorAction(delegate(){ + return BehaviorReturnCode.Success; + }),new BehaviorAction( delegate(){ + if(first){ + first = false; + return BehaviorReturnCode.Running; + }else{ + return BehaviorReturnCode.Success; + } + }),new BehaviorAction(delegate(){ + return BehaviorReturnCode.Success; + })); + + new VerificationPoint ().VerifyEquals ("1st running", true, foo.Behave (), BehaviorReturnCode.Running); + new VerificationPoint ().VerifyEquals ("2nd success", true, foo.Behave (), BehaviorReturnCode.Success); + new VerificationPoint ().VerifyEquals ("3rd success", true, foo.Behave (), BehaviorReturnCode.Success); + + log.logMessage ("restting first"); + first = true; + + new VerificationPoint ().VerifyEquals ("after reset running", true, foo.Behave (), BehaviorReturnCode.Running); + new VerificationPoint ().VerifyEquals ("final success", true, foo.Behave (), BehaviorReturnCode.Success); + + log.exitScope (); + } + + [Test] + public void testStatefulSel(){ + log.enterScope("testStatefulSel"); + + bool first = true; + + var foo = new StatefulSelector (new BehaviorAction (delegate(){ + return BehaviorReturnCode.Failure; + }), new BehaviorAction (delegate() { + if(first){ + first = false; + return BehaviorReturnCode.Running; + }else{ + return BehaviorReturnCode.Failure; + } + }), new BehaviorAction (delegate(){ + return BehaviorReturnCode.Success; + })); + + new VerificationPoint ().VerifyEquals ("1st running", true, foo.Behave (), BehaviorReturnCode.Running); + new VerificationPoint ().VerifyEquals ("2nd success", true, foo.Behave (), BehaviorReturnCode.Success); + new VerificationPoint ().VerifyEquals ("3rd success", true, foo.Behave (), BehaviorReturnCode.Success); + + log.logMessage ("restting first"); + first = true; + + new VerificationPoint ().VerifyEquals ("after reset running", true, foo.Behave (), BehaviorReturnCode.Running); + new VerificationPoint ().VerifyEquals ("final success", true, foo.Behave (), BehaviorReturnCode.Success); + + log.exitScope (); + } + } +} + diff --git a/Tests/Tests.csproj b/Tests/Tests.csproj new file mode 100644 index 0000000..48a0ff3 --- /dev/null +++ b/Tests/Tests.csproj @@ -0,0 +1,52 @@ + + + + Debug + AnyCPU + 10.0.0 + 2.0 + {A7D9F051-A51E-42FF-AD2B-D94DC8A06650} + Exe + Tests + Tests + + + true + full + false + bin\Debug + DEBUG; + prompt + 4 + true + + + full + true + bin\Release + prompt + 4 + true + + + + + ..\..\CSTester\CSLogging\bin\Release\CSLogging.dll + + + ..\..\CSTester\CSTester\bin\Release\CSTester.dll + + + + + + + + + + + {CC824B6F-6145-485F-9604-FB94F0ECACA7} + BehaviorLibrary + + + \ No newline at end of file From ffc9953011fd5eb6d630585c2e6e19d5f3417ad6 Mon Sep 17 00:00:00 2001 From: NetGnome Date: Fri, 4 Oct 2013 07:12:04 -0400 Subject: [PATCH 02/27] removed userprefs, shouldnt have been added --- BehaviorLibrary.userprefs | 31 ------------------------------- 1 file changed, 31 deletions(-) delete mode 100644 BehaviorLibrary.userprefs diff --git a/BehaviorLibrary.userprefs b/BehaviorLibrary.userprefs deleted file mode 100644 index a70d3d6..0000000 --- a/BehaviorLibrary.userprefs +++ /dev/null @@ -1,31 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file From 17dfae52b3aa8df850a62eb7762037c2920f41a2 Mon Sep 17 00:00:00 2001 From: NetGnome Date: Fri, 4 Oct 2013 11:03:03 -0400 Subject: [PATCH 03/27] improved some test cases --- BehaviorLibrary.userprefs | 31 ------------------------------- Tests/TestCases.cs | 28 ++++++++++++++++++++++------ 2 files changed, 22 insertions(+), 37 deletions(-) delete mode 100644 BehaviorLibrary.userprefs diff --git a/BehaviorLibrary.userprefs b/BehaviorLibrary.userprefs deleted file mode 100644 index a70d3d6..0000000 --- a/BehaviorLibrary.userprefs +++ /dev/null @@ -1,31 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/Tests/TestCases.cs b/Tests/TestCases.cs index 5ed4908..2a2832d 100644 --- a/Tests/TestCases.cs +++ b/Tests/TestCases.cs @@ -60,7 +60,11 @@ public void testStatefulSeq(){ bool first = true; var foo = new StatefulSequence (new BehaviorAction(delegate(){ - return BehaviorReturnCode.Success; + if(first){ + return BehaviorReturnCode.Success; + }else{ + return BehaviorReturnCode.Failure; + } }),new BehaviorAction( delegate(){ if(first){ first = false; @@ -74,13 +78,14 @@ public void testStatefulSeq(){ new VerificationPoint ().VerifyEquals ("1st running", true, foo.Behave (), BehaviorReturnCode.Running); new VerificationPoint ().VerifyEquals ("2nd success", true, foo.Behave (), BehaviorReturnCode.Success); - new VerificationPoint ().VerifyEquals ("3rd success", true, foo.Behave (), BehaviorReturnCode.Success); + new VerificationPoint ().VerifyEquals ("3rd failure", true, foo.Behave (), BehaviorReturnCode.Failure); log.logMessage ("restting first"); first = true; new VerificationPoint ().VerifyEquals ("after reset running", true, foo.Behave (), BehaviorReturnCode.Running); new VerificationPoint ().VerifyEquals ("final success", true, foo.Behave (), BehaviorReturnCode.Success); + new VerificationPoint ().VerifyEquals ("final failure", true, foo.Behave (), BehaviorReturnCode.Failure); log.exitScope (); } @@ -90,7 +95,7 @@ public void testStatefulSel(){ log.enterScope("testStatefulSel"); bool first = true; - + bool second = true; var foo = new StatefulSelector (new BehaviorAction (delegate(){ return BehaviorReturnCode.Failure; }), new BehaviorAction (delegate() { @@ -101,18 +106,29 @@ public void testStatefulSel(){ return BehaviorReturnCode.Failure; } }), new BehaviorAction (delegate(){ - return BehaviorReturnCode.Success; + if(first){ + return BehaviorReturnCode.Success; + }else{ + if(second){ + second = false; + return BehaviorReturnCode.Success; + }else{ + return BehaviorReturnCode.Failure; + } + } })); new VerificationPoint ().VerifyEquals ("1st running", true, foo.Behave (), BehaviorReturnCode.Running); new VerificationPoint ().VerifyEquals ("2nd success", true, foo.Behave (), BehaviorReturnCode.Success); - new VerificationPoint ().VerifyEquals ("3rd success", true, foo.Behave (), BehaviorReturnCode.Success); + new VerificationPoint ().VerifyEquals ("3rd failure", true, foo.Behave (), BehaviorReturnCode.Failure); - log.logMessage ("restting first"); + log.logMessage ("restting flags"); first = true; + second = true; new VerificationPoint ().VerifyEquals ("after reset running", true, foo.Behave (), BehaviorReturnCode.Running); new VerificationPoint ().VerifyEquals ("final success", true, foo.Behave (), BehaviorReturnCode.Success); + new VerificationPoint ().VerifyEquals ("final failure", true, foo.Behave (), BehaviorReturnCode.Failure); log.exitScope (); } From dc57d0664ff9e565dc969e5fcd539b533ce1259f Mon Sep 17 00:00:00 2001 From: NetGnome Date: Wed, 30 Oct 2013 12:22:00 -0400 Subject: [PATCH 04/27] Sequence will now return running if any are running Sequence will now only return success if all are success updated private variables to start with underscore rather than initial class character to follow more standardized approach... --- BehaviorLibrary/Behavior.cs | 152 +++++++++--------- .../Components/Actions/BehaviorAction.cs | 102 ++++++------ .../Components/Composites/PartialSelector.cs | 24 +-- .../Components/Composites/PartialSequence.cs | 22 +-- .../Components/Composites/RandomSelector.cs | 128 +++++++-------- .../Components/Composites/RootSelector.cs | 120 +++++++------- .../Components/Composites/Selector.cs | 8 +- .../Components/Composites/Sequence.cs | 16 +- .../Components/Composites/StatefulSelector.cs | 14 +- .../Components/Composites/StatefulSequence.cs | 18 +-- .../Components/Conditionals/Conditional.cs | 112 ++++++------- .../Components/Decorators/Counter.cs | 118 +++++++------- .../Components/Decorators/Inverter.cs | 122 +++++++------- .../Components/Decorators/RandomDecorator.cs | 112 ++++++------- .../Components/Decorators/Timer.cs | 128 +++++++-------- Tests/Issue2.cs | 84 ++++++++++ Tests/Program.cs | 1 + Tests/TestCases.cs | 38 ++--- Tests/Tests.csproj | 1 + 19 files changed, 705 insertions(+), 615 deletions(-) create mode 100644 Tests/Issue2.cs diff --git a/BehaviorLibrary/Behavior.cs b/BehaviorLibrary/Behavior.cs index 8f0b24c..0752a9f 100644 --- a/BehaviorLibrary/Behavior.cs +++ b/BehaviorLibrary/Behavior.cs @@ -1,76 +1,76 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using BehaviorLibrary.Components; -using BehaviorLibrary.Components.Composites; - -namespace BehaviorLibrary -{ - public enum BehaviorReturnCode - { - Failure, - Success, - Running - } - - public delegate BehaviorReturnCode BehaviorReturn(); - - /// - /// - /// - public class Behavior - { - - private RootSelector b_Root; - - private BehaviorReturnCode b_ReturnCode; - - public BehaviorReturnCode ReturnCode - { - get { return b_ReturnCode; } - set { b_ReturnCode = value; } - } - - /// - /// - /// - /// - public Behavior(RootSelector root) - { - b_Root = root; - } - - /// - /// perform the behavior - /// - public BehaviorReturnCode Behave() - { - try - { - switch (b_Root.Behave()) - { - case BehaviorReturnCode.Failure: - ReturnCode = BehaviorReturnCode.Failure; - return ReturnCode; - case BehaviorReturnCode.Success: - ReturnCode = BehaviorReturnCode.Success; - return ReturnCode; - case BehaviorReturnCode.Running: - ReturnCode = BehaviorReturnCode.Running; - return ReturnCode; - default: - ReturnCode = BehaviorReturnCode.Running; - return ReturnCode; - } - } - catch (Exception e) - { -#if DEBUG - Console.Error.WriteLine(e.ToString()); -#endif - ReturnCode = BehaviorReturnCode.Failure; - return ReturnCode; - } - } - } -} +using System; +using System.Collections.Generic; +using System.Linq; +using BehaviorLibrary.Components; +using BehaviorLibrary.Components.Composites; + +namespace BehaviorLibrary +{ + public enum BehaviorReturnCode + { + Failure, + Success, + Running + } + + public delegate BehaviorReturnCode BehaviorReturn(); + + /// + /// + /// + public class Behavior + { + + private RootSelector _Root; + + private BehaviorReturnCode _ReturnCode; + + public BehaviorReturnCode ReturnCode + { + get { return _ReturnCode; } + set { _ReturnCode = value; } + } + + /// + /// + /// + /// + public Behavior(RootSelector root) + { + _Root = root; + } + + /// + /// perform the behavior + /// + public BehaviorReturnCode Behave() + { + try + { + switch (_Root.Behave()) + { + case BehaviorReturnCode.Failure: + ReturnCode = BehaviorReturnCode.Failure; + return ReturnCode; + case BehaviorReturnCode.Success: + ReturnCode = BehaviorReturnCode.Success; + return ReturnCode; + case BehaviorReturnCode.Running: + ReturnCode = BehaviorReturnCode.Running; + return ReturnCode; + default: + ReturnCode = BehaviorReturnCode.Running; + return ReturnCode; + } + } + catch (Exception e) + { +#if DEBUG + Console.Error.WriteLine(e.ToString()); +#endif + ReturnCode = BehaviorReturnCode.Failure; + return ReturnCode; + } + } + } +} diff --git a/BehaviorLibrary/Components/Actions/BehaviorAction.cs b/BehaviorLibrary/Components/Actions/BehaviorAction.cs index f899abe..89047bf 100644 --- a/BehaviorLibrary/Components/Actions/BehaviorAction.cs +++ b/BehaviorLibrary/Components/Actions/BehaviorAction.cs @@ -1,51 +1,51 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; - -namespace BehaviorLibrary.Components.Actions -{ - public class BehaviorAction : BehaviorComponent - { - - private Func ba_Action; - - public BehaviorAction() { } - - public BehaviorAction(Func action) - { - ba_Action = action; - } - - public override BehaviorReturnCode Behave() - { - try - { - switch (ba_Action.Invoke()) - { - case BehaviorReturnCode.Success: - ReturnCode = BehaviorReturnCode.Success; - return ReturnCode; - case BehaviorReturnCode.Failure: - ReturnCode = BehaviorReturnCode.Failure; - return ReturnCode; - case BehaviorReturnCode.Running: - ReturnCode = BehaviorReturnCode.Running; - return ReturnCode; - default: - ReturnCode = BehaviorReturnCode.Failure; - return ReturnCode; - } - } - catch (Exception e) - { -#if DEBUG - Console.Error.WriteLine(e.ToString()); -#endif - ReturnCode = BehaviorReturnCode.Failure; - return ReturnCode; - } - } - - } -} +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace BehaviorLibrary.Components.Actions +{ + public class BehaviorAction : BehaviorComponent + { + + private Func _Action; + + public BehaviorAction() { } + + public BehaviorAction(Func action) + { + _Action = action; + } + + public override BehaviorReturnCode Behave() + { + try + { + switch (_Action.Invoke()) + { + case BehaviorReturnCode.Success: + ReturnCode = BehaviorReturnCode.Success; + return ReturnCode; + case BehaviorReturnCode.Failure: + ReturnCode = BehaviorReturnCode.Failure; + return ReturnCode; + case BehaviorReturnCode.Running: + ReturnCode = BehaviorReturnCode.Running; + return ReturnCode; + default: + ReturnCode = BehaviorReturnCode.Failure; + return ReturnCode; + } + } + catch (Exception e) + { +#if DEBUG + Console.Error.WriteLine(e.ToString()); +#endif + ReturnCode = BehaviorReturnCode.Failure; + return ReturnCode; + } + } + + } +} diff --git a/BehaviorLibrary/Components/Composites/PartialSelector.cs b/BehaviorLibrary/Components/Composites/PartialSelector.cs index 3391c47..4c9d689 100644 --- a/BehaviorLibrary/Components/Composites/PartialSelector.cs +++ b/BehaviorLibrary/Components/Composites/PartialSelector.cs @@ -8,11 +8,11 @@ namespace BehaviorLibrary.Components.Composites public class PartialSelector : BehaviorComponent { - protected BehaviorComponent[] s_Behaviors; + protected BehaviorComponent[] _Behaviors; - private short selections = 0; + private short _selections = 0; - private short selLength = 0; + private short _selLength = 0; /// /// Selects among the given behavior components (one evaluation per Behave call) @@ -24,8 +24,8 @@ public class PartialSelector : BehaviorComponent /// one to many behavior components public PartialSelector(params BehaviorComponent[] behaviors) { - s_Behaviors = behaviors; - selLength = (short)s_Behaviors.Length; + _Behaviors = behaviors; + _selLength = (short)_Behaviors.Length; } /// @@ -34,25 +34,25 @@ public PartialSelector(params BehaviorComponent[] behaviors) /// the behaviors return code public override BehaviorReturnCode Behave() { - while (selections < selLength) + while (_selections < _selLength) { try { - switch (s_Behaviors[selections].Behave()) + switch (_Behaviors[_selections].Behave()) { case BehaviorReturnCode.Failure: - selections++; + _selections++; ReturnCode = BehaviorReturnCode.Running; return ReturnCode; case BehaviorReturnCode.Success: - selections = 0; + _selections = 0; ReturnCode = BehaviorReturnCode.Success; return ReturnCode; case BehaviorReturnCode.Running: ReturnCode = BehaviorReturnCode.Running; return ReturnCode; default: - selections++; + _selections++; ReturnCode = BehaviorReturnCode.Failure; return ReturnCode; } @@ -62,13 +62,13 @@ public override BehaviorReturnCode Behave() #if DEBUG Console.Error.WriteLine(e.ToString()); #endif - selections++; + _selections++; ReturnCode = BehaviorReturnCode.Failure; return ReturnCode; } } - selections = 0; + _selections = 0; ReturnCode = BehaviorReturnCode.Failure; return ReturnCode; } diff --git a/BehaviorLibrary/Components/Composites/PartialSequence.cs b/BehaviorLibrary/Components/Composites/PartialSequence.cs index 04b36d2..e689871 100644 --- a/BehaviorLibrary/Components/Composites/PartialSequence.cs +++ b/BehaviorLibrary/Components/Composites/PartialSequence.cs @@ -8,11 +8,11 @@ namespace BehaviorLibrary.Components.Composites public class PartialSequence : BehaviorComponent { - protected BehaviorComponent[] s_Behaviors; + protected BehaviorComponent[] _Behaviors; - private short sequence = 0; + private short _sequence = 0; - private short seqLength = 0; + private short _seqLength = 0; /// /// Performs the given behavior components sequentially (one evaluation per Behave call) @@ -24,8 +24,8 @@ public class PartialSequence : BehaviorComponent /// one to many behavior components public PartialSequence(params BehaviorComponent[] behaviors) { - s_Behaviors = behaviors; - seqLength = (short) s_Behaviors.Length; + _Behaviors = behaviors; + _seqLength = (short) _Behaviors.Length; } /// @@ -35,18 +35,18 @@ public PartialSequence(params BehaviorComponent[] behaviors) public override BehaviorReturnCode Behave() { //while you can go through them, do so - while (sequence < seqLength) + while (_sequence < _seqLength) { try { - switch (s_Behaviors[sequence].Behave()) + switch (_Behaviors[_sequence].Behave()) { case BehaviorReturnCode.Failure: - sequence = 0; + _sequence = 0; ReturnCode = BehaviorReturnCode.Failure; return ReturnCode; case BehaviorReturnCode.Success: - sequence++; + _sequence++; ReturnCode = BehaviorReturnCode.Running; return ReturnCode; case BehaviorReturnCode.Running: @@ -59,14 +59,14 @@ public override BehaviorReturnCode Behave() #if DEBUG Console.Error.WriteLine(e.ToString()); #endif - sequence = 0; + _sequence = 0; ReturnCode = BehaviorReturnCode.Failure; return ReturnCode; } } - sequence = 0; + _sequence = 0; ReturnCode = BehaviorReturnCode.Success; return ReturnCode; diff --git a/BehaviorLibrary/Components/Composites/RandomSelector.cs b/BehaviorLibrary/Components/Composites/RandomSelector.cs index e2ab30a..3e540f8 100644 --- a/BehaviorLibrary/Components/Composites/RandomSelector.cs +++ b/BehaviorLibrary/Components/Composites/RandomSelector.cs @@ -1,64 +1,64 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; - -namespace BehaviorLibrary.Components.Composites -{ - public class RandomSelector : BehaviorComponent - { - - private BehaviorComponent[] r_Behaviors; - - //use current milliseconds to set random seed - private Random r_Random = new Random(DateTime.Now.Millisecond); - - /// - /// Randomly selects and performs one of the passed behaviors - /// -Returns Success if selected behavior returns Success - /// -Returns Failure if selected behavior returns Failure - /// -Returns Running if selected behavior returns Running - /// - /// one to many behavior components - public RandomSelector(params BehaviorComponent[] behaviors) - { - r_Behaviors = behaviors; - } - - /// - /// performs the given behavior - /// - /// the behaviors return code - public override BehaviorReturnCode Behave() - { - r_Random = new Random(DateTime.Now.Millisecond); - - try - { - switch (r_Behaviors[r_Random.Next(0, r_Behaviors.Length - 1)].Behave()) - { - case BehaviorReturnCode.Failure: - ReturnCode = BehaviorReturnCode.Failure; - return ReturnCode; - case BehaviorReturnCode.Success: - ReturnCode = BehaviorReturnCode.Success; - return ReturnCode; - case BehaviorReturnCode.Running: - ReturnCode = BehaviorReturnCode.Running; - return ReturnCode; - default: - ReturnCode = BehaviorReturnCode.Failure; - return ReturnCode; - } - } - catch (Exception e) - { -#if DEBUG - Console.Error.WriteLine(e.ToString()); -#endif - ReturnCode = BehaviorReturnCode.Failure; - return ReturnCode; - } - } - } -} +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace BehaviorLibrary.Components.Composites +{ + public class RandomSelector : BehaviorComponent + { + + private BehaviorComponent[] _Behaviors; + + //use current milliseconds to set random seed + private Random _Random = new Random(DateTime.Now.Millisecond); + + /// + /// Randomly selects and performs one of the passed behaviors + /// -Returns Success if selected behavior returns Success + /// -Returns Failure if selected behavior returns Failure + /// -Returns Running if selected behavior returns Running + /// + /// one to many behavior components + public RandomSelector(params BehaviorComponent[] behaviors) + { + _Behaviors = behaviors; + } + + /// + /// performs the given behavior + /// + /// the behaviors return code + public override BehaviorReturnCode Behave() + { + _Random = new Random(DateTime.Now.Millisecond); + + try + { + switch (_Behaviors[_Random.Next(0, _Behaviors.Length - 1)].Behave()) + { + case BehaviorReturnCode.Failure: + ReturnCode = BehaviorReturnCode.Failure; + return ReturnCode; + case BehaviorReturnCode.Success: + ReturnCode = BehaviorReturnCode.Success; + return ReturnCode; + case BehaviorReturnCode.Running: + ReturnCode = BehaviorReturnCode.Running; + return ReturnCode; + default: + ReturnCode = BehaviorReturnCode.Failure; + return ReturnCode; + } + } + catch (Exception e) + { +#if DEBUG + Console.Error.WriteLine(e.ToString()); +#endif + ReturnCode = BehaviorReturnCode.Failure; + return ReturnCode; + } + } + } +} diff --git a/BehaviorLibrary/Components/Composites/RootSelector.cs b/BehaviorLibrary/Components/Composites/RootSelector.cs index 53c1156..a6a36de 100644 --- a/BehaviorLibrary/Components/Composites/RootSelector.cs +++ b/BehaviorLibrary/Components/Composites/RootSelector.cs @@ -1,60 +1,60 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; - -namespace BehaviorLibrary.Components.Composites -{ - public class RootSelector : PartialSelector - { - - private BehaviorComponent[] rs_Behaviors; - - private Func rs_Index; - - /// - /// The selector for the root node of the behavior tree - /// - /// an index representing which of the behavior branches to perform - /// the behavior branches to be selected from - public RootSelector(Func index, params BehaviorComponent[] behaviors) - { - rs_Index = index; - rs_Behaviors = behaviors; - } - - /// - /// performs the given behavior - /// - /// the behaviors return code - public override BehaviorReturnCode Behave() - { - try - { - switch (rs_Behaviors[rs_Index.Invoke()].Behave()) - { - case BehaviorReturnCode.Failure: - ReturnCode = BehaviorReturnCode.Failure; - return ReturnCode; - case BehaviorReturnCode.Success: - ReturnCode = BehaviorReturnCode.Success; - return ReturnCode; - case BehaviorReturnCode.Running: - ReturnCode = BehaviorReturnCode.Running; - return ReturnCode; - default: - ReturnCode = BehaviorReturnCode.Running; - return ReturnCode; - } - } - catch (Exception e) - { -#if DEBUG - Console.Error.WriteLine(e.ToString()); -#endif - ReturnCode = BehaviorReturnCode.Failure; - return ReturnCode; - } - } - } -} +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace BehaviorLibrary.Components.Composites +{ + public class RootSelector : PartialSelector + { + + private BehaviorComponent[] _Behaviors; + + private Func _Index; + + /// + /// The selector for the root node of the behavior tree + /// + /// an index representing which of the behavior branches to perform + /// the behavior branches to be selected from + public RootSelector(Func index, params BehaviorComponent[] behaviors) + { + _Index = index; + _Behaviors = behaviors; + } + + /// + /// performs the given behavior + /// + /// the behaviors return code + public override BehaviorReturnCode Behave() + { + try + { + switch (_Behaviors[_Index.Invoke()].Behave()) + { + case BehaviorReturnCode.Failure: + ReturnCode = BehaviorReturnCode.Failure; + return ReturnCode; + case BehaviorReturnCode.Success: + ReturnCode = BehaviorReturnCode.Success; + return ReturnCode; + case BehaviorReturnCode.Running: + ReturnCode = BehaviorReturnCode.Running; + return ReturnCode; + default: + ReturnCode = BehaviorReturnCode.Running; + return ReturnCode; + } + } + catch (Exception e) + { +#if DEBUG + Console.Error.WriteLine(e.ToString()); +#endif + ReturnCode = BehaviorReturnCode.Failure; + return ReturnCode; + } + } + } +} diff --git a/BehaviorLibrary/Components/Composites/Selector.cs b/BehaviorLibrary/Components/Composites/Selector.cs index 200a507..bef14fa 100644 --- a/BehaviorLibrary/Components/Composites/Selector.cs +++ b/BehaviorLibrary/Components/Composites/Selector.cs @@ -8,7 +8,7 @@ namespace BehaviorLibrary.Components.Composites public class Selector : BehaviorComponent { - protected BehaviorComponent[] s_Behaviors; + protected BehaviorComponent[] _Behaviors; /// @@ -21,7 +21,7 @@ public class Selector : BehaviorComponent /// one to many behavior components public Selector(params BehaviorComponent[] behaviors) { - s_Behaviors = behaviors; + _Behaviors = behaviors; } /// @@ -31,11 +31,11 @@ public Selector(params BehaviorComponent[] behaviors) public override BehaviorReturnCode Behave() { - for (int i = 0; i < s_Behaviors.Length; i++) + for (int i = 0; i < _Behaviors.Length; i++) { try { - switch (s_Behaviors[i].Behave()) + switch (_Behaviors[i].Behave()) { case BehaviorReturnCode.Failure: continue; diff --git a/BehaviorLibrary/Components/Composites/Sequence.cs b/BehaviorLibrary/Components/Composites/Sequence.cs index 3e2a96d..f027332 100644 --- a/BehaviorLibrary/Components/Composites/Sequence.cs +++ b/BehaviorLibrary/Components/Composites/Sequence.cs @@ -8,18 +8,18 @@ namespace BehaviorLibrary.Components.Composites public class Sequence : BehaviorComponent { - private BehaviorComponent[] s_Behaviors; + private BehaviorComponent[] _behaviors; /// /// attempts to run the behaviors all in one cycle /// -Returns Success when all are successful /// -Returns Failure if one behavior fails or an error occurs - /// -Does not Return Running + /// -Returns Running if any are running /// /// public Sequence(params BehaviorComponent[] behaviors) { - s_Behaviors = behaviors; + _behaviors = behaviors; } /// @@ -28,12 +28,14 @@ public Sequence(params BehaviorComponent[] behaviors) /// the behaviors return code public override BehaviorReturnCode Behave() { + //add watch for any running behaviors + bool anyRunning = false; - for(int i = 0; i < s_Behaviors.Length;i++) + for(int i = 0; i < _behaviors.Length;i++) { try { - switch (s_Behaviors[i].Behave()) + switch (_behaviors[i].Behave()) { case BehaviorReturnCode.Failure: ReturnCode = BehaviorReturnCode.Failure; @@ -41,6 +43,7 @@ public override BehaviorReturnCode Behave() case BehaviorReturnCode.Success: continue; case BehaviorReturnCode.Running: + anyRunning = true; continue; default: ReturnCode = BehaviorReturnCode.Success; @@ -57,7 +60,8 @@ public override BehaviorReturnCode Behave() } } - ReturnCode = BehaviorReturnCode.Success; + //if none running, return success, otherwise return running + ReturnCode = !anyRunning ? BehaviorReturnCode.Success : BehaviorReturnCode.Running; return ReturnCode; } diff --git a/BehaviorLibrary/Components/Composites/StatefulSelector.cs b/BehaviorLibrary/Components/Composites/StatefulSelector.cs index e66bf69..e0f07b8 100644 --- a/BehaviorLibrary/Components/Composites/StatefulSelector.cs +++ b/BehaviorLibrary/Components/Composites/StatefulSelector.cs @@ -5,9 +5,9 @@ namespace BehaviorLibrary { public class StatefulSelector : BehaviorComponent { - private BehaviorComponent[] s_Behaviors; + private BehaviorComponent[] _Behaviors; - private int s_LastBehavior = 0; + private int _LastBehavior = 0; /// /// Selects among the given behavior components (stateful on running) @@ -18,7 +18,7 @@ public class StatefulSelector : BehaviorComponent /// /// one to many behavior components public StatefulSelector(params BehaviorComponent[] behaviors){ - this.s_Behaviors = behaviors; + this._Behaviors = behaviors; } /// @@ -27,13 +27,13 @@ public StatefulSelector(params BehaviorComponent[] behaviors){ /// the behaviors return code public override BehaviorReturnCode Behave(){ - for(; s_LastBehavior < s_Behaviors.Length; s_LastBehavior++){ + for(; _LastBehavior < _Behaviors.Length; _LastBehavior++){ try{ - switch (s_Behaviors[s_LastBehavior].Behave()){ + switch (_Behaviors[_LastBehavior].Behave()){ case BehaviorReturnCode.Failure: continue; case BehaviorReturnCode.Success: - s_LastBehavior = 0; + _LastBehavior = 0; ReturnCode = BehaviorReturnCode.Success; return ReturnCode; case BehaviorReturnCode.Running: @@ -51,7 +51,7 @@ public override BehaviorReturnCode Behave(){ } } - s_LastBehavior = 0; + _LastBehavior = 0; ReturnCode = BehaviorReturnCode.Failure; return ReturnCode; } diff --git a/BehaviorLibrary/Components/Composites/StatefulSequence.cs b/BehaviorLibrary/Components/Composites/StatefulSequence.cs index 5c3ddc7..1c359a3 100644 --- a/BehaviorLibrary/Components/Composites/StatefulSequence.cs +++ b/BehaviorLibrary/Components/Composites/StatefulSequence.cs @@ -5,9 +5,9 @@ namespace BehaviorLibrary { public class StatefulSequence : BehaviorComponent { - private BehaviorComponent[] s_Behaviors; + private BehaviorComponent[] _Behaviors; - private int s_LastBehavior = 0; + private int _LastBehavior = 0; /// /// attempts to run the behaviors all in one cycle (stateful on running) @@ -17,7 +17,7 @@ public class StatefulSequence : BehaviorComponent /// /// public StatefulSequence (params BehaviorComponent[] behaviors){ - this.s_Behaviors = behaviors; + this._Behaviors = behaviors; } /// @@ -27,11 +27,11 @@ public StatefulSequence (params BehaviorComponent[] behaviors){ public override BehaviorReturnCode Behave(){ //start from last remembered position - for(; s_LastBehavior < s_Behaviors.Length;s_LastBehavior++){ + for(; _LastBehavior < _Behaviors.Length;_LastBehavior++){ try{ - switch (s_Behaviors[s_LastBehavior].Behave()){ + switch (_Behaviors[_LastBehavior].Behave()){ case BehaviorReturnCode.Failure: - s_LastBehavior = 0; + _LastBehavior = 0; ReturnCode = BehaviorReturnCode.Failure; return ReturnCode; case BehaviorReturnCode.Success: @@ -40,7 +40,7 @@ public override BehaviorReturnCode Behave(){ ReturnCode = BehaviorReturnCode.Running; return ReturnCode; default: - s_LastBehavior = 0; + _LastBehavior = 0; ReturnCode = BehaviorReturnCode.Success; return ReturnCode; } @@ -49,13 +49,13 @@ public override BehaviorReturnCode Behave(){ #if DEBUG Console.Error.WriteLine(e.ToString()); #endif - s_LastBehavior = 0; + _LastBehavior = 0; ReturnCode = BehaviorReturnCode.Failure; return ReturnCode; } } - s_LastBehavior = 0; + _LastBehavior = 0; ReturnCode = BehaviorReturnCode.Success; return ReturnCode; } diff --git a/BehaviorLibrary/Components/Conditionals/Conditional.cs b/BehaviorLibrary/Components/Conditionals/Conditional.cs index db79631..2db9682 100644 --- a/BehaviorLibrary/Components/Conditionals/Conditional.cs +++ b/BehaviorLibrary/Components/Conditionals/Conditional.cs @@ -1,56 +1,56 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; - -namespace BehaviorLibrary.Components.Conditionals -{ - public class Conditional : BehaviorComponent - { - - private Func c_Bool; - - /// - /// Returns a return code equivalent to the test - /// -Returns Success if true - /// -Returns Failure if false - /// - /// the value to be tested - public Conditional(Func test) - { - c_Bool = test; - } - - /// - /// performs the given behavior - /// - /// the behaviors return code - public override BehaviorReturnCode Behave() - { - - try - { - switch (c_Bool.Invoke()) - { - case true: - ReturnCode = BehaviorReturnCode.Success; - return ReturnCode; - case false: - ReturnCode = BehaviorReturnCode.Failure; - return ReturnCode; - default: - ReturnCode = BehaviorReturnCode.Failure; - return ReturnCode; - } - } - catch (Exception e) - { -#if DEBUG - Console.Error.WriteLine(e.ToString()); -#endif - ReturnCode = BehaviorReturnCode.Failure; - return ReturnCode; - } - } - } -} +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace BehaviorLibrary.Components.Conditionals +{ + public class Conditional : BehaviorComponent + { + + private Func _Bool; + + /// + /// Returns a return code equivalent to the test + /// -Returns Success if true + /// -Returns Failure if false + /// + /// the value to be tested + public Conditional(Func test) + { + _Bool = test; + } + + /// + /// performs the given behavior + /// + /// the behaviors return code + public override BehaviorReturnCode Behave() + { + + try + { + switch (_Bool.Invoke()) + { + case true: + ReturnCode = BehaviorReturnCode.Success; + return ReturnCode; + case false: + ReturnCode = BehaviorReturnCode.Failure; + return ReturnCode; + default: + ReturnCode = BehaviorReturnCode.Failure; + return ReturnCode; + } + } + catch (Exception e) + { +#if DEBUG + Console.Error.WriteLine(e.ToString()); +#endif + ReturnCode = BehaviorReturnCode.Failure; + return ReturnCode; + } + } + } +} diff --git a/BehaviorLibrary/Components/Decorators/Counter.cs b/BehaviorLibrary/Components/Decorators/Counter.cs index a51f377..67bb08f 100644 --- a/BehaviorLibrary/Components/Decorators/Counter.cs +++ b/BehaviorLibrary/Components/Decorators/Counter.cs @@ -1,59 +1,59 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; - -namespace BehaviorLibrary.Components.Decorators -{ - public class Counter : BehaviorComponent - { - private int c_MaxCount; - private int c_Counter = 0; - - private BehaviorComponent c_Behavior; - - /// - /// executes the behavior based on a counter - /// -each time Counter is called the counter increments by 1 - /// -Counter executes the behavior when it reaches the supplied maxCount - /// - /// max number to count to - /// behavior to run - public Counter(int maxCount, BehaviorComponent behavior) - { - c_MaxCount = maxCount; - c_Behavior = behavior; - } - - /// - /// performs the given behavior - /// - /// the behaviors return code - public override BehaviorReturnCode Behave() - { - try - { - if (c_Counter < c_MaxCount) - { - c_Counter++; - ReturnCode = BehaviorReturnCode.Running; - return BehaviorReturnCode.Running; - } - else - { - c_Counter = 0; - ReturnCode = c_Behavior.Behave(); - return ReturnCode; - } - } - catch (Exception e) - { -#if DEBUG - Console.Error.WriteLine(e.ToString()); -#endif - ReturnCode = BehaviorReturnCode.Failure; - return BehaviorReturnCode.Failure; - } - } - } -} +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace BehaviorLibrary.Components.Decorators +{ + public class Counter : BehaviorComponent + { + private int _MaxCount; + private int _Counter = 0; + + private BehaviorComponent _Behavior; + + /// + /// executes the behavior based on a counter + /// -each time Counter is called the counter increments by 1 + /// -Counter executes the behavior when it reaches the supplied maxCount + /// + /// max number to count to + /// behavior to run + public Counter(int maxCount, BehaviorComponent behavior) + { + _MaxCount = maxCount; + _Behavior = behavior; + } + + /// + /// performs the given behavior + /// + /// the behaviors return code + public override BehaviorReturnCode Behave() + { + try + { + if (_Counter < _MaxCount) + { + _Counter++; + ReturnCode = BehaviorReturnCode.Running; + return BehaviorReturnCode.Running; + } + else + { + _Counter = 0; + ReturnCode = _Behavior.Behave(); + return ReturnCode; + } + } + catch (Exception e) + { +#if DEBUG + Console.Error.WriteLine(e.ToString()); +#endif + ReturnCode = BehaviorReturnCode.Failure; + return BehaviorReturnCode.Failure; + } + } + } +} diff --git a/BehaviorLibrary/Components/Decorators/Inverter.cs b/BehaviorLibrary/Components/Decorators/Inverter.cs index 1a75f74..5f6675f 100644 --- a/BehaviorLibrary/Components/Decorators/Inverter.cs +++ b/BehaviorLibrary/Components/Decorators/Inverter.cs @@ -1,61 +1,61 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; - -namespace BehaviorLibrary.Components.Decorators -{ - public class Inverter : BehaviorComponent - { - - private BehaviorComponent d_Behavior; - - /// - /// inverts the given behavior - /// -Returns Success on Failure or Error - /// -Returns Failure on Success - /// -Returns Running on Running - /// - /// - public Inverter(BehaviorComponent behavior) - { - d_Behavior = behavior; - } - - /// - /// performs the given behavior - /// - /// the behaviors return code - public override BehaviorReturnCode Behave() - { - try - { - switch (d_Behavior.Behave()) - { - case BehaviorReturnCode.Failure: - ReturnCode = BehaviorReturnCode.Success; - return ReturnCode; - case BehaviorReturnCode.Success: - ReturnCode = BehaviorReturnCode.Failure; - return ReturnCode; - case BehaviorReturnCode.Running: - ReturnCode = BehaviorReturnCode.Running; - return ReturnCode; - } - } - catch (Exception e) - { -#if DEBUG - Console.Error.WriteLine(e.ToString()); -#endif - ReturnCode = BehaviorReturnCode.Success; - return ReturnCode; - } - - ReturnCode = BehaviorReturnCode.Success; - return ReturnCode; - - } - - } -} +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace BehaviorLibrary.Components.Decorators +{ + public class Inverter : BehaviorComponent + { + + private BehaviorComponent _Behavior; + + /// + /// inverts the given behavior + /// -Returns Success on Failure or Error + /// -Returns Failure on Success + /// -Returns Running on Running + /// + /// + public Inverter(BehaviorComponent behavior) + { + _Behavior = behavior; + } + + /// + /// performs the given behavior + /// + /// the behaviors return code + public override BehaviorReturnCode Behave() + { + try + { + switch (_Behavior.Behave()) + { + case BehaviorReturnCode.Failure: + ReturnCode = BehaviorReturnCode.Success; + return ReturnCode; + case BehaviorReturnCode.Success: + ReturnCode = BehaviorReturnCode.Failure; + return ReturnCode; + case BehaviorReturnCode.Running: + ReturnCode = BehaviorReturnCode.Running; + return ReturnCode; + } + } + catch (Exception e) + { +#if DEBUG + Console.Error.WriteLine(e.ToString()); +#endif + ReturnCode = BehaviorReturnCode.Success; + return ReturnCode; + } + + ReturnCode = BehaviorReturnCode.Success; + return ReturnCode; + + } + + } +} diff --git a/BehaviorLibrary/Components/Decorators/RandomDecorator.cs b/BehaviorLibrary/Components/Decorators/RandomDecorator.cs index 4273243..f834b0e 100644 --- a/BehaviorLibrary/Components/Decorators/RandomDecorator.cs +++ b/BehaviorLibrary/Components/Decorators/RandomDecorator.cs @@ -1,56 +1,56 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; - -namespace BehaviorLibrary.Components.Decorators -{ - public class RandomDecorator : BehaviorComponent - { - - private float r_Probability; - - private Func r_RandomFunction; - - private BehaviorComponent r_Behavior; - - /// - /// randomly executes the behavior - /// - /// probability of execution - /// function that determines probability to execute - /// behavior to execute - public RandomDecorator(float probability, Func randomFunction, BehaviorComponent behavior) - { - r_Probability = probability; - r_RandomFunction = randomFunction; - r_Behavior = behavior; - } - - - public override BehaviorReturnCode Behave() - { - try - { - if (r_RandomFunction.Invoke() <= r_Probability) - { - ReturnCode = r_Behavior.Behave(); - return ReturnCode; - } - else - { - ReturnCode = BehaviorReturnCode.Running; - return BehaviorReturnCode.Running; - } - } - catch (Exception e) - { -#if DEBUG - Console.Error.WriteLine(e.ToString()); -#endif - ReturnCode = BehaviorReturnCode.Failure; - return BehaviorReturnCode.Failure; - } - } - } -} +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace BehaviorLibrary.Components.Decorators +{ + public class RandomDecorator : BehaviorComponent + { + + private float _Probability; + + private Func _RandomFunction; + + private BehaviorComponent _Behavior; + + /// + /// randomly executes the behavior + /// + /// probability of execution + /// function that determines probability to execute + /// behavior to execute + public RandomDecorator(float probability, Func randomFunction, BehaviorComponent behavior) + { + _Probability = probability; + _RandomFunction = randomFunction; + _Behavior = behavior; + } + + + public override BehaviorReturnCode Behave() + { + try + { + if (_RandomFunction.Invoke() <= _Probability) + { + ReturnCode = _Behavior.Behave(); + return ReturnCode; + } + else + { + ReturnCode = BehaviorReturnCode.Running; + return BehaviorReturnCode.Running; + } + } + catch (Exception e) + { +#if DEBUG + Console.Error.WriteLine(e.ToString()); +#endif + ReturnCode = BehaviorReturnCode.Failure; + return BehaviorReturnCode.Failure; + } + } + } +} diff --git a/BehaviorLibrary/Components/Decorators/Timer.cs b/BehaviorLibrary/Components/Decorators/Timer.cs index 7004dc8..7f2a093 100644 --- a/BehaviorLibrary/Components/Decorators/Timer.cs +++ b/BehaviorLibrary/Components/Decorators/Timer.cs @@ -1,64 +1,64 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; - -namespace BehaviorLibrary.Components.Decorators -{ - public class Timer : BehaviorComponent - { - - private Func t_ElapsedTimeFunction; - - private BehaviorComponent t_Behavior; - - private int t_TimeElapsed = 0; - - private int t_WaitTime; - - /// - /// executes the behavior after a given amount of time in miliseconds has passed - /// - /// function that returns elapsed time - /// maximum time to wait before executing behavior - /// behavior to run - public Timer(Func elapsedTimeFunction, int timeToWait, BehaviorComponent behavior) - { - t_ElapsedTimeFunction = elapsedTimeFunction; - t_Behavior = behavior; - t_WaitTime = timeToWait; - } - - /// - /// performs the given behavior - /// - /// the behaviors return code - public override BehaviorReturnCode Behave() - { - try - { - t_TimeElapsed += t_ElapsedTimeFunction.Invoke(); - - if (t_TimeElapsed >= t_WaitTime) - { - t_TimeElapsed = 0; - ReturnCode = t_Behavior.Behave(); - return ReturnCode; - } - else - { - ReturnCode = BehaviorReturnCode.Running; - return BehaviorReturnCode.Running; - } - } - catch (Exception e) - { -#if DEBUG - Console.Error.WriteLine(e.ToString()); -#endif - ReturnCode = BehaviorReturnCode.Failure; - return BehaviorReturnCode.Failure; - } - } - } -} +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace BehaviorLibrary.Components.Decorators +{ + public class Timer : BehaviorComponent + { + + private Func _ElapsedTimeFunction; + + private BehaviorComponent _Behavior; + + private int _TimeElapsed = 0; + + private int _WaitTime; + + /// + /// executes the behavior after a given amount of time in miliseconds has passed + /// + /// function that returns elapsed time + /// maximum time to wait before executing behavior + /// behavior to run + public Timer(Func elapsedTimeFunction, int timeToWait, BehaviorComponent behavior) + { + _ElapsedTimeFunction = elapsedTimeFunction; + _Behavior = behavior; + _WaitTime = timeToWait; + } + + /// + /// performs the given behavior + /// + /// the behaviors return code + public override BehaviorReturnCode Behave() + { + try + { + _TimeElapsed += _ElapsedTimeFunction.Invoke(); + + if (_TimeElapsed >= _WaitTime) + { + _TimeElapsed = 0; + ReturnCode = _Behavior.Behave(); + return ReturnCode; + } + else + { + ReturnCode = BehaviorReturnCode.Running; + return BehaviorReturnCode.Running; + } + } + catch (Exception e) + { +#if DEBUG + Console.Error.WriteLine(e.ToString()); +#endif + ReturnCode = BehaviorReturnCode.Failure; + return BehaviorReturnCode.Failure; + } + } + } +} diff --git a/Tests/Issue2.cs b/Tests/Issue2.cs new file mode 100644 index 0000000..77aef76 --- /dev/null +++ b/Tests/Issue2.cs @@ -0,0 +1,84 @@ +using System; +using CSTester; +using CSLogging; +using BehaviorLibrary; +using BehaviorLibrary.Components; +using BehaviorLibrary.Components.Composites; +using BehaviorLibrary.Components.Actions; + +namespace Tests +{ + public class Issue2 + { + public Issue2 () + { + } + + CSLogger _log = CSLogger.Instance; + + [BuildUp] + public void buildup(){ + _log.setEnableLogging (true); + _log.setEnableDebug (true); + _log.setEnableError (true); + _log.setEnableMessage (true); + _log.loadLog ("./", "issue2.log"); + _log.enterScope ("buildup"); + _log.logMessage ("---------- BEGIN TESTING ISSUE 2 ----------"); + _log.exitScope (); + } + + [TearDown] + public void teardown(){ + _log.enterScope ("teardown"); + _log.logMessage ("---------- END TESTING ISSUE 2 ----------"); + _log.exitScope (); + _log.closeLog (); + } + + [Test] + public void test1(){ + _log.enterScope ("test1"); + + var foo = new Sequence (new BehaviorAction (delegate() { + return BehaviorReturnCode.Running; + }), new BehaviorAction (delegate() { + return BehaviorReturnCode.Running; + }), new BehaviorAction (delegate() { + return BehaviorReturnCode.Running; + }), new BehaviorAction (delegate() { + return BehaviorReturnCode.Running; + })); + + new VerificationPoint ().VerifyEquals ("all running is running", true, foo.Behave(), BehaviorReturnCode.Running); + + foo = new Sequence (new BehaviorAction (delegate() { + return BehaviorReturnCode.Running; + }), new BehaviorAction (delegate() { + return BehaviorReturnCode.Running; + }), new BehaviorAction (delegate() { + return BehaviorReturnCode.Running; + }), new BehaviorAction (delegate() { + return BehaviorReturnCode.Success; + })); + + new VerificationPoint ().VerifyEquals ("all but one running is running", true, foo.Behave(), BehaviorReturnCode.Running); + + foo = new Sequence (new BehaviorAction (delegate() { + return BehaviorReturnCode.Success; + }), new BehaviorAction (delegate() { + return BehaviorReturnCode.Success; + }), new BehaviorAction (delegate() { + return BehaviorReturnCode.Success; + }), new BehaviorAction (delegate() { + return BehaviorReturnCode.Success; + })); + + new VerificationPoint ().VerifyEquals ("all success is success", true, foo.Behave(), BehaviorReturnCode.Success); + + + _log.exitScope (); + } + } +} + diff --git a/Tests/Program.cs b/Tests/Program.cs index 0ae1742..a1a8c47 100644 --- a/Tests/Program.cs +++ b/Tests/Program.cs @@ -32,6 +32,7 @@ public static void Main (string[] args) { Tester tester = new Tester(); tester.registerTestCase(new TestCases()); + tester.registerTestCase(new Issue2()); tester.initialize(); Console.Write(tester.getResults()); } diff --git a/Tests/TestCases.cs b/Tests/TestCases.cs index 2a2832d..f7e4cf4 100644 --- a/Tests/TestCases.cs +++ b/Tests/TestCases.cs @@ -31,31 +31,31 @@ public class TestCases { public TestCases (){} - private CSLogger log = CSLogger.Instance; + private CSLogger _log = CSLogger.Instance; [BuildUp] public void buildup(){ - log.setEnableLogging (true); - log.setEnableDebug (true); - log.setEnableError (true); - log.setEnableMessage (true); - log.loadLog("","behaviorLibrary.log"); - log.enterScope ("TestCases"); - log.logMessage ("----------------- STARTING BEHAVIOR LIBRARY TESTS -----------------"); + _log.setEnableLogging (true); + _log.setEnableDebug (true); + _log.setEnableError (true); + _log.setEnableMessage (true); + _log.loadLog("","behaviorLibrary.log"); + _log.enterScope ("TestCases"); + _log.logMessage ("----------------- STARTING BEHAVIOR LIBRARY TESTS -----------------"); } [TearDown] public void teardown(){ - log.enterScope("teardown"); - log.exitScope (); - log.logMessage ("----------------- ENDING BEHAVIOR LIBRARY TESTS -----------------"); - log.exitScope (); - log.closeLog (); + _log.enterScope("teardown"); + _log.exitScope (); + _log.logMessage ("----------------- ENDING BEHAVIOR LIBRARY TESTS -----------------"); + _log.exitScope (); + _log.closeLog (); } [Test] public void testStatefulSeq(){ - log.enterScope("testStatefulSeq"); + _log.enterScope("testStatefulSeq"); bool first = true; @@ -80,19 +80,19 @@ public void testStatefulSeq(){ new VerificationPoint ().VerifyEquals ("2nd success", true, foo.Behave (), BehaviorReturnCode.Success); new VerificationPoint ().VerifyEquals ("3rd failure", true, foo.Behave (), BehaviorReturnCode.Failure); - log.logMessage ("restting first"); + _log.logMessage ("restting first"); first = true; new VerificationPoint ().VerifyEquals ("after reset running", true, foo.Behave (), BehaviorReturnCode.Running); new VerificationPoint ().VerifyEquals ("final success", true, foo.Behave (), BehaviorReturnCode.Success); new VerificationPoint ().VerifyEquals ("final failure", true, foo.Behave (), BehaviorReturnCode.Failure); - log.exitScope (); + _log.exitScope (); } [Test] public void testStatefulSel(){ - log.enterScope("testStatefulSel"); + _log.enterScope("testStatefulSel"); bool first = true; bool second = true; @@ -122,7 +122,7 @@ public void testStatefulSel(){ new VerificationPoint ().VerifyEquals ("2nd success", true, foo.Behave (), BehaviorReturnCode.Success); new VerificationPoint ().VerifyEquals ("3rd failure", true, foo.Behave (), BehaviorReturnCode.Failure); - log.logMessage ("restting flags"); + _log.logMessage ("restting flags"); first = true; second = true; @@ -130,7 +130,7 @@ public void testStatefulSel(){ new VerificationPoint ().VerifyEquals ("final success", true, foo.Behave (), BehaviorReturnCode.Success); new VerificationPoint ().VerifyEquals ("final failure", true, foo.Behave (), BehaviorReturnCode.Failure); - log.exitScope (); + _log.exitScope (); } } } diff --git a/Tests/Tests.csproj b/Tests/Tests.csproj index 48a0ff3..c961055 100644 --- a/Tests/Tests.csproj +++ b/Tests/Tests.csproj @@ -41,6 +41,7 @@ + From 1bc75432e71310817f24d32badd8e2ed21ad1ea1 Mon Sep 17 00:00:00 2001 From: NetGnome Date: Thu, 31 Oct 2013 07:50:12 -0400 Subject: [PATCH 05/27] updated tests for CSTester updates --- Tests/Issue2.cs | 1 + Tests/Program.cs | 5 ++--- Tests/TestCases.cs | 1 + 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/Tests/Issue2.cs b/Tests/Issue2.cs index 77aef76..3415cd1 100644 --- a/Tests/Issue2.cs +++ b/Tests/Issue2.cs @@ -8,6 +8,7 @@ namespace Tests { + [TestCase] public class Issue2 { public Issue2 () diff --git a/Tests/Program.cs b/Tests/Program.cs index a1a8c47..cb82c06 100644 --- a/Tests/Program.cs +++ b/Tests/Program.cs @@ -31,9 +31,8 @@ public static void Main (string[] args) try { Tester tester = new Tester(); - tester.registerTestCase(new TestCases()); - tester.registerTestCase(new Issue2()); - tester.initialize(); + tester.registerTestCases(); + tester.executeTestCases(); Console.Write(tester.getResults()); } catch (Exception e) diff --git a/Tests/TestCases.cs b/Tests/TestCases.cs index f7e4cf4..7a18d4c 100644 --- a/Tests/TestCases.cs +++ b/Tests/TestCases.cs @@ -27,6 +27,7 @@ namespace Tests { + [TestCase] public class TestCases { public TestCases (){} From 85e7798aa00960ba336b18068ba1621181e0bf09 Mon Sep 17 00:00:00 2001 From: NetGnome Date: Sun, 20 Apr 2014 13:50:13 -0400 Subject: [PATCH 06/27] fixed tests to use latest revision of CSTester --- Tests/Issue2.cs | 6 +++--- Tests/TestCases.cs | 24 ++++++++++++------------ 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/Tests/Issue2.cs b/Tests/Issue2.cs index 3415cd1..925c7c5 100644 --- a/Tests/Issue2.cs +++ b/Tests/Issue2.cs @@ -51,7 +51,7 @@ public void test1(){ return BehaviorReturnCode.Running; })); - new VerificationPoint ().VerifyEquals ("all running is running", true, foo.Behave(), BehaviorReturnCode.Running); + VerificationPoint.VerifyEquals ("all running is running", true, foo.Behave(), BehaviorReturnCode.Running); foo = new Sequence (new BehaviorAction (delegate() { return BehaviorReturnCode.Running; @@ -63,7 +63,7 @@ public void test1(){ return BehaviorReturnCode.Success; })); - new VerificationPoint ().VerifyEquals ("all but one running is running", true, foo.Behave(), BehaviorReturnCode.Running); + VerificationPoint.VerifyEquals ("all but one running is running", true, foo.Behave(), BehaviorReturnCode.Running); foo = new Sequence (new BehaviorAction (delegate() { return BehaviorReturnCode.Success; @@ -75,7 +75,7 @@ public void test1(){ return BehaviorReturnCode.Success; })); - new VerificationPoint ().VerifyEquals ("all success is success", true, foo.Behave(), BehaviorReturnCode.Success); + VerificationPoint.VerifyEquals ("all success is success", true, foo.Behave(), BehaviorReturnCode.Success); _log.exitScope (); diff --git a/Tests/TestCases.cs b/Tests/TestCases.cs index 7a18d4c..956c980 100644 --- a/Tests/TestCases.cs +++ b/Tests/TestCases.cs @@ -77,16 +77,16 @@ public void testStatefulSeq(){ return BehaviorReturnCode.Success; })); - new VerificationPoint ().VerifyEquals ("1st running", true, foo.Behave (), BehaviorReturnCode.Running); - new VerificationPoint ().VerifyEquals ("2nd success", true, foo.Behave (), BehaviorReturnCode.Success); - new VerificationPoint ().VerifyEquals ("3rd failure", true, foo.Behave (), BehaviorReturnCode.Failure); + VerificationPoint.VerifyEquals ("1st running", true, foo.Behave (), BehaviorReturnCode.Running); + VerificationPoint.VerifyEquals ("2nd success", true, foo.Behave (), BehaviorReturnCode.Success); + VerificationPoint.VerifyEquals ("3rd failure", true, foo.Behave (), BehaviorReturnCode.Failure); _log.logMessage ("restting first"); first = true; - new VerificationPoint ().VerifyEquals ("after reset running", true, foo.Behave (), BehaviorReturnCode.Running); - new VerificationPoint ().VerifyEquals ("final success", true, foo.Behave (), BehaviorReturnCode.Success); - new VerificationPoint ().VerifyEquals ("final failure", true, foo.Behave (), BehaviorReturnCode.Failure); + VerificationPoint.VerifyEquals ("after reset running", true, foo.Behave (), BehaviorReturnCode.Running); + VerificationPoint.VerifyEquals ("final success", true, foo.Behave (), BehaviorReturnCode.Success); + VerificationPoint.VerifyEquals ("final failure", true, foo.Behave (), BehaviorReturnCode.Failure); _log.exitScope (); } @@ -119,17 +119,17 @@ public void testStatefulSel(){ } })); - new VerificationPoint ().VerifyEquals ("1st running", true, foo.Behave (), BehaviorReturnCode.Running); - new VerificationPoint ().VerifyEquals ("2nd success", true, foo.Behave (), BehaviorReturnCode.Success); - new VerificationPoint ().VerifyEquals ("3rd failure", true, foo.Behave (), BehaviorReturnCode.Failure); + VerificationPoint.VerifyEquals ("1st running", true, foo.Behave (), BehaviorReturnCode.Running); + VerificationPoint.VerifyEquals ("2nd success", true, foo.Behave (), BehaviorReturnCode.Success); + VerificationPoint.VerifyEquals ("3rd failure", true, foo.Behave (), BehaviorReturnCode.Failure); _log.logMessage ("restting flags"); first = true; second = true; - new VerificationPoint ().VerifyEquals ("after reset running", true, foo.Behave (), BehaviorReturnCode.Running); - new VerificationPoint ().VerifyEquals ("final success", true, foo.Behave (), BehaviorReturnCode.Success); - new VerificationPoint ().VerifyEquals ("final failure", true, foo.Behave (), BehaviorReturnCode.Failure); + VerificationPoint.VerifyEquals ("after reset running", true, foo.Behave (), BehaviorReturnCode.Running); + VerificationPoint.VerifyEquals ("final success", true, foo.Behave (), BehaviorReturnCode.Success); + VerificationPoint.VerifyEquals ("final failure", true, foo.Behave (), BehaviorReturnCode.Failure); _log.exitScope (); } From 27d8cb3190e9e0b4376e2302e044d694444c53ff Mon Sep 17 00:00:00 2001 From: NetGnome Date: Fri, 20 Jun 2014 10:08:23 -0700 Subject: [PATCH 07/27] initial utility setup --- BehaviorLibrary/BehaviorLibrary.csproj | 3 + .../Components/Utility/UtilityPair.cs | 18 +++++ .../Components/Utility/UtilitySelector.cs | 22 +++++ .../Components/Utility/UtilityVector.cs | 12 +++ Tests/Tests.csproj | 3 +- Tests/UtilityTests.cs | 80 +++++++++++++++++++ 6 files changed, 137 insertions(+), 1 deletion(-) create mode 100644 BehaviorLibrary/Components/Utility/UtilityPair.cs create mode 100644 BehaviorLibrary/Components/Utility/UtilitySelector.cs create mode 100644 BehaviorLibrary/Components/Utility/UtilityVector.cs create mode 100644 Tests/UtilityTests.cs diff --git a/BehaviorLibrary/BehaviorLibrary.csproj b/BehaviorLibrary/BehaviorLibrary.csproj index 92e478b..d7173bd 100644 --- a/BehaviorLibrary/BehaviorLibrary.csproj +++ b/BehaviorLibrary/BehaviorLibrary.csproj @@ -52,6 +52,9 @@ + + + diff --git a/BehaviorLibrary/Components/Utility/UtilityPair.cs b/BehaviorLibrary/Components/Utility/UtilityPair.cs new file mode 100644 index 0000000..c1f22a4 --- /dev/null +++ b/BehaviorLibrary/Components/Utility/UtilityPair.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace BehaviorLibrary.Components.Utility +{ + public class UtilityPair + { + public UtilityPair(UtilityVector vector, BehaviorComponent behavior) + { + + } + + public UtilityVector vector { get; set; } + public BehaviorComponent behavior { get; set; } + } +} diff --git a/BehaviorLibrary/Components/Utility/UtilitySelector.cs b/BehaviorLibrary/Components/Utility/UtilitySelector.cs new file mode 100644 index 0000000..0407ceb --- /dev/null +++ b/BehaviorLibrary/Components/Utility/UtilitySelector.cs @@ -0,0 +1,22 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace BehaviorLibrary.Components.Utility +{ + public class UtilitySelector : BehaviorComponent + { + private UtilityPair[] _utility_pairs; + + public UtilitySelector(params UtilityPair[] pairs) + { + this._utility_pairs = pairs; + } + + public override BehaviorReturnCode Behave() + { + return BehaviorReturnCode.Success; + } + } +} diff --git a/BehaviorLibrary/Components/Utility/UtilityVector.cs b/BehaviorLibrary/Components/Utility/UtilityVector.cs new file mode 100644 index 0000000..6d17567 --- /dev/null +++ b/BehaviorLibrary/Components/Utility/UtilityVector.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace BehaviorLibrary.Components.Utility +{ + public class UtilityVector + { + public UtilityVector(params float[] values){} + } +} diff --git a/Tests/Tests.csproj b/Tests/Tests.csproj index c961055..820b505 100644 --- a/Tests/Tests.csproj +++ b/Tests/Tests.csproj @@ -1,4 +1,4 @@ - + Debug @@ -42,6 +42,7 @@ + diff --git a/Tests/UtilityTests.cs b/Tests/UtilityTests.cs new file mode 100644 index 0000000..187644c --- /dev/null +++ b/Tests/UtilityTests.cs @@ -0,0 +1,80 @@ +// +// UtilityTests.cs +// +// Author: +// Thomas H. Jonell <@Net_Gnome> +// +// Copyright (c) 2014 Thomas H. Jonell +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Lesser General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with this program. If not, see .using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +using CSTester; +using CSLogging; + +using BehaviorLibrary; +using BehaviorLibrary.Components; +using BehaviorLibrary.Components.Utility; +using BehaviorLibrary.Components.Actions; + +namespace Tests +{ + [TestCase] + class UtilityTests + { + + private CSLogger _log = CSLogger.Instance; + + [BuildUp] + public void buildup() + { + _log.setEnableLogging(true); + _log.setEnableDebug(true); + _log.setEnableError(true); + _log.setEnableMessage(true); + _log.loadLog("", "utility_tests.log"); + _log.enterScope(); + _log.logMessage("----------------- STARTING BEHAVIOR LIBRARY UTILITY TESTS -----------------"); + } + + [TearDown] + public void teardown() + { + _log.enterScope(); + _log.exitScope(); + _log.logMessage("----------------- ENDING BEHAVIOR LIBRARY UTILITY TESTS -----------------"); + _log.exitScope(); + _log.closeLog(); + } + + [Test] + public void test_case_1() + { + _log.enterScope(); + + UtilityVector vector = new UtilityVector(0, 1, 0, 2); + BehaviorAction action = new BehaviorAction(delegate() { return BehaviorReturnCode.Success; }); + UtilityPair pair = new UtilityPair(vector, action); + UtilitySelector sel = new UtilitySelector(pair); + + var result = sel.Behave(); + + VerificationPoint.VerifyNotEquals("test_case_1", true, result, BehaviorReturnCode.Failure); + + _log.exitScope(); + } + } +} From 300ee553636fff3240cd4456492816e792aa005b Mon Sep 17 00:00:00 2001 From: NetGnome Date: Sat, 21 Jun 2014 18:10:59 -0400 Subject: [PATCH 08/27] vectors should be minimally functional... --- .../Components/Utility/UtilityVector.cs | 51 +++++++++++++++++-- Tests/UtilityTests.cs | 23 ++++++++- 2 files changed, 70 insertions(+), 4 deletions(-) diff --git a/BehaviorLibrary/Components/Utility/UtilityVector.cs b/BehaviorLibrary/Components/Utility/UtilityVector.cs index 6d17567..eb1f070 100644 --- a/BehaviorLibrary/Components/Utility/UtilityVector.cs +++ b/BehaviorLibrary/Components/Utility/UtilityVector.cs @@ -7,6 +7,51 @@ namespace BehaviorLibrary.Components.Utility { public class UtilityVector { - public UtilityVector(params float[] values){} - } -} + public UtilityVector(params float[] values){ + this.values = values; + } + + public float[] values{ get; set; } + + public float magnitude{ + get{ + float mag = 0; + for (int i = 0; i < this.values.Length; i++) + mag += this.values [i] * this.values [i]; + + return (float) Math.Sqrt (mag); + } + } + + public UtilityVector normalize(){ + if (this.values.Length <= 0) + return null; + + UtilityVector vec = new UtilityVector(); + vec.values = new float[this.values.Length]; + this.values.CopyTo (vec.values, 0); + + float mag = vec.magnitude; + + for (int i = 0; i < vec.values.Length; i++) + vec.values [i] = vec.values [i] / mag; + + return vec; + } + + public float dot(UtilityVector vector){ + if (this.magnitude == 0 || vector.magnitude == 0) + return -2; + + UtilityVector a = this.normalize (); + UtilityVector b = vector.normalize (); + + float val = 0; + + for (int i = 0; i < this.values.Length; i++) + val += a.values [i] * b.values [i]; + + return val; + } + } +} \ No newline at end of file diff --git a/Tests/UtilityTests.cs b/Tests/UtilityTests.cs index 187644c..e6c6fc6 100644 --- a/Tests/UtilityTests.cs +++ b/Tests/UtilityTests.cs @@ -60,8 +60,29 @@ public void teardown() _log.closeLog(); } + [Test] + public void test_vector(){ + _log.enterScope (); + + UtilityVector vec1 = new UtilityVector (0, 1, 2, 3, 4, 5); + + float mag = vec1.magnitude; + _log.logDebug ("mag: " + mag); + VerificationPoint.VerifyTrue ("verify mag gte 0", true, mag >= 0); + + VerificationPoint.VerifyTrue ("norm is not null", true, vec1.normalize () != null); + + UtilityVector vec2 = new UtilityVector (5, 4, 3, 2, 1, 0); + + float dot = vec1.dot (vec2); + _log.logDebug ("dot: " + dot); + VerificationPoint.VerifyTrue ("dot between 1 and -1", true, (dot <= 1) && (dot >= -1)); + + _log.exitScope (); + } + [Test] - public void test_case_1() + public void test_1() { _log.enterScope(); From 96aa8025f5044289abe49095467896d3fdfc3e8c Mon Sep 17 00:00:00 2001 From: NetGnome Date: Sun, 22 Jun 2014 15:38:02 -0400 Subject: [PATCH 09/27] updated some more tests --- BehaviorLibrary/Components/Utility/UtilityVector.cs | 8 ++++++++ Tests/UtilityTests.cs | 5 +++++ 2 files changed, 13 insertions(+) diff --git a/BehaviorLibrary/Components/Utility/UtilityVector.cs b/BehaviorLibrary/Components/Utility/UtilityVector.cs index eb1f070..7397464 100644 --- a/BehaviorLibrary/Components/Utility/UtilityVector.cs +++ b/BehaviorLibrary/Components/Utility/UtilityVector.cs @@ -13,6 +13,7 @@ public UtilityVector(params float[] values){ public float[] values{ get; set; } + //return the magnitude of this vector public float magnitude{ get{ float mag = 0; @@ -23,6 +24,9 @@ public float magnitude{ } } + /// + /// Return a new vector based on the normalization of this instance. + /// public UtilityVector normalize(){ if (this.values.Length <= 0) return null; @@ -39,6 +43,10 @@ public UtilityVector normalize(){ return vec; } + /// + /// Dot between this and another specified vector. (based on normalized vectors) + /// + /// Vector. public float dot(UtilityVector vector){ if (this.magnitude == 0 || vector.magnitude == 0) return -2; diff --git a/Tests/UtilityTests.cs b/Tests/UtilityTests.cs index e6c6fc6..b6fe120 100644 --- a/Tests/UtilityTests.cs +++ b/Tests/UtilityTests.cs @@ -78,6 +78,11 @@ public void test_vector(){ _log.logDebug ("dot: " + dot); VerificationPoint.VerifyTrue ("dot between 1 and -1", true, (dot <= 1) && (dot >= -1)); + dot = vec1.dot (vec1); + _log.logError ("self dot: " + dot); + VerificationPoint.VerifyTrue ("dot with itself should be 1", true, dot == 1); + + _log.exitScope (); } From f1b358e07a414eab5bc991239af94acaaec76bae Mon Sep 17 00:00:00 2001 From: NetGnome Date: Thu, 3 Jul 2014 09:54:27 -0400 Subject: [PATCH 10/27] basic utility framework is up... more tests to come --- .../Components/Utility/UtilityPair.cs | 3 +- .../Components/Utility/UtilitySelector.cs | 39 ++++++++++++++++++- Tests/Tests.csproj | 4 +- Tests/UtilityTests.cs | 8 ++-- 4 files changed, 45 insertions(+), 9 deletions(-) diff --git a/BehaviorLibrary/Components/Utility/UtilityPair.cs b/BehaviorLibrary/Components/Utility/UtilityPair.cs index c1f22a4..bac8230 100644 --- a/BehaviorLibrary/Components/Utility/UtilityPair.cs +++ b/BehaviorLibrary/Components/Utility/UtilityPair.cs @@ -9,7 +9,8 @@ public class UtilityPair { public UtilityPair(UtilityVector vector, BehaviorComponent behavior) { - + this.vector = vector; + this.behavior = behavior; } public UtilityVector vector { get; set; } diff --git a/BehaviorLibrary/Components/Utility/UtilitySelector.cs b/BehaviorLibrary/Components/Utility/UtilitySelector.cs index 0407ceb..1ab4b44 100644 --- a/BehaviorLibrary/Components/Utility/UtilitySelector.cs +++ b/BehaviorLibrary/Components/Utility/UtilitySelector.cs @@ -8,15 +8,50 @@ namespace BehaviorLibrary.Components.Utility public class UtilitySelector : BehaviorComponent { private UtilityPair[] _utility_pairs; + private Func _utility_function; - public UtilitySelector(params UtilityPair[] pairs) + public UtilitySelector(Func utility_function, params UtilityPair[] pairs) { this._utility_pairs = pairs; + this._utility_function = utility_function; } public override BehaviorReturnCode Behave() { - return BehaviorReturnCode.Success; + try{ + UtilityVector func_vector = this._utility_function.Invoke (); + + float min = -2.0f; + UtilityPair best_match = null; + + //find max pair match + foreach(UtilityPair pair in this._utility_pairs){ + float val = func_vector.dot(pair.vector); + if(val > min){ + min = val; + best_match = pair; + } + } + + //make sure we found a match + if(best_match == null){ + #if DEBUG + Console.WriteLine("best_match not defined..."); + #endif + this.ReturnCode = BehaviorReturnCode.Failure; + return this.ReturnCode; + } + + //execute best pair match and return result + this.ReturnCode = best_match.behavior.Behave(); + return this.ReturnCode; + }catch(Exception e){ + #if DEBUG + Console.WriteLine(e.ToString()); + #endif + this.ReturnCode = BehaviorReturnCode.Failure; + return BehaviorReturnCode.Failure; + } } } } diff --git a/Tests/Tests.csproj b/Tests/Tests.csproj index 820b505..f5419e8 100644 --- a/Tests/Tests.csproj +++ b/Tests/Tests.csproj @@ -3,7 +3,7 @@ Debug AnyCPU - 10.0.0 + 8.0.30703 2.0 {A7D9F051-A51E-42FF-AD2B-D94DC8A06650} Exe @@ -15,7 +15,7 @@ full false bin\Debug - DEBUG; + DEBUG;TRACE prompt 4 true diff --git a/Tests/UtilityTests.cs b/Tests/UtilityTests.cs index b6fe120..5db64d3 100644 --- a/Tests/UtilityTests.cs +++ b/Tests/UtilityTests.cs @@ -79,7 +79,7 @@ public void test_vector(){ VerificationPoint.VerifyTrue ("dot between 1 and -1", true, (dot <= 1) && (dot >= -1)); dot = vec1.dot (vec1); - _log.logError ("self dot: " + dot); + _log.logDebug ("self dot: " + dot); VerificationPoint.VerifyTrue ("dot with itself should be 1", true, dot == 1); @@ -94,11 +94,11 @@ public void test_1() UtilityVector vector = new UtilityVector(0, 1, 0, 2); BehaviorAction action = new BehaviorAction(delegate() { return BehaviorReturnCode.Success; }); UtilityPair pair = new UtilityPair(vector, action); - UtilitySelector sel = new UtilitySelector(pair); + UtilitySelector sel = new UtilitySelector(delegate(){return new UtilityVector(0,1,1,2);},pair,pair); - var result = sel.Behave(); + BehaviorReturnCode result = sel.Behave(); - VerificationPoint.VerifyNotEquals("test_case_1", true, result, BehaviorReturnCode.Failure); + VerificationPoint.VerifyNotEquals("basic vector compare", true, result, BehaviorReturnCode.Failure); _log.exitScope(); } From 79f3f4d48ea1ef61e6209e8c3c3e9979f3d12c07 Mon Sep 17 00:00:00 2001 From: NetGnome Date: Thu, 3 Jul 2014 13:25:34 -0400 Subject: [PATCH 11/27] more tests --- Tests/UtilityTests.cs | 81 +++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 79 insertions(+), 2 deletions(-) diff --git a/Tests/UtilityTests.cs b/Tests/UtilityTests.cs index 5db64d3..b1f7b1a 100644 --- a/Tests/UtilityTests.cs +++ b/Tests/UtilityTests.cs @@ -17,11 +17,14 @@ // GNU Lesser General Public License for more details. // // You should have received a copy of the GNU Lesser General Public License -// along with this program. If not, see .using System; +// along with this program. If not, see . +using System; using System.Collections.Generic; using System.Linq; using System.Text; + + using CSTester; using CSLogging; @@ -87,7 +90,7 @@ public void test_vector(){ } [Test] - public void test_1() + public void selector_1() { _log.enterScope(); @@ -102,5 +105,79 @@ public void test_1() _log.exitScope(); } + + [Test] + public void selector_2(){ + _log.enterScope(); + + //build vectors + UtilityVector a = new UtilityVector(0, 1, 0, 1); + UtilityVector b = new UtilityVector(1, 1, 0, 0); + UtilityVector c = new UtilityVector(1, 0, 1, 0); + UtilityVector d = new UtilityVector(0, 0, 1, 1); + + string choice = ""; + + //build actions that change choice if called + BehaviorAction aa = new BehaviorAction (delegate() { + choice = "a"; + return BehaviorReturnCode.Success; + }); + BehaviorAction ba = new BehaviorAction (delegate() { + choice = "b"; + return BehaviorReturnCode.Success; + }); + BehaviorAction ca = new BehaviorAction (delegate() { + choice = "c"; + return BehaviorReturnCode.Success; + }); + BehaviorAction da = new BehaviorAction (delegate() { + choice = "d"; + return BehaviorReturnCode.Success; + }); + + //build the appropraite pairs + UtilityPair ap = new UtilityPair (a, aa); + UtilityPair bp = new UtilityPair (b, ba); + UtilityPair cp = new UtilityPair (c, ca); + UtilityPair dp = new UtilityPair (d, da); + + + //execute tests + UtilitySelector sel = new UtilitySelector (delegate() { + return new UtilityVector (0,1,0,1); + }, ap, bp, cp, dp); + + sel.Behave (); + + VerificationPoint.VerifyTrue ("a chosen", true, choice == "a"); + + sel = new UtilitySelector (delegate() { + return new UtilityVector (1,1,0,0); + }, ap, bp, cp, dp); + + sel.Behave (); + + VerificationPoint.VerifyTrue ("b chosen", true, choice == "b"); + + sel = new UtilitySelector (delegate() { + return new UtilityVector (1,0,1,0); + }, ap, bp, cp, dp); + + sel.Behave (); + + VerificationPoint.VerifyTrue ("c chosen", true, choice == "c"); + + sel = new UtilitySelector (delegate() { + return new UtilityVector (0,0,1,1); + }, ap, bp, cp, dp); + + sel.Behave (); + + VerificationPoint.VerifyTrue ("d chosen", true, choice == "d"); + + + _log.exitScope (); + } } } From f1a33101e9f4969d964e92562d66f5fcf0cc3074 Mon Sep 17 00:00:00 2001 From: NetGnome Date: Sat, 12 Jul 2014 14:16:17 -0400 Subject: [PATCH 12/27] updated for changed in CSTester --- Tests/Issue2.cs | 6 +-- Tests/TestCases.cs | 24 +++++------ Tests/UtilityTests.cs | 92 ++++++++++++++++++++++++++++++++++++++----- 3 files changed, 98 insertions(+), 24 deletions(-) diff --git a/Tests/Issue2.cs b/Tests/Issue2.cs index 925c7c5..b1889cf 100644 --- a/Tests/Issue2.cs +++ b/Tests/Issue2.cs @@ -51,7 +51,7 @@ public void test1(){ return BehaviorReturnCode.Running; })); - VerificationPoint.VerifyEquals ("all running is running", true, foo.Behave(), BehaviorReturnCode.Running); + Verify.VerifyEquals ("all running is running", true, foo.Behave(), BehaviorReturnCode.Running); foo = new Sequence (new BehaviorAction (delegate() { return BehaviorReturnCode.Running; @@ -63,7 +63,7 @@ public void test1(){ return BehaviorReturnCode.Success; })); - VerificationPoint.VerifyEquals ("all but one running is running", true, foo.Behave(), BehaviorReturnCode.Running); + Verify.VerifyEquals ("all but one running is running", true, foo.Behave(), BehaviorReturnCode.Running); foo = new Sequence (new BehaviorAction (delegate() { return BehaviorReturnCode.Success; @@ -75,7 +75,7 @@ public void test1(){ return BehaviorReturnCode.Success; })); - VerificationPoint.VerifyEquals ("all success is success", true, foo.Behave(), BehaviorReturnCode.Success); + Verify.VerifyEquals ("all success is success", true, foo.Behave(), BehaviorReturnCode.Success); _log.exitScope (); diff --git a/Tests/TestCases.cs b/Tests/TestCases.cs index 956c980..0eb4a51 100644 --- a/Tests/TestCases.cs +++ b/Tests/TestCases.cs @@ -77,16 +77,16 @@ public void testStatefulSeq(){ return BehaviorReturnCode.Success; })); - VerificationPoint.VerifyEquals ("1st running", true, foo.Behave (), BehaviorReturnCode.Running); - VerificationPoint.VerifyEquals ("2nd success", true, foo.Behave (), BehaviorReturnCode.Success); - VerificationPoint.VerifyEquals ("3rd failure", true, foo.Behave (), BehaviorReturnCode.Failure); + Verify.VerifyEquals ("1st running", true, foo.Behave (), BehaviorReturnCode.Running); + Verify.VerifyEquals ("2nd success", true, foo.Behave (), BehaviorReturnCode.Success); + Verify.VerifyEquals ("3rd failure", true, foo.Behave (), BehaviorReturnCode.Failure); _log.logMessage ("restting first"); first = true; - VerificationPoint.VerifyEquals ("after reset running", true, foo.Behave (), BehaviorReturnCode.Running); - VerificationPoint.VerifyEquals ("final success", true, foo.Behave (), BehaviorReturnCode.Success); - VerificationPoint.VerifyEquals ("final failure", true, foo.Behave (), BehaviorReturnCode.Failure); + Verify.VerifyEquals ("after reset running", true, foo.Behave (), BehaviorReturnCode.Running); + Verify.VerifyEquals ("final success", true, foo.Behave (), BehaviorReturnCode.Success); + Verify.VerifyEquals ("final failure", true, foo.Behave (), BehaviorReturnCode.Failure); _log.exitScope (); } @@ -119,17 +119,17 @@ public void testStatefulSel(){ } })); - VerificationPoint.VerifyEquals ("1st running", true, foo.Behave (), BehaviorReturnCode.Running); - VerificationPoint.VerifyEquals ("2nd success", true, foo.Behave (), BehaviorReturnCode.Success); - VerificationPoint.VerifyEquals ("3rd failure", true, foo.Behave (), BehaviorReturnCode.Failure); + Verify.VerifyEquals ("1st running", true, foo.Behave (), BehaviorReturnCode.Running); + Verify.VerifyEquals ("2nd success", true, foo.Behave (), BehaviorReturnCode.Success); + Verify.VerifyEquals ("3rd failure", true, foo.Behave (), BehaviorReturnCode.Failure); _log.logMessage ("restting flags"); first = true; second = true; - VerificationPoint.VerifyEquals ("after reset running", true, foo.Behave (), BehaviorReturnCode.Running); - VerificationPoint.VerifyEquals ("final success", true, foo.Behave (), BehaviorReturnCode.Success); - VerificationPoint.VerifyEquals ("final failure", true, foo.Behave (), BehaviorReturnCode.Failure); + Verify.VerifyEquals ("after reset running", true, foo.Behave (), BehaviorReturnCode.Running); + Verify.VerifyEquals ("final success", true, foo.Behave (), BehaviorReturnCode.Success); + Verify.VerifyEquals ("final failure", true, foo.Behave (), BehaviorReturnCode.Failure); _log.exitScope (); } diff --git a/Tests/UtilityTests.cs b/Tests/UtilityTests.cs index b1f7b1a..f71587c 100644 --- a/Tests/UtilityTests.cs +++ b/Tests/UtilityTests.cs @@ -71,19 +71,19 @@ public void test_vector(){ float mag = vec1.magnitude; _log.logDebug ("mag: " + mag); - VerificationPoint.VerifyTrue ("verify mag gte 0", true, mag >= 0); + Verify.VerifyTrue ("verify mag gte 0", true, mag >= 0); - VerificationPoint.VerifyTrue ("norm is not null", true, vec1.normalize () != null); + Verify.VerifyTrue ("norm is not null", true, vec1.normalize () != null); UtilityVector vec2 = new UtilityVector (5, 4, 3, 2, 1, 0); float dot = vec1.dot (vec2); _log.logDebug ("dot: " + dot); - VerificationPoint.VerifyTrue ("dot between 1 and -1", true, (dot <= 1) && (dot >= -1)); + Verify.VerifyTrue ("dot between 1 and -1", true, (dot <= 1) && (dot >= -1)); dot = vec1.dot (vec1); _log.logDebug ("self dot: " + dot); - VerificationPoint.VerifyTrue ("dot with itself should be 1", true, dot == 1); + Verify.VerifyTrue ("dot with itself should be 1", true, dot == 1); _log.exitScope (); @@ -101,7 +101,7 @@ public void selector_1() BehaviorReturnCode result = sel.Behave(); - VerificationPoint.VerifyNotEquals("basic vector compare", true, result, BehaviorReturnCode.Failure); + Verify.VerifyNotEquals("basic vector compare", true, result, BehaviorReturnCode.Failure); _log.exitScope(); } @@ -150,7 +150,7 @@ public void selector_2(){ sel.Behave (); - VerificationPoint.VerifyTrue ("a chosen", true, choice == "a"); + Verify.VerifyTrue ("a chosen", true, choice == "a"); sel = new UtilitySelector (delegate() { return new UtilityVector (1,1,0,0); @@ -158,7 +158,7 @@ public void selector_2(){ sel.Behave (); - VerificationPoint.VerifyTrue ("b chosen", true, choice == "b"); + Verify.VerifyTrue ("b chosen", true, choice == "b"); sel = new UtilitySelector (delegate() { return new UtilityVector (1,0,1,0); @@ -166,7 +166,7 @@ public void selector_2(){ sel.Behave (); - VerificationPoint.VerifyTrue ("c chosen", true, choice == "c"); + Verify.VerifyTrue ("c chosen", true, choice == "c"); sel = new UtilitySelector (delegate() { return new UtilityVector (0,0,1,1); @@ -174,7 +174,81 @@ public void selector_2(){ sel.Behave (); - VerificationPoint.VerifyTrue ("d chosen", true, choice == "d"); + Verify.VerifyTrue ("d chosen", true, choice == "d"); + + + _log.exitScope (); + } + + [Test] + public void selector_3(){ + _log.enterScope(); + + //build vectors + UtilityVector a = new UtilityVector(0, 1, 0, 1); + UtilityVector b = new UtilityVector(1, 1, 0, 0); + UtilityVector c = new UtilityVector(1, 0, 1, 0); + UtilityVector d = new UtilityVector(0, 0, 1, 1); + + string choice = ""; + + //build actions that change choice if called + BehaviorAction aa = new BehaviorAction (delegate() { + choice = "a"; + return BehaviorReturnCode.Success; + }); + BehaviorAction ba = new BehaviorAction (delegate() { + choice = "b"; + return BehaviorReturnCode.Success; + }); + BehaviorAction ca = new BehaviorAction (delegate() { + choice = "c"; + return BehaviorReturnCode.Success; + }); + BehaviorAction da = new BehaviorAction (delegate() { + choice = "d"; + return BehaviorReturnCode.Success; + }); + + //build the appropraite pairs + UtilityPair ap = new UtilityPair (a, aa); + UtilityPair bp = new UtilityPair (b, ba); + UtilityPair cp = new UtilityPair (c, ca); + UtilityPair dp = new UtilityPair (d, da); + + + //execute tests + UtilitySelector sel = new UtilitySelector (delegate() { + return new UtilityVector (0.5f,0.7f,0.4f,0.8f); + }, ap, bp, cp, dp); + + sel.Behave (); + + Verify.VerifyTrue ("a chosen", true, choice == "a"); + + sel = new UtilitySelector (delegate() { + return new UtilityVector (0.7f,0.8f,0.5f,0.4f); + }, ap, bp, cp, dp); + + sel.Behave (); + + Verify.VerifyTrue ("b chosen", true, choice == "b"); + + sel = new UtilitySelector (delegate() { + return new UtilityVector (0.7f,0.5f,0.8f,0.4f); + }, ap, bp, cp, dp); + + sel.Behave (); + + Verify.VerifyTrue ("c chosen", true, choice == "c"); + + sel = new UtilitySelector (delegate() { + return new UtilityVector (0.5f,0.4f,0.7f,0.8f); + }, ap, bp, cp, dp); + + sel.Behave (); + + Verify.VerifyTrue ("d chosen", true, choice == "d"); _log.exitScope (); From 7aa9d64c8198d26573b68e510b119205d2e59ee3 Mon Sep 17 00:00:00 2001 From: NetGnome Date: Sat, 12 Jul 2014 14:22:31 -0400 Subject: [PATCH 13/27] merged in utility branch --- README.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/README.md b/README.md index bafd1b8..52cbe53 100644 --- a/README.md +++ b/README.md @@ -3,6 +3,11 @@ Behavior Library BehaviorLibrary is a framework for creating behavior trees for game AI. It is free to use, modify, and redestribute as covered under the attached License (FreeBSD). +New +--- + +Added Utility components, see Utility Test cases for examples + It is simple to use and with that simplicity comes performance. Example of a simple A* following AI on a tilemap From ad8b548986ba6fdcc054ef24389744f579340f9f Mon Sep 17 00:00:00 2001 From: NetGnome Date: Sat, 12 Jul 2014 14:23:26 -0400 Subject: [PATCH 14/27] updated README --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index 52cbe53..4b9bc38 100644 --- a/README.md +++ b/README.md @@ -8,6 +8,10 @@ New Added Utility components, see Utility Test cases for examples + +Example +------- + It is simple to use and with that simplicity comes performance. Example of a simple A* following AI on a tilemap From 7449dd2e37b4e959646e476b9d784cbcf3ca304a Mon Sep 17 00:00:00 2001 From: NetGnome Date: Sat, 12 Jul 2014 14:25:12 -0400 Subject: [PATCH 15/27] udpated utility licenses --- .../Components/Utility/UtilityPair.cs | 22 ++++++++++++++++++- .../Components/Utility/UtilitySelector.cs | 22 ++++++++++++++++++- .../Components/Utility/UtilityVector.cs | 22 ++++++++++++++++++- 3 files changed, 63 insertions(+), 3 deletions(-) diff --git a/BehaviorLibrary/Components/Utility/UtilityPair.cs b/BehaviorLibrary/Components/Utility/UtilityPair.cs index bac8230..d153514 100644 --- a/BehaviorLibrary/Components/Utility/UtilityPair.cs +++ b/BehaviorLibrary/Components/Utility/UtilityPair.cs @@ -1,4 +1,24 @@ -using System; +// +// UtilityTests.cs +// +// Author: +// Thomas H. Jonell <@Net_Gnome> +// +// Copyright (c) 2014 Thomas H. Jonell +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Lesser General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with this program. If not, see . +using System; using System.Collections.Generic; using System.Linq; using System.Text; diff --git a/BehaviorLibrary/Components/Utility/UtilitySelector.cs b/BehaviorLibrary/Components/Utility/UtilitySelector.cs index 1ab4b44..09d28f3 100644 --- a/BehaviorLibrary/Components/Utility/UtilitySelector.cs +++ b/BehaviorLibrary/Components/Utility/UtilitySelector.cs @@ -1,4 +1,24 @@ -using System; +// +// UtilityTests.cs +// +// Author: +// Thomas H. Jonell <@Net_Gnome> +// +// Copyright (c) 2014 Thomas H. Jonell +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Lesser General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with this program. If not, see . +using System; using System.Collections.Generic; using System.Linq; using System.Text; diff --git a/BehaviorLibrary/Components/Utility/UtilityVector.cs b/BehaviorLibrary/Components/Utility/UtilityVector.cs index 7397464..747e6fc 100644 --- a/BehaviorLibrary/Components/Utility/UtilityVector.cs +++ b/BehaviorLibrary/Components/Utility/UtilityVector.cs @@ -1,4 +1,24 @@ -using System; +// +// UtilityTests.cs +// +// Author: +// Thomas H. Jonell <@Net_Gnome> +// +// Copyright (c) 2014 Thomas H. Jonell +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Lesser General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with this program. If not, see . +using System; using System.Collections.Generic; using System.Linq; using System.Text; From 9f0d01ce39eb5cba963af067e934680386998529 Mon Sep 17 00:00:00 2001 From: NetGnome Date: Sat, 12 Jul 2014 14:40:16 -0400 Subject: [PATCH 16/27] improved Utility descriptions in README --- README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 4b9bc38..2ca6fc2 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,9 @@ BehaviorLibrary is a framework for creating behavior trees for game AI. It is fr New --- -Added Utility components, see Utility Test cases for examples +Added Utility components, see Utility Test cases for test examples. + +The basic point is to use a vector of floating numbers representing weights/values that will be paired with a BehaviorComponent object. When a UtilitySelector is called, it will execute a function that returns a UtilityVector that will then be compared against the BehaviorComponents' paired vectors (via a dot product) and select the pair that best matches and execute its Behavior. Example From e6517ff0f2a1033d99011287792219cf3c3d6f41 Mon Sep 17 00:00:00 2001 From: NetGnome Date: Mon, 14 Jul 2014 07:20:17 -0400 Subject: [PATCH 17/27] fixed license text to reference correct source files --- BehaviorLibrary/Components/Utility/UtilityPair.cs | 2 +- BehaviorLibrary/Components/Utility/UtilitySelector.cs | 2 +- BehaviorLibrary/Components/Utility/UtilityVector.cs | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/BehaviorLibrary/Components/Utility/UtilityPair.cs b/BehaviorLibrary/Components/Utility/UtilityPair.cs index d153514..25edc05 100644 --- a/BehaviorLibrary/Components/Utility/UtilityPair.cs +++ b/BehaviorLibrary/Components/Utility/UtilityPair.cs @@ -1,5 +1,5 @@ // -// UtilityTests.cs +// UtilityPair.cs // // Author: // Thomas H. Jonell <@Net_Gnome> diff --git a/BehaviorLibrary/Components/Utility/UtilitySelector.cs b/BehaviorLibrary/Components/Utility/UtilitySelector.cs index 09d28f3..b6709da 100644 --- a/BehaviorLibrary/Components/Utility/UtilitySelector.cs +++ b/BehaviorLibrary/Components/Utility/UtilitySelector.cs @@ -1,5 +1,5 @@ // -// UtilityTests.cs +// UtilitySelector.cs // // Author: // Thomas H. Jonell <@Net_Gnome> diff --git a/BehaviorLibrary/Components/Utility/UtilityVector.cs b/BehaviorLibrary/Components/Utility/UtilityVector.cs index 747e6fc..aed7248 100644 --- a/BehaviorLibrary/Components/Utility/UtilityVector.cs +++ b/BehaviorLibrary/Components/Utility/UtilityVector.cs @@ -1,5 +1,5 @@ // -// UtilityTests.cs +// UtilityVector.cs // // Author: // Thomas H. Jonell <@Net_Gnome> From 57a8907867f887d7fd3545ba0d4ac025f25ad7e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nils=20K=C3=BCbler?= Date: Tue, 21 Oct 2014 10:12:41 +0200 Subject: [PATCH 18/27] Add Repeater, RepeatUntilFail and Succeeder decorators --- .../Components/Decorators/RepeatUntilFail.cs | 41 +++++++++++++++++++ .../Components/Decorators/Repeater.cs | 37 +++++++++++++++++ .../Components/Decorators/Succeeder.cs | 38 +++++++++++++++++ 3 files changed, 116 insertions(+) create mode 100644 BehaviorLibrary/Components/Decorators/RepeatUntilFail.cs create mode 100644 BehaviorLibrary/Components/Decorators/Repeater.cs create mode 100644 BehaviorLibrary/Components/Decorators/Succeeder.cs diff --git a/BehaviorLibrary/Components/Decorators/RepeatUntilFail.cs b/BehaviorLibrary/Components/Decorators/RepeatUntilFail.cs new file mode 100644 index 0000000..94e8f20 --- /dev/null +++ b/BehaviorLibrary/Components/Decorators/RepeatUntilFail.cs @@ -0,0 +1,41 @@ +using System; +using BehaviorLibrary; +using BehaviorLibrary.Components; +using BehaviorLibrary.Components.Composites; +using BehaviorLibrary.Components.Actions; +using BehaviorLibrary.Components.Conditionals; +using BehaviorLibrary.Components.Decorators; +using BehaviorLibrary.Components.Utility; + +namespace BehaviorLibrary.Components.Decorators +{ + public class RepeatUntilFail : BehaviorComponent + { + private BehaviorComponent _Behavior; + + /// + /// executes the behavior every time again + /// + /// maximum time to wait before executing behavior + /// behavior to run + public RepeatUntilFail(BehaviorComponent behavior) + { + _Behavior = behavior; + } + + /// + /// performs the given behavior + /// + /// the behaviors return code + public override BehaviorReturnCode Behave() + { + ReturnCode = _Behavior.Behave(); + if (ReturnCode == BehaviorReturnCode.Failure) { + return BehaviorReturnCode.Failure; + } else { + ReturnCode = BehaviorReturnCode.Running; + return BehaviorReturnCode.Running; + } + } + } +} diff --git a/BehaviorLibrary/Components/Decorators/Repeater.cs b/BehaviorLibrary/Components/Decorators/Repeater.cs new file mode 100644 index 0000000..f71923a --- /dev/null +++ b/BehaviorLibrary/Components/Decorators/Repeater.cs @@ -0,0 +1,37 @@ +using System; +using BehaviorLibrary; +using BehaviorLibrary.Components; +using BehaviorLibrary.Components.Composites; +using BehaviorLibrary.Components.Actions; +using BehaviorLibrary.Components.Conditionals; +using BehaviorLibrary.Components.Decorators; +using BehaviorLibrary.Components.Utility; + +namespace BehaviorLibrary.Components.Decorators +{ + public class Repeater : BehaviorComponent + { + private BehaviorComponent _Behavior; + + /// + /// executes the behavior every time again + /// + /// maximum time to wait before executing behavior + /// behavior to run + public Repeater(BehaviorComponent behavior) + { + _Behavior = behavior; + } + + /// + /// performs the given behavior + /// + /// the behaviors return code + public override BehaviorReturnCode Behave() + { + ReturnCode = _Behavior.Behave(); + ReturnCode = BehaviorReturnCode.Running; + return BehaviorReturnCode.Running; + } + } +} diff --git a/BehaviorLibrary/Components/Decorators/Succeeder.cs b/BehaviorLibrary/Components/Decorators/Succeeder.cs new file mode 100644 index 0000000..b0795f1 --- /dev/null +++ b/BehaviorLibrary/Components/Decorators/Succeeder.cs @@ -0,0 +1,38 @@ +using System; +using BehaviorLibrary; +using BehaviorLibrary.Components; +using BehaviorLibrary.Components.Composites; +using BehaviorLibrary.Components.Actions; +using BehaviorLibrary.Components.Conditionals; +using BehaviorLibrary.Components.Decorators; +using BehaviorLibrary.Components.Utility; + +namespace BehaviorLibrary.Components.Decorators +{ + public class Succeeder : BehaviorComponent + { + private BehaviorComponent _Behavior; + + /// + /// returns a success even when the decorated component failed + /// + /// behavior to run + public Succeeder(BehaviorComponent behavior) + { + _Behavior = behavior; + } + + /// + /// performs the given behavior + /// + /// the behaviors return code + public override BehaviorReturnCode Behave() + { + ReturnCode = _Behavior.Behave(); + if (ReturnCode == BehaviorReturnCode.Failure) { + ReturnCode = BehaviorReturnCode.Success; + } + return ReturnCode; + } + } +} From 37ec62a093a83d413dae861e8b86f43b014c747b Mon Sep 17 00:00:00 2001 From: NetGnome Date: Tue, 21 Oct 2014 08:54:53 -0400 Subject: [PATCH 19/27] mergining in meniku's decorators --- BehaviorLibrary/BehaviorLibrary.csproj | 3 +++ 1 file changed, 3 insertions(+) diff --git a/BehaviorLibrary/BehaviorLibrary.csproj b/BehaviorLibrary/BehaviorLibrary.csproj index d7173bd..29c89c3 100644 --- a/BehaviorLibrary/BehaviorLibrary.csproj +++ b/BehaviorLibrary/BehaviorLibrary.csproj @@ -61,6 +61,9 @@ + + + From c296e15ed9a57a10f9710f7cc7fcd8009ac820de Mon Sep 17 00:00:00 2001 From: NetGnome Date: Tue, 21 Oct 2014 09:16:00 -0400 Subject: [PATCH 20/27] refactors and updates --- BehaviorLibrary/Behavior.cs | 8 ++++++-- BehaviorLibrary/BehaviorLibrary.csproj | 2 +- .../{RootSelector.cs => IndexSelector.cs} | 6 +++--- .../Components/Composites/PartialSelector.cs | 6 +++--- .../Components/Composites/PartialSequence.cs | 6 +++--- .../Components/Composites/RandomSelector.cs | 6 +++--- BehaviorLibrary/Components/Composites/Selector.cs | 6 +++--- BehaviorLibrary/Components/Composites/Sequence.cs | 6 +++--- README.md | 14 ++++++++++++-- 9 files changed, 37 insertions(+), 23 deletions(-) rename BehaviorLibrary/Components/Composites/{RootSelector.cs => IndexSelector.cs} (90%) diff --git a/BehaviorLibrary/Behavior.cs b/BehaviorLibrary/Behavior.cs index 0752a9f..9b0a031 100644 --- a/BehaviorLibrary/Behavior.cs +++ b/BehaviorLibrary/Behavior.cs @@ -21,7 +21,7 @@ public enum BehaviorReturnCode public class Behavior { - private RootSelector _Root; + private BehaviorComponent _Root; private BehaviorReturnCode _ReturnCode; @@ -35,11 +35,15 @@ public BehaviorReturnCode ReturnCode /// /// /// - public Behavior(RootSelector root) + public Behavior(IndexSelector root) { _Root = root; } + public Behavior(BehaviorComponent root){ + _Root = root; + } + /// /// perform the behavior /// diff --git a/BehaviorLibrary/BehaviorLibrary.csproj b/BehaviorLibrary/BehaviorLibrary.csproj index 29c89c3..36f18d0 100644 --- a/BehaviorLibrary/BehaviorLibrary.csproj +++ b/BehaviorLibrary/BehaviorLibrary.csproj @@ -45,7 +45,6 @@ - @@ -64,6 +63,7 @@ + diff --git a/BehaviorLibrary/Components/Composites/RootSelector.cs b/BehaviorLibrary/Components/Composites/IndexSelector.cs similarity index 90% rename from BehaviorLibrary/Components/Composites/RootSelector.cs rename to BehaviorLibrary/Components/Composites/IndexSelector.cs index a6a36de..a788e9a 100644 --- a/BehaviorLibrary/Components/Composites/RootSelector.cs +++ b/BehaviorLibrary/Components/Composites/IndexSelector.cs @@ -5,10 +5,10 @@ namespace BehaviorLibrary.Components.Composites { - public class RootSelector : PartialSelector + public class IndexSelector : BehaviorComponent { - private BehaviorComponent[] _Behaviors; + private BehaviorComponent[] _Behaviors; private Func _Index; @@ -17,7 +17,7 @@ public class RootSelector : PartialSelector /// /// an index representing which of the behavior branches to perform /// the behavior branches to be selected from - public RootSelector(Func index, params BehaviorComponent[] behaviors) + public IndexSelector(Func index, params BehaviorComponent[] behaviors) { _Index = index; _Behaviors = behaviors; diff --git a/BehaviorLibrary/Components/Composites/PartialSelector.cs b/BehaviorLibrary/Components/Composites/PartialSelector.cs index 4c9d689..039899a 100644 --- a/BehaviorLibrary/Components/Composites/PartialSelector.cs +++ b/BehaviorLibrary/Components/Composites/PartialSelector.cs @@ -5,10 +5,10 @@ namespace BehaviorLibrary.Components.Composites { - public class PartialSelector : BehaviorComponent + public class PartialSelector : BehaviorComponent { - protected BehaviorComponent[] _Behaviors; + protected BehaviorComponent[] _Behaviors; private short _selections = 0; @@ -22,7 +22,7 @@ public class PartialSelector : BehaviorComponent /// -Returns Failure if all behavior components returned Failure or an error has occured /// /// one to many behavior components - public PartialSelector(params BehaviorComponent[] behaviors) + public PartialSelector(params BehaviorComponent[] behaviors) { _Behaviors = behaviors; _selLength = (short)_Behaviors.Length; diff --git a/BehaviorLibrary/Components/Composites/PartialSequence.cs b/BehaviorLibrary/Components/Composites/PartialSequence.cs index e689871..478f462 100644 --- a/BehaviorLibrary/Components/Composites/PartialSequence.cs +++ b/BehaviorLibrary/Components/Composites/PartialSequence.cs @@ -5,10 +5,10 @@ namespace BehaviorLibrary.Components.Composites { - public class PartialSequence : BehaviorComponent + public class PartialSequence : BehaviorComponent { - protected BehaviorComponent[] _Behaviors; + protected BehaviorComponent[] _Behaviors; private short _sequence = 0; @@ -22,7 +22,7 @@ public class PartialSequence : BehaviorComponent /// -Returns Failure if a behavior components returns Failure or an error is encountered /// /// one to many behavior components - public PartialSequence(params BehaviorComponent[] behaviors) + public PartialSequence(params BehaviorComponent[] behaviors) { _Behaviors = behaviors; _seqLength = (short) _Behaviors.Length; diff --git a/BehaviorLibrary/Components/Composites/RandomSelector.cs b/BehaviorLibrary/Components/Composites/RandomSelector.cs index 3e540f8..bca4269 100644 --- a/BehaviorLibrary/Components/Composites/RandomSelector.cs +++ b/BehaviorLibrary/Components/Composites/RandomSelector.cs @@ -5,10 +5,10 @@ namespace BehaviorLibrary.Components.Composites { - public class RandomSelector : BehaviorComponent + public class RandomSelector : BehaviorComponent { - private BehaviorComponent[] _Behaviors; + private BehaviorComponent[] _Behaviors; //use current milliseconds to set random seed private Random _Random = new Random(DateTime.Now.Millisecond); @@ -20,7 +20,7 @@ public class RandomSelector : BehaviorComponent /// -Returns Running if selected behavior returns Running /// /// one to many behavior components - public RandomSelector(params BehaviorComponent[] behaviors) + public RandomSelector(params BehaviorComponent[] behaviors) { _Behaviors = behaviors; } diff --git a/BehaviorLibrary/Components/Composites/Selector.cs b/BehaviorLibrary/Components/Composites/Selector.cs index bef14fa..16a4daf 100644 --- a/BehaviorLibrary/Components/Composites/Selector.cs +++ b/BehaviorLibrary/Components/Composites/Selector.cs @@ -5,10 +5,10 @@ namespace BehaviorLibrary.Components.Composites { - public class Selector : BehaviorComponent + public class Selector : BehaviorComponent { - protected BehaviorComponent[] _Behaviors; + protected BehaviorComponent[] _Behaviors; /// @@ -19,7 +19,7 @@ public class Selector : BehaviorComponent /// -Returns Failure if all behavior components returned Failure /// /// one to many behavior components - public Selector(params BehaviorComponent[] behaviors) + public Selector(params BehaviorComponent[] behaviors) { _Behaviors = behaviors; } diff --git a/BehaviorLibrary/Components/Composites/Sequence.cs b/BehaviorLibrary/Components/Composites/Sequence.cs index f027332..6339241 100644 --- a/BehaviorLibrary/Components/Composites/Sequence.cs +++ b/BehaviorLibrary/Components/Composites/Sequence.cs @@ -5,10 +5,10 @@ namespace BehaviorLibrary.Components.Composites { - public class Sequence : BehaviorComponent + public class Sequence : BehaviorComponent { - private BehaviorComponent[] _behaviors; + private BehaviorComponent[] _behaviors; /// /// attempts to run the behaviors all in one cycle @@ -17,7 +17,7 @@ public class Sequence : BehaviorComponent /// -Returns Running if any are running /// /// - public Sequence(params BehaviorComponent[] behaviors) + public Sequence(params BehaviorComponent[] behaviors) { _behaviors = behaviors; } diff --git a/README.md b/README.md index 2ca6fc2..2625411 100644 --- a/README.md +++ b/README.md @@ -3,8 +3,18 @@ Behavior Library BehaviorLibrary is a framework for creating behavior trees for game AI. It is free to use, modify, and redestribute as covered under the attached License (FreeBSD). -New ---- +Changes +------- + +RootSelector has been refactored to IndexSelector. It works exactly the same, just has a more appropriate name. + +Behavior has had an additional constructor added that allows BehaviorComponent objects to be used rather than just RootSelector/IndexSelector objects. + +Merged in new Repeater, Succeeder, RepeatUntilFail Decorators. + + +Utilities +--------- Added Utility components, see Utility Test cases for test examples. From 413eaec3f441e29ed9a44c40331486c9c07d290c Mon Sep 17 00:00:00 2001 From: NetGnome Date: Thu, 13 Nov 2014 12:44:04 -0500 Subject: [PATCH 21/27] updated README --- README.md | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 2625411..0f9615c 100644 --- a/README.md +++ b/README.md @@ -6,9 +6,9 @@ BehaviorLibrary is a framework for creating behavior trees for game AI. It is fr Changes ------- -RootSelector has been refactored to IndexSelector. It works exactly the same, just has a more appropriate name. +IndexSelector has been refactored to IndexSelector. It works exactly the same, just has a more appropriate name. -Behavior has had an additional constructor added that allows BehaviorComponent objects to be used rather than just RootSelector/IndexSelector objects. +Behavior has had an additional constructor added that allows BehaviorComponent objects to be used rather than just IndexSelector/IndexSelector objects. Merged in new Repeater, Succeeder, RepeatUntilFail Decorators. @@ -48,20 +48,20 @@ Example of a simple A* following AI on a tilemap BehaviorAction animate = new BehaviorAction(updateAnimation); //setup an initilization branch - ParallelSequence initialize = new ParallelSequence(initPathfinder, calcPath); + Sequence initialize = new Sequence(initPathfinder, calcPath); //if the target has moved, reset and calculate a new path - ParallelSelector ifMovedCreateNewPath = new ParallelSelector(new Inverter(targetMoved), new Inverter(reset), calcPath); - ParallelSelector ifPathFoundGetPath = new ParallelSelector(new Inverter(pathFound), getPath); - ParallelSelector ifPathNewUseIt = new ParallelSelector(new Inverter(isNewPath), setPath); - ParallelSelector ifReachedCellGetNext = new ParallelSelector(new Inverter(reachedCell), getNextCell); - ParallelSelector ifNotReachedTargetMoveTowardsCell = new ParallelSelector(reachedTarget, moveToCell); + Selector ifMovedCreateNewPath = new Selector(new Inverter(targetMoved), new Inverter(reset), calcPath); + Selector ifPathFoundGetPath = new Selector(new Inverter(pathFound), getPath); + Selector ifPathNewUseIt = new Selector(new Inverter(isNewPath), setPath); + Selector ifReachedCellGetNext = new Selector(new Inverter(reachedCell), getNextCell); + Selector ifNotReachedTargetMoveTowardsCell = new Selector(reachedTarget, moveToCell); //follow target so long as you're not too close and then animate - ParallelSequence follow = new ParallelSequence(new Inverter(tooClose), updatePosition, ifMovedCreateNewPath, ifPathFoundGetPath, ifPathIsNewUseIt, ifReachedCellGetNext, ifNotReachedTargetMoveTowardsCell, animate); + Sequence follow = new Sequence(new Inverter(tooClose), updatePosition, ifMovedCreateNewPath, ifPathFoundGetPath, ifPathIsNewUseIt, ifReachedCellGetNext, ifNotReachedTargetMoveTowardsCell, animate); //setup root node, choose initialization phase or pathing/movement phase - RootSelector root = new RootSelector(switchBehaviors, initialize, follow); + IndexSelector root = new IndexSelector(switchBehaviors, initialize, follow); //set a reference to the root Behavior behavior = new Behavior(root); From 0cd8f754b2f1e91b8bc71fd653f3f371e68483f8 Mon Sep 17 00:00:00 2001 From: NetGnome Date: Thu, 13 Nov 2014 12:48:15 -0500 Subject: [PATCH 22/27] fixed mistake with auto-replace --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 0f9615c..3c3439b 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ BehaviorLibrary is a framework for creating behavior trees for game AI. It is fr Changes ------- -IndexSelector has been refactored to IndexSelector. It works exactly the same, just has a more appropriate name. +RootSelector has been refactored to IndexSelector. It works exactly the same, just has a more appropriate name. Behavior has had an additional constructor added that allows BehaviorComponent objects to be used rather than just IndexSelector/IndexSelector objects. From d8714823fef8c7fdde7a104be53930d2e56fa670 Mon Sep 17 00:00:00 2001 From: Tom J Date: Thu, 13 Nov 2014 16:56:32 -0500 Subject: [PATCH 23/27] Fixed another auto-replace oops --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 3c3439b..872bdeb 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ Changes RootSelector has been refactored to IndexSelector. It works exactly the same, just has a more appropriate name. -Behavior has had an additional constructor added that allows BehaviorComponent objects to be used rather than just IndexSelector/IndexSelector objects. +Behavior has had an additional constructor added that allows BehaviorComponent objects to be used rather than just RootSelector/IndexSelector objects. Merged in new Repeater, Succeeder, RepeatUntilFail Decorators. From f313c9051bbc71bfe074864bab63c71e80390715 Mon Sep 17 00:00:00 2001 From: NetGnome Date: Thu, 26 Feb 2015 05:38:43 -0500 Subject: [PATCH 24/27] updates for issue7 --- .../Components/Composites/RandomSelector.cs | 2 +- Tests/Issue7.cs | 85 +++++++++++++++++++ Tests/Tests.csproj | 1 + 3 files changed, 87 insertions(+), 1 deletion(-) create mode 100644 Tests/Issue7.cs diff --git a/BehaviorLibrary/Components/Composites/RandomSelector.cs b/BehaviorLibrary/Components/Composites/RandomSelector.cs index bca4269..17f8cf3 100644 --- a/BehaviorLibrary/Components/Composites/RandomSelector.cs +++ b/BehaviorLibrary/Components/Composites/RandomSelector.cs @@ -35,7 +35,7 @@ public override BehaviorReturnCode Behave() try { - switch (_Behaviors[_Random.Next(0, _Behaviors.Length - 1)].Behave()) + switch (_Behaviors[_Random.Next(0, _Behaviors.Length)].Behave()) { case BehaviorReturnCode.Failure: ReturnCode = BehaviorReturnCode.Failure; diff --git a/Tests/Issue7.cs b/Tests/Issue7.cs new file mode 100644 index 0000000..c93de4f --- /dev/null +++ b/Tests/Issue7.cs @@ -0,0 +1,85 @@ +using System; +using CSTester; +using CSLogging; +using BehaviorLibrary; +using BehaviorLibrary.Components.Composites; +using BehaviorLibrary.Components.Actions; + +namespace Tests +{ + [TestCase] + public class Issue7 + { + public Issue7 () + { + } + + private CSLogger _log = CSLogger.Instance; + + [BuildUp] + public void build_up(){ + _log.setEnableLogging (true); + _log.setEnableDebug (true); + _log.setEnableError (true); + _log.setEnableMessage (true); + _log.loadLog ("./", "issue7.log"); + _log.enterScope (); + _log.logMessage ("---------- BEGIN TESTING ISSUE 7 ----------"); + _log.exitScope (); + + } + + [TearDown] + public void tear_down(){ + _log.enterScope (); + _log.logMessage ("---------- END TESTING ISSUE 7 ----------"); + _log.exitScope (); + _log.closeLog (); + } + + private int[] counts = new int[4]; + + public BehaviorReturnCode component_1(){ + counts [0]++; + + return BehaviorReturnCode.Success; + } + + public BehaviorReturnCode component_2(){ + counts [1]++; + return BehaviorReturnCode.Success; + } + + public BehaviorReturnCode component_3(){ + counts [2]++; + return BehaviorReturnCode.Success; + } + + public BehaviorReturnCode component_4(){ + counts [3]++; + return BehaviorReturnCode.Success; + } + + + [Test] + public void test_1(){ + _log.enterScope (); + + RandomSelector rs = new RandomSelector (new BehaviorAction(component_1), + new BehaviorAction(component_2), + new BehaviorAction(component_3), + new BehaviorAction(component_4)); + + + for (int i = 0; i < 100000; i++) + rs.Behave (); + + _log.logMessage ("1:" + counts[0] +", 2:" + counts[1]+ ", 3:" + counts[2]+ ", 4:" + counts [3]); + + Verify.VerifyTrue ("verify last component actioned", true, counts [2] > 0); + + _log.exitScope (); + } + } +} + diff --git a/Tests/Tests.csproj b/Tests/Tests.csproj index f5419e8..b8da6f3 100644 --- a/Tests/Tests.csproj +++ b/Tests/Tests.csproj @@ -43,6 +43,7 @@ + From b2dbd61bfdd4ef5caaf0dc436e022e09ea7487dc Mon Sep 17 00:00:00 2001 From: NetGnome Date: Thu, 26 Feb 2015 05:40:18 -0500 Subject: [PATCH 25/27] fixed typo ;) --- Tests/Issue7.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Tests/Issue7.cs b/Tests/Issue7.cs index c93de4f..f7f7f04 100644 --- a/Tests/Issue7.cs +++ b/Tests/Issue7.cs @@ -76,7 +76,7 @@ public void test_1(){ _log.logMessage ("1:" + counts[0] +", 2:" + counts[1]+ ", 3:" + counts[2]+ ", 4:" + counts [3]); - Verify.VerifyTrue ("verify last component actioned", true, counts [2] > 0); + Verify.VerifyTrue ("verify last component actioned", true, counts [3] > 0); _log.exitScope (); } From 5efef7c9f3d08ed798e924757334ce490458d744 Mon Sep 17 00:00:00 2001 From: NetGnome Date: Thu, 26 Feb 2015 08:04:40 -0500 Subject: [PATCH 26/27] omg... I cannot believe I didn't see this spelling error earlier, lol --- README.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 872bdeb..8963121 100644 --- a/README.md +++ b/README.md @@ -29,12 +29,12 @@ It is simple to use and with that simplicity comes performance. Example of a simple A* following AI on a tilemap //setup all coniditionals and their delegate functions - Coniditional tooClose = new Conditional(isTooClose); - Coniditional targetMoved = new Conditional(hasTargetMoved); - Coniditional pathFound = new Conditional(hasPathBeenFound); - Coniditional reachedCell = new Conditional(hasReachedCell); - Coniditional reachedTarget = new Conditional(hasReachedTarget); - Coniditional isNewPath = new Conditional(hasNewPath); + Conditional tooClose = new Conditional(isTooClose); + Conditional targetMoved = new Conditional(hasTargetMoved); + Conditional pathFound = new Conditional(hasPathBeenFound); + Conditional reachedCell = new Conditional(hasReachedCell); + Conditional reachedTarget = new Conditional(hasReachedTarget); + Conditional isNewPath = new Conditional(hasNewPath); //setup all actions and their delegate functions BehaviorAction moveToCell = new BehaviorAction(moveTowardsCell); From 0463d4fe69a6571ec84975e5614114583af5a99b Mon Sep 17 00:00:00 2001 From: igrir Date: Fri, 22 May 2015 11:05:55 +0700 Subject: [PATCH 27/27] Change the way running node executed. Well.. I think it's debug --- .../Components/Actions/BehaviorAction.cs | 0 .../Components/BehaviorComponent.cs | 3 +- .../Components/Composites/IndexSelector.cs | 0 .../Components/Composites/PartialSelector.cs | 0 .../Components/Composites/PartialSequence.cs | 0 .../Components/Composites/RandomSelector.cs | 98 ++++++++++---- .../Components/Composites/Selector.cs | 99 ++++++++++---- .../Components/Composites/Sequence.cs | 124 +++++++++++++----- .../Components/Composites/StatefulSelector.cs | 0 .../Components/Composites/StatefulSequence.cs | 0 .../Components/Conditionals/Conditional.cs | 0 11 files changed, 237 insertions(+), 87 deletions(-) mode change 100644 => 100755 BehaviorLibrary/Components/Actions/BehaviorAction.cs mode change 100644 => 100755 BehaviorLibrary/Components/BehaviorComponent.cs mode change 100644 => 100755 BehaviorLibrary/Components/Composites/IndexSelector.cs mode change 100644 => 100755 BehaviorLibrary/Components/Composites/PartialSelector.cs mode change 100644 => 100755 BehaviorLibrary/Components/Composites/PartialSequence.cs mode change 100644 => 100755 BehaviorLibrary/Components/Composites/RandomSelector.cs mode change 100644 => 100755 BehaviorLibrary/Components/Composites/Selector.cs mode change 100644 => 100755 BehaviorLibrary/Components/Composites/Sequence.cs mode change 100644 => 100755 BehaviorLibrary/Components/Composites/StatefulSelector.cs mode change 100644 => 100755 BehaviorLibrary/Components/Composites/StatefulSequence.cs mode change 100644 => 100755 BehaviorLibrary/Components/Conditionals/Conditional.cs diff --git a/BehaviorLibrary/Components/Actions/BehaviorAction.cs b/BehaviorLibrary/Components/Actions/BehaviorAction.cs old mode 100644 new mode 100755 diff --git a/BehaviorLibrary/Components/BehaviorComponent.cs b/BehaviorLibrary/Components/BehaviorComponent.cs old mode 100644 new mode 100755 index ee31e8b..92b5c1c --- a/BehaviorLibrary/Components/BehaviorComponent.cs +++ b/BehaviorLibrary/Components/BehaviorComponent.cs @@ -7,7 +7,8 @@ namespace BehaviorLibrary.Components { public abstract class BehaviorComponent { - protected BehaviorReturnCode ReturnCode; +// protected BehaviorReturnCode ReturnCode; + public BehaviorReturnCode ReturnCode { get; protected set; } public BehaviorComponent() { } diff --git a/BehaviorLibrary/Components/Composites/IndexSelector.cs b/BehaviorLibrary/Components/Composites/IndexSelector.cs old mode 100644 new mode 100755 diff --git a/BehaviorLibrary/Components/Composites/PartialSelector.cs b/BehaviorLibrary/Components/Composites/PartialSelector.cs old mode 100644 new mode 100755 diff --git a/BehaviorLibrary/Components/Composites/PartialSequence.cs b/BehaviorLibrary/Components/Composites/PartialSequence.cs old mode 100644 new mode 100755 diff --git a/BehaviorLibrary/Components/Composites/RandomSelector.cs b/BehaviorLibrary/Components/Composites/RandomSelector.cs old mode 100644 new mode 100755 index 17f8cf3..cf746c0 --- a/BehaviorLibrary/Components/Composites/RandomSelector.cs +++ b/BehaviorLibrary/Components/Composites/RandomSelector.cs @@ -31,34 +31,78 @@ public RandomSelector(params BehaviorComponent[] behaviors) /// the behaviors return code public override BehaviorReturnCode Behave() { - _Random = new Random(DateTime.Now.Millisecond); + + //if it's running, then just run the running node + if (ReturnCode == BehaviorReturnCode.Running) { + + //search the running node + for (int i = 0; i < _Behaviors.Length; i++) + { + + + if (_Behaviors[i].ReturnCode == BehaviorReturnCode.Running) { + try + { + switch (_Behaviors[i].Behave()) + { + case BehaviorReturnCode.Failure: + continue; + case BehaviorReturnCode.Success: + ReturnCode = BehaviorReturnCode.Success; + return ReturnCode; + case BehaviorReturnCode.Running: + ReturnCode = BehaviorReturnCode.Running; + return ReturnCode; + default: + continue; + } + } + catch (Exception e) + { + #if DEBUG + Console.Error.WriteLine(e.ToString()); + #endif + continue; + } + } + + + } + + }else{ + _Random = new Random(DateTime.Now.Millisecond); + + try + { + switch (_Behaviors[_Random.Next(0, _Behaviors.Length)].Behave()) + { + case BehaviorReturnCode.Failure: + ReturnCode = BehaviorReturnCode.Failure; + return ReturnCode; + case BehaviorReturnCode.Success: + ReturnCode = BehaviorReturnCode.Success; + return ReturnCode; + case BehaviorReturnCode.Running: + ReturnCode = BehaviorReturnCode.Running; + return ReturnCode; + default: + ReturnCode = BehaviorReturnCode.Failure; + return ReturnCode; + } + } + catch (Exception e) + { + #if DEBUG + Console.Error.WriteLine(e.ToString()); + #endif + ReturnCode = BehaviorReturnCode.Failure; + return ReturnCode; + } + } + + ReturnCode = BehaviorReturnCode.Failure; + return ReturnCode; - try - { - switch (_Behaviors[_Random.Next(0, _Behaviors.Length)].Behave()) - { - case BehaviorReturnCode.Failure: - ReturnCode = BehaviorReturnCode.Failure; - return ReturnCode; - case BehaviorReturnCode.Success: - ReturnCode = BehaviorReturnCode.Success; - return ReturnCode; - case BehaviorReturnCode.Running: - ReturnCode = BehaviorReturnCode.Running; - return ReturnCode; - default: - ReturnCode = BehaviorReturnCode.Failure; - return ReturnCode; - } - } - catch (Exception e) - { -#if DEBUG - Console.Error.WriteLine(e.ToString()); -#endif - ReturnCode = BehaviorReturnCode.Failure; - return ReturnCode; - } } } } diff --git a/BehaviorLibrary/Components/Composites/Selector.cs b/BehaviorLibrary/Components/Composites/Selector.cs old mode 100644 new mode 100755 index 16a4daf..d6c385f --- a/BehaviorLibrary/Components/Composites/Selector.cs +++ b/BehaviorLibrary/Components/Composites/Selector.cs @@ -31,35 +31,78 @@ public Selector(params BehaviorComponent[] behaviors) public override BehaviorReturnCode Behave() { - for (int i = 0; i < _Behaviors.Length; i++) - { - try - { - switch (_Behaviors[i].Behave()) - { - case BehaviorReturnCode.Failure: - continue; - case BehaviorReturnCode.Success: - ReturnCode = BehaviorReturnCode.Success; - return ReturnCode; - case BehaviorReturnCode.Running: - ReturnCode = BehaviorReturnCode.Running; - return ReturnCode; - default: - continue; - } - } - catch (Exception e) - { -#if DEBUG - Console.Error.WriteLine(e.ToString()); -#endif - continue; - } - } + //if it's running, then just run the running node + if (ReturnCode == BehaviorReturnCode.Running) { - ReturnCode = BehaviorReturnCode.Failure; - return ReturnCode; + //search the running node + for (int i = 0; i < _Behaviors.Length; i++) + { + + + if (_Behaviors[i].ReturnCode == BehaviorReturnCode.Running) { + try + { + switch (_Behaviors[i].Behave()) + { + case BehaviorReturnCode.Failure: + continue; + case BehaviorReturnCode.Success: + ReturnCode = BehaviorReturnCode.Success; + return ReturnCode; + case BehaviorReturnCode.Running: + ReturnCode = BehaviorReturnCode.Running; + return ReturnCode; + default: + continue; + } + } + catch (Exception e) + { + #if DEBUG + Console.Error.WriteLine(e.ToString()); + #endif + continue; + } + } + + + } + + }else{ + + for (int i = 0; i < _Behaviors.Length; i++) + { + try + { + switch (_Behaviors[i].Behave()) + { + case BehaviorReturnCode.Failure: + continue; + case BehaviorReturnCode.Success: + ReturnCode = BehaviorReturnCode.Success; + return ReturnCode; + case BehaviorReturnCode.Running: + ReturnCode = BehaviorReturnCode.Running; + return ReturnCode; + default: + continue; + } + } + catch (Exception e) + { + #if DEBUG + Console.Error.WriteLine(e.ToString()); + #endif + continue; + } + } + + + } + + ReturnCode = BehaviorReturnCode.Failure; + return ReturnCode; + } } } diff --git a/BehaviorLibrary/Components/Composites/Sequence.cs b/BehaviorLibrary/Components/Composites/Sequence.cs old mode 100644 new mode 100755 index 6339241..936c2b5 --- a/BehaviorLibrary/Components/Composites/Sequence.cs +++ b/BehaviorLibrary/Components/Composites/Sequence.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Linq; using System.Text; +using UnityEngine; namespace BehaviorLibrary.Components.Composites { @@ -28,41 +29,102 @@ public Sequence(params BehaviorComponent[] behaviors) /// the behaviors return code public override BehaviorReturnCode Behave() { - //add watch for any running behaviors - bool anyRunning = false; - for(int i = 0; i < _behaviors.Length;i++) - { - try - { - switch (_behaviors[i].Behave()) - { - case BehaviorReturnCode.Failure: - ReturnCode = BehaviorReturnCode.Failure; - return ReturnCode; - case BehaviorReturnCode.Success: - continue; - case BehaviorReturnCode.Running: - anyRunning = true; - continue; - default: - ReturnCode = BehaviorReturnCode.Success; - return ReturnCode; - } - } - catch (Exception e) - { + if (ReturnCode == BehaviorReturnCode.Running) { + //search the running node + + //add watch for any running behaviors + bool anyRunning = false; + for (int i = 0; i < _behaviors.Length; i++) + { + if (_behaviors[i].ReturnCode == BehaviorReturnCode.Running) { + try + { + switch (_behaviors[i].Behave()) + { + case BehaviorReturnCode.Failure: + ReturnCode = BehaviorReturnCode.Failure; + return ReturnCode; + case BehaviorReturnCode.Success: + continue; + case BehaviorReturnCode.Running: + anyRunning = true; + //TODO: GiriDebug + ReturnCode = BehaviorReturnCode.Running; + return ReturnCode; + + continue; + default: + ReturnCode = BehaviorReturnCode.Success; + return ReturnCode; + } + } + catch (Exception e) + { #if DEBUG - Console.Error.WriteLine(e.ToString()); + Console.Error.WriteLine(e.ToString()); #endif - ReturnCode = BehaviorReturnCode.Failure; - return ReturnCode; - } - } + ReturnCode = BehaviorReturnCode.Failure; + return ReturnCode; + } + + //if none running, return success, otherwise return running + ReturnCode = !anyRunning ? BehaviorReturnCode.Success : BehaviorReturnCode.Running; + return ReturnCode; + } + + } + //if none running, return success, otherwise return running + ReturnCode = !anyRunning ? BehaviorReturnCode.Success : BehaviorReturnCode.Running; + return ReturnCode; + + }else{ + + + //add watch for any running behaviors + bool anyRunning = false; + + for(int i = 0; i < _behaviors.Length;i++) + { + + try + { + switch (_behaviors[i].Behave()) + { + case BehaviorReturnCode.Failure: + ReturnCode = BehaviorReturnCode.Failure; + return ReturnCode; + case BehaviorReturnCode.Success: + continue; + case BehaviorReturnCode.Running: + anyRunning = true; + //TODO: GiriDebug + ReturnCode = BehaviorReturnCode.Running; + return ReturnCode; + + continue; + default: + ReturnCode = BehaviorReturnCode.Success; + return ReturnCode; + } + } + catch (Exception e) + { + #if DEBUG + Console.Error.WriteLine(e.ToString()); + #endif + ReturnCode = BehaviorReturnCode.Failure; + return ReturnCode; + } + } + + //if none running, return success, otherwise return running + ReturnCode = !anyRunning ? BehaviorReturnCode.Success : BehaviorReturnCode.Running; + return ReturnCode; + + } + - //if none running, return success, otherwise return running - ReturnCode = !anyRunning ? BehaviorReturnCode.Success : BehaviorReturnCode.Running; - return ReturnCode; } diff --git a/BehaviorLibrary/Components/Composites/StatefulSelector.cs b/BehaviorLibrary/Components/Composites/StatefulSelector.cs old mode 100644 new mode 100755 diff --git a/BehaviorLibrary/Components/Composites/StatefulSequence.cs b/BehaviorLibrary/Components/Composites/StatefulSequence.cs old mode 100644 new mode 100755 diff --git a/BehaviorLibrary/Components/Conditionals/Conditional.cs b/BehaviorLibrary/Components/Conditionals/Conditional.cs old mode 100644 new mode 100755