Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -146,7 +146,7 @@ public boolean canClaim(int provinceId, boolean sea) {
if (target == null || !target.isValid()) return false;
if (provinces.isEmpty()) return true;

return bordersRealm(pm, provinceId) || sea && isSeaAdjacentFromSet(pm, target, seaClaimSources(pm));
return bordersRealm(pm, provinceId) || sea && seaReachableCoasts(pm, seaClaimSources(pm)).contains(provinceId);
}

/**
Expand All @@ -161,17 +161,13 @@ public boolean canClaimAny(java.util.Collection<Integer> targets) {
if (pm == null) return false;

Set<Integer> capitalCoasts = seaReachableCoasts(pm, capitalLandBlob(pm));
// Land touching a sea reachable from the sea claim sources: what isSeaAdjacentFromSet accepts.
Set<Integer> seaSources = seaClaimSources(pm);
Set<Integer> sailingCoasts = seaReachableCoasts(pm, seaSources);
Set<Integer> sailingCoasts = seaReachableCoasts(pm, seaClaimSources(pm));
Set<Integer> sources = claimSources(pm);
for (Integer provinceId : targets) {
if (provinceId == null || provinces.contains(provinceId)) continue;
Province target = pm.get(provinceId);
if (target == null || !target.isValid()) continue;
if (capitalCoasts.contains(provinceId) || sailingCoasts.contains(provinceId)) return true;
// The coast sets hold only land, so a sea province in the list takes the per-target check.
if (target.isSea() && isSeaAdjacentFromSet(pm, target, seaSources)) return true;
for (int ownedId : sources) {
if (isEffectivelyAdjacent(pm, ownedId, provinceId, capitalCoasts)) return true;
}
Expand Down Expand Up @@ -205,7 +201,8 @@ private Set<Integer> claimSources(ProvinceManager pm) {
/**
* Land held by this faction's vassals and their vassals. It anchors the realm like the capital:
* claims may border it and own land joined to it stays claimed, even where it does not touch the
* capital. It is never this faction's own.
* capital. It is never this faction's own. Only vassal land joined by land to the capital opens a
* sea crossing; see {@link #capitalLandBlob(ProvinceManager)}.
*/
private Set<Integer> vassalLand() {
Set<Integer> land = new HashSet<>();
Expand Down Expand Up @@ -266,17 +263,10 @@ public boolean canClaim(int provinceId, boolean sea, Guild guild) {

// Apply the same river/coastal adjacency as normal claims, without the old guild capital.
Set<Integer> capitalBlob = new HashSet<>();
if (hasCapital() && capital != guild.getCapital()) {
boolean seated = hasCapital() && provinces.contains(capital);
if (seated && capital != guild.getCapital()) {
capitalBlob.add(capital);
floodLandExcluding(pm, capital, capitalBlob, guild.getCapital());
if (provinces.contains(capital)) {
// Vassal capitals open the sea too, as in capitalLandBlob.
for (int seat : subjectSeats()) {
if (seat != guild.getCapital() && !capitalBlob.contains(seat)) {
floodLandExcluding(pm, seat, capitalBlob, guild.getCapital());
}
}
}
}
Set<Integer> seaCoasts = seaReachableCoasts(pm, capitalBlob);
for (int ownedId : legalWithoutGuildCapital) {
Expand All @@ -285,12 +275,8 @@ public boolean canClaim(int provinceId, boolean sea, Guild guild) {
}
}

// 2) Sea-adjacency mode (only from legal provinces)
if (sea) {
return isSeaAdjacentFromSet(pm, target, legalWithoutGuildCapital);
}

return false;
// 2) Sea mode: a realm without a seated capital may sail from what it keeps.
return sea && !seated && seaReachableCoasts(pm, legalWithoutGuildCapital).contains(provinceId);
}

/**
Expand Down Expand Up @@ -360,99 +346,6 @@ private void floodLandExcluding(ProvinceManager pm, int start, Set<Integer> out,
}
}

/**
* Check sea adjacency from a specific set of provinces (used for guild relocation checks).
*/
private boolean isSeaAdjacentFromSet(ProvinceManager pm, Province target, Set<Integer> fromProvinces) {
Set<Integer> visited = new HashSet<>();
ArrayDeque<Integer> queue = new ArrayDeque<>();

// Seed the queue with sea provinces adjacent to the legal province set
for (int ownedId : fromProvinces) {
Province owned = pm.get(ownedId);
if (owned == null) continue;

for (int nId : owned.getNeighbours()) {
Province n = pm.get(nId);
if (n == null) continue;

if (n.isSea()) {
queue.add(nId);
visited.add(nId);
}
}
}

// Flood-fill sea region
while (!queue.isEmpty()) {
int currentId = queue.poll();
Province current = pm.get(currentId);
if (current == null) continue;

// If target touches this sea province
if (current.getNeighbours().contains(target.getId())) {
return true;
}

for (int nId : current.getNeighbours()) {
if (visited.contains(nId)) continue;

Province n = pm.get(nId);
if (n == null || !n.isSea()) continue;

visited.add(nId);
queue.add(nId);
}
}

return false;
}

private boolean isSeaAdjacent(ProvinceManager pm, Province target) {
Set<Integer> visited = new HashSet<>();
ArrayDeque<Integer> queue = new ArrayDeque<>();

// Seed the queue with sea/water provinces adjacent to owned territory
for (int ownedId : provinces) {
Province owned = pm.get(ownedId);
if (owned == null) continue;

for (int nId : owned.getNeighbours()) {
Province n = pm.get(nId);
if (n == null) continue;

if (n.isSea()) {
queue.add(nId);
visited.add(nId);
}
}
}

// Flood-fill sea region
while (!queue.isEmpty()) {
int currentId = queue.poll();
Province current = pm.get(currentId);
if (current == null) continue;

// If target touches this sea province
if (current.getNeighbours().contains(target.getId())) {
return true;
}

for (int nId : current.getNeighbours()) {
if (visited.contains(nId)) continue;

Province n = pm.get(nId);
if (n == null || !n.isSea()) continue;

visited.add(nId);
queue.add(nId);
}
}

return false;
}

public void revalidateClaims() {
ProvinceManager pm = SimpleFactions.getInstance().getProvinceManager();

Expand Down Expand Up @@ -566,24 +459,26 @@ private Set<Integer> computeLegalProvinces(ProvinceManager pm, int factionCapita
}

/**
* Where a sea-mode claim may set sail from: land that traces back to the faction capital.
* A realm without a seated capital keeps sailing from anything it owns.
* Where a sea-mode claim may set sail from: the capital's land blob, as for any other claim.
* A realm without a seated capital sails from anything it owns.
*/
private Set<Integer> seaClaimSources(ProvinceManager pm) {
if (!provinces.contains(capital)) {
return new HashSet<>(provinces);
}
return computeLegalProvinces(pm, capital);
return capitalLandBlob(pm);
}

private Set<Integer> legalGuildCapitals(ProvinceManager pm, int factionCapital, java.util.Collection<Integer> candidates) {
return legalGuildCapitals(pm, factionCapital, candidates, capitalLandBlob(pm, factionCapital));
}

/**
* Guild capitals that trace back to the faction capital: over owned land, or by water from
* land already traced, so one colony can anchor the next. An enclave cut off from the realm
* cannot vouch for itself by touching its own lake.
* Guild capitals that keep their seat: on land joined to the faction capital or to vassal land,
* on a shore a sea route from the capital's land blob reaches, or across one river or lake from
* land that traces back. A guild capital never opens a sea route of its own, so one colony cannot
* anchor the next, and an enclave cut off from the realm cannot vouch for itself by touching its
* own lake.
*/
private Set<Integer> legalGuildCapitals(ProvinceManager pm, int factionCapital,
java.util.Collection<Integer> candidates, Set<Integer> capitalBlob) {
Expand All @@ -597,47 +492,55 @@ private Set<Integer> legalGuildCapitals(ProvinceManager pm, int factionCapital,
}
if (!provinces.contains(factionCapital)) {
// Without a seated faction capital there is nothing to trace back to.
Set<Integer> shores = seaReachableCoasts(pm, new HashSet<>(provinces));
for (int cap : pending) {
if (isSeaAdjacent(pm, pm.get(cap))) legal.add(cap);
if (shores.contains(cap)) legal.add(cap);
}
return legal;
}

Set<Integer> seeds = new HashSet<>(Set.of(factionCapital));
// Vassal land anchors guild capitals as the faction capital does: over land, or by water.
// Vassal land anchors guild capitals as the faction capital does.
Set<Integer> anchors = vassalLand();
seeds.addAll(anchors);
Set<Integer> anchoredLand = new HashSet<>();
for (int anchor : anchors) {
if (!anchoredLand.contains(anchor)) floodLand(pm, anchor, anchoredLand);
}
boolean changed = true;
while (changed) {
changed = false;
Set<Integer> reached = expandLegal(pm, seeds, capitalBlob);
for (java.util.Iterator<Integer> it = pending.iterator(); it.hasNext();) {
int cap = it.next();
if (capitalBlob.contains(cap) || anchoredLand.contains(cap)
|| isSeaAdjacentFromSet(pm, pm.get(cap), reached)) {
legal.add(cap);
seeds.add(cap);
it.remove();
changed = true;
}
Set<Integer> seeds = new HashSet<>(Set.of(factionCapital));
seeds.addAll(anchors);
Set<Integer> reached = expandLegal(pm, seeds, capitalBlob);
Set<Integer> seaCoasts = seaReachableCoasts(pm, capitalBlob);
for (int cap : pending) {
if (capitalBlob.contains(cap) || anchoredLand.contains(cap) || seaCoasts.contains(cap)
|| acrossWaterFrom(pm, cap, reached)) {
legal.add(cap);
}
}
return legal;
}

/** Whether {@code id} faces land in {@code from} across one WATER (river or lake) province. */
private boolean acrossWaterFrom(ProvinceManager pm, int id, Set<Integer> from) {
Province province = pm.get(id);
if (province == null) return false;
for (int neighbour : province.getNeighbours()) {
Province water = pm.get(neighbour);
if (water == null || water.getTerrain() != Terrain.WATER) continue;
for (int shore : water.getNeighbours()) {
if (shore != id && from.contains(shore)) return true;
}
}
return false;
}

private Set<Integer> expandLegal(ProvinceManager pm, Set<Integer> seeds, Set<Integer> capitalBlob) {
Set<Integer> legal = new HashSet<>(seeds);
Set<Integer> seaCoasts = seaReachableCoasts(pm, capitalBlob);

// Follow the same edges that allowed these claims: owned land, WATER
// bridges, and continuous sea routes from the proposed capital's land blob.
// Reached overseas holdings may extend inland or over WATER, but do not
// become new sources for SEA crossings. Vassal land carries the connection
// like owned land, so the result can include it.
// Reached overseas holdings and vassal land may extend inland or over WATER,
// but never become new sources for sea crossings. Vassal land carries the
// connection like owned land, so the result can include it.
Set<Integer> realm = realmLand();
ArrayDeque<Integer> queue = new ArrayDeque<>(legal);
while (!queue.isEmpty()) {
Expand Down Expand Up @@ -752,17 +655,18 @@ public String getClaimDeniedReason(int provinceId, boolean sea) {
: "§cThis province does not border your realm or your vassals' land.";
}

if (adjacent || isSeaAdjacentFromSet(pm, target, seaClaimSources(pm))) {
if (adjacent || seaReachableCoasts(pm, seaClaimSources(pm)).contains(provinceId)) {
return "Success";
}

return "§cThis province must be connected to your realm by land or by sea.";
}

/**
* Realm land reachable without crossing sea from the faction capital, or from a vassal's capital,
* since vassals anchor the realm too. A sea crossing is allowed from any province in this blob,
* not only the capitals themselves. Empty while the faction capital is not seated.
* The realm's main region: land joined by land to the faction capital, through its own provinces
* or its vassals'. Vassal land counts only where it connects to the capital this way. A sea
* route may start from any province in this blob and from nowhere else. Empty while the
* faction capital is not seated.
*/
private Set<Integer> capitalLandBlob(ProvinceManager pm) {
return capitalLandBlob(pm, capital);
Expand All @@ -774,46 +678,35 @@ private Set<Integer> capitalLandBlob(ProvinceManager pm, int factionCapital) {
return blob;
}
floodLand(pm, factionCapital, blob);
for (int seat : subjectSeats()) {
if (!blob.contains(seat)) {
floodLand(pm, seat, blob);
}
}
return blob;
}

/** The capitals this faction's vassals and their vassals hold. */
private List<Integer> subjectSeats() {
List<Integer> seats = new ArrayList<>();
for (Faction subject : allSubjects()) {
int seat = subject.getCapital();
if (seat != -1 && subject.getProvinces().contains(seat)) {
seats.add(seat);
}
}
return seats;
}

/** Shores reachable through SEA/WATER, without crossing any intervening land. */
private Set<Integer> seaReachableCoasts(ProvinceManager pm, Set<Integer> capitalBlob) {
Set<Integer> coasts = new HashSet<>();
Set<Integer> visited = new HashSet<>();
/**
* What a sea route from {@code from} reaches: every SEA or WATER province joined to one those
* provinces border, without crossing land, and every province on the shore of one of them.
*/
private Set<Integer> seaReachableCoasts(ProvinceManager pm, Set<Integer> from) {
Set<Integer> reached = new HashSet<>();
ArrayDeque<Integer> queue = new ArrayDeque<>();
for (int id : capitalBlob) {
for (int id : from) {
Province province = pm.get(id);
if (province == null) continue;
for (int neighbour : province.getNeighbours()) {
Province water = pm.get(neighbour);
if (water != null && water.isSea() && visited.add(neighbour)) queue.add(neighbour);
if (water != null && water.isSea() && reached.add(neighbour)) queue.add(neighbour);
}
}
Set<Integer> coasts = new HashSet<>(reached);
while (!queue.isEmpty()) {
Province water = pm.get(queue.poll());
for (int neighbour : water.getNeighbours()) {
Province next = pm.get(neighbour);
if (next == null) continue;
if (!next.isSea()) coasts.add(neighbour);
else if (visited.add(neighbour)) queue.add(neighbour);
else if (reached.add(neighbour)) {
coasts.add(neighbour);
queue.add(neighbour);
}
}
}
return coasts;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -366,11 +366,11 @@ void releasePreviewCountsDisconnectedLandLossAndMatchesDissolve() {

@Test
void releasePreviewKeepsTheLaterGuildCapitalReachableThroughOrdinarySeaClaims() {
// Realm capital 1. Colony 4 lies two sea tiles away; colony 6 two more sea tiles beyond 4,
// on a separate sea. Ordinary sea claims now keep 4 legal even after its guild leaves.
// Realm capital 1. Colony 4 lies two sea tiles away; colony 6 two more sea tiles beyond 4, on
// water the capital's sea also reaches. Ordinary sea claims keep both legal after a guild leaves.
for (int id : List.of(1, 4, 6)) province(id, "PLAINS");
for (int id : List.of(2, 3, 5, 7)) province(id, "SEA");
link(1, 2); link(2, 3); link(3, 4); link(4, 5); link(5, 7); link(7, 6);
link(1, 2); link(2, 3); link(3, 4); link(4, 5); link(3, 5); link(5, 7); link(7, 6);
faction.getProvinceHandler().getProvinces().addAll(List.of(1, 4, 6));
faction.setCapital(1, true, false);
fixture.guild(faction, "Anchor", "One");
Expand Down
Loading