tighten order encoding

This commit is contained in:
Chris Forbes
2010-11-23 12:58:59 +13:00
parent f4f9abe4d4
commit 2f74207bf6
2 changed files with 48 additions and 12 deletions

View File

@@ -11,9 +11,27 @@
using System; using System;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using OpenRA.Network;
namespace OpenRA namespace OpenRA
{ {
[Flags]
enum OrderFields : byte
{
TargetActor = 0x01,
TargetLocation = 0x02,
TargetString = 0x04,
Queued = 0x08,
}
static class OrderFieldsExts
{
public static bool HasField(this OrderFields of, OrderFields f)
{
return (of & f) != 0;
}
}
public sealed class Order public sealed class Order
{ {
public readonly string OrderString; public readonly string OrderString;
@@ -78,13 +96,25 @@ namespace OpenRA
w.Write( (byte)0xFF ); w.Write( (byte)0xFF );
w.Write(OrderString); w.Write(OrderString);
w.Write(UIntFromActor(Subject)); w.Write(UIntFromActor(Subject));
OrderFields fields = 0;
if (TargetActor != null) fields |= OrderFields.TargetActor;
if (TargetLocation != int2.Zero) fields |= OrderFields.TargetLocation;
if (TargetString != null) fields |= OrderFields.TargetString;
if (Queued) fields |= OrderFields.Queued;
w.Write((byte)fields);
if (TargetActor != null)
w.Write(UIntFromActor(TargetActor)); w.Write(UIntFromActor(TargetActor));
if (TargetLocation != int2.Zero)
{
w.Write(TargetLocation.X); w.Write(TargetLocation.X);
w.Write(TargetLocation.Y); w.Write(TargetLocation.Y);
w.Write(TargetString != null); }
if (TargetString != null) if (TargetString != null)
w.Write(TargetString); w.Write(TargetString);
w.Write(Queued);
return ret.ToArray(); return ret.ToArray();
} }
} }
@@ -98,13 +128,12 @@ namespace OpenRA
{ {
var order = r.ReadString(); var order = r.ReadString();
var subjectId = r.ReadUInt32(); var subjectId = r.ReadUInt32();
var targetActorId = r.ReadUInt32(); var flags = (OrderFields)r.ReadByte();
var targetLocation = new int2(r.ReadInt32(), 0);
targetLocation.Y = r.ReadInt32(); var targetActorId = flags.HasField(OrderFields.TargetActor) ? r.ReadUInt32() : 0xffffffff;
var targetString = null as string; var targetLocation = flags.HasField(OrderFields.TargetLocation) ? r.ReadInt2() : int2.Zero;
if (r.ReadBoolean()) var targetString = flags.HasField(OrderFields.TargetString) ? r.ReadString() : null;
targetString = r.ReadString(); var queued = flags.HasField(OrderFields.Queued);
var queued = r.ReadBoolean();
Actor subject, targetActor; Actor subject, targetActor;
if( !TryGetActorFromUInt( world, subjectId, out subject ) || !TryGetActorFromUInt( world, targetActorId, out targetActor ) ) if( !TryGetActorFromUInt( world, subjectId, out subject ) || !TryGetActorFromUInt( world, targetActorId, out targetActor ) )

View File

@@ -47,5 +47,12 @@ namespace OpenRA.Network
} }
return ms.ToArray(); return ms.ToArray();
} }
public static int2 ReadInt2(this BinaryReader r)
{
var x = r.ReadInt32();
var y = r.ReadInt32();
return new int2(x, y);
}
} }
} }