Traits can now resolve orders (ATM, they all resolve the orders they issue)

This commit is contained in:
Bob
2009-11-25 20:42:03 +13:00
parent e9b87640bd
commit 7c1c0835e1
10 changed files with 117 additions and 82 deletions

View File

@@ -83,7 +83,7 @@ namespace OpenRa.Game
var underCursor = Game.UnitInfluence.GetUnitAt( xy ) ?? Game.BuildingInfluence.GetBuildingAt( xy ); var underCursor = Game.UnitInfluence.GetUnitAt( xy ) ?? Game.BuildingInfluence.GetBuildingAt( xy );
return traits.WithInterface<Traits.IOrder>() return traits.WithInterface<Traits.IOrder>()
.Select( x => x.Order( this, xy, lmb, underCursor ) ) .Select( x => x.IssueOrder( this, xy, lmb, underCursor ) )
.FirstOrDefault( x => x != null ); .FirstOrDefault( x => x != null );
} }
@@ -149,10 +149,10 @@ namespace OpenRa.Game
act.NextActivity = nextActivity; act.NextActivity = nextActivity;
} }
public void CancelActivity( Actor self ) public void CancelActivity()
{ {
if( currentActivity != null ) if( currentActivity != null )
currentActivity.Cancel( self ); currentActivity.Cancel( this );
} }
// For pathdebug, et al // For pathdebug, et al

View File

@@ -85,6 +85,8 @@ namespace OpenRa.Game
return Game.world.Actors.Where(x => x.ActorID == aID).First(); return Game.world.Actors.Where(x => x.ActorID == aID).First();
} }
// Named constructors for Orders.
// Now that Orders are resolved by individual Actors, these are weird; you unpack orders manually, but not pack them.
public static Order Chat(Player subject, string text) public static Order Chat(Player subject, string text)
{ {
return new Order(subject, "Chat", null, null, int2.Zero, text); return new Order(subject, "Chat", null, null, int2.Zero, text);

View File

@@ -69,11 +69,30 @@ namespace OpenRa.Game.Traits
return true; return true;
} }
public Order Order( Actor self, int2 xy, bool lmb, Actor underCursor ) public Order IssueOrder( Actor self, int2 xy, bool lmb, Actor underCursor )
{ {
if( lmb || underCursor == null ) return null; if( lmb || underCursor == null ) return null;
if( underCursor.Owner == self.Owner ) return null; if( underCursor.Owner == self.Owner ) return null;
return OpenRa.Game.Order.Attack( self, underCursor ); return Order.Attack( self, underCursor );
}
public void ResolveOrder( Actor self, Order order )
{
if( order.OrderString == "Attack" )
{
self.CancelActivity();
QueueAttack( self, order );
}
}
protected virtual void QueueAttack( Actor self, Order order )
{
const int RangeTolerance = 1; /* how far inside our maximum range we should try to sit */
/* todo: choose the appropriate weapon, when only one works against this target */
var weapon = order.Subject.unitInfo.Primary ?? order.Subject.unitInfo.Secondary;
self.QueueActivity( new Traits.Activities.Attack( order.TargetActor,
Math.Max( 0, (int)Rules.WeaponInfo[ weapon ].Range - RangeTolerance ) ) );
} }
} }
@@ -94,5 +113,16 @@ namespace OpenRa.Game.Traits
DoAttack( self ); DoAttack( self );
} }
protected override void QueueAttack( Actor self, Order order )
{
const int RangeTolerance = 1; /* how far inside our maximum range we should try to sit */
/* todo: choose the appropriate weapon, when only one works against this target */
var weapon = order.Subject.unitInfo.Primary ?? order.Subject.unitInfo.Secondary;
self.QueueActivity( new Traits.Activities.Follow( order.TargetActor,
Math.Max( 0, (int)Rules.WeaponInfo[ weapon ].Range - RangeTolerance ) ) );
self.traits.Get<AttackTurreted>().target = order.TargetActor;
}
} }
} }

View File

