Objective recipes
Every classic campaign objective, one recipe each: destroy a base, kill a boss, keep the hero alive, survive, reach a place, escort, rescue, find an item, and the optional, timed and secret ones. Each recipe is an objective, the trigger that moves it, and what fails it.
const razeTheCamp = defineObjective({ id: 'm.raze', text: 'quest.raze', detail: 'quest.raze.detail' }) // main
const findTheRelic = defineObjective({ id: 'm.relic', text: 'quest.relic', required: false }) // optional
world.objectives.add(PLAYER, razeTheCamp, { progress: { value: 0, max: 6 } }) // "New objective: Raze the camp" 0/6
world.objectives.progress(PLAYER, razeTheCamp.id, 4) // the bar at 4/6
world.objectives.complete(PLAYER, razeTheCamp.id) // "Objective done"
world.objectives.fail(PLAYER, findTheRelic.id) // "Objective failed"With victory: objectiveVictory in the rules, completing every main quest wins and failing any main quest loses. The recipes below assume it, with PLAYER the human and ENEMY the computer. Each listener and sequence goes in the rules' listeners and sequences.
Destroy the enemy base
Every building of the enemy, counted down on the bar.
const buildingsOf = (world: World, owner: PlayerId) => world.find({ owner }).filter((entity) => entity.def.kind === 'building')
// at the start
world.objectives.add(PLAYER, razeTheCamp, { progress: { value: 0, max: buildingsOf(world, ENEMY).length } })
defineListener({
id: 'm.buildingRazed',
event: entityDied,
run(world, { entity }) {
if (entity.owner !== ENEMY || entity.def.kind !== 'building' || !world.objectives.has(PLAYER, razeTheCamp.id)) return
const { max } = world.objectives.get(PLAYER, razeTheCamp.id).progress!
const left = buildingsOf(world, ENEMY).length
world.objectives.progress(PLAYER, razeTheCamp.id, max - left)
if (left === 0) world.objectives.complete(PLAYER, razeTheCamp.id)
}
})To count only some (the tents, the towers), filter by entity.def.id. To destroy one building ("Destroy the Black Citadel"), name it in the editor and complete on world.nameOf(entity) === 'citadel'.
Kill a boss
const slayTheWarlord = defineObjective({ id: 'm.warlord', text: 'quest.warlord' })
defineSequence({ id: 'm.warlordFalls', steps: [
waitFor(entityDied, (world, { entity }) => world.nameOf(entity) === 'warlord'),
wait(1.5),
cinematic('m.outro', { hold: true }), // the outro before the victory screen
(world) => world.objectives.complete(PLAYER, slayTheWarlord.id)
] })A boss bar for the HUD: Your own HUD.
The hero must survive
const heroLives = defineObjective({ id: 'm.hero', text: 'quest.heroLives' }) // "Kaelen must survive"
defineListener({
id: 'm.heroFell',
event: entityDied,
run(world, { entity }) {
if (world.nameOf(entity) !== 'kaelen' || !world.objectives.has(PLAYER, heroLives.id)) return
if (world.objectives.get(PLAYER, heroLives.id).status === 'open') world.objectives.fail(PLAYER, heroLives.id, 'defeat.heroFell')
}
})Complete it with the mission's last main quest, so it does not stay open. For any of the player's heroes, test entity.def.hero && entity.owner === PLAYER. A game whose heroes come back at an altar (heroesPack) can let the mission go on instead: leave this quest out.
Protect a building or a unit
The same as the hero: a main quest that fails on entityDied of the named building ("The Lighthouse must stand"). To warn the player first, listen for damage with a hook or check its health on a timer:
const checkTheLighthouse = defineTimer({ id: 'm.lighthouse', run: (world) => {
const lighthouse = world.named('lighthouse')
if (lighthouse?.alive && lighthouse.health < lighthouse.maxHealth / 2) world.message(PLAYER, 'msg.lighthouseBurning', 'error')
} })
world.timers.every(5, checkTheLighthouse, {})Survive for a time
A countdown window, and the quest done when it reaches zero.
const holdOut = defineObjective({ id: 'm.holdOut', text: 'quest.holdOut' }) // "Survive until dawn"
const dawn = defineTimer({ id: 'm.dawn', run: (world) => world.objectives.complete(PLAYER, holdOut.id) })
// at the start
world.objectives.add(PLAYER, holdOut)
const id = world.timers.after(600, dawn)
world.timers.show(PLAYER, id, { label: 'timer.dawn' }) // "Dawn: 9:59" at the top of the screenWith waves, move the bar instead of a timer: progress(PLAYER, holdOut.id, wave) as each wave is beaten (the campaign starter's siege).
Reach a place
const reachTheGate = defineObjective({ id: 'm.gate', text: 'quest.gate' })
defineListener({
id: 'm.gateReached',
event: enteredArea,
run(world, { area, entity }) {
if (area !== 'm.gate' || world.nameOf(entity) !== 'kaelen' || !world.objectives.has(PLAYER, reachTheGate.id)) return
world.objectives.complete(PLAYER, reachTheGate.id)
}
})The area is a region of the map the mission adds (world.areas.add({ id: 'm.gate', shape: areaShapeOf({ region: 'gate' }, world.map.regions)! })). For "any unit", test entity.owner === PLAYER; for "the whole army", count world.areas.inside('m.gate').
Before time runs out
const tooLate = defineTimer({ id: 'm.tooLate', run: (world) => {
if (world.objectives.get(PLAYER, reachTheGate.id).status === 'open') world.objectives.fail(PLAYER, reachTheGate.id, 'defeat.tooLate')
} })
world.timers.show(PLAYER, world.timers.after(300, tooLate), { label: 'timer.gate' })Escort a unit
The caravan walks a path of the map; it must arrive, and must not die.
const escortTheCaravan = defineObjective({ id: 'm.caravan', text: 'quest.caravan' })
defineSequence({ id: 'm.caravanWalks', steps: [
(world) => world.named('caravan')?.order('move', world.map.point('town')),
waitFor(enteredArea, (world, { area, entity }) => area === 'm.town' && world.nameOf(entity) === 'caravan'),
(world) => world.objectives.complete(PLAYER, escortTheCaravan.id)
] })
defineListener({
id: 'm.caravanLost',
event: entityDied,
run(world, { entity }) {
if (world.nameOf(entity) === 'caravan' && world.objectives.has(PLAYER, escortTheCaravan.id))
world.objectives.fail(PLAYER, escortTheCaravan.id, 'defeat.caravan')
}
})To make it wait for its guards, order it on only while a unit of the player is near (a timer that checks world.query({ owner: PLAYER, near: caravan, radius: 8 }).length). Ambushes on the way: AI for missions.
Rescue: units, or a whole town
Classic rescuable units are neutral until the player's units reach them; then they join.
const freeThePrisoners = defineObjective({ id: 'm.prisoners', text: 'quest.prisoners', required: false })
defineListener({
id: 'm.cellsOpened',
event: enteredArea,
run(world, { area, entity }) {
if (area !== 'm.cells' || entity.owner !== PLAYER || !world.objectives.has(PLAYER, freeThePrisoners.id)) return
if (world.objectives.get(PLAYER, freeThePrisoners.id).status !== 'open') return
for (const prisoner of world.find({ owner: world.neutral, area: 'm.cells' })) world.setOwner(prisoner, PLAYER)
world.objectives.complete(PLAYER, freeThePrisoners.id)
}
})A town that becomes an ally instead of yours: give it a player of its own on the map, and change the relation:
world.sides.setRelation(PLAYER, TOWN, 'ally')
world.computers.seat(TOWN, { brain: townGuard }) // it defends itself from now on (see AI for missions)Find an item, or bring one somewhere
const findTheRelic = defineObjective({ id: 'm.relic', text: 'quest.relic', required: false })
const bringTheRelic = defineObjective({ id: 'm.bring', text: 'quest.bring' })
// picked up: the next quest
defineListener({
id: 'm.relicFound',
event: itemPicked,
run(world, { entity, item }) {
if (item.item !== 'relic' || entity.owner !== PLAYER || !world.objectives.has(PLAYER, findTheRelic.id)) return
world.objectives.complete(PLAYER, findTheRelic.id)
world.objectives.add(PLAYER, bringTheRelic) // "New objective"
}
})
// brought to the altar by whoever carries it
defineListener({
id: 'm.relicBrought',
event: enteredArea,
run(world, { area, entity }) {
if (area !== 'm.altar' || !world.objectives.has(PLAYER, bringTheRelic.id)) return
if (slotsOf(entity)?.some((slot) => slot?.item === 'relic')) world.objectives.complete(PLAYER, bringTheRelic.id)
}
})Put the relic on the map in the editor, or drop it from a creep or a chest. Tag it so the shop never sells it (Tag my items).
Kill so many
"Slay 10 trolls", with a bar:
const cullTheTrolls = defineObjective({ id: 'm.trolls', text: 'quest.trolls', required: false })
world.objectives.add(PLAYER, cullTheTrolls, { params: { count: 10 }, progress: { value: 0, max: 10 } })
defineListener({
id: 'm.trollSlain',
event: entityDied,
run(world, { entity, killer }) {
if (entity.def.id !== 'troll' || killer?.owner !== PLAYER || !world.objectives.has(PLAYER, cullTheTrolls.id)) return
const quest = world.objectives.get(PLAYER, cullTheTrolls.id)
if (quest.status !== 'open') return
const value = quest.progress!.value + 1
world.objectives.progress(PLAYER, cullTheTrolls.id, value)
if (value >= quest.progress!.max) world.objectives.complete(PLAYER, cullTheTrolls.id)
}
})params fill the quest's words: 'Slay {count} trolls'.
An optional quest found on the way
Classic side quests are discovered: a villager calls out as you pass. Give the quest in a sequence, not at the start:
defineSequence({ id: 'm.theMill', steps: [
waitFor(enteredArea, (_world, { area, entity }) => area === 'm.mill' && entity.owner === PLAYER),
...talk(PLAYER, [{ speaker: 'miller', text: 'line.miller.help', seconds: 4 }]),
(world) => world.objectives.add(PLAYER, freeThePrisoners) // "New objective" (optional)
] })Start the sequence as the mission starts; it waits. A quest given to a player who talks to a unit: Make a unit's face talk, or a cinematic with a choice (Triggering from a mission).
Quests in stages
"Quest updated": the first part done, the next given, the bar reset.
const stageTwo = defineObjective({ id: 'm.stage2', text: 'quest.stage2' })
defineListener({
id: 'm.stages',
event: objectiveChanged,
run(world, { player, objective }) {
if (objective.id === razeTheCamp.id && objective.status === 'done') {
world.objectives.add(player, stageTwo, { progress: { value: 0, max: 3 } })
world.checkpoint('checkpoint.stage2') // "Retry from the second stage"
}
}
})The mission is won only once every main quest given is done, so a later stage keeps it going.
A secret
No quest at all: a message, a reward, and maybe a line in the debrief.
const found = defineComponent<{ grotto?: boolean }>({ id: 'm.found', default: () => ({}) }) // in Rules.components, saved
defineListener({
id: 'm.secret',
event: enteredArea,
run(world, { area, entity }) {
if (area !== 'm.grotto' || entity.owner !== PLAYER || world.state(found).grotto) return
world.state(found).grotto = true
earn(world.player(PLAYER).resources, 'gold', 500)
world.message(PLAYER, 'msg.secretFound', 'info')
}
})Close the open optional quests at the end
Optional quests left open when the mission is won should fail, as in the classics:
const closeOptional = (world: World) => {
for (const quest of world.objectives.of(PLAYER)) if (!quest.required && quest.status === 'open') world.objectives.fail(PLAYER, quest.id)
}Call it in the step before the last main quest completes.
Without code
The editor's Objectives panel makes quests given at the start or by a rule; its Rules panel makes triggers that give, complete or fail them (when a region holds units, a time, a quest's status, a word told; do an objective, a line, a cinematic, a prefab, a timer, win, lose). Add missionRules() and mapObjectives() to the rules' packs. See Missions, objectives and triggers.
Reference
| Call | What it does |
|---|---|
defineObjective({ id, text, detail, required }) | A quest: text is its words (a message key), detail opens under it, required: false makes it optional |
world.objectives.add(player, def, { params, progress }) | Gives it ("New objective") |
world.objectives.progress(player, id, value, { max, params }) | Moves its bar, or changes its words' params |
world.objectives.complete(player, id) | "Objective done" |
world.objectives.fail(player, id, reason) | "Objective failed"; a main quest's reason is the defeat screen's words |
world.objectives.has(player, id), get, of(player) | Whether it was given; its state (status: open, done, failed); all of a player's |
objectiveChanged | Heard on every change (player, objective, was) |
objectiveVictory | Won when every main quest is done; lost when one fails |
The HUD announces each change in words you can reword in GameView.hud.messages. For the classic wording:
hud: { messages: { objectiveNew: 'Quest discovered: {text}', objectiveDone: 'Quest completed: {text}', objectiveFailed: 'Quest failed: {text}' } }