Skip to content

Let trees burn ​

Add burningPack() to your rules and set a tree alight with setAlight: it burns with fire for its seconds, chars to black, and comes down. Any type can burn, the fire can pass from tree to tree with the wind, and a game can give burning an effect of its own.

ts
// in src/rules.ts: the pack in the `createGame` call, a tree (the starter has none) and a row of them on the field
import { burningPack, createGame } from '@v12-engine/engine'

export const game = createGame({
	id: 'my-game',
	name: 'My game',
	// trees burn 20 seconds and are felled by the fire; a burning tree lights the trees beside it, likelier downwind
	packs: [burningPack({ spread: { chance: 0.15 }, damage: 8 })]
})

// what can be felled burns: a tree
game.unit({ id: 'tree', name: 'Tree', kind: 'resource', radius: 0.5, footprint: { w: 1, d: 1 }, stats: { health: 1 }, sight: 0, invulnerable: true, fellable: true })

game.onStart((world) => {
	for (let x = 6; x < 27; x += 2) world.spawn('tree', { at: { x: x + 0.5, z: 8.5 }, owner: 0 })
})
ts
// in src/view.ts: in `units`, how the tree looks (a trunk and a crown)
import type { GameView } from '@v12-engine/client'

export const units: GameView['units'] = {
	looks: {
		tree: {
			height: 3,
			shapes: [
				{ shape: 'cylinder', size: [0.3, 1.6, 0.3], offset: [0, 0.8, 0], color: [0.4, 0.28, 0.16] },
				{ shape: 'sphere', size: [1.6, 1.8, 1.6], offset: [0, 2.2, 0], color: [0.2, 0.45, 0.18] }
			]
		}
	}
}
ts
// anywhere in your rules: a tree set alight by a hero, who is credited with what the fire hurts
import { setAlight, type Entity, type World } from '@v12-engine/engine'

export function torch(world: World, tree: Entity, hero: Entity): boolean {
	return setAlight(world, tree, { by: hero })
}

Done: the tree plays the ready-made fire (flames, embers, smoke and a light), darkens as it burns, and falls once it has burnt down. Units standing next to it take 8 damage a second from the hero.

Put a fire out ​

putOut(world, tree) stops it. The tree stays standing, charred as far as it burnt; set alight again, it goes on from there.

ts
import { burningOf, isBurning, putOut, type Entity, type World } from '@v12-engine/engine'

export function douse(world: World, tree: Entity): number {
	if (isBurning(tree)) putOut(world, tree)
	return burningOf(world, tree)?.share ?? 0 // how far it burnt, 0 to 1
}

Set things alight with an ability ​

The burn effect lights what burns where it lands (act: 'light') or puts it out (act: 'putOut'), by the caster. It takes the fields of whom every effect has: affects: 'enemies' lights only what the caster's enemies own (not its own walls, nor the neutral trees), kinds: ['resource'] only trees, and tags, layers, where, sort and limit as for any effect. With none of them it lights everything that burns where it lands.

ts
import type { AbilityDef } from '@v12-engine/engine'

export const fireArrow: AbilityDef = {
	id: 'fireArrow',
	name: 'Fire Arrow',
	target: 'point',
	range: 9,
	cooldown: 20,
	effects: [{ type: 'burn', act: 'light', radius: 2 }]
}

// the enemy's huts and walls, never your own
export const firePot: AbilityDef = { id: 'firePot', name: 'Fire Pot', target: 'point', range: 7, effects: [{ type: 'burn', act: 'light', radius: 3, affects: 'enemies' }] }

Let a hut or a wall burn, and choose what is left ​

Trees (what can be felled) burn by default. Any type burns with burns on it, and says how long and what it leaves: felled (it comes down), nothing (it burns away), or a unit type put down where it stood.

ts
import type { UnitTypeDef } from '@v12-engine/engine'

