Ejemplo n.º 1
0
 public Block(int Value, Rectangle Location)
 {
     val = Value;
     location = Location;
     player = 0;
     _change = false;
 }
Ejemplo n.º 2
0
        public bool Equals(Rectangle rect)
        {
            if (x == rect.x
                && y == rect.y
                && width == rect.width)
                return true;

            return false;
        }
Ejemplo n.º 3
0
        public Block getBlock(Rectangle coord)
        {
            Rectangle localCoord = new Rectangle(coord.x % tiles, coord.y % tiles, 1);
            Rectangle treePosition = new Rectangle(coord.x / tiles, coord.y / tiles, tiles);
            //Console.WriteLine("Searching tree X: {0}, Y: {1}", treePosition.x, treePosition.y);

            Trees.QuadTree.QuadTree<Block> tree = getTree(treePosition);
            return tree.Get(localCoord);
        }
Ejemplo n.º 4
0
        public Map(Models.TilesModel Model, int Tiles = 256, int TreeChaining = 1)
        {
            tiles = Tiles;
            treeChaining = TreeChaining;
            model = Model;

            space = new Rectangle(0, 0, tiles);
            Trees.QuadTree.QuadTree<Block>.model = model;
            root = Trees.QuadTree.QuadTree<Block>.getFreeTree(space, treeChaining);
            generator = new MapGenerator(tiles, tiles);
        }
Ejemplo n.º 5
0
        public bool Contains(Rectangle rect)
        {
            if ((x  <= rect.x )
                && (x + width >= rect.x + rect.width)
                && (y <= rect.y )
                && (y + width >= rect.y + rect.width))
            {
                return true;
            }

            return false;
        }
Ejemplo n.º 6
0
        public static void WorldDataTest(int tyles = 256, int treeChaining = 2)
        {
            MySqlConnection connection = new MySqlConnection("Database=cubeworld;DataSource=localhost;UserId=root;Password=root");
            Models.TilesModel model = new Models.TilesModel(connection, tyles);
            Map.Map map = new Map.Map(model, tyles, treeChaining);

            Map.Rectangle coord = new Map.Rectangle(0, 0, 1), coords = new Map.Rectangle(0, 0, 1);
            Map.Block blockA, blockB;
            Trees.QuadTree.QuadTree<Map.Block> tree;

            int totalBlocks = (int)Math.Pow(tyles, treeChaining + 1);

            int window = 8, tests = 0, errors = 0;

            Map.Rectangle positionA = new Map.Rectangle(19, 8, 0);
            Map.Rectangle positionB = new Map.Rectangle(25, 8, 0);

            Map.Rectangle spaceA = new Map.Rectangle(positionA.x - window, positionA.y - window, 2 * window);
            Map.Rectangle spaceB = new Map.Rectangle(positionB.x - window, positionB.y - window, 2 * window);

            List<Map.Block> partsA = map.getIntersect(spaceA);
            List<Map.Block> partsB = map.getIntersect(spaceB);

            foreach (Map.Block pA in partsA)
            {
                foreach (Map.Block pB in partsB)
                {
                    if (pA.location.x == pB.location.x
                        && pA.location.y == pB.location.y)
                    {
                        tests++;

                        if (pA.val != pB.val)
                        {
                            //Console.Write("| Chyba: x={0}, y={1}", pA.location.x, pB.location.y);
                            errors++;
                        }
                    }
                }
            }

            Console.WriteLine("Testování světa o šířce {0} dlaždic.", totalBlocks);

            if (tests != 0)
                Console.WriteLine("Výsledek testu: {0}% ({1} ok, {2} bad)", (100 * (tests - errors)) / tests, tests - errors, errors);
            else
                Console.WriteLine("Neproběhl žádný test!");
        }
Ejemplo n.º 7
0
        public void Generate()
        {
            _world = new Block[width, height];
            Random rand = new Random();
            int value;
            Rectangle location = new Rectangle(0, 0, 1);

            for (int x = 0; x < width; x++)
            {
                for (int y = 0; y < height; y++)
                {
                    location.x = x;
                    location.y = y;

                    value = rand.Next(1, 5);
                    _world[x, y] = new Block(value, location);
                }
            }

            _isGenerated = true;
        }
