#region Copyright & License Information /* * Copyright 2007-2011 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 COPYING. */ #endregion using System; using System.Collections.Generic; using System.Linq; using OpenRA.FileFormats; using OpenRA.Network; using OpenRA.Traits; namespace OpenRA.Mods.RA { public class MPStartLocationsInfo : TraitInfo { public readonly int InitialExploreRange = 5; } public class MPStartLocations : IWorldLoaded { public Dictionary Start = new Dictionary(); public void WorldLoaded(World world) { var taken = world.LobbyInfo.Clients.Where(c => c.SpawnPoint != 0 && c.Slot != -1) .Select(c => world.Map.SpawnPoints.ElementAt(c.SpawnPoint - 1)).ToList(); var available = world.Map.SpawnPoints.Except(taken).ToList(); // Set spawn foreach (var slot in world.LobbyInfo.Slots) { if (slot.Spectator) continue; // Skip spectator slots var client = world.LobbyInfo.Clients.FirstOrDefault(c => c.Slot == slot.Index); var player = FindPlayerInSlot(world, slot); if (player == null) continue; var spid = (client == null || client.SpawnPoint == 0) ? ChooseSpawnPoint(world, available, taken) : world.Map.SpawnPoints.ElementAt(client.SpawnPoint - 1); Start.Add(player, spid); } // Explore allied shroud foreach (var p in Start) if ((world.LocalPlayer != null ) &&(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 Player FindPlayerInSlot(World world, Session.Slot slot) { return world.players.Values.FirstOrDefault(p => p.PlayerRef.Name == slot.MapPlayer); } 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; } } }