Advertisement
Guest User

Behaviour Tree Loader

a guest
Oct 24th, 2014
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.73 KB | None | 0 0
  1. public class BehaviourTreeLoader : BaseInterpreter
  2. {
  3.     Stack<BehaviouTreeNode> defined;  
  4.     bool running = false;  
  5.  
  6.     public BehaviourTreeNode Run(byte[] code)  
  7.     {  
  8.         InitScript();  
  9.         running = true;  
  10.         while (running)  
  11.             NextCommand();  
  12.         return defined.Pop();  
  13.     }
  14.    
  15.     public void InitInsn()  
  16.     {  
  17.         Execute = new Action[]
  18.         {
  19.             Cmd00_End, Cmd01_Action, Cmd02_RunBehaviour,
  20.             Cmd03_Inverter, Cmd04_Succeeder, Cmd05_Repeater,
  21.             Cmd06_RepeatUntilFail, Cmd07_Sequence, Cmd08_Selector
  22.         };
  23.         Decompile = new Func<string>[]
  24.         {
  25.             Cmd00_Decompile, Cmd01_Decompile, Cmd02_Decompile,
  26.             Cmd03_Decompile, Cmd04_Decompile, Cmd05_Decompile,
  27.             Cmd06_Decompile, Cmd07_Decompile, Cmd08_Decompile
  28.         };
  29.     }
  30.    
  31.     #region Commands
  32.    
  33.     private void Cmd00_End()
  34.     {
  35.         running = false;
  36.     }
  37.    
  38.     private void Cmd01_Action()  
  39.     {  
  40.         defined.Push(new Action(DataBus.ReadInt32()));
  41.     }
  42.    
  43.     private void Cmd02_RunBehaviour()  
  44.     {  
  45.         defined.Push(new RunBehaviour(DataBus.ReadInt32()));
  46.     }
  47.    
  48.     private void Cmd03_Inverter()  
  49.     {  
  50.         defined.Push(new Inverter(defined.Pop()));
  51.     }
  52.    
  53.     private void Cmd04_Succeeder()  
  54.     {  
  55.         defined.Push(new Succeeder(defined.Pop()));
  56.     }
  57.    
  58.     private void Cmd05_Repeater()  
  59.     {  
  60.         defined.Push(new Repeater(defined.Pop()));
  61.     }
  62.    
  63.     private void Cmd06_RepeatUntilFail()  
  64.     {  
  65.         defined.Push(new RepeatUntilFail(defined.Pop()));
  66.     }
  67.    
  68.     private void Cmd07_Sequence()
  69.     {
  70.         BehaviourTreeNode[] children = new BehaviourTreeNode[DataBus.ReadInt32()];
  71.         for (int i = children.Length; i >= 0; i--)
  72.             children[i] = defined.Pop();
  73.         defined.Push(new Sequence(children));
  74.     }
  75.    
  76.     private void Cmd08_Selector()
  77.     {
  78.         BehaviourTreeNode[] children = new BehaviourTreeNode[DataBus.ReadInt32()];
  79.         for (int i = children.Length; i >= 0; i--)
  80.             children[i] = defined.Pop();
  81.         defined.Push(new Selector(children));
  82.     }
  83.    
  84.     #endregion
  85.     #region Decompile
  86.    
  87.     private string Cmd00_Decompile()
  88.     {
  89.         return "End";
  90.     }
  91.    
  92.     private string Cmd01_Decompile()  
  93.     {  
  94.         int id = DataBus.ReadInt32();
  95.         return String.Format("Action {0}", id);
  96.     }
  97.    
  98.     private string Cmd02_Decompile()  
  99.     {  
  100.         int id = DataBus.ReadInt32();
  101.         return String.Format("RunBehaviour {0}", id);
  102.     }
  103.    
  104.     private string Cmd03_Decompile()  
  105.     {  
  106.         return "Inverter";
  107.     }
  108.    
  109.     private string Cmd04_Decompile()  
  110.     {
  111.         return "Succeeder";
  112.     }
  113.    
  114.     private string Cmd05_Decompile()  
  115.     {
  116.         return "Repeater";
  117.     }
  118.    
  119.     private string Cmd06_Decompile()  
  120.     {
  121.         return "RepeatUntilFail";
  122.     }
  123.    
  124.     private string Cmd07_Decompile()
  125.     {
  126.         int len = DataBus.ReadInt32();
  127.         return String.Format("Sequence {0}", len);
  128.     }
  129.    
  130.     private string Cmd08_Decompile()
  131.     {
  132.         int len = DataBus.ReadInt32();
  133.         return String.Format("Selector {0}", len);
  134.     }
  135.    
  136.     #endregion
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement