Diese Seite auf Deutsch

Shaping canvases ​

A program as a town planner: it draws one small house of oak planks, and makes a whole street of it. One copy gets brick walls, one is still a building site, one is only a façade, one is blown up into a hall, one is turned to close the end of the street, and the house across the road is a mirror image. At the end, every door gets a path.

Functions: CANVAS_MIN and CANVAS_MAX, CANVAS_GET, CANVAS_CLEAR and CANVAS_CROP, CANVAS_MOVE, CANVAS_REPLACE, CANVAS_MERGE, CANVAS_SCALE, CANVAS_ROTATE and CANVAS_MIRROR, with DRAW_BOX, DRAW_LINE, DRAW_BLOCK and RENDER_CANVAS

Needs an operator

The street needs oak planks, oak stairs, oak doors, bricks and dirt paths – too many kinds for a chest, and dirt paths can't be carried at all. So RENDER_CANVAS builds it without input, out of nowhere, and replaces whatever 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 it raises PERMISSION_DENIED. In a single-player world with cheats, you are an operator. Copying a building shows how to build from chests.

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 flat place of about 45 × 18 blocks: the street runs 45 blocks east of the trigger block, from 6 blocks north of it to 11 blocks south
  • operator rights or allowBlockChanges (see above)

The idea ​

A canvas is a value like a number: ASSIGN copy home ; makes a copy, and changing the copy leaves home as it is. So the program draws the house only once, in a function of its own, and then takes a fresh copy for every plot of the street. The canvas operations change a copy without drawing anything new:

Only one RENDER_CANVAS at the very end builds the whole street in the world.

The program ​

c0de
PROGRAM street
NOOP
INCLUDE c0de/minecraft
INCLUDE c0de_paint/cpaint
NOOP
FUNCTION house
    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 c STRUCT c0de_paint/cpaint/POINT
    VARIABLE block STRUCT c0de_paint/cpaint/BLOCK
    VARIABLE planks TYPE c0de_paint/cpaint/STYLE
    VARIABLE south TYPE c0de_paint/cpaint/STYLE
    VARIABLE north TYPE c0de_paint/cpaint/STYLE
    VARIABLE lower TYPE c0de_paint/cpaint/STYLE
    VARIABLE upper TYPE c0de_paint/cpaint/STYLE
    ASSIGN block.id "oak_planks" ;
    CALL c0de_paint/cpaint/STYLE_SOLID
        PARAMETER block block ;
        RESULT result planks
    ASSIGN block.id "oak_stairs" ;
    ASSIGN block.properties["facing"] "south" ;
    CALL c0de_paint/cpaint/STYLE_SOLID
        PARAMETER block block ;
        RESULT result south
    ASSIGN block.properties["facing"] "north" ;
    CALL c0de_paint/cpaint/STYLE_SOLID
        PARAMETER block block ;
        RESULT result north
    ASSIGN block.id "oak_door" ;
    ASSIGN block.properties["hinge"] "left" ;
    ASSIGN block.properties["half"] "lower" ;
    CALL c0de_paint/cpaint/STYLE_SOLID
        PARAMETER block block ;
        RESULT result lower
    ASSIGN block.properties["half"] "upper" ;
    CALL c0de_paint/cpaint/STYLE_SOLID
        PARAMETER block block ;
        RESULT result upper
    NOOP
    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 side_style planks ;
    ASSIGN a.x 1 ;
    CALL c0de_paint/cpaint/DRAW_BLOCK
        PARAMETER canvas result ;
        PARAMETER point a ;
        PARAMETER style lower ;
    ASSIGN a.y 1 ;
    CALL c0de_paint/cpaint/DRAW_BLOCK
        PARAMETER canvas result ;
        PARAMETER point a ;
        PARAMETER style upper ;
    NOOP
    ASSIGN a.x 0 ;
    ASSIGN a.y height ;
    ASSIGN b.y height ;
    WHILE a.z ≤ b.z ;
    BEGIN
        CALL c0de_paint/cpaint/DRAW_BOX
            PARAMETER canvas result ;
            PARAMETER from a ;
            PARAMETER to b ;
            PARAMETER fill_style planks ;
        IF a.z < b.z ;
        BEGIN
            ASSIGN c a ;
            ASSIGN c.x b.x ;
            CALL c0de_paint/cpaint/DRAW_LINE
                PARAMETER canvas result ;
                PARAMETER from a ;
                PARAMETER to c ;
                PARAMETER style south ;
            ASSIGN c b ;
            ASSIGN c.x 0 ;
            CALL c0de_paint/cpaint/DRAW_LINE
                PARAMETER canvas result ;
                PARAMETER from c ;
                PARAMETER to b ;
                PARAMETER style north ;
        END IF
        ASSIGN a.y a.y + 1 ;
        ASSIGN b.y b.y + 1 ;
        ASSIGN a.z a.z + 1 ;
        ASSIGN b.z b.z - 1 ;
    END WHILE
END FUNCTION
NOOP
FUNCTION addHouse
    PARAMETER street TYPE c0de_paint/cpaint/CANVAS
    PARAMETER house TYPE c0de_paint/cpaint/CANVAS
    PARAMETER z INTEGER 0
    RESULT result TYPE c0de_paint/cpaint/CANVAS
BEGIN
    VARIABLE low STRUCT c0de_paint/cpaint/POINT
    VARIABLE high STRUCT c0de_paint/cpaint/POINT
    VARIABLE offset STRUCT c0de_paint/cpaint/POINT
    VARIABLE moved TYPE c0de_paint/cpaint/CANVAS
    CALL c0de_paint/cpaint/CANVAS_MAX
        PARAMETER canvas street ;
        RESULT result high
    CALL c0de_paint/cpaint/CANVAS_MIN
        PARAMETER canvas house ;
        RESULT result low
    ASSIGN offset.x high.x + 3 - low.x ;
    ASSIGN offset.y - low.y ;
    ASSIGN offset.z z - low.z ;
    ASSIGN moved house ;
    CALL c0de_paint/cpaint/CANVAS_MOVE
        PARAMETER canvas moved ;
        PARAMETER offset offset ;
    ASSIGN result street ;
    CALL c0de_paint/cpaint/CANVAS_MERGE
        PARAMETER target result ;
        PARAMETER source moved ;
END FUNCTION
NOOP
FUNCTION paths
    PARAMETER street TYPE c0de_paint/cpaint/CANVAS
    RESULT result TYPE c0de_paint/cpaint/CANVAS
BEGIN
    VARIABLE low STRUCT c0de_paint/cpaint/POINT
    VARIABLE high STRUCT c0de_paint/cpaint/POINT
    VARIABLE p STRUCT c0de_paint/cpaint/POINT
    VARIABLE block STRUCT c0de_paint/cpaint/BLOCK
    VARIABLE path TYPE c0de_paint/cpaint/STYLE
    VARIABLE facing STRING
    VARIABLE dx HASH INTEGER
    BEGIN
        east 1
        west -1
        north 0
        south 0
    END VARIABLE
    VARIABLE dz HASH INTEGER
    BEGIN
        east 0
        west 0
        north -1
        south 1
    END VARIABLE
    ASSIGN result street ;
    ASSIGN block.id "dirt_path" ;
    CALL c0de_paint/cpaint/STYLE_SOLID
        PARAMETER block block ;
        RESULT result path
    CALL c0de_paint/cpaint/CANVAS_MIN
        PARAMETER canvas street ;
        RESULT result low
    CALL c0de_paint/cpaint/CANVAS_MAX
        PARAMETER canvas street ;
        RESULT result high
    FOR x INTEGER low.x ; high.x ;
    BEGIN
        FOR z INTEGER low.z ; high.z ;
        BEGIN
            ASSIGN p.x x ;
            ASSIGN p.y 0 ;
            ASSIGN p.z z ;
            CALL c0de_paint/cpaint/CANVAS_GET
                PARAMETER canvas street ;
                PARAMETER point p ;
                RESULT result block
            IF block.id = "minecraft:oak_door" ;
            BEGIN
                ASSIGN facing block.properties["facing"] ;
                ASSIGN p.x x + dx[facing] ;
                ASSIGN p.y -1 ;
                ASSIGN p.z z + dz[facing] ;
                CALL c0de_paint/cpaint/DRAW_BLOCK
                    PARAMETER canvas result ;
                    PARAMETER point p ;
                    PARAMETER style path ;
            END IF
        END FOR
    END FOR
END FUNCTION
NOOP
FUNCTION build
    PARAMETER width INTEGER 5
    PARAMETER depth INTEGER 5
    PARAMETER height INTEGER 4
