The Art of Random Cityscapes in MakeCode for Minecraft

 

This MakeCode for Minecraft program is called “Cityscape.” Zillions of cityscapes can be generated by customizing input parameters. A few cityscapes generated by t he program are shown in this blog post.
tall city

ice city.png

dark down

Idea

I had the idea for generating random cityscapes as I was developing The Art of Random Walks in MakeCode for Minecraft.  Essentially, the idea was that instead of placing blocks at each random step, the program would place a building built with a random block with random height, width and depth (depth here means how far back). The program would then move to another  location a random distance from the prior building. It would then place another building, and so on, until the specified number of buildings were placed. The random selections would all be within player specified limits. Optionally, the program would place “ground” of a player-specified block within a rectangle around the city.

city 200 bldg

Customizing Cityscape

Cityscape’s has many options. Such flexibility is marvelous for an experienced player, but can be confusing for a first timer. This is a challenge frequently faced  by software designers. In this case, the solution was to have a default for every option so that a player need only type “city” in the command window to generate a beautiful, probably unique, cityscape. Typing “city” again, generates another city that likely will appear very different from the first, and so on, until the player decides that something a little different would be nice — time to start exploring the options.

Cityscape’s options are specified as follows:

  1. As the values of the parameters in the functions “default_params” and “custom_params.”
  2. As the three arguments to the on-chat “city” command, which starts the program.
  3. As the blocks specified in the block arrays.

Default and Custom Parameter Functions.

Both the default and custom parameter values, which are set in their respective functions,  can be changed.  I suggest having the values in “default_params” be the ones you use most often and the params in “custom_params” be experimental. I also save sets of favorite values by copying them to a scratch area of the program so that I can drag them in and out of the parameter functions.

Cityscape parameters include the following (in the order shown in the param functions):

  • the number of buildings
  • the block array to be used for buildings
  • whether or not to do a compass
  • the ground block to be used in the rectangle surrounding the city
  • the block to be used under the ground block. This is useful if a glass block is used for the ground.
  • foundation block to be used under each building. This is replaced if a new ground is laid.
  • whether a random block is to be used for the ground
  • whether all buildings are to be cubes
  • lower and upper limits for building height
  • lower and upper limits for building width
  • lower and upper limits for building depth (how far back)
  • the maximum distance from one building to another

parms code.png

In the left cityscape below,  all dimensions are set to 1 with a move range of 3. This cityscape illustrates the random walk from the origin, which is indicated by the clear glass block above the lower right TNT block. The right cityscape below has a move range of 0, which means all buildings, which have varying dimensions,  are  built in the same place.

A Function to Verify Parameters

The function “verify_params” checks the parameters for correct values. If one is wrong, for example, a negative value for width, the program will say a message and stop. The last verification in the routine is to check whether the fill command volume limit of 32,768 will be exceeded for any building. This is done by calculating the largest possible volume,  which is calculated as the product of the three variables height limit, width limit  and depth limit.

verify code

color sym city

Blocks from Which Buildings Are Constructed

Building blocks are  specified by the variable “which_array” in the parameter routines above. The values can be 1, 2, 3 or 4. This value can be overridden by an  on-chat “city” command argument. The values 1, 2, 3 or 4 specify, in order, one of the following arrays:

  1. jewel_array,
  2. black_array,
  3. other_array and
  4. an array constructed from all three of the above arrays.

Each of these arrays can have any number of blocks and can contain any type of Minecraft block from which a building can be constructed. The names of the arrays cannot be changed unless they are also changed in the code that reference the arrays. The variable “this_array” is set to the array that is selected by the “which_array” parameter or argument. For building construction, blocks are randomly selected from “this_array.”

array code

on-chat “city” Command Arguments

The on-chat “city” command (see below)  has three arguments.

city args

If num1 is 0, default_params are used. If 1, custom_params are used. A valid num2 overrides the number of buildings provided in default or custom parameters. A valid num3 overrides the array of blocks to be used.

