Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Distance option to prevent new towns to block old towns expansion. #7364

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1105,6 +1105,15 @@ public enum ConfigNodes {
"# Put in other words: the buffer area around every claim that no other town can claim into.",
"# Does not affect towns which are in the same nation.",
"# This will prevent town encasement to a certain degree."),
CLAIMING_MIN_PLOT_DISTANCE_FROM_OLDER_TOWN_PLOT(
"claiming.distance_rules.min_plot_distance_from_older_town_plot",
"5",
"",
"# Minimum number of plots any towns plot must be from the next older town's own plots.",
"# It work exactly as min_plot_distance_from_town_plot except that only the new town need to leave this space to the older one.",
"# Only higher value than min_plot_distance_from_town_plot will have an effect.",
"# Does not affect towns which are in the same nation.",
"# This will prevent old town to be blocked in there expansion by new town to a certain degree."),
CLAIMING_MIN_DISTANCE_FROM_TOWN_HOMEBLOCK(
"claiming.distance_rules.min_distance_from_town_homeblock",
"5",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2569,6 +2569,10 @@ public static int getMinDistanceFromTownPlotblocks() {
return getInt(ConfigNodes.CLAIMING_MIN_PLOT_DISTANCE_FROM_TOWN_PLOT);
}

public static int getMinDistanceFromOlderTownPlotblocks() {
return getInt(ConfigNodes.CLAIMING_MIN_PLOT_DISTANCE_FROM_OLDER_TOWN_PLOT);
}

public static int getMaxDistanceForTownMerge() {
return getInt(ConfigNodes.GTOWN_SETTINGS_MAX_DISTANCE_FOR_MERGE);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import java.util.Objects;
import java.util.Set;
import java.util.UUID;
import java.util.function.Predicate;

public class TownyWorld extends TownyObject {
private UUID uuid;
Expand Down Expand Up @@ -875,14 +876,17 @@ public int getMinDistanceFromOtherTownsPlots(Coord key) {
*
* @param key - Coord to check from.
* @param homeTown Players town
* @param isConcernedTown Predicate to filter out towns that should not be considered.
* @return the closest distance to another towns nearest plot.
*/
public int getMinDistanceFromOtherTownsPlots(Coord key, Town homeTown) {
private int getMinDistanceFromOtherTownsPlots(Coord key, Town homeTown, @NotNull Predicate<Town> isConcernedTown) {
final int keyX = key.getX();
final int keyZ = key.getZ();

double minSqr = -1;
for (Town town : getTowns().values()) {
if (!isConcernedTown.test(town)) continue;

if (homeTown != null)
// If the townblock either: the town is the same as homeTown OR
// both towns are in the same nation (and this is set to ignore distance in the config,) skip over the proximity filter.
Expand All @@ -906,6 +910,28 @@ public int getMinDistanceFromOtherTownsPlots(Coord key, Town homeTown) {
}
return minSqr == -1 ? Integer.MAX_VALUE : (int) Math.ceil(Math.sqrt(minSqr));
}

/**
* Checks the distance from a another town's plots.
*
* @param key - Coord to check from.
* @param homeTown Players town
* @return the closest distance to another towns nearest plot.
*/
public int getMinDistanceFromOtherTownsPlots(Coord key, Town homeTown) {
return getMinDistanceFromOtherTownsPlots(key, homeTown, t -> true);
}

/**
* Checks the distance from a another town's plots.
*
* @param key - Coord to check from.
* @param homeTown Players town
* @return the closest distance to another older towns nearest plot.
*/
public int getMinDistanceFromOtherOlderTownsPlots(Coord key, Town homeTown) {
return getMinDistanceFromOtherTownsPlots(key, homeTown, t -> homeTown == null || t.getRegistered() < homeTown.getRegistered());
}


/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,8 @@ public static List<WorldCoord> filterInvalidProximityTownBlocks(List<WorldCoord>

List<WorldCoord> out = new ArrayList<>();
for (WorldCoord worldCoord : selection)
if (worldCoord.getTownyWorld().getMinDistanceFromOtherTownsPlots(worldCoord, town) >= TownySettings.getMinDistanceFromTownPlotblocks()) {
if (worldCoord.getTownyWorld().getMinDistanceFromOtherTownsPlots(worldCoord, town) >= TownySettings.getMinDistanceFromTownPlotblocks() &&
worldCoord.getTownyWorld().getMinDistanceFromOtherOlderTownsPlots(worldCoord, town) >= TownySettings.getMinDistanceFromOlderTownPlotblocks()) {
out.add(worldCoord);
} else {
TownyMessaging.sendDebugMsg("AreaSelectionUtil:filterInvalidProximity - Coord: " + worldCoord + " too close to another town." );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,10 @@ public static boolean OutpostTests(Town town, Resident resident, TownyWorld worl
}
// Outposts can have a minimum required distance from other towns' townblocks.
int minDistance = world.getMinDistanceFromOtherTownsPlots(key, isPlotSetOutpost ? town : null);
int minDistanceOlder = world.getMinDistanceFromOtherOlderTownsPlots(key, isPlotSetOutpost ? town : null);
// Outposts can have a minimum required distance from other outposts.
if (minDistance < TownySettings.getMinDistanceFromTownPlotblocks() ||
minDistanceOlder < TownySettings.getMinDistanceFromOlderTownPlotblocks() ||
minDistance < TownySettings.getMinDistanceForOutpostsFromPlot())
throw new TownyException(Translatable.of("msg_too_close2", Translatable.of("townblock")));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,13 @@ public static void allowTownHomeBlockOrThrow(TownyWorld world, Coord key, @Nulla
return;

if (newTown) { // Tests run only when there is a new town involved.
if (TownySettings.getMinDistanceFromTownPlotblocks() > 0 || TownySettings.getNewTownMinDistanceFromTownPlots() > 0) {
if (TownySettings.getMinDistanceFromTownPlotblocks() > 0 ||
TownySettings.getMinDistanceFromOlderTownPlotblocks() > 0 ||
TownySettings.getNewTownMinDistanceFromTownPlots() > 0) {
// Sometimes new towns have special min. distances from other towns.
int minDistance = TownySettings.getNewTownMinDistanceFromTownPlots();
if (minDistance <= 0)
minDistance = TownySettings.getMinDistanceFromTownPlotblocks();
minDistance = Math.max(TownySettings.getMinDistanceFromTownPlotblocks(), TownySettings.getMinDistanceFromOlderTownPlotblocks());

// throws when a new town is being made to close to another town's land.
if (world.getMinDistanceFromOtherTownsPlots(key) < minDistance)
Expand Down Expand Up @@ -86,6 +88,10 @@ public static void allowTownClaimOrThrow(TownyWorld world, WorldCoord townBlockT
if (world.getMinDistanceFromOtherTownsPlots(townBlockToClaim, town) < TownySettings.getMinDistanceFromTownPlotblocks())
throw new TownyException(Translatable.of("msg_too_close2", Translatable.of("townblock")));

// Check distance to other older townblocks.
if (world.getMinDistanceFromOtherOlderTownsPlots(townBlockToClaim, town) < TownySettings.getMinDistanceFromOlderTownPlotblocks())
throw new TownyException(Translatable.of("msg_too_close2", Translatable.of("townblock")));

// Check adjacent claims rules.
testAdjacentClaimsRulesOrThrow(townBlockToClaim, town, outpost);

Expand Down
Loading