Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RandomTable.roll() returning None fairly freqently. #205

Open
ThePhantomLemon opened this issue Jan 24, 2023 · 1 comment
Open

RandomTable.roll() returning None fairly freqently. #205

ThePhantomLemon opened this issue Jan 24, 2023 · 1 comment

Comments

@ThePhantomLemon
Copy link

To see the problem in action, get to a random map from chapter 45 on-wards.
The message "WARNING: We don't know how to spawn [None]!" should show up a number of times in the log.

Currently, whenever you roll 0, or exactly the sum of a number of item weights, nothing is spawned.
For example, if the first few items are weighted at [1,3,2,3,1...], then roll() would return "None" if the rng hits 0,1,4,6,9 or 10.
I'm assuming this is unintended behavior, but you can see pretty clearly why this happens in the code:

while roll > 0 {
  if roll < self.entries[index].weight {
    return self.entries[index].name.clone();
 }
    
  roll -= self.entries[index].weight;
  index += 1;
}

There are two easy fixes that I can see. Both effectively just looping more:

while roll >= 0 { ...

or

while index < self.entries.len() { ..
@Adrijan127
Copy link

Adrijan127 commented Mar 27, 2023

I think not subtracting one from the roll and using:
if roll <= self.entries[index].weight { return self.entries[index].name.clone() }
instead of if roll < self.entries[index].weight { return self.entries[index].name.clone() }
should result in the intended behavior.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants