Exemple #1
0
    /// <summary>
    /// Creates a transition between one <see cref="BehaviourTreeEngine"/>, the submachine; and another <see cref="BehaviourTreeEngine"/>, the supermachine.
    /// </summary>
    /// <param name="name">The name of the transition</param>
    /// <param name="nodeFrom">The exit node of the transition</param>
    /// <param name="stateTo">The node where the Behaviour tree is coming back</param>
    /// <param name="superMachine">The supermachine</param>
    /// <param name="subMachine">The submachine</param>
    public Transition(string name, TreeNode nodeFrom, LeafNode stateTo, BehaviourTreeEngine superMachine, BehaviourTreeEngine subMachine)
    {
        this.Name       = name;
        this.StateFrom  = nodeFrom.StateNode;
        this.StateTo    = stateTo.StateNode;
        this.Perception = new OrPerception(new BehaviourTreeStatusPerception(subMachine, ReturnValues.Succeed, subMachine),
                                           new BehaviourTreeStatusPerception(subMachine, ReturnValues.Failed, subMachine),
                                           subMachine);
        this.type = TRANSITION_TYPE.SUPER_TRANSITION;

        if (StateFrom.BehaviourEngine == superMachine)   // Exits from the super-machine
        {
            this.BehaviourEngine = superMachine;
            Perception.SetBehaviourMachine(superMachine);
            superMachine.Configure(StateFrom)
            .OnExit(this.Name, () => Perception.Reset())
            .InternalTransition(Perception, this.Name, () => ExitTransition(StateFrom, stateTo, subMachine.GetRootNode().ReturnValue, subMachine.GetState("Entry_Machine"), superMachine, subMachine));
        }
        else   // Exits from the sub-machine
        {
            this.BehaviourEngine = subMachine;
            Perception.SetBehaviourMachine(subMachine);
            subMachine.Configure(StateFrom)
            .OnExit(this.Name, () => Perception.Reset())
            .InternalTransition(Perception, this.Name, () => ExitTransition(StateFrom, stateTo, subMachine.GetRootNode().ReturnValue, subMachine.GetState("Entry_Machine"), superMachine, subMachine));
        }
    }
Exemple #2
0
    /// <summary>
    /// Creates a transition between two <see cref="BehaviourEngine"/>, one is the submachine the other the supermachine
    /// </summary>
    /// <param name="name">The name of the transition</param>
    /// <param name="stateFrom">The exit state of the transition</param>
    /// <param name="perception">The perception that activates the transition</param>
    /// <param name="stateTo">the entry state of the transition</param>
    /// <param name="superMachine">The supermachine</param>
    /// <param name="subMachine">The submachine</param>
    public Transition(string name, State stateFrom, Perception perception, State stateTo, BehaviourEngine superMachine, BehaviourEngine subMachine)
    {
        this.Name       = name;
        this.StateFrom  = stateFrom;
        this.Perception = perception;
        this.StateTo    = stateTo;
        this.type       = TRANSITION_TYPE.SUPER_TRANSITION;

        if (stateFrom.BehaviourEngine == superMachine)   // Exits from the super-machine
        {
            this.BehaviourEngine = superMachine;
            Perception.SetBehaviourMachine(superMachine);
            superMachine.Configure(StateFrom)
            .OnExit(this.Name, () => Perception.Reset())
            .InternalTransition(Perception, this.Name, () => ExitTransition(StateFrom, StateTo, subMachine.GetState("Entry_Machine"), superMachine, subMachine));
        }
        else   // Exits from the sub-machine
        {
            this.BehaviourEngine = subMachine;
            Perception.SetBehaviourMachine(subMachine);
            subMachine.Configure(StateFrom)
            .OnExit(this.Name, () => Perception.Reset())
            .InternalTransition(Perception, this.Name, () => ExitTransition(StateFrom, StateTo, subMachine.GetState("Entry_Machine"), superMachine, subMachine));
        }
    }
Exemple #3
0
 public AndPerception(Perception perceptionLeft, Perception perceptionRight, BehaviourEngine behaviourEngine) : base()
 {
     this.PerceptionLeft  = perceptionLeft;
     this.PerceptionRight = perceptionRight;
     this.reseter         = true;
     base.behaviourEngine = behaviourEngine;
 }
Exemple #4
0
    public TimerPerception(float time, BehaviourEngine behaviourEngine) : base()
    {
        this.Time     = time;
        this.launched = false;
        this.timer    = new Timer(time * 1000);

        base.behaviourEngine = behaviourEngine;
    }
