From 21b0b129485472d141a46ce47d1081de0703b1c8 Mon Sep 17 00:00:00 2001 From: Paul Chote Date: Wed, 18 Aug 2010 22:21:08 +1200 Subject: [PATCH] Fix MP gamestart --- OpenRA.Game/Game.cs | 5 +- OpenRA.Game/Graphics/Viewport.cs | 5 -- OpenRA.Game/OpenRA.Game.csproj | 1 + OpenRA.Game/Traits/MPStartLocations.cs | 69 +++++++++++++++++++ .../Widgets/Delegates/DiplomacyDelegate.cs | 2 +- OpenRA.Mods.RA/SpawnMPUnits.cs | 42 +---------- .../Widgets/Delegates/IngameChromeDelegate.cs | 2 +- mods/cnc/system.yaml | 1 + mods/ra/maps/shellmap/map.yaml | 1 + mods/ra/system.yaml | 1 + 10 files changed, 80 insertions(+), 49 deletions(-) create mode 100644 OpenRA.Game/Traits/MPStartLocations.cs diff --git a/OpenRA.Game/Game.cs b/OpenRA.Game/Game.cs index 9cba342cfa..8e2d2d53ff 100644 --- a/OpenRA.Game/Game.cs +++ b/OpenRA.Game/Game.cs @@ -390,7 +390,7 @@ namespace OpenRA orderManager.StartGame(); } - public static event Action OnGameStart = () => {}; + public static event Action AfterGameStart = () => {}; public static event Action BeforeGameStart = () => {}; internal static void StartGame() { @@ -404,9 +404,8 @@ namespace OpenRA foreach (var gs in world.WorldActor.TraitsImplementing()) gs.GameStarted(world); - viewport.GoToStartLocation(world.LocalPlayer); orderManager.StartGame(); - OnGameStart(); + AfterGameStart(); } public static Stance ChooseInitialStance(Player p, Player q) diff --git a/OpenRA.Game/Graphics/Viewport.cs b/OpenRA.Game/Graphics/Viewport.cs index 95f73449d2..83c982729d 100644 --- a/OpenRA.Game/Graphics/Viewport.cs +++ b/OpenRA.Game/Graphics/Viewport.cs @@ -110,11 +110,6 @@ namespace OpenRA.Graphics scrollPosition = (avgPos - .5f * new float2(Width, Height)).ToInt2(); } - public void GoToStartLocation( Player player ) - { - Center( player.World.Queries.OwnedBy[ player ].WithTrait().Select( a => a.Actor ) ); - } - public Rectangle? ShroudBounds() { var localPlayer = Game.world.LocalPlayer; diff --git a/OpenRA.Game/OpenRA.Game.csproj b/OpenRA.Game/OpenRA.Game.csproj index 6b2628670a..6e0c2b5b9c 100755 --- a/OpenRA.Game/OpenRA.Game.csproj +++ b/OpenRA.Game/OpenRA.Game.csproj @@ -223,6 +223,7 @@ + diff --git a/OpenRA.Game/Traits/MPStartLocations.cs b/OpenRA.Game/Traits/MPStartLocations.cs new file mode 100644 index 0000000000..69d430cf6c --- /dev/null +++ b/OpenRA.Game/Traits/MPStartLocations.cs @@ -0,0 +1,69 @@ +#region Copyright & License Information +/* + * Copyright 2007-2010 The OpenRA Developers (see AUTHORS) + * This file is part of OpenRA, which is free software. It is made + * available to you under the terms of the GNU General Public License + * as published by the Free Software Foundation. For more information, + * see LICENSE. + */ +#endregion + +using System.Collections.Generic; +using System.Linq; +using System; +using OpenRA.FileFormats; + +namespace OpenRA.Traits +{ + public class MPStartLocationsInfo : TraitInfo + { + public readonly int InitialExploreRange = 5; + } + + public class MPStartLocations : IGameStarted + { + public Dictionary Start = new Dictionary(); + public void GameStarted(World world) + { + var taken = Game.LobbyInfo.Clients.Where(c => c.SpawnPoint != 0) + .Select(c => world.Map.SpawnPoints.ElementAt(c.SpawnPoint - 1)).ToList(); + var available = world.Map.SpawnPoints.Except(taken).ToList(); + + // Set spawn + foreach (var client in Game.LobbyInfo.Clients) + Start.Add(world.players[client.Index],(client.SpawnPoint == 0) + ? ChooseSpawnPoint(world, available, taken) + : world.Map.SpawnPoints.ElementAt(client.SpawnPoint - 1)); + + // Explore allied shroud + foreach (var p in Start) + if (p.Key == world.LocalPlayer || p.Key.Stances[world.LocalPlayer] == Stance.Ally) + world.WorldActor.Trait().Explore(world, p.Value, + world.WorldActor.Info.Traits.Get().InitialExploreRange); + + // Set viewport + if (world.LocalPlayer != null && Start.ContainsKey(world.LocalPlayer)) + Game.viewport.Center(Start[world.LocalPlayer]); + } + + static int2 ChooseSpawnPoint(World world, List available, List taken) + { + if (available.Count == 0) + throw new InvalidOperationException("No free spawnpoint."); + + var n = taken.Count == 0 + ? world.SharedRandom.Next(available.Count) + : available // pick the most distant spawnpoint from everyone else + .Select((k, i) => Pair.New(k, i)) + .OrderByDescending(a => taken.Sum(t => (t - a.First).LengthSquared)) + .Select(a => a.Second) + .First(); + + var sp = available[n]; + available.RemoveAt(n); + taken.Add(sp); + return sp; + } + } +} + diff --git a/OpenRA.Game/Widgets/Delegates/DiplomacyDelegate.cs b/OpenRA.Game/Widgets/Delegates/DiplomacyDelegate.cs index e6732cc988..2787b614d4 100644 --- a/OpenRA.Game/Widgets/Delegates/DiplomacyDelegate.cs +++ b/OpenRA.Game/Widgets/Delegates/DiplomacyDelegate.cs @@ -34,7 +34,7 @@ namespace OpenRA.Widgets.Delegates return true; }; - Game.OnGameStart += () => validPlayers = Game.world.players.Values.Where(a => a != Game.world.LocalPlayer && !a.NonCombatant).Count(); + Game.AfterGameStart += () => validPlayers = Game.world.players.Values.Where(a => a != Game.world.LocalPlayer && !a.NonCombatant).Count(); diplomacy.IsVisible = () => (validPlayers > 0); } diff --git a/OpenRA.Mods.RA/SpawnMPUnits.cs b/OpenRA.Mods.RA/SpawnMPUnits.cs index d15e1fdd76..ffbdf149b9 100644 --- a/OpenRA.Mods.RA/SpawnMPUnits.cs +++ b/OpenRA.Mods.RA/SpawnMPUnits.cs @@ -16,27 +16,14 @@ using OpenRA.Traits; namespace OpenRA.Mods.RA { - class SpawnMPUnitsInfo : TraitInfo - { - public readonly int InitialExploreRange = 5; - } + class SpawnMPUnitsInfo : TraitInfo, ITraitPrerequisite {} class SpawnMPUnits : IGameStarted { public void GameStarted(World world) { - var taken = Game.LobbyInfo.Clients.Where(c => c.SpawnPoint != 0) - .Select(c => world.Map.SpawnPoints.ElementAt(c.SpawnPoint - 1)).ToList(); - - var available = world.Map.SpawnPoints.Except(taken).ToList(); - - foreach (var client in Game.LobbyInfo.Clients) - { - SpawnUnitsForPlayer(world.players[client.Index], - (client.SpawnPoint == 0) - ? ChooseSpawnPoint(world, available, taken) - : world.Map.SpawnPoints.ElementAt(client.SpawnPoint - 1)); - } + foreach (var s in world.WorldActor.Trait().Start) + SpawnUnitsForPlayer(s.Key, s.Value); } void SpawnUnitsForPlayer(Player p, int2 sp) @@ -46,29 +33,6 @@ namespace OpenRA.Mods.RA new LocationInit( sp ), new OwnerInit( p ), }); - - if (p == p.World.LocalPlayer || p.Stances[p.World.LocalPlayer] == Stance.Ally) - p.World.WorldActor.Trait().Explore(p.World, sp, - p.World.WorldActor.Info.Traits.Get().InitialExploreRange); - } - - static int2 ChooseSpawnPoint(World world, List available, List taken) - { - if (available.Count == 0) - throw new InvalidOperationException("No free spawnpoint."); - - var n = taken.Count == 0 - ? world.SharedRandom.Next(available.Count) - : available // pick the most distant spawnpoint from everyone else - .Select((k, i) => Pair.New(k, i)) - .OrderByDescending(a => taken.Sum(t => (t - a.First).LengthSquared)) - .Select(a => a.Second) - .First(); - - var sp = available[n]; - available.RemoveAt(n); - taken.Add(sp); - return sp; } } } diff --git a/OpenRA.Mods.RA/Widgets/Delegates/IngameChromeDelegate.cs b/OpenRA.Mods.RA/Widgets/Delegates/IngameChromeDelegate.cs index 01001cce1a..0908d165f3 100755 --- a/OpenRA.Mods.RA/Widgets/Delegates/IngameChromeDelegate.cs +++ b/OpenRA.Mods.RA/Widgets/Delegates/IngameChromeDelegate.cs @@ -22,7 +22,7 @@ namespace OpenRA.Mods.RA.Widgets.Delegates var optionsBG = gameRoot.GetWidget("INGAME_OPTIONS_BG"); Game.BeforeGameStart += () => r.OpenWindow("INGAME_ROOT"); - Game.OnGameStart += () => gameRoot.GetWidget("INGAME_RADAR_BIN").SetWorld(Game.world); + Game.AfterGameStart += () => gameRoot.GetWidget("INGAME_RADAR_BIN").SetWorld(Game.world); r.GetWidget("INGAME_OPTIONS_BUTTON").OnMouseUp = mi => { optionsBG.Visible = !optionsBG.Visible; diff --git a/mods/cnc/system.yaml b/mods/cnc/system.yaml index aee6a6a8c7..8618348e24 100644 --- a/mods/cnc/system.yaml +++ b/mods/cnc/system.yaml @@ -119,6 +119,7 @@ World: SpawnMapActors: CreateMPPlayers: SpawnMPUnits: + MPStartLocations: EvaAlerts: RadarUp: comcntr1.aud RadarDown: powrdn1.aud diff --git a/mods/ra/maps/shellmap/map.yaml b/mods/ra/maps/shellmap/map.yaml index e81610c053..8e174aee7e 100644 --- a/mods/ra/maps/shellmap/map.yaml +++ b/mods/ra/maps/shellmap/map.yaml @@ -1237,3 +1237,4 @@ Rules: DefaultShellmapScript: -CreateMPPlayers: -SpawnMPUnits: + -MPStartLocations: diff --git a/mods/ra/system.yaml b/mods/ra/system.yaml index e75bb45f87..bed6dd6ae7 100644 --- a/mods/ra/system.yaml +++ b/mods/ra/system.yaml @@ -174,6 +174,7 @@ World: CreateMapPlayers: SpawnMapActors: CreateMPPlayers: + MPStartLocations: SpawnMPUnits: EvaAlerts: SpatialBins: