public static void GenerateRoomsInsideContainersNode(BspTree node)
 {
     // should create rooms for leafs
     if (node.IsLeaf())
     {
         var randomX = UnityEngine.Random.Range(DungeonGenerator.MIN_ROOM_DELTA, node.container.width / 4);
         var randomY = UnityEngine.Random.Range(DungeonGenerator.MIN_ROOM_DELTA, node.container.height / 4);
         int roomX   = node.container.x + randomX;
         int roomY   = node.container.y + randomY;
         int roomW   = node.container.width - (int)(randomX * UnityEngine.Random.Range(1f, 2f));
         int roomH   = node.container.height - (int)(randomY * UnityEngine.Random.Range(1f, 2f));
         node.room = new RectInt(roomX, roomY, roomW, roomH);
     }
     else
     {
         if (node.left != null)
         {
             GenerateRoomsInsideContainersNode(node.left);
         }
         if (node.right != null)
         {
             GenerateRoomsInsideContainersNode(node.right);
         }
     }
 }
        public JToken GetDisplacements([Url] string mapName)
        {
            var bsp           = GetBspFile(Request, mapName);
            var tree          = new BspTree(bsp, 0);
            var displacements = new JArray();

            foreach (var dispInfo in bsp.DisplacementInfos)
            {
                var face = bsp.FacesHdr[dispInfo.MapFace];

                Vector3 min, max;
                GetDisplacementBounds(bsp, face.DispInfo, out min, out max, 1f);

                displacements.Add(new JObject
                {
                    { "index", face.DispInfo },
                    { "power", dispInfo.Power },
                    { "min", min.ToJson() },
                    { "max", max.ToJson() },
                    { "clusters", GetIntersectingClusters(tree, min, max) }
                });
            }

            return(new JObject
            {
                { "displacements", displacements }
            });
        }
Beispiel #3
0
    private void buildBspTree()
    {
        var mesh = GetComponent <MeshFilter>().mesh;

        bspTree = new BspTree();

        bspTree.buildFromMesh(mesh, transform);
    }
Beispiel #4
0
 public MapGeometry(MapData map)
 {
     CreateSectorsAndPlanes(map);
     CreateSides(map);
     CreateLines(map);
     CreateWalls();
     BspTree = CreateSubsectorsAndBspTree(map);
 }
Beispiel #5
0
        public override void Apply(Generator gen, LevelGenerator levelGen, Level level)
        {
            var rnd   = new Random(gen.Seed);
            var bound = Padding.Apply(new Rectangle(0, 0, level.Width, level.Height));
            var tree  = BspTree.BuildBspTree((int)Padding.Left, (int)Padding.Top,
                                             level.Width - (int)Padding.Left - (int)Padding.Right,
                                             level.Height - (int)Padding.Top - (int)Padding.Bottom,
                                             Depth, rnd);

            Build(tree.Root, rnd, level);
        }
Beispiel #6
0
            public MapParams(ValveBspFile bsp)
            {
                Bsp  = bsp;
                Tree = new BspTree(bsp, 0);

                AreaPortalNames =
                    new HashSet <string>(bsp.Entities
                                         .Where(x => x["classname"] == "func_areaportal")
                                         .Select(x => (string)x["target"])
                                         .Where(x => x != null));
            }
    internal static BspTree Split(int numberOfIterations, RectInt container)
    {
        var node = new BspTree(container);

        if (numberOfIterations == 0)
        {
            return(node);
        }

        var splittedContainers = SplitContainer(container);

        node.left  = Split(numberOfIterations - 1, splittedContainers[0]);
        node.right = Split(numberOfIterations - 1, splittedContainers[1]);

        return(node);
    }
Beispiel #8
0
        public JToken GetIndex([Url] string mapName)
        {
            var bsp     = GetBspFile(Request, mapName);
            var skyName = bsp.Entities.GetFirst <WorldSpawn>().SkyName;
            var fogInfo = bsp.Entities.GetFirst <EnvFogController>(false);
            var skyInfo = bsp.Entities.GetFirst <SkyCamera>();

            var fogData = new JObject();

            AddFogInfo(fogData, fogInfo, 1f);

            var skyData = new JObject
            {
                { "enabled", skyInfo != null }
            };

            if (skyInfo != null)
            {
                skyData.Add("origin", skyInfo.Origin.ToJson());
                skyData.Add("scale", skyInfo.Scale);
                AddFogInfo(skyData, skyInfo, skyInfo.Scale);
            }

            var tree            = new BspTree(bsp, 0);
            var areaPortalNames = new HashSet <string>(bsp.Entities.OfType <FuncAreaPortal>().Select(x => x.Target).Where(x => x != null));

            return(new JObject
            {
                { "name", mapName },
                { "skyMaterial", GetSkyMaterial(bsp, skyName) },
                { "fog", fogData },
                { "skyCamera", skyData },
                { "playerStarts", new JArray(bsp.Entities.OfType <InfoPlayerStart>().Select(x => x.Origin.ToJson())) },
                { "numClusters", bsp.Visibility.NumClusters },
                { "numModels", bsp.Models.Length },
                { "brushEnts", new JArray(bsp.Entities.OfType <FuncBrush>()
                                          .Where(x => x.Model != null && x.RenderMode != 10 && (x.TargetName == null || !areaPortalNames.Contains(x.TargetName)))
                                          .Select(x => SerializeFuncBrush(bsp, tree, x))) },
                { "modelUrl", GetActionUrl(nameof(GetModels), Replace("mapName", mapName)) },
                { "displacementsUrl", GetActionUrl(nameof(GetDisplacements), Replace("mapName", mapName)) },
                { "facesUrl", GetActionUrl(nameof(GetFaces), Replace("mapName", mapName)) },
                { "visibilityUrl", GetActionUrl(nameof(GetVisibility), Replace("mapName", mapName)) },
                { "lightmapUrl", GetActionUrl(nameof(GetLightmap), Replace("mapName", mapName)) },
                { "materialsUrl", GetActionUrl(nameof(GetMaterials), Replace("mapName", mapName)) },
                { "staticPropsUrl", GetActionUrl(nameof(GetStaticProps), Replace("mapName", mapName)) }
            });
        }