Exemple #5
0
 //Acción con submáquina
 public UtilityAction(State utilState, Factor factor, UtilitySystemEngine uSystemEngine, BehaviourEngine subMachine)
 {
     this.HasSubmachine = true;
     this.utilityState  = utilState;
     this.factor        = factor;
     this.uCurvesEngine = uSystemEngine;
     this.subMachine    = subMachine;
 }
Exemple #6
0
    /// <summary>
    /// Creates a state with a sub-machine in it
    /// </summary>
    /// <param name="stateName">The name of the state</param>
    /// <param name="subStateMachine">The sub-state machine that the state has</param>
    /// <param name="entryState">The entry state of the submachine</param>
    /// <param name="stateMachineB">The state machine which the state belongs to</param>
    public State(string stateName, State entrySubMachineState, State stateTo, BehaviourEngine subMachine, BehaviourEngine behaviourEngine)
    {
        this.Name            = stateName;
        this.BehaviourEngine = behaviourEngine;
        this.configurator    = new StateConfigurator(StateConfigurator.STATE_TYPE.NOT_EMPTY);

        BehaviourEngine.Configure(this)
        .OnEntry(() => EntrySubmachine(entrySubMachineState, stateTo, subMachine, behaviourEngine));
    }
    /// <summary>
    /// Adds a type of <see cref="State"/> with a sub-state machine in it and its transition to the entry state
    /// </summary>
    /// <param name="stateName">The name of the state</param>
    /// <param name="subBehaviourEngine">The sub behaviour engine inside the state. It will enter in the entry state of the sub behaviour engine</param>
    public State CreateSubStateMachine(string stateName, BehaviourEngine subBehaviourEngine)
    {
        State stateTo = subBehaviourEngine.GetEntryState();
        State state   = new State(stateName, subBehaviourEngine.GetState("Entry_Machine"), stateTo, subBehaviourEngine, this);

        states.Add(state.Name, state);

        return(state);
    }
Exemple #8
0
    /// <summary>
    /// State that runs a method with no parameters
    /// </summary>
    /// <param name="stateName">Name of the state</param>
    /// <param name="method">Method with no parameters. MUST be a void method</param>
    /// <param name="behaviourEngine">The machine which the state belongs to</param>
    public State(string stateName, Action method, BehaviourEngine behaviourEngine)
    {
        this.Name            = stateName;
        this.StateActionVoid = method;
        this.BehaviourEngine = behaviourEngine;
        this.configurator    = new StateConfigurator(StateConfigurator.STATE_TYPE.NOT_EMPTY);

        BehaviourEngine.Configure(this)
        .OnEntry(() => method());
    }
Exemple #9
0
    /// <summary>
    /// Adds a type of <see cref="LeafNode"/> with a sub-behaviour engine in it and its transition to the entry state
    /// </summary>
    /// <param name="stateName">The name of the state</param>
    /// <param name="subBehaviourTree">The sub-behaviour tree inside the </param>
    public LeafNode CreateSubBehaviour(string nodeName, BehaviourEngine subBehaviourEngine)
    {
        State    stateTo  = subBehaviourEngine.GetEntryState();
        State    state    = new State(nodeName, subBehaviourEngine.GetState("Entry_Machine"), stateTo, subBehaviourEngine, this);
        LeafNode leafNode = new LeafNode("Node to return", state, this);

        subBehaviourEngine.NodeToReturn = leafNode;
        states.Add(leafNode.StateNode.Name, leafNode.StateNode);

        return(subBehaviourEngine.NodeToReturn);
    }
Exemple #10
0
    /// <summary>
    /// State that runs a method with a <see cref="Perception"/> parameter
    /// </summary>
    /// <param name="stateName">Name of the state</param>
    /// <param name="method">Method with no parameters. MUST be a void method</param>
    /// <param name="behaviourEngine">The machine which the state belongs to</param>
    public State(string stateName, Action <Perception> method, BehaviourEngine behaviourEngine)
    {
        this.Name = stateName;
        this.StateActionPerception = method;
        this.StatePerception       = (Perception)method.Method.GetParameters().GetValue(0);
        this.BehaviourEngine       = behaviourEngine;
        this.configurator          = new StateConfigurator(StateConfigurator.STATE_TYPE.NOT_EMPTY);

        BehaviourEngine.Configure(this)
        .OnEntry(() => method((Perception)method.Method.GetParameters().GetValue(0)));
    }
