Diese Seite auf Deutsch

A first canvas ​

A program as an architect: it draws a garden shed of oak planks with posts of oak logs, and then builds it from the blocks of a chest, for every player. Before you fill the chest, a laptop tells you what the shed needs.

Functions: DRAW_BOX, STYLE_SOLID, RENDER_CANVAS, CANVAS_MATERIALS and CANVAS_COUNT, with the types CANVAS, POINT, BLOCK and STYLE

What you need

  • the mods c0de and c0de paint
  • a trigger block with a button on it, and an empty sheet for the program
  • a chest with 36 oak logs and 30 oak planks
  • a laptop for the list of blocks
  • a flat place of 5 × 4 blocks, free of tall grass and flowers
  • no rights: the shed is built from the chest, like a player builds from their inventory

The idea ​

The functions of c0de/minecraft place one block at a time. c0de paint works like a drawing program instead: you draw into a canvas, a grid of voxels in three dimensions, and the canvas is built in the world in one go. The canvas is only in the program – nothing happens in the world until RENDER_CANVAS.

Four kinds of values do the drawing:

  • a CANVAS holds the drawing. A new variable holds an empty canvas, and it grows with whatever you draw.
  • a POINT is a voxel of the canvas, with x (east), y (up) and z (south). A new POINT is 0, 0, 0.
  • a BLOCK names a Minecraft block by its id, like "oak_planks".
  • a STYLE says how something is drawn. STYLE_SOLID makes one: always the same block.

DRAW_BOX draws a box between two corners. It has three styles: line_style for its 12 edges, side_style for its six faces and fill_style for the inside. A shed is a box with planks on its faces, logs on its edges and nothing inside.

The shed ​

c0de
PROGRAM shed
NOOP
INCLUDE c0de/minecraft
INCLUDE c0de_paint/cpaint
NOOP
FUNCTION draw
    PARAMETER width INTEGER
    PARAMETER depth INTEGER
    PARAMETER height INTEGER
    RESULT result TYPE c0de_paint/cpaint/CANVAS
BEGIN
    VARIABLE a STRUCT c0de_paint/cpaint/POINT
    VARIABLE b STRUCT c0de_paint/cpaint/POINT
    VARIABLE planks STRUCT c0de_paint/cpaint/BLOCK
    VARIABLE log STRUCT c0de_paint/cpaint/BLOCK
    VARIABLE walls TYPE c0de_paint/cpaint/STYLE
    VARIABLE posts TYPE c0de_paint/cpaint/STYLE
    VARIABLE air TYPE c0de_paint/cpaint/STYLE
    ASSIGN planks.id "oak_planks" ;
    ASSIGN log.id "oak_log" ;
    CALL c0de_paint/cpaint/STYLE_SOLID
        PARAMETER block planks ;
        RESULT result walls
    CALL c0de_paint/cpaint/STYLE_SOLID
        PARAMETER block log ;
        RESULT result posts
    CALL c0de_paint/cpaint/STYLE_EMPTY
        RESULT result air
    ASSIGN b.x width - 1 ;
    ASSIGN b.y height - 1 ;
    ASSIGN b.z depth - 1 ;
    CALL c0de_paint/cpaint/DRAW_BOX
        PARAMETER canvas result ;
        PARAMETER from a ;
        PARAMETER to b ;
        PARAMETER line_style posts ;
        PARAMETER side_style walls ;
    ASSIGN a.y 1 ;
    ASSIGN a.z 1 ;
    ASSIGN b.x 0 ;
    ASSIGN b.y 2 ;
    ASSIGN b.z 1 ;
    CALL c0de_paint/cpaint/DRAW_BOX
        PARAMETER canvas result ;
        PARAMETER from a ;
        PARAMETER to b ;
        PARAMETER fill_style air ;
END FUNCTION
NOOP
FUNCTION build
    PARAMETER width INTEGER 5
    PARAMETER depth INTEGER 4
    PARAMETER height INTEGER 4
BEGIN
    VARIABLE shed TYPE c0de_paint/cpaint/CANVAS
    VARIABLE site STRUCT c0de/minecraft/POSITION
    VARIABLE chest STRUCT c0de/minecraft/POSITION
    CALL draw
        PARAMETER width width ;
        PARAMETER depth depth ;
        PARAMETER height height ;
        RESULT result shed
    ASSIGN site.x PID.x + 2 ;
    ASSIGN site.y PID.y ;
    ASSIGN site.z PID.z ;
    ASSIGN chest.x PID.x - 2 ;
    ASSIGN chest.y PID.y ;
    ASSIGN chest.z PID.z ;
    CALL c0de_paint/cpaint/RENDER_CANVAS
        PARAMETER canvas shed ;
        PARAMETER position site ;
        PARAMETER input chest ;
