Diese Seite auf Deutsch

Styles ​

A style decides how a shape is drawn. So far, that was one block. This recipe paints with more: a city wall of stone bricks with moss and cracks in random places, a sunset of wool behind it, a chequered floor, and stripes in the colours of whatever you put into a chest.

Functions: STYLE_RANDOM, STYLE_GRADIENT, STYLE_PATTERN, STYLE_NONE, STYLE_EMPTY and INVENTORY_BLOCKS, and block states in a BLOCK

Needs an operator

These programs call RENDER_CANVAS without input, so the blocks come out of nowhere and replace what is in the way. That only works if the owner of the trigger block is an operator, or if the server allows it with allowBlockChanges=true in config/c0de.properties; otherwise RENDER_CANVAS raises PERMISSION_DENIED. In a single player world with cheats, you are an operator. To build from a chest instead, see A first canvas and exercise 4.

What you need

  • the mods c0de and c0de paint
  • a trigger block with a button on it, and an empty sheet for each program
  • for the palette: a chest with a few stacks of blocks, e.g. 64 sandstone, 32 smooth sandstone and 32 terracotta
  • free space: 17 blocks wide and 11 high north of the trigger block for the wall and the sunset, and 8 × 8 blocks east of it for the floor

The idea ​

Every shape has up to three parts, each with its own style: the lines (line_style), the faces (side_style) and the inside (fill_style). The styles are values like any other: a program makes them with the STYLE functions, keeps them in variables and hands them to the drawing functions. Besides STYLE_SOLID, there are:

Styledraws
STYLE_NONEnothing: the voxels stay as they are
STYLE_EMPTYair, which removes blocks
STYLE_RANDOMa random block of a list for every voxel
STYLE_GRADIENTthe blocks of a list one after the other, by the distance from a point
STYLE_PATTERNa small canvas, repeated again and again

The wall ​

c0de
PROGRAM stonewall
NOOP
INCLUDE c0de/minecraft
INCLUDE c0de_paint/cpaint
NOOP
FUNCTION build
    PARAMETER half INTEGER 6
    PARAMETER height INTEGER 5
BEGIN
    VARIABLE c TYPE c0de_paint/cpaint/CANVAS
    VARIABLE a STRUCT c0de_paint/cpaint/POINT
    VARIABLE b STRUCT c0de_paint/cpaint/POINT
    VARIABLE stone STRUCT c0de_paint/cpaint/BLOCK
    VARIABLE stones ARRAY STRUCT c0de_paint/cpaint/BLOCK
    VARIABLE mixed TYPE c0de_paint/cpaint/STYLE
    VARIABLE air TYPE c0de_paint/cpaint/STYLE
    VARIABLE site STRUCT c0de/minecraft/POSITION
    ASSIGN stone.id "stone_bricks" ;
    ASSIGN stone.weight 6 ;
    ASSIGN stones[0] stone ;
    ASSIGN stone.id "mossy_stone_bricks" ;
    ASSIGN stone.weight 3 ;
    ASSIGN stones[1] stone ;
    ASSIGN stone.id "cracked_stone_bricks" ;
    ASSIGN stone.weight 1 ;
    ASSIGN stones[2] stone ;
    CALL c0de_paint/cpaint/STYLE_RANDOM
        PARAMETER blocks stones ;
        RESULT result mixed
    CALL c0de_paint/cpaint/STYLE_EMPTY
        RESULT result air
    ASSIGN a.x - half ;
    ASSIGN b.x half ;
    ASSIGN b.y height - 1 ;
    CALL c0de_paint/cpaint/DRAW_BOX
        PARAMETER canvas c ;
        PARAMETER from a ;
        PARAMETER to b ;
        PARAMETER fill_style mixed ;
    ASSIGN a.x -1 ;
    ASSIGN b.x 1 ;
    ASSIGN b.y 2 ;
    CALL c0de_paint/cpaint/DRAW_BOX
        PARAMETER canvas c ;
        PARAMETER from a ;
        PARAMETER to b ;
        PARAMETER fill_style air ;
    ASSIGN site.x PID.x ;
    ASSIGN site.y PID.y ;
    ASSIGN site.z PID.z - 3 ;
    CALL c0de_paint/cpaint/RENDER_CANVAS
        PARAMETER canvas c ;
        PARAMETER position site ;
