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
| Written | Takes | Default | What it does |
|---|---|---|---|
let NAME | a name | — | What callers write. Required. |
param NAME | a 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, bare | an 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 height | Value | Meaning |
|---|---|---|
$waterBedHeight | 0.4dmm | The bottom of standing water: two print layers. |
$waterSurfaceHeight | 2.6dmm | Calm 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. |
$dirtEdgeHeight | 3.4dmm | Where dirt meets a tile edge. |
$groundHeight | 5dmm | Open land. |
$floorHeight | 7.4dmm | A built floor's top. |
$floorEdgeHeight | $floorHeight | Where a built floor meets a tile edge: flush. |
$shortWallHeight | 16.55dmm | Half the wall top. |
$wallHeight | 33.1dmm | The standard wall top. |
$tallWallHeight | 72.3dmm | The 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.
