Skip to content

Commit

Permalink
Make zero-sized zones crash immediately. Fixes #10
Browse files Browse the repository at this point in the history
  • Loading branch information
RundownRhino committed Mar 8, 2024
1 parent 8f00f6c commit b5fda18
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 5 deletions.
35 changes: 32 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,41 @@ impl BlockFrequencies {
}
}
#[derive(Copy, Clone)]
pub struct Zone(pub isize, pub isize, pub isize, pub isize);
pub struct Zone {
pub from_x: isize,
pub to_x: isize,
pub from_z: isize,
pub to_z: isize,
}

impl Zone {
pub fn new(from_x: isize, to_x: isize, from_z: isize, to_z: isize) -> Self {
if to_x <= from_x {
panic!(
"Tried to create a Zone with from_x={from_x}, to_x={to_x}. This is invalid - to_x \
must be larger than from_x for the zone to be nonempty. Perhaps the order of \
arguments is wrong?"
);
}
Self {
from_x,
to_x,
from_z,
to_z,
}
}

pub fn size(&self) -> usize {
(self.to_x - self.from_x) as usize * (self.to_z - self.from_z) as usize
}
}

impl From<Vec<isize>> for Zone {
fn from(vec: Vec<isize>) -> Self {
if vec.len() < 4 {
panic!("Vector too small to convert to a Zone:{:?}", vec);
}
Zone(vec[0], vec[1], vec[2], vec[3])
Zone::new(vec[0], vec[1], vec[2], vec[3])
}
}
#[derive(Clone, Copy, Debug)]
Expand All @@ -97,7 +125,8 @@ pub enum RegionVersion {
/// finds in the zone provided.
pub fn determine_version(regions: &mut RegionFileLoader, zone: Zone) -> RegionVersion {
use fastanvil::JavaChunk as JavaChunkEnum;
for mut region in iproduct!(zone.0..zone.1, zone.2..zone.3).filter_map(|(reg_x, reg_z)|
for mut region in
iproduct!(zone.from_x..zone.to_x, zone.from_z..zone.to_z).filter_map(|(reg_x, reg_z)|
// This ignores regions that fail to load, which may or may not be a good idea
regions.region(RCoord(reg_x), RCoord(reg_z)).ok().flatten())
{
Expand Down
5 changes: 3 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,8 +231,9 @@ fn process_zone_in_folder<S: AsRef<std::path::Path> + std::marker::Sync>(
zone: Zone,
dimension: &str,
) -> DimensionScanResult {
let regions_num = (zone.1 - zone.0) * (zone.3 - zone.2);
let indexes: Vec<(isize, isize)> = iproduct!(zone.0..zone.1, zone.2..zone.3).collect();
let regions_num = zone.size();
let indexes: Vec<(isize, isize)> =
iproduct!(zone.from_x..zone.to_x, zone.from_z..zone.to_z).collect();
let start = Instant::now();
let verbose = false;
// RegionFileLoader takes specifically a PathBuf, so we have to clone this one
Expand Down

0 comments on commit b5fda18

Please sign in to comment.