Interface for an upgrade.

interface IUpgradeStatic {
    bounds?: ((currency: Decimal, start: Decimal, end: Decimal) => [min: Decimal, max: Decimal]);
    cost: ((level: Decimal) => Decimal);
    costBulk?: ((currencyValue: Decimal, level: Decimal, target: Decimal) => [amount: Decimal, cost: Decimal]);
    defaultLevel: Decimal;
    description: string;
    effect?: ((level: Decimal, upgradeContext: UpgradeStatic, currencyContext: CurrencyStatic<[], string, [], string>) => void);
    el?: boolean | (() => boolean);
    id: string;
    maxLevel?: Decimal;
    name: string;
}

Hierarchy

Implemented by

Properties

bounds?: ((currency: Decimal, start: Decimal, end: Decimal) => [min: Decimal, max: Decimal])

A function to provide the bounds of what level could be given currency. By default, the lower bound is 0 and the upper bound is currency. This can make calculations extremely inaccurate at higher currencies.

For example, if you have a currency that is 1e100, and you can only buy 102 upgrades with it, the function first has to binary search from 1e50, then to 1e25, then to 3.16e12, etc.

By providing a bounds function, you can make the calculations more accurate. For example, if you can only buy 102 upgrades with 1e100 currency, you can provide a bounds function that returns something like [0, 1000].

It should satisfy the following for all currency >= 0 (and where cost' is the inverse of the cost function):

  • 0 <= min < cost'(currency) < max < currency

Basically, the bounds function should include the interval where the inverse cost function is within that interval, but the max bound should grow slower than the currency (y=x), for accurate calculations.

Type declaration

    • (currency, start, end): [min: Decimal, max: Decimal]
    • Parameters

      • currency: Decimal

        The currency value.

      • start: Decimal

        The starting level of the upgrade.

      • end: Decimal

        The ending level or quantity to reach for the upgrade.

      Returns [min: Decimal, max: Decimal]

      [min, max] - The minimum and maximum level that can be bought with the currency.

// Given a cost function,
const costFn = (n) => n.pow(2);
// the bounds function should be something like:
const boundsFn = (currency) => [new Decimal(0), currency.pow(0.75)];

// This is because the inverse of the cost function is n = sqrt(cost),
// and the maximum level that can be bought with the currency is the square root of the currency.
// So the bounds grows faster (y=x^0.75) than the inverse (y=x^0.5), but still slower than the currency (y=x).
cost: ((level: Decimal) => Decimal)

The cost of upgrades at a certain level. This function should evaluate to a non-negative number, and should be deterministic and continuous for all levels above 0. Also, if you do not set your own costBulk function, the function should always be greater than the level.

Type declaration

    • (level): Decimal
    • Parameters

      • level: Decimal

        The CURRENT (not next) level of the upgrade. It will always be a positive integer.

      Returns Decimal

      The cost of the upgrade. It should be a non-negative integer greater than or equal to 0.

// A cost function that returns twice the level.
(level) => level.mul(2)
costBulk?: ((currencyValue: Decimal, level: Decimal, target: Decimal) => [amount: Decimal, cost: Decimal])

The cost of buying a bulk of upgrades at a certain level. (inverse of cost function). EL is automatically applied to the cost. WARNING: In v8.x.x and above, the return order is [amount, cost] instead of [cost, amount].

Type declaration

    • (currencyValue, level, target): [amount: Decimal, cost: Decimal]
    • Parameters

      • currencyValue: Decimal
      • level: Decimal

        The current level of the upgrade.

      • target: Decimal

        The target level of the upgrade. If you want to buy the maximum amount of upgrades possible, this will be Infinity.

      Returns [amount: Decimal, cost: Decimal]

      [amount, cost] - The cost of the upgrades and the amount of upgrades you can buy. If you can't afford any, it returns [Decimal.dZero, Decimal.dZero].

// A cost function that returns the sum of the levels and the target.
// In this example, the cost function is twice the level. The cost bulk function is the sum of the levels and the target.
// -target^2 + target + level^2 + level
(level, target) => target.pow(2).mul(-1).add(target).add(level.pow(2)).add(level)
defaultLevel: Decimal
description: string

The description of the upgrade. Can be a string or a function that returns a string.

The current level of the upgrade.

The upgrade object that the description is being run on.

The currency static class that the upgrade is being run on.

// A dynamic description that returns a string
const description = (level) => `This upgrade is at level ${level}`;

// ... create upgrade here (see currencyStatic.addUpgrade)

const upgrade = currencyStatic.getUpgrade("upgradeID");

// Buy 1 level of the upgrade
currencyStatic.buyUpgrade("upgradeID", 1);

// Getter property
console.log(upgrade.description); // "This upgrade is at level 1"
effect?: ((level: Decimal, upgradeContext: UpgradeStatic, currencyContext: CurrencyStatic<[], string, [], string>) => void)

The effect of the upgrade. This runs when the upgrade is bought, and instantly if runEffectInstantly is true.

Type declaration

    • (level, upgradeContext, currencyContext): void
    • Parameters

      • level: Decimal

        The current level of the upgrade.

      • upgradeContext: UpgradeStatic

        The upgrade object that the effect is being run on.

      • currencyContext: CurrencyStatic<[], string, [], string>

        The currency static class that the upgrade is being run on.

      Returns void

el?: boolean | (() => boolean)

Endless / Everlasting: Flag to exclude the sum calculation and only perform binary search. Note: A function value is also allowed, and will be evaluated when the upgrade is bought or calculated. (but you should use a getter function instead)

id: string

The ID of the upgrade. Used to retrieve the upgrade later.

maxLevel?: Decimal

The maximum level of the upgrade. Warning: If not set, the upgrade will not have a maximum level and can continue to increase indefinitely.

name: string

The name of the upgrade. Defaults to the ID.