Implements ChoiceQueue::move_pokemon_choice_next, micro optimization for BattleRandom::EffectChance.
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
This commit is contained in:
parent
ba5992e925
commit
4c06dc17e2
|
@ -50,8 +50,36 @@ impl ChoiceQueue {
|
|||
}
|
||||
|
||||
/// This moves the choice of a specific Pokemon up to the next choice to be executed.
|
||||
pub fn move_pokemon_choice_next(&mut self, _pokemon: &Pokemon) {
|
||||
todo!()
|
||||
pub fn move_pokemon_choice_next(&mut self, pokemon: &Pokemon) {
|
||||
let mut desired_index = None;
|
||||
// Find the index for the choice we want to move up.
|
||||
for index in self.current..self.queue.len() {
|
||||
if let Some(choice) = &self.queue[index] {
|
||||
if std::ptr::eq(choice.user().as_ref(), pokemon) {
|
||||
desired_index = Some(index);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
// If we couldn't find a choice, we can't execute, return.
|
||||
if desired_index.is_none() {
|
||||
return;
|
||||
}
|
||||
let desired_index = desired_index.unwrap();
|
||||
// If the choice we want to move up is already the next choice, just return.
|
||||
if desired_index == self.current {
|
||||
return;
|
||||
}
|
||||
|
||||
// Take the choice we want to move forward out of it's place.
|
||||
let choice = self.queue[desired_index].take().unwrap();
|
||||
// Iterate backwards from the spot before the choice we want to move up, push them all back
|
||||
// by 1 spot.
|
||||
for index in (desired_index - 1)..self.current {
|
||||
self.queue.swap(index, index + 1);
|
||||
}
|
||||
// Place the choice that needs to be next in the next to be executed position.
|
||||
let _ = self.queue[self.current].insert(choice);
|
||||
}
|
||||
|
||||
/// Internal helper function to be easily able to iterate over the yet to be executed choices.
|
||||
|
|
|
@ -67,7 +67,15 @@ impl BattleRandom {
|
|||
hit_number,
|
||||
&mut chance
|
||||
);
|
||||
self.get_rng().lock().unwrap().get_float() < (chance / 100.0)
|
||||
if chance < 100.0 {
|
||||
if chance > 0.0 {
|
||||
self.get_rng().lock().unwrap().get_float() < (chance / 100.0)
|
||||
} else {
|
||||
false
|
||||
}
|
||||
} else {
|
||||
true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue