Diese Seite auf Deutsch

Automatic doors ​

A shop door that opens by itself: step on the pressure plate in front of it, and it swings open; two seconds after you have passed, it closes again. And before you go to bed, one button tells you which doors, gates and trapdoors of your base are still open.

Functions: GET_PRESSURE_PLATE, GET_OPEN, SET_OPEN, and SLEEP of c0de/time

What you need

  • for the door: a timer block, a wooden door and two pressure plates
  • for the report: a trigger block with a button on it, and a few more doors, fence gates or trapdoors
  • an empty sheet for each program
  • for the exercises: a light weighted pressure plate (the gold one)
  • no rights for wooden doors, gates and trapdoors. Iron doors and iron trapdoors need allowBlockChanges on a server, or an operator as owner, like in the guarded door.

The idea ​

A pressure plate right next to a door opens it by redstone – but then the door shuts the moment you step off, often in front of your nose. A program can do better: it reads the plates, opens the door, and closes it only when nobody has stepped on a plate for two seconds.

GET_PRESSURE_PLATE reads a plate's signal strength from 0 (nobody) to 15. GET_OPEN tells if a door is open, and SET_OPEN opens or closes it. All three also work for trapdoors and fence gates.

The door ​

c0de
PROGRAM autodoor
NOOP
INCLUDE c0de/minecraft
INCLUDE c0de/time
NOOP
FUNCTION near
    PARAMETER dx INTEGER
    PARAMETER dy INTEGER
    PARAMETER dz INTEGER
    RESULT spot STRUCT c0de/minecraft/POSITION
BEGIN
    ASSIGN spot.x PID.x + dx ;
    ASSIGN spot.y PID.y + dy ;
    ASSIGN spot.z PID.z + dz ;
END FUNCTION
NOOP
FUNCTION someone
    RESULT result BOOLEAN
BEGIN
    VARIABLE plate STRUCT c0de/minecraft/POSITION
    VARIABLE outside INTEGER
    VARIABLE inside INTEGER
    CALL near
        PARAMETER dx 0 ;
        PARAMETER dy 0 ;
        PARAMETER dz -2 ;
        RESULT spot plate
    CALL c0de/minecraft/GET_PRESSURE_PLATE
        PARAMETER position plate ;
        RESULT result outside
    CALL near
        PARAMETER dx 0 ;
        PARAMETER dy 0 ;
        PARAMETER dz -6 ;
        RESULT spot plate
    CALL c0de/minecraft/GET_PRESSURE_PLATE
        PARAMETER position plate ;
        RESULT result inside
    ASSIGN result outside > 0 OR inside > 0 ;
END FUNCTION
NOOP
FUNCTION watch
BEGIN
    VARIABLE door STRUCT c0de/minecraft/POSITION
    VARIABLE isOpen BOOLEAN
    VARIABLE waiting BOOLEAN
    CALL near
        PARAMETER dx 0 ;
        PARAMETER dy 0 ;
        PARAMETER dz -4 ;
        RESULT spot door
    CALL c0de/minecraft/GET_OPEN
        PARAMETER position door ;
        RESULT result isOpen
    CALL someone
        RESULT result waiting
    IF waiting AND NOT isOpen ;
    BEGIN
        CALL c0de/minecraft/SET_OPEN
            PARAMETER position door ;
            PARAMETER open TRUE ;
    END IF
    IF isOpen AND NOT waiting ;
    BEGIN
        CALL c0de/time/SLEEP
            PARAMETER ticks 40 ;
        CALL someone
            RESULT result waiting
        IF NOT waiting ;
        BEGIN
            CALL c0de/minecraft/SET_OPEN
                PARAMETER position door ;
                PARAMETER open FALSE ;
        END IF
    END IF
END FUNCTION

Building and running it ​

Stand south of the place for the timer block and look north.

