Skip to content

Commit

Permalink
changes
Browse files Browse the repository at this point in the history
  • Loading branch information
gc committed Sep 28, 2024
1 parent 36e5793 commit 6a9a69a
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 58 deletions.
7 changes: 4 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import "./structures/Items";
import { EItem } from "./EItem";
import { EMonster } from "./EMonster";
import * as constants from "./constants";
import { MonsterSlayerMaster } from "./meta/monsterData";
import { MonsterKillOptions } from "./meta/types";
import type { MonsterKillOptions } from "./meta/types";
import * as Misc from "./simulation/misc";
import Monsters from "./simulation/monsters/index";
import Openables from "./simulation/openables/index";
Expand All @@ -23,7 +24,7 @@ export {
Items,
LootTable,
Misc,
MonsterKillOptions,
type MonsterKillOptions,
Monsters,
MonsterSlayerMaster,
Openables,
Expand All @@ -47,4 +48,4 @@ export * from "./simulation/openables";
export * from "./simulation/misc";
export * from "./simulation/openables/Implings";
export * from "./meta/monsterData";
export * from "./simulation/subtables/index";
export * from "./simulation/subtables/index";
3 changes: 1 addition & 2 deletions src/structures/Bank.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,7 @@ export default class Bank {
public addItem(item: number, quantity = 1): this {
if (this.frozen) throw new Error(frozenErrorStr);
if (quantity < 1) return this;
const current = this.map.get(item) ?? 0;
this.map.set(item, current + quantity);
this.map.set(item, (this.map.get(item) ?? 0) + quantity);
return this;
}

Expand Down
15 changes: 7 additions & 8 deletions src/structures/Items.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import deepMerge from "deepmerge";

import _items from "../data/items/item_data.json";
import _items from "../data/items/item_data.json" assert { type: "json" };
import type { Item, ItemID } from "../meta/types";
import { cleanString } from "../util/cleanString";
import Collection from "./Collection";
import { Collection } from "./Collection";

// @ts-ignore asdf
const items = _items as Record<string, Item>;
Expand Down Expand Up @@ -50,7 +49,7 @@ export const USELESS_ITEMS = [
23_814, 23_815, 23_816, 23_817,
];

class Items extends Collection<number, Item> {
class Items extends Collection<ItemID, Item> {
public get(item: ItemResolvable): Item | undefined {
const id = this.resolveID(item);
if (typeof id === "undefined") return undefined;
Expand All @@ -71,8 +70,7 @@ class Items extends Collection<number, Item> {
}

if (typeof input === "string") {
const cleanName = cleanString(input);
return itemNameMap.get(cleanName);
return itemNameMap.get(input);
}

return undefined;
Expand All @@ -86,8 +84,9 @@ for (const [id, item] of Object.entries(items)) {

if (USELESS_ITEMS.includes(numID)) continue;
itemsExport.set(numID, item);
const cleanName = cleanString(item.name);
if (!itemNameMap.has(cleanName)) itemNameMap.set(cleanName, numID);
if (!itemNameMap.has(item.name)) {
itemNameMap.set(item.name, numID);
}
}

export default itemsExport;
102 changes: 57 additions & 45 deletions src/structures/LootTable.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { randArrItem } from "e";
import itemID from "../util/itemID";
import Bank from "./Bank";
import Items from "./Items";
Expand Down Expand Up @@ -48,6 +49,7 @@ export interface LootTableRollOptions {
*/
tertiaryItemPercentageChanges?: Map<string, number>;
targetBank?: Bank;
effectiveTertiaryItems?: OneInItems[];
}

export default class LootTable {
Expand Down Expand Up @@ -193,12 +195,7 @@ export default class LootTable {
return this;
}

roll(quantity?: number): Bank;
roll(quantity: number, options: { targetBank?: undefined } & LootTableRollOptions): Bank;
roll(quantity: number, options: { targetBank: Bank } & LootTableRollOptions): null;
public roll(quantity = 1, options: LootTableRollOptions = {}): Bank | null {
const loot = options.targetBank ?? new Bank();

public calculateTertiary(options: LootTableRollOptions = {}) {
const effectiveTertiaryItems = options.tertiaryItemPercentageChanges
? this.tertiaryItems.map(i => {
if (typeof i.item !== "number") return i;
Expand All @@ -212,64 +209,79 @@ export default class LootTable {
})
: this.tertiaryItems;

console.trace("effectiveTertiaryItems");
return effectiveTertiaryItems;
}

private cachedOptimizedTable: number[] | null = null;
roll(quantity?: number): Bank;
roll(quantity: number, options: { targetBank?: undefined } & LootTableRollOptions): Bank;
roll(quantity: number, options: { targetBank: Bank } & LootTableRollOptions): null;
public roll(quantity = 1, options: LootTableRollOptions = {}): Bank | null {
const loot = options.targetBank ?? new Bank();

const effectiveTertiaryItems = options.effectiveTertiaryItems ?? this.calculateTertiary(options);

const limit = this.limit || this.totalWeight;

if (this.table.every(i => Number.isInteger(i.weight)) && this.cachedOptimizedTable === null) {
this.cachedOptimizedTable = [];
for (const item of this.table) {
for (let j = 0; j < item.weight!; j++) {
this.cachedOptimizedTable.push(this.table.indexOf(item));
}
}
}

outerLoop: for (let i = 0; i < quantity; i++) {
// The items that are rolled.
for (const item of this.everyItems) {
this.addResultToLoot(item, loot);
for (let j = 0; j < this.everyItems.length; j++) {
this.addResultToLoot(this.everyItems[j], loot, effectiveTertiaryItems);
}

for (const { chance, item, quantity, options } of effectiveTertiaryItems) {
if (roll(chance)) {
this.addResultToLoot({ item, quantity, options }, loot);
for (let j = 0; j < effectiveTertiaryItems.length; j++) {
if (roll(effectiveTertiaryItems[j].chance)) {
this.addResultToLoot(effectiveTertiaryItems[j], loot, effectiveTertiaryItems);
}
}

for (const { chance, item, quantity, options } of this.oneInItems) {
if (roll(chance)) {
this.addResultToLoot({ item, quantity, options }, loot);
for (let j = 0; j < this.oneInItems.length; j++) {
if (roll(this.oneInItems[j].chance)) {
this.addResultToLoot(this.oneInItems[j], loot, effectiveTertiaryItems);
continue outerLoop;
}
}

// Random float between 0 and the total weighting
const randomWeight = randFloat(0, this.limit || this.totalWeight);

// The index of the item that will be used.
let result = -1;
let weight = 0;

for (let i = 0; i < this.table.length; i++) {
const item = this.table[i]!;
weight += item.weight!;
if (randomWeight <= weight) {
result = i;
break;
if (this.cachedOptimizedTable) {
this.addResultToLoot(this.table[randArrItem(this.cachedOptimizedTable)], loot, effectiveTertiaryItems);
} else {
const randomWeight = randFloat(0, limit);
let weight = 0;
for (let i = 0; i < this.table.length; i++) {
weight += this.table[i].weight!;
if (randomWeight <= weight) {
this.addResultToLoot(this.table[i], loot, effectiveTertiaryItems);
break;
}
}
}

const chosenItem = this.table[result];
if (chosenItem) {
this.addResultToLoot(chosenItem, loot);
}
}

if (options.targetBank) return null;
return loot;
if (!options.targetBank) {
return loot;
}
return null;
}

private addResultToLoot(result: LootTableItem, loot: Bank): void {
if (!result) return;
const { item, quantity, options } = result;

if (typeof item === "number") {
loot.add(item, this.determineQuantity(quantity));
private addResultToLoot(result: LootTableItem, loot: Bank, effectiveTertiaryItems: OneInItems[]): void {
if (typeof result?.item === "number") {
loot.addItem(result.item, this.determineQuantity(result.quantity));
return;
}

if (item instanceof LootTable) {
const qty = this.determineQuantity(quantity);
if (options?.multiply) loot.add(item.roll(1).multiply(qty));
else item.roll(qty, { targetBank: loot });
if (result?.item instanceof LootTable) {
const qty = this.determineQuantity(result.quantity);
if (result.options?.multiply) loot.add(result.item.roll(1, { effectiveTertiaryItems }).multiply(qty));
else result.item.roll(qty, { targetBank: loot, effectiveTertiaryItems });
return;
}
}
Expand Down

0 comments on commit 6a9a69a

Please sign in to comment.