[TF2] Improve the random class selection algorithm to reduce the chance of failure #1434
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
When picking a random class, the game requires that the randomly chosen class meets the following criteria:
Currently, when you choose the random button on the class select menu, the game rolls the dice upwards of 20 times and compares the randomly chosen class to the criteria above on each roll. In the unlikely event that this algorithm fails to find a valid class, the player will be shown the class select menu again. It's possible for this algorithm to fail even when there are valid classes to randomly choose from. This algorithm fails the most when strict class limits are involved on a full server.
This PR reworks the random class selection algorithm to form a list of valid classes before rolling for a valid class from the list. If the only class available is your current class, then it won't redisplay the menu unnecessarily. If there are two classes available and you are already one of them, the random button will always choose the other available class when rolling the dice. The only case where the class select menu will redisplay (to my knowledge) is when every class's limit is full, in which case there's a problem with the server's configuration.
This video showcases a before and after of what happens when the algorithm fails to pick a valid class despite your current class being the only one available. This also happens when the old algorithm picks invalid classes 20 times.
random.class.select.improvement.mp4