END FUNCTION
NOOP
FUNCTION materials
    PARAMETER width INTEGER 5
    PARAMETER depth INTEGER 4
    PARAMETER height INTEGER 4
BEGIN
    VARIABLE shed TYPE c0de_paint/cpaint/CANVAS
    VARIABLE list HASH INTEGER
    VARIABLE voxels INTEGER
    CALL draw
        PARAMETER width width ;
        PARAMETER depth depth ;
        PARAMETER height height ;
        RESULT result shed
    CALL c0de_paint/cpaint/CANVAS_MATERIALS
        PARAMETER canvas shed ;
        RESULT result list
    CALL c0de_paint/cpaint/CANVAS_COUNT
        PARAMETER canvas shed ;
        RESULT result voxels
    CALL CLS
    CALL PRINT
        PARAMETER value "The shed needs:" ;
    EACH id list
    BEGIN
        CALL PRINT
            PARAMETER value list[id] ⊕ " " ⊕ id ;
    END EACH
    CALL PRINT
        PARAMETER value voxels ⊕ " voxels in all" ;
END FUNCTION

Building and running it ​

  1. Place the trigger block with the button 2 blocks west of where the shed should stand.
  2. Place the chest 2 blocks west of the trigger block, and put the oak logs and planks into it.
  3. Set the trigger block to shed and build, and press the button.

The shed takes the 5 × 4 blocks from 2 to 6 blocks east of the trigger block and from the trigger block's row to 3 blocks south of it. It is 4 blocks high, with a floor of planks, and its doorway in the west wall faces the trigger block. The chest is empty afterwards.

the finished garden shed, 5 × 4 blocks and 4 high, oak planks with oak logs on all edges, its doorway in the west wall, the trigger block with its button 2 blocks west of it and the chest 2 blocks further west

How it works ​

  • draw only draws. It gets the size, gives back the canvas as its result, and doesn't touch the world. build builds the canvas, materials counts it – both call draw, so they always agree on the shed.
  • The two styles. Every STYLE_SOLID gets a BLOCK. The id "oak_planks" has no namespace, so it becomes minecraft:oak_planks.
  • The box. a stays 0, 0, 0; b is the opposite corner, width - 1 east, height - 1 up and depth - 1 south. A box from 0 to 4 is 5 voxels long. There is no fill_style, so the inside isn't drawn at all: whatever is there in the world stays.
  • The drawing functions change the canvas. DRAW_BOX gets canvas result, the canvas of draw, and draws into it, like INPUT stores in its variable.
  • The doorway is a second box, 1 voxel wide and 2 high, in the west wall, drawn with STYLE_EMPTY: air. It overwrites the planks there. When the shed is built, air means "no block here".
  • Building. RENDER_CANVAS puts the voxel 0, 0, 0 of the canvas at site, 2 blocks east of the trigger block. With input, it takes the blocks from the chest. Before the first block, it checks that the chest has all of them, so a missing plank doesn't leave half a shed: the program crashes with ITEM_MISSING, and nothing is built. Blocks that are already in place are skipped, so pressing the button again does no harm.
  • Without input, RENDER_CANVAS creates the blocks out of nowhere and replaces whatever is in the way. That needs allowBlockChanges on the server or an operator as owner, like REMOVE_BLOCK.

The list of blocks ​

How many logs, how many planks? You don't have to count: the second function of the program, materials, prints the list on a laptop. Bind the laptop to shed / materials, switch the trigger block to the same function and press the button. The laptop shows

The shed needs:
36 minecraft:oak_log
30 minecraft:oak_planks
68 voxels in all
the laptop screen with the list "The shed needs:", "36 minecraft:oak_log", "30 minecraft:oak_planks" and "68 voxels in all", the trigger block and the chest next to it
  • CANVAS_MATERIALS gives a HASH from block id to number: exactly what the chest must hold. EACH goes through its keys in alphabetical order.
  • CANVAS_COUNT counts the set voxels: 68, but the list only adds up to 66. The two missing voxels are the air of the doorway – a set voxel, but no block you need. CANVAS_MATERIALS leaves air out.

Try it yourself ​

  1. Put a window of glass into the east wall, opposite the door: 2 blocks at eye height.
Solution

