Block Merger

Home --> Programming Projects --> Block Engine --> Block Merger
Anyone who makes a Block Based game will at some point have a need for compressing thousands of blocks into a smaller amount of information. The program BlockMerger does just this. This program demonstates the same sort of algorithm as the Quad Algorithm, but it is somewhat simpler.

How To Use It

The python script block_merge.py reads block information from standard input and outputs information to standard output. The program can be run as follows:
block_merger.py < input.txt > output.txt
Both input.txt and output.txt are text files. Suppose the content of input.txt is as follows:
0 0 0 "grass"
1 0 0 "stone"
1 12 0 "wood"
0 1 0 "grass"
The file format is as follows: each line consists of 3 integers: "x y z" followed by a string (in quotes) describing the type of the block. Hence, the file input.txt is claiming that there is a block at position (0,0,0) in the world of type "grass", a block at (1,0,0) of type "stone", a block at (1,12,0) of type "wood", and a block at (0,1,0) of type "grass". Notice that there are two grass blocks that are next to each other, so we can merge them together to form a "box" of blocks.

Adjacent blocks of the same type can be "merger together", but blocks of different textures cannot. After running the program, the contents of the file output.txt should be as follows:
1 0 0 1 0 0 stone
1 12 0 1 12 0 wood
0 0 0 0 1 0 grass
Each line of the file output.txt is of the form "x_min y_min z_min x_max y_max z_max t". Each line is expressing the fact that there is a three-dimensional array of blocks (a box) in the world with a particular block type. The bounds are inclusive. For instance, the last line of output.txt is saying that there is a grass block at both position (0,0,0) and (0,1,0).


How It Works

The algorithm works very similar to the Quad Algorithm but with "boxes" in 3D-space instead of "quads" in 2D-space. That is, blocks are added one at a time, and block rectangles are merged together greedily.

The Python and the C++ Version

Here is the Python version of the program. Just extract the zip file and run the script block_merge.py as desired.

Here is the C++ version of the program. This version uses an integer to represent the block type instead of a string. The executable is not included. To compile the program, simply extract the zip file, cd into that directory, and run the command "make". The Makefile will then call g++ to compile the c++ program bc.exe.