import LRU = require ( 'lru-cache' ) ; import MersenneTwister = require ( 'mersenne-twister' ) ; import Point from 'slime-finder/point' ; import { umul32_lo , umul32_hi } from 'slime-finder/umul32' ; const cache = new LRU < string , _Chunk > ( 5000 ) ; export default class Chunk { private readonly _chunk : _Chunk ; constructor ( origin ) { let o = new Point ( Math . floor ( origin . x / 16 ) * 16 , Math . floor ( origin . z / 16 ) * 16 ) ; let k = `${ o . x },${ o . z }` ; let c = cache . get ( k ) ; if ( c ) { this . _chunk = c ; } else { this . _chunk = new _Chunk ( o ) ; cache . set ( k , this . _chunk ) ; } } get origin ( ) : Point { return this . _chunk . origin ; } get isSlimy ( ) : boolean { return this . _chunk . isSlimy ; } offset ( x , z ) { return new Chunk ( new Point ( this . origin . x + x * 16 , this . origin . z + z * 16 ) ) ; } } class _Chunk { readonly origin : Point ; private _isSlimy : boolean | null ; constructor ( origin ) { this . origin = origin ; this . _isSlimy = null ; } get isSlimy ( ) : boolean { if ( this . _isSlimy == null ) { let x_uint = ( this . origin . x / 16 ) >>> 0 ; let z_uint = ( this . origin . z / 16 ) >>> 0 ; let seed = umul32_lo ( x_uint , 0x1f1f1f1f ) ^ z_uint ; let mt = new MersenneTwister ( seed ) ; let n = mt . random_int ( ) ; this . _isSlimy = ( n % 10 == 0 ) ; } return this . _isSlimy ; } }