Added Cancel() to Mobile.CurrentAction. Added QueueAction to Mobile. Made Order.Apply use queue rather than SetNextAction.

This commit is contained in:
Bob
2009-10-24 19:17:13 +13:00
parent d66475edcb
commit d25bd8550a
4 changed files with 156 additions and 118 deletions

View File

@@ -5,9 +5,9 @@ using OpenRa.TechTree;
using System.Drawing; using System.Drawing;
using System.Linq; using System.Linq;
using IrrKlang; using IrrKlang;
using IjwFramework.Collections; using IjwFramework.Collections;
using System; using System;
using IjwFramework.Types; using IjwFramework.Types;
using OpenRa.Game.Traits; using OpenRa.Game.Traits;
namespace OpenRa.Game namespace OpenRa.Game
@@ -16,7 +16,7 @@ namespace OpenRa.Game
{ {
public static readonly int CellSize = 24; public static readonly int CellSize = 24;
public static World world; public static World world;
public static Map map; public static Map map;
static TreeCache treeCache; static TreeCache treeCache;
public static TerrainRenderer terrain; public static TerrainRenderer terrain;
@@ -40,8 +40,8 @@ namespace OpenRa.Game
Rules.LoadRules( mapName ); Rules.LoadRules( mapName );
for( int i = 0 ; i < 8 ; i++ ) for( int i = 0 ; i < 8 ; i++ )
players.Add(i, new Player(i, string.Format("Multi{0}", i), Race.Soviet)); players.Add(i, new Player(i, string.Format("Multi{0}", i), Race.Soviet));
localPlayerIndex = localPlayer; localPlayerIndex = localPlayer;
var mapFile = new IniFile( FileSystem.Open( mapName ) ); var mapFile = new IniFile( FileSystem.Open( mapName ) );
@@ -55,8 +55,8 @@ namespace OpenRa.Game
treeCache = new TreeCache(map); treeCache = new TreeCache(map);
foreach( TreeReference treeReference in map.Trees ) foreach( TreeReference treeReference in map.Trees )
world.Add( new Actor( treeReference, treeCache, map.Offset ) ); world.Add( new Actor( treeReference, treeCache, map.Offset ) );
BuildingInfluence = new BuildingInfluenceMap(world, 8); BuildingInfluence = new BuildingInfluenceMap(world, 8);
LoadMapBuildings( mapFile ); LoadMapBuildings( mapFile );
@@ -136,67 +136,69 @@ namespace OpenRa.Game
return map.IsInMap(a.X, a.Y) && return map.IsInMap(a.X, a.Y) &&
TerrainCosts.Cost(umt, TerrainCosts.Cost(umt,
terrain.tileSet.GetWalkability(map.MapTiles[a.X, a.Y])) < double.PositiveInfinity; terrain.tileSet.GetWalkability(map.MapTiles[a.X, a.Y])) < double.PositiveInfinity;
} }
static IEnumerable<Actor> FindUnits(float2 a, float2 b) static IEnumerable<Actor> FindUnits(float2 a, float2 b)
{ {
var min = float2.Min(a, b); var min = float2.Min(a, b);
var max = float2.Max(a, b); var max = float2.Max(a, b);
var rect = new RectangleF(min.X, min.Y, max.X - min.X, max.Y - min.Y); var rect = new RectangleF(min.X, min.Y, max.X - min.X, max.Y - min.Y);
return world.Actors return world.Actors
.Where(x => x.Bounds.IntersectsWith(rect)); .Where(x => x.Bounds.IntersectsWith(rect));
} }
public static IEnumerable<Actor> FindUnitsInCircle(float2 a, float r) public static IEnumerable<Actor> FindUnitsInCircle(float2 a, float r)
{ {
return FindUnits(a - new float2(r, r), a + new float2(r, r)) return FindUnits(a - new float2(r, r), a + new float2(r, r))
.Where(x => (x.CenterLocation - a).LengthSquared < r * r); .Where(x => (x.CenterLocation - a).LengthSquared < r * r);
} }
public static IEnumerable<Actor> SelectUnitsInBox(float2 a, float2 b) public static IEnumerable<Actor> SelectUnitsInBox(float2 a, float2 b)
{ {
return FindUnits(a, b).Where(x => x.Owner == LocalPlayer && x.traits.Contains<Traits.Mobile>()); return FindUnits(a, b).Where(x => x.Owner == LocalPlayer && x.traits.Contains<Traits.Mobile>());
} }
public static IEnumerable<Actor> SelectUnitOrBuilding(float2 a) public static IEnumerable<Actor> SelectUnitOrBuilding(float2 a)
{ {
var q = FindUnits(a, a); var q = FindUnits(a, a);
return q.Where(x => x.traits.Contains<Traits.Mobile>()).Concat(q).Take(1); return q.Where(x => x.traits.Contains<Traits.Mobile>()).Concat(q).Take(1);
} }
public static int GetDistanceToBase(int2 b, Player p) public static int GetDistanceToBase(int2 b, Player p)
{ {
var building = BuildingInfluence.GetNearestBuilding(b); var building = BuildingInfluence.GetNearestBuilding(b);
if (building == null || building.Owner != p) if (building == null || building.Owner != p)
return int.MaxValue; return int.MaxValue;
return BuildingInfluence.GetDistanceToBuilding(b); return BuildingInfluence.GetDistanceToBuilding(b);
} }
public static Random SharedRandom = new Random(); /* for things that require sync */ public static Random SharedRandom = new Random(); /* for things that require sync */
public static Random CosmeticRandom = new Random(); /* for things that are just fluff */ public static Random CosmeticRandom = new Random(); /* for things that are just fluff */
public static readonly Pair<VoicePool, VoicePool> SovietVoices = public static readonly Pair<VoicePool, VoicePool> SovietVoices =
Pair.New( Pair.New(
new VoicePool("ackno", "affirm1", "noprob", "overout", "ritaway", "roger", "ugotit"), new VoicePool("ackno", "affirm1", "noprob", "overout", "ritaway", "roger", "ugotit"),
new VoicePool("await1", "ready", "report1", "yessir1")); new VoicePool("await1", "ready", "report1", "yessir1"));
public static void BuildUnit(Player player, string name) public static void BuildUnit(Player player, string name)
{ {
var producer = world.Actors var producer = world.Actors
.FirstOrDefault(a => a.unitInfo != null && a.unitInfo.Name == "weap" && a.Owner == player); .FirstOrDefault(a => a.unitInfo != null && a.unitInfo.Name == "weap" && a.Owner == player);
if (producer == null) if (producer == null)
throw new InvalidOperationException("BuildUnit without suitable production structure!"); throw new InvalidOperationException("BuildUnit without suitable production structure!");
var unit = new Actor(name, (1/24f * producer.CenterLocation).ToInt2(), player); var unit = new Actor(name, (1/24f * producer.CenterLocation).ToInt2(), player);
unit.Order(unit.Location + new int2(0, 3)).Apply(false); var mobile = unit.traits.Get<Mobile>();
mobile.facing = 128;
world.AddFrameEndTask(_ => world.Add(unit)); mobile.SetNextAction( new Traits.Mobile.MoveTo( unit.Location + new int2( 0, 3 ) ) );
// todo: make the producing building play `build` world.AddFrameEndTask(_ => world.Add(unit));
}
// todo: make the producing building play `build`
}
} }
} }