Ejemplo n.º 8
0
        public List<Block> getIntersect(Rectangle space)
        {
            List<Block> list = new List<Block>();
            List<Block> listTree = new List<Block>();

            Rectangle coords;
            Boolean added = false;
            Block treeBlock;

            for (int x = 0; x < 2; x++)
            {
                for (int y = 0; y < 2; y++)
                {
                    added = false;
                    coords = new Rectangle((x * space.width + space.x) / tiles, (y * space.width + space.y) / tiles, tiles);

                    foreach (Block i in listTree)
                    {
                        if (i.location.x == coords.x && i.location.y == coords.y)
                        {
                            added = true;
                        }
                    }

                    if (!added)
                    {
                        treeBlock = new Block(listTree.Count, coords);

                        treeBlock.tree = getTree(coords);

                        if (treeBlock.tree != null)
                            listTree.Add(treeBlock);
                    }
                }
            }

            /*Console.WriteLine("Space {0} {1} => {2} {3}", space.x, space.y, space.x + space.width, space.y + space.width);
            Console.WriteLine("In {0} tree.", listTree.Count());

            foreach (Block i in listTree)
            {
                Console.WriteLine("== tree {0} {1}", i.location.x, i.location.y);
            }*/

            Block block;
            int bx, by;
            foreach (Block i in listTree)
            {
                //Console.WriteLine("Saving tree {0} {1}", i.location.x, i.location.y);
                for (int x = 0; x < tiles; x++)
                {
                    for (int y = 0; y < tiles; y++)
                    {
                        if (space.Contains(new Rectangle(x + i.location.x * i.location.width, y + i.location.y * i.location.width, 1)))
                        {
                            block = i.tree.Get(new Rectangle(x, y, 1));

                            if (block != null)
                            {
                                bx = block.location.x + i.location.x * i.location.width;
                                by = block.location.y + i.location.y * i.location.width;

                                list.Add(new Block(block.val, new Rectangle(bx, by, 1)));
                            }
                        }
                    }
                }
            }

            return list;
        }
Ejemplo n.º 9
0
        protected void insetrBottomTreeBlock(Block block)
        {
            int newX = block.location.x, newY = block.location.y;
            int oldWidth, width, height = treeChaining;

            width = getActualNodeWidth(height);
            Trees.QuadTree.QuadTree<Block> node = root, parent;
            parent = node;
            Rectangle location = null;

            Block bottomBlock = null;

            while (height > 0)
            {
                parent = node;
                height--;

                oldWidth = width;
                width = getActualNodeWidth(height);

                location = new Rectangle(newX / width, newY / width, oldWidth);

                newX = newX % width;
                newY = newY % width;

                bottomBlock = node.Get(location);

                if (bottomBlock == null)
                {
                    bottomBlock = new Block(1, new Rectangle(location.x, location.y, oldWidth));
                    bottomBlock.tree = Trees.QuadTree.QuadTree<Block>.getFreeTree(location, height);
                    node.Insert(bottomBlock);
                }

                node = bottomBlock.tree;
            }

            if (parent != null)
            {
                block.location = location;
                block.location.width = tiles;

                parent.Insert(block);
            }
        }
Ejemplo n.º 10
0
        protected Block getBottomTreeBlock(Rectangle coords)
        {
            int newX = coords.x, newY = coords.y;
            int oldWidth, width, height = treeChaining;

            width = getActualNodeWidth(height);
            Trees.QuadTree.QuadTree<Block> node = root, parent = null;
            Rectangle location = null;

            Block bottomBlock = null;

            //Console.WriteLine("==");

            while (height > 0)
            {
                parent = node;
                height--;

                oldWidth = width;
                width = getActualNodeWidth(height);

                location = new Rectangle(newX / width, newY / width, oldWidth);
                //Console.WriteLine("MTree {0} {1} {2}", location.x, location.y, location.width);

                newX = newX % width;
                newY = newY % width;

                bottomBlock = node.Get(location);

                if (bottomBlock == null)
                {
                    bottomBlock = new Block(1, new Rectangle(location.x, location.y, oldWidth));
                    bottomBlock.tree = Trees.QuadTree.QuadTree<Block>.getFreeTree(location, height);
                    node.Insert(bottomBlock);
                }

                node = bottomBlock.tree;
            }

            return bottomBlock;
        }
