Restructuring orders.

This commit is contained in:
Bob
2009-10-28 22:06:39 +13:00
parent 5d1ae1cb2a
commit c0cb248bc1
14 changed files with 632 additions and 621 deletions

View File

@@ -0,0 +1,27 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace OpenRa.Game.Traits.Activities
{
class DeployMcv : Mobile.CurrentActivity
{
public Mobile.CurrentActivity NextActivity { get; set; }
public void Tick( Actor self, Mobile mobile )
{
Game.world.AddFrameEndTask( _ =>
{
Game.world.Remove( self );
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

@@ -1,111 +1,79 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace OpenRa.Game.Traits
{
abstract class AttackBase : IOrder
{
public Actor target;
// time (in frames) until each weapon can fire again.
protected int primaryFireDelay = 0;
protected int secondaryFireDelay = 0;
protected bool CanAttack( Actor self )
{
if( primaryFireDelay > 0 ) --primaryFireDelay;
if( secondaryFireDelay > 0 ) --secondaryFireDelay;
if( target != null && target.IsDead ) target = null; /* he's dead, jim. */
if( target == null ) return false;
return true;
}
protected void DoAttack( Actor self )
{
if( self.unitInfo.Primary != null && CheckFire( self, self.unitInfo.Primary, ref primaryFireDelay ) )
{
secondaryFireDelay = Math.Max( 4, secondaryFireDelay );
return;
}
if( self.unitInfo.Secondary != null && CheckFire( self, self.unitInfo.Secondary, ref secondaryFireDelay ) )
return;
}
bool CheckFire( Actor self, string weaponName, ref int fireDelay )
{
if( fireDelay > 0 ) return false;
var weapon = Rules.WeaponInfo[ weaponName ];
if( weapon.Range * weapon.Range < ( target.Location - self.Location ).LengthSquared ) return false;
fireDelay = weapon.ROF;
Game.world.Add( new Bullet( weaponName, self.Owner, self,
self.CenterLocation.ToInt2(),
target.CenterLocation.ToInt2() ) );
return true;
}
public Order Order( Actor self, int2 xy, bool lmb, Actor underCursor )
{
if( lmb || underCursor == null ) return null;
if( underCursor.Owner == self.Owner ) return null;
return new AttackOrder( self, underCursor );
}
}
class AttackTurreted : AttackBase, ITick
{
public AttackTurreted( Actor self ) { self.traits.Get<Turreted>(); }
public void Tick(Actor self)
{
if( !CanAttack( self ) ) return;
var turreted = self.traits.Get<Turreted>();
turreted.desiredFacing = Util.GetFacing( target.CenterLocation - self.CenterLocation, turreted.turretFacing );
if( turreted.desiredFacing != turreted.turretFacing )
return;
DoAttack( self );
}
}
class AttackOrder : Order
{
public readonly Actor Attacker;
public readonly Actor Target;
const int RangeTolerance = 1; /* how far inside our maximum range we should try to sit */
public AttackOrder( Actor attacker, Actor target )
{
this.Attacker = attacker;
this.Target = target;
}
public override void Apply( bool doVoice )
{
var mobile = Attacker.traits.GetOrDefault<Mobile>();
if (mobile != null)
{
var weapon = Attacker.unitInfo.Primary ?? Attacker.unitInfo.Secondary;
/* todo: choose the appropriate weapon, when only one works against this target */
var range = Rules.WeaponInfo[weapon].Range;
mobile.Cancel(Attacker);
mobile.QueueActivity(
new Mobile.MoveTo(Target,
Math.Max(0, (int)range - RangeTolerance)));
}
Attacker.traits.Get<AttackTurreted>().target = Target;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace OpenRa.Game.Traits
{
abstract class AttackBase : IOrder
{
public Actor target;
// time (in frames) until each weapon can fire again.
protected int primaryFireDelay = 0;
protected int secondaryFireDelay = 0;
protected bool CanAttack( Actor self )
{
if( primaryFireDelay > 0 ) --primaryFireDelay;
if( secondaryFireDelay > 0 ) --secondaryFireDelay;
if( target != null && target.IsDead ) target = null; /* he's dead, jim. */
if( target == null ) return false;
return true;
}
protected void DoAttack( Actor self )
{
if( self.unitInfo.Primary != null && CheckFire( self, self.unitInfo.Primary, ref primaryFireDelay ) )
{
secondaryFireDelay = Math.Max( 4, secondaryFireDelay );
return;
}
if( self.unitInfo.Secondary != null && CheckFire( self, self.unitInfo.Secondary, ref secondaryFireDelay ) )
return;
}
bool CheckFire( Actor self, string weaponName, ref int fireDelay )
{
if( fireDelay > 0 ) return false;
var weapon = Rules.WeaponInfo[ weaponName ];
if( weapon.Range * weapon.Range < ( target.Location - self.Location ).LengthSquared ) return false;
fireDelay = weapon.ROF;
Game.world.Add( new Bullet( weaponName, self.Owner, self,
self.CenterLocation.ToInt2(),
target.CenterLocation.ToInt2() ) );
return true;
}
public Order Order( Actor self, int2 xy, bool lmb, Actor underCursor )
{
if( lmb || underCursor == null ) return null;
if( underCursor.Owner == self.Owner ) return null;
return OpenRa.Game.Order.Attack( self, underCursor );
}
}
class AttackTurreted : AttackBase, ITick
{
public AttackTurreted( Actor self ) { self.traits.Get<Turreted>(); }
public void Tick(Actor self)
{
if( !CanAttack( self ) ) return;
var turreted = self.traits.Get<Turreted>();
turreted.desiredFacing = Util.GetFacing( target.CenterLocation - self.CenterLocation, turreted.turretFacing );
if( turreted.desiredFacing != turreted.turretFacing )
return;
DoAttack( self );
}
}
}

View File

@@ -1,64 +1,25 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace OpenRa.Game.Traits
{
class McvDeploy : IOrder
{
public McvDeploy(Actor self)
{
}
public Order Order(Actor self, int2 xy, bool lmb, Actor underCursor)
{
if( lmb ) return null;
// TODO: check that there's enough space at the destination.
if( xy == self.Location )
return new DeployMcvOrder( self, xy );
return null;
}
}
class DeployMcvOrder : Order
{
Actor Unit;
int2 Location;
public DeployMcvOrder( Actor unit, int2 location )
{
Unit = unit;
Location = location;
}
public override void Apply( bool doVoice )
{
var mobile = Unit.traits.Get<Mobile>();
mobile.QueueActivity( new Mobile.Turn( 96 ) );
mobile.QueueActivity( new DeployAction() );
}
class DeployAction : Mobile.CurrentActivity
{
public Mobile.CurrentActivity NextActivity { get; set; }
public void Tick( Actor self, Mobile mobile )
{
Game.world.AddFrameEndTask( _ =>
{
Game.world.Remove( self );
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." );
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace OpenRa.Game.Traits
{
class McvDeploy : IOrder
{
public McvDeploy(Actor self)
{
}
public Order Order(Actor self, int2 xy, bool lmb, Actor underCursor)
{
if( lmb ) return null;
// TODO: check that there's enough space at the destination.
if( xy == self.Location )
return OpenRa.Game.Order.DeployMcv( self );
return null;
}
}
}

View File

@@ -21,7 +21,7 @@ namespace OpenRa.Game.Traits
public Mobile(Actor self)
{
this.self = self;
fromCell = toCell;
fromCell = toCell;
Game.UnitInfluence.Update( this );
}
@@ -55,8 +55,8 @@ namespace OpenRa.Game.Traits
if( underCursor != null )
return null;
if (xy != toCell)
return new MoveOrder(self, xy);
if( xy != toCell )
return OpenRa.Game.Order.Move( self, xy );
return null;
}