Querying the world
Find what's on the map by where it is and what it is: enemies near a hero, wounded allies in an area, the three nearest towers, a random marked unit. You can ask from code with world.query, or from ability data with an effect's where, sort and limit.
Quick start
import type { System } from '@v12-engine/engine'
// every second, each tower of player 1 burns the most hurt enemy unit it can see within 9
export const towersBurn: System = {
name: 'towersBurn',
every: 30,
update(world) {
for (const tower of world.query({ owner: 1, type: 'tower' })) {
const [target] = world.query({ near: tower, radius: 9, is: 'enemy', kinds: ['unit'], visibleTo: 1, sort: 'lowestHealthShare', limit: 1 })
if (target) world.damage(target, 25, tower)
}
}
}world.query returns a new list. Every option you give must hold for an entity to be in it.
Common tasks
Find what's around a unit or a point
world.query({ near: hero, radius: 8, is: 'enemy' }) // enemies of the hero's owner within 8
world.query({ near: { x: 40, z: 12 }, radius: 5, of: 1, is: 'ally' }) // a point has no owner: say whose eyes with `of`
world.query({ near: hero, radius: 8, is: 'ally', except: hero }) // allies, the hero left outradius reaches the edge of each entity, the same way an effect's radius does: a unit whose center is just outside still counts if its body is inside.
Filter by what it is
The query takes every field of a UnitCondition, the same conditions an if effect checks:
world.query({ owner: 2, type: ['footman', 'knight'] }) // one owner or a list; one type or a list
world.query({ kinds: ['building'] }) // unit, building, resource
world.query({ layers: ['air'] }) // flying, or on the ground
world.query({ owner: 2, healthBelow: 0.3 }) // under 30% of their max health
world.query({ buff: 'marked', notTags: ['mechanical'], hero: false })
world.query({ area: 'td.gate' }) // inside an area (`defineArea`, a region of the map)
world.query({ owner: 2, visibleTo: 1 }) // what player 1's team sees now: fog and invisibility countAnything else: code
world.query({ near: hero, radius: 12, is: 'enemy', where: (unit) => unit.orders[0]?.type === 'cast' }) // enemies channellingwhere runs last, only on entities that passed every other option.
Sort and take a few
world.query({ near: hero, radius: 10, is: 'enemy', sort: 'nearest', limit: 3 })
world.query({ owner: 1, kinds: ['unit'], sort: 'lowestHealthShare', limit: 1 }) // the most hurt
world.query({ owner: 2, sort: 'random', limit: 1 }) // the same pick every run of the same seed
world.query({ owner: 2, sort: (a, b) => b.mana - a.mana }) // your own orderWithout sort, entities come in the order they came into the world. Ties always break the same way, so every machine in a multiplayer game gets the same answer.
Pick an ability's targets by condition
Every effect with a radius or shape takes where, sort and limit. They work the same as in a query, applied to what the area reaches:
import type { AbilityDef } from '@v12-engine/engine'
// heals the two most hurt allies around the caster
export const mend: AbilityDef = {
id: 'mend',
name: 'Mend',
target: 'none',
effects: [{ type: 'heal', amount: 100, radius: 10, affects: 'allies', sort: 'lowestHealthShare', limit: 2 }]
}
// strikes one marked enemy at random, two at level 2; the mechanical are spared
export const hunt: AbilityDef = {
id: 'hunt',
name: 'Hunt',
target: 'none',
maxLevel: 2,
effects: [
{
type: 'damage',
amount: 40,
radius: 10,
affects: 'enemies',
where: { buff: 'marked', notTags: ['mechanical'] },
sort: 'random',
limit: [1, 2]
}
]
}whereis aUnitCondition, or code:where: (context, unit) => unit.health === unit.maxHealth. Without an area, it is checked against the effect's single target.limitalone takes the nearest to the middle of the area. That is where it landed, or the caster for a cone, a line or an arc.- Targets are chosen before the
beforeEffecthook runs. A spell shield on a chosen unit blocks the blow; the effect doesn't move on to someone else.
In an effect of your own
effectTargets(context, effect, 'enemies') gives what an effect may reach, with every filter applied, including where, sort and limit. To order a list of your own the same way, use sortTargets(world, list, 'nearest', point) (Write your own effect).
Reference
WorldQuery
| Option | Keeps |
|---|---|
near | A point or a unit: what radius and nearest measure from |
radius | Within this distance of near, to each one's edge |
area | Inside this area |
of | Whose eyes is uses: the owner of near, when near is a unit |
owner | Owned by this player, or one of these |
type | Of this unit type, or one of these |
kinds | Of these kinds (unit, building, resource) |
layers | On the ground or in the air |
visibleTo | Seen now by this player's team |
except | Not this entity, or these |
dead | The dead too (true), or the dead alone ('only'). The living alone by default |
hidden | Entities out of the world for now too (a worker in a mine, a unit in a transport) |
buff, noBuff, healthBelow, healthAbove, hero, tags, notTags, is | As in a UnitCondition |
where | (entity, world) => boolean, run last |
sort | A TargetOrder, or (a, b) => number |
limit | At most this many, the first in the sort order |
world.query throws if it is asked for is with no player to ask it of, or sorted by distance with no near.
TargetOrder
| Order | First |
|---|---|
nearest | Closest to near (to the edge) |
farthest | Farthest from it |
lowestHealth | The least health |
highestHealth | The most health |
lowestHealthShare | The least health as a share of its max: the most hurt |
highestHealthShare | The most as a share: the least hurt |
random | Drawn from world.rng: the same every run of a seed |
Effect fields
| Field | Type | What it does |
|---|---|---|
where | UnitCondition or UnitConditionCheck | Hits only what it holds for |
sort | TargetOrder or (a, b) => number | Which of what the area reaches come first (nearest with a limit) |
limit | number, or one per level | Hits at most this many |
checkRules warns of a limit or sort on an effect with no area, of a limit under 1, and of a where that names a buff that doesn't exist. The Data panel shows all three fields on every effect.
Other ways to find things
| Call | Gives |
|---|---|
world.get(id) | One entity by id |
world.named(name) | One named on the map |
world.find({ owner, type, area, dead }) | A short form of query, for scripts |
world.areas.inside(id) | Who is in an area now: kept up as they come and go, cheap every tick |
world.index.ownedBy(player), world.index.with(component) | Sets kept up as the world changes |
world.spatial.query(x, z, radius) | The raw grid lookup: a rough answer, reused list; check distances yourself |
world.query starts from the smallest set it can (the grid around near, an area, one owner's units) and checks the rest against each entity. For code that runs every tick over many units, it's fine. For the tightest loops, use the kept-up sets above. packages/engine/tests/worldQuery.test.ts covers every option.