Initial work on rune as scripting library
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
2024-04-07 18:55:41 +02:00
parent 6379abf446
commit 67b0abe59f
24 changed files with 1186 additions and 739 deletions

View File

@@ -216,21 +216,13 @@ impl Pokemon {
}
/// The library data of the Pokemon.
pub fn library(&self) -> &Arc<dyn DynamicLibrary> {
&self.data.library
}
pub fn library(&self) -> &Arc<dyn DynamicLibrary> { &self.data.library }
/// The species of the Pokemon.
pub fn species(&self) -> Arc<dyn Species> {
self.data.species.read().clone()
}
pub fn species(&self) -> Arc<dyn Species> { self.data.species.read().clone() }
/// The form of the Pokemon.
pub fn form(&self) -> Arc<dyn Form> {
self.data.form.read().clone()
}
pub fn form(&self) -> Arc<dyn Form> { self.data.form.read().clone() }
/// Whether or not the Pokemon is showing as a different species than it actually is.
pub fn has_different_display_species(&self) -> bool {
self.data.display_species.is_some()
}
pub fn has_different_display_species(&self) -> bool { self.data.display_species.is_some() }
/// The species that should be displayed to the user. This handles stuff like the Illusion ability.
pub fn display_species(&self) -> Arc<dyn Species> {
if let Some(v) = &self.data.display_species {
@@ -240,9 +232,7 @@ impl Pokemon {
}
}
/// Whether or not the Pokemon is showing as a different form than it actually is.
pub fn has_different_display_form(&self) -> bool {
self.data.display_form.is_some()
}
pub fn has_different_display_form(&self) -> bool { self.data.display_form.is_some() }
/// The form that should be displayed to the user. This handles stuff like the Illusion ability.
pub fn display_form(&self) -> Arc<dyn Form> {
if let Some(v) = &self.data.display_form {
@@ -253,32 +243,20 @@ impl Pokemon {
}
/// The current level of the Pokemon.
/// [See also](https://bulbapedia.bulbagarden.net/wiki/Level)
pub fn level(&self) -> LevelInt {
self.data.level.load(Ordering::Relaxed)
}
pub fn level(&self) -> LevelInt { self.data.level.load(Ordering::Relaxed) }
/// The amount of experience of the Pokemon.
/// [See also](https://bulbapedia.bulbagarden.net/wiki/Experience)
pub fn experience(&self) -> u32 {
self.data.experience.load(Ordering::Relaxed)
}
pub fn experience(&self) -> u32 { self.data.experience.load(Ordering::Relaxed) }
/// The personality value of the Pokemon.
/// [See also](https://bulbapedia.bulbagarden.net/wiki/Personality_value)
pub fn personality_value(&self) -> u32 {
self.data.personality_value
}
pub fn personality_value(&self) -> u32 { self.data.personality_value }
/// The gender of the Pokemon.
pub fn gender(&self) -> Gender {
*self.data.gender.read()
}
pub fn gender(&self) -> Gender { *self.data.gender.read() }
/// The coloring of the Pokemon. Value 0 is the default, value 1 means shiny. Other values are
/// currently not used, and can be used for other implementations.
pub fn coloring(&self) -> u8 {
self.data.coloring
}
pub fn coloring(&self) -> u8 { self.data.coloring }
/// Gets the held item of a Pokemon
pub fn held_item(&self) -> &RwLock<Option<Arc<dyn Item>>> {
&self.data.held_item
}
pub fn held_item(&self) -> &RwLock<Option<Arc<dyn Item>>> { &self.data.held_item }
/// Checks whether the Pokemon is holding a specific item.
pub fn has_held_item(&self, name: &StringKey) -> bool {
// Only true if we have an item, and the item name is the same as the requested item.
@@ -292,9 +270,7 @@ impl Pokemon {
self.data.held_item.write().replace(item.clone())
}
/// Removes the held item from the Pokemon. Returns the previously held item.
pub fn remove_held_item(&self) -> Option<Arc<dyn Item>> {
self.data.held_item.write().take()
}
pub fn remove_held_item(&self) -> Option<Arc<dyn Item>> { self.data.held_item.write().take() }
/// Makes the Pokemon uses its held item.
pub fn consume_held_item(&self) -> Result<bool> {
if self.data.held_item.read().is_none() {
@@ -316,72 +292,42 @@ impl Pokemon {
}
/// The remaining health points of the Pokemon.
pub fn current_health(&self) -> u32 {
self.data.current_health.load(Ordering::Relaxed)
}
pub fn current_health(&self) -> u32 { self.data.current_health.load(Ordering::Relaxed) }
/// The max health points of the Pokemon.
pub fn max_health(&self) -> u32 {
self.data.boosted_stats.hp()
}
pub fn max_health(&self) -> u32 { self.data.boosted_stats.hp() }
/// The weight of the Pokemon in kilograms.
pub fn weight(&self) -> f32 {
self.data.weight.load(Ordering::Relaxed)
}
pub fn weight(&self) -> f32 { self.data.weight.load(Ordering::Relaxed) }
/// Sets the weight of the Pokemon in kilograms.
pub fn set_weight(&self, weight: f32) {
self.data.weight.store(weight, Ordering::Relaxed)
}
pub fn set_weight(&self, weight: f32) { self.data.weight.store(weight, Ordering::Relaxed) }
/// The height of the Pokemon in meters.
pub fn height(&self) -> f32 {
self.data.height.load(Ordering::Relaxed)
}
pub fn height(&self) -> f32 { self.data.height.load(Ordering::Relaxed) }
/// The current happiness of the Pokemon. Also known as friendship.
pub fn happiness(&self) -> u8 {
self.data.happiness.load(Ordering::Relaxed)
}
pub fn happiness(&self) -> u8 { self.data.happiness.load(Ordering::Relaxed) }
/// An optional nickname of the Pokemon.
pub fn nickname(&self) -> &Option<String> {
&self.data.nickname
}
pub fn nickname(&self) -> &Option<String> { &self.data.nickname }
/// An index of the ability to find the actual ability on the form.
pub fn real_ability(&self) -> &AbilityIndex {
&self.data.ability_index
}
pub fn real_ability(&self) -> &AbilityIndex { &self.data.ability_index }
/// The current types of the Pokemon.
pub fn types(&self) -> RwLockReadGuard<'_, RawRwLock, Vec<TypeIdentifier>> {
self.data.types.read()
}
pub fn types(&self) -> RwLockReadGuard<'_, RawRwLock, Vec<TypeIdentifier>> { self.data.types.read() }
/// The moves the Pokemon has learned. This is of a set length of [`MAX_MOVES`]. Empty move slots
/// are defined by None.
pub fn learned_moves(&self) -> &RwLock<[Option<Arc<LearnedMove>>; MAX_MOVES]> {
&self.data.moves
}
pub fn learned_moves(&self) -> &RwLock<[Option<Arc<LearnedMove>>; MAX_MOVES]> { &self.data.moves }
/// The stats of the Pokemon when disregarding any stat boosts.
pub fn flat_stats(&self) -> &Arc<StatisticSet<u32>> {
&self.data.flat_stats
}
pub fn flat_stats(&self) -> &Arc<StatisticSet<u32>> { &self.data.flat_stats }
/// The amount of boosts on a specific stat.
pub fn stat_boosts(&self) -> &Arc<ClampedStatisticSet<i8, -6, 6>> {
&self.data.stat_boost
}
pub fn stat_boosts(&self) -> &Arc<ClampedStatisticSet<i8, -6, 6>> { &self.data.stat_boost }
/// Whether or not this Pokemon is still an egg, and therefore cannot battle.
pub fn is_egg(&self) -> bool {
self.data.is_egg
}
pub fn is_egg(&self) -> bool { self.data.is_egg }
/// The stats of the Pokemon including the stat boosts
pub fn boosted_stats(&self) -> &Arc<StatisticSet<u32>> {
&self.data.boosted_stats
}
pub fn boosted_stats(&self) -> &Arc<StatisticSet<u32>> { &self.data.boosted_stats }
/// Get the stat boosts for a specific stat.
pub fn stat_boost(&self, stat: Statistic) -> i8 {
self.data.stat_boost.get_stat(stat)
}
pub fn stat_boost(&self, stat: Statistic) -> i8 { self.data.stat_boost.get_stat(stat) }
/// Change a boosted stat by a certain amount.
pub fn change_stat_boost(&self, stat: Statistic, mut diff_amount: i8, self_inflicted: bool) -> Result<bool> {
let mut prevent = false;
@@ -440,14 +386,10 @@ impl Pokemon {
/// Gets an individual value of the Pokemon.
/// [See also](https://bulbapedia.bulbagarden.net/wiki/Individual_values)
pub fn individual_values(&self) -> &Arc<ClampedStatisticSet<u8, 0, 31>> {
&self.data.individual_values
}
pub fn individual_values(&self) -> &Arc<ClampedStatisticSet<u8, 0, 31>> { &self.data.individual_values }
/// Gets an effort value of the Pokemon.
/// [See also](https://bulbapedia.bulbagarden.net/wiki/Effort_values)
pub fn effort_values(&self) -> &Arc<ClampedStatisticSet<u8, 0, 252>> {
&self.data.effort_values
}
pub fn effort_values(&self) -> &Arc<ClampedStatisticSet<u8, 0, 252>> { &self.data.effort_values }
/// Gets the battle the battle is currently in.
pub fn get_battle(&self) -> Option<Battle> {
@@ -469,13 +411,9 @@ impl Pokemon {
}
/// Get the index of the slot on the side of the battle the Pokemon is in. Only returns a value
/// if the Pokemon is on the battlefield.
pub fn get_battle_index(&self) -> Option<u8> {
self.data.battle_data.read().as_ref().map(|data| data.index())
}
pub fn get_battle_index(&self) -> Option<u8> { self.data.battle_data.read().as_ref().map(|data| data.index()) }
/// Returns whether something overrides the ability.
pub fn is_ability_overriden(&self) -> bool {
self.data.override_ability.is_some()
}
pub fn is_ability_overriden(&self) -> bool { self.data.override_ability.is_some() }
/// Returns the currently active ability.
pub fn active_ability(&self) -> Result<Arc<dyn Ability>> {
if let Some(v) = &self.data.override_ability {
@@ -496,25 +434,17 @@ impl Pokemon {
}
/// The script for the status.
pub fn status(&self) -> &ScriptContainer {
&self.data.status_script
}
pub fn status(&self) -> &ScriptContainer { &self.data.status_script }
/// Returns the script for the currently active ability.
pub fn ability_script(&self) -> &ScriptContainer {
&self.data.ability_script
}
pub fn ability_script(&self) -> &ScriptContainer { &self.data.ability_script }
/// Whether or not the Pokemon is allowed to gain experience.
pub fn allowed_experience_gain(&self) -> bool {
self.data.allowed_experience
}
pub fn allowed_experience_gain(&self) -> bool { self.data.allowed_experience }
/// The nature of the Pokemon.
/// [See also](https://bulbapedia.bulbagarden.net/wiki/Nature)
pub fn nature(&self) -> &Arc<dyn Nature> {
&self.data.nature
}
pub fn nature(&self) -> &Arc<dyn Nature> { &self.data.nature }
/// Calculates the flat stats on the Pokemon. This should be called when for example the base
/// stats, level, nature, IV, or EV changes. This has a side effect of recalculating the boosted
@@ -606,7 +536,7 @@ impl Pokemon {
.set(ability_script)
.as_ref()
// Ensure the ability script gets initialized with the parameters for the ability.
.on_initialize(&self.data.library, ability.parameters().to_vec());
.on_initialize(&self.data.library, ability.parameters());
match script_result {
Ok(_) => (),
Err(e) => {
@@ -646,14 +576,10 @@ impl Pokemon {
}
/// Whether or not the Pokemon is useable in a battle.
pub fn is_usable(&self) -> bool {
!self.data.is_caught && !self.data.is_egg && !self.is_fainted()
}
pub fn is_usable(&self) -> bool { !self.data.is_caught && !self.data.is_egg && !self.is_fainted() }
/// Returns whether the Pokemon is fainted.
pub fn is_fainted(&self) -> bool {
self.current_health() == 0
}
pub fn is_fainted(&self) -> bool { self.current_health() == 0 }
/// Sets the current battle the Pokemon is in.
pub fn set_battle_data(&self, battle: WeakBattleReference, battle_side_index: u8) {
@@ -836,9 +762,7 @@ impl Pokemon {
}
/// Removes the current non-volatile status from the Pokemon.
pub fn clear_status(&self) {
self.data.status_script.clear()
}
pub fn clear_status(&self) { self.data.status_script.clear() }
/// Increases the level by a certain amount
pub fn change_level_by(&self, amount: LevelInt) -> Result<()> {
@@ -858,9 +782,7 @@ impl Pokemon {
/// Converts the Pokemon into a serializable form.
#[cfg(feature = "serde")]
pub fn serialize(&self) -> Result<super::serialization::SerializedPokemon> {
self.into()
}
pub fn serialize(&self) -> Result<super::serialization::SerializedPokemon> { self.into() }
/// Deserializes a Pokemon from a serializable form.
#[cfg(feature = "serde")]
@@ -995,23 +917,17 @@ impl Pokemon {
}
/// Gets the inner pointer to the reference counted data.
pub fn as_ptr(&self) -> *const c_void {
Arc::as_ptr(&self.data) as *const c_void
}
pub fn as_ptr(&self) -> *const c_void { Arc::as_ptr(&self.data) as *const c_void }
}
impl PartialEq for Pokemon {
fn eq(&self, other: &Self) -> bool {
Arc::ptr_eq(&self.data, &other.data)
}
fn eq(&self, other: &Self) -> bool { Arc::ptr_eq(&self.data, &other.data) }
}
impl Eq for Pokemon {}
impl PartialEq for WeakPokemonReference {
fn eq(&self, other: &Self) -> bool {
Weak::ptr_eq(&self.data, &other.data)
}
fn eq(&self, other: &Self) -> bool { Weak::ptr_eq(&self.data, &other.data) }
}
impl Eq for WeakPokemonReference {}
@@ -1025,9 +941,7 @@ impl WeakPokemonReference {
}
/// Gets the pointer to the underlying data.
pub(crate) fn as_ptr(&self) -> *const c_void {
self.data.as_ptr() as *const c_void
}
pub(crate) fn as_ptr(&self) -> *const c_void { self.data.as_ptr() as *const c_void }
}
/// The data of the Pokemon related to being in a battle.
@@ -1047,26 +961,16 @@ pub struct PokemonBattleData {
impl PokemonBattleData {
/// The battle data of the Pokemon
pub fn battle(&self) -> Option<Battle> {
self.battle.upgrade()
}
pub fn battle(&self) -> Option<Battle> { self.battle.upgrade() }
/// The index of the side of the Pokemon
pub fn battle_side_index(&self) -> u8 {
self.battle_side_index.load(Ordering::Relaxed)
}
pub fn battle_side_index(&self) -> u8 { self.battle_side_index.load(Ordering::Relaxed) }
/// The index of the slot on the side of the Pokemon.
pub fn index(&self) -> u8 {
self.index.load(Ordering::Relaxed)
}
pub fn index(&self) -> u8 { self.index.load(Ordering::Relaxed) }
/// Whether or not the Pokemon is on the battlefield.
pub fn on_battle_field(&self) -> bool {
self.on_battle_field.load(Ordering::Relaxed)
}
pub fn on_battle_field(&self) -> bool { self.on_battle_field.load(Ordering::Relaxed) }
/// A list of opponents the Pokemon has seen this battle.
pub fn seen_opponents(&self) -> &RwLock<Vec<WeakPokemonReference>> {
&self.seen_opponents
}
pub fn seen_opponents(&self) -> &RwLock<Vec<WeakPokemonReference>> { &self.seen_opponents }
}
impl ScriptSource for Pokemon {
@@ -1083,9 +987,7 @@ impl ScriptSource for Pokemon {
Ok(c)
}
fn get_script_source_data(&self) -> &RwLock<ScriptSourceData> {
&self.data.script_source_data
}
fn get_script_source_data(&self) -> &RwLock<ScriptSourceData> { &self.data.script_source_data }
fn get_own_scripts(&self, scripts: &mut Vec<ScriptWrapper>) {
scripts.push((&self.data.held_item_trigger_script).into());
@@ -1109,9 +1011,7 @@ impl ScriptSource for Pokemon {
}
impl VolatileScriptsOwner for Pokemon {
fn volatile_scripts(&self) -> &Arc<ScriptSet> {
&self.data.volatile
}
fn volatile_scripts(&self) -> &Arc<ScriptSet> { &self.data.volatile }
fn load_volatile_script(&self, key: &StringKey) -> Result<Option<Arc<dyn Script>>> {
self.data.library.load_script(self.into(), ScriptCategory::Pokemon, key)