blue city

Using Cityscape

The on-chat “city” command is the main command. It generates a cityscape according to the previously set parameters and arrays, which may have been overridden by the command’s arguments (see above).

The on-chat “view” command moves through views of the city from 20 different vantage points, including 4 sides, 4 corners, over the origin and over center of rectangle. For each of these  2 different heights are used. The on-chat command “s” will stop movement at the current view. The argument num1 to view is the number of seconds to pause at each view, default 3.

The Code

The left third of Cityscape’s code has to do with the inputs that tailor the program to generate an near-endless variety of cityscapes. The middle third, which is launched with on-chat “city” command,  is the guts of the program and actually constructs the city building by building within the bounds of the input parameters. The right third of the program is primarily that launched by the “view” command, which assists the player in looking at the city from various vantage points.

All Code

on-chat Command “city” Starts the Program

The on-chat “city” command defines the main flow of the program in the following steps:

  1. Processes arguments to
    • choose between default and custom parameters,
    • optionally, override number of buildings and
    • optionally, override the array of blocks used in building construction.
  2. Initializes variables, sets origin and, optionally, launches an on-chat to draw a compass.
  3. Uses a repeat block for the following:
    • calls “do_one_building” function to construct a building and
    • calls “get_new_rposition” to pick random position for next building.
  4. Adjusts height of origin marker to be taller than highest building and moves player to above origin.
  5. Calls “build_ground” to create rectangle at ground level.

city code

Function to Do One Building

The “do_one_building” function does the following:

  1. picks random height, width and depth.
  2. calls function to pick block to be used.
  3. uses fill block to create hollow building.
  4. uses fill block to lay foundation of building.
  5. calls function to update minimum and maximum X and Z.

do one bldg code

Function to Get New Random Position

The position of the next building is selected as a random position between the current building’s position plus {minus the move range, 0, minus the move range} and the current building’s position plus {move range, 0, move range}. Simply put, the new building can be any direction up to a distance of  move range from the current position.

get new position code

Function to Pick the Block for Building Construction

The “pick_block” function chooses a random block from the “this_array” array. It also call the function “give_flint_if_needed” to give the player a flint-and-stone item if the block is TNT.

pick block code

Function to Give Player Flint and Steel Item If Block Is TNT

This function gives the player a flint-and-stone item if “this_block” is TNT and the item had not been previously given in this run of “city.” This function is called when a new building block is chosen and also when the foundation block is selected.

give flint

blown up

Functions to Initialize and Finalize Origin Marker and Player Position

Origin marker is a tower of clear glass blocks that is a bit higher than the tallest building. It marks the place of the first building. I use it to view the direction that the random sequence of buildings developed. Initially, the marker is at the maximum height that a building can be. Finally, it is adjusted to be just above the actual highest building. The on-chat “no” command erases the origin marker. This is useful for those situations where the origin marker detracts from the view of the city.

origin marker

Functions to Keep Track of Minimum and Maximum X and Z coordinates

Maximum and minimum X and Z coordinates are updated with each building so that, in the end, the ground can be placed under the city with the same distances from buildings on all sides. Most of the view points are on the perimeter of the rectangle surrounding the city.

min max XZ code

Function to Do Ground Rectangle

This function uses the minimum and maximum X and Z coordinates to define a rectangle. Within that rectangle, grass is replaced by a block, which is either a random block or a block supplied as a parameter in either default or custom parameters functions. An underground block is also placed below the ground. This is useful when the ground is transparent.

do ground code

on-chat “view” Command Helps the Player in Appreciating Her Handiwork

After a cityscape is generated, the player may want to look at it from various vantage points because the view can be very different from various directions. Moving around a city can be tedious; therefore, automation to the rescue. The on-chat command “view” moves the player through 20 positions, pausing to allow time to view each one. The number of seconds to pause is specified to num1, which defaults to 3 seconds. If a particular view is spectacular, an “s” on-chat command stops movement to the next view.

