33 lines
1.4 KiB
JavaScript
33 lines
1.4 KiB
JavaScript
// Endless upgrades: once a family of upgrades is complete (its `requires`
|
|
// milestone is owned), it continues forever as a repeatable line. Every level
|
|
// multiplies the effect once more and costs exponentially more, up to
|
|
// `maxLevel` levels, which nobody will ever reach.
|
|
|
|
import { bulkCost, maxAffordable } from './formulas.js';
|
|
|
|
export const lineLevel = (state, id) => state.endless[id] ?? 0;
|
|
|
|
export function lineAvailable(state, line) {
|
|
return !line.requires || state.upgrades.includes(line.requires);
|
|
}
|
|
|
|
/** How many levels `amount` ("max" or a number) buys right now, and what they cost. */
|
|
export function endlessQuote(state, ctx, id, amount) {
|
|
const line = ctx.endlessById.get(id);
|
|
const level = lineLevel(state, id);
|
|
const wanted =
|
|
amount === 'max' ? maxAffordable(line.baseCost, line.costGrowth, level, state.funds) : amount;
|
|
const count = Math.max(0, Math.min(wanted, line.maxLevel - level));
|
|
return { count, cost: bulkCost(line.baseCost, line.costGrowth, level, count) };
|
|
}
|
|
|
|
export function buyEndless(state, ctx, id, amount = 1) {
|
|
const line = ctx.endlessById.get(id);
|
|
if (!line || !lineAvailable(state, line)) return false;
|
|
const { count, cost } = endlessQuote(state, ctx, id, amount);
|
|
if (count <= 0 || !(state.funds >= cost)) return false; // an infinite cost is never affordable
|
|
state.funds -= cost;
|
|
state.endless[id] = lineLevel(state, id) + count;
|
|
return true;
|
|
}
|