├── .gitignore ├── BehaviorLibrary.sln ├── BehaviorLibrary ├── Behavior.cs ├── BehaviorLibrary.csproj ├── Components │ ├── Actions │ │ └── BehaviorAction.cs │ ├── BehaviorComponent.cs │ ├── Composites │ │ ├── ParallelSelector.cs │ │ ├── ParallelSequence.cs │ │ ├── RandomSelector.cs │ │ ├── RootSelector.cs │ │ ├── Selector.cs │ │ └── Sequence.cs │ ├── Conditionals │ │ └── Conditional.cs │ └── Decorators │ │ ├── Counter.cs │ │ ├── Inverter.cs │ │ ├── RandomDecorator.cs │ │ └── Timer.cs └── Properties │ └── AssemblyInfo.cs ├── README.md └── license.txt /.gitignore: -------------------------------------------------------------------------------- 1 | ################# 2 | ## Eclipse 3 | ################# 4 | 5 | *.pydevproject 6 | .project 7 | .metadata 8 | bin/ 9 | tmp/ 10 | *.tmp 11 | *.bak 12 | *.swp 13 | *~.nib 14 | local.properties 15 | .classpath 16 | .settings/ 17 | .loadpath 18 | 19 | # External tool builders 20 | .externalToolBuilders/ 21 | 22 | # Locally stored "Eclipse launch configurations" 23 | *.launch 24 | 25 | # CDT-specific 26 | .cproject 27 | 28 | # PDT-specific 29 | .buildpath 30 | 31 | 32 | ################# 33 | ## Visual Studio 34 | ################# 35 | 36 | ## Ignore Visual Studio temporary files, build results, and 37 | ## files generated by popular Visual Studio add-ons. 38 | 39 | # User-specific files 40 | *.suo 41 | *.user 42 | *.sln.docstates 43 | 44 | # Build results 45 | [Dd]ebug/ 46 | [Rr]elease/ 47 | *_i.c 48 | *_p.c 49 | *.ilk 50 | *.meta 51 | *.obj 52 | *.pch 53 | *.pdb 54 | *.pgc 55 | *.pgd 56 | *.rsp 57 | *.sbr 58 | *.tlb 59 | *.tli 60 | *.tlh 61 | *.tmp 62 | *.vspscc 63 | .builds 64 | *.dotCover 65 | 66 | ## TODO: If you have NuGet Package Restore enabled, uncomment this 67 | #packages/ 68 | 69 | # Visual C++ cache files 70 | ipch/ 71 | *.aps 72 | *.ncb 73 | *.opensdf 74 | *.sdf 75 | 76 | # Visual Studio profiler 77 | *.psess 78 | *.vsp 79 | 80 | # ReSharper is a .NET coding add-in 81 | _ReSharper* 82 | 83 | # Installshield output folder 84 | [Ee]xpress 85 | 86 | # DocProject is a documentation generator add-in 87 | DocProject/buildhelp/ 88 | DocProject/Help/*.HxT 89 | DocProject/Help/*.HxC 90 | DocProject/Help/*.hhc 91 | DocProject/Help/*.hhk 92 | DocProject/Help/*.hhp 93 | DocProject/Help/Html2 94 | DocProject/Help/html 95 | 96 | # Click-Once directory 97 | publish 98 | 99 | # Others 100 | [Bb]in 101 | [Oo]bj 102 | sql 103 | TestResults 104 | *.Cache 105 | ClientBin 106 | stylecop.* 107 | ~$* 108 | *.dbmdl 109 | Generated_Code #added for RIA/Silverlight projects 110 | 111 | # Backup & report files from converting an old project file to a newer 112 | # Visual Studio version. Backup files are not needed, because we have git ;-) 113 | _UpgradeReport_Files/ 114 | Backup*/ 115 | UpgradeLog*.XML 116 | 117 | 118 | 119 | ############ 120 | ## Windows 121 | ############ 122 | 123 | # Windows image file caches 124 | Thumbs.db 125 | 126 | # Folder config file 127 | Desktop.ini 128 | 129 | 130 | ############# 131 | ## Python 132 | ############# 133 | 134 | *.py[co] 135 | 136 | # Packages 137 | *.egg 138 | *.egg-info 139 | dist 140 | build 141 | eggs 142 | parts 143 | bin 144 | var 145 | sdist 146 | develop-eggs 147 | .installed.cfg 148 | 149 | # Installer logs 150 | pip-log.txt 151 | 152 | # Unit test / coverage reports 153 | .coverage 154 | .tox 155 | 156 | #Translations 157 | *.mo 158 | 159 | #Mr Developer 160 | .mr.developer.cfg 161 | 162 | # Mac crap 163 | .DS_Store 164 | -------------------------------------------------------------------------------- /BehaviorLibrary.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 11.00 3 | # Visual Studio 2010 4 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BehaviorLibrary", "BehaviorLibrary\BehaviorLibrary.csproj", "{CC824B6F-6145-485F-9604-FB94F0ECACA7}" 5 | EndProject 6 | Global 7 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 8 | Debug|Any CPU = Debug|Any CPU 9 | Release|Any CPU = Release|Any CPU 10 | EndGlobalSection 11 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 12 | {CC824B6F-6145-485F-9604-FB94F0ECACA7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 13 | {CC824B6F-6145-485F-9604-FB94F0ECACA7}.Debug|Any CPU.Build.0 = Debug|Any CPU 14 | {CC824B6F-6145-485F-9604-FB94F0ECACA7}.Release|Any CPU.ActiveCfg = Release|Any CPU 15 | {CC824B6F-6145-485F-9604-FB94F0ECACA7}.Release|Any CPU.Build.0 = Release|Any CPU 16 | EndGlobalSection 17 | GlobalSection(SolutionProperties) = preSolution 18 | HideSolutionNode = FALSE 19 | EndGlobalSection 20 | EndGlobal 21 | -------------------------------------------------------------------------------- /BehaviorLibrary/Behavior.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using BehaviorLibrary.Components; 5 | using BehaviorLibrary.Components.Composites; 6 | 7 | namespace BehaviorLibrary 8 | { 9 | public enum BehaviorReturnCode 10 | { 11 | Failure, 12 | Success, 13 | Running 14 | } 15 | 16 | public delegate BehaviorReturnCode BehaviorReturn(); 17 | 18 | /// 19 | /// 20 | /// 21 | public class Behavior 22 | { 23 | 24 | private RootSelector b_Root; 25 | 26 | private BehaviorReturnCode b_ReturnCode; 27 | 28 | public BehaviorReturnCode ReturnCode 29 | { 30 | get { return b_ReturnCode; } 31 | set { b_ReturnCode = value; } 32 | } 33 | 34 | /// 35 | /// 36 | /// 37 | /// 38 | public Behavior(RootSelector root) 39 | { 40 | b_Root = root; 41 | } 42 | 43 | /// 44 | /// perform the behavior 45 | /// 46 | public BehaviorReturnCode Behave() 47 | { 48 | try 49 | { 50 | switch (b_Root.Behave()) 51 | { 52 | case BehaviorReturnCode.Failure: 53 | ReturnCode = BehaviorReturnCode.Failure; 54 | return ReturnCode; 55 | case BehaviorReturnCode.Success: 56 | ReturnCode = BehaviorReturnCode.Success; 57 | return ReturnCode; 58 | case BehaviorReturnCode.Running: 59 | ReturnCode = BehaviorReturnCode.Running; 60 | return ReturnCode; 61 | default: 62 | ReturnCode = BehaviorReturnCode.Running; 63 | return ReturnCode; 64 | } 65 | } 66 | catch (Exception e) 67 | { 68 | #if DEBUG 69 | Console.Error.WriteLine(e.ToString()); 70 | #endif 71 | ReturnCode = BehaviorReturnCode.Failure; 72 | return ReturnCode; 73 | } 74 | } 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /BehaviorLibrary/BehaviorLibrary.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | Debug 5 | AnyCPU 6 | 8.0.30703 7 | 2.0 8 | {CC824B6F-6145-485F-9604-FB94F0ECACA7} 9 | Library 10 | Properties 11 | BehaviorLibrary 12 | BehaviorLibrary 13 | 512 14 | 15 | 16 | true 17 | full 18 | false 19 | bin\Debug\ 20 | DEBUG;TRACE 21 | prompt 22 | 4 23 | false 24 | 25 | 26 | pdbonly 27 | true 28 | bin\Release\ 29 | TRACE 30 | prompt 31 | 4 32 | false 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 69 | -------------------------------------------------------------------------------- /BehaviorLibrary/Components/Actions/BehaviorAction.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | 6 | namespace BehaviorLibrary.Components.Actions 7 | { 8 | public class BehaviorAction : BehaviorComponent 9 | { 10 | 11 | private Func ba_Action; 12 | 13 | public BehaviorAction() { } 14 | 15 | public BehaviorAction(Func action) 16 | { 17 | ba_Action = action; 18 | } 19 | 20 | public override BehaviorReturnCode Behave() 21 | { 22 | try 23 | { 24 | switch (ba_Action.Invoke()) 25 | { 26 | case BehaviorReturnCode.Success: 27 | ReturnCode = BehaviorReturnCode.Success; 28 | return ReturnCode; 29 | case BehaviorReturnCode.Failure: 30 | ReturnCode = BehaviorReturnCode.Failure; 31 | return ReturnCode; 32 | case BehaviorReturnCode.Running: 33 | ReturnCode = BehaviorReturnCode.Running; 34 | return ReturnCode; 35 | default: 36 | ReturnCode = BehaviorReturnCode.Failure; 37 | return ReturnCode; 38 | } 39 | } 40 | catch (Exception e) 41 | { 42 | #if DEBUG 43 | Console.Error.WriteLine(e.ToString()); 44 | #endif 45 | ReturnCode = BehaviorReturnCode.Failure; 46 | return ReturnCode; 47 | } 48 | } 49 | 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /BehaviorLibrary/Components/BehaviorComponent.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | 6 | namespace BehaviorLibrary.Components 7 | { 8 | public abstract class BehaviorComponent 9 | { 10 | protected BehaviorReturnCode ReturnCode; 11 | 12 | public BehaviorComponent() { } 13 | 14 | public abstract BehaviorReturnCode Behave(); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /BehaviorLibrary/Components/Composites/ParallelSelector.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | 6 | namespace BehaviorLibrary.Components.Composites 7 | { 8 | public class ParallelSelector : BehaviorComponent 9 | { 10 | 11 | protected BehaviorComponent[] p_Behaviors; 12 | 13 | private short p_Selections = 0; 14 | 15 | private short p_SelLength = 0; 16 | 17 | /// 18 | /// Selects among the given behavior components 19 | /// Performs an OR-Like behavior and will "fail-over" to each successive component until Success is reached or Failure is certain 20 | /// -Returns Success if a behavior component returns Success 21 | /// -Returns Running if a behavior component returns Running 22 | /// -Returns Failure if all behavior components returned Failure 23 | /// 24 | /// one to many behavior components 25 | public ParallelSelector(params BehaviorComponent[] behaviors) 26 | { 27 | p_Behaviors = behaviors; 28 | p_SelLength = (short)p_Behaviors.Length; 29 | } 30 | 31 | /// 32 | /// performs the given behavior 33 | /// 34 | /// the behaviors return code 35 | public override BehaviorReturnCode Behave() 36 | { 37 | 38 | for (int i = 0; i < p_SelLength; i++) 39 | { 40 | try 41 | { 42 | switch (p_Behaviors[i].Behave()) 43 | { 44 | case BehaviorReturnCode.Failure: 45 | continue; 46 | case BehaviorReturnCode.Success: 47 | ReturnCode = BehaviorReturnCode.Success; 48 | return ReturnCode; 49 | case BehaviorReturnCode.Running: 50 | ReturnCode = BehaviorReturnCode.Running; 51 | return ReturnCode; 52 | default: 53 | continue; 54 | } 55 | } 56 | catch (Exception e) 57 | { 58 | #if DEBUG 59 | Console.Error.WriteLine(e.ToString()); 60 | #endif 61 | continue; 62 | } 63 | } 64 | 65 | 66 | /* 67 | while (p_Selections < p_SelLength) 68 | { 69 | try 70 | { 71 | switch (p_Behaviors[p_Selections].Behave()) 72 | { 73 | case BehaviorReturnCode.Failure: 74 | p_Selections++; 75 | continue; 76 | case BehaviorReturnCode.Success: 77 | p_Selections = 0; 78 | ReturnCode = BehaviorReturnCode.Success; 79 | return ReturnCode; 80 | case BehaviorReturnCode.Running: 81 | ReturnCode = BehaviorReturnCode.Running; 82 | return ReturnCode; 83 | default: 84 | p_Selections++; 85 | continue; 86 | } 87 | } 88 | catch (Exception) 89 | { 90 | p_Selections++; 91 | continue; 92 | } 93 | }*/ 94 | 95 | p_Selections = 0; 96 | ReturnCode = BehaviorReturnCode.Failure; 97 | return ReturnCode; 98 | } 99 | } 100 | } 101 | -------------------------------------------------------------------------------- /BehaviorLibrary/Components/Composites/ParallelSequence.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | 6 | namespace BehaviorLibrary.Components.Composites 7 | { 8 | public class ParallelSequence : BehaviorComponent 9 | { 10 | 11 | private BehaviorComponent[] p_Behaviors; 12 | 13 | /// 14 | /// attempts to run the behaviors all in one cycle 15 | /// -Returns Success when all are successful 16 | /// -Returns Failure if one behavior fails or an error occurs 17 | /// -Does not Return Running 18 | /// 19 | /// 20 | public ParallelSequence(params BehaviorComponent[] behaviors) 21 | { 22 | p_Behaviors = behaviors; 23 | } 24 | 25 | /// 26 | /// performs the given behavior 27 | /// 28 | /// the behaviors return code 29 | public override BehaviorReturnCode Behave() 30 | { 31 | 32 | for(int i = 0; i < p_Behaviors.Length;i++) 33 | { 34 | try 35 | { 36 | switch (p_Behaviors[i].Behave()) 37 | { 38 | case BehaviorReturnCode.Failure: 39 | ReturnCode = BehaviorReturnCode.Failure; 40 | return ReturnCode; 41 | case BehaviorReturnCode.Success: 42 | continue; 43 | case BehaviorReturnCode.Running: 44 | continue; 45 | default: 46 | ReturnCode = BehaviorReturnCode.Success; 47 | return ReturnCode; 48 | } 49 | } 50 | catch (Exception e) 51 | { 52 | #if DEBUG 53 | Console.Error.WriteLine(e.ToString()); 54 | #endif 55 | ReturnCode = BehaviorReturnCode.Failure; 56 | return ReturnCode; 57 | } 58 | } 59 | 60 | ReturnCode = BehaviorReturnCode.Success; 61 | return ReturnCode; 62 | } 63 | 64 | 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /BehaviorLibrary/Components/Composites/RandomSelector.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | 6 | namespace BehaviorLibrary.Components.Composites 7 | { 8 | public class RandomSelector : BehaviorComponent 9 | { 10 | 11 | private BehaviorComponent[] r_Behaviors; 12 | 13 | //use current milliseconds to set random seed 14 | private Random r_Random = new Random(DateTime.Now.Millisecond); 15 | 16 | /// 17 | /// Randomly selects and performs one of the passed behaviors 18 | /// -Returns Success if selected behavior returns Success 19 | /// -Returns Failure if selected behavior returns Failure 20 | /// -Returns Running if selected behavior returns Running 21 | /// 22 | /// one to many behavior components 23 | public RandomSelector(params BehaviorComponent[] behaviors) 24 | { 25 | r_Behaviors = behaviors; 26 | } 27 | 28 | /// 29 | /// performs the given behavior 30 | /// 31 | /// the behaviors return code 32 | public override BehaviorReturnCode Behave() 33 | { 34 | r_Random = new Random(DateTime.Now.Millisecond); 35 | 36 | try 37 | { 38 | switch (r_Behaviors[r_Random.Next(0, r_Behaviors.Length - 1)].Behave()) 39 | { 40 | case BehaviorReturnCode.Failure: 41 | ReturnCode = BehaviorReturnCode.Failure; 42 | return ReturnCode; 43 | case BehaviorReturnCode.Success: 44 | ReturnCode = BehaviorReturnCode.Success; 45 | return ReturnCode; 46 | case BehaviorReturnCode.Running: 47 | ReturnCode = BehaviorReturnCode.Running; 48 | return ReturnCode; 49 | default: 50 | ReturnCode = BehaviorReturnCode.Failure; 51 | return ReturnCode; 52 | } 53 | } 54 | catch (Exception e) 55 | { 56 | #if DEBUG 57 | Console.Error.WriteLine(e.ToString()); 58 | #endif 59 | ReturnCode = BehaviorReturnCode.Failure; 60 | return ReturnCode; 61 | } 62 | } 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /BehaviorLibrary/Components/Composites/RootSelector.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | 6 | namespace BehaviorLibrary.Components.Composites 7 | { 8 | public class RootSelector : Selector 9 | { 10 | 11 | private BehaviorComponent[] rs_Behaviors; 12 | 13 | private Func rs_Index; 14 | 15 | /// 16 | /// The selector for the root node of the behavior tree 17 | /// 18 | /// an index representing which of the behavior branches to perform 19 | /// the behavior branches to be selected from 20 | public RootSelector(Func index, params BehaviorComponent[] behaviors) 21 | { 22 | rs_Index = index; 23 | rs_Behaviors = behaviors; 24 | } 25 | 26 | /// 27 | /// performs the given behavior 28 | /// 29 | /// the behaviors return code 30 | public override BehaviorReturnCode Behave() 31 | { 32 | try 33 | { 34 | switch (rs_Behaviors[rs_Index.Invoke()].Behave()) 35 | { 36 | case BehaviorReturnCode.Failure: 37 | ReturnCode = BehaviorReturnCode.Failure; 38 | return ReturnCode; 39 | case BehaviorReturnCode.Success: 40 | ReturnCode = BehaviorReturnCode.Success; 41 | return ReturnCode; 42 | case BehaviorReturnCode.Running: 43 | ReturnCode = BehaviorReturnCode.Running; 44 | return ReturnCode; 45 | default: 46 | ReturnCode = BehaviorReturnCode.Running; 47 | return ReturnCode; 48 | } 49 | } 50 | catch (Exception e) 51 | { 52 | #if DEBUG 53 | Console.Error.WriteLine(e.ToString()); 54 | #endif 55 | ReturnCode = BehaviorReturnCode.Failure; 56 | return ReturnCode; 57 | } 58 | } 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /BehaviorLibrary/Components/Composites/Selector.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | 6 | namespace BehaviorLibrary.Components.Composites 7 | { 8 | public class Selector : BehaviorComponent 9 | { 10 | 11 | protected BehaviorComponent[] s_Behaviors; 12 | 13 | private short selections = 0; 14 | 15 | private short selLength = 0; 16 | 17 | /// 18 | /// Selects among the given behavior components 19 | /// Performs an OR-Like behavior and will "fail-over" to each successive component until Success is reached or Failure is certain 20 | /// -Returns Success if a behavior component returns Success 21 | /// -Returns Running if a behavior component returns Failure or Running 22 | /// -Returns Failure if all behavior components returned Failure or an error has occured 23 | /// 24 | /// one to many behavior components 25 | public Selector(params BehaviorComponent[] behaviors) 26 | { 27 | s_Behaviors = behaviors; 28 | selLength = (short)s_Behaviors.Length; 29 | } 30 | 31 | /// 32 | /// performs the given behavior 33 | /// 34 | /// the behaviors return code 35 | public override BehaviorReturnCode Behave() 36 | { 37 | while (selections < selLength) 38 | { 39 | try 40 | { 41 | switch (s_Behaviors[selections].Behave()) 42 | { 43 | case BehaviorReturnCode.Failure: 44 | selections++; 45 | ReturnCode = BehaviorReturnCode.Running; 46 | return ReturnCode; 47 | case BehaviorReturnCode.Success: 48 | selections = 0; 49 | ReturnCode = BehaviorReturnCode.Success; 50 | return ReturnCode; 51 | case BehaviorReturnCode.Running: 52 | ReturnCode = BehaviorReturnCode.Running; 53 | return ReturnCode; 54 | default: 55 | selections++; 56 | ReturnCode = BehaviorReturnCode.Failure; 57 | return ReturnCode; 58 | } 59 | } 60 | catch (Exception e) 61 | { 62 | #if DEBUG 63 | Console.Error.WriteLine(e.ToString()); 64 | #endif 65 | selections++; 66 | ReturnCode = BehaviorReturnCode.Failure; 67 | return ReturnCode; 68 | } 69 | } 70 | 71 | selections = 0; 72 | ReturnCode = BehaviorReturnCode.Failure; 73 | return ReturnCode; 74 | } 75 | 76 | 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /BehaviorLibrary/Components/Composites/Sequence.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | 6 | namespace BehaviorLibrary.Components.Composites 7 | { 8 | public class Sequence : BehaviorComponent 9 | { 10 | 11 | protected BehaviorComponent[] s_Behaviors; 12 | 13 | private short sequence = 0; 14 | 15 | private short seqLength = 0; 16 | 17 | /// 18 | /// Performs the given behavior components sequentially 19 | /// Performs an AND-Like behavior and will perform each successive component 20 | /// -Returns Success if all behavior components return Success 21 | /// -Returns Running if an individual behavior component returns Success or Running 22 | /// -Returns Failure if a behavior components returns Failure or an error is encountered 23 | /// 24 | /// one to many behavior components 25 | public Sequence(params BehaviorComponent[] behaviors) 26 | { 27 | s_Behaviors = behaviors; 28 | seqLength = (short) s_Behaviors.Length; 29 | } 30 | 31 | /// 32 | /// performs the given behavior 33 | /// 34 | /// the behaviors return code 35 | public override BehaviorReturnCode Behave() 36 | { 37 | //while you can go through them, do so 38 | while (sequence < seqLength) 39 | { 40 | try 41 | { 42 | switch (s_Behaviors[sequence].Behave()) 43 | { 44 | case BehaviorReturnCode.Failure: 45 | sequence = 0; 46 | ReturnCode = BehaviorReturnCode.Failure; 47 | return ReturnCode; 48 | case BehaviorReturnCode.Success: 49 | sequence++; 50 | ReturnCode = BehaviorReturnCode.Running; 51 | return ReturnCode; 52 | case BehaviorReturnCode.Running: 53 | ReturnCode = BehaviorReturnCode.Running; 54 | return ReturnCode; 55 | } 56 | } 57 | catch (Exception e) 58 | { 59 | #if DEBUG 60 | Console.Error.WriteLine(e.ToString()); 61 | #endif 62 | sequence = 0; 63 | ReturnCode = BehaviorReturnCode.Failure; 64 | return ReturnCode; 65 | } 66 | 67 | } 68 | 69 | sequence = 0; 70 | ReturnCode = BehaviorReturnCode.Success; 71 | return ReturnCode; 72 | 73 | } 74 | 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /BehaviorLibrary/Components/Conditionals/Conditional.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | 6 | namespace BehaviorLibrary.Components.Conditionals 7 | { 8 | public class Conditional : BehaviorComponent 9 | { 10 | 11 | private Func c_Bool; 12 | 13 | /// 14 | /// Returns a return code equivalent to the test 15 | /// -Returns Success if true 16 | /// -Returns Failure if false 17 | /// 18 | /// the value to be tested 19 | public Conditional(Func test) 20 | { 21 | c_Bool = test; 22 | } 23 | 24 | /// 25 | /// performs the given behavior 26 | /// 27 | /// the behaviors return code 28 | public override BehaviorReturnCode Behave() 29 | { 30 | 31 | try 32 | { 33 | switch (c_Bool.Invoke()) 34 | { 35 | case true: 36 | ReturnCode = BehaviorReturnCode.Success; 37 | return ReturnCode; 38 | case false: 39 | ReturnCode = BehaviorReturnCode.Failure; 40 | return ReturnCode; 41 | default: 42 | ReturnCode = BehaviorReturnCode.Failure; 43 | return ReturnCode; 44 | } 45 | } 46 | catch (Exception e) 47 | { 48 | #if DEBUG 49 | Console.Error.WriteLine(e.ToString()); 50 | #endif 51 | ReturnCode = BehaviorReturnCode.Failure; 52 | return ReturnCode; 53 | } 54 | } 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /BehaviorLibrary/Components/Decorators/Counter.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | 6 | namespace BehaviorLibrary.Components.Decorators 7 | { 8 | public class Counter : BehaviorComponent 9 | { 10 | private int c_MaxCount; 11 | private int c_Counter = 0; 12 | 13 | private BehaviorComponent c_Behavior; 14 | 15 | /// 16 | /// executes the behavior based on a counter 17 | /// -each time Counter is called the counter increments by 1 18 | /// -Counter executes the behavior when it reaches the supplied maxCount 19 | /// 20 | /// max number to count to 21 | /// behavior to run 22 | public Counter(int maxCount, BehaviorComponent behavior) 23 | { 24 | c_MaxCount = maxCount; 25 | c_Behavior = behavior; 26 | } 27 | 28 | /// 29 | /// performs the given behavior 30 | /// 31 | /// the behaviors return code 32 | public override BehaviorReturnCode Behave() 33 | { 34 | try 35 | { 36 | if (c_Counter < c_MaxCount) 37 | { 38 | c_Counter++; 39 | ReturnCode = BehaviorReturnCode.Running; 40 | return BehaviorReturnCode.Running; 41 | } 42 | else 43 | { 44 | c_Counter = 0; 45 | ReturnCode = c_Behavior.Behave(); 46 | return ReturnCode; 47 | } 48 | } 49 | catch (Exception e) 50 | { 51 | #if DEBUG 52 | Console.Error.WriteLine(e.ToString()); 53 | #endif 54 | ReturnCode = BehaviorReturnCode.Failure; 55 | return BehaviorReturnCode.Failure; 56 | } 57 | } 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /BehaviorLibrary/Components/Decorators/Inverter.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | 6 | namespace BehaviorLibrary.Components.Decorators 7 | { 8 | public class Inverter : BehaviorComponent 9 | { 10 | 11 | private BehaviorComponent d_Behavior; 12 | 13 | /// 14 | /// inverts the given behavior 15 | /// -Returns Success on Failure or Error 16 | /// -Returns Failure on Success 17 | /// -Returns Running on Running 18 | /// 19 | /// 20 | public Inverter(BehaviorComponent behavior) 21 | { 22 | d_Behavior = behavior; 23 | } 24 | 25 | /// 26 | /// performs the given behavior 27 | /// 28 | /// the behaviors return code 29 | public override BehaviorReturnCode Behave() 30 | { 31 | try 32 | { 33 | switch (d_Behavior.Behave()) 34 | { 35 | case BehaviorReturnCode.Failure: 36 | ReturnCode = BehaviorReturnCode.Success; 37 | return ReturnCode; 38 | case BehaviorReturnCode.Success: 39 | ReturnCode = BehaviorReturnCode.Failure; 40 | return ReturnCode; 41 | case BehaviorReturnCode.Running: 42 | ReturnCode = BehaviorReturnCode.Running; 43 | return ReturnCode; 44 | } 45 | } 46 | catch (Exception e) 47 | { 48 | #if DEBUG 49 | Console.Error.WriteLine(e.ToString()); 50 | #endif 51 | ReturnCode = BehaviorReturnCode.Success; 52 | return ReturnCode; 53 | } 54 | 55 | ReturnCode = BehaviorReturnCode.Success; 56 | return ReturnCode; 57 | 58 | } 59 | 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /BehaviorLibrary/Components/Decorators/RandomDecorator.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | 6 | namespace BehaviorLibrary.Components.Decorators 7 | { 8 | public class RandomDecorator : BehaviorComponent 9 | { 10 | 11 | private float r_Probability; 12 | 13 | private Func r_RandomFunction; 14 | 15 | private BehaviorComponent r_Behavior; 16 | 17 | /// 18 | /// randomly executes the behavior 19 | /// 20 | /// probability of execution 21 | /// function that determines probability to execute 22 | /// behavior to execute 23 | public RandomDecorator(float probability, Func randomFunction, BehaviorComponent behavior) 24 | { 25 | r_Probability = probability; 26 | r_RandomFunction = randomFunction; 27 | r_Behavior = behavior; 28 | } 29 | 30 | 31 | public override BehaviorReturnCode Behave() 32 | { 33 | try 34 | { 35 | if (r_RandomFunction.Invoke() <= r_Probability) 36 | { 37 | ReturnCode = r_Behavior.Behave(); 38 | return ReturnCode; 39 | } 40 | else 41 | { 42 | ReturnCode = BehaviorReturnCode.Running; 43 | return BehaviorReturnCode.Running; 44 | } 45 | } 46 | catch (Exception e) 47 | { 48 | #if DEBUG 49 | Console.Error.WriteLine(e.ToString()); 50 | #endif 51 | ReturnCode = BehaviorReturnCode.Failure; 52 | return BehaviorReturnCode.Failure; 53 | } 54 | } 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /BehaviorLibrary/Components/Decorators/Timer.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | 6 | namespace BehaviorLibrary.Components.Decorators 7 | { 8 | public class Timer : BehaviorComponent 9 | { 10 | 11 | private Func t_ElapsedTimeFunction; 12 | 13 | private BehaviorComponent t_Behavior; 14 | 15 | private int t_TimeElapsed = 0; 16 | 17 | private int t_WaitTime; 18 | 19 | /// 20 | /// executes the behavior after a given amount of time in miliseconds has passed 21 | /// 22 | /// function that returns elapsed time 23 | /// maximum time to wait before executing behavior 24 | /// behavior to run 25 | public Timer(Func elapsedTimeFunction, int timeToWait, BehaviorComponent behavior) 26 | { 27 | t_ElapsedTimeFunction = elapsedTimeFunction; 28 | t_Behavior = behavior; 29 | t_WaitTime = timeToWait; 30 | } 31 | 32 | /// 33 | /// performs the given behavior 34 | /// 35 | /// the behaviors return code 36 | public override BehaviorReturnCode Behave() 37 | { 38 | try 39 | { 40 | t_TimeElapsed += t_ElapsedTimeFunction.Invoke(); 41 | 42 | if (t_TimeElapsed >= t_WaitTime) 43 | { 44 | t_TimeElapsed = 0; 45 | ReturnCode = t_Behavior.Behave(); 46 | return ReturnCode; 47 | } 48 | else 49 | { 50 | ReturnCode = BehaviorReturnCode.Running; 51 | return BehaviorReturnCode.Running; 52 | } 53 | } 54 | catch (Exception e) 55 | { 56 | #if DEBUG 57 | Console.Error.WriteLine(e.ToString()); 58 | #endif 59 | ReturnCode = BehaviorReturnCode.Failure; 60 | return BehaviorReturnCode.Failure; 61 | } 62 | } 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /BehaviorLibrary/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("BehaviorLibrary")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("BehaviorLibrary")] 13 | [assembly: AssemblyCopyright("Copyright © 2012")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("981a6e5e-8ee8-4873-b97c-e71a0d7d0d7e")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Behavior Library 2 | ================ 3 | 4 | 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). 5 | 6 | It is simple to use and with that simplicity comes performance. 7 | 8 | Example of a simple A* following AI on a tilemap 9 | 10 | //setup all coniditionals and their delegate functions 11 | Coniditional tooClose = new Conditional(isTooClose); 12 | Coniditional targetMoved = new Conditional(hasTargetMoved); 13 | Coniditional pathFound = new Conditional(hasPathBeenFound); 14 | Coniditional reachedCell = new Conditional(hasReachedCell); 15 | Coniditional reachedTarget = new Conditional(hasReachedTarget); 16 | Coniditional isNewPath = new Conditional(hasNewPath); 17 | 18 | //setup all actions and their delegate functions 19 | BehaviorAction moveToCell = new BehaviorAction(moveTowardsCell); 20 | BehaviorAction calcPath = new BehaviorAction(calculatePath); 21 | BehaviorAction initPathfinder = new BehaviorAction(initializePathfinder); 22 | BehaviorAction getNextCell = new BehaviorAction(getNextPathCell); 23 | BehaviorAction setPath = new BehaviorAction(setNewPath); 24 | BehaviorAction getPath = new BehaviorAction(getCurrentPath); 25 | BehaviorAction updatePosition = new BehaviorAction(updateTargetPosision); 26 | BehaviorAction reset = new BehaviorAction(resetPathfinder); 27 | BehaviorAction animate = new BehaviorAction(updateAnimation); 28 | 29 | //setup an initilization branch 30 | ParallelSequence initialize = new ParallelSequence(initPathfinder, calcPath); 31 | 32 | //if the target has moved, reset and calculate a new path 33 | ParallelSelector ifMovedCreateNewPath = new ParallelSelector(new Inverter(targetMoved), new Inverter(reset), calcPath); 34 | ParallelSelector ifPathFoundGetPath = new ParallelSelector(new Inverter(pathFound), getPath); 35 | ParallelSelector ifPathNewUseIt = new ParallelSelector(new Inverter(isNewPath), setPath); 36 | ParallelSelector ifReachedCellGetNext = new ParallelSelector(new Inverter(reachedCell), getNextCell); 37 | ParallelSelector ifNotReachedTargetMoveTowardsCell = new ParallelSelector(reachedTarget, moveToCell); 38 | 39 | //follow target so long as you're not too close and then animate 40 | ParallelSequence follow = new ParallelSequence(new Inverter(tooClose), updatePosition, ifMovedCreateNewPath, ifPathFoundGetPath, ifPathIsNewUseIt, ifReachedCellGetNext, ifNotReachedTargetMoveTowardsCell, animate); 41 | 42 | //setup root node, choose initialization phase or pathing/movement phase 43 | RootSelector root = new RootSelector(switchBehaviors, initialize, follow); 44 | 45 | //set a reference to the root 46 | Behavior behavior = new Behavior(root); 47 | 48 | //to execute the behavior 49 | behavior.Behave(); 50 | 51 | -------------------------------------------------------------------------------- /license.txt: -------------------------------------------------------------------------------- 1 | Copyright (c) 2012, Thomas H. Jonell 2 | All rights reserved. 3 | 4 | Redistribution and use in source and binary forms, with or without 5 | modification, are permitted provided that the following conditions are met: 6 | 7 | 1. Redistributions of source code must retain the above copyright notice, this 8 | list of conditions and the following disclaimer. 9 | 2. Redistributions in binary form must reproduce the above copyright notice, 10 | this list of conditions and the following disclaimer in the documentation 11 | and/or other materials provided with the distribution. 12 | 13 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 14 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 15 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 16 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 17 | ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 18 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 19 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 20 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 21 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 22 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 23 | 24 | The views and conclusions contained in the software and documentation are those 25 | of the authors and should not be interpreted as representing official policies, 26 | either expressed or implied, of the FreeBSD Project. --------------------------------------------------------------------------------