import * as _ from "lodash" ; import { Link } from "./link" ; export interface IScene { func : ( obj : any ) => any ; odds ? : number | ( ( ) => number ) ; cond ? : boolean | ( ( ) => boolean ) ; obj ? : any ; } export class EncounterTable { public encounters : IScene [ ] ; public hunt : Link [ ] ; constructor ( ) { this . encounters = [ ] ; this . hunt = [ ] ; } public AddEnc ( func , odds ? , cond ? , obj ? ) { this . encounters . push ( { func , odds , cond , obj } ) ; } public Num ( ) { return this . encounters . length ; } public Get ( ) { const scenes = [ ] ; let sum = 0 ; for ( const e of this . encounters ) { let canFind = e . cond ; if ( canFind === undefined ) { canFind = true ; } if ( canFind ) { if ( _ . isFunction ( canFind ) ) { canFind = canFind ( ) ; } if ( canFind ) { let odds = e . odds ; if ( odds === undefined ) { odds = 1.0 ; } if ( _ . isFunction ( odds ) ) { odds = odds ( ) ; } scenes . push ( { func : e . func , odds , obj : e . obj } ) ; sum += odds ; } } } let step = Math . random ( ) * sum ; for ( const enc of scenes ) { step -= enc . odds as number ; if ( step <= 0.0 ) { return enc . func ? enc . func ( enc . obj ) : undefined ; } } return undefined ; } }