Skip to content

Commit

Permalink
use best random function #2154
Browse files Browse the repository at this point in the history
use best random function #2154
  • Loading branch information
BastLast committed Nov 30, 2023
1 parent c5bed1d commit 4289830
Show file tree
Hide file tree
Showing 16 changed files with 35 additions and 17 deletions.
3 changes: 2 additions & 1 deletion src/core/fights/actions/interfaces/alterations/burned.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ import {format} from "../../../../utils/StringFormatter";
import {FightActionController} from "../../FightActionController";
import {attackInfo, statsInfo} from "../../FightAction";
import {FightAlteration} from "../../FightAlteration";
import {RandomUtils} from "../../../../utils/RandomUtils";

export default class BurnedAlteration extends FightAlteration {
use(victim: Fighter, sender: Fighter, turn: number, language: string): string {
victim.alterationTurn++;
const burnedTranslationModule = Translations.getModule(`fightactions.${this.name}`, language);
// 60 % chance to be healed from the poison (except for the first two turns)
if (Math.random() < 0.6 && victim.alterationTurn > 1) {
if (RandomUtils.draftbotRandom.realZeroToOneInclusive() < 0.6 && victim.alterationTurn > 1) {
victim.removeAlteration();
return burnedTranslationModule.get("heal");
}
Expand Down
3 changes: 2 additions & 1 deletion src/core/fights/actions/interfaces/alterations/confused.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ import {Translations} from "../../../../Translations";
import {FightActionController} from "../../FightActionController";
import {attackInfo, FightAction, statsInfo} from "../../FightAction";
import {FightActions} from "../../FightActions";
import {RandomUtils} from "../../../../utils/RandomUtils";

export default class ConfusedAlteration extends FightAction {
use(sender: Fighter, receiver: Fighter, turn: number, language: string): string {
sender.alterationTurn++;
const randomValue = Math.random();
const randomValue = RandomUtils.draftbotRandom.realZeroToOneInclusive();

const confusionTranslationModule = Translations.getModule(`fightactions.${this.name}`, language);

Expand Down
3 changes: 2 additions & 1 deletion src/core/fights/actions/interfaces/alterations/cursed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ import {attackInfo, statsInfo} from "../../FightAction";
import {FightAlteration} from "../../FightAlteration";
import {MathUtils} from "../../../../utils/MathUtils";
import {FightConstants} from "../../../../constants/FightConstants";
import {RandomUtils} from "../../../../utils/RandomUtils";

export default class PoisonedAlteration extends FightAlteration {
use(victim: Fighter, sender: Fighter, turn: number, language: string): string {
victim.alterationTurn++;
const curseTranslationModule = Translations.getModule(`fightactions.${this.name}`, language);
// 50 % chance to be healed from the cursed (except for the first two turn) and 100 % after 5 turns of being cursed
if (Math.random() < 0.25 && victim.alterationTurn > 2 || victim.alterationTurn > 4) {
if (RandomUtils.draftbotRandom.realZeroToOneInclusive() < 0.25 && victim.alterationTurn > 2 || victim.alterationTurn > 4) {
victim.removeAlteration();
let damageDealt = FightActionController.getAttackDamage(this.getStatsInfo(victim, sender), victim, this.getAttackInfo(), true);
damageDealt += MathUtils.getIntervalValue(0, damageDealt * 2, (victim.alterationTurn - 2) / 3);
Expand Down
3 changes: 2 additions & 1 deletion src/core/fights/actions/interfaces/alterations/frozen.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import {Fighter, FightStatModifierOperation} from "../../../fighter/Fighter";
import {FightAlteration} from "../../FightAlteration";
import {Translations} from "../../../../Translations";
import {RandomUtils} from "../../../../utils/RandomUtils";

export default class FrozenAlteration extends FightAlteration {
use(victim: Fighter, sender: Fighter, turn: number, language: string): string {
victim.alterationTurn++;
const frozenTranslationModule = Translations.getModule(`fightactions.${this.name}`, language);
// 50% chance to be healed from the frozen (except for the first two turns)
if (Math.random() < 0.5 && victim.alterationTurn > 2) {
if (RandomUtils.draftbotRandom.realZeroToOneInclusive() < 0.5 && victim.alterationTurn > 2) {
victim.removeSpeedModifiers(this);
victim.removeAlteration();
return frozenTranslationModule.get("heal");
Expand Down
3 changes: 2 additions & 1 deletion src/core/fights/actions/interfaces/alterations/paralyzed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {Fighter, FightStatModifierOperation} from "../../../fighter/Fighter";
import {Translations} from "../../../../Translations";
import {FightActions} from "../../FightActions";
import {FightAlteration} from "../../FightAlteration";
import {RandomUtils} from "../../../../utils/RandomUtils";

export default class ParalyzedAlteration extends FightAlteration {
use(victim: Fighter, sender: Fighter, turn: number, language: string): string {
Expand All @@ -14,7 +15,7 @@ export default class ParalyzedAlteration extends FightAlteration {
}

// 20% chance to not attack this turn
if (Math.random() < 0.2) {
if (RandomUtils.draftbotRandom.realZeroToOneInclusive() < 0.2) {
victim.nextFightAction = FightActions.getNoAttack();
return paralyzedTranslationModule.get("noAttack");
}
Expand Down
3 changes: 2 additions & 1 deletion src/core/fights/actions/interfaces/alterations/stunned.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {Fighter} from "../../../fighter/Fighter";
import {Translations} from "../../../../Translations";
import {FightActions} from "../../FightActions";
import {FightAlteration} from "../../FightAlteration";
import {RandomUtils} from "../../../../utils/RandomUtils";

export default class StunnedAlteration extends FightAlteration {
use(victim: Fighter, sender: Fighter, turn: number, language: string): string {
Expand All @@ -13,7 +14,7 @@ export default class StunnedAlteration extends FightAlteration {
}

// 50% chance to not attack this turn
if (Math.random() < 0.5) {
if (RandomUtils.draftbotRandom.realZeroToOneInclusive() < 0.5) {
victim.nextFightAction = FightActions.getNoAttack();
return stunnedTranslationModule.get("noAttack");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {FightAlterations} from "../../FightAlterations";
import {FightConstants} from "../../../../constants/FightConstants";
import {Translations} from "../../../../Translations";
import {FightActionStatus} from "../../FightActionStatus";
import {RandomUtils} from "../../../../utils/RandomUtils";

export default class BoulderTossAttack extends FightAction {
use(sender: Fighter, receiver: Fighter, turn: number, language: string): string {
Expand All @@ -16,7 +17,7 @@ export default class BoulderTossAttack extends FightAction {
let sideEffects = "";

// 50% chance to stun the defender
if (this.getAttackStatus(damageDealt, initialDamage) !== FightActionStatus.MISSED && Math.random() < 0.5) {
if (this.getAttackStatus(damageDealt, initialDamage) !== FightActionStatus.MISSED && RandomUtils.draftbotRandom.realZeroToOneInclusive() < 0.5) {
const alteration = receiver.newAlteration(FightAlterations.STUNNED);
if (alteration === FightAlterations.STUNNED) {
sideEffects = attackTranslationModule.format("actions.sideEffects.newAlteration", {
Expand Down
3 changes: 2 additions & 1 deletion src/core/fights/actions/interfaces/players/canonAttack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {FightConstants} from "../../../../constants/FightConstants";
import {MathUtils} from "../../../../utils/MathUtils";
import {attackInfo, FightAction, statsInfo} from "../../FightAction";
import {FightAlterations} from "../../FightAlterations";
import {RandomUtils} from "../../../../utils/RandomUtils";

export default class CanonAttack extends FightAction {
use(sender: Fighter, receiver: Fighter, turn: number, language: string): string {
Expand All @@ -24,7 +25,7 @@ export default class CanonAttack extends FightAction {
let sideEffects = "";

// The receiver has a 65% chance to be slowed
if (Math.random() < 0.65) {
if (RandomUtils.draftbotRandom.realZeroToOneInclusive() < 0.65) {
const alteration = receiver.newAlteration(FightAlterations.SLOWED);
if (alteration === FightAlterations.SLOWED) {
sideEffects = attackTranslationModule.format("actions.sideEffects.newAlteration", {
Expand Down
3 changes: 2 additions & 1 deletion src/core/fights/actions/interfaces/players/divineAttack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {FightConstants} from "../../../../constants/FightConstants";
import {attackInfo, FightAction, statsInfo} from "../../FightAction";
import {FightAlterations} from "../../FightAlterations";
import Benediction from "./benediction";
import {RandomUtils} from "../../../../utils/RandomUtils";

export default class DivineAttack extends FightAction {
static getUsedGodMoves(sender: Fighter, receiver: Fighter): number {
Expand Down Expand Up @@ -51,7 +52,7 @@ export default class DivineAttack extends FightAction {

let sideEffects = "";

if (Math.random() < 0.2) {
if (RandomUtils.draftbotRandom.realZeroToOneInclusive() < 0.2) {
const alteration = receiver.newAlteration(FightAlterations.PARALYZED);
if (alteration === FightAlterations.PARALYZED) {
sideEffects = attackTranslationModule.format("actions.sideEffects.newAlteration", {
Expand Down
3 changes: 2 additions & 1 deletion src/core/fights/actions/interfaces/players/piercingAttack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {Translations} from "../../../../Translations";
import {FightActionController} from "../../FightActionController";
import {FightConstants} from "../../../../constants/FightConstants";
import {attackInfo, FightAction, statsInfo} from "../../FightAction";
import {RandomUtils} from "../../../../utils/RandomUtils";

export default class PiercingAttack extends FightAction {
use(sender: Fighter, receiver: Fighter, turn: number, language: string): string {
Expand All @@ -16,7 +17,7 @@ export default class PiercingAttack extends FightAction {


// 45% chance to lower the target's defense by 10%
if (Math.random() < 0.45) {
if (RandomUtils.draftbotRandom.realZeroToOneInclusive() < 0.45) {
const reductionAmont = 10;
receiver.applyDefenseModifier({
origin: this,
Expand Down
3 changes: 2 additions & 1 deletion src/core/fights/actions/interfaces/players/powerfulAttack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {FightActionController} from "../../FightActionController";
import {FightConstants} from "../../../../constants/FightConstants";
import {attackInfo, FightAction, statsInfo} from "../../FightAction";
import {FightAlterations} from "../../FightAlterations";
import {RandomUtils} from "../../../../utils/RandomUtils";

export default class PowerfulAttack extends FightAction {
use(sender: Fighter, receiver: Fighter, turn: number, language: string): string {
Expand All @@ -21,7 +22,7 @@ export default class PowerfulAttack extends FightAction {
let sideEffects = "";

// 20% chance to stun the sender and deal 50% more damage
if (Math.random() < 0.2) {
if (RandomUtils.draftbotRandom.realZeroToOneInclusive() < 0.2) {
const alteration = sender.newAlteration(FightAlterations.STUNNED);
if (alteration === FightAlterations.STUNNED) {
sideEffects = attackTranslationModule.format("actions.sideEffects.newAlteration", {
Expand Down
5 changes: 3 additions & 2 deletions src/core/fights/actions/interfaces/players/ramAttack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {FightActionController} from "../../FightActionController";
import {FightConstants} from "../../../../constants/FightConstants";
import {attackInfo, FightAction, statsInfo} from "../../FightAction";
import {FightAlterations} from "../../FightAlterations";
import {RandomUtils} from "../../../../utils/RandomUtils";

export default class RamAttack extends FightAction {
use(sender: Fighter, receiver: Fighter, turn: number, language: string): string {
Expand All @@ -15,7 +16,7 @@ export default class RamAttack extends FightAction {
let sideEffects = "";

// 70% chance to stun the defender
if (Math.random() < 0.70) {
if (RandomUtils.draftbotRandom.realZeroToOneInclusive() < 0.70) {
const alteration = receiver.newAlteration(FightAlterations.STUNNED);
if (alteration === FightAlterations.STUNNED) {
sideEffects = attackTranslationModule.format("actions.sideEffects.newAlteration", {
Expand All @@ -26,7 +27,7 @@ export default class RamAttack extends FightAction {
}

// Sender has a 25% chance to be stunned and 75% chance to be hurt by his own attack
if (Math.random() < 0.25) {
if (RandomUtils.draftbotRandom.realZeroToOneInclusive() < 0.25) {
const alteration = sender.newAlteration(FightAlterations.STUNNED);
if (alteration === FightAlterations.STUNNED) {
sideEffects += attackTranslationModule.format("actions.sideEffects.newAlteration", {
Expand Down
3 changes: 2 additions & 1 deletion src/core/fights/actions/interfaces/players/sabotageAttack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {attackInfo, FightAction, statsInfo} from "../../FightAction";
import {FightAlterations} from "../../FightAlterations";
import {FightConstants} from "../../../../constants/FightConstants";
import {Translations} from "../../../../Translations";
import {RandomUtils} from "../../../../utils/RandomUtils";

export default class SabotageAttack extends FightAction {
use(sender: Fighter, receiver: Fighter, turn: number, language: string): string {
Expand All @@ -14,7 +15,7 @@ export default class SabotageAttack extends FightAction {

let sideEffects = "";

if (Math.random() < 0.9) {
if (RandomUtils.draftbotRandom.realZeroToOneInclusive() < 0.9) {
const alteration = sender.newAlteration(FightAlterations.PARALYZED);
if (alteration === FightAlterations.PARALYZED) {
sideEffects = attackTranslationModule.format("actions.sideEffects.newAlteration", {
Expand Down
3 changes: 2 additions & 1 deletion src/core/smallEvents/fightPet/interfaces/prayGod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {FeralPet} from "../../../database/game/models/FeralPet";
import {SmallEventConstants} from "../../../constants/SmallEventConstants";
import {InventorySlots} from "../../../database/game/models/InventorySlot";
import {ItemConstants} from "../../../constants/ItemConstants";
import {RandomUtils} from "../../../utils/RandomUtils";

/**
* The player prays god to not be attacked by the feral pet
Expand All @@ -12,7 +13,7 @@ export default class PrayGod extends FightPetAction {

async applyOutcome(player: Player, feralPet: FeralPet): Promise<boolean> {
// This action has 10% times how many holy items the player has times the rarity of the pet
return Math.random() <
return RandomUtils.draftbotRandom.realZeroToOneInclusive() <
SmallEventConstants.FIGHT_PET.PRAYER_CHANCE * feralPet.originalPet.rarity *
await InventorySlots.countObjectsOfPlayer(player.id, ItemConstants.TAGS.HOLY)
+ SmallEventConstants.FIGHT_PET.HAS_AN_HOLY_ATTACK_CHANCE * (player.hasHolyClass() ? 1 : 0);
Expand Down
5 changes: 4 additions & 1 deletion src/core/smallEvents/fightPet/interfaces/provoke.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import Player from "../../../database/game/models/Player";
import {FeralPet} from "../../../database/game/models/FeralPet";
import {InventorySlots} from "../../../database/game/models/InventorySlot";
import {ItemConstants} from "../../../constants/ItemConstants";
import {RandomUtils} from "../../../utils/RandomUtils";

/**
* Provoke it
Expand All @@ -11,7 +12,9 @@ export default class Provoke extends FightPetAction {

public async applyOutcome(player: Player, feralPet: FeralPet): Promise<boolean> {
// Succeeds if the player has an attack greater than his level times the rarity of the animal and if random > level / 100 OR if the player has the "insults" object in his inventory
return await player.getCumulativeAttack(await InventorySlots.getMainSlotsItems(player.id)) >= player.level * feralPet.originalPet.rarity && Math.random() > player.level / 100
return await player.getCumulativeAttack(
await InventorySlots.getMainSlotsItems(player.id)) >= player.level * feralPet.originalPet.rarity
&& RandomUtils.draftbotRandom.realZeroToOneInclusive() > player.level / 100
|| await InventorySlots.hasItem(player.id, 83, ItemConstants.CATEGORIES.WEAPON);
}
}
3 changes: 2 additions & 1 deletion src/core/smallEvents/fightPet/interfaces/scream.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import {FightPetAction} from "../FightPetAction";
import Player from "../../../database/game/models/Player";
import {FeralPet} from "../../../database/game/models/FeralPet";
import {RandomUtils} from "../../../utils/RandomUtils";

/**
* Scream
*/
export default class Scream extends FightPetAction {
public applyOutcome(player: Player, feralPet: FeralPet): boolean {
// Succeeds 4/10 if the pet is masculine, 6/10 if the pet is feminine
return Math.random() < (feralPet.isFemale ? 0.4 : 0.6);
return RandomUtils.draftbotRandom.realZeroToOneInclusive() < (feralPet.isFemale ? 0.4 : 0.6);
}
}

0 comments on commit 4289830

Please sign in to comment.