# Enums and structs
Phases / states as `enum`, per-player or per-round state as `struct` instances, struct methods. Open when the game has phases or a group of fields per player / team.

## Enums and structs (compile-time only: the JSON is exactly the hand-written code)
- A struct DECLARATION does NOT apply defaults: call `x = Run()` (game state, e.g. in player_join) or
  `x ??= Profile()` (save state). The compiler warns (`struct-no-init`) if neither appears in the file.
- `enum Phase { LOBBY, GAME, END }` — numbers 0, 1, 2 (`GAME = 5` sets a value, the next is +1). Members
  UPPERCASE (`game`, `save`, `if`... are keywords). `Phase.GAME` is a number literal everywhere (if,
  arguments, factories `item("x", Slot.FOOD)`, `inline var`, parameter defaults). A misspelled member is a
  compile error. Prefer enums to magic numbers and phase texts: `state == Phase.GAME` with -O is ≈1.5,
  `state == "game"` ≈8. Never-set variable = 0 = the first member.
- `game var state: Phase = Phase.LOBBY`, `function f(p: Phase)` — `: Type` gives compile checks only
  (member of another enum, number that is no member, text = error); `state += 1` / arithmetic stay unchecked.
  A saved enum (`save var x: Phase`) — give every member an explicit `= N` (the order becomes save data).
- Name for output: `Phase.name(Phase.GAME)` → "GAME" (free); `Phase.name(state)` / `state.name()` → a
  temp list + `l[v]` (≈9: list-argument actions are costly); for repeated use `game var NAMES = Phase.names`
  once, then `NAMES[state]` (`names` is indexed by the value itself; holes are "?"). `"${state}"` prints the
  NUMBER. Player-facing text: labels `enum Phase { LOBBY "&eЛобби", GAME "&aИгра" }` + `Phase.label(x)` /
  `Phase.labels`. `Phase.count` — number of members. Your enums are not action enums: `merging = "SPACES"` stays text.
- `struct Run { phase = Phase.LOBBY, hp = 20, kills = 0, owner }` — fields; defaults are constants; `owner`
  (no default) is never touched by `Run()`/`??=`; `phase: Phase` types a field without a default.
  An instance is plain variables `<instance>_<field>`: `game var %player%: Run` → `%player%_hp`,
  `%player%_kills`… per player; `game var round = Run()` → `round_hp`… (set in world_start).
  `%player%.hp` IS `%player%_hp` (old and new forms mix); `%killer%.kills` is the killer's copy (no extra declaration).
  Name-keyed (`%player%`, `%killer%`, `%victim%`...) and UUID-keyed (`%uuid%`, `%player_uuid%`, `%killer_uuid%`...)
  instances are different variables: with `save var %uuid%: Profile` write `%killer_uuid%.coins`, not
  `%killer%.coins` (compile error `struct-key-mismatch` with the fix). `%player% = Profile()` on a SAVE instance in
  player_join wipes progress on every join (warning `save-reinit`) — use `??=`.
- `x = Run()` sets every field that has a default (in field order), `x = Run(hp = 30)` overrides one (named
  only), `x ??= Profile()` sets only fields that do not exist yet (one check per field: a field added later
  reaches old players), `x = y` copies field by field. `x.hp += 1`, `x.items[0]`, `x.hp++`, `"${x.hp}"` work as on
  any variable. `x = Run(kills = x.hp)` is an error (hp was already reset above — read it first).
- Save + game state of a player under one name: `save var %player%: Profile` + `game var %player%: Run`
  (field names must differ; `x = y` copy is then an error — copy fields one by one).
- Another player's instance by name (world loop, `line var w`): declare it in that function/lambda before use —
  ``repeat::for_each_in_list(value::all_players_names) { i, w -> game var `${w}`: Run; `${w}` = Run() }``;
  ``save var `${w}`: Profile; `${w}`.wins += 1``. Per team/arena: numbered instances ``game var `team${i}`: Team``.
- Struct parameters and methods — `inline function` (the body is pasted with the instance name):
  `inline function heal(self: Run, n) { self.hp = min(self.hp + n, 20) }` → `heal(%player%, 5)`, `%player%.heal(5)`,
  ``heal(`${w}`, 2)`` = `%player%.hp = min(...)` written by hand. Pass an instance NAME (`%player%`, `` `${w}` ``, `round`,
  another struct parameter); a number or text is `inline-struct-arg`. One name for different structs is fine
  (`reset(self: Run)` and `reset(self: Profile)`: picked by the receiver's type). `x.f(args)` = `f(x, args)` for any
  own function `f` (inline or normal) when `x` has no action `.f`.
- An instance is not a value: no `"$x"` (write `"${x.hp}"`), no `x == y`, no lists of structs, no `T()` outside
  `=` / `??=`, no struct parameters of a normal `function` (use `inline function`; a function called from a player
  event already sees that player's `%player%.hp`). No inheritance, virtual methods, nesting.
