Ejemplo n.º 1
0
 public void SetState(FSMState state)
 {
     if (this._stateStack.Count != 0)
         this.OnStatFinished(this._states[this._stateStack[0]]);
     this._stateStack.Add(state.stateId);
     state.OnStateEnter();
 }
Ejemplo n.º 2
0
        public void OnStatFinished(FSMState state)
        {
            int idChild = this.FindStat(state);

            this.ParentKillChildren(idChild);
            state.OnStateLeave();
            this._stateStack.RemoveAt(this._stateStack.Count - 1);
            this._states[this._stateStack[this._stateStack.Count - 1]].OnChildFinished();
        }
Ejemplo n.º 3
0
 public void MoveToState(StateId state)
 {
     currentState.OnStateExit();
     m_preStateId = m_curStateId;
     m_curStateId = state;
     preState = currentState;
     currentState = CreateState(state);
     currentState.OnStateEnter();
 }
Ejemplo n.º 4
0
 private int FindStat(FSMState stat)
 {
     for (int i = 0, size = this._stateStack.Count; i < size; ++i)
     {
         if (this._states[this._stateStack[i]] == stat)
         {
             return (i);
         }
     }
     return (-1);
 }
Ejemplo n.º 5
0
        protected void InitTransition(TState src, TEvent eventID, TState dst)
        {
            FSMState state = this.GetState(src);

            state.AddTransition(eventID.GetHashCode(), this.GetState(dst));
        }
Ejemplo n.º 6
0
 /// <summary>
 /// Добавить исходящую дугу
 /// </summary>
 /// <param name="action">Суть действия, характеризующего дугу</param>
 /// <param name="destinationState">Состояние, в которое ведёт дуга</param>
 internal bool AddOutgoing(TInput action, FSMState <TInput, TOutput> destinationState)
 {
     return(AddOutgoing(action, destinationState, null));
 }
Ejemplo n.º 7
0
 private void AddTransition(FSMSystem fsm, FSMState state)
 {
     fsm.StatAddChild(state, this.idDest, this.data);
 }
Ejemplo n.º 8
0
 public virtual void InitFSM()
 {
     m_curStateId = AnimStateId.ASIDLE;
     currentState = CreateState(AnimStateId.ASIDLE);
 }
Ejemplo n.º 9
0
        public bool ChangeValues(FSMState <TInput, TOutput> newDestState, TOutput newOutput, double newProbability)
        {
            if (newDestState == null)
            {
                throw new ArgumentNullException("newDestState");
            }
            if (newOutput == null)
            {
                throw new ArgumentNullException("newOutput");
            }
            if (newDestState.FSM != DestState.FSM)
            {
                throw new ArgumentException("newDestState");
            }

            bool result = false;
            TransitionRes <TInput, TOutput> trR = new TransitionRes <TInput, TOutput>()
            {
                DestState = newDestState,
                Output    = newOutput
            };


            double avProb = 0;

            foreach (var destinationState in Transition.destinationStates)
            {
                if (destinationState.KeyName != this.KeyName)
                {
                    avProb += destinationState.Probability;
                }
                else
                {
                    avProb += newProbability;
                }
            }
            if (avProb > 1)
            {
                throw new ArgumentException("Probability must be in (0;1]", "newProbability");
            }


            if (trR.KeyName == this.KeyName)
            {
                this.Probability = newProbability;
                result           = true;
            }
            else
            {
                var existTrRes = Transition.destinationStates.FirstOrDefault(t => t.KeyName == trR.KeyName);
                if (existTrRes == null)
                {
                    this.DestState   = newDestState;
                    this.Output      = newOutput;
                    this.Probability = newProbability;
                    result           = true;
                }
                else
                {
                    throw new ArgumentException("Перехода с указанными параметрами уже существует");
                }
            }
            return(result);
        }
Ejemplo n.º 10
0
 public FSMAction(FSMState <StateEnum> initialState, FSMEvent <EventEnum> fsmEvent, FSMState <StateEnum> finalState)
 {
     _InitialState = initialState;
     _Event        = fsmEvent;
     _FinalState   = finalState;
 }
Ejemplo n.º 11
0
	private void createMoveToState() {
		moveToState = (fsm, gameObj) => {
			// move the game object

			GoapAction action = currentActions.Peek();
			if (action.requiresInRange() && action.target == null) {
				Debug.Log("<color=red>Fatal error:</color> Action requires a target but has none. Planning failed. You did not assign the target in your Action.checkProceduralPrecondition()");
				fsm.popState(); // move
				fsm.popState(); // perform
				fsm.pushState(idleState);
				return;
			}

			// get the agent to move itself
			if ( dataProvider.moveAgent(action) ) {
				fsm.popState();
			}

			/*MovableComponent movable = (MovableComponent) gameObj.GetComponent(typeof(MovableComponent));
			if (movable == null) {
				Debug.Log("<color=red>Fatal error:</color> Trying to move an Agent that doesn't have a MovableComponent. Please give it one.");
				fsm.popState(); // move
				fsm.popState(); // perform
				fsm.pushState(idleState);
				return;
			}

			float step = movable.moveSpeed * Time.deltaTime;
			gameObj.transform.position = Vector3.MoveTowards(gameObj.transform.position, action.target.transform.position, step);

			if (gameObj.transform.position.Equals(action.target.transform.position) ) {
				// we are at the target location, we are done
				action.setInRange(true);
				fsm.popState();
			}*/
		};
	}
