/// <summary> /// Generate new map from values stored in this view model. /// Note that this will also remove all placed items. /// </summary> public void GenerateMap() { GameMap = MapGeneratorFactory.CreateSimpleMapGenerator().GenerateMap(MapWidth, MapHeight, MapSeed); PlacedItems.Clear(); GameMap.MapName = MapName; HumanPlayerPlaced = false; OnPropertyChanged("PlacedItems"); }
public GameViewModel() { // game initialization will be somehow placed here GameMap = MapGeneratorFactory.CreateSimpleMapGenerator().GenerateMap(20, 20, MAP_SEED); AbstractPlayer player = new HumanPlayer("Test player", GameMap.Grid[2, 2]); Player.Inventory.AddRange(new BasicItem[] { new BasicItem("Test item", new MapBlock(), 10), new BasicItem("Test item 2", new MapBlock(), 10), new BasicItem("Some cool item", new MapBlock(), 10) }); GameInstance = new Game(); GameInstance.GameMap = GameMap; GameInstance.AddHumanPlayer(player); }
public void TestSimpleMapGeneratorSeed() { IMapGenerator simpleMapGenerator = MapGeneratorFactory.CreateSimpleMapGenerator(); int w = 5; int h = 10; int seed = 87452; Direction[] allDirections = DirectionMethods.GetAllDirections(); MapBlock[,] grid = simpleMapGenerator.GenerateGrid(w, h, seed); MapBlock[,] grid2 = simpleMapGenerator.GenerateGrid(w, h, seed); Assert.IsNotNull(grid, "Grid 1 is null!"); Assert.IsNotNull(grid2, "Grid 2 is null!"); Assert.AreEqual(w, grid.GetLength(0), "Wrong width of map grid!"); Assert.AreEqual(h, grid.GetLength(1), "Wrong height of map grid!"); Assert.AreEqual(w, grid2.GetLength(0), "Wrong width of map grid 2!"); Assert.AreEqual(h, grid2.GetLength(1), "Wrong height of map grid 2!"); // all map block should be same for (int i = 0; i < w; i++) { for (int j = 0; j < h; j++) { int entrances = 0; foreach (Direction dir in allDirections) { Assert.AreEqual(grid[i, j].EntranceInDirection(dir).Exists(), grid2[i, j].EntranceInDirection(dir).Exists(), $"Map block at position [{i},{j}] has different entrance in direction {dir}."); if (grid[i, j].EntranceInDirection(dir).Exists()) { entrances++; } } Assert.IsTrue(entrances > 0, $"Block at [{i},{j}] has no entrance!"); } } }
public void TestSimpleMapGenerator() { IMapGenerator simpleMapGenerator = MapGeneratorFactory.CreateSimpleMapGenerator(); int w = 5; int h = 10; Direction[] allDirections = DirectionMethods.GetAllDirections(); Map map = simpleMapGenerator.GenerateMap(w, h, IMapGeneratorConstants.NO_SEED); MapBlock[,] grid = map.Grid; Assert.IsNotNull(grid, "Null grid returned!"); Assert.AreEqual(w, grid.GetLength(0), "Wrong width of map grid!"); Assert.AreEqual(h, grid.GetLength(1), "Wrong height of map grid!"); Assert.IsNotNull(map.WinningBlock, "Winning block is null!"); Assert.AreEqual((w - 1) / 2, map.WinningBlock.X, "Wrong X coordinate of winning block."); Assert.AreEqual((h - 1) / 2, map.WinningBlock.Y, "Wrong Y coordinate of winning block."); // test that no map block has all entrances closed for (int i = 0; i < w; i++) { for (int j = 0; j < h; j++) { int entrances = 0; foreach (Direction dir in allDirections) { if (grid[i, j].EntranceInDirection(dir).Exists()) { entrances++; } } Assert.IsTrue(entrances > 0, $"Block at [{i},{j}] has no entrance!"); } } }
/// <summary> /// Generates new game with given parameters. If aiCount+1 (1 for human player) is greater than width*heght, exception is thrown. /// </summary> /// <param name="width">Width of the map.</param> /// <param name="height">Height of the map.</param> /// <param name="mapSeed">Map seed.</param> /// <param name="aiCount">Number of AIs (simple AI is used).</param> /// <param name="monsterDensity">Density of monsters 0..1</param> /// <param name="itemDensity">Density of items 0..1</param> /// <param name="humanPlayerName">Name of the human player.</param> /// <returns>Generated game instance.</returns> public static Game GenerateGame(int width, int height, int mapSeed, int aiCount, double monsterDensity, double itemDensity, string humanPlayerName) { // check if (aiCount + 1 > width * height) { throw new Exception($"Počet protihráčů {aiCount} je na plochu {width}x{height} moc velký!"); } Map.Map gameMap = MapGeneratorFactory.CreateSimpleMapGenerator().GenerateMap(width, height, mapSeed); Game game = new Game() { GameMap = gameMap }; Random r = new Random(); // sets of occupied position for creatures and items, '{x}:{y}' HashSet <String> creatureOccupiedPositions = new HashSet <string>(); HashSet <string> itemsOccupiedPositions = new HashSet <string>(); // place human player int x = r.Next(width); int y = r.Next(height); AbstractPlayer player = new HumanPlayer(humanPlayerName, gameMap.Grid[x, y]); game.AddHumanPlayer(player); creatureOccupiedPositions.Add($"{x}:{y}"); // place AI players int pCount = 1; AddObjectsToGame(width, height, aiCount, 20, (ox, oy) => { if (!creatureOccupiedPositions.Contains($"{ox}:{oy}")) { game.AddAIPlayer(AIPlayerFactory.CreateSimpleAIPLayer($"Simple AI Player {pCount}", gameMap.Grid[ox, oy])); creatureOccupiedPositions.Add(($"{ox}:{oy}")); pCount++; return(true); } else { return(false); } }); // monster count from density: // density is expected to be in range 0..1 and will be mapped to the count of remaining free map blocks // this number is then lowered to 2/3 and this result is used as a base // then monsterCount = base + random.next(base/3) double monsterCountBase = 2 * (monsterDensity * (width * height - aiCount)) / 3; int monsterCount = (int)(monsterCountBase + r.NextDouble() * (monsterCountBase / 3)); // place monsters AddObjectsToGame(width, height, monsterCount, 20, (ox, oy) => { if (!creatureOccupiedPositions.Contains($"{ox}:{oy}")) { game.AddMonster(MonsterFactory.CreateRandomMonster(gameMap.Grid[ox, oy])); creatureOccupiedPositions.Add(($"{ox}:{oy}")); return(true); } else { return(false); } }); // item count is calculated the same way as monster count is double itemCountBase = 2 * (itemDensity * (width * height - aiCount)) / 3; int itemCount = (int)(itemCountBase + r.NextDouble() * (itemCountBase / 3)); // place items AddObjectsToGame(width, height, itemCount, 20, (ox, oy) => { if (!itemsOccupiedPositions.Contains($"{ox}:{oy}")) { game.AddItem(ItemFactory.CreateRandomItem(gameMap.Grid[ox, oy])); itemsOccupiedPositions.Add(($"{ox}:{oy}")); return(true); } else { return(false); } }); return(game); }
public void TestSerializeMaze() { int w = 4; int h = 4; Map map = MapGeneratorFactory.CreateSimpleMapGenerator().GenerateMap(w, h, IMapGeneratorConstants.NO_SEED); map.MapName = "Test map"; // add creatures to map Monster origMonster = new Monster("Test monster", map.Grid[0, 0], 4, 3654123, 87621235); map.AddCreature(origMonster); SimpleAIPlayer aiPlayer = new SimpleAIPlayer("Test player", map.Grid[3, 2]); map.AddCreature(aiPlayer); HumanPlayer hPlayer = new HumanPlayer("Příliš žluťoučký kůň úpěl ďábelské ódy", map.Grid[1, 3]) { BaseHitPoints = 98432156, BaseAttack = 112348, BaseDeffense = 41226987 }; map.AddCreature(hPlayer); // add items to map AbstractWeapon weapon = ItemFactory.CreateAxe(map.Grid[1, 3]); map.AddItem(weapon); AbstractArmor armor = ItemFactory.CreateLeatherArmor(map.Grid[1, 1]); map.AddItem(armor); AbstractInventoryItem item = new BasicItem("Příliš žluťoučký kůň úpěl ďábelské ódy.!?_/()':123456789<>&@{}[]", map.Grid[2, 2], 514) { UniqueId = 6284 }; map.AddItem(item); // serialize - deserialize IMapSerializer <byte[], byte[]> byteSerializer = new BinaryMapSerializer(); byte[] serializedMap = byteSerializer.Serialize(map); Map deserializedMap = byteSerializer.Deserialize(serializedMap); // check map Assert.AreEqual(map.MapName, deserializedMap.MapName, "Wrong map name!"); Assert.AreEqual(w, deserializedMap.Width, "Wrong width after deserialization!"); Assert.AreEqual(h, deserializedMap.Width, "Wrong height after deserialization!"); Assert.AreEqual(map.WinningBlock.X, deserializedMap.WinningBlock.X, "Wrong x coordinate of winning block!"); Assert.AreEqual(map.WinningBlock.Y, deserializedMap.WinningBlock.Y, "Wrong Y coordinate of winning block!"); // check map blocks for (int i = 0; i < w; i++) { for (int j = 0; j < h; j++) { MapBlock origBlock = map.Grid[i, j]; MapBlock testedBlock = deserializedMap.Grid[i, j]; foreach (Direction dir in DirectionMethods.GetAllDirections()) { Assert.AreEqual(origBlock.EntranceInDirection(dir).IsOpen(), testedBlock.EntranceInDirection(dir).IsOpen(), $"Wrong entrance in direction {dir} in block [{i},{j}]."); } } } // check creatures Monster m = (Monster)deserializedMap.Grid[0, 0].Creature; CheckCreature(origMonster, m); SimpleAIPlayer p = (SimpleAIPlayer)deserializedMap.Grid[3, 2].Creature; CheckCreature(aiPlayer, p); HumanPlayer hp = (HumanPlayer)deserializedMap.Grid[1, 3].Creature; CheckCreature(hPlayer, hp); // check items AbstractWeapon weap = (AbstractWeapon)map.Grid[1, 3].Item; CheckItem(weap, weapon); AbstractArmor arm = (AbstractArmor)map.Grid[1, 1].Item; CheckItem(arm, armor); AbstractInventoryItem itm = (AbstractInventoryItem)map.Grid[2, 2].Item; CheckItem(item, itm); }