use std::collections::HashMap;
use std::fs::File;
use crate::assigner::MapColoringAssigner;
use crate::sampler::MultinomialDistribution;
use super::visualizers::MapColor;
pub const TUNNELS_COLORMAP_FILENAME: &str = "morkovmap_colormap_tunnel.json";
pub const TUNNELS_RULESET_FILENAME: &str = "morkovmap_rules_tunnel.json";
pub(crate) const WATER: i8 = 1;
pub(crate) const GRASS: i8 = 2;
pub(crate) const SAND: i8 = 3;
pub(crate) const SNOW: i8 = 4;
pub(crate) const ROCKY: i8 = 5;
pub const LANDMASS_COLORMAP_FILENAME: &str = "morkovmap_colormap_landmass.json";
pub const LANDMASS_RULESET_FILENAME: &str = "morkovmap_rules_landmass.json";
fn landmass_generate_colormap() -> HashMap<i8, MapColor> {
let colormap: HashMap<i8, MapColor> = HashMap::from_iter([
(WATER, ril::Rgb::new(50, 50, 225).into()),
(GRASS, ril::Rgb::new(50, 200, 50).into()),
(SAND, ril::Rgb::new(200, 200, 50).into()),
(SNOW, ril::Rgb::new(210, 210, 220).into()),
(ROCKY, ril::Rgb::new(40, 40, 65).into()),
]);
colormap
}
fn landmass_generate_rules() -> MapColoringAssigner<i8> {
let rules = HashMap::from([
(WATER, MultinomialDistribution::from(
HashMap::from([
(WATER, 45.),
(SAND, 1.),
])
)),
(GRASS, MultinomialDistribution::from(
HashMap::from([
(GRASS, 80.),
(SAND, 5.),
(SNOW, 3.),
(ROCKY, 1.),
])
)),
(SAND, MultinomialDistribution::from(
HashMap::from([
(1, 50.),
(GRASS, 40.),
(SAND, 30.),
(ROCKY, 0.000001),
])
)),
(SNOW, MultinomialDistribution::from(
HashMap::from([
(GRASS, 10.),
(SAND, 1.),
(SNOW, 25.),
(ROCKY, 5.),
])
)),
(ROCKY, MultinomialDistribution::from(
HashMap::from([
(1, 0.000001),
(GRASS, 1.),
(SAND, 0.000001),
(SNOW, 45.),
(ROCKY, 70.),
])
)),
]);
MapColoringAssigner::with_rules(rules)
}
fn save_colormap(filepath: &str) -> HashMap<i8, MapColor> {
let colormap = landmass_generate_colormap();
let rule_file = File::create(filepath).unwrap();
serde_json::to_writer_pretty(rule_file, &colormap).unwrap();
colormap
}
pub fn read_colormap(filepath: &str) -> HashMap<i8, ril::Rgb> {
let rule_file = File::open(filepath);
let raw_result = match rule_file {
Ok(rule_fh) => serde_json::from_reader(rule_fh).unwrap(),
Err(err) => {
eprintln!("{}", err);
save_colormap(filepath)
}
};
let cast_result = raw_result
.iter()
.map(|(k, v)| (
k.to_owned(),
{ let rgb: ril::Rgb = v.to_owned().into(); rgb }
)).collect();
cast_result
}
pub fn save_rules(filepath: &str) -> MapColoringAssigner<i8> {
let rules = landmass_generate_rules();
serde_json::to_writer_pretty(
File::create(filepath).unwrap(),
&rules
).unwrap();
rules
}
pub fn read_rules(filepath: &str) -> MapColoringAssigner<i8> {
let rule_file = File::open(filepath);
match rule_file {
Ok(rule_fh) => serde_json::from_reader(rule_fh).unwrap(),
Err(err) => {
eprintln!("{}", err);
save_rules(filepath)
}
}
}