diff --git a/OpenRA.Mods.RA/AI/Squad.cs b/OpenRA.Mods.RA/AI/Squad.cs index b97459781c..ab79b7bf99 100644 --- a/OpenRA.Mods.RA/AI/Squad.cs +++ b/OpenRA.Mods.RA/AI/Squad.cs @@ -42,19 +42,19 @@ namespace OpenRA.Mods.RA.AI this.random = bot.random; this.type = type; this.target = target; - fsm = new StateMachine(this); + fsm = new StateMachine(); switch (type) { case SquadType.Assault: case SquadType.Rush: - fsm.ChangeState(new GroundUnitsIdleState(), true); + fsm.ChangeState(this, new GroundUnitsIdleState(), true); break; case SquadType.Air: - fsm.ChangeState(new AirIdleState(), true); + fsm.ChangeState(this, new AirIdleState(), true); break; case SquadType.Protection: - fsm.ChangeState(new UnitsForProtectionIdleState(), true); + fsm.ChangeState(this, new UnitsForProtectionIdleState(), true); break; } } @@ -62,7 +62,7 @@ namespace OpenRA.Mods.RA.AI public void Update() { if (IsEmpty) return; - fsm.UpdateFsm(); + fsm.Update(this); } public bool IsEmpty diff --git a/OpenRA.Mods.RA/AI/StateMachine.cs b/OpenRA.Mods.RA/AI/StateMachine.cs index f3ac9007f1..9693a2682e 100644 --- a/OpenRA.Mods.RA/AI/StateMachine.cs +++ b/OpenRA.Mods.RA/AI/StateMachine.cs @@ -1,6 +1,6 @@ #region Copyright & License Information /* - * Copyright 2007-2011 The OpenRA Developers (see AUTHORS) + * Copyright 2007-2013 The OpenRA Developers (see AUTHORS) * This file is part of OpenRA, which is free software. It is made * available to you under the terms of the GNU General Public License * as published by the Free Software Foundation. For more information, @@ -12,61 +12,31 @@ namespace OpenRA.Mods.RA.AI { class StateMachine { - //a pointer to the agent that owns this instance - private Squad owner; + IState currentState; + IState previousState; - private IState currentState; - - //a record of the last state the agent was in - private IState previousState; - - public StateMachine(Squad owner) + public void Update(Squad squad) { - this.owner = owner; + currentState.Execute(squad); } - public IState CurrentState + public void ChangeState(Squad squad, IState newState, bool rememberPrevious) { - get { return currentState; } - set { currentState = value; } - } - - public IState PreviousState - { - get { return previousState; } - set { previousState = value; } - } - - //call this to update the FSM - public void UpdateFsm() - { - currentState.Execute(owner); - } - - //change to a new state - //boolean variable isSaveCurrentState respons on save or not current state - public void ChangeState(IState newState, bool saveCurrentState) - { - if (saveCurrentState) - //keep a record of the previous state + if (rememberPrevious) previousState = currentState; - //call the exit method of the existing state - if(currentState != null) - currentState.Exit(owner); + if (currentState != null) + currentState.Exit(squad); - //change state to the new state if (newState != null) currentState = newState; - //call the entry method of the new state - currentState.Enter(owner); + currentState.Enter(squad); } - //change state back to the previous state - public void RevertToPreviousState(bool saveCurrentState) + public void RevertToPreviousState(Squad squad, bool saveCurrentState) { - ChangeState(previousState, saveCurrentState); + ChangeState(squad, previousState, saveCurrentState); } } diff --git a/OpenRA.Mods.RA/AI/States/AirStates.cs b/OpenRA.Mods.RA/AI/States/AirStates.cs index 71afee7d00..60536f7e92 100644 --- a/OpenRA.Mods.RA/AI/States/AirStates.cs +++ b/OpenRA.Mods.RA/AI/States/AirStates.cs @@ -155,7 +155,7 @@ namespace OpenRA.Mods.RA.AI if (MayBeFlee(owner)) { - owner.fsm.ChangeState(new AirFleeState(), true); + owner.fsm.ChangeState(owner, new AirFleeState(), true); return; } @@ -165,7 +165,7 @@ namespace OpenRA.Mods.RA.AI else { owner.Target = e; - owner.fsm.ChangeState(new AirAttackState(), true); + owner.fsm.ChangeState(owner, new AirAttackState(), true); } } @@ -188,14 +188,14 @@ namespace OpenRA.Mods.RA.AI owner.Target = closestEnemy; else { - owner.fsm.ChangeState(new AirFleeState(), true); + owner.fsm.ChangeState(owner, new AirFleeState(), true); return; } } if (!NearToPosSafely(owner, owner.Target.CenterPosition)) { - owner.fsm.ChangeState(new AirFleeState(), true); + owner.fsm.ChangeState(owner, new AirFleeState(), true); return; } @@ -243,7 +243,7 @@ namespace OpenRA.Mods.RA.AI } owner.world.IssueOrder(new Order("Move", a, false) { TargetLocation = RandomBuildingLocation(owner) }); } - owner.fsm.ChangeState(new AirIdleState(), true); + owner.fsm.ChangeState(owner, new AirIdleState(), true); } public void Exit(Squad owner) { } diff --git a/OpenRA.Mods.RA/AI/States/GroundStates.cs b/OpenRA.Mods.RA/AI/States/GroundStates.cs index 7f2c2b47a6..96e84e6d17 100644 --- a/OpenRA.Mods.RA/AI/States/GroundStates.cs +++ b/OpenRA.Mods.RA/AI/States/GroundStates.cs @@ -54,11 +54,11 @@ namespace OpenRA.Mods.RA.AI foreach(var u in owner.units) owner.world.IssueOrder(new Order("AttackMove", u, false) { TargetLocation = owner.Target.CenterPosition.ToCPos() }); // We have gathered sufficient units. Attack the nearest enemy unit. - owner.fsm.ChangeState(new GroundUnitsAttackMoveState(), true); + owner.fsm.ChangeState(owner, new GroundUnitsAttackMoveState(), true); return; } else - owner.fsm.ChangeState(new GroundUnitsFleeState(), true); + owner.fsm.ChangeState(owner, new GroundUnitsFleeState(), true); } } @@ -80,7 +80,7 @@ namespace OpenRA.Mods.RA.AI owner.Target = closestEnemy; else { - owner.fsm.ChangeState(new GroundUnitsFleeState(), true); + owner.fsm.ChangeState(owner, new GroundUnitsFleeState(), true); return; } } @@ -104,7 +104,7 @@ namespace OpenRA.Mods.RA.AI if (enemynearby.Any()) { owner.Target = enemynearby.ClosestTo(leader.CenterPosition); - owner.fsm.ChangeState(new GroundUnitsAttackState(), true); + owner.fsm.ChangeState(owner, new GroundUnitsAttackState(), true); return; } else @@ -114,7 +114,7 @@ namespace OpenRA.Mods.RA.AI if (MayBeFlee(owner)) { - owner.fsm.ChangeState(new GroundUnitsFleeState(), true); + owner.fsm.ChangeState(owner, new GroundUnitsFleeState(), true); return; } } @@ -137,7 +137,7 @@ namespace OpenRA.Mods.RA.AI owner.Target = closestEnemy; else { - owner.fsm.ChangeState(new GroundUnitsFleeState(), true); + owner.fsm.ChangeState(owner, new GroundUnitsFleeState(), true); return; } } @@ -147,7 +147,7 @@ namespace OpenRA.Mods.RA.AI if (MayBeFlee(owner)) { - owner.fsm.ChangeState(new GroundUnitsFleeState(), true); + owner.fsm.ChangeState(owner, new GroundUnitsFleeState(), true); return; } } @@ -164,7 +164,7 @@ namespace OpenRA.Mods.RA.AI if (owner.IsEmpty) return; GoToRandomOwnBuilding(owner); - owner.fsm.ChangeState(new GroundUnitsIdleState(), true); + owner.fsm.ChangeState(owner, new GroundUnitsIdleState(), true); } public void Exit(Squad owner) { owner.units.Clear(); } diff --git a/OpenRA.Mods.RA/AI/States/ProtectionStates.cs b/OpenRA.Mods.RA/AI/States/ProtectionStates.cs index dae3b62214..948e1f025a 100644 --- a/OpenRA.Mods.RA/AI/States/ProtectionStates.cs +++ b/OpenRA.Mods.RA/AI/States/ProtectionStates.cs @@ -17,7 +17,7 @@ namespace OpenRA.Mods.RA.AI class UnitsForProtectionIdleState : GroundStateBase, IState { public void Enter(Squad owner) { } - public void Execute(Squad owner) { owner.fsm.ChangeState(new UnitsForProtectionAttackState(), true); } + public void Execute(Squad owner) { owner.fsm.ChangeState(owner, new UnitsForProtectionAttackState(), true); } public void Exit(Squad owner) { } } @@ -36,7 +36,7 @@ namespace OpenRA.Mods.RA.AI if (owner.Target == null) { - owner.fsm.ChangeState(new UnitsForProtectionFleeState(), true); + owner.fsm.ChangeState(owner, new UnitsForProtectionFleeState(), true); return; } } @@ -56,7 +56,7 @@ namespace OpenRA.Mods.RA.AI if (owner.IsEmpty) return; GoToRandomOwnBuilding(owner); - owner.fsm.ChangeState(new UnitsForProtectionIdleState(), true); + owner.fsm.ChangeState(owner, new UnitsForProtectionIdleState(), true); } public void Exit(Squad owner) { owner.units.Clear(); }