Diese Seite auf Deutsch

Game time and real time ​

A ball that bounces across a display, and a display that shows the day and the clock of the Minecraft world: sunrise, day, sunset or night.

Functions: SLEEP and TICKS of c0de/minecraft.

What you need

For the animation: a trigger block with a button and a display block; optionally a variable item delay of type INTEGER. For day and night: a timer block and a second display block; to try phase alone, a variable item ticks of type INTEGER and two uninitialized items for the trigger block. For the exercises: one more uninitialized item.

The idea ​

The other recipes of c0de/time read the real time, the clock of the server. Minecraft has a second clock, the game time, counted in ticks. When the server keeps up, there are 20 ticks in a second, and a Minecraft day of 24 000 ticks lasts 20 minutes.

  • SLEEP pauses a program for a number of ticks, 1 to 72 000 (an hour). It is how a program waits: for an animation, for a door to close, for the next try.
  • TICKS returns the game time of the world, the ticks since it was created. The remainder \ 24000 is the time of day: 0 is sunrise at 6:00, 6000 noon, 12 000 sunset and 18 000 midnight.

An animation ​

c0de
PROGRAM bouncer
NOOP
INCLUDE c0de/time
NOOP
FUNCTION bounce
    PARAMETER rounds INTEGER 3
    PARAMETER delay INTEGER 2
BEGIN
    CALL CLS
    FOR round INTEGER 1 ; rounds ;
    BEGIN
        FOR column INTEGER 0 ; 14 ;
        BEGIN
            CALL LOCATE
                PARAMETER row 3 ;
                PARAMETER column column ;
            CALL PRINT
                PARAMETER value " o" ;
            CALL c0de/time/SLEEP
                PARAMETER ticks delay ;
        END FOR
        FOR column INTEGER 14 ; 0 ; -1 ;
        BEGIN
            CALL LOCATE
                PARAMETER row 3 ;
                PARAMETER column column ;
            CALL PRINT
                PARAMETER value "o " ;
            CALL c0de/time/SLEEP
                PARAMETER ticks delay ;
        END FOR
    END FOR
END FUNCTION

Bind a display block to the program bouncer and the function bounce, set the trigger block to the same and press its button. A ball runs across row 3 of the display and back, three times.

a display block with the ball "o" in the middle of row 3, the trigger block with its button in front of it

How it works ​

  • One frame, one SLEEP. Each pass of the inner loops draws one frame and then waits delay ticks.
  • Drawing over the old ball. On the way right, PRINT writes " o": the space wipes out the ball of the last frame, one column to the left. On the way back, "o " wipes out the one to the right. No CLS, no flicker.
  • The lines count too. The cursor needs a tick for every line, so a frame takes about four ticks plus the SLEEP. A program never runs faster than one line per tick.
  • Parameters with default values. rounds and delay have default values, so the trigger block needs no items. Put an INTEGER variable item delay with 5 into it for a slow ball. With 0, SLEEP raises INVALID_ARGUMENT and the process crashes.

Day and night ​

c0de
PROGRAM daynight
NOOP
INCLUDE c0de/minecraft
INCLUDE c0de/math
INCLUDE c0de/strings
NOOP
FUNCTION phase
    PARAMETER ticks INTEGER
    RESULT day INTEGER
    RESULT clock STRING
    RESULT text STRING
BEGIN
    VARIABLE time INTEGER
    VARIABLE hour INTEGER
    VARIABLE minute INTEGER
    VARIABLE digits STRING
    CALL c0de/math/DIV
        PARAMETER dividend ticks ;
        PARAMETER divisor 24000 ;
        RESULT result day
    ASSIGN day day + 1 ;
    ASSIGN time ticks \ 24000 ;
    CALL c0de/math/DIV
        PARAMETER dividend time ;
        PARAMETER divisor 1000 ;
        RESULT result hour
    ASSIGN hour (hour + 6) \ 24 ;
    CALL c0de/math/DIV
        PARAMETER dividend (time \ 1000) * 60 ;
        PARAMETER divisor 1000 ;
        RESULT result minute
    CALL c0de/strings/FORMAT
        PARAMETER value hour ;
        PARAMETER format "%02d" ;
        RESULT result clock
    CALL c0de/strings/FORMAT
        PARAMETER value minute ;
        PARAMETER format ":%02d" ;
        RESULT result digits
    ASSIGN clock clock ⊕ digits ;
    ASSIGN text "Day" ;
    IF time ≥ 12000 ;
    BEGIN
        ASSIGN text "Sunset" ;
    END IF
    IF time ≥ 13000 ;
    BEGIN
        ASSIGN text "Night" ;
    END IF
    IF time ≥ 23000 ;
    BEGIN
        ASSIGN text "Sunrise" ;
    END IF
