mirror of
https://github.com/Chaoscaot/schemsearch.git
synced 2025-11-04 21:54:00 +01:00
Improvements
This commit is contained in:
@ -17,7 +17,7 @@ pub fn search(
|
||||
schem: &Schematic,
|
||||
pattern_schem: &Schematic,
|
||||
search_behavior: SearchBehavior,
|
||||
) -> Vec<(u16, u16, u16)> {
|
||||
) -> Vec<(u16, u16, u16, f64)> {
|
||||
if schem.width < pattern_schem.width || schem.height < pattern_schem.height || schem.length < pattern_schem.length {
|
||||
return vec![];
|
||||
}
|
||||
@ -31,7 +31,7 @@ pub fn search(
|
||||
None => return vec![],
|
||||
};
|
||||
|
||||
let mut matches: Vec<(u16, u16, u16)> = Vec::new();
|
||||
let mut matches: Vec<(u16, u16, u16, f64)> = Vec::new();
|
||||
|
||||
let pattern_data = pattern_schem.block_data;
|
||||
let schem_data = &schem.block_data;
|
||||
@ -56,8 +56,9 @@ pub fn search(
|
||||
}
|
||||
}
|
||||
}
|
||||
if matching as f64 / pattern_blocks > search_behavior.threshold {
|
||||
matches.push((x as u16, y as u16, z as u16));
|
||||
let matching_percent = matching as f64 / pattern_blocks;
|
||||
if matching_percent > search_behavior.threshold {
|
||||
matches.push((x as u16, y as u16, z as u16, matching_percent));
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -70,7 +71,7 @@ pub fn normalize_data(data: &String, ignore_data: bool) -> String {
|
||||
if ignore_data {
|
||||
data.split('[').next().unwrap().to_string()
|
||||
} else {
|
||||
data.clone()
|
||||
data.to_string()
|
||||
}
|
||||
}
|
||||
|
||||
@ -93,7 +94,7 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn read_schematic() {
|
||||
let schematic = Schematic::load(Path::new("../tests/simple.schem"));
|
||||
let schematic = Schematic::load(Path::new("../tests/simple.schem")).unwrap();
|
||||
assert_eq!(schematic.width as usize * schematic.height as usize * schematic.length as usize, schematic.block_data.len());
|
||||
assert_eq!(schematic.palette_max, schematic.palette.len() as i32);
|
||||
}
|
||||
@ -108,7 +109,7 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn test_strip_schem() {
|
||||
let schematic = Schematic::load(Path::new("../tests/simple.schem"));
|
||||
let schematic = Schematic::load(Path::new("../tests/simple.schem")).unwrap();
|
||||
let stripped = strip_data(&schematic);
|
||||
|
||||
assert_eq!(stripped.palette.keys().any(|k| k.contains('[')), false);
|
||||
@ -116,28 +117,26 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn test_match_palette() {
|
||||
let schematic = Schematic::load(Path::new("../tests/simple.schem"));
|
||||
let endstone = Schematic::load(Path::new("../tests/endstone.schem"));
|
||||
let schematic = Schematic::load(Path::new("../tests/simple.schem")).unwrap();
|
||||
let endstone = Schematic::load(Path::new("../tests/endstone.schem")).unwrap();
|
||||
|
||||
let matched_schematic = match_palette(&schematic, &endstone, true);
|
||||
let _ = match_palette(&schematic, &endstone, true);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_match_palette_ignore_data() {
|
||||
let schematic = Schematic::load(Path::new("../tests/simple.schem"));
|
||||
let endstone = Schematic::load(Path::new("../tests/endstone.schem"));
|
||||
let schematic = Schematic::load(Path::new("../tests/simple.schem")).unwrap();
|
||||
let endstone = Schematic::load(Path::new("../tests/endstone.schem")).unwrap();
|
||||
|
||||
let matched_schematic = match_palette(&schematic, &endstone, false);
|
||||
let _ = match_palette(&schematic, &endstone, false);
|
||||
}
|
||||
|
||||
#[test]
|
||||
pub fn test_big_search() {
|
||||
let file = std::fs::File::open("../tests/simple.schem").expect("Failed to open file");
|
||||
let schematic = &std::io::Read::bytes(file).map(|b| b.unwrap()).collect();
|
||||
let file = std::fs::File::open("../tests/endstone.schem").expect("Failed to open file");
|
||||
let pattern = &std::io::Read::bytes(file).map(|b| b.unwrap()).collect();
|
||||
let schematic = Schematic::load(Path::new("../tests/simple.schem")).unwrap();
|
||||
let endstone = Schematic::load(Path::new("../tests/endstone.schem")).unwrap();
|
||||
|
||||
let matches = search(schematic, pattern, SearchBehavior {
|
||||
let _ = search(&schematic, &endstone, SearchBehavior {
|
||||
ignore_block_data: true,
|
||||
ignore_block_entities: true,
|
||||
ignore_entities: true,
|
||||
@ -149,12 +148,10 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
pub fn test_search() {
|
||||
let file = std::fs::File::open("../tests/Random.schem").expect("Failed to open file");
|
||||
let schematic = &std::io::Read::bytes(file).map(|b| b.unwrap()).collect();
|
||||
let file = std::fs::File::open("../tests/Pattern.schem").expect("Failed to open file");
|
||||
let pattern = &std::io::Read::bytes(file).map(|b| b.unwrap()).collect();
|
||||
let schematic = Schematic::load(Path::new("../tests/Random.schem")).unwrap();
|
||||
let pattern = Schematic::load(Path::new("../tests/Pattern.schem")).unwrap();
|
||||
|
||||
let matches = search(schematic, pattern, SearchBehavior {
|
||||
let matches = search(&schematic, &pattern, SearchBehavior {
|
||||
ignore_block_data: true,
|
||||
ignore_block_entities: true,
|
||||
ignore_entities: true,
|
||||
@ -165,6 +162,6 @@ mod tests {
|
||||
|
||||
println!("{:?}", matches);
|
||||
assert_eq!(matches.len(), 1);
|
||||
assert_eq!(matches[0], (1, 0, 3));
|
||||
assert_eq!(matches[0], (1, 0, 3, 1.0));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user