Compare commits

..
19 Commits
Author SHA1 Message Date
twisla 4252515c2f Merge pull request 'Site: an Apps page, and a front page that points to it' (#105) from apps-page into main
Site / build (push) Successful in 14s
CI / build (push) Successful in 3m26s
2026-10-15 10:38:05 +00:00
twislaandClaude Opus 5.5 f1c497794b Site: an Apps page, and a front page that points to it
Site / build (pull_request) Successful in 12s
The front page's App cards were nine, and had stopped at v0.18.0: no
SSH, no Shell, no games, no compass, no MeshCore. A new page, /apps/,
has all twelve in the Launcher's order and the planned messenger, each
with its icon, what it does, a fact, its guide page, and a screen from
the device (ten taken from it now). The front page keeps six of them,
links each to its row, and says how many there are; the menu's Apps goes
to the page.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0162FokPdvY2KsS4NBfwyWPk
2026-10-15 12:36:13 +02:00
twislaandClaude Opus 5.5 a618eee38e Site: SSH, Shell and Games in the icon sheet
The device's own icons for the three, at the website's 16 pixels, in
three more cells of the sheet.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0162FokPdvY2KsS4NBfwyWPk
2026-10-15 12:36:13 +02:00
twisla d03ad01cac Merge pull request 'Games: Minesweeper, 2048 and Asteroids, and Pong by tilting' (#104) from more-games into main
CI / build (push) Successful in 1m11s
Site / build (push) Successful in 15s
2026-10-15 10:36:06 +00:00
twislaandClaude Opus 5.5 77260af157 Games App: Asteroids, and Pong by tilting
CI / build (pull_request) Successful in 3m2s
Site / build (pull_request) Successful in 12s
Asteroids is the sixth Game: lean the device to turn, ; for thrust, .
to fire, the arrows too for a device on a table. In Pong, 0 switches
between the keys and tilting and 9 turns the tilt round, both
remembered. Both read the accelerometer through the same smoothing, and
take however the device is held at the start as the middle.

Played on the device: the default way of Pong's tilt was swapped after
trying it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0162FokPdvY2KsS4NBfwyWPk
2026-10-15 12:06:29 +02:00
twislaandClaude Opus 5.5 e1d1ec899e Pong: a paddle steered by tilting
The player's move goes from -1 to 1 instead of three steps, and two
rules make it from the device's tilt: where the paddle is asked to be
(it rolls to the low side, about 15 degrees for the whole height) and
how to get there without overshooting. Host-tested.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0162FokPdvY2KsS4NBfwyWPk
2026-10-15 12:06:29 +02:00
twislaandClaude Opus 5.5 af9077cb94 Games: Asteroids as a state machine, and a lean as a turn
A ship, rocks that break in two and again in two, short-lived bullets,
three ships and waves that grow, on a field that wraps round. Its turn
comes from -1 to 1, which tiltTurn makes from the device's lean around
the angle it was held at when the game began. Host-tested.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0162FokPdvY2KsS4NBfwyWPk
2026-10-15 12:06:29 +02:00
twislaandClaude Opus 5.5 b1e6242b29 Games App: Minesweeper and 2048
Two more Games in the row of icons, which now holds five. Minesweeper
moves a cursor (a held arrow runs it along), digs with Enter, flags with
f, shows where the mines were when it is lost, and keeps the fastest
cleared field. 2048 slides with the arrows and keeps the best score; it
has no clock, so nothing to pause. Neither keeps the screen lit.

Played on the device: Minesweeper's cells went from 8 to 12 pixels to be
read, and its field to 20 x 9.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0162FokPdvY2KsS4NBfwyWPk
2026-10-14 18:46:49 +02:00
twislaandClaude Opus 5.5 fcc5533c83 Games: Minesweeper and 2048 as state machines
Minesweeper on 20 x 9 with 28 mines, laid at the first dig and never
under or next to it, with flags and the open-around-a-number dig; 2048
on 4 x 4, a tile merging once a move. Seeded, host-tested.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0162FokPdvY2KsS4NBfwyWPk
2026-10-14 18:46:49 +02:00
twisla b9d352555f Merge pull request 'Devlog: Not finished until it plays Tetris' (#103) from devlog-games into main
Site / build (push) Successful in 13s
2026-10-13 21:18:11 +00:00
twislaandClaude Opus 5.5 19d4d4577d Devlog: Not finished until it plays Tetris
Site / build (pull_request) Successful in 10s
The Games App of v0.24.0: why a firmware wants games, what Pong, Snake
and Tetris carry, how little room they take, and what playing changed
in the design. With five screenshots from the device.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0162FokPdvY2KsS4NBfwyWPk
2026-10-13 23:14:56 +02:00
twisla 8cbc641d20 Merge pull request 'Games App: Pong, Snake and Tetris' (#102) from games into main
Site / build (push) Successful in 13s
CI / build (push) Successful in 3m18s
2026-10-13 21:09:00 +00:00
twislaandClaude Opus 5.5 ec15c2f52a Games App: Pong, Snake and Tetris
CI / build (pull_request) Successful in 2m20s
Site / build (pull_request) Successful in 10s
A new App (issue #101) that shows its three Games as icons, as the
Launcher shows Apps, and plays the one chosen. A Game left in the middle
is found again paused; one being played keeps the screen lit. Tetris
plays Korobeiniki on a speaker channel of its own, faster with each
level: 0 switches it, 8 and 9 set its volume, both remembered. The drop
is on 1, away from the arrows. The best of each Game is kept.

Played on the device. The speeds are as tuned there.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0162FokPdvY2KsS4NBfwyWPk
2026-10-13 22:59:05 +02:00
twislaandClaude Opus 5.5 68d50b320f Games: Pong, Snake and Tetris as seeded state machines, and a tune
The rules of the three Games (issue #101), with no screen and no
keyboard in them: time and keys go in, the new state and what happened
come out, and a seed decides the dice. With a player for a tune a note
at a time, and Korobeiniki's notes. Host-tested.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0162FokPdvY2KsS4NBfwyWPk
2026-10-13 22:59:05 +02:00
twislaandClaude Opus 5.5 5c660d866d Keys: which ones are held, not only which were pressed
Apps heard a key once, when it went down. A Game that moves something
for as long as a key is held (issue #101) asks the key mapper instead.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0162FokPdvY2KsS4NBfwyWPk
2026-10-13 22:59:05 +02:00
twisla 8aa2848c5f Merge pull request 'Updates: a download that breaks is carried on' (#100) from resume-download into main
CI / build (push) Successful in 1m4s
Site / build (push) Successful in 12s
2026-10-11 16:30:05 +00:00
twislaandClaude Opus 5.5 472f13260f Updates: a download that breaks is carried on
CI / build (pull_request) Successful in 2m6s
Site / build (pull_request) Successful in 11s
When the connection breaks during a release download (issue #54), the
device waits up to a minute for Wi-Fi, asks for the rest of the file
with a Range request and carries on: the slot and the hash so far are
kept. It gives up after three tries in a row that bring nothing.
Nothing is kept across a restart.

Carrying on asks for 64 KB free, not 80: between two connections of an
install 78 KB is free, and the first attempt on the device was refused
for that. update damage cut now breaks every connection after n bytes.

Tried on the device: v0.23.0 installed over four connections, and a
download that brought nothing given up with nothing switched.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0162FokPdvY2KsS4NBfwyWPk
2026-10-11 18:26:39 +02:00
twislaandClaude Opus 5.5 468dc0dc0d Updates: when an answer is the rest of the same file, and how long to try
For carrying on a download that broke (issue #54): the head's ETag and
Content-Range are read, a 206 is checked to be the rest of the same
file, and a policy says how many tries a download is worth. Host-tested.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0162FokPdvY2KsS4NBfwyWPk
2026-10-11 18:26:39 +02:00
twisla 7e14e270df Merge pull request 'Updates: a newer release installs by itself, after a question' (#99) from auto-update into main
CI / build (push) Successful in 1m5s
Site / build (push) Successful in 16s
2026-10-11 15:54:03 +00:00
75 changed files with 4682 additions and 72 deletions
+4
View File
@@ -186,6 +186,10 @@ Installing a new firmware image without a USB cable: pushed over Wi-Fi from the
A tag `v*` on the project's Gitea with a signed **Update File**, a factory image for USB, the ELF to decode crashes and checksums, built and published by CI. The device reads them to look for updates.
_Avoid_: flash, upgrade (alone)
**Game**:
Something played inside the Games App: Pong, Snake, Tetris, Minesweeper, 2048 or Asteroids. A Game is not an App: it has no tile in the Launcher, and is found again paused when the Games App is left in the middle of one.
_Avoid_: mini-app, toy
**Update File**:
One signed file (`.ota`) that carries a firmware version, its image and a signature. The same file works over Wi-Fi and from the SD card. Anything not signed with the project's key is refused.
_Avoid_: binary, bin
+9 -2
View File
@@ -16,6 +16,7 @@ What it does today:
- **Shell:** the firmware's commands on the device itself, with completion, including `ping`, `nslookup`, `port`, `traceroute`, `tls` and `ifconfig`.
- **System:** load, tasks, memory, network, battery, live.
- **SSH:** a terminal on another machine, with a password or the device's own key.
- **Games:** Pong, Snake, Tetris, Minesweeper, 2048 and Asteroids.
- **VPN:** a WireGuard tunnel.
- **Everywhere:** Fn+h lists the keys of the screen you are on; Fn+p takes a screenshot.
- **Updates:** signed, from the project's server, the card or a PC, with a rollback if the new firmware fails.
@@ -104,7 +105,7 @@ To install from the SD card instead, copy the `.ota` file from `.pio/build/cardp
With no PC and no card, the device can install the project's releases itself (docs/milestones/R1.md). In **Settings → System → Firmware**:
- **Latest release** checks the server (Enter, or `c`) and says `v0.11.0 (new)` or `(current)`. Enter again opens the release: its version, date, size and the tag's message, with **Install** when it's newer. The download goes straight into the inactive slot, so no card is needed; the signature is checked after the first 160 bytes, before anything is written, and the image's hash at the end. The new firmware then runs on Probation as for any update.
- **Latest release** checks the server (Enter, or `c`) and says `v0.11.0 (new)` or `(current)`. Enter again opens the release: its version, date, size and the tag's message, with **Install** when it's newer. The download goes straight into the inactive slot, so no card is needed; the signature is checked after the first 160 bytes, before anything is written, and the image's hash at the end. The new firmware then runs on Probation as for any update. A download that breaks is carried on from where it was (issue #54): the device waits up to a minute for Wi-Fi, asks for the rest of the file, and gives up only after three tries in a row that bring nothing.
- **Older releases** lists the last ten, newest first. Opening an older one offers to go back to it, with a different question.
- **Settings → System → Check for updates** (on by default): once a day, with Wi-Fi up and the clock set, the device looks at the latest release and says `v0.11.0 is out: see Settings > System > Firmware`, once per version. It installs nothing by itself, and doesn't announce a version that already failed and rolled back on this device.
- **Settings → System → Install updates** (`When asked` by default; `By itself`, issue #52): a newer release found by the daily check is installed without being asked for. The device waits until no key was pressed for 2 minutes and nothing would be cut by a restart (a Track, a Capture, a copy, an upload, an SSH session, a web share), then asks on the screen for 30 seconds, lighting it if it was off: Cancel leaves it until the next day's check; Accept, or no answer, installs it and restarts. What was installed and when, and what was undone, is written in `/system/updates.log` on the card.
@@ -187,6 +188,12 @@ The SSH App (docs/milestones/N1.md, issue #2) is a terminal on another machine:
The terminal (`lib/term`, host-tested) understands what a shell, `less`, `top`, `nano` and `vim` send: the cursor, erasing, sixteen colours, scroll regions, the alternate screen; no mouse. Five text sizes with Ctrl and + or -, from 60 x 20 to 26 x 8, told to the far end; 100 lines of scrollback with Alt and up or down. The backtick key sends Esc. **Leaving the App doesn't end the session:** `SSH` shows in the Status Bar and the App finds it again. It costs 292 KB of flash (109 KB of it one table, for signing with the device's key) and about 50 KB of memory while a session is open, so it isn't started under 75 KB free and IRC doesn't connect while one is open.
## Games
The Games App (issue #101) holds six Games, shown as a row of icons like Apps in the Launcher. Two wait for a move: Minesweeper (20 x 9 with 28 mines, in cells of 12 pixels, laid at the first dig and never under or next to it; the fastest cleared field is kept) and 2048. Four run on a clock: Asteroids, steered by leaning the device (and Pong too, if `0` says so, tilted up and down) (the accelerometer, around the angle it was held at when the game began; `tiltTurn` in `lib/games` is host-tested), and Pong against the device (first to 7; the device's paddle has a top speed, so it can be beaten), Snake (38 x 19, the walls kill) and Tetris (10 x 20, the 7-bag, the next piece shown, a turn that shifts away from a wall, soft and hard drop). Each is a state machine in `lib/games`, host-tested: time and keys go in, the new state and what happened (a bounce, a line) come out, and a seed decides the dice, so a rule can be tested without playing.
Apps used to hear only key presses. `KeyMapper::held()` says which keys are down, for a paddle that moves while a key is held and for Tetris's repeat. An App that is in use with no key pressed says so (`App::keepsScreenOn`): the screen stays lit, and an update waiting for a quiet moment keeps waiting. A Game left in the middle is found again paused. The best of each Game (for Pong, the longest rally) is kept in internal flash. Tetris plays Korobeiniki, the folk tune it has always had, a note at a time on a speaker channel of its own (`lib/games/src/tune.h`, host-tested timing); `0` switches it, `8` and `9` set its loudness, both kept; it hurries with each level. The beeps and the tune obey Settings > This device > Sound & LED.
## VPN
A WireGuard tunnel (docs/milestones/N1.md), over whatever Wi-Fi the device is on: one peer, IPv4. Copy a client's `.conf` to the card as `/vpn/wg0.conf` and import it in Settings > Network > VPN (or `vpn import`); the configuration, private key included, is then kept in the device and never shown, and Settings offers to delete the file. A switch brings the tunnel up until the next restart; "Start with Wi-Fi" does it every time. It waits for the clock, which WireGuard needs. `VPN` shows in the Status Bar, bright once the server has answered.
@@ -234,7 +241,7 @@ The Shell App (docs/milestones/S1.md) runs the commands below on the device's ow
| `cp [-f] <from> <to>` / `mv [-f] <from> <to>` / `rm [-r] [-f] <path>` / `mkdir <path>` / `cancel` | What the Storage App does, with its rules: copy (folders too), move or rename, delete (`rm -r` for a folder and what's in it, as Unix has it), new folder. `-f` replaces a file that's in the way; `*` and `?` in the last part of a path (`ls`, `du`, `rm`, `cp`, `mv`) run the command for each name matched, 64 at most; a tab separates paths that hold spaces; `cancel` stops a copy or a delete |
| `install <path>` | Update from SD with that `.ota` file, as Settings → System → Firmware does |
| `update check` / `update list` / `update status` / `update install <tag>` | The project's releases on Gitea: look at the latest, list the last ten, say what's known, or download and install one |
| `update pretend <version>` / `update probe <host>` / `update damage cut\|flip <n>` / `update daily` | Pretend to run another version (so a release counts as an update), see whether a server's certificate is accepted, cut or damage the next download, run the daily check again |
| `update pretend <version>` / `update probe <host>` / `update damage cut\|flip <n>` / `update daily` | Pretend to run another version (so a release counts as an update), see whether a server's certificate is accepted, break each connection of the next download after n bytes (it is carried on, issue #54) or damage one byte of it, run the daily check again |
| `lora probe` | Finds the radio: chip, oscillator, antenna switch, DIO1 interrupt, noise floor |
| `lora status` | Radio settings, who's listening, packet and error counters, noise floor, task stack |
| `lora rx on` / `lora rx off` | Listens and prints each packet on the console |
Binary file not shown.

After

Width:  |  Height:  |  Size: 138 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 124 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 135 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 112 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 115 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 99 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 100 B

+32 -1
View File
@@ -1,6 +1,6 @@
# R1 — Releases
**Status:** in progress. Shipped: CI and signed releases on Gitea (issue #5), a release for every tag; updates from Gitea (#6, **v0.11.0**); one firmware with the Debug Console in it (#68, **v0.12.0**); CI in about a minute (#74, **v0.13.0**). Built, not released yet: installing a release by itself (#52). Not started: the Issues App (#4), release channels (#53), resuming a download (#54).
**Status:** in progress. Shipped: CI and signed releases on Gitea (issue #5), a release for every tag; updates from Gitea (#6, **v0.11.0**); one firmware with the Debug Console in it (#68, **v0.12.0**); CI in about a minute (#74, **v0.13.0**). Built, not released yet: installing a release by itself (#52). Built, not released yet: carrying on a download that broke (#54). Not started: the Issues App (#4), release channels (#53).
**Goal:** a tag is a release, built the same way every time and published where a device can find it.
@@ -156,6 +156,37 @@ With the setting on, `update pretend v0.22.0` and `update daily`:
- `/system/updates.log` got its line at each confirmed update.
- Not checked: "by itself" in the record (the release installed did not have this code yet), the 2 minutes without a key and the busy cases (host tests only), and a screen that was off lighting up. Keys sent through the Debug Console do not count as keys.
## Carrying on a download that broke (issue #54)
A release goes straight into the inactive slot (Q167), so a connection that broke meant starting again, and on a weak link a 2.5 MB file might never arrive whole.
### What the server gives (checked 2026-10-11)
A request with `Range: bytes=0-0` for a release's file gets `206`, `Content-Range: bytes 0-0/2510384` and a strong `ETag`.
### Decisions (2026-10-11)
| # | Decision |
|---|---|
| Q282 | **Carried on within the same install, in memory only.** The slot's write handle and the parser, with its hash so far, stay as they are while the connection is opened again with `Range: bytes=<where it was>-`. The parser never sees the break. |
| Q283 | **Nothing is kept across a restart** (the issue's second question): the slot cannot be reopened at an offset, so a download cut by a power-off starts over. |
| Q284 | **The answer must be the rest of the same file:** a `206` from that byte to the end, of the same total size, with the same `ETag` if both answers have one. The `ETag` goes in `If-Range`, so a file that changed comes back whole (a `200`), which is refused. What decides whether the whole is good is still the Update File's own signature and hash. |
| Q285 | **How long:** before each try, up to a minute for Wi-Fi to come back. Tries go on while each brings more bytes, 20 at most in one download; three in a row that bring nothing end it. |
| Q286 | **`update damage cut <n>`** now breaks each connection of the next download after n bytes, so both the carrying on and the giving up can be tried. |
### As built
- **`lib/release/src/resume.h`**, host-tested (5 tests with the head's new fields): `resumeRefusal`, `ifRangeFor`, `ResumePolicy`. `HttpHead` reads `ETag` and `Content-Range`.
- **`HttpsGet::openFrom`** asks for a range; **`GiteaSource`** in the Update Service does the waiting and the trying inside its `read()`.
- `update status` says how often the last download was carried on.
### Checked on the device (2026-10-11)
- **`update damage cut 800000`, then an install of v0.23.0** (2 510 384 bytes): it arrived over four connections, its hash matched, and the device restarted into it.
- **`update damage cut 0`:** three connections that brought nothing, then it gave up (`carried on 2 time(s)`), with the running slot still the one to boot.
- **Memory decided the first attempt.** Between two connections 78 KB was free, under the 80 KB a TLS connection asks for before it starts, so every try was refused and the download gave up. The install's own buffers (about 8 KB) are what is missing, and nothing else asks for memory while the screen is taken over: carrying on asks for 64 KB (the 52 KB peak and 12 KB). The lowest the heap went during the broken download was 20 KB, with a console client connected.
- Not checked: a real Wi-Fi drop in the middle (the wait for Wi-Fi to come back), and a file that changed on the server.
## One firmware: the Debug Console in every build (issue #68)
The issue asked for a token that could be set, so that Debug Builds could be published. The design round ended somewhere simpler: no Debug Builds. The console is in every firmware, off until switched on, with a token that belongs to the device (ADR 0010, which supersedes part of ADR 0004).
+4
View File
@@ -50,6 +50,10 @@ class App {
virtual bool retainsContent() const { return false; }
virtual void contentLost() {}
// True while the App is in use with no key being pressed (a Game being played, issue #101):
// it counts as activity, so the screen neither dims nor goes off.
virtual bool keepsScreenOn() const { return false; }
// True while something goes on in the App that is worth a look, whether it is in front or
// not (unread messages, a recording running): the Launcher marks its tile (issue #9).
virtual bool badge() const { return false; }
+65
View File
@@ -329,6 +329,71 @@ inline constexpr KeyHelp kVpn[] = {
{"; .", "up, down"},
};
// games: Games, the games
inline constexpr KeyHelp kGames[] = {
{"Enter", "this game"},
{", /", "the game before, the next"},
};
// games-pong: Games, Pong
inline constexpr KeyHelp kGamesPong[] = {
{"; .", "the paddle, while held"},
{"0", "steer with the keys, or by tilting"},
{"9", "tilting the other way up"},
{"Enter", "start, or start again"},
{"p", "pause; any key carries on"},
{"`", "the other games; this one waits"},
};
// games-snake: Games, Snake
inline constexpr KeyHelp kGamesSnake[] = {
{"; . , /", "turn"},
{"Enter", "start, or start again"},
{"p", "pause; any key carries on"},
{"`", "the other games; this one waits"},
};
// games-tetris: Games, Tetris
inline constexpr KeyHelp kGamesTetris[] = {
{", /", "left, right; held, it repeats"},
{";", "turn"},
{"z", "turn the other way"},
{".", "fall faster, while held"},
{"1", "drop"},
{"0", "the music, on or off"},
{"8 9", "the music, quieter, louder"},
{"Enter", "start, or start again"},
{"p", "pause; any key carries on"},
{"`", "the other games; this one waits"},
};
// games-mines: Games, Minesweeper
inline constexpr KeyHelp kGamesMines[] = {
{"; . , /", "move; held, it runs"},
{"Enter", "dig; on a number, the rest"},
{"f", "a flag, on or off"},
{"p", "pause; any key carries on"},
{"`", "the other games; this one waits"},
};
// games-2048: Games, 2048
inline constexpr KeyHelp kGames2048[] = {
{"; . , /", "slide every tile that way"},
{"Enter", "start, or start again"},
{"`", "the other games; this one waits"},
};
// games-asteroids: Games, Asteroids
inline constexpr KeyHelp kGamesAsteroids[] = {
{"lean", "turn: left or right"},
{", /", "turn, while held"},
{";", "thrust, while held"},
{".", "fire"},
{"Enter", "start, or start again"},
{"p", "pause; any key carries on"},
{"`", "the other games; this one waits"},
};
// ssh: SSH, the hosts
inline constexpr KeyHelp kSsh[] = {
{"Enter", "connect, open"},
+180
View File
@@ -0,0 +1,180 @@
#include "asteroids.h"
#include <algorithm>
#include <cmath>
namespace roro::games {
namespace {
constexpr float kTick = 0.004f;
constexpr float kDeadZone = 0.05f, kFullLean = 0.35f; // about 3 degrees of lean, and 20
constexpr int kRockScore[4] = {0, 100, 50, 20};
constexpr float kPi = 3.14159265f;
} // namespace
float tiltTurn(float lean, float level) {
float d = lean - level;
float magnitude = std::fabs(d);
if (magnitude < kDeadZone) return 0;
float t = std::min(1.0f, (magnitude - kDeadZone) / (kFullLean - kDeadZone));
return d > 0 ? t : -t;
}
float Asteroids::Rock::radius() const { return size == 3 ? 12.0f : size == 2 ? 7.0f : 4.0f; }
float Asteroids::wrap(float v, float max) {
while (v < 0) v += max;
while (v >= max) v -= max;
return v;
}
// The shortest way round the wrapping field.
bool Asteroids::touches(float ax, float ay, float bx, float by, float distance) {
float dx = std::fabs(ax - bx), dy = std::fabs(ay - by);
dx = std::min(dx, kWidth - dx);
dy = std::min(dy, kHeight - dy);
return dx * dx + dy * dy <= distance * distance;
}
Asteroids::Rock Asteroids::rock(float x, float y, uint8_t size) {
Rock r{};
r.x = x;
r.y = y;
r.size = size;
float speed = (size == 3 ? 18 : size == 2 ? 28 : 40) * (1 + 0.6f * rng_.unit()) * (1 + 0.08f * (wave_ - 1));
float heading = rng_.unit() * 2 * kPi;
r.vx = std::sin(heading) * speed;
r.vy = -std::cos(heading) * speed;
for (auto& corner : r.look) corner = static_cast<uint8_t>(185 + rng_.below(71));
return r;
}
void Asteroids::start() {
score_ = 0;
lives_ = kLives;
wave_ = 0;
over_ = false;
bullets_.clear();
placeShip(kWidth / 2, kHeight / 2, 0);
newWave();
}
void Asteroids::placeShip(float x, float y, float angle) {
x_ = x;
y_ = y;
vx_ = vy_ = 0;
angle_ = angle;
wait_ = 0;
shield_ = kShielded;
}
// Large rocks along the edges, never near the ship.
void Asteroids::newWave() {
wave_++;
rocks_.clear();
int count = std::min(kMaxWave, wave_ + 2);
for (int i = 0; i < count; i++) {
float x, y;
do {
x = rng_.unit() * kWidth;
y = rng_.unit() * kHeight;
} while (touches(x, y, x_, y_, 45));
rocks_.push_back(rock(x, y, 3));
}
}
uint8_t Asteroids::step(float seconds, const AsteroidsInput& in) {
uint8_t events = 0;
seconds = std::min(seconds, 0.25f);
bool fire = in.fire;
while (seconds > 0 && !over_) {
float dt = std::min(seconds, kTick);
seconds -= dt;
events |= tick(dt, in, fire);
fire = false; // once a press
}
return events;
}
uint8_t Asteroids::tick(float dt, const AsteroidsInput& in, bool fire) {
uint8_t events = 0;
thrusting_ = false;
if (wait_ > 0) { // between two ships
wait_ -= dt;
if (wait_ <= 0) placeShip(kWidth / 2, kHeight / 2, 0);
} else {
angle_ += std::clamp(in.turn, -1.0f, 1.0f) * kTurnSpeed * dt;
if (in.thrust) {
thrusting_ = true;
vx_ += std::sin(angle_) * kThrust * dt;
vy_ -= std::cos(angle_) * kThrust * dt;
}
float slow = std::max(0.0f, 1 - kDrag * dt);
vx_ *= slow;
vy_ *= slow;
float speed = std::sqrt(vx_ * vx_ + vy_ * vy_);
if (speed > kTopSpeed) {
vx_ *= kTopSpeed / speed;
vy_ *= kTopSpeed / speed;
}
x_ = wrap(x_ + vx_ * dt, kWidth);
y_ = wrap(y_ + vy_ * dt, kHeight);
if (shield_ > 0) shield_ -= dt;
if (fire && static_cast<int>(bullets_.size()) < kMaxBullets) {
float sx = std::sin(angle_), sy = -std::cos(angle_);
bullets_.push_back({x_ + sx * kShipRadius, y_ + sy * kShipRadius, sx * kBulletSpeed + vx_, sy * kBulletSpeed + vy_, kBulletLife});
events |= Shot;
}
}
for (auto& b : bullets_) {
b.x = wrap(b.x + b.vx * dt, kWidth);
b.y = wrap(b.y + b.vy * dt, kHeight);
b.life -= dt;
}
bullets_.erase(std::remove_if(bullets_.begin(), bullets_.end(), [](const Bullet& b) { return b.life <= 0; }), bullets_.end());
for (auto& r : rocks_) {
r.x = wrap(r.x + r.vx * dt, kWidth);
r.y = wrap(r.y + r.vy * dt, kHeight);
}
// A bullet breaks a rock in two smaller ones, or into nothing if it was small.
std::vector<Rock> born;
for (size_t i = 0; i < rocks_.size();) {
Rock& r = rocks_[i];
auto hit = std::find_if(bullets_.begin(), bullets_.end(), [&](const Bullet& b) { return touches(b.x, b.y, r.x, r.y, r.radius()); });
if (hit == bullets_.end()) {
i++;
continue;
}
bullets_.erase(hit);
score_ += kRockScore[r.size];
events |= Hit;
if (r.size > 1) {
born.push_back(rock(r.x, r.y, static_cast<uint8_t>(r.size - 1)));
born.push_back(rock(r.x, r.y, static_cast<uint8_t>(r.size - 1)));
}
rocks_.erase(rocks_.begin() + static_cast<long>(i));
}
rocks_.insert(rocks_.end(), born.begin(), born.end());
if (wait_ <= 0 && shield_ <= 0)
for (const Rock& r : rocks_)
if (touches(x_, y_, r.x, r.y, r.radius() + kShipRadius - 1)) {
events |= Died;
lives_--;
if (lives_ <= 0) {
over_ = true;
events |= Over;
} else wait_ = kRespawnWait;
break;
}
if (rocks_.empty() && !over_) {
newWave();
events |= Wave;
}
return events;
}
} // namespace roro::games
+82
View File
@@ -0,0 +1,82 @@
#pragma once
#include <cstddef>
#include <cstdint>
#include <vector>
#include "game_rng.h"
namespace roro::games {
// What steers the ship in one step: a turn from -1 (as fast as it goes, anticlockwise) to 1
// (clockwise), the thrust held or not, and the trigger pressed since the last step.
struct AsteroidsInput {
float turn = 0;
bool thrust = false, fire = false;
};
// How the device's tilt becomes a turn (issue #101's follow-up): leaning it is turning, the
// further the faster, around the angle it was held at when the game began. `lean` and `level`
// are "up" along the device's left-right axis, -1 to 1, as the accelerometer gives it.
float tiltTurn(float lean, float level);
// Asteroids: a ship in the middle of a field that wraps round, rocks that break in two and again
// in two, and bullets that live a moment. Three ships; a new wave when the last rock is gone.
// Lengths are pixels of the 240 x 123 field, times seconds.
class Asteroids {
public:
static constexpr float kWidth = 240, kHeight = 123;
static constexpr float kShipRadius = 4, kTurnSpeed = 4.7f; // radians a second, at a full turn
static constexpr float kThrust = 130, kDrag = 0.6f, kTopSpeed = 140;
static constexpr float kBulletSpeed = 190, kBulletLife = 0.85f;
static constexpr int kMaxBullets = 4, kLives = 3, kMaxWave = 8;
static constexpr float kRespawnWait = 1.5f, kShielded = 2.0f; // seconds out, then seconds safe
enum Event : uint8_t { Shot = 1, Hit = 2, Died = 4, Wave = 8, Over = 16 };
struct Rock {
float x, y, vx, vy;
uint8_t size; // 3 large, 2 middle, 1 small
uint8_t look[8]; // the outline: how far out each of its eight corners is, of 255
float radius() const;
};
struct Bullet {
float x, y, vx, vy, life;
};
explicit Asteroids(uint32_t seed) : rng_(seed) { start(); }
void start();
uint8_t step(float seconds, const AsteroidsInput& in);
float shipX() const { return x_; }
float shipY() const { return y_; }
float shipAngle() const { return angle_; } // radians, 0 pointing up, clockwise
bool shipShown() const { return wait_ <= 0 && !over_; }
bool shielded() const { return shield_ > 0; }
bool thrusting() const { return thrusting_; }
const std::vector<Rock>& rocks() const { return rocks_; }
const std::vector<Bullet>& bullets() const { return bullets_; }
int score() const { return score_; }
int lives() const { return lives_; } // ships left, the one flying included
int wave() const { return wave_; }
bool over() const { return over_; }
// For the tests: the rocks set by hand, and the ship put somewhere.
void setRocks(const std::vector<Rock>& rocks) { rocks_ = rocks; }
void placeShip(float x, float y, float angle);
private:
uint8_t tick(float dt, const AsteroidsInput& in, bool fire);
void newWave();
Rock rock(float x, float y, uint8_t size);
static float wrap(float v, float max);
static bool touches(float ax, float ay, float bx, float by, float distance);
Rng rng_;
std::vector<Rock> rocks_;
std::vector<Bullet> bullets_;
float x_ = 0, y_ = 0, vx_ = 0, vy_ = 0, angle_ = 0, wait_ = 0, shield_ = 0;
int score_ = 0, lives_ = kLives, wave_ = 0;
bool over_ = false, thrusting_ = false;
};
} // namespace roro::games
+25
View File
@@ -0,0 +1,25 @@
#pragma once
#include <cstdint>
namespace roro::games {
// The Games' own dice (issue #101): the same seed gives the same game, which is what lets their
// rules be tested. xorshift32; never 0.
class Rng {
public:
explicit Rng(uint32_t seed) : s_(seed ? seed : 0x9E3779B9u) {}
uint32_t next() {
s_ ^= s_ << 13;
s_ ^= s_ >> 17;
s_ ^= s_ << 5;
return s_;
}
int below(int n) { return n <= 1 ? 0 : static_cast<int>(next() % static_cast<uint32_t>(n)); }
float unit() { return (next() >> 8) / 16777216.0f; } // 0 up to 1
private:
uint32_t s_;
};
} // namespace roro::games
+108
View File
@@ -0,0 +1,108 @@
#include "mines.h"
#include <vector>
namespace roro::games {
void Mines::start() {
cells_.fill(0);
phase_ = Phase::Fresh;
laid_ = false;
flags_ = opened_ = 0;
elapsedMs_ = 0;
}
void Mines::count() {
for (int row = 0; row < kRows; row++)
for (int col = 0; col < kCols; col++) {
int n = 0;
for (int dr = -1; dr <= 1; dr++)
for (int dc = -1; dc <= 1; dc++)
if ((dr || dc) && inside(col + dc, row + dr) && mine(col + dc, row + dr)) n++;
cells_[at(col, row)] = static_cast<uint8_t>((cells_[at(col, row)] & ~kCount) | n);
}
}
void Mines::lay(const uint16_t* where, int n) {
for (auto& c : cells_) c &= static_cast<uint8_t>(~kMine);
for (int i = 0; i < n; i++) cells_[where[i]] |= kMine;
laid_ = true;
count();
}
// kMines of them, anywhere but under the first dig and the eight cells around it.
void Mines::layAwayFrom(int col, int row) {
int placed = 0;
while (placed < kMines) {
int c = rng_.below(kCols), r = rng_.below(kRows);
if ((c >= col - 1 && c <= col + 1 && r >= row - 1 && r <= row + 1) || mine(c, r)) continue;
cells_[at(c, r)] |= kMine;
placed++;
}
laid_ = true;
count();
}
// Opens a cell, and from a cell with no mine around it, everything up to the numbers.
uint8_t Mines::reveal(int col, int row) {
if (open(col, row) || flagged(col, row)) return 0;
if (mine(col, row)) {
cells_[at(col, row)] |= kOpen;
phase_ = Phase::Lost;
return Boom;
}
std::vector<uint16_t> todo{static_cast<uint16_t>(at(col, row))};
while (!todo.empty()) {
int i = todo.back();
todo.pop_back();
if (cells_[static_cast<size_t>(i)] & (kOpen | kFlag)) continue;
cells_[static_cast<size_t>(i)] |= kOpen;
opened_++;
if (cells_[static_cast<size_t>(i)] & kCount) continue;
int c = i % kCols, r = i / kCols;
for (int dr = -1; dr <= 1; dr++)
for (int dc = -1; dc <= 1; dc++)
if ((dr || dc) && inside(c + dc, r + dr) && !open(c + dc, r + dr)) todo.push_back(static_cast<uint16_t>(at(c + dc, r + dr)));
}
return Opened;
}
uint8_t Mines::settle(uint8_t events) {
if (phase_ != Phase::Lost && opened_ == kCols * kRows - kMines) {
phase_ = Phase::Won;
events |= Won;
}
return events;
}
uint8_t Mines::dig(int col, int row) {
if (!inside(col, row) || phase_ == Phase::Lost || phase_ == Phase::Won) return 0;
if (!laid_) layAwayFrom(col, row);
if (phase_ == Phase::Fresh) phase_ = Phase::Running;
if (!open(col, row)) return settle(reveal(col, row));
// An open number with all its flags placed: what is left around it is opened, mines included
// if a flag is wrong.
int flags = 0;
for (int dr = -1; dr <= 1; dr++)
for (int dc = -1; dc <= 1; dc++)
if ((dr || dc) && inside(col + dc, row + dr) && flagged(col + dc, row + dr)) flags++;
if (!around(col, row) || flags != around(col, row)) return 0;
uint8_t events = 0;
for (int dr = -1; dr <= 1; dr++)
for (int dc = -1; dc <= 1; dc++)
if ((dr || dc) && inside(col + dc, row + dr)) events |= reveal(col + dc, row + dr);
return settle(events);
}
uint8_t Mines::flag(int col, int row) {
if (!inside(col, row) || phase_ == Phase::Lost || phase_ == Phase::Won || open(col, row)) return 0;
cells_[at(col, row)] ^= kFlag;
flags_ += flagged(col, row) ? 1 : -1;
return Flagged;
}
void Mines::tick(uint32_t ms) {
if (phase_ == Phase::Running) elapsedMs_ += ms;
}
} // namespace roro::games
+56
View File
@@ -0,0 +1,56 @@
#pragma once
#include <array>
#include <cstddef>
#include <cstdint>
#include "game_rng.h"
namespace roro::games {
// Minesweeper (issue #101's follow-up): a field of 20 x 9 with 28 mines, cells big enough to read
// on this screen. The mines are laid at
// the first dig, never under it or next to it, so the first dig always opens something. Digging
// an open number whose flags are all placed opens what is left around it. The clock runs from the
// first dig to the end.
class Mines {
public:
static constexpr int kCols = 20, kRows = 9, kMines = 28; // the classic "intermediate" share: one cell in six or seven
enum Event : uint8_t { Opened = 1, Flagged = 2, Boom = 4, Won = 8 };
enum class Phase : uint8_t { Fresh, Running, Lost, Won };
explicit Mines(uint32_t seed) : rng_(seed) { start(); }
void start();
uint8_t dig(int col, int row);
uint8_t flag(int col, int row); // put on a closed cell, or taken off
void tick(uint32_t ms); // the clock, while the game runs
Phase phase() const { return phase_; }
bool open(int col, int row) const { return cells_[at(col, row)] & kOpen; }
bool flagged(int col, int row) const { return cells_[at(col, row)] & kFlag; }
bool mine(int col, int row) const { return cells_[at(col, row)] & kMine; }
int around(int col, int row) const { return cells_[at(col, row)] & kCount; } // mines next to it
int minesLeft() const { return kMines - flags_; } // by the flags placed, right or wrong
uint32_t seconds() const { return elapsedMs_ / 1000; }
// For the tests: the mines put where one wants them, before any dig.
void lay(const uint16_t* where, int count);
private:
static constexpr uint8_t kCount = 0x0F, kMine = 0x10, kOpen = 0x20, kFlag = 0x40;
static size_t at(int col, int row) { return static_cast<size_t>(row * kCols + col); }
static bool inside(int col, int row) { return col >= 0 && col < kCols && row >= 0 && row < kRows; }
void layAwayFrom(int col, int row);
void count();
uint8_t reveal(int col, int row);
uint8_t settle(uint8_t events);
Rng rng_;
std::array<uint8_t, kCols * kRows> cells_{};
Phase phase_ = Phase::Fresh;
bool laid_ = false;
int flags_ = 0, opened_ = 0;
uint32_t elapsedMs_ = 0;
};
} // namespace roro::games
+114
View File
@@ -0,0 +1,114 @@
#include "pong.h"
#include <algorithm>
#include <cmath>
namespace roro::games {
namespace {
constexpr float kTick = 0.004f; // the ball moves a pixel at most between two looks
constexpr float kSteepest = 0.87f; // 50 degrees, in radians: off the very end of a paddle
float clampPaddle(float y) { return std::clamp(y, Pong::kPaddleH / 2, Pong::kHeight - Pong::kPaddleH / 2); }
} // namespace
float tiltTarget(float pitch, float level) {
constexpr float kFullTilt = 0.26f; // 15 degrees either way covers the whole field
float travel = Pong::kHeight / 2 - Pong::kPaddleH / 2;
return Pong::kHeight / 2 + std::clamp((pitch - level) / kFullTilt, -1.0f, 1.0f) * travel;
}
float moveToward(float paddleY, float targetY) {
constexpr float kSlowWithin = 10; // pixels: closer than that, it eases in
return std::clamp((targetY - paddleY) / kSlowWithin, -1.0f, 1.0f);
}
void Pong::start() {
playerScore_ = deviceScore_ = rally_ = longest_ = 0;
player_ = device_ = kHeight / 2;
serve(rng_.below(2) == 0);
}
void Pong::serve(bool toPlayer) {
x_ = kWidth / 2;
y_ = kHeight / 2;
speed_ = kServeSpeed;
float angle = (rng_.unit() - 0.5f) * 1.0f; // within 29 degrees of level
vx_ = std::cos(angle) * speed_ * (toPlayer ? -1 : 1);
vy_ = std::sin(angle) * speed_;
rally_ = 0;
wait_ = kServePause;
phase_ = Phase::Serve;
}
bool Pong::meets(float paddleY) const { return std::fabs(y_ - paddleY) <= kPaddleH / 2 + kBall / 2; }
// Where on the paddle it hit decides the angle it leaves at, and each return is a little faster.
void Pong::bounceOff(float paddleY, bool toRight) {
float where = std::clamp((y_ - paddleY) / (kPaddleH / 2), -1.0f, 1.0f);
speed_ = std::min(kMaxSpeed, speed_ * kFaster);
vx_ = std::cos(where * kSteepest) * speed_ * (toRight ? 1 : -1);
vy_ = std::sin(where * kSteepest) * speed_;
}
uint8_t Pong::step(float seconds, float move) {
uint8_t events = 0;
seconds = std::min(seconds, 0.25f); // a held-up loop is not a quarter of the field
while (seconds > 0 && phase_ != Phase::Over) {
float dt = std::min(seconds, kTick);
seconds -= dt;
events |= tick(dt, move);
}
return events;
}
uint8_t Pong::tick(float dt, float move) {
player_ = clampPaddle(player_ + std::clamp(move, -1.0f, 1.0f) * kPlayerSpeed * dt);
// The device watches the ball once it comes its way, and waits in the middle otherwise.
float target = vx_ > 0 && phase_ == Phase::Play ? y_ : kHeight / 2;
float reach = kDeviceSpeed * dt;
device_ = clampPaddle(device_ + std::clamp(target - device_, -reach, reach));
if (phase_ == Phase::Serve) {
wait_ -= dt;
if (wait_ <= 0) phase_ = Phase::Play;
return 0;
}
uint8_t events = 0;
float before = x_;
x_ += vx_ * dt;
y_ += vy_ * dt;
if (y_ < kBall / 2) {
y_ = kBall - y_;
vy_ = -vy_;
events |= Wall;
} else if (y_ > kHeight - kBall / 2) {
y_ = 2 * (kHeight - kBall / 2) - y_;
vy_ = -vy_;
events |= Wall;
}
const float playerFace = kMargin + kPaddleW + kBall / 2, deviceFace = kWidth - kMargin - kPaddleW - kBall / 2;
if (vx_ < 0 && before >= playerFace && x_ < playerFace && meets(player_)) {
x_ = playerFace;
bounceOff(player_, true);
rally_++;
longest_ = std::max(longest_, rally_);
events |= Hit;
} else if (vx_ > 0 && before <= deviceFace && x_ > deviceFace && meets(device_)) {
x_ = deviceFace;
bounceOff(device_, false);
events |= Hit;
} else if (x_ < 0 || x_ > kWidth) {
bool playerScored = x_ > kWidth;
(playerScored ? playerScore_ : deviceScore_)++;
events |= Point;
if (playerScore_ >= kWinScore || deviceScore_ >= kWinScore) {
phase_ = Phase::Over;
events |= Over;
} else serve(playerScored); // to whoever lost the point
}
return events;
}
} // namespace roro::games
+64
View File
@@ -0,0 +1,64 @@
#pragma once
#include <cstdint>
#include "game_rng.h"
namespace roro::games {
// Pong against the device (issue #101): the player's paddle on the left, the device's on the
// right, first to 7. The ball goes faster with each return, and the device's paddle only moves so
// fast: a steep, fast ball gets past it. Lengths are pixels of the 240 x 122 field, times seconds.
// Where the paddle is asked to be when the device is tilted (issue #101's follow-up): its top
// lowered, the paddle goes up, as a ball rolls to the low side (it felt the right way round on the
// device); held as it was when the game began, the middle. `pitch` and
// `level` are "up" along the device's top-bottom axis, -1 to 1, as the accelerometer gives it.
float tiltTarget(float pitch, float level);
// The move that brings the paddle at `paddleY` to `targetY` with no overshoot: full speed far away,
// slower close by.
float moveToward(float paddleY, float targetY);
class Pong {
public:
static constexpr float kWidth = 240, kHeight = 122;
static constexpr float kPaddleH = 24, kPaddleW = 3, kMargin = 6, kBall = 4;
static constexpr float kPlayerSpeed = 150, kDeviceSpeed = 88;
static constexpr float kServeSpeed = 110, kMaxSpeed = 270, kFaster = 1.06f;
static constexpr float kServePause = 0.8f;
static constexpr int kWinScore = 7;
enum Event : uint8_t { Wall = 1, Hit = 2, Point = 4, Over = 8 };
enum class Phase : uint8_t { Serve, Play, Over };
explicit Pong(uint32_t seed) : rng_(seed) { start(); }
void start();
// `seconds` later, with the player's paddle asked up (-1), down (1) or left alone (0), or
// anything between for a slower move. What happened, as Events.
uint8_t step(float seconds, float move);
Phase phase() const { return phase_; }
float ballX() const { return x_; }
float ballY() const { return y_; }
float speed() const { return speed_; }
float playerY() const { return player_; } // the paddle's middle
float deviceY() const { return device_; }
int playerScore() const { return playerScore_; }
int deviceScore() const { return deviceScore_; }
bool playerWon() const { return playerScore_ >= kWinScore; }
int rally() const { return rally_; } // the player's returns since the serve
int longestRally() const { return longest_; } // in this game
private:
uint8_t tick(float dt, float move);
void serve(bool toPlayer);
bool meets(float paddleY) const;
void bounceOff(float paddleY, bool toRight);
Rng rng_;
Phase phase_ = Phase::Serve;
float x_ = 0, y_ = 0, vx_ = 0, vy_ = 0, speed_ = 0;
float player_ = 0, device_ = 0, wait_ = 0;
int playerScore_ = 0, deviceScore_ = 0, rally_ = 0, longest_ = 0;
};
} // namespace roro::games
+92
View File
@@ -0,0 +1,92 @@
#include "snake.h"
#include <algorithm>
namespace roro::games {
namespace {
bool opposite(Snake::Dir a, Snake::Dir b) { return (static_cast<int>(a) + 2) % 4 == static_cast<int>(b); }
} // namespace
void Snake::start() {
body_.clear();
taken_.fill(false);
for (int i = 0; i < 3; i++) { // three cells in the middle, heading right
Cell c{static_cast<int8_t>(kCols / 2 - i), static_cast<int8_t>(kRows / 2)};
body_.push_back(c);
mark(c, true);
}
dir_ = Dir::Right;
turnCount_ = 0;
score_ = 0;
sinceMs_ = 0;
alive_ = true;
placeFood();
}
uint32_t Snake::intervalMs() const {
uint32_t off = static_cast<uint32_t>(score_ / kFoodsAStep) * kFasterMs;
return off >= kSlowestMs - kFastestMs ? kFastestMs : kSlowestMs - off;
}
void Snake::turn(Dir dir) {
Dir last = turnCount_ ? turns_[turnCount_ - 1] : dir_;
if (dir == last || opposite(dir, last) || turnCount_ >= 2) return;
turns_[turnCount_++] = dir;
}
// On a free cell, each as likely as another.
void Snake::placeFood() {
int free = kCols * kRows - static_cast<int>(body_.size());
if (free <= 0) return;
int pick = rng_.below(free);
for (int i = 0; i < kCols * kRows; i++) {
if (taken_[static_cast<size_t>(i)]) continue;
if (pick-- == 0) {
food_ = {static_cast<int8_t>(i % kCols), static_cast<int8_t>(i / kCols)};
return;
}
}
}
uint8_t Snake::step(uint32_t ms) {
uint8_t events = 0;
if (!alive_) return 0;
sinceMs_ += std::min<uint32_t>(ms, 500);
while (alive_ && sinceMs_ >= intervalMs()) {
sinceMs_ -= intervalMs();
events |= move();
}
return events;
}
uint8_t Snake::move() {
if (turnCount_) {
dir_ = turns_[0];
turns_[0] = turns_[1];
turnCount_--;
}
static const int8_t dc[4] = {0, 1, 0, -1}, dr[4] = {-1, 0, 1, 0};
Cell head{static_cast<int8_t>(body_.front().col + dc[static_cast<int>(dir_)]),
static_cast<int8_t>(body_.front().row + dr[static_cast<int>(dir_)])};
bool eats = head == food_;
// The tail moves on as the head arrives: the head may go where the tail was. A snake that
// dies stays as it was, whole.
bool ontoTail = !eats && head == body_.back();
if (head.col < 0 || head.col >= kCols || head.row < 0 || head.row >= kRows || (taken(head) && !ontoTail)) {
alive_ = false;
return Died;
}
if (!eats) {
mark(body_.back(), false);
body_.pop_back();
}
body_.push_front(head);
mark(head, true);
if (!eats) return Moved;
score_++;
placeFood();
return Moved | Ate;
}
} // namespace roro::games
+60
View File
@@ -0,0 +1,60 @@
#pragma once
#include <array>
#include <cstddef>
#include <cstdint>
#include <deque>
#include "game_rng.h"
namespace roro::games {
// Snake (issue #101): a field of 38 x 19 cells, the walls and its own body kill it, and it goes
// faster as it eats. Time comes in from outside, in milliseconds.
class Snake {
public:
static constexpr int kCols = 38, kRows = 19;
static constexpr uint32_t kSlowestMs = 150, kFastestMs = 60, kFasterMs = 5;
static constexpr int kFoodsAStep = 3; // it gets faster every so many
enum class Dir : uint8_t { Up, Right, Down, Left };
enum Event : uint8_t { Moved = 1, Ate = 2, Died = 4 };
struct Cell {
int8_t col, row;
bool operator==(const Cell& o) const { return col == o.col && row == o.row; }
};
explicit Snake(uint32_t seed) : rng_(seed) { start(); }
void start();
// A turn asked for; it is taken at the next move. Two are remembered, so a quick corner (up,
// then left) works; turning back on itself is not a turn.
void turn(Dir dir);
uint8_t step(uint32_t ms);
bool alive() const { return alive_; }
int score() const { return score_; } // foods eaten
const std::deque<Cell>& body() const { return body_; } // the head first
Cell food() const { return food_; }
Dir heading() const { return dir_; }
uint32_t intervalMs() const; // between two moves, now
// For the tests: the food put where one wants it.
void putFood(Cell at) { food_ = at; }
private:
bool taken(Cell c) const { return taken_[static_cast<size_t>(c.row * kCols + c.col)]; }
void mark(Cell c, bool on) { taken_[static_cast<size_t>(c.row * kCols + c.col)] = on; }
void placeFood();
uint8_t move();
Rng rng_;
std::deque<Cell> body_;
std::array<bool, kCols * kRows> taken_{};
Cell food_{0, 0};
Dir dir_ = Dir::Right;
Dir turns_[2] = {Dir::Right, Dir::Right};
int turnCount_ = 0, score_ = 0;
uint32_t sinceMs_ = 0;
bool alive_ = true;
};
} // namespace roro::games
+202
View File
@@ -0,0 +1,202 @@
#include "tetris.h"
#include <algorithm>
namespace roro::games {
namespace {
// Each piece in its four turns, as the cells it fills in a 4 x 4 box: bit (row * 4 + col), the
// top left first. They turn as in the Super Rotation System.
constexpr uint16_t bits(const char* rows) {
uint16_t v = 0;
for (int i = 0; i < 16; i++)
if (rows[i] == '#') v |= static_cast<uint16_t>(1u << i);
return v;
}
constexpr uint16_t kShapes[7][4] = {
{bits("....####........"), bits("..#...#...#...#."), bits("........####...."), bits(".#...#...#...#..")}, // I
{bits(".##..##........."), bits(".##..##........."), bits(".##..##........."), bits(".##..##.........")}, // O
{bits(".#..###........."), bits(".#...##..#......"), bits("....###..#......"), bits(".#..##...#......")}, // T
{bits(".##.##.........."), bits(".#...##...#....."), bits(".....##.##......"), bits("#...##...#......")}, // S
{bits("##...##........."), bits("..#..##..#......"), bits("....##...##....."), bits(".#..##..#.......")}, // Z
{bits("#...###........."), bits(".##..#...#......"), bits("....###...#....."), bits(".#...#..##......")}, // J
{bits("..#.###........."), bits(".#...#...##....."), bits("....###.#......."), bits("##...#...#......")}, // L
};
// A turn that does not fit where the piece is tries these shifts, in order: a step from a wall,
// one up off the floor, two for the long piece.
constexpr int8_t kKicks[6][2] = {{0, 0}, {-1, 0}, {1, 0}, {0, -1}, {-2, 0}, {2, 0}};
constexpr int kLineScore[5] = {0, 100, 300, 500, 800};
} // namespace
std::array<Tetris::Cell, 4> Tetris::cells(const Piece& p) {
std::array<Cell, 4> out{};
if (p.kind < 1 || p.kind > 7) return out;
uint16_t shape = kShapes[p.kind - 1][p.turn & 3];
int n = 0;
for (int i = 0; i < 16 && n < 4; i++)
if (shape & (1u << i)) out[static_cast<size_t>(n++)] = {static_cast<int8_t>(p.col + i % 4), static_cast<int8_t>(p.row + i / 4)};
return out;
}
void Tetris::start() {
well_.fill(0);
score_ = lines_ = lastCleared_ = 0;
fallSince_ = heldMs_ = 0;
heldDir_ = 0;
repeating_ = over_ = softSpent_ = false;
bagLeft_ = 0;
next_ = deal();
spawn(deal());
}
// The bag: the seven pieces in a shuffled order, then seven again.
uint8_t Tetris::deal() {
if (bagLeft_ == 0) {
for (int i = 0; i < 7; i++) bag_[i] = static_cast<uint8_t>(i + 1);
for (int i = 6; i > 0; i--) std::swap(bag_[i], bag_[rng_.below(i + 1)]);
bagLeft_ = 7;
}
return bag_[--bagLeft_];
}
uint32_t Tetris::fallMs() const {
uint32_t ms = kSlowestMs;
for (int i = 0; i < level() && ms > kFastestMs; i++) ms = ms * 85 / 100;
return std::max(ms, kFastestMs);
}
// Inside the walls and above the floor, on no cell already filled. Above the well is free.
bool Tetris::fits(const Piece& p) const {
for (const Cell& c : cells(p)) {
if (c.col < 0 || c.col >= kCols || c.row >= kRows) return false;
if (c.row >= 0 && cell(c.col, c.row)) return false;
}
return true;
}
bool Tetris::shift(int dc, int dr) {
Piece moved = piece_;
moved.col = static_cast<int8_t>(moved.col + dc);
moved.row = static_cast<int8_t>(moved.row + dr);
if (!fits(moved)) return false;
piece_ = moved;
return true;
}
bool Tetris::rotate(int by) {
Piece turned = piece_;
turned.turn = static_cast<uint8_t>((turned.turn + 4 + by) & 3);
for (auto& kick : kKicks) {
Piece tried = turned;
tried.col = static_cast<int8_t>(tried.col + kick[0]);
tried.row = static_cast<int8_t>(tried.row + kick[1]);
if (fits(tried)) {
piece_ = tried;
return true;
}
}
return false;
}
// The piece comes in at the top, in the middle. If there is no room for it, the game is over.
uint8_t Tetris::spawn(uint8_t kind) {
piece_ = Piece{kind, 0, 3, static_cast<int8_t>(kind == 1 ? -1 : 0)};
fallSince_ = 0;
if (fits(piece_)) return 0;
over_ = true;
return Over;
}
void Tetris::give(uint8_t kind) { spawn(kind); }
uint8_t Tetris::lock() {
uint8_t events = Locked;
for (const Cell& c : cells(piece_)) {
if (c.row < 0) { // it came to rest above the well
over_ = true;
return static_cast<uint8_t>(events | Over);
}
well_[static_cast<size_t>(c.row * kCols + c.col)] = piece_.kind;
}
// Full rows go, and what is above comes down.
int cleared = 0, before = level();
for (int row = kRows - 1; row >= 0;) {
bool full = true;
for (int col = 0; col < kCols && full; col++) full = cell(col, row) != 0;
if (!full) {
row--;
continue;
}
for (int r = row; r > 0; r--)
for (int col = 0; col < kCols; col++) well_[static_cast<size_t>(r * kCols + col)] = cell(col, r - 1);
for (int col = 0; col < kCols; col++) well_[static_cast<size_t>(col)] = 0;
cleared++; // the same row again: it now holds the one that was above
}
lastCleared_ = cleared;
if (cleared) {
score_ += kLineScore[cleared] * (before + 1);
lines_ += cleared;
events |= Lines;
if (level() > before) events |= LevelUp;
}
uint8_t coming = next_;
next_ = deal();
return static_cast<uint8_t>(events | spawn(coming));
}
uint8_t Tetris::step(uint32_t ms, const TetrisInput& in) {
if (over_) return 0;
uint8_t events = 0;
ms = std::min<uint32_t>(ms, 1000); // a held-up loop is not a piece through the floor
if (in.rotate && rotate(1)) events |= Turned;
if (in.rotateBack && rotate(-1)) events |= Turned;
// Left and right: once at the press, then again and again while held.
int dir = in.left == in.right ? 0 : in.left ? -1 : 1;
if (dir != heldDir_) {
heldDir_ = dir;
heldMs_ = 0;
repeating_ = false;
if (dir && shift(dir, 0)) events |= Moved;
} else if (dir) {
heldMs_ += ms;
uint32_t wait = repeating_ ? kRepeatEveryMs : kRepeatAfterMs;
while (heldMs_ >= wait) {
heldMs_ -= wait;
repeating_ = true;
wait = kRepeatEveryMs;
if (shift(dir, 0)) events |= Moved;
}
}
if (in.hardDrop) {
int rows = 0;
while (shift(0, 1)) rows++;
score_ += 2 * rows;
return static_cast<uint8_t>(events | lock());
}
// Falling faster is for the piece it was asked for: the key still held when that one lands
// does not send the next one down too. It has to be let go and pressed again.
if (!in.soft) softSpent_ = false;
bool soft = in.soft && !softSpent_;
uint32_t every = soft ? std::min(fallMs(), kSoftMs) : fallMs();
fallSince_ += ms;
while (!over_ && fallSince_ >= every) {
fallSince_ -= every;
if (shift(0, 1)) {
if (soft) score_++;
events |= Moved;
} else {
events |= lock();
softSpent_ = in.soft;
break; // the next piece starts its own fall
}
}
return events;
}
} // namespace roro::games
+81
View File
@@ -0,0 +1,81 @@
#pragma once
#include <array>
#include <cstddef>
#include <cstdint>
#include "game_rng.h"
namespace roro::games {
// What the player asks of Tetris in one step: the first three for as long as the key is held,
// the others once for each press.
struct TetrisInput {
bool left = false, right = false, soft = false;
bool rotate = false, rotateBack = false, hardDrop = false;
};
// Tetris (issue #101): a well of 10 x 20, the seven pieces dealt from a bag (each once in every
// seven), the next one shown, a turn that shifts away from a wall, a soft and a hard drop, and a
// fall that quickens every ten lines. No hold, no ghost. Time comes in milliseconds.
class Tetris {
public:
static constexpr int kCols = 10, kRows = 20;
static constexpr uint32_t kSlowestMs = 800, kFastestMs = 80;
static constexpr uint32_t kSoftMs = 90; // a row, falling faster: quick, and still steered
static constexpr uint32_t kRepeatAfterMs = 170, kRepeatEveryMs = 50; // a held left or right
static constexpr int kLinesALevel = 10;
enum Event : uint8_t { Moved = 1, Turned = 2, Locked = 4, Lines = 8, LevelUp = 16, Over = 32 };
// The pieces, 1 to 7, in the order I O T S Z J L; 0 is an empty cell.
struct Piece {
uint8_t kind = 0;
uint8_t turn = 0; // 0 to 3, quarter turns clockwise
int8_t col = 0, row = 0; // of the top left of its 4 x 4 box; the row may be above the well
};
struct Cell {
int8_t col, row;
};
explicit Tetris(uint32_t seed) : rng_(seed) { start(); }
void start();
uint8_t step(uint32_t ms, const TetrisInput& in);
bool over() const { return over_; }
uint8_t cell(int col, int row) const { return well_[static_cast<size_t>(row * kCols + col)]; }
const Piece& piece() const { return piece_; }
uint8_t next() const { return next_; }
int score() const { return score_; }
int lines() const { return lines_; }
int level() const { return lines_ / kLinesALevel; }
uint32_t fallMs() const; // between two rows of fall, at this level
int lastCleared() const { return lastCleared_; } // by the last piece locked
// The four cells a piece covers.
static std::array<Cell, 4> cells(const Piece& p);
// For the tests: a cell of the well set, and the piece in play replaced.
void put(int col, int row, uint8_t kind) { well_[static_cast<size_t>(row * kCols + col)] = kind; }
void give(uint8_t kind);
private:
bool fits(const Piece& p) const;
bool shift(int dc, int dr);
bool rotate(int by);
uint8_t lock();
uint8_t spawn(uint8_t kind);
uint8_t deal();
Rng rng_;
std::array<uint8_t, kCols * kRows> well_{};
Piece piece_;
uint8_t next_ = 1;
uint8_t bag_[7] = {};
int bagLeft_ = 0;
int score_ = 0, lines_ = 0, lastCleared_ = 0;
uint32_t fallSince_ = 0, heldMs_ = 0;
int heldDir_ = 0;
bool repeating_ = false, over_ = false;
bool softSpent_ = false; // the key that brought the last piece down is still held
};
} // namespace roro::games
+101
View File
@@ -0,0 +1,101 @@
#pragma once
#include <cstddef>
#include <cstdint>
namespace roro::games {
// A tune as the speaker can play one: a note at a time, each so many eighths long. hz 0 is a rest.
struct Note {
uint16_t hz;
uint8_t eighths;
};
// Plays a tune round and round as time passes (issue #101). It makes no sound itself: step()
// says when the next note begins, and the caller sounds it.
class TunePlayer {
public:
// `at(i)` is the tune's i-th note, of `count`.
TunePlayer(Note (*at)(size_t), size_t count, uint32_t eighthMs) : note_(at), count_(count), eighthMs_(eighthMs) {}
void restart() {
at_ = 0;
leftMs_ = 0;
begun_ = false;
}
// `ms` later: true if a note begins now, with the note and how long it lasts. After a long
// gap the notes that would have gone by are skipped, not played in a burst.
bool step(uint32_t ms, Note& note, uint32_t& lengthMs) {
if (!count_) return false;
if (!begun_) {
begun_ = true;
ms = 0; // the time before the first note is nobody's
} else if (ms < leftMs_) {
leftMs_ -= ms;
return false;
} else {
ms -= leftMs_;
at_ = (at_ + 1) % count_;
while (ms >= note_(at_).eighths * eighthMs_) { // whole notes missed
ms -= note_(at_).eighths * eighthMs_;
at_ = (at_ + 1) % count_;
}
}
note = note_(at_);
lengthMs = note.eighths * eighthMs_;
leftMs_ = lengthMs - ms;
return true;
}
size_t position() const { return at_; }
// A new pace, from the next note on.
void setEighthMs(uint32_t ms) { eighthMs_ = ms; }
uint32_t eighthMs() const { return eighthMs_; }
private:
Note (*note_)(size_t);
size_t count_;
uint32_t eighthMs_;
size_t at_ = 0;
uint32_t leftMs_ = 0;
bool begun_ = false;
};
// Korobeiniki, the Russian folk song (1861) that Tetris is played to: the tune twice, then its
// slow second part.
namespace korobeiniki {
constexpr uint16_t E4 = 330, Gs4 = 415, A4 = 440, B4 = 494, C5 = 523, D5 = 587, E5 = 659, F5 = 698, G5 = 784, Gs5 = 831, A5 = 880;
// clang-format off
constexpr Note kFirst[] = {
{E5, 2}, {B4, 1}, {C5, 1}, {D5, 2}, {C5, 1}, {B4, 1},
{A4, 2}, {A4, 1}, {C5, 1}, {E5, 2}, {D5, 1}, {C5, 1},
{B4, 3}, {C5, 1}, {D5, 2}, {E5, 2},
{C5, 2}, {A4, 2}, {A4, 2}, {0, 2},
{0, 1}, {D5, 2}, {F5, 1}, {A5, 2}, {G5, 1}, {F5, 1},
{E5, 3}, {C5, 1}, {E5, 2}, {D5, 1}, {C5, 1},
{B4, 2}, {B4, 1}, {C5, 1}, {D5, 2}, {E5, 2},
{C5, 2}, {A4, 2}, {A4, 2}, {0, 2},
};
constexpr Note kSecond[] = {
{E5, 4}, {C5, 4}, {D5, 4}, {B4, 4}, {C5, 4}, {A4, 4}, {Gs4, 4}, {B4, 2}, {0, 2},
{E5, 4}, {C5, 4}, {D5, 4}, {B4, 4}, {C5, 2}, {E5, 2}, {A5, 4}, {Gs5, 4}, {0, 4},
};
// clang-format on
constexpr size_t kFirstCount = sizeof(kFirst) / sizeof(kFirst[0]), kSecondCount = sizeof(kSecond) / sizeof(kSecond[0]);
constexpr size_t kCount = 2 * kFirstCount + kSecondCount;
constexpr uint32_t kEighthMs = 200; // 150 quarter notes a minute, at the first level
constexpr uint32_t kFastestEighthMs = 110; // 272: as fast as it is still a tune
constexpr uint32_t kFasterMs = 8; // off each eighth, a level
// The tune hurries as the game does: its pace at a level of Tetris, the first being 0.
constexpr uint32_t eighthMsAt(int level) {
return level <= 0 ? kEighthMs
: static_cast<uint32_t>(level) * kFasterMs >= kEighthMs - kFastestEighthMs ? kFastestEighthMs
: kEighthMs - static_cast<uint32_t>(level) * kFasterMs;
}
// The whole of it, in the order it is played.
constexpr Note at(size_t i) {
return i < kFirstCount ? kFirst[i] : i < 2 * kFirstCount ? kFirst[i - kFirstCount] : kSecond[i - 2 * kFirstCount];
}
} // namespace korobeiniki
} // namespace roro::games
+89
View File
@@ -0,0 +1,89 @@
#include "twenty48.h"
namespace roro::games {
void Twenty48::start() {
cells_.fill(0);
score_ = 0;
over_ = reached_ = false;
spawn();
spawn();
}
void Twenty48::set(const uint8_t powers[kSize * kSize]) {
for (size_t i = 0; i < cells_.size(); i++) cells_[i] = powers[i];
over_ = !canMove();
}
void Twenty48::spawn() {
int empty = 0;
for (uint8_t c : cells_) empty += c == 0;
if (!empty) return;
int pick = rng_.below(empty);
uint8_t tile = rng_.below(10) == 0 ? 2 : 1;
for (auto& c : cells_) {
if (c) continue;
if (pick-- == 0) {
c = tile;
return;
}
}
}
bool Twenty48::canMove() const {
for (int row = 0; row < kSize; row++)
for (int col = 0; col < kSize; col++) {
uint8_t p = power(col, row);
if (!p) return true;
if (col + 1 < kSize && power(col + 1, row) == p) return true;
if (row + 1 < kSize && power(col, row + 1) == p) return true;
}
return false;
}
uint8_t Twenty48::slide(Dir dir) {
if (over_) return 0;
uint8_t events = 0;
// Each of the four lines, read from the side the tiles slide to.
for (int line = 0; line < kSize; line++) {
size_t index[kSize];
for (int i = 0; i < kSize; i++) {
int col = dir == Dir::Left ? i : dir == Dir::Right ? kSize - 1 - i : line;
int row = dir == Dir::Up ? i : dir == Dir::Down ? kSize - 1 - i : line;
index[i] = static_cast<size_t>(row * kSize + col);
}
uint8_t out[kSize] = {};
int n = 0;
bool merged = false; // the last tile put down came from a merge: it takes no other
for (int i = 0; i < kSize; i++) {
uint8_t p = cells_[index[i]];
if (!p) continue;
if (n > 0 && out[n - 1] == p && !merged) {
out[n - 1] = static_cast<uint8_t>(p + 1);
score_ += 1u << (p + 1);
merged = true;
events |= Merged;
if (p + 1 == 11 && !reached_) {
reached_ = true;
events |= Reached;
}
} else {
out[n++] = p;
merged = false;
}
}
for (int i = 0; i < kSize; i++) {
if (cells_[index[i]] != out[i]) events |= Moved;
cells_[index[i]] = out[i];
}
}
if (!events) return 0;
spawn();
if (!canMove()) {
over_ = true;
events |= Over;
}
return events;
}
} // namespace roro::games
+43
View File
@@ -0,0 +1,43 @@
#pragma once
#include <array>
#include <cstddef>
#include <cstdint>
#include "game_rng.h"
namespace roro::games {
// 2048: a board of 4 x 4. A move slides every tile as far as it goes; two alike that meet become
// one of twice the value, once a move. Then a new tile comes, a 2 nine times in ten, else a 4.
// The game is over when nothing can move. Reaching 2048 is said once, and the game goes on.
class Twenty48 {
public:
static constexpr int kSize = 4;
enum class Dir : uint8_t { Up, Right, Down, Left };
enum Event : uint8_t { Moved = 1, Merged = 2, Reached = 4, Over = 8 };
explicit Twenty48(uint32_t seed) : rng_(seed) { start(); }
void start();
uint8_t slide(Dir dir); // 0 when nothing can move that way
// A tile's value (2, 4, 8...), or 0 for an empty place.
uint32_t value(int col, int row) const { return power(col, row) ? 1u << power(col, row) : 0; }
uint8_t power(int col, int row) const { return cells_[static_cast<size_t>(row * kSize + col)]; } // 1 for a 2, 11 for 2048
uint32_t score() const { return score_; } // the sum of every tile made by a merge
bool over() const { return over_; }
bool reached() const { return reached_; }
// For the tests: the board set, a row after another, in powers of two.
void set(const uint8_t powers[kSize * kSize]);
private:
bool canMove() const;
void spawn();
Rng rng_;
std::array<uint8_t, kSize * kSize> cells_{};
uint32_t score_ = 0;
bool over_ = false, reached_ = false;
};
} // namespace roro::games
+18
View File
@@ -54,6 +54,24 @@ KeyEvent keyEvent(Key key, const RawKeys& keys) {
} // namespace
bool KeyMapper::heldChar(char c) const {
return std::find(previous_.chars.begin(), previous_.chars.end(), c) != previous_.chars.end();
}
bool KeyMapper::held(Key key) const {
bool arrows = previous_.fn || !textEntry_;
switch (key) {
case Key::Up: return arrows && heldChar(';');
case Key::Down: return arrows && heldChar('.');
case Key::Left: return arrows && heldChar(',');
case Key::Right: return arrows && heldChar('/');
case Key::Select: return previous_.enter;
case Key::Delete: return previous_.del;
case Key::Tab: return previous_.tab;
default: return false;
}
}
std::vector<KeyEvent> KeyMapper::update(const RawKeys& keys) {
std::vector<KeyEvent> out;
+6
View File
@@ -33,6 +33,12 @@ class KeyMapper {
// don't need Fn.
void setTextEntry(bool active) { textEntry_ = active; }
// Is this key down right now, as of the last update()? Events only say when a key goes down:
// a Game that moves something for as long as a key is held asks this (issue #101). The arrows
// are ; . , / as for events: alone outside Text Entry, with Fn anywhere.
bool held(Key key) const;
bool heldChar(char c) const; // a character key, as it types
// 0 when idle, '*' when opt was pressed, or the accent character waiting for its letter.
char pendingCompose() const { return compose_; }
+11
View File
@@ -1,5 +1,7 @@
#include "http_head.h"
#include <cstdio>
#include <algorithm>
#include <cctype>
#include <cstdlib>
@@ -58,6 +60,15 @@ void HttpHeadParser::line() {
else if (name == "transfer-encoding") head_.chunked = lower(value).find("chunked") != std::string::npos;
else if (name == "location") head_.location = value;
else if (name == "content-type") head_.contentType = value;
else if (name == "etag") head_.etag = value;
else if (name == "content-range") {
long from = -1, to = -1, total = -1;
if (std::sscanf(lower(value).c_str(), "bytes %ld-%ld/%ld", &from, &to, &total) == 3 && from >= 0 && to >= from && total > to) {
head_.rangeFrom = from;
head_.rangeTo = to;
head_.rangeTotal = total;
}
}
}
size_t ChunkedDecoder::decode(const uint8_t* in, size_t len, uint8_t* out) {
+3
View File
@@ -12,6 +12,9 @@ struct HttpHead {
long contentLength = -1; // -1: not given
bool chunked = false;
std::string location, contentType;
std::string etag; // as sent, quotes included; "" if none
// "Content-Range: bytes 1000-2999/3000", the answer to a range request; -1: not given.
long rangeFrom = -1, rangeTo = -1, rangeTotal = -1;
};
class HttpHeadParser {
+53
View File
@@ -0,0 +1,53 @@
#pragma once
#include <string>
#include "http_head.h"
namespace roro::release {
// Carrying on a download that broke (issue #54): the connection is opened again with
// "Range: bytes=<from>-", and what was already received stays where it is. The Update File's own
// hash and signature are what decide whether the whole is good; these checks only keep the device
// from carrying on with something that plainly is not the rest of the same file.
// The validator to send as "If-Range", so a file that changed comes back whole (a 200) instead of
// in part: only a strong ETag may be used for that. "" for none.
inline std::string ifRangeFor(const std::string& etag) {
return etag.size() >= 2 && etag.front() == '"' && etag.back() == '"' ? etag : "";
}
// Is this answer the rest of the file, from byte `from` of `total`? "" if so, or why not, in
// words for the screen. `etag` is what the first answer gave ("" if nothing).
inline std::string resumeRefusal(const HttpHead& head, long from, long total, const std::string& etag) {
if (head.status == 200) return "The file changed on the server";
if (head.status != 206) return "The server answered " + std::to_string(head.status);
if (head.chunked || head.rangeFrom != from) return "The server sent another part of the file";
if (head.rangeTotal != total || head.rangeTo != total - 1 || head.contentLength != total - from) return "The file changed size on the server";
if (!etag.empty() && !head.etag.empty() && head.etag != etag) return "The file changed on the server";
return "";
}
// How long to keep trying. A break that came after new bytes is the link being poor, and is
// worth another try; breaks with nothing in between mean it is not coming back.
class ResumePolicy {
public:
static constexpr int kMaxResumes = 20; // in one download
static constexpr int kMaxStuck = 3; // in a row without a byte
// The connection broke with `received` bytes of the file here: try again?
bool again(long received) {
stuck_ = received > last_ ? 0 : stuck_ + 1;
last_ = received;
if (resumes_ >= kMaxResumes || stuck_ >= kMaxStuck) return false;
resumes_++;
return true;
}
int resumes() const { return resumes_; }
private:
long last_ = 0;
int resumes_ = 0, stuck_ = 0;
};
} // namespace roro::release
+5
View File
@@ -0,0 +1,5 @@
+++
title = "The Apps"
description = "Everything on the device, an App at a time: what it does, a screen from the device itself, and where its guide is."
template = "apps.html"
+++
+1 -1
View File
@@ -46,7 +46,7 @@ To install from the SD card instead, copy the `.ota` file from `.pio/build/cardp
With no PC and no card, the device can install the project's releases itself (docs/milestones/R1.md). In **Settings → System → Firmware**:
- **Latest release** checks the server (Enter, or `c`) and says `v0.11.0 (new)` or `(current)`. Enter again opens the release: its version, date, size and the tag's message, with **Install** when it's newer. The download goes straight into the inactive slot, so no card is needed; the signature is checked after the first 160 bytes, before anything is written, and the image's hash at the end. The new firmware then runs on Probation as for any update.
- **Latest release** checks the server (Enter, or `c`) and says `v0.11.0 (new)` or `(current)`. Enter again opens the release: its version, date, size and the tag's message, with **Install** when it's newer. The download goes straight into the inactive slot, so no card is needed; the signature is checked after the first 160 bytes, before anything is written, and the image's hash at the end. The new firmware then runs on Probation as for any update. A download that breaks is carried on from where it was (issue #54): the device waits up to a minute for Wi-Fi, asks for the rest of the file, and gives up only after three tries in a row that bring nothing.
- **Older releases** lists the last ten, newest first. Opening an older one offers to go back to it, with a different question.
- **Settings → System → Check for updates** (on by default): once a day, with Wi-Fi up and the clock set, the device looks at the latest release and says `v0.11.0 is out: see Settings > System > Firmware`, once per version. It installs nothing by itself, and doesn't announce a version that already failed and rolled back on this device.
- **Settings → System → Install updates** (`When asked` by default; `By itself`, issue #52): a newer release found by the daily check is installed without being asked for. The device waits until no key was pressed for 2 minutes and nothing would be cut by a restart (a Track, a Capture, a copy, an upload, an SSH session, a web share), then asks on the screen for 30 seconds, lighting it if it was off: Cancel leaves it until the next day's check; Accept, or no answer, installs it and restarts. What was installed and when, and what was undone, is written in `/system/updates.log` on the card.
+1 -1
View File
@@ -100,7 +100,7 @@ In **Safe Mode** (see [Crashes and Safe Mode](/dev/debug/crashes/)) only a few r
| `cp [-f] <from> <to>` / `mv [-f] <from> <to>` / `rm [-r] [-f] <path>` / `mkdir <path>` / `cancel` | What the Storage App does, with its rules: copy (folders too), move or rename, delete (`rm -r` for a folder and what's in it, as Unix has it), new folder. `-f` replaces a file that's in the way; `*` and `?` in the last part of a path (`ls`, `du`, `rm`, `cp`, `mv`) run the command for each name matched, 64 at most; a tab separates paths that hold spaces; `cancel` stops a copy or a delete |
| `install <path>` | Update from SD with that `.ota` file, as Settings → System → Firmware does |
| `update check` / `update list` / `update status` / `update install <tag>` | The project's releases on Gitea: look at the latest, list the last ten, say what's known, or download and install one |
| `update pretend <version>` / `update probe <host>` / `update damage cut\|flip <n>` / `update daily` | Pretend to run another version (so a release counts as an update), see whether a server's certificate is accepted, cut or damage the next download, run the daily check again |
| `update pretend <version>` / `update probe <host>` / `update damage cut\|flip <n>` / `update daily` | Pretend to run another version (so a release counts as an update), see whether a server's certificate is accepted, break each connection of the next download after n bytes (it is carried on, issue #54) or damage one byte of it, run the daily check again |
| `lora probe` | Finds the radio: chip, oscillator, antenna switch, DIO1 interrupt, noise floor |
| `lora status` | Radio settings, who's listening, packet and error counters, noise floor, task stack |
| `lora rx on` / `lora rx off` | Listens and prints each packet on the console |
+3 -2
View File
@@ -83,7 +83,7 @@ An update that goes wrong is the case you most want to rehearse, and the firmwar
update status # what the device runs, what failed here before, the daily check, heap
update check | update list # look at the server: the latest release, or the last ten
update pretend v0.9.0 # take the running version to be v0.9.0: the latest release now counts as "new"
update damage cut 50000 # the next download is cut after 50000 bytes
update damage cut 800000 # each connection of the next download breaks after 800000 bytes
update damage flip 100000 # ...or has the byte at offset 100000 damaged
update install v0.11.0 # try the install
update probe git.twis.la # is that server's certificate accepted? (the two ISRG roots only)
@@ -91,7 +91,8 @@ update daily # forget today's daily check: it runs again
update pretend off # back to the real version
```
- **A damaged download must be refused cleanly:** the Update Service checks the signature after the first 160 bytes and the image hash at the end, so a cut or a flipped byte must end in a refusal with **nothing switched**. Check `info` afterwards: both slots, and `update: confirmed`.
- **A download that breaks is carried on:** with `damage cut 800000` a 2.5 MB release comes in four connections, each asking for the rest of the file, and installs; `update status` then says how often it was carried on. With `damage cut 0` no connection brings anything, and it gives up after three tries.
- **A damaged download must be refused cleanly:** the Update Service checks the signature after the first 160 bytes and the image hash at the end, so a download that gave up or a flipped byte must end in a refusal with **nothing switched**. Check `info` afterwards: both slots, and `update: confirmed`.
- **An undamaged install really installs** the release, into the other slot, and the device restarts into it. Your build stays in the slot it was in until the next update overwrites it, and the console's setting and token are untouched: the release has the console too.
- The server is read by the device itself, with Wi-Fi up; the download is one TLS connection, about 52 KB of heap at its peak, so IRC steps aside (see [the memory limit](/howto/not-enough-memory/)). `status: heap` in the stream shows it happen.
+32 -1
View File
@@ -8,7 +8,7 @@ docs = true
source = "docs/milestones/R1.md"
tag = "R1"
+++
**Status:** in progress. Shipped: CI and signed releases on Gitea (issue #5), a release for every tag; updates from Gitea (#6, **v0.11.0**); one firmware with the Debug Console in it (#68, **v0.12.0**); CI in about a minute (#74, **v0.13.0**). Built, not released yet: installing a release by itself (#52). Not started: the Issues App (#4), release channels (#53), resuming a download (#54).
**Status:** in progress. Shipped: CI and signed releases on Gitea (issue #5), a release for every tag; updates from Gitea (#6, **v0.11.0**); one firmware with the Debug Console in it (#68, **v0.12.0**); CI in about a minute (#74, **v0.13.0**). Built, not released yet: installing a release by itself (#52). Built, not released yet: carrying on a download that broke (#54). Not started: the Issues App (#4), release channels (#53).
**Goal:** a tag is a release, built the same way every time and published where a device can find it.
@@ -164,6 +164,37 @@ With the setting on, `update pretend v0.22.0` and `update daily`:
- `/system/updates.log` got its line at each confirmed update.
- Not checked: "by itself" in the record (the release installed did not have this code yet), the 2 minutes without a key and the busy cases (host tests only), and a screen that was off lighting up. Keys sent through the Debug Console do not count as keys.
## Carrying on a download that broke (issue #54)
A release goes straight into the inactive slot (Q167), so a connection that broke meant starting again, and on a weak link a 2.5 MB file might never arrive whole.
### What the server gives (checked 2026-10-11)
A request with `Range: bytes=0-0` for a release's file gets `206`, `Content-Range: bytes 0-0/2510384` and a strong `ETag`.
### Decisions (2026-10-11)
| # | Decision |
|---|---|
| Q282 | **Carried on within the same install, in memory only.** The slot's write handle and the parser, with its hash so far, stay as they are while the connection is opened again with `Range: bytes=<where it was>-`. The parser never sees the break. |
| Q283 | **Nothing is kept across a restart** (the issue's second question): the slot cannot be reopened at an offset, so a download cut by a power-off starts over. |
| Q284 | **The answer must be the rest of the same file:** a `206` from that byte to the end, of the same total size, with the same `ETag` if both answers have one. The `ETag` goes in `If-Range`, so a file that changed comes back whole (a `200`), which is refused. What decides whether the whole is good is still the Update File's own signature and hash. |
| Q285 | **How long:** before each try, up to a minute for Wi-Fi to come back. Tries go on while each brings more bytes, 20 at most in one download; three in a row that bring nothing end it. |
| Q286 | **`update damage cut <n>`** now breaks each connection of the next download after n bytes, so both the carrying on and the giving up can be tried. |
### As built
- **`lib/release/src/resume.h`**, host-tested (5 tests with the head's new fields): `resumeRefusal`, `ifRangeFor`, `ResumePolicy`. `HttpHead` reads `ETag` and `Content-Range`.
- **`HttpsGet::openFrom`** asks for a range; **`GiteaSource`** in the Update Service does the waiting and the trying inside its `read()`.
- `update status` says how often the last download was carried on.
### Checked on the device (2026-10-11)
- **`update damage cut 800000`, then an install of v0.23.0** (2 510 384 bytes): it arrived over four connections, its hash matched, and the device restarted into it.
- **`update damage cut 0`:** three connections that brought nothing, then it gave up (`carried on 2 time(s)`), with the running slot still the one to boot.
- **Memory decided the first attempt.** Between two connections 78 KB was free, under the 80 KB a TLS connection asks for before it starts, so every try was refused and the download gave up. The install's own buffers (about 8 KB) are what is missing, and nothing else asks for memory while the screen is taken over: carrying on asks for 64 KB (the 52 KB peak and 12 KB). The lowest the heap went during the broken download was 20 KB, with a console client connected.
- Not checked: a real Wi-Fi drop in the middle (the wait for Wi-Fi to come back), and a file that changed on the server.
## One firmware: the Debug Console in every build (issue #68)
The issue asked for a token that could be set, so that Debug Builds could be published. The design round ended somewhere simpler: no Debug Builds. The console is in every firmware, off until switched on, with a token that belongs to the device (ADR 0010, which supersedes part of ADR 0004).
Binary file not shown.

After

Width:  |  Height:  |  Size: 1.8 KiB

@@ -0,0 +1,215 @@
+++
title = '''Not finished until it plays Tetris'''
description = '''roro9stack had a radio scanner, an SSH client and a VPN, and no games, which for a firmware means it wasn't finished. Now it has Pong, Snake and Tetris, with the tune, in 14.6 KB of flash. Seventeen design questions were answered with one word each, and then an evening of playing overturned three of the answers.'''
date = 2026-10-13T23:30:00+02:00
[extra]
topics = '''ESP32-S3 · Games · Chiptune'''
read_label = '''Read how three games got in →'''
uid = '''<b>tetris:</b> level 1, music 6/8'''
dek = "[roro9stack](/devlog/roro9stack/), my firmware for the M5Stack Cardputer, could sniff a LoRa mesh, browse Gemini and open a shell on a server. It could not play Snake. That is fixed: a Games App with Pong, Snake and Tetris, and the tune Tetris has always been played to. This post is about why a firmware needs them, what three very old games still do to people, and how little room fun takes."
byline = '''seventeen questions answered "Ok", then eight things changed by playing'''
[extra.sign]
label = "Bytes of flash for three games and a folk song"
note = "The Tetris cartridge for the Game Boy was 32,768."
count = "14,616"
[[extra.cast]]
name = "The Launcher"
role = "a grid of twelve tiles"
text = "Had eleven Apps in it. Three games would have made fourteen, so there is one Games App, and the grid is full."
[[extra.cast]]
name = "The key mapper"
role = "turns the keyboard into events"
text = "Told Apps when a key went down and never that it was still down. Fine for a menu. Useless for a paddle."
[[extra.cast]]
name = "Korobeiniki"
role = "a Russian folk song, 1861"
text = "The tune of Tetris. Fifty-eight notes in 232 bytes. I had decided there would be no music, and held to that until Tetris was running."
[[extra.cast]]
name = "The 1 key"
role = "top left of the keyboard"
text = "Drops the piece. It was going to be Space, which on this keyboard sits right next to the arrow for 'right'. A drop cannot be taken back."
[[extra.cast]]
name = "A script"
role = "playing over Wi-Fi"
text = "Presses keys through the Debug Console and reads the screen back. Played the Snake in this post's picture, and lost 35 games in the two runs I counted."
+++
## TL;DR
- **A Games App with Pong, Snake and Tetris** (**v0.24.0**).
- **It costs 14.6 KB of flash and 8 bytes of static memory.** The games allocate their few hundred bytes when one is started.
- **Each game is a small state machine with no screen in it:** time and keys go in, the new state comes out, and a seed decides the dice. 44 tests play them on the PC.
- **Apps can now ask which keys are held.** Until this week they only heard presses.
- **Tetris plays Korobeiniki,** faster with each level. `0` switches it, `8` and `9` set its volume.
- **Playing changed what the design round had decided:** icons instead of a list, the drop moved off Space, and music after all.
- 673 host tests, 47 more than the firmware had without games.
## The cast
{{ cast() }}
## It wasn't finished
A firmware that can't play a game isn't done. I don't mean that as a joke.
Look at what people do the moment a machine has a screen and a few buttons. Emacs has had `M-x tetris` for decades, next to `M-x snake` and `M-x pong`. The BSDs still ship `tetris(6)` and `snake(6)` in `/usr/games`. An HP oscilloscope from the nineties, a serious instrument that cost a serious amount, hides a Tetris behind a key sequence. The Flipper Zero comes with Snake. Chrome has a dinosaur for when the network is down. Nobody needed any of these. Somebody put each of them in anyway.
A game is the first thing you write for yourself. Everything else in this firmware answers a need: a clock to set, a packet to decode, a file to move. A game answers nothing. It is there because the machine is yours and you wanted to play on it, and a device that has one feels owned in a way that a device with a perfect settings menu doesn't.
{{ figure(src="launcher.png", alt="The Cardputer's Launcher at 2x: a grid of twelve cyan icons on black, four across and three down. The tenth, a game pad, is on a filled cyan tile, and the name under the grid reads Games.", width=480, height=270, caption="The Launcher, full: twelve tiles, and the game pad is one of them.") }}
## What these three carry
**Pong** is from 1972 and had no program at all. The first machines were wired from logic chips: no processor, nothing to load. Two paddles, a square ball, a score. It is still the clearest picture there is of what a video game is.
**Tetris** was written in 1984 on a Soviet computer whose terminal could not draw, so the first blocks were pairs of brackets. Five years later it came in the box with the Game Boy, and with it came a folk tune from 1861 that half the planet now calls "the Tetris music".
**Snake** shipped on a Nokia phone in 1997, on a screen 84 pixels wide, and turned waiting rooms and bus stops into something else for a decade.
You don't have to have owned any of those machines for these games to pull at something. The sound of a line going, the snake one cell from its own tail, the ball coming back faster than you sent it: they are in the hands more than in the head. Nobody has to be told the rules of any of the three. That is rare enough in software to be worth an evening.
And they are small. That is the part I can't get over. This device has a screen of 240 by 135 pixels, somewhat more than the Game Boy's 160 by 144, and 8 MB of flash where the Game Boy's Tetris had 32 KB to itself. Three games fit here in less than half of that cartridge. They were small when memory cost a fortune, and they stayed small because there was nothing in them to grow: a field, a rule, a clock that speeds up.
{{ figure(src="games.png", alt="The Games App at 2x: three icons in a row on black. Two paddles and a ball; a snake with a head, on a filled cyan tile; stacked blocks over a floor, with a small orange dot at its corner. Under the row, Snake in bold, and below it, best 11.", width=480, height=270, caption="The three games, shown the way the Launcher shows Apps. The orange dot says a game of Tetris is waiting, paused. Best 11 is mine, by hand.") }}
## Seventeen questions, all answered "Ok"
The design round was seventeen questions, each with a recommended answer, and I took every recommendation. It went fast because the facts had been looked up first, and two of them shaped everything:
**The Launcher was full.** Its grid shows twelve Apps and eleven were there. So: one Games App holding three games. In the project's vocabulary a **Game** is not an App. It has no tile, and it is found again paused when you leave in the middle of one.
**Apps only heard key presses.** A key gave one event when it went down, and nothing while it stayed down. A Pong paddle moved by tapping is not Pong. The key mapper now answers "is this key held?", and three tests on the PC hold keys down to prove it.
The rest, briefly:
{% table() %}
| | Decided |
|---|---|
| Pong | Against the device, first to 7. Its paddle has a top speed, so a steep fast ball beats it |
| Snake | 38 by 19 cells. The walls kill |
| Tetris | 10 by 20, upright, 6 pixels a cell. Pieces dealt from a bag of seven, the next one shown |
| Difficulty | No choice. Each game starts gently and speeds up |
| Colours | The seven pieces take the theme's seven accent colours |
| Scores | One best for each game, kept in the device |
| Sound | Short beeps. **No music** |
{% end %}
Each game lives in `lib/games` as a state machine that knows nothing about a screen or a keyboard: so many milliseconds and these keys go in, the new state and what happened (a bounce, a line) come out. A seed decides the dice, so the same seed is the same game. That is what lets a test say "a player who always meets the ball must take points off the device over five games", and have it checked on every build without anyone playing.
{{ figure(src="pong.png", alt="Pong on the Cardputer at 2x: a cyan paddle on the left near the top, an orange one on the right, a dashed line down the middle, a small white ball left of centre, and the score 1 to 0 at the top.", width=480, height=270, caption="One-nil against the device. The cyan paddle is the player's.") }}
## Then I played it
A design round answers the questions you can think of from a chair. Then the thing is in your hands, and in a little over an hour I had asked for eight changes:
1. **A list of three games looked wrong.** The Launcher shows icons; so should this. Three icons, drawn on a 16 by 16 grid and doubled, like the Apps'. The first snake looked like the digit 2 and was redrawn with a head.
2. **Space was dangerous.** On this keyboard it sits next to `/`, which is the arrow for "right". A hard drop can't be taken back, and one slip of a finger would do it. It is on `1` now, far from the arrows.
3. **"No music" lasted until Tetris was running.** I changed my mind and asked for the tune. See below.
4. **`8` and `9`** for the music's volume, once there was music.
5. **The tune speeds up with the level,** because the fall does.
6. **The top volume was too low.** The speaker had only ever been used for short beeps, at a quarter of what it can do.
7. **Then it was too high,** and the scale was cut to four fifths.
8. **The soft drop sometimes behaved like a hard drop.** That one was a bug, and is under "What went wrong".
None of these would have come out of more questions. They came out of having the thing in my hands.
{{ figure(src="tetris.png", alt="Tetris on the Cardputer at 2x: a well in the middle, with a pile of blocks in seven colours filling its lower half except the rightmost column, and a yellow square piece falling. On the left, Score 0, Best 56, Lines 0. On the right, Next with a long cyan piece, Level 1, and at the bottom, music 2/8.", width=480, height=270, caption="All seven colours, from the theme. The score is 0 because the script that played this let every piece fall by itself: a drop scores, and I didn't want a script's score kept as the best. The 56 is from a test.") }}
## The tune is 232 bytes
Korobeiniki is a song about a pedlar, from a poem of 1861, and it has been the sound of Tetris since the Game Boy. Here it is a table of 58 notes, each a frequency and a length in eighths, four bytes a note: the first part, played twice, then the slow second part. A player the size of a paragraph says when the next note starts, and the speaker's tone generator does the rest. There is no sample, no synthesiser and no file.
It has a speaker channel of its own, so the beep of a line cleared doesn't cut a note short. At the first level it runs at 150 beats a minute, and each level takes 8 milliseconds off every eighth, down to about 270 beats a minute from the thirteenth level on. A new pace starts at the next note, never in the middle of one. The timing has its own tests: 9.9 seconds fed in steps of 33 ms must give exactly a hundred notes, not ninety-nine and a drift.
`0` switches it off, and the device remembers.
## What went wrong
**The soft drop that wouldn't let go.** Holding `.` makes a piece fall faster. When it landed with the key still down, the next piece came in already falling fast, and so did the one after: a soft drop that felt like a hard one. Now the key that brought a piece down does nothing for the next piece until it is let go and pressed again. It was also simply too fast, a row every 50 ms; it is 90 now.
**A dead snake was a cell short.** The tail was moved before checking whether the head had hit something, so the snake on the "Game over" screen was missing its last cell. One cell, and it looked wrong at once.
**The pictures in this post tore.** The Debug Console's screenshot reads the frame while the App may be drawing it. For a settings page that never matters. A game redraws thirty times a second, and the first Tetris picture had a black band through the pile where a new frame was half drawn. The code has said so in a comment since the day it was written: "it can tear; it's for looking". The picture above is one of two taken back to back that agreed.
**A script is bad at Snake.** I wanted a picture of a snake that had eaten something, and the console can press keys, so a script read the screen and steered. It lost 24 games in under five minutes and never got longer than six. The console goes quiet for a second or two now and then, and at a cell every 150 ms that is a wall. It went quiet most often right after the beep of a meal. With the sound off it lost 11 games in two and a half minutes and reached ten cells, which is the snake below. I haven't found out why a beep and a stalled connection go together.
{{ figure(src="snake.png", alt="Snake on the Cardputer at 2x: a framed black field, a green snake ten cells long with a white head, folded back on itself in the middle, and an orange square of food near the bottom right corner. Above the field, the score 7 on the left and best 11 on the right.", width=480, height=270, caption="Ten cells, seven meals, played by a script over Wi-Fi, a beat late. The best of 11 it didn't touch.") }}
## What I checked, and what I didn't
Checked on the device, by playing: all three games start, pause, resume and end; the paddle follows a held key; Tetris's left and right repeat; the tune, its switch, its volume and its memory of both; the screen staying lit during a game and dimming again when one is paused.
Checked on the PC, by 44 tests: the rules. Bounces, the first to seven, the device's paddle having a top speed and still losing points; turning back on itself not being a turn, following its own tail being allowed; the bag dealing each piece once in seven, a turn against a wall shifting the piece, four rows at once scoring 800.
Not checked: Tetris past the first few levels, so nobody has heard the tune at its fastest. A long session on battery, with the screen redrawn thirty times a second. Whether the top of the volume scale distorts on every unit or only on mine. And in the Nord theme two of the seven pieces come out the same orange, because that theme's red and orange are neighbours on a screen with 256 colours.
## Also since last time
There was no post for the last three releases, and v0.24.0 has more in it than games:
- **An SSH client** (**v0.22.0**): a real terminal on another machine, with `vim` and `top` in it, and `scp` from the Shell (**v0.22.1**).
- **MeshCore in the LoRa Scanner, a Launcher of icons, seven colour themes, Settings in groups** (**v0.23.0**).
- Also in **v0.24.0**: **a compass** in the GNSS App, which has no magnetometer and takes north from walking or from the sun; **updates that install themselves** after a 30-second question, if you ask for that; and **downloads that carry on** after the Wi-Fi drops instead of starting over.
## By the numbers
{% table() %}
| | |
|---|---|
| Games | 3 |
| Flash | 14,616 bytes |
| Static memory | 8 bytes |
| Lines of game rules, `lib/games` | 719 |
| Tests that play them | 44 |
| The tune | 58 notes, 232 bytes |
| Its pace | 150 to 272 beats a minute |
| Pictures a second while a game runs | 30 |
| Design questions | 17 |
| Answered "Ok" | 17 |
| Answers overturned by playing | 3 |
| Games of Snake lost by a script, in the two runs counted | 35 |
| Host tests | 673 |
{% end %}
## Where it stands
{% steps() %}
1. ~~M0 and M1: the skeleton, Wi-Fi, IRC, Wi-Fi Tools.~~ v0.1.0 to v0.2.1, [the first post](/devlog/roro9stack/).
2. ~~Updates and debugging over the air.~~ v0.3.0, [Look, no cables](/devlog/roro9stack-ota/).
3. ~~M2: GNSS.~~ v0.4.0, [Seventeen satellites](/devlog/roro9stack-gnss/).
4. ~~G1: Gemini.~~ v0.5.0, [A browser in the RAM IRC left over](/devlog/roro9stack-gemini/).
5. ~~M3: the LoRa radio, listening.~~ v0.6.0, [The loudest thing it hears is itself](/devlog/roro9stack-lora/).
6. ~~S1: the card, fixed addresses, the System App.~~ v0.6.1 to v0.8.1, [One byte too early](/devlog/roro9stack-s1/).
7. ~~F1 and the start of R1: files, notes, signed releases, updates from Gitea.~~ v0.9.0 to v0.11.0, [836 bytes](/devlog/roro9stack-f1-r1/).
8. ~~W1: the website, and one firmware with the Debug Console in it.~~ v0.12.0, [It was off](/devlog/roro9stack-console/).
9. ~~A help key, the Shell, notes of any size.~~ v0.13.0 to v0.15.0, [It said "No"](/devlog/roro9stack-shell/).
10. ~~Pictures, a screenshot key, the card in a phone's browser.~~ v0.16.0 to v0.18.0, [Press w](/devlog/roro9stack-share/).
11. ~~A WireGuard tunnel, and nine commands for a bad network day.~~ v0.19.0 to v0.21.0, [Six releases behind](/devlog/roro9stack-vpn/).
12. ~~SSH, MeshCore, icons and themes.~~ v0.22.0 to v0.23.0.
13. ~~A compass, updates that install themselves, and Pong, Snake and Tetris.~~ v0.24.0, this post.
14. Next: M4, the mesh, which still wants a second node.
{% end %}
{% signoff() %}
Fourteen thousand bytes, three games, one tune. Most of what this firmware does took measuring, and bit me first. This took an evening and asked for almost nothing. The device could already do a great deal. Now it is also something to pick up when there is nothing to do, and that was the missing feature.
{% end %}
Binary file not shown.

After

Width:  |  Height:  |  Size: 3.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.9 KiB

+91
View File
@@ -0,0 +1,91 @@
+++
title = "Games"
description = "Pong, Snake, Tetris, Minesweeper, 2048 and Asteroids, for when the radio is quiet."
weight = 17
[extra]
tag = "Games"
+++
The Games App opens on its six games, an icon each, as the Launcher shows Apps: <kbd>,</kbd> and <kbd>/</kbd> go from one to the next, with its name and your best so far under the row. <kbd>Enter</kbd> picks one, <kbd>Enter</kbd> again starts it.
In every game:
| Key | Does |
|---|---|
| <kbd>Enter</kbd> | Starts, or starts again when a game is over |
| <kbd>p</kbd> | Pauses. Any key carries on |
| <kbd>`</kbd> | Back to the other games. This one waits, paused |
A game you leave in the middle, for another game or another App, is **found again paused**, and its icon has an orange dot. While Pong, Snake, Tetris or Asteroids is being played the screen does not dim. The games beep, and Tetris has its tune, unless **Settings → This device → Sound & LED** is off. There is no choice of difficulty: the games on a clock start gently and get faster, and the other two are always the same size.
## Pong
You have the left paddle, the device the right one. Hold <kbd>;</kbd> to go up and <kbd>.</kbd> to go down. **First to 7 wins.**
Or steer by **tilting the device**: <kbd>0</kbd> switches between the keys and tilting, and the device remembers which. Tilted, the paddle rolls to the low side: lower the top edge and the paddle goes up, raise it and it goes down, about 15 degrees either way for the whole height. However you hold it when the game starts, or when you switch, is the middle. If that feels upside down, <kbd>9</kbd> turns it round (raise the top edge to go up), and that is remembered too. The keys still work while tilting. The top left corner says `keys`, `tilt` or `tilt, inverted`.
Where the ball meets your paddle decides how it leaves: off the middle it goes straight, off an end it goes steeply. Each return makes the ball a little faster. The device's paddle only moves so fast: a steep, fast ball gets past it.
What is kept is your **longest rally**: the most returns you made between two serves.
## Snake
The arrows (<kbd>;</kbd> <kbd>.</kbd> <kbd>,</kbd> <kbd>/</kbd>) turn the snake. It grows by one with each thing it eats, and goes a little faster every third. **The walls kill it**, and so does its own body.
Two turns pressed quickly one after the other are both taken, one a step: that is how to turn round in two rows.
## Tetris
| Key | Does |
|---|---|
| <kbd>,</kbd> <kbd>/</kbd> | Left and right. Held, it repeats |
| <kbd>;</kbd> | Turns the piece. <kbd>z</kbd> turns it the other way |
| <kbd>.</kbd> | Falls faster, while held |
| <kbd>1</kbd> | Drops it at once. It is far from the arrows on purpose: a drop cannot be taken back |
| <kbd>0</kbd> | The music, on or off. The device remembers it |
| <kbd>8</kbd> <kbd>9</kbd> | The music, quieter and louder, in eight steps. Remembered too |
The well is 10 wide and 20 high. The pieces come from a bag: each of the seven once, in a shuffled order, then seven again, so the long one is never more than twelve pieces away. The **next piece** is shown on the right. A piece turned against a wall shifts a step away from it.
It plays to **Korobeiniki**, the tune Tetris has always had, round and round, and **faster with each level**, as the fall is; it stops when the game is paused or over. With Sound & LED off in the Settings there is no music either.
A full row goes: 100 points for one, 300 for two, 500 for three, **800 for four at once**, times the level. Falling faster gives a point a row, dropping two. The key that brought a piece down does not send the next one down too: let it go and press again. Every ten rows is a level, and the fall quickens.
## Minesweeper
A field of 20 by 9 cells hides **28 mines**, about one cell in seven. Open every cell that has none.
| Key | Does |
|---|---|
| <kbd>;</kbd> <kbd>.</kbd> <kbd>,</kbd> <kbd>/</kbd> | Move the cursor. Held, it runs along |
| <kbd>Enter</kbd> | Digs the cell under the cursor |
| <kbd>f</kbd> | Puts a flag on a cell you think is a mine, or takes it off |
A cell you open shows **how many mines touch it**, of the eight around; one that touches none opens its neighbours too, and so on. The **first dig is never a mine**, nor next to one, so it always opens something.
<kbd>Enter</kbd> on an open number whose mines are all flagged opens the rest of the cells around it at once. If a flag is wrong, that is a mine dug.
The top line says how many mines are left to flag and how long you have been at it; the clock starts at the first dig. Dig a mine and the field shows where they all were, with the wrong flags gone pale. What is kept is your **fastest cleared field**.
## 2048
The arrows slide every tile on the 4 by 4 board as far as it goes. **Two tiles alike that meet become one of twice the value**, and after each move a new 2 (sometimes a 4) appears. The score is the sum of every tile you made.
Reaching **2048** is said once, and the game goes on for as long as a move is left. There is nothing to pause: leave it and come back, and the board is as you left it.
## Asteroids
You steer the ship by **leaning the device**: lean it to the right and the ship turns clockwise, to the left and it turns the other way, faster the further you lean. However you hold it when the game starts counts as straight on, so there is no need to hold it flat. <kbd>,</kbd> and <kbd>/</kbd> held turn it too, for a device lying on a table.
| Key | Does |
|---|---|
| <kbd>;</kbd> | Thrust, while held. The ship keeps drifting when you let go, slowing a little |
| <kbd>.</kbd> | Fires. Four shots at most at a time, and they don't go far |
Everything wraps round: what leaves at one edge comes back at the other. A large rock breaks in two, each of those in two again, and the small ones are gone: 20, 50 and 100 points. Clear the field and the next wave has one rock more. You have three ships; a new one comes in the middle, blinking, and nothing can touch it for two seconds.
## The keys, as the device lists them
What <kbd>Fn</kbd> + <kbd>h</kbd> shows on these screens. These tables are generated from the firmware's own lists, so they are always the current ones.
{{ keys(scopes=["games", "games-pong", "games-snake", "games-tetris", "games-mines", "games-2048", "games-asteroids"]) }}
+2
View File
@@ -19,6 +19,8 @@ The page shows the **version** running, its **status**, the address to **push up
An install needs Wi-Fi if it is a download. The new firmware is checked before anything is written (the signature after the first 160 bytes) and again at the end (the image's hash). Then the device restarts. It will **wait up to 60 seconds** if you are typing, so that a restart never eats a note.
A download that **breaks** (the Wi-Fi drops, the link is poor) is not started over: the device waits up to a minute for Wi-Fi to come back, asks the server for the rest of the file, and carries on from where it was, as often as it takes while each try brings something more. It gives up after three tries in a row that bring nothing, and then nothing is changed. If the device is switched off in between, the download starts from the beginning next time.
## Check for updates
**Settings → System → Check for updates** is on by default. Once a day, with Wi-Fi up and the clock set, the device looks at the latest release and says `v0.11.0 is out: see Settings > System > Firmware`, once per version. It installs **nothing** by itself unless you ask for that (below), and it does not announce a version that already failed and rolled back on this device.
+99 -38
View File
@@ -1,72 +1,133 @@
# The App cards on the home page. Each says what the firmware does today (docs/milestones/W1.md, Q185):
# what isn't built is marked planned, with its milestone. `icon` is a cell of static/img/icons-*.svg.
# The Apps, in the Launcher's order: the Apps page shows them all, the home page those marked
# `featured`. Each says what the firmware does today (docs/milestones/W1.md, Q185); what isn't
# built is marked planned, with its milestone. `icon` is a cell of static/img/icons-*.svg (three
# across: 0 planned, 1 LoRa, 2 GNSS, 3 Wi-Fi, 4 Gemini, 5 IRC, 6 Notes, 7 System, 8 Storage,
# 9 Settings, 10 SSH, 11 Shell, 12 Games). `screen` is a file in static/screens/, a screenshot
# from the device at 2x; an App without one has none.
[[app]]
num = "01"
tag = "LoRa Scanner"
title = "See what is on the air"
text = "Lists every packet it hears with time, RSSI, SNR, sender and hops, reading the Meshtastic or MeshCore header. A Sweep shows signal strength from 863 to 870 MHz. Capture to a pcap for Wireshark."
fact = "Only listens, never transmits"
icon = 1
tag = "IRC"
title = "Chat from the keyboard"
text = "Join channels and type commands as you would anywhere, with a log a day written to the SD card. It stays connected when you leave the App, and the Launcher marks unread messages."
fact = "Logs to SD"
icon = 5
guide = "/guide/irc/"
[[app]]
tag = "Wi-Fi Tools"
title = "Scan the air for networks"
text = "The networks around you, sorted and filtered; how busy each channel is; a tracker that follows one network's signal as you walk. Scans log to a CSV file on the SD card."
fact = "Scans log to CSV"
icon = 3
guide = "/guide/wifi-tools/"
[[app]]
num = "02"
tag = "GNSS"
title = "Find your place in the sky"
text = "Position and Sky views show where you are and which satellites you can see. Tracks save to the SD card as GPX, and the clock follows the fix."
fact = "Sky view and GPX Tracks"
title = "Find your place, and your way"
text = "Position, a Sky view of every satellite, and a Compass that leads back to a place you marked. Tracks save to the SD card as GPX, and the clock follows the fix."
fact = "No magnetometer: north from walking, or the sun"
icon = 2
guide = "/guide/gnss/"
screen = "apps/gnss.png"
alt = "The GNSS App's sky view: a circle with the satellites of four constellations as coloured dots, filled when used, and a legend counting them: GPS 6 of 12, GLONASS 3 of 4, Galileo 2 of 2, BeiDou 3 of 3, 3D Fix"
featured = true
[[app]]
num = "03"
tag = "Gemini"
title = "Browse Geminispace"
text = "Follow links with Tab and Enter, go Back to where you were, bookmark pages and save them to the SD card to read offline."
fact = "Certificates trusted on first use"
icon = 4
guide = "/guide/gemini/"
screen = "apps/gemini.png"
alt = "The Gemini App's start page: Gemini, your bookmarks and Saved Pages, then a few places to start, and a saved page from bbs.geminispace.org"
featured = true
[[app]]
num = "04"
tag = "IRC"
title = "Chat from the keyboard"
text = "Join channels and type commands as you would anywhere, with daily logs written to the SD card."
fact = "Logs to SD"
icon = 5
tag = "LoRa Scanner"
title = "See what is on the air"
text = "Lists every packet it hears with its signal, and reads the Meshtastic and MeshCore headers, and MeshCore's public channel. A Sweep shows signal strength from 863 to 870 MHz. Capture to a pcap for Wireshark."
fact = "Only listens, never transmits"
icon = 1
guide = "/guide/lora-scanner/"
screen = "apps/lora.png"
alt = "The LoRa Scanner's Sweep: bars of signal strength from 863.0 to 870.0 MHz with peak marks, a waterfall under them, and a footer reading floor -100, 866.3 MHz at -88 dBm"
featured = true
[[app]]
num = "05"
tag = "Wi-Fi tools"
title = "Scan the air for networks"
text = "See the networks around you, sort and filter the list, and log scans to a CSV file on the SD card."
fact = "Scans log to CSV"
icon = 3
[[app]]
num = "06"
tag = "Notes"
title = "Write it down"
text = "Keep plain text notes on the SD card, of any size: a megabyte opens as fast as a line. There is no save key: the editor writes five seconds after you stop typing, so a power cut never costs the note."
fact = "Plain .txt files in /notes"
icon = 6
[[app]]
num = "07"
tag = "Storage"
title = "Manage the SD card"
text = "Browse, copy, cut, rename and delete with a clipboard. Opens text, pictures, hex, captures, tracks and update files. Press w and the card is a web page in your phone's browser: upload and download with nothing to install."
fact = "Checks every copy by size"
icon = 8
guide = "/guide/storage/"
screen = "apps/storage.png"
alt = "The Storage App at the top of the SD card: the folders captures, gemini, gnss, irc, notes, pictures, screenshots and system, with 15 GB free"
featured = true
[[app]]
tag = "Notes"
title = "Write it down"
text = "Keep plain text notes on the SD card, of any size: a megabyte opens as fast as a line. There is no save key: the editor writes five seconds after you stop typing, so a power cut never costs the note."
fact = "Plain .txt files in /notes"
icon = 6
guide = "/guide/notes/"
screen = "apps/notes.png"
alt = "The Notes App listing the notes in /notes, each under its first line, with its date"
[[app]]
tag = "SSH"
title = "A terminal on another machine"
text = "Log in to a server with a password or with the device's own key, and run a shell, vim or top on the Cardputer's screen, in sixteen colours. Files go with scp, from the Shell."
fact = "The session stays open when you leave"
icon = 10
guide = "/guide/ssh/"
screen = "apps/ssh.png"
alt = "The SSH App: a host connected to before, then New connection and This device's key"
featured = true
[[app]]
tag = "Shell"
title = "The console, on the device"
text = "Every command the firmware's console knows, typed on its own keyboard: the card's files, ping, nslookup, traceroute, a certificate check, the radio and the GNSS receiver. Tab completes."
fact = "The same commands as over USB"
icon = 11
guide = "/guide/shell/"
screen = "apps/shell.png"
alt = "The Shell after uname -a: roro9stack, its version, xtensa-lx7 esp32s3, and an empty line to type the next command"
[[app]]
tag = "Games"
title = "For when the radio is quiet"
text = "Pong, Snake and Tetris with its tune, Minesweeper, 2048, and Asteroids steered by leaning the device. Pong can be steered by tilting too."
fact = "The best of each is kept"
icon = 12
guide = "/guide/games/"
screen = "apps/games.png"
alt = "The Games App: six icons in a row, Pong chosen on a filled tile, and under them Pong and its longest rally"
featured = true
[[app]]
num = "08"
tag = "System"
title = "Watch it think"
text = "Per-core load, every task and its spare stack, free heap, network traffic, battery and chip temperature, live."
fact = "Live and read-only"
icon = 7
guide = "/guide/system/"
screen = "apps/system.png"
alt = "The System App's overview: the load of both cores, 84 KB of memory free, the network's traffic, the battery at 99 percent, uptime and temperature, and a graph"
[[app]]
tag = "Settings"
title = "Make it yours"
text = "The device's name and region, brightness and seven colour themes, GNSS and the radio, Wi-Fi networks and a WireGuard VPN, firmware updates and the Debug Console, in five groups."
fact = "Every theme dark and light"
icon = 9
guide = "/guide/settings/"
screen = "apps/settings.png"
alt = "Settings, in five groups: This device, Display, GNSS and radio, Network and System"
[[app]]
num = "09"
tag = "Planned"
title = "A mesh messenger"
text = "Sending and reading messages on a Meshtastic-compatible mesh. The radio listens today, in the LoRa Scanner; transmitting is the next step and waits for a second node to test with."
+79
View File
@@ -385,6 +385,85 @@ rows = [
["; .", "up, down"],
]
[[scope]]
id = "games"
title = "Games, the games"
rows = [
["Enter", "this game"],
[", /", "the game before, the next"],
]
[[scope]]
id = "games-pong"
title = "Games, Pong"
rows = [
["; .", "the paddle, while held"],
["0", "steer with the keys, or by tilting"],
["9", "tilting the other way up"],
["Enter", "start, or start again"],
["p", "pause; any key carries on"],
["`", "the other games; this one waits"],
]
[[scope]]
id = "games-snake"
title = "Games, Snake"
rows = [
["; . , /", "turn"],
["Enter", "start, or start again"],
["p", "pause; any key carries on"],
["`", "the other games; this one waits"],
]
[[scope]]
id = "games-tetris"
title = "Games, Tetris"
rows = [
[", /", "left, right; held, it repeats"],
[";", "turn"],
["z", "turn the other way"],
[".", "fall faster, while held"],
["1", "drop"],
["0", "the music, on or off"],
["8 9", "the music, quieter, louder"],
["Enter", "start, or start again"],
["p", "pause; any key carries on"],
["`", "the other games; this one waits"],
]
[[scope]]
id = "games-mines"
title = "Games, Minesweeper"
rows = [
["; . , /", "move; held, it runs"],
["Enter", "dig; on a number, the rest"],
["f", "a flag, on or off"],
["p", "pause; any key carries on"],
["`", "the other games; this one waits"],
]
[[scope]]
id = "games-2048"
title = "Games, 2048"
rows = [
["; . , /", "slide every tile that way"],
["Enter", "start, or start again"],
["`", "the other games; this one waits"],
]
[[scope]]
id = "games-asteroids"
title = "Games, Asteroids"
rows = [
["lean", "turn: left or right"],
[", /", "turn, while held"],
[";", "thrust, while held"],
[".", "fire"],
["Enter", "start, or start again"],
["p", "pause; any key carries on"],
["`", "the other games; this one waits"],
]
[[scope]]
id = "ssh"
title = "SSH, the hosts"
+24 -2
View File
@@ -142,11 +142,32 @@ section { padding-top: 64px; padding-bottom: 64px; }
.card .fact::before { content: ""; width: 8px; height: 8px; background: var(--cyan); flex: none; }
.card.planned .tag { color: var(--orangetext); }
.card.planned .fact::before { background: var(--orange); }
.ic { width: 48px; height: 48px; flex: none; background-image: var(--icons); background-repeat: no-repeat; background-size: 144px 192px; image-rendering: pixelated; }
.ic { width: 48px; height: 48px; flex: none; background-image: var(--icons); background-repeat: no-repeat; background-size: 144px 288px; image-rendering: pixelated; }
.i0 { background-position: 0 0; } .i1 { background-position: -48px 0; } .i2 { background-position: -96px 0; }
.i3 { background-position: 0 -48px; } .i4 { background-position: -48px -48px; } .i5 { background-position: -96px -48px; }
.i6 { background-position: 0 -96px; } .i7 { background-position: -48px -96px; } .i8 { background-position: -96px -96px; }
.i9 { background-position: 0 -144px; }
.i9 { background-position: 0 -144px; } .i10 { background-position: -48px -144px; } .i11 { background-position: -96px -144px; }
.i12 { background-position: 0 -192px; }
/* The Apps page: an App a row, its words beside a screen from the device. */
.apps { display: flex; flex-direction: column; gap: 24px; margin-top: 40px; }
.app { background: var(--s2); padding: 32px; display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 400px); gap: 32px; align-items: center; scroll-margin-top: 96px; }
.app-text { display: flex; flex-direction: column; gap: 16px; min-width: 0; }
.app .card-top { justify-content: flex-start; gap: 16px; }
.app .tag { font: 500 12px/16px var(--mono); letter-spacing: .08em; text-transform: uppercase; color: var(--cyantext); }
.app h2 { font-size: 24px; line-height: 28px; }
.app h2 a { color: inherit; text-decoration: none; }
.app h2 a:hover, .app h2 a:focus-visible { text-decoration: underline; text-underline-offset: 4px; }
.app p { color: var(--muted); }
.app .fact { display: flex; align-items: center; gap: 8px; font: 500 12px/16px var(--mono); letter-spacing: .04em; }
.app .fact::before { content: ""; width: 8px; height: 8px; background: var(--cyan); flex: none; }
.app.planned .tag { color: var(--orangetext); }
.app.planned .fact::before { background: var(--orange); }
.app-guide { font-size: 14px; }
.app-shot { margin: 0; }
.app-shot img { display: block; width: 100%; height: auto; }
.cards-more { margin-top: 24px; }
.card h3 a { color: inherit; text-decoration: none; }
.card h3 a:hover, .card h3 a:focus-visible { text-decoration: underline; text-underline-offset: 4px; }
.cards-note { margin-top: 24px; font-size: 14px; line-height: 20px; color: var(--muted); }
/* Real screenshots */
@@ -225,6 +246,7 @@ footer small { display: block; max-width: 760px; }
section { padding-top: 48px; padding-bottom: 48px; }
.slab, .updates, .cta { padding: 24px; }
.panel, .card { padding: 24px; }
.app { grid-template-columns: minmax(0, 1fr); padding: 24px; gap: 24px; }
.hero .caption { margin-top: -24px; }
.num-row .big { font-size: 48px; line-height: 48px; min-width: 120px; }
}
File diff suppressed because one or more lines are too long

Before

Width:  |  Height:  |  Size: 15 KiB

After

Width:  |  Height:  |  Size: 17 KiB

File diff suppressed because one or more lines are too long

Before

Width:  |  Height:  |  Size: 15 KiB

After

Width:  |  Height:  |  Size: 17 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.6 KiB

+32
View File
@@ -0,0 +1,32 @@
{% extends "base.html" %}
{% block title %}The Apps: roro9stack{% endblock %}
{% block description %}{{ page.description }}{% endblock %}
{% block main %}
{% set apps = load_data(path="data/apps.toml", format="toml") %}
<div class="wrap page">
<header>
<span class="eyebrow">What's inside</span>
<h1>{{ page.title }}</h1>
<p class="lead">{{ page.description }} On the device, the Launcher shows them as a grid of icons; <kbd>Fn</kbd> + <kbd>h</kbd> lists the keys of any screen.</p>
</header>
<div class="apps">
{% for a in apps.app %}
<article class="app n-lg{% if a.planned %} planned{% endif %}" id="{{ a.tag | slugify }}">
<div class="app-text">
<div class="card-top"><div class="ic i{{ a.icon }}" aria-hidden="true"></div><span class="tag">{{ a.tag }}</span></div>
<h2>{% if a.guide %}<a href="{{ a.guide }}">{{ a.title }}</a>{% else %}{{ a.title }}{% endif %}</h2>
<p>{{ a.text }}</p>
<span class="fact">{{ a.fact }}</span>
{% if a.guide %}<a class="accent-link app-guide" href="{{ a.guide }}">Its page in the guide →</a>{% endif %}
</div>
{% if a.screen %}
<figure class="app-shot">
<img src="/screens/{{ a.screen | safe }}" width="480" height="270" alt="{{ a.alt }}" loading="lazy">
</figure>
{% endif %}
</article>
{% endfor %}
</div>
</div>
{% endblock main %}
+1 -1
View File
@@ -27,7 +27,7 @@
<img class="logo logo-light" src="/img/logo-light.svg" alt="" width="201" height="28">
</a>
<nav aria-label="Main">
<a href="/#apps">Apps</a>
<a href="/apps/">Apps</a>
<a href="/install/">Install</a>
<a href="/guide/">Guide</a>
<a href="/dev/">Developers</a>
+9 -8
View File
@@ -12,7 +12,7 @@
<h1 id="hero-title">Open firmware for a <span class="accent">pocket computer.</span></h1>
</div>
<div>
<p class="lead">roro9stack is firmware for the M5Stack Cardputer ADV with the Cap LoRa-1262: a LoRa scanner that reads Meshtastic traffic, GNSS, IRC, a Gemini browser, Wi-Fi tools, notes and more.</p>
<p class="lead">roro9stack is firmware for the M5Stack Cardputer ADV with the Cap LoRa-1262: a LoRa scanner that reads Meshtastic and MeshCore traffic, GNSS with a compass, an SSH client, a Gemini browser, IRC, Wi-Fi tools, notes, games and more.</p>
<div class="actions">
<a class="btn btn-primary n-md" href="/install/">Install{% if latest %} {{ latest.tag_name }}{% endif %}</a>
<a class="btn btn-secondary n-md" href="{{ config.extra.repo }}">Read the source</a>
@@ -55,20 +55,21 @@
<section class="wrap" id="apps" aria-labelledby="apps-title">
<div class="section-head">
<span class="eyebrow">What's inside</span>
<h2 id="apps-title">Apps for the radio, the web and the card.</h2>
<h2 id="apps-title">Apps for the radio, the network, the card and the evening.</h2>
</div>
<div class="cards">
{% for a in apps.app %}
<article class="card n-lg{% if a.planned %} planned{% endif %}">
{% for a in apps.app %}{% if a.featured %}
<article class="card n-lg">
<div class="ic i{{ a.icon }}" aria-hidden="true"></div>
<div class="card-top"><span class="tag">{{ a.tag }}</span><span class="num">{{ a.num }}</span></div>
<h3>{{ a.title }}</h3>
<div class="card-top"><span class="tag">{{ a.tag }}</span></div>
<h3><a class="card-link" href="/apps/#{{ a.tag | slugify }}">{{ a.title }}</a></h3>
<p>{{ a.text }}</p>
<span class="fact">{{ a.fact }}</span>
</article>
{% endfor %}
{% endif %}{% endfor %}
</div>
<p class="cards-note">Plus a <a class="accent-link" href="/guide/shell/">Shell</a> that runs the firmware's commands on the device, an <a class="accent-link" href="/guide/ssh/">SSH client</a> with a real terminal, a <a class="accent-link" href="/guide/vpn/">WireGuard VPN</a>, a screenshot key that works on every screen, and Settings, with its Wi-Fi and Firmware pages. Every App has a page in the <a class="accent-link" href="/guide/">user guide</a>.</p>
<p class="cards-more"><a class="btn btn-secondary n-md" href="/apps/">All {{ apps.app | length - 1 }} Apps, with their screens</a></p>
<p class="cards-note">Also on every screen: a key that lists the keys, and a key that takes a screenshot. Every App has a page in the <a class="accent-link" href="/guide/">user guide</a>.</p>
</section>
<section class="wrap" id="screens" aria-labelledby="screens-title">
+798
View File
@@ -0,0 +1,798 @@
#include "apps/games_app.h"
#include <Arduino.h>
#include <M5Cardputer.h>
#include <M5Unified.h>
#include <esp_random.h>
#include <algorithm>
#include <cstdio>
#include "app_keys.h"
#include "ui/fonts.h"
#include "ui/icons.h"
#include "ui/theme.h"
#include "ui/widgets.h"
namespace roro {
using namespace games;
namespace {
constexpr uint32_t kFrameMs = 33; // 30 pictures a second while a Game runs
constexpr uint32_t kTapMs = 70; // how long a tap moves Pong's paddle
constexpr int kCell = 6; // Snake's and Tetris's cells, in pixels
const char* const kNames[6] = {"Pong", "Snake", "Tetris", "Minesweeper", "2048", "Asteroids"};
const char* const kBestKeys[6] = {"best_pong", "best_snake", "best_tetris", "best_mines", "best_2048", "best_asteroids"};
// Which way a lean turns the ship. The sensor's x runs to the device's right (the Compass's level
// found that): leaning right brings it down, and that is a clockwise turn.
constexpr float kLeanSign = -1.0f;
constexpr float kLeanSeconds = 0.06f; // how fast the reading follows the hand
constexpr float kImuEvery = 0.01f; // seconds between two readings: the sensor gives 100 a second
constexpr int kMineCell = 12; // Minesweeper's cells, in pixels: a digit in each reads
constexpr uint32_t kRunAfterMs = 220, kRunEveryMs = 60; // a held arrow runs the cursor along
// The seven pieces in the theme's seven accents: they follow the theme chosen.
uint16_t pieceColor(uint8_t kind) {
switch (kind) {
case 1: return theme::kAccent;
case 2: return theme::kYellow;
case 3: return theme::kViolet;
case 4: return theme::kGood;
case 5: return theme::kRed;
case 6: return theme::kMessage;
default: return theme::kWarning;
}
}
} // namespace
// The speaker mixes its channels: the tune has one, the beeps another, so a line cleared does
// not cut a note short.
constexpr int kTuneChannel = 0, kBeepChannel = 1;
constexpr uint32_t kBreathMs = 25; // of silence at the end of each note, so two alike are two
void GamesApp::beep(int hz, int ms) {
if (settings_.getBool(Setting::Sound)) M5Cardputer.Speaker.tone(hz, ms, kBeepChannel, true);
}
bool GamesApp::musicOn() {
if (music_ < 0) {
int32_t stored = 1;
music_ = store_.getInt("tetris_music", stored) ? (stored != 0) : 1; // on, until switched off
}
return music_ == 1;
}
// The tune's loudness, in eight steps. The speaker is set for short beeps, at a quarter of what
// it can do: for the tune it is turned up all the way, and each channel's own volume does the
// rest, the tune's by the step chosen, the others' a quarter so a beep stays as loud as it was.
constexpr int kLoudest = 8, kUsualVolume = 4;
// The top is four fifths of all the speaker can do: above that the small speaker distorts.
constexpr uint8_t kStepVolume[kLoudest] = {5, 10, 16, 26, 45, 77, 128, 204}; // 64 is the speaker's usual
int GamesApp::volume() {
if (volume_ < 0) {
int32_t stored = kUsualVolume;
volume_ = store_.getInt("tetris_volume", stored) && stored >= 1 && stored <= kLoudest ? stored : kUsualVolume;
}
return volume_;
}
void GamesApp::setVolume(int level) {
level = std::clamp(level, 1, kLoudest);
if (level == volume()) return;
volume_ = level;
store_.putInt("tetris_volume", level);
if (masterWas_ >= 0) M5Cardputer.Speaker.setChannelVolume(kTuneChannel, kStepVolume[level - 1]);
}
void GamesApp::loud(bool on) {
auto& speaker = M5Cardputer.Speaker;
if (on == (masterWas_ >= 0)) return;
if (on) {
masterWas_ = speaker.getVolume();
speaker.setAllChannelVolume(static_cast<uint8_t>(masterWas_)); // times a master of 255: as before
speaker.setChannelVolume(kTuneChannel, kStepVolume[volume() - 1]);
speaker.setVolume(255);
} else {
speaker.setVolume(static_cast<uint8_t>(masterWas_));
speaker.setAllChannelVolume(255);
masterWas_ = -1;
}
}
void GamesApp::quiet() {
M5Cardputer.Speaker.stop(kTuneChannel);
loud(false);
}
int GamesApp::best(Which w) {
if (best_[w] < 0) {
int32_t stored = 0;
best_[w] = store_.getInt(kBestKeys[w], stored) && stored > 0 ? stored : 0;
}
return best_[w];
}
void GamesApp::onEnter() {
lastMs_ = millis();
requestRedraw();
}
void GamesApp::pause() {
if (which_ != Menu && which_ != Twenty48 && state_[which_] == State::Playing) state_[which_] = State::Paused;
quiet();
}
void GamesApp::onExit() { pause(); }
void GamesApp::begin() {
uint32_t seed = esp_random();
if (which_ == Pong) {
pong_.reset(new games::Pong(seed));
levelTilt();
}
else if (which_ == Snake) snake_.reset(new games::Snake(seed));
else if (which_ == Tetris) tetris_.reset(new games::Tetris(seed));
else if (which_ == Mines) {
mines_.reset(new games::Mines(seed));
cursorCol_ = games::Mines::kCols / 2;
cursorRow_ = games::Mines::kRows / 2;
runDir_ = -1;
} else if (which_ == Twenty48) twenty48_.reset(new games::Twenty48(seed));
else if (which_ == Asteroids) {
asteroids_.reset(new games::Asteroids(seed));
levelTilt();
fired_ = false;
}
state_[which_] = State::Playing;
tune_.restart();
newBest_ = false;
tapped_ = TetrisInput();
tapMove_ = 0;
lastMs_ = millis();
}
// `score`: what is kept as the best if it beats it. For Minesweeper it is the seconds a cleared
// field took, where less is better, and 0 says the field was not cleared.
void GamesApp::ended(int score) {
state_[which_] = State::Over;
quiet();
newBest_ = which_ == Mines ? score > 0 && (best(Mines) == 0 || score < best(Mines)) : score > best(which_);
if (newBest_) {
best_[which_] = score;
store_.putInt(kBestKeys[which_], score);
}
beep(newBest_ ? 1320 : 220, 250);
}
bool GamesApp::onKey(const KeyEvent& e) {
requestRedraw();
if (which_ == Menu) {
switch (e.key) {
case Key::Left: chosen_ = (chosen_ + kGames - 1) % kGames; return true;
case Key::Right: chosen_ = (chosen_ + 1) % kGames; return true;
case Key::Select:
which_ = static_cast<Which>(chosen_);
lastMs_ = millis();
return true;
default: return false; // Back leaves the App
}
}
State& state = state_[which_];
if (which_ == Tetris && e.key == Key::Char && e.ch == '0') { // the tune, on or off, whatever the game is doing
music_ = musicOn() ? 0 : 1;
store_.putInt("tetris_music", music_);
if (!music_) quiet();
return true;
}
if (which_ == Tetris && e.key == Key::Char && (e.ch == '8' || e.ch == '9')) { // quieter, louder
setVolume(volume() + (e.ch == '9' ? 1 : -1));
return true;
}
if (which_ == Pong && e.key == Key::Char && e.ch == '0') { // the keys, or tilting: at any moment
pongTilt_ = pongTilt() ? 0 : 1;
store_.putInt("pong_tilt", pongTilt_);
levelTilt(); // however it is held when switched is the middle
return true;
}
if (which_ == Pong && e.key == Key::Char && e.ch == '9') { // tilting the other way up
pongInverted_ = pongInverted() ? 0 : 1;
store_.putInt("pong_invert", pongInverted_);
return true;
}
if (e.key == Key::Back) { // to the other games; this one waits
pause();
which_ = Menu;
return true;
}
if (which_ == Twenty48 && state != State::Ready && state != State::Over) { // no clock: nothing to pause or carry on
state = State::Playing;
return twenty48Key(e);
}
if (state == State::Ready || state == State::Over) {
if (e.key == Key::Select) begin();
return true;
}
if (state == State::Paused) { // any key carries on
state = State::Playing;
lastMs_ = millis();
return true;
}
if (e.key == Key::Char && (e.ch == 'p' || e.ch == 'P')) {
pause();
return true;
}
return which_ == Pong ? pongKey(e) : which_ == Snake ? snakeKey(e) : which_ == Tetris ? tetrisKey(e) : which_ == Asteroids ? asteroidsKey(e) : minesKey(e);
}
void GamesApp::moveCursor(int dc, int dr) {
cursorCol_ = std::clamp(cursorCol_ + dc, 0, games::Mines::kCols - 1);
cursorRow_ = std::clamp(cursorRow_ + dr, 0, games::Mines::kRows - 1);
}
bool GamesApp::minesKey(const KeyEvent& e) {
uint8_t events = 0;
switch (e.key) {
case Key::Up: moveCursor(0, -1); break;
case Key::Down: moveCursor(0, 1); break;
case Key::Left: moveCursor(-1, 0); break;
case Key::Right: moveCursor(1, 0); break;
case Key::Select: events = mines_->dig(cursorCol_, cursorRow_); break;
case Key::Char:
if (e.ch == 'f' || e.ch == 'F') events = mines_->flag(cursorCol_, cursorRow_);
break;
default: break;
}
if (events & games::Mines::Boom) ended(0);
else if (events & games::Mines::Won) ended(std::max<int>(1, static_cast<int>(mines_->seconds())));
else if (events & games::Mines::Flagged) beep(660, 10);
else if (events & games::Mines::Opened) beep(880, 8);
return true;
}
bool GamesApp::asteroidsKey(const KeyEvent& e) {
if (e.key == Key::Down) fired_ = true; // . fires; ; held is thrust, read in update()
return true;
}
void GamesApp::readTilt() {
if (!M5.Imu.update()) return;
auto data = M5.Imu.getImuData();
float g = std::sqrt(data.accel.x * data.accel.x + data.accel.y * data.accel.y + data.accel.z * data.accel.z);
if (g < 0.5f || g > 1.5f) return; // a shake, not a lean
float k = std::min(1.0f, kImuEvery / kLeanSeconds);
lean_ += k * (data.accel.x / g - lean_);
pitch_ += k * (data.accel.y / g - pitch_);
}
void GamesApp::levelTilt() {
for (int i = 0; i < 20 && !M5.Imu.update(); i++) delay(2); // a fresh reading
auto data = M5.Imu.getImuData();
float g = std::sqrt(data.accel.x * data.accel.x + data.accel.y * data.accel.y + data.accel.z * data.accel.z);
lean_ = level_ = g > 0.5f ? data.accel.x / g : 0;
pitch_ = pitchLevel_ = g > 0.5f ? data.accel.y / g : 0;
}
bool GamesApp::pongInverted() {
if (pongInverted_ < 0) {
int32_t stored = 0;
pongInverted_ = store_.getInt("pong_invert", stored) && stored == 1 ? 1 : 0;
}
return pongInverted_ == 1;
}
bool GamesApp::pongTilt() {
if (pongTilt_ < 0) {
int32_t stored = 0;
pongTilt_ = store_.getInt("pong_tilt", stored) && stored == 1 ? 1 : 0; // the keys, until switched
}
return pongTilt_ == 1;
}
bool GamesApp::twenty48Key(const KeyEvent& e) {
using Dir = games::Twenty48::Dir;
uint8_t events = 0;
switch (e.key) {
case Key::Up: events = twenty48_->slide(Dir::Up); break;
case Key::Down: events = twenty48_->slide(Dir::Down); break;
case Key::Left: events = twenty48_->slide(Dir::Left); break;
case Key::Right: events = twenty48_->slide(Dir::Right); break;
default: break;
}
if (events & games::Twenty48::Over) ended(static_cast<int>(twenty48_->score()));
else if (events & games::Twenty48::Reached) beep(1320, 250);
else if (events & games::Twenty48::Merged) beep(880, 10);
return true;
}
bool GamesApp::pongKey(const KeyEvent& e) {
if (e.key != Key::Up && e.key != Key::Down) return true;
tapMove_ = e.key == Key::Up ? -1 : 1;
tapUntilMs_ = millis() + kTapMs;
return true;
}
bool GamesApp::snakeKey(const KeyEvent& e) {
switch (e.key) {
case Key::Up: snake_->turn(Snake::Dir::Up); break;
case Key::Down: snake_->turn(Snake::Dir::Down); break;
case Key::Left: snake_->turn(Snake::Dir::Left); break;
case Key::Right: snake_->turn(Snake::Dir::Right); break;
default: break;
}
return true;
}
bool GamesApp::tetrisKey(const KeyEvent& e) {
switch (e.key) {
case Key::Up: tapped_.rotate = true; break;
case Key::Left: tapped_.left = true; break;
case Key::Right: tapped_.right = true; break;
case Key::Down: tapped_.soft = true; break;
case Key::Char:
if (e.ch == 'z' || e.ch == 'Z') tapped_.rotateBack = true;
else if (e.ch == '1') tapped_.hardDrop = true; // far from the arrows: not hit by mistake
break;
default: break;
}
return true;
}
void GamesApp::update(uint32_t nowMs) {
uint32_t ms = std::min<uint32_t>(nowMs - lastMs_, 100);
if (which_ == Menu || state_[which_] != State::Playing) {
lastMs_ = nowMs;
return;
}
if (ms == 0) return;
lastMs_ = nowMs;
if (which_ == Pong) {
float move = keys_.held(Key::Up) ? -1 : keys_.held(Key::Down) ? 1 : 0;
if (!move && static_cast<int32_t>(tapUntilMs_ - nowMs) > 0) move = tapMove_;
if (pongTilt()) { // tilted: the paddle rolls to the low side, the keys still win
readTilt();
float pitch = pongInverted() ? 2 * pitchLevel_ - pitch_ : pitch_; // the other way up: mirrored round the middle
if (!move) move = games::moveToward(pong_->playerY(), games::tiltTarget(pitch, pitchLevel_));
}
uint8_t events = pong_->step(ms / 1000.0f, move);
if (events & Pong::Over) ended(pong_->longestRally());
else if (events & Pong::Point) beep(330, 120);
else if (events & Pong::Hit) beep(880, 15);
else if (events & Pong::Wall) beep(440, 10);
} else if (which_ == Snake) {
uint8_t events = snake_->step(ms);
if (events & Snake::Died) ended(snake_->score());
else if (events & Snake::Ate) beep(990, 25);
} else if (which_ == Mines) {
mines_->tick(ms);
// An arrow held runs the cursor along, after a moment: the field is thirty cells wide.
static const Key kArrows[4] = {Key::Up, Key::Right, Key::Down, Key::Left};
int dir = -1;
for (int i = 0; i < 4; i++)
if (keys_.held(kArrows[i])) dir = i;
if (dir != runDir_) {
runDir_ = dir;
runMs_ = 0;
running_ = false;
} else if (dir >= 0) {
runMs_ += ms;
uint32_t wait = running_ ? kRunEveryMs : kRunAfterMs;
if (runMs_ >= wait) {
runMs_ -= wait;
running_ = true;
moveCursor(dir == 1 ? 1 : dir == 3 ? -1 : 0, dir == 0 ? -1 : dir == 2 ? 1 : 0);
requestRedraw();
}
}
if (mines_->seconds() != shownSeconds_) {
shownSeconds_ = mines_->seconds();
requestRedraw();
}
return;
} else if (which_ == Twenty48) {
return; // it only moves when a key says so
} else if (which_ == Asteroids) {
games::AsteroidsInput in;
readTilt();
in.turn = kLeanSign * games::tiltTurn(lean_, level_);
if (keys_.held(Key::Left)) in.turn = -1; // the arrows too, for a device on a table
if (keys_.held(Key::Right)) in.turn = 1;
in.thrust = keys_.held(Key::Up);
in.fire = fired_;
fired_ = false;
uint8_t events = asteroids_->step(ms / 1000.0f, in);
if (events & games::Asteroids::Over) ended(asteroids_->score());
else if (events & games::Asteroids::Died) beep(150, 300);
else if (events & games::Asteroids::Wave) beep(990, 120);
else if (events & games::Asteroids::Hit) beep(300, 30);
else if (events & games::Asteroids::Shot) beep(1400, 8);
} else {
TetrisInput in = tapped_;
tapped_ = TetrisInput();
in.left |= keys_.held(Key::Left);
in.right |= keys_.held(Key::Right);
in.soft |= keys_.held(Key::Down);
uint8_t events = tetris_->step(ms, in);
if (events & Tetris::Over) ended(tetris_->score());
else if (events & Tetris::LevelUp) beep(1320, 120);
else if (events & Tetris::Lines) beep(tetris_->lastCleared() == 4 ? 1320 : 880, 60);
else if (events & Tetris::Locked) beep(220, 12);
Note note{};
uint32_t length = 0;
tune_.setEighthMs(korobeiniki::eighthMsAt(tetris_->level())); // it hurries as the game does
if (state_[Tetris] == State::Playing && musicOn() && settings_.getBool(Setting::Sound) && tune_.step(ms, note, length) && note.hz) {
loud(true);
M5Cardputer.Speaker.tone(note.hz, length - kBreathMs, kTuneChannel, true);
}
}
if (nowMs - lastDrawMs_ >= kFrameMs) {
lastDrawMs_ = nowMs;
requestRedraw();
}
}
void GamesApp::help(std::vector<KeyHelp>& out) const {
switch (which_) {
case Pong: return keys::add(out, keys::kGamesPong);
case Snake: return keys::add(out, keys::kGamesSnake);
case Tetris: return keys::add(out, keys::kGamesTetris);
case Mines: return keys::add(out, keys::kGamesMines);
case Twenty48: return keys::add(out, keys::kGames2048);
case Asteroids: return keys::add(out, keys::kGamesAsteroids);
default: return keys::add(out, keys::kGames);
}
}
const char* GamesApp::helpTitle() const {
static const char* const kTitles[kGames] = {"Games: Pong", "Games: Snake", "Games: Tetris", "Games: Minesweeper", "Games: 2048", "Games: Asteroids"};
return which_ == Menu ? nullptr : kTitles[which_];
}
// A box in the middle of the field: where the Game stands when it is not running.
void GamesApp::drawBanner(Canvas& c, const std::string& title, const std::string& detail) {
const auto& area = theme::kContent;
const int w = 150, h = detail.empty() ? 24 : 40, x = (area.w - w) / 2, y = area.y + (area.h - h) / 2;
c.fillRect(x, y, w, h, theme::kBackground);
c.drawRect(x, y, w, h, theme::kAccent);
c.setTextDatum(top_center);
c.setFont(&fonts::bold);
c.setTextColor(theme::kText);
c.drawString(title.c_str(), area.w / 2, y + 5);
if (!detail.empty()) {
c.setFont(&fonts::body);
c.setTextColor(theme::kMuted);
c.drawString(detail.c_str(), area.w / 2, y + 21);
}
c.setTextDatum(top_left);
}
// The Games as the Launcher shows Apps: an icon each, the chosen one on a cyan tile, its
// name under the row. A Game waiting paused has the Launcher's orange dot.
void GamesApp::drawMenu(Canvas& c) {
using namespace theme;
static const uint8_t* const kIcons[kGames] = {icons::pong, icons::snake, icons::tetris, icons::mines, icons::twenty48, icons::asteroids};
const int tileW = 40, tileH = 36, x0 = (kContent.w - kGames * tileW) / 2, y = kContent.y + 24; // six fill the width
for (int i = 0; i < kGames; i++) {
int x = x0 + i * tileW, ix = x + (tileW - icons::kSize) / 2, iy = y + (tileH - icons::kSize) / 2;
bool chosen = i == chosen_;
if (chosen) {
int tw = std::min(icons::kSize + 12, tileW), tx = ix - (tw - icons::kSize) / 2;
c.fillRect(tx + 4, y, tw - 8, tileH, kAccent);
c.fillRect(tx + 2, y + 2, tw - 4, tileH - 4, kAccent);
c.fillRect(tx, y + 4, tw, tileH - 8, kAccent);
}
c.drawBitmap(ix, iy, kIcons[i], icons::kSize, icons::kSize, chosen ? kFaint : kAccent);
if (state_[i] == State::Paused || (i == Twenty48 && state_[i] == State::Playing)) { // a game is waiting
c.fillRect(ix + icons::kSize - 6, iy - 1, 10, 10, chosen ? kAccent : kBackground);
c.fillRect(ix + icons::kSize - 4, iy + 1, 6, 6, kWarning);
}
}
auto w = static_cast<Which>(chosen_);
c.setTextDatum(top_center);
c.setFont(&fonts::bold);
c.setTextColor(kText);
c.drawString(kNames[chosen_], kContent.w / 2, y + tileH + 8);
int b = best(w);
std::string under = state_[chosen_] == State::Paused ? "paused" : w == Twenty48 && state_[chosen_] == State::Playing ? "waiting" : "";
if (b > 0)
under += (under.empty() ? "" : ", ") + std::string(w == Pong ? "longest rally " : w == Mines ? "cleared in " : "best ") + std::to_string(b) + (w == Mines ? " s" : "");
c.setFont(&fonts::body);
c.setTextColor(kMuted);
c.drawString(under.c_str(), kContent.w / 2, y + tileH + 8 + kLineHeight + 2);
c.setTextDatum(top_left);
}
void GamesApp::drawPong(Canvas& c) {
const auto& area = theme::kContent;
State state = state_[Pong];
const int top = area.y;
for (int y = top + 2; y < top + static_cast<int>(Pong::kHeight); y += 8) c.drawFastVLine(area.w / 2, y, 4, theme::kFaint);
if (pong_) {
char score[8];
c.setFont(&fonts::large);
c.setTextColor(theme::kMuted);
c.setTextDatum(top_center);
std::snprintf(score, sizeof score, "%d", pong_->playerScore());
c.drawString(score, area.w / 2 - 24, top + 3);
std::snprintf(score, sizeof score, "%d", pong_->deviceScore());
c.drawString(score, area.w / 2 + 24, top + 3);
c.setTextDatum(top_left);
const int ph = static_cast<int>(Pong::kPaddleH), pw = static_cast<int>(Pong::kPaddleW), m = static_cast<int>(Pong::kMargin);
c.fillRect(m, top + static_cast<int>(pong_->playerY()) - ph / 2, pw, ph, theme::kAccent);
c.fillRect(area.w - m - pw, top + static_cast<int>(pong_->deviceY()) - ph / 2, pw, ph, theme::kWarning);
const int b = static_cast<int>(Pong::kBall);
c.fillRect(static_cast<int>(pong_->ballX()) - b / 2, top + static_cast<int>(pong_->ballY()) - b / 2, b, b, theme::kText);
}
c.setFont(&fonts::small);
c.setTextColor(theme::kMuted);
c.drawString(!pongTilt() ? "keys" : pongInverted() ? "tilt, inverted" : "tilt", 2, top + 2);
int b = best(Pong);
std::string bestLine = b > 0 ? "longest rally " + std::to_string(b) : "first to 7";
if (state == State::Ready) drawBanner(c, "Pong", bestLine);
else if (state == State::Paused) drawBanner(c, "Paused", "");
else if (state == State::Over)
drawBanner(c, pong_->playerWon() ? "You win" : "The device wins",
newBest_ ? "a new longest rally: " + std::to_string(b) : "rally " + std::to_string(pong_->longestRally()) + ", best " + std::to_string(b));
}
void GamesApp::drawSnake(Canvas& c) {
const auto& area = theme::kContent;
State state = state_[Snake];
const int w = Snake::kCols * kCell, h = Snake::kRows * kCell, x0 = (area.w - w) / 2, y0 = area.y + area.h - h - 1;
c.drawRect(x0 - 1, y0 - 1, w + 2, h + 2, theme::kFaint);
c.setFont(&fonts::small);
c.setTextColor(theme::kMuted);
char line[32];
std::snprintf(line, sizeof line, "%d", snake_ ? snake_->score() : 0);
c.drawString(line, x0, area.y);
std::snprintf(line, sizeof line, "best %d", best(Snake));
c.setTextDatum(top_right);
c.drawString(line, x0 + w, area.y);
c.setTextDatum(top_left);
if (snake_) {
auto f = snake_->food();
c.fillRect(x0 + f.col * kCell + 1, y0 + f.row * kCell + 1, kCell - 2, kCell - 2, theme::kWarning);
bool head = true;
for (auto& cell : snake_->body()) {
c.fillRect(x0 + cell.col * kCell, y0 + cell.row * kCell, kCell - 1, kCell - 1, head ? theme::kText : theme::kGood);
head = false;
}
}
if (state == State::Ready) drawBanner(c, "Snake", "the walls bite");
else if (state == State::Paused) drawBanner(c, "Paused", "");
else if (state == State::Over)
drawBanner(c, "Game over", newBest_ ? "a new best: " + std::to_string(snake_->score()) : std::to_string(snake_->score()) + ", best " + std::to_string(best(Snake)));
}
void GamesApp::drawTetris(Canvas& c) {
const auto& area = theme::kContent;
State state = state_[Tetris];
const int w = Tetris::kCols * kCell, h = Tetris::kRows * kCell, x0 = (area.w - w) / 2, y0 = area.y + 1;
c.drawFastVLine(x0 - 1, y0, h + 1, theme::kMuted);
c.drawFastVLine(x0 + w, y0, h + 1, theme::kMuted);
c.drawFastHLine(x0 - 1, y0 + h, w + 2, theme::kMuted);
auto block = [&](int x, int y, uint8_t kind) { c.fillRect(x, y, kCell - 1, kCell - 1, pieceColor(kind)); };
c.setFont(&fonts::body);
const int right = x0 + w + 12, left = 6;
auto readout = [&](int x, int y, const char* label, int value) {
c.setTextColor(theme::kMuted);
c.drawString(label, x, y);
c.setTextColor(theme::kText);
c.drawString(std::to_string(value).c_str(), x, y + theme::kLineHeight);
};
readout(left, area.y + 4, "Score", tetris_ ? tetris_->score() : 0);
readout(left, area.y + 38, "Best", best(Tetris));
readout(left, area.y + 72, "Lines", tetris_ ? tetris_->lines() : 0);
c.setTextColor(theme::kMuted);
c.drawString("Next", right, area.y + 4);
readout(right, area.y + 72, "Level", tetris_ ? tetris_->level() + 1 : 1);
c.setFont(&fonts::small);
c.setTextColor(theme::kMuted);
char music[20];
std::snprintf(music, sizeof music, "music %d/%d", volume(), kLoudest);
c.drawString(!settings_.getBool(Setting::Sound) ? "sound off" : musicOn() ? music : "music off", right, area.y + area.h - 10);
c.setFont(&fonts::body);
if (tetris_) {
for (int row = 0; row < Tetris::kRows; row++)
for (int col = 0; col < Tetris::kCols; col++)
if (uint8_t kind = tetris_->cell(col, row)) block(x0 + col * kCell, y0 + row * kCell, kind);
if (state != State::Over)
for (auto& cell : Tetris::cells(tetris_->piece()))
if (cell.row >= 0) block(x0 + cell.col * kCell, y0 + cell.row * kCell, tetris_->piece().kind);
Tetris::Piece next{tetris_->next(), 0, 0, 0};
for (auto& cell : Tetris::cells(next)) block(right + cell.col * kCell, area.y + 22 + cell.row * kCell, next.kind);
}
if (state == State::Ready) drawBanner(c, "Tetris", "");
else if (state == State::Paused) drawBanner(c, "Paused", "");
else if (state == State::Over)
drawBanner(c, "Game over", newBest_ ? "a new best: " + std::to_string(tetris_->score()) : std::to_string(tetris_->lines()) + " lines");
}
// Minesweeper: the field under a line that says how it stands. No box is drawn over the field when
// the game ends: where the mines were is what one wants to see.
void GamesApp::drawMines(Canvas& c) {
using games::Mines;
const auto& area = theme::kContent;
State state = state_[GamesApp::Mines];
const int x0 = 0, y0 = area.y + area.h - Mines::kRows * kMineCell;
static const uint16_t* const kDigit[8] = {&theme::kAccent, &theme::kGood, &theme::kRed, &theme::kViolet,
&theme::kWarning, &theme::kMessage, &theme::kYellow, &theme::kText};
char line[40];
c.setFont(&fonts::body);
c.setTextColor(theme::kMuted);
int b = best(GamesApp::Mines);
bool lost = mines_ && mines_->phase() == Mines::Phase::Lost, won = mines_ && mines_->phase() == Mines::Phase::Won;
if (lost) {
c.setTextColor(theme::kWarning);
std::snprintf(line, sizeof line, "A mine. %lu s", (unsigned long)mines_->seconds());
} else if (won) {
c.setTextColor(theme::kGood);
if (newBest_) std::snprintf(line, sizeof line, "Cleared in %lu s: a new best", (unsigned long)mines_->seconds());
else std::snprintf(line, sizeof line, "Cleared in %lu s", (unsigned long)mines_->seconds());
} else std::snprintf(line, sizeof line, "%d mines", mines_ ? mines_->minesLeft() : Mines::kMines);
c.drawString(line, 2, area.y + 1);
c.setTextColor(theme::kMuted);
c.setTextDatum(top_right);
if (b > 0) std::snprintf(line, sizeof line, "%lu s best %d", (unsigned long)(mines_ ? mines_->seconds() : 0), b);
else std::snprintf(line, sizeof line, "%lu s", (unsigned long)(mines_ ? mines_->seconds() : 0));
c.drawString(line, area.w - 2, area.y + 1);
c.setTextDatum(top_left);
c.setFont(&fonts::bold);
c.setTextDatum(middle_center);
for (int row = 0; row < Mines::kRows; row++)
for (int col = 0; col < Mines::kCols; col++) {
int x = x0 + col * kMineCell, y = y0 + row * kMineCell;
bool open = mines_ && mines_->open(col, row), flagged = mines_ && mines_->flagged(col, row);
bool mine = mines_ && mines_->mine(col, row);
if (lost && mine && !flagged) { // where they were; the one dug is the bright one
c.fillRect(x, y, kMineCell - 1, kMineCell - 1, open ? theme::kRed : theme::kFaint);
c.fillCircle(x + kMineCell / 2 - 1, y + kMineCell / 2 - 1, 3, open ? theme::kText : theme::kRed);
} else if (!open) {
c.fillRect(x, y, kMineCell - 1, kMineCell - 1, theme::kFaint);
if (flagged) { // a little flag on a pole; a wrong one goes pale when the game is lost
uint16_t colour = lost && !mine ? theme::kMuted : theme::kWarning;
c.fillTriangle(x + 4, y + 2, x + 4, y + 6, x + 8, y + 4, colour);
c.drawFastVLine(x + 4, y + 2, 7, colour);
}
} else if (int n = mines_->around(col, row)) {
c.setTextColor(*kDigit[n - 1]);
c.drawString(std::to_string(n).c_str(), x + kMineCell / 2, y + kMineCell / 2);
}
}
c.setTextDatum(top_left);
if (state == State::Playing)
c.drawRect(x0 + cursorCol_ * kMineCell - 1, y0 + cursorRow_ * kMineCell - 1, kMineCell + 1, kMineCell + 1, theme::kText);
if (state == State::Ready) drawBanner(c, "Minesweeper", b > 0 ? "cleared in " + std::to_string(b) + " s" : std::to_string(Mines::kMines) + " mines");
else if (state == State::Paused) drawBanner(c, "Paused", "");
}
// 2048: the board in the middle, the score beside it.
void GamesApp::drawTwenty48(Canvas& c) {
using games::Twenty48;
const auto& area = theme::kContent;
State state = state_[GamesApp::Twenty48];
const int tile = 27, gap = 3, size = 4 * tile + 5 * gap, x0 = (area.w - size) / 2, y0 = area.y;
c.fillRect(x0, y0, size, size, theme::kBar);
// A colour a power of two, round the theme's accents; the low ones are quiet.
static const uint16_t* const kTile[] = {&theme::kFaint, &theme::kMuted, &theme::kAccent, &theme::kGood, &theme::kYellow, &theme::kWarning,
&theme::kRed, &theme::kMessage, &theme::kViolet, &theme::kAccent, &theme::kText};
c.setTextDatum(middle_center);
for (int row = 0; row < 4; row++)
for (int col = 0; col < 4; col++) {
int x = x0 + gap + col * (tile + gap), y = y0 + gap + row * (tile + gap);
uint8_t p = twenty48_ ? twenty48_->power(col, row) : 0;
if (!p) {
c.fillRect(x, y, tile, tile, theme::kBackground);
continue;
}
c.fillRect(x, y, tile, tile, *kTile[std::min<int>(p, 11) - 1]);
uint32_t v = twenty48_->value(col, row);
c.setFont(v < 100 ? &fonts::bold : v < 10000 ? &fonts::medium : &fonts::small);
c.setTextColor(p == 1 ? theme::kText : theme::kBackground);
c.drawString(std::to_string(v).c_str(), x + tile / 2 + 1, y + tile / 2 + 1);
}
c.setTextDatum(top_left);
c.setFont(&fonts::body);
auto readout = [&](int x, int y, const char* label, uint32_t value) {
c.setTextColor(theme::kMuted);
c.drawString(label, x, y);
c.setTextColor(theme::kText);
c.drawString(std::to_string(value).c_str(), x, y + theme::kLineHeight);
};
readout(6, area.y + 4, "Score", twenty48_ ? twenty48_->score() : 0);
readout(x0 + size + 8, area.y + 4, "Best", static_cast<uint32_t>(best(GamesApp::Twenty48)));
c.setFont(&fonts::small);
if (state == State::Over) {
c.setTextColor(theme::kWarning);
c.drawString("No move", 6, area.y + area.h - 20);
c.drawString(newBest_ ? "left: a best" : "left", 6, area.y + area.h - 10);
} else if (twenty48_ && twenty48_->reached()) {
c.setTextColor(theme::kGood);
c.drawString("2048!", 6, area.y + area.h - 10);
}
if (state == State::Ready) drawBanner(c, "2048", best(GamesApp::Twenty48) > 0 ? "best " + std::to_string(best(GamesApp::Twenty48)) : "");
}
// Asteroids, in outlines: the rocks, the ship, the bullets, and a line of figures along the top.
void GamesApp::drawAsteroids(Canvas& c) {
using games::Asteroids;
const auto& area = theme::kContent;
State state = state_[GamesApp::Asteroids];
const int top = area.y;
if (asteroids_) {
for (auto& r : asteroids_->rocks()) {
float radius = r.radius();
// A rock across an edge is drawn on both sides of it, as the field wraps round.
for (float ox : {-Asteroids::kWidth, 0.0f, Asteroids::kWidth})
for (float oy : {-Asteroids::kHeight, 0.0f, Asteroids::kHeight}) {
float x = r.x + ox, y = r.y + oy;
if (x < -radius || x > Asteroids::kWidth + radius || y < -radius || y > Asteroids::kHeight + radius) continue;
int px[8], py[8];
for (int k = 0; k < 8; k++) {
float a = k * 0.785398f, d = radius * r.look[k] / 255.0f;
px[k] = static_cast<int>(std::lround(x + std::sin(a) * d));
py[k] = top + static_cast<int>(std::lround(y - std::cos(a) * d));
}
for (int k = 0; k < 8; k++) c.drawLine(px[k], py[k], px[(k + 1) % 8], py[(k + 1) % 8], theme::kText);
}
}
for (auto& b : asteroids_->bullets()) c.fillRect(static_cast<int>(b.x), top + static_cast<int>(b.y), 2, 2, theme::kYellow);
// The ship: a dart pointing where it goes. Shielded, it blinks.
if (asteroids_->shipShown() && (!asteroids_->shielded() || (millis() / 120) % 2)) {
float a = asteroids_->shipAngle(), s = std::sin(a), co = std::cos(a);
auto at = [&](float dx, float dy, int& x, int& y) {
x = static_cast<int>(std::lround(asteroids_->shipX() + dx * co - dy * s));
y = top + static_cast<int>(std::lround(asteroids_->shipY() + dx * s + dy * co));
};
int nx, ny, lx, ly, rx, ry, bx, by;
at(0, -6, nx, ny);
at(-4, 4, lx, ly);
at(4, 4, rx, ry);
at(0, 2, bx, by);
c.drawLine(nx, ny, lx, ly, theme::kAccent);
c.drawLine(nx, ny, rx, ry, theme::kAccent);
c.drawLine(lx, ly, bx, by, theme::kAccent);
c.drawLine(rx, ry, bx, by, theme::kAccent);
if (asteroids_->thrusting() && (millis() / 60) % 2) {
int fx, fy;
at(0, 7, fx, fy);
c.drawLine(bx, by, fx, fy, theme::kWarning);
}
}
}
c.setFont(&fonts::small);
c.setTextColor(theme::kMuted);
char line[32];
std::snprintf(line, sizeof line, "%d", asteroids_ ? asteroids_->score() : 0);
c.drawString(line, 2, top + 1);
c.setTextDatum(top_right);
std::snprintf(line, sizeof line, "wave %d ships %d", asteroids_ ? asteroids_->wave() : 1, asteroids_ ? asteroids_->lives() : Asteroids::kLives);
c.drawString(line, area.w - 2, top + 1);
c.setTextDatum(top_left);
int b = best(GamesApp::Asteroids);
if (state == State::Ready) drawBanner(c, "Asteroids", b > 0 ? "best " + std::to_string(b) : "lean to turn");
else if (state == State::Paused) drawBanner(c, "Paused", "");
else if (state == State::Over)
drawBanner(c, "Game over", newBest_ ? "a new best: " + std::to_string(asteroids_->score()) : std::to_string(asteroids_->score()) + ", best " + std::to_string(b));
}
void GamesApp::draw(Canvas& c) {
c.setTextDatum(top_left);
lastDrawMs_ = millis();
switch (which_) {
case Pong: drawPong(c); break;
case Snake: drawSnake(c); break;
case Tetris: drawTetris(c); break;
case Mines: drawMines(c); break;
case Twenty48: drawTwenty48(c); break;
case Asteroids: drawAsteroids(c); break;
default: drawMenu(c); break;
}
}
} // namespace roro
+107
View File
@@ -0,0 +1,107 @@
#pragma once
#include <memory>
#include <string>
#include "app.h"
#include "key_mapper.h"
#include "asteroids.h"
#include "key_value_store.h"
#include "mines.h"
#include "pong.h"
#include "settings.h"
#include "snake.h"
#include "tetris.h"
#include "tune.h"
#include "twenty48.h"
namespace roro {
// The Games App (issue #101): three Games as a row of icons, and the one being played. The rules are in
// lib/games; this draws them, gives them the keys and the time, beeps, and keeps the best of each.
// A Game left in the middle, for the list or for another App, is found again paused.
class GamesApp : public App {
public:
GamesApp(const KeyMapper& keys, const Settings& settings, KeyValueStore& store) : keys_(keys), settings_(settings), store_(store) {}
void onEnter() override;
void onExit() override;
bool onKey(const KeyEvent& e) override;
void update(uint32_t nowMs) override;
void draw(Canvas& c) override;
void help(std::vector<KeyHelp>& out) const override;
const char* helpTitle() const override;
// A Game being played is the device in use, key or no key: the screen stays lit.
// Not for the two that wait for a move: there a key is pressed, or nobody is playing.
bool keepsScreenOn() const override { return which_ != Menu && which_ != Mines && which_ != Twenty48 && state_[which_] == State::Playing; }
private:
enum Which { Pong, Snake, Tetris, Mines, Twenty48, Asteroids, Menu }; // all but the last index the arrays
static constexpr int kGames = 6;
enum class State : uint8_t { Ready, Playing, Paused, Over };
void begin(); // a new game of the one chosen
void pause();
void ended(int score);
void beep(int hz, int ms);
bool musicOn();
int volume(); // of the tune, 1 to kLoudest
void setVolume(int level);
void quiet(); // the tune stops where it is
void loud(bool on); // the speaker turned up for the tune, or back as it was
int best(Which w);
bool pongKey(const KeyEvent& e);
bool snakeKey(const KeyEvent& e);
bool tetrisKey(const KeyEvent& e);
bool minesKey(const KeyEvent& e);
bool twenty48Key(const KeyEvent& e);
bool asteroidsKey(const KeyEvent& e);
void readTilt(); // the device's lean, both ways, from the accelerometer, smoothed
void levelTilt(); // however it is held now is "straight on"
bool pongTilt(); // Pong steered by tilting (0 switches, remembered)
bool pongInverted(); // and the other way up (9 switches, remembered)
void drawAsteroids(Canvas& c);
void moveCursor(int dc, int dr);
void drawMines(Canvas& c);
void drawTwenty48(Canvas& c);
void drawMenu(Canvas& c);
void drawPong(Canvas& c);
void drawSnake(Canvas& c);
void drawTetris(Canvas& c);
void drawBanner(Canvas& c, const std::string& title, const std::string& detail);
const KeyMapper& keys_;
const Settings& settings_;
KeyValueStore& store_;
int chosen_ = 0; // in the row of games
Which which_ = Menu;
State state_[kGames] = {State::Ready, State::Ready, State::Ready, State::Ready, State::Ready, State::Ready};
int best_[kGames] = {-1, -1, -1, -1, -1, -1}; // -1: not read from the store yet
bool newBest_ = false;
std::unique_ptr<games::Pong> pong_;
std::unique_ptr<games::Snake> snake_;
std::unique_ptr<games::Tetris> tetris_;
std::unique_ptr<games::Mines> mines_;
std::unique_ptr<games::Twenty48> twenty48_;
std::unique_ptr<games::Asteroids> asteroids_;
// Asteroids is steered by leaning the device left and right, Pong if asked by tilting it up and
// down: "level" is how it was held when the game began.
float lean_ = 0, level_ = 0, pitch_ = 0, pitchLevel_ = 0;
int pongTilt_ = -1, pongInverted_ = -1; // -1: not read from the store yet
bool fired_ = false;
// Minesweeper's cursor, and the arrow held to run it along.
int cursorCol_ = 0, cursorRow_ = 0, runDir_ = -1;
uint32_t runMs_ = 0, shownSeconds_ = 0;
bool running_ = false;
uint32_t lastMs_ = 0, lastDrawMs_ = 0;
// Keys that came as presses since the last step: a tap moves too, held or not.
int tapMove_ = 0;
uint32_t tapUntilMs_ = 0;
games::TetrisInput tapped_;
// Tetris's tune (Korobeiniki), switched with 0 and remembered.
games::TunePlayer tune_{games::korobeiniki::at, games::korobeiniki::kCount, games::korobeiniki::kEighthMs};
int music_ = -1, volume_ = -1; // -1: not read from the store yet
int masterWas_ = -1; // the speaker's own volume, while the tune has it turned up
};
} // namespace roro
+13 -1
View File
@@ -18,6 +18,7 @@
#include "services/ssh_service.h"
#include "services/vpn_service.h"
#include "services/web_share.h"
#include "apps/games_app.h"
#include "apps/gemini_app.h"
#include "apps/gnss_app.h"
#include "auto_update.h"
@@ -261,6 +262,7 @@ void setup() {
netTools.ntpServer = []() { return settings.getString(Setting::Ntp1); };
// A long note being rewritten (issue #47): the editor waits for the card, so it draws from there.
NoteEditor::onProgress = [](const std::string& name, int percent) { screen.renderUpdate("Saving", name, percent); };
apps->registerApp({"games", "Games", false, new GamesApp(keyMapper, settings, nvs), icons::games});
apps->registerApp({"system", "System", false,
new SystemApp(*wifi, *battery, *storageService, *radioService, *gnssService, nvs), icons::system});
apps->registerApp({"settings", "Settings", false,
@@ -579,6 +581,7 @@ static void updateCommand(const String& args) {
console.printf("update: installing by itself %s%s%s\n", settings.getBool(Setting::InstallUpdates) ? "on" : "off",
autoUpdate.waiting() ? ", waiting for a quiet moment to offer " : autoUpdate.asking() ? ", asking about " : "",
autoUpdate.waiting() || autoUpdate.asking() ? autoUpdate.tag().c_str() : "");
console.printf("update: the last download was carried on %d time(s) after a break\n", update->lastResumes());
console.printf("update: gitea %s %s\n", g.status() == GiteaReleases::Status::Done ? "done" : g.status() == GiteaReleases::Status::Failed ? "failed" : g.status() == GiteaReleases::Status::Busy ? "busy" : "never asked", g.error().c_str());
printReleases(false);
} else if (args.startsWith("install ")) {
@@ -594,7 +597,8 @@ static void updateCommand(const String& args) {
} else if (args.startsWith("damage ")) { // update damage cut <bytes> | flip <offset>: for the next download
long n = args.substring(args.lastIndexOf(' ') + 1).toInt();
args.startsWith("damage cut") ? update->damageNextDownload(n, -1) : update->damageNextDownload(-1, n);
console.printf("update: the next download will be %s at byte %ld\n", args.startsWith("damage cut") ? "cut" : "damaged", n);
if (args.startsWith("damage cut")) console.printf("update: each connection of the next download will break after %ld bytes\n", n);
else console.printf("update: the next download will be damaged at byte %ld\n", n);
} else if (args == "daily") { // forget today's check: the daily one runs again at once, if it may
update->forgetToday();
@@ -1609,6 +1613,14 @@ static void loopPass() {
bus.dispatch();
notifier->update(now);
apps->update(now);
// An App in use with no key pressed (a Game being played): the screen stays lit, and it is
// not the quiet moment an update waits for.
static uint32_t keptOnMs = 0;
if (apps->foreground().keepsScreenOn() && now - keptOnMs >= 1000) {
keptOnMs = now;
lastKeyMs = now;
power->onKey(now);
}
autoUpdateStep(now);
if (now - lastLog > 10000) {
+13 -3
View File
@@ -27,9 +27,20 @@ std::string whyNotConnected(NetworkClientSecure& tls, const std::string& host) {
} // namespace
std::string HttpsGet::open(const std::string& host, const std::string& path, const char* accept) {
std::string why = request(host, path, accept, "", kNeedFree);
if (!why.empty()) return why;
if (head_.status != 200) return host + " answered " + std::to_string(head_.status) + (head_.status / 100 == 3 ? " (a redirect)" : "");
return "";
}
std::string HttpsGet::openFrom(const std::string& host, const std::string& path, const char* accept, long from, const std::string& ifRange) {
return request(host, path, accept, "Range: bytes=" + std::to_string(from) + "-\r\n" + (ifRange.empty() ? "" : "If-Range: " + ifRange + "\r\n"), kNeedFreeToCarryOn);
}
std::string HttpsGet::request(const std::string& host, const std::string& path, const char* accept, const std::string& more, size_t needFree) {
close();
if (time(nullptr) < kClockSetAfter) return "The clock isn't set: can't check certificates";
if (esp_get_free_heap_size() < kNeedFree) return "Not enough memory for a secure connection";
if (esp_get_free_heap_size() < needFree) return "Not enough memory for a secure connection";
tls_.setCACert(kTrustedRootsPem);
tls_.setTimeout(15);
@@ -38,7 +49,7 @@ std::string HttpsGet::open(const std::string& host, const std::string& path, con
raw_.reset(new (std::nothrow) uint8_t[kRaw]);
if (!raw_) return "Not enough memory";
tls_.print(("GET " + path + " HTTP/1.1\r\nHost: " + host + "\r\nUser-Agent: roro9stack/" + versionString() +
"\r\nAccept: " + accept + "\r\nAccept-Encoding: identity\r\nConnection: close\r\n\r\n")
"\r\nAccept: " + accept + "\r\nAccept-Encoding: identity\r\n" + more + "Connection: close\r\n\r\n")
.c_str());
release::HttpHeadParser parser;
@@ -50,7 +61,6 @@ std::string HttpsGet::open(const std::string& host, const std::string& path, con
if (parser.complete() && used < static_cast<size_t>(n)) early_.assign(reinterpret_cast<const char*>(raw_.get()) + used, n - used);
}
head_ = parser.head();
if (head_.status != 200) return host + " answered " + std::to_string(head_.status) + (head_.status / 100 == 3 ? " (a redirect)" : "");
left_ = head_.chunked ? -1 : head_.contentLength;
return "";
}
+11
View File
@@ -20,12 +20,22 @@ class HttpsGet {
// checking the certificate); with Q86's 20 KB to spare, it starts with at least this much free.
static constexpr size_t kNeedFree = 80 * 1024;
// Carrying on a download (issue #54) happens in the middle of an install, whose own buffers
// (about 8 KB, measured: 78 KB free between two connections) are already taken out of what is
// free, and with the screen taken over nothing else will ask for memory: the peak and 12 KB.
static constexpr size_t kNeedFreeToCarryOn = 64 * 1024;
explicit HttpsGet(net::User user) : tls_(user) {}
~HttpsGet() { close(); }
// Connects and reads the head. "" when a 200 is on its way, or why not, in words for the screen.
std::string open(const std::string& host, const std::string& path, const char* accept);
// The same, for the rest of a file from byte `from` (issue #54): asks for that range, with
// `ifRange` (an ETag, or "") so that a file that changed comes whole. The answer, a 206 or
// not, is for the caller to judge with head(): "" here only says a head was read.
std::string openFrom(const std::string& host, const std::string& path, const char* accept, long from, const std::string& ifRange);
long contentLength() const { return head_.contentLength; } // -1: not known
const release::HttpHead& head() const { return head_; }
// Body bytes into `buf`: how many, 0 at the end, -1 when the connection broke or stalled
// before the end.
@@ -37,6 +47,7 @@ class HttpsGet {
static constexpr uint32_t kStallMs = 10000;
int readRaw(uint8_t* into, size_t len); // from the connection, waiting up to kStallMs
std::string request(const std::string& host, const std::string& path, const char* accept, const std::string& more, size_t needFree);
Counted<NetworkClientSecure> tls_;
release::HttpHead head_;
+51 -7
View File
@@ -10,6 +10,7 @@
#include <ctime>
#include "http_head.h"
#include "resume.h"
#include "platform/https_get.h"
#include "platform/ota_device.h"
#include "platform/system_info.h"
@@ -64,21 +65,61 @@ class FileSource : public UpdateSource {
File& f_;
};
// A release being downloaded from Gitea: the same bytes a push or a card would give.
// A release being downloaded from Gitea: the same bytes a push or a card would give. When the
// connection breaks, it is opened again for the rest of the file (issue #54): what was written
// to the slot stays, and the parser never sees the break.
class GiteaSource : public UpdateSource {
public:
GiteaSource(HttpsGet& get, long cutAfter, long flipAt) : get_(get), cut_(cutAfter), flip_(flipAt) {}
static constexpr uint32_t kWifiWaitMs = 60000; // for Wi-Fi to come back, before each try
static constexpr uint32_t kPauseMs = 2000;
GiteaSource(HttpsGet& get, const release::Url& url, WifiService& wifi, long cutAfter, long flipAt)
: get_(get), url_(url), wifi_(wifi), total_(get.contentLength()), etag_(get.head().etag), cut_(cutAfter), flip_(flipAt) {}
int read(uint8_t* buf, size_t len) override {
if (cut_ >= 0 && pos_ >= cut_) return -1; // Debug Builds: the connection "breaks" here
int n = get_.read(buf, len);
for (;;) {
// Debug: each connection "breaks" after cut_ bytes.
int n = cut_ >= 0 && pos_ - openedAt_ >= cut_ ? -1 : get_.read(buf, len);
if (n > 0 && flip_ >= pos_ && flip_ < pos_ + n) buf[flip_ - pos_] ^= 1; // and a byte "arrives wrong"
if (n > 0) pos_ += n;
return n;
if (n >= 0) return n;
if (!resume()) return -1;
}
}
int resumes() const { return policy_.resumes(); }
const std::string& why() const { return why_; } // why it gave up, when it did
private:
bool resume() {
// Without a length there is no knowing what "the rest" is.
while (total_ > 0 && pos_ < total_ && policy_.again(pos_)) {
get_.close();
delay(kPauseMs);
uint32_t since = millis();
while (wifi_.state() != WifiController::State::Connected && millis() - since < kWifiWaitMs) delay(250);
if (wifi_.state() != WifiController::State::Connected) {
why_ = "Wi-Fi did not come back";
continue;
}
why_ = get_.openFrom(url_.host, url_.path, "application/octet-stream", pos_, release::ifRangeFor(etag_));
if (!why_.empty()) continue; // no connection yet: another try, while the policy allows
why_ = release::resumeRefusal(get_.head(), pos_, total_, etag_);
if (!why_.empty()) return false; // it answered, and it is not the rest of this file
openedAt_ = pos_;
ESP_LOGW("update", "carrying on from byte %ld of %ld (try %d)", pos_, total_, policy_.resumes());
return true;
}
if (why_.empty()) why_ = "The download broke";
return false;
}
HttpsGet& get_;
long cut_, flip_, pos_ = 0;
release::Url url_;
WifiService& wifi_;
long total_;
std::string etag_, why_;
release::ResumePolicy policy_;
long cut_, flip_, pos_ = 0, openedAt_ = 0;
};
constexpr time_t kClockSetAfter = 1700000000; // anything earlier is the clock's default
@@ -363,9 +404,12 @@ void UpdateService::installFromGitea(const release::Release& r) {
notify("Update refused: " + why, NotificationLevel::Warning);
return;
}
GiteaSource source(get, damageCut_, damageFlip_);
GiteaSource source(get, url, wifi_, damageCut_, damageFlip_);
damageCut_ = damageFlip_ = -1;
install(source, "Gitea");
resumes_ = source.resumes();
// What the parser says after a break is only that the file was short: say why it broke.
if (phase_ != Phase::Installed && !source.why().empty()) notify("Download gave up: " + source.why(), NotificationLevel::Warning);
}
void UpdateService::taskEntry(void* self) { static_cast<UpdateService*>(self)->listen(); }
+2
View File
@@ -71,6 +71,7 @@ class UpdateService : public Service {
// (is its certificate accepted?), and a download cut short or with one byte flipped.
void requestProbe(const std::string& host, const std::string& path) { probeHost_ = host; probePath_ = path; probeResult_ = "..."; request_ = Request::Probe; }
std::string probeResult() const { return probeResult_; }
int lastResumes() const { return resumes_; } // how often the last download from Gitea was carried on (issue #54)
void damageNextDownload(long cutAfter, long flipAt) { damageCut_ = cutAfter; damageFlip_ = flipAt; }
void forgetToday() { store_.putInt("rel_day", 0); nextCheckMs_ = 0; announced_.clear(); } // the daily check runs at the next tick
@@ -115,6 +116,7 @@ class UpdateService : public Service {
uint32_t nextCheckMs_ = 90000; // not before the device has settled
std::string probeHost_, probePath_;
volatile long damageCut_ = -1, damageFlip_ = -1;
volatile int resumes_ = 0;
std::string probeResult_;
};
+245
View File
@@ -3,6 +3,76 @@
namespace roro::icons {
const uint8_t asteroids[128] = {
0x00, 0x00, 0x00, 0x00, // ................................
0x00, 0x00, 0x00, 0x00, // ................................
0x00, 0x00, 0x3f, 0xc0, // ..................########......
0x00, 0x00, 0x3f, 0xc0, // ..................########......
0x00, 0x00, 0xc0, 0x30, // ................##........##....
0x00, 0x00, 0xc0, 0x30, // ................##........##....
0x00, 0x03, 0x00, 0x0c, // ..............##............##..
0x00, 0x03, 0x00, 0x0c, // ..............##............##..
0x00, 0x03, 0x00, 0x30, // ..............##..........##....
0x00, 0x03, 0x00, 0x30, // ..............##..........##....
0x00, 0x00, 0xc0, 0x0c, // ................##..........##..
0x00, 0x00, 0xc0, 0x0c, // ................##..........##..
0x00, 0x00, 0x3f, 0xf0, // ..................##########....
0x00, 0x00, 0x3f, 0xf0, // ..................##########....
0x00, 0x00, 0x00, 0x00, // ................................
0x00, 0x00, 0x00, 0x00, // ................................
0x03, 0x00, 0x00, 0x00, // ......##........................
0x03, 0x00, 0x00, 0x00, // ......##........................
0x03, 0x00, 0x03, 0x00, // ......##..............##........
0x03, 0x00, 0x03, 0x00, // ......##..............##........
0x0c, 0xc0, 0x00, 0x00, // ....##..##......................
0x0c, 0xc0, 0x00, 0x00, // ....##..##......................
0x0c, 0xc0, 0x0c, 0x00, // ....##..##..........##..........
0x0c, 0xc0, 0x0c, 0x00, // ....##..##..........##..........
0x30, 0x30, 0x00, 0x00, // ..##......##....................
0x30, 0x30, 0x00, 0x00, // ..##......##....................
0x33, 0x30, 0x00, 0x00, // ..##..##..##....................
0x33, 0x30, 0x00, 0x00, // ..##..##..##....................
0x3c, 0xf0, 0x00, 0x00, // ..####..####....................
0x3c, 0xf0, 0x00, 0x00, // ..####..####....................
0x00, 0x00, 0x00, 0x00, // ................................
0x00, 0x00, 0x00, 0x00, // ................................
};
const uint8_t games[128] = {
0x00, 0x00, 0x00, 0x00, // ................................
0x00, 0x00, 0x00, 0x00, // ................................
0x00, 0x00, 0x00, 0x00, // ................................
0x00, 0x00, 0x00, 0x00, // ................................
0x00, 0x00, 0x00, 0x00, // ................................
0x00, 0x00, 0x00, 0x00, // ................................
0x03, 0xff, 0xff, 0xc0, // ......####################......
0x03, 0xff, 0xff, 0xc0, // ......####################......
0x0c, 0x00, 0x00, 0x30, // ....##....................##....
0x0c, 0x00, 0x00, 0x30, // ....##....................##....
0x30, 0x00, 0x00, 0x0c, // ..##........................##..
0x30, 0x00, 0x00, 0x0c, // ..##........................##..
0x30, 0xc0, 0x03, 0x0c, // ..##....##............##....##..
0x30, 0xc0, 0x03, 0x0c, // ..##....##............##....##..
0x33, 0xf0, 0x0c, 0xcc, // ..##..######........##..##..##..
0x33, 0xf0, 0x0c, 0xcc, // ..##..######........##..##..##..
0x30, 0xc0, 0x03, 0x0c, // ..##....##............##....##..
0x30, 0xc0, 0x03, 0x0c, // ..##....##............##....##..
0x30, 0x00, 0x00, 0x0c, // ..##........................##..
0x30, 0x00, 0x00, 0x0c, // ..##........................##..
0x30, 0x0f, 0xf0, 0x0c, // ..##........########........##..
0x30, 0x0f, 0xf0, 0x0c, // ..##........########........##..
0x30, 0x30, 0x0c, 0x0c, // ..##......##........##......##..
0x30, 0x30, 0x0c, 0x0c, // ..##......##........##......##..
0x0f, 0xc0, 0x03, 0xf0, // ....######............######....
0x0f, 0xc0, 0x03, 0xf0, // ....######............######....
0x00, 0x00, 0x00, 0x00, // ................................
0x00, 0x00, 0x00, 0x00, // ................................
0x00, 0x00, 0x00, 0x00, // ................................
0x00, 0x00, 0x00, 0x00, // ................................
0x00, 0x00, 0x00, 0x00, // ................................
0x00, 0x00, 0x00, 0x00, // ................................
};
const uint8_t gemini[128] = {
0x00, 0x00, 0x00, 0x00, // ................................
0x00, 0x00, 0x00, 0x00, // ................................
@@ -143,6 +213,41 @@ const uint8_t lora[128] = {
0x00, 0x00, 0x00, 0x00, // ................................
};
const uint8_t mines[128] = {
0x00, 0x00, 0x00, 0x00, // ................................
0x00, 0x00, 0x00, 0x00, // ................................
0x00, 0x03, 0xc0, 0x00, // ..............####..............
0x00, 0x03, 0xc0, 0x00, // ..............####..............
0x0c, 0x03, 0xc0, 0x30, // ....##........####........##....
0x0c, 0x03, 0xc0, 0x30, // ....##........####........##....
0x03, 0x3f, 0xfc, 0xc0, // ......##..############..##......
0x03, 0x3f, 0xfc, 0xc0, // ......##..############..##......
0x00, 0xff, 0xff, 0x00, // ........################........
0x00, 0xff, 0xff, 0x00, // ........################........
0x03, 0xf0, 0xff, 0xc0, // ......######....##########......
0x03, 0xf0, 0xff, 0xc0, // ......######....##########......
0x03, 0xc0, 0xff, 0xc0, // ......####......##########......
0x03, 0xc0, 0xff, 0xc0, // ......####......##########......
0x3f, 0xff, 0xff, 0xfc, // ..############################..
0x3f, 0xff, 0xff, 0xfc, // ..############################..
0x3f, 0xff, 0xff, 0xfc, // ..############################..
0x3f, 0xff, 0xff, 0xfc, // ..############################..
0x03, 0xff, 0xff, 0xc0, // ......####################......
0x03, 0xff, 0xff, 0xc0, // ......####################......
0x03, 0xff, 0xff, 0xc0, // ......####################......
0x03, 0xff, 0xff, 0xc0, // ......####################......
0x00, 0xff, 0xff, 0x00, // ........################........
0x00, 0xff, 0xff, 0x00, // ........################........
0x03, 0x3f, 0xfc, 0xc0, // ......##..############..##......
0x03, 0x3f, 0xfc, 0xc0, // ......##..############..##......
0x0c, 0x03, 0xc0, 0x30, // ....##........####........##....
0x0c, 0x03, 0xc0, 0x30, // ....##........####........##....
0x00, 0x03, 0xc0, 0x00, // ..............####..............
0x00, 0x03, 0xc0, 0x00, // ..............####..............
0x00, 0x00, 0x00, 0x00, // ................................
0x00, 0x00, 0x00, 0x00, // ................................
};
const uint8_t notes[128] = {
0x00, 0x00, 0x00, 0x00, // ................................
0x00, 0x00, 0x00, 0x00, // ................................
@@ -178,6 +283,41 @@ const uint8_t notes[128] = {
0x00, 0x00, 0x00, 0x00, // ................................
};
const uint8_t pong[128] = {
0x00, 0x00, 0x00, 0x00, // ................................
0x00, 0x00, 0x00, 0x00, // ................................
0x00, 0x00, 0x00, 0x00, // ................................
0x00, 0x00, 0x00, 0x00, // ................................
0x00, 0x03, 0x00, 0x00, // ..............##................
0x00, 0x03, 0x00, 0x00, // ..............##................
0x3c, 0x00, 0x00, 0x00, // ..####..........................
0x3c, 0x00, 0x00, 0x00, // ..####..........................
0x3c, 0x03, 0x00, 0x00, // ..####........##................
0x3c, 0x03, 0x00, 0x00, // ..####........##................
0x3c, 0x00, 0x00, 0x00, // ..####..........................
0x3c, 0x00, 0x00, 0x00, // ..####..........................
0x3c, 0x03, 0x0f, 0x00, // ..####........##....####........
0x3c, 0x03, 0x0f, 0x00, // ..####........##....####........
0x3c, 0x00, 0x0f, 0x0f, // ..####..............####....####
0x3c, 0x00, 0x0f, 0x0f, // ..####..............####....####
0x00, 0x03, 0x00, 0x0f, // ..............##............####
0x00, 0x03, 0x00, 0x0f, // ..............##............####
0x00, 0x00, 0x00, 0x0f, // ............................####
0x00, 0x00, 0x00, 0x0f, // ............................####
0x00, 0x03, 0x00, 0x0f, // ..............##............####
0x00, 0x03, 0x00, 0x0f, // ..............##............####
0x00, 0x00, 0x00, 0x0f, // ............................####
0x00, 0x00, 0x00, 0x0f, // ............................####
0x00, 0x03, 0x00, 0x00, // ..............##................
0x00, 0x03, 0x00, 0x00, // ..............##................
0x00, 0x00, 0x00, 0x00, // ................................
0x00, 0x00, 0x00, 0x00, // ................................
0x00, 0x00, 0x00, 0x00, // ................................
0x00, 0x00, 0x00, 0x00, // ................................
0x00, 0x00, 0x00, 0x00, // ................................
0x00, 0x00, 0x00, 0x00, // ................................
};
const uint8_t settings[128] = {
0x00, 0x00, 0x00, 0x00, // ................................
0x00, 0x00, 0x00, 0x00, // ................................
@@ -248,6 +388,41 @@ const uint8_t shell[128] = {
0x00, 0x00, 0x00, 0x00, // ................................
};
const uint8_t snake[128] = {
0x00, 0x00, 0x00, 0x00, // ................................
0x00, 0x00, 0x00, 0x00, // ................................
0x00, 0x00, 0x0f, 0xf0, // ....................########....
0x00, 0x00, 0x0f, 0xf0, // ....................########....
0x00, 0x00, 0x0c, 0xf0, // ....................##..####....
0x00, 0x00, 0x0c, 0xf0, // ....................##..####....
0x00, 0x00, 0x0f, 0xf0, // ....................########....
0x00, 0x00, 0x0f, 0xf0, // ....................########....
0x00, 0x00, 0x03, 0xc0, // ......................####......
0x00, 0x00, 0x03, 0xc0, // ......................####......
0x03, 0xff, 0xff, 0x00, // ......##################........
0x03, 0xff, 0xff, 0x00, // ......##################........
0x03, 0xc0, 0x00, 0x00, // ......####......................
0x03, 0xc0, 0x00, 0x00, // ......####......................
0x03, 0xc0, 0x00, 0x00, // ......####......................
0x03, 0xc0, 0x00, 0x00, // ......####......................
0x03, 0xff, 0xff, 0x00, // ......##################........
0x03, 0xff, 0xff, 0x00, // ......##################........
0x00, 0x00, 0x0f, 0x00, // ....................####........
0x00, 0x00, 0x0f, 0x00, // ....................####........
0x00, 0x00, 0x0f, 0x00, // ....................####........
0x00, 0x00, 0x0f, 0x00, // ....................####........
0x3f, 0xff, 0xfc, 0x00, // ..####################..........
0x3f, 0xff, 0xfc, 0x00, // ..####################..........
0x3c, 0x00, 0x00, 0x00, // ..####..........................
0x3c, 0x00, 0x00, 0x00, // ..####..........................
0x00, 0x00, 0x00, 0x00, // ................................
0x00, 0x00, 0x00, 0x00, // ................................
0x00, 0x00, 0x00, 0x00, // ................................
0x00, 0x00, 0x00, 0x00, // ................................
0x00, 0x00, 0x00, 0x00, // ................................
0x00, 0x00, 0x00, 0x00, // ................................
};
const uint8_t ssh[128] = {
0x00, 0x00, 0x00, 0x00, // ................................
0x00, 0x00, 0x00, 0x00, // ................................
@@ -353,6 +528,76 @@ const uint8_t system[128] = {
0x00, 0x00, 0x00, 0x00, // ................................
};
const uint8_t tetris[128] = {
0x00, 0x00, 0x00, 0x00, // ................................
0x00, 0x00, 0x00, 0x00, // ................................
0x00, 0x00, 0x00, 0x00, // ................................
0x00, 0x00, 0x00, 0x00, // ................................
0x0f, 0xcf, 0xcf, 0xc0, // ....######..######..######......
0x0f, 0xcf, 0xcf, 0xc0, // ....######..######..######......
0x0f, 0xcf, 0xcf, 0xc0, // ....######..######..######......
0x0f, 0xcf, 0xcf, 0xc0, // ....######..######..######......
0x0f, 0xcf, 0xcf, 0xc0, // ....######..######..######......
0x0f, 0xcf, 0xcf, 0xc0, // ....######..######..######......
0x00, 0x00, 0x00, 0x00, // ................................
0x00, 0x00, 0x00, 0x00, // ................................
0x00, 0x0f, 0xc0, 0x00, // ............######..............
0x00, 0x0f, 0xc0, 0x00, // ............######..............
0x00, 0x0f, 0xc0, 0x00, // ............######..............
0x00, 0x0f, 0xc0, 0x00, // ............######..............
0x00, 0x0f, 0xc0, 0x00, // ............######..............
0x00, 0x0f, 0xc0, 0x00, // ............######..............
0x00, 0x00, 0x00, 0x00, // ................................
0x00, 0x00, 0x00, 0x00, // ................................
0x0f, 0xc0, 0x0f, 0xc0, // ....######..........######......
0x0f, 0xc0, 0x0f, 0xc0, // ....######..........######......
0x0f, 0xc0, 0x0f, 0xc0, // ....######..........######......
0x0f, 0xc0, 0x0f, 0xc0, // ....######..........######......
0x0f, 0xc0, 0x0f, 0xc0, // ....######..........######......
0x0f, 0xc0, 0x0f, 0xc0, // ....######..........######......
0x00, 0x00, 0x00, 0x00, // ................................
0x00, 0x00, 0x00, 0x00, // ................................
0x3f, 0xff, 0xff, 0xf0, // ..##########################....
0x3f, 0xff, 0xff, 0xf0, // ..##########################....
0x00, 0x00, 0x00, 0x00, // ................................
0x00, 0x00, 0x00, 0x00, // ................................
};
const uint8_t twenty48[128] = {
0x00, 0x00, 0x00, 0x00, // ................................
0x00, 0x00, 0x00, 0x00, // ................................
0x3f, 0xfc, 0xff, 0xf0, // ..############..############....
0x3f, 0xfc, 0xff, 0xf0, // ..############..############....
0x30, 0x0c, 0xff, 0xf0, // ..##........##..############....
0x30, 0x0c, 0xff, 0xf0, // ..##........##..############....
0x30, 0x0c, 0xff, 0xf0, // ..##........##..############....
0x30, 0x0c, 0xff, 0xf0, // ..##........##..############....
0x30, 0x0c, 0xff, 0xf0, // ..##........##..############....
0x30, 0x0c, 0xff, 0xf0, // ..##........##..############....
0x30, 0x0c, 0xff, 0xf0, // ..##........##..############....
0x30, 0x0c, 0xff, 0xf0, // ..##........##..############....
0x3f, 0xfc, 0xff, 0xf0, // ..############..############....
0x3f, 0xfc, 0xff, 0xf0, // ..############..############....
0x00, 0x00, 0x00, 0x00, // ................................
0x00, 0x00, 0x00, 0x00, // ................................
0x3f, 0xfc, 0xff, 0xf0, // ..############..############....
0x3f, 0xfc, 0xff, 0xf0, // ..############..############....
0x3f, 0xfc, 0xc0, 0x30, // ..############..##........##....
0x3f, 0xfc, 0xc0, 0x30, // ..############..##........##....
0x3f, 0xfc, 0xc0, 0x30, // ..############..##........##....
0x3f, 0xfc, 0xc0, 0x30, // ..############..##........##....
0x3f, 0xfc, 0xc0, 0x30, // ..############..##........##....
0x3f, 0xfc, 0xc0, 0x30, // ..############..##........##....
0x3f, 0xfc, 0xc0, 0x30, // ..############..##........##....
0x3f, 0xfc, 0xc0, 0x30, // ..############..##........##....
0x3f, 0xfc, 0xff, 0xf0, // ..############..############....
0x3f, 0xfc, 0xff, 0xf0, // ..############..############....
0x00, 0x00, 0x00, 0x00, // ................................
0x00, 0x00, 0x00, 0x00, // ................................
0x00, 0x00, 0x00, 0x00, // ................................
0x00, 0x00, 0x00, 0x00, // ................................
};
const uint8_t wifi_tools[128] = {
0x00, 0x00, 0x00, 0x00, // ................................
0x00, 0x00, 0x00, 0x00, // ................................
+7
View File
@@ -8,16 +8,23 @@ namespace roro::icons {
// One bit a pixel, the leftmost first, a row after another: what Canvas::drawBitmap takes.
constexpr int kSize = 32;
extern const uint8_t asteroids[128]; // asteroids.png
extern const uint8_t games[128]; // games.png
extern const uint8_t gemini[128]; // gemini.png
extern const uint8_t gnss[128]; // gnss.png
extern const uint8_t irc[128]; // irc.png
extern const uint8_t lora[128]; // lora.png
extern const uint8_t mines[128]; // mines.png
extern const uint8_t notes[128]; // notes.png
extern const uint8_t pong[128]; // pong.png
extern const uint8_t settings[128]; // settings.png
extern const uint8_t shell[128]; // shell.png
extern const uint8_t snake[128]; // snake.png
extern const uint8_t ssh[128]; // ssh.png
extern const uint8_t storage[128]; // storage.png
extern const uint8_t system[128]; // system.png
extern const uint8_t tetris[128]; // tetris.png
extern const uint8_t twenty48[128]; // twenty48.png
extern const uint8_t wifi_tools[128]; // wifi-tools.png
} // namespace roro::icons
+719
View File
@@ -0,0 +1,719 @@
#include <unity.h>
#include <cmath>
#include <set>
#include "pong.h"
#include "snake.h"
#include "tetris.h"
#include "tune.h"
using namespace roro::games;
void setUp() {}
void tearDown() {}
// ---- Pong ----
namespace {
// Plays until the phase or an event says so; the player follows the ball if `follow`.
uint8_t playPong(Pong& p, bool follow, uint8_t until, int maxSteps = 200000) {
for (int i = 0; i < maxSteps; i++) {
int move = !follow ? 0 : p.ballY() < p.playerY() - 2 ? -1 : p.ballY() > p.playerY() + 2 ? 1 : 0;
uint8_t e = p.step(0.01f, move);
if (e & until) return e;
}
return 0;
}
} // namespace
void test_pong_starts_with_a_serve_from_the_middle() {
Pong p(1);
TEST_ASSERT_TRUE(p.phase() == Pong::Phase::Serve);
TEST_ASSERT_FLOAT_WITHIN(0.01, Pong::kWidth / 2, p.ballX());
p.step(Pong::kServePause / 2, 0);
TEST_ASSERT_FLOAT_WITHIN(0.01, Pong::kWidth / 2, p.ballX()); // it waits
for (int i = 0; i < 100; i++) p.step(0.01f, 0);
TEST_ASSERT_TRUE(p.phase() == Pong::Phase::Play);
p.step(0.1f, 0);
TEST_ASSERT_TRUE(std::fabs(p.ballX() - Pong::kWidth / 2) > 5);
}
void test_pong_the_same_seed_is_the_same_game() {
Pong a(42), b(42), c(43);
for (int i = 0; i < 500; i++) {
a.step(0.01f, i % 3 - 1);
b.step(0.01f, i % 3 - 1);
c.step(0.01f, i % 3 - 1);
}
TEST_ASSERT_EQUAL_FLOAT(a.ballX(), b.ballX());
TEST_ASSERT_EQUAL_FLOAT(a.ballY(), b.ballY());
TEST_ASSERT_TRUE(a.ballY() != c.ballY() || a.ballX() != c.ballX());
}
void test_pong_the_ball_stays_between_the_walls() {
Pong p(7);
bool wall = false;
for (int i = 0; i < 20000; i++) {
uint8_t e = p.step(0.01f, p.ballY() < p.playerY() ? -1 : 1);
wall |= (e & Pong::Wall) != 0;
TEST_ASSERT_TRUE(p.ballY() >= 0 && p.ballY() <= Pong::kHeight);
if (p.phase() == Pong::Phase::Over) p.start();
}
TEST_ASSERT_TRUE(wall);
}
void test_pong_a_paddle_stays_on_the_field() {
Pong p(3);
for (int i = 0; i < 300; i++) p.step(0.01f, -1);
TEST_ASSERT_FLOAT_WITHIN(0.01, Pong::kPaddleH / 2, p.playerY());
for (int i = 0; i < 300; i++) p.step(0.01f, 1);
TEST_ASSERT_FLOAT_WITHIN(0.01, Pong::kHeight - Pong::kPaddleH / 2, p.playerY());
}
void test_pong_a_return_counts_and_speeds_the_ball_up() {
Pong p(5);
float served = Pong::kServeSpeed;
// Follow the ball until the player returns it once.
int before = p.rally();
for (int i = 0; i < 100000 && p.rally() == before; i++) {
int move = p.ballY() < p.playerY() - 2 ? -1 : p.ballY() > p.playerY() + 2 ? 1 : 0;
p.step(0.01f, move);
}
TEST_ASSERT_EQUAL(1, p.rally());
TEST_ASSERT_EQUAL(1, p.longestRally());
TEST_ASSERT_TRUE(p.speed() > served);
TEST_ASSERT_TRUE(p.speed() <= Pong::kMaxSpeed);
}
void test_pong_a_miss_is_a_point_for_the_other_side() {
Pong p(9);
// The player never moves from the top corner: sooner or later the ball gets past.
for (int i = 0; i < 300; i++) p.step(0.01f, -1);
while (p.deviceScore() == 0 && p.phase() != Pong::Phase::Over) p.step(0.01f, -1);
TEST_ASSERT_EQUAL(1, p.deviceScore());
TEST_ASSERT_TRUE(p.phase() == Pong::Phase::Serve); // served again, from the middle
TEST_ASSERT_FLOAT_WITHIN(0.01, Pong::kWidth / 2, p.ballX());
TEST_ASSERT_EQUAL(0, p.rally());
}
void test_pong_ends_at_seven() {
Pong p(11);
uint8_t e = playPong(p, false, Pong::Over); // a player who does nothing loses
TEST_ASSERT_TRUE(e & Pong::Over);
TEST_ASSERT_TRUE(p.phase() == Pong::Phase::Over);
TEST_ASSERT_EQUAL(Pong::kWinScore, std::max(p.playerScore(), p.deviceScore()));
TEST_ASSERT_EQUAL(0, p.step(1.0f, 1)); // nothing moves once it is over
p.start();
TEST_ASSERT_EQUAL(0, p.deviceScore());
TEST_ASSERT_TRUE(p.phase() == Pong::Phase::Serve);
}
void test_pong_the_device_can_be_beaten() {
// A player who always meets the ball: the device, slower than a steep fast ball, must lose
// points over a few games.
int playerPoints = 0;
for (uint32_t seed = 1; seed <= 5; seed++) {
Pong p(seed);
playPong(p, true, Pong::Over, 2000000);
playerPoints += p.playerScore();
}
TEST_ASSERT_TRUE(playerPoints > 0);
}
void test_pong_a_tilt_puts_the_paddle_where_it_points() {
TEST_ASSERT_FLOAT_WITHIN(0.01, Pong::kHeight / 2, tiltTarget(0.3f, 0.3f)); // held as at the start: the middle
TEST_ASSERT_TRUE(tiltTarget(0.2f, 0.3f) < Pong::kHeight / 2); // top lowered: up
TEST_ASSERT_TRUE(tiltTarget(0.4f, 0.3f) > Pong::kHeight / 2);
TEST_ASSERT_FLOAT_WITHIN(0.01, Pong::kPaddleH / 2, tiltTarget(-0.9f, 0.3f)); // and no further than the edge
TEST_ASSERT_FLOAT_WITHIN(0.01, Pong::kHeight - Pong::kPaddleH / 2, tiltTarget(0.9f, 0.3f));
TEST_ASSERT_EQUAL_FLOAT(1, moveToward(20, 80));
TEST_ASSERT_EQUAL_FLOAT(-1, moveToward(80, 20));
TEST_ASSERT_FLOAT_WITHIN(0.01, 0.5f, moveToward(60, 65));
TEST_ASSERT_EQUAL_FLOAT(0, moveToward(60, 60));
}
void test_pong_a_tilted_paddle_settles_on_its_target() {
Pong p(3);
float target = tiltTarget(0.4f, 0.3f);
for (int i = 0; i < 200; i++) p.step(0.01f, moveToward(p.playerY(), target));
TEST_ASSERT_FLOAT_WITHIN(0.5, target, p.playerY());
}
void test_pong_the_device_paddle_has_a_top_speed() {
Pong p(13);
float before = p.deviceY();
for (int i = 0; i < 1000; i++) {
p.step(0.01f, 0);
TEST_ASSERT_TRUE(std::fabs(p.deviceY() - before) <= Pong::kDeviceSpeed * 0.01f + 0.01f);
before = p.deviceY();
if (p.phase() == Pong::Phase::Over) p.start();
}
}
// ---- Snake ----
namespace {
void moves(Snake& s, int n) {
for (int i = 0; i < n && s.alive(); i++) s.step(s.intervalMs());
}
} // namespace
void test_snake_starts_in_the_middle_three_long() {
Snake s(1);
TEST_ASSERT_TRUE(s.alive());
TEST_ASSERT_EQUAL(3, s.body().size());
TEST_ASSERT_EQUAL(Snake::kCols / 2, s.body().front().col);
TEST_ASSERT_EQUAL(Snake::kRows / 2, s.body().front().row);
TEST_ASSERT_TRUE(s.heading() == Snake::Dir::Right);
for (auto& c : s.body()) TEST_ASSERT_FALSE(c == s.food());
}
void test_snake_moves_at_its_pace() {
Snake s(1);
s.putFood({0, 0});
TEST_ASSERT_EQUAL(0, s.step(Snake::kSlowestMs - 1));
TEST_ASSERT_EQUAL(Snake::Moved, s.step(1));
TEST_ASSERT_EQUAL(Snake::kCols / 2 + 1, s.body().front().col);
TEST_ASSERT_EQUAL(3, s.body().size());
}
void test_snake_turns_but_not_back_on_itself() {
Snake s(1);
s.putFood({0, 0});
s.turn(Snake::Dir::Left); // straight back: not a turn
moves(s, 1);
TEST_ASSERT_TRUE(s.heading() == Snake::Dir::Right);
s.turn(Snake::Dir::Up);
moves(s, 1);
TEST_ASSERT_TRUE(s.heading() == Snake::Dir::Up);
TEST_ASSERT_EQUAL(Snake::kRows / 2 - 1, s.body().front().row);
}
void test_snake_remembers_a_quick_corner() {
Snake s(1);
s.putFood({0, 0});
s.turn(Snake::Dir::Up); // both before the next move
s.turn(Snake::Dir::Left); // would be "back" for a snake heading right, but not after the first
s.turn(Snake::Dir::Down); // a third is not remembered
moves(s, 1);
TEST_ASSERT_TRUE(s.heading() == Snake::Dir::Up);
moves(s, 1);
TEST_ASSERT_TRUE(s.heading() == Snake::Dir::Left);
moves(s, 1);
TEST_ASSERT_TRUE(s.heading() == Snake::Dir::Left);
TEST_ASSERT_TRUE(s.alive());
}
void test_snake_eats_grows_and_gets_new_food() {
Snake s(1);
Snake::Cell head = s.body().front();
s.putFood({static_cast<int8_t>(head.col + 1), head.row});
uint8_t e = s.step(s.intervalMs());
TEST_ASSERT_TRUE(e & Snake::Ate);
TEST_ASSERT_EQUAL(1, s.score());
TEST_ASSERT_EQUAL(4, s.body().size());
for (auto& c : s.body()) TEST_ASSERT_FALSE(c == s.food());
TEST_ASSERT_TRUE(s.food().col >= 0 && s.food().col < Snake::kCols && s.food().row >= 0 && s.food().row < Snake::kRows);
}
void test_snake_dies_at_a_wall() {
Snake s(1);
s.putFood({0, 0});
uint8_t last = 0;
for (int i = 0; i < Snake::kCols && s.alive(); i++) last = s.step(s.intervalMs());
TEST_ASSERT_FALSE(s.alive());
TEST_ASSERT_TRUE(last & Snake::Died);
TEST_ASSERT_EQUAL(Snake::kCols - 1, s.body().front().col); // it stopped at the edge
TEST_ASSERT_EQUAL(3, s.body().size()); // whole
TEST_ASSERT_EQUAL(0, s.step(1000));
}
void test_snake_dies_on_itself() {
Snake s(1);
// Fed four times in a row, it is long enough to bite itself in a tight turn.
for (int i = 0; i < 4; i++) {
Snake::Cell head = s.body().front();
s.putFood({static_cast<int8_t>(head.col + 1), head.row});
s.step(s.intervalMs());
}
s.putFood({0, 0});
TEST_ASSERT_EQUAL(7, s.body().size());
s.turn(Snake::Dir::Down);
moves(s, 1);
s.turn(Snake::Dir::Left);
moves(s, 1);
s.turn(Snake::Dir::Up);
moves(s, 1);
TEST_ASSERT_FALSE(s.alive());
}
void test_snake_may_follow_its_own_tail() {
Snake s(1);
Snake::Cell head = s.body().front();
s.putFood({static_cast<int8_t>(head.col + 1), head.row});
s.step(s.intervalMs()); // four long: a square of 2 x 2 is its own tail chased
s.putFood({0, 0});
for (auto d : {Snake::Dir::Down, Snake::Dir::Left, Snake::Dir::Up, Snake::Dir::Right, Snake::Dir::Down}) {
s.turn(d);
moves(s, 1);
}
TEST_ASSERT_TRUE(s.alive());
}
void test_snake_gets_faster_as_it_eats() {
Snake s(1);
uint32_t start = s.intervalMs();
TEST_ASSERT_EQUAL(Snake::kSlowestMs, start);
for (int i = 0; i < Snake::kFoodsAStep; i++) {
Snake::Cell head = s.body().front();
s.putFood({static_cast<int8_t>(head.col + 1), head.row});
s.step(s.intervalMs());
}
TEST_ASSERT_EQUAL(start - Snake::kFasterMs, s.intervalMs());
}
void test_snake_the_same_seed_puts_the_food_in_the_same_places() {
Snake a(77), b(77), c(78);
TEST_ASSERT_TRUE(a.food() == b.food());
bool differs = !(a.food() == c.food());
for (int i = 0; i < 3; i++) {
for (Snake* s : {&a, &b, &c}) {
Snake::Cell head = s->body().front();
s->putFood({static_cast<int8_t>(head.col + 1), head.row});
s->step(s->intervalMs());
}
TEST_ASSERT_TRUE(a.food() == b.food());
differs |= !(a.food() == c.food());
}
TEST_ASSERT_TRUE(differs);
}
// ---- Tetris ----
namespace {
const TetrisInput kNothing;
TetrisInput press(bool TetrisInput::*key) {
TetrisInput in;
in.*key = true;
return in;
}
int filled(const Tetris& t) {
int n = 0;
for (int r = 0; r < Tetris::kRows; r++)
for (int c = 0; c < Tetris::kCols; c++) n += t.cell(c, r) != 0;
return n;
}
// A row filled but for one column.
void fillRow(Tetris& t, int row, int gap) {
for (int c = 0; c < Tetris::kCols; c++)
if (c != gap) t.put(c, row, 7);
}
} // namespace
void test_tetris_deals_each_piece_once_in_seven() {
Tetris t(5);
for (int bag = 0; bag < 4; bag++) {
std::set<int> seen;
for (int i = 0; i < 7; i++) {
seen.insert(t.piece().kind);
t.step(0, press(&TetrisInput::hardDrop));
if (t.over()) t.start(), i = -1, seen.clear(); // start over on a full well
}
TEST_ASSERT_EQUAL(7, seen.size());
t.start();
}
}
void test_tetris_shows_the_piece_that_comes_next() {
Tetris t(9);
uint8_t next = t.next();
t.step(0, press(&TetrisInput::hardDrop));
TEST_ASSERT_EQUAL(next, t.piece().kind);
}
void test_tetris_a_piece_falls_at_the_levels_pace() {
Tetris t(1);
t.give(3); // a T
int row = t.piece().row;
TEST_ASSERT_EQUAL(0, t.step(Tetris::kSlowestMs - 1, kNothing));
TEST_ASSERT_EQUAL(row, t.piece().row);
TEST_ASSERT_EQUAL(Tetris::Moved, t.step(1, kNothing));
TEST_ASSERT_EQUAL(row + 1, t.piece().row);
}
void test_tetris_a_soft_drop_is_faster_and_scores() {
Tetris t(1);
t.give(3);
int row = t.piece().row;
t.step(Tetris::kSoftMs * 3, press(&TetrisInput::soft));
TEST_ASSERT_EQUAL(row + 3, t.piece().row);
TEST_ASSERT_EQUAL(3, t.score());
}
void test_tetris_a_held_soft_drop_stops_with_its_piece() {
Tetris t(1);
t.give(2);
TetrisInput soft = press(&TetrisInput::soft);
uint8_t all = 0;
for (int i = 0; i < 40 && !(all & Tetris::Locked); i++) all |= t.step(Tetris::kSoftMs, soft);
TEST_ASSERT_TRUE(all & Tetris::Locked);
// The key is still down: the next piece falls at the level's pace, not the fast one.
int row = t.piece().row, score = t.score();
t.step(Tetris::kSoftMs * 4, soft);
TEST_ASSERT_EQUAL(row, t.piece().row);
TEST_ASSERT_EQUAL(score, t.score());
t.step(0, kNothing); // let go, and pressed again: it works
t.step(Tetris::kSoftMs * 2, soft);
TEST_ASSERT_TRUE(t.piece().row > row);
}
void test_tetris_moves_stop_at_the_walls() {
Tetris t(1);
t.give(2); // an O: two wide
for (int i = 0; i < 20; i++) {
t.step(0, press(&TetrisInput::left));
t.step(0, kNothing); // released: the next press is a press
}
int leftmost = Tetris::kCols;
for (auto& c : Tetris::cells(t.piece())) leftmost = std::min<int>(leftmost, c.col);
TEST_ASSERT_EQUAL(0, leftmost);
for (int i = 0; i < 20; i++) {
t.step(0, press(&TetrisInput::right));
t.step(0, kNothing);
}
int rightmost = 0;
for (auto& c : Tetris::cells(t.piece())) rightmost = std::max<int>(rightmost, c.col);
TEST_ASSERT_EQUAL(Tetris::kCols - 1, rightmost);
}
void test_tetris_a_held_key_repeats() {
Tetris t(1);
t.give(2);
int col = t.piece().col;
TetrisInput left = press(&TetrisInput::left);
t.step(0, left); // the press: one step at once
TEST_ASSERT_EQUAL(col - 1, t.piece().col);
t.step(Tetris::kRepeatAfterMs - 1, left);
TEST_ASSERT_EQUAL(col - 1, t.piece().col); // not yet
t.step(1, left);
TEST_ASSERT_EQUAL(col - 2, t.piece().col);
t.step(Tetris::kRepeatEveryMs, left);
TEST_ASSERT_EQUAL(col - 3, t.piece().col);
TetrisInput both = left;
both.right = true; // both held: neither
t.step(Tetris::kRepeatEveryMs * 3, both);
TEST_ASSERT_EQUAL(col - 3, t.piece().col);
}
void test_tetris_turns_and_turns_back() {
Tetris t(1);
t.give(3);
auto before = Tetris::cells(t.piece());
TEST_ASSERT_EQUAL(Tetris::Turned, t.step(0, press(&TetrisInput::rotate)));
TEST_ASSERT_EQUAL(1, t.piece().turn);
t.step(0, press(&TetrisInput::rotateBack));
TEST_ASSERT_EQUAL(0, t.piece().turn);
auto after = Tetris::cells(t.piece());
for (int i = 0; i < 4; i++) {
TEST_ASSERT_EQUAL(before[i].col, after[i].col);
TEST_ASSERT_EQUAL(before[i].row, after[i].row);
}
for (int i = 0; i < 4; i++) t.step(0, press(&TetrisInput::rotate));
TEST_ASSERT_EQUAL(0, t.piece().turn); // four quarter turns: as it was
}
void test_tetris_a_turn_against_a_wall_shifts_the_piece() {
Tetris t(1);
t.give(1); // the long one
t.step(0, press(&TetrisInput::rotate)); // upright
for (int i = 0; i < 10; i++) {
t.step(0, press(&TetrisInput::right));
t.step(0, kNothing);
}
for (auto& c : Tetris::cells(t.piece())) TEST_ASSERT_EQUAL(Tetris::kCols - 1, c.col); // against the right wall
TEST_ASSERT_EQUAL(Tetris::Turned, t.step(0, press(&TetrisInput::rotate))); // flat again: it needs four columns
for (auto& c : Tetris::cells(t.piece())) TEST_ASSERT_TRUE(c.col >= Tetris::kCols - 4 && c.col < Tetris::kCols);
}
void test_tetris_a_turn_with_no_room_does_not_happen() {
Tetris t(1);
t.give(1);
t.step(0, press(&TetrisInput::rotate));
// Walled in on both sides, a column wide.
int col = Tetris::cells(t.piece())[0].col;
for (int r = 0; r < Tetris::kRows; r++)
for (int c = 0; c < Tetris::kCols; c++)
if (c != col) t.put(c, r, 7);
TEST_ASSERT_EQUAL(0, t.step(0, press(&TetrisInput::rotate)));
TEST_ASSERT_EQUAL(1, t.piece().turn);
}
void test_tetris_a_hard_drop_lands_locks_and_scores() {
Tetris t(1);
t.give(2); // an O
int from = t.piece().row;
uint8_t e = t.step(0, press(&TetrisInput::hardDrop));
TEST_ASSERT_TRUE(e & Tetris::Locked);
TEST_ASSERT_EQUAL(4, filled(t));
TEST_ASSERT_NOT_EQUAL(0, t.cell(4, Tetris::kRows - 1));
TEST_ASSERT_NOT_EQUAL(0, t.cell(5, Tetris::kRows - 2));
TEST_ASSERT_EQUAL(2 * (Tetris::kRows - 2 - from), t.score()); // two a row fallen
TEST_ASSERT_EQUAL(0, t.lastCleared());
}
void test_tetris_a_piece_locks_when_it_can_fall_no_more() {
Tetris t(1);
t.give(2);
uint8_t all = 0;
for (int i = 0; i < Tetris::kRows + 2 && !(all & Tetris::Locked); i++) all |= t.step(Tetris::kSlowestMs, kNothing);
TEST_ASSERT_TRUE(all & Tetris::Locked);
TEST_ASSERT_EQUAL(4, filled(t));
}
void test_tetris_a_full_row_goes_and_the_rest_comes_down() {
Tetris t(1);
fillRow(t, Tetris::kRows - 1, 4); // the gap where the O will not fit: use the long one upright
t.put(0, Tetris::kRows - 2, 5); // something above, to see it come down
t.give(1);
t.step(0, press(&TetrisInput::rotate)); // upright, in column 5
t.step(0, press(&TetrisInput::left)); // column 4
uint8_t e = t.step(0, press(&TetrisInput::hardDrop));
TEST_ASSERT_TRUE(e & Tetris::Lines);
TEST_ASSERT_EQUAL(1, t.lastCleared());
TEST_ASSERT_EQUAL(1, t.lines());
TEST_ASSERT_EQUAL(5, t.cell(0, Tetris::kRows - 1)); // what was above
TEST_ASSERT_EQUAL(1, t.cell(4, Tetris::kRows - 1)); // and the rest of the long piece
TEST_ASSERT_EQUAL(0, t.cell(1, Tetris::kRows - 1));
TEST_ASSERT_EQUAL(1 + 3, filled(t));
}
void test_tetris_four_rows_at_once_score_most() {
Tetris t(1);
for (int r = Tetris::kRows - 4; r < Tetris::kRows; r++) fillRow(t, r, 9);
t.give(1);
t.step(0, press(&TetrisInput::rotate));
for (int i = 0; i < 10; i++) {
t.step(0, press(&TetrisInput::right));
t.step(0, kNothing);
}
int before = t.score();
uint8_t e = t.step(0, press(&TetrisInput::hardDrop));
TEST_ASSERT_TRUE(e & Tetris::Lines);
TEST_ASSERT_EQUAL(4, t.lastCleared());
TEST_ASSERT_EQUAL(0, filled(t));
int dropped = (t.score() - before) - 800;
TEST_ASSERT_TRUE(dropped > 0 && dropped <= 2 * Tetris::kRows); // 800 for the four, and the drop
}
void test_tetris_ten_lines_are_a_level_and_a_faster_fall() {
Tetris t(1);
TEST_ASSERT_EQUAL(0, t.level());
TEST_ASSERT_EQUAL(Tetris::kSlowestMs, t.fallMs());
uint8_t all = 0;
for (int i = 0; i < 3; i++) { // 4 + 4 + 4
for (int r = Tetris::kRows - 4; r < Tetris::kRows; r++) fillRow(t, r, 9);
t.give(1);
t.step(0, press(&TetrisInput::rotate));
for (int k = 0; k < 10; k++) {
t.step(0, press(&TetrisInput::right));
t.step(0, kNothing);
}
all |= t.step(0, press(&TetrisInput::hardDrop));
}
TEST_ASSERT_EQUAL(12, t.lines());
TEST_ASSERT_EQUAL(1, t.level());
TEST_ASSERT_TRUE(all & Tetris::LevelUp);
TEST_ASSERT_EQUAL(Tetris::kSlowestMs * 85 / 100, t.fallMs());
}
void test_tetris_the_fall_never_gets_faster_than_its_limit() {
Tetris t(1);
for (int i = 0; i < 80; i++) {
for (int r = Tetris::kRows - 4; r < Tetris::kRows; r++) fillRow(t, r, 9);
t.give(1);
t.step(0, press(&TetrisInput::rotate));
for (int k = 0; k < 10; k++) {
t.step(0, press(&TetrisInput::right));
t.step(0, kNothing);
}
t.step(0, press(&TetrisInput::hardDrop));
}
TEST_ASSERT_TRUE(t.level() >= 30);
TEST_ASSERT_EQUAL(Tetris::kFastestMs, t.fallMs());
}
void test_tetris_is_over_when_a_piece_has_no_room() {
Tetris t(3);
uint8_t last = 0;
for (int i = 0; i < 40 && !t.over(); i++) last = t.step(0, press(&TetrisInput::hardDrop)); // all in the middle
TEST_ASSERT_TRUE(t.over());
TEST_ASSERT_TRUE(last & Tetris::Over);
TEST_ASSERT_EQUAL(0, t.step(1000, press(&TetrisInput::hardDrop)));
t.start();
TEST_ASSERT_FALSE(t.over());
TEST_ASSERT_EQUAL(0, filled(t));
TEST_ASSERT_EQUAL(0, t.score());
}
void test_tetris_the_same_seed_deals_the_same_pieces() {
Tetris a(21), b(21), c(22);
bool differs = false;
for (int i = 0; i < 14; i++) {
TEST_ASSERT_EQUAL(a.piece().kind, b.piece().kind);
differs |= a.piece().kind != c.piece().kind;
for (Tetris* t : {&a, &b, &c}) {
t->step(0, press(&TetrisInput::hardDrop));
if (t->over()) t->start();
}
}
TEST_ASSERT_TRUE(differs);
}
void test_tetris_every_piece_in_every_turn_has_four_cells_in_its_box() {
for (uint8_t kind = 1; kind <= 7; kind++)
for (uint8_t turn = 0; turn < 4; turn++) {
Tetris::Piece p{kind, turn, 0, 0};
std::set<int> where;
for (auto& c : Tetris::cells(p)) {
TEST_ASSERT_TRUE(c.col >= 0 && c.col < 4 && c.row >= 0 && c.row < 4);
where.insert(c.row * 4 + c.col);
}
TEST_ASSERT_EQUAL(4, where.size());
}
}
// ---- The tune ----
void test_a_tune_gives_its_notes_in_time() {
static const Note kThree[] = {{440, 2}, {0, 1}, {880, 1}};
TunePlayer p([](size_t i) { return kThree[i]; }, 3, 100);
Note n{};
uint32_t ms = 0;
TEST_ASSERT_TRUE(p.step(0, n, ms)); // the first note at once
TEST_ASSERT_EQUAL(440, n.hz);
TEST_ASSERT_EQUAL(200, ms);
TEST_ASSERT_FALSE(p.step(199, n, ms));
TEST_ASSERT_TRUE(p.step(1, n, ms)); // a rest is a note too: the caller plays nothing
TEST_ASSERT_EQUAL(0, n.hz);
TEST_ASSERT_EQUAL(100, ms);
TEST_ASSERT_TRUE(p.step(100, n, ms));
TEST_ASSERT_EQUAL(880, n.hz);
TEST_ASSERT_TRUE(p.step(100, n, ms)); // and round again
TEST_ASSERT_EQUAL(440, n.hz);
TEST_ASSERT_EQUAL(0, p.position());
}
void test_a_tune_keeps_time_over_uneven_steps() {
static const Note kTwo[] = {{440, 1}, {880, 1}};
TunePlayer p([](size_t i) { return kTwo[i]; }, 2, 100);
Note n{};
uint32_t ms = 0;
p.step(0, n, ms);
int notes = 1;
for (int i = 0; i < 300; i++) notes += p.step(33, n, ms); // 9.9 seconds in steps of 33 ms
TEST_ASSERT_EQUAL(100, notes); // one every 100 ms: the 33s do not add up to a drift
}
void test_a_tune_skips_what_a_long_gap_missed() {
static const Note kFour[] = {{100, 1}, {200, 1}, {300, 1}, {400, 1}};
TunePlayer p([](size_t i) { return kFour[i]; }, 4, 100);
Note n{};
uint32_t ms = 0;
p.step(0, n, ms);
TEST_ASSERT_TRUE(p.step(250, n, ms)); // 200 and half of 300 went by
TEST_ASSERT_EQUAL(300, n.hz);
TEST_ASSERT_FALSE(p.step(49, n, ms));
TEST_ASSERT_TRUE(p.step(1, n, ms));
TEST_ASSERT_EQUAL(400, n.hz);
p.restart();
TEST_ASSERT_TRUE(p.step(0, n, ms));
TEST_ASSERT_EQUAL(100, n.hz);
}
void test_korobeiniki_is_whole_bars() {
// Each part is eight bars of eight eighths, and it begins on its famous E.
int first = 0, second = 0;
for (size_t i = 0; i < korobeiniki::kFirstCount; i++) first += korobeiniki::kFirst[i].eighths;
for (size_t i = 0; i < korobeiniki::kSecondCount; i++) second += korobeiniki::kSecond[i].eighths;
TEST_ASSERT_EQUAL(64, first);
TEST_ASSERT_EQUAL(64, second);
TEST_ASSERT_EQUAL(659, korobeiniki::at(0).hz);
TEST_ASSERT_EQUAL(659, korobeiniki::at(korobeiniki::kFirstCount).hz); // the tune again
TEST_ASSERT_EQUAL(659, korobeiniki::at(2 * korobeiniki::kFirstCount).hz); // then the slow part
TEST_ASSERT_EQUAL(4, korobeiniki::at(2 * korobeiniki::kFirstCount).eighths);
TEST_ASSERT_EQUAL(0, korobeiniki::at(korobeiniki::kCount - 1).hz); // and a breath before it starts over
}
void test_the_tune_hurries_with_the_level() {
TEST_ASSERT_EQUAL(200, korobeiniki::eighthMsAt(0));
TEST_ASSERT_EQUAL(192, korobeiniki::eighthMsAt(1));
TEST_ASSERT_EQUAL(160, korobeiniki::eighthMsAt(5));
TEST_ASSERT_EQUAL(korobeiniki::kFastestEighthMs, korobeiniki::eighthMsAt(12));
TEST_ASSERT_EQUAL(korobeiniki::kFastestEighthMs, korobeiniki::eighthMsAt(99));
for (int level = 0; level < 20; level++) TEST_ASSERT_TRUE(korobeiniki::eighthMsAt(level + 1) <= korobeiniki::eighthMsAt(level));
}
void test_a_new_pace_starts_at_the_next_note() {
static const Note kTwo[] = {{440, 1}, {880, 1}};
TunePlayer p([](size_t i) { return kTwo[i]; }, 2, 100);
Note n{};
uint32_t ms = 0;
p.step(0, n, ms);
p.setEighthMs(50);
TEST_ASSERT_FALSE(p.step(99, n, ms)); // the note that had begun keeps its length
TEST_ASSERT_TRUE(p.step(1, n, ms));
TEST_ASSERT_EQUAL(50, ms);
TEST_ASSERT_TRUE(p.step(50, n, ms));
}
int main() {
UNITY_BEGIN();
RUN_TEST(test_pong_starts_with_a_serve_from_the_middle);
RUN_TEST(test_pong_the_same_seed_is_the_same_game);
RUN_TEST(test_pong_the_ball_stays_between_the_walls);
RUN_TEST(test_pong_a_paddle_stays_on_the_field);
RUN_TEST(test_pong_a_return_counts_and_speeds_the_ball_up);
RUN_TEST(test_pong_a_miss_is_a_point_for_the_other_side);
RUN_TEST(test_pong_ends_at_seven);
RUN_TEST(test_pong_the_device_can_be_beaten);
RUN_TEST(test_pong_a_tilt_puts_the_paddle_where_it_points);
RUN_TEST(test_pong_a_tilted_paddle_settles_on_its_target);
RUN_TEST(test_pong_the_device_paddle_has_a_top_speed);
RUN_TEST(test_snake_starts_in_the_middle_three_long);
RUN_TEST(test_snake_moves_at_its_pace);
RUN_TEST(test_snake_turns_but_not_back_on_itself);
RUN_TEST(test_snake_remembers_a_quick_corner);
RUN_TEST(test_snake_eats_grows_and_gets_new_food);
RUN_TEST(test_snake_dies_at_a_wall);
RUN_TEST(test_snake_dies_on_itself);
RUN_TEST(test_snake_may_follow_its_own_tail);
RUN_TEST(test_snake_gets_faster_as_it_eats);
RUN_TEST(test_snake_the_same_seed_puts_the_food_in_the_same_places);
RUN_TEST(test_tetris_deals_each_piece_once_in_seven);
RUN_TEST(test_tetris_shows_the_piece_that_comes_next);
RUN_TEST(test_tetris_a_piece_falls_at_the_levels_pace);
RUN_TEST(test_tetris_a_soft_drop_is_faster_and_scores);
RUN_TEST(test_tetris_a_held_soft_drop_stops_with_its_piece);
RUN_TEST(test_tetris_moves_stop_at_the_walls);
RUN_TEST(test_tetris_a_held_key_repeats);
RUN_TEST(test_tetris_turns_and_turns_back);
RUN_TEST(test_tetris_a_turn_against_a_wall_shifts_the_piece);
RUN_TEST(test_tetris_a_turn_with_no_room_does_not_happen);
RUN_TEST(test_tetris_a_hard_drop_lands_locks_and_scores);
RUN_TEST(test_tetris_a_piece_locks_when_it_can_fall_no_more);
RUN_TEST(test_tetris_a_full_row_goes_and_the_rest_comes_down);
RUN_TEST(test_tetris_four_rows_at_once_score_most);
RUN_TEST(test_tetris_ten_lines_are_a_level_and_a_faster_fall);
RUN_TEST(test_tetris_the_fall_never_gets_faster_than_its_limit);
RUN_TEST(test_tetris_is_over_when_a_piece_has_no_room);
RUN_TEST(test_tetris_the_same_seed_deals_the_same_pieces);
RUN_TEST(test_tetris_every_piece_in_every_turn_has_four_cells_in_its_box);
RUN_TEST(test_a_tune_gives_its_notes_in_time);
RUN_TEST(test_a_tune_keeps_time_over_uneven_steps);
RUN_TEST(test_a_tune_skips_what_a_long_gap_missed);
RUN_TEST(test_korobeiniki_is_whole_bars);
RUN_TEST(test_the_tune_hurries_with_the_level);
RUN_TEST(test_a_new_pace_starts_at_the_next_note);
return UNITY_END();
}
+92
View File
@@ -3,6 +3,7 @@
#include <string>
#include "http_head.h"
#include "resume.h"
using namespace roro::release;
@@ -42,6 +43,92 @@ void test_a_download_head() {
TEST_ASSERT_EQUAL_STRING("https://elsewhere.example/x", r.head().location.c_str());
}
void test_a_part_of_a_file() {
std::string head = "HTTP/1.1 206 Partial Content\r\nContent-Range: bytes 1000-2510383/2510384\r\n"
"ETag: \"ac948b3f\"\r\nContent-Length: 2509384\r\n\r\n";
HttpHeadParser p;
p.feed(head.data(), head.size());
TEST_ASSERT_EQUAL_INT(206, p.head().status);
TEST_ASSERT_EQUAL_INT(1000, p.head().rangeFrom);
TEST_ASSERT_EQUAL_INT(2510383, p.head().rangeTo);
TEST_ASSERT_EQUAL_INT(2510384, p.head().rangeTotal);
TEST_ASSERT_EQUAL_STRING("\"ac948b3f\"", p.head().etag.c_str());
// A range that makes no sense is no range.
for (const char* bad : {"bytes */2510384", "bytes 5-2/10", "bytes 0-10/10", "items 0-1/2", ""}) {
std::string h = std::string("HTTP/1.1 206 Partial Content\r\nContent-Range: ") + bad + "\r\n\r\n";
HttpHeadParser q;
q.feed(h.data(), h.size());
TEST_ASSERT_EQUAL_INT(-1, q.head().rangeFrom);
}
}
static HttpHead part(long from, long total, const char* etag = "\"abc\"") {
HttpHead h;
h.status = 206;
h.rangeFrom = from;
h.rangeTo = total - 1;
h.rangeTotal = total;
h.contentLength = total - from;
h.etag = etag;
return h;
}
void test_the_rest_of_the_same_file_is_taken() {
TEST_ASSERT_EQUAL_STRING("", resumeRefusal(part(1000, 5000), 1000, 5000, "\"abc\"").c_str());
TEST_ASSERT_EQUAL_STRING("", resumeRefusal(part(1000, 5000, ""), 1000, 5000, "\"abc\"").c_str()); // no ETag this time
TEST_ASSERT_EQUAL_STRING("", resumeRefusal(part(1000, 5000), 1000, 5000, "").c_str()); // none the first time
}
void test_anything_else_is_refused() {
HttpHead whole;
whole.status = 200; // If-Range did not match: the file is another one now
whole.contentLength = 5000;
TEST_ASSERT_EQUAL_STRING("The file changed on the server", resumeRefusal(whole, 1000, 5000, "\"abc\"").c_str());
HttpHead gone;
gone.status = 404;
TEST_ASSERT_EQUAL_STRING("The server answered 404", resumeRefusal(gone, 1000, 5000, "").c_str());
TEST_ASSERT_FALSE(resumeRefusal(part(0, 5000), 1000, 5000, "\"abc\"").empty()); // from the start
TEST_ASSERT_FALSE(resumeRefusal(part(1000, 6000), 1000, 5000, "\"abc\"").empty()); // another size
TEST_ASSERT_FALSE(resumeRefusal(part(1000, 5000, "\"xyz\""), 1000, 5000, "\"abc\"").empty());
HttpHead shortPart = part(1000, 5000);
shortPart.rangeTo = 2000; // not up to the end
TEST_ASSERT_FALSE(resumeRefusal(shortPart, 1000, 5000, "\"abc\"").empty());
HttpHead chunked = part(1000, 5000);
chunked.chunked = true;
TEST_ASSERT_FALSE(resumeRefusal(chunked, 1000, 5000, "\"abc\"").empty());
}
void test_only_a_strong_etag_goes_in_if_range() {
TEST_ASSERT_EQUAL_STRING("\"abc\"", ifRangeFor("\"abc\"").c_str());
TEST_ASSERT_EQUAL_STRING("", ifRangeFor("W/\"abc\"").c_str());
TEST_ASSERT_EQUAL_STRING("", ifRangeFor("").c_str());
}
void test_how_long_a_download_is_tried() {
ResumePolicy poor; // a poor link: each try brings some more
for (int i = 0; i < ResumePolicy::kMaxResumes; i++) TEST_ASSERT_TRUE(poor.again(1000 * (i + 1)));
TEST_ASSERT_FALSE(poor.again(1000000));
TEST_ASSERT_EQUAL_INT(ResumePolicy::kMaxResumes, poor.resumes());
ResumePolicy dead; // the link is gone: nothing more comes
TEST_ASSERT_TRUE(dead.again(5000));
TEST_ASSERT_TRUE(dead.again(5000));
TEST_ASSERT_TRUE(dead.again(5000));
TEST_ASSERT_FALSE(dead.again(5000));
ResumePolicy fromNothing; // not even a first byte
TEST_ASSERT_TRUE(fromNothing.again(0));
TEST_ASSERT_TRUE(fromNothing.again(0));
TEST_ASSERT_FALSE(fromNothing.again(0));
ResumePolicy recovering; // stuck twice, then bytes again: the count starts over
recovering.again(5000);
recovering.again(5000);
recovering.again(5000);
TEST_ASSERT_TRUE(recovering.again(9000));
TEST_ASSERT_TRUE(recovering.again(9000));
}
void test_a_head_that_is_not_http() {
HttpHeadParser p;
std::string junk = "\x16\x03\x01 not http at all\r\n\r\n";
@@ -114,6 +201,11 @@ int main() {
UNITY_BEGIN();
RUN_TEST(test_a_head_in_pieces);
RUN_TEST(test_a_download_head);
RUN_TEST(test_a_part_of_a_file);
RUN_TEST(test_the_rest_of_the_same_file_is_taken);
RUN_TEST(test_anything_else_is_refused);
RUN_TEST(test_only_a_strong_etag_goes_in_if_range);
RUN_TEST(test_how_long_a_download_is_tried);
RUN_TEST(test_a_head_that_is_not_http);
RUN_TEST(test_chunked_bodies);
RUN_TEST(test_urls);
+53
View File
@@ -301,6 +301,56 @@ void test_question_mark_is_help_only_when_not_typing() {
TEST_ASSERT_EQUAL('?', ev[0].ch);
}
void test_held_keys_are_known_between_events() {
KeyMapper m;
m.setTextEntry(false);
TEST_ASSERT_FALSE(m.held(Key::Up));
RawKeys keys;
keys.chars = {';'};
TEST_ASSERT_EQUAL(1, m.update(keys).size()); // the press, once
TEST_ASSERT_TRUE(m.held(Key::Up));
TEST_ASSERT_FALSE(m.held(Key::Down));
TEST_ASSERT_EQUAL(0, m.update(keys).size()); // still held: no new event, still held
TEST_ASSERT_TRUE(m.held(Key::Up));
keys.chars = {';', '/'}; // a second key with it
m.update(keys);
TEST_ASSERT_TRUE(m.held(Key::Up));
TEST_ASSERT_TRUE(m.held(Key::Right));
keys.chars = {'/'};
m.update(keys);
TEST_ASSERT_FALSE(m.held(Key::Up));
TEST_ASSERT_TRUE(m.held(Key::Right));
m.update(RawKeys());
TEST_ASSERT_FALSE(m.held(Key::Right));
}
void test_held_arrows_need_fn_while_typing() {
KeyMapper m;
m.setTextEntry(true);
RawKeys keys;
keys.chars = {'.'};
m.update(keys);
TEST_ASSERT_FALSE(m.held(Key::Down)); // it types a full stop
TEST_ASSERT_TRUE(m.heldChar('.'));
keys.fn = true;
m.update(keys);
TEST_ASSERT_TRUE(m.held(Key::Down));
}
void test_held_characters_and_enter() {
KeyMapper m;
m.setTextEntry(false);
RawKeys keys;
keys.chars = {'z', ' '};
keys.enter = true;
m.update(keys);
TEST_ASSERT_TRUE(m.heldChar('z'));
TEST_ASSERT_TRUE(m.heldChar(' '));
TEST_ASSERT_FALSE(m.heldChar('x'));
TEST_ASSERT_TRUE(m.held(Key::Select));
TEST_ASSERT_FALSE(m.held(Key::Back)); // not asked for by anything: only what a Game steers with
}
int main() {
UNITY_BEGIN();
RUN_TEST(test_plain_character);
@@ -327,5 +377,8 @@ int main() {
RUN_TEST(test_fn_p_is_a_screenshot_in_both_modes);
RUN_TEST(test_plain_h_still_types);
RUN_TEST(test_question_mark_is_help_only_when_not_typing);
RUN_TEST(test_held_keys_are_known_between_events);
RUN_TEST(test_held_arrows_need_fn_while_typing);
RUN_TEST(test_held_characters_and_enter);
return UNITY_END();
}
+471
View File
@@ -0,0 +1,471 @@
#include <unity.h>
#include <cmath>
#include "asteroids.h"
#include "mines.h"
#include "twenty48.h"
using namespace roro::games;
void setUp() {}
void tearDown() {}
// ---- Minesweeper ----
namespace {
uint16_t at(int col, int row) { return static_cast<uint16_t>(row * Mines::kCols + col); }
int countOpen(const Mines& m) {
int n = 0;
for (int r = 0; r < Mines::kRows; r++)
for (int c = 0; c < Mines::kCols; c++) n += m.open(c, r);
return n;
}
int countMines(const Mines& m) {
int n = 0;
for (int r = 0; r < Mines::kRows; r++)
for (int c = 0; c < Mines::kCols; c++) n += m.mine(c, r);
return n;
}
} // namespace
void test_mines_the_first_dig_is_never_a_mine_and_opens_something() {
for (uint32_t seed = 1; seed <= 30; seed++) {
Mines m(seed);
TEST_ASSERT_TRUE(m.phase() == Mines::Phase::Fresh);
int col = static_cast<int>(seed * 7 % Mines::kCols), row = static_cast<int>(seed * 3 % Mines::kRows);
uint8_t e = m.dig(col, row);
TEST_ASSERT_TRUE(e & Mines::Opened);
TEST_ASSERT_FALSE(e & Mines::Boom);
TEST_ASSERT_EQUAL(Mines::kMines, countMines(m));
TEST_ASSERT_EQUAL(0, m.around(col, row)); // nor is one next to it
TEST_ASSERT_TRUE(countOpen(m) >= 4); // so its neighbours opened too
TEST_ASSERT_TRUE(m.phase() == Mines::Phase::Running);
}
}
void test_mines_numbers_count_the_mines_around() {
Mines m(1);
const uint16_t where[] = {at(5, 5), at(6, 5), at(7, 6)};
m.lay(where, 3);
TEST_ASSERT_EQUAL(3, m.around(6, 6)); // all three touch it
TEST_ASSERT_EQUAL(1, m.around(4, 4)); // only (5,5), by a corner
TEST_ASSERT_EQUAL(2, m.around(6, 4)); // the two above which it sits
TEST_ASSERT_EQUAL(0, m.around(20, 10));
}
void test_mines_an_empty_cell_opens_up_to_the_numbers() {
Mines m(1);
const uint16_t where[] = {at(0, 0)};
m.lay(where, 1);
m.dig(15, 6);
TEST_ASSERT_EQUAL(Mines::kCols * Mines::kRows - 1, countOpen(m)); // everything but the mine
TEST_ASSERT_TRUE(m.open(1, 1));
TEST_ASSERT_FALSE(m.open(0, 0));
}
void test_mines_a_number_opens_alone() {
Mines m(1);
const uint16_t where[] = {at(5, 5)};
m.lay(where, 1);
TEST_ASSERT_EQUAL(Mines::Opened, m.dig(4, 4));
TEST_ASSERT_EQUAL(1, countOpen(m));
}
void test_mines_digging_a_mine_loses() {
Mines m(1);
const uint16_t where[] = {at(5, 5), at(20, 3)};
m.lay(where, 2);
m.dig(4, 4);
uint8_t e = m.dig(5, 5);
TEST_ASSERT_TRUE(e & Mines::Boom);
TEST_ASSERT_TRUE(m.phase() == Mines::Phase::Lost);
TEST_ASSERT_EQUAL(0, m.dig(0, 0)); // nothing more
TEST_ASSERT_EQUAL(0, m.flag(0, 0));
}
void test_mines_a_flag_guards_its_cell_and_counts() {
Mines m(1);
const uint16_t where[] = {at(5, 5)};
m.lay(where, 1);
TEST_ASSERT_EQUAL(Mines::kMines, m.minesLeft());
TEST_ASSERT_EQUAL(Mines::Flagged, m.flag(5, 5));
TEST_ASSERT_EQUAL(Mines::kMines - 1, m.minesLeft());
TEST_ASSERT_EQUAL(0, m.dig(5, 5)); // not while it is flagged
TEST_ASSERT_FALSE(m.open(5, 5));
m.flag(5, 5); // taken off
TEST_ASSERT_EQUAL(Mines::kMines, m.minesLeft());
m.dig(4, 4);
TEST_ASSERT_EQUAL(0, m.flag(4, 4)); // an open cell takes no flag
}
void test_mines_a_number_with_its_flags_opens_the_rest() {
Mines m(1);
const uint16_t where[] = {at(5, 5), at(20, 3)};
m.lay(where, 2);
m.dig(4, 4); // a 1
TEST_ASSERT_EQUAL(0, m.dig(4, 4)); // no flag yet: nothing
m.flag(5, 5);
uint8_t e = m.dig(4, 4);
TEST_ASSERT_TRUE(e & Mines::Opened);
TEST_ASSERT_TRUE(m.open(3, 3));
TEST_ASSERT_TRUE(m.open(4, 5));
TEST_ASSERT_FALSE(m.open(5, 5));
}
void test_mines_a_wrong_flag_and_the_same_dig_loses() {
Mines m(1);
const uint16_t where[] = {at(5, 5), at(20, 3)};
m.lay(where, 2);
m.dig(4, 4);
m.flag(3, 3); // the wrong cell
TEST_ASSERT_TRUE(m.dig(4, 4) & Mines::Boom);
}
void test_mines_opening_everything_else_wins() {
Mines m(1);
uint16_t many[Mines::kMines];
for (int i = 0; i < Mines::kMines; i++) many[i] = at(i % Mines::kCols, i / Mines::kCols); // the first row and part of the second
m.lay(many, Mines::kMines);
uint8_t all = 0;
for (int r = 0; r < Mines::kRows; r++)
for (int c = 0; c < Mines::kCols; c++)
if (!m.mine(c, r)) all |= m.dig(c, r);
TEST_ASSERT_TRUE(all & Mines::Won);
TEST_ASSERT_FALSE(all & Mines::Boom);
TEST_ASSERT_TRUE(m.phase() == Mines::Phase::Won);
}
void test_mines_the_clock_runs_from_the_first_dig_to_the_end() {
Mines m(1);
m.tick(5000);
TEST_ASSERT_EQUAL(0, m.seconds()); // not before the first dig
const uint16_t where[] = {at(5, 5), at(20, 3)};
m.lay(where, 2);
m.dig(4, 4);
m.tick(2500);
TEST_ASSERT_EQUAL(2, m.seconds());
m.dig(5, 5);
m.tick(9000);
TEST_ASSERT_EQUAL(2, m.seconds()); // stopped
m.start();
TEST_ASSERT_EQUAL(0, m.seconds());
TEST_ASSERT_EQUAL(0, countOpen(m));
}
// ---- 2048 ----
namespace {
void board(Twenty48& t, std::initializer_list<uint8_t> powers) {
uint8_t p[16] = {};
int i = 0;
for (uint8_t v : powers) p[i++] = v;
t.set(p);
}
int tiles(const Twenty48& t) {
int n = 0;
for (int r = 0; r < 4; r++)
for (int c = 0; c < 4; c++) n += t.power(c, r) != 0;
return n;
}
} // namespace
void test_2048_starts_with_two_tiles() {
for (uint32_t seed = 1; seed <= 20; seed++) {
Twenty48 t(seed);
TEST_ASSERT_EQUAL(2, tiles(t));
TEST_ASSERT_EQUAL(0, t.score());
TEST_ASSERT_FALSE(t.over());
}
}
void test_2048_tiles_slide_to_the_side() {
Twenty48 t(1);
board(t, {0, 1, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0});
uint8_t e = t.slide(Twenty48::Dir::Left);
TEST_ASSERT_TRUE(e & Twenty48::Moved);
TEST_ASSERT_FALSE(e & Twenty48::Merged);
TEST_ASSERT_EQUAL(2, t.value(0, 0));
TEST_ASSERT_EQUAL(4, t.value(1, 0));
TEST_ASSERT_EQUAL(3, tiles(t)); // and a new one came
}
void test_2048_two_alike_become_one_and_score() {
Twenty48 t(1);
board(t, {1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0});
uint8_t e = t.slide(Twenty48::Dir::Right);
TEST_ASSERT_TRUE(e & Twenty48::Merged);
TEST_ASSERT_EQUAL(4, t.value(3, 0));
TEST_ASSERT_EQUAL(4, t.score());
}
void test_2048_a_tile_merges_once_a_move() {
Twenty48 t(1);
board(t, {1, 1, 1, 1, 2, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0});
t.slide(Twenty48::Dir::Left);
TEST_ASSERT_EQUAL(4, t.value(0, 0)); // 2 2 2 2 is 4 4, not 8
TEST_ASSERT_EQUAL(4, t.value(1, 0));
TEST_ASSERT_EQUAL(4, t.value(0, 1)); // 4 2 2 is 4 4, not 8
TEST_ASSERT_EQUAL(4, t.value(1, 1));
TEST_ASSERT_EQUAL(12, t.score());
}
void test_2048_the_pair_nearest_the_wall_merges_first() {
Twenty48 t(1);
board(t, {1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0});
t.slide(Twenty48::Dir::Right);
TEST_ASSERT_EQUAL(4, t.value(3, 0));
TEST_ASSERT_EQUAL(2, t.value(2, 0));
}
void test_2048_up_and_down() {
Twenty48 t(1);
board(t, {1, 0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0});
t.slide(Twenty48::Dir::Down);
TEST_ASSERT_EQUAL(4, t.value(0, 3)); // the 4 at the bottom...
TEST_ASSERT_EQUAL(4, t.value(0, 2)); // ...and the two 2s made into one above it
board(t, {0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0});
t.slide(Twenty48::Dir::Up);
TEST_ASSERT_EQUAL(16, t.value(3, 0));
}
void test_2048_a_move_that_moves_nothing_is_no_move() {
Twenty48 t(1);
board(t, {1, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0});
TEST_ASSERT_EQUAL(0, t.slide(Twenty48::Dir::Left));
TEST_ASSERT_EQUAL(0, t.slide(Twenty48::Dir::Up));
TEST_ASSERT_EQUAL(2, tiles(t)); // no new tile either
}
void test_2048_reaching_2048_is_said_once() {
Twenty48 t(1);
board(t, {10, 10, 0, 0, 10, 10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0});
uint8_t e = t.slide(Twenty48::Dir::Left);
TEST_ASSERT_TRUE(e & Twenty48::Reached);
TEST_ASSERT_TRUE(t.reached());
TEST_ASSERT_EQUAL(2048, t.value(0, 0));
TEST_ASSERT_FALSE(t.over()); // and it goes on
board(t, {10, 10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0});
TEST_ASSERT_FALSE(t.slide(Twenty48::Dir::Left) & Twenty48::Reached);
}
void test_2048_is_over_when_nothing_can_move() {
Twenty48 t(1);
board(t, {1, 2, 1, 2, 2, 1, 2, 1, 1, 2, 1, 2, 2, 1, 2, 1});
TEST_ASSERT_TRUE(t.over());
TEST_ASSERT_EQUAL(0, t.slide(Twenty48::Dir::Left));
// One pair left: it is not over, and the pair can be made into one.
board(t, {1, 2, 1, 2, 2, 1, 2, 1, 1, 2, 1, 2, 2, 1, 3, 3});
TEST_ASSERT_FALSE(t.over());
uint8_t e = t.slide(Twenty48::Dir::Left);
TEST_ASSERT_TRUE(e & Twenty48::Merged);
TEST_ASSERT_EQUAL(16, t.score());
}
void test_2048_the_same_seed_is_the_same_game() {
Twenty48 a(9), b(9);
const Twenty48::Dir moves[] = {Twenty48::Dir::Left, Twenty48::Dir::Up, Twenty48::Dir::Right, Twenty48::Dir::Down};
for (int i = 0; i < 60; i++) {
a.slide(moves[i % 4]);
b.slide(moves[i % 4]);
}
for (int r = 0; r < 4; r++)
for (int c = 0; c < 4; c++) TEST_ASSERT_EQUAL(a.power(c, r), b.power(c, r));
TEST_ASSERT_EQUAL(a.score(), b.score());
}
void test_2048_new_tiles_are_twos_and_sometimes_fours() {
int twos = 0, fours = 0;
for (uint32_t seed = 1; seed <= 300; seed++) {
Twenty48 t(seed);
for (int r = 0; r < 4; r++)
for (int c = 0; c < 4; c++) {
twos += t.value(c, r) == 2;
fours += t.value(c, r) == 4;
}
}
TEST_ASSERT_EQUAL(600, twos + fours);
TEST_ASSERT_TRUE(fours > 20 && fours < 120); // about one in ten
}
// ---- Asteroids ----
namespace {
Asteroids::Rock rockAt(float x, float y, uint8_t size, float vx = 0, float vy = 0) {
Asteroids::Rock r{};
r.x = x;
r.y = y;
r.vx = vx;
r.vy = vy;
r.size = size;
return r;
}
void fly(Asteroids& a, float seconds, AsteroidsInput in = AsteroidsInput()) {
for (float t = 0; t < seconds; t += 0.02f) a.step(0.02f, in);
}
} // namespace
void test_asteroids_a_lean_is_a_turn() {
TEST_ASSERT_EQUAL_FLOAT(0, tiltTurn(0.02f, 0)); // a steady hand: straight on
TEST_ASSERT_EQUAL_FLOAT(0, tiltTurn(0.32f, 0.30f)); // around how it was held at the start
TEST_ASSERT_TRUE(tiltTurn(0.2f, 0) > 0.3f && tiltTurn(0.2f, 0) < 0.7f);
TEST_ASSERT_EQUAL_FLOAT(1, tiltTurn(0.6f, 0));
TEST_ASSERT_EQUAL_FLOAT(-1, tiltTurn(-0.6f, 0));
TEST_ASSERT_EQUAL_FLOAT(-tiltTurn(0.2f, 0), tiltTurn(-0.2f, 0));
}
void test_asteroids_a_wave_starts_away_from_the_ship() {
for (uint32_t seed = 1; seed <= 20; seed++) {
Asteroids a(seed);
TEST_ASSERT_EQUAL(1, a.wave());
TEST_ASSERT_EQUAL(3, a.rocks().size());
for (auto& r : a.rocks()) {
TEST_ASSERT_EQUAL(3, r.size);
TEST_ASSERT_TRUE(std::hypot(r.x - a.shipX(), r.y - a.shipY()) > 40);
}
TEST_ASSERT_EQUAL(Asteroids::kLives, a.lives());
}
}
void test_asteroids_the_ship_turns_and_thrusts() {
Asteroids a(1);
a.setRocks({rockAt(10, 10, 1)});
AsteroidsInput in;
in.turn = 1;
fly(a, 0.5f, in);
TEST_ASSERT_FLOAT_WITHIN(0.1, Asteroids::kTurnSpeed * 0.5f, a.shipAngle());
a.placeShip(120, 60, 0); // pointing up
in = AsteroidsInput();
in.thrust = true;
fly(a, 0.5f, in);
TEST_ASSERT_TRUE(a.shipY() < 50);
TEST_ASSERT_FLOAT_WITHIN(1, 120, a.shipX());
TEST_ASSERT_TRUE(a.thrusting());
float y = a.shipY();
fly(a, 3); // no thrust: it drifts on, slowing
TEST_ASSERT_TRUE(a.shipY() != y);
}
void test_asteroids_everything_wraps_round() {
Asteroids a(1);
a.setRocks({rockAt(235, 60, 1, 40, 0)});
fly(a, 0.5f);
TEST_ASSERT_TRUE(a.rocks()[0].x < 30);
a.placeShip(120, 2, 0);
AsteroidsInput in;
in.thrust = true;
fly(a, 0.3f, in);
TEST_ASSERT_TRUE(a.shipY() > 100); // out at the top, in at the bottom
}
void test_asteroids_a_bullet_breaks_a_rock_in_two() {
Asteroids a(1);
a.placeShip(120, 100, 0);
a.setRocks({rockAt(120, 60, 3), rockAt(10, 10, 1)});
AsteroidsInput in;
in.fire = true;
uint8_t all = a.step(0.02f, in);
TEST_ASSERT_TRUE(all & Asteroids::Shot);
for (int i = 0; i < 20; i++) all |= a.step(0.02f, AsteroidsInput());
TEST_ASSERT_TRUE(all & Asteroids::Hit);
TEST_ASSERT_EQUAL(20, a.score());
int middle = 0;
for (auto& r : a.rocks()) middle += r.size == 2;
TEST_ASSERT_EQUAL(2, middle);
TEST_ASSERT_EQUAL(0, a.bullets().size()); // spent on it
}
void test_asteroids_a_small_rock_is_gone_and_scores_most() {
Asteroids a(1);
a.placeShip(120, 100, 0);
a.setRocks({rockAt(120, 70, 1), rockAt(10, 10, 1)});
AsteroidsInput in;
in.fire = true;
a.step(0.02f, in);
fly(a, 0.4f);
TEST_ASSERT_EQUAL(100, a.score());
TEST_ASSERT_EQUAL(1, a.rocks().size());
}
void test_asteroids_bullets_are_few_and_short_lived() {
Asteroids a(1);
a.setRocks({rockAt(10, 10, 1)});
a.placeShip(120, 60, 1.57f); // to the right, where nothing is
AsteroidsInput in;
in.fire = true;
for (int i = 0; i < 10; i++) a.step(0.01f, in);
TEST_ASSERT_EQUAL(Asteroids::kMaxBullets, a.bullets().size());
fly(a, Asteroids::kBulletLife + 0.1f);
TEST_ASSERT_EQUAL(0, a.bullets().size());
}
void test_asteroids_a_rock_costs_a_ship_and_the_last_ends_it() {
Asteroids a(1);
a.setRocks({rockAt(120, 60, 3)});
a.placeShip(120, 60, 0);
fly(a, 0.5f); // shielded for a moment after coming in
TEST_ASSERT_EQUAL(Asteroids::kLives, a.lives());
uint8_t all = 0;
for (int i = 0; i < 200 && !(all & Asteroids::Died); i++) all |= a.step(0.02f, AsteroidsInput());
TEST_ASSERT_TRUE(all & Asteroids::Died);
TEST_ASSERT_EQUAL(Asteroids::kLives - 1, a.lives());
TEST_ASSERT_FALSE(a.shipShown()); // out for a moment, then back in the middle
fly(a, Asteroids::kRespawnWait + 0.1f);
TEST_ASSERT_TRUE(a.shipShown());
TEST_ASSERT_TRUE(a.shielded());
for (int life = 0; life < 2; life++) {
a.setRocks({rockAt(a.shipX(), a.shipY(), 3)});
fly(a, Asteroids::kShielded + Asteroids::kRespawnWait + 0.5f);
}
TEST_ASSERT_TRUE(a.over());
TEST_ASSERT_EQUAL(0, a.lives());
TEST_ASSERT_EQUAL(0, a.step(1, AsteroidsInput()));
}
void test_asteroids_the_last_rock_brings_the_next_wave() {
Asteroids a(1);
a.placeShip(120, 100, 0);
a.setRocks({rockAt(120, 70, 1)});
AsteroidsInput in;
in.fire = true;
uint8_t all = a.step(0.02f, in);
for (int i = 0; i < 30; i++) all |= a.step(0.02f, AsteroidsInput());
TEST_ASSERT_TRUE(all & Asteroids::Wave);
TEST_ASSERT_EQUAL(2, a.wave());
TEST_ASSERT_EQUAL(4, a.rocks().size()); // one more each wave
}
int main() {
UNITY_BEGIN();
RUN_TEST(test_mines_the_first_dig_is_never_a_mine_and_opens_something);
RUN_TEST(test_mines_numbers_count_the_mines_around);
RUN_TEST(test_mines_an_empty_cell_opens_up_to_the_numbers);
RUN_TEST(test_mines_a_number_opens_alone);
RUN_TEST(test_mines_digging_a_mine_loses);
RUN_TEST(test_mines_a_flag_guards_its_cell_and_counts);
RUN_TEST(test_mines_a_number_with_its_flags_opens_the_rest);
RUN_TEST(test_mines_a_wrong_flag_and_the_same_dig_loses);
RUN_TEST(test_mines_opening_everything_else_wins);
RUN_TEST(test_mines_the_clock_runs_from_the_first_dig_to_the_end);
RUN_TEST(test_2048_starts_with_two_tiles);
RUN_TEST(test_2048_tiles_slide_to_the_side);
RUN_TEST(test_2048_two_alike_become_one_and_score);
RUN_TEST(test_2048_a_tile_merges_once_a_move);
RUN_TEST(test_2048_the_pair_nearest_the_wall_merges_first);
RUN_TEST(test_2048_up_and_down);
RUN_TEST(test_2048_a_move_that_moves_nothing_is_no_move);
RUN_TEST(test_2048_reaching_2048_is_said_once);
RUN_TEST(test_2048_is_over_when_nothing_can_move);
RUN_TEST(test_2048_the_same_seed_is_the_same_game);
RUN_TEST(test_2048_new_tiles_are_twos_and_sometimes_fours);
RUN_TEST(test_asteroids_a_lean_is_a_turn);
RUN_TEST(test_asteroids_a_wave_starts_away_from_the_ship);
RUN_TEST(test_asteroids_the_ship_turns_and_thrusts);
RUN_TEST(test_asteroids_everything_wraps_round);
RUN_TEST(test_asteroids_a_bullet_breaks_a_rock_in_two);
RUN_TEST(test_asteroids_a_small_rock_is_gone_and_scores_most);
RUN_TEST(test_asteroids_bullets_are_few_and_short_lived);
RUN_TEST(test_asteroids_a_rock_costs_a_ship_and_the_last_ends_it);
RUN_TEST(test_asteroids_the_last_rock_brings_the_next_wave);
return UNITY_END();
}