END FUNCTION

Building and running it ​

Stand behind the trigger block and look north. Set it to stonewall and build, and press the button. The wall rises 3 blocks north of the trigger block: 13 blocks long, with the trigger block in front of its middle, and 5 high. A gate 3 blocks wide and 3 high goes through its middle.

Press the button again: the wall gets new moss and new cracks every time.

the city wall of stone bricks with mossy and cracked bricks in random places and the gate in its middle, seen from the trigger block

How it works ​

  • A list of blocks. stones is an ARRAY of BLOCKs. The program fills one BLOCK, stone, and copies it into the list three times, with a new id and weight each time.
  • Weights. STYLE_RANDOM picks a block for every voxel. A block's chance is its weight divided by the sum: 6 of 10 voxels become plain stone bricks, 3 mossy and 1 cracked – on average.
  • A full box. The wall is a box 1 voxel thick, drawn with only a fill_style. A box without line and side styles is full: lines and faces take the style of the inside.
  • The canvas goes both ways. The wall runs from - half to half, so the voxel 0, 0, 0 lies in its middle. RENDER_CANVAS puts this voxel at site, right in front of the trigger block, 3 blocks north. Negative coordinates are fine in a canvas.
  • The gate is a second box from -1 to 1, 3 high, drawn with STYLE_EMPTY: air replaces the stones there. Air is drawn like a block – when it is built, it removes the block in the world.
  • New every time. The random numbers come from the process, like RANDOM. Every run draws a different wall.

The sunset ​

The second program paints a picture: a sunset in wool, 15 blocks wide and 9 high, in a frame of oak logs.

c0de
PROGRAM sunset
NOOP
INCLUDE c0de/minecraft
INCLUDE c0de_paint/cpaint
NOOP
FUNCTION paint
    PARAMETER half INTEGER 7
    PARAMETER height INTEGER 9