A third box, from width - 1, 2, 1 to width - 1, 2, depth - 2, with a STYLE_SOLID of glass. It overwrites two planks, so the chest needs 2 glass and only 28 planks.

c0de
PROGRAM shed
NOOP
INCLUDE c0de/minecraft
INCLUDE c0de_paint/cpaint
NOOP
FUNCTION draw
    PARAMETER width INTEGER
    PARAMETER depth INTEGER
    PARAMETER height INTEGER
    RESULT result TYPE c0de_paint/cpaint/CANVAS
BEGIN
    VARIABLE a STRUCT c0de_paint/cpaint/POINT
    VARIABLE b STRUCT c0de_paint/cpaint/POINT
    VARIABLE planks STRUCT c0de_paint/cpaint/BLOCK
    VARIABLE log STRUCT c0de_paint/cpaint/BLOCK
    VARIABLE pane STRUCT c0de_paint/cpaint/BLOCK
    VARIABLE walls TYPE c0de_paint/cpaint/STYLE
    VARIABLE posts TYPE c0de_paint/cpaint/STYLE
    VARIABLE air TYPE c0de_paint/cpaint/STYLE
    VARIABLE glass TYPE c0de_paint/cpaint/STYLE
    ASSIGN planks.id "oak_planks" ;
    ASSIGN log.id "oak_log" ;
    ASSIGN pane.id "glass" ;
    CALL c0de_paint/cpaint/STYLE_SOLID
        PARAMETER block planks ;
        RESULT result walls
    CALL c0de_paint/cpaint/STYLE_SOLID
        PARAMETER block log ;
        RESULT result posts
    CALL c0de_paint/cpaint/STYLE_SOLID
        PARAMETER block pane ;
        RESULT result glass
    CALL c0de_paint/cpaint/STYLE_EMPTY
        RESULT result air
    ASSIGN b.x width - 1 ;
    ASSIGN b.y height - 1 ;
    ASSIGN b.z depth - 1 ;
    CALL c0de_paint/cpaint/DRAW_BOX
        PARAMETER canvas result ;
        PARAMETER from a ;
        PARAMETER to b ;
        PARAMETER line_style posts ;
        PARAMETER side_style walls ;
    ASSIGN a.y 1 ;
    ASSIGN a.z 1 ;
    ASSIGN b.x 0 ;
    ASSIGN b.y 2 ;
    ASSIGN b.z 1 ;
    CALL c0de_paint/cpaint/DRAW_BOX
        PARAMETER canvas result ;
        PARAMETER from a ;
        PARAMETER to b ;
        PARAMETER fill_style air ;
    ASSIGN a.x width - 1 ;
    ASSIGN a.y 2 ;
    ASSIGN b.x width - 1 ;
    ASSIGN b.z depth - 2 ;
    CALL c0de_paint/cpaint/DRAW_BOX
        PARAMETER canvas result ;
        PARAMETER from a ;
        PARAMETER to b ;
        PARAMETER fill_style glass ;
END FUNCTION
NOOP
FUNCTION build
    PARAMETER width INTEGER 5
    PARAMETER depth INTEGER 4
    PARAMETER height INTEGER 4
BEGIN
    VARIABLE shed TYPE c0de_paint/cpaint/CANVAS
    VARIABLE site STRUCT c0de/minecraft/POSITION
    VARIABLE chest STRUCT c0de/minecraft/POSITION
    CALL draw
        PARAMETER width width ;
        PARAMETER depth depth ;
        PARAMETER height height ;
        RESULT result shed
    ASSIGN site.x PID.x + 2 ;
    ASSIGN site.y PID.y ;
    ASSIGN site.z PID.z ;
    ASSIGN chest.x PID.x - 2 ;
    ASSIGN chest.y PID.y ;
    ASSIGN chest.z PID.z ;
    CALL c0de_paint/cpaint/RENDER_CANVAS
        PARAMETER canvas shed ;
        PARAMETER position site ;
        PARAMETER input chest ;
END FUNCTION
  1. Your horse wants a stable: make the shed 7 blocks wide, 5 deep and 5 high. How many logs and planks does it need?
Solution

No new code: put the INTEGER variable items width 7, depth 5 and height 5 into the trigger block, for materials and then for build. The laptop says 52 oak logs and 76 oak planks, 130 voxels in all.

  1. Take 5 planks out of the chest and press the button. What happens – and what stays in the world?
Solution

The program crashes with ITEM_MISSING, and the trigger block's dialog shows the error. Not a single block is placed: RENDER_CANVAS checks the chest before the first block. Put the planks back and press again.

Read more ​