Move Form and Species to traits, implement a bunch of mocks
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2022-11-28 21:34:28 +01:00
parent c1e09c654b
commit bce636b97e
25 changed files with 447 additions and 155 deletions

View File

@@ -12,8 +12,8 @@ use std::sync::Arc;
#[no_mangle]
extern "C" fn pokemon_new(
library: ExternPointer<Arc<DynamicLibrary>>,
species: ExternPointer<Arc<Species>>,
form: ExternPointer<Arc<Form>>,
species: ExternPointer<Arc<dyn Species>>,
form: ExternPointer<Arc<dyn Form>>,
hidden_ability: bool,
ability_index: u8,
level: LevelInt,
@@ -54,25 +54,25 @@ extern "C" fn pokemon_library(ptr: ExternPointer<Arc<Pokemon>>) -> IdentifiableP
/// The species of the Pokemon.
#[no_mangle]
extern "C" fn pokemon_species(ptr: ExternPointer<Arc<Pokemon>>) -> IdentifiablePointer<Arc<Species>> {
extern "C" fn pokemon_species(ptr: ExternPointer<Arc<Pokemon>>) -> IdentifiablePointer<Arc<dyn Species>> {
ptr.as_ref().species().into()
}
/// The form of the Pokemon.
#[no_mangle]
extern "C" fn pokemon_form(ptr: ExternPointer<Arc<Pokemon>>) -> IdentifiablePointer<Arc<Form>> {
extern "C" fn pokemon_form(ptr: ExternPointer<Arc<Pokemon>>) -> IdentifiablePointer<Arc<dyn Form>> {
ptr.as_ref().form().into()
}
/// The species that should be displayed to the user. This handles stuff like the Illusion ability.
#[no_mangle]
extern "C" fn pokemon_display_species(ptr: ExternPointer<Arc<Pokemon>>) -> IdentifiablePointer<Arc<Species>> {
extern "C" fn pokemon_display_species(ptr: ExternPointer<Arc<Pokemon>>) -> IdentifiablePointer<Arc<dyn Species>> {
ptr.as_ref().display_species().into()
}
/// The form that should be displayed to the user. This handles stuff like the Illusion ability.
#[no_mangle]
extern "C" fn pokemon_display_form(ptr: ExternPointer<Arc<Pokemon>>) -> IdentifiablePointer<Arc<Form>> {
extern "C" fn pokemon_display_form(ptr: ExternPointer<Arc<Pokemon>>) -> IdentifiablePointer<Arc<dyn Form>> {
ptr.as_ref().display_form().into()
}
@@ -294,8 +294,8 @@ extern "C" fn pokemon_recalculate_boosted_stats(ptr: ExternPointer<Arc<Pokemon>>
#[no_mangle]
extern "C" fn pokemon_change_species(
ptr: ExternPointer<Arc<Pokemon>>,
species: ExternPointer<Arc<Species>>,
form: ExternPointer<Arc<Form>>,
species: ExternPointer<Arc<dyn Species>>,
form: ExternPointer<Arc<dyn Form>>,
) {
ptr.as_ref()
.change_species(species.as_ref().clone(), form.as_ref().clone())
@@ -303,7 +303,7 @@ extern "C" fn pokemon_change_species(
/// Change the form of the Pokemon.
#[no_mangle]
extern "C" fn pokemon_change_form(ptr: ExternPointer<Arc<Pokemon>>, form: ExternPointer<Arc<Form>>) {
extern "C" fn pokemon_change_form(ptr: ExternPointer<Arc<Pokemon>>, form: ExternPointer<Arc<dyn Form>>) {
ptr.as_ref().change_form(form.as_ref())
}

View File

@@ -58,7 +58,7 @@ macro_rules! ffi_vec_value_getters {
/// length function, and a getter.
macro_rules! ffi_vec_stringkey_getters {
(
$type:ident, $func:ident
$type:ty, $func:ident
) => {
paste::paste! {
#[no_mangle]

View File

@@ -2,7 +2,7 @@ use crate::ffi::{
ffi_arc_getter, ffi_vec_stringkey_getters, ffi_vec_value_getters, BorrowedPtr, ExternPointer, IdentifiablePointer,
OwnedPtr,
};
use crate::static_data::{Form, LearnableMoves, StaticStatisticSet, TypeIdentifier};
use crate::static_data::{Form, FormImpl, LearnableMoves, StaticStatisticSet, TypeIdentifier};
use crate::StringKey;
use hashbrown::HashSet;
use std::ffi::{c_char, CStr, CString};
@@ -26,7 +26,7 @@ unsafe extern "C" fn form_new(
moves: OwnedPtr<Box<dyn LearnableMoves>>,
flags: *const *const c_char,
flags_length: usize,
) -> IdentifiablePointer<Arc<Form>> {
) -> IdentifiablePointer<Arc<dyn Form>> {
let name: StringKey = CStr::from_ptr(name).to_str().unwrap().into();
let abilities = std::slice::from_raw_parts(abilities, abilities_length);
@@ -46,7 +46,7 @@ unsafe extern "C" fn form_new(
flags_set.insert(CStr::from_ptr(*flag).to_str().unwrap().into());
}
Arc::new(Form::new(
let a: Arc<dyn Form> = Arc::new(FormImpl::new(
&name,
height,
weight,
@@ -57,47 +57,49 @@ unsafe extern "C" fn form_new(
hidden_abilities_vec,
*Box::from_raw(moves),
flags_set,
))
.into()
));
a.into()
}
/// Drops a reference count for a form.
#[no_mangle]
unsafe extern "C" fn form_drop(ptr: OwnedPtr<Arc<Form>>) {
unsafe extern "C" fn form_drop(ptr: OwnedPtr<Arc<dyn Form>>) {
drop_in_place(ptr)
}
/// The name of the form.
#[no_mangle]
unsafe extern "C" fn form_name(ptr: ExternPointer<Arc<Form>>) -> OwnedPtr<c_char> {
unsafe extern "C" fn form_name(ptr: ExternPointer<Arc<dyn Form>>) -> OwnedPtr<c_char> {
let name = ptr.as_ref().name();
CString::new(name.str()).unwrap().into_raw()
}
ffi_arc_getter!(Form, height, f32);
ffi_arc_getter!(Form, weight, f32);
ffi_arc_getter!(Form, base_experience, u32);
ffi_arc_getter!(dyn Form, height, f32);
ffi_arc_getter!(dyn Form, weight, f32);
ffi_arc_getter!(dyn Form, base_experience, u32);
ffi_vec_value_getters!(Form, types, TypeIdentifier);
ffi_vec_value_getters!(dyn Form, types, TypeIdentifier);
/// The inherent values of a form of species that are used for the stats of a Pokemon.
#[no_mangle]
unsafe extern "C" fn form_base_stats(ptr: ExternPointer<Arc<Form>>) -> IdentifiablePointer<StaticStatisticSet<u16>> {
unsafe extern "C" fn form_base_stats(
ptr: ExternPointer<Arc<dyn Form>>,
) -> IdentifiablePointer<StaticStatisticSet<u16>> {
(ptr.as_ref().base_stats() as *const StaticStatisticSet<u16>).into()
}
ffi_vec_stringkey_getters!(Form, abilities);
ffi_vec_stringkey_getters!(Form, hidden_abilities);
ffi_vec_stringkey_getters!(dyn Form, abilities);
ffi_vec_stringkey_getters!(dyn Form, hidden_abilities);
/// The moves a Pokemon with this form can learn.
#[no_mangle]
extern "C" fn form_moves(ptr: ExternPointer<Arc<Form>>) -> BorrowedPtr<Box<dyn LearnableMoves>> {
extern "C" fn form_moves(ptr: ExternPointer<Arc<dyn Form>>) -> BorrowedPtr<Box<dyn LearnableMoves>> {
ptr.as_ref().moves() as *const Box<dyn LearnableMoves>
}
/// Check if the form has a specific flag set.
#[no_mangle]
unsafe extern "C" fn form_has_flag(ptr: ExternPointer<Arc<Form>>, flag: *const c_char) -> u8 {
unsafe extern "C" fn form_has_flag(ptr: ExternPointer<Arc<dyn Form>>, flag: *const c_char) -> u8 {
let flag = CStr::from_ptr(flag).into();
u8::from(ptr.as_ref().has_flag(&flag))
}

View File

@@ -72,7 +72,7 @@ macro_rules! library_interface {
};
}
library_interface!(SpeciesLibrary, Species);
library_interface!(SpeciesLibrary, dyn Species);
library_interface!(MoveLibrary, dyn MoveData);
library_interface!(AbilityLibrary, dyn Ability);
library_interface!(ItemLibrary, dyn Item);

View File

@@ -1,5 +1,5 @@
use crate::ffi::{ffi_arc_getter, ffi_arc_stringkey_getter, BorrowedPtr, ExternPointer, IdentifiablePointer, OwnedPtr};
use crate::static_data::{Form, Species};
use crate::static_data::{Form, Species, SpeciesImpl};
use crate::StringKey;
use hashbrown::HashSet;
use std::ffi::{c_char, CStr};
@@ -14,10 +14,10 @@ unsafe extern "C" fn species_new(
gender_rate: f32,
growth_rate: BorrowedPtr<c_char>,
capture_rate: u8,
default_form: OwnedPtr<Arc<Form>>,
default_form: OwnedPtr<Arc<dyn Form>>,
flags: *const *const c_char,
flags_length: usize,
) -> IdentifiablePointer<Arc<Species>> {
) -> IdentifiablePointer<Arc<dyn Species>> {
let name: StringKey = CStr::from_ptr(name).to_str().unwrap().into();
let growth_rate: StringKey = CStr::from_ptr(growth_rate).to_str().unwrap().into();
@@ -26,7 +26,7 @@ unsafe extern "C" fn species_new(
for flag in flags {
flags_set.insert(CStr::from_ptr(*flag).to_str().unwrap().into());
}
Arc::new(Species::new(
let a: Arc<dyn Species> = Arc::new(SpeciesImpl::new(
id,
&name,
gender_rate,
@@ -34,28 +34,28 @@ unsafe extern "C" fn species_new(
capture_rate,
default_form.as_ref().unwrap().clone(),
flags_set,
))
.into()
));
a.into()
}
/// Drop a reference to the species.
#[no_mangle]
unsafe extern "C" fn species_drop(ptr: OwnedPtr<Arc<Species>>) {
unsafe extern "C" fn species_drop(ptr: OwnedPtr<Arc<dyn Species>>) {
drop_in_place(ptr);
}
ffi_arc_getter!(Species, id, u16);
ffi_arc_stringkey_getter!(Species, name);
ffi_arc_getter!(Species, gender_rate, f32);
ffi_arc_stringkey_getter!(Species, growth_rate);
ffi_arc_getter!(Species, capture_rate, u8);
ffi_arc_getter!(dyn Species, id, u16);
ffi_arc_stringkey_getter!(dyn Species, name);
ffi_arc_getter!(dyn Species, gender_rate, f32);
ffi_arc_stringkey_getter!(dyn Species, growth_rate);
ffi_arc_getter!(dyn Species, capture_rate, u8);
/// Adds a new form to the species.
#[no_mangle]
unsafe extern "C" fn species_add_form(
mut species: ExternPointer<Arc<Species>>,
mut species: ExternPointer<Arc<dyn Species>>,
name: BorrowedPtr<c_char>,
form: OwnedPtr<Arc<Form>>,
form: OwnedPtr<Arc<dyn Form>>,
) {
let form = form.as_ref().unwrap().clone();
species.as_mut().add_form(CStr::from_ptr(name).into(), form)
@@ -64,9 +64,9 @@ unsafe extern "C" fn species_add_form(
/// Gets a form by name.
#[no_mangle]
unsafe extern "C" fn species_get_form(
species: ExternPointer<Arc<Species>>,
species: ExternPointer<Arc<dyn Species>>,
name: BorrowedPtr<c_char>,
) -> IdentifiablePointer<Arc<Form>> {
) -> IdentifiablePointer<Arc<dyn Form>> {
let form = species.as_ref().get_form(&CStr::from_ptr(name).into());
if let Some(form) = form {
form.into()