BEGIN
    VARIABLE c TYPE c0de_paint/cpaint/CANVAS
    VARIABLE a STRUCT c0de_paint/cpaint/POINT
    VARIABLE b STRUCT c0de_paint/cpaint/POINT
    VARIABLE sun STRUCT c0de_paint/cpaint/POINT
    VARIABLE wool STRUCT c0de_paint/cpaint/BLOCK
    VARIABLE sky ARRAY STRUCT c0de_paint/cpaint/BLOCK
    VARIABLE log STRUCT c0de_paint/cpaint/BLOCK
    VARIABLE colors TYPE c0de_paint/cpaint/STYLE
    VARIABLE posts TYPE c0de_paint/cpaint/STYLE
    VARIABLE beams TYPE c0de_paint/cpaint/STYLE
    VARIABLE site STRUCT c0de/minecraft/POSITION
    ASSIGN wool.id "yellow_wool" ;
    ASSIGN wool.weight 2 ;
    ASSIGN sky[0] wool ;
    ASSIGN wool.id "orange_wool" ;
    ASSIGN sky[1] wool ;
    ASSIGN wool.id "red_wool" ;
    ASSIGN sky[2] wool ;
    ASSIGN wool.id "magenta_wool" ;
    ASSIGN wool.weight 1 ;
    ASSIGN sky[3] wool ;
    ASSIGN wool.id "purple_wool" ;
    ASSIGN sky[4] wool ;
    ASSIGN wool.id "blue_wool" ;
    ASSIGN wool.weight 3 ;
    ASSIGN sky[5] wool ;
    ASSIGN sun.y 1 ;
    CALL c0de_paint/cpaint/STYLE_GRADIENT
        PARAMETER blocks sky ;
        PARAMETER from sun ;
        PARAMETER fuzzy 1 ;
        RESULT result colors
    ASSIGN a.x - half ;
    ASSIGN a.y 1 ;
    ASSIGN b.x half ;
    ASSIGN b.y height ;
    CALL c0de_paint/cpaint/DRAW_BOX
        PARAMETER canvas c ;
        PARAMETER from a ;
        PARAMETER to b ;
        PARAMETER fill_style colors ;
    NOOP
    ASSIGN log.id "oak_log" ;
    CALL c0de_paint/cpaint/STYLE_SOLID
        PARAMETER block log ;
        RESULT result posts
    ASSIGN log.properties["axis"] "x" ;
    CALL c0de_paint/cpaint/STYLE_SOLID
        PARAMETER block log ;
        RESULT result beams
    ASSIGN a.x - half - 1 ;
    ASSIGN a.y 0 ;
    ASSIGN b.x half + 1 ;
    ASSIGN b.y height + 1 ;
    CALL c0de_paint/cpaint/DRAW_BOX
        PARAMETER canvas c ;
        PARAMETER from a ;
        PARAMETER to b ;
        PARAMETER line_style posts ;
    ASSIGN a.x - half ;
    ASSIGN b.x half ;
    ASSIGN b.y 0 ;
    CALL c0de_paint/cpaint/DRAW_BOX
        PARAMETER canvas c ;
        PARAMETER from a ;
        PARAMETER to b ;
        PARAMETER fill_style beams ;
    ASSIGN a.y height + 1 ;
    ASSIGN b.y height + 1 ;
    CALL c0de_paint/cpaint/DRAW_BOX
        PARAMETER canvas c ;
        PARAMETER from a ;
        PARAMETER to b ;
        PARAMETER fill_style beams ;
    NOOP
    ASSIGN site.x PID.x ;
    ASSIGN site.y PID.y ;
    ASSIGN site.z PID.z - 4 ;
    CALL c0de_paint/cpaint/RENDER_CANVAS
        PARAMETER canvas c ;
        PARAMETER position site ;
END FUNCTION

Leave the trigger block where it is, set it to sunset and paint, and press the button. The picture stands 4 blocks north of the trigger block, right behind the wall, so the sun sets in the gate and the sky rises above the wall.

the wool sunset in its frame of oak logs right behind the city wall, seen from the trigger block: the sun sets in the gate, and the circles of yellow, orange, red, magenta, purple and blue rise above the wall (run `stonewall` with the function `build` from the same trigger block first)
  • A gradient. STYLE_GRADIENT draws the blocks of the list one after the other, by the distance from from. from is the sun, in the middle of the lowest row of the picture. Without axes, the distance is measured in all directions, so on a flat wall the colours form circles around the sun: yellow, orange, red, magenta, purple and blue.
  • The weights give the share of each colour. Yellow, orange and red have 2, magenta and purple 1, the blue sky 3. Without to, the gradient ends at the voxel farthest from the sun, the upper corners.
  • fuzzy 1 blurs the borders between the colours: in the row next to a border, a voxel takes the neighbouring colour with a chance of 50 %.
  • Block states. A log has the state axis: y stands upright, x lies east-west. log.properties["axis"] sets it, just like the states of PLACE_BLOCK. The posts have no state – an upright log is the default.
  • The frame. A box with only a line_style is just its edges: around a flat picture, that's the rim. Then two boxes overwrite the top and the bottom row with beams, except the corners. CANVAS_MATERIALS would count both kinds of logs as minecraft:oak_log: the states don't matter for the materials.

The floor ​

c0de
PROGRAM floor
NOOP
INCLUDE c0de/minecraft
INCLUDE c0de_paint/cpaint
NOOP
FUNCTION lay
    PARAMETER width INTEGER 8
    PARAMETER depth INTEGER 8
    PARAMETER border BOOLEAN TRUE
