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-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-07-18 08:49:58 +00:00
|
|
|
use pkmn_lib::dynamic_data::DynamicLibrary;
|
2022-06-19 19:34:08 +00:00
|
|
|
use pkmn_lib::dynamic_data::Gen7BattleStatCalculator;
|
|
|
|
use pkmn_lib::dynamic_data::Gen7DamageLibrary;
|
|
|
|
use pkmn_lib::dynamic_data::Gen7MiscLibrary;
|
2022-07-18 08:16:47 +00:00
|
|
|
use pkmn_lib::script_implementations::wasm::script_resolver::WebAssemblyScriptResolver;
|
2022-06-17 17:53:33 +00:00
|
|
|
use pkmn_lib::static_data::{
|
|
|
|
Ability, AbilityLibrary, BattleItemCategory, DataLibrary, EffectParameter, Form, GrowthRateLibrary, Item,
|
|
|
|
ItemLibrary, LearnableMoves, LibrarySettings, LookupGrowthRate, MoveData, MoveLibrary, Nature, NatureLibrary,
|
2022-06-18 12:17:29 +00:00
|
|
|
SecondaryEffect, Species, StaticData, StaticStatisticSet, Statistic, TypeLibrary,
|
2022-06-17 17:53:33 +00:00
|
|
|
};
|
|
|
|
use pkmn_lib::StringKey;
|
|
|
|
|
|
|
|
pub fn load_library() -> DynamicLibrary {
|
|
|
|
let mut path = get_project_root().unwrap();
|
|
|
|
path.push("tests/data/");
|
|
|
|
let path = path.to_str().unwrap().to_string();
|
|
|
|
let mut data = StaticData::new(LibrarySettings::new(100));
|
|
|
|
load_types(&path, data.types_mut());
|
|
|
|
load_natures(&path, data.natures_mut());
|
|
|
|
load_items(&path, data.items_mut());
|
|
|
|
load_growth_rates(&path, data.growth_rates_mut());
|
|
|
|
load_abilities(&path, data.abilities_mut());
|
|
|
|
load_moves(&path, &mut data);
|
|
|
|
load_species(&path, &mut data);
|
2022-07-18 08:16:47 +00:00
|
|
|
let mut resolver = WebAssemblyScriptResolver::new();
|
2022-07-18 08:49:58 +00:00
|
|
|
load_wasm(&path, resolver.as_mut());
|
2022-07-18 08:16:47 +00:00
|
|
|
|
2022-06-17 17:53:33 +00:00
|
|
|
let dynamic = DynamicLibrary::new(
|
|
|
|
data,
|
2022-06-19 19:34:08 +00:00
|
|
|
Box::new(Gen7BattleStatCalculator {}),
|
2022-06-17 17:53:33 +00:00
|
|
|
Box::new(Gen7DamageLibrary::new(false)),
|
|
|
|
Box::new(Gen7MiscLibrary::new()),
|
2022-07-18 08:49:58 +00:00
|
|
|
resolver,
|
2022-06-17 17:53:33 +00:00
|
|
|
);
|
|
|
|
dynamic
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn load_types(path: &String, type_library: &mut TypeLibrary) {
|
|
|
|
let mut reader = csv::ReaderBuilder::new()
|
|
|
|
.delimiter(b'|')
|
|
|
|
.from_path(path.to_string() + "Types.csv")
|
|
|
|
.unwrap();
|
|
|
|
let headers = reader.headers().unwrap();
|
|
|
|
for header in headers.iter().skip(1) {
|
2022-06-18 14:06:54 +00:00
|
|
|
type_library.register_type(&StringKey::new(header));
|
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-06-18 14:06:54 +00:00
|
|
|
let offensive_type_id = type_library.get_type_id(&StringKey::new(offensive_type));
|
2022-06-17 17:53:33 +00:00
|
|
|
|
|
|
|
for (i, v) in record.iter().skip(1).enumerate() {
|
|
|
|
let effectiveness = v.parse::<f32>().unwrap();
|
2022-07-01 15:07:22 +00:00
|
|
|
type_library.set_effectiveness(offensive_type_id, (i as u8).into(), effectiveness);
|
2022-06-17 17:53:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn load_natures(path: &String, nature_library: &mut NatureLibrary) {
|
|
|
|
let mut reader = csv::ReaderBuilder::new()
|
|
|
|
.delimiter(b'|')
|
|
|
|
.from_path(path.to_string() + "Natures.csv")
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
for record in reader.records() {
|
|
|
|
let record = record.unwrap();
|
|
|
|
let nature_name = StringKey::new(record.get(0).unwrap());
|
|
|
|
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() {
|
|
|
|
nature_library.load_nature(nature_name, Nature::new(Statistic::HP, Statistic::HP, 1.0, 1.0));
|
|
|
|
} 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,
|
|
|
|
Nature::new(increased_statistic, decreased_statistic, 1.1, 0.9),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn load_items(path: &String, lib: &mut ItemLibrary) {
|
|
|
|
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();
|
|
|
|
|
|
|
|
for v in json_array {
|
|
|
|
let name = StringKey::new(v["name"].as_str().unwrap());
|
|
|
|
let category = serde_json::from_value(v["itemType"].clone()).unwrap();
|
|
|
|
let mut battle_category = BattleItemCategory::None;
|
|
|
|
if let Some(c) = v.get("battleType") {
|
|
|
|
battle_category = serde_json::from_value(c.clone()).unwrap();
|
|
|
|
}
|
|
|
|
let price = v["price"].as_i64().unwrap();
|
|
|
|
let mut flags = HashSet::new();
|
|
|
|
if let Some(f) = v.get("flags") {
|
|
|
|
let a = f.as_array().unwrap();
|
|
|
|
for flag in a {
|
|
|
|
flags.insert(StringKey::new(flag.as_str().unwrap()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
lib.add(
|
|
|
|
&name,
|
|
|
|
Box::new(Item::new(&name, category, battle_category, price as i32, flags)),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn load_growth_rates(path: &String, growth_rate_library: &mut GrowthRateLibrary) {
|
|
|
|
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();
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
growth_rate_library.add_growth_rate(&name, Box::new(LookupGrowthRate::new(experience_required)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn load_abilities(path: &String, ability_library: &mut AbilityLibrary) {
|
|
|
|
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();
|
|
|
|
|
|
|
|
for (key, value) in o {
|
|
|
|
let name = StringKey::new(key);
|
|
|
|
let mut effect = StringKey::empty();
|
|
|
|
if let Some(e) = value.get("effect") {
|
|
|
|
effect = StringKey::new(e.as_str().unwrap());
|
|
|
|
}
|
|
|
|
let mut parameters = Vec::new();
|
|
|
|
if let Some(p) = value.get("parameters") {
|
|
|
|
for par in p.as_array().unwrap() {
|
|
|
|
parameters.push(parse_effect_parameter(par));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ability_library.add(&name, Box::new(Ability::new(&name, &effect, parameters)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn load_moves(path: &String, lib: &mut StaticData) {
|
|
|
|
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();
|
|
|
|
for move_data in data {
|
|
|
|
let move_data = move_data.as_object().unwrap();
|
2022-06-18 14:06:54 +00:00
|
|
|
let move_name = StringKey::new(move_data["name"].as_str().unwrap());
|
2022-06-17 17:53:33 +00:00
|
|
|
let move_type = StringKey::new(move_data["type"].as_str().unwrap());
|
|
|
|
let move_type_id = lib.types().get_type_id(&move_type);
|
|
|
|
let move_category = serde_json::from_value(move_data["category"].clone()).unwrap();
|
|
|
|
let base_power = move_data["power"].as_i64().unwrap() as u8;
|
|
|
|
let accuracy = move_data["accuracy"].as_i64().unwrap() as u8;
|
|
|
|
let pp = move_data["pp"].as_i64().unwrap() as u8;
|
|
|
|
let target = serde_json::from_value(move_data["target"].clone()).unwrap();
|
|
|
|
let priority = move_data["priority"].as_i64().unwrap() as i8;
|
|
|
|
let secondary_effect = if let Some(v) = move_data.get("effect") {
|
|
|
|
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 {
|
|
|
|
parameters.push(parse_effect_parameter(par));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-19 10:07:54 +00:00
|
|
|
Some(SecondaryEffect::new(
|
|
|
|
chance,
|
|
|
|
StringKey::new(v["name"].as_str().unwrap()),
|
|
|
|
parameters,
|
|
|
|
))
|
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 {
|
|
|
|
flags.insert(StringKey::new(flag.as_str().unwrap()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
lib.moves_mut().add(
|
|
|
|
&move_name,
|
|
|
|
MoveData::new(
|
|
|
|
&move_name.clone(),
|
|
|
|
move_type_id,
|
|
|
|
move_category,
|
|
|
|
base_power,
|
|
|
|
accuracy,
|
|
|
|
pp,
|
|
|
|
target,
|
|
|
|
priority,
|
|
|
|
secondary_effect,
|
|
|
|
flags,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn load_species(path: &String, library: &mut StaticData) {
|
|
|
|
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();
|
|
|
|
|
|
|
|
for (key, value) in o {
|
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);
|
|
|
|
let id = value["id"].as_i64().unwrap();
|
|
|
|
let gender_rate = value["genderRatio"].as_f64().unwrap();
|
|
|
|
let growth_rate_name = StringKey::new(value["growthRate"].as_str().unwrap());
|
|
|
|
let _base_happiness = value["baseHappiness"].as_i64().unwrap();
|
|
|
|
let catch_rate = value["catchRate"].as_i64().unwrap();
|
|
|
|
let _color = value["color"].as_str().unwrap();
|
|
|
|
// let egg_groups = value["eggGroups"]
|
|
|
|
// .as_array()
|
|
|
|
// .unwrap()
|
|
|
|
// .iter()
|
|
|
|
// .map(|&a| a.as_str().unwrap())
|
|
|
|
// .collect();
|
|
|
|
let _egg_cycle = value["eggCycles"].as_i64().unwrap();
|
|
|
|
// TODO: tags
|
|
|
|
// TODO: evolutions
|
|
|
|
|
|
|
|
let forms = value["formes"].as_object().unwrap();
|
|
|
|
let default_form_value = &forms["default"];
|
|
|
|
let default_form = parse_form("default".into(), default_form_value, library);
|
|
|
|
|
2022-06-18 07:49:17 +00:00
|
|
|
let species = Box::new(Species::new(
|
2022-06-17 17:53:33 +00:00
|
|
|
id as u16,
|
|
|
|
&name,
|
|
|
|
gender_rate as f32,
|
|
|
|
&growth_rate_name,
|
|
|
|
catch_rate as u8,
|
|
|
|
default_form,
|
|
|
|
Default::default(),
|
|
|
|
));
|
|
|
|
library.species_mut().add(&name, species);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-18 08:16:47 +00:00
|
|
|
fn load_wasm(path: &String, library: &mut WebAssemblyScriptResolver) {
|
2022-07-18 08:49:58 +00:00
|
|
|
let file = File::open(path.to_string() + "gen7_scripts_rs.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();
|
2022-07-18 08:16:47 +00:00
|
|
|
library.load_wasm_from_bytes(&buffer);
|
|
|
|
}
|
|
|
|
|
2022-06-17 17:53:33 +00:00
|
|
|
fn parse_form(name: StringKey, value: &Value, library: &mut StaticData) -> Form {
|
|
|
|
let mut abilities = Vec::new();
|
|
|
|
for a in value["abilities"].as_array().unwrap() {
|
|
|
|
abilities.push(StringKey::new(a.as_str().unwrap()));
|
|
|
|
}
|
|
|
|
let mut hidden_abilities = Vec::new();
|
|
|
|
for a in value["hiddenAbilities"].as_array().unwrap() {
|
|
|
|
hidden_abilities.push(StringKey::new(a.as_str().unwrap()));
|
|
|
|
}
|
|
|
|
let base_stats = parse_statistics(&value["baseStats"]);
|
|
|
|
// TODO: ev reward
|
|
|
|
let height = value["height"].as_f64().unwrap();
|
|
|
|
let weight = value["weight"].as_f64().unwrap();
|
|
|
|
let base_experience = value["baseExp"].as_u64().unwrap();
|
|
|
|
let types = value["types"]
|
|
|
|
.as_array()
|
|
|
|
.unwrap()
|
|
|
|
.iter()
|
|
|
|
.map(|a| library.types().get_type_id(&StringKey::new(a.as_str().unwrap())))
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
let moves = parse_moves(&value["moves"], library.moves());
|
|
|
|
|
|
|
|
Form::new(
|
|
|
|
&name,
|
|
|
|
height as f32,
|
|
|
|
weight as f32,
|
|
|
|
base_experience as u32,
|
|
|
|
types,
|
|
|
|
base_stats,
|
|
|
|
abilities,
|
|
|
|
hidden_abilities,
|
|
|
|
moves,
|
|
|
|
Default::default(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-06-18 12:17:29 +00:00
|
|
|
fn parse_statistics<T>(value: &Value) -> StaticStatisticSet<T>
|
2022-06-17 17:53:33 +00:00
|
|
|
where
|
|
|
|
T: PrimInt + TryFrom<u64>,
|
|
|
|
<T as TryFrom<u64>>::Error: Debug,
|
|
|
|
{
|
2022-06-18 12:17:29 +00:00
|
|
|
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(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn parse_moves(value: &Value, move_library: &MoveLibrary) -> LearnableMoves {
|
|
|
|
let mut moves = LearnableMoves::default();
|
|
|
|
|
|
|
|
let level_moves = value["levelMoves"].as_array().unwrap();
|
|
|
|
for level_move in level_moves {
|
|
|
|
let name = StringKey::new(level_move["name"].as_str().unwrap());
|
|
|
|
let level = level_move["level"].as_u64().unwrap() as LevelInt;
|
|
|
|
assert!(move_library.get(&name).is_some());
|
|
|
|
moves.add_level_move(level, &name);
|
|
|
|
}
|
|
|
|
|
|
|
|
moves
|
|
|
|
}
|
|
|
|
|
|
|
|
fn parse_effect_parameter(value: &Value) -> EffectParameter {
|
|
|
|
match value {
|
|
|
|
Value::Null => {
|
|
|
|
panic!("Unexpected type")
|
|
|
|
}
|
|
|
|
Value::Bool(b) => EffectParameter::Bool(*b),
|
|
|
|
Value::Number(n) => {
|
|
|
|
if n.is_f64() {
|
|
|
|
EffectParameter::Float(n.as_f64().unwrap() as f32)
|
|
|
|
} else {
|
|
|
|
EffectParameter::Int(n.as_i64().unwrap())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Value::String(s) => EffectParameter::String(s.clone()),
|
|
|
|
Value::Array(_) => {
|
|
|
|
panic!("Unexpected type")
|
|
|
|
}
|
|
|
|
Value::Object(_) => {
|
|
|
|
panic!("Unexpected type")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[cfg_attr(miri, ignore)]
|
|
|
|
fn test_type_library_loaded() {
|
|
|
|
let mut path = get_project_root().unwrap();
|
|
|
|
path.push("tests/data/");
|
|
|
|
let mut lib = TypeLibrary::new(18);
|
|
|
|
load_types(&path.to_str().unwrap().to_string(), &mut lib);
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
lib.get_effectiveness(
|
|
|
|
lib.get_type_id(&StringKey::new("fire")),
|
2022-06-18 14:06:54 +00:00
|
|
|
&[lib.get_type_id(&StringKey::new("grass"))],
|
2022-06-17 17:53:33 +00:00
|
|
|
),
|
|
|
|
2.0
|
|
|
|
);
|
|
|
|
}
|