Don't store the TurnChoices as Arc<RwLock>>, to prevent locking issues.
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2022-06-18 09:49:17 +02:00
parent 94403e2ca1
commit 09c2533bf5
6 changed files with 33 additions and 46 deletions

View File

@@ -237,35 +237,35 @@ impl<'own, 'library> Battle<'own, 'library> {
let start_time = chrono::Utc::now();
let mut choices = Vec::with_capacity(self.number_of_sides as usize * self.pokemon_per_side as usize);
for side in &mut self.sides {
for choice in side.choices() {
if choice.is_none() {
for choice_opt in side.choices_mut() {
if choice_opt.is_none() {
panic!("Choice was none, but all choices were set? Logic error.");
}
let mut choice_guard = choice.as_ref().unwrap().write();
let c = choice_guard.deref();
let mut choice = choice_opt.as_mut().unwrap();
let c = choice.deref();
if let TurnChoice::Move(data) = c {
let mut change_priority = data.priority();
script_hook!(change_priority, c, c, &mut change_priority);
if change_priority != data.priority() {
if let TurnChoice::Move(data) = choice_guard.deref_mut() {
if let TurnChoice::Move(data) = choice.deref_mut() {
*data.priority_mut() = change_priority;
}
}
}
let mut speed = choice_guard.speed();
let c = choice_guard.deref();
let mut speed = choice.speed();
let c = choice.deref();
script_hook!(change_speed, c, c, &mut speed);
*choice_guard.speed_mut() = speed;
*choice.speed_mut() = speed;
choice_guard.set_random_value(self.random.get() as u32);
choices.push(choice.as_ref().unwrap().clone());
choice.set_random_value(self.random.get() as u32);
choices.push(choice_opt.take().unwrap());
}
side.reset_choices();
}
self.current_turn += 1;
choices.sort_unstable_by(|a, b| b.read().deref().cmp(a.read().deref()));
choices.sort_unstable_by(|a, b| b.cmp(a));
self.current_turn_queue = Some(Arc::new(RwLock::new(ChoiceQueue::new(choices))));
{

View File

@@ -16,7 +16,7 @@ pub struct BattleSide<'own, 'library> {
index: u8,
pokemon_per_side: u8,
pokemon: Vec<Option<Arc<RwLock<Pokemon<'own, 'library>>>>>,
choices: Vec<Option<Arc<RwLock<TurnChoice<'own, 'library>>>>>,
choices: Vec<Option<TurnChoice<'own, 'library>>>,
fillable_slots: Vec<bool>,
choices_set: u8,
battle: *mut Battle<'own, 'library>,
@@ -65,10 +65,10 @@ impl<'own, 'library> BattleSide<'own, 'library> {
pub fn pokemon(&self) -> &Vec<Option<Arc<RwLock<Pokemon<'own, 'library>>>>> {
&self.pokemon
}
pub fn choices(&self) -> &Vec<Option<Arc<RwLock<TurnChoice<'own, 'library>>>>> {
pub fn choices(&self) -> &Vec<Option<TurnChoice<'own, 'library>>> {
&self.choices
}
pub fn choices_mut(&mut self) -> &mut Vec<Option<Arc<RwLock<TurnChoice<'own, 'library>>>>> {
pub fn choices_mut(&mut self) -> &mut Vec<Option<TurnChoice<'own, 'library>>> {
&mut self.choices
}
@@ -110,7 +110,7 @@ impl<'own, 'library> BattleSide<'own, 'library> {
for (index, pokemon_slot) in self.pokemon.iter().enumerate() {
if let Some(pokemon) = pokemon_slot {
if std::ptr::eq(pokemon.data_ptr(), choice.user().data_ptr()) {
self.choices[index] = Some(Arc::new(RwLock::new(choice)));
self.choices[index] = Some(choice);
self.choices_set += 1;
return;
}