Gene source and serde

This commit is contained in:
rendo 2026-04-01 09:06:25 +05:00
commit 264a8d9723
5 changed files with 55 additions and 31 deletions

View file

@ -333,6 +333,7 @@ version = "0.1.0"
dependencies = [ dependencies = [
"godot", "godot",
"rand", "rand",
"serde",
] ]
[[package]] [[package]]
@ -348,6 +349,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9a8e94ea7f378bd32cbbd37198a4a91436180c5bb472411e48b5ec2e2124ae9e" checksum = "9a8e94ea7f378bd32cbbd37198a4a91436180c5bb472411e48b5ec2e2124ae9e"
dependencies = [ dependencies = [
"serde_core", "serde_core",
"serde_derive",
] ]
[[package]] [[package]]

View file

@ -6,6 +6,7 @@ edition = "2024"
[dependencies] [dependencies]
godot = "0.4.5" godot = "0.4.5"
rand = "0.10.0" rand = "0.10.0"
serde = { version = "1.0.228", features = ["derive"] }
[lib] [lib]
crate-type = ["cdylib"] crate-type = ["cdylib"]

View file

@ -4,7 +4,7 @@ use crate::genetics::flow::Flow;
#[derive(PartialEq,Eq,Clone)] #[derive(PartialEq,Eq,Clone)]
pub struct Gene { pub struct Gene {
pub plant_name: String, pub plant_source: GeneSource,
pub place: String, pub place: String,
pub kind: GeneType pub kind: GeneType
} }
@ -13,6 +13,14 @@ pub struct GeneContext {
pub flow: Flow pub flow: Flow
} }
#[derive(PartialEq,Eq,Clone,Copy)]
pub enum GeneSource {
Peashooter,
Sunflower,
CherryBomb,
PotatoMine,
}
/// GeneType is an enum that defines behaviour of gene. /// GeneType is an enum that defines behaviour of gene.
#[derive(PartialEq,Eq,Clone)] #[derive(PartialEq,Eq,Clone)]
pub enum GeneType { pub enum GeneType {
@ -67,18 +75,30 @@ pub enum ProduceDecision {
} }
impl Gene { impl Gene {
pub fn new(plant_str: impl Into<String>, place_str: impl Into<String>, kind: GeneType) -> Self { pub fn new(plant_source: GeneSource, place_str: impl Into<String>, kind: GeneType) -> Self {
Self { Self {
plant_name: plant_str.into(), plant_source,
place: place_str.into(), place: place_str.into(),
kind, kind,
} }
} }
} }
impl Display for GeneSource {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f,"{}",match self {
GeneSource::Peashooter => "Peashooter",
GeneSource::Sunflower => "Sunflower",
GeneSource::CherryBomb => "CherryBomb",
GeneSource::PotatoMine => "PotatoMine",
});
Ok(())
}
}
impl Display for Gene { impl Display for Gene {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f,"({}) {}",self.plant_name,self.place); write!(f,"({}) {}",self.plant_source,self.place);
Ok(()) Ok(())
} }
} }
@ -121,3 +141,4 @@ impl GeneType {
ChildDistribution::All ChildDistribution::All
} }
} }

View file