Beispiel #9
0
        private JToken SerializeFuncBrush(ValveBspFile bsp, BspTree tree, FuncBrush ent)
        {
            var modelIndex = int.Parse(ent.Model.Substring(1));
            var model      = bsp.Models[modelIndex];

            var min = model.Min + ent.Origin;
            var max = model.Max + ent.Origin;

            return(new JObject
            {
                { "classname", ent.ClassName },
                { "origin", ent.Origin.ToJson() },
                { "angles", ent.Angles.ToJson() },
                { "model", modelIndex },
                { "clusters", modelIndex == 0 ? new JArray() : GetIntersectingClusters(tree, min, max) }
            });
        }
 private void GenerateCorridorsNode(BspTree node)
 {
     print('a');
     if (node.IsInternal())
     {
         print('b');
         RectInt leftContainer  = node.left.container;
         RectInt rightContainer = node.right.container;
         Vector2 leftCenter     = leftContainer.center;
         Vector2 rightCenter    = rightContainer.center;
         Vector2 direction      = (rightCenter - leftCenter).normalized;        // arbitrarily choosing right as the target point
         while (Vector2.Distance(leftCenter, rightCenter) > 1)
         {
             if (direction.Equals(Vector2.right))
             {
                 for (int i = 0; i < corridorThickness; i++)
                 {
                     map.SetTile(new Vector3Int((int)leftCenter.x, (int)leftCenter.y + i, 0), mmTile);
                 }
             }
             else if (direction.Equals(Vector2.up))
             {
                 for (int i = 0; i < corridorThickness; i++)
                 {
                     map.SetTile(new Vector3Int((int)leftCenter.x + i, (int)leftCenter.y, 0), mmTile);
                 }
             }
             leftCenter.x += direction.x;
             leftCenter.y += direction.y;
         }
         if (node.left != null)
         {
             GenerateCorridorsNode(node.left);
         }
         if (node.right != null)
         {
             GenerateCorridorsNode(node.right);
         }
     }
 }
Beispiel #11
0
        private static List <int> GetIntersectingClusters(BspTree tree, SourceUtils.Vector3 min, SourceUtils.Vector3 max)
        {
            if (_sLeafBuffer == null)
            {
                _sLeafBuffer = new List <BspTree.Leaf>();
            }
            else
            {
                _sLeafBuffer.Clear();
            }

            tree.GetIntersectingLeaves(min, max, _sLeafBuffer);

            var clusters = new List <int>();

            foreach (var cluster in _sLeafBuffer.Select(x => x.Info.Cluster).Distinct())
            {
                clusters.Add(cluster);
            }

            return(clusters);
        }
    public void DebugDrawBspNode(BspTree node)
    {
        // Container
        Gizmos.color = Color.green;
        // top
        Gizmos.DrawLine(new Vector3(node.container.x, node.container.y, 0), new Vector3Int(node.container.xMax, node.container.y, 0));
        // right
        Gizmos.DrawLine(new Vector3(node.container.xMax, node.container.y, 0), new Vector3Int(node.container.xMax, node.container.yMax, 0));
        // bottom
        Gizmos.DrawLine(new Vector3(node.container.x, node.container.yMax, 0), new Vector3Int(node.container.xMax, node.container.yMax, 0));
        // left
        Gizmos.DrawLine(new Vector3(node.container.x, node.container.y, 0), new Vector3Int(node.container.x, node.container.yMax, 0));

        // children
        if (node.left != null)
        {
            DebugDrawBspNode(node.left);
        }
        if (node.right != null)
        {
            DebugDrawBspNode(node.right);
        }
    }
 private void UpdateTilemapUsingTreeNode(BspTree node)
 {
     if (node.left == null && node.right == null)
     {
         for (int i = node.room.x; i < node.room.xMax; i++)
         {
             for (int j = node.room.y; j < node.room.yMax; j++)
             {
                 map.SetTile(new Vector3Int(i, j, 0), mmTile);
             }
         }
     }
     else
     {
         if (node.left != null)
         {
             UpdateTilemapUsingTreeNode(node.left);
         }
         if (node.right != null)
         {
             UpdateTilemapUsingTreeNode(node.right);
         }
     }
 }
 private void GenerateContainersUsingBsp()
 {
     tree = BspTree.Split(numberOfIterations, new RectInt(0, 0, dungeonSize, dungeonSize));
 }
 private void GenerateRoomsInsideContainers()
 {
     BspTree.GenerateRoomsInsideContainersNode(tree);
 }