Example #1
0
        public static void DrawAdjacentCells(Cell cell, GridDungeonModel model, Color color)
        {
            foreach (var adjacentId in cell.AdjacentCells)
            {
                var adjacentCell = model.GetCell(adjacentId);
                if (adjacentCell == null)
                {
                    return;
                }
                var centerA = Vector3.Scale(cell.Bounds.CenterF(), model.Config.GridCellSize);
                var centerB = Vector3.Scale(adjacentCell.Bounds.CenterF(), model.Config.GridCellSize);
                Debug.DrawLine(centerA, centerB, color, 0, false);
            }

            foreach (var adjacentId in cell.FixedRoomConnections)
            {
                var adjacentCell = model.GetCell(adjacentId);
                if (adjacentCell == null)
                {
                    return;
                }
                var centerA = Vector3.Scale(cell.Bounds.CenterF(), model.Config.GridCellSize) + new Vector3(0, 0.2f, 0);
                var centerB = Vector3.Scale(adjacentCell.Bounds.CenterF(), model.Config.GridCellSize) + new Vector3(0, 0.2f, 0);
                Debug.DrawLine(centerA, centerB, Color.red, 0, false);
            }
        }
        void UpdateHeights(GridDungeonModel model)
        {
            if (terrain == null || terrain.terrainData == null)
            {
                return;
            }
            var rasterizer = new LandscapeDataRasterizer(terrain, groundLevelHeight);

            rasterizer.LoadData();
            var gridSize = model.Config.GridCellSize;

            // Raise the terrain
            foreach (var cell in model.Cells)
            {
                var locationGrid = cell.Bounds.Location;
                var location     = locationGrid * gridSize;
                var size         = cell.Bounds.Size * gridSize;
                var cellY        = location.y + layoutLevelOffset;
                rasterizer.DrawCell(location.x, location.z, size.x, size.z, cellY);
            }

            // Smooth the terrain
            foreach (var cell in model.Cells)
            {
                var locationGrid = cell.Bounds.Location;
                var location     = locationGrid * gridSize;
                var size         = cell.Bounds.Size * gridSize;
                var cellY        = location.y + layoutLevelOffset;
                var curve        = (cell.CellType == CellType.Room) ? roomElevationCurve : corridorElevationCurve;
                rasterizer.SmoothCell(location.x, location.z, size.x, size.z, cellY, smoothingDistance, curve);
            }

            rasterizer.SaveData();
        }
        void EmitCornerMarker(DungeonBuilder builder, GridDungeonModel model, IntVector point, string markerName)
        {
            // Add an empty marker here
            var gridSize = model.Config.GridCellSize;
            var position = point * gridSize;

            position += Vector3.Scale(new Vector3(0.5f, 0, 0.5f), gridSize);
            var transform = Matrix4x4.TRS(position, Quaternion.identity, Vector3.one);

            builder.EmitMarker(markerName, transform, point, -1);
        }
 void EmitForPoint(DungeonBuilder builder, GridDungeonModel model, IntVector point)
 {
     foreach (var config in CornerConfigs)
     {
         if (ConfigMatches(model, point, config))
         {
             EmitCornerMarker(builder, model, point, config.markerName);
             break;
         }
     }
 }
Example #5
0
        public GridDungeonModel GetDungeonModelGrid()
        {
            var model = base.GetDungeonModel();

            gridModel = model as GridDungeonModel;
            if (gridModel == null)
            {
                Debug.LogWarning("Invalid dungeon model type for this type of paint tool.  Expected DungeonModelGrid.  Received:" + (model != null ? model.GetType().ToString() : "null"));
            }

            return(gridModel);
        }
