Esempio n. 1
0
        public override LispOperator <TBb> CreateOperator(TBb blackboard, LispParser.Node node, LispParser.ICompiler <TBb> compiler)
        {
            LispParser.MethodNode methodNode = node as LispParser.MethodNode;
            if (methodNode == null || methodNode.Identifier.Value != Name)
            {
                return(null);
            }

            LispMethod <TBb> method = CreateMethod(blackboard, methodNode, compiler);

            if (method != null)
            {
                if (method.GetType() != GetType())
                {
                    throw new InvalidOperationException(
                              string.Format(
                                  "Expected {0} but compiled to {1}!",
                                  TypeHelper.GetFriendlyTypeName(GetType()),
                                  TypeHelper.GetFriendlyTypeName(method.GetType())));
                }

                foreach (LispParser.Node child in methodNode.Children)
                {
                    method.Children.Add((LispOperator <TBb>)compiler.Compile(blackboard, child));
                }
            }
            return(method);
        }
Esempio n. 2
0
        protected override TreeTask <TBb> CreateTreeTaskFromParseTree(TBb blackboard, LispParser.MethodNode method, LispParser.ICompiler <TBb> compiler)
        {
            Decorator <TBb> decorator = CreateDecoratorFromParseTree(blackboard, method, compiler);

            if (decorator != null)
            {
                if (decorator.GetType() != GetType())
                {
                    throw new InvalidOperationException(
                              string.Format(
                                  "Expected {0} but compiled to {1}!",
                                  TypeHelper.GetFriendlyTypeName(GetType()),
                                  TypeHelper.GetFriendlyTypeName(decorator.GetType())));
                }

                decorator.DecoratedTask = (TreeTask <TBb>)compiler.Compile(blackboard, method.Children.OfType <LispParser.MethodNode>().Single());
            }
            return(decorator);
        }
Esempio n. 3
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);
        }
Esempio n. 4
0
        protected override TreeTask <TBb> CreateTreeTaskFromParseTree(TBb blackboard, LispParser.MethodNode method, LispParser.ICompiler <TBb> compiler)
        {
            ControlFlowNode <TBb> controlFlowNode = CreateControlFlow(blackboard, method, compiler);

            if (controlFlowNode == null)
            {
                return(null);
            }

            foreach (LispParser.Node child in method.Children)
            {
                bool handled =
                    child.NamedParamX <LispParser.StringNode>("Name", v => controlFlowNode.Name = v.Value) ||
                    child.ParamX <LispParser.MethodNode>(null, v => controlFlowNode.Children.Add((TreeTask <TBb>)compiler.Compile(blackboard, child)));

                if (!handled)
                {
                    LispParser.NamedParameterNode named = child as LispParser.NamedParameterNode;

                    if (named != null && named.Identifier.Value == "if")
                    {
                        controlFlowNode.Predicate = ((BehaviourTreeCompiler <TBb>)compiler).LispCompiler.Compile(blackboard, named.Value);
                        if (!(controlFlowNode.Predicate is ICanEvaluateToBool))
                        {
                            throw new InvalidOperationException(
                                      string.Format(
                                          "The if predicate must evaluate to a bool, {0} doesn't!",
                                          named.Value));
                        }
                        continue;
                    }

                    throw new InvalidOperationException(
                              string.Format(
                                  "Unhandled child {0} on {1}!",
                                  child,
                                  this));
                }
            }

            return(controlFlowNode);
        }
Esempio n. 5
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);
        }
Esempio n. 6
0
        protected override TreeTask <TBb> CreateTreeTaskFromParseTree(TBb blackboard, LispParser.MethodNode method, LispParser.ICompiler <TBb> compiler)
        {
            UtilityControlFlowNode <TBb> utilityControlFlowNode = CreateUtilityControlFlow(blackboard, method, compiler);

            if (utilityControlFlowNode == null)
            {
                return(null);
            }

            foreach (LispParser.Node child in method.Children)
            {
                child.NamedParamX <LispParser.StringNode>("Name", v => utilityControlFlowNode.Name = v.Value);
                child.ParamX <LispParser.MethodNode>(null, v => utilityControlFlowNode.Children.Add((TaskUtility <TBb>)compiler.Compile(blackboard, child)));
            }

            return(utilityControlFlowNode);
        }