WhatWhere, seen from the timer block
the wooden doorin a wall 4 blocks north (lower half at the timer's height)
the outside plate2 blocks north, in front of the door
the inside plate6 blocks north, behind the door

Between each plate and the door, one block stays free: a plate that touches the door would open it by redstone itself. Set the timer block to autodoor and watch, with an interval of 10 ticks. Walk over the plate: the door opens before you reach it and closes behind you.

the open wooden door seen from outside, the player on the outside plate two blocks in front of it, the inside plate visible through the doorway and the timer block in the foreground

How it works ​

  • someone reads both plates and gives TRUE if one of them has a signal. Because the program needs this twice, it is a function of its own.
  • Opening. If somebody is on a plate and the door is closed, SET_OPEN opens it. GET_OPEN avoids opening a door that is already open – and it tells the program where it is: door open, nobody on a plate means it's time to close.
  • The delay. Then the run sleeps 40 ticks, two seconds, and looks at the plates again. Only if they are still empty, the door closes. If somebody stepped on a plate meanwhile, the door stays open, and the next run starts over.
  • One run at a time. While a run sleeps, the timer block starts no new run. So there is never a second run that closes the door while the first is waiting.

The report ​

The second program looks at a list of doors and writes the open ones into the chat. Three arrays describe the doors: a name, and the distance east (dx), up (dy) and south (dz) from the trigger block. Element i of each array belongs to the same door.

c0de
PROGRAM doorreport
NOOP
INCLUDE c0de/minecraft
NOOP
VARIABLE names ARRAY STRING
BEGIN
    "front door" "garden gate" "cellar trapdoor"
END VARIABLE
VARIABLE dx ARRAY INTEGER
BEGIN
    0 8 3
END VARIABLE
VARIABLE dy ARRAY INTEGER
BEGIN
    0 0 -1
END VARIABLE
VARIABLE dz ARRAY INTEGER
BEGIN
    -4 -4 -8
END VARIABLE
NOOP
FUNCTION report
BEGIN
    VARIABLE spot STRUCT c0de/minecraft/POSITION
    VARIABLE count INTEGER
    VARIABLE isOpen BOOLEAN
    VARIABLE openDoors STRING
    CALL LEN
        PARAMETER value names ;
        RESULT result count
    FOR i INTEGER 0 ; count - 1 ;
    BEGIN
        ASSIGN spot.x PID.x + dx[i] ;
        ASSIGN spot.y PID.y + dy[i] ;
        ASSIGN spot.z PID.z + dz[i] ;
        CALL c0de/minecraft/GET_OPEN
            PARAMETER position spot ;
            RESULT result isOpen
        IF isOpen ;
        BEGIN
            IF openDoors ≠ "" ;
            BEGIN
                ASSIGN openDoors openDoors ⊕ ", " ;
            END IF
            ASSIGN openDoors openDoors ⊕ names[i] ;
        END IF
    END FOR
    IF openDoors = "" ;
    BEGIN
        CALL c0de/minecraft/SAY
            PARAMETER message "All doors are closed." ;
    END
    ELSE
    BEGIN
        CALL c0de/minecraft/SAY
            PARAMETER message "Open: " ⊕ openDoors ;
    END IF
END FUNCTION

The numbers in the arrays are an example; measure your own. Press F3 and look at a door: "Targeted Block" shows its x, y and z. Subtract the x, y and z of the trigger block, and you have dx, dy and dz. For a door, take the lower half. Put the trigger block with its button by your bed, set it to doorreport and report, and press the button. The chat says Open: front door, cellar trapdoor or All doors are closed.

  • The position of each door is computed right in the loop: spot.x becomes PID.x + dx[i], and so on.
  • Joining names. openDoors starts empty. Before each further name, the loop adds ", ", so the list has commas only between the names.

Try it yourself ​

  1. Don't just report the open doors – close them, too, and say which ones you closed.
Solution
c0de
PROGRAM doorcloser
NOOP
INCLUDE c0de/minecraft
NOOP
VARIABLE names ARRAY STRING
BEGIN
    "front door" "garden gate" "cellar trapdoor"
END VARIABLE
VARIABLE dx ARRAY INTEGER
BEGIN
    0 8 3
END VARIABLE
VARIABLE dy ARRAY INTEGER
BEGIN
    0 0 -1
END VARIABLE
VARIABLE dz ARRAY INTEGER
BEGIN
    -4 -4 -8
END VARIABLE
NOOP
FUNCTION closeAll
BEGIN
    VARIABLE spot STRUCT c0de/minecraft/POSITION
    VARIABLE count INTEGER
    VARIABLE isOpen BOOLEAN
    VARIABLE closed STRING
    CALL LEN
        PARAMETER value names ;
        RESULT result count
    FOR i INTEGER 0 ; count - 1 ;
    BEGIN
        ASSIGN spot.x PID.x + dx[i] ;
        ASSIGN spot.y PID.y + dy[i] ;
        ASSIGN spot.z PID.z + dz[i] ;
        CALL c0de/minecraft/GET_OPEN
            PARAMETER position spot ;
            RESULT result isOpen
        IF isOpen ;
        BEGIN
            CALL c0de/minecraft/SET_OPEN
                PARAMETER position spot ;
                PARAMETER open FALSE ;
            IF closed ≠ "" ;
            BEGIN
                ASSIGN closed closed ⊕ ", " ;
            END IF
            ASSIGN closed closed ⊕ names[i] ;
        END IF
    END FOR
    IF closed = "" ;
    BEGIN
        CALL c0de/minecraft/SAY
            PARAMETER message "All doors were closed." ;
    END
    ELSE
    BEGIN
        CALL c0de/minecraft/SAY
            PARAMETER message "Closed: " ⊕ closed ;
    END IF
END FUNCTION

SET_OPEN with open FALSE closes each door that GET_OPEN finds open. Closing a closed door would do no harm, but this way the program knows what it did.

  1. Use a light weighted pressure plate (the gold one) outside: its signal grows with the number of entities on it. Let the door open only when at least two stand on it, for example you and your horse.
Solution

someone gets ASSIGN result outside ≥ 2 OR inside > 0 ;. A wooden or stone plate always gives 15 or 0, a weighted plate gives more the more there is on it.

Read more ​