Element · the document

let

A name for a value, a thing or a layer: the body tells which. let adds a name; override replaces a $ name, such as one of the library's heights, for its block. A $ name is read from the context every formula is called with; a plain name is read where it was written.

A tile in three strips of grass: a full lawn on the left, a thin patchy lawn in the middle, long tall blades on the right.
One let with a body, lawn, called three ways, left to right: with its defaults, with cover="35%" clearings=6, and through long-grass, a definition that calls lawn with longer blades. From docs/elements/demo/let.dtile.

Writing it

dharmatiles version="1.0.0"

tile size="1x1" seed=31 {
    let knollTop  "$groundHeight + 4dmm"            // a plain name, this tile's own
    let hump      "$groundHeight + [1dmm .. 2dmm]"  // a range: a little rise, its height left free
    let $treeLine "40dmm"                           // a $ name: in the context, read by what is called below
    override $waterBedHeight "0.8dmm"               // deeper water, everywhere on this tile
    stack { base; dirt { pin top="knollTop" at=center } }
}

A value is an expression: numbers with their units, plain and $ names, $x and $y (squares), $bottom (the surface under a body), + - * /, a range in brackets [a .. b] anywhere, and the functions sin cos (of an angle), abs, min, max, clamp and smooth. Trigonometry is computed in the program, so a build gives the same bits on every machine.

A thing or a layer

A let with a body names a thing or a layer in a parts file, and a tile calls it like an element. Its param lines are the controls a caller can set. The body is one entry written bare, or a stack of several. What the body is decides what the name is: one bare entry that is an unplaced thing names a thing, which can be placed with at= or scattered; any other bare entry, and every stack, names a layer.

dharmatiles version="1.0.0"

// library/parts/grass.dtile
let lawn {
    param length "3.5dmm .. 6.5dmm"   // blade length along the ground
    param height "1.5dmm .. 2dmm"     // how tall a blade stands
    param clearings 0                 // patches of bare earth per square
    param cover "100%"                // share of the ground grassed over
    stack {
        grass length=length height=height clearings=clearings cover=cover
    }
}

let long-grass {                   // a definition can call another
    stack { lawn length="5dmm .. 9dmm" height="2dmm .. 3dmm" }
}

A tile then writes use "parts/grass" lawn and lawn cover="80%" in its stack. A thing is the one thing that needs no stack:

// library/parts/trees.dtile: a bare thing, so it can be scattered
let oak {
    tree top="$bottom + 59dmm" { trunk height="18dmm" }
}

A let has a value or a body, never both, and a body is never empty. The old word for a body was define; it is refused (dt::retired) and the fix writes let over it.

Reference

A let with a body

WrittenTakesDefaultWhat it does
let NAMEa name—What callers write. Required.
param NAMEa default, or none—A control callers may set. A name of a kind, from kind= or else its default's: param length "3.5dmm .. 6.5dmm" is a length, param clearings 0 a plain number, and a caller that writes length=5 is asked for 5dmm (dt::unit). Usable anywhere a let of its kind may be, in a formula as well as a control's whole value. With no default, write kind=, and the caller must give it.
one entry, barean element or a name—What it makes. A bare unplaced thing names a thing; any other bare entry names a layer. Exactly one, and not beside a stack.
stack—What it makes, when there are several entries. A stack is always a layer. One, and not beside a bare entry.

The library's heights

Library heightValueMeaning
$waterBedHeight0.4dmmThe bottom of standing water: two print layers.
$waterSurfaceHeight2.6dmmCalm water.
$waterEdgeHeight$waterSurfaceHeight + 0.6dmm * cos(720deg * ($x - $y))Water at a tile edge: two waves a square, so water tiles butt any way round.
$dirtEdgeHeight3.4dmmWhere dirt meets a tile edge.
$groundHeight5dmmOpen land.
$floorHeight7.4dmmA built floor's top.
$floorEdgeHeight$floorHeightWhere a built floor meets a tile edge: flush.
$shortWallHeight16.55dmmHalf the wall top.
$wallHeight33.1dmmThe standard wall top.
$tallWallHeight72.3dmmThe tall wall top.

They come from library/parts/heights.dtile, and every tile sees them without a use line.

Notes

Calling a body. A place given to the call (at=) passes through to the elements inside, and a call's top= is the height of the tree inside it (oak top="$bottom + 40dmm"). Expert controls, such as a tree's branch knobs, are allowed only in parts files. Parts that call each other in a loop are an error (dt::loop). A tile has no params. A part has no tile of its own, so a point it writes ("(50%, 0.5sq)") is worked out at each place it is called, and a $ name it reads comes from its caller: check refuses a call whose context lacks one. A let with a body is written at the top level of a file.

Two kinds of name. A $ name is read from the context: the library installs its heights at the root, an element sets $x $y $d $a $bottom $region at each point it builds, and let $name installs one of your own for its block. A plain name (let knollTop …, a definition's param) is declared once, where it is written, and never passed to a definition you call. let $name on a name the context already has is refused (dt::name) and the message offers override; override of a name nothing installed, or of one an element sets, is refused. A plain name that is a word of the language (x y d a parent, a function, a weighting or a unit) is refused too. let and override work in any block, such as scatter { override $treeLine "30dmm"; tree … }, and hold for that block and everything called from it.

An override reaches library expressions. A $ name is read where it is used, so overriding $waterSurfaceHeight at the top of a tile moves $waterEdgeHeight with it. An override of a name that does not exist is refused, with a nearby name if there is one, so a misspelt override is caught instead of doing nothing. Overriding an edge height means the tile no longer meets standard tiles without a step. dharmatiles check --explain lists each override (override $waterBedHeight 0.4dmm → 0.8dmm).

Kinds. A length carries its unit (4dmm), an angle deg, and a count or a fraction is a plain number; the kinds are checked before any build. $x and $y are squares counted, plain numbers, so $groundHeight + $x is refused (dt::type); scale them with a length or inside sin or cos, as the tide does. A range is a set: [0dmm .. 1dmm] - [0dmm .. 1dmm] is [-1dmm .. 1dmm]. See units and kinds.

Seen from the language

The guide pages that use let: The tile document, Values and ranges, Names, Parts, Things and layers.