Spam.cpp: small improvements

- Chose spam line at random only when source contain more than 1 line
- Use UniformRandomInt instead of rand() % count
This commit is contained in:
Unnamed 2019-08-21 11:38:30 +00:00 committed by TotallyNotElite
parent 87e9892a88
commit 2935a0c727

View File

@ -341,17 +341,16 @@ void createMove()
{ {
if (current_index >= source->size()) if (current_index >= source->size())
current_index = 0; current_index = 0;
if (random_order && source->size()) if (random_order && source->size() > 1)
{ {
current_index = rand() % source->size(); current_index = UniformRandomInt(0, source->size() - 1);
int tries = 0; while (current_index == last_index)
while (current_index == last_index && tries++ < 1000)
{ {
current_index = rand() % source->size(); current_index = UniformRandomInt(0, source->size() - 1);
} }
} }
last_index = current_index; last_index = current_index;
std::string spamString = source->at(current_index); std::string spamString = (*source)[current_index];
if (FormatSpamMessage(spamString)) if (FormatSpamMessage(spamString))
chat_stack::Say(spamString, *team_only); chat_stack::Say(spamString, *team_only);
current_index++; current_index++;