export const hut: Partial<UnitTypeDef> = { burns: { seconds: 30, leaves: 'nothing' } }
// a tree that leaves a charred stump, which keeps the tree's wood to gather
export const pine: Partial<UnitTypeDef> = { fellable: true, burns: { leaves: 'charredStump', wood: true } }

In code: burningPack({ burns: (world, entity) => entity.owner === 0 }) decides for each thing.

In the editor, a game that lets its data be changed (editable: 'all') shows burns.seconds, burns.leaves, burns.wood and burns.how in the Data panel for each unit type that may burn.

Give burning an effect of your own ​

A look says what plays while something burns (UnitLook.burning), and the game says it for every type (GameView.units.burning). The rules name the kind of burning with how ('burnt' when left out): a blight that withers, frost, acid. Each kind has its look, and its own end in the death's look (death.how).

ts
// in src/view.ts: in `units`, the tree of the first snippet with how it burns and how it ends
import type { GameView } from '@v12-engine/client'

export const units: GameView['units'] = {
	// every type: the ready-made fire, lit for the nearest 4 only, the nearest 24 burning at all
	burning: { params: { size: 1.4 }, most: 24, lights: 4 },
	looks: {
		tree: {
			height: 3,
			shapes: [
				{ shape: 'cylinder', size: [0.3, 1.6, 0.3], offset: [0, 0.8, 0], color: [0.4, 0.28, 0.16] },
				{ shape: 'sphere', size: [1.6, 1.8, 1.6], offset: [0, 2.2, 0], color: [0.2, 0.45, 0.18] }
			],
			burning: {
				// a blight withers it: falling leaves (a ready-made effect, or one of the view's own `fx`), green, no charring
				how: { withered: { fx: 'leaves', tint: [0.6, 0.9, 0.5], char: false, light: false } }
			},
			// how it ends: burnt down it sinks in ash; withered, it crumbles into pieces
			death: { how: { burnt: { collapse: 'sink', fx: 'dust' }, withered: { shatter: 10 } } }
		}
	}
}

