示例#1
0
        public BehaviourTree <TBb> Compile(TBb blackboard, string code)
        {
            LispParser lispParser = new LispParser();

            LispParser.Node parseTree = lispParser.Parse(code);
            return(Compile(blackboard, parseTree));
        }
示例#2
0
        private void TestParse(string code)
        {
            LispParser lispParser = new LispParser();

            LispParser.Node node = lispParser.Parse(code);
            Debug.WriteLine(" in:{0}\nout:{1}\n", code, node.ToCode());
        }
        public void CanDeserializeFromLisp()
        {
            BehaviourTree <BehaviourTreeTests.Blackboard>         behaviourTree = CreateBehaviourTree();
            BehaviourTreeCompiler <BehaviourTreeTests.Blackboard> compiler      = new BehaviourTreeCompiler <BehaviourTreeTests.Blackboard>();
            //string code = serializer.Convert(behaviourTree).ToCode();
            string code = behaviourTree.ToString();

            Debug.WriteLine(" in:" + code);
            LispParser lispParser = new LispParser();

            LispParser.Node parseTree = lispParser.Parse(code);
            //Debug.WriteLine("out:" + parseTree.ToCode());
            BehaviourTreeTests.Blackboard blackboard = new BehaviourTreeTests.Blackboard();
            BehaviourTree <BehaviourTreeTests.Blackboard> compiled = compiler.Compile(blackboard, parseTree);

            Debug.WriteLine("dep:" + compiled);
            compiled.Tick();
            compiled.Tick();
            compiled.Tick();
        }
示例#4
0
        LispParser.ICanCreateFromLispParseTree <TBb> LispParser.ICompiler <TBb> .Compile(TBb blackboard, LispParser.Node parseTree)
        {
            foreach (LispParser.ICanCreateFromLispParseTree <TBb> creator in Creators)
            {
                LispParser.ICanCreateFromLispParseTree <TBb> result = creator.CreateFromParseTree(blackboard, parseTree, this);
                if (result != null)
                {
                    return(result);
                }
            }

            // For named couritines, we rewrite the parsetree to be able to use the regular flow.
            LispParser.MethodNode method = parseTree as LispParser.MethodNode;
            if (method != null)
            {
                Func <IEnumerator <TaskState> > function = blackboard.GetCoRoutineFunc(method.Identifier.Value);
                if (function != null)
                {
                    method.Add(method.Identifier);
                    method.Identifier = new LispParser.IdentifierNode("NamedCoroutine");
                    LispParser.ICanCreateFromLispParseTree <TBb> result = ((LispParser.ICanCreateFromLispParseTree <TBb>)_namedCoroutine).CreateFromParseTree(blackboard, method, this);
                    if (result != null)
                    {
                        return(result);
                    }
                }
            }

            throw new InvalidOperationException(
                      string.Format("Unrecognized token: {0}.", parseTree));
        }
示例#5
0
        public BehaviourTree <TBb> Compile(TBb blackboard, LispParser.Node parseTree)
        {
            BehaviourTree <TBb> behaviourTree = (BehaviourTree <TBb>)_behaviourTree.CreateFromParseTree(blackboard, parseTree, this);

            return(behaviourTree);
        }
示例#6
0
        LispParser.ICanCreateFromLispParseTree <TBb> LispParser.ICanCreateFromLispParseTree <TBb> .CreateFromParseTree(TBb blackboard, LispParser.Node node, LispParser.ICompiler <TBb> compiler)
        {
            LispParser.MethodNode method = node as LispParser.MethodNode;
            if (method == null)
            {
                return(null);
            }

            if (method.MethodName == GetClassName())
            {
                TreeTask <TBb> treeTask = CreateTreeTaskFromParseTree(blackboard, method, compiler);
                foreach (LispParser.Node child in method.Children)
                {
                    child.NamedParamX <LispParser.StringNode>("Name", v => treeTask.Name = v.Value);
                }
                return(treeTask);
            }
            else
            {
                return(null);
            }
        }
示例#7
0
        public LispParser.ICanCreateFromLispParseTree <TBb> CreateFromParseTree(TBb blackboard, LispParser.Node node, LispParser.ICompiler <TBb> compiler)
        {
            LispParser.MethodNode method = node as LispParser.MethodNode;
            if (method == null)
            {
                throw new InvalidOperationException("Expected BehaviourTree but found: null");
            }

            if (method.MethodName != "BehaviourTree")
            {
                throw new InvalidOperationException("Expected BehaviourTree but found: " + method);
            }

            BehaviourTree <TBb> behaviourTree = new BehaviourTree <TBb>(null, null);

            foreach (LispParser.Node child in method.Children)
            {
                child.NamedParamX <LispParser.NumberNode>("RunningTaskHysterisis", v => behaviourTree.RunningTaskHysterisis = (int)v.FloatValue);
                child.NamedParamX <LispParser.StringNode>("Name", v => behaviourTree.Name = v.Value);
                child.ParamX <LispParser.MethodNode>(null, v => behaviourTree.RootTask    = (TreeTask <TBb>)compiler.Compile(blackboard, v));
            }

            return(behaviourTree);
        }
示例#8
0
        public LispParser.ICanCreateFromLispParseTree <TBb> CreateFromParseTree(TBb blackboard, LispParser.Node node, LispParser.ICompiler <TBb> compiler)
        {
            LispParser.MethodNode method = node as LispParser.MethodNode;

            if (method == null || method.MethodName != "TaskUtility")
            {
                return(null);
            }

            TaskUtility <TBb> taskUtility = new TaskUtility <TBb>(null, null);

            taskUtility.UtilityComputer = ((BehaviourTreeCompiler <TBb>)compiler).LispCompiler.Compile(blackboard, method.Children[0]);
            if (!(taskUtility.UtilityComputer is ICanEvaluateToFloat))
            {
                throw new InvalidOperationException("The utility computer must evaluate to a float!");
            }
            taskUtility.Task = (TreeTask <TBb>)compiler.Compile(blackboard, method.Children[1]);
            return(taskUtility);
        }