Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUGFIX: Wrong calculation in team casualties of Bladeburner action #1672

Conversation

catloversg
Copy link
Contributor

There are at least 2 regression bugs caused by the refactor in #1654.

Bug 1:

resolveTeamCasualties does not short-circuit when teamCount is 0.

With BlackOps, minCasualties is 1. If teamCount is 0, worstCase is 0. When we call team.getTeamCasualtiesRoll(action.getMinimumCasualties(), worstCase), it will call getRandomIntInclusive(1, 0). getRandomIntInclusive only accepts parameters in the form of (min, max).

Exception

Bug 2:

humans is not "clamped" properly. It should be const humans = Math.max(action.teamCount - team.sleeveSize, 0);.
If humans becomes negative, all following calculations are wrong.
For example: TeamSize = 3; SleeveSize = 3; TeamCount = 0; deaths = 1. In this case:

  • humans is -3.
  • humanDeaths is -3.
  • team.killRandomSupportingSleeves is called with 1 - (-3) = 4.
  • team.teamSize = Math.max(3 - (-3), 3) = 6;

These bugs were introduced because resolveTeamCasualties does not reuse the old logic:

  • It does not short-circuit when teamCount is 0.
  • It introduces the new logic of humans and humanDeaths.

This PR rewrites the entire resolveTeamCasualties and reuses the old logic.

For quick reference, this is how it was implemented:

Operation:

const teamCount = action.teamCount;
if (teamCount >= 1) {
  const maxLosses = success ? Math.ceil(teamCount / 2) : Math.floor(teamCount);
  const losses = getRandomIntInclusive(0, maxLosses);
  this.teamSize -= losses;
  if (this.teamSize < this.sleeveSize) {
    const sup = Player.sleeves.filter((x) => isSleeveSupportWork(x.currentWork));
    for (let i = 0; i > this.teamSize - this.sleeveSize; i--) {
      const r = Math.floor(Math.random() * sup.length);
      sup[r].takeDamage(sup[r].hp.max);
      sup.splice(r, 1);
    }
    // If this happens, all team members died and some sleeves took damage. In this case, teamSize = sleeveSize.
    this.teamSize = this.sleeveSize;
  }
  this.teamLost += losses;
  if (this.logging.ops && losses > 0) {
    this.log("Lost " + formatNumberNoSuffix(losses, 0) + " team members during this " + action.name);
  }
}

Black Operation:

if (teamCount >= 1) {
  const losses = getRandomIntInclusive(1, teamLossMax);
  this.teamSize -= losses;
  if (this.teamSize < this.sleeveSize) {
    const sup = Player.sleeves.filter((x) => isSleeveSupportWork(x.currentWork));
    for (let i = 0; i > this.teamSize - this.sleeveSize; i--) {
      const r = Math.floor(Math.random() * sup.length);
      sup[r].takeDamage(sup[r].hp.max);
      sup.splice(r, 1);
    }
    // If this happens, all team members died and some sleeves took damage. In this case, teamSize = sleeveSize.
    this.teamSize = this.sleeveSize;
  }
  this.teamLost += losses;
  if (this.logging.blackops) {
    this.log(
      `${person.whoAmI()}:  You lost ${formatNumberNoSuffix(losses, 0)} team members during ${action.name}.`,
    );
  }
}

@d0sboots d0sboots merged commit cace34d into bitburner-official:dev Oct 8, 2024
5 checks passed
@catloversg catloversg deleted the pull-request/bugfix/wrong-calculation-in-team-casualties-of-bladeburner-action branch October 8, 2024 07:49
antoinedube pushed a commit to antoinedube/bitburner-source that referenced this pull request Oct 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants