public void SetState(FSMState state) { if (this._stateStack.Count != 0) this.OnStatFinished(this._states[this._stateStack[0]]); this._stateStack.Add(state.stateId); state.OnStateEnter(); }
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(); }
public void MoveToState(StateId state) { currentState.OnStateExit(); m_preStateId = m_curStateId; m_curStateId = state; preState = currentState; currentState = CreateState(state); currentState.OnStateEnter(); }
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); }
protected void InitTransition(TState src, TEvent eventID, TState dst) { FSMState state = this.GetState(src); state.AddTransition(eventID.GetHashCode(), this.GetState(dst)); }
/// <summary> /// Добавить исходящую дугу /// </summary> /// <param name="action">Суть действия, характеризующего дугу</param> /// <param name="destinationState">Состояние, в которое ведёт дуга</param> internal bool AddOutgoing(TInput action, FSMState <TInput, TOutput> destinationState) { return(AddOutgoing(action, destinationState, null)); }
private void AddTransition(FSMSystem fsm, FSMState state) { fsm.StatAddChild(state, this.idDest, this.data); }
public virtual void InitFSM() { m_curStateId = AnimStateId.ASIDLE; currentState = CreateState(AnimStateId.ASIDLE); }
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); }
public FSMAction(FSMState <StateEnum> initialState, FSMEvent <EventEnum> fsmEvent, FSMState <StateEnum> finalState) { _InitialState = initialState; _Event = fsmEvent; _FinalState = finalState; }
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(); }*/ }; }
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); } }; }
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(); } }; }
public FSMTransition(FSMState targetState, FSMAction action) { this.target = targetState; this.action = action; }
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); }
/// <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) { }
public virtual void StopFSM() { currentState = null; }
public FSMTransitionEventArgs(FSMState <TInput, TOutput> fromState, FSMState <TInput, TOutput> toState) : this(fromState, toState, null) { }
public void PerformTransition(FSMSystem fsm, FSMState state) { if (this.transitionType == eTransitionType.NoneTransition) return; this._transitions[(int)this.transitionType](fsm, state); }
public FSMTransitionEventArgs(FSMState <TInput, TOutput> fromState, FSMState <TInput, TOutput> toState, TOutput output) { FromState = fromState; ToState = toState; Output = output; }
private void SwitchStateTransition(FSMSystem fsm, FSMState state) { fsm.StatTransitTo(state, this.idDest, this.data); }
public static FSMContext createFSMInstance(FSMState startState, FSMAction initAction) { return(new FSMContext(startState, initAction)); }
/// <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)); }
public FSMContext(FSMState startState, FSMAction initAction) { this.currentState = startState; this.initAction = initAction; this.initAction.execute(this); }
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(); }
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); }
public void AddState(FSMState state) { this._states.Add(state.stateId, state); }
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); }
public bool AddOutgoing(FSMState <TInput, TOutput> sourceState, TInput action, FSMState <TInput, TOutput> destinationState) { return(AddOutgoing(sourceState, action, destinationState, null)); }
public StateInfo(FSMState <TInput, TOutput> state) { State = state; }
public TOutput Lambda(FSMState <TInput, TOutput> state, TInput input, double p) { throw new NotImplementedException(); }