add match-all option to AdjacentPatternLocator

This commit is contained in:
dfsek
2022-01-15 01:41:26 -07:00
parent 37d80b63d9
commit 9794bf3565
2 changed files with 21 additions and 6 deletions

View File

@@ -7,6 +7,7 @@
package com.dfsek.terra.addons.feature.locator.config;
import com.dfsek.tectonic.api.config.template.annotations.Default;
import com.dfsek.tectonic.api.config.template.annotations.Value;
import com.dfsek.tectonic.api.config.template.object.ObjectTemplate;
@@ -17,6 +18,7 @@ import com.dfsek.terra.api.structure.feature.Locator;
import com.dfsek.terra.api.util.Range;
@SuppressWarnings({ "FieldCanBeLocal", "FieldMayBeFinal" })
public class AdjacentPatternLocatorTemplate implements ObjectTemplate<Locator> {
@Value("range")
private @Meta Range range;
@@ -24,8 +26,12 @@ public class AdjacentPatternLocatorTemplate implements ObjectTemplate<Locator> {
@Value("pattern")
private @Meta Pattern pattern;
@Value("match-all")
@Default
private @Meta boolean matchAll = false;
@Override
public Locator get() {
return new AdjacentPatternLocator(pattern, range);
return new AdjacentPatternLocator(pattern, range, matchAll);
}
}

View File

@@ -17,10 +17,12 @@ import com.dfsek.terra.api.world.chunk.generation.util.Column;
public class AdjacentPatternLocator implements Locator {
private final Pattern pattern;
private final Range search;
private final boolean matchAll;
public AdjacentPatternLocator(Pattern pattern, Range search) {
public AdjacentPatternLocator(Pattern pattern, Range search, boolean matchAll) {
this.pattern = pattern;
this.search = search;
this.matchAll = matchAll;
}
@Override
@@ -35,9 +37,16 @@ public class AdjacentPatternLocator implements Locator {
}
private boolean isValid(int y, Column<?> column) {
return pattern.matches(y, column.adjacent(0, -1)) ||
pattern.matches(y, column.adjacent(0, 1)) ||
pattern.matches(y, column.adjacent(-1, 0)) ||
pattern.matches(y, column.adjacent(1, 0));
if(matchAll) {
return pattern.matches(y, column.adjacent(0, -1)) &&
pattern.matches(y, column.adjacent(0, 1)) &&
pattern.matches(y, column.adjacent(-1, 0)) &&
pattern.matches(y, column.adjacent(1, 0));
} else {
return pattern.matches(y, column.adjacent(0, -1)) ||
pattern.matches(y, column.adjacent(0, 1)) ||
pattern.matches(y, column.adjacent(-1, 0)) ||
pattern.matches(y, column.adjacent(1, 0));
}
}
}