view code

Function to Calculate the Positions for Viewing a Cityscape

The ground plane positions from with views are provided are shown by the drawing below. Minimum and maximum X and Z are used to do the calculation in the function that initializes the view positions array.

drawing directions

calc view positions

Note that, as far as I know,  arrays of positions are an documented feature of MakeCode for Minecraft.

Other Code Used by Cityscape

Two other pieces of code are used by Cityscape:

  • compass and
  • a function to convert a number on-chat argument  to a Boolean array.

This version of compass is particularly fast and flexible. It will be described in my next block post.

compass

This handy little function was motivated by the fact I needed to use several Booleans arguments to an on-chat command. It will be documented in a soon-to-come blog post.

bits

flat world

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

up black city.png

Get the Code

Cityscape code is shared on Code Connection at this URL

https://makecode.com/_MXigqm5HTdev

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.

gray city.png

red on black down

black on cyan city

down-pink.png

down green city.png

down city.png

This slideshow requires JavaScript.

Creating Symmetric Patterns Using MakeCode for Minecraft

 

red white and blue 50 vista

We created this symmetric red, white and blue 50-block square pattern  in appreciation for U.S. Independence day. The left and right halves are mirror images as are the upper and lower halves.

red white and blue border rotated 50

For July 4th, we couldn’t resist making a red, white and blue pattern including one extra block TNT for a big bang (see YouTube video above).

Symmetric patterns are beautiful, fascinating and endlessly entertaining. Amazon lists 4,000 books on symmetry.  Wikipedia explores symmetry in mathematics,  biology, art, physics, chemistry, social interactions, architecture, poetry, quilts, rugs, physical attractiveness and music among other areas of human endeavor.

We created the MakeCode for Minecraft program described in this post just for the fun of it.  With the program, symmetric squares of any size (within reason) and using any number of Minecraft blocks can be created. The program user picks the Minecraft blocks to be used for the canvas, for the pattern and for the optional  border. The program first builds the background canvas for either one or two levels, then builds the pattern using the number of blocks specified by the user. Optionally, a border can be added.

We are amazed that such a simple program can create such beautiful patterns. Gazillions of combinations are possible, some more pleasing than others. Make your own.

The Plan for the Symmetric Patterns Program

The plan for this symmetric patterns program is that it enable a user  to build a random, 4-way symmetric pattern in the following steps:

  1. Specify Minecraft blocks in three lists; namely, canvasBlocks, patternBlocks and borderBlocks.
  2. Use the on-chat command “canvas” to place one or two layers of blocks from the canvasBlocks list.
  3. Use the on-chat command “pattern” to create a random, symmetric  pattern with blocks from the patternBlocks list.
  4. The on-chat command “border” builds a simple border around the pattern using the first block in the borderBlocks list.

Details of the on-chat commands’ arguments are provided below in the description of the code. This table is a summary and handy when using the program.

The MakeCode for Minecraft Code

The on-start Block

The lists canvasBlocks, patternBlocks and borderBlocks are inputs to the program; that is, they can be changed to create different patterns.

symmetry code 1

Any number of blocks can be included in each list, but only the number specified in the on-chat commands will be used. I sometimes include more blocks than are used so that changing blocks can be done quickly with drag-and-drop. Because now the border can be only one block wide, only the first block is used; however, since I might add additional borders, it is specified as a list rather than a simple one-block variable.

Canvas on-chat Command

The chat command arguments specify the size of the canvas and the number layers to be build. The top layer is even with the grass in a flat world. An optional underlayer can be specified, which is useful when glass blocks are the top layer.

