@@ -30,34 +30,58 @@ public override CommandAlias[] Aliases {
3030 new CommandAlias ( "GotoRandom" , "-random" ) , new CommandAlias ( "JoinRandom" , "-random" ) } ; }
3131 }
3232 public override bool SuperUseable { get { return false ; } }
33+ public override CommandParallelism Parallelism { get { return CommandParallelism . NoAndWarn ; } }
3334
3435 public override void Use ( Player p , string message , CommandData data ) {
3536 if ( message . Length == 0 ) { Help ( p ) ; return ; }
3637
3738 if ( message . CaselessStarts ( "-random" ) ) {
39+ Random r = new Random ( ) ;
3840 string [ ] files = LevelInfo . AllMapFiles ( ) ;
3941 string [ ] args = message . SplitSpaces ( 2 ) ;
40- string map ;
41-
42- // randomly only visit certain number of maps
43- if ( args . Length > 1 ) {
44- List < string > maps = Wildcard . Filter ( files , args [ 1 ] ,
45- mapFile => Path . GetFileNameWithoutExtension ( mapFile ) ) ;
46- if ( maps . Count == 0 ) {
47- p . Message ( "No maps found containing \" {0}\" " , args [ 1 ] ) ;
48- return ;
49- }
50- map = maps [ new Random ( ) . Next ( maps . Count ) ] ;
51- } else {
52- map = files [ new Random ( ) . Next ( files . Length ) ] ;
53- map = Path . GetFileNameWithoutExtension ( map ) ;
42+
43+ int attempts = 0 ;
44+ GrResult res ;
45+ do {
46+ attempts ++ ;
47+ res = TryGotoRandom ( p , r , files , args ) ;
48+ } while ( attempts < 5 && res == GrResult . NoPermission ) ;
49+
50+ if ( res == GrResult . NoPermission ) {
51+ p . Message ( "&WTook too long to find a random map to go to. Giving up." ) ;
5452 }
5553
56- PlayerActions . ChangeMap ( p , map ) ;
5754 } else if ( Formatter . ValidMapName ( p , message ) ) {
5855 PlayerActions . ChangeMap ( p , message ) ;
5956 }
6057 }
58+
59+ enum GrResult { NoLevels , NoPermission , Success }
60+ static GrResult TryGotoRandom ( Player p , Random r , string [ ] files , string [ ] args ) {
61+ string map ;
62+
63+ // randomly visit a specified subset of all levels
64+ if ( args . Length > 1 ) {
65+ List < string > maps = Wildcard . Filter ( files , args [ 1 ] ,
66+ mapFile => Path . GetFileNameWithoutExtension ( mapFile ) ) ;
67+ if ( maps . Count == 0 ) {
68+ p . Message ( "No maps found containing \" {0}\" " , args [ 1 ] ) ;
69+ return GrResult . NoLevels ;
70+ }
71+ map = maps [ r . Next ( maps . Count ) ] ;
72+ } else {
73+ map = files [ r . Next ( files . Length ) ] ;
74+ map = Path . GetFileNameWithoutExtension ( map ) ;
75+ }
76+ if ( p . level . name == map ) {
77+ // try again silently
78+ return GrResult . NoPermission ;
79+ }
80+
81+ bool changed = PlayerActions . ChangeMap ( p , map ) ;
82+ if ( changed ) return GrResult . Success ;
83+ return GrResult . NoPermission ;
84+ }
6185
6286 public override void Help ( Player p ) {
6387 p . Message ( "&T/Goto [map name]" ) ;
0 commit comments