Repository navigation
Add per-province infrastructure that raises effective terrain #104
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
Merged
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
src/main/java/net/tfminecraft/simplefactions/map/infra/EffectiveTerrain.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| package net.tfminecraft.simplefactions.map.infra; | ||
|
|
||
| public final class EffectiveTerrain { | ||
| private EffectiveTerrain() {} | ||
|
|
||
| public static double calculate(double terrain, double infrastructure, double full, double target, double access) { | ||
| if (terrain >= target || infrastructure <= 0 || full <= 0) return terrain; | ||
| double fill = Math.min(1, infrastructure / full); | ||
| return terrain + (target - terrain) * fill * access; | ||
| } | ||
| } |
71 changes: 71 additions & 0 deletions
71
src/main/java/net/tfminecraft/simplefactions/map/infra/InfrastructureAccess.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| package net.tfminecraft.simplefactions.map.infra; | ||
|
|
||
| import java.util.HashSet; | ||
| import java.util.Set; | ||
|
|
||
| import net.tfminecraft.simplefactions.enums.FactionModifiers; | ||
| import net.tfminecraft.simplefactions.enums.Region; | ||
| import net.tfminecraft.simplefactions.enums.Scope; | ||
| import net.tfminecraft.simplefactions.guild.income.IncomePreviewContext; | ||
| import net.tfminecraft.simplefactions.laws.Law; | ||
| import net.tfminecraft.simplefactions.laws.LawEffect; | ||
| import net.tfminecraft.simplefactions.laws.LawGroup; | ||
| import net.tfminecraft.simplefactions.objects.Faction; | ||
|
|
||
| public final class InfrastructureAccess { | ||
| private InfrastructureAccess() {} | ||
|
|
||
| public static double forGuild(Faction guildFaction, Faction owner) { | ||
| if (owner == null) return 1; | ||
| if (guildFaction != null && sameFaction(topRealm(guildFaction), topRealm(owner))) return 1; | ||
| double grant = amount(economyLaw(owner), Scope.FOREIGN_GUILDS, Region.OUR_TERRITORY); | ||
| double reach = amount(economyLaw(guildFaction), Scope.DOMESTIC_GUILDS, Region.FOREIGN_TERRITORY); | ||
| return Math.max(0, Math.min(1, grant + reach)); | ||
| } | ||
|
|
||
| public static Faction topRealm(Faction faction) { | ||
| Set<Faction> seen = new HashSet<>(); | ||
| Faction current = faction; | ||
| while (current != null) { | ||
| // Ignore a cyclic overlord chain rather than granting access through it. | ||
| if (!seen.add(current)) return faction; | ||
| Faction overlord = current.getOverlord(); | ||
| if (overlord == null) return current; | ||
| current = overlord; | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| private static boolean sameFaction(Faction first, Faction second) { | ||
| return first == second || (first != null && second != null && first.getId() != null | ||
| && first.getId().equalsIgnoreCase(second.getId())); | ||
| } | ||
|
|
||
| private static Law economyLaw(Faction faction) { | ||
| if (faction == null || faction.getLawHandler() == null) return null; | ||
| LawGroup group = faction.getLawHandler().getGroup("economy"); | ||
| if (group == null) return null; | ||
| IncomePreviewContext context = IncomePreviewContext.current(); | ||
| Law overlay = context != null && context.previewsLaw(faction) | ||
| ? IncomePreviewContext.overlayLaw(group) : null; | ||
| return overlay != null ? overlay : group.getCurrent(); | ||
| } | ||
|
|
||
| public static double amount(Law law, Scope scope, Region region) { | ||
| if (law == null) return 0; | ||
| LawEffect effect = law.getScopedEffects().get(scope); | ||
| Double explicit = effect == null ? null : effect.getModifierAmount(FactionModifiers.INFRASTRUCTURE_ACCESS, region); | ||
| if (explicit != null) return explicit; | ||
| boolean grant = scope == Scope.FOREIGN_GUILDS && region == Region.OUR_TERRITORY; | ||
| boolean reach = scope == Scope.DOMESTIC_GUILDS && region == Region.FOREIGN_TERRITORY; | ||
| if (!grant && !reach) return 0; | ||
| return switch (law.getId()) { | ||
| case "free_trade" -> grant ? 0.80 : 0.20; | ||
| case "mercantilism" -> grant ? 0.50 : 0.20; | ||
| case "decentralized" -> grant ? 0.50 : 0.00; | ||
| case "protectionism" -> grant ? 0.20 : -0.10; | ||
| case "isolationism" -> grant ? 0.00 : -0.30; | ||
| default -> 0; | ||
| }; | ||
| } | ||
| } |
45 changes: 45 additions & 0 deletions
45
src/main/java/net/tfminecraft/simplefactions/map/infra/InfrastructureSources.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| package net.tfminecraft.simplefactions.map.infra; | ||
|
|
||
| import java.util.Collection; | ||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
|
|
||
| import net.tfminecraft.simplefactions.Cache; | ||
| import net.tfminecraft.simplefactions.enums.GuildModifier; | ||
| import net.tfminecraft.simplefactions.guild.Guild; | ||
| import net.tfminecraft.simplefactions.guild.GuildModifierOverride; | ||
| import net.tfminecraft.simplefactions.installation.Installation; | ||
| import net.tfminecraft.simplefactions.map.provinces.Province; | ||
| import net.tfminecraft.simplefactions.objects.Faction; | ||
|
|
||
| public final class InfrastructureSources { | ||
| private InfrastructureSources() {} | ||
|
|
||
| public static Map<Integer, Double> collect( | ||
| Map<Integer, Province> provinces, Collection<Guild> guilds, Collection<Faction> factions) { | ||
| Map<Integer, Double> sources = new HashMap<>(); | ||
| for (Guild guild : guilds) { | ||
| if (guild == null || !guild.isBase() || !guild.hasCapital()) continue; | ||
| add(provinces, sources, guild.getCapital(), GuildModifierOverride.resolve(guild, GuildModifier.INFRASTRUCTURE)); | ||
| } | ||
| for (Faction faction : factions) { | ||
| if (faction == null || faction.getInstallationHandler() == null) continue; | ||
| for (Installation installation : faction.getInstallationHandler().getAll()) { | ||
| double amount = switch (installation.getKind()) { | ||
| case TRAIN_STATION -> Cache.infrastructureStation; | ||
| case PORT -> Cache.infrastructurePort; | ||
| case AIRPORT -> Cache.infrastructureAirport; | ||
| default -> 0; | ||
| }; | ||
| add(provinces, sources, installation.getProvince(), amount); | ||
| } | ||
| } | ||
| return sources; | ||
| } | ||
|
|
||
| private static void add(Map<Integer, Province> provinces, Map<Integer, Double> sources, int id, double amount) { | ||
| Province province = provinces.get(id); | ||
| if (province == null || province.isSea() || amount <= 0) return; | ||
| sources.merge(id, amount, Double::sum); | ||
| } | ||
| } |
49 changes: 49 additions & 0 deletions
49
src/main/java/net/tfminecraft/simplefactions/map/infra/InfrastructureSpread.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| package net.tfminecraft.simplefactions.map.infra; | ||
|
|
||
| import java.util.Collection; | ||
| import java.util.Comparator; | ||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
| import java.util.Objects; | ||
| import java.util.PriorityQueue; | ||
|
|
||
| public final class InfrastructureSpread { | ||
| private InfrastructureSpread() {} | ||
|
|
||
| public record Node(double terrain, boolean land, String realm, Collection<Integer> neighbours) {} | ||
| public record Arrival(double amount, String origin) {} | ||
| private record Step(int province, Arrival arrival) {} | ||
|
|
||
| public static Map<Integer, Arrival> spread( | ||
| Map<Integer, Node> provinces, Map<Integer, Double> sources, double wilderness, double floor) { | ||
| Map<Integer, Arrival> best = new HashMap<>(); | ||
| PriorityQueue<Step> pending = new PriorityQueue<>( | ||
| Comparator.comparingDouble((Step step) -> step.arrival().amount()).reversed() | ||
| .thenComparingInt(Step::province)); | ||
| for (Map.Entry<Integer, Double> source : sources.entrySet()) { | ||
| Node province = provinces.get(source.getKey()); | ||
| if (province == null || !province.land() || source.getValue() <= 0) continue; | ||
| Arrival arrival = new Arrival(source.getValue(), province.realm()); | ||
| best.put(source.getKey(), arrival); | ||
| pending.add(new Step(source.getKey(), arrival)); | ||
| } | ||
| while (!pending.isEmpty()) { | ||
| Step step = pending.remove(); | ||
| Arrival here = step.arrival(); | ||
| if (!here.equals(best.get(step.province())) || here.amount() < floor) continue; | ||
| for (Integer neighbour : provinces.get(step.province()).neighbours()) { | ||
| Node entered = provinces.get(neighbour); | ||
| if (entered == null || !entered.land()) continue; | ||
| if (entered.realm() != null && !Objects.equals(entered.realm(), here.origin())) continue; | ||
| double amount = here.amount() * entered.terrain(); | ||
| if (entered.realm() == null) amount *= wilderness; | ||
| Arrival previous = best.get(neighbour); | ||
| if (amount < floor || (previous != null && amount <= previous.amount())) continue; | ||
| Arrival arrival = new Arrival(amount, here.origin()); | ||
| best.put(neighbour, arrival); | ||
| pending.add(new Step(neighbour, arrival)); | ||
| } | ||
| } | ||
| return best; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.