Fix MP gamestart

This commit is contained in:
Paul Chote
2010-08-18 22:21:08 +12:00
parent cb3f6435ad
commit 21b0b12948
10 changed files with 80 additions and 49 deletions

View File

@@ -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<IGameStarted>())
gs.GameStarted(world);
viewport.GoToStartLocation(world.LocalPlayer);
orderManager.StartGame();
OnGameStart();
AfterGameStart();
}
public static Stance ChooseInitialStance(Player p, Player q)

View File

@@ -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<Selectable>().Select( a => a.Actor ) );
}
public Rectangle? ShroudBounds()
{
var localPlayer = Game.world.LocalPlayer;

View File

@@ -223,6 +223,7 @@
<Compile Include="Traits\Activities\Drag.cs" />
<Compile Include="Widgets\VqaPlayerWidget.cs" />
<Compile Include="Widgets\Delegates\VideoPlayerDelegate.cs" />
<Compile Include="Traits\MPStartLocations.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\OpenRA.FileFormats\OpenRA.FileFormats.csproj">

View File

@@ -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<MPStartLocations>
{
public readonly int InitialExploreRange = 5;
}
public class MPStartLocations : IGameStarted
{
public Dictionary<Player, int2> Start = new Dictionary<Player, int2>();
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<Shroud>().Explore(world, p.Value,
world.WorldActor.Info.Traits.Get<MPStartLocationsInfo>().InitialExploreRange);
// Set viewport
if (world.LocalPlayer != null && Start.ContainsKey(world.LocalPlayer))
Game.viewport.Center(Start[world.LocalPlayer]);
}
static int2 ChooseSpawnPoint(World world, List<int2> available, List<int2> 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;
}
}
}

View File

@@ -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);
}

View File

@@ -16,27 +16,14 @@ using OpenRA.Traits;
namespace OpenRA.Mods.RA
{
class SpawnMPUnitsInfo : TraitInfo<SpawnMPUnits>
{
public readonly int InitialExploreRange = 5;
}
class SpawnMPUnitsInfo : TraitInfo<SpawnMPUnits>, ITraitPrerequisite<MPStartLocationsInfo> {}
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<MPStartLocations>().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<Shroud>().Explore(p.World, sp,
p.World.WorldActor.Info.Traits.Get<SpawnMPUnitsInfo>().InitialExploreRange);
}
static int2 ChooseSpawnPoint(World world, List<int2> available, List<int2> 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;
}
}
}

View File

@@ -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<RadarBinWidget>("INGAME_RADAR_BIN").SetWorld(Game.world);
Game.AfterGameStart += () => gameRoot.GetWidget<RadarBinWidget>("INGAME_RADAR_BIN").SetWorld(Game.world);
r.GetWidget("INGAME_OPTIONS_BUTTON").OnMouseUp = mi => {
optionsBG.Visible = !optionsBG.Visible;

View File

@@ -119,6 +119,7 @@ World:
SpawnMapActors:
CreateMPPlayers:
SpawnMPUnits:
MPStartLocations:
EvaAlerts:
RadarUp: comcntr1.aud
RadarDown: powrdn1.aud

View File

@@ -1237,3 +1237,4 @@ Rules:
DefaultShellmapScript:
-CreateMPPlayers:
-SpawnMPUnits:
-MPStartLocations:

View File

@@ -174,6 +174,7 @@ World:
CreateMapPlayers:
SpawnMapActors:
CreateMPPlayers:
MPStartLocations:
SpawnMPUnits:
EvaAlerts:
SpatialBins: