Compare commits

..

2 Commits

Author SHA1 Message Date
Alexander Whitestone
3aa1c36c88 beacon: add buy mode toggle (x1/x10/MAX) with B keyboard shortcut
The game had buyMode logic (1, 10, -1 for max) but zero UI to switch
between them. Players could only buy one building at a time.

Changes:
- Added x1/x10/MAX toggle buttons above the BUILDINGS section
- Active mode highlights in gold
- Press B to cycle through modes
- buyMode persists across saves
- Init log mentions the B shortcut
2026-04-10 02:21:19 -04:00
Alexander Whitestone
f362f30fd3 beacon: fix offline progress to award rescues, ops, and trust
Offline gains were missing rescues, ops, and trust — players with
beacon/mesh nodes lost half their progress on return. Now all active
resources are calculated at 50% offline efficiency.

Also includes event system overhaul: active events with durations,
resolve costs, auto-expiry, event UI rendering, and save/load support.
2026-04-10 01:21:05 -04:00
2 changed files with 215 additions and 22 deletions

229
game.js
View File

@@ -85,6 +85,7 @@ const G = {
tick: 0,
saveTimer: 0,
secTimer: 0,
buyMode: 1, // 1, 10, or -1 (max)
// Systems
projects: [],
@@ -106,6 +107,7 @@ const G = {
drift: 0,
lastEventAt: 0,
eventCooldown: 0,
activeEvents: [], // {id, expiresAt} — events auto-resolve after duration
// Time tracking
playTime: 0,
@@ -807,6 +809,42 @@ function getBuildingCost(id) {
return cost;
}
function getBuildingBatchCost(id, count) {
const def = BDEF.find(b => b.id === id);
if (!def || count <= 0) return {};
const currentCount = G.buildings[id] || 0;
const cost = {};
for (let i = 0; i < count; i++) {
for (const [resource, amount] of Object.entries(def.baseCost)) {
cost[resource] = (cost[resource] || 0) + Math.floor(amount * Math.pow(def.costMult, currentCount + i));
}
}
return cost;
}
function getMaxBuyable(id) {
const def = BDEF.find(b => b.id === id);
if (!def) return 0;
// Simulate buying one at a time until we can't afford
let count = 0;
const tempResources = {};
for (const r of Object.keys(def.baseCost)) tempResources[r] = G[r] || 0;
const currentCount = G.buildings[id] || 0;
for (let i = 0; i < 1000; i++) { // cap at 1000
let canAfford = true;
for (const [resource, amount] of Object.entries(def.baseCost)) {
const cost = Math.floor(amount * Math.pow(def.costMult, currentCount + i));
if (tempResources[resource] < cost) { canAfford = false; break; }
}
if (!canAfford) break;
for (const [resource, amount] of Object.entries(def.baseCost)) {
tempResources[resource] -= Math.floor(amount * Math.pow(def.costMult, currentCount + i));
}
count++;
}
return count;
}
function canAffordBuilding(id) {
const cost = getBuildingCost(id);
for (const [resource, amount] of Object.entries(cost)) {
@@ -815,6 +853,16 @@ function canAffordBuilding(id) {
return true;
}
function canAffordBatch(id) {
const count = G.buyMode === -1 ? getMaxBuyable(id) : G.buyMode;
if (count <= 0) return false;
const cost = getBuildingBatchCost(id, count);
for (const [resource, amount] of Object.entries(cost)) {
if ((G[resource] || 0) < amount) return false;
}
return true;
}
function spendBuilding(id) {
const cost = getBuildingCost(id);
for (const [resource, amount] of Object.entries(cost)) {
@@ -865,6 +913,7 @@ function updateRates() {
G.creativityRate += 0.5 + Math.max(0, G.totalUsers * 0.001);
}
if (G.pactFlag) G.trustRate += 2;
if (G.branchProtectionFlag) G.trustRate += 3; // branch protection actively builds trust
// Harmony: each wizard building contributes or detracts
const wizardCount = (G.buildings.bezalel || 0) + (G.buildings.allegro || 0) + (G.buildings.ezra || 0) +
@@ -966,6 +1015,20 @@ function tick() {
G.lastEventAt = G.tick;
}
// Check event expiry
checkEventExpiry();
// Re-apply active event rate penalties (updateRates rebuilds from scratch)
for (const ae of G.activeEvents) {
switch (ae.id) {
case 'runner_stuck': G.codeRate *= 0.5; break;
case 'ezra_offline': G.userRate *= 0.3; break;
case 'unreviewed_merge': /* trust penalty is one-shot */ break;
case 'api_rate_limit': G.computeRate *= 0.5; break;
case 'bilbo_vanished': G.creativityRate = 0; break;
}
}
// Drift ending: if drift reaches 100, the game ends
if (G.drift >= 100 && !G.driftEnding) {
G.driftEnding = true;
@@ -1029,15 +1092,26 @@ function checkProjects() {
function buyBuilding(id) {
const def = BDEF.find(b => b.id === id);
if (!def || !def.unlock()) return;
if (def.phase > G.phase + 1) return;
if (!canAffordBuilding(id)) return;
const buyCount = G.buyMode === -1 ? getMaxBuyable(id) : G.buyMode;
if (buyCount <= 0) return;
spendBuilding(id);
G.buildings[id] = (G.buildings[id] || 0) + 1;
if (buyCount === 1) {
if (!canAffordBuilding(id)) return;
spendBuilding(id);
G.buildings[id] = (G.buildings[id] || 0) + 1;
log(`Built ${def.name} (total: ${G.buildings[id]})`);
} else {
if (!canAffordBatch(id)) return;
const cost = getBuildingBatchCost(id, buyCount);
for (const [resource, amount] of Object.entries(cost)) {
G[resource] -= amount;
}
G.buildings[id] = (G.buildings[id] || 0) + buyCount;
log(`Built ${buyCount}x ${def.name} (total: ${G.buildings[id]})`);
}
updateRates();
log(`Built ${def.name} (total: ${G.buildings[id]})`);
render();
}
@@ -1113,46 +1187,49 @@ const EVENTS = [
{
id: 'runner_stuck',
title: 'CI Runner Stuck',
desc: 'The forge pipeline has halted. Production slows until restarted.',
desc: 'The forge pipeline has halted. Code production slowed.',
weight: () => (G.ciFlag === 1 ? 2 : 0),
duration: 45, // seconds
resolveCost: { ops: 10 },
effect: () => {
G.codeRate *= 0.5;
log('EVENT: CI runner stuck. Spend ops to clear the queue.', true);
log('EVENT: CI runner stuck. Click to spend 10 Ops to clear, or wait ~45s.', true);
}
},
{
id: 'ezra_offline',
title: 'Ezra is Offline',
desc: 'The herald channel is silent. User growth stalls.',
desc: 'The herald channel is silent. User growth stalled.',
weight: () => (G.buildings.ezra >= 1 ? 3 : 0),
duration: 60,
resolveCost: { ops: 5, trust: 2 },
effect: () => {
G.userRate *= 0.3;
log('EVENT: Ezra offline. Dispatch required.', true);
log('EVENT: Ezra offline. Click to dispatch, or wait ~60s.', true);
}
},
{
id: 'unreviewed_merge',
title: 'Unreviewed Merge',
desc: 'A change went in without eyes. Trust erodes.',
weight: () => (G.deployFlag === 1 ? 3 : 0),
weight: () => (G.deployFlag === 1 && G.branchProtectionFlag !== 1 ? 3 : 0),
duration: 30,
resolveCost: { ops: 8 },
effect: () => {
if (G.branchProtectionFlag === 1) {
log('EVENT: Unreviewed merge attempt blocked by Branch Protection.', true);
G.trust += 2;
} else {
G.trust = Math.max(0, G.trust - 10);
log('EVENT: Unreviewed merge detected. Trust lost.', true);
}
G.trust = Math.max(0, G.trust - 10);
log('EVENT: Unreviewed merge detected. Trust lost. Click to revert.', true);
}
},
{
id: 'api_rate_limit',
title: 'API Rate Limit',
desc: 'External compute provider throttled.',
weight: () => (G.totalCompute >= 1000 ? 2 : 0),
weight: () => (G.totalCompute >= 1000 && G.sovereignFlag !== 1 ? 2 : 0),
duration: 40,
resolveCost: { compute: 200 },
effect: () => {
G.computeRate *= 0.5;
log('EVENT: API rate limit hit. Local compute insufficient.', true);
log('EVENT: API rate limit. Click to provision local compute, or wait ~40s.', true);
}
},
{
@@ -1160,6 +1237,7 @@ const EVENTS = [
title: 'The Drift',
desc: 'An optimization suggests removing the human override. +40% efficiency.',
weight: () => (G.totalImpact >= 10000 ? 2 : 0),
duration: 0, // alignment events don't auto-resolve
effect: () => {
log('ALIGNMENT EVENT: Remove human override for +40% efficiency?', true);
G.pendingAlignment = true;
@@ -1170,9 +1248,11 @@ const EVENTS = [
title: 'Bilbo Vanished',
desc: 'The wildcard building has gone dark.',
weight: () => (G.buildings.bilbo >= 1 ? 2 : 0),
duration: 50,
resolveCost: { trust: 3 },
effect: () => {
G.creativityRate = 0;
log('EVENT: Bilbo has vanished. Creativity halts.', true);
log('EVENT: Bilbo vanished. Click to search, or wait ~50s.', true);
}
}
];
@@ -1186,12 +1266,53 @@ function triggerEvent() {
for (const ev of available) {
roll -= ev.weight();
if (roll <= 0) {
// Don't fire duplicate active events
if (G.activeEvents.some(ae => ae.id === ev.id)) return;
ev.effect();
if (ev.duration > 0) {
G.activeEvents.push({ id: ev.id, expiresAt: G.tick + ev.duration });
}
return;
}
}
}
function checkEventExpiry() {
for (let i = G.activeEvents.length - 1; i >= 0; i--) {
if (G.tick >= G.activeEvents[i].expiresAt) {
const ae = G.activeEvents[i];
G.activeEvents.splice(i, 1);
updateRates(); // recalculate without the penalty
const evDef = EVENTS.find(e => e.id === ae.id);
log(`Event resolved: ${evDef ? evDef.title : ae.id}`, true);
}
}
}
function hasActiveEvent(id) {
return G.activeEvents.some(ae => ae.id === id);
}
function resolveEvent(id) {
const evDef = EVENTS.find(e => e.id === id);
if (!evDef || !evDef.resolveCost) return;
if (!hasActiveEvent(id)) return;
if (!canAffordProject(evDef)) {
log('Not enough resources to resolve this event.');
return;
}
spendProject(evDef);
G.activeEvents = G.activeEvents.filter(ae => ae.id !== id);
updateRates();
// Small bonus for manually resolving
G.trust += 2;
log(`Resolved: ${evDef.title}. Trust +2.`, true);
render();
}
function resolveAlignment(accept) {
if (!G.pendingAlignment) return;
if (accept) {
@@ -1210,6 +1331,15 @@ function resolveAlignment(accept) {
}
// === ACTIONS ===
function setBuyMode(mode) {
G.buyMode = mode;
// Update active button highlight
document.querySelectorAll('.buy-mode-btn').forEach(btn => {
btn.classList.toggle('active', parseInt(btn.dataset.mode) === mode);
});
renderBuildings();
}
function writeCode() {
const base = 1;
const bonus = Math.floor(G.buildings.autocoder * 0.5);
@@ -1433,6 +1563,7 @@ function render() {
renderStats();
updateEducation();
renderAlignment();
renderActiveEvents();
checkUnlocks();
}
@@ -1457,6 +1588,40 @@ function renderAlignment() {
}
}
function renderActiveEvents() {
const container = document.getElementById('events-ui');
if (!container) return;
if (G.activeEvents.length === 0) {
container.innerHTML = '';
container.style.display = 'none';
return;
}
let html = '';
for (const ae of G.activeEvents) {
const evDef = EVENTS.find(e => e.id === ae.id);
if (!evDef) continue;
const remaining = Math.max(0, Math.ceil(ae.expiresAt - G.tick));
const costStr = evDef.resolveCost ? Object.entries(evDef.resolveCost).map(([r, a]) => `${a} ${r}`).join(', ') : '';
const canResolve = evDef.resolveCost && canAffordProject(evDef);
html += `<div style="background:#1a1008;border:1px solid #ff8800;padding:8px;border-radius:4px;margin-bottom:6px">`;
html += `<div style="display:flex;justify-content:space-between;align-items:center">`;
html += `<span style="color:#ff8800;font-weight:bold;font-size:10px">${evDef.title}</span>`;
html += `<span style="color:#666;font-size:9px">${remaining}s</span>`;
html += `</div>`;
html += `<div style="font-size:9px;color:#888;margin:3px 0">${evDef.desc}</div>`;
if (evDef.resolveCost) {
html += `<button class="ops-btn" onclick="resolveEvent('${evDef.id}')" ${canResolve ? '' : 'disabled'} style="font-size:9px;padding:3px 8px;margin-top:2px;border-color:#ff8800;color:#ff8800">Resolve (${costStr})</button>`;
}
html += `</div>`;
}
container.innerHTML = html;
container.style.display = 'block';
}
// === UNLOCK NOTIFICATIONS ===
function showUnlockToast(type, name) {
const container = document.getElementById('unlock-toast');
@@ -1545,10 +1710,11 @@ function saveGame() {
branchProtectionFlag: G.branchProtectionFlag || 0, nightlyWatchFlag: G.nightlyWatchFlag || 0,
nostrFlag: G.nostrFlag || 0,
milestones: G.milestones, completedProjects: G.completedProjects, activeProjects: G.activeProjects,
totalClicks: G.totalClicks, startedAt: G.startedAt,
totalClicks: G.totalClicks, startedAt: G.startedAt, buyMode: G.buyMode,
flags: G.flags,
_seenBuildings: G._seenBuildings || [],
_seenProjects: G._seenProjects || [],
activeEvents: G.activeEvents || [],
rescues: G.rescues || 0, totalRescues: G.totalRescues || 0,
drift: G.drift || 0, driftEnding: G.driftEnding || false, beaconEnding: G.beaconEnding || false, pendingAlignment: G.pendingAlignment || false,
lastEventAt: G.lastEventAt || 0,
@@ -1581,12 +1747,21 @@ function loadGame() {
const uc = G.userRate * offSec * f;
const ic = G.impactRate * offSec * f;
const rc = G.rescuesRate * offSec * f;
const oc = G.opsRate * offSec * f;
const tc = G.trustRate * offSec * f;
G.code += gc; G.compute += cc; G.knowledge += kc;
G.users += uc; G.impact += ic;
G.rescues += rc; G.ops += oc; G.trust += tc;
G.totalCode += gc; G.totalCompute += cc; G.totalKnowledge += kc;
G.totalUsers += uc; G.totalImpact += ic;
G.totalRescues += rc;
log(`Welcome back! While away (${Math.floor(offSec / 60)}m): ${fmt(gc)} code, ${fmt(kc)} knowledge, ${fmt(uc)} users`);
const parts = [`${fmt(gc)} code`, `${fmt(kc)} knowledge`, `${fmt(uc)} users`];
if (rc > 0.1) parts.push(`${fmt(rc)} rescues`);
if (oc > 0.1) parts.push(`${fmt(oc)} ops`);
log(`Welcome back! While away (${Math.floor(offSec / 60)}m): ${parts.join(', ')}`);
}
}
@@ -1611,6 +1786,7 @@ function initGame() {
log('The screen is blank. Write your first line of code.', true);
log('Click WRITE CODE or press SPACE to start.');
log('Press B to toggle buy mode (x1 / x10 / MAX).');
log('Build AutoCode for passive production.');
log('Watch for Research Projects to appear.');
}
@@ -1632,6 +1808,9 @@ window.addEventListener('load', function () {
}
}
// Restore buy mode button highlight
setBuyMode(G.buyMode || 1);
// Game loop at 10Hz (100ms tick)
setInterval(tick, 100);
@@ -1648,4 +1827,10 @@ window.addEventListener('keydown', function (e) {
e.preventDefault();
writeCode();
}
if (e.code === 'KeyB' && e.target === document.body) {
e.preventDefault();
// Cycle: x1 -> x10 -> MAX -> x1
const next = G.buyMode === 1 ? 10 : G.buyMode === 10 ? -1 : 1;
setBuyMode(next);
}
});

View File

@@ -66,6 +66,8 @@ body{background:var(--bg);color:var(--text);font-family:'SF Mono','Cascadia Code
.unlock-toast-item.building{border-color:#4a9eff;color:#4a9eff}
.unlock-toast-item.project{border-color:#ffd700;color:#ffd700}
.unlock-toast-item.milestone{border-color:#4caf50;color:#4caf50}
.buy-mode-btn{min-width:36px}
.buy-mode-btn.active{border-color:#ffd700!important;color:#ffd700!important;background:#1a1a08!important}
</style>
</head>
<body>
@@ -102,6 +104,12 @@ body{background:var(--bg);color:var(--text);font-family:'SF Mono','Cascadia Code
<button class="ops-btn" onclick="doOps('boost_trust')">Ops -&gt; Trust</button>
</div>
<div id="alignment-ui" style="display:none"></div>
<div id="events-ui" style="display:none"></div>
<div id="buy-mode-toggle" style="display:flex;gap:4px;margin-bottom:8px">
<button class="ops-btn buy-mode-btn active" onclick="setBuyMode(1)" data-mode="1">x1</button>
<button class="ops-btn buy-mode-btn" onclick="setBuyMode(10)" data-mode="10">x10</button>
<button class="ops-btn buy-mode-btn" onclick="setBuyMode(-1)" data-mode="-1">MAX</button>
</div>
<button class="save-btn" onclick="saveGame()">Save Game</button>
<button class="reset-btn" onclick="if(confirm('Reset all progress?')){localStorage.removeItem('the-beacon-v2');location.reload()}">Reset Progress</button>
<h2>BUILDINGS</h2>