This function returns all neighbors that share the same position as agentA
. The current agent's state
can be passed in as agentA
, and dummy agents can be used as well.
function behavior(state, context) {// Find neighbors on my positionconst neighbors = context.neighbors();const cohabitators = hash_stdlib.neighborsOnPosition(state, neighbors);// Create an adjacent "agent"let adjPos = state.get("position");adjPos[0] + 1;const adjAgent = { "position": adjPos };// Find the agents adjacent to meconst adjacents = hash_stdlib.neighborsOnPosition({ "position": adjPos }, neighbors);...}
This function returns all neighbors within the specified radii. The current agent's state
can be passed in as agentA
, and dummy agents can be used as well. By default the max and min radii are [1, 0]. z_axis
is false by default, but by passing true you can enable search in a spherical, as opposed to circular radius.
function behavior(state, context) {// Count the number of electrons close to meconst electrons = context.neighbors().filter(n => n.agent_type === "electron");const close_electrons = neighborsInRadius(state, electrons, 2, 0, true).length;...}
This function returns all neighbors located in front of an agent. agentA
must have a "direction" property since "in front" is determined by the plane perpendicular to that vector. colinear
defaults to false, but by passing true the function will only return agents on the same line as the "direction". The current agent's state
can be passed in as agentA
, and dummy agents can be used as well.
function behavior(state, context) {const neighbors = context.neighbors();// Check which of my neighbors I can see "in front" of meconst visibleAgents = hash_stdlib.neighborsInFront(state, neighbors);// Check which agents are in front of one of my neighborsconst neighborFront = hash_stdlib.neighborsInFront(neighbors[0], neighbors);...}
This function returns all neighbors located behind the agent. It functions identically to neighborsInFront but returns agents behind the plane of agentA
. The current agent's state
can be passed in as agentA
, and dummy agents can be used as well.
function behavior(state, context) {const neighbors = context.neighbors();// Check which of my neighbors are tailing meconst tailingAgents = hash_stdlib.neighborsBehind(state, neighbors, true);// Check if my neighbor is being tailedconst neighborTail = hash_stdlib.neighborsBehind(neighbors[0], neighbors, true);const neighborTailed = neighborTail.length > 0;...}