/// <summary> /// Generates new game instance from existing map. /// Good for using imported maps. /// </summary> /// <param name="map">Map to be used for generation.</param> /// <param name="humanPlayerName"> /// If the provided map doesn't contain any human players, one will be randomly generated (if there's place left on the map) and this name will be used. /// If the provided map does contain human palyers, name of the first player will be set to this. /// </param> /// <returns>Generated game instance.</returns> public static Game GenerateGame(Map.Map map, string humanPlayerName) { int blockCount = map.Width * map.Height; List <AbstractPlayer> humanPlayers = new List <AbstractPlayer>(); List <AbstractPlayer> aiPlayers = new List <AbstractPlayer>(); List <AbstractCreature> monsters = new List <AbstractCreature>(); foreach (Map.MapBlock mapBlock in map.Grid) { if (mapBlock.Occupied) { if (mapBlock.Creature is HumanPlayer) { humanPlayers.Add((HumanPlayer)mapBlock.Creature); } else if (mapBlock.Creature is AbstractPlayer) { aiPlayers.Add((AbstractPlayer)mapBlock.Creature); } else { monsters.Add(mapBlock.Creature); } } } // check if there's human player and that there's also free map blocks to place him if (humanPlayers.Count == 0 && (blockCount > (aiPlayers.Count + monsters.Count))) { Random r = new Random(); bool placed = false; // if the player is not placed after this number of attempts, something is probably wrong int maxAttempts = 1000; int attempt = 0; while (!placed && attempt < maxAttempts) { int px = r.Next(map.Width); int py = r.Next(map.Height); if (!map.Grid[px, py].Occupied) { HumanPlayer hp = new HumanPlayer(humanPlayerName, map.Grid[px, py]); map.AddCreature(hp); humanPlayers.Add(hp); placed = true; } attempt++; } } else { humanPlayers[0].Name = humanPlayerName; } // generate game instance Game game = new Game() { GameMap = map, HumanPlayers = humanPlayers, AiPlayers = aiPlayers, Monsters = monsters }; return(game); }
/// <summary> /// Adds a human player. /// </summary> /// <param name="humanPlayer">Human player to be added.</param> public void AddHumanPlayer(AbstractPlayer humanPlayer) { HumanPlayers.Add(humanPlayer); GameMap.AddCreature(humanPlayer); }