// Events from the simulation (emit) turned into sounds, toasts and cards. // ---------- Events from the simulation ---------- function drainEvents() { let n = 0; while (EV.length && n++ < 300) { const e = EV.shift(); if (e.t === 'discover') { discoveryCard(e); playSfx('discover'); if (!$('m-notebook').hidden) renderNotebook(); } else if (e.t === 'milestone') { const m = MILESTONES.find(x => x.id === e.id); if (m && !['bit1', 'bit2', 'bit3', 'tank'].includes(m.id)) { showCard({ kind: 'Milestone', title: m.title, body: [m.msg, m.reward], accent: 'milestone' }); playSfx('milestone'); } } else if (e.t === 'cast_ready') { playSfx('cast', undefined, e.x, e.y); } else if (e.t === 'upgrade') { playSfx('upgrade'); if (SET.shake) R.shake = 1.4; showCard({ kind: e.kind === 'bit' ? 'Drill bit upgraded' : 'Bag upgraded', title: e.kind === 'bit' ? BIT_TIERS[e.tier].name : TANK_TIERS[e.tier].name, accent: 'milestone', body: [e.kind === 'bit' ? `Digs hardness ${BIT_TIERS[e.tier].hard} and below.` : `Holds ${TANK_TIERS[e.tier].cap.toLocaleString('en-US')} grains.`] }); hudDirty = true; } else if (e.t === 'debris') spawnDebris(e.x, e.y, e.m); else if (e.t === 'suck') spawnSuck(e.x, e.y, e.m); else if (e.t === 'warn') spawnFloater(e.x, e.y, e.text, '#ff8a72'); else if (e.t === 'sold') spawnFloater(e.x, e.y, '+' + fmtMoney(e.v)); else if (e.t === 'order_new') { const o = e.o; showCard({ kind: 'New order', title: orderName(o), swatch: o.m, body: [`Within ${o.mins} minutes, for ${fmtMoney(o.pay)}: ${o.mult}× the market price.`, 'Goods dropped into the trade dock fill it first.'] }); playSfx('discover'); hudDirty = true; } else if (e.t === 'order_done') { showCard({ kind: 'Order filled', title: '+' + fmtMoney(e.pay), swatch: e.m, accent: 'milestone', body: [`${orderName(e)} delivered.`, debt > 0 ? `${fmtMoney(debt)} still owed on the claim.` : ''] }); playSfx('sale'); hudDirty = true; if (!$('m-notebook').hidden) renderNotebook(); } else if (e.t === 'order_expired') { showCard({ kind: 'Order expired', title: orderName(e), swatch: e.m, body: [e.got ? `You delivered ${e.got} of ${e.qty}. They were paid at market price: ${fmtMoney(e.paid)}.` : 'Nothing was delivered. No harm done.'] }); hudDirty = true; } else if (e.t === 'claim_paid') { showCard({ kind: 'Claim', title: 'The land is yours', accent: 'milestone', hold: 1.5, body: ['You have paid off the claim on this land.', 'Keep filling orders: bigger buyers bring bigger ones. The ledger has bigger goals too.'] }); playSfx('milestone'); } else if (e.t === 'sprout') { showCard({ kind: 'Discovery', title: 'A sapling', swatch: ACORN, body: ['An acorn on soil under open sky has sprouted.', 'It grows into a tree in about a minute and a half. Keep the space above it clear.'] }); playSfx('discover'); } else if (e.t === 'sfx') { if (e.s) playSfx(e.s, e.a, e.x, e.y); } else if (e.t === 'toast') { if (e.text) toast(e.text); } } }