Server/project/src/helpers/BotDifficultyHelper.ts

132 lines
5.1 KiB
TypeScript
Raw Normal View History

2023-03-03 16:23:46 +01:00
import { inject, injectable } from "tsyringe";
import { BotHelper } from "@spt/helpers/BotHelper";
import { Difficulty } from "@spt/models/eft/common/tables/IBotType";
import { ConfigTypes } from "@spt/models/enums/ConfigTypes";
import { IPmcConfig } from "@spt/models/spt/config/IPmcConfig";
import { ILogger } from "@spt/models/spt/utils/ILogger";
import { ConfigServer } from "@spt/servers/ConfigServer";
import { DatabaseServer } from "@spt/servers/DatabaseServer";
import { LocalisationService } from "@spt/services/LocalisationService";
import { ICloner } from "@spt/utils/cloners/ICloner";
import { RandomUtil } from "@spt/utils/RandomUtil";
2023-03-03 16:23:46 +01:00
@injectable()
export class BotDifficultyHelper
{
protected pmcConfig: IPmcConfig;
2023-03-03 16:23:46 +01:00
constructor(
@inject("WinstonLogger") protected logger: ILogger,
@inject("DatabaseServer") protected databaseServer: DatabaseServer,
@inject("RandomUtil") protected randomUtil: RandomUtil,
@inject("LocalisationService") protected localisationService: LocalisationService,
@inject("BotHelper") protected botHelper: BotHelper,
2023-11-16 02:35:05 +01:00
@inject("ConfigServer") protected configServer: ConfigServer,
@inject("RecursiveCloner") protected cloner: ICloner,
2023-03-03 16:23:46 +01:00
)
{
this.pmcConfig = this.configServer.getConfig(ConfigTypes.PMC);
2023-03-03 16:23:46 +01:00
}
2023-11-16 02:35:05 +01:00
public getPmcDifficultySettings(
pmcType: "bear" | "usec",
difficulty: string,
usecType: string,
bearType: string,
): Difficulty
2023-03-03 16:23:46 +01:00
{
const difficultySettings = this.getDifficultySettings(pmcType, difficulty);
2023-11-16 02:35:05 +01:00
const friendlyType = pmcType === "bear" ? bearType : usecType;
const enemyType = pmcType === "bear" ? usecType : bearType;
2023-03-03 16:23:46 +01:00
this.botHelper.addBotToEnemyList(difficultySettings, this.pmcConfig.enemyTypes, friendlyType); // Add generic bot types to enemy list
2023-03-03 16:23:46 +01:00
this.botHelper.addBotToEnemyList(difficultySettings, [enemyType, friendlyType], ""); // add same/opposite side to enemy list
this.botHelper.randomizePmcHostility(difficultySettings);
return difficultySettings;
}
/**
* Get difficulty settings for desired bot type, if not found use assault bot types
* @param type bot type to retrieve difficulty of
* @param difficulty difficulty to get settings for (easy/normal etc)
* @returns Difficulty object
*/
public getBotDifficultySettings(type: string, difficulty: string): Difficulty
{
const desiredType = type.toLowerCase();
const bot = this.databaseServer.getTables().bots.types[desiredType];
2023-03-03 16:23:46 +01:00
if (!bot)
{
// get fallback
this.logger.warning(this.localisationService.getText("bot-unable_to_get_bot_fallback_to_assault", type));
this.databaseServer.getTables().bots.types[desiredType] = this.cloner.clone(
2023-11-16 02:35:05 +01:00
this.databaseServer.getTables().bots.types.assault,
);
2023-03-03 16:23:46 +01:00
}
const difficultySettings = this.botHelper.getBotTemplate(desiredType).difficulty[difficulty];
2023-03-03 16:23:46 +01:00
if (!difficultySettings)
{
2023-11-16 02:35:05 +01:00
this.logger.warning(
this.localisationService.getText("bot-unable_to_get_bot_difficulty_fallback_to_assault", {
botType: desiredType,
2023-11-16 02:35:05 +01:00
difficulty: difficulty,
}),
);
this.databaseServer.getTables().bots.types[desiredType].difficulty[difficulty] = this.cloner.clone(
2023-11-16 02:35:05 +01:00
this.databaseServer.getTables().bots.types.assault.difficulty[difficulty],
);
2023-03-03 16:23:46 +01:00
}
return this.cloner.clone(difficultySettings);
2023-03-03 16:23:46 +01:00
}
/**
* Get difficulty settings for a PMC
* @param type "usec" / "bear"
2023-11-16 02:35:05 +01:00
* @param difficulty what difficulty to retrieve
2023-03-03 16:23:46 +01:00
* @returns Difficulty object
*/
protected getDifficultySettings(type: string, difficulty: string): Difficulty
{
let difficultySetting
= this.pmcConfig.difficulty.toLowerCase() === "asonline"
? difficulty
: this.pmcConfig.difficulty.toLowerCase();
2023-03-03 16:23:46 +01:00
difficultySetting = this.convertBotDifficultyDropdownToBotDifficulty(difficultySetting);
return this.cloner.clone(this.databaseServer.getTables().bots.types[type].difficulty[difficultySetting]);
2023-03-03 16:23:46 +01:00
}
/**
* Translate chosen value from pre-raid difficulty dropdown into bot difficulty value
* @param dropDownDifficulty Dropdown difficulty value to convert
* @returns bot difficulty
*/
public convertBotDifficultyDropdownToBotDifficulty(dropDownDifficulty: string): string
{
switch (dropDownDifficulty.toLowerCase())
2023-03-03 16:23:46 +01:00
{
case "medium":
return "normal";
case "random":
return this.chooseRandomDifficulty();
default:
return dropDownDifficulty.toLowerCase();
2023-03-03 16:23:46 +01:00
}
}
/**
* Choose a random difficulty from - easy/normal/hard/impossible
* @returns random difficulty
*/
public chooseRandomDifficulty(): string
{
return this.randomUtil.getArrayValue(["easy", "normal", "hard", "impossible"]);
}
2023-11-16 02:35:05 +01:00
}