wreckwren / core.js
CyberMax-tools's picture
Deploy wreckwren
afef699 verified
Raw History Blame Contribute Delete
10.3 kB
// Wreckwren core: the rules, pure and deterministic, so the toys Worker can replay a daily run and check its score.
// The same file runs in three places: the daily challenge page, the any-page mode (wreck.js, real page elements as
// blocks) and the leaderboard Worker (a copy made by scripts/games/sync_kit.py). No DOM, no clock, no randomness
// except the seeded PRNG below. (c) 2026 CyberMax. All rights reserved.
export const TPS = 60; // ticks per second
export const ROUND = 30 * TPS; // a daily round is 30 seconds
export const W = 1000, H = 1400; // logical size of the daily page (the page is scaled to fit the screen)
/** The three free weapons. Fun Pass skins change how they look, never these numbers (no pay-to-win). */
export const WEAPONS = {
kick: { cd: 12, dmg: 1, r: 0, label: 'Kick' }, // one block under the point
hammer: { cd: 36, dmg: 2, r: 70, label: 'Hammer' }, // every block within 70 px
fire: { cd: 48, dmg: 1, r: 0, label: 'Fire' }, // sets a block on fire; fire spreads to touching blocks
};
export const BURN_EVERY = 40, BURN_FOR = 160, SPREAD_GAP = 10;
export const COMBO_GAP = 50, COMBO_MAX = 5;
export const KIND = {
text: { hp: 2, pts: 10 },
link: { hp: 1, pts: 15 },
heading: { hp: 3, pts: 40 },
image: { hp: 4, pts: 30 },
button: { hp: 2, pts: 25 },
box: { hp: 5, pts: 50 },
};
function mulberry(seed) {
let a = seed >>> 0;
return () => {
a = (a + 0x6d2b79f5) >>> 0;
let t = a;
t = Math.imul(t ^ (t >>> 15), t | 1);
t ^= t + Math.imul(t ^ (t >>> 7), t | 61);
return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
};
}
export function hash(str) {
let h = 0x811c9dc5;
for (let i = 0; i < str.length; i++) { h ^= str.charCodeAt(i); h = Math.imul(h, 0x01000193); }
return h >>> 0;
}
/** Today's target page types rotate by day: everyone gets the same page on the same date. */
export const THEMES = ['encyclopedia', 'news', 'shop', 'blog', 'search'];
export const THEME_TITLE = {
encyclopedia: 'an encyclopedia article', news: 'a news front page', shop: 'an online shop',
blog: 'a cooking blog', search: 'a search results page',
};
const WORDS = ('the of and history city river famous people early modern found known world local north great small new old ' +
'free sale best today recipe garden market review story photo price learn more about every cheap fresh daily guide ' +
'season local weather music sport team record open travel coffee bread kitchen science moon ocean forest mountain').split(' ');
/**
* Build the daily page from a date string (YYYY-MM-DD): a list of blocks laid out on a 1000 x 1400 page.
* Each block: { id, kind, x, y, w, h, text }. Layouts differ per theme; words and sizes come from the seed.
*/
export function dailyPage(day) {
const r = mulberry(hash('wreckwren|' + day));
const theme = THEMES[hash(day) % THEMES.length];
const pick = (a) => a[Math.floor(r() * a.length)];
const words = (n) => Array.from({ length: n }, () => pick(WORDS)).join(' ');
const cap = (s) => s.charAt(0).toUpperCase() + s.slice(1);
const blocks = [];
const add = (kind, x, y, w, h, text = '') => blocks.push({ id: blocks.length, kind, x, y, w, h, text });
// header strip: logo, nav links, search button
add('heading', 30, 24, 260, 56, cap(words(2)));
for (let i = 0; i < 5; i++) add('link', 320 + i * 110, 40, 96, 28, cap(pick(WORDS)));
add('button', 880, 32, 92, 44, 'Search');
let y = 120;
if (theme === 'encyclopedia' || theme === 'blog') {
add('heading', 30, y, 640, 64, cap(words(3)));
add('box', 700, y, 270, 420, 'Infobox');
y += 90;
add('image', 720, y + 10, 230, 160);
for (let i = 0; i < 9; i++) {
add('text', 30, y, 640, 74 + Math.floor(r() * 30), cap(words(14 + Math.floor(r() * 10))) + '.');
y += 120;
if (i === 3) { add('heading', 30, y, 420, 44, cap(words(2))); y += 64; }
}
for (let i = 0; i < 6; i++) add('link', 720, 560 + i * 44, 220, 28, cap(words(2)));
} else if (theme === 'news') {
add('image', 30, y, 600, 300);
add('heading', 650, y, 320, 120, cap(words(5)));
add('text', 650, y + 140, 320, 150, cap(words(18)) + '.');
y += 330;
for (let row = 0; row < 3; row++) {
for (let c = 0; c < 3; c++) {
add('image', 30 + c * 325, y, 300, 160);
add('link', 30 + c * 325, y + 172, 300, 48, cap(words(5)));
}
y += 260;
}
add('box', 30, y, 940, 160, 'Newsletter');
add('button', 780, y + 50, 160, 60, 'Subscribe');
} else if (theme === 'shop') {
add('box', 30, y, 940, 200, 'Big sale banner');
add('button', 780, y + 120, 160, 56, 'Shop now');
y += 230;
for (let row = 0; row < 3; row++) {
for (let c = 0; c < 4; c++) {
const x = 30 + c * 240;
add('image', x, y, 220, 180);
add('link', x, y + 190, 220, 30, cap(words(2)));
add('text', x, y + 224, 100, 30, '$' + (5 + Math.floor(r() * 90)) + '.99');
add('button', x + 120, y + 222, 100, 36, 'Add');
}
y += 300;
}
} else { // search
add('box', 30, y, 700, 70, 'Search box');
add('button', 750, y + 8, 120, 54, 'Go');
y += 110;
for (let i = 0; i < 8; i++) {
add('link', 30, y, 520, 34, cap(words(5)));
add('text', 30, y + 40, 700, 60, cap(words(16)) + '.');
y += 130;
}
add('box', 760, 230, 210, 520, 'Ads');
for (let i = 0; i < 3; i++) add('image', 780, 260 + i * 160, 170, 130);
}
add('text', 30, H - 70, 940, 40, 'Footer: ' + words(8));
return { day, theme, blocks: blocks.filter((b) => b.y + b.h <= H) };
}
const hit = (b, x, y, r) => {
const cx = Math.max(b.x, Math.min(x, b.x + b.w)), cy = Math.max(b.y, Math.min(y, b.y + b.h));
return (cx - x) ** 2 + (cy - y) ** 2 <= r * r;
};
const touching = (a, b, g) => a.x - g < b.x + b.w && b.x - g < a.x + a.w && a.y - g < b.y + b.h && b.y - g < a.y + a.h;
/**
* A round. blocks = [{ id, kind, x, y, w, h }]. limit = ticks (ROUND for the daily, Infinity for free play).
* step(action|null) advances one tick; action = { w: 'kick'|'hammer'|'fire', x, y }.
* Each tick returns the events of that tick (hits, burns, kills) so the page can animate exactly what the rules did.
*/
export function makeRound(blocks, limit = ROUND) {
const B = blocks.map((b) => ({ ...b, hp: (KIND[b.kind] || KIND.text).hp, burn: 0, dead: false }));
const s = { t: 0, score: 0, combo: 1, lastKill: -999, kills: 0, total: B.length, over: false, cool: { kick: 0, hammer: 0, fire: 0 }, blocks: B };
function damage(b, n, how, ev) {
if (b.dead) return;
b.hp -= n;
if (b.hp > 0) { ev.push({ e: 'hit', id: b.id, how, hp: b.hp }); return; }
b.dead = true; b.burn = 0;
s.combo = s.t - s.lastKill <= COMBO_GAP ? Math.min(COMBO_MAX, s.combo + 1) : 1;
s.lastKill = s.t;
const pts = (KIND[b.kind] || KIND.text).pts * s.combo;
s.score += pts; s.kills++;
ev.push({ e: 'kill', id: b.id, how, pts, combo: s.combo });
}
s.step = function step(a) {
const ev = [];
if (s.over) return ev;
for (const k in s.cool) if (s.cool[k] > 0) s.cool[k]--;
if (a && WEAPONS[a.w] && s.cool[a.w] === 0 && Number.isFinite(a.x) && Number.isFinite(a.y)) {
const wp = WEAPONS[a.w];
s.cool[a.w] = wp.cd;
ev.push({ e: 'swing', w: a.w, x: a.x, y: a.y });
if (a.w === 'hammer') {
for (const b of B) if (!b.dead && hit(b, a.x, a.y, wp.r)) damage(b, wp.dmg, 'hammer', ev);
} else {
// the topmost (last drawn, smallest) live block under the point
let best = null;
for (const b of B) if (!b.dead && hit(b, a.x, a.y, 0) && (!best || b.w * b.h <= best.w * best.h)) best = b;
if (best) {
if (a.w === 'fire') { if (!best.burn) ev.push({ e: 'ignite', id: best.id }); best.burn = BURN_FOR; damage(best, wp.dmg, 'fire', ev); }
else damage(best, wp.dmg, 'kick', ev);
}
}
}
// fire: burning blocks lose 1 hp every BURN_EVERY ticks and set touching blocks alight
if (s.t % BURN_EVERY === 0) {
const lit = B.filter((b) => !b.dead && b.burn > 0);
for (const b of lit) {
damage(b, 1, 'fire', ev);
for (const o of B) if (!o.dead && !o.burn && o !== b && touching(b, o, SPREAD_GAP)) { o.burn = BURN_FOR; ev.push({ e: 'ignite', id: o.id }); }
}
}
for (const b of B) if (b.burn > 0) b.burn--;
s.t++;
if (s.kills === s.total) {
const bonus = 500 + (Number.isFinite(limit) ? Math.floor((limit - s.t) / TPS) * 20 : 0);
s.score += bonus; s.over = true; ev.push({ e: 'clear', bonus });
} else if (s.t >= limit) { s.over = true; ev.push({ e: 'time' }); }
return ev;
};
s.percent = () => Math.round((100 * s.kills) / Math.max(1, s.total));
return s;
}
/**
* Replay a daily run for the leaderboard. moves = [[tick, weaponIndex, x, y], ...] with ticks strictly rising.
* Returns { ok, score, kills, total, percent }. A move during a weapon's cool-down makes the run invalid.
*/
export const WEAPON_ORDER = ['kick', 'hammer', 'fire'];
export function simulate(day, moves) {
const bad = { ok: false, score: 0, kills: 0, total: 0, percent: 0 };
if (!/^\d{4}-\d{2}-\d{2}$/.test(String(day)) || !Array.isArray(moves) || moves.length > ROUND) return bad;
const s = makeRound(dailyPage(day).blocks, ROUND);
let k = 0, prev = -1;
for (const m of moves) {
if (!Array.isArray(m) || m.length !== 4) return bad;
const [t, wi, x, y] = m;
if (!Number.isInteger(t) || t <= prev || t < 0 || t >= ROUND || !WEAPON_ORDER[wi] || !Number.isInteger(x) || !Number.isInteger(y) || x < 0 || x > W || y < 0 || y > H) return bad;
prev = t;
}
while (!s.over) {
let a = null;
if (k < moves.length && moves[k][0] === s.t) {
const [, wi, x, y] = moves[k++];
a = { w: WEAPON_ORDER[wi], x, y };
}
const ev = s.step(a);
if (a && !ev.some((e) => e.e === 'swing')) return bad; // swung during a cool-down: not a real run
}
if (k < moves.length) return bad;
return { ok: true, score: s.score, kills: s.kills, total: s.total, percent: s.percent() };
}
/** Emoji result grid for sharing: 10 cells, each one tenth of the page destroyed. */
export function grid(percent) {
const n = Math.round(percent / 10);
return '🟥'.repeat(n) + '⬜'.repeat(10 - n);
}