@@ -590,7 +590,8 @@ function initialize(width, height) {
590
590
return map ;
591
591
}
592
592
593
- export default function ALGORITHM_METROIDVANIA ( tileMap , options ) {
593
+ export default function ALGORITHM_METROIDVANIA ( tileMap , options = { } ) {
594
+ try {
594
595
tileMap . fill ( 0 ) ; // Fill with walls
595
596
const maxDimension = Math . max ( tileMap . width , tileMap . height ) ;
596
597
if ( maxDimension <= 5 ) {
@@ -622,13 +623,77 @@ export default function ALGORITHM_METROIDVANIA(tileMap, options) {
622
623
roomDiff : doorDiff , // When adding a new door, room ID distance
623
624
roomDiffOdds : 1 / 2 // Odds of inserting a new door on opportunity
624
625
} ) ;
625
- const built = generator . build ( ( ) => {
626
- return tileMap . random ( ) ;
627
- } ) ;
626
+ let built = null ;
627
+ if ( options . retries ) {
628
+ let generated = false ;
629
+ let currentTry = 0 ;
630
+ while ( currentTry < options . retries && ! generated ) {
631
+ try {
632
+ currentTry ++
633
+ built = generator . build ( ( ) => {
634
+ return tileMap . random ( ) ;
635
+ } ) ;
636
+ generated = true ;
637
+ } catch ( ex ) { }
638
+ }
639
+ } else {
640
+ built = generator . build ( ( ) => {
641
+ return tileMap . random ( ) ;
642
+ } ) ;
643
+ }
628
644
const flattened = built . world . reduce ( ( ( agg , line ) => agg . concat ( line ) ) , [ ] ) ;
629
645
built . world = null ;
646
+ built . scaledExits = { } ;
647
+ let chr = null ;
648
+ [ 'north' , 'south' , 'east' , 'west' ] . forEach ( ( direction ) => {
649
+ chr = direction [ 0 ] ;
650
+ const scaled = {
651
+ x : built . exits [ chr ] . x * roomSizeWidth ,
652
+ y : ( built . exits [ chr ] . y * roomSizeHeight )
653
+ } ;
654
+ built . scaledExits [ direction ] = [ ] ;
655
+ switch ( direction ) {
656
+ case 'north' :
657
+ scaled . x += Math . floor ( roomSizeWidth / 2 ) ;
658
+ built . scaledExits [ direction ] . push ( scaled ) ;
659
+ built . scaledExits [ direction ] . push ( {
660
+ x : scaled . x - 1 ,
661
+ y : scaled . y
662
+ } ) ;
663
+ break ;
664
+ case 'south' :
665
+ scaled . x += Math . floor ( roomSizeWidth / 2 ) ;
666
+ scaled . y += roomSizeHeight - 1 ;
667
+ built . scaledExits [ direction ] . push ( scaled ) ;
668
+ built . scaledExits [ direction ] . push ( {
669
+ x : scaled . x - 1 ,
670
+ y : scaled . y
671
+ } ) ;
672
+ break ;
673
+ case 'east' :
674
+ scaled . y += Math . floor ( roomSizeHeight / 2 ) ;
675
+ scaled . x += roomSizeWidth - 1 ;
676
+ built . scaledExits [ direction ] . push ( scaled ) ;
677
+ built . scaledExits [ direction ] . push ( {
678
+ x : scaled . x ,
679
+ y : scaled . y - 1
680
+ } ) ;
681
+ break ;
682
+ case 'west' :
683
+ scaled . y += Math . floor ( roomSizeHeight / 2 ) ;
684
+ built . scaledExits [ direction ] . push ( scaled ) ;
685
+ built . scaledExits [ direction ] . push ( {
686
+ x : scaled . x ,
687
+ y : scaled . y - 1
688
+ } ) ;
689
+ break ;
690
+ }
691
+ } ) ;
630
692
tileMap . world = built ;
631
693
for ( let lcv = 0 ; lcv < tileMap . data . length ; lcv ++ ) {
632
694
tileMap . data [ lcv ] = flattened [ lcv ] ;
633
695
}
696
+ } catch ( ex ) {
697
+ console . log ( ex ) ;
698
+ }
634
699
}
0 commit comments