Initial cleanup of Squad.

This commit is contained in:
Paul Chote
2013-08-24 10:18:40 +12:00
parent aadfd6979b
commit b2a91cc8f8
5 changed files with 33 additions and 63 deletions

View File

@@ -42,19 +42,19 @@ namespace OpenRA.Mods.RA.AI
this.random = bot.random; this.random = bot.random;
this.type = type; this.type = type;
this.target = target; this.target = target;
fsm = new StateMachine(this); fsm = new StateMachine();
switch (type) switch (type)
{ {
case SquadType.Assault: case SquadType.Assault:
case SquadType.Rush: case SquadType.Rush:
fsm.ChangeState(new GroundUnitsIdleState(), true); fsm.ChangeState(this, new GroundUnitsIdleState(), true);
break; break;
case SquadType.Air: case SquadType.Air:
fsm.ChangeState(new AirIdleState(), true); fsm.ChangeState(this, new AirIdleState(), true);
break; break;
case SquadType.Protection: case SquadType.Protection:
fsm.ChangeState(new UnitsForProtectionIdleState(), true); fsm.ChangeState(this, new UnitsForProtectionIdleState(), true);
break; break;
} }
} }
@@ -62,7 +62,7 @@ namespace OpenRA.Mods.RA.AI
public void Update() public void Update()
{ {
if (IsEmpty) return; if (IsEmpty) return;
fsm.UpdateFsm(); fsm.Update(this);
} }
public bool IsEmpty public bool IsEmpty

View File

@@ -1,6 +1,6 @@
#region Copyright & License Information #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 * 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 * available to you under the terms of the GNU General Public License
* as published by the Free Software Foundation. For more information, * as published by the Free Software Foundation. For more information,
@@ -12,61 +12,31 @@ namespace OpenRA.Mods.RA.AI
{ {
class StateMachine class StateMachine
{ {
//a pointer to the agent that owns this instance IState currentState;
private Squad owner; IState previousState;
private IState currentState; public void Update(Squad squad)
//a record of the last state the agent was in
private IState previousState;
public StateMachine(Squad owner)
{ {
this.owner = owner; currentState.Execute(squad);
} }
public IState CurrentState public void ChangeState(Squad squad, IState newState, bool rememberPrevious)
{ {
get { return currentState; } if (rememberPrevious)
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
previousState = currentState; previousState = currentState;
//call the exit method of the existing state if (currentState != null)
if(currentState != null) currentState.Exit(squad);
currentState.Exit(owner);
//change state to the new state
if (newState != null) if (newState != null)
currentState = newState; currentState = newState;
//call the entry method of the new state currentState.Enter(squad);
currentState.Enter(owner);
} }
//change state back to the previous state public void RevertToPreviousState(Squad squad, bool saveCurrentState)
public void RevertToPreviousState(bool saveCurrentState)
{ {
ChangeState(previousState, saveCurrentState); ChangeState(squad, previousState, saveCurrentState);
} }
} }

View File

@@ -155,7 +155,7 @@ namespace OpenRA.Mods.RA.AI
if (MayBeFlee(owner)) if (MayBeFlee(owner))
{ {
owner.fsm.ChangeState(new AirFleeState(), true); owner.fsm.ChangeState(owner, new AirFleeState(), true);
return; return;
} }
@@ -165,7 +165,7 @@ namespace OpenRA.Mods.RA.AI
else else
{ {
owner.Target = e; 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; owner.Target = closestEnemy;
else else
{ {
owner.fsm.ChangeState(new AirFleeState(), true); owner.fsm.ChangeState(owner, new AirFleeState(), true);
return; return;
} }
} }
if (!NearToPosSafely(owner, owner.Target.CenterPosition)) if (!NearToPosSafely(owner, owner.Target.CenterPosition))
{ {
owner.fsm.ChangeState(new AirFleeState(), true); owner.fsm.ChangeState(owner, new AirFleeState(), true);
return; return;
} }
@@ -243,7 +243,7 @@ namespace OpenRA.Mods.RA.AI
} }
owner.world.IssueOrder(new Order("Move", a, false) { TargetLocation = RandomBuildingLocation(owner) }); 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) { } public void Exit(Squad owner) { }

View File