@@ -13,6 +13,8 @@ namespace OpenRa.Game.Traits
public bool IsFull { get { return oreCarried + gemsCarried == Rules.General.BailCount; } } public bool IsFull { get { return oreCarried + gemsCarried == Rules.General.BailCount; } }
public bool IsEmpty { get { return oreCarried == 0 && gemsCarried == 0; } } public bool IsEmpty { get { return oreCarried == 0 && gemsCarried == 0; } }
public Harvester( Actor self ) { }
public void AcceptResource(bool isGem) public void AcceptResource(bool isGem)
{ {
if (isGem) gemsCarried++; if (isGem) gemsCarried++;
@@ -27,21 +29,34 @@ namespace OpenRa.Game.Traits
gemsCarried = 0; gemsCarried = 0;
} }
public Order Order(Actor self, int2 xy, bool lmb, Actor underCursor) public Order IssueOrder(Actor self, int2 xy, bool lmb, Actor underCursor)
{ {
if (lmb) return null; if (lmb) return null;
if (underCursor != null if (underCursor != null
&& underCursor.Owner == self.Owner && underCursor.Owner == self.Owner
&& underCursor.traits.Contains<AcceptsOre>() && !IsEmpty) && underCursor.traits.Contains<AcceptsOre>() && !IsEmpty)
return OpenRa.Game.Order.DeliverOre(self, underCursor); return Order.DeliverOre(self, underCursor);
if (underCursor == null && Rules.Map.ContainsResource(xy)) if (underCursor == null && Rules.Map.ContainsResource(xy))
return OpenRa.Game.Order.Harvest(self, xy); return Order.Harvest(self, xy);
return null; return null;
} }
public Harvester(Actor self) { } public void ResolveOrder( Actor self, Order order )
{
if( order.OrderString == "Harvest" )
{
self.CancelActivity();
self.QueueActivity( new Traits.Activities.Move( order.TargetLocation, 0 ) );
self.QueueActivity( new Traits.Activities.Harvest() );
}
else if( order.OrderString == "DeliverOre" )
{
self.CancelActivity();
self.QueueActivity( new Traits.Activities.DeliverOre( order.TargetActor ) );
}
}
} }
} }

View File

@@ -18,16 +18,28 @@ namespace OpenRa.Game.Traits
targetLocation = self.Location; targetLocation = self.Location;
} }
public Order Order(Actor self, int2 xy, bool lmb, Actor underCursor) public Order IssueOrder(Actor self, int2 xy, bool lmb, Actor underCursor)
{ {
if (lmb) return null; if (lmb) return null;
if (underCursor == null) if (underCursor == null)
return OpenRa.Game.Order.Move(self, xy); return Order.Move(self, xy);
return null; return null;
} }
public void ResolveOrder( Actor self, Order order )
{
if( order.OrderString == "Move" )
{
targetLocation = order.TargetLocation;
var attackBase = self.traits.WithInterface<AttackBase>().FirstOrDefault();
if( attackBase != null )
attackBase.target = null; /* move cancels attack order */
}
}
public void Tick(Actor self) public void Tick(Actor self)
{ {
var unit = self.traits.Get<Unit>(); var unit = self.traits.Get<Unit>();

View File

@@ -10,12 +10,26 @@ namespace OpenRa.Game.Traits
{ {
public McvDeploy(Actor self) { } public McvDeploy(Actor self) { }
public Order Order(Actor self, int2 xy, bool lmb, Actor underCursor) public Order IssueOrder(Actor self, int2 xy, bool lmb, Actor underCursor)
{ {
if (lmb) return null; if (lmb) return null;
if( xy != self.Location ) return null; if( xy != self.Location ) return null;
return OpenRa.Game.Order.DeployMcv(self); return Order.DeployMcv(self);
}
public void ResolveOrder( Actor self, Order order )
{
if( order.OrderString == "DeployMcv" )
{
var factBuildingInfo = (UnitInfo.BuildingInfo)Rules.UnitInfo[ "fact" ];
if( Game.CanPlaceBuilding( factBuildingInfo, self.Location - new int2( 1, 1 ), self, false ) )
{
self.CancelActivity();
self.QueueActivity( new Traits.Activities.Turn( 96 ) );
self.QueueActivity( new Traits.Activities.DeployMcv() );
}
}
} }
} }
} }

View File

@@ -25,7 +25,7 @@ namespace OpenRa.Game.Traits
Game.UnitInfluence.Update( this ); Game.UnitInfluence.Update( this );
} }
public Order Order(Actor self, int2 xy, bool lmb, Actor underCursor) public Order IssueOrder(Actor self, int2 xy, bool lmb, Actor underCursor)
{ {
if( lmb ) return null; if( lmb ) return null;
@@ -34,7 +34,20 @@ namespace OpenRa.Game.Traits
if (xy == toCell) return null; if (xy == toCell) return null;
return OpenRa.Game.Order.Move( self, xy ); return Order.Move( self, xy );
}
public void ResolveOrder( Actor self, Order order )
{
if( order.OrderString == "Move" )
{
self.CancelActivity();
self.QueueActivity( new Traits.Activities.Move( order.TargetLocation, 8 ) );
var attackBase = self.traits.WithInterface<AttackBase>().FirstOrDefault();
if( attackBase != null )
attackBase.target = null; /* move cancels attack order */
}
} }
public IEnumerable<int2> OccupiedCells() public IEnumerable<int2> OccupiedCells()

View File

@@ -29,10 +29,16 @@ namespace OpenRa.Game.Traits
anim.Image, Game.CellSize * (new float2(.5f, .5f) + rallyPoint.ToFloat2())); anim.Image, Game.CellSize * (new float2(.5f, .5f) + rallyPoint.ToFloat2()));
} }
public Order Order(Actor self, int2 xy, bool lmb, Actor underCursor) public Order IssueOrder(Actor self, int2 xy, bool lmb, Actor underCursor)
{ {
if (lmb || underCursor != null) return null; if (lmb || underCursor != null) return null;
return OpenRa.Game.Order.SetRallyPoint(self, xy); return Order.SetRallyPoint(self, xy);
}
public void ResolveOrder( Actor self, Order order )
{
if( order.OrderString == "SetRallyPoint" )
rallyPoint = order.TargetLocation;
} }
public void Tick(Actor self) { anim.Tick(); } public void Tick(Actor self) { anim.Tick(); }