The canvas on-chat command is implemented in three steps:

  1. The two arguments are verified and, if out of range, set to default values.
  2. The variable “center” is set to the player world position, but Y (the up/down coordinate) is set to the same as the grass. This variable is used as the reference point for building the canvas.
  3. The canvas is built around center one  layer at a time in a loop. Each layer is built with a single fill block.

The argument num1 is the number of layers to build. num1 can have values 1 or 2, but also must not be greater than the number of blocks in the canvasBlocs list. If canvasBlocks contains no blocks, a white concrete block is added as the default.

The argument num2 is the size of the canvas specified as half of the final size. for example, if num2 is 80, the canvas will be 160 blocks square. Some limit is required because of the fill block limit volume is 32,768 blocks.

symmetry code 2A

Canvas Code — Part 1 of 2 — Validate Arguments

If num1 and num2 are omitted on the command line, the defaults of 1 layer and 50 (100 square blocks) are used.

After the arguments are verified, the variable minusNum2 is calculated as the negative value of num2.

Next, the position variable center is calculated as coordinates X and Z being the same as the player world  position, but the Y coordinate is specified as 3, which is the same level as the grass in a flat world. Notice that the player need not be on the ground for center to be calculated correctly. By using this variable instead of player world position for later program actions, the reference is always the same even if the player moves. I often like to have my player at a high vantage point and moving around  so that I can see the action. I don’t want a program behaving erratically just because I move the player. For example, a fence built relative to player world position might be built, or partly built, in the air. Fixing a reference point like center solves that problem.

symmetry code 2B

Canvas Code — Part 2 of 2 — Place 1 or 2 Layers Around Center.

The guts of the code is the loop on index variable Y_level that runs from 0 to num1 -1. If num1 (the number of levels) is 1, the loop only runs once. If it is 2, the loop runs twice.

The fill command uses the block at Y_Level in canvasBlocks; therefore, the top canvas level is the first block in canvasBlocks and the second block in canvasBlocks is the underlayer (the layer of blocks under the top level).

Each time through the loop, the Y coordinate for the from and to are the same, thus only a single layer of blocks are laid. The first pass through the loop, the top layer is set. The second pass, if any, the underlayer is set.

The from and to coordinates of the fill  block  define a square around the variable center as shown in the drawing below.

drawing 1

Pattern on-chat Command

The size of the pattern and the number of blocks (num2) to be used are specified as chat command arguments. num1 is half the final square side. The pattern is left-right mirrored and upper-lower mirrored. It can also the thought of as a quarter of the final pattern rotated to each of the four quadrants.

The pattern on-chat code has three parts:

  1. Validation of the parameters.
  2. Setting the variable origin as the reference position. Then setting q1, q2, q3 and q4 as the lower left corner of each of the final quadrants.
  3. Generating a random pattern of num2 blocks in quadrant 1, then mirroring it appropriately in quadrants 2, 3 and 4.

In pattern arguments, the size of the square (num1) that forms each of the quadrants of the final square must be positive. If zero or negative, it is set to default 5, which will result in a final square of 10 blocks. num2 is the number of blocks to be used, which must be at least 2. The patternBlocks list must have at least two blocks, the minimum number required to create a pattern.

pattern code 1

Pattern Code — Part 1 of 3 — Argument Validation

After the arguments are validated the reference positions are set. origin is the set to the player’s initial world position except at grass level. q1, q2, q3 and q4 are set to the lower-left corner of each of the 4 quadrants, respectively.

quadrants

q1 is set to origin. q2 is num1 blocks to the right (south)  of q1. q3 is num1 blocks west (below in drawing)  and num1 blocks south (right)  of q1. q4 is num1 blocks west (below in drawing) of q1.

pattern code 2

Pattern Code — Part 2 of 3

quandrants 2

The goal is to create a final square with four quadrants as shown here. I think of the upper-left quadrant as not mirrored with the other three quadrants mirrored as shown.

single quandrat rows and columns

For each quadrant,  rows are thought of as starting at the lower left corner at  row 0, moving upward to row num1 – 1. Columns are thought of as starting at column 0, moving left to right to column num1-1.