@@ -54,11 +54,11 @@ namespace OpenRA.Mods.RA.AI
foreach(var u in owner.units) foreach(var u in owner.units)
owner.world.IssueOrder(new Order("AttackMove", u, false) { TargetLocation = owner.Target.CenterPosition.ToCPos() }); owner.world.IssueOrder(new Order("AttackMove", u, false) { TargetLocation = owner.Target.CenterPosition.ToCPos() });
// We have gathered sufficient units. Attack the nearest enemy unit. // We have gathered sufficient units. Attack the nearest enemy unit.
owner.fsm.ChangeState(new GroundUnitsAttackMoveState(), true); owner.fsm.ChangeState(owner, new GroundUnitsAttackMoveState(), true);
return; return;
} }
else 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; owner.Target = closestEnemy;
else else
{ {
owner.fsm.ChangeState(new GroundUnitsFleeState(), true); owner.fsm.ChangeState(owner, new GroundUnitsFleeState(), true);
return; return;
} }
} }
@@ -104,7 +104,7 @@ namespace OpenRA.Mods.RA.AI
if (enemynearby.Any()) if (enemynearby.Any())
{ {
owner.Target = enemynearby.ClosestTo(leader.CenterPosition); owner.Target = enemynearby.ClosestTo(leader.CenterPosition);
owner.fsm.ChangeState(new GroundUnitsAttackState(), true); owner.fsm.ChangeState(owner, new GroundUnitsAttackState(), true);
return; return;
} }
else else
@@ -114,7 +114,7 @@ namespace OpenRA.Mods.RA.AI
if (MayBeFlee(owner)) if (MayBeFlee(owner))
{ {
owner.fsm.ChangeState(new GroundUnitsFleeState(), true); owner.fsm.ChangeState(owner, new GroundUnitsFleeState(), true);
return; return;
} }
} }
@@ -137,7 +137,7 @@ namespace OpenRA.Mods.RA.AI
owner.Target = closestEnemy; owner.Target = closestEnemy;
else else
{ {
owner.fsm.ChangeState(new GroundUnitsFleeState(), true); owner.fsm.ChangeState(owner, new GroundUnitsFleeState(), true);
return; return;
} }
} }
@@ -147,7 +147,7 @@ namespace OpenRA.Mods.RA.AI
if (MayBeFlee(owner)) if (MayBeFlee(owner))
{ {
owner.fsm.ChangeState(new GroundUnitsFleeState(), true); owner.fsm.ChangeState(owner, new GroundUnitsFleeState(), true);
return; return;
} }
} }
@@ -164,7 +164,7 @@ namespace OpenRA.Mods.RA.AI
if (owner.IsEmpty) return; if (owner.IsEmpty) return;
GoToRandomOwnBuilding(owner); GoToRandomOwnBuilding(owner);
owner.fsm.ChangeState(new GroundUnitsIdleState(), true); owner.fsm.ChangeState(owner, new GroundUnitsIdleState(), true);
} }
public void Exit(Squad owner) { owner.units.Clear(); } public void Exit(Squad owner) { owner.units.Clear(); }

View File

@@ -17,7 +17,7 @@ namespace OpenRA.Mods.RA.AI
class UnitsForProtectionIdleState : GroundStateBase, IState class UnitsForProtectionIdleState : GroundStateBase, IState
{ {
public void Enter(Squad owner) { } 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) { } public void Exit(Squad owner) { }
} }
@@ -36,7 +36,7 @@ namespace OpenRA.Mods.RA.AI
if (owner.Target == null) if (owner.Target == null)
{ {
owner.fsm.ChangeState(new UnitsForProtectionFleeState(), true); owner.fsm.ChangeState(owner, new UnitsForProtectionFleeState(), true);
return; return;
} }
} }
@@ -56,7 +56,7 @@ namespace OpenRA.Mods.RA.AI
if (owner.IsEmpty) return; if (owner.IsEmpty) return;
GoToRandomOwnBuilding(owner); GoToRandomOwnBuilding(owner);
owner.fsm.ChangeState(new UnitsForProtectionIdleState(), true); owner.fsm.ChangeState(owner, new UnitsForProtectionIdleState(), true);
} }
public void Exit(Squad owner) { owner.units.Clear(); } public void Exit(Squad owner) { owner.units.Clear(); }