Diese Seite auf Deutsch

Distance and direction ​

A finder at the gate of your base: press the button, and the chat tells you where every player nearby is – "Alex is 7 blocks away, north-east, higher up". The program measures the straight distance, the distance on foot, and the direction as one step along each axis.

Functions: SQR, ABS and SGN, with ROUND of c0de/math and FIND_ENTITIES and SAY of c0de/minecraft

What you need

A trigger block with a button. To try the calculation alone: the variable items dx, dy and dz of type FLOAT and six uninitialized items. For the finder in the world: nothing else, and a friend (or a second account) to find.

The idea ​

Minecraft counts x towards the east, z towards the south and y upwards. The difference of two positions, dx, dy and dz, says everything about the way between them:

  • The straight distance is the diagonal of a box with the sides dx, dy and dz. Pythagoras says its square is dx² + dy² + dz², and SQR, the square root, turns that back into blocks.
  • The distance on foot, when you can only walk along the axes, adds the sizes of the three sides. A side is a size even if it is negative – 6 blocks to the north are dz = -6 – so ABS drops the sign first.
  • The direction is the sign of each side: SGN gives -1, 0 or 1. Those three numbers are also one step from one block towards the other: x + stepX, y + stepY, z + stepZ.

The calculation ​

First the calculation alone, so you can test it with variable items:

c0de
PROGRAM compass
NOOP
INCLUDE c0de/math
NOOP
FUNCTION measure
    PARAMETER dx FLOAT
    PARAMETER dy FLOAT
    PARAMETER dz FLOAT
    RESULT distance FLOAT
    RESULT walk FLOAT
    RESULT stepX INTEGER
    RESULT stepY INTEGER
    RESULT stepZ INTEGER
    RESULT direction STRING
BEGIN
    VARIABLE size FLOAT
    CALL c0de/math/SQR
        PARAMETER value dx ^ 2 + dy ^ 2 + dz ^ 2 ;
        RESULT result distance
    CALL c0de/math/ABS
        PARAMETER value dx ;
        RESULT result size
    ASSIGN walk size ;
    CALL c0de/math/ABS
        PARAMETER value dy ;
        RESULT result size
    ASSIGN walk walk + size ;
    CALL c0de/math/ABS
        PARAMETER value dz ;
        RESULT result size
    ASSIGN walk walk + size ;
    CALL c0de/math/SGN
        PARAMETER value dx ;
        RESULT result stepX
    CALL c0de/math/SGN
        PARAMETER value dy ;
        RESULT result stepY
    CALL c0de/math/SGN
        PARAMETER value dz ;
        RESULT result stepZ
    IF stepZ = -1 ;
    BEGIN
        ASSIGN direction "north" ;
    END IF
    IF stepZ = 1 ;
    BEGIN
        ASSIGN direction "south" ;
    END IF
    IF stepX ≠ 0 AND stepZ ≠ 0 ;
    BEGIN
        ASSIGN direction direction ⊕ "-" ;
    END IF
    IF stepX = 1 ;
    BEGIN
        ASSIGN direction direction ⊕ "east" ;
    END IF
    IF stepX = -1 ;
    BEGIN
        ASSIGN direction direction ⊕ "west" ;
    END IF
    IF direction = "" ;
    BEGIN
        ASSIGN direction "above or below" ;
    END IF
END FUNCTION

With dx = 3, dy = 2 and dz = -6, the results are distance = 7.0 (the square root of 9 + 4 + 36), walk = 11.0, the steps 1, 1 and -1 and direction = north-east. With dx = -4.5 and the others 0, it is 4.5, 4.5 and west.

The finder in the world ​

The function measure goes unchanged into a program with c0de/minecraft. find looks for players around the trigger block and asks measure about each of them:

c0de
PROGRAM playerfinder
NOOP
INCLUDE c0de/math
INCLUDE c0de/minecraft
NOOP
FUNCTION measure
    PARAMETER dx FLOAT
    PARAMETER dy FLOAT
    PARAMETER dz FLOAT
    RESULT distance FLOAT
    RESULT walk FLOAT
    RESULT stepX INTEGER
    RESULT stepY INTEGER
    RESULT stepZ INTEGER
    RESULT direction STRING
BEGIN
    VARIABLE size FLOAT
    CALL c0de/math/SQR
        PARAMETER value dx ^ 2 + dy ^ 2 + dz ^ 2 ;
        RESULT result distance
    CALL c0de/math/ABS
        PARAMETER value dx ;
        RESULT result size
    ASSIGN walk size ;
    CALL c0de/math/ABS
        PARAMETER value dy ;
        RESULT result size
    ASSIGN walk walk + size ;
    CALL c0de/math/ABS
        PARAMETER value dz ;
        RESULT result size
    ASSIGN walk walk + size ;
    CALL c0de/math/SGN
        PARAMETER value dx ;
        RESULT result stepX
    CALL c0de/math/SGN
        PARAMETER value dy ;
        RESULT result stepY
    CALL c0de/math/SGN
        PARAMETER value dz ;
        RESULT result stepZ
    IF stepZ = -1 ;
    BEGIN
        ASSIGN direction "north" ;
    END IF
    IF stepZ = 1 ;
    BEGIN
        ASSIGN direction "south" ;
    END IF
    IF stepX ≠ 0 AND stepZ ≠ 0 ;
    BEGIN
        ASSIGN direction direction ⊕ "-" ;
    END IF
    IF stepX = 1 ;
    BEGIN
        ASSIGN direction direction ⊕ "east" ;
    END IF
    IF stepX = -1 ;
    BEGIN
        ASSIGN direction direction ⊕ "west" ;
    END IF
    IF direction = "" ;
    BEGIN
        ASSIGN direction "above or below" ;
    END IF
