Skip to content

Add an order ​

An order is a behaviour in your rules' orders: whether a unit accepts it, and what it does as it starts, every tick, and as it ends.

ts
// in src/rules.ts
import { defineIds, type EntityId, type Rules, halt, moveToEntity, moving, type OrderBehavior } from '@v12-engine/engine'

export const UNIT = defineIds('scout', 'caravan')

/** What an escort keeps in its order: where its ward was when it last set out after it */
interface EscortState {
	lastAt?: { x: number; z: number }
}

/** What an escort is given: the unit it walks beside */
interface EscortInit {
	target?: EntityId
}

/** Escort: walk beside another unit until it dies or stops, then stand where it stood */
const escort: OrderBehavior<EscortState, EscortInit> = {
	type: 'escort',
	// a unit that moves, sent to a unit (not a point)
	accepts: (world, entity, init) => entity.has(moving) && init.target !== undefined && !!world.get(init.target),
	start(world, entity, order) {
		const ward = world.get(order.init.target)
		if (ward) moveToEntity(world, entity, ward, 1.5)
		order.state.lastAt = ward ? { ...ward.pos } : undefined
	},
	update(world, entity, order) {
		const ward = world.get(order.init.target)
		if (!ward || !ward.alive) return 'done'
		// the ward moved: after it again (a second between asks, so that the path is not made every tick)
		const last = order.state.lastAt
		const moved = !last || Math.hypot(ward.pos.x - last.x, ward.pos.z - last.z) > 1
		if (moved && world.tick % 20 === entity.id % 20) {
			moveToEntity(world, entity, ward, 1.5)
			order.state.lastAt = { ...ward.pos }
		}
		return 'running'
	},
	end(world, entity) {
		halt(world, entity)
	}
}

export const rules: Rules = {
	id: 'my-game',
	name: 'My game',
	units: [
		{ id: UNIT.scout, name: 'Scout', kind: 'unit', radius: 0.4, sight: 10, stats: { health: 100, moveSpeed: 5 } },
		{ id: UNIT.caravan, name: 'Caravan', kind: 'unit', radius: 0.6, sight: 6, stats: { health: 300, moveSpeed: 2.5 } }
	],
	orders: [escort],
	victory: null
}

Done: unit.order('escort', { target: caravan.id }) makes a scout follow the caravan. Shift-queueing works like any order.

Issue it ​

ts
unit.order('escort', { target: caravan.id })                    // now
unit.order('escort', { target: caravan.id }, { queue: true })   // after its current orders
world.issue({ type: 'order', entities: [a.id, b.id], order: { type: 'escort', target: caravan.id } }) // many units

Type it ​

Add it to OrderStates and OrderInits so registry.order('escort') and OrderInit<'escort'> are typed:

ts
declare module '@v12-engine/engine' {
	interface OrderStates { escort: EscortState }
	interface OrderInits { escort: EscortInit }
}

Reference ​

PartWhat it does
acceptsReturns whether this unit takes the order. Check init: it comes from a player.
startRuns once as the order begins.
updateRuns every tick. Returns running, done or failed.
endRuns once, told how it ended.
order.statePlain data, saved with the unit, so the order resumes after a load. Make every field optional.
order.initWhat it was given. Make every field optional.

Write behaviours with moveToEntity, halt and the engine's own orders (move, attack, hold, patrol, gather, build). The Code panel's New with code writes an order stub. See Orders and commands for the engine's orders and Economy for commands.