View File

@@ -33,7 +33,7 @@ namespace OpenRa.Game
if (Game.LocalPlayer == Unit.Owner) if (Game.LocalPlayer == Unit.Owner)
Game.PlaySound("ackno.r00", false); Game.PlaySound("ackno.r00", false);
var mobile = Unit.traits.Get<Traits.Mobile>(); var mobile = Unit.traits.Get<Traits.Mobile>();
mobile.SetNextAction( new Traits.Mobile.MoveTo( Destination ) ); mobile.QueueAction( new Traits.Mobile.MoveTo( Destination ) );
} }
} }
} }

View File

@@ -35,7 +35,9 @@ namespace OpenRa.Game.Traits
public override void Apply( bool leftMouseButton ) public override void Apply( bool leftMouseButton )
{ {
if( leftMouseButton ) return; if( leftMouseButton ) return;
Unit.traits.Get<Mobile>().SetNextAction( new Mobile.Turn( 96 ) { NextAction = new DeployAction() } ); var mobile = Unit.traits.Get<Mobile>();
mobile.QueueAction( new Mobile.Turn( 96 ) );
mobile.QueueAction( new DeployAction() );
} }
class DeployAction : Mobile.CurrentAction class DeployAction : Mobile.CurrentAction
@@ -50,6 +52,12 @@ namespace OpenRa.Game.Traits
Game.world.Add( new Actor( "fact", self.Location - new int2( 1, 1 ), self.Owner ) ); Game.world.Add( new Actor( "fact", self.Location - new int2( 1, 1 ), self.Owner ) );
} ); } );
} }
public void Cancel( Actor self, Mobile mobile )
{
// Cancel can't happen between this being moved to the head of the list, and it being Ticked.
throw new InvalidOperationException( "DeployMcvAction: Cancel() should never occur." );
}
} }
} }
} }

