Refactored the internal calculateCost function for readability

This commit is contained in:
Undeemiss 2022-05-29 11:35:28 -05:00
parent 06acc423ae
commit a0bff34761

@ -134,16 +134,29 @@ export class Skill {
} }
calculateCost(currentLevel: number, count = 1): number { calculateCost(currentLevel: number, count = 1): number {
if (count == 1) { //Recursive mode does not handle invalid inputs properly, but it should never
return Math.floor((this.baseCost + currentLevel * this.costInc) * BitNodeMultipliers.BladeburnerSkillCost); //be possible for it to run with them. For the sake of not crashing the game,
} else if (count < 0 || isNaN(count) || count == Infinity || count % 1 != 0) { const recursiveMode = (currentLevel: number, count: number): number => {
if (count <= 1) {
return Math.floor((this.baseCost + currentLevel * this.costInc) * BitNodeMultipliers.BladeburnerSkillCost);
} else {
const thisUpgrade = Math.floor(
(this.baseCost + currentLevel * this.costInc) * BitNodeMultipliers.BladeburnerSkillCost,
);
return this.calculateCost(currentLevel + 1, count - 1) + thisUpgrade;
}
};
//Count must be a positive integer.
if (count < 0 || count % 1 != 0) {
throw new Error(`${count} is an invalid number of upgrades`); throw new Error(`${count} is an invalid number of upgrades`);
} else if (count < 100) { }
const thisUpgrade = Math.floor( //Use recursive mode if count is small
(this.baseCost + currentLevel * this.costInc) * BitNodeMultipliers.BladeburnerSkillCost, if (count <= 100) {
); return recursiveMode(currentLevel, count);
return this.calculateCost(currentLevel + 1, count - 1) + thisUpgrade; }
} else { //Use optimized mode if count is large
else {
//unFloored is roughly equivalent to //unFloored is roughly equivalent to
//(this.baseCost + currentLevel * this.costInc) * BitNodeMultipliers.BladeburnerSkillCost //(this.baseCost + currentLevel * this.costInc) * BitNodeMultipliers.BladeburnerSkillCost
//being repeated for increasing currentLevel //being repeated for increasing currentLevel