Skip to content

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 ​

ts
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 ​

ts
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 out

radius 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:

ts
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 count

Anything else: code ​

ts
world.query({ near: hero, radius: 12, is: 'enemy', where: (unit) => unit.orders[0]?.type === 'cast' }) // enemies channelling

where runs last, only on entities that passed every other option.

Sort and take a few ​

ts
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 order

Without 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:

ts
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]
		}
	]
}
  • where is a UnitCondition, or code: where: (context, unit) => unit.health === unit.maxHealth. Without an area, it is checked against the effect's single target.
  • limit alone 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 beforeEffect hook 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 ​

OptionKeeps
nearA point or a unit: what radius and nearest measure from
radiusWithin this distance of near, to each one's edge
areaInside this area
ofWhose eyes is uses: the owner of near, when near is a unit
ownerOwned by this player, or one of these
typeOf this unit type, or one of these
kindsOf these kinds (unit, building, resource)
layersOn the ground or in the air
visibleToSeen now by this player's team
exceptNot this entity, or these
deadThe dead too (true), or the dead alone ('only'). The living alone by default
hiddenEntities out of the world for now too (a worker in a mine, a unit in a transport)
buff, noBuff, healthBelow, healthAbove, hero, tags, notTags, isAs in a UnitCondition
where(entity, world) => boolean, run last
sortA TargetOrder, or (a, b) => number
limitAt 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 ​

OrderFirst
nearestClosest to near (to the edge)
farthestFarthest from it
lowestHealthThe least health
highestHealthThe most health
lowestHealthShareThe least health as a share of its max: the most hurt
highestHealthShareThe most as a share: the least hurt
randomDrawn from world.rng: the same every run of a seed

Effect fields ​

FieldTypeWhat it does
whereUnitCondition or UnitConditionCheckHits only what it holds for
sortTargetOrder or (a, b) => numberWhich of what the area reaches come first (nearest with a limit)
limitnumber, or one per levelHits 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 ​

CallGives
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.