Faster FISH Print in MakeCode for Minecraft Aquarium

 

FISH

This post describes a change to Aquarium in MakeCode for Minecraft that was suggested by a YouTube viewer..

The challenge in the design of the aquarium was to  print  “FISH” on the front of the aquarium by using the Minecraft Print block. The spaces within each character are to be filled with cyan glass blocks, not air (the Minecraft default). Filling with cyan glass blocks  makes the print appear to be embedded in the wall.

The Code

The helpful viewer suggested that I use the Minecraft Replace block instead of the much slower and more verbose nested loops, which test each position in each character for air and replace it with glass.

LOOPS

A loop within a loop that tests each position in a 5×5 character for air. If an air block  is found, it is replaced with a cyan glass block.

replace block code

A single Replace block replaces any air blocks in a 5×5 character with a cyan glass block.

The screenshot below shows the entire Aquarium code with the new print routines.

code all.png

Aquarium code with new print routines.

One print_n routine is required for each character in the text to be printed to enable the four prints to run at the same time rather than each character waiting for the preceding character to print. This was described in the original post Aquarium in MakeCode for Minecraft. The four print routines for “FISH” are shown below. The only differences in the four routines are:

  1. The character to print and
  2. The position in the wall.

The character to print is selected from the string print_text at the position print_num1, which is an argument to the routine. For example, print_2 is passed the argument print_num1 = 2, which selects the string “S” from the string “FISH”.

The position in the wall at which to print is print_num1 position from the array print_pos. For example, for print_num1 = 2, the position chosen from the array is the position for “S”.

prints.png

The four new print routines.

These routines would be a great deal simpler if arguments to an on chat command could be of type string or of type position instead of just a number. This not being so, dictated that the routines use the number argument print_num1 to select the string to print and the position at which to print.

one print

This code assumes a flat world such as that described in MakeCode for Minecraft Sandbox World: Make It Flat and Simple.

Faster, Smaller and Easier to Understand

What’s not to like? The Minecraft Replace block does exactly what the nested loops (above) did. The Replace is much faster, as demonstrated in the video (above) . Further, the Replace is less verbose; that is, is fewer lines of code. The Replace is also much easier to understand.

Why Didn’t I Think of That? Wasn’t It Obvious?

It was obvious to my viewer. But,  I am only a few months into learning MakeCode for Minecraft. I have not used every block in the MakeCode language. One can argue that that was a mistake. I, as usual, was so impatient to start creating programs that I did not take the time  to methodically learn every MakeCode block before I started. I prefer Just-in-Time learning, which I am sorry to say that along with one of my life’s themes “not knowing what I don’t know” results in projects that can be improved. Don’t expect me to change. I like improving my projects and I love learning  whenever it is done. So bring it on — every constructive criticism  is welcome.

Get the Code

Aquarium with the improvements described here is shared on Code Connection at this URL

https://makecode.com/_YALWi6RbUXe2

To get and use the code, follow these steps.

Click the Import button import button , which is upper right in the Code Connection window just below the banner. This will open the window shown below.
Import choices

Click the Import URL button Import URL, which is on the right, to open the window shown below.

Import Copy link

Paste the URL supplied for the program you want to use in the space under the text “Copy the URL …”
import url with url

Click the Go ahead! button go ahead button.

The next window you will see will be the MakeCode window with the code downloaded from the URL. At this point, you can treat it like any other code, e.g., run it, save it locally, modify it, publish your changes or whatever else your heat desires.

We have tested several other methods of downloading the code using the URL, for example, pasting the URL in a browser. No joy. For more detailed instruction see our post How to Use Shared MakeCode on Microsoft Code Connection for Minecraft.

Aquarium in MakeCode for Minecraft

 

main

Aquarium Front. Pool of dolphins on left. Watch them frolic in the  fountain. Salmon in the right pool. Slime seem to be attracted to the salmon.

glass view from door

The main fish tank as seen from the entry.  All kinds of Minecraft fish are included with a bit of kelp and coral, also.

at glass

A look through the glass of the main fish tank.

inside top view

A view from the top of the main fish tank. The green things are kelp.

inside tank

From inside the main fish tank.

from inside

Aquarium interior

slime in pool 2

Slime seems to be attracted to the salmon tank.

The Design

When viewing the final aquarium from above, one can see the concentric squares of the main building, the steps up to the aquarium, the fish tank and the glass square at the center of the tank. The porch on the aquarium features a left pool with a fountain and a right pool.

building from above