Example #6
0
        public static Cell[] FindFurthestRooms(GridDungeonModel model)
        {
            var bestLength = 0;
            var result     = new Cell[2];

            foreach (var startCell in model.Cells)
            {
                var queue     = new Queue <LongestPathBFSData>();
                var visited   = new HashSet <int>();
                var startData = new LongestPathBFSData {
                    cell = startCell, length = 0
                };
                LongestPathBFSData endData = startData;

                queue.Enqueue(startData);
                while (queue.Count > 0)
                {
                    var front = queue.Dequeue();
                    visited.Add(front.cell.Id);

                    foreach (var childId in front.cell.FixedRoomConnections)
                    {
                        if (visited.Contains(childId))
                        {
                            continue;
                        }
                        var child     = model.GetCell(childId);
                        var childData = new LongestPathBFSData {
                            cell = child, length = front.length + 1
                        };
                        queue.Enqueue(childData);
                    }

                    if (queue.Count == 0)
                    {
                        endData = front;
                    }
                }

                if (endData.length > bestLength)
                {
                    bestLength = endData.length;
                    result[0]  = startData.cell;
                    result[1]  = endData.cell;
                }
            }

            return(result);
        }
        void UpdateTerrainTextures(GridDungeonModel model)
        {
            if (terrain == null || terrain.terrainData == null)
            {
                return;
            }

            var numTextures = textures.Length;
            var data        = terrain.terrainData;
            var map         = new float[data.alphamapWidth, data.alphamapHeight, numTextures];

            UpdateBaseTexture(model, map);
            UpdateCliffTexture(map);

            data.SetAlphamaps(0, 0, map);
        }
        bool ConfigMatches(GridDungeonModel model, IntVector point, CellSpatialConfig config)
        {
            var neighbors = config.neighborConfig;

            for (int i = 0; i < neighbors.Length; i++)
            {
                var code = neighbors[i];
                if (code == 0)
                {
                    // Don't care about this cell
                    continue;
                }
                var dx = i % 3;
                var dz = i / 3;
                dx--; dz--;                      // bring to -1..1 range (from previous 0..2)
                dz *= -1;
                var x = point.x + dx;
                var z = point.z + dz;

                var  cellInfo = model.GetGridCellLookup(x, z);
                bool empty    = cellInfo.CellType == CellType.Unknown;
                if (code == 1 && empty)
                {
                    // We were expecting a non-empty space here, but it is empty
                    return(false);
                }
                else if (code == 2 && !empty)
                {
                    // We were expecting a empty space here, but it is not empty
                    return(false);
                }
            }

            // Matches, all tests have passed
            return(true);
        }
 void BuildTerrain(GridDungeonModel model)
 {
     SetupTextures();
     UpdateHeights(model);
     UpdateTerrainTextures(model);
 }
Example #10
0
        void UpdateBaseTexture(GridDungeonModel model, float[,,] map)
        {
            if (terrain == null)
            {
                return;
            }
            int fillIndex = GetTextureIndex(LandscapeTextureType.Fill);

            if (fillIndex < 0)
            {
                return;
            }

            var data = terrain.terrainData;

            // Fill up the entire space with the fill texture
            for (var y = 0; y < data.alphamapHeight; y++)
            {
                for (var x = 0; x < data.alphamapWidth; x++)
                {
                    for (int t = 0; t < textures.Length; t++)
                    {
                        var ratio = (t == fillIndex) ? 1 : 0;
                        map[y, x, t] = ratio;
                    }
                }
            }

            int roadIndex = GetTextureIndex(LandscapeTextureType.Corridor);

            if (roadIndex >= 0)
            {
                var gridSize  = model.Config.GridCellSize;
                var layoutMap = new float[map.GetLength(0), map.GetLength(1)];
                foreach (var cell in model.Cells)
                {
                    var bounds = cell.Bounds;
                    var locationGrid = bounds.Location;
                    var location = locationGrid * gridSize;
                    var size = bounds.Size * gridSize;
                    int gx1, gy1, gx2, gy2;
                    LandscapeDataRasterizer.WorldToTerrainTextureCoord(terrain, location.x, location.z, out gx1, out gy1);
                    LandscapeDataRasterizer.WorldToTerrainTextureCoord(terrain, location.x + size.x, location.z + size.z, out gx2, out gy2);
                    for (var gx = gx1; gx <= gx2; gx++)
                    {
                        for (var gy = gy1; gy <= gy2; gy++)
                        {
                            layoutMap[gy, gx] = 1;
                        }
                    }
                }

                // Blur the layout data
                var filter = new BlurFilter(roadBlurDistance);
                layoutMap = filter.ApplyFilter(layoutMap);

                // Fill up the inner region with corridor index
                for (var y = 0; y < data.alphamapHeight; y++)
                {
                    for (var x = 0; x < data.alphamapWidth; x++)
                    {
                        bool corridor = (layoutMap[y, x] > corridorBlurThreshold);
                        if (corridor)
                        {
                            map[y, x, roadIndex] = 1;
                            map[y, x, fillIndex] = 0;
                        }
                    }
                }
            }
        }