A lot more work on handling scripts properly.
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Deukhoofd 2022-06-12 17:57:39 +02:00
parent 56e1237c22
commit 2aadab5a42
Signed by: Deukhoofd
GPG Key ID: F63E044490819F6F
11 changed files with 788 additions and 175 deletions

View File

@ -50,3 +50,4 @@ failure_derive = "0.1.8"
lazy_static = "1.4.0"
hashbrown = "0.12.1"
indexmap = "1.8.2"
parking_lot = "0.12.1"

View File

@ -9,8 +9,10 @@ use crate::dynamic_data::models::battle_side::BattleSide;
use crate::dynamic_data::script_handling::script::Script;
use crate::dynamic_data::script_handling::script_set::ScriptSet;
use crate::dynamic_data::script_handling::volatile_scripts::VolatileScripts;
use crate::dynamic_data::script_handling::{ScriptSource, ScriptSourceData, ScriptWrapper};
use crate::{PkmnResult, ScriptCategory, StringKey};
use std::sync::{Arc, RwLock};
use parking_lot::RwLock;
use std::sync::Arc;
#[derive(Debug)]
pub struct Battle<'a> {
@ -27,7 +29,7 @@ pub struct Battle<'a> {
event_hook: EventHook,
history_holder: Box<HistoryHolder>,
current_turn: u32,
volatile: Arc<RwLock<ScriptSet>>,
volatile_scripts: Arc<RwLock<ScriptSet>>,
last_turn_time: i64,
}
@ -60,12 +62,12 @@ impl<'a> Battle<'a> {
event_hook: Default::default(),
history_holder: Box::new(HistoryHolder {}),
current_turn: 0,
volatile: Default::default(),
volatile_scripts: Default::default(),
last_turn_time: 0,
}));
for i in 0..number_of_sides {
battle.write().unwrap().sides[i as usize] =
battle.write().sides[i as usize] =
BattleSide::new(i, Arc::downgrade(&battle), pokemon_per_side);
}
battle
@ -164,10 +166,28 @@ impl<'a> Battle<'a> {
impl<'a> VolatileScripts<'a> for Battle<'a> {
fn volatile_scripts(&self) -> &Arc<RwLock<ScriptSet>> {
&self.volatile
&self.volatile_scripts
}
fn load_volatile_script(&self, key: &StringKey) -> PkmnResult<Option<Box<dyn Script>>> {
self.library.load_script(ScriptCategory::Battle, key)
}
}
impl<'a> ScriptSource<'a> for Battle<'a> {
fn get_script_count(&self) -> usize {
todo!()
}
fn get_script_source_data(&self) -> &RwLock<ScriptSourceData> {
todo!()
}
fn get_own_scripts(&self, scripts: &mut Vec<ScriptWrapper>) {
scripts.push((&self.volatile_scripts).into());
}
fn collect_scripts(&self, scripts: &mut Vec<ScriptWrapper>) {
self.get_own_scripts(scripts);
}
}

View File

