This function returns the distance between two agents, using a specific distance function. You can pass the current agent's `state`

as one of the agents. The different distance functions are:

â€‹Euclidean (default)

â€‹Manhattanâ€‹

â€‹Euclidean Squaredâ€‹

â€‹Chebyshevâ€‹

function behavior(state, context) {const { neighborA, neighborB } = context.neighbors();â€‹// Find the closest of 2 neighbors to youconst distanceToA = hstd.distanceBetween(state, neighborA);const distanceToB = hstd.distanceBetween(state, neighborB);state.closest = distanceToB > distanceToA ? "A" : "B";â€‹// Check if neighbors are closer to each other than to youconst neighborDistance = hstd.distanceBetween(neighborA, neighborB);const selfDistance = state.closest === "A" ? distanceToA : distanceToB;â€‹state.closer_to_neighbors = selfDistance < neighborDistance;}

This function returns the unit vector of the `vec`

array. You can use it to normalize an agent's direction vector after it's modified.

function behavior(state, context) {const dir = state.direction;â€‹// Modify the direction by adding a vector [1, 2, 0]dir[0] += 1;dir[1] += 2;â€‹// Turn it back into a unit vectorstate.direction = hstd.normalizeVector(dir);}

This function returns a random integer position within the bounds of the `topology`

. The Topology should be user-defined in **globals.json** (see Topology). By default`z_plane`

is `false`

and the returned position is in a 2D plane. Pass true to return a position in 3D space.

function behavior(state, context) {// Move to a random positionconst topology = context.globals().topology;const new_pos = hstd.randomPosition(topology);â€‹state.position = new_pos;}