private static void ProcessNeighbours(Position currentNode, Dictionary <Position, int> delays, IFloodArea enemyFlood, int currentNodeScore, int currentDelay, int allowedDelay, Dictionary <Position, Position> predecessors, Queue <Position> openPositions, int[] bestScoresForDelays, Position[] bestPositionsForDelays) { foreach (Position neighbour in PositionUtilities.Neighbours8(currentNode) .Where(n => !delays.ContainsKey(n))) { int neighbourScore = enemyFlood.GetValueAtPosition(neighbour); if (neighbourScore == int.MaxValue) { continue; } int neighbourDelay = currentNodeScore - neighbourScore + 1 + currentDelay; if (neighbourDelay > allowedDelay) { continue; } delays[neighbour] = neighbourDelay; predecessors[neighbour] = currentNode; openPositions.Enqueue(neighbour); if (neighbourScore > bestScoresForDelays[neighbourDelay]) { bestScoresForDelays[neighbourDelay] = neighbourScore; bestPositionsForDelays[neighbourDelay] = neighbour; } } }
public void FindForItem_FirstDestinationAndMostNeighboursAreNotEligibleButOneNeighbourIs_ReturnsNeighbour() { var checkedPosition = new Position(1, 1); var eligibleNeighbour = new Position(2, 1); IGrid gip = Mock.Of <IGrid>(p => p.IsWalkable( It.Is <Position>(v => PositionUtilities.Neighbours8(checkedPosition).Contains(v)) ) == false && p.IsWalkable(eligibleNeighbour) == true); IEntityDetector entityDetector = Mock.Of <IEntityDetector>(d => d.DetectEntities(checkedPosition) == Enumerable.Empty <GameEntity>()); var finder = new FirstPlaceInAreaFinder(gip, entityDetector, new RandomNumberGenerator(93432)); Position?result = finder.FindForItem(checkedPosition); result.Should().Be(eligibleNeighbour); }
public override IEnumerator Recalculating() { SpreadInitialSeeds(); yield return(new WaitForSeconds(0.1f)); int iterations = _config.Iterations; for (var iteration = 0; iteration < iterations; iteration++) { SimulateOneGeneration(iteration < iterations - 2); yield return(new WaitForSeconds(0.1f)); } for (var x = 1; x < Values.XSize - 1; x++) { for (var y = 1; y < Values.YSize - 1; y++) { Plant plant = _plants[x, y]; if (plant != null) { if (plant.Tile == null) { throw new ArgumentNullException($"Missing tile for plant {plant.name}."); } TilemapLayer layer = plant.Tile.Layer; byte id = plant.Tile.Id; _tileMatricesByte[(int)layer].Set(x, y, id); if (!plant.GrowsBelowOtherPlants) //not grass layer { Plant neighbourGrowingBelow = PositionUtilities.Neighbours8(new Position(x, y)) .Select(n => _plants[n.x, n.y]) .FirstOrDefault(p => p != null && p.GrowsBelowOtherPlants); if (neighbourGrowingBelow != null) { _tileMatricesByte[(int)neighbourGrowingBelow.Tile.Layer] .Set(x, y, neighbourGrowingBelow.Tile.Id); } } } } } }
public Position?FindForActor(Position source) { bool sourceIsEligible = _grid.IsWalkable(source) && !_entityDetector.DetectEntities(source).Any(); if (sourceIsEligible) { return(source); } ; IList <Position> neighboursShuffled = _rng.Shuffle(PositionUtilities.Neighbours8(source)); foreach (Position neighbour in neighboursShuffled) { bool neighbourIsEligible = _grid.IsWalkable(neighbour) && !_entityDetector.DetectEntities(neighbour).Any(); if (neighbourIsEligible) { return(neighbour); } } return(null); }
public Position?FindForItem(Position source) { List <GameEntity> allAtSource = _entityDetector.DetectEntities(source).ToList(); bool sourceIsEligible = _grid.IsWalkable(source) && allAtSource.All(e => e.hasHeld || e.isBlockingPosition); if (sourceIsEligible) { return(source); } ; IList <Position> neighboursShuffled = _rng.Shuffle(PositionUtilities.Neighbours8(source)); foreach (Position neighbour in neighboursShuffled) { bool neighbourIsEligible = _grid.IsWalkable(neighbour) && !_entityDetector.DetectEntities(neighbour).Any(); if (neighbourIsEligible) { return(neighbour); } } return(null); }