Exemple #11
0
    /// <summary>
    /// Creates a transition between states from a <see cref="BehaviourStateMachine"/>
    /// </summary>
    /// <param name="name">The name of the transition</param>
    /// <param name="stateFrom">The exit state of the transition</param>
    /// <param name="perception">The perception that activates the transition</param>
    /// <param name="stateTo">The entry state of the transition</param>
    /// <param name="behaviourEngine">The machine the transitions belongs to</param>
    public Transition(string name, State stateFrom, Perception perception, State stateTo, BehaviourEngine behaviourEngine)
    {
        this.Name            = name;
        this.StateFrom       = stateFrom;
        this.Perception      = perception;
        this.StateTo         = stateTo;
        this.BehaviourEngine = behaviourEngine;
        this.type            = TRANSITION_TYPE.NORMAL;

        BehaviourEngine.Configure(StateFrom)
        .OnExit(this.Name, () => { Perception.Reset(); });
    }
Exemple #12
0
    private void EntrySubmachine(State entrySubmachineState, State stateTo, BehaviourEngine subMachine, BehaviourEngine behaviourEngine)
    {
        if (subMachine.actualState != subMachine.GetState("Entry_Machine"))
        {
            return;
        }

        new Transition("Entry_submachine state", subMachine.actualState, new PushPerception(subMachine), stateTo, subMachine)
        .FireTransition();

        behaviourEngine.Active = false;
        subMachine.Active      = true;
    }
    /// <summary>
    /// Adds a type of <see cref="UtilityAction"/> with a sub-behaviour engine in it and its transition to the specified state
    /// </summary>
    /// <param name="actionName">The name of the action</param>
    /// <param name="factor">The factor that gives the utility value to the action</param>
    /// <param name="subBehaviourEngine">The sub-behaviour tree inside the </param>
    /// <param name="stateTo">The name of the state where the sub-state machine will enter</param>
    public UtilityAction CreateSubBehaviour(string actionName, Factor factor, BehaviourEngine subBehaviourEngine, State stateTo)
    {
        if (!states.ContainsKey(actionName))
        {
            State         state      = new State(actionName, subBehaviourEngine.GetState("Entry_Machine"), stateTo, subBehaviourEngine, this);
            UtilityAction utilAction = new UtilityAction(state, factor, this, subBehaviourEngine);
            states.Add(utilAction.utilityState.Name, utilAction.utilityState);
            actions.Add(utilAction);

            return(utilAction);
        }
        else
        {
            throw new DuplicateWaitObjectException(actionName, "The utility action already exists in the utility engine");
        }
    }
 public BehaviourTreeStatusPerception(BehaviourTreeEngine behaviourTree, ReturnValues statusToReach, BehaviourEngine behaviourEngine)
 {
     this.behaviourTree       = behaviourTree;
     this.BehaviourTreeStatus = statusToReach;
     this.behaviourEngine     = behaviourEngine;
 }
Exemple #15
0
 public LeafNode(string name, State stateNode, BehaviourEngine behaviourEngine)
 {
     base.HasSubmachine = true;
     base.StateNode     = stateNode;
     base.behaviourTree = behaviourEngine as BehaviourTreeEngine;
 }
Exemple #16
0
 public ValuePerception(Func <bool>[] comparisons, BehaviourEngine behaviourEngine) : base()
 {
     this.comparisons     = comparisons;
     base.behaviourEngine = behaviourEngine;
 }
Exemple #17
0
 public OrPerception(Perception perceptionLeft, Perception perceptionRight, BehaviourEngine behaviourEngine)
 {
     this.PerceptionLeft  = perceptionLeft;
     this.PerceptionRight = perceptionRight;
     base.behaviourEngine = behaviourEngine;
 }
Exemple #18
0
 public void SetBehaviourMachine(BehaviourEngine behaviourEngine)
 {
     this.behaviourEngine = behaviourEngine;
 }