BEGIN
    VARIABLE home TYPE c0de_paint/cpaint/CANVAS
    VARIABLE street TYPE c0de_paint/cpaint/CANVAS
    VARIABLE copy 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 bricks TYPE c0de_paint/cpaint/STYLE
    VARIABLE air TYPE c0de_paint/cpaint/STYLE
    VARIABLE at STRUCT c0de/minecraft/POSITION
    CALL house
        PARAMETER width width ;
        PARAMETER depth depth ;
        PARAMETER height height ;
        RESULT result home
    ASSIGN street home ;
    NOOP
    ASSIGN block.id "bricks" ;
    CALL c0de_paint/cpaint/STYLE_SOLID
        PARAMETER block block ;
        RESULT result bricks
    ASSIGN copy home ;
    CALL c0de_paint/cpaint/CANVAS_REPLACE
        PARAMETER canvas copy ;
        PARAMETER id "oak_planks" ;
        PARAMETER style bricks ;
    CALL addHouse
        PARAMETER street street ;
        PARAMETER house copy ;
        RESULT result street
    NOOP
    ASSIGN copy home ;
    ASSIGN a.y 2 ;
    CALL c0de_paint/cpaint/CANVAS_MAX
        PARAMETER canvas copy ;
        RESULT result b
    CALL c0de_paint/cpaint/CANVAS_CLEAR
        PARAMETER canvas copy ;
        PARAMETER from a ;
        PARAMETER to b ;
    CALL addHouse
        PARAMETER street street ;
        PARAMETER house copy ;
        RESULT result street
    NOOP
    ASSIGN copy home ;
    ASSIGN a.y 0 ;
    ASSIGN b.z 0 ;
    CALL c0de_paint/cpaint/CANVAS_CROP
        PARAMETER canvas copy ;
        PARAMETER from a ;
        PARAMETER to b ;
    CALL addHouse
        PARAMETER street street ;
        PARAMETER house copy ;
        RESULT result street
    NOOP
    ASSIGN copy home ;
    CALL c0de_paint/cpaint/CANVAS_SCALE
        PARAMETER canvas copy ;
        PARAMETER x 200 ;
        PARAMETER y 200 ;
        PARAMETER z 200 ;
    CALL c0de_paint/cpaint/STYLE_EMPTY
        RESULT result air
    CALL c0de_paint/cpaint/CANVAS_REPLACE
        PARAMETER canvas copy ;
        PARAMETER id "oak_door" ;
        PARAMETER style air ;
    CALL addHouse
        PARAMETER street street ;
        PARAMETER house copy ;
        RESULT result street
    NOOP
    ASSIGN copy home ;
    CALL c0de_paint/cpaint/CANVAS_ROTATE
        PARAMETER canvas copy ;
        PARAMETER center a ;
        PARAMETER axis "y" ;
        PARAMETER degrees 90 ;
    CALL addHouse
        PARAMETER street street ;
        PARAMETER house copy ;
        PARAMETER z - width ;
        RESULT result street
    NOOP
    ASSIGN copy home ;
    CALL c0de_paint/cpaint/CANVAS_MIRROR
        PARAMETER canvas copy ;
        PARAMETER axis "z" ;
        PARAMETER position -2.0 ;
    CALL c0de_paint/cpaint/CANVAS_MERGE
        PARAMETER target street ;
        PARAMETER source copy ;
    NOOP
    CALL paths
        PARAMETER street street ;
        RESULT result street
    ASSIGN at.x PID.x + 2 ;
    ASSIGN at.y PID.y ;
    ASSIGN at.z PID.z + 2 ;
    CALL c0de_paint/cpaint/RENDER_CANVAS
        PARAMETER canvas street ;
        PARAMETER position at ;
END FUNCTION

Building and running it ​

  1. Stand where the street should begin, facing east, and place the trigger block there with the button on it. The trigger block stands in the middle of the road, at its west end.
  2. Set the trigger block to street and build, and press the button.

The houses rise from the ground layer by layer. South of the road, from west to east: the oak house, the brick house, the building site, the façade and the hall. Across the road stands the mirror image of the oak house, and the turned house closes the east end of the road, its door looking back at the trigger block. In front of every door there is a dirt path.

the whole street seen from above its west end: the trigger block in the middle of the road, south of it the oak house, the brick house, the two-block-high building site, the flat façade and the big hall with its gateway, across the road the mirrored oak house, and the turned house across the east end of the road; dirt paths in front of the doors

How it works ​

