Moved reduceWeightValues from PMCLootGenerator into weightedRandomHelper and made public
This commit is contained in:
parent
c33773a318
commit
17296fabfd
@ -1,6 +1,7 @@
|
|||||||
import { inject, injectable } from "tsyringe";
|
import { inject, injectable } from "tsyringe";
|
||||||
|
|
||||||
import { ItemHelper } from "@spt-aki/helpers/ItemHelper";
|
import { ItemHelper } from "@spt-aki/helpers/ItemHelper";
|
||||||
|
import { WeightedRandomHelper } from "@spt-aki/helpers/WeightedRandomHelper";
|
||||||
import { ITemplateItem } from "@spt-aki/models/eft/common/tables/ITemplateItem";
|
import { ITemplateItem } from "@spt-aki/models/eft/common/tables/ITemplateItem";
|
||||||
import { ConfigTypes } from "@spt-aki/models/enums/ConfigTypes";
|
import { ConfigTypes } from "@spt-aki/models/enums/ConfigTypes";
|
||||||
import { IPmcConfig } from "@spt-aki/models/spt/config/IPmcConfig";
|
import { IPmcConfig } from "@spt-aki/models/spt/config/IPmcConfig";
|
||||||
@ -29,6 +30,7 @@ export class PMCLootGenerator
|
|||||||
@inject("ItemFilterService") protected itemFilterService: ItemFilterService,
|
@inject("ItemFilterService") protected itemFilterService: ItemFilterService,
|
||||||
@inject("RagfairPriceService") protected ragfairPriceService: RagfairPriceService,
|
@inject("RagfairPriceService") protected ragfairPriceService: RagfairPriceService,
|
||||||
@inject("SeasonalEventService") protected seasonalEventService: SeasonalEventService,
|
@inject("SeasonalEventService") protected seasonalEventService: SeasonalEventService,
|
||||||
|
@inject("WeightedRandomHelper") protected weightedRandomHelper: WeightedRandomHelper,
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
this.pmcConfig = this.configServer.getConfig(ConfigTypes.PMC);
|
this.pmcConfig = this.configServer.getConfig(ConfigTypes.PMC);
|
||||||
@ -87,7 +89,7 @@ export class PMCLootGenerator
|
|||||||
this.pocketLootPool[key] = Math.round((1 / this.pocketLootPool[key]) * highestPrice);
|
this.pocketLootPool[key] = Math.round((1 / this.pocketLootPool[key]) * highestPrice);
|
||||||
}
|
}
|
||||||
|
|
||||||
this.reduceWeightValues(this.pocketLootPool);
|
this.weightedRandomHelper.reduceWeightValues(this.pocketLootPool);
|
||||||
}
|
}
|
||||||
|
|
||||||
return this.pocketLootPool;
|
return this.pocketLootPool;
|
||||||
@ -145,7 +147,7 @@ export class PMCLootGenerator
|
|||||||
this.vestLootPool[key] = Math.round((1 / this.vestLootPool[key]) * highestPrice);
|
this.vestLootPool[key] = Math.round((1 / this.vestLootPool[key]) * highestPrice);
|
||||||
}
|
}
|
||||||
|
|
||||||
this.reduceWeightValues(this.vestLootPool);
|
this.weightedRandomHelper.reduceWeightValues(this.vestLootPool);
|
||||||
}
|
}
|
||||||
|
|
||||||
return this.vestLootPool;
|
return this.vestLootPool;
|
||||||
@ -213,71 +215,9 @@ export class PMCLootGenerator
|
|||||||
this.backpackLootPool[key] = Math.round((1 / this.backpackLootPool[key]) * highestPrice);
|
this.backpackLootPool[key] = Math.round((1 / this.backpackLootPool[key]) * highestPrice);
|
||||||
}
|
}
|
||||||
|
|
||||||
this.reduceWeightValues(this.backpackLootPool);
|
this.weightedRandomHelper.reduceWeightValues(this.backpackLootPool);
|
||||||
}
|
}
|
||||||
|
|
||||||
return this.backpackLootPool;
|
return this.backpackLootPool;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Find the greated common divisor of all weights and use it on the passed in dictionary
|
|
||||||
* @param weightedDict
|
|
||||||
*/
|
|
||||||
protected reduceWeightValues(weightedDict: Record<string, number>): void
|
|
||||||
{
|
|
||||||
// No values, nothing to reduce
|
|
||||||
if (Object.keys(weightedDict).length === 0)
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Only one value, set to 1 and exit
|
|
||||||
if (Object.keys(weightedDict).length === 1)
|
|
||||||
{
|
|
||||||
const key = Object.keys(weightedDict)[0];
|
|
||||||
weightedDict[key] = 1;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const weights = Object.values(weightedDict).slice();
|
|
||||||
const commonDivisor = this.commonDivisor(weights);
|
|
||||||
|
|
||||||
// No point in dividing by 1
|
|
||||||
if (commonDivisor === 1)
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (const key in weightedDict)
|
|
||||||
{
|
|
||||||
if (Object.hasOwn(weightedDict, key))
|
|
||||||
{
|
|
||||||
weightedDict[key] /= commonDivisor;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
protected commonDivisor(numbers: number[]): number
|
|
||||||
{
|
|
||||||
let result = numbers[0];
|
|
||||||
for (let i = 1; i < numbers.length; i++)
|
|
||||||
{
|
|
||||||
result = this.gcd(result, numbers[i]);
|
|
||||||
}
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected gcd(a: number, b: number): number
|
|
||||||
{
|
|
||||||
let x = a;
|
|
||||||
let y = b;
|
|
||||||
while (y !== 0)
|
|
||||||
{
|
|
||||||
const temp = y;
|
|
||||||
y = x % y;
|
|
||||||
x = temp;
|
|
||||||
}
|
|
||||||
return x;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -92,4 +92,66 @@ export class WeightedRandomHelper
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Find the greated common divisor of all weights and use it on the passed in dictionary
|
||||||
|
* @param weightedDict values to reduce
|
||||||
|
*/
|
||||||
|
public reduceWeightValues(weightedDict: Record<string, number>): void
|
||||||
|
{
|
||||||
|
// No values, nothing to reduce
|
||||||
|
if (Object.keys(weightedDict).length === 0)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Only one value, set to 1 and exit
|
||||||
|
if (Object.keys(weightedDict).length === 1)
|
||||||
|
{
|
||||||
|
const key = Object.keys(weightedDict)[0];
|
||||||
|
weightedDict[key] = 1;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const weights = Object.values(weightedDict).slice();
|
||||||
|
const commonDivisor = this.commonDivisor(weights);
|
||||||
|
|
||||||
|
// No point in dividing by 1
|
||||||
|
if (commonDivisor === 1)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const key in weightedDict)
|
||||||
|
{
|
||||||
|
if (Object.hasOwn(weightedDict, key))
|
||||||
|
{
|
||||||
|
weightedDict[key] /= commonDivisor;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected commonDivisor(numbers: number[]): number
|
||||||
|
{
|
||||||
|
let result = numbers[0];
|
||||||
|
for (let i = 1; i < numbers.length; i++)
|
||||||
|
{
|
||||||
|
result = this.gcd(result, numbers[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected gcd(a: number, b: number): number
|
||||||
|
{
|
||||||
|
let x = a;
|
||||||
|
let y = b;
|
||||||
|
while (y !== 0)
|
||||||
|
{
|
||||||
|
const temp = y;
|
||||||
|
y = x % y;
|
||||||
|
x = temp;
|
||||||
|
}
|
||||||
|
return x;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -253,9 +253,9 @@ export class RagfairPriceService implements OnLoad
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param itemTemplateId
|
* @param itemTemplateId items tpl value
|
||||||
* @param desiredCurrency
|
* @param desiredCurrency Currency to return result in
|
||||||
* @param item
|
* @param item Item object (used for weapon presets)
|
||||||
* @param offerItems
|
* @param offerItems
|
||||||
* @param isPackOffer
|
* @param isPackOffer
|
||||||
* @returns
|
* @returns
|
||||||
|
Loading…
Reference in New Issue
Block a user