I started the design of the aquarium with the drawing below. Note that the concentric squares of the main building, as well as the porch with pools, correspond to structures in the final aquarium. Each square is defined by its lower-left corner and the number of blocks in a side of  square.

plan

Because the lower-left corner position of each structure is used as the reference position, each structure is built to the east and south of the corner; therefore, all coordinates X and Z positive.

Space and Time Challenges

I encountered several challenges in coding this program. I wanted to type run and a few seconds later view my aquarium.

I wanted to print  “FISH” on the building in a manner that the characters are embedded in the outer blocks, not in front of the blocks. Printing “FISH” with one print block is unbearably slow. Further, the code to embed the characters at least doubles the print time. I don’t have the patience for it.

print embedded

I solved the slow-print problem by printing each of the four characters in “FISH” asynchronously. The code is described later in this post. This approach required greatly increasing the number of code blocks  making the program so large that if I ever viewed it in JavaScript, MakeCode would not convert it back to blocks, so I gave up JavaScript.

The program was still too slow for me. Again, I used asynchronous code to nearly halve the time by running the code that builds the porch at the same time as the main building is built. This choice only increased the code by one on-chat block.

The Code

This code assumes a flat world such as that described in MakeCode for Minecraft Sandbox World: Make It Flat and Simple.

All the code for Aquarium is shown in the graphic below. Upper left t is the main on-chat command “run.” Below it is the function that sets the dimensions, such as, the size of each square, that define the structures. Below that is the function that calculates the corners as shown in the drawing above. From left to right, is the code for the outer building, the tank and fish, the porch with pools and, lastly, the fast print. Notice how much code is required for the fast print, which caused the program to be fast but larger than I would like.

ALL CODE LABELED

This program could have required much more code to build such a complex structure. The clever, if I do say so myself, use of 25 fill blocks enabled it to be amazingly compact. The use of the fill-block hollow option was particularly useful.

run on-chat command

in this on-chat command block, which starts the program, notice the use of the two run-chat-command blocks instead of functions. Had functions been used, the function would have to complete before the following block would be executed. By implementing it as shown, the code for porch and print_fish runs asynchronously with the functions that follow. You can think of the run-chat-command blocks as launching independent programs then immediately to run the code that follows.

run code

Beware of Common Variables in Asynchronous Code

When using asynchronous code, the variables used in two code sections that run at the same time, must not share variables except for those that are read only.

Set Dimensions and Define Corner Positions of Structures

define dims

define corners

Build Outer Building and Foundation

outer building code

do outer door

do foundation code

Build Tank and Populate with Fish

do tank code

do tank interior code

AQUA THINGS CODE

Build Porch

pool code
porch entry code

Build Two Porch Pools and Populate with Dolphins and Salmon

TWO POOLS CODE

pool code

Fast Print “FISH”

The key to print speed is to print each character with a separate print on-chat command.  In the case of “FISH,” which has four  characters, the on-chat commands are named print_0, print_1, print_2 and print_3. The only difference in code for the four commands is the name. All the interior code is identical in the four functions. The code following the print block fills in the air spaces left by the character within the area where the character was printed. A for-index block would have been neater; however, the index in such a loop cannot be an element of an array. Of course, I tried it.

print fish one letter

Before you ask, of course, the four functions can be collapsed to one; but, in that case, the commands will not run asynchronously and no time is saved.

print fish several letters

The code below is the main code for the fast print. It launches each of the four print commands after initializing the appropriate element of the three arrays print_pos, print_i and print_j. Each on-chat command using a different element of the three arrays. This was necessary to avoid having the asynchronous routines from changing the same variables. This could have been done by  naming the variables differently for each on-chat command; however, that would have  made the interior codes different.

print fish main

I plan a future post that will further explore fast printing.

Get the Code

Aquarium code is shared on Code Connection at this URL

https://makecode.com/_0kDWR4UsX9KP

To get and use the code, follow these steps.

Click the Import button import button , which is upper right in the Code Connection window just below the banner. This will open the window shown below.
Import choices

Click the Import URL button Import URL, which is on the right, to open the window shown below.

Import Copy link

Paste the URL supplied for the program you want to use in the space under the text Copy the URL …”
import url with url

Click the Go ahead! button go ahead button.

The next window you will see will be the MakeCode window with the code downloaded from the URL. At this point, you can treat it like any other code, e.g., run it, save it locally, modify it, publish your changes or whatever else your heat desires.

We have tested several other methods of downloading the code using the URL, for example, pasting the URL in a browser. No joy. For more detailed instruction see our post How to Use Shared MakeCode on Microsoft Code Connection for Minecraft.