Grass and bushes
New to grass?
Start at the Grass and bushes section: an overview, every setting with examples, and a cookbook of real-world recipes. This page is the reference: every field, call and event in one place.
Cover the ground with grass and bushes that sway in the wind, part as units walk through, hide who stands in them, and can be cut, trampled, burnt and grown back. Grass is a layer of cells; each value painted on it is a style with its own look and its own rules.
// in src/rules.ts: add `cellLayers` and `packs` to the `createGame` call, and paint a field as the game starts
import { createGame, defineCellLayer, foliagePack, presetFoliageStyles } from '@v12-engine/engine'
export const game = createGame({
id: 'my-game',
name: 'My game',
cellLayers: [defineCellLayer({ id: 'grass', name: 'Grass', values: [{ value: 1, name: 'Meadow', foliage: presetFoliageStyles.meadow }] })],
packs: [foliagePack({ layer: 'grass' })]
})
game.onStart((world) => {
world.setCells('grass', { kind: 'circle', x: 16, z: 16, radius: 12 }, 1)
})// in src/view.ts: in `ground`, how the layer is drawn
import { foliage, type GameView, presetFoliage } from '@v12-engine/client'
export const ground: GameView['ground'] = { cellLayers: { grass: foliage(presetFoliage.meadow) } }Done: a round meadow with a few flowers covers the middle of the empty starter's field, sways in the wind and parts round your scout. It burns if something lights it, and grows back 90 seconds after.
The two halves never meet. The rules half (FoliageStyleDef, on the layer's values, and foliagePack) says what a style hides, slows and how it burns; it is the same on every machine. The look half (FoliageLook, given to foliage) says how it is drawn; it is the screen's alone. A game that only wants grass to look at can leave the pack out.
A look of your own
Grass is read by groups: shape, sizes, rotation, colors, placing, light, levels, wind, walked, ruined. Every field has a default, so say only what you change.
import { foliage } from '@v12-engine/client'
export const grass = foliage({
height: 1.4, // world units
density: 6, // tufts a cell
seed: 3, // the same seed lays the same field on every screen
shape: { kind: 'blade', blades: 4, curve: 0.4, droop: 0.2 },
sizes: { each: [0.7, 1.3], edge: { cells: 2, to: 0.5 } }, // smaller towards the edge of a patch
rotation: { lean: 12, leanTo: 'wind' },
colors: { foot: [0.18, 0.38, 0.1], tip: [0.6, 0.72, 0.3], vary: 0.15, patches: { amount: 0.5, to: [0.7, 0.6, 0.3] } },
placing: { clump: { amount: 0.6 }, slope: [0, 35], notTiles: ['sand'] },
light: { translucency: 0.4, footFade: 0.2 }
})A blade field that clumps, with dry patches, and none on sand or on slopes over 35°. Grass keeps off water, cliff walls, buildings and scenery that blocks by itself (placing.keepOff). It grows on the road tile unless you ask: placing: { keepOff: { roads: true } }.
A look can be a function of the part's context, for one that needs the map:
import { foliage } from '@v12-engine/client'
export const sparse = foliage(({ terrain }) => ({ density: terrain.width > 200 ? 2 : 4 }))Two patches with different settings
Each value of the layer is a style. Its rules go on the value (foliage), its look in styles under the same number; what a style does not say is the layer's.
import { defineCellLayer, foliagePack, presetFoliageStyles, type Rules, setFoliage, type World } from '@v12-engine/engine'
import { foliage, presetFoliage } from '@v12-engine/client'
declare const rules: Rules
declare const world: World
export const grassRules: Rules = {
...rules,
cellLayers: [
defineCellLayer({
id: 'grass',
name: 'Grass',
values: [
{ value: 1, name: 'Meadow', foliage: presetFoliageStyles.meadow },
{ value: 2, name: 'Tall grass', foliage: presetFoliageStyles.tallGrass }
]
})
],
packs: [...(rules.packs ?? []), foliagePack({ layer: 'grass' })]
}
export const grass = foliage({ ...presetFoliage.meadow, styles: { 2: presetFoliage.tallGrass } })
// a patch of tall grass, painted in play (checked: a value the layer has, or 0)
setFoliage(world, 'grass', { kind: 'rect', x: 40, z: 30, width: 10, depth: 6 }, 2)Up to 255 styles a layer. Paint them on the map in the editor, in the map file (GameMap.cells), or with world.setCells and setFoliage. A value the game doesn't name is drawn as the layer and is nothing to the rules.
Bushes from your models
draw: 'models' (or just models) draws your models in place of grass, picked by weight, each turned, sized and leaned its own way.
import { foliage } from '@v12-engine/client'
export const bushes = foliage({
models: [{ src: 'forest/bush_common_flowers.glb' }, { src: 'forest/bush_common.glb', weight: 0.2 }], // a few with red leaves, as in autumn
density: 0.8, // models a cell: a part of one is a chance of one
height: 1.2,
shadow: 'near' // cast shadows near the camera only
})See Make bushes of my own for the rest.
Grass and bushes on one layer
A style draws grass or models, so one layer holds a meadow and a thicket side by side:
import { foliage } from '@v12-engine/client'
export const mixed = foliage({
density: 4, // value 1: grass
styles: { 2: { draw: 'models', models: [{ src: 'forest/bush_common_flowers.glb' }], density: 0.7, colors: { tint: [0.9, 1, 0.8] } } },
levels: { mid: 60, far: 100, cull: 160 }
})Ready-made kinds
presetFoliage (looks) and presetFoliageStyles (rules) share their names: meadow, tallGrass, dryGrass, reeds, wheat, snowGrass, thicket, hedge. Take one whole, or change what you want.
import { defineCellLayer, presetFoliageStyles } from '@v12-engine/engine'
import { foliage, presetFoliage } from '@v12-engine/client'
export const layer = defineCellLayer({
id: 'grass',
name: 'Grass',
values: [
{ value: 1, name: 'Wheat', foliage: { ...presetFoliageStyles.wheat, trample: 'none' } },
{ value: 2, name: 'Reeds', foliage: presetFoliageStyles.reeds }
]
})
export const look = foliage({ ...presetFoliage.wheat, styles: { 2: presetFoliage.reeds } })| Name | Looks | To the rules |
|---|---|---|
meadow | short blades, a few flowers | burns, grows back after 90 s |
tallGrass | 1.4 tall, drooping | hides (1.4 tall), burns, grows back after 120 s |
dryGrass | short, straw-coloured | burns fast and hot, grows back after 150 s |
reeds | 1.8 tall, stiff, brown heads; grows in water | hides, slows to 0.85, burns a little |
wheat | dense, gold, leans downwind | hides units up to 1.1 tall, burns, crushed underfoot |
snowGrass | short, pale patches | hardly burns, grows back after 180 s |
thicket | wide dark tufts, red berries | hides, slows to 0.6, burns long |
hedge | dense, stiff | hides, nothing walks through it |
All of them are grass made in code; thicket and hedge are not models.
Grass from ground tiles
on grows grass wherever the ground is painted with some tiles, kept in step as they are painted. It is the screen's alone: short and cheap by default, it hides nobody and never burns.
import { foliage } from '@v12-engine/client'
export const grass = foliage({ density: 4, on: { tiles: ['grass', 'darkGrass'], density: 1, height: 0.6 } })A cell painted with the layer wins over its tile. Grown cells are drawn as style 255 (FOLIAGE_TILE_STYLE: give it a look in styles[255]), or as on.style. For the rules to see them, paint them into the layer once as the world begins:
import { growOnTiles, type World } from '@v12-engine/engine'
declare const world: World
// from now on these cells hide and burn as style 1 says; a tile painted later grows nothing to the rules
growOnTiles(world, 'grass', ['grass', 'darkGrass'], 1)Flowers
scatter mixes small plants into a style of grass, laid in the same chunks. They sway, burn and are cut with it.
import { foliage } from '@v12-engine/client'
export const meadow = foliage({
scatter: [
{ density: 0.15, height: 0.5, colors: [[0.95, 0.88, 0.35], [0.85, 0.4, 0.62]] }, // a flower's head on a short tuft
{ density: 0.05, src: 'plants/fern.glb' } // a model of yours
]
})See-through grass
Any colour of the grass can say how much of it is seen: a fourth number, from 0 (not at all) to 1 (whole). A colour of three is whole, as it always was.
import { foliage } from '@v12-engine/client'
export const fading = foliage({
colors: { foot: [0.2, 0.42, 0.12], tip: [0.55, 0.72, 0.28, 0.2] }, // blades that fade out towards their tips
styles: { 2: { colors: { tint: [1, 1, 1, 0.5] } } } // a style half seen, its bushes too
})What each colour's fourth means:
| Colour | Its fourth |
|---|---|
foot, tip, stops | How much is seen at the foot, the tip and between: blades that fade towards the tip, or rise out of nothing |
tint | How much of the whole style is seen, grass and models |
patches.to, dry.to | Turned towards as far as the colour is: thin patches, grass that dries to nothing |
ruined.crushed, ruined.burnt | How much of crushed (and cut) grass and of what is charred is seen |
scatter colours | How much of each kind of flower is seen |
The glow of what burns (fire.color) and a light's colour (fire.light.color) take three numbers: they are light added, not a surface. How bright they are is fire.bright.
What is partly seen is drawn by leaving out a fixed pattern of its pixels, as the grass around your own units is seen through: nothing is sorted or blended, and the pattern stays put as the camera moves. Blades over each other leave out different pixels, so a thick field still covers more than a thin one. A colour that is whole costs nothing more. Changing only how much of the foot, tip, tint, crushed or burnt is seen lays nothing again; changing a patch's lays the chunks of that style. Bushes take the tint's and the patches': shadows they cast stay whole.
Wind and gusts
The map's wind (MapEnvironment.wind) rolls waves of bend downwind over a field. Each style answers in its own way (wind), and a gust bends grass at one place.
import { gust, type World } from '@v12-engine/engine'
import { foliage, type GameClient } from '@v12-engine/client'
declare const world: World
declare const client: GameClient
export const look = foliage({
wind: { stiffness: 0.2, waves: 1.2, sheen: 0.4 }, // a soft meadow
styles: { 2: { wind: { stiffness: 0.9, waves: 0.2, flutter: 0 } } } // reeds that hardly move
})
// a storm gets up over 5 seconds
world.setEnvironment({ wind: { around: 90, strength: 1 } }, { over: 5 })
// a ring of wind out from a blast, on every screen that sees it
gust(world, { x: 20, z: 20 }, { radius: 6, strength: 1.5 })
// a push one way, on this screen alone
client.gust({ x: 20, z: 20 }, { radius: 4, strength: 1, around: 90, lasts: 1 })A gust from every explosion of a barrel, as a game ships it:
import { defineListener, entityDied, gust } from '@v12-engine/engine'
export const blast = defineListener({
event: entityDied,
id: 'mygame.barrelBlast',
run(world, { entity }) {
if (entity.def.id === 'barrel') gust(world, { x: entity.pos.x, z: entity.pos.z }, { radius: 7, strength: 2, lasts: 0.8 })
}
})Put it in Rules.listeners. Four gusts blow at once at most; another takes the place of the one nearest its end. A gust changes nothing of the world and is not saved. See Make grass follow the wind.
Hide units
A style with conceals hides who stands in it from the teams that have nobody in the same patch (connected cells of hiding grass) and nobody within reach (2 cells).
import { defineCellLayer, foliagePack, type Rules } from '@v12-engine/engine'
declare const rules: Rules
export const hidingRules: Rules = {
...rules,
cellLayers: [
defineCellLayer({
id: 'grass',
name: 'Grass',
values: [
{ value: 1, name: 'Meadow' },
{ value: 2, name: 'Tall grass', foliage: { conceals: true, height: 2 } },
// a unit taller than 1.2 (twice its radius) is seen over it
{ value: 3, name: 'Reeds', foliage: { conceals: true, concealsTaller: false, height: 1.2 } }
]
})
],
// the hiding unit's moves shake the grass on the screens of those who don't see it
packs: [...(rules.packs ?? []), foliagePack({ layer: 'grass', conceal: { rustle: true, reach: 1.5 } })]
}Grass that is cut, burnt, burning or crushed hides nobody; growing grass hides again. See Hide units in tall grass.
Walked on, and trails
Every unit you see parts the grass, however many there are. With walked.flatten the grass lies down under feet and stands up again over recover seconds, leaving a trail behind who moves. It is your screen's alone.
import { foliage } from '@v12-engine/client'
export const look = foliage({
walked: { bend: 1, flatten: 0.7, recover: 12, radius: 'size' },
// wheat stays down until it is grown again; reeds spring back and leave no trail
styles: { 2: { walked: { recover: 0 } }, 3: { walked: { flatten: 0.3, trail: false } } }
})Trampling, and grass nothing walks through
To have the rules crush a style for good, give it trample: 'crush': a unit that moves over it lays it flat (state flattened), and it hides nobody until it grows back. walkable: false makes a style the ways go round, as a wall.
import { defineCellLayer, foliagePack, type Rules } from '@v12-engine/engine'
declare const rules: Rules
export const fieldRules: Rules = {
...rules,
cellLayers: [
defineCellLayer({
id: 'grass',
name: 'Grass',
values: [
{ value: 1, name: 'Wheat', foliage: { conceals: true, trample: 'crush', regrows: 60 } },
{ value: 2, name: 'Hedge', foliage: { walkable: false } }
]
})
],
// only the big ones crush wheat; every unit that walks when left out
packs: [...(rules.packs ?? []), foliagePack({ layer: 'grass', tramples: (_world, unit) => unit.def.radius >= 1 })]
}See Cut or crush grass.
Ruin and regrow
ruin cuts (cut: stubble, a firebreak), crushes (crush: laid flat) or scorches (scorch: black, nothing left to burn). regrow brings it back; a style that regrows comes back by itself.
import { regrow, ruin, type Entity, type World } from '@v12-engine/engine'
declare const world: World
declare const mower: Entity
const lawn = { kind: 'rect', x: 32, z: 32, width: 12, depth: 8 } as const
ruin(world, 'grass', lawn, { how: 'cut', by: mower })
// later: it rises from the ground over 20 seconds
regrow(world, 'grass', lawn, { over: 20 })A mower that cuts the grass it drives over, as a game ships it:
import { ruin, type System } from '@v12-engine/engine'
export const mowing: System = {
name: 'mygame.mowing',
phase: 'resolve',
every: 15, // ticks: twice a second at 30 ticks a second
update(world) {
for (const unit of world.units.values())
if (unit.def.id === 'mower') ruin(world, 'grass', { kind: 'circle', x: unit.pos.x, z: unit.pos.z, radius: 1.2 }, { how: 'cut', by: unit })
}
}Put it in Rules.systems. How ruined grass looks is the look's ruined group. See Cut or crush grass and Make grass grow back.
Fire: catch, spread, stop
A style burns where it is flammable. Set it alight with ignite; it spreads to the cells about it, likelier downwind, uphill and in dry grass, burns for the style's burns seconds and is burnt.
import { defineCellLayer, foliagePack, ignite, type Entity, type Rules, type World } from '@v12-engine/engine'
declare const rules: Rules
declare const world: World
declare const hero: Entity
export const fireRules: Rules = {
...rules,
cellLayers: [defineCellLayer({ id: 'grass', name: 'Grass', values: [{ value: 1, name: 'Dry field', foliage: { flammable: 1, burns: 5, regrows: 120 } }] })],
// 8 damage a second to who stands in it, dealt by whoever lit it
packs: [...(rules.packs ?? []), foliagePack({ layer: 'grass', fire: { damage: 8, wind: 1.5, maxBurning: 3000 } })]
}
ignite(world, 'grass', { kind: 'circle', x: 30, z: 30, radius: 1.5 }, { by: hero })Stop it in any of these ways:
import { defineHook, extinguish, firebreak, foliageIgnite, stopFire, type World } from '@v12-engine/engine'
declare const world: World
extinguish(world, 'grass', { kind: 'circle', x: 30, z: 30, radius: 4 }) // put out what burns there: it stays burnt
firebreak(world, 'grass', { kind: 'rect', x: 40, z: 30, width: 2, depth: 20 }) // cut a strip fire does not cross
stopFire(world, 'grass') // every fire of the layer; every layer of the pack when none is named
// no fire crosses the river: a hook sees every cell about to catch
export const wetBanks = defineHook({
hook: foliageIgnite,
id: 'mygame.wetBanks',
run(_world, event) {
if (event.cause === 'spread' && event.z > 100) event.cancel()
}
})How it looks: a blade chars from the tip down with a glowing edge, flames, smoke and embers play along the front, and ash is left under what burnt. Change each part in the look's fire group, the layer's and each style's:
import { foliage } from '@v12-engine/client'
export const grass = foliage({
fire: { color: [1, 0.55, 0.15], scorch: 0.7, embers: false }, // a lighter ash, no embers
styles: {
2: { fire: { bright: 1.3, smoky: 0.5 } }, // dry grass: big, bright, little smoke
3: { fire: { bright: 0.8, smoky: 2, sound: false } } // a thicket: low flames, thick smoke
}
})The loading screen plays the fire's effects once ahead where the rules burn grass, so the first fire does not hitch.
Rain and snow (MapEnvironment.weather) damp it and put it out, water, roads and blocked cells never burn, and fire.lasts ends every fire after so long. See Set grass on fire and Stop a fire spreading.
Burn it with an ability
The foliage effect does to the grass where it lands what its act says: ignite, extinguish, ruin, regrow or clear.
import type { AbilityDef } from '@v12-engine/engine'
export const fireArrow: AbilityDef = { id: 'fireArrow', name: 'Fire Arrow', target: 'point', range: 9, cooldown: 12, effects: [{ type: 'foliage', act: 'ignite', radius: 1.5 }] }
export const rain: AbilityDef = { id: 'rain', name: 'Rain', target: 'point', range: 10, cooldown: 30, effects: [{ type: 'foliage', act: 'extinguish', radius: 6 }] }See Burn grass with an ability.
Change it in play
From the rules, for every player: paint styles, and change looks through the map's environment.
import { setFoliage, type World } from '@v12-engine/engine'
declare const world: World
setFoliage(world, 'grass', { kind: 'circle', x: 20, z: 20, radius: 5 }, 2)
// autumn: every screen eases the meadow's tips to gold over 30 seconds
world.setEnvironment({ bushes: { grass: { styles: { 1: { colors: { tip: [0.85, 0.7, 0.3] } } } } } }, { over: 30 })On one screen, through the layer's handle:
import type { GameClient } from '@v12-engine/client'
declare const client: GameClient
const grass = client.foliage('grass') // undefined where the layer is not drawn as grass or bushes
grass?.setStyle(2, { colors: { tip: [0.9, 0.6, 0.2] } })
grass?.setLook({ wind: { sway: 2 } })
grass?.disturb({ x: 20, z: 20 }, 2) // shake it, as one running through would
grass?.flatten({ kind: 'circle', x: 20, z: 20, radius: 3 }, 0.8) // lay it down: it stands again by `recover`
console.log(grass?.stats.instancesDrawn)A change of how many, how big or what colour lays again only the chunks holding that style; a change of sway, shaking or distance lays nothing.
Quality
The player's Tall grass setting (Settings › View: None, Some, All; the quality knob foliage) draws none, a third, or all of the tufts, and thins bushes the same way. The rules are the same whatever it is: grass hides as much at None. The Detail setting (Settings › View: Near, Medium, Far; the quality knob detail) brings the levels nearer, the grass's levels as the bushes' and every model's: half as far on Near, three quarters on Medium, as the look says on Far. A fire's flames, smoke and embers are as many as the Effects setting says, and its lights only at the Lights setting's All. Laying a big map is spread over frames from the frame's budget, its foliage work (4 ms when left out):
import type { GameView } from '@v12-engine/client'
export const world: GameView['world'] = { frameBudget: { most: { foliage: 6 } } }The code that draws grass and bushes is fetched apart, as a game is made where its looks or its map draw any: a game without grass loads none of it. A world view made in code (createLiteWorldView) draws its grass as that code comes, a moment later.
See Graphics quality.
In the editor
- In the Terrain panel, under your layer's name (Grass), click one of its styles and paint it.
- Pick cells with the Patch tool (Rectangle, Circle, Polygon, Wand, By style) and press Its own settings to give them a style of their own.
- Open Grass and bushes to set the look group by group, or start from a Preset.
- In a style's settings, Gameplay sets what it is to the rules: hides, slows, burns, grows back, trampled.
- Play here, then light, put out, cut and regrow grass from the Cheats panel's Grass brushes.
See Grass and bushes in the editor.
Reference
Where each thing is kept
| What | In code | Every map, from the editor | One map, from the editor |
|---|---|---|---|
| A style's name, colour and rules | CellLayerDef.values[n] (name, color, foliage) | data.json: cellLayers[layer].values[n] | GameMap.definitions.cellLayers[layer].values[n] |
| The layer's base rules, fire, hiding, who tramples | foliagePack(options) | code only | code only |
| A style's look | foliage({ styles: { n: … } }) | cellLayers.json: [layer].styles[n] | MapEnvironment.bushes[layer].styles[n] |
| The layer's look | foliage(look) | cellLayers.json: [layer] | MapEnvironment.bushes[layer] |
| Which cells have which style | world.setCells, setFoliage | GameMap.cells[layer] | |
| What happened to each cell | ruin, ignite… | GameMap.cells['<layer>.state'] |
Each column lies over the one to its left, field by field. A look read from a file is checked by readMapBushes; a style's rules by foliageStyleProblem.
FoliageStyleDef: a style to the rules
| Field | Default | What it does |
|---|---|---|
conceals | no | Hides who stands in it |
concealsTaller | yes | With false, a unit taller than height is seen over it (a unit stands twice its radius: FoliageConceal.heightOf) |
height | 1 | How tall it stands to the rules, in cells |
speed | 1 | A multiplier of the speed of who walks through it; the ways go round slow ground where that is quicker |
walkable | yes | With false, nothing on foot goes through it, whatever its state |
flammable | 0 | How easily it catches, 0 (never) to 1 |
burns | 6 | Seconds a cell burns |
fuel | 1 | How hot it burns: how much likelier it lights its neighbours |
regrows | never | Seconds after it was burnt, cut or crushed that it starts to grow back by itself; false for never |
grows | 10 | Seconds it takes to grow back once it starts by itself |
trample | 'none' | 'crush': lays flat under who walks over it, by tramples |
The pack's style is the layer's base; each value's foliage lies over it (foliageStyle(game, layer, value) reads the result).
foliagePack(options)
| Option | Default | What it does |
|---|---|---|
layer | The layer of cells (Rules.cellLayers) | |
style | What every style of the layer is where it says nothing | |
conceal | reach (2), hides (units on the ground), seenBy, rustle (no), rustleEvery (0.3 s), heightOf (twice the radius) | |
tramples | every unit that walks | Who crushes a style with trample: 'crush': not structures, flyers or ships |
also | More layers, each with layer, style, conceal, tramples: fire passes between them | |
fire | on | FoliageFire, or false for grass that never burns |
A layer given to foliagePack is not given to concealmentPack too. foliageLayers(options) lists the pack's layers.
FoliageFire
| Field | Default | What it does |
|---|---|---|
spread | yes | Whether a burning cell lights those beside it |
every | 0.25 | Seconds between a burning cell's tries |
wind | 1 | How much the wind decides the way: in a gale a try downwind is 7.4 times likelier, upwind a seventh |
slope | 1 | How much faster uphill: about 2.7 times on a 45° slope |
jump | never | From this wind strength (0 to 1), it jumps a gap of one cell downwind |
wet | 0 | How wet everything is, 0 to 1 (nothing catches) |
weather | yes | Rain and snow damp it and put it out |
maxBurning | 4000 | The most cells burning at once, over all the pack's layers; past it, cells wait their turn |
lasts | never | Seconds after which a fire dies |
damage | none | Damage a second to units in a burning cell, by whoever lit it |
damageType | magic | The damage's type |
lights | no | A burning cell tries to set alight what burns on or beside it at each of its tries, with burningPack (Let trees burn) |
A try lights a neighbour with chance 0.08 × flammable × fuel × (1 − wet), halved across a corner, then weighed by wind and slope. The rolls are the world's own numbers, so a fire burns the same on every machine and in a replay.
Calls
| Call | What it does |
|---|---|
ignite(world, layer, where, { by }) | Sets it alight where it can burn; the cells that caught |
extinguish(world, layer, where) | Puts out what burns there, left burnt |
firebreak(world, layer, where, { by }) | ruin with cut |
stopFire(world, layer?) | Puts out every fire of a layer, or of the pack; how many were burning |
ruin(world, layer, where, { how, by }) | cut, crush or scorch (FoliageRuin) |
regrow(world, layer, where, { over, by }) | Grows ruined or burnt grass back, over over seconds or at once |
clearFoliage(world, layer, where) | Takes the grass away: value 0, nothing grows back |
setFoliage(world, layer, where, value) | Paints a style, checked; the cells whole again |
foliageAt(world, layer, x, z) | FoliageCell: cell, value, style, state, burning, left (seconds), by |
burningCells(world, layer) | The cells burning now, in order: don't keep or change it |
gust(world, at, gust) | A gust on every screen that sees it (WindGust: radius, strength, around, lasts 0.6) |
growOnTiles(world, layer, tiles, value) | Paints the cells of these tiles with value, once |
foliageStateLayer(layer) | The id of the state layer: grass.state |
patchCells(terrain, layer, cell, options) | The cells of the patch a cell is in |
where is a FoliageWhere: a shape (AreaShape) or cell indices. Each call throws for a layer that is not the pack's.
State
FOLIAGE_STATE | Value | Hides | Burns | Drawn as |
|---|---|---|---|---|
whole | 0 | if the style conceals | if flammable | as it grew |
flattened | 1 | no | no | laid one way, browned |
cut | 2 | no | no (a firebreak) | stubble at ruined.stubble |
burning | 3 | no | burning | charring from the tip down, its edge glowing, under flames |
burnt | 4 | no | no | short black stubble over ash; most copies gone |
growing | 5 | yes | yes | rising from the ground |
The state lives in the layer <layer>.state (foliageStateLayer), saved, sent to the players who see a cell and remembered through the fog by the others. foliageStateOf(value) names a value.
Events, the hook and the effect
| Name | Kind | Carries |
|---|---|---|
foliageIgnite | hook, before a cell catches, once a try | layer, cell, x, z, cause (ignite, spread, burning: from a burning tree), from, thing, by, chance (change it, held to 0 to 1), cancel() |
foliageIgnited | event | layer, cells, cause, by |
foliageBurnt | event | layer, cells |
foliageRuined | event | layer, cells, how, by (trampling too) |
foliageRegrown | event | layer, cells, by |
foliageFireOut | event | layer: its last fire went out |
foliage effect | act, layer?, how?, over? | Over the effect's radius or shape, else the cell it landed on; every layer of the pack when layer is left out |
The events are for your rules and computer players and are not sent to screens: screens see the state layer's cells as far as they see.
FoliageLook
Old flat fields still work: color, tip, size, sway, shake, tilt and far are colors.foot, colors.tip, sizes.each, wind.sway, walked, rotation.lean and levels said the old way.
| Field | Default | What it does |
|---|---|---|
draw | models where models names any, else grass | 'grass' or 'models' |
models | { src, scale, weight } each | |
height | 0.6 (grass); as made (models) | World units: a unit stands about 1.5, a tree 3.5 to 4.7 |
density | 3 tufts, 0.7 models | A cell; fewer with the player's Tall grass setting |
seed | 0 | The layout's seed |
noise | 8 | Cells across the patches of sizes.noise, colors.patches, placing.clump |
seeThrough | yes | See-through round your own units in it |
sound, volume | none, 0.7 | Heard as one moves through it: a sound by name ('rustle' is the client's own); none when left out |
leaves | none | An effect played as one steps in (the ready-made leaves) |
shadow | no | Models cast shadows: true, 'near' or false. Grass never does |
styles | A look for each value over the layer's; false for none drawn | |
on | Grass from tiles: tiles, style (255), density (1), height (0.7 of the layer's) | |
scatter | Flowers: { shape, src, density, height, colors, sizes } each |
shape | Default | |
|---|---|---|
kind | 'tuft' | 'tuft', 'blade' (tapers and curves), 'cross' (crossed cards with a picture), 'mesh' (a model) |
blades | 5 | Blades a tuft |
width | 0.09 | A blade's width at its foot, of a tuft a unit tall |
taper, curve, droop, spread | 1, 0.3, 0, 0 | Narrowing, lean of the tip, hang, how far apart the feet stand |
segments | [3, 2, 1] | Parts of a blade near, further, far |
cards, texture, alphaCutoff | 2, , 0.5 | A cross's cards and picture |
src | A mesh's model |
sizes | Default | |
|---|---|---|
each | [0.8, 1.25] | Each its size, of 1 |
width | [1, 1] | Each its width, of 1 |
noise | none | Bigger in patches: { scale, amount } |
edge | none | Smaller towards the edge: { cells, to } |
rotation | Default | |
|---|---|---|
yaw | [0, 360] | Degrees, one or a range |
faceWind | 0 | Turns to face downwind, 0 to 1 |
lean | 0 grass, 6 models | Degrees |
leanTo | 'random' | 'random', 'wind' or degrees |
upright | yes | Upright on a slope, or square to it |
colors | Default | |
|---|---|---|
foot, tip | greens | Up the blade (grass); a fourth number is how much is seen there |
stops | More colours up the blade: { at, color }, color with a fourth too | |
vary | 0.12 (0 models) | Lighter and darker, each its own |
hue, saturation | 0 | Each its own hue and strength (saturation: grass) |
patches | none | { scale, amount, to }: dry spots; to may have a fourth |
ground | 0 | How much of the ground's colour it takes (grass) |
dry | none | { to, amount }: dried out; to may have a fourth |
tint | white | Multiplies everything, models too; its fourth is how much of the style is seen |
placing | Default | |
|---|---|---|
clump | none | Thicker in patches: { scale, amount } |
edge | none | Thinner towards the edge: { cells, to } |
jitter | 0.8 (0.7 models) | 0 in rows to 1 anywhere in the cell |
slope | every slope | [least, steepest] in degrees |
tiles, notTiles | Tile ids it grows only on, never on | |
keepOff | water, cliffs, buildings, scenery, for grass; none, for models unless said | water, cliffs, buildings, doodads: false to grow through. roads: true keeps it off the road tile: left out, grass grows where it is painted whatever the ground's tile |
light (grass) | Default | |
|---|---|---|
shadows | no | Shadows fall on it |
normals | 'up' | 'up' (soft, even) or 'true' |
translucency, specular, footFade | 0 | Light through the tips, shine, fade into the ground at the foot |
levels | Default | |
|---|---|---|
mid, far | Cells from the camera where fewer, then the fewest, are drawn (models: their .lod1, .lod2 files) | |
cull | never | None past this |
midKeeps | a ramp | The part kept from mid |
farKeeps | 0.25 | The part kept from far |
fade | 4 | Cells one level fades into the next over |
wind | Default | |
|---|---|---|
sway | 1 | How much it sways |
stiffness | 0.4 | 0 soft to 1 stiff |
waves | 1 | How much it joins the waves rolling over a field |
flutter | 0.3 | Tips shivering in a strong wind |
lean | 1 | Leaning downwind as the wind gets up |
speed | 1 | How fast it sways |
sheen | 0.3 | Lighter tips as the blades lie over |
walked | Default | |
|---|---|---|
bend | 1 | How far it bends away |
radius | 1.6 | Cells, or 'size' (by each unit's size, at most 3.2) |
wobble, settle, entry | 1, 1.2 s, 2 | Swing as it settles, how long, how much harder on stepping in |
who | 'all' | 'all', 'friends', 'none' or a function, of the units you see |
flatten | 0 | How far it lies down underfoot, 0 to 1 |
recover | 8 | Seconds to stand again; 0 for never, until grown again |
trail | yes | One moving through leaves laid grass behind |
walked: false turns it off.
ruined | Default | |
|---|---|---|
stubble | 0.15 | Cut height, of its height (a bush's stump) |
crushed | a dry brown | Crushed (and cut) colour; a fourth is how much of it is seen |
burnt | near black | Burnt colour; a fourth is how much of the charred part is seen |
paint | { burnt: 'ash' } | Ground paint under cut and burnt: 'ash', 'mown', a picture's path, or false |
On the screen a blade chars from its tip down over the time its style burns, its edge glowing, and ends as black stubble over ash; a bush chars and loses its leaves. Flames, smoke, embers, a few lights and a crackle play along the front, the nearest the camera's that the player sees (fewer at a lower quality of effects, no lights below the lights' high quality). fire: false turns all of it off. A style says its own, but for color and scorch, which are the layer's. A cell that comes into sight already burning is shown half burnt.
fire | Default | |
|---|---|---|
flames | low flames over the ground | An effect by name (burning-ground, a game's own), or false |
smoke, embers | smoke, embers | Effects by name, or false |
light | yes | Whether the front lights what is round it, and how (color, range, intensity) |
color | orange | The flames and the glowing edge: three numbers, no fourth |
bright | 1 | How big and bright it burns |
smoky | 1 | How much smoke, 0 for none |
scorch | 1 | How dark the ash left under it is, or false |
sound, catches | a crackle, a whoosh | Sounds by name, or false |
The handle: client.foliage(layer)
| Member | What it does |
|---|---|
look | The look drawn now, by groups |
setLook(change) | Changes the layer's look, field by field |
style(value), setStyle(value, change), removeStyle(value), styles() | A style's look |
disturb(at, strength) | Shakes it at a place |
flatten(where, amount) | Lays grass down; it stands again by recover |
stats | FoliageStats: chunks, chunksDrawn, instances, instancesDrawn, layMs, frameMs |
onChange(listener) | Told when its look changes |
Its changes are this screen's and stay over whatever look comes after. client.gust(at, gust) is a gust on this screen. foliageLookParts, layFoliageLook and foliageStyleLook read and lay looks as the engine does.
Limits
- 255 styles a layer (values 1 to 255); value 255 is grass from tiles where
on.stylesays none. - Four gusts at once; a gust's strength is held to 3.
- Bushes (models) don't take
shape,colors.foot,tip,stops,saturation,ground,lightorscatter; they take the layer'swind, not a style's; they shake but are never laid flat or crushed-looking; far away they are their lightest model, then nothing (no pictures). - Grass grown from tiles (
on) is nothing to the rules (no hiding, slowing or burning) untilgrowOnTilespaints it. - Grass never casts shadows; by default none fall on it.
walkedandflattenare each screen's own; trampling that matters istrample: 'crush'.
What costs time in play
- Setting or clearing a style with
speed, or withwalkable: false, while the game runs cuts the navigation again where it changed, a piece of 12 × 12 cells at a time: under a millisecond for a small patch, about a third of a millisecond for each piece a large paint reaches and each unit size the game has (10 ms for a 20 × 100 band, 41 ms for the whole of a 128 × 128 map, one unit size). A layer with awalkable: falsestyle also cuts the pieces beside a painted cell at a piece's edge. The calls of one tick are cut together, each piece of the navigation once, as the tick ends (or as soon as something asks the navigation, so nothing finds its way on the ground as it was). Do it in a few ticks, not every tick. - The state (fire, ruin, trampling) costs nothing of the navigation.
- Hiding patches are worked out again only where cells change, once a tick at most.
Measured
Measured on an Apple M4 Max laptop, often while it was busy with other work; the browser numbers in headless Chromium with WebGPU, the rules on the CPU benchmark (npm run bench -- foliage).
| What | Scene | Measured |
|---|---|---|
| Triangles drawn | 256 × 256 meadow, 314,576 tufts | 1.57 M before chunks, 0.16 M with them |
| Laying 100 changed cells | 256 × 256 meadow | 0.6 ms in a browser (headless, the frame held to 60 a second, load 1–3); 0.30 ms on the CPU benchmark (load 1–3) |
| Laying the whole map | 256 × 256 meadow | in slices of at most 4.0 ms, done in 225 ms, no frame over 16.8 ms (headless, the frame held to 60 a second, load 1–3); 41 ms at once on the CPU benchmark, nine tenths of it working out each tuft (its place, size, turn and colour) |
| A style's colour changed | in play | 4.3 ms (only its chunks laid again); sway 0.05 ms |
| A style's look changed in the editor | 256 × 256 | 4.7 ms median |
| Draw calls for grass and bushes | six grass styles and two bush models | 32–37 (bushes 9); six styles of one shape cost what one does. The meadow 21–26, a fire 26–28 (headless, load 4–8) |
| Grass from tiles | 256 × 256, 85% grass tiles | 0.033–0.041 ms CPU a frame, 23–27 draws, whole lay 13.8 ms in pieces of ≤ 3.9 ms, 100 tiles painted 0.1–0.2 ms |
| Foliage CPU a frame | 200 units walking through grass | 0.11–0.18 ms in a browser (headless, load 1–8); 0.039 ms on the CPU benchmark (load 1–3). The meadow still 0.015 ms |
| Allocated a frame | the meadow, 200 walkers; grass from tiles, still (CPU benchmark, load 1–19) | 0.30–0.59 KB and 0.23 KB, from run to run (0.40 and 0.45 before), numbers boxed by the JavaScript engine: no object, array or closure is made a frame |
| Fire, the rules' tick | 2,000–2,500 cells burning, 8 teams | 0.091 ms; the worst tick 0.24 ms (CPU benchmark, load 1–3; 0.105 and 0.84 ms at load 11–19) |
| Hiding patches worked out whole | 256 × 256 | 0.8–1.2 ms (at most once a tick, where cells changed) |
| Grass waiting to grow back | 20,000 cells | nothing measurable a tick |
| What each team remembers of cells changed out of its sight | 20,000 cells cut, 8 teams; CPU benchmark, load 11–15 on 14 cores | 0.023–0.028 ms a tick (0.07 ms each time the fog is worked out, every third tick) |
| Burning trees (Let trees burn) | 2,000 trees alight | 0.078 ms a tick; 0.0003 ms with none |
A speed style painted in play | 10 × 10 cells of the 256 × 256 meadow, one unit size; CPU benchmark, load 17–21 on 14 cores | 0.58–0.70 ms a tick (6–9 ms a call measured earlier on another scene) |
Large speed paints in play | 128 × 128 rolling ground, 40 huts, one unit size (two); CPU benchmark, load 11–15 on 14 cores | a 20 × 100 band 10.3 ms (23.9); over 66 of the 121 pieces 23.2 ms (49.4); the whole map 43.4 ms (87.4). A building put up and taken down again on pieces painted in play 0.21 ms (0.61), on ground painted as the world was made 0.16 ms (0.32) |
Four speed paints in one tick | four 5 × 5 squares side by side, the same | 0.64–0.72 ms a tick, cut together (2.3–2.4 ms cut one by one) |
| The ground reshaped and painted in one tick | 10 × 10 cells levelled, then painted, the same | 12.4–14.0 ms a tick (12.9–13.6 ms cut one after the other): most of it the reshape's, which reads the whole ground again |
| Fire on the screen, foliage CPU a frame | about 3,800 cells burning, 1,590–1,600 in view (a field of 90% grass, the camera at the game's farthest); headless, load 1–8 | 0.025–0.17 ms |
| The first fire | 415 cells set alight, the warm-up on; headless, the frame held to 60 a second, load 1–3 | no frame over 20 ms (the worst 16.8 ms) |
| Fire on the screen, uploads | the same | no texture upload beyond the cells newly lit |
| Fire on the screen, effects at once | the same | flames 32, smoke 8, embers 8, lights 4 |
GPU time and frame times on a real display, at full resolution and its rate, are still to be taken: a headless page draws to no display.