Exemple #19
0
    /// <summary>
    /// Executes the exit transitions between a submachine and the supermachine which is a <see cref="BehaviourTreeEngine"/>
    /// </summary>
    /// <param name="stateFrom">The exit state of the transition</param>
    /// <param name="stateTo">The entry state of the transition</param>
    /// <param name="entrySubMachineState">The 'Entry_State' of the submachine</param>
    /// <param name="superMachine">The supermachine where the trasition is going to</param>
    /// <param name="subMachine">The subsmachine the transition is leaving</param>
    private void ExitTransition(State stateFrom, LeafNode stateTo, ReturnValues returnValue, State entrySubMachineState, BehaviourTreeEngine superMachine, BehaviourEngine subMachine)
    {
        if (stateFrom.BehaviourEngine == superMachine)  // Exits from the super-machine
        // Transitions from Submachine.CurrentState -> Submachine.EntryState
        {
            Transition returnTransition = new Transition("reset_submachine", subMachine.actualState, new PushPerception(subMachine), entrySubMachineState, subMachine);
            returnTransition.FireTransition();

            subMachine.Reset();
            subMachine.Active   = false;
            superMachine.Active = true;
            stateTo.ReturnValue = returnValue;

            // Transitions from stateFrom -> stateTo
            // Maybe I should change this too, as I changed the next one?
            // TODO review if this is working properly

            Transition superTransition = new Transition("to_state", stateFrom, new PushPerception(superMachine), stateTo.StateNode, superMachine);
            superTransition.FireTransition();
        }
        else   // Exits from the sub-machine
               // Transitions from stateFrom -> Submachine.EntryState
        {
            Transition returnTransition = new Transition("reset_submachine", stateFrom, new PushPerception(subMachine), entrySubMachineState, subMachine);
            returnTransition.FireTransition();

            subMachine.Reset();
            subMachine.Active   = false;
            superMachine.Active = true;
            stateTo.ReturnValue = returnValue;

            // Transitions from Supermachine.CurrentState -> stateTo

            /* ¿BUG?
             * stateTo SHOULD BE THE SAME AS SuperMachine.CurrentState  --> We are exiting from a machine
             * that returns Succeed or Failed to the SuperMachine LeafNode, if we enter again to that Node
             * we are again putting the ReturnType to "process", so we enter in an infinite loop
             */

            //Transition superTransition = new Transition("to_state", superMachine.actualState, new PushPerception(superMachine), stateTo.StateNode, superMachine);
            //superTransition.FireTransition();
        }
    }
Exemple #20
0
    /// <summary>
    /// Executes the exit transitions between a submachine and the supermachine
    /// </summary>
    /// <param name="stateFrom">The exit state of the transition</param>
    /// <param name="stateTo">The entry state of the transition</param>
    /// <param name="entrySubMachineState">The 'Entry_State' of the submachine</param>
    /// <param name="superMachine">The supermachine where the trasition is going to</param>
    /// <param name="subMachine">The subsmachine the transition is leaving</param>
    private void ExitTransition(State stateFrom, State stateTo, State entrySubMachineState, BehaviourEngine superMachine, BehaviourEngine subMachine)
    {
        if (stateFrom.BehaviourEngine == superMachine)  // Exits from the super-machine
        // Transitions from Submachine.CurrentState -> Submachine.EntryState
        {
            Transition returnTransition = new Transition("reset_submachine", subMachine.actualState, new PushPerception(subMachine), entrySubMachineState, subMachine);
            returnTransition.FireTransition();

            subMachine.Reset();
            subMachine.Active   = false;
            superMachine.Active = true;

            // Transitions from stateFrom -> stateTo
            Transition superTransition = new Transition("to_state", stateFrom, new PushPerception(superMachine), stateTo, superMachine);
            superTransition.FireTransition();
        }
        else   // Exits from the sub-machine
               // Transitions from stateFrom -> Submachine.EntryState
        {
            Transition returnTransition = new Transition("reset_submachine", stateFrom, new PushPerception(subMachine), entrySubMachineState, subMachine);
            returnTransition.FireTransition();

            subMachine.Reset();
            subMachine.Active   = false;
            superMachine.Active = true;

            // Transitions from Supermachine.CurrentState -> stateTo
            Transition superTransition = new Transition("to_state", superMachine.actualState, new PushPerception(superMachine), stateTo, superMachine);
            superTransition.FireTransition();
        }
    }
Exemple #21
0
 public KeyPerception(ConsoleKey key, BehaviourEngine behaviourEngine) : base()
 {
     this.pressed = false;
     this.key     = key;
 }
Exemple #22
0
 public PushPerception(BehaviourEngine behaviourEngine) : base()
 {
     base.behaviourEngine = behaviourEngine;
 }
Exemple #23
0
 /// <summary>
 /// Creates a <see cref="Perception"/> that returns true when a machine is in the State/Action provided
 /// </summary>
 /// <param name="engineToLook">The engine that contains the State/Action to look</param>
 /// <param name="stateToLookFor">The name of the State or Action that you want to fetch</param>
 /// <param name="behaviourEngine">The behaviout engine that contains this perception</param>
 public IsInStatePerception(BehaviourEngine engineToLook, string stateToLookFor, BehaviourEngine behaviourEngine)
 {
     this.engineToLook    = engineToLook;
     this.stateToLook     = engineToLook.GetState(stateToLookFor);
     base.behaviourEngine = behaviourEngine;
 }
Exemple #24
0
 /// <summary>
 /// Empty state
 /// </summary>
 /// <param name="stateName">The name of the state</param>
 /// <param name="behaviourEngine">The machine which the state belongs to</param>
 public State(string stateName, BehaviourEngine behaviourEngine)
 {
     this.Name            = stateName;
     this.BehaviourEngine = behaviourEngine;
     this.configurator    = new StateConfigurator(StateConfigurator.STATE_TYPE.EMPTY);
 }