Replay stuff.

FYI: Don't try to replay a game where you built any vehicles. It WILL fail horribly.
This commit is contained in:
Bob
2009-10-29 01:57:47 +13:00
parent 7c5fc4aa76
commit 89f9a96de5
6 changed files with 308 additions and 225 deletions

View File

@@ -16,12 +16,14 @@ namespace OpenRa.Game
public readonly TypeDictionary traits = new TypeDictionary();
public readonly UnitInfo unitInfo;
public readonly uint ActorID;
public int2 Location;
public Player Owner;
public int Health;
public Actor( string name, int2 location, Player owner )
{
ActorID = Game.world.NextAID();
unitInfo = Rules.UnitInfo[ name ];
Location = location;
CenterLocation = new float2( 12, 12 ) + Game.CellSize * (float2)Location;

View File

@@ -13,13 +13,16 @@ namespace OpenRa.Game
{
public IOrderGenerator orderGenerator;
List<Order> recentOrders = new List<Order>();
void ApplyOrders(float2 xy, bool left)
{
var doVoice = null as Actor;
if (orderGenerator != null)
if( orderGenerator != null )
foreach( var order in orderGenerator.Order( xy.ToInt2(), left ) )
{
UnitOrders.ProcessOrder( order );
recentOrders.Add( order );
//UnitOrders.ProcessOrder( order );
if( order.Subject != null && order.Player == Game.LocalPlayer )
doVoice = order.Subject;
}
@@ -27,6 +30,13 @@ namespace OpenRa.Game
Game.PlaySound( Game.SovietVoices.First.GetNext() + GetVoiceSuffix( doVoice ), false );
}
public List<Order> GetRecentOrders()
{
var ret = recentOrders;
recentOrders = new List<Order>();
return ret;
}
static string GetVoiceSuffix( Actor unit )
{
var suffixes = new[] { ".r01", ".r03" };

View File

@@ -27,6 +27,8 @@ namespace OpenRa.Game
public static WorldRenderer worldRenderer;
public static Controller controller;
public static OrderManager orderManager;
static int localPlayerIndex;
public static Dictionary<int, Player> players = new Dictionary<int, Player>();
@@ -75,6 +77,8 @@ namespace OpenRa.Game
soundEngine = new ISoundEngine();
sounds = new Cache<string, ISoundSource>(LoadSound);
orderManager = new OrderManager( new OrderSource[] { new LocalOrderSource() }, "replay.rep" );
PlaySound("intro.aud", false);
}
@@ -129,6 +133,8 @@ namespace OpenRa.Game
UnitInfluence.Tick();
viewport.DrawRegions();
orderManager.Tick();
}
public static bool IsCellBuildable(int2 a, UnitMovementType umt)

View File

@@ -74,6 +74,7 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="OrderManager.cs" />
<Compile Include="Traits\Activities\DeployMcv.cs" />
<Compile Include="Actor.cs" />
<Compile Include="Bullet.cs" />

View File

@@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Text;
using System.Linq;
using OpenRa.Game.Traits;
using System.IO;
namespace OpenRa.Game
{
@@ -25,7 +26,64 @@ namespace OpenRa.Game
this.TargetString = targetString;
}
// TODO: serialize / deserialize
public byte[] Serialize()
{
switch( OrderString )
{
// Format:
// u32 : player, with msb set. (if msb is clear, not an order)
// u8 : orderID.
// 0xFF: Full serialized order.
// varies: rest of order.
default:
// TODO: specific serializers for specific orders.
{
var ret = new MemoryStream();
var w = new BinaryWriter(ret);
w.Write( (uint)Player.Palette | 0x80000000u );
w.Write( (byte)0xFF ); //
w.Write( OrderString );
w.Write( Subject == null ? 0xFFFFFFFF : Subject.ActorID );
w.Write( TargetActor == null ? 0xFFFFFFFF : TargetActor.ActorID );
w.Write( TargetLocation.X );
w.Write( TargetLocation.Y );
w.Write( TargetString != null );
if( TargetString != null )
w.Write( TargetString );
return ret.ToArray();
}
}
}
public static Order Deserialize( BinaryReader r, uint first )
{
if( ( first >> 31 ) == 0 ) return null;
var player = Game.players.Where( x => x.Value.Palette == (first & 0x7FFFFFFF) ).First().Value;
switch( r.ReadByte() )
{
case 0xFF:
{
var order = r.ReadString();
var subject = ActorFromUInt( r.ReadUInt32() );
var targetActor = ActorFromUInt( r.ReadUInt32() );
var targetLocation = new int2( r.ReadInt32(), 0 );
targetLocation.Y = r.ReadInt32();
var targetString = null as string;
if( r.ReadBoolean() )
targetString = r.ReadString();
return new Order( player, order, subject, targetActor, targetLocation, targetString );
}
default:
throw new NotImplementedException();
}
}
static Actor ActorFromUInt( uint aID )
{
if( aID == 0xFFFFFFFF ) return null;
return Game.world.Actors.Where( x => x.ActorID == aID ).First();
}
public static Order Attack( Actor subject, Actor target )
{

View File

@@ -50,5 +50,11 @@ namespace OpenRa.Game
public IEnumerable<Actor> Actors { get { return actors; } }
public IEnumerable<IEffect> Effects { get { return effects; } }
uint nextAID = 0;
internal uint NextAID()
{
return nextAID++;
}
}
}