View File

@@ -11,8 +11,12 @@ namespace OpenRa.Game.Traits
interface ITick { void Tick(Actor self); } interface ITick { void Tick(Actor self); }
interface IRender { IEnumerable<Tuple<Sprite, float2, int>> Render(Actor self); } interface IRender { IEnumerable<Tuple<Sprite, float2, int>> Render(Actor self); }
interface IOrder { Order Order(Actor self, int2 xy, bool lmb, Actor underCursor); }
interface INotifyDamage { void Damaged(Actor self, DamageState ds); } interface INotifyDamage { void Damaged(Actor self, DamageState ds); }
interface INotifyDamageEx : INotifyDamage { void Damaged(Actor self, int damage); } interface INotifyDamageEx : INotifyDamage { void Damaged(Actor self, int damage); }
interface INotifyBuildComplete { void BuildingComplete (Actor self); } interface INotifyBuildComplete { void BuildingComplete (Actor self); }
interface IOrder
{
Order IssueOrder( Actor self, int2 xy, bool lmb, Actor underCursor );
void ResolveOrder( Actor self, Order order );
}
} }

View File

@@ -14,69 +14,14 @@ namespace OpenRa.Game
switch( order.OrderString ) switch( order.OrderString )
{ {
case "Move": case "Move":
{
var mobile = order.Subject.traits.GetOrDefault<Mobile>();
if (mobile != null)
{
order.Subject.CancelActivity( order.Subject );
order.Subject.QueueActivity( new Traits.Activities.Move( order.TargetLocation, 8 ) );
}
var heli = order.Subject.traits.GetOrDefault<Helicopter>();
if (heli != null)
heli.targetLocation = order.TargetLocation;
var attackBase = order.Subject.traits.WithInterface<AttackBase>().FirstOrDefault();
if( attackBase != null )
attackBase.target = null; /* move cancels attack order */
break;
}
case "Attack": case "Attack":
{
const int RangeTolerance = 1; /* how far inside our maximum range we should try to sit */
var mobile = order.Subject.traits.GetOrDefault<Mobile>();
/* todo: choose the appropriate weapon, when only one works against this target */
var weapon = order.Subject.unitInfo.Primary ?? order.Subject.unitInfo.Secondary;
order.Subject.CancelActivity(order.Subject);
if (order.Subject.traits.Contains<AttackTurreted>())
{
order.Subject.QueueActivity(
new Traits.Activities.Follow(order.TargetActor,
Math.Max(0, (int)Rules.WeaponInfo[weapon].Range - RangeTolerance)));
order.Subject.traits.Get<AttackTurreted>().target = order.TargetActor;
}
else
{
order.Subject.QueueActivity(
new Traits.Activities.Attack(order.TargetActor,
Math.Max(0, (int)Rules.WeaponInfo[weapon].Range - RangeTolerance)));
}
break;
}
case "DeployMcv": case "DeployMcv":
{
var factBuildingInfo = (UnitInfo.BuildingInfo)Rules.UnitInfo[ "fact" ];
if( !Game.CanPlaceBuilding( factBuildingInfo, order.Subject.Location - new int2( 1, 1 ), order.Subject, false ) )
break; /* throw the order on the floor */
order.Subject.CancelActivity( order.Subject );
order.Subject.QueueActivity( new Traits.Activities.Turn( 96 ) );
order.Subject.QueueActivity( new Traits.Activities.DeployMcv() );
break;
}
case "DeliverOre": case "DeliverOre":
{
order.Subject.CancelActivity( order.Subject );
order.Subject.QueueActivity( new Traits.Activities.DeliverOre( order.TargetActor ) );
break;
}
case "Harvest": case "Harvest":
case "SetRallyPoint":
{ {
order.Subject.CancelActivity( order.Subject ); foreach( var t in order.Subject.traits.WithInterface<IOrder>() )
order.Subject.QueueActivity( new Traits.Activities.Move( order.TargetLocation, 0 ) ); t.ResolveOrder( order.Subject, order );
order.Subject.QueueActivity( new Traits.Activities.Harvest() );
break; break;
} }
case "PlaceBuilding": case "PlaceBuilding":
@@ -142,12 +87,6 @@ namespace OpenRa.Game
order.Player.CancelProduction( Rules.UnitCategory[ order.TargetString ] ); order.Player.CancelProduction( Rules.UnitCategory[ order.TargetString ] );
break; break;
} }
case "SetRallyPoint":
{
var pt = order.Subject.traits.Get<RallyPoint>();
pt.rallyPoint = order.TargetLocation;
break;
}
case "Chat": case "Chat":
{ {
Game.chat.AddLine(Pair.New(order.Player.PlayerName + ":", order.TargetString)); Game.chat.AddLine(Pair.New(order.Player.PlayerName + ":", order.TargetString));