The house. house draws into its result and doesn't touch the world.

  • The walls are a box with only a side_style from 0, 0, 0 to width - 1, height - 1, depth - 1: walls, a floor and a ceiling of planks, nothing inside.
  • The door is two voxels at x 1 on the north side, z 0: the lower and the upper half. The BLOCK variable still has "facing" "north" from the stairs, so the door looks north, and "hinge" "left" says on which side it hangs.
  • The roof grows layer by layer. In every layer, a box of planks fills the attic from the front row a.z to the back row b.z, then two lines of stairs go over the front and the back row. Then both rows move one voxel inward and one up, until they meet: an odd depth ends with a ridge of planks. The stairs on the north side face south and the ones on the south side north, so both rise towards the ridge.

The street. build keeps the first house in street and adds the others one by one.

  • The brick house. CANVAS_REPLACE draws every oak_planks voxel again with the style bricks: walls, floor, gables and ridge. The stairs and the door stay oak.
  • The building site. CANVAS_MAX gives the highest corner of the house, the top of its ridge. CANVAS_CLEAR unsets everything from layer 2 up to there. Two layers stay: the floor and the walls of the ground floor, with the whole door.
  • The façade. CANVAS_CROP does the opposite: it unsets everything outside the box from 0, 0, 0 to the corner b with b.z 0. Only the front row is left, one block thick, like a film set.
  • The hall. CANVAS_SCALE with 200 % on all three axes makes every voxel 2 × 2 × 2 voxels. Stairs stay stairs and keep their states, so the roof gets steps of two. A door of 4 blocks doesn't work in Minecraft, though: CANVAS_REPLACE draws the door again with STYLE_EMPTY, and the hall gets an open gateway instead.
  • The turned house. CANVAS_ROTATE turns it by 90° about the y axis through 0, 0, 0. +90° turns north to west: the door looks west, and the stairs and the door states turn along. The house now lies at negative z, which doesn't matter – addHouse finds it.
  • The mirror image. CANVAS_MIRROR mirrors at the plane z = -2, in the middle of the road: z 0 becomes -4, so the front faces the front of the oak house across the road. The door faces south now, and its hinge is on the right: a mirrored door opens the other way round. CANVAS_MERGE draws the copy into the street where it is.

Putting a house on its plot. addHouse puts the next house 3 voxels east of the street, whatever was done to it. CANVAS_MAX gives the east end of the street so far, CANVAS_MIN the lowest corner of the house. The offset moves that corner to 2 free voxels after the end of the street, to height 0, and to the row z: 0 for the houses south of the road, - width for the turned house, so that its door lands in the middle of the road. CANVAS_MOVE moves the copy, and CANVAS_MERGE draws it into the street. Merging only changes the voxels the house sets, so the street stays as it is around it.

The paths. paths looks at every voxel of the ground layer of the street with CANVAS_GET. Its id is "" for a voxel that is not set, and minecraft:oak_door for the lower half of a door. The facing of the door tells which way is out: the HASHes dx and dz turn it into a step east or south. One voxel further and one down, DRAW_BLOCK puts a dirt path into the ground. Every door gets its path, whichever way the house was turned.

Building. RENDER_CANVAS puts the voxel 0, 0, 0 of the street 2 blocks east and 2 blocks south of the trigger block. The road between the two rows is 3 blocks wide: z -1 to -3 in the canvas.

The program runner builds the street with houses of 3 × 3 × 3 blocks and compares every block with a list – the doors and stairs included, with all their states.

Try it yourself ​

  1. Make the other side of the street the mirror image of the whole row, not only of the first house.
Solution

Move the part with CANVAS_MIRROR up, right before the turned house, and mirror the street instead of the house: ASSIGN copy street ;. Now the brick house, the building site, the façade and the hall get their mirror images too. The turned house comes afterwards, so it isn't mirrored onto itself, and paths finds the new doors without a change, because it reads their facing.

  1. Give the brick house a brick roof: replace "oak_stairs" with a style of "brick_stairs". Do the steps of the roof still face the right way?
Solution

Yes. A second CANVAS_REPLACE after the planks, with a STYLE_SOLID of brick_stairs without states, is enough: CANVAS_REPLACE finds the stairs whatever their states are, and the new block keeps the states of the old one that it has as well – facing, half and shape – so every brick stair lies like the oak stair before it. Only a state that the style sets itself wins: a style of brick_stairs with facing "north" would turn all steps north.

  1. Add a tall tower house: scale a copy only upwards, with x 100, y 200 and z 100. What has to happen to the door?
Solution

The same as in the hall: the door becomes two lower and two upper halves, which Minecraft doesn't accept. Replace "oak_door" with STYLE_EMPTY after scaling, and the tower gets a doorway 1 block wide and 4 high. Add it with addHouse, like the hall.

Read more ​