2023-02-18 01:32:13 +00:00
|
|
|
import { currency, lootTables } from "settings/Datatypes";
|
2023-02-05 12:43:18 +00:00
|
|
|
|
2023-02-08 13:13:11 +00:00
|
|
|
export function generateLoot(enableCurrency: boolean, currencyFrequency: number, currencyTypes: currency[], lootTable: lootTables) {
|
2023-04-04 02:24:00 +00:00
|
|
|
const { adj, items } = lootTable;
|
|
|
|
|
const itemAmount = Math.floor(Math.random() * 5) + 1;
|
2023-02-03 10:55:51 +00:00
|
|
|
|
|
|
|
|
let loot = '';
|
2023-04-04 02:24:00 +00:00
|
|
|
for (let index = 0; index < itemAmount; index++) {
|
|
|
|
|
const randomAdjective = adj[Math.floor(Math.random() * adj.length)];
|
2023-02-03 10:55:51 +00:00
|
|
|
const amount = generateRareHighNumber(50, 0.1);
|
2023-04-04 02:24:00 +00:00
|
|
|
const randomNoun = getRandomElement(items);
|
|
|
|
|
const article = /^[aeiou]/i.test(randomAdjective) ? 'an' : 'a';
|
|
|
|
|
const plural = randomNoun?.endsWith('s') ? `${randomNoun}'` : `${randomNoun}s`;
|
|
|
|
|
|
|
|
|
|
loot += `${amount > 1 ? `${amount} ${randomAdjective} ${plural}` : `${article} ${randomAdjective} ${randomNoun}`}, `;
|
2023-02-03 10:55:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (enableCurrency) {
|
2023-04-04 02:24:00 +00:00
|
|
|
const shouldGenCurrency = Math.random() * 100;
|
|
|
|
|
const currencyLoot = currencyTypes
|
|
|
|
|
.map((element) => {
|
2023-02-03 10:55:51 +00:00
|
|
|
const randomCurrencyAmount = generateRareHighNumberByRarity(element.rarity);
|
2023-04-04 02:24:00 +00:00
|
|
|
return randomCurrencyAmount > 0 ? `${randomCurrencyAmount} ${element.name}, ` : '';
|
|
|
|
|
})
|
|
|
|
|
.join('');
|
2023-02-03 10:55:51 +00:00
|
|
|
|
2023-04-04 02:24:00 +00:00
|
|
|
if (shouldGenCurrency < currencyFrequency) {
|
|
|
|
|
loot += currencyLoot;
|
|
|
|
|
}
|
2023-02-03 10:55:51 +00:00
|
|
|
}
|
|
|
|
|
|
2023-04-04 02:24:00 +00:00
|
|
|
return loot.slice(0, -2);
|
2023-02-03 10:55:51 +00:00
|
|
|
}
|
2023-04-04 02:24:00 +00:00
|
|
|
|
2023-02-03 10:55:51 +00:00
|
|
|
function generateRareHighNumber(maxNumber: number, rarityFactor: number) {
|
|
|
|
|
const randomNumber = Math.random();
|
2023-04-04 02:24:00 +00:00
|
|
|
const rarity = randomNumber < rarityFactor ? rarityFactor : rarityFactor / 10;
|
|
|
|
|
|
|
|
|
|
return Math.floor(Math.random() * maxNumber * rarity);
|
2023-02-03 10:55:51 +00:00
|
|
|
}
|
2023-04-04 02:24:00 +00:00
|
|
|
|
2023-02-03 10:55:51 +00:00
|
|
|
function generateRareHighNumberByRarity(rarity: string) {
|
2023-04-04 02:24:00 +00:00
|
|
|
const rarityFactors: { [key: string]: number } = { common: 0.7, uncommon: 0.5, rare: 0.2, rarest: 0.02 };
|
|
|
|
|
const maxNumbers: { [key: string]: number } = { common: 300, uncommon: 150, rare: 30, rarest: 10 };
|
2023-02-03 10:55:51 +00:00
|
|
|
const randomNumber = Math.random();
|
|
|
|
|
|
2023-04-04 02:24:00 +00:00
|
|
|
if (randomNumber > rarityFactors[rarity] || rarityFactors[rarity] === undefined) {
|
|
|
|
|
return 0;
|
2023-02-03 10:55:51 +00:00
|
|
|
}
|
|
|
|
|
|
2023-04-04 02:24:00 +00:00
|
|
|
return Math.floor(Math.random() * maxNumbers[rarity]);
|
|
|
|
|
}
|
2023-02-03 10:55:51 +00:00
|
|
|
|
2023-04-04 02:24:00 +00:00
|
|
|
function getRandomElement<T>(arr: { item: T, weight: number }[]): T | undefined {
|
|
|
|
|
const totalWeight = arr.reduce((acc, cur) => acc + cur.weight, 0);
|
|
|
|
|
let randomWeight = Math.random() * totalWeight;
|
|
|
|
|
for (const { item, weight } of arr) {
|
|
|
|
|
if (randomWeight < weight) {
|
|
|
|
|
return item;
|
|
|
|
|
}
|
|
|
|
|
randomWeight -= weight;
|
2023-02-03 10:55:51 +00:00
|
|
|
}
|
2023-04-04 02:24:00 +00:00
|
|
|
return undefined;
|
2023-02-03 10:55:51 +00:00
|
|
|
}
|