END FUNCTION
NOOP
FUNCTION find
    PARAMETER radius INTEGER 32
BEGIN
    VARIABLE here STRUCT c0de/minecraft/POSITION
    VARIABLE players ARRAY STRUCT c0de/minecraft/ENTITY
    VARIABLE distance FLOAT
    VARIABLE blocks INTEGER
    VARIABLE stepY INTEGER
    VARIABLE direction STRING
    VARIABLE height STRING
    ASSIGN here.x PID.x ;
    ASSIGN here.y PID.y ;
    ASSIGN here.z PID.z ;
    CALL c0de/minecraft/FIND_ENTITIES
        PARAMETER position here ;
        PARAMETER radius radius ;
        PARAMETER type "minecraft:player" ;
        RESULT result players
    EACH player players
    BEGIN
        CALL measure
            PARAMETER dx player.position.x - here.x ;
            PARAMETER dy player.position.y - here.y ;
            PARAMETER dz player.position.z - here.z ;
            RESULT distance distance
            RESULT stepY stepY
            RESULT direction direction
        CALL c0de/math/ROUND
            PARAMETER value distance ;
            RESULT result blocks
        ASSIGN height "" ;
        IF stepY = 1 ;
        BEGIN
            ASSIGN height ", higher up" ;
        END IF
        IF stepY = -1 ;
        BEGIN
            ASSIGN height ", further down" ;
        END IF
        CALL c0de/minecraft/SAY
            PARAMETER message player.name ⊕ " is " ⊕ blocks ⊕ " blocks away, " ⊕ direction ⊕ height ;
    END EACH
END FUNCTION

Building and running it ​

  1. Set the trigger block to the program playerfinder and the function find. It needs no items: radius has the default 32.
  2. Ask your friend to walk away from the trigger block, but not more than 32 blocks, and maybe climb a little. Then press the button yourself. (Or swap: you walk away, and your friend presses it.)
  3. The chat shows one line per player within 32 blocks, like [playerfinder/0] Alex is 7 blocks away, north-east, higher up. The player who pressed the button is on the list too, right next to the trigger block.

How it works ​

  • PID.x, PID.y and PID.z are the position of the trigger block. here is a POSITION struct with them, the center of FIND_ENTITIES.
  • The differences go straight into the PARAMETER lines: player.position.x - here.x. measure takes FLOATs, and an INTEGER goes into a FLOAT by itself.
  • dx ^ 2 squares a value; the sum is never negative, so SQR never raises INVALID_ARGUMENT here. Without the square, a negative value would.
  • ABS and SGN together split a number into its size and its sign: size * sign is the number again.
  • The direction text puts north or south first, then east or west, with a dash only if there are both. For a player straight above or below the trigger block, there is no compass direction.
  • RESULT lines pick results. find only needs distance, stepY and direction of measure; the others are thrown away.

Try it yourself ​

  1. A guard zone is a cube, not a ball: a player is inside if they are at most size blocks away along each axis. Write a function inside with ABS.
Solution

Each of the three distances must be at most size. With dx = -8, dy = 3, dz = 10 and the default size 10, inside is TRUE; with dz = -11, it is FALSE.

c0de
PROGRAM guardzone
NOOP
INCLUDE c0de/math
NOOP
FUNCTION inside
    PARAMETER dx FLOAT
    PARAMETER dy FLOAT
    PARAMETER dz FLOAT
    PARAMETER size INTEGER 10
    RESULT inside BOOLEAN
BEGIN
    VARIABLE ax FLOAT
    VARIABLE ay FLOAT
    VARIABLE az FLOAT
    CALL c0de/math/ABS
        PARAMETER value dx ;
        RESULT result ax
    CALL c0de/math/ABS
        PARAMETER value dy ;
        RESULT result ay
    CALL c0de/math/ABS
        PARAMETER value dz ;
        RESULT result az
    ASSIGN inside ax ≤ size AND ay ≤ size AND az ≤ size ;
END FUNCTION
  1. Why is walk (11 blocks) longer than distance (7 blocks) in the example – and when are they the same?
Solution

distance is the straight line through the air, the diagonal; walk goes along the edges of the box, which is always at least as long. They are the same when two of the three differences are 0, like dx = -4.5 alone: then the way is a straight line along one axis.

Read more ​