# DoorBench script

For engine 0.2. Files are `scripts/*.dbs`; pieces of script also go in places
(`enter`, `do`, `show`, `allow`) and screen fields (`value`).

## Functions

    function name(a, b: text, c: claims): number
      ...
    end

A value is a `number` unless it says otherwise: `text`, `flag`, `player`, or the name
of a table (a row of it). `-- starts a comment`. Names are small letters, digits and _.

## Statements

| | |
|---|---|
| `let x = 1` | a new value, or a new content for one; its kind never changes |
| `x = 2`, `x += 1`, `x -= 1` | |
| `player.cash += 5` | a field; `=`, and for numbers `+=` `-=` |
| `if a then ... elseif b then ... else ... end` | |
| `while a do ... end` | |
| `for i = 1 to 10 do ... end` | |
| `for c in claims where c.square == 3 do ... end` | rows; `players` for players |
| `for i in items of p do ... end` | an owned table; without `of`, this player's |
| `break`, `return`, `return x` | |
| `atomic ... end` | all of it happens, or none of it; see below |

`do` may be left out.

## Values

`+ - * / %` (whole numbers; `/` drops the rest), `==` `!=` `<` `<=` `>` `>=`, `and` `or`
`not`, `..` joins texts. `yes` `no` `none`. Numbers stop at 999,999,999,999,999; dividing
by zero gives 0; both are noted in DB.LOG.

In a text, `{value}` puts in a number or text, `{{` and `}}` are braces, `\n` a new line,
`\"` a quote, `|14` a colour (00 to 15 text, 16 to 23 behind it), `||` a bar.

Fields follow dots: `player.tool.better.name`. A link to a row that is not there reads as
0 or nothing. A choice field is compared with and set to the texts of its choices.

## Atomic

Other nodes are playing too. The world, shared tables and other players change only
inside `atomic`, where everything read is up to date. Nothing in it may ask or show
anything. `atomic function` makes a whole function so. A single change to a field of
`player` needs none.

`spend(player.cash, 10)` takes 10 if there are 10, and says whether it did. Use it rather
than testing and then subtracting.

`return` and `break` inside `atomic` finish the step first. One step may change 64 records.

## Special

| | |
|---|---|
| `spend(field, n)` | flag |
| `exists(row)` | is it (still) there? flag |
| `add(table)`, `add(table, player)` | a new row, `none` when full; in atomic |
| `remove(row)` | in atomic |
| `row(table, n)`, `row(owned, n, player)` | row number n, `none` when it is not in use |
| `setting.name` | what the sysop chose |

System fields: `player.name` `.turns` `.created_day` `.last_day` `.node`, `world.day`.
All but `turns` are read only.

## Functions that are there

| | |
|---|---|
| `random(a, b)` `chance(percent)` | |
| `min(a, b)` `max(a, b)` `abs(a)` `clamp(a, low, high)` `percent(a, p)` | |
| `len(t)` `upper(t)` `lower(t)` `trim(t)` `sub(t, from, count)` `find(t, part)` | `find` gives 0 for not there |
| `pad(t, n)` `padleft(t, n)` `times(t, n)` | |
| `str(n)` `grouped(n)` `number(t)` | `number` gives 0 for what is not one |
| `today()` `time_left()` `level()` `node()` `ansi()` `real_name()` | `real_name` is empty unless the sysop allows it |
| `player_count()` `find_player(name)` `online(player)` | |
| `mail(player, text)` | flag; 10 a day for each player, 50 kept |
| `mail_count()` `news(text)` `log(text)` | |
| `say(t)` `print(t)` `cls()` `pause()` `color(n)` `show(screen)` | |
| `goto(place)` `back()` `quit()` | the handler ends there |
| `ask_text(prompt, most)` `ask_number(prompt, low, high, usual)` `ask_yes(prompt)` | |
| `ask_key(prompt, keys)` `ask_message(prompt)` | |
| `list_clear()` `list_add(text, number)` `list_pick(title)` | `list_pick` gives the number, 0 for none |
| `list_top(text, number, most)` | keeps the highest, the highest first |
| `list_show(title)` | shows the list, with nothing to choose |
| `read_mail()` `read_news()` | |

Those from `say` down are for players only, not for maintenance.

## Events

In project.json: `new_player` `login` `logout` `hangup` (shows nothing), `day` and
`player_day` (atomic functions). A shared table's `on_row_day` is an atomic function
taking a row. In maintenance each call is one step: if it goes wrong, it is noted and
the day goes on.

## Asking, then changing

Ask first, then do everything in one `atomic`: take the turns and the money there too, and
look again at whatever may have changed while the player thought. Then hanging up at any
question loses nothing. The `turns` of a choice in a place are taken before its handler
runs, so use them only for handlers that ask nothing. `games/goldfield` is written this way.

## Limits for one handler

200,000 instructions between questions (5,000,000 in maintenance), 500 questions, 32
calls deep, 200 numbers and 24 texts in a function, 255 characters in a text (one longer is an
error, except straight in `say` or `print`; one that grows longer is cut and noted), 16 KB of
code in a function. Past one, the handler stops, its unfinished atomic step is dropped,
and DB.LOG says where.
