Cleaner handling of tracking seen opponents.

Instead of keeping track of unique identifiers, keep track of weak pointers to the pokemon, so we can just try and upgrade them later.
This commit is contained in:
Deukhoofd 2022-06-06 16:24:13 +02:00
parent c27ea0ae1e
commit 10e93949e4
Signed by: Deukhoofd
GPG Key ID: F63E044490819F6F
2 changed files with 14 additions and 10 deletions

View File

@ -137,10 +137,10 @@ impl<'a> BattleSide<'a> {
if side.index() == self.index { if side.index() == self.index {
continue; continue;
} }
for opponent in side.pokemon().iter().flatten() { for opponent_mutex in side.pokemon().iter().flatten() {
let mut opponent = opponent.write().unwrap(); let mut opponent = opponent_mutex.write().unwrap();
opponent.mark_opponent_as_seen(pokemon.unique_identifier()); opponent.mark_opponent_as_seen(Arc::downgrade(pokemon_mutex));
pokemon.mark_opponent_as_seen(opponent.unique_identifier()); pokemon.mark_opponent_as_seen(Arc::downgrade(opponent_mutex));
} }
} }
battle.event_hook().trigger(Event::Switch { battle.event_hook().trigger(Event::Switch {

View File

@ -16,7 +16,6 @@ use crate::static_data::statistic_set::{ClampedStatisticSet, StatisticSet};
use crate::static_data::statistics::Statistic; use crate::static_data::statistics::Statistic;
use crate::utils::random::Random; use crate::utils::random::Random;
use crate::{PkmnResult, ScriptCategory}; use crate::{PkmnResult, ScriptCategory};
use std::collections::HashSet;
use std::sync::{Arc, RwLock, Weak}; use std::sync::{Arc, RwLock, Weak};
#[derive(Debug)] #[derive(Debug)]
@ -25,7 +24,7 @@ pub struct PokemonBattleData<'a> {
battle_side_index: u8, battle_side_index: u8,
index: u8, index: u8,
on_battle_field: bool, on_battle_field: bool,
seen_opponents: HashSet<u32>, seen_opponents: Vec<Weak<RwLock<Pokemon<'a>>>>,
} }
impl<'a> PokemonBattleData<'a> { impl<'a> PokemonBattleData<'a> {
@ -38,7 +37,7 @@ impl<'a> PokemonBattleData<'a> {
pub fn on_battle_field(&'a mut self) -> &mut bool { pub fn on_battle_field(&'a mut self) -> &mut bool {
&mut self.on_battle_field &mut self.on_battle_field
} }
pub fn seen_opponents(&'a mut self) -> &'a mut HashSet<u32> { pub fn seen_opponents(&'a mut self) -> &'a mut Vec<Weak<RwLock<Pokemon<'a>>>> {
&mut self.seen_opponents &mut self.seen_opponents
} }
} }
@ -253,7 +252,7 @@ impl<'a> Pokemon<'a> {
pub fn active_ability(&self) -> &Option<Box<dyn Script>> { pub fn active_ability(&self) -> &Option<Box<dyn Script>> {
&self.ability_script &self.ability_script
} }
pub fn seen_opponents(&self) -> Option<&HashSet<u32>> { pub fn seen_opponents(&self) -> Option<&Vec<Weak<RwLock<Pokemon<'a>>>>> {
if let Some(data) = &self.battle_data { if let Some(data) = &self.battle_data {
Some(&data.seen_opponents) Some(&data.seen_opponents)
} else { } else {
@ -351,9 +350,14 @@ impl<'a> Pokemon<'a> {
} }
} }
pub fn mark_opponent_as_seen(&mut self, unique_identifier: u32) { pub fn mark_opponent_as_seen(&mut self, pokemon: Weak<RwLock<Pokemon<'a>>>) {
if let Some(battle_data) = &mut self.battle_data { if let Some(battle_data) = &mut self.battle_data {
battle_data.seen_opponents.insert(unique_identifier); for seen_opponent in &battle_data.seen_opponents {
if seen_opponent.ptr_eq(&pokemon) {
return;
}
}
battle_data.seen_opponents.push(pokemon);
} }
} }
} }