Merge branch 'master' of git://github.com/chrisforbes/OpenRA into cpu-independent

This commit is contained in:
Matthew Bowra-Dean
2010-01-20 15:01:28 +13:00
9 changed files with 48 additions and 46 deletions

View File

@@ -47,8 +47,7 @@ namespace OpenRa
{ {
if (orderGenerator == null) return; if (orderGenerator == null) return;
var orders = orderGenerator.Order(xy.ToInt2(), mi) var orders = orderGenerator.Order(xy.ToInt2(), mi).ToArray();
.Where(order => order.Validate()).ToArray();
recentOrders.AddRange( orders ); recentOrders.AddRange( orders );
var voicedActor = orders.Select(o => o.Subject) var voicedActor = orders.Select(o => o.Subject)

View File

@@ -5,7 +5,7 @@ namespace OpenRa.Orders
interface IOrderSource interface IOrderSource
{ {
void SendLocalOrders(int localFrame, List<Order> localOrders); void SendLocalOrders(int localFrame, List<Order> localOrders);
List<Order> OrdersForFrame(int currentFrame); List<byte[]> OrdersForFrame(int currentFrame);
bool IsReadyForFrame(int frameNumber); bool IsReadyForFrame(int frameNumber);
} }
} }

View File

@@ -1,15 +1,16 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
namespace OpenRa.Orders namespace OpenRa.Orders
{ {
class LocalOrderSource : IOrderSource class LocalOrderSource : IOrderSource
{ {
Dictionary<int, List<Order>> orders = new Dictionary<int, List<Order>>(); Dictionary<int, List<byte[]>> orders = new Dictionary<int, List<byte[]>>();
public List<Order> OrdersForFrame(int currentFrame) public List<byte[]> OrdersForFrame(int currentFrame)
{ {
if (!orders.ContainsKey(currentFrame)) if (!orders.ContainsKey(currentFrame))
return new List<Order>(); return new List<byte[]>();
var result = orders[currentFrame]; var result = orders[currentFrame];
orders.Remove(currentFrame); orders.Remove(currentFrame);
@@ -19,7 +20,7 @@ namespace OpenRa.Orders
public void SendLocalOrders(int localFrame, List<Order> localOrders) public void SendLocalOrders(int localFrame, List<Order> localOrders)
{ {
if (localFrame == 0) return; if (localFrame == 0) return;
orders[localFrame] = localOrders; orders[localFrame] = localOrders.Select(o=>o.Serialize()).ToList();
} }
public bool IsReadyForFrame(int frameNumber) public bool IsReadyForFrame(int frameNumber)

View File

@@ -92,10 +92,9 @@ namespace OpenRa.Orders
} }
} }
public List<Order> OrdersForFrame(int currentFrame) public List<byte[]> OrdersForFrame(int currentFrame)
{ {
var orderData = ExtractOrders(currentFrame); return ExtractOrders(currentFrame).ToList();
return orderData.SelectMany(a => a.ToOrderList()).ToList();
} }
public void SendLocalOrders(int localFrame, List<Order> localOrders) public void SendLocalOrders(int localFrame, List<Order> localOrders)

View File

@@ -8,41 +8,24 @@ namespace OpenRa
public sealed class Order public sealed class Order
{ {
public readonly string OrderString; public readonly string OrderString;
readonly uint SubjectId; public readonly Actor Subject;
readonly uint TargetActorId; public readonly Actor TargetActor;
public readonly int2 TargetLocation; public readonly int2 TargetLocation;
public readonly string TargetString; public readonly string TargetString;
public bool IsImmediate; public bool IsImmediate;
public Actor Subject { get { return ActorFromUInt(SubjectId); } }
public Actor TargetActor { get { return ActorFromUInt(TargetActorId); } }
public Player Player { get { return Subject.Owner; } } public Player Player { get { return Subject.Owner; } }
public Order(string orderString, Actor subject, public Order(string orderString, Actor subject,
Actor targetActor, int2 targetLocation, string targetString) Actor targetActor, int2 targetLocation, string targetString)
: this( orderString, UIntFromActor( subject ),
UIntFromActor( targetActor ), targetLocation, targetString) {}
Order(string orderString, uint subjectId,
uint targetActorId, int2 targetLocation, string targetString)
{ {
this.OrderString = orderString; this.OrderString = orderString;
this.SubjectId = subjectId; this.Subject = subject;
this.TargetActorId = targetActorId; this.TargetActor = targetActor;
this.TargetLocation = targetLocation; this.TargetLocation = targetLocation;
this.TargetString = targetString; this.TargetString = targetString;
} }
public bool Validate()
{
if ((SubjectId != 0xffffffff) && Subject == null)
return false;
if ((TargetActorId != 0xffffffff) && TargetActor == null)
return false;
return true;
}
public byte[] Serialize() public byte[] Serialize()
{ {
if (IsImmediate) /* chat, whatever */ if (IsImmediate) /* chat, whatever */
@@ -69,8 +52,8 @@ namespace OpenRa
var w = new BinaryWriter(ret); var w = new BinaryWriter(ret);
w.Write( (byte)0xFF ); w.Write( (byte)0xFF );
w.Write(OrderString); w.Write(OrderString);
w.Write(SubjectId); w.Write(UIntFromActor(Subject));
w.Write(TargetActorId); w.Write(UIntFromActor(TargetActor));
w.Write(TargetLocation.X); w.Write(TargetLocation.X);
w.Write(TargetLocation.Y); w.Write(TargetLocation.Y);
w.Write(TargetString != null); w.Write(TargetString != null);
@@ -103,7 +86,11 @@ namespace OpenRa
if (r.ReadBoolean()) if (r.ReadBoolean())
targetString = r.ReadString(); targetString = r.ReadString();
return new Order( order, subjectId, targetActorId, targetLocation, targetString); Actor subject, targetActor;
if( !TryGetActorFromUInt( subjectId, out subject ) || !TryGetActorFromUInt( targetActorId, out targetActor ) )
return null;
return new Order( order, subject, targetActor, targetLocation, targetString);
} }
case 0xfe: case 0xfe:
@@ -126,10 +113,23 @@ namespace OpenRa
return a.ActorID; return a.ActorID;
} }
static Actor ActorFromUInt(uint aID) static bool TryGetActorFromUInt(uint aID, out Actor ret )
{ {
if (aID == 0xFFFFFFFF) return null; if( aID == 0xFFFFFFFF )
return Game.world.Actors.SingleOrDefault(x => x.ActorID == aID); {
ret = null;
return true;
}
else
{
foreach( var a in Game.world.Actors.Where( x => x.ActorID == aID ) )
{
ret = a;
return true;
}
ret = null;
return false;
}
} }
// Named constructors for Orders. // Named constructors for Orders.

View File

@@ -27,8 +27,12 @@ namespace OpenRa.Orders
var ms = new MemoryStream(bytes); var ms = new MemoryStream(bytes);
var reader = new BinaryReader(ms); var reader = new BinaryReader(ms);
var ret = new List<Order>(); var ret = new List<Order>();
while (ms.Position < ms.Length) while( ms.Position < ms.Length )
ret.Add(Order.Deserialize(reader)); {
var o = Order.Deserialize( reader );
if( o != null )
ret.Add( o );
}
return ret; return ret;
} }
} }

