diff --git a/OpenRa.Game/Controller.cs b/OpenRa.Game/Controller.cs index db8d074d3e..fb44ad7411 100644 --- a/OpenRa.Game/Controller.cs +++ b/OpenRa.Game/Controller.cs @@ -8,26 +8,26 @@ using System.Drawing; using OpenRa.Game.Traits; namespace OpenRa.Game -{ - class Controller - { +{ + class Controller + { public IOrderGenerator orderGenerator; List recentOrders = new List(); - void ApplyOrders(float2 xy, bool left) - { + void ApplyOrders(float2 xy, bool left) + { var doVoice = null as Actor; - if( orderGenerator != null ) - foreach( var order in orderGenerator.Order( xy.ToInt2(), left ) ) + if (orderGenerator != null) + foreach (var order in orderGenerator.Order(xy.ToInt2(), left)) { - recentOrders.Add( order ); + recentOrders.Add(order); //UnitOrders.ProcessOrder( order ); - if( order.Subject != null && order.Player == Game.LocalPlayer ) + if (order.Subject != null && order.Player == Game.LocalPlayer) doVoice = order.Subject; - } - if( doVoice != null ) - Game.PlaySound( Game.SovietVoices.First.GetNext() + GetVoiceSuffix( doVoice ), false ); + } + if (doVoice != null) + Game.PlaySound(Game.SovietVoices.First.GetNext() + GetVoiceSuffix(doVoice), false); } public List GetRecentOrders() @@ -35,89 +35,89 @@ namespace OpenRa.Game var ret = recentOrders; recentOrders = new List(); return ret; - } - - static string GetVoiceSuffix( Actor unit ) - { - var suffixes = new[] { ".r01", ".r03" }; - return suffixes[ unit.traits.Get().Voice ]; - } - - float2 dragStart, dragEnd; - public void HandleMouseInput(MouseInput mi) - { - var xy = Game.viewport.ViewToWorld(mi); - - if (mi.Button == MouseButtons.Left && mi.Event == MouseInputEvent.Down) - { - if (!(orderGenerator is PlaceBuilding)) - dragStart = dragEnd = xy; - ApplyOrders(xy, true); - } - - if (mi.Button == MouseButtons.Left && mi.Event == MouseInputEvent.Move) - dragEnd = xy; - - if (mi.Button == MouseButtons.Left && mi.Event == MouseInputEvent.Up) - { - if (!(orderGenerator is PlaceBuilding)) - { - if (dragStart != xy) - orderGenerator = new UnitOrderGenerator( - Game.SelectUnitsInBox( Game.CellSize * dragStart, Game.CellSize * xy ) ); - else - orderGenerator = new UnitOrderGenerator( - Game.SelectUnitOrBuilding( Game.CellSize * xy ) ); - } - - dragStart = dragEnd = xy; - } - - if (mi.Button == MouseButtons.None && mi.Event == MouseInputEvent.Move) - { - /* update the cursor to reflect the thing under us - note this - * needs to also happen when the *thing* changes, so per-frame hook */ - dragStart = dragEnd = xy; - } - - if (mi.Button == MouseButtons.Right && mi.Event == MouseInputEvent.Down) - ApplyOrders(xy, false); - } - - public Pair? SelectionBox - { - get - { - if (dragStart == dragEnd) return null; - return Pair.New(Game.CellSize * dragStart, Game.CellSize * dragEnd); - } - } - - public Cursor ChooseCursor() - { - var uog = orderGenerator as UnitOrderGenerator; - - if (uog != null) - uog.selection.RemoveAll(a => a.IsDead); - - if (uog != null && uog.selection.Count > 0 - && uog.selection.Any(a => a.traits.Contains()) - && uog.selection.All( a => a.Owner == Game.LocalPlayer )) - { - var umts = uog.selection.Select(a => a.traits.GetOrDefault()) - .Where(m => m != null) - .Select(m => m.GetMovementType()) - .Distinct(); - - if (!umts.Any( umt => Game.IsCellBuildable( dragEnd.ToInt2(), umt ) )) - return Cursor.MoveBlocked; - return Cursor.Move; - } - - if (Game.SelectUnitOrBuilding(Game.CellSize * dragEnd).Any()) - return Cursor.Select; - - return Cursor.Default; - } + } + + static string GetVoiceSuffix(Actor unit) + { + var suffixes = new[] { ".r01", ".r03" }; + return suffixes[unit.traits.Get().Voice]; + } + + float2 dragStart, dragEnd; + public void HandleMouseInput(MouseInput mi) + { + var xy = Game.viewport.ViewToWorld(mi); + + if (mi.Button == MouseButtons.Left && mi.Event == MouseInputEvent.Down) + { + if (!(orderGenerator is PlaceBuilding)) + dragStart = dragEnd = xy; + ApplyOrders(xy, true); + } + + if (mi.Button == MouseButtons.Left && mi.Event == MouseInputEvent.Move) + dragEnd = xy; + + if (mi.Button == MouseButtons.Left && mi.Event == MouseInputEvent.Up) + { + if (!(orderGenerator is PlaceBuilding)) + { + if (dragStart != xy) + orderGenerator = new UnitOrderGenerator( + Game.SelectUnitsInBox(Game.CellSize * dragStart, Game.CellSize * xy)); + else + orderGenerator = new UnitOrderGenerator( + Game.SelectUnitOrBuilding(Game.CellSize * xy)); + } + + dragStart = dragEnd = xy; + } + + if (mi.Button == MouseButtons.None && mi.Event == MouseInputEvent.Move) + { + /* update the cursor to reflect the thing under us - note this + * needs to also happen when the *thing* changes, so per-frame hook */ + dragStart = dragEnd = xy; + } + + if (mi.Button == MouseButtons.Right && mi.Event == MouseInputEvent.Down) + ApplyOrders(xy, false); + } + + public Pair? SelectionBox + { + get + { + if (dragStart == dragEnd) return null; + return Pair.New(Game.CellSize * dragStart, Game.CellSize * dragEnd); + } + } + + public Cursor ChooseCursor() + { + var uog = orderGenerator as UnitOrderGenerator; + + if (uog != null) + uog.selection.RemoveAll(a => a.IsDead); + + if (uog != null && uog.selection.Count > 0 + && uog.selection.Any(a => a.traits.Contains()) + && uog.selection.All(a => a.Owner == Game.LocalPlayer)) + { + var umts = uog.selection.Select(a => a.traits.GetOrDefault()) + .Where(m => m != null) + .Select(m => m.GetMovementType()) + .Distinct(); + + if (!umts.Any(umt => Game.IsCellBuildable(dragEnd.ToInt2(), umt))) + return Cursor.MoveBlocked; + return Cursor.Move; + } + + if (Game.SelectUnitOrBuilding(Game.CellSize * dragEnd).Any()) + return Cursor.Select; + + return Cursor.Default; + } } } diff --git a/OpenRa.Game/Game.cs b/OpenRa.Game/Game.cs index 048b99f8b9..163864c159 100644 --- a/OpenRa.Game/Game.cs +++ b/OpenRa.Game/Game.cs @@ -23,7 +23,6 @@ namespace OpenRa.Game public static TerrainRenderer terrain; public static Viewport viewport; public static PathFinder PathFinder; - public static Network network; public static WorldRenderer worldRenderer; public static Controller controller; @@ -37,7 +36,9 @@ namespace OpenRa.Game public static BuildingInfluenceMap BuildingInfluence; public static UnitInfluenceMap UnitInfluence; - static ISoundEngine soundEngine; + static ISoundEngine soundEngine; + + public static string Replay; public static void Initialize(string mapName, Renderer renderer, int2 clientSize, int localPlayer) { @@ -69,15 +70,15 @@ namespace OpenRa.Game PathFinder = new PathFinder(map, terrain.tileSet); - network = new Network(); - controller = new Controller(); worldRenderer = new WorldRenderer(renderer); soundEngine = new ISoundEngine(); sounds = new Cache(LoadSound); - orderManager = new OrderManager( new OrderSource[] { new LocalOrderSource() }, "replay.rep" ); + orderManager = (Replay == "") + ? new OrderManager(new[] { new LocalOrderSource() }, "replay.rep") + : new OrderManager(new[] { new ReplayOrderSource(Replay) }); PlaySound("intro.aud", false); } @@ -128,7 +129,6 @@ namespace OpenRa.Game public static void Tick() { - var stuffFromOtherPlayers = network.Tick(); // todo: actually use the orders! world.Update(); UnitInfluence.Tick(); diff --git a/OpenRa.Game/MainWindow.cs b/OpenRa.Game/MainWindow.cs index c9fd6de09e..113dc39e95 100755 --- a/OpenRa.Game/MainWindow.cs +++ b/OpenRa.Game/MainWindow.cs @@ -51,7 +51,8 @@ namespace OpenRa.Game SheetBuilder.Initialize(renderer); UiOverlay.ShowUnitDebug = settings.GetValue("udebug", false); - WorldRenderer.ShowUnitPaths = settings.GetValue("pathdebug", false); + WorldRenderer.ShowUnitPaths = settings.GetValue("pathdebug", false); + Game.Replay = settings.GetValue("replay", ""); Game.Initialize(settings.GetValue("map", "scg11eb.ini"), renderer, new int2(ClientSize), settings.GetValue("player", 1)); diff --git a/OpenRa.Game/Network/Network.cs b/OpenRa.Game/Network/Network.cs deleted file mode 100644 index 083a1d0b6f..0000000000 --- a/OpenRa.Game/Network/Network.cs +++ /dev/null @@ -1,85 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Text; -using System.Net.Sockets; -using System.Threading; -using System.Net; -using System.IO; - -namespace OpenRa.Game -{ - class Network - { - public const int Port = 6543; - const int netSyncInterval = 40 * 5; - - UdpClient client = new UdpClient(Port); - int nextSyncTime = 0; - int currentFrame = 0; - - public int CurrentFrame { get { return currentFrame; } } - public int RemainingNetSyncTime { get { return Math.Max(0, nextSyncTime - Environment.TickCount); } } - - Queue incomingPackets = new Queue(); - - public Network() - { - client.EnableBroadcast = true; - - Thread receiveThread = new Thread( () => - { - for (; ; ) - { - IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0); - byte[] data = client.Receive(ref sender); - - Packet packet = Packet.FromReceivedData(sender, data); - - lock (this) - if (currentFrame <= packet.Frame) - incomingPackets.Enqueue(packet); - } - }); - - receiveThread.IsBackground = true; - receiveThread.Start(); - } - - public void Send(byte[] data) - { - IPEndPoint destination = new IPEndPoint(IPAddress.Broadcast, Port); - using(MemoryStream ms = new MemoryStream()) - using (BinaryWriter writer = new BinaryWriter(ms)) - { - writer.Write(currentFrame); - writer.Write(data); - writer.Flush(); - - byte[] toSend = ms.ToArray(); - - client.Send(toSend, toSend.Length); - } - } - - public Queue Tick() - { - Queue toProcess = new Queue(); - - if (Environment.TickCount > nextSyncTime) - lock (this) - { - while (incomingPackets.Count > 0 && incomingPackets.Peek().Frame <= currentFrame) - { - Packet p = incomingPackets.Dequeue(); - if (p.Frame == currentFrame) - toProcess.Enqueue(p); - } - - ++currentFrame; - nextSyncTime = Environment.TickCount + netSyncInterval; - } - - return toProcess; - } - } -} diff --git a/OpenRa.Game/Network/Packet.cs b/OpenRa.Game/Network/Packet.cs deleted file mode 100644 index 01d36b5f9a..0000000000 --- a/OpenRa.Game/Network/Packet.cs +++ /dev/null @@ -1,33 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Text; -using System.Net; -using System.IO; - -namespace OpenRa.Game -{ - class Packet : IComparable - { - IPEndPoint address; - int frame; - byte[] data; - - public int Frame { get { return frame; } } - - Packet(IPEndPoint address, byte[] data) - { - this.address = address; - - using (MemoryStream ms = new MemoryStream(data)) - using (BinaryReader reader = new BinaryReader(ms)) - { - frame = reader.ReadInt32(); - this.data = reader.ReadBytes(data.Length - 4); - } - } - - public static Packet FromReceivedData(IPEndPoint sender, byte[] data) { return new Packet(sender, data); } - - public int CompareTo(Packet other) { return frame.CompareTo(other.frame); } - } -} diff --git a/OpenRa.Game/OpenRa.Game.csproj b/OpenRa.Game/OpenRa.Game.csproj index d06c1f319c..76bf0fc33e 100644 --- a/OpenRa.Game/OpenRa.Game.csproj +++ b/OpenRa.Game/OpenRa.Game.csproj @@ -100,13 +100,11 @@ - -