Ejemplo n.º 11
0
        public Trees.QuadTree.QuadTree<Block> getTree(Rectangle coord)
        {
            //Console.WriteLine("Tree: {0} {1}", coord.x, coord.y);
            Block treeBlock = getBottomTreeBlock(coord);

            if (coord.x < 0
                || coord.y < 0
                ||coord.x >= Math.Pow(tiles, treeChaining - 1)
                || coord.y >= Math.Pow(tiles, treeChaining - 1))
            {
                return null;
            }

            getMutex.WaitOne();
            if (treeBlock == null || treeBlock.tree == null || treeBlock.tree.DumpCount() == 0)
            {
                Block block = new Block(0, coord);

                if (model.treeExist(coord.x * tiles, coord.y * tiles))
                {
                    //Console.WriteLine("Loading tree...");
                    treeBlock.tree = model.loadTree(new Rectangle(coord.x, coord.y, tiles));
                }
                else
                {
                    //Console.WriteLine("Creating tree...");
                    Rectangle baseCoords = new Rectangle(coord.x * tiles, coord.y * tiles, tiles);
                    block.tree = generate(tiles);

                    model.insertTree(block.tree, baseCoords);
                    treeBlock.tree = model.loadTree(new Rectangle(coord.x, coord.y, tiles));
                }
            }
            getMutex.ReleaseMutex();

            //Console.WriteLine("DUMP {0}", treeBlock.tree.DumpCount());
            return treeBlock.tree;
        }
Ejemplo n.º 12
0
        public Trees.QuadTree.QuadTree<Block> getIntersectTree(Rectangle coord)
        {
            if (coord.x < 0 || coord.y < 0)
                return null;

            Rectangle treeCoord = new Rectangle(coord.x / tiles, coord.y / tiles, tiles);
            return getTree(treeCoord);
        }
Ejemplo n.º 13
0
        public static void WorldSizeTest(int tyles = 256, int trees = 10)
        {
            Map.Rectangle coord = new Map.Rectangle(0, 0, 1);
            Map.Block block;

            long GC_MemStart = System.GC.GetTotalMemory(true);

            MySqlConnection connection = new MySqlConnection("Database=cubeworld;DataSource=localhost;UserId=root;Password=root");
            Map.Map map = new Map.Map(new Models.TilesModel(connection, tyles), tyles);

            for (int i = 0; i < trees; i++)
            {
                coord.x = tyles * i;
                block = map.getBlock(coord);
            }

            long GC_MemEnd = System.GC.GetTotalMemory(true);

            Console.WriteLine("{0} trees, {1}mb", trees, (GC_MemEnd - GC_MemStart) / 8000000);
        }
Ejemplo n.º 14
0
        public static void WorldTestBlock(int tyles = 16, int treeChaining = 2)
        {
            MySqlConnection connection = new MySqlConnection("Database=cubeworld;DataSource=localhost;UserId=root;Password=root");
            Models.TilesModel model = new Models.TilesModel(connection, tyles);
            Map.Map map = new Map.Map(model, tyles, treeChaining);

            Map.Rectangle coord = new Map.Rectangle(0, 0, 1), coords = new Map.Rectangle(0, 0, 1);
            Map.Block blockA, blockB;
            Trees.QuadTree.QuadTree<Map.Block> tree;

            int totalBlocks = (int)Math.Pow(tyles, treeChaining + 1) * (int)Math.Pow(tyles, treeChaining + 1);

            List<Map.Block> list, list2;
            int errors = 0, tests = 0;

            tree = map.getTree(new Map.Rectangle(0, 0, 16));

            return;

            for (int i = 1; i < 15; i++)
            {
                list = map.getIntersect(new Map.Rectangle(8, 0, 16));
                list2 = map.getIntersect(new Map.Rectangle(i, 0, 16));

                foreach (Map.Block b in list)
                {
                    foreach (Map.Block b2 in list2)
                    {
                        if (b.location.x == b2.location.x)
                        {
                            if (b.location.y == b2.location.y)
                            {
                                tests++;
                                if (b.val != b2.val)
                                {
                                    errors++;
                                }
                            }
                        }
                    }
                }

                //System.Threading.Thread.Sleep(1000);
            }

            Console.WriteLine("Hodnoceni počtu dlaždic: {0}% ({1} ok, {2} bad)", (100 * (tests - errors)) / tests, tests - errors, errors);
        }