@ -1,12 +1,12 @@
use crate::genetics::{flow::Flow, prelude::*}; use crate::genetics::{gene::GeneSource,flow::Flow, prelude::*};
pub fn peashooter_template() -> Genome { pub fn peashooter_template() -> Genome {
Genome::from_edges(vec![ Genome::from_edges(vec![
Gene::new("Peashooter","root", GeneType::pure_producer(|_|{Flow::empty()})), Gene::new(GeneSource::Peashooter,"root", GeneType::pure_producer(|_|{Flow::empty()})),
Gene::new("Peashooter","stem", GeneType::fill_all()), Gene::new(GeneSource::Peashooter,"stem", GeneType::fill_all()),
Gene::new("Peashooter","head",GeneType::random_distribution()), Gene::new(GeneSource::Peashooter,"head",GeneType::random_distribution()),
Gene::new("Peashooter","leaf",GeneType::modifier(|_|{Flow::empty()})), Gene::new(GeneSource::Peashooter,"leaf",GeneType::modifier(|_|{Flow::empty()})),
Gene::new("Peashooter","face",GeneType::consumer(|_|{})) Gene::new(GeneSource::Peashooter,"face",GeneType::consumer(|_|{}))
],vec![ ],vec![
(0,1), (0,1),
(1,2), (1,2),
@ -17,10 +17,10 @@ pub fn peashooter_template() -> Genome {
pub fn sunflower_template() -> Genome { pub fn sunflower_template() -> Genome {
Genome::from_edges(vec![ Genome::from_edges(vec![
Gene::new("Sunflower","root", GeneType::Dummy), Gene::new(GeneSource::Sunflower,"root", GeneType::Dummy),
Gene::new("Sunflower","stem", GeneType::Dummy), Gene::new(GeneSource::Sunflower,"stem", GeneType::Dummy),
Gene::new("Sunflower","head", GeneType::modifier(|_|{Flow::empty()})), Gene::new(GeneSource::Sunflower,"head", GeneType::modifier(|_|{Flow::empty()})),
Gene::new("Sunflower","face", GeneType::consumer(|_|{})), Gene::new(GeneSource::Sunflower,"face", GeneType::consumer(|_|{})),
], vec![ ], vec![
(0,1), (0,1),
(1,2), (1,2),
@ -30,11 +30,11 @@ pub fn sunflower_template() -> Genome {
pub fn cherry_bomb_template() -> Genome { pub fn cherry_bomb_template() -> Genome {
Genome::from_edges(vec![ Genome::from_edges(vec![
Gene::new("Cherry bomb","root", GeneType::modifier(|_|{Flow::empty()})), Gene::new(GeneSource::CherryBomb,"root", GeneType::modifier(|_|{Flow::empty()})),
Gene::new("Cherry bomb","head", GeneType::consumer(|_|{})), Gene::new(GeneSource::CherryBomb,"head", GeneType::consumer(|_|{})),
Gene::new("Cherry bomb","head", GeneType::consumer(|_|{})), Gene::new(GeneSource::CherryBomb,"head", GeneType::consumer(|_|{})),
Gene::new("Cherry bomb","face", GeneType::Dummy), Gene::new(GeneSource::CherryBomb,"face", GeneType::Dummy),
Gene::new("Cherry bomb","face", GeneType::Dummy), Gene::new(GeneSource::CherryBomb,"face", GeneType::Dummy),
], vec![ ], vec![
(0,1), (0,1),
(0,2), (0,2),

View file

@ -1,4 +1,4 @@
use crate::genetics::{flow::Flow, gene::{Gene, GeneType}, genome::Genome, manipulations::PairGenomeModificator, plant_templates::{peashooter_template, sunflower_template}}; use crate::genetics::{flow::Flow, gene::{Gene, GeneType,GeneSource}, genome::Genome, manipulations::PairGenomeModificator, plant_templates::{peashooter_template, sunflower_template}};
#[test] #[test]
fn test_display() { fn test_display() {
@ -11,8 +11,8 @@ fn test_allelic_crossingover() {
let mut peashooter = peashooter_template(); let mut peashooter = peashooter_template();
let mut sunflower = sunflower_template(); let mut sunflower = sunflower_template();
println!("Peashooter:\n{}",peashooter); println!("Peashooter\n{}",peashooter);
println!("Sunflower:\n{}",sunflower); println!("Sunflower\n{}",sunflower);
(peashooter,sunflower) = PairGenomeModificator::with_seed(peashooter, sunflower,3996684975687038250u64).allelic_crossingover(None); (peashooter,sunflower) = PairGenomeModificator::with_seed(peashooter, sunflower,3996684975687038250u64).allelic_crossingover(None);
@ -20,11 +20,11 @@ fn test_allelic_crossingover() {
println!("Sunflower:\n{}",sunflower); println!("Sunflower:\n{}",sunflower);
assert!(peashooter.to_string() == Genome::from_edges(vec![ assert!(peashooter.to_string() == Genome::from_edges(vec![
Gene::new("Sunflower","root", GeneType::Dummy), Gene::new(GeneSource::Sunflower,"root", GeneType::Dummy),
Gene::new("Peashooter","stem", GeneType::fill_all()), Gene::new(GeneSource::Peashooter,"stem", GeneType::fill_all()),
Gene::new("Peashooter","head",GeneType::random_distribution()), Gene::new(GeneSource::Peashooter,"head",GeneType::random_distribution()),
Gene::new("Sunflower","face", GeneType::consumer(|_|{})), Gene::new(GeneSource::Sunflower,"face", GeneType::consumer(|_|{})),
Gene::new("Peashooter","face",GeneType::consumer(|_|{})) Gene::new(GeneSource::Peashooter,"face",GeneType::consumer(|_|{}))
],vec![ ],vec![
(0,1), (0,1),
(1,2), (1,2),
@ -33,10 +33,10 @@ fn test_allelic_crossingover() {
]).unwrap().to_string()); ]).unwrap().to_string());
assert!(sunflower.to_string() == Genome::from_edges(vec![ assert!(sunflower.to_string() == Genome::from_edges(vec![
Gene::new("Peashooter","root", GeneType::pure_producer(|_|{Flow::empty()})), Gene::new(GeneSource::Peashooter,"root", GeneType::pure_producer(|_|{Flow::empty()})),
Gene::new("Sunflower","stem", GeneType::Dummy), Gene::new(GeneSource::Sunflower,"stem", GeneType::Dummy),
Gene::new("Sunflower","head", GeneType::modifier(|_|{Flow::empty()})), Gene::new(GeneSource::Sunflower,"head", GeneType::modifier(|_|{Flow::empty()})),
Gene::new("Peashooter","leaf",GeneType::modifier(|_|{Flow::empty()})), Gene::new(GeneSource::Peashooter,"leaf",GeneType::modifier(|_|{Flow::empty()})),
], vec![ ], vec![
(0,1), (0,1),
(1,2), (1,2),