Skip to content

Grass and bushes ​

Grass and bushes cover the ground of your map: they sway in the wind, part as units walk through, and can hide who stands in them. They can be cut, crushed, set on fire, put out and grown back, and every part of how they look is yours to set. The game decides what grass does, the same on every machine; each screen decides how it looks.

Quick start ​

In the empty starter, add these lines to src/rules.ts, after createGame:

ts
// src/rules.ts
import { createGame, defineCellLayer, foliagePack, presetFoliageStyles } from '@v12-engine/engine'

export const game = createGame({ id: 'my-game', name: 'My game' }) // the starter has this already
// a layer of cells: value 1 is a meadow, which burns and grows back
game.cellLayer(defineCellLayer({ id: 'grass', name: 'Grass', values: [{ value: 1, name: 'Meadow', foliage: presetFoliageStyles.meadow }] }))
// the foliage pack: hiding, trampling, fire and growing back, by each style's rules
game.pack(foliagePack({ layer: 'grass' }))
// paint a round meadow in the middle of the field as the map starts
game.onStart((world) => {
	world.setCells('grass', { kind: 'circle', x: 16, z: 16, radius: 10 }, 1)
})

And in src/view.ts, add cellLayers to the view's ground, beside its tiles:

ts
// src/view.ts: presetView({ ..., ground: { cellLayers, tiles: ... } })
import { foliage, presetFoliage } from '@v12-engine/client'

// how each layer of cells is drawn, by its id: this one as the ready-made meadow
export const cellLayers = { grass: foliage(presetFoliage.meadow) }

Done: a round meadow with a few flowers fills the middle of the field. It sways, parts round your scout as it walks, and grows on the road too (keeping off roads is a setting: placing.keepOff.roads).

How it fits together ​

  1. A layer of cells. The map is a grid of cells, one world unit a side. A layer (defineCellLayer) holds a number from 0 to 255 for every cell; 0 is no grass.
  2. Styles painted on cells. Each number is a style: meadow, tall grass, wheat. You paint them in the editor, in the map file, or from code (world.setCells, setFoliage).
  3. A look for each style, on the screen. foliage(look) in your view says how the layer is drawn, and styles[n] how style n differs (FoliageLook). Each screen draws its own; nothing it draws changes the game.
  4. Rules for each style, in the game. The value's foliage field says what the style does (FoliageStyleDef): hides, slows, burns, grows back. foliagePack makes it happen, the same on every machine.
  5. A state for each cell. As the game goes, each cell is whole, flattened, cut, burning, burnt or growing (FOLIAGE_STATE). The rules change it (ignite, ruin, regrow, trampling) and every screen draws it.

A game that only wants grass to look at can leave the pack out: then nothing hides, burns or is crushed.

I want to… ​

I want to…Go here
Change how grass looks: height, colour, shape, windCustomising grass and bushes
Copy a ready look: lawn, wheat, reeds, swamp, hedge, bushesTen looks to copy
Make a meadowMake a meadow
Give one patch its own grassGive a patch its own grass
Draw my own bush modelsMake bushes of my own
Grow grass wherever the ground is painted greenGrow grass on ground tiles
Make grass follow the wind, or blow a gustMake grass follow the wind, wind recipes
Leave trails where units walkLeave trails in grass
Hide units in tall grass, or set up an ambushHide units in tall grass, hiding recipes
Set grass on fireSet grass on fire, fire recipes
Burn grass with a spellBurn grass with an ability
Put a fire out, or stop it spreadingStop a fire spreading, water and rain spells
Cut, mow or crush grassCut or crush grass
Make grass grow backMake grass grow back
Change grass while the game runs: seasons, night, blightLooks that change in play
Let trees burn tooLet trees burn
Paint and set grass in the map editorGrass and bushes in the editor
See every field, call and event in one placeGrass and bushes: the reference
Find a recipe for something elseGrass cookbook

What it costs, and its limits ​

  • A 256 × 256 meadow draws in about 21 to 26 draw calls and lays out in slices of 4 ms over a few frames; a hundred changed cells take well under a millisecond. Measured numbers are in the reference.
  • Fire in the rules costs about 0.1 ms a tick with two thousand cells burning; at most 4,000 cells burn at once (fire.maxBurning).
  • A layer has up to 255 styles. At most four gusts blow at once on a screen.
  • The player's Tall grass setting draws none, a third or all of the tufts; the rules are the same whatever it is, so grass hides as much at None.
  • Grass never casts shadows, and bushes far away are their lightest model, then nothing. Painting a style that slows (speed) or blocks (walkable: false) in play cuts the navigation again: do it now and then, not every tick.