View File

@@ -30,6 +30,21 @@ namespace OpenRa.Game.Traits
currentAction = nextAction; currentAction = nextAction;
else else
currentAction.NextAction = nextAction; currentAction.NextAction = nextAction;
}
public void QueueAction( CurrentAction nextAction )
{
if( currentAction == null )
{
currentAction = nextAction;
return;
}
var act = currentAction;
while( act.NextAction != null )
{
act = act.NextAction;
}
act.NextAction = nextAction;
} }
public void Tick(Actor self) public void Tick(Actor self)
@@ -50,16 +65,17 @@ namespace OpenRa.Game.Traits
public interface CurrentAction public interface CurrentAction
{ {
CurrentAction NextAction { set; } CurrentAction NextAction { get; set; }
void Tick( Actor self, Mobile mobile ); void Tick( Actor self, Mobile mobile );
void Cancel( Actor self, Mobile mobile );
} }
public class Turn : CurrentAction public class Turn : CurrentAction
{ {
public CurrentAction NextAction { get; set; } public CurrentAction NextAction { get; set; }
public readonly int desiredFacing; public int desiredFacing;
public Turn( int desiredFacing ) public Turn( int desiredFacing )
{ {
@@ -76,6 +92,12 @@ namespace OpenRa.Game.Traits
return; return;
} }
Util.TickFacing( ref mobile.facing, desiredFacing, self.unitInfo.ROT ); Util.TickFacing( ref mobile.facing, desiredFacing, self.unitInfo.ROT );
}
public void Cancel( Actor self, Mobile mobile )
{
desiredFacing = mobile.facing;
NextAction = null;
} }
} }
@@ -87,7 +109,7 @@ namespace OpenRa.Game.Traits
List<int2> path; List<int2> path;
int moveFraction, moveFractionTotal; int moveFraction, moveFractionTotal;
float2 from, to; float2 from, to;
int fromFacing, toFacing; int fromFacing, toFacing;
Action<Actor, Mobile> OnComplete; Action<Actor, Mobile> OnComplete;
@@ -130,14 +152,14 @@ namespace OpenRa.Game.Traits
path.RemoveAt( path.Count - 1 ); path.RemoveAt( path.Count - 1 );
moveFractionTotal = ( dir.X != 0 && dir.Y != 0 ) ? 35 : 25; moveFractionTotal = ( dir.X != 0 && dir.Y != 0 ) ? 35 : 25;
from = CenterOfCell( mobile.fromCell ); from = CenterOfCell( mobile.fromCell );
to = BetweenCells( mobile.fromCell, mobile.toCell ); to = BetweenCells( mobile.fromCell, mobile.toCell );
CalculateMoveFraction(); CalculateMoveFraction();
fromFacing = mobile.facing; fromFacing = mobile.facing;
toFacing = mobile.facing; toFacing = mobile.facing;
OnComplete = OnCompleteFirstHalf; OnComplete = OnCompleteFirstHalf;
} }
mobile.currentAction.Tick( self, mobile ); mobile.currentAction.Tick( self, mobile );
} }
void TickMove( Actor self, Mobile mobile ) void TickMove( Actor self, Mobile mobile )
{ {
@@ -150,21 +172,21 @@ namespace OpenRa.Game.Traits
//mobile.fromCell = mobile.toCell; //mobile.fromCell = mobile.toCell;
} }
return; return;
} }
void UpdateCenterLocation( Actor self, Mobile mobile, float frac ) void UpdateCenterLocation( Actor self, Mobile mobile, float frac )
{ {
self.CenterLocation = float2.Lerp( from, to, frac ); self.CenterLocation = float2.Lerp( from, to, frac );
if( moveFraction >= moveFractionTotal ) if( moveFraction >= moveFractionTotal )
mobile.facing = toFacing; mobile.facing = toFacing;
else else
mobile.facing = ( fromFacing + ( toFacing - fromFacing ) * moveFraction / moveFractionTotal ) & 0xFF; mobile.facing = ( fromFacing + ( toFacing - fromFacing ) * moveFraction / moveFractionTotal ) & 0xFF;
} }
void CalculateMoveFraction() void CalculateMoveFraction()
{ {
var d = to - from; var d = to - from;
moveFractionTotal = (int)Math.Sqrt( d.X * d.X + d.Y * d.Y ) * (25 / 6); moveFractionTotal = (int)Math.Sqrt( d.X * d.X + d.Y * d.Y ) * (25 / 6);
} }
static float2 CenterOfCell( int2 loc ) static float2 CenterOfCell( int2 loc )
@@ -178,38 +200,44 @@ namespace OpenRa.Game.Traits
} }
void OnCompleteFirstHalf( Actor self, Mobile mobile ) void OnCompleteFirstHalf( Actor self, Mobile mobile )
{ {
if( path.Count > 0 ) if( path.Count > 0 )
{ {
var nextCell = path[ path.Count - 1 ]; var nextCell = path[ path.Count - 1 ];
if( ( nextCell - mobile.toCell ) != ( mobile.toCell - mobile.fromCell ) ) if( ( nextCell - mobile.toCell ) != ( mobile.toCell - mobile.fromCell ) )
{ {
path.RemoveAt( path.Count - 1 ); path.RemoveAt( path.Count - 1 );
from = BetweenCells( mobile.fromCell, mobile.toCell ); from = BetweenCells( mobile.fromCell, mobile.toCell );
to = BetweenCells( mobile.toCell, nextCell ); to = BetweenCells( mobile.toCell, nextCell );
CalculateMoveFraction(); CalculateMoveFraction();
mobile.fromCell = mobile.toCell; mobile.fromCell = mobile.toCell;
mobile.toCell = nextCell; mobile.toCell = nextCell;
fromFacing = mobile.facing; fromFacing = mobile.facing;
toFacing = Util.GetNearestFacing( fromFacing, Util.GetFacing( mobile.toCell-mobile.fromCell, fromFacing ) ); toFacing = Util.GetNearestFacing( fromFacing, Util.GetFacing( mobile.toCell-mobile.fromCell, fromFacing ) );
OnComplete = OnCompleteFirstHalf; OnComplete = OnCompleteFirstHalf;
return; return;
} }
} }
from = BetweenCells( mobile.fromCell, mobile.toCell ); from = BetweenCells( mobile.fromCell, mobile.toCell );
to = CenterOfCell( mobile.toCell ); to = CenterOfCell( mobile.toCell );
CalculateMoveFraction(); CalculateMoveFraction();
fromFacing = toFacing = mobile.facing; fromFacing = toFacing = mobile.facing;
OnComplete = OnCompleteSecondHalf; OnComplete = OnCompleteSecondHalf;
mobile.fromCell = mobile.toCell; mobile.fromCell = mobile.toCell;
} }
void OnCompleteSecondHalf( Actor self, Mobile mobile ) void OnCompleteSecondHalf( Actor self, Mobile mobile )
{ {
moveFractionTotal = 0; moveFractionTotal = 0;
self.CenterLocation = CenterOfCell( mobile.toCell ); self.CenterLocation = CenterOfCell( mobile.toCell );
OnComplete = null; OnComplete = null;
mobile.fromCell = mobile.toCell; mobile.fromCell = mobile.toCell;
}
public void Cancel( Actor self, Mobile mobile )
{
path.Clear();
NextAction = null;
} }
} }
} }