A code-along is a story about coding in which you are encouraged to “code up” all the examples in the story. In a code-along you should not cut-and-paste.
This code-along continues from point where our previous code-alone left off.
In this code-along we will write a Bricklayer program that builds a tower. Our goal is to place this tower in the center of our “island surrounded by a lava-filled moat” which we developed in the previous code-along. This code-along shows how to use the following Bricklayer functions.
- The setPieceMapping function allows customization of Bricklayer’s translation between Minecraft blocks and LEGO bricks.
- The setBricklayerOrigin function allows the origin of Bricklayer’s virtual space to be moved away from the minecraftOrigin. This is done in relative terms using an offset.
- The showAsAir function causes Bricklayer to build an artifact exclusively out of Air blocks. This is useful for “erasing” an artifact.
- The setSuppressOrientationMarker functions allows program-level control over whether the orientation marker, which Bricklayer automatically creates, to be suppressed (i.e., turned off/on).
Step 1 – Use LDD to Develop a Prototype
The program below creates a tower whose shaft consists of Prismarine blocks and whose roof is made of SeaLantern blocks. Note that the tower shaft contains (1) a ring of Glowstone blocks for lighting purposes, (2) an OakDoor, and (3) a large window made of Glass blocks. On the floor of the tower is a ring of Torch blocks (except for in front of the door) and a beacon. The center of the roof is made of LightBlueGlass blocks. This lets the beam created by the beacon to pass through the roof.
A high-resolution rendering of the LDD rendition of tower created is shown next to the code. This high-resolution image is created using a tool called LDD to POV-Ray Converter. This converter creates wonderful high-resolution images. However, artifacts that contain transparent/clear bricks take extremely long to render (e.g., several days). If we want more reasonable rendering times, then the LDD rendition of the tower should not contain any clear bricks. Unfortunately (in this case), Bricklayer’s block-to-brick translation function maps Glass blocks to TRANSPARENT bricks and LightBlueGlass blocks to CLEARLIGHTBLUE bricks. In order to create our high-resolution image we need to change this mapping. Bricklayer provides a function, called setPieceMapping, that allows the mappings that Bricklayer uses to be changed. The setPieceMapping function takes a list as its argument. Each element in this list is a brick-block (or block-brick) pair. The following function call is made on the program shown below.
This instructs Bricklayer to map Glass blocks to BLUE bricks and LightBlueGlass blocks to LIGHTBLUE bricks. Through this mapping we are able to create an LDD artifact containing no clear bricks.
Aside. Aside from high-resolution rendering, when developing Minecraft prototypes (using LDD) the setPieceMapping can be extremely useful. Note that there are many more Minecraft blocks than there are LEGO bricks. This means that certain kinds of distinctions that are visually apparent in Minecraft will not be possible in LDD. For example, Bricklayer’s block-to-brick mapping function will map an OakSlab, a DoubleOakSlab, OakStairs, and an OakDoor to the MEDIUMNOUGAT brick. This is a significant loss of information. Consider constructing a house made of DoubleOakSlab blocks having an OakDoor. Using Bricklayer’s default block-to-brick mapping an LDD prototype will be created where the doors and walls cannot be distinguished from one another. In such a case, the setPieceMapping can be used to customize the block-to-brick mapping. For instance, an OakDoor might be mapped to the BROWN brick, which would then visually distinguish doors from walls in the house prototype.
Step 2 – Migrate Artifact to Minecraft
After developing an acceptable prototype we are ready to build our artifact in Minecraft. The following changes need to be made to the program shown in the previous step.
- Calls to the functions setSpawnPoint and setMinecraftOrigin are added to set the minecraftOrigin to the desired location.
- A call to the function setPlayerOffset is added to position the avatar at an appropriate distance from the tower.
- The output function is changed from show to showMC.
The screenshots below show the tower that is created in Minecraft. Note that the tower is improperly positioned, it needs to be place in the center of the island. The best way to do this is to shift the origin of Bricklayer’s virtual space. Step 3 shows how this can be done. However, first we must erase the tower that has been built. Bricklayer provides an output function, called showAsAir, that does just that. To erase our (mispositioned) tower all we need do is change the output function of the above program from showMC to showAsAir. When this modified program is executed, it will replace all the blocks in our artifact with Air blocks.
Step 3 – Final Form
The program shown below is the our final version. It uses the Bricklayer function setBricklayerOrigin to define the bricklayerOrigin as an offset from the minecraftOrigin. It also uses the function call, supressOrientationMarker true to stop Bricklayer from outputting its orientation marker.
The following screenshot shows the final version of our tower which is centered on our island surrounded by a moat of lava.
The inside of the tower is shown below. Note how the Glowstone ring and the Torch ring is used for lighting. Also note how the beacon shines up to the center of the roof. Recall that this part of the roof is constructed from LightBlueGlass which will allow the light from the beacon to shine through (in a blueish form) into the sky.
This last screenshot shows the tower by night. Note how the SeaLantern roof, Glowstone ring, and lava moat provide lighting. Also note the blueish light from the beacon shining into the night sky.