Harvesting works better, and other related stuff.

This commit is contained in:
Bob
2009-11-05 13:23:23 +13:00
parent 7e0b0541e2
commit edc4a8e6e7
12 changed files with 191 additions and 143 deletions

View File

@@ -21,8 +21,9 @@ namespace OpenRa.Game.Traits.Activities
harv.gemsCarried = 0;
harv.oreCarried = 0;
if( NextActivity == null )
NextActivity = new Harvest();
mobile.InternalSetActivity(NextActivity);
/* todo: return to the ore patch */
return;
}

View File

@@ -12,17 +12,27 @@ namespace OpenRa.Game.Traits.Activities
public void Tick(Actor self, Mobile mobile)
{
if( NextActivity != null )
{
mobile.InternalSetActivity( NextActivity );
NextActivity.Tick( self, mobile );
return;
}
var harv = self.traits.Get<Harvester>();
var isGem = false;
if (!harv.IsFull &&
Game.map.ContainsResource(self.Location) &&
if (!harv.IsFull &&
Game.map.ContainsResource(self.Location) &&
Game.map.Harvest(self.Location, out isGem))
{
var harvestAnim = "harvest" + Util.QuantizeFacing(mobile.facing, 8);
var renderUnit = self.traits.WithInterface<RenderUnit>().First(); /* better have one of these! */
if (harvestAnim != renderUnit.anim.CurrentSequence.Name)
renderUnit.PlayCustomAnimation(self, harvestAnim, () => isHarvesting = false);
if( harvestAnim != renderUnit.anim.CurrentSequence.Name )
{
isHarvesting = true;
renderUnit.PlayCustomAnimation( self, harvestAnim, () => isHarvesting = false );
}
harv.AcceptResource(isGem);
return;
}
@@ -35,23 +45,19 @@ namespace OpenRa.Game.Traits.Activities
PlanMoreHarvesting(self, mobile);
}
/* maybe this doesnt really belong here, since it's the
/* maybe this doesnt really belong here, since it's the
* same as what UnitOrders has to do for an explicit return */
void PlanReturnToBase(Actor self, Mobile mobile)
void PlanReturnToBase(Actor self, Mobile mobile)
{
/* find a proc */
var proc = ChooseReturnLocation(self);
if (proc == null)
if( proc != null )
{
Cancel(self, mobile); /* is this a sane way to cancel? */
return;
mobile.QueueActivity( new Move( proc.Location + new int2( 1, 2 ), 0 ) );
mobile.QueueActivity( new Turn( 64 ) );
mobile.QueueActivity( new DeliverOre() );
}
mobile.QueueActivity(new Move(proc.Location + new int2(1, 2)));
mobile.QueueActivity(new Turn(64));
mobile.QueueActivity(new DeliverOre());
mobile.InternalSetActivity(NextActivity);
}
@@ -72,12 +78,24 @@ namespace OpenRa.Game.Traits.Activities
/* find a nearby patch */
/* todo: add the queries we need to support this! */
var path = Game.PathFinder.FindUnitPath( self.Location, loc =>
{
if( Game.UnitInfluence.GetUnitAt( loc ) != null ) return double.PositiveInfinity;
return Game.map.ContainsResource( loc ) ? 0 : 1;
}, UnitMovementType.Wheel )
.TakeWhile( a => a != self.Location )
.ToList();
if( path.Count != 0 )
{
mobile.QueueActivity( new Move( path ) );
mobile.QueueActivity( new Harvest() );
}
mobile.InternalSetActivity(NextActivity);
}
public void Cancel(Actor self, Mobile mobile)
{
mobile.InternalSetActivity(null); /* bob: anything else required? */
}
}
}

View File

@@ -10,17 +10,19 @@ namespace OpenRa.Game.Traits.Activities
public Activity NextActivity { get; set; }
int2? destination;
int nearEnough;
public List<int2> path;
Func<Actor, Mobile, List<int2>> getPath;
MovePart move;
public Move( int2 destination )
public Move( int2 destination, int nearEnough )
{
this.getPath = ( self, mobile ) => Game.PathFinder.FindUnitPath(
self.Location, destination,
mobile.GetMovementType() );
this.destination = destination;
this.nearEnough = nearEnough;
}
public Move( Actor target, int range )
@@ -29,6 +31,13 @@ namespace OpenRa.Game.Traits.Activities
self.Location, target.Location,
mobile.GetMovementType(), range );
this.destination = null;
this.nearEnough = range;
}
public Move( List<int2> path )
{
this.path = path;
this.destination = path[ 0 ];
}
static bool CanEnterCell( int2 c, Actor self )
@@ -98,7 +107,7 @@ namespace OpenRa.Game.Traits.Activities
var nextCell = path[ path.Count - 1 ];
if( !CanEnterCell( nextCell, self ) )
{
if( ( mobile.toCell - destination.Value ).LengthSquared <= 8 )
if( ( mobile.toCell - destination.Value ).LengthSquared <= nearEnough )
{
path.Clear();
return null;
@@ -120,6 +129,11 @@ namespace OpenRa.Game.Traits.Activities
if( path.Count == 0 )
return null;
nextCell = path[ path.Count - 1 ];
if( !CanEnterCell( nextCell, self ) )
{
path.Clear();
return null;
}
}
path.RemoveAt( path.Count - 1 );
return nextCell;