mirror of
https://github.com/Chaoscaot/schemsearch.git
synced 2025-11-05 05:54:02 +01:00
Reduce Boilerplate
This commit is contained in:
@ -19,7 +19,7 @@ pub mod pattern_mapper;
|
||||
|
||||
use serde::{Serialize, Deserialize};
|
||||
use pattern_mapper::match_palette;
|
||||
use schemsearch_files::SchematicVersioned;
|
||||
use schemsearch_files::SpongeSchematic;
|
||||
use crate::pattern_mapper::match_palette_adapt;
|
||||
|
||||
#[derive(Debug, Clone, Copy, Deserialize, Serialize)]
|
||||
@ -33,15 +33,15 @@ pub struct SearchBehavior {
|
||||
}
|
||||
|
||||
pub fn search(
|
||||
schem: SchematicVersioned,
|
||||
pattern_schem: &SchematicVersioned,
|
||||
schem: SpongeSchematic,
|
||||
pattern_schem: &SpongeSchematic,
|
||||
search_behavior: SearchBehavior,
|
||||
) -> Vec<Match> {
|
||||
if schem.get_width() < pattern_schem.get_width() || schem.get_height() < pattern_schem.get_height() || schem.get_length() < pattern_schem.get_length() {
|
||||
if schem.width < pattern_schem.width || schem.height < pattern_schem.height || schem.length < pattern_schem.length {
|
||||
return vec![];
|
||||
}
|
||||
|
||||
if pattern_schem.get_palette().len() > schem.get_palette().len() {
|
||||
if pattern_schem.palette.len() > schem.palette.len() {
|
||||
return vec![];
|
||||
}
|
||||
|
||||
@ -49,27 +49,27 @@ pub fn search(
|
||||
|
||||
let mut matches: Vec<Match> = Vec::new();
|
||||
|
||||
let pattern_data = pattern_schem.get_block_data().as_slice();
|
||||
let pattern_data = pattern_schem.block_data.as_slice();
|
||||
|
||||
let schem_data = if search_behavior.ignore_block_data {
|
||||
match_palette_adapt(&schem, &pattern_schem.get_palette(), search_behavior.ignore_block_data)
|
||||
match_palette_adapt(&schem, &pattern_schem.palette, search_behavior.ignore_block_data)
|
||||
} else {
|
||||
schem.get_block_data().clone()
|
||||
schem.block_data.clone()
|
||||
};
|
||||
|
||||
let schem_data = schem_data.as_slice();
|
||||
|
||||
let air_id = if search_behavior.ignore_air || search_behavior.air_as_any { pattern_schem.get_palette().get("minecraft:air").unwrap_or(&-1) } else { &-1};
|
||||
let air_id = if search_behavior.ignore_air || search_behavior.air_as_any { pattern_schem.palette.get("minecraft:air").unwrap_or(&-1) } else { &-1};
|
||||
|
||||
let pattern_blocks = pattern_data.len() as f32;
|
||||
|
||||
let pattern_width = pattern_schem.get_width() as usize;
|
||||
let pattern_height = pattern_schem.get_height() as usize;
|
||||
let pattern_length = pattern_schem.get_length() as usize;
|
||||
let pattern_width = pattern_schem.width as usize;
|
||||
let pattern_height = pattern_schem.height as usize;
|
||||
let pattern_length = pattern_schem.length as usize;
|
||||
|
||||
let schem_width = schem.get_width() as usize;
|
||||
let schem_height = schem.get_height() as usize;
|
||||
let schem_length = schem.get_length() as usize;
|
||||
let schem_width = schem.width as usize;
|
||||
let schem_height = schem.height as usize;
|
||||
let schem_length = schem.length as usize;
|
||||
|
||||
for y in 0..=schem_height - pattern_height {
|
||||
for z in 0..=schem_length - pattern_length {
|
||||
|
||||
@ -17,26 +17,26 @@
|
||||
|
||||
use std::collections::HashMap;
|
||||
use nbt::CompoundTag;
|
||||
use schemsearch_files::{SchematicVersioned, SpongeV2Schematic};
|
||||
use schemsearch_files::SpongeSchematic;
|
||||
use crate::normalize_data;
|
||||
|
||||
fn create_reverse_palette(schem: &SchematicVersioned) -> Vec<&str> {
|
||||
let mut reverse_palette = Vec::with_capacity(schem.get_palette_max() as usize);
|
||||
(0..schem.get_palette_max()).for_each(|_| reverse_palette.push(""));
|
||||
for (key, value) in schem.get_palette().iter() {
|
||||
fn create_reverse_palette(schem: &SpongeSchematic) -> Vec<&str> {
|
||||
let mut reverse_palette = Vec::with_capacity(schem.palette_max as usize);
|
||||
(0..schem.palette_max).for_each(|_| reverse_palette.push(""));
|
||||
for (key, value) in schem.palette.iter() {
|
||||
reverse_palette[*value as usize] = key;
|
||||
}
|
||||
reverse_palette
|
||||
}
|
||||
|
||||
pub fn strip_data(schem: &SchematicVersioned) -> SchematicVersioned {
|
||||
pub fn strip_data(schem: &SpongeSchematic) -> SpongeSchematic {
|
||||
let mut data: Vec<i32> = Vec::new();
|
||||
|
||||
let mut palette: HashMap<String, i32> = HashMap::new();
|
||||
let mut palette_max: i32 = 0;
|
||||
let reverse_palette = create_reverse_palette(schem);
|
||||
|
||||
for block in schem.get_block_data().iter() {
|
||||
for block in schem.block_data.iter() {
|
||||
let block_name = reverse_palette[*block as usize].clone();
|
||||
let block_name = block_name.split('[').next().unwrap().to_string();
|
||||
|
||||
@ -48,28 +48,28 @@ pub fn strip_data(schem: &SchematicVersioned) -> SchematicVersioned {
|
||||
data.push(*entry);
|
||||
}
|
||||
|
||||
SchematicVersioned::V2(SpongeV2Schematic {
|
||||
SpongeSchematic {
|
||||
data_version: 1,
|
||||
palette,
|
||||
palette_max,
|
||||
block_data: data,
|
||||
block_entities: schem.get_block_entities().clone(),
|
||||
height: schem.get_height(),
|
||||
length: schem.get_length(),
|
||||
width: schem.get_width(),
|
||||
block_entities: schem.block_entities.clone(),
|
||||
height: schem.height,
|
||||
length: schem.length,
|
||||
width: schem.width,
|
||||
metadata: CompoundTag::new(),
|
||||
offset: [0; 3],
|
||||
entities: None,
|
||||
},)
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
pub fn match_palette_adapt(schem: &SchematicVersioned, matching_palette: &HashMap<String, i32>, ignore_data: bool) -> Vec<i32> {
|
||||
pub fn match_palette_adapt(schem: &SpongeSchematic, matching_palette: &HashMap<String, i32>, ignore_data: bool) -> Vec<i32> {
|
||||
let mut data: Vec<i32> = Vec::new();
|
||||
let reverse_palette = create_reverse_palette(schem);
|
||||
|
||||
for x in schem.get_block_data() {
|
||||
for x in schem.block_data.iter() {
|
||||
let blockname = reverse_palette[*x as usize];
|
||||
let blockname = if ignore_data { normalize_data(blockname, ignore_data) } else { blockname };
|
||||
let block_id = match matching_palette.get(&*blockname) {
|
||||
@ -83,10 +83,10 @@ pub fn match_palette_adapt(schem: &SchematicVersioned, matching_palette: &HashMa
|
||||
}
|
||||
|
||||
pub fn match_palette(
|
||||
schem: &SchematicVersioned,
|
||||
pattern: &SchematicVersioned,
|
||||
schem: &SpongeSchematic,
|
||||
pattern: &SpongeSchematic,
|
||||
ignore_data: bool,
|
||||
) -> SchematicVersioned {
|
||||
) -> SpongeSchematic {
|
||||
if ignore_data {
|
||||
match_palette_internal(&strip_data(schem), &strip_data(pattern), ignore_data)
|
||||
} else {
|
||||
@ -95,23 +95,23 @@ pub fn match_palette(
|
||||
}
|
||||
|
||||
fn match_palette_internal(
|
||||
schem: &SchematicVersioned,
|
||||
pattern: &SchematicVersioned,
|
||||
schem: &SpongeSchematic,
|
||||
pattern: &SpongeSchematic,
|
||||
ignore_data: bool,
|
||||
) -> SchematicVersioned {
|
||||
let data_pattern: Vec<i32> = match_palette_adapt(&pattern, schem.get_palette(), ignore_data);
|
||||
) -> SpongeSchematic {
|
||||
let data_pattern: Vec<i32> = match_palette_adapt(&pattern, &schem.palette, ignore_data);
|
||||
|
||||
SchematicVersioned::V2(SpongeV2Schematic {
|
||||
SpongeSchematic {
|
||||
data_version: 0,
|
||||
palette: schem.get_palette().clone(),
|
||||
palette_max: schem.get_palette_max(),
|
||||
palette: schem.palette.clone(),
|
||||
palette_max: schem.palette_max,
|
||||
block_data: data_pattern,
|
||||
block_entities: pattern.get_block_entities().clone(),
|
||||
height: pattern.get_height(),
|
||||
length: pattern.get_length(),
|
||||
width: pattern.get_width(),
|
||||
block_entities: pattern.block_entities.clone(),
|
||||
height: pattern.height,
|
||||
length: pattern.length,
|
||||
width: pattern.width,
|
||||
metadata: CompoundTag::new(),
|
||||
offset: [0; 3],
|
||||
entities: None,
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user