context: #region Copyright & License Information
  /*
   * Copyright 2007-2015 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.Effects;
  using OpenRA.FileFormats;
  using OpenRA.Graphics;
  using OpenRA.Network;
  using OpenRA.Orders;
  using OpenRA.Primitives;
  using OpenRA.Support;
  using OpenRA.Traits;
  
  namespace OpenRA
  {
  	public enum WorldType { Regular, Shellmap, Editor }
  
  	public sealed class World : IDisposable
  	{
  		class ActorIDComparer : IComparer<Actor>
  		{
  			public static readonly ActorIDComparer Instance = new ActorIDComparer();
  			ActorIDComparer() { }
  			public int Compare(Actor x, Actor y) { return x.ActorID.CompareTo(y.ActorID); }
  		}
  
  		internal readonly TraitDictionary TraitDict = new TraitDictionary();
  		readonly SortedSet<Actor> actors = new SortedSet<Actor>(ActorIDComparer.Instance);
  		readonly List<IEffect> effects = new List<IEffect>();
  		readonly Queue<Action<World>> frameEndActions = new Queue<Action<World>>();
  
  		public int Timestep;
  
  		internal readonly OrderManager OrderManager;
  		public Session LobbyInfo { get { return OrderManager.LobbyInfo; } }
  
  		public readonly MersenneTwister SharedRandom;
  
  		public readonly List<Player> Players = new List<Player>();
  
  		public void AddPlayer(Player p) { Players.Add(p); }
  		public Player LocalPlayer { get; private set; }
  
  		public event Action GameOver = () => { };
  		public bool IsGameOver { get; private set; }
  		public void EndGame()
  		{
  			if (!IsGameOver)
  			{
  				IsGameOver = true;
  
  				foreach (var t in WorldActor.TraitsImplementing<IGameOver>())
  					t.GameOver(this);
  
  				GameOver();
  			}
  		}
  
  		Player renderPlayer;
  		public Player RenderPlayer
  		{
  			get { return renderPlayer == null || (renderPlayer.WinState != WinState.Undefined && !Map.Visibility.HasFlag(MapVisibility.MissionSelector)) ? null : renderPlayer; }
  			set { renderPlayer = value; }
  		}
  
  		public bool FogObscures(Actor a) { return RenderPlayer != null && !RenderPlayer.CanViewActor(a); }
  		public bool FogObscures(CPos p) { return RenderPlayer != null && !RenderPlayer.Shroud.IsVisible(p); }
  		public bool FogObscures(WPos pos) { return RenderPlayer != null && !RenderPlayer.Shroud.IsVisible(pos); }
  		public bool ShroudObscures(CPos p) { return RenderPlayer != null && !RenderPlayer.Shroud.IsExplored(p); }
  		public bool ShroudObscures(WPos pos) { return RenderPlayer != null && !RenderPlayer.Shroud.IsExplored(pos); }
  		public bool ShroudObscures(PPos uv) { return RenderPlayer != null && !RenderPlayer.Shroud.IsExplored(uv); }
  
  		public bool IsReplay
  		{
  			get { return OrderManager.Connection is ReplayConnection; }
  		}
  
  		public bool AllowDevCommands
  		{
  			get { return LobbyInfo.GlobalSettings.AllowCheats || LobbyInfo.IsSinglePlayer; }
  		}
  
  		public void SetLocalPlayer(string pr)
  		{
  			if (IsReplay)
  				return;
  
  			LocalPlayer = Players.FirstOrDefault(p => p.InternalName == pr);
  			RenderPlayer = LocalPlayer;
  		}
  
  		public readonly Actor WorldActor;
  
  		public readonly Map Map;
  
  		public readonly TileSet TileSet;
  
  		public readonly ActorMap ActorMap;
  		public readonly ScreenMap ScreenMap;
  		public readonly WorldType Type;
  
  		readonly GameInformation gameInfo;
  
  		public void IssueOrder(Order o) { OrderManager.IssueOrder(o); } /* avoid exposing the OM to mod code */
  
  		IOrderGenerator orderGenerator;
  		public IOrderGenerator OrderGenerator
  		{
  			get
  			{
  				return orderGenerator;
  			}
  
  			set
  			{
  				Sync.AssertUnsynced("The current order generator may not be changed from synced code");
  				orderGenerator = value;
  			}
  		}
  
  		public Selection Selection = new Selection();
  
  		public void CancelInputMode() { OrderGenerator = new UnitOrderGenerator(); }
  
  		public bool ToggleInputMode<T>() where T : IOrderGenerator, new()
  		{
  			if (OrderGenerator is T)
  			{
  				CancelInputMode();
  				return false;
  			}
  			else
  			{
  				OrderGenerator = new T();
  				return true;
  			}
  		}
  
  		internal World(Map map, OrderManager orderManager, WorldType type)
  		{
  			Type = type;
  			OrderManager = orderManager;
  			orderGenerator = new UnitOrderGenerator();
  			Map = map;
  
  			TileSet = map.Rules.TileSets[Map.Tileset];
  			SharedRandom = new MersenneTwister(orderManager.LobbyInfo.GlobalSettings.RandomSeed);
  
  			var worldActorType = type == WorldType.Editor ? "EditorWorld" : "World";
  			WorldActor = CreateActor(worldActorType, new TypeDictionary());
  			ActorMap = WorldActor.Trait<ActorMap>();
  			ScreenMap = WorldActor.Trait<ScreenMap>();
  
  			// Add players
  			foreach (var cmp in WorldActor.TraitsImplementing<ICreatePlayers>())
  				cmp.CreatePlayers(this);
  
  			// Set defaults for any unset stances
  			foreach (var p in Players)
  				foreach (var q in Players)
  					if (!p.Stances.ContainsKey(q))
  						p.Stances[q] = Stance.Neutral;
  
  			Sound.SoundVolumeModifier = 1.0f;
  
  			gameInfo = new GameInformation
  			{
  				Mod = Game.ModData.Manifest.Mod.Id,
  				Version = Game.ModData.Manifest.Mod.Version,
  
  				MapUid = Map.Uid,
  				MapTitle = Map.Title
  			};
  		}
  
  		public void LoadComplete(WorldRenderer wr)
  		{
  			// ScreenMap must be initialized before anything else
  			using (new Support.PerfTimer("ScreenMap.WorldLoaded"))
  				ScreenMap.WorldLoaded(this, wr);
  
  			foreach (var wlh in WorldActor.TraitsImplementing<IWorldLoaded>())
  			{
  				// These have already been initialized
  				if (wlh == ScreenMap)
  					continue;
  
  				using (new Support.PerfTimer(wlh.GetType().Name + ".WorldLoaded"))
  					wlh.WorldLoaded(this, wr);
  			}
  
  			gameInfo.StartTimeUtc = DateTime.UtcNow;
  			foreach (var player in Players)
  				gameInfo.AddPlayer(player, OrderManager.LobbyInfo);
  
  			var rc = OrderManager.Connection as ReplayRecorderConnection;
  			if (rc != null)
  				rc.Metadata = new ReplayMetadata(gameInfo);
  		}
  
  		public Actor CreateActor(string name, TypeDictionary initDict)
  		{
  			return CreateActor(true, name, initDict);
  		}
  
  		public Actor CreateActor(bool addToWorld, string name, TypeDictionary initDict)
  		{
  
gt: 			var a = new Actor(this, name, initDict);