moshcode 0.46.0 → 0.47.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/games-breakout.mjs +33 -7
- package/src/games-draw.mjs +78 -13
- package/src/games-pong.mjs +25 -5
package/package.json
CHANGED
package/src/games-breakout.mjs
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
// decides the angle it leaves at, so the paddle is a steering wheel rather than
|
|
5
5
|
// a wall. Without that you cannot dig a channel up the side of the wall, and
|
|
6
6
|
// digging a channel is the entire reason anybody still plays this.
|
|
7
|
-
import {
|
|
7
|
+
import { advanceBall, drawnBall, drawnCell, snapBall } from "./games-draw.mjs";
|
|
8
8
|
import { acid, amber, bone, danger, rgb } from "./ui.mjs";
|
|
9
9
|
|
|
10
10
|
export const WIDTH = 40;
|
|
@@ -17,7 +17,11 @@ export const BRICK_TOP = 1;
|
|
|
17
17
|
|
|
18
18
|
export const PADDLE_W = 7;
|
|
19
19
|
export const PADDLE_ROW = HEIGHT - 1;
|
|
20
|
-
|
|
20
|
+
// A keypress, not a tick, so this is unchanged by the tick rate — but it does
|
|
21
|
+
// have to keep up with the ball, and a ball taken off the end of the paddle now
|
|
22
|
+
// crosses a column in under two ticks. Three columns a press stays ahead of it
|
|
23
|
+
// at a terminal's key-repeat rate; two only just did.
|
|
24
|
+
const PADDLE_STEP = 3;
|
|
21
25
|
|
|
22
26
|
/**
|
|
23
27
|
* How often the wall is stepped. See the note in games-pong.mjs: the ball can
|
|
@@ -32,8 +36,20 @@ const PADDLE_STEP = 2; // a keypress, not a tick — unchanged by the tick rate
|
|
|
32
36
|
*/
|
|
33
37
|
export const TICK_MS = 16;
|
|
34
38
|
|
|
39
|
+
/**
|
|
40
|
+
* How hard the wall is played, against the pace it was first tuned at.
|
|
41
|
+
*
|
|
42
|
+
* Launched, the old ball took three seconds to cross the board and two and a
|
|
43
|
+
* half to fall the height of it, which is a slow enough ball that you can put
|
|
44
|
+
* the paddle under it and go and make a cup of tea. It also meant the drawn
|
|
45
|
+
* ball moved twenty times a second, and twenty steps a second does not read as
|
|
46
|
+
* travel however even they are. Everything below scales together, so a ball off
|
|
47
|
+
* the end of the paddle leaves at the angle it always did.
|
|
48
|
+
*/
|
|
49
|
+
const PACE = 1.9;
|
|
50
|
+
|
|
35
51
|
/** The speeds below are still written per 50ms, the rate this was tuned at. */
|
|
36
|
-
const SCALE = TICK_MS / 50;
|
|
52
|
+
const SCALE = (TICK_MS / 50) * PACE;
|
|
37
53
|
|
|
38
54
|
const LIVES = 3;
|
|
39
55
|
const BASE_VX = 0.62 * SCALE;
|
|
@@ -67,6 +83,7 @@ export function brickAt(wall, x, y) {
|
|
|
67
83
|
function rest(state) {
|
|
68
84
|
state.ball = { x: state.paddle + PADDLE_W / 2, y: PADDLE_ROW - 1, vx: 0, vy: 0 };
|
|
69
85
|
state.stuck = true;
|
|
86
|
+
state.drawn = drawnBall(state.ball.x, state.ball.y);
|
|
70
87
|
return state;
|
|
71
88
|
}
|
|
72
89
|
|
|
@@ -82,8 +99,12 @@ export function launch(state) {
|
|
|
82
99
|
export function step(state) {
|
|
83
100
|
if (state.stuck) {
|
|
84
101
|
// A ball that has not been launched rides the paddle, so moving before you
|
|
85
|
-
// serve aims the serve.
|
|
102
|
+
// serve aims the serve. It is carried rather than travelling, so it is put
|
|
103
|
+
// where the paddle is rather than paced there — a stationary ball earns no
|
|
104
|
+
// steps, and would otherwise sit still while the paddle slid out from under
|
|
105
|
+
// it.
|
|
86
106
|
state.ball.x = state.paddle + PADDLE_W / 2;
|
|
107
|
+
snapBall(state.drawn, state.ball.x, state.ball.y);
|
|
87
108
|
return state;
|
|
88
109
|
}
|
|
89
110
|
|
|
@@ -122,11 +143,16 @@ export function step(state) {
|
|
|
122
143
|
}
|
|
123
144
|
}
|
|
124
145
|
|
|
146
|
+
advanceBall(state.drawn, ball);
|
|
147
|
+
|
|
125
148
|
if (ball.y > PADDLE_ROW) {
|
|
126
149
|
state.lives--;
|
|
127
150
|
if (state.lives <= 0) {
|
|
128
151
|
state.lives = 0;
|
|
129
152
|
state.over = `out of balls · ${state.score} points`;
|
|
153
|
+
// The last ball is left where it went, below the board and so off it,
|
|
154
|
+
// rather than resting on the row it fell past.
|
|
155
|
+
snapBall(state.drawn, ball.x, ball.y);
|
|
130
156
|
return state;
|
|
131
157
|
}
|
|
132
158
|
rest(state);
|
|
@@ -198,9 +224,9 @@ export const BREAKOUT = {
|
|
|
198
224
|
}
|
|
199
225
|
|
|
200
226
|
for (let i = 0; i < PADDLE_W; i++) put(state.paddle + i, PADDLE_ROW, bone("▀"));
|
|
201
|
-
// Drawn on half-rows, so the ball steps the same
|
|
202
|
-
//
|
|
203
|
-
const ball =
|
|
227
|
+
// Drawn on half-rows and on its own even clock, so the ball steps the same
|
|
228
|
+
// distance down the wall as across it, and at a rate. See games-draw.mjs.
|
|
229
|
+
const ball = drawnCell(state.drawn);
|
|
204
230
|
put(ball.col, ball.row, (state.stuck ? amber : bone)(ball.glyph));
|
|
205
231
|
|
|
206
232
|
return grid.map((row) => row.map((cell) => cell ?? " ").join(""));
|
package/src/games-draw.mjs
CHANGED
|
@@ -24,27 +24,92 @@
|
|
|
24
24
|
// one, and the corner it turns is half as wide. It is also a better ball than
|
|
25
25
|
// `●` was — a square pixel moving on a square grid, rather than a round dot
|
|
26
26
|
// snapping between cells twice its own height apart.
|
|
27
|
+
//
|
|
28
|
+
// Half blocks fix the size of the steps. They do not fix when the steps happen,
|
|
29
|
+
// which turned out to be the other half of it — see `drawnBall` below.
|
|
27
30
|
|
|
28
31
|
/**
|
|
29
|
-
*
|
|
32
|
+
* The ball's position in half-rows — the unit it is actually drawn in.
|
|
30
33
|
*
|
|
31
34
|
* `y` is a row centre, so row r covers y from r - 0.5 up to r + 0.5: below the
|
|
32
|
-
* centre the ball is in the top half of
|
|
33
|
-
*
|
|
35
|
+
* centre the ball is in the top half of that cell, at or above it the bottom.
|
|
36
|
+
* Half-rows and columns are the same size on screen, so this and the column
|
|
37
|
+
* together are a square lattice, and a step is a step whichever way it goes.
|
|
34
38
|
*/
|
|
35
|
-
export
|
|
36
|
-
const col = Math.round(x);
|
|
39
|
+
export const halfRow = (y) => {
|
|
37
40
|
const row = Math.round(y);
|
|
38
|
-
return
|
|
41
|
+
return y < row ? row * 2 : row * 2 + 1;
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* A drawn ball, released on its own even clock.
|
|
46
|
+
*
|
|
47
|
+
* Equal pitch fixed the size of the ball's steps but not their timing, and the
|
|
48
|
+
* timing is the rest of the jiggle. Rounding the true position to the lattice
|
|
49
|
+
* moves the ball whenever it happens to cross a column edge or a half-row edge,
|
|
50
|
+
* and those two are on unrelated schedules: a ball with the two periods close
|
|
51
|
+
* but not equal — which is what a near-diagonal is, and what breakout launches
|
|
52
|
+
* at — beats between them. Measured, breakout held a cell for 16ms, then 80ms,
|
|
53
|
+
* then 48ms, five times a second. The steps were the right size and still the
|
|
54
|
+
* ball looked like it was struggling, because nothing was moving at a rate.
|
|
55
|
+
*
|
|
56
|
+
* So the true position is not what is drawn. It decides only which way the
|
|
57
|
+
* drawn ball owes a step; when that step is paid is decided by a clock that
|
|
58
|
+
* ticks at the ball's own speed. `owed` accrues at |vx| + 2|vy| lattice units
|
|
59
|
+
* per tick — the distance the true ball covers, measured the way the drawn one
|
|
60
|
+
* has to travel it — and a whole unit buys one step. The drawn ball therefore
|
|
61
|
+
* moves every 1/speed ticks whatever angle it is on, trailing the true one by
|
|
62
|
+
* under a unit, which is under half a character.
|
|
63
|
+
*
|
|
64
|
+
* It cannot fall behind: the same accrual that paces the ball also lets it pay
|
|
65
|
+
* two steps in a tick when the ball is genuinely moving that fast.
|
|
66
|
+
*/
|
|
67
|
+
export function drawnBall(x, y) {
|
|
68
|
+
return snapBall({ col: 0, half: 0, owed: 0 }, x, y);
|
|
39
69
|
}
|
|
40
70
|
|
|
41
71
|
/**
|
|
42
|
-
*
|
|
72
|
+
* Put the drawn ball exactly where the real one is, with no debt either way.
|
|
43
73
|
*
|
|
44
|
-
*
|
|
45
|
-
*
|
|
74
|
+
* For the moves that are not travel and so have nothing to smooth: a serve, a
|
|
75
|
+
* fresh ball on the paddle, the ball riding a paddle that is being aimed.
|
|
46
76
|
*/
|
|
47
|
-
export
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
77
|
+
export function snapBall(drawn, x, y) {
|
|
78
|
+
drawn.col = Math.round(x);
|
|
79
|
+
drawn.half = halfRow(y);
|
|
80
|
+
drawn.owed = 0;
|
|
81
|
+
return drawn;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/** Far enough apart that the ball was put there rather than travelled there. */
|
|
85
|
+
const TELEPORT = 4;
|
|
86
|
+
|
|
87
|
+
/** Pay out whatever steps the ball has earned this tick. */
|
|
88
|
+
export function advanceBall(drawn, ball) {
|
|
89
|
+
const col = Math.round(ball.x);
|
|
90
|
+
const half = halfRow(ball.y);
|
|
91
|
+
if (Math.abs(col - drawn.col) + Math.abs(half - drawn.half) >= TELEPORT) return snapBall(drawn, ball.x, ball.y);
|
|
92
|
+
|
|
93
|
+
drawn.owed += Math.abs(ball.vx) + Math.abs(ball.vy) * 2;
|
|
94
|
+
while (drawn.owed >= 1) {
|
|
95
|
+
const dcol = col - drawn.col;
|
|
96
|
+
const dhalf = half - drawn.half;
|
|
97
|
+
if (dcol === 0 && dhalf === 0) break;
|
|
98
|
+
// Whichever axis is further behind goes first, which is what keeps a
|
|
99
|
+
// diagonal a staircase instead of a sideways run and then a drop.
|
|
100
|
+
if (Math.abs(dcol) >= Math.abs(dhalf)) drawn.col += Math.sign(dcol);
|
|
101
|
+
else drawn.half += Math.sign(dhalf);
|
|
102
|
+
drawn.owed -= 1;
|
|
103
|
+
}
|
|
104
|
+
// A ball that has caught up banks at most one step, so that standing still
|
|
105
|
+
// for a moment cannot be turned into a lurch later.
|
|
106
|
+
if (drawn.owed > 1) drawn.owed = 1;
|
|
107
|
+
return drawn;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/** The cell and half block to write for a drawn ball. */
|
|
111
|
+
export const drawnCell = (drawn) => ({
|
|
112
|
+
col: drawn.col,
|
|
113
|
+
row: drawn.half >> 1,
|
|
114
|
+
glyph: drawn.half % 2 === 0 ? "▀" : "▄",
|
|
115
|
+
});
|
package/src/games-pong.mjs
CHANGED
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
// A flat return it will always get; one taken off the end of your paddle it will
|
|
8
8
|
// not. That is the whole game, and it is why the angle off the paddle depends on
|
|
9
9
|
// where the ball hit it.
|
|
10
|
-
import {
|
|
10
|
+
import { advanceBall, drawnBall, drawnCell, snapBall } from "./games-draw.mjs";
|
|
11
11
|
import { acid, bone, danger, dim } from "./ui.mjs";
|
|
12
12
|
|
|
13
13
|
export const WIDTH = 44;
|
|
@@ -37,13 +37,25 @@ export const TARGET = 7; // first to this many
|
|
|
37
37
|
*/
|
|
38
38
|
export const TICK_MS = 16;
|
|
39
39
|
|
|
40
|
+
/**
|
|
41
|
+
* How hard the table is played, against the pace it was first tuned at.
|
|
42
|
+
*
|
|
43
|
+
* The old pace put the ball across the table in just under three seconds. That
|
|
44
|
+
* is not a ball being hit, it is a ball being carried, and it was also why the
|
|
45
|
+
* drawn ball only moved twenty times a second — too few steps for any of them
|
|
46
|
+
* to be smooth. Everything below is scaled by this, the machine along with the
|
|
47
|
+
* ball, so the balance is exactly the one that was tuned; only the clock it is
|
|
48
|
+
* played against changes.
|
|
49
|
+
*/
|
|
50
|
+
const PACE = 1.9;
|
|
51
|
+
|
|
40
52
|
/**
|
|
41
53
|
* The speeds below are still written per 55ms — the rate this game was tuned
|
|
42
54
|
* at — and scaled to the tick. Keeping the tuned numbers legible matters more
|
|
43
55
|
* than saving a multiply: they are what makes the machine beatable off the end
|
|
44
56
|
* of the paddle and not from the middle, and that balance is the game.
|
|
45
57
|
*/
|
|
46
|
-
const SCALE = TICK_MS / 55;
|
|
58
|
+
const SCALE = (TICK_MS / 55) * PACE;
|
|
47
59
|
|
|
48
60
|
const SERVE_SPEED = 0.85 * SCALE;
|
|
49
61
|
const MAX_SPEED = 1.7 * SCALE;
|
|
@@ -65,6 +77,8 @@ export function serve(state, toward) {
|
|
|
65
77
|
// Never dead flat: a ball with no angle is a rally nobody can lose.
|
|
66
78
|
vy: (state.rng() < 0.5 ? -1 : 1) * (0.15 + state.rng() * 0.2) * SCALE,
|
|
67
79
|
};
|
|
80
|
+
// A serve is a ball put on the table, not a ball that travelled there.
|
|
81
|
+
state.drawn = drawnBall(state.ball.x, state.ball.y);
|
|
68
82
|
return state;
|
|
69
83
|
}
|
|
70
84
|
|
|
@@ -106,6 +120,12 @@ export function step(state) {
|
|
|
106
120
|
|
|
107
121
|
if (ball.x < 0) { state.theirs++; point(state, 1); }
|
|
108
122
|
else if (ball.x > WIDTH - 1) { state.yours++; point(state, -1); }
|
|
123
|
+
// Anything else is the ball travelling, which is the only thing the drawn
|
|
124
|
+
// ball is asked to follow — a serve puts it back itself.
|
|
125
|
+
else advanceBall(state.drawn, ball);
|
|
126
|
+
// A match ends with the ball where it went out, off the table and so off the
|
|
127
|
+
// board, rather than parked on the edge it left by.
|
|
128
|
+
if (state.over) snapBall(state.drawn, ball.x, ball.y);
|
|
109
129
|
|
|
110
130
|
// The machine: idle in the middle until the ball is on its half, then chase
|
|
111
131
|
// the ball's row. Perfect tracking here would make the game unloseable for it,
|
|
@@ -165,9 +185,9 @@ export const PONG = {
|
|
|
165
185
|
|
|
166
186
|
for (const row of paddleRows(state.you)) put(YOU_COL, row, acid("█"));
|
|
167
187
|
for (const row of paddleRows(state.them)) put(THEM_COL, row, danger("█"));
|
|
168
|
-
// Drawn on half-rows, so the ball steps the same
|
|
169
|
-
//
|
|
170
|
-
const ball =
|
|
188
|
+
// Drawn on half-rows and on its own even clock, so the ball steps the same
|
|
189
|
+
// distance up the table as across it, and at a rate. See games-draw.mjs.
|
|
190
|
+
const ball = drawnCell(state.drawn);
|
|
171
191
|
put(ball.col, ball.row, bone(ball.glyph));
|
|
172
192
|
|
|
173
193
|
return grid.map((row, y) => row.map((cell, x) => (
|