View File

@@ -58,7 +58,7 @@ namespace OpenRa.Orders
{ {
var orders = sources var orders = sources
.SelectMany(s => s.OrdersForFrame(frame)) .SelectMany(s => s.OrdersForFrame(frame))
.Where(o => o.Validate()) /* drop bogus things */ .SelectMany(x => x.ToOrderList())
.OrderBy(o => o.Player.Index) .OrderBy(o => o.Player.Index)
.ToList(); .ToList();

View File

@@ -14,25 +14,25 @@ namespace OpenRa.Orders
public void SendLocalOrders(int localFrame, List<Order> localOrders) { } public void SendLocalOrders(int localFrame, List<Order> localOrders) { }
public List<Order> OrdersForFrame(int frameNumber) public List<byte[]> OrdersForFrame(int frameNumber)
{ {
if (frameNumber == 0) if (frameNumber == 0)
return new List<Order>(); return new List<byte[]>();
try try
{ {
var len = replayReader.ReadInt32() - 4; var len = replayReader.ReadInt32() - 4;
var frame = replayReader.ReadInt32(); var frame = replayReader.ReadInt32();
var ret = replayReader.ReadBytes(len).ToOrderList(); var ret = replayReader.ReadBytes(len);
if (frameNumber != frame) if (frameNumber != frame)
throw new InvalidOperationException("Attempted time-travel in OrdersForFrame (replay)"); throw new InvalidOperationException("Attempted time-travel in OrdersForFrame (replay)");
return ret; return new List<byte[]> { ret };
} }
catch (EndOfStreamException) catch (EndOfStreamException)
{ {
return new List<Order>(); return new List<byte[]>();
} }
} }

View File

@@ -45,7 +45,6 @@ namespace OpenRa.Orders
{ {
var p = Game.controller.MousePosition; var p = Game.controller.MousePosition;
var c = Order(p.ToInt2(), mi) var c = Order(p.ToInt2(), mi)
.Where(o => o.Validate())
.Select(o => CursorForOrderString(o.OrderString, o.Subject, o.TargetLocation)) .Select(o => CursorForOrderString(o.OrderString, o.Subject, o.TargetLocation))
.FirstOrDefault(a => a != null); .FirstOrDefault(a => a != null);