Conceptually,  the pattern code strategy is to crate a pattern by loping through each row and column. For each (row, column) coordinate, a block is selected randomly form patternBlocks list. The selected block is then placed appropriately in each quadrant. In quadrant 1, the block is simply placed at position (row, column) relative to q1. In quadrant 2, the block is placed at (row, num1 -1 – column), which causes the placement to be the mirrored across the vertical boundary between the quadrants. The other two quadrants are similarly placed in mirror positions.

pattern code 3

Pattern Code — Part 3 of 3

The drawing below is an illustration of the mirroring of quadrant 1 position (5, 6)

rows columns mirror

Border on-chat Command

The border on-chat command calls the function “border” to do the work. It is implemented is a function so that it can be easily moved from one program to another.

After setting the reference point one block to the west and one block to the north of the lower-left corner of the large 4-quadrant square, the builder is employed to create a border around the square using the first block in borderBlocks list. The builder-algorithm is similar to that used my Builder Fence post.

symmetry code 4

Pattern Code — 3 of 3 — Mirror Quadrant 1 Random Pattern in Quadrants 2, 3 and 4.

border starting position

Just One More Favorite Pattern

halloween 4

Get the Code

Symmetric Patterns  code is shared on Code Connection at this URL

https://makecode.com/_3C89FjDJaEbR

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.

The Art of Random Walks in MakeCode for Minecraft

A random walk is the path traveled by taking a step, then randomly turning either left or right, then repeating: a step, a random turn, a step, a random turn, and so on. Although a random walk sounds simple and is easy to code, it is an enormously important subject in many branches of science, such as physics, biology, economics and mathematics. Google Scholar lists 2,380,000 academic research papers on the subject.

1000 2 1

The science of random walks is not the subject of this blog. Rather, our aims are  to improve our coding skills in MakeCode for Minecraft and to explore random walk patterns as art.

Demo and How It Works

Program Inputs

  1. steps — the number of steps in the walk, maximum 3000, default 100.
  2. stepSize — the number of blocks in a single step, maximum of 10, default 3.
  3. blockGroup — is the group of blocks that can be used to create a random walk. The values can be 0, 1 or 2, default 0.
    • blockGroup 0 is LapisLazuliBlock or TNT depending or the random direction  (left or right).
    • blockGroup 1 choses a block randomly from a colorful array of choices.
    • blockGroup 2 choses a block randomly from a variations of black choices.

The Code

A simple random walk program called Agent Wanderer is found on Microsoft MakeCode for Minecraft site. The code below provides more flexibility by providing three arguments (see above) and using the Builder, which is faster than the Agent. the code below is shared at https://makecode.com/_AF0cdjKVu4zq, where it can be downloaded and run in MakeCode for Minecraft.

Random Walk Code

The code also includes a few utility Chats or function, which I plan to document in other posts. The utilities are:

  • Move Me — moves the player to the east and/or south the specified number of blocks.  Good for moving to a clean spot in the world.
  • Handy Compass– prints directions on the sky asynchronously. Handy for knowing the direction a random walk moves.
  • Go to Ground — moves the player to be “on the ground,” which is Y=4 in a flat world.
  • Canvas — replaces grass over a large area to create a white background for a random walk.

Further Explorations

  1. Add code to determine the percentage of the time does a random walk wander to the north, to the south, and so on?
  2. Add a counter for the number of  right turns as opposed to left turns.
  3. Add a counter for the number of blocks that are south of the origin, east of the origin, and so on.
  4. How often does a walk return to land on  the origin block during its path?
  5. Instead of replacing blocks that have already been replaced, stack them; thus, counting the number of times each block has been traversed.

A particularly intriguing activity is to try replacing

run chat command dir

with

 

call function dirFunction
What happens? Why?  This will be covered soon in another blog.