Remove lifetime mess, replace a lot of code with Arc instead of borrows.
Some checks failed
continuous-integration/drone/push Build is failing
Some checks failed
continuous-integration/drone/push Build is failing
This cleans up the codebase massively, and allows me to maintain some semblance of sanity.
This commit is contained in:
@@ -25,7 +25,7 @@ pub struct DynamicLibrary {
|
||||
damage_calculator: Box<dyn DamageLibrary>,
|
||||
/// The Misc Library holds minor functions that do not fall in any of the other libraries and
|
||||
/// calculators.
|
||||
misc_library: Box<dyn MiscLibrary<'static>>,
|
||||
misc_library: Box<dyn MiscLibrary>,
|
||||
|
||||
/// The script resolver deals with how to resolve the scripts from specific unique key combinations.
|
||||
script_resolver: Box<dyn ScriptResolver>,
|
||||
@@ -40,7 +40,7 @@ impl DynamicLibrary {
|
||||
static_data: StaticData,
|
||||
stat_calculator: Box<dyn BattleStatCalculator>,
|
||||
damage_calculator: Box<dyn DamageLibrary>,
|
||||
misc_library: Box<dyn MiscLibrary<'static>>,
|
||||
misc_library: Box<dyn MiscLibrary>,
|
||||
script_resolver: Box<dyn ScriptResolver>,
|
||||
) -> Self {
|
||||
Self {
|
||||
@@ -67,7 +67,7 @@ impl DynamicLibrary {
|
||||
}
|
||||
/// The Misc Library holds minor functions that do not fall in any of the other libraries and
|
||||
/// calculators.
|
||||
pub fn misc_library(&self) -> &dyn MiscLibrary<'static> {
|
||||
pub fn misc_library(&self) -> &dyn MiscLibrary {
|
||||
self.misc_library.deref()
|
||||
}
|
||||
|
||||
@@ -84,7 +84,7 @@ impl DynamicLibrary {
|
||||
/// Loads an item script with the given unique key. If no script can be created with this
|
||||
/// combinations, returns None. Note that ItemScripts are immutable, as their script should be
|
||||
/// shared between all different usages.
|
||||
pub fn load_item_script(&self, _key: &Item) -> PkmnResult<Option<Arc<dyn ItemScript>>> {
|
||||
pub fn load_item_script(&self, _key: &Arc<Item>) -> PkmnResult<Option<Arc<dyn ItemScript>>> {
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,35 +10,26 @@ use crate::static_data::{MoveCategory, MoveData, MoveTarget, SecondaryEffect};
|
||||
use crate::StringKey;
|
||||
|
||||
/// The misc library holds several misc functions required for the battle to run.
|
||||
pub trait MiscLibrary<'library>: Debug {
|
||||
pub trait MiscLibrary: Debug {
|
||||
/// Returns whether or not a Pokemon is allowed to flee or switch out.
|
||||
fn can_flee(&self, choice: &TurnChoice) -> bool;
|
||||
/// Returns the move we need to use if we can't use another move. Typically Struggle.
|
||||
fn replacement_move<'func>(
|
||||
&'func self,
|
||||
user: &Arc<Pokemon<'func, 'library>>,
|
||||
target_side: u8,
|
||||
target_index: u8,
|
||||
) -> TurnChoice<'func, 'library>;
|
||||
fn replacement_move<'func>(&'func self, user: &Arc<Pokemon>, target_side: u8, target_index: u8) -> TurnChoice;
|
||||
// TODO: can evolve from level up?
|
||||
// TODO: get time
|
||||
}
|
||||
|
||||
/// A gen 7 implementation for the MiscLibrary.
|
||||
#[derive(Debug)]
|
||||
pub struct Gen7MiscLibrary<'library> {
|
||||
/// The move data for struggle. This is a pointer due to lifetime issues; we know that the
|
||||
/// learned move based on this has the same lifetime as the move data, but the compiler does not.
|
||||
/// If possible in a sane manner, we should get rid of this pointer.
|
||||
struggle_data: *const MoveData,
|
||||
pub struct Gen7MiscLibrary {
|
||||
/// The learned move data for struggle.
|
||||
struggle_learned_move: Arc<LearnedMove<'library>>,
|
||||
struggle_learned_move: Arc<LearnedMove>,
|
||||
}
|
||||
|
||||
impl<'library> Gen7MiscLibrary<'library> {
|
||||
impl Gen7MiscLibrary {
|
||||
/// Instantiates a new MiscLibrary.
|
||||
pub fn new() -> Self {
|
||||
let struggle_data = Box::new(MoveData::new(
|
||||
let struggle_data = Arc::new(MoveData::new(
|
||||
&StringKey::new("struggle"),
|
||||
0.into(),
|
||||
MoveCategory::Physical,
|
||||
@@ -50,40 +41,23 @@ impl<'library> Gen7MiscLibrary<'library> {
|
||||
Some(SecondaryEffect::new(-1.0, StringKey::new("struggle"), vec![])),
|
||||
HashSet::new(),
|
||||
));
|
||||
let struggle_ptr = Box::into_raw(struggle_data);
|
||||
let struggle_learned_move = Arc::new(LearnedMove::new(unsafe { &*struggle_ptr }, MoveLearnMethod::Unknown));
|
||||
Self {
|
||||
struggle_data: struggle_ptr,
|
||||
struggle_learned_move,
|
||||
}
|
||||
let struggle_learned_move = Arc::new(LearnedMove::new(&struggle_data.clone(), MoveLearnMethod::Unknown));
|
||||
Self { struggle_learned_move }
|
||||
}
|
||||
}
|
||||
|
||||
impl<'library> Default for Gen7MiscLibrary<'library> {
|
||||
impl Default for Gen7MiscLibrary {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
impl<'library> Drop for Gen7MiscLibrary<'library> {
|
||||
fn drop(&mut self) {
|
||||
unsafe {
|
||||
let _ = Box::from_raw(self.struggle_data as *mut MoveData);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'library> MiscLibrary<'library> for Gen7MiscLibrary<'library> {
|
||||
impl MiscLibrary for Gen7MiscLibrary {
|
||||
fn can_flee(&self, _choice: &TurnChoice) -> bool {
|
||||
todo!()
|
||||
}
|
||||
|
||||
fn replacement_move<'func>(
|
||||
&'func self,
|
||||
user: &Arc<Pokemon<'func, 'library>>,
|
||||
target_side: u8,
|
||||
target_index: u8,
|
||||
) -> TurnChoice<'func, 'library> {
|
||||
fn replacement_move<'func>(&'func self, user: &Arc<Pokemon>, target_side: u8, target_index: u8) -> TurnChoice {
|
||||
self.struggle_learned_move.restore_all_uses();
|
||||
TurnChoice::Move(MoveChoice::new(
|
||||
user.clone(),
|
||||
|
||||
Reference in New Issue
Block a user