END FUNCTION
NOOP
FUNCTION show
BEGIN
    VARIABLE ticks INTEGER
    VARIABLE day INTEGER
    VARIABLE clock STRING
    VARIABLE text STRING
    CALL c0de/minecraft/TICKS
        RESULT result ticks
    CALL phase
        PARAMETER ticks ticks ;
        RESULT day day
        RESULT clock clock
        RESULT text text
    CALL CLS
    CALL PRINT
        PARAMETER value "Day " ⊕ day ;
    CALL PRINT
        PARAMETER value clock ;
    CALL PRINT
        PARAMETER value text ;
END FUNCTION

Bind the second display block to daynight / show, set the timer block to the same and an interval of 100 ticks. The display shows the number of the Minecraft day, the clock of the world and what the sky is doing:

Day 3
18:30
Sunset

How it works ​

  • From ticks to a clock. A Minecraft hour has 1000 ticks, and tick 0 is 6:00 in the morning. So the hour is the whole thousands plus 6, and the minute the rest of a thousand ticks times 60 / 1000.
  • The day count. Every 24 000 ticks a new day starts; phase counts from day 1.
  • The phases are compared from the earliest to the latest: Day, then Sunset from 12 000, Night from 13 000 and Sunrise from 23 000. A later IF overwrites an earlier one.
  • TICKS is not the time of day. TICKS counts every tick of the world. Sleeping in a bed or /time set change the sun, but not TICKS. In a world where somebody has slept, the display shows the wrong phase; in a world where nobody sleeps, it is right.
  • TICKS needs the game. Programs with c0de/minecraft only run in Minecraft, so phase takes the ticks as a parameter: try it with a trigger block set to phase, an INTEGER variable item ticks with 12 500 and two uninitialized items (the parameter item makes room for the third result). The results are 1, 18:30 and Sunset.

Try it yourself ​

  1. Set delay to 1. Why is the ball not twice as fast as with 2?
Solution

A frame takes about four ticks for the lines of the loop and delay ticks of SLEEP: six ticks with 2, five ticks with 1. Only the SLEEP gets shorter. For a faster ball, move it two columns per frame.

  1. SLEEP 20 is a second only when the server keeps up. Measure how many ticks your world really runs per second: read TICKS and NOW, sleep 200 ticks, read both again.
Solution
c0de
PROGRAM serverspeed
NOOP
INCLUDE c0de/minecraft
INCLUDE c0de/time
INCLUDE c0de/strings
NOOP
FUNCTION measure
    RESULT speed FLOAT
BEGIN
    VARIABLE firstTick INTEGER
    VARIABLE lastTick INTEGER
    VARIABLE firstTime TIME
    VARIABLE lastTime TIME
    VARIABLE text STRING
    CALL c0de/minecraft/TICKS
        RESULT result firstTick
    CALL c0de/time/NOW
        RESULT result firstTime
    CALL c0de/time/SLEEP
        PARAMETER ticks 200 ;
    CALL c0de/minecraft/TICKS
        RESULT result lastTick
    CALL c0de/time/NOW
        RESULT result lastTime
    ASSIGN speed (lastTick - firstTick) / ((lastTime - firstTime) / 1000) ;
    CALL c0de/strings/FORMAT
        PARAMETER value speed ;
        PARAMETER format "%.1f" ;
        RESULT result text
    CALL PRINT
        PARAMETER value "Ticks per second: " ⊕ text ;
END FUNCTION

Set the trigger block of the animation to serverspeed / measure, put in an uninitialized item for the result speed, and bind its display block to serverspeed / measure too. After 200 ticks, the display shows Ticks per second: 20.0, and speed holds the same number.

The ticks between the two readings divided by the seconds between them. A healthy server shows 20.0; a busy one less, and then every SLEEP and every program takes longer in real time.

Read more ​