private void AddTerrainText(Position mousePosition, StringBuilder positionText) { IOsnowaContext context = _contextManager.Current; byte standingIdAtPosition = context.TileMatricesByLayer[(int)TilemapLayer.Standing].Get(mousePosition); byte floorIdAtPosition = context.TileMatricesByLayer[(int)TilemapLayer.Floor].Get(mousePosition); byte soilIdAtPosition = context.TileMatricesByLayer[(int)TilemapLayer.Soil].Get(mousePosition); OsnowaBaseTile[] tilesByIds = _tileByIdProvider.GetTilesByIds(); OsnowaBaseTile standingBaseTile = tilesByIds[standingIdAtPosition]; OsnowaBaseTile floorBaseTile = tilesByIds[floorIdAtPosition]; OsnowaBaseTile soilBaseTile = tilesByIds[soilIdAtPosition]; if (standingBaseTile != null) { positionText.Append(standingBaseTile.name + ". " + Environment.NewLine); } if (floorBaseTile != null) { positionText.Append(floorBaseTile.name + ". " + Environment.NewLine); } if (soilBaseTile != null) { positionText.Append(soilBaseTile.name + ". " + Environment.NewLine); } positionText.Append(mousePosition); }
private void GenerateWildlife(IOsnowaContext osnowaContext, float enemyCountRate) { int countPerSpecies = (int)(enemyCountRate * 10); GenerateSingleMonsters(osnowaContext, _gameConfig.EntityRecipees.Wolf, countPerSpecies, enemyCountRate); GenerateSingleMonsters(osnowaContext, _gameConfig.EntityRecipees.Bear, countPerSpecies, enemyCountRate); GenerateSingleMonsters(osnowaContext, _gameConfig.EntityRecipees.Deer, countPerSpecies, enemyCountRate); }
public void InitializeAlgorithms(IOsnowaContext newContext) { _jps = new Jps(newContext.PathfindingData.WallMatrixForJps); _jpsTightDiagonal = new JpsTightDiagonal(newContext.PathfindingData.WallMatrixForJps); _jpsStrictWalk = new JpsStrictWalk(newContext.PathfindingData.WallMatrixForJps); _spatialAStar = new SpatialAStar <MyPathNode, Position>(newContext.PathfindingData.PathNodeMatrixForSpatialAStar); _ready = true; }
private void GenerateSingleMonsters(IOsnowaContext osnowaContext, IEntityRecipee monsterRecipee, int count, float enemyCountRate) { for (int i = 0; i < count; i++) { Position position; position = GetRandomWalkablePositionOnBiggestArea(osnowaContext); GameEntity singleAnimal; _entityGenerator.GenerateActorFromRecipeeAndAddToContext(_context, monsterRecipee, position, out singleAnimal); } }
private Position GetRandomWalkablePositionOnBiggestArea(IOsnowaContext osnowaContext, bool farFromPlayer = true, float chanceForPreferredBiomeRequirement = 1f) { Position position; bool positionIsAccepted; do { position = _rng.NextPosition(osnowaContext.PositionFlags.XSize, osnowaContext.PositionFlags.YSize); positionIsAccepted = (!farFromPlayer || Position.Distance(osnowaContext.StartingPosition, position) > 25) && osnowaContext.PositionFlags.Get(position).HasFlag(PositionFlag.Walkable); } while (!positionIsAccepted); return(position); }
/// <summary> /// Marking /// </summary> private void MarkIsolatedAreas(IOsnowaContext context) { var floodSpiller = new FloodSpiller(); PositionFlags positionFlags = context.PositionFlags; var stopwatch = Stopwatch.StartNew(); byte areaIndex = 0; byte biggestAreaIndex = 0; int maxArea = 0; for (int probeX = 0; probeX < context.PositionFlags.XSize; probeX += 10) { for (int probeY = 0; probeY < context.PositionFlags.YSize; probeY += 10) { bool isFine = false; int totalVisited = 0; Position start = new Position(probeX, probeY); var parameters = new FloodParameters(start.x, start.y) { Qualifier = (x, y) => positionFlags.IsWalkable(x, y), NeighbourProcessor = (x, y, mark) => { if (!isFine) { isFine = true; areaIndex += 1; } totalVisited += 1; } }; int[,] markMatrix = new int[context.PositionFlags.XSize, context.PositionFlags.YSize]; floodSpiller.SpillFlood(parameters, markMatrix); if (totalVisited > 50) { // BUG looks like areas are not isolated, all are of same size of the whole island! // Debug.Log("visited " + totalVisited + "from " + start.x + ", " + start.y + " with index " + areaIndex); } if (totalVisited > maxArea) { maxArea = totalVisited; biggestAreaIndex = areaIndex; } } } Debug.Log("biggest isolated area index: " + biggestAreaIndex); Debug.Log("biggest isolated area index: " + maxArea); Debug.Log("marking isolated areas took: " + stopwatch.ElapsedMilliseconds); }
private void GeneratePlayer(IOsnowaContext osnowaContext) { Position startingPlayerPosition = new Position(osnowaContext.PositionFlags.XSize / 2, osnowaContext.PositionFlags.YSize / 2); // context.StartingPosition; if (osnowaContext.PositionFlags.Get(startingPlayerPosition + Position.Up).HasFlag(PositionFlag.Walkable)) { startingPlayerPosition = startingPlayerPosition + Position.Up; } if (osnowaContext.PositionFlags.Get(startingPlayerPosition + Position.Up).HasFlag(PositionFlag.Walkable)) { startingPlayerPosition = startingPlayerPosition + Position.Up; } //Position position = villageWithEntrance?.Entrance ?? context.Villages.First().Square.Positions.First(); GameEntity playerEntity; _entityGenerator.GenerateActorFromRecipeeAndAddToContext(_context, _gameConfig.EntityRecipees.Player, startingPlayerPosition, out playerEntity, true); }
public void Generate() { var stopwatch = Stopwatch.StartNew(); IOsnowaContext context = _contextManager.Current; int xSize = context.PositionFlags.XSize; int ySize = context.PositionFlags.YSize; _allTilemaps = _sceneContext.AllTilemapsByLayers.ToArray(); foreach (Tilemap tilemap in _sceneContext.AllPresentableTilemaps) { tilemap.ClearAllTiles(); } int totalMapArea = xSize * ySize; List <Vector3Int>[] batchPositionsLayers; List <TileBase>[] batchTilesLayers; CreateBatchPositionsAndTilesLayers(totalMapArea, out batchPositionsLayers, out batchTilesLayers); OsnowaBaseTile[] tilesByIds = _tileByIdFromFolderProvider.GetTilesByIds(); for (int x = 0; x < xSize; x++) { for (int y = 0; y < ySize; y++) { for (int matrixLayer = 0; matrixLayer < context.TileMatricesByLayer.Length; matrixLayer++) { // if (matrixLayer == TilemapLayers.Roof || matrixLayer == TilemapLayers.OnRoof) // continue; MatrixByte tileMatrixByte = context.TileMatricesByLayer[matrixLayer]; byte tileId = tileMatrixByte.Get(x, y); if (tileId == 0) { continue; } OsnowaBaseTile baseTile = tilesByIds[tileId]; if (baseTile == null) { throw new System.Exception($"Tile with ID {tileId} not found in tileset, but placed on map."); } PrepareTileToSet(x, y, batchPositionsLayers, batchTilesLayers, (TilemapLayer)matrixLayer, baseTile); } } } var stopwatchBatch = Stopwatch.StartNew(); for (int tilemapIndex = 0; tilemapIndex < _allTilemaps.Length; tilemapIndex++) { Tilemap tilemap = _allTilemaps[tilemapIndex]; tilemap.SetTiles(batchPositionsLayers[tilemapIndex].ToArray(), batchTilesLayers[tilemapIndex].ToArray()); UnityEngine.Debug.Log(tilemap.name + " tilemap: " + stopwatchBatch.ElapsedMilliseconds + " milliseconds."); stopwatchBatch.Restart(); } BoundsInt fogOfWarBounds = _sceneContext.TilemapDefiningOuterBounds.cellBounds; OsnowaBaseTile[] fogOfWarToSet = Enumerable.Repeat(_tileset.FogOfWar, fogOfWarBounds.size.x * fogOfWarBounds.size.y).ToArray(); _sceneContext.FogOfWarTilemap.SetTilesBlock(fogOfWarBounds, fogOfWarToSet); BoundsInt maskBounds = _sceneContext.TilemapDefiningOuterBounds.cellBounds; OsnowaBaseTile[] unseenMaskToSet = Enumerable.Repeat(_tileset.UnseenMask, maskBounds.size.x * maskBounds.size.y).ToArray(); _sceneContext.UnseenMaskTilemap.SetTilesBlock(maskBounds, unseenMaskToSet); UnityEngine.Debug.Log("Tile generation time for " + xSize * ySize + " positions: " + stopwatch.ElapsedMilliseconds); UnityEngine.Debug.Log("Total tiles: " + batchPositionsLayers.Sum(l => l.Count)); }
public void ReplaceContext(IOsnowaContext newContext) { _contextManager.ReplaceContext(newContext); }
private void OnContextReplaced(IOsnowaContext newContext) { _positionFlags = newContext.PositionFlags; _tileMatricesByteByLayer = newContext.TileMatricesByLayer; }