@@ -74,29 +74,34 @@ public static void RandomizeMap(bool setMapType = true)
7474 var fileIDs = new List < string > ( MapFileAPI . ListIDs ( ) ) ;
7575 var mapIDs = fileIDs . FindAll ( id => ! blacklistMaps . Contains ( id ) ) ;
7676 if ( mapIDs . Count <= 0 )
77- return null ; // <-- No valid maps found
77+ return null ; // <-- No valid maps left
7878
7979 // Get map weights
8080 var mapWeights = new float [ mapIDs . Count ] ;
81- float mapWeightSum = 0 ;
81+ var sumOfAllMapWeights = 0.0f ;
8282 for ( var i = 0 ; i < mapIDs . Count ; i ++ )
8383 {
8484 var mapWeight = ConfigAPI . GetMapWeight ( mapIDs [ i ] ) ;
85- mapWeights [ i ] = mapWeightSum + mapWeight ;
86- mapWeightSum += mapWeight ;
85+ mapWeights [ i ] = sumOfAllMapWeights + mapWeight ;
86+ sumOfAllMapWeights += mapWeight ;
8787 }
8888
89+ // All maps are of zero weight
90+ if ( sumOfAllMapWeights <= 0 )
91+ return null ;
92+
8993 // Choose a random map
90- // TODO: FIX ME, this logic is flawed and does not properly account for weights
91- var randomValue = Random . Range ( 0 , mapWeightSum ) ;
94+ var randomValue = Random . Range ( 0 , sumOfAllMapWeights ) ;
95+ var remainingWeight = sumOfAllMapWeights ;
9296 for ( var i = 0 ; i < mapIDs . Count ; i ++ )
9397 {
9498 // Check weight
95- var mapID = mapIDs [ i ] ;
96- if ( mapWeights [ i ] < randomValue )
99+ remainingWeight -= mapWeights [ i ] ;
100+ if ( remainingWeight > 0 )
97101 continue ;
98102
99103 // Check if map is in workshop
104+ var mapID = mapIDs [ i ] ;
100105 var isInWorkshop = Guid . TryParse ( mapID , out _ ) ;
101106 if ( isInWorkshop )
102107 return mapID ;
0 commit comments