public override void Execute() { TypedResult = new GetRandomUserResult(); User[] users = new User[] {}; var supplier = suppliersRepository.GetSupplierByName(SupplierName); if (supplier != null) { var order = orderRepository.GetActiveOrder(supplier); if (order == null) { throw new AppException(ErrorCodes.Order.NoActiveOrder, supplier.Name); } TypedResult.Order = order; users = order.Items.Select(i => i.User).Distinct().ToArray(); } else { var today = DateTime.Now.Date; users = orderItemsRepository.AsQueryable() .Where(u => u.Order.OrderDate == today) .Select(i => i.User).Distinct().ToArray(); } if (users.Any()) { TypedResult.User = randomizer.GetRandomItem(users); TypedResult.Users = users; } base.Execute(); }
public override void Execute() { TypedResult = new PickOperatorResult(); var users = new User[] { }; var supplier = suppliersRepository.GetSupplierByName(TargetName); if (supplier == null) { throw new AppException(ErrorCodes.Supplier.UnknownSupplier); } var order = orderRepository.GetActiveOrder(supplier); if (order == null) { throw new AppException(ErrorCodes.Order.NoActiveOrder, supplier.Name); } TypedResult.Order = order; users = userRepository.GetUserOperatorPool(order).ToArray(); if (users.Any()) { TypedResult.User = randomizer.GetRandomItem(users); TypedResult.Users = users; TypedResult.Order.Operator = TypedResult.User; } base.Execute(); }
private Direction?GetRandomValidDirection(Map <T> map, T currentCell, T previousCell, IRandomizer randomizer) { var invalidDirections = new List <Direction>(); var squareDirections = new List <Direction>(); while (invalidDirections.Count + squareDirections.Count < GetAll.ValuesOf <Direction>().Count()) { var direction = randomizer.GetRandomEnumValue(invalidDirections.Union(squareDirections)); if (IsDirectionValid(map, currentCell, direction, previousCell)) { var nextCell = map.GetAdjacentCell(currentCell, direction); //Try to avoid creating squares, but do it if there's no other way if (nextCell.IsOpen && ((nextCell.Sides[direction.Rotate()] && currentCell.Sides[direction.Rotate()]) || (nextCell.Sides[direction.Rotate(false)] && currentCell.Sides[direction.Rotate(false)]))) { squareDirections.Add(direction); } else { return(direction); } } else { invalidDirections.Add(direction); } } return(squareDirections.Any() ? randomizer.GetRandomItem(squareDirections) : (Direction?)null); }
private static Room CreateRoom(IRandomizer randomizer, HashSet <Size> validSizes) { if (validSizes.Count == 0) { return(null); } var size = randomizer.GetRandomItem(validSizes); var room = new Room() { Size = size }; return(room); }
public void ProcessMap(Map <T> map, DungeonConfiguration configuration, IRandomizer randomizer) { //Start with a rectangular grid, x units wide and y units tall. Mark each cell in the grid unvisited var visitedCells = new HashSet <T>(); var visitedValidCells = new HashSet <T>(); // var deadEndCells = new HashSet<T>(); Direction?previousDirection = null; //Pick a random cell in the grid and mark it visited. This is the current cell. var currentCell = randomizer.GetRandomCell(map); currentCell.IsOpen = true; while (visitedCells.Count < map.Width * map.Height) { var oldCell = currentCell; var changed = false; visitedCells.Add(currentCell); visitedValidCells.Add(currentCell); //From the current cell, pick a random direction (north, south, east, or west). //If (1) there is no cell adjacent to the current cell in that direction, or (2) if //the adjacent cell in that direction has been visited, then that direction //is invalid, and you must pick a different random direction. var direction = GetRandomValidDirection(map, currentCell, visitedCells, configuration.Randomness, previousDirection, randomizer); if (direction.HasValue) { //Let's call the cell in the chosen direction C. Create a corridor between the //current cell and C, and then make C the current cell. Mark C visited. changed = !currentCell.Sides[direction.Value]; currentCell = map.GetAdjacentCell(currentCell, direction.Value); currentCell.Sides[direction.Value.Opposite()] = oldCell.Sides[direction.Value] = true; previousDirection = direction; } else { //If all directions are invalid, pick a different random visited cell in the grid and start this step over again. // deadEndCells.Add(currentCell); visitedValidCells.Remove(currentCell); currentCell = randomizer.GetRandomItem(visitedValidCells); } if (currentCell.IsOpen && !changed) { continue; } currentCell.IsOpen = true; //Repeat until all cells in the grid have been visited. } }
private void ConnectRoom(Map <T> map, IRandomizer randomizer, Room room, List <Room> isolatedRooms) { //need to tunnel to a nearby area List <T> adjacentCells = null; var distance = 2; do { adjacentCells = map.GetCellsAdjacentToRoom(room, distance).ToList(); var validAdjacentCells = adjacentCells.Where(cell => cell.Terrain != TerrainType.Rock && !isolatedRooms.Any(r => r.IsLocationInRoom(cell.Row, cell.Column))).ToList(); if (validAdjacentCells.Any()) { var targetCell = randomizer.GetRandomItem(validAdjacentCells); var targetDirection = Direction.East; if (targetCell.Row < room.Row) { targetDirection = Direction.South; } else if (targetCell.Column >= room.Right) { targetDirection = Direction.West; } else if (targetCell.Row >= room.Bottom) { targetDirection = Direction.North; } do { var nextCell = map.GetAdjacentCell(targetCell, targetDirection); nextCell.Terrain = TerrainType.Floor; targetCell = nextCell; } while (!room.IsLocationInRoom(targetCell.Row, targetCell.Column)); isolatedRooms.Remove(room); break; } distance++; } while (adjacentCells.Any()); }
public void ProcessMap(Map <T> map, DungeonConfiguration configuration, IRandomizer randomizer) { var validSizes = GetAllPossibleRoomSizes(configuration); for (var i = 0; i < configuration.RoomCount; i++) { //Generate a room such that Wmin <= Rw <= Wmax and Hmin <= Rh <= Hmax. var room = CreateRoom(randomizer, validSizes); if (room == null) { break; } var visitedCells = new HashSet <T>(); var unvisitedCells = new HashSet <T>(map.AllCells); var roomPlaced = false; while (visitedCells.Count < map.Height * map.Width) { //get a random cell var cell = randomizer.GetRandomItem(unvisitedCells); visitedCells.Add(cell); unvisitedCells.Remove(cell); //place the room room.Row = cell.Row; room.Column = cell.Column; if (room.Column <= 0 || room.Right >= map.Width || room.Row <= 0 || room.Bottom >= map.Height) { continue; //out of bounds } var cells = map.GetRoomCells(room).ToList(); //don't place room where it is overlapping another room if (cells.Any(c => map.IsLocationInRoom(c.Row, c.Column))) { continue; } //don't place room where it is adjacent to another room if (map.GetCellsAdjacentToRoom(room).Any(c => map.IsLocationInRoom(c.Row, c.Column))) { continue; } //corners are rock if (!AreAllCornerCellsRocks(map, room)) { continue; //NW corner } //all corridors leading into room can become doors (are isolated) if (!CanAllCorridorsLeadingToRoomBeDoors(map, room)) { continue; } PlaceRoom(map, room); roomPlaced = true; break; } if (!roomPlaced) { validSizes.Remove(room.Size); } } }