A tree with a model names the points of it the effect plays at (burning.at: ['crown', 'trunk'], ModelDef.points), and a sound to hear as it catches (burning.sound, one of the view's sounds).

ts
import type { AbilityDef } from '@v12-engine/engine'

// the rules' side: what it sets off is the blight, for 6 seconds
export const blight: AbilityDef = {
	id: 'blight',
	name: 'Blight',
	target: 'point',
	range: 8,
	effects: [{ type: 'burn', act: 'light', how: 'withered', seconds: 6, radius: 3 }]
}

Let it spread, and stop it ​

spread has fire pass from a burning thing to what burns and stands within reach (trees, buildings, walls), by a chance a try, much likelier downwind (the map's wind, by wind). The burnStart hook is asked once for every try and can refuse it or change its chance (held to 0 to 1).

ts
import { burningPack, burnStart, defineHook } from '@v12-engine/engine'

export const pack = burningPack({ spread: { reach: 1.5, chance: 0.2, wind: 1, every: 1 }, maxBurning: 1500 })
// nothing catches by the river
export const wet = defineHook({
	hook: burnStart,
	id: 'mygame.wetBanks',
	run: (_world, event) => {
		if (event.cause === 'spread' && event.entity.pos.z > 100) event.chance *= 0.2
	}
})

With grass that burns ​

Where the game has foliagePack too, each may light the other: fire: { lights: true } on the grass lets a burning cell set alight what burns and stands on it or next to it, and lightsFoliage: true here lets a burning tree set alight the grass under it and one cell round. Either pack works alone, and either way works without the other.

ts
packs: [foliagePack({ layer: 'grass', fire: { lights: true } }), burningPack({ lightsFoliage: true })]

The fire crosses as fire does, never all at once. A burning tree tries each cell round it once a second, at 0.2 for grass that catches as dry grass does (its style's flammable), less in the wet and likelier downwind. A burning cell tries what stands beside it at each of its own tries (four a second), at 0.05 for grass of fuel 1. So a tree a front reaches catches in a second or two, and a meadow with a wood in it burns over minutes, not in one tick. What catches through the link is asked of the game like any other catch: burnStart with cause: 'foliage' (and the layer and cell it caught from), foliageIgnite with cause: 'burning' (and the thing it caught from), once a try, so a handler can damp it or refuse it. Each pack keeps to its own most (maxBurning); what has no room does not catch. What caught is set alight at the end of the burning pack's turn and tries nothing before its own next turn, so nothing lights within the call or the event that lit another.

How grass catches, spreads and is stopped: Set grass on fire and Stop a fire spreading.

On the screen the grass burns by itself: each blade chars from its tip down over the time its style burns, its edge glowing, and ends as black stubble over ash; flames, smoke, embers, a few lights and a crackle play along the front, the nearest the camera's. The look's fire changes each part, or turns one off, per style too:

ts
foliage({ fire: { smoke: false, bright: 1.3, scorch: 0.6 }, styles: { 3: { fire: { smoky: 2 } } } })

Reference ​

burningPack(options) ​

OptionDefaultWhat it does
seconds20How long a thing burns
leaves'felled'What is left: felled, nothing, or a unit type's id
woodfalseWhat is left keeps the wood it had (a resource left in its place)
how'burnt'The kind of burning, for the look and the death
burnstype's burns, then treesWhat burns, in code: true, false or a BurnsDef
spreadoff{ reach: 1.5, chance: 0.15, wind: 1, every: 1 }, or true for those
damage, damageReach, damageTypenone, 1, magicDamage a second to units on the ground near it, by whoever lit it
maxBurning2000The most that burn at once: nothing more catches past it
lightsFoliagefalseA burning thing tries the grass round it once a second (or the layers named)

UnitTypeDef.burns (BurnsDef): seconds, leaves, wood, how, over the pack's.

Calls, hook and events ​

NameWhat it does
setAlight(world, thing, { by, how, seconds })Sets it alight; whether it caught
putOut(world, thing)Puts it out, charred as far as it burnt
isBurning(thing)Whether it burns now: false once put out or burnt down
burningOf(world, thing){ burning, how, share, left, by }, for one that burns or burnt
burningThings(world)The ids of what burns, in order
canBurn(world, thing)Whether it burns at all in this game
burnStartHook before anything catches, once a try: cancel(), or change chance; cause is light, spread or foliage (with layer and cell)
burnStarted, burnt, burnPutOutEvents: it caught, burnt down (with what it left), was put out
onFireThe component the screens read: { how, since, ends, out } in ticks

BurningLook ​

FieldDefaultWhat it does
fx'fire'The effect while it burns, by name or inline; false for none
atits middlePoints of its model the effect plays at
paramssize by its heightKnobs of the effect
soundnoneHeard as it catches
lightyesWhether its effect gives light (the nearest few: GameView.units.burning.lights)
tintnoneA colour over it from the moment it catches
charnear blackThe colour it darkens to as it burns; false for none
howThe same fields again for a kind of burning

GameView.units.burning adds most (40: how many nearest the camera play their effect; the rest only char) and lights (6).

Notes ​

  • Nothing burns, and the pack costs nothing a tick, until something is set alight. Trees that burn stay asleep as scenery: a forest of 30,000 with 2,000 alight is measured in Destruction and debris.
  • A burning or charred tree is drawn as a model, never as a picture far off, and its char is a colour of its copy: no material a tree. A burnt-down tree comes down as its look's death.how says: falls when felled, sinks in smoke when it burnt away, or your own.
  • The loading screen's warm-up plays the burning effect ahead by itself (the look's fx, fire when it names none) when the game has burningPack, and the grass's flames, smoke and embers when it has foliagePack with its fire on, so the first fire does not hitch.
  • Put 'fire' in your view's effects (presetFx(['fire'])) to change it in the editor's FX panel; it plays as made where you don't.