private void AttatchNewDeerFSM(Deer deer, Game game) { //Declaring Actions for states ScaredAction scaredAction = new ScaredAction(); WanderAction wanderAction = new WanderAction(); GrazeAction grazeAction = new GrazeAction(); //FleeFromLionAction fleeFromLionAction = new FleeFromLionAction(); //FleeFromHunterAction fleeFromHunterAction = new FleeFromHunterAction(); DeerFleeAction deerFleeAction = new DeerFleeAction(); resetWander resetWander = new resetWander(); emptyAction emptyAction = new emptyAction(); FlockAction flockAction = new FlockAction(); //Declaring States for FSM State scaredState = new State("scared", scaredAction); State wanderState = new State("wander", emptyAction, wanderAction, resetWander); State grazeState = new State("graze", grazeAction); State fleeState = new State("flee", deerFleeAction); //fleeState.AddActions(); State flockState = new State("flock", flockAction); //Declaring conditions used for transitions FearGreaterThan fearGreaterThan80 = new FearGreaterThan(80); FearGreaterThan fearGreaterThan60 = new FearGreaterThan(60); FearLessThan fearLessThan40 = new FearLessThan(40); FearLessThan fearLessThan20 = new FearLessThan(20); ThreatLevel hunterHighThreatLevel = new ThreatLevel(75f); DistanceToHunter distanceToHunter = new DistanceToHunter(200f); AndCondition andThreatDistanceHunter = new AndCondition(hunterHighThreatLevel, distanceToHunter); OrCondition orFearThreat = new OrCondition(fearGreaterThan60, andThreatDistanceHunter); NeighborCountGreaterCondition neighborCountGreater = new NeighborCountGreaterCondition(5); NeighborCountLessCondition neighborCountLess = new NeighborCountLessCondition(2); RandomTimerCondition fleetoScaredTimer = new RandomTimerCondition(initialTime, 600); RandomTimerCondition wandertoGrazeTimer = new RandomTimerCondition(initialTime, shortTimer);//800 to 1200 RandomTimerCondition flocktoGrazeTimer = new RandomTimerCondition(initialTime, 600); RandomTimerCondition grazetoFlockTimer = new RandomTimerCondition(initialTime, 600); RandomCondition randomCondition = new RandomCondition(2, 2); //AndCondition andRandomLowFear = new AndCondition(randomCondition, fearLessThan40); AndCondition andTimerLowFear = new AndCondition(fleetoScaredTimer, fearLessThan20); AndCondition andRandomNeighborCountGreater = new AndCondition(randomCondition, neighborCountGreater); AndCondition andRandomNeighborCountLess = new AndCondition(grazetoFlockTimer, neighborCountLess); //AndCondition andRNCRandom = new AndCondition(andRandomNeighborCount, grazetoFlockTimer); WanderCondition wanderTrue = new WanderCondition(); //Declaring Transitions Transition gotoWanderFromScared = new Transition(andTimerLowFear, wanderState, 0); //Transition gotoWanderFromScared = new Transition(andRandomLowFear, wanderState, 0); Transition gotoWanderFromGraze = new Transition(wanderTrue, wanderState, 0); Transition gotoWanderFromGraze2 = new Transition(andRandomNeighborCountLess, wanderState, 0); Transition gotoGrazeFromScared = new Transition(andTimerLowFear, grazeState, 0); //Transition gotoGrazeFromScared = new Transition(andRandomLowFear, grazeState, 0); Transition gotoGrazeFromWander = new Transition(wandertoGrazeTimer, grazeState, 0); Transition gotoFlockfromGraze = new Transition(andRandomNeighborCountGreater, flockState, 0); Transition gotoGrazefromFlock = new Transition(flocktoGrazeTimer, grazeState, 0); Transition gotoScaredFromWander = new Transition(orFearThreat, scaredState, 0); //old cond was feargreaterthan 60 Transition gotoScaredFromGraze = new Transition(orFearThreat, scaredState, 0); Transition gotoScaredFromFlock = new Transition(orFearThreat, scaredState, 0); Transition gotoFlee = new Transition(fearGreaterThan80, fleeState, 0); //Transition gotoScaredFromFlee = new Transition(andTimerLowFear, scaredState, 0); Transition gotoScaredFromFlee = new Transition(fearLessThan40, scaredState, 0); //Declaring actions for transitions gotoWanderFromScared.addActions(wanderAction); gotoWanderFromGraze.addActions(wanderAction); gotoWanderFromGraze2.addActions(wanderAction); gotoGrazeFromScared.addActions(grazeAction); gotoGrazeFromWander.addActions(grazeAction); gotoGrazefromFlock.addActions(grazeAction); gotoScaredFromWander.addActions(scaredAction); gotoScaredFromGraze.addActions(scaredAction); gotoFlee.addActions(deerFleeAction); gotoFlockfromGraze.addActions(flockAction); gotoScaredFromFlock.addActions(scaredAction); gotoScaredFromFlee.addActions(scaredAction); //Hooking up transitions to states scaredState.addTransition(gotoWanderFromScared); scaredState.addTransition(gotoGrazeFromScared); scaredState.addTransition(gotoFlee); wanderState.addTransition(gotoScaredFromWander); wanderState.addTransition(gotoGrazeFromWander); grazeState.addTransition(gotoScaredFromGraze); grazeState.addTransition(gotoWanderFromGraze); grazeState.addTransition(gotoFlockfromGraze); grazeState.addTransition(gotoWanderFromGraze2); fleeState.addTransition(gotoScaredFromFlee); flockState.addTransition(gotoGrazefromFlock); flockState.addTransition(gotoScaredFromFlock); FiniteStateMachine newFSM = new FiniteStateMachine(wanderState); deer.fsm = newFSM; }
void AttachLionHFSM() { // hunt deer sub macine state //////////////////////////// State wanderState = new State("wander deer", new WanderAction()); State hideState = new State("hide deer", new SetTargetToClosestBush(), new GoToBushAction(), new emptyAction()); State waitState = new State("wait for deer", new emptyAction(), new WaitInBushAction(false), new emptyVisibleAction()); State pounceState = new State("pounce deer", new SetLionTargetToDeerAction(), new PounceAction(), new emptyAction()); pounceState.entryActions.Add(new emptyVisibleAction()); State eatState = new State("eat", new KillDeerAction(), new EatAction(), new emptyAction()); State napState = new State("nap", new NapAction()); State creepState = new State("creep deer", new CreepDeerAction()); State chaseState = new State("chase deer", new ChaseDeerAction()); Transition arriveAtBush = new Transition(new ReachedBush(), waitState, -1); Transition waitToPounce = new Transition(new AndCondition(new TimerCondition(200), new DeerInRangeCondition(RangeToPounce)), pounceState, -1); Transition pounceToKill = new Transition(new ReachedDeerTarget(KillRange), eatState, -1); Transition pounceToWander = new Transition(new ReachedPounceTarget(30), wanderState, -1); Transition pounceMissToWander = new Transition(new NotCondition(new ReachedPounceTarget(200)), wanderState, -1); Transition wanderToNap = new Transition(new RandomTimerCondition(new TimeSpan(), 600), napState, -1); Transition napToWander = new Transition(new RandomTimerCondition(new TimeSpan(), 600), wanderState, -1); Transition napToCreep = new Transition(new LionHungerGreaterThanCondition(800), creepState, -1); Transition wanderToCreep = new Transition(new LionHungerGreaterThanCondition(800), creepState, -1); Transition creepToHide = new Transition(new LionHungerGreaterThanCondition(1300), hideState, -1); Transition creepToPounce = new Transition(new DeerInRangeCondition(RangeToPounce), pounceState, -1); Transition waitToChase = new Transition(new LionHungerGreaterThanCondition(1800), chaseState, -1); Transition chaseToPounce = new Transition(new DeerInRangeCondition(RangeToPounce), pounceState, -1); Transition eatToWander = new Transition(new RandomTimerCondition(new TimeSpan(), 200), wanderState, -1); hideState.addTransition(arriveAtBush); waitState.addTransition(waitToPounce, waitToChase); pounceState.addTransition(pounceToWander, pounceToKill, pounceMissToWander); wanderState.addTransition(wanderToNap, wanderToCreep); napState.addTransition(napToWander, napToCreep); eatState.addTransition(eatToWander); creepState.addTransition(waitToChase, creepToPounce, creepToHide); chaseState.addTransition(chaseToPounce); SubMachineState HuntDeerSubMachineState = new SubMachineState(game, wanderState, hideState, waitState, pounceState, wanderState, eatState, chaseState, creepState, napState); // HUNT HUNTER SUBMACHINE STATE ///////////////////////////////////////////////// State creepHunter = new State("creepHunter", new CreepHunterAction()); State chaseHunter = new State("chaseHunter", new ChaseHunterAction()); State pounceHunter = new State("pouncePlayer", new SetLionTargetToHunterAction(), new PounceAction(), new emptyVisibleAction()); State goToBushState = new State("hideFromPlayer", new SetTargetToClosestBush(), new GoToBushAction(), new emptyAction()); State hideInBush = new State("hide", new emptyAction(), new WaitInBushAction(true), new emptyVisibleAction()); State hurtHunterState = new State("bite!", new BiteHunterAction(), new NapAction(), new emptyAction()); Transition creepToChaseH = new Transition(new DistanceToHunter(200), chaseHunter, -1); Transition creepToPounceH = new Transition(new DistanceToHunter(150), pounceHunter, -1); Transition creepToBushH = new Transition(new NotCondition(new DistanceToHunter(300)), goToBushState, -1); creepHunter.addTransition(creepToPounce, creepToChaseH, creepToBushH); Transition chaseToPounceH = new Transition(new DistanceToHunter(150), pounceHunter, -1); chaseHunter.addTransition(chaseToPounceH); Transition pounceToHurtHunter = new Transition(new DistanceToHunter(40), hurtHunterState, -1); Transition pounceToCreepHunter = new Transition(new ReachedPounceTarget(40), creepHunter, -1); pounceHunter.addTransition(pounceToHurtHunter, pounceToCreepHunter); Transition hurtHunterToCreep = new Transition(new TimerCondition(50), creepHunter, -1); hurtHunterState.addTransition(hurtHunterToCreep); Transition bushToChase = new Transition(new DistanceToHunter(200), chaseHunter, -1); Transition goToBushToHide = new Transition(new ReachedBush(), hideInBush, -1); goToBushState.addTransition(bushToChase, goToBushToHide); Transition hideToPounce = new Transition(new DistanceToHunter(RangeToPounce), pounceHunter, -1); Transition hideToCreep = new Transition(new TimerCondition(400), chaseHunter, -1); hideInBush.addTransition(hideToPounce, hideToCreep); SubMachineState HuntHunterSubMachine = new SubMachineState(game, creepHunter, creepHunter, chaseHunter, pounceHunter, hurtHunterState, goToBushState, hideInBush); HuntHunterSubMachine.name = "hunt Hunter"; //top level machine Transition huntDeerToHuntHunter = new Transition(new OrCondition(new OrCondition( new AndCondition(new DistanceToHunter(300), new AndCondition(new NotCondition(new LionHealthCondition(4)), new LionHungerGreaterThanCondition(2500))), new DeerCount(0)), new AndCondition(new ThreatLevel(75f), new DistanceToHunter(299))), HuntHunterSubMachine, 0); HuntDeerSubMachineState.addTransition(huntDeerToHuntHunter); State fleeFromHunterState = new State("flee hunter", new FleeFromHunterAction()); State fleeWanderState = new State("wandering", new WanderAction()); Transition fleeFFHtoWander = new Transition(new NotCondition(new DistanceToHunter(300)), fleeWanderState, -1); Transition fleeWandertoFFH = new Transition(new DistanceToHunter(300), fleeFromHunterState, -1); fleeFromHunterState.addTransition(fleeFFHtoWander); fleeWanderState.addTransition(fleeWandertoFFH); Transition fleeHunterToHuntDeer = new Transition(new AndCondition(new NotCondition(new DistanceToHunter(200)), new NotCondition(new DeerCount(0))), HuntDeerSubMachineState, 0); Transition fleeHuntertoHuntHunter = new Transition(new LionHungerGreaterThanCondition(3000), HuntHunterSubMachine, 0); fleeHuntertoHuntHunter.addActions(new LionDesperateAction()); SubMachineState fleeHunterSubMachine = new SubMachineState(game, fleeFromHunterState, fleeFromHunterState, fleeWanderState); fleeHunterSubMachine.name = "fleeHunter"; fleeHunterSubMachine.addTransition(fleeHunterToHuntDeer, fleeHuntertoHuntHunter); Transition huntHunterToHuntDeer = new Transition(new OrCondition(new PlayerHealthCondition(0), ( new AndCondition(new NotCondition(new DeerCount(0)), new NotCondition(new DistanceToHunter(300))))), HuntDeerSubMachineState, 0); Transition huntHunterToFleeHunter = new Transition(new AndCondition(new NotCondition(new LionDesperateCondition()), new OrCondition(new PlayerHealthCondition(0), new LionHealthCondition(5))), fleeHunterSubMachine, 0); HuntHunterSubMachine.addTransition(huntHunterToHuntDeer, huntHunterToFleeHunter); Transition huntDeerToFleeHunter = new Transition(new AndCondition( new DistanceToHunter(200), new LionHealthCondition(5)), fleeHunterSubMachine, 0); HuntDeerSubMachineState.addTransition(huntDeerToFleeHunter); //finalize machine and attach to lion. PHEW this.hfsm = new HierarchicalStateMachine(game, HuntDeerSubMachineState, fleeHunterSubMachine, HuntHunterSubMachine); }