BEGIN
    VARIABLE tile TYPE c0de_paint/cpaint/CANVAS
    VARIABLE c TYPE c0de_paint/cpaint/CANVAS
    VARIABLE a STRUCT c0de_paint/cpaint/POINT
    VARIABLE b STRUCT c0de_paint/cpaint/POINT
    VARIABLE block STRUCT c0de_paint/cpaint/BLOCK
    VARIABLE black TYPE c0de_paint/cpaint/STYLE
    VARIABLE white TYPE c0de_paint/cpaint/STYLE
    VARIABLE checks TYPE c0de_paint/cpaint/STYLE
    VARIABLE edge TYPE c0de_paint/cpaint/STYLE
    VARIABLE site STRUCT c0de/minecraft/POSITION
    ASSIGN block.id "black_concrete" ;
    CALL c0de_paint/cpaint/STYLE_SOLID
        PARAMETER block block ;
        RESULT result black
    ASSIGN block.id "white_concrete" ;
    CALL c0de_paint/cpaint/STYLE_SOLID
        PARAMETER block block ;
        RESULT result white
    ASSIGN b.x 1 ;
    ASSIGN b.z 1 ;
    CALL c0de_paint/cpaint/DRAW_BOX
        PARAMETER canvas tile ;
        PARAMETER from a ;
        PARAMETER to b ;
        PARAMETER fill_style black ;
    CALL c0de_paint/cpaint/DRAW_BLOCK
        PARAMETER canvas tile ;
        PARAMETER point a ;
        PARAMETER style white ;
    CALL c0de_paint/cpaint/DRAW_BLOCK
        PARAMETER canvas tile ;
        PARAMETER point b ;
        PARAMETER style white ;
    CALL c0de_paint/cpaint/STYLE_PATTERN
        PARAMETER pattern tile ;
        RESULT result checks
    NOOP
    IF border ;
    BEGIN
        ASSIGN block.id "dark_oak_planks" ;
        CALL c0de_paint/cpaint/STYLE_SOLID
            PARAMETER block block ;
            RESULT result edge
    END
    ELSE
    BEGIN
        CALL c0de_paint/cpaint/STYLE_NONE
            RESULT result edge
    END IF
    ASSIGN b.x width - 1 ;
    ASSIGN b.z depth - 1 ;
    CALL c0de_paint/cpaint/DRAW_BOX
        PARAMETER canvas c ;
        PARAMETER from a ;
        PARAMETER to b ;
        PARAMETER line_style edge ;
        PARAMETER fill_style checks ;
    ASSIGN site.x PID.x + 2 ;
    ASSIGN site.y PID.y - 1 ;
    ASSIGN site.z PID.z ;
    CALL c0de_paint/cpaint/RENDER_CANVAS
        PARAMETER canvas c ;
        PARAMETER position site ;
END FUNCTION

Set the trigger block to floor and lay, and press the button. The floor replaces the ground east of the trigger block: 8 × 8 blocks from 2 blocks east of it, starting in its row and reaching south, in black and white concrete with a border of dark oak planks. For a floor without a border, put a BOOLEAN variable item border with FALSE into the trigger block.

the chequered floor of black and white concrete, 8 × 8 blocks, with its border of dark oak planks, set into the grass east of the trigger block, seen from above
  • The tile. tile is a canvas of its own, 2 × 1 × 2 voxels: a black box with two white voxels on the diagonal, drawn with DRAW_BLOCK.
  • The pattern. STYLE_PATTERN repeats the tile along all axes, starting at 0, 0, 0. Every voxel of the floor takes the voxel of the tile at its place.
  • STYLE_NONE on purpose. Without a border, edge becomes STYLE_NONE. A part with STYLE_NONE takes the style of the next part inward, so the edges of the floor get the pattern, too. A new STYLE variable holds STYLE_NONE anyway; the call makes it visible.
  • Into the ground. site.y is one below the trigger block, so the floor lies flush with the ground and replaces grass and dirt.

The palette ​

Instead of writing the blocks into the program, take them from a chest. INVENTORY_BLOCKS reads them: in the order of the slots, with the number of items as weight – a list ready for STYLE_RANDOM and STYLE_GRADIENT.

