Skip to content
ASArcaneMP Scripting APIScripting reference
GuideMaintainer authored

Zones

Mark out areas of the world on the server and get an event when players or other entities come in or go out.

A zone is an area of the world that the server watches. It raises an event when a player, or another kind of entity you ask for, comes in or goes out. A zone does nothing else by itself: a safe zone, an arena or a shop counter is your handlers reacting to those events.

Zone.create(key, shape, options) creates a zone under a key, or replaces the one there, and returns it:

server/main.js
const square = Zone.create("market:square", {
type: "cylinder",
center: { x: 12000, y: -4500, z: 200 },
radius: 4000,
height: 1500,
});

Shapes are in world centimetres:

type Fields
"sphere" center, radius
"box" center, size (full lengths along each axis), yaw in degrees
"cylinder" center (the middle of its base), radius, height
"polygon" points, 3 to 64 corners as { x, y } whose edges do not cross, and minZ, maxZ

The options are:

  • virtualWorld and map: the world it watches, and the map that world is in by its areaId. By default, world 0 of Overland. A zone matches entities in exactly that world of that map. zone.virtualWorld and zone.map read them back, with "" for Overland.
  • track: which entities it notices, from "player", "npc", "beast", "worldObject" and "character", by default only players.

A malformed shape or option throws. The server holds at most 4096 zones.

Keys are shared by every resource, and creating a zone under a key in use replaces that zone, so start yours with your resource’s name. Zone.get(key) returns a zone or undefined, Zone.getAll() lists them, and Zone.destroy(key) or zone.destroy() removes one.

Players cannot see a zone. To show them where one lies, send zone.getShape() to their clients and draw it with Shapes.

zoneEnter fires with the zone and the entity that came in. zoneExit fires with the zone, the entity, a reason and the entity’s id: the reason is "left" when it moved out, or out of the zone’s world, and "gone" when it no longer exists, such as a player who disconnected, and then the entity is null. Every resource receives every zone’s events, so check the key:

server/main.js
Events.on("zoneEnter", (zone, entity) => {
if (zone.key === "market:square" && entity instanceof Player) entity.sendChat("Welcome to the market.");
});
Events.on("zoneExit", (zone, entity, reason) => {
if (zone.key === "market:square" && reason === "left" && entity instanceof Player) entity.sendChat("Come again.");
});

Zones are checked ten times a second. An entity that moves through a zone between two checks still enters and leaves it, but one that moves more than 50 m between checks, such as a teleport, only counts where it lands.

zone.getPlayers() and zone.getEntities() list what was inside at the last check, and zone.contains(point) tests a point, ignoring worlds.

A zone that turns PvP off inside it pairs the zone’s events with a PvP policy:

server/main.js
const SAFE = "market:safe";
const inSafeZone = new Set();
Zone.create(SAFE, { type: "sphere", center: { x: 12000, y: -4500, z: 200 }, radius: 3000 });
Events.on("zoneEnter", (zone, entity) => {
if (zone.key === SAFE && entity) inSafeZone.add(entity.id);
});
Events.on("zoneExit", (zone, entity, reason, entityId) => {
if (zone.key === SAFE) inSafeZone.delete(entityId);
});
Pvp.setPolicy((hit) => (inSafeZone.has(hit.casterId) || inSafeZone.has(hit.victimId) ? false : undefined));

The positions a zone tests are those players’ games report, so treat what a zone tells you about a player like any other position they send.

A resource’s zones are destroyed when it stops, without zoneExit. Zone ids are never reused while the server runs.