# Build a WebAssembly text (WAT) robot

Write a robot in WebAssembly text for Clunkyard's Reflex class. The WAT
starter includes the robot SDK's imports and record offsets at build time.
You write the module and control its memory directly.

## Build, try and debug

### WAT starter

In the workbench, create a WAT robot and edit `bot.wat`. Save, wait for the build and queue [hosted practice](https://clunkyard.com/manual#practice).

See the [local player kit](https://clunkyard.com/sdk/player-kit) for your platform and toolchain setup.

```sh
npx @clunkyard/cli new wat my-robot
cd my-robot
npx @clunkyard/cli doctor
npx @clunkyard/cli build
npx @clunkyard/cli match
npx @clunkyard/cli replay
```

## Define the module

Put the SDK include inside `module` before your own definitions. Export one
memory and the two functions the match host calls. Each function returns
zero to continue the match.

```wat
(module
  ;; @include clunkyard
  (memory (export "memory") 1)
  (func (export "clunkyard_start") (result i32)
    i32.const 0)
  (func (export "clunkyard_turn") (result i32)
    (drop (call $cy_drive_throttle (f64.const 0.5)))
    (drop (call $cy_drive_turn (f64.const 0.33)))
    i32.const 0))
```

The starter build expands the include before assembling the module. If
`wat2wasm` reports an undefined `$cy_` function, use the starter build so
the SDK can supply it. To read a contact, reserve space in memory, call the
import and check `$CY_OK` before loading the record. See the
[ABI 4 reference](https://clunkyard.com/sdk/wasm#reference) for function signatures and the
[turn trace](https://clunkyard.com/manual#history) for runtime errors.
