예제 #1
0
    Combat::Combat(const NSFString& name)
        : NSFStateMachine(name, new NSFEventThread(name)), distanceToEnemy(100), inRangeDistance(25), nearDistance(50),

        // State Machine Components
        // Define and initialize in the order:
        //   1) Events
        //   2) Regions and states, from outer to inner
        //   3) Transitions, ordered internal, local, external
        //   4) Group states and transitions within a region together.
        // Maintain the same order of declaration and initialization.

        // Events
        scoutEvent("Scout", this),

        // Regions and states, from outer to inner
        combatInitialState("CombatInitial", this),
        scoutingState("Scouting", this, NULL, NULL),
        scoutingInitialState("ScountingInitial", &scoutingState),
        patrolState("Patrol", &scoutingState, NULL, NULL),
        moveToEnemyState("MoveToEnemy", &scoutingState, NULL, NULL),
        attackChoiceState("AttackChoice", this),
        attackState("Attack", this, NULL, NULL),

        // Transitions, ordered internal, local, external
        combatInitialToScoutingTransition("CombatInitialToScouting", &combatInitialState, &scoutingState, NULL, NULL, NULL),
        scoutingToAttackChoiceTransition("ScoutingToAttackChoice", &scoutingState, &attackChoiceState, &scoutEvent, NULL, NULL),
        scoutingInitialToPatrolTransition("ScoutingInitialToPatrol", &scoutingInitialState, &patrolState, NULL, NULL, NULL),
        attackChoiceToPatrolTransition("AttackChoiceToPatrol", &attackChoiceState, &patrolState, NULL, Else, NULL),
        attackChoiceToAttackTransition("AttackChoiceToAttack", &attackChoiceState, &attackState, NULL, NSFGuard(this, &Combat::isEnemyInRange), NULL),
        attackChoiceToMoveToEnemyTransition("AttackChoiceToMoveToEnemy", &attackChoiceState, &moveToEnemyState, NULL, NSFGuard(this, &Combat::isEnemyNear), NULL)
    {
    }
    ChoiceStateTest::ChoiceStateTest(const NSFString& name)
        : NSFStateMachine(name, new NSFEventThread(name)), value (0),
        // Events
        evaluateEvent("EvaluateEvent", this),
        waitEvent("WaitEvent", this), 

        // Regions and states, from outer to inner
        initialChoiceStateTestState("InitialChoiceStateTest", this),
        waitToEvaluateState("WaitToEvaluate", this, NULL, NULL),
        evaluateValueState("EvaluateValue", this),
        evaluatedState("Evaluated", this, NULL, NULL),
        initialEvaluatedState("InitialEvaluatedState", &evaluatedState),
        valueLowState("ValueLow", &evaluatedState, NULL, NULL),
        valueMiddleState("ValueMiddle", &evaluatedState, NULL, NULL),
        valueHighState("ValueHigh", &evaluatedState, NULL, NULL),

        // Transitions, ordered internal, local, external
        initialChoiceStateTestToWaitToEvaluateTransition("InitialChoiceStateTestToWaitToEvaluate", &initialChoiceStateTestState, &waitToEvaluateState, NULL, NULL, NULL),
        waitToEvaluateToEvaluateValueTransition("WaitToEvaluateToEvaluateValueTransition", &waitToEvaluateState, &evaluateValueState, &evaluateEvent, NULL, NULL),
        initialEvaluatedToValueLowTransition("InitialEvaluatedToValueLowTransition", &initialEvaluatedState, &valueLowState, NULL, NULL, NULL),
        evaluateValueToValueLowTransition("EvaluateValueToValueLowTransition", &evaluateValueState, &valueLowState, NULL, NSFGuard(this, &ChoiceStateTest::isValueLow), NULL),
        evaluateValueToValueMiddleTransition("EvaluateValueToValueMiddleTransition", &evaluateValueState, &valueMiddleState, NULL, Else, NULL),
        evaluateValueToValueHighTransition("EvaluateValueToValueHighTransition", &evaluateValueState, &valueHighState, NULL, NSFGuard(this, &ChoiceStateTest::isValueHigh), NULL),
        evaluatedToWaitToEvaluateTransition("EvaluatedToWaitToEvaluateTransition", &evaluatedState, &waitToEvaluateState, &waitEvent, NULL, NSFAction(this, &ChoiceStateTest::addValue))
    {
    }
 CommandProcessorWithResetStrategy::CommandProcessorWithResetStrategy(const NSFString& name)
     : CommandProcessor(name), resetStrategy("ResetStrategy", &resetState)
 {
     resetToWaitForCommandTransition.Guards += NSFGuard(this, &CommandProcessorWithResetStrategy::isReady);
 }
    ResetStrategy::ResetStrategy(const NSFString& name, NSFCompositeState* parentState)
        : NSFStateMachine(name, parentState),  hardwareReset(false), commReady(false), hardwareResetDelayTime(1000), commReadyDelayTime(1000),

        // State Machine Components
        // Define and initialize in the order:
        //   1) Events
        //   2) Regions and states, from outer to inner
        //   3) Transitions, ordered internal, local, external
        //   4) Group states and transitions within a region together.
        // Maintain the same order of declaration and initialization.

        // Events
        commReadyEvent("CommReady", this),
        hardwareResetEvent("HardwareReset", this),

        // Regions and states, from outer to inner 
        // Initial state construtors take the form (name, parent)
        initialResetStrategyState("InitialResetStrategy", this),
        // Composite state construtors take the form (name, parent, entry action, exit action)
        resetHardwareState("ResetHardware", this, NSFAction(this, &ResetStrategy::resetHardwareEntryActions), NULL),
        reestablishCommunicationsState("ReestablishCommunications", this, NSFAction(this, &ResetStrategy::reestablishCommunicationsEntryActions), NULL),
        readyState("Ready", this, NULL, NULL),

        // Transitions, ordered internal, local, external
        // External transition construtors take the form (name, source, target, trigger, guard, action)
        initialResetStrategyToResetHardwareTransition("InitialToResetHardware", &initialResetStrategyState, &resetHardwareState, NULL, NULL, NSFAction(this, &ResetStrategy::resetVariables)),        
        resetHardwareToReestablishCommunicationsTransition("ResetHardwareToReestablishCommunications", &resetHardwareState, &reestablishCommunicationsState, NULL, NSFGuard(this, &ResetStrategy::isHardwareReset), NULL),
        reestablishCommunicationsStateToReadyTransition("ReestablishCommunicationsStateToReady", &reestablishCommunicationsState, &readyState, NULL, NSFGuard(this, &ResetStrategy::isCommReady), NULL),

        // Actions
        resetHardwareAction("ReadHardware", NSFAction(this, &ResetStrategy::resetHardware), getEventThread()),
        readyCommAction("ResetComm", NSFAction(this, &ResetStrategy::resetComm), getEventThread())
    {
    }