
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.


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.

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:
- As the values of the parameters in the functions “default_params” and “custom_params.”
- As the three arguments to the on-chat “city” command, which starts the program.
- 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

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.


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:
- jewel_array,
- black_array,
- other_array and
- 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.”

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

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.

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.

on-chat Command “city” Starts the Program
The on-chat “city” command defines the main flow of the program in the following steps:
- 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.
- Initializes variables, sets origin and, optionally, launches an on-chat to draw a compass.
- 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.
- Adjusts height of origin marker to be taller than highest building and moves player to above origin.
- Calls “build_ground” to create rectangle at ground level.

Function to Do One Building
The “do_one_building” function does the following:
- picks random height, width and depth.
- calls function to pick block to be used.
- uses fill block to create hollow building.
- uses fill block to lay foundation of building.
- calls function to update minimum and maximum X and Z.

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.

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.

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.


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.

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.

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.

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.

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.


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.

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.

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

Get the Code
Cityscape code is shared on Code Connection at this URL
To get and use the code, follow these steps.


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

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


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

Click the 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.






You must be logged in to post a comment.