git-svn-id: svn://svn.ijw.co.nz/svn/OpenRa@1365 993157c7-ee19-0410-b2c4-bb4e9862e678
This commit is contained in:
147
OpenRa.Game/UnitMissions.cs
Normal file
147
OpenRa.Game/UnitMissions.cs
Normal file
@@ -0,0 +1,147 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace OpenRa.Game
|
||||
{
|
||||
//Unit Missions:
|
||||
//{
|
||||
//in rules.ini:
|
||||
// Sleep - no-op
|
||||
// Harmless - no-op, and also not considered a threat
|
||||
// Sticky
|
||||
// Attack
|
||||
// Move
|
||||
// QMove
|
||||
// Retreat
|
||||
// Guard
|
||||
// Enter
|
||||
// Capture
|
||||
// Harvest
|
||||
// Area Guard
|
||||
// [Return] - unused
|
||||
// Stop
|
||||
// [Ambush] - unused
|
||||
// Hunt
|
||||
// Unload
|
||||
// Sabotage
|
||||
// Construction
|
||||
// Selling
|
||||
// Repair
|
||||
// Rescue
|
||||
// Missile
|
||||
//
|
||||
//not in original RA:
|
||||
// Deploy (Mcv -> Fact) [should this be construction/unload?]
|
||||
//}
|
||||
|
||||
[Flags]
|
||||
enum SupportedMissions
|
||||
{
|
||||
Stop = 0,
|
||||
Harvest = 1,
|
||||
Deploy = 2,
|
||||
}
|
||||
|
||||
delegate void UnitMission( int t );
|
||||
static class UnitMissions
|
||||
{
|
||||
public static UnitMission Sleep()
|
||||
{
|
||||
return delegate { };
|
||||
}
|
||||
|
||||
public static UnitMission Move( Unit unit, int2 destination )
|
||||
{
|
||||
return delegate( int t )
|
||||
{
|
||||
Game game = unit.game;
|
||||
|
||||
if( unit.nextOrder != null )
|
||||
destination = unit.toCell;
|
||||
|
||||
if( Turn( unit, unit.GetFacing( unit.toCell - unit.fromCell ) ) )
|
||||
return;
|
||||
|
||||
unit.moveFraction += t * unit.unitInfo.Speed;
|
||||
if( unit.moveFraction < unit.moveFractionTotal )
|
||||
return;
|
||||
|
||||
unit.moveFraction = 0;
|
||||
unit.moveFractionTotal = 0;
|
||||
unit.fromCell = unit.toCell;
|
||||
|
||||
if( unit.toCell == destination )
|
||||
{
|
||||
unit.currentOrder = null;
|
||||
return;
|
||||
}
|
||||
|
||||
List<int2> res = game.pathFinder.FindUnitPath( unit.toCell, PathFinder.DefaultEstimator( destination ) );
|
||||
if( res.Count != 0 )
|
||||
{
|
||||
unit.toCell = res[ res.Count - 1 ];
|
||||
|
||||
int2 dir = unit.toCell - unit.fromCell;
|
||||
unit.moveFractionTotal = ( dir.X != 0 && dir.Y != 0 ) ? 2500 : 2000;
|
||||
}
|
||||
else
|
||||
destination = unit.toCell;
|
||||
};
|
||||
}
|
||||
|
||||
public static UnitMission Deploy( Unit unit )
|
||||
{
|
||||
return delegate( int t )
|
||||
{
|
||||
Game game = unit.game;
|
||||
|
||||
if( Turn( unit, 12 ) )
|
||||
return;
|
||||
|
||||
game.world.AddFrameEndTask( delegate
|
||||
{
|
||||
game.world.Remove( unit );
|
||||
game.world.Add( new ConstructionYard( unit.fromCell - new int2( 1, 1 ), unit.owner, game ) );
|
||||
} );
|
||||
unit.currentOrder = null;
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
public static UnitMission Harvest( Unit unit )
|
||||
{
|
||||
UnitMission order = null;
|
||||
order = delegate
|
||||
{
|
||||
// TODO: check that there's actually some ore in this cell :)
|
||||
|
||||
// face in one of the 8 directions
|
||||
if( Turn( unit, ( unit.facing + 1 ) & ~3 ) )
|
||||
return;
|
||||
|
||||
unit.currentOrder = delegate { };
|
||||
if( unit.nextOrder == null )
|
||||
unit.nextOrder = order;
|
||||
|
||||
string sequenceName = string.Format( "harvest{0}", unit.facing / 4 );
|
||||
unit.animation.PlayThen( sequenceName, delegate
|
||||
{
|
||||
unit.currentOrder = null;
|
||||
unit.animation.PlayFetchIndex( "idle", delegate { return unit.facing; } );
|
||||
} );
|
||||
};
|
||||
return order;
|
||||
}
|
||||
|
||||
static bool Turn( Unit unit, int desiredFacing )
|
||||
{
|
||||
if( unit.facing == desiredFacing )
|
||||
return false;
|
||||
|
||||
int df = ( desiredFacing - unit.facing + 32 ) % 32;
|
||||
unit.facing = ( unit.facing + ( df > 16 ? 31 : 1 ) ) % 32;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user