示例#1
0
        public void TestLeaf()
        {
            using (TCODBSP bspSized = new TCODBSP(0, 0, 10, 10))
            {
                bspSized.SplitOnce(false, 6);

                Assert.IsFalse(bspSized.IsLeaf());
                Assert.IsTrue(bspSized.GetLeft().IsLeaf());
                Assert.IsTrue(bspSized.GetRight().IsLeaf());
            }
        }
示例#2
0
 public void IsMapCellInsideNodeTest()
 {
     using (TCODBSP bspSized = new TCODBSP(0, 0, 10, 10))
     {
         Assert.IsTrue(bspSized.IsMapCellInsideNode(0, 0));
         Assert.IsFalse(bspSized.IsMapCellInsideNode(10, 10));
         bspSized.SplitOnce(false, 6);
         Assert.IsTrue(bspSized.GetLeft().IsMapCellInsideNode(1, 1));
         Assert.IsFalse(bspSized.GetRight().IsMapCellInsideNode(1, 1));
     }
 }
示例#3
0
        public void FatherTest()
        {
            using (TCODBSP bspSized = new TCODBSP(0, 0, 10, 10))
            {
                bspSized.SplitOnce(false, 6);

                TCODBSP father1 = bspSized.GetLeft().GetFather();
                TCODBSP father2 = bspSized.GetRight().GetFather();
                Assert.IsTrue(bspSized == father1 && father1 == father2 && bspSized == father2);
            }
        }
示例#4
0
 public void FindNodeTest()
 {
     using (TCODBSP bspSized = new TCODBSP(0, 0, 10, 10))
     {
         bspSized.SplitOnce(false, 6);
         TCODBSP nullNode = bspSized.FindNode(10, 9);
         Assert.IsNull(nullNode);
         TCODBSP foundNode = bspSized.FindNode(1, 1);
         Assert.IsNotNull(foundNode);
         Assert.IsTrue(foundNode.w == 6 && foundNode.h == 10);
     }
 }
示例#5
0
        public void LeftRightTest()
        {
            using (TCODBSP bspSized = new TCODBSP(0, 0, 10, 10))
            {
                bspSized.SplitOnce(false, 6);

                TCODBSP left = bspSized.GetLeft();
                Assert.AreEqual(left.w, 6);
                Assert.AreEqual(left.level, 1);

                TCODBSP right = bspSized.GetRight();
                Assert.AreEqual(right.w, 4);
                Assert.AreEqual(right.level, 1);
            }
        }
示例#6
0
 public void TestRemoveKids()
 {
     using (TCODBSP bspSized = new TCODBSP(0, 0, 10, 10))
     {
         bspSized.SplitOnce(false, 6);
         Assert.IsNotNull(bspSized.GetLeft());
         Assert.IsNotNull(bspSized.GetRight());
         bspSized.RemoveSons();
         Assert.IsNull(bspSized.GetLeft());
         Assert.IsNull(bspSized.GetRight());
     }
 }
示例#7
0
 bool TCODBSPTraversal(TCODBSP bsp)
 {
     passes++;
     return true;
 }
示例#8
0
        public void TestWalking()
        {
            TCODBSP bspSized = new TCODBSP(0, 0, 10, 10);
            bspSized.SplitOnce(false, 5);
            bspSized.GetLeft().SplitOnce(true, 5);
            bspSized.GetRight().SplitOnce(true, 5);

            passes = 0;
            bspSized.TraversePreOrder(new TCODBSPTraversalDelegate(this.TCODBSPTraversal));
            Assert.AreEqual(7, passes);

            passes = 0;
            bspSized.TraverseInOrder(new TCODBSPTraversalDelegate(this.TCODBSPTraversal));
            Assert.AreEqual(7, passes);

            passes = 0;
            bspSized.TraverseInvertedOrder(new TCODBSPTraversalDelegate(this.TCODBSPTraversal));
            Assert.AreEqual(7, passes);

            passes = 0;
            bspSized.TraverseLevelOrder(new TCODBSPTraversalDelegate(this.TCODBSPTraversal));
            Assert.AreEqual(7, passes);

            passes = 0;
            bspSized.TraversePostOrder(new TCODBSPTraversalDelegate(this.TCODBSPTraversal));
            Assert.AreEqual(7, passes);
        }
