unit idle tracking via dummy activity; wasn't so bad.

This commit is contained in:
Chris Forbes
2009-12-28 08:26:13 +13:00
parent 47f4815205
commit b7bac5bd16
11 changed files with 83 additions and 48 deletions

View File

@@ -48,17 +48,23 @@ namespace OpenRa.Game
public void Tick()
{
var nextActivity = currentActivity;
while( nextActivity != null )
while (currentActivity != null)
{
currentActivity = nextActivity;
nextActivity = nextActivity.Tick( this );
var a = currentActivity;
currentActivity = a.Tick(this) ?? new Idle();
if (a == currentActivity) break;
}
foreach (var tick in traits.WithInterface<ITick>())
tick.Tick(this);
}
public bool IsIdle
{
get { return currentActivity == null || currentActivity is Idle; }
}
public float2 CenterLocation;
public float2 SelectedSize
{

View File

@@ -132,6 +132,7 @@
<Compile Include="Graphics\LineRenderer.cs" />
<Compile Include="Graphics\OverlayRenderer.cs" />
<Compile Include="Graphics\WorldRenderer.cs" />
<Compile Include="Traits\Activities\Idle.cs" />
<Compile Include="Traits\Activities\Teleport.cs" />
<Compile Include="BuildingInfluenceMap.cs" />
<Compile Include="IOrderGenerator.cs" />

View File

@@ -40,7 +40,7 @@ namespace OpenRa.Game.Traits.Activities
var attack = self.traits.WithInterface<AttackBase>().First();
attack.target = Target;
attack.DoAttack(self);
return null;
return this;
}
public void Cancel(Actor self)

View File

@@ -59,7 +59,7 @@ namespace OpenRa.Game.Traits.Activities
}
else
// no refineries reachable?
return null;
return this;
}
else if( unit.Facing != 64 )
return new Turn( 64 ) { NextActivity = this };
@@ -69,7 +69,7 @@ namespace OpenRa.Game.Traits.Activities
renderUnit.PlayCustomAnimation( self, "empty",
() => isDone = true );
return null;
return this;
}
public void Cancel(Actor self)

View File

@@ -15,7 +15,7 @@ namespace OpenRa.Game.Traits.Activities
Sound.Play("build5.aud");
Game.world.Add( new Actor( Rules.UnitInfo["fact"], self.Location - new int2( 1, 1 ), self.Owner ) );
} );
return null;
return this;
}
public void Cancel( Actor self )

View File

@@ -38,7 +38,7 @@ namespace OpenRa.Game.Traits.Activities
self.CenterLocation += speed * -float2.FromAngle((float)angle);
self.Location = ((1 / 24f) * self.CenterLocation).ToInt2();
return null;
return this;
}
public void Cancel(Actor self) { isCanceled = true; NextActivity = null; }
@@ -74,7 +74,7 @@ namespace OpenRa.Game.Traits.Activities
self.CenterLocation += speed * -float2.FromAngle((float)angle);
self.Location = ((1 / 24f) * self.CenterLocation).ToInt2();
return null;
return this;
}
public void Cancel(Actor self) { isCanceled = true; NextActivity = null; }
@@ -103,7 +103,7 @@ namespace OpenRa.Game.Traits.Activities
self.Location = ((1 / 24f) * self.CenterLocation).ToInt2();
unit.Altitude += Math.Sign(targetAltitude - unit.Altitude);
return null;
return this;
}
public void Cancel(Actor self) { remainingTicks = 0; NextActivity = null; }

View File

@@ -24,7 +24,7 @@ namespace OpenRa.Game.Traits.Activities
if( !inRange )
return new Move( Target, Range ) { NextActivity = this };
return null;
return this;
}
public void Cancel(Actor self)

View File

