alainbryden / bitburner-scripts

My own scripts for playing bitburner
MIT License
608 stars 272 forks source link

helpers.js: Measure rep gains since last tick #395

Open alainbryden opened 1 month ago

alainbryden commented 1 month ago

In order to help with issues like #353 and #388, we need a consistent way of measuring current gain rates that any script can use.

While there are formulas APIs that let us get expected gain rates, there will not always be available, and it would be nice to have something that works for new players from the very start (before they have the SF that auto-grants formulas.exe). It would also be nice not to have to pay the price of formulas.exe and/or resort to ram-dodging, at least for now.

One idea I had was to take the "measureRepGainRate" function that work-for-factions.js uses, and expand on it:

/** Measure our rep gain rate (per second)
 * TODO: Move this to helpers.js, measure all rep gain rates over a parameterizable number of game ticks (default 1) and return them all.
 * @param {NS} ns
 * @param {() => Promise<number>} fnSampleReputation - An async function that samples the reputation at a current point in time */
async function measureRepGainRate(ns, fnSampleReputation) {
    //return (await getPlayerInfo(ns)).workRepGainRate;
    // The game no longer provides the rep gain rate for a given work type, so we must measure it
    const initialReputation = await fnSampleReputation();
    let nextTickReputation;
    let start = Date.now();
    while (initialReputation == (nextTickReputation = await fnSampleReputation()) && Date.now() - start < 450)
        await ns.sleep(50);
    return (nextTickReputation - initialReputation) * 5; // Assume this rep gain was for a 200 tick
}
/** Measure our faction rep gain rate (per second)
 * @param {NS} ns */
async function measureFactionRepGainRate(ns, factionName) {
    return await measureRepGainRate(ns, async () => await getFactionReputation(ns, factionName));
}
/** Measure our company rep gain rate (per second)
 * @param {NS} ns */
async function measureCompanyRepGainRate(ns, companyName) {
    return await measureRepGainRate(ns, async () => await getCompanyReputation(ns, companyName));
}

Currently, it supports getting faction or company reputation, but we could have other variations that measure changes in all player stats between ticks, and also get a more "stable" view of gains by measuring over several ticks and returning only the smallest gain for each stat (removing noise from occasional sporadic earnings)