Ejemplo n.º 15
0
        public static void WorldTest(int tyles = 16, int treeChaining = 2)
        {
            Stopwatch sw = new Stopwatch();
            sw.Start();

            MySqlConnection connection = new MySqlConnection("Database=cubeworld;DataSource=localhost;UserId=root;Password=root");
            Models.TilesModel model = new Models.TilesModel(connection, tyles);
            Map.Map map = new Map.Map(model, tyles, treeChaining);

            Map.Rectangle coord = new Map.Rectangle(0, 0, 1), coords = new Map.Rectangle(0, 0, 1);
            Map.Block blockA, blockB;
            Trees.QuadTree.QuadTree<Map.Block> tree;

            int totalBlocks = (int)Math.Pow(tyles, treeChaining + 1) * (int)Math.Pow(tyles, treeChaining + 1);

            /*
            for (int i = 0; i < 16; i++)
            {
                coord.x = tyles * i;
                block = map.getBlock(coord);
                Console.WriteLine("Block X: {0}, Y: {1}, Value: {2}.", block.location.x, block.location.y, block.val);
            }
             */

            int test = 0, failsTiles = 0, failsValues = 0, valuesTest = 0;
            for (int i = 0; i < 2; i++)
            {
                for (int x = 0; x < (int)Math.Pow(tyles, treeChaining); x += tyles)
                {
                    for (int y = 0; y < (int)Math.Pow(tyles, treeChaining); y += tyles)
                    {
                        test++;
                        //Console.WriteLine("{0}. iteration, Tree {1}, {2}", tyles * x + y, x, y);
                        coord.x = x;
                        coord.y = y;

                        tree = map.getTree(coord);
                        Trees.QuadTree.QuadTree<Map.Block> dbtree = model.loadTree(new Map.Rectangle(coord.x, coord.y, tyles));

                        for (int ix = 0; ix < tyles; ix++)
                        {
                            for (int iy = 0; iy < tyles; iy++)
                            {
                                valuesTest++;
                                coords.x = ix + coord.x * tyles;
                                coords.y = iy + coord.y * tyles;

                                //Console.WriteLine("Try find {0} {1} {2}", coords.x, coords.y, dbtree.DumpCount());

                                blockA = tree.Get(coords);
                                blockB = dbtree.Get(coords);

                                //Console.WriteLine("Found A:{0}, B:{1}", blockA != null, blockB != null);

                                if (blockA != null && blockB != null)
                                {
                                    if (blockA.val != blockB.val)
                                    {
                                        failsValues++;
                                    }
                                }
                                else if (blockA == null ^ blockB == null)
                                {
                                    failsValues++;
                                }

                                //System.Threading.Thread.Sleep(1000);
                            }
                        }

                        //Console.WriteLine("Test: {0} valid: {1}", tree.DumpCount(), tree.DumpCount() == tyles * tyles);

                        if (tree.DumpCount() != tyles * tyles)
                        {
                            //Console.WriteLine("Test: {0} valid: {1}; x {2} y {3}", tree.DumpCount(), tree.DumpCount() == tyles * tyles, x, y);
                            tree.Dump();
                            dbtree.Dump();
                            failsTiles += 1;
                            //Console.ReadKey();
                        }

                        Trees.QuadTree.QuadTree<Map.Block>.unsetAllTree();
                    }
                }
            }

            sw.Stop();

            mt.WaitOne();
            Console.WriteLine("Očekávaný počet mapových bloků {0}.", totalBlocks);
            Console.WriteLine("Hodnoceni počtu dlaždic: {0}% ({1} ok, {2} bad)", (100 * (test - failsTiles)) / test, test - failsTiles, failsTiles);
            Console.WriteLine("Hodnoceni hodnot bloků : {0}% ({1} ok, {2} bad)", (100 * (valuesTest - failsValues)) / valuesTest, valuesTest - failsValues, failsValues);
            Console.WriteLine("Celkem uloženo bloků {0}", Program.TotalInserted);
            Console.WriteLine("Test zabral {0} s", sw.ElapsedMilliseconds / 1000);
            mt.ReleaseMutex();
        }