@@ -12,49 +12,62 @@ namespace OpenRa.Game.Traits.Activities
var unit = self.traits.Get<Unit>();
var mobile = self.traits.Get<Mobile>();
if( isHarvesting ) return null;
if( NextActivity != null )
return NextActivity;
if( isHarvesting ) return this;
if( NextActivity != null ) return NextActivity;
var harv = self.traits.Get<Harvester>();
if( harv.IsFull )
return new DeliverOre { NextActivity = NextActivity };
var isGem = false;
if( Rules.Map.ContainsResource( self.Location ) &&
Rules.Map.Harvest( self.Location, out isGem ) )
{
var harvestAnim = "harvest" + Util.QuantizeFacing( unit.Facing, 8 );
var renderUnit = self.traits.WithInterface<RenderUnit>().First(); /* better have one of these! */
if( harvestAnim != renderUnit.anim.CurrentSequence.Name )
{
isHarvesting = true;
renderUnit.PlayCustomAnimation( self, harvestAnim, () => isHarvesting = false );
}
harv.AcceptResource( isGem );
return null;
}
if (HarvestThisTile(self))
return this;
else
{
self.QueueActivity( new Move(
() =>
{
var search = new PathSearch
{
heuristic = loc => ( Rules.Map.ContainsResource( loc ) ? 0 : 1 ),
umt = UnitMovementType.Wheel,
checkForBlocked = true
};
search.AddInitialCell( self.Location );
return Game.PathFinder.FindPath( search );
} ) );
self.QueueActivity( new Harvest() );
FindMoreOre(self);
return NextActivity;
}
}
bool HarvestThisTile(Actor self)
{
var unit = self.traits.Get<Unit>();
var harv = self.traits.Get<Harvester>();
var renderUnit = self.traits.WithInterface<RenderUnit>().First(); /* better have one of these! */
var isGem = false;
if (!Rules.Map.ContainsResource(self.Location) ||
!Rules.Map.Harvest(self.Location, out isGem))
return false;
var harvestAnim = "harvest" + Util.QuantizeFacing(unit.Facing, 8);
if (harvestAnim != renderUnit.anim.CurrentSequence.Name)
{
isHarvesting = true;
renderUnit.PlayCustomAnimation(self, harvestAnim, () => isHarvesting = false);
}
harv.AcceptResource(isGem);
return true;
}
void FindMoreOre(Actor self)
{
self.QueueActivity(new Move(
() =>
{
var search = new PathSearch
{
heuristic = loc => (Rules.Map.ContainsResource(loc) ? 0 : 1),
umt = UnitMovementType.Wheel,
checkForBlocked = true
};
search.AddInitialCell(self.Location);
return Game.PathFinder.FindPath(search);
}));
self.QueueActivity(new Harvest());
}
public void Cancel(Actor self) { }
}
}

View File

@@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace OpenRa.Game.Traits.Activities
{
class Idle : IActivity
{
public IActivity NextActivity { get; set; }
public IActivity Tick(Actor self) { return NextActivity; }
public void Cancel(Actor self) {}
}
}

View File

@@ -59,7 +59,7 @@ namespace OpenRa.Game.Traits.Activities
if( move != null )
{
move.TickMove( self, mobile, this );
return null;
return this;
}
if( destination == self.Location )
@@ -74,14 +74,14 @@ namespace OpenRa.Game.Traits.Activities
if( path.Count == 0 )
{
destination = mobile.toCell;
return null;
return this;
}
destination = path[ 0 ];
var nextCell = PopPath( self, mobile );
if( nextCell == null )
return null;
return NextActivity;
int2 dir = nextCell.Value - mobile.fromCell;
var firstFacing = Util.GetFacing( dir, unit.Facing );
@@ -103,7 +103,7 @@ namespace OpenRa.Game.Traits.Activities
move.TickMove( self, mobile, this );
return null;
return this;
}
}

View File

@@ -20,7 +20,7 @@ namespace OpenRa.Game.Traits.Activities
return NextActivity;
Util.TickFacing( ref unit.Facing, desiredFacing, self.Info.ROT );
return null;
return this;
}
public void Cancel( Actor self )