示例#9
0
 public void TestResize()
 {
     using (TCODBSP origSized = new TCODBSP(0, 0, 10, 10))
     {
         using(TCODBSP newSized = new TCODBSP(0, 0, 20, 25))
         {
             Assert.IsFalse(origSized == newSized);
             origSized.Resize(0, 0, 20, 25);
             Assert.IsTrue(origSized == newSized);
         }
     }
 }
示例#10
0
        void render_bsp(bool first, KeyPress key)
        {
            int x, y;

            if (generate || refresh)
            {
                // dungeon generation
                if (bsp == null)
                {
                    // create the bsp
                    bsp = new TCODBSP(0, 0, SAMPLE_SCREEN_WIDTH, SAMPLE_SCREEN_HEIGHT);
                }
                else
                {
                    // restore the nodes size
                    bsp.Resize(0, 0, SAMPLE_SCREEN_WIDTH, SAMPLE_SCREEN_HEIGHT);
                }

                for (int x1 = 0; x1 < SAMPLE_SCREEN_WIDTH; x1++)
                    for (int y1 = 0; y1 < SAMPLE_SCREEN_HEIGHT; y1++)
                        bsp_map[x1, y1] = '#';

                if (generate)
                {
                    // build a new random bsp tree
                    bsp.RemoveSons();
                    bsp.SplitRecursive(null, bspDepth, minRoomSize + (roomWalls ? 1 : 0), minRoomSize + (roomWalls ? 1 : 0), 1.5f, 1.5f);
                }
                // create the dungeon from the bsp
                bsp.TraverseInvertedOrder(new TCODBSPTraversalDelegate(traverse_node));
                generate = false;
                refresh = false;
            }

            sampleConsole.Clear();
            sampleConsole.ForegroundColor = (ColorPresets.White);
            sampleConsole.PrintLine("ENTER : rebuild bsp\nSPACE : rebuild dungeon\n+-: bsp depth " + bspDepth + "\n*/: room size " + minRoomSize + "\n1 : random room size " + (randomRoom ? "ON" : "OFF"), 1, 1, Background.None, LineAlignment.Left);

            if (randomRoom)
                sampleConsole.PrintLine("2 : room walls " + (roomWalls ? "ON" : "OFF"), 1, 6, Background.None, LineAlignment.Left);

            // render the level
            for (y = 0; y < SAMPLE_SCREEN_HEIGHT; y++)
            {
                for (x = 0; x < SAMPLE_SCREEN_WIDTH; x++)
                {
                    bool wall = (bsp_map[x,y] == '#');
                    sampleConsole.SetCharBackground(x, y, (wall ? darkWall : darkGround), Background.Set);
                }
            }

            if (key.KeyCode == KeyCode.TCODK_ENTER || key.KeyCode == KeyCode.TCODK_KPENTER)
            {
                generate = true;
            }
            else if (key.Character == ' ')
            {
                refresh = true;
            }
            else if (key.Character == '+')
            {
                bspDepth++;
                generate = true;
            }
            else if (key.Character == '-' && bspDepth > 1)
            {
                bspDepth--;
                generate = true;
            }
            else if (key.Character == '*')
            {
                minRoomSize++;
                generate = true;
            }
            else if (key.Character == '/' && minRoomSize > 2)
            {
                minRoomSize--;
                generate = true;
            }
            else if (key.Character == '1' || key.KeyCode == KeyCode.TCODK_1 || key.KeyCode == KeyCode.TCODK_KP1)
            {
                randomRoom = !randomRoom;
                if (!randomRoom) roomWalls = true;
                refresh = true;
            }
            else if (key.Character == '2' || key.KeyCode == KeyCode.TCODK_2 || key.KeyCode == KeyCode.TCODK_KP2)
            {
                roomWalls = !roomWalls;
                refresh = true;
            }
        }
