2022-12-25 10:26:14 +00:00
|
|
|
use chrono::Duration;
|
2022-06-19 19:34:08 +00:00
|
|
|
use std::convert::TryFrom;
|
|
|
|
use std::fmt::Debug;
|
|
|
|
use std::fs::File;
|
2022-07-18 08:16:47 +00:00
|
|
|
use std::io::{BufReader, Read};
|
2022-10-08 11:15:04 +00:00
|
|
|
use std::sync::Arc;
|
2022-06-19 19:34:08 +00:00
|
|
|
|
2022-06-17 17:53:33 +00:00
|
|
|
use hashbrown::HashSet;
|
|
|
|
use num_traits::PrimInt;
|
2022-06-19 19:34:08 +00:00
|
|
|
use project_root::get_project_root;
|
|
|
|
use serde_json::Value;
|
|
|
|
|
2022-06-17 17:53:33 +00:00
|
|
|
use pkmn_lib::defines::LevelInt;
|
2022-06-19 19:34:08 +00:00
|
|
|
use pkmn_lib::dynamic_data::Gen7DamageLibrary;
|
|
|
|
use pkmn_lib::dynamic_data::Gen7MiscLibrary;
|
2022-12-24 11:00:50 +00:00
|
|
|
use pkmn_lib::dynamic_data::{DynamicLibrary, DynamicLibraryImpl};
|
2023-01-03 12:56:20 +00:00
|
|
|
use pkmn_lib::dynamic_data::{Gen7BattleStatCalculator, ScriptResolver};
|
2022-06-17 17:53:33 +00:00
|
|
|
use pkmn_lib::static_data::{
|
2023-07-29 10:57:52 +00:00
|
|
|
AbilityImpl, AbilityLibrary, AbilityLibraryImpl, BattleItemCategory, DataLibrary, EvolutionData, EvolutionMethod,
|
|
|
|
Form, FormImpl, Gender, GrowthRateLibrary, GrowthRateLibraryImpl, ItemImpl, ItemLibrary, ItemLibraryImpl,
|
|
|
|
LearnableMoves, LearnableMovesImpl, LibrarySettingsImpl, LookupGrowthRate, MoveDataImpl, MoveLibrary,
|
|
|
|
MoveLibraryImpl, NatureImpl, NatureLibrary, NatureLibraryImpl, Parameter, SecondaryEffect, SecondaryEffectImpl,
|
|
|
|
SpeciesImpl, SpeciesLibrary, SpeciesLibraryImpl, StaticDataImpl, StaticStatisticSet, Statistic, TimeOfDay,
|
|
|
|
TypeLibrary, TypeLibraryImpl,
|
2022-06-17 17:53:33 +00:00
|
|
|
};
|
|
|
|
use pkmn_lib::StringKey;
|
|
|
|
|
2022-12-25 10:26:14 +00:00
|
|
|
pub struct LoadResult {
|
|
|
|
pub library: Arc<dyn DynamicLibrary>,
|
|
|
|
pub types_load_time: Duration,
|
|
|
|
pub natures_load_time: Duration,
|
|
|
|
pub items_load_time: Duration,
|
|
|
|
pub growth_rate_load_time: Duration,
|
|
|
|
pub abilities_load_time: Duration,
|
|
|
|
pub moves_load_time: Duration,
|
|
|
|
pub species_load_time: Duration,
|
|
|
|
pub wasm_load_time: Duration,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn load_library() -> LoadResult {
|
2022-06-17 17:53:33 +00:00
|
|
|
let mut path = get_project_root().unwrap();
|
|
|
|
path.push("tests/data/");
|
|
|
|
let path = path.to_str().unwrap().to_string();
|
2022-12-24 11:48:00 +00:00
|
|
|
|
2022-12-25 10:26:14 +00:00
|
|
|
let t1 = chrono::Utc::now();
|
2022-12-24 11:48:00 +00:00
|
|
|
let types = load_types(&path);
|
2022-12-25 10:26:14 +00:00
|
|
|
let t2 = chrono::Utc::now();
|
|
|
|
let types_load_time = t2 - t1;
|
2022-12-24 11:48:00 +00:00
|
|
|
let natures = load_natures(&path);
|
2022-12-25 10:26:14 +00:00
|
|
|
let t1 = chrono::Utc::now();
|
|
|
|
let natures_load_time = t1 - t2;
|
2022-12-24 11:48:00 +00:00
|
|
|
let items = load_items(&path);
|
2022-12-25 10:26:14 +00:00
|
|
|
let t2 = chrono::Utc::now();
|
|
|
|
let items_load_time = t2 - t1;
|
2022-12-24 11:48:00 +00:00
|
|
|
let growth_rates = load_growth_rates(&path);
|
2022-12-25 10:26:14 +00:00
|
|
|
let t1 = chrono::Utc::now();
|
|
|
|
let growth_rate_load_time = t1 - t2;
|
2022-12-24 11:48:00 +00:00
|
|
|
let abilities = load_abilities(&path);
|
2022-12-25 10:26:14 +00:00
|
|
|
let t2 = chrono::Utc::now();
|
|
|
|
let abilities_load_time = t2 - t1;
|
2022-12-24 11:48:00 +00:00
|
|
|
let moves = load_moves(&path, &types);
|
2022-12-25 10:26:14 +00:00
|
|
|
let t1 = chrono::Utc::now();
|
|
|
|
let moves_load_time = t1 - t2;
|
2022-12-24 11:48:00 +00:00
|
|
|
let species = load_species(&path, &types, &moves);
|
2022-12-25 10:26:14 +00:00
|
|
|
let t2 = chrono::Utc::now();
|
|
|
|
let species_load_time = t2 - t1;
|
2022-12-24 11:48:00 +00:00
|
|
|
|
2022-12-24 13:03:47 +00:00
|
|
|
let data = StaticDataImpl::new(
|
2023-06-22 13:43:41 +00:00
|
|
|
Arc::new(LibrarySettingsImpl::new(100, 100).unwrap()),
|
2022-12-24 11:48:00 +00:00
|
|
|
species,
|
|
|
|
moves,
|
|
|
|
items,
|
|
|
|
growth_rates,
|
|
|
|
types,
|
|
|
|
natures,
|
|
|
|
abilities,
|
|
|
|
);
|
2022-12-25 10:26:14 +00:00
|
|
|
|
|
|
|
let t1 = chrono::Utc::now();
|
2023-01-03 11:58:23 +00:00
|
|
|
let script_resolver = load_script_resolver(&path);
|
2022-12-25 10:26:14 +00:00
|
|
|
let t2 = chrono::Utc::now();
|
|
|
|
let wasm_load_time = t2 - t1;
|
2022-07-18 08:16:47 +00:00
|
|
|
|
2022-12-25 10:26:14 +00:00
|
|
|
let library = Arc::new(DynamicLibraryImpl::new(
|
2023-06-22 13:43:41 +00:00
|
|
|
Arc::new(data),
|
|
|
|
Arc::new(Gen7BattleStatCalculator::new()),
|
|
|
|
Arc::new(Gen7DamageLibrary::new(false)),
|
2023-07-22 11:22:47 +00:00
|
|
|
Arc::new(Gen7MiscLibrary::new(Box::new(|| TimeOfDay::Day))),
|
2023-01-03 11:58:23 +00:00
|
|
|
script_resolver,
|
2022-12-25 10:26:14 +00:00
|
|
|
));
|
|
|
|
|
|
|
|
LoadResult {
|
|
|
|
library,
|
|
|
|
types_load_time,
|
|
|
|
natures_load_time,
|
|
|
|
items_load_time,
|
|
|
|
growth_rate_load_time,
|
|
|
|
abilities_load_time,
|
|
|
|
moves_load_time,
|
|
|
|
species_load_time,
|
|
|
|
wasm_load_time,
|
|
|
|
}
|
2022-06-17 17:53:33 +00:00
|
|
|
}
|
|
|
|
|
2023-06-22 13:43:41 +00:00
|
|
|
pub fn load_types(path: &String) -> Arc<dyn TypeLibrary> {
|
2022-06-17 17:53:33 +00:00
|
|
|
let mut reader = csv::ReaderBuilder::new()
|
|
|
|
.delimiter(b'|')
|
|
|
|
.from_path(path.to_string() + "Types.csv")
|
|
|
|
.unwrap();
|
2022-12-24 11:48:00 +00:00
|
|
|
|
2023-06-24 12:44:23 +00:00
|
|
|
let type_library = TypeLibraryImpl::new(20);
|
2022-12-24 11:48:00 +00:00
|
|
|
|
2022-06-17 17:53:33 +00:00
|
|
|
let headers = reader.headers().unwrap();
|
|
|
|
for header in headers.iter().skip(1) {
|
2022-10-14 14:53:30 +00:00
|
|
|
type_library.register_type(&header.into());
|
2022-06-17 17:53:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for record in reader.records() {
|
|
|
|
let record = record.unwrap();
|
|
|
|
let offensive_type = record.get(0).unwrap();
|
2022-10-14 14:53:30 +00:00
|
|
|
let offensive_type_id = type_library.get_type_id(&offensive_type.into()).unwrap();
|
2022-06-17 17:53:33 +00:00
|
|
|
|
|
|
|
for (i, v) in record.iter().skip(1).enumerate() {
|
|
|
|
let effectiveness = v.parse::<f32>().unwrap();
|
2023-04-19 16:44:11 +00:00
|
|
|
type_library
|
|
|
|
.set_effectiveness(offensive_type_id, ((i + 1) as u8).into(), effectiveness)
|
|
|
|
.unwrap();
|
2022-06-17 17:53:33 +00:00
|
|
|
}
|
|
|
|
}
|
2023-06-22 13:43:41 +00:00
|
|
|
Arc::new(type_library)
|
2022-06-17 17:53:33 +00:00
|
|
|
}
|
|
|
|
|
2023-06-22 13:43:41 +00:00
|
|
|
pub fn load_natures(path: &String) -> Arc<dyn NatureLibrary> {
|
2022-06-17 17:53:33 +00:00
|
|
|
let mut reader = csv::ReaderBuilder::new()
|
|
|
|
.delimiter(b'|')
|
|
|
|
.from_path(path.to_string() + "Natures.csv")
|
|
|
|
.unwrap();
|
|
|
|
|
2023-06-24 12:44:23 +00:00
|
|
|
let nature_library = NatureLibraryImpl::new(24);
|
2022-06-17 17:53:33 +00:00
|
|
|
for record in reader.records() {
|
|
|
|
let record = record.unwrap();
|
2022-10-14 14:53:30 +00:00
|
|
|
let nature_name = record.get(0).unwrap().into();
|
2022-06-17 17:53:33 +00:00
|
|
|
let increased_statistic_str = record.get(1).unwrap();
|
|
|
|
let decreased_statistic_str = record.get(2).unwrap();
|
|
|
|
if increased_statistic_str.is_empty() || decreased_statistic_str.is_empty() {
|
2022-11-27 16:36:42 +00:00
|
|
|
nature_library.load_nature(nature_name, NatureImpl::new(Statistic::HP, Statistic::HP, 1.0, 1.0));
|
2022-06-17 17:53:33 +00:00
|
|
|
} else {
|
|
|
|
let increased_statistic = serde_plain::from_str(increased_statistic_str).unwrap();
|
|
|
|
let decreased_statistic = serde_plain::from_str(decreased_statistic_str).unwrap();
|
|
|
|
nature_library.load_nature(
|
|
|
|
nature_name,
|
2022-11-27 16:36:42 +00:00
|
|
|
NatureImpl::new(increased_statistic, decreased_statistic, 1.1, 0.9),
|
2022-06-17 17:53:33 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2023-06-22 13:43:41 +00:00
|
|
|
Arc::new(nature_library)
|
2022-06-17 17:53:33 +00:00
|
|
|
}
|
|
|
|
|
2023-06-22 13:43:41 +00:00
|
|
|
pub fn load_items(path: &String) -> Arc<dyn ItemLibrary> {
|
2022-06-17 17:53:33 +00:00
|
|
|
let mut file = File::open(path.to_string() + "Items.json").unwrap();
|
|
|
|
let mut data = String::new();
|
|
|
|
file.read_to_string(&mut data).unwrap();
|
|
|
|
let json: Value = serde_json::from_str(&data).unwrap();
|
|
|
|
let json_array = json.as_array().unwrap();
|
|
|
|
|
2023-06-24 12:44:23 +00:00
|
|
|
let item_library = ItemLibraryImpl::new(400);
|
2022-06-17 17:53:33 +00:00
|
|
|
for v in json_array {
|
2022-10-14 14:53:30 +00:00
|
|
|
let name = v.get("name").unwrap().as_str().unwrap().into();
|
2022-09-17 07:38:02 +00:00
|
|
|
let category = serde_json::from_value(v.get("itemType").unwrap().clone()).unwrap();
|
2022-06-17 17:53:33 +00:00
|
|
|
let mut battle_category = BattleItemCategory::None;
|
|
|
|
if let Some(c) = v.get("battleType") {
|
|
|
|
battle_category = serde_json::from_value(c.clone()).unwrap();
|
|
|
|
}
|
2022-09-17 07:38:02 +00:00
|
|
|
let price = v.get("price").unwrap().as_i64().unwrap();
|
2022-06-17 17:53:33 +00:00
|
|
|
let mut flags = HashSet::new();
|
|
|
|
if let Some(f) = v.get("flags") {
|
|
|
|
let a = f.as_array().unwrap();
|
|
|
|
for flag in a {
|
2022-10-14 14:53:30 +00:00
|
|
|
flags.insert(flag.as_str().unwrap().into());
|
2022-06-17 17:53:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-24 11:48:00 +00:00
|
|
|
item_library.add(
|
2022-10-08 11:15:04 +00:00
|
|
|
&name,
|
2022-11-27 16:29:29 +00:00
|
|
|
Arc::new(ItemImpl::new(&name, category, battle_category, price as i32, flags)),
|
2022-10-08 11:15:04 +00:00
|
|
|
);
|
2022-06-17 17:53:33 +00:00
|
|
|
}
|
2023-06-22 13:43:41 +00:00
|
|
|
Arc::new(item_library)
|
2022-06-17 17:53:33 +00:00
|
|
|
}
|
|
|
|
|
2023-06-22 13:43:41 +00:00
|
|
|
pub fn load_growth_rates(path: &String) -> Arc<dyn GrowthRateLibrary> {
|
2022-06-17 17:53:33 +00:00
|
|
|
let mut file = File::open(path.to_string() + "GrowthRates.json").unwrap();
|
|
|
|
let mut data = String::new();
|
|
|
|
file.read_to_string(&mut data).unwrap();
|
|
|
|
let json: Value = serde_json::from_str(&data).unwrap();
|
|
|
|
let o = json.as_object().unwrap();
|
|
|
|
|
2023-06-24 12:44:23 +00:00
|
|
|
let growth_rate_library = GrowthRateLibraryImpl::new(10);
|
2022-06-17 17:53:33 +00:00
|
|
|
for (key, value) in o {
|
|
|
|
let name = StringKey::new(key);
|
|
|
|
let experience_required_json = value.as_array().unwrap();
|
|
|
|
let mut experience_required = Vec::with_capacity(experience_required_json.len());
|
|
|
|
for v in experience_required_json {
|
|
|
|
experience_required.push(v.as_i64().unwrap() as u32);
|
|
|
|
}
|
|
|
|
|
2023-06-24 12:44:23 +00:00
|
|
|
growth_rate_library.add_growth_rate(&name, Arc::new(LookupGrowthRate::new(experience_required)));
|
2022-06-17 17:53:33 +00:00
|
|
|
}
|
2023-06-22 13:43:41 +00:00
|
|
|
Arc::new(growth_rate_library)
|
2022-06-17 17:53:33 +00:00
|
|
|
}
|
|
|
|
|
2023-06-22 13:43:41 +00:00
|
|
|
pub fn load_abilities(path: &String) -> Arc<dyn AbilityLibrary> {
|
2022-06-17 17:53:33 +00:00
|
|
|
let mut file = File::open(path.to_string() + "Abilities.json").unwrap();
|
|
|
|
let mut data = String::new();
|
|
|
|
file.read_to_string(&mut data).unwrap();
|
|
|
|
let json: Value = serde_json::from_str(&data).unwrap();
|
|
|
|
let o = json.as_object().unwrap();
|
|
|
|
|
2023-06-24 12:44:23 +00:00
|
|
|
let ability_library = AbilityLibraryImpl::new(400);
|
2022-06-17 17:53:33 +00:00
|
|
|
for (key, value) in o {
|
|
|
|
let name = StringKey::new(key);
|
|
|
|
let mut effect = StringKey::empty();
|
|
|
|
if let Some(e) = value.get("effect") {
|
2022-10-14 14:53:30 +00:00
|
|
|
effect = e.as_str().unwrap().into();
|
2022-06-17 17:53:33 +00:00
|
|
|
}
|
|
|
|
let mut parameters = Vec::new();
|
|
|
|
if let Some(p) = value.get("parameters") {
|
|
|
|
for par in p.as_array().unwrap() {
|
2023-07-29 10:57:52 +00:00
|
|
|
parameters.push(parse_parameter(par));
|
2022-06-17 17:53:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-27 16:47:51 +00:00
|
|
|
ability_library.add(&name, Arc::new(AbilityImpl::new(&name, &effect, parameters)));
|
2022-06-17 17:53:33 +00:00
|
|
|
}
|
2023-06-22 13:43:41 +00:00
|
|
|
Arc::new(ability_library)
|
2022-06-17 17:53:33 +00:00
|
|
|
}
|
|
|
|
|
2023-06-22 13:43:41 +00:00
|
|
|
pub fn load_moves(path: &String, types: &Arc<dyn TypeLibrary>) -> Arc<dyn MoveLibrary> {
|
2022-06-17 17:53:33 +00:00
|
|
|
let mut file = File::open(path.to_string() + "Moves.json").unwrap();
|
|
|
|
let mut data = String::new();
|
|
|
|
file.read_to_string(&mut data).unwrap();
|
|
|
|
let json: Value = serde_json::from_str(&data).unwrap();
|
|
|
|
let data = json.as_object().unwrap().get("data").unwrap().as_array().unwrap();
|
2023-06-24 12:44:23 +00:00
|
|
|
let move_library = MoveLibraryImpl::new(600);
|
2022-06-17 17:53:33 +00:00
|
|
|
for move_data in data {
|
|
|
|
let move_data = move_data.as_object().unwrap();
|
2022-10-14 14:53:30 +00:00
|
|
|
let move_name = move_data.get("name").unwrap().as_str().unwrap().into();
|
|
|
|
let move_type = move_data.get("type").unwrap().as_str().unwrap().into();
|
2022-12-24 11:48:00 +00:00
|
|
|
let move_type_id = types.get_type_id(&move_type).unwrap();
|
2022-09-17 07:38:02 +00:00
|
|
|
let move_category = serde_json::from_value(move_data.get("category").unwrap().clone()).unwrap();
|
|
|
|
let base_power = move_data.get("power").unwrap().as_i64().unwrap() as u8;
|
|
|
|
let accuracy = move_data.get("accuracy").unwrap().as_i64().unwrap() as u8;
|
|
|
|
let pp = move_data.get("pp").unwrap().as_i64().unwrap() as u8;
|
|
|
|
let target = serde_json::from_value(move_data.get("target").unwrap().clone()).unwrap();
|
|
|
|
let priority = move_data.get("priority").unwrap().as_i64().unwrap() as i8;
|
2023-06-24 12:44:23 +00:00
|
|
|
let secondary_effect: Option<Arc<dyn SecondaryEffect>> = if let Some(v) = move_data.get("effect") {
|
2022-06-17 17:53:33 +00:00
|
|
|
let mut chance = -1.0;
|
|
|
|
if let Some(chance_value) = v.get("chance") {
|
|
|
|
chance = chance_value.as_f64().unwrap() as f32;
|
|
|
|
}
|
|
|
|
let mut parameters = Vec::new();
|
|
|
|
if let Some(pars) = v.get("parameters") {
|
|
|
|
let pars = pars.as_array().unwrap();
|
|
|
|
for par in pars {
|
2023-07-29 10:57:52 +00:00
|
|
|
parameters.push(parse_parameter(par));
|
2022-06-17 17:53:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-24 12:44:23 +00:00
|
|
|
Some(Arc::new(SecondaryEffectImpl::new(
|
2022-06-19 10:07:54 +00:00
|
|
|
chance,
|
2022-10-14 14:53:30 +00:00
|
|
|
v.get("name").unwrap().as_str().unwrap().into(),
|
2022-06-19 10:07:54 +00:00
|
|
|
parameters,
|
2022-11-27 17:22:57 +00:00
|
|
|
)))
|
2022-06-17 17:53:33 +00:00
|
|
|
} else {
|
2022-06-19 10:07:54 +00:00
|
|
|
None
|
2022-06-17 17:53:33 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
let mut flags = HashSet::new();
|
|
|
|
if let Some(f) = move_data.get("flags") {
|
|
|
|
let f = f.as_array().unwrap();
|
|
|
|
for flag in f {
|
2022-10-14 14:53:30 +00:00
|
|
|
flags.insert(flag.as_str().unwrap().into());
|
2022-06-17 17:53:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-24 11:48:00 +00:00
|
|
|
move_library.add(
|
2022-06-17 17:53:33 +00:00
|
|
|
&move_name,
|
2022-11-27 21:29:11 +00:00
|
|
|
Arc::new(MoveDataImpl::new(
|
2022-06-17 17:53:33 +00:00
|
|
|
&move_name.clone(),
|
|
|
|
move_type_id,
|
|
|
|
move_category,
|
|
|
|
base_power,
|
|
|
|
accuracy,
|
|
|
|
pp,
|
|
|
|
target,
|
|
|
|
priority,
|
|
|
|
secondary_effect,
|
|
|
|
flags,
|
2022-10-08 11:15:04 +00:00
|
|
|
)),
|
2022-06-17 17:53:33 +00:00
|
|
|
);
|
|
|
|
}
|
2023-06-22 13:43:41 +00:00
|
|
|
Arc::new(move_library)
|
2022-06-17 17:53:33 +00:00
|
|
|
}
|
|
|
|
|
2022-12-24 11:48:00 +00:00
|
|
|
pub fn load_species(
|
|
|
|
path: &String,
|
2023-06-22 13:43:41 +00:00
|
|
|
types: &Arc<dyn TypeLibrary>,
|
|
|
|
moves: &Arc<dyn MoveLibrary>,
|
|
|
|
) -> Arc<dyn SpeciesLibrary> {
|
2022-06-17 17:53:33 +00:00
|
|
|
let mut file = File::open(path.to_string() + "Pokemon.json").unwrap();
|
|
|
|
let mut data = String::new();
|
|
|
|
file.read_to_string(&mut data).unwrap();
|
|
|
|
let json: Value = serde_json::from_str(&data).unwrap();
|
|
|
|
let o = json.as_object().unwrap();
|
|
|
|
|
2023-06-24 12:44:23 +00:00
|
|
|
let species_library = SpeciesLibraryImpl::new(800);
|
2022-09-17 07:38:02 +00:00
|
|
|
for (key, value) in o.iter() {
|
2022-06-18 14:06:54 +00:00
|
|
|
if key.starts_with('$') {
|
2022-06-17 17:53:33 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
let name = StringKey::new(key);
|
2022-09-17 07:38:02 +00:00
|
|
|
let id = value.get("id").unwrap().as_i64().unwrap();
|
|
|
|
let gender_rate = value.get("genderRatio").unwrap().as_f64().unwrap();
|
2022-10-14 14:53:30 +00:00
|
|
|
let growth_rate_name = value.get("growthRate").unwrap().as_str().unwrap().into();
|
2023-07-29 10:57:52 +00:00
|
|
|
let base_happiness = value.get("baseHappiness").unwrap().as_i64().unwrap();
|
2022-09-17 07:38:02 +00:00
|
|
|
let catch_rate = value.get("catchRate").unwrap().as_i64().unwrap();
|
|
|
|
let _color = value.get("color").unwrap().as_str().unwrap();
|
|
|
|
// let egg_groups = value.get("eggGroups").unwrap()
|
2022-06-17 17:53:33 +00:00
|
|
|
// .as_array()
|
|
|
|
// .unwrap()
|
|
|
|
// .iter()
|
|
|
|
// .map(|&a| a.as_str().unwrap())
|
|
|
|
// .collect();
|
2022-09-17 07:38:02 +00:00
|
|
|
let _egg_cycle = value.get("eggCycles").unwrap().as_i64().unwrap();
|
2022-06-17 17:53:33 +00:00
|
|
|
// TODO: tags
|
2023-07-29 10:57:52 +00:00
|
|
|
let evolutions = if let Some(evolutions) = value.get("evolutions") {
|
|
|
|
evolutions.as_array().unwrap().iter().map(parse_evolution).collect()
|
|
|
|
} else {
|
|
|
|
Vec::new()
|
|
|
|
};
|
2022-06-17 17:53:33 +00:00
|
|
|
|
2022-09-17 07:38:02 +00:00
|
|
|
let forms = value.get("formes").unwrap().as_object().unwrap();
|
|
|
|
let default_form_value = forms.get("default").unwrap();
|
2022-12-24 11:48:00 +00:00
|
|
|
let default_form = parse_form("default".into(), default_form_value, types, moves);
|
2022-06-17 17:53:33 +00:00
|
|
|
|
2022-11-28 20:34:28 +00:00
|
|
|
let species = SpeciesImpl::new(
|
2022-06-17 17:53:33 +00:00
|
|
|
id as u16,
|
|
|
|
&name,
|
|
|
|
gender_rate as f32,
|
|
|
|
&growth_rate_name,
|
|
|
|
catch_rate as u8,
|
2023-07-29 10:57:52 +00:00
|
|
|
base_happiness as u8,
|
2022-06-17 17:53:33 +00:00
|
|
|
default_form,
|
|
|
|
Default::default(),
|
2023-07-29 10:57:52 +00:00
|
|
|
evolutions,
|
2022-08-20 11:17:20 +00:00
|
|
|
);
|
2022-12-24 11:48:00 +00:00
|
|
|
species_library.add(&name, Arc::new(species));
|
2022-06-17 17:53:33 +00:00
|
|
|
}
|
2023-06-22 13:43:41 +00:00
|
|
|
Arc::new(species_library)
|
2022-06-17 17:53:33 +00:00
|
|
|
}
|
|
|
|
|
2023-07-29 10:57:52 +00:00
|
|
|
fn parse_evolution(value: &Value) -> EvolutionData {
|
|
|
|
let species = StringKey::new(value.get("species").unwrap().as_str().unwrap());
|
|
|
|
let method = value.get("method").unwrap().as_str().unwrap();
|
|
|
|
let method = match method {
|
|
|
|
"level" => {
|
|
|
|
let level = value.get("data").unwrap().as_i64().unwrap() as LevelInt;
|
|
|
|
EvolutionMethod::Level { level }
|
|
|
|
}
|
|
|
|
"levelfemale" => {
|
|
|
|
let level = value.get("data").unwrap().as_i64().unwrap() as LevelInt;
|
|
|
|
EvolutionMethod::LevelGender {
|
|
|
|
level,
|
|
|
|
gender: Gender::Female,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"levelmale" => {
|
|
|
|
let level = value.get("data").unwrap().as_i64().unwrap() as LevelInt;
|
|
|
|
EvolutionMethod::LevelGender {
|
|
|
|
level,
|
|
|
|
gender: Gender::Male,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"happiness" => {
|
|
|
|
let happiness = value.get("data").unwrap().as_i64().unwrap() as u8;
|
|
|
|
EvolutionMethod::Happiness { happiness }
|
|
|
|
}
|
|
|
|
"happinessday" => {
|
|
|
|
let happiness = value.get("data").unwrap().as_i64().unwrap() as u8;
|
|
|
|
EvolutionMethod::HappinessDay { happiness }
|
|
|
|
}
|
|
|
|
"happinessnight" => {
|
|
|
|
let happiness = value.get("data").unwrap().as_i64().unwrap() as u8;
|
|
|
|
EvolutionMethod::HappinessNight { happiness }
|
|
|
|
}
|
|
|
|
"item" => {
|
|
|
|
let item = StringKey::new(value.get("data").unwrap().as_str().unwrap());
|
|
|
|
EvolutionMethod::Item { item }
|
|
|
|
}
|
|
|
|
"itemmale" => {
|
|
|
|
let item = StringKey::new(value.get("data").unwrap().as_str().unwrap());
|
|
|
|
EvolutionMethod::ItemGender {
|
|
|
|
item,
|
|
|
|
gender: Gender::Male,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"itemfemale" => {
|
|
|
|
let item = StringKey::new(value.get("data").unwrap().as_str().unwrap());
|
|
|
|
EvolutionMethod::ItemGender {
|
|
|
|
item,
|
|
|
|
gender: Gender::Female,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"holditem" => {
|
|
|
|
let item = StringKey::new(value.get("data").unwrap().as_str().unwrap());
|
|
|
|
EvolutionMethod::HoldItem { item }
|
|
|
|
}
|
|
|
|
"dayholditem" => {
|
|
|
|
let item = StringKey::new(value.get("data").unwrap().as_str().unwrap());
|
|
|
|
EvolutionMethod::DayHoldItem { item }
|
|
|
|
}
|
|
|
|
"nightholditem" => {
|
|
|
|
let item = StringKey::new(value.get("data").unwrap().as_str().unwrap());
|
|
|
|
EvolutionMethod::NightHoldItem { item }
|
|
|
|
}
|
|
|
|
"hasmove" => {
|
|
|
|
let move_name = StringKey::new(value.get("data").unwrap().as_str().unwrap());
|
|
|
|
EvolutionMethod::HasMove { move_name }
|
|
|
|
}
|
|
|
|
"trade" => EvolutionMethod::Trade,
|
|
|
|
"tradespecies" => {
|
|
|
|
let species = StringKey::new(value.get("data").unwrap().as_str().unwrap());
|
|
|
|
EvolutionMethod::TradeSpecies { species }
|
|
|
|
}
|
|
|
|
"tradeitem" => {
|
|
|
|
let item = StringKey::new(value.get("data").unwrap().as_str().unwrap());
|
|
|
|
EvolutionMethod::TradeItem { item }
|
|
|
|
}
|
|
|
|
"location" => {
|
|
|
|
let location = StringKey::new(value.get("data").unwrap().as_str().unwrap());
|
|
|
|
EvolutionMethod::Location { location }
|
|
|
|
}
|
|
|
|
"custom" => {
|
|
|
|
let data = value.get("data").unwrap().as_array().unwrap();
|
|
|
|
let name = StringKey::new(data[0].as_str().unwrap());
|
|
|
|
let params = data[1..].iter().map(parse_parameter).collect();
|
|
|
|
EvolutionMethod::Custom { name, params }
|
|
|
|
}
|
|
|
|
_ => panic!("Unknown evolution method: {}", method),
|
|
|
|
};
|
|
|
|
EvolutionData::new(method, species)
|
|
|
|
}
|
|
|
|
|
2023-01-03 11:58:23 +00:00
|
|
|
#[cfg(not(feature = "wasm"))]
|
2023-06-24 12:44:23 +00:00
|
|
|
fn load_script_resolver(path: &String) -> Arc<dyn ScriptResolver> {
|
|
|
|
Arc::new(EmptyScriptResolver::default())
|
2023-01-03 11:58:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(feature = "wasm")]
|
2023-06-24 12:44:23 +00:00
|
|
|
fn load_script_resolver(path: &String) -> Arc<dyn ScriptResolver> {
|
|
|
|
let resolver = pkmn_lib::script_implementations::wasm::script_resolver::WebAssemblyScriptResolver::new();
|
2022-08-20 10:22:12 +00:00
|
|
|
let file = File::open(path.to_string() + "gen7_scripts.wasm").unwrap();
|
2022-07-18 08:16:47 +00:00
|
|
|
let mut reader = BufReader::new(file);
|
|
|
|
let mut buffer = Vec::new();
|
2022-07-18 08:49:58 +00:00
|
|
|
reader.read_to_end(&mut buffer).unwrap();
|
2023-04-23 08:10:06 +00:00
|
|
|
resolver.load_wasm_from_bytes(&buffer).unwrap();
|
|
|
|
resolver.finalize().unwrap();
|
2023-01-03 11:58:23 +00:00
|
|
|
resolver
|
2022-07-18 08:16:47 +00:00
|
|
|
}
|
|
|
|
|
2022-12-24 11:48:00 +00:00
|
|
|
fn parse_form(
|
|
|
|
name: StringKey,
|
|
|
|
value: &Value,
|
2023-06-22 13:43:41 +00:00
|
|
|
types: &Arc<dyn TypeLibrary>,
|
|
|
|
moves: &Arc<dyn MoveLibrary>,
|
2022-12-24 11:48:00 +00:00
|
|
|
) -> Arc<dyn Form> {
|
2022-06-17 17:53:33 +00:00
|
|
|
let mut abilities = Vec::new();
|
2022-09-17 07:38:02 +00:00
|
|
|
for a in value.get("abilities").unwrap().as_array().unwrap() {
|
2022-10-14 14:53:30 +00:00
|
|
|
abilities.push(a.as_str().unwrap().into());
|
2022-06-17 17:53:33 +00:00
|
|
|
}
|
|
|
|
let mut hidden_abilities = Vec::new();
|
2022-09-17 07:38:02 +00:00
|
|
|
for a in value.get("hiddenAbilities").unwrap().as_array().unwrap() {
|
2022-10-14 14:53:30 +00:00
|
|
|
hidden_abilities.push(a.as_str().unwrap().into());
|
2022-06-17 17:53:33 +00:00
|
|
|
}
|
2022-10-14 14:53:30 +00:00
|
|
|
let base_stats = parse_statistics(value.get("baseStats").unwrap());
|
2022-06-17 17:53:33 +00:00
|
|
|
// TODO: ev reward
|
2022-09-17 07:38:02 +00:00
|
|
|
let height = value.get("height").unwrap().as_f64().unwrap();
|
|
|
|
let weight = value.get("weight").unwrap().as_f64().unwrap();
|
|
|
|
let base_experience = value.get("baseExp").unwrap().as_u64().unwrap();
|
|
|
|
let types = value
|
|
|
|
.get("types")
|
|
|
|
.unwrap()
|
2022-06-17 17:53:33 +00:00
|
|
|
.as_array()
|
|
|
|
.unwrap()
|
|
|
|
.iter()
|
2022-12-24 11:48:00 +00:00
|
|
|
.map(|a| types.get_type_id(&a.as_str().unwrap().into()).unwrap())
|
2022-06-17 17:53:33 +00:00
|
|
|
.collect();
|
|
|
|
|
2022-12-24 11:48:00 +00:00
|
|
|
let moves = parse_moves(value.get("moves").unwrap(), moves);
|
2022-06-17 17:53:33 +00:00
|
|
|
|
2022-11-28 20:34:28 +00:00
|
|
|
Arc::new(FormImpl::new(
|
2022-06-17 17:53:33 +00:00
|
|
|
&name,
|
|
|
|
height as f32,
|
|
|
|
weight as f32,
|
|
|
|
base_experience as u32,
|
|
|
|
types,
|
|
|
|
base_stats,
|
|
|
|
abilities,
|
|
|
|
hidden_abilities,
|
|
|
|
moves,
|
|
|
|
Default::default(),
|
2022-10-08 11:15:04 +00:00
|
|
|
))
|
2022-06-17 17:53:33 +00:00
|
|
|
}
|
|
|
|
|
2023-06-24 12:44:23 +00:00
|
|
|
fn parse_statistics<T>(value: &Value) -> Arc<StaticStatisticSet<T>>
|
2022-06-17 17:53:33 +00:00
|
|
|
where
|
|
|
|
T: PrimInt + TryFrom<u64>,
|
|
|
|
<T as TryFrom<u64>>::Error: Debug,
|
|
|
|
{
|
2023-06-24 12:44:23 +00:00
|
|
|
Arc::new(StaticStatisticSet::new(
|
2022-06-17 17:53:33 +00:00
|
|
|
<T as TryFrom<u64>>::try_from(value.get("hp").unwrap_or(&Value::Number(0.into())).as_u64().unwrap()).unwrap(),
|
|
|
|
<T as TryFrom<u64>>::try_from(
|
|
|
|
value
|
|
|
|
.get("attack")
|
|
|
|
.unwrap_or(&Value::Number(0.into()))
|
|
|
|
.as_u64()
|
|
|
|
.unwrap(),
|
|
|
|
)
|
|
|
|
.unwrap(),
|
|
|
|
<T as TryFrom<u64>>::try_from(
|
|
|
|
value
|
|
|
|
.get("defense")
|
|
|
|
.unwrap_or(&Value::Number(0.into()))
|
|
|
|
.as_u64()
|
|
|
|
.unwrap(),
|
|
|
|
)
|
|
|
|
.unwrap(),
|
|
|
|
<T as TryFrom<u64>>::try_from(
|
|
|
|
value
|
|
|
|
.get("specialAttack")
|
|
|
|
.unwrap_or(&Value::Number(0.into()))
|
|
|
|
.as_u64()
|
|
|
|
.unwrap(),
|
|
|
|
)
|
|
|
|
.unwrap(),
|
|
|
|
<T as TryFrom<u64>>::try_from(
|
|
|
|
value
|
|
|
|
.get("specialDefense")
|
|
|
|
.unwrap_or(&Value::Number(0.into()))
|
|
|
|
.as_u64()
|
|
|
|
.unwrap(),
|
|
|
|
)
|
|
|
|
.unwrap(),
|
|
|
|
<T as TryFrom<u64>>::try_from(value.get("speed").unwrap_or(&Value::Number(0.into())).as_u64().unwrap())
|
|
|
|
.unwrap(),
|
2023-06-24 12:44:23 +00:00
|
|
|
))
|
2022-06-17 17:53:33 +00:00
|
|
|
}
|
|
|
|
|
2023-06-24 12:44:23 +00:00
|
|
|
fn parse_moves(value: &Value, move_library: &Arc<dyn MoveLibrary>) -> Arc<dyn LearnableMoves> {
|
|
|
|
let moves = LearnableMovesImpl::new(100);
|
2022-06-17 17:53:33 +00:00
|
|
|
|
2022-09-17 07:38:02 +00:00
|
|
|
let level_moves = value.get("levelMoves").unwrap().as_array().unwrap();
|
2022-06-17 17:53:33 +00:00
|
|
|
for level_move in level_moves {
|
2022-10-14 14:53:30 +00:00
|
|
|
let name = level_move.get("name").unwrap().as_str().unwrap().into();
|
2022-09-17 07:38:02 +00:00
|
|
|
let level = level_move.get("level").unwrap().as_u64().unwrap() as LevelInt;
|
2022-06-17 17:53:33 +00:00
|
|
|
assert!(move_library.get(&name).is_some());
|
2023-04-19 16:44:11 +00:00
|
|
|
moves.add_level_move(level, &name).unwrap();
|
2022-06-17 17:53:33 +00:00
|
|
|
}
|
|
|
|
|
2023-06-24 12:44:23 +00:00
|
|
|
Arc::new(moves)
|
2022-06-17 17:53:33 +00:00
|
|
|
}
|
|
|
|
|
2023-07-29 10:57:52 +00:00
|
|
|
fn parse_parameter(value: &Value) -> Arc<Parameter> {
|
2023-06-22 13:43:41 +00:00
|
|
|
Arc::new(match value {
|
2022-06-17 17:53:33 +00:00
|
|
|
Value::Null => {
|
|
|
|
panic!("Unexpected type")
|
|
|
|
}
|
2022-10-08 11:15:04 +00:00
|
|
|
Value::Bool(b) => (*b).into(),
|
2022-06-17 17:53:33 +00:00
|
|
|
Value::Number(n) => {
|
|
|
|
if n.is_f64() {
|
2022-10-08 11:15:04 +00:00
|
|
|
(n.as_f64().unwrap() as f32).into()
|
2022-06-17 17:53:33 +00:00
|
|
|
} else {
|
2022-10-08 11:15:04 +00:00
|
|
|
n.as_i64().unwrap().into()
|
2022-06-17 17:53:33 +00:00
|
|
|
}
|
|
|
|
}
|
2022-10-14 14:53:30 +00:00
|
|
|
Value::String(s) => StringKey::new(s.as_str()).into(),
|
2022-06-17 17:53:33 +00:00
|
|
|
Value::Array(_) => {
|
|
|
|
panic!("Unexpected type")
|
|
|
|
}
|
|
|
|
Value::Object(_) => {
|
|
|
|
panic!("Unexpected type")
|
|
|
|
}
|
2023-06-22 13:43:41 +00:00
|
|
|
})
|
2022-06-17 17:53:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[cfg_attr(miri, ignore)]
|
|
|
|
fn test_type_library_loaded() {
|
|
|
|
let mut path = get_project_root().unwrap();
|
|
|
|
path.push("tests/data/");
|
2022-12-24 11:48:00 +00:00
|
|
|
let types = load_types(&path.to_str().unwrap().to_string());
|
2022-06-17 17:53:33 +00:00
|
|
|
|
|
|
|
assert_eq!(
|
2023-04-19 16:44:11 +00:00
|
|
|
types
|
|
|
|
.get_effectiveness(
|
|
|
|
types.get_type_id(&"fire".into()).unwrap(),
|
|
|
|
&[types.get_type_id(&"grass".into()).unwrap()],
|
|
|
|
)
|
|
|
|
.unwrap(),
|
2022-06-17 17:53:33 +00:00
|
|
|
2.0
|
|
|
|
);
|
|
|
|
}
|