@ -6,10 +6,11 @@ use crate::dynamic_data::models::pokemon::Pokemon;
use crate::dynamic_data::script_handling::script::Script;
use crate::dynamic_data::script_handling::script_set::ScriptSet;
use crate::dynamic_data::script_handling::volatile_scripts::VolatileScripts;
use crate::dynamic_data::script_handling::ScriptSource;
use crate::dynamic_data::script_handling::{ScriptSource, ScriptSourceData, ScriptWrapper};
use crate::{script_hook, PkmnResult, StringKey};
use parking_lot::RwLock;
use std::ops::Deref;
use std::sync::{Arc, RwLock, Weak};
use std::sync::{Arc, Weak};
#[derive(Debug)]
pub struct BattleSide<'a> {
@ -85,13 +86,12 @@ impl<'a> BattleSide<'a> {
/// empty, but can't be filled by any party anymore.
pub fn all_slots_filled(&self) -> bool {
for (i, pokemon) in self.pokemon.iter().enumerate() {
if (!pokemon.is_none() || !pokemon.as_ref().unwrap().read().unwrap().is_usable())
if (!pokemon.is_none() || !pokemon.as_ref().unwrap().read().is_usable())
&& self
.battle
.upgrade()
.unwrap()
.read()
.unwrap()
.can_slot_be_filled(self.index, i as u8)
{
return false;
@ -103,8 +103,7 @@ impl<'a> BattleSide<'a> {
pub fn set_choice(&mut self, choice: TurnChoice<'a>) {
for (index, pokemon_slot) in self.pokemon.iter().enumerate() {
if let Some(pokemon) = pokemon_slot {
if pokemon.read().unwrap().unique_identifier() == choice.user().unique_identifier()
{
if pokemon.read().unique_identifier() == choice.user().unique_identifier() {
self.choices[index] = Some(Arc::new(choice));
self.choices_set += 1;
return;
@ -120,26 +119,26 @@ impl<'a> BattleSide<'a> {
pub fn set_pokemon(&mut self, index: u8, pokemon: Option<Arc<RwLock<Pokemon<'a>>>>) {
let old = &mut self.pokemon[index as usize];
if let Some(old_pokemon) = old {
let mut p = old_pokemon.write().unwrap();
let mut p = old_pokemon.write();
script_hook!(on_remove, p,);
p.set_on_battlefield(false);
}
self.pokemon[index as usize] = pokemon;
let pokemon = &self.pokemon[index as usize];
if let Some(pokemon_mutex) = pokemon {
let mut pokemon = pokemon_mutex.write().unwrap();
let mut pokemon = pokemon_mutex.write();
pokemon.set_battle_data(self.battle.clone(), self.index);
pokemon.set_on_battlefield(true);
pokemon.set_battle_index(index);
let battle = self.battle.upgrade().unwrap();
let battle = battle.read().unwrap();
let battle = battle.read();
for side in battle.sides() {
if side.index() == self.index {
continue;
}
for opponent_mutex in side.pokemon().iter().flatten() {
let mut opponent = opponent_mutex.write().unwrap();
let mut opponent = opponent_mutex.write();
opponent.mark_opponent_as_seen(Arc::downgrade(pokemon_mutex));
pokemon.mark_opponent_as_seen(Arc::downgrade(opponent_mutex));
}
@ -152,7 +151,7 @@ impl<'a> BattleSide<'a> {
script_hook!(on_switch_in, pokemon, &pokemon);
} else {
let battle = self.battle.upgrade().unwrap();
let battle = battle.read().unwrap();
let battle = battle.read();
battle.event_hook().trigger(Event::Switch {
side_index: self.index,
index,
@ -163,7 +162,7 @@ impl<'a> BattleSide<'a> {
pub fn is_pokemon_on_side(&self, pokemon: Arc<Pokemon<'a>>) -> bool {
for p in self.pokemon.iter().flatten() {
if p.read().unwrap().unique_identifier() == pokemon.unique_identifier() {
if p.read().unique_identifier() == pokemon.unique_identifier() {
return true;
}
}
@ -173,7 +172,7 @@ impl<'a> BattleSide<'a> {
pub fn mark_slot_as_unfillable(&mut self, pokemon: &Pokemon<'a>) {
for (i, slot) in self.pokemon.iter().enumerate() {
if let Some(p) = slot {
if p.read().unwrap().deref() as *const Pokemon == pokemon as *const Pokemon {
if p.read().deref() as *const Pokemon == pokemon as *const Pokemon {
self.fillable_slots[i] = false;
return;
}
@ -184,7 +183,7 @@ impl<'a> BattleSide<'a> {
pub fn is_slot_unfillable(&self, pokemon: Arc<Pokemon<'a>>) -> bool {
for (i, slot) in self.pokemon.iter().enumerate() {
if let Some(p) = slot {
if p.read().unwrap().unique_identifier() == pokemon.unique_identifier() {
if p.read().unique_identifier() == pokemon.unique_identifier() {
return self.fillable_slots[i];
}
}
@ -215,7 +214,6 @@ impl<'a> BattleSide<'a> {
.upgrade()
.unwrap()
.read()
.unwrap()
.random()
.get_max(self.pokemon_per_side as i32) as u8
}
@ -231,7 +229,7 @@ impl<'a> BattleSide<'a> {
}
let battle = self.battle.upgrade().unwrap();
let battle = battle.read().unwrap();
let battle = battle.read();
// Fetch parties for the two indices.
let mut party_a = None;
let mut party_b = None;
@ -272,14 +270,30 @@ impl<'a> VolatileScripts<'a> for BattleSide<'a> {
.upgrade()
.unwrap()
.read()
.unwrap()
.library()
.load_script(crate::ScriptCategory::Side, key)
}
}
impl<'a> ScriptSource for BattleSide<'a> {
fn get_script_count(&self) {
impl<'a> ScriptSource<'a> for BattleSide<'a> {
fn get_script_count(&self) -> usize {
todo!()
}
fn get_script_source_data(&self) -> &RwLock<ScriptSourceData> {
todo!()
}
fn get_own_scripts(&self, scripts: &mut Vec<ScriptWrapper>) {
scripts.push((&self.volatile_scripts).into());
}
fn collect_scripts(&self, scripts: &mut Vec<ScriptWrapper>) {
self.get_own_scripts(scripts);
self.battle
.upgrade()
.unwrap()
.read()
.collect_scripts(scripts);
}
}

View File

@ -0,0 +1,164 @@
use crate::dynamic_data::models::learned_move::LearnedMove;
use crate::dynamic_data::models::pokemon::Pokemon;
use crate::dynamic_data::script_handling::script::ScriptContainer;
use crate::dynamic_data::script_handling::{ScriptSource, ScriptSourceData, ScriptWrapper};
use crate::static_data::MoveData;
use crate::{PkmnResult, PokemonError};
use parking_lot::RwLock;
use std::ops::Deref;
#[derive(Default)]
pub struct HitData {
critical: bool,
base_power: u8,
effectiveness: f32,
damage: u32,
move_type: u8,
has_failed: bool,
}
impl HitData {
pub fn is_critical(&self) -> bool {
self.critical
}
pub fn base_power(&self) -> u8 {
self.base_power
}
pub fn effectiveness(&self) -> f32 {
self.effectiveness
}
pub fn damage(&self) -> u32 {
self.damage
}
pub fn move_type(&self) -> u8 {
self.move_type
}
pub fn has_failed(&self) -> bool {
self.has_failed
}
pub fn set_critical(&mut self, value: bool) {
self.critical = value;
}
pub fn set_base_power(&mut self, value: u8) {
self.base_power = value;
}
pub fn set_effectiveness(&mut self, value: f32) {
self.effectiveness = value;
}
pub fn set_damage(&mut self, value: u32) {
self.damage = value;
}
pub fn set_move_type(&mut self, value: u8) {
self.move_type = value;
}
pub fn fail(&mut self) {
self.has_failed = true;
}
}
pub struct ExecutingMove<'a, 'b> {
number_of_hits: u8,
hits: Vec<HitData>,
user: &'a Pokemon<'b>,
chosen_move: &'a LearnedMove,
use_move: &'a MoveData,
script: ScriptContainer,
targets: Vec<Option<&'a Pokemon<'b>>>,
}
impl<'a, 'b> ExecutingMove<'a, 'b> {
pub fn new(
targets: Vec<Option<&'a Pokemon<'b>>>,
number_of_hits: u8,
user: &'a Pokemon<'b>,
chosen_move: &'a LearnedMove,
use_move: &'a MoveData,
script: ScriptContainer,
) -> Self {
let total_hits = number_of_hits as usize * targets.len();
let mut hits = Vec::with_capacity(total_hits);
for _i in 0..total_hits {
hits.push(HitData::default())
}
Self {
number_of_hits,
hits,
user,
chosen_move,
use_move,
script,
targets,
}
}
pub fn target_count(&self) -> usize {
self.targets.len()
}
pub fn number_of_hits(&self) -> u8 {
self.number_of_hits
}
pub fn user(&self) -> &'a Pokemon<'b> {
self.user
}
pub fn chosen_move(&self) -> &'a LearnedMove {
self.chosen_move
}
pub fn use_move(&self) -> &'a MoveData {
self.use_move
}
pub fn script(&self) -> &ScriptContainer {
&self.script
}
pub fn get_hit_data(&self, for_target: &Pokemon<'b>, hit: u8) -> PkmnResult<&HitData> {
for (index, target) in self.targets.iter().enumerate() {
if let Some(target) = target {
if std::ptr::eq(target.deref(), for_target) {
let i = index * self.number_of_hits as usize + hit as usize;
return Ok(&self.hits[i]);
}
}
}
Err(PokemonError::InvalidTargetRequested)
}
pub fn get_target_slice(&self, for_target: &Pokemon<'b>) -> PkmnResult<&[HitData]> {
for (index, target) in self.targets.iter().enumerate() {
if let Some(target) = target {
if std::ptr::eq(target.deref(), for_target) {
let i = index * self.number_of_hits as usize;
return Ok(&self.hits[i..i + self.number_of_hits as usize]);
}
}
}
Err(PokemonError::InvalidTargetRequested)
}
pub fn is_pokemon_target(&self, pokemon: &Pokemon<'b>) -> bool {
for target in self.targets.iter().flatten() {
if std::ptr::eq(target.deref(), pokemon) {
return true;
}
}
false
}
}
impl<'a, 'b> ScriptSource<'a> for ExecutingMove<'a, 'b> {
fn get_script_count(&self) -> usize {
1
}
fn get_script_source_data(&self) -> &RwLock<ScriptSourceData> {
todo!()
}
fn get_own_scripts(&self, scripts: &mut Vec<ScriptWrapper>) {
scripts.push((&self.script).into());
}
fn collect_scripts(&self, scripts: &mut Vec<ScriptWrapper>) {
self.get_own_scripts(scripts);
self.user.get_own_scripts(scripts);
}
}

View File

@ -4,6 +4,7 @@ pub mod battle_random;
pub mod battle_result;
pub mod battle_side;
pub mod damage_source;
pub mod executing_move;
pub mod learned_move;
pub mod pokemon;
pub mod pokemon_party;

View File

@ -4,10 +4,10 @@ use crate::dynamic_data::libraries::dynamic_library::DynamicLibrary;
use crate::dynamic_data::models::battle::Battle;
use crate::dynamic_data::models::damage_source::DamageSource;
use crate::dynamic_data::models::learned_move::LearnedMove;
use crate::dynamic_data::script_handling::script::Script;
use crate::dynamic_data::script_handling::script::{Script, ScriptContainer};
use crate::dynamic_data::script_handling::script_set::ScriptSet;
use crate::dynamic_data::script_handling::volatile_scripts::VolatileScripts;
use crate::dynamic_data::script_handling::ScriptSource;
use crate::dynamic_data::script_handling::{ScriptSource, ScriptSourceData, ScriptWrapper};
use crate::static_data::items::item::Item;
use crate::static_data::natures::Nature;
use crate::static_data::species_data::ability::Ability;
@ -19,7 +19,8 @@ use crate::static_data::statistic_set::{ClampedStatisticSet, StatisticSet};
use crate::static_data::statistics::Statistic;
use crate::utils::random::Random;
use crate::{script_hook, PkmnResult, ScriptCategory, StringKey};
use std::sync::{Arc, RwLock, Weak};
use parking_lot::RwLock;
use std::sync::{Arc, Weak};
#[derive(Debug)]
pub struct PokemonBattleData<'a> {
@ -87,8 +88,9 @@ pub struct Pokemon<'a> {
is_egg: bool,
is_caught: bool,
ability_script: Option<Arc<Box<dyn Script>>>,
status_script: Option<Box<dyn Script>>,
held_item_trigger_script: ScriptContainer,
ability_script: ScriptContainer,
status_script: ScriptContainer,
volatile: Arc<RwLock<ScriptSet>>,
}
@ -148,8 +150,9 @@ impl<'a> Pokemon<'a> {
types: form.types().to_vec(),
is_egg: false,
is_caught: false,
ability_script: None,
status_script: None,
held_item_trigger_script: ScriptContainer::default(),
ability_script: ScriptContainer::default(),
status_script: ScriptContainer::default(),
volatile: Default::default(),
};
pokemon.recalculate_flat_stats();
@ -251,7 +254,7 @@ impl<'a> Pokemon<'a> {
pub fn learned_moves(&self) -> &[Option<LearnedMove>; MAX_MOVES] {
&self.moves
}
pub fn status(&self) -> &Option<Box<dyn Script>> {
pub fn status(&self) -> &ScriptContainer {
&self.status_script
}
pub fn flat_stats(&self) -> &StatisticSet<u32> {
@ -295,7 +298,7 @@ impl<'a> Pokemon<'a> {
self.form.get_ability(self.ability_index)
}
pub fn ability_script(&self) -> &Option<Arc<Box<dyn Script>>> {
pub fn ability_script(&self) -> &ScriptContainer {
&self.ability_script
}
@ -337,7 +340,6 @@ impl<'a> Pokemon<'a> {
.upgrade()
.unwrap()
.read()
.unwrap()
.random()
.get_rng()
.lock()
@ -354,15 +356,11 @@ impl<'a> Pokemon<'a> {
}
if let Some(battle_data) = &self.battle_data {
if let Some(battle) = battle_data.battle.upgrade() {
battle
.read()
.unwrap()
.event_hook()
.trigger(Event::SpeciesChange {
pokemon: self,
species,
form,
})
battle.read().event_hook().trigger(Event::SpeciesChange {
pokemon: self,
species,
form,
})
}
}
}
@ -385,14 +383,16 @@ impl<'a> Pokemon<'a> {
.load_script(ScriptCategory::Ability, self.active_ability().name())
.unwrap();
if let Some(ability_script) = ability_script {
self.ability_script = Some(Arc::new(ability_script));
self.ability_script.set(ability_script);
// Ensure the ability script gets initialized with the parameters for the ability.
self.ability_script()
.as_ref()
.get()
.as_mut()
.unwrap()
.as_mut()
.on_initialize(self.active_ability().parameters())
} else {
self.ability_script = None;
self.ability_script.clear();
}
let old_health = self.max_health();
self.recalculate_flat_stats();
@ -406,14 +406,10 @@ impl<'a> Pokemon<'a> {
if let Some(battle_data) = &self.battle_data {
if let Some(battle) = battle_data.battle.upgrade() {
battle
.read()
.unwrap()
.event_hook()
.trigger(Event::FormChange {
pokemon: self,
form,
})
battle.read().event_hook().trigger(Event::FormChange {
pokemon: self,
form,
})
}
}
}
@ -445,7 +441,7 @@ impl<'a> Pokemon<'a> {
if let Some(data) = &mut self.battle_data {
data.on_battle_field = value;
if !value {
self.volatile.write().unwrap().clear();
self.volatile.write().clear();
self.weight = self.form.weight();
self.height = self.form.height();
}
@ -487,7 +483,7 @@ impl<'a> Pokemon<'a> {
let new_health = self.current_health() - damage;
if let Some(battle_data) = &self.battle_data {
if let Some(battle) = battle_data.battle.upgrade() {
battle.read().unwrap().event_hook().trigger(Event::Damage {
battle.read().event_hook().trigger(Event::Damage {
pokemon: self,
source,
original_health: self.current_health(),
@ -515,7 +511,6 @@ impl<'a> Pokemon<'a> {
if let Some(battle) = battle_data.battle.upgrade() {
battle
.read()
.unwrap()
.event_hook()
.trigger(Event::Faint { pokemon: self });
script_hook!(on_faint, self, self, source);
@ -526,23 +521,43 @@ impl<'a> Pokemon<'a> {
if let Some(battle) = battle_data.battle.upgrade() {
if !battle
.read()
.unwrap()
.can_slot_be_filled(battle_data.battle_side_index, battle_data.index)
{
let mut battle = battle.write().unwrap();
let mut battle = battle.write();
let side = &mut battle.sides_mut()[battle_data.battle_side_index as usize];
side.mark_slot_as_unfillable(self);
}
battle.write().unwrap().validate_battle_state();
battle.write().validate_battle_state();
}
}
}
}
impl<'a> ScriptSource for Pokemon<'a> {
fn get_script_count(&self) {
impl<'a> ScriptSource<'a> for Pokemon<'a> {
fn get_script_count(&self) -> usize {
todo!()
}
fn get_script_source_data(&self) -> &RwLock<ScriptSourceData> {
todo!()
}
fn get_own_scripts(&self, scripts: &mut Vec<ScriptWrapper>) {
scripts.push((&self.held_item_trigger_script).into());
scripts.push((&self.ability_script).into());
scripts.push((&self.status_script).into());
scripts.push((&self.volatile).into());
}
fn collect_scripts(&self, scripts: &mut Vec<ScriptWrapper>) {
self.get_own_scripts(scripts);
if let Some(battle_data) = &self.battle_data {
if let Some(battle) = battle_data.battle.upgrade() {
battle.read().sides()[battle_data.battle_side_index as usize]
.collect_scripts(scripts);
}
}
}
}
impl<'a> VolatileScripts<'a> for Pokemon<'a> {

View File

@ -1,6 +1,7 @@
use crate::dynamic_data::script_handling::script::Script;
use crate::dynamic_data::script_handling::script::{Script, ScriptContainer};
use crate::dynamic_data::script_handling::script_set::ScriptSet;
use std::sync::Weak;
use parking_lot::RwLock;
use std::sync::{Arc, Weak};
pub mod item_script;
pub mod script;
@ -12,6 +13,8 @@ macro_rules! script_hook {
($hook_name: ident, $source: ident, $($parameters: expr),*) => {
let mut aggregator = $source.get_script_iterator();
while let Some(script) = aggregator.get_next() {
let lock = &mut script.get();
let script = lock.as_mut().unwrap();
if script.is_suppressed() {
continue;
}
@ -20,47 +23,76 @@ macro_rules! script_hook {
};
}
pub trait ScriptSource {
#[derive(Default)]
pub struct ScriptSourceData {
is_initialized: bool,
scripts: Vec<ScriptWrapper>,
}
pub trait ScriptSource<'a> {
fn get_script_iterator(&self) -> ScriptAggregator {
todo!()
let lock = self.get_script_source_data();
if !lock.read().is_initialized {
let mut data = lock.write();
data.scripts = Vec::with_capacity(self.get_script_count());
self.collect_scripts(&mut data.scripts);
data.is_initialized = true;
}
ScriptAggregator::new(&lock.read().scripts as *const Vec<ScriptWrapper>)
}
fn get_script_count(&self);
fn get_script_count(&self) -> usize;
fn get_script_source_data(&self) -> &RwLock<ScriptSourceData>;
fn get_own_scripts(&self, scripts: &mut Vec<ScriptWrapper>);
fn collect_scripts(&self, scripts: &mut Vec<ScriptWrapper>);
}
pub enum ScriptWrapper {
Script(Weak<Box<dyn Script>>),
Set(Weak<ScriptSet>),
Script(Weak<RwLock<Option<Box<dyn Script>>>>),
Set(Weak<RwLock<ScriptSet>>),
}
impl From<&ScriptContainer> for ScriptWrapper {
fn from(c: &ScriptContainer) -> Self {
ScriptWrapper::Script(Arc::downgrade(c.arc()))
}
}
impl From<&Arc<RwLock<ScriptSet>>> for ScriptWrapper {
fn from(c: &Arc<RwLock<ScriptSet>>) -> Self {
ScriptWrapper::Set(Arc::downgrade(c))
}
}
pub struct ScriptAggregator {
scripts: Vec<Option<ScriptWrapper>>,
scripts: *const Vec<ScriptWrapper>,
size: i32,
index: i32,
set_index: i32,
}
impl ScriptAggregator {
pub fn new(scripts: Vec<Option<ScriptWrapper>>) -> Self {
let len = scripts.len();
Self {
scripts,
size: len as i32,
index: -1,
set_index: -1,
pub fn new(scripts: *const Vec<ScriptWrapper>) -> Self {
unsafe {
let len = scripts.as_ref().unwrap().len();
Self {
scripts,
size: len as i32,
index: -1,
set_index: -1,
}
}
}
fn increment_to_next_value(&mut self) -> bool {
if self.index != -1 {
if let Some(wrapper) = &self.scripts[self.index as usize] {
if let ScriptWrapper::Set(set) = wrapper {
if let Some(set) = set.upgrade() {
self.set_index += 1;
if self.set_index as usize >= set.count() {
self.set_index = -1;
} else {
return true;
}
let wrapper = unsafe { &self.scripts.as_ref().unwrap()[self.index as usize] };
if let ScriptWrapper::Set(set) = wrapper {
if let Some(set) = set.upgrade() {
self.set_index += 1;
if self.set_index as usize >= set.read().count() {
self.set_index = -1;
} else {
return true;
}
}
}
@ -68,14 +100,15 @@ impl ScriptAggregator {
self.index += 1;
for index in self.index..self.size {
self.index = index;
if let Some(wrapper) = &self.scripts[index as usize] {
if let ScriptWrapper::Set(s) = wrapper {
if let Some(..) = s.upgrade() {
self.set_index = 0;
return true;
}
} else if let ScriptWrapper::Script(script) = wrapper {
if let Some(..) = script.upgrade() {
let wrapper = unsafe { &self.scripts.as_ref().unwrap()[self.index as usize] };
if let ScriptWrapper::Set(s) = wrapper {
if let Some(..) = s.upgrade() {
self.set_index = 0;
return true;
}
} else if let ScriptWrapper::Script(script) = wrapper {
if let Some(v) = script.upgrade() {
if let Some(..) = v.read().as_ref() {
return true;
}
}
@ -85,18 +118,318 @@ impl ScriptAggregator {
false
}
pub fn get_next(&mut self) -> Option<&Box<dyn Script>> {
pub fn get_next(&mut self) -> Option<ScriptContainer> {
if !self.increment_to_next_value() {
return None;
}
return match self.scripts[self.index as usize].as_ref().unwrap() {
// We can make this unsafe as we know there is a strong reference. This is validated in
// increment_to_next_value
ScriptWrapper::Script(script) => unsafe { Some(&*script.as_ptr()) },
ScriptWrapper::Set(set) => unsafe {
let r = (&*set.as_ptr()).at(self.set_index as usize);
Some(r.as_ref())
},
};
unsafe {
return match &self.scripts.as_ref().unwrap()[self.index as usize] {
// increment_to_next_value
ScriptWrapper::Script(script) => Some(script.upgrade().unwrap().into()),
ScriptWrapper::Set(set) => {
let lock = set.as_ptr().as_ref().unwrap();
let l = lock.read();
let sc = l.at(self.set_index as usize);
return Some(sc.clone());
}
};
}
}
pub fn reset(&mut self) {
self.index = -1;
self.set_index = -1;
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::dynamic_data::script_handling::script::ScriptContainer;
use crate::static_data::EffectParameter;
use crate::StringKey;
use std::any::Any;
pub struct TestScript {
name: StringKey,
test_count: usize,
}
impl TestScript {
fn new() -> Self {
Self {
name: StringKey::new("test"),
test_count: 0,
}
}
fn new_with_name(name: &str) -> Self {
Self {
name: StringKey::new(name),
test_count: 0,
}
}
}
impl Script for TestScript {
fn name(&self) -> &StringKey {
&self.name
}
fn get_suppressed_count(&self) -> usize {
0
}
fn add_suppression(&self) {}
fn remove_suppression(&self) {}
fn on_initialize(&mut self, _pars: &[EffectParameter]) {
self.test_count += 1;
}
fn as_any(&self) -> &dyn Any {
self
}
}
#[test]
fn script_aggregator_property_iterates_single_script() {
let script = ScriptContainer::new(Box::new(TestScript::new()));
let scripts = vec![ScriptWrapper::from(&script)];
let mut aggregator = ScriptAggregator::new(&scripts as *const Vec<ScriptWrapper>);
while let Some(v) = aggregator.get_next() {
v.get().as_mut().unwrap().on_initialize(&[]);
}
let a = script.get_as::<TestScript>();
assert_eq!(a.test_count, 1);
}
#[test]
fn script_aggregator_property_iterates_single_script_with_resets() {
let script = ScriptContainer::new(Box::new(TestScript::new()));
let scripts = vec![ScriptWrapper::from(&script)];
let mut aggregator = ScriptAggregator::new(&scripts as *const Vec<ScriptWrapper>);
for i in 1..11 {
aggregator.reset();
while let Some(v) = aggregator.get_next() {
v.get().as_mut().unwrap().on_initialize(&[]);
}
let a = script.get_as::<TestScript>();
assert_eq!(a.test_count, i);
}
}
#[test]
fn script_aggregator_property_iterates_three_script() {
let script1 = ScriptContainer::new(Box::new(TestScript::new()));
let script2 = ScriptContainer::new(Box::new(TestScript::new()));
let script3 = ScriptContainer::new(Box::new(TestScript::new()));
let scripts = vec![
ScriptWrapper::from(&script1),
ScriptWrapper::from(&script2),
ScriptWrapper::from(&script3),
];
let mut aggregator = ScriptAggregator::new(&scripts as *const Vec<ScriptWrapper>);
while let Some(v) = aggregator.get_next() {
v.get().as_mut().unwrap().on_initialize(&[]);
}
let a = script1.get_as::<TestScript>();
assert_eq!(a.test_count, 1);
let a = script2.get_as::<TestScript>();
assert_eq!(a.test_count, 1);
let a = script3.get_as::<TestScript>();
assert_eq!(a.test_count, 1);
}
#[test]
fn script_aggregator_property_iterates_three_script_with_resets() {
let script1 = ScriptContainer::new(Box::new(TestScript::new()));
let script2 = ScriptContainer::new(Box::new(TestScript::new()));
let script3 = ScriptContainer::new(Box::new(TestScript::new()));
let scripts = vec![
ScriptWrapper::from(&script1),
ScriptWrapper::from(&script2),
ScriptWrapper::from(&script3),
];
let mut aggregator = ScriptAggregator::new(&scripts as *const Vec<ScriptWrapper>);
for i in 1..11 {
aggregator.reset();
while let Some(v) = aggregator.get_next() {
v.get().as_mut().unwrap().on_initialize(&[]);
}
let a = script1.get_as::<TestScript>();
assert_eq!(a.test_count, i);
let a = script2.get_as::<TestScript>();
assert_eq!(a.test_count, i);
let a = script3.get_as::<TestScript>();
assert_eq!(a.test_count, i);
}
}
#[test]
fn script_aggregator_property_iterates_script_set() {
let set = Arc::new(RwLock::new(ScriptSet::default()));
let mut mut_set = set.write();
mut_set.add(Box::new(TestScript::new_with_name("test_a")));
mut_set.add(Box::new(TestScript::new_with_name("test_b")));
mut_set.add(Box::new(TestScript::new_with_name("test_c")));
// Drop so we don't have a lock on it anymore.
drop(mut_set);
let scripts = vec![ScriptWrapper::from(&set)];
let mut aggregator = ScriptAggregator::new(&scripts as *const Vec<ScriptWrapper>);
for i in 1..11 {
aggregator.reset();
while let Some(v) = aggregator.get_next() {
v.get().as_mut().unwrap().on_initialize(&[]);
}
let set = set.read();
let s = set.at(0).get_as::<TestScript>();
assert_eq!(s.test_count, i);
assert_eq!(s.name().str(), "test_a");
let s = set.at(1).get_as::<TestScript>();
assert_eq!(s.test_count, i);
assert_eq!(s.name().str(), "test_b");
let s = set.at(2).get_as::<TestScript>();
assert_eq!(s.test_count, i);
assert_eq!(s.name().str(), "test_c");
}
}
#[test]
fn script_aggregator_property_iterates_script_set_when_removing_last() {
let set = Arc::new(RwLock::new(ScriptSet::default()));
let mut mut_set = set.write();
mut_set.add(Box::new(TestScript::new_with_name("test_a")));
mut_set.add(Box::new(TestScript::new_with_name("test_b")));
mut_set.add(Box::new(TestScript::new_with_name("test_c")));
// Drop so we don't have a lock on it anymore.
drop(mut_set);
let scripts = vec![ScriptWrapper::from(&set)];
let mut aggregator = ScriptAggregator::new(&scripts as *const Vec<ScriptWrapper>);
assert_eq!(
aggregator
.get_next()
.unwrap()
.get()
.as_mut()
.unwrap()
.name()
.str(),
"test_a"
);
assert_eq!(
aggregator
.get_next()
.unwrap()
.get()
.as_mut()
.unwrap()
.name()
.str(),
"test_b"
);
let mut mut_set = set.write();
mut_set.remove(&StringKey::new("test_c"));
drop(mut_set);
assert!(aggregator.get_next().is_none());
}
#[test]
fn script_aggregator_property_iterates_script_set_when_removing_middle() {
let set = Arc::new(RwLock::new(ScriptSet::default()));
let mut mut_set = set.write();
mut_set.add(Box::new(TestScript::new_with_name("test_a")));
mut_set.add(Box::new(TestScript::new_with_name("test_b")));
mut_set.add(Box::new(TestScript::new_with_name("test_c")));
// Drop so we don't have a lock on it anymore.
drop(mut_set);
let scripts = vec![ScriptWrapper::from(&set)];
let mut aggregator = ScriptAggregator::new(&scripts as *const Vec<ScriptWrapper>);
assert_eq!(
aggregator
.get_next()
.unwrap()
.get()
.as_mut()
.unwrap()
.name()
.str(),
"test_a"
);
let mut mut_set = set.write();
mut_set.remove(&StringKey::new("test_b"));
drop(mut_set);
assert_eq!(
aggregator
.get_next()
.unwrap()
.get()
.as_mut()
.unwrap()
.name()
.str(),
"test_c"
);
assert!(aggregator.get_next().is_none());
}
pub struct TestScriptSource {
pub data: RwLock<ScriptSourceData>,
pub script: ScriptContainer,
}
impl<'a> ScriptSource<'a> for TestScriptSource {
fn get_script_count(&self) -> usize {
1
}
fn get_script_source_data(&self) -> &RwLock<ScriptSourceData> {
&self.data
}
fn get_own_scripts(&self, scripts: &mut Vec<ScriptWrapper>) {
scripts.push((&self.script).into());
}
fn collect_scripts(&self, scripts: &mut Vec<ScriptWrapper>) {
self.get_own_scripts(scripts);
}
}
#[test]
fn script_source_set_script_then_rerun() {
let source = TestScriptSource {
data: RwLock::new(ScriptSourceData::default()),
script: ScriptContainer::default(),
};
let mut aggregator = source.get_script_iterator();
aggregator.reset();
assert!(aggregator.get_next().is_none());
aggregator.reset();
source.script.set(Box::new(TestScript::new()));
assert!(aggregator.get_next().is_some());
}
#[test]
fn script_source_clear_script_then_rerun() {
let source = TestScriptSource {
data: RwLock::new(ScriptSourceData::default()),
script: ScriptContainer::default(),
};
let mut aggregator = source.get_script_iterator();
source.script.set(Box::new(TestScript::new()));
assert!(aggregator.get_next().is_some());
aggregator.reset();
source.script.clear();
assert!(aggregator.get_next().is_none());
}
}

View File

@ -2,7 +2,10 @@ use crate::dynamic_data::models::damage_source::DamageSource;
use crate::dynamic_data::models::pokemon::Pokemon;
use crate::static_data::moves::secondary_effect::EffectParameter;
use crate::StringKey;
use parking_lot::{MappedRwLockReadGuard, RwLock, RwLockReadGuard, RwLockWriteGuard};
use std::any::Any;
use std::fmt::{Debug, Formatter};
use std::sync::Arc;
pub trait Script {
fn name(&self) -> &StringKey;
@ -15,50 +18,59 @@ pub trait Script {
fn remove_suppression(&self);
// FIXME: add missing parameters
fn stack(&self);
fn on_remove(&self);
fn on_initialize(&self, pars: &Vec<EffectParameter>);
fn on_before_turn(&self);
fn change_speed(&self);
fn change_priority(&self);
fn change_attack(&self);
fn change_number_of_hits(&self);
fn prevent_attack(&self);
fn fail_attack(&self);
fn stop_before_attack(&self);
fn on_before_attack(&self);
fn fail_incoming_attack(&self);
fn is_invulnerable(&self);
fn on_attack_miss(&self);
fn change_attack_type(&self);
fn block_critical(&self);
fn override_base_power(&self);
fn change_damage_stats_user(&self);
fn bypass_defensive_stat(&self);
fn bypass_offensive_stat(&self);
fn change_stat_modifier(&self);
fn change_damage_modifier(&self);
fn change_damage(&self);
fn change_incoming_damage(&self);
fn on_incoming_hit(&self);
fn on_opponent_faints(&self);
fn prevent_stat_boost_change(&self);
fn change_stat_boost_change(&self);
fn on_secondary_effect(&self);
fn on_after_hits(&self);
fn prevent_self_switch(&self);
fn prevent_opponent_switch(&self);
fn modify_effect_chance(&self);
fn modify_incoming_effect_change(&self);
fn on_fail(&self);
fn on_opponent_fail(&self);
fn prevent_self_run_away(&self);
fn prevent_opponent_run_away(&self);
fn on_end_turn(&self);
fn on_damage(&self, pokemon: &Pokemon, source: DamageSource, old_health: u32, new_health: u32);
fn on_faint(&self, pokemon: &Pokemon, source: DamageSource);
fn on_switch_in(&self, pokemon: &Pokemon);
fn on_after_held_item_consume(&self);
fn stack(&self) {}
fn on_remove(&self) {}
fn on_initialize(&mut self, _pars: &[EffectParameter]) {}
fn on_before_turn(&self) {}
fn change_speed(&self) {}
fn change_priority(&self) {}
fn change_attack(&self) {}
fn change_number_of_hits(&self) {}
fn prevent_attack(&self) {}
fn fail_attack(&self) {}
fn stop_before_attack(&self) {}
fn on_before_attack(&self) {}
fn fail_incoming_attack(&self) {}
fn is_invulnerable(&self) {}
fn on_attack_miss(&self) {}
fn change_attack_type(&self) {}
fn block_critical(&self) {}
fn override_base_power(&self) {}
fn change_damage_stats_user(&self) {}
fn bypass_defensive_stat(&self) {}
fn bypass_offensive_stat(&self) {}
fn change_stat_modifier(&self) {}
fn change_damage_modifier(&self) {}
fn change_damage(&self) {}
fn change_incoming_damage(&self) {}
fn on_incoming_hit(&self) {}
fn on_opponent_faints(&self) {}
fn prevent_stat_boost_change(&self) {}
fn change_stat_boost_change(&self) {}
fn on_secondary_effect(&self) {}
fn on_after_hits(&self) {}
fn prevent_self_switch(&self) {}
fn prevent_opponent_switch(&self) {}
fn modify_effect_chance(&self) {}
fn modify_incoming_effect_change(&self) {}
fn on_fail(&self) {}
fn on_opponent_fail(&self) {}
fn prevent_self_run_away(&self) {}
fn prevent_opponent_run_away(&self) {}
fn on_end_turn(&self) {}
fn on_damage(
&self,
_pokemon: &Pokemon,
_source: DamageSource,
_old_health: u32,
_new_health: u32,
) {
}
fn on_faint(&self, _pokemon: &Pokemon, _source: DamageSource) {}
fn on_switch_in(&self, _pokemon: &Pokemon) {}
fn on_after_held_item_consume(&self) {}
fn as_any(&self) -> &dyn Any;
}
impl Debug for dyn Script {
@ -66,3 +78,48 @@ impl Debug for dyn Script {
Ok(())
}
}
type ScriptHolder = Arc<RwLock<Option<Box<dyn Script>>>>;
#[derive(Default, Debug, Clone)]
pub struct ScriptContainer {
script: ScriptHolder,
}
impl ScriptContainer {
pub fn new(script: Box<dyn Script>) -> ScriptContainer {
Self {
script: Arc::new(RwLock::new(Some(script))),
}
}
pub fn get(&self) -> RwLockWriteGuard<'_, Option<Box<dyn Script>>> {
self.script.write()
}
pub fn set(&self, script: Box<dyn Script>) {
self.script.write().replace(script);
}
pub fn clear(&self) {
self.script.write().take();
}
pub fn arc(&self) -> &ScriptHolder {
&self.script
}
pub fn get_as<T: 'static>(&self) -> MappedRwLockReadGuard<T> {
RwLockReadGuard::map(self.script.read(), |a| unsafe {
let ptr = a.as_ref().as_ref().unwrap().as_ref() as *const dyn Script;
let any = ptr.as_ref().unwrap().as_any();
any.downcast_ref::<T>().unwrap()
})
}
}
impl From<ScriptHolder> for ScriptContainer {
fn from(a: ScriptHolder) -> Self {
Self { script: a }
}
}

View File

@ -1,21 +1,23 @@
use crate::dynamic_data::script_handling::script::Script;
use crate::dynamic_data::script_handling::script::{Script, ScriptContainer};
use crate::{PkmnResult, StringKey};
use indexmap::IndexMap;
use std::sync::Arc;
#[derive(Debug, Default)]
pub struct ScriptSet {
scripts: IndexMap<StringKey, Arc<Box<dyn Script>>>,
scripts: IndexMap<StringKey, ScriptContainer>,
}
impl ScriptSet {
pub fn add(&mut self, script: Box<dyn Script>) -> Arc<Box<dyn Script>> {
if let Some(existing) = self.scripts.get(script.name()) {
existing.stack();
return existing.clone();
pub fn add(&mut self, script: Box<dyn Script>) -> ScriptContainer {
if let Some(lock) = self.scripts.get(script.name()) {
let existing = lock.get();
if let Some(v) = &*existing {
v.stack();
return lock.clone();
}
}
let arc = Arc::new(script);
self.scripts.insert(arc.name().clone(), arc.clone());
self.scripts
.insert(script.name().clone(), ScriptContainer::new(script));
self.scripts.last().unwrap().1.clone()
}
@ -23,38 +25,42 @@ impl ScriptSet {
&mut self,
key: &StringKey,
instantiation: &'b F,
) -> PkmnResult<Option<Arc<Box<dyn Script>>>>
) -> PkmnResult<Option<ScriptContainer>>
where
F: Fn() -> PkmnResult<Option<Box<dyn Script>>>,
{
if let Some(existing) = self.scripts.get(key) {
existing.stack();
return Ok(Some(existing.clone()));
if let Some(lock) = self.scripts.get(key) {
let existing = lock.get();
if let Some(v) = &*existing {
v.stack();
return Ok(Some(lock.clone()));
}
}
let script = instantiation()?;
if let Some(script) = script {
let arc = Arc::new(script);
self.scripts.insert(arc.name().clone(), arc.clone());
let name = script.name().clone();
let arc = ScriptContainer::new(script);
self.scripts.insert(name, arc.clone());
Ok(Some(self.scripts.last().unwrap().1.clone()))
} else {
Ok(None)
}
}
pub fn get(&self, key: &StringKey) -> Option<&Arc<Box<dyn Script>>> {
pub fn get(&self, key: &StringKey) -> Option<&ScriptContainer> {
self.scripts.get(key)
}
pub fn remove(&mut self, key: &StringKey) {
let value = self.scripts.shift_remove(key);
if let Some(script) = value {
script.on_remove();
script.get().as_ref().as_ref().unwrap().on_remove();
}
}
pub fn clear(&mut self) {
for script in &self.scripts {
script.1.on_remove();
script.1.get().as_ref().as_ref().unwrap().on_remove();
}
self.scripts.clear();
}
@ -63,7 +69,7 @@ impl ScriptSet {
self.scripts.contains_key(key)
}
pub fn at(&self, index: usize) -> &Arc<Box<dyn Script>> {
pub fn at(&self, index: usize) -> &ScriptContainer {
&self.scripts[index]
}

View File

@ -1,30 +1,30 @@
use crate::dynamic_data::script_handling::script::Script;
use crate::dynamic_data::script_handling::script::{Script, ScriptContainer};
use crate::dynamic_data::script_handling::script_set::ScriptSet;
use crate::{PkmnResult, StringKey};
use std::sync::{Arc, RwLock};
use parking_lot::RwLock;
use std::sync::Arc;
pub trait VolatileScripts<'a> {
fn volatile_scripts(&self) -> &Arc<RwLock<ScriptSet>>;
fn load_volatile_script(&self, key: &StringKey) -> PkmnResult<Option<Box<dyn Script>>>;
fn has_volatile_script(&self, key: &StringKey) -> bool {
self.volatile_scripts().read().unwrap().has(key)
self.volatile_scripts().read().has(key)
}
fn get_volatile_script(&self, key: &StringKey) -> Option<Arc<Box<dyn Script>>> {
let scripts = self.volatile_scripts().read().unwrap();
fn get_volatile_script(&self, key: &StringKey) -> Option<ScriptContainer> {
let scripts = self.volatile_scripts().read();
let s = scripts.get(key);
s.cloned()
}
fn add_volatile_script(&mut self, key: &StringKey) -> PkmnResult<Option<Arc<Box<dyn Script>>>> {
fn add_volatile_script(&mut self, key: &StringKey) -> PkmnResult<Option<ScriptContainer>> {
self.volatile_scripts()
.write()
.unwrap()
.stack_or_add(key, &|| self.load_volatile_script(key))
}
fn remove_volatile_script(&mut self, key: &StringKey) {
self.volatile_scripts().write().unwrap().remove(key)
self.volatile_scripts().write().remove(key)
}
}

View File

@ -4,6 +4,7 @@
#![feature(bench_black_box)]
#![feature(let_chains)]
extern crate core;
extern crate lazy_static;
use crate::dynamic_data::libraries::script_resolver::ScriptCategory;
@ -23,6 +24,7 @@ pub enum PokemonError {
name: String,
},
MiscError,
InvalidTargetRequested,
}
pub type PkmnResult<T> = Result<T, PokemonError>;