Skip to content

Commit

Permalink
feat(explore): monster formations are weighted
Browse files Browse the repository at this point in the history
  • Loading branch information
seiyria committed Sep 8, 2023
1 parent 3a4ba9a commit 4929a08
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 3 deletions.
4 changes: 3 additions & 1 deletion server/src/modules/player/player.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { zeroResistances, zeroStats } from '@helpers/stats';
import {
Element,
ILocation,
IMonsterFormation,
INotificationAction,
Rarity,
Stat,
Expand All @@ -19,6 +20,7 @@ import {
Injectable,
} from '@nestjs/common';
import { getPatchesAfterPropChanges } from '@utils/patches';
import { pickWeighted } from '@utils/weighted';
import { sample } from 'lodash';
import { Logger } from 'nestjs-pino';

Expand Down Expand Up @@ -353,7 +355,7 @@ export class PlayerService {
}

async handleFindMonster(player: Player, location: ILocation): Promise<void> {
const randomFormationForLocation = sample(
const randomFormationForLocation = pickWeighted<IMonsterFormation>(
this.contentService
.allFormations()
.filter((formation) => formation.location === location.name),
Expand Down
6 changes: 6 additions & 0 deletions server/src/utils/weighted.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { IWeighted } from '@interfaces';
import { sample } from 'lodash';

export function pickWeighted<T>(weights: IWeighted[]): T {
return sample(weights.map((w) => new Array(w.weight).fill(w)).flat()) as T;
}
5 changes: 5 additions & 0 deletions shared/interfaces/buildingblocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,8 @@ export enum Stat {
Resistance = 'resistance',
Special = 'special',
}

export interface IWeighted {
weight: number;
itemId: string;
}
5 changes: 3 additions & 2 deletions shared/interfaces/monster.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Element, Stat } from './buildingblocks';
import { Element, IWeighted, Stat } from './buildingblocks';

export interface ICombatMonsterAbility {
ability: string;
Expand Down Expand Up @@ -31,9 +31,10 @@ export interface IMonsterGroup {
monster: string;
}

export interface IMonsterFormation {
export interface IMonsterFormation extends IWeighted {
name: string;
itemId: string;
weight: number;
location: string;
monsters: IMonsterGroup[];
}

0 comments on commit 4929a08

Please sign in to comment.