示例#11
0
        // the class building the dungeon from the bsp nodes
        bool traverse_node(TCODBSP node)
        {
            TCODRandom rnd = new TCODRandom();

            if (node.IsLeaf())
            {
                // calculate the room size
                int minx = node.x + 1;
                int maxx = node.x + node.w - 1;
                int miny = node.y + 1;
                int maxy = node.y + node.h - 1;
                int x, y;
                if (!roomWalls)
                {
                    if (minx > 1) minx--;
                    if (miny > 1) miny--;
                }
                if (maxx == SAMPLE_SCREEN_WIDTH - 1) maxx--;
                if (maxy == SAMPLE_SCREEN_HEIGHT - 1) maxy--;
                if (randomRoom)
                {
                    minx = rnd.GetRandomInt(minx, maxx - minRoomSize + 1);
                    miny = rnd.GetRandomInt(miny, maxy - minRoomSize + 1);
                    maxx = rnd.GetRandomInt(minx + minRoomSize - 1, maxx);
                    maxy = rnd.GetRandomInt(miny + minRoomSize - 1, maxy);
                }
                // resize the node to fit the room
                //	printf("node %dx%d %dx%d => room %dx%d %dx%d\n",node->x,node->y,node->w,node->h,minx,miny,maxx-minx+1,maxy-miny+1);
                node.x = minx;
                node.y = miny;
                node.w = maxx - minx + 1;
                node.h = maxy - miny + 1;
                // dig the room
                for (x = minx; x <= maxx; x++)
                {
                    for (y = miny; y <= maxy; y++)
                    {
                        bsp_map[x,y] = ' ';
                    }
                }
            }
            else
            {
                //	printf("lvl %d %dx%d %dx%d\n",node->level, node->x,node->y,node->w,node->h);
                // resize the node to fit its sons
                TCODBSP left = node.GetLeft();
                TCODBSP right = node.GetRight();

                node.x = System.Math.Min(left.x, right.x);
                node.y = System.Math.Min(left.y, right.y);
                node.w = System.Math.Max(left.x + left.w, right.x + right.w) - node.x;
                node.h = System.Math.Max(left.y + left.h, right.y + right.h) - node.y;
                // create a corridor between the two lower nodes
                if (node.horizontal)
                {
                    // vertical corridor
                    if (left.x + left.w - 1 < right.x || right.x + right.w - 1 < left.x)
                    {
                        // no overlapping zone. we need a Z shaped corridor
                        int x1 = rnd.GetRandomInt(left.x, left.x + left.w - 1);
                        int x2 = rnd.GetRandomInt(right.x, right.x + right.w - 1);
                        int y = rnd.GetRandomInt(left.y + left.h, right.y);
                        vline_up(bsp_map, x1, y - 1);
                        hline(bsp_map, x1, y, x2);
                        vline_down(bsp_map, x2, y + 1);
                    }
                    else
                    {
                        // straight vertical corridor
                        int minx = System.Math.Max(left.x, right.x);
                        int maxx = System.Math.Min(left.x + left.w - 1, right.x + right.w - 1);
                        int x = rnd.GetRandomInt(minx, maxx);
                        vline_down(bsp_map, x, right.y);
                        vline_up(bsp_map, x, right.y - 1);
                    }
                }
                else
                {
                    // horizontal corridor
                    if (left.y + left.h - 1 < right.y || right.y + right.h - 1 < left.y)
                    {
                        // no overlapping zone. we need a Z shaped corridor
                        int y1 = rnd.GetRandomInt(left.y, left.y + left.h - 1);
                        int y2 = rnd.GetRandomInt(right.y, right.y + right.h - 1);
                        int x = rnd.GetRandomInt(left.x + left.w, right.x);
                        hline_left(bsp_map, x - 1, y1);
                        vline(bsp_map, x, y1, y2);
                        hline_right(bsp_map, x + 1, y2);
                    }
                    else
                    {
                        // straight horizontal corridor
                        int miny = System.Math.Max(left.y, right.y);
                        int maxy = System.Math.Min(left.y + left.h - 1, right.y + right.h - 1);
                        int y = rnd.GetRandomInt(miny, maxy);
                        hline_left(bsp_map, right.x - 1, y);
                        hline_right(bsp_map, right.x, y);
                    }
                }
            }
            return true;
        }