Hands-on Rust: spawn entities doesn't honor the frequency (page 276)

Hi!

With hindsight, I should have used the rand crate’s weighted selection, but this seems to work. NUM_MONSTERS is meant to limit the total number of monsters - the actual weighting happens with available_entities.

At the top of spawn_entities (HandsOnRust/template.rs at main · thebracket/HandsOnRust · GitHub), available_entities is created by first filtering on level (so it only sees entities that can be on the level) and then inserting each entity a number of times equal to its frequency. So an orc with a frequency of 3 would be in available_entities 3 times.

So when random_slice_entry comes along and picks a slice entry, it includes all of the slice entries - including the duplicates. That preserves the weighting by template type.

Hope that makes sense?