c0de
PROGRAM palette
NOOP
INCLUDE c0de/minecraft
INCLUDE c0de_paint/cpaint
NOOP
FUNCTION show
    PARAMETER palette ARRAY STRUCT c0de_paint/cpaint/BLOCK
    PARAMETER length INTEGER 16
    PARAMETER height INTEGER 4
BEGIN
    VARIABLE c TYPE c0de_paint/cpaint/CANVAS
    VARIABLE a STRUCT c0de_paint/cpaint/POINT
    VARIABLE b STRUCT c0de_paint/cpaint/POINT
    VARIABLE stripes TYPE c0de_paint/cpaint/STYLE
    VARIABLE site STRUCT c0de/minecraft/POSITION
    CALL c0de_paint/cpaint/STYLE_GRADIENT
        PARAMETER blocks palette ;
        PARAMETER from a ;
        PARAMETER axes "x" ;
        PARAMETER fuzzy 1 ;
        RESULT result stripes
    ASSIGN b.x length - 1 ;
    ASSIGN b.y height - 1 ;
    CALL c0de_paint/cpaint/DRAW_BOX
        PARAMETER canvas c ;
        PARAMETER from a ;
        PARAMETER to b ;
        PARAMETER fill_style stripes ;
    ASSIGN site.x PID.x + 2 ;
    ASSIGN site.y PID.y ;
    ASSIGN site.z PID.z ;
    CALL c0de_paint/cpaint/RENDER_CANVAS
        PARAMETER canvas c ;
        PARAMETER position site ;
END FUNCTION
NOOP
FUNCTION build
    PARAMETER length INTEGER 16
    PARAMETER height INTEGER 4
BEGIN
    VARIABLE chest STRUCT c0de/minecraft/POSITION
    VARIABLE blocks ARRAY STRUCT c0de_paint/cpaint/BLOCK
    ASSIGN chest.x PID.x - 2 ;
    ASSIGN chest.y PID.y ;
    ASSIGN chest.z PID.z ;
    CALL c0de_paint/cpaint/INVENTORY_BLOCKS
        PARAMETER position chest ;
        RESULT result blocks
    CALL show
        PARAMETER palette blocks ;
        PARAMETER length length ;
        PARAMETER height height ;
END FUNCTION

Move the trigger block to a free place and put the chest 2 blocks west of it, with 64 sandstone in the first slot, 32 smooth sandstone in the second and 32 terracotta in the third. Set the trigger block to palette and build, and press the button. A wall 16 blocks long and 4 high appears 2 blocks east of the trigger block, in stripes: 8 blocks of sandstone, 4 of smooth sandstone, 4 of terracotta, with blurred borders. The chest keeps its blocks; it is only the palette.

the open chest with sandstone, smooth sandstone and terracotta in its first three slots, and the striped wall 16 blocks long beside it, sandstone fading to smooth sandstone and terracotta from west to east
  • Two functions. build reads the chest and hands the list to show, which draws and builds. show works with any list of blocks, so it can be tried without a chest, too.
  • Stripes. axes "x" measures the distance from a only along x, so all voxels of a column have the same distance: the gradient becomes vertical stripes. The 128 items share the 16 blocks of the wall: 64 sandstone get half of them.
  • Your own palette. Change the order of the slots, and the stripes change their order. Wool, concrete or glass in many colours make a rainbow.

Try it yourself ​

  1. The wall is getting old: more moss, and here and there a block of cobblestone.
Solution

Give mossy_stone_bricks a weight of 5, and add a fourth block:

c0de
ASSIGN stone.id "cobblestone" ;
ASSIGN stone.weight 1 ;
ASSIGN stones[3] stone ;

Now the weights add up to 13: 6 of 13 voxels are plain stone bricks, 5 mossy, 1 cracked and 1 cobblestone.

  1. Paint the sky in horizontal bands instead of circles.
Solution

Add PARAMETER axes "y" ; to STYLE_GRADIENT. Now only the height counts: the row of the sun is yellow, and the colours follow in layers up to the blue at the top.

  1. A parquet floor: stripped oak logs, lying alternately east-west and north-south, like the squares of the chequered floor.
Solution

The tile has two styles of the same block with different states: axis x lies east-west, z north-south.

c0de
PROGRAM parquet
NOOP
INCLUDE c0de/minecraft
INCLUDE c0de_paint/cpaint
NOOP
FUNCTION lay
    PARAMETER width INTEGER 8
    PARAMETER depth INTEGER 8
BEGIN
    VARIABLE tile TYPE c0de_paint/cpaint/CANVAS
    VARIABLE c TYPE c0de_paint/cpaint/CANVAS
    VARIABLE a STRUCT c0de_paint/cpaint/POINT
    VARIABLE b STRUCT c0de_paint/cpaint/POINT
    VARIABLE log STRUCT c0de_paint/cpaint/BLOCK
    VARIABLE eastwest TYPE c0de_paint/cpaint/STYLE
    VARIABLE northsouth TYPE c0de_paint/cpaint/STYLE
    VARIABLE parquet TYPE c0de_paint/cpaint/STYLE
    VARIABLE site STRUCT c0de/minecraft/POSITION
    ASSIGN log.id "stripped_oak_log" ;
    ASSIGN log.properties["axis"] "x" ;
    CALL c0de_paint/cpaint/STYLE_SOLID
        PARAMETER block log ;
        RESULT result eastwest
    ASSIGN log.properties["axis"] "z" ;
    CALL c0de_paint/cpaint/STYLE_SOLID
        PARAMETER block log ;
        RESULT result northsouth
    ASSIGN b.x 1 ;
    ASSIGN b.z 1 ;
    CALL c0de_paint/cpaint/DRAW_BOX
        PARAMETER canvas tile ;
        PARAMETER from a ;
        PARAMETER to b ;
        PARAMETER fill_style northsouth ;
    CALL c0de_paint/cpaint/DRAW_BLOCK
        PARAMETER canvas tile ;
        PARAMETER point a ;
        PARAMETER style eastwest ;
    CALL c0de_paint/cpaint/DRAW_BLOCK
        PARAMETER canvas tile ;
        PARAMETER point b ;
        PARAMETER style eastwest ;
    CALL c0de_paint/cpaint/STYLE_PATTERN
        PARAMETER pattern tile ;
        RESULT result parquet
    ASSIGN b.x width - 1 ;
    ASSIGN b.z depth - 1 ;
    CALL c0de_paint/cpaint/DRAW_BOX
        PARAMETER canvas c ;
        PARAMETER from a ;
        PARAMETER to b ;
        PARAMETER fill_style parquet ;
    ASSIGN site.x PID.x + 2 ;
    ASSIGN site.y PID.y - 1 ;
    ASSIGN site.z PID.z ;
    CALL c0de_paint/cpaint/RENDER_CANVAS
        PARAMETER canvas c ;
        PARAMETER position site ;
END FUNCTION
  1. Build the palette wall from the chest itself, with STYLE_RANDOM instead of the gradient. Does it always work?
Solution

Replace the STYLE_GRADIENT call with

c0de
CALL c0de_paint/cpaint/STYLE_RANDOM
    PARAMETER blocks palette ;
    RESULT result stripes

Then give show a third parameter chest, which build passes on, and RENDER_CANVAS the line PARAMETER input chest ;, like the shed of A first canvas. Now the wall takes its 64 blocks from the chest. With 128 items for 64 blocks, there is nearly always enough of each. But fill the chest with exactly 64 items, and the random blocks don't match it: where the chest has 32 sandstone, one run wants 35, the next 29. RENDER_CANVAS checks the chest before the first block and raises ITEM_MISSING when a block is short – without placing a single one. A gradient without fuzzy has no such surprises: its stripes always have the same width.

Read more ​