Ejemplo n.º 12
0
	private void createIdleState() {
		idleState = (fsm, gameObj) => {
			// GOAP planning

			// get the world state and the goal we want to plan for
			HashSet<KeyValuePair<string,object>> worldState = dataProvider.getWorldState();
			HashSet<KeyValuePair<string,object>> goal = dataProvider.createGoalState();

			// Plan
			Queue<GoapAction> plan = planner.plan(gameObject, availableActions, worldState, goal);
			if (plan != null) {
				// we have a plan, hooray!
				currentActions = plan;
				dataProvider.planFound(goal, plan);

				fsm.popState(); // move to PerformAction state
				fsm.pushState(performActionState);

			} else {
				// ugh, we couldn't get a plan
				Debug.Log("<color=orange>Failed Plan:</color>"+prettyPrint(goal));
				dataProvider.planFailed(goal);
				fsm.popState (); // move back to IdleAction state
				fsm.pushState (idleState);
			}

		};
	}
Ejemplo n.º 13
0
	private void createPerformActionState() {

		performActionState = (fsm, gameObj) => {
			// perform the action

			if (!hasActionPlan()) {
				// no actions to perform
				Debug.Log("<color=red>Done actions</color>");
				fsm.popState();
				fsm.pushState(idleState);
				dataProvider.actionsFinished();
				return;
			}

			GoapAction action = currentActions.Peek();
			if ( action.isDone() ) {
				// the action is done. Remove it so we can perform the next one
				currentActions.Dequeue();
			}

			if (hasActionPlan()) {
				// perform the next action
				action = currentActions.Peek();
				bool inRange = action.requiresInRange() ? action.isInRange() : true;

				if ( inRange ) {
					// we are in range, so perform the action
					bool success = action.perform(gameObj);

					if (!success) {
						// action failed, we need to plan again
						fsm.popState();
						fsm.pushState(idleState);
						dataProvider.planAborted(action);
					}
				} else {
					// we need to move there first
					// push moveTo state
					fsm.pushState(moveToState);
				}

			} else {
				// no actions left, move to Plan state
				fsm.popState();
				fsm.pushState(idleState);
				dataProvider.actionsFinished();
			}

		};
	}
Ejemplo n.º 14
0
 public FSMTransition(FSMState targetState, FSMAction action)
 {
     this.target = targetState;
     this.action = action;
 }
Ejemplo n.º 15
0
 public void StatTransitTo(FSMState state, eStateId destState, TransitionData tData)
 {
     this.OnStatFinished(state);
     if (this._stateStack.Count != 0)
         this.StatAddChild(this._states[this._stateStack[this._stateStack.Count - 1]], destState, tData);
 }
Ejemplo n.º 16
0
 public virtual void InitFSM()
 {
     m_curStateId = AnimStateId.ASIDLE;
     currentState = CreateState(AnimStateId.ASIDLE);
 }
Ejemplo n.º 17
0
 /// <summary>
 /// Конструктор
 /// </summary>
 /// <param name="actionCore">Суть действия</param>
 /// <param name="fromState">Откуда ведёт дуга</param>
 /// <param name="toState">Куда ведёт дуга</param>
 public FSMAction(TInput actionCore, FSMState <TInput, TOutput> fromState, FSMState <TInput, TOutput> toState)
     : this(actionCore, fromState)
 {
 }
Ejemplo n.º 18
0
 public virtual void StopFSM()
 {
     currentState = null;
 }
Ejemplo n.º 19
0
 public FSMTransitionEventArgs(FSMState <TInput, TOutput> fromState, FSMState <TInput, TOutput> toState) :
     this(fromState, toState, null)
 {
 }
Ejemplo n.º 20
0
 public void PerformTransition(FSMSystem fsm, FSMState state)
 {
     if (this.transitionType == eTransitionType.NoneTransition)
         return;
     this._transitions[(int)this.transitionType](fsm, state);
 }
Ejemplo n.º 21
0
 public FSMTransitionEventArgs(FSMState <TInput, TOutput> fromState, FSMState <TInput, TOutput> toState, TOutput output)
 {
     FromState = fromState;
     ToState   = toState;
     Output    = output;
 }
Ejemplo n.º 22
0
 private void SwitchStateTransition(FSMSystem fsm, FSMState state)
 {
     fsm.StatTransitTo(state, this.idDest, this.data);
 }
Ejemplo n.º 23
0
 public static FSMContext createFSMInstance(FSMState startState, FSMAction initAction)
 {
     return(new FSMContext(startState, initAction));
 }
Ejemplo n.º 24
0
 /// <summary>
 /// Добавить исходящую дугу
 /// </summary>
 /// <param name="action">Суть действия, характеризующего дугу</param>
 /// <param name="destinationState">Состояние, в которое ведёт дуга</param>
 /// <param name="output">Выходной элемент автомата</param>
 internal bool AddOutgoing(TInput action, FSMState <TInput, TOutput> destinationState, TOutput output)
 {
     return(AddOutgoing(action, destinationState, output, 1.0));
 }
Ejemplo n.º 25
0
 public FSMContext(FSMState startState, FSMAction initAction)
 {
     this.currentState = startState;
     this.initAction   = initAction;
     this.initAction.execute(this);
 }
Ejemplo n.º 26
0
        public void StatAddChild(FSMState parent, eStateId childType, TransitionData tData)
        {
            int idParent = this.FindStat(parent);

            if (idParent == -1)
            {
                Debug.LogWarning("StatAddChild : parent not found");
                return;
            }
            if (idParent != this._stateStack.Count)
            {
                this.ParentKillChildren(idParent);
            }
            this._stateStack.Add(childType);
            this._states[childType].OnStateEnter();
        }
Ejemplo n.º 27
0
        public bool AddDestination(FSMState <TInput, TOutput> state, TOutput output, double probability)
        {
            if (state == null)
            {
                throw new ArgumentNullException("state");
            }

            if ((probability <= 0) || (probability > 1))
            {
                throw new ArgumentException("Probability must be in (0;1]", "probability");
            }

            TransitionRes <TInput, TOutput> tr = new TransitionRes <TInput, TOutput>(this)
            {
                DestState   = state,
                Output      = output,
                Probability = probability
            };
            bool result = true;

            if (!destinationStates.Contains(tr))
            {
                double available = 0;
                foreach (var destinationState in destinationStates)
                {
                    available += destinationState.Probability;
                }

                if (probability + available > 1)
                {
                    throw new ArgumentException("Sum probability must be in (0;1]", "probability");
                }

                destinationStates.Add(tr);
            }
            else
            {
                var v = destinationStates.First(d => d.KeyName == tr.KeyName);

                double available = 0;
                foreach (var destinationState in destinationStates)
                {
                    if (destinationState.KeyName != v.KeyName)
                    {
                        available += destinationState.Probability;
                    }
                    else
                    {
                        available += probability;
                    }
                }

                if (available > 1)
                {
                    throw new ArgumentException("Sum probability must be in (0;1]", "probability");
                }

                v.Probability = probability;
            }
            return(result);
        }
Ejemplo n.º 28
0
 public void AddState(FSMState state)
 {
     this._states.Add(state.stateId, state);
 }
Ejemplo n.º 29
0
        public bool AddOutgoing(FSMState <TInput, TOutput> sourceState, TInput action, FSMState <TInput, TOutput> destinationState, TOutput output, double probability)
        {
            if ((probability <= 0) || (probability > 1))
            {
                throw new ArgumentException("Probability must be in (0;1]", "probability");
            }

            bool result = false;
            var  st     = stateSet.FirstOrDefault(s => s.StateCore == sourceState.StateCore);

            if (st != null)
            {
                Transition <TInput, TOutput> transition = new Transition <TInput, TOutput>(this)
                {
                    SourceState = sourceState,
                    Input       = action,
                    //DestinationState = destinationState,
                    //Output = output
                };
                transition.AddDestination(destinationState, output, probability);
                if (!Transitions.ContainsKey(transition.ToString()))
                {
                    if (st.AddOutgoing(action, destinationState, output, probability))
                    {
                        Transitions.Add(transition.ToString(), transition);
                        result = true;
                    }
                }
                else
                {
                    if (st.AddOutgoing(action, destinationState, output, probability))
                    {
                        result = Transitions[transition.ToString()].AddDestination(destinationState, output, probability);
                    }
                }
            }

            if (result)
            {
                if (probability < 1)
                {
                    IsProbabilityMachine = true;
                }
            }

            return(result);
        }
Ejemplo n.º 30
0
 public bool AddOutgoing(FSMState <TInput, TOutput> sourceState, TInput action, FSMState <TInput, TOutput> destinationState)
 {
     return(AddOutgoing(sourceState, action, destinationState, null));
 }
Ejemplo n.º 31
0
 public StateInfo(FSMState <TInput, TOutput> state)
 {
     State = state;
 }
Ejemplo n.º 32
0
 public TOutput Lambda(FSMState <TInput, TOutput> state, TInput input, double p)
 {
     throw new NotImplementedException();
 }
Ejemplo n.º 33
0
 public virtual void StopFSM()
 {
     currentState = null;
 }