Repository navigation
Rebalance rail trade and fix accepted trade agreements #134
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
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
246 changes: 215 additions & 31 deletions
246
src/main/java/net/tfminecraft/simplefactions/guild/hub/Highway.java
Large diffs are not rendered by default.
Oops, something went wrong.
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
69 changes: 69 additions & 0 deletions
69
src/main/java/net/tfminecraft/simplefactions/guild/hub/OpenTrackSettings.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,69 @@ | ||
| package net.tfminecraft.simplefactions.guild.hub; | ||
|
|
||
| import org.bukkit.configuration.ConfigurationSection; | ||
| import org.bukkit.configuration.file.FileConfiguration; | ||
|
|
||
| /** How far a lone train station pushes trade along track that has no second station. */ | ||
| public final class OpenTrackSettings { | ||
| public static final double DEFAULT_SHARE = 0.75; | ||
| public static final double DEFAULT_KEPT = 0.85; | ||
| public static final double DEFAULT_RANGE = 2500; | ||
|
|
||
| private static volatile boolean enabled = true; | ||
| private static volatile double share = DEFAULT_SHARE; | ||
| private static volatile double keptPer1000 = DEFAULT_KEPT; | ||
| private static volatile double rangeBlocks = DEFAULT_RANGE; | ||
|
|
||
| private OpenTrackSettings() { | ||
| } | ||
|
|
||
| public static void load(FileConfiguration config) { | ||
| ConfigurationSection open = config == null | ||
| ? null : config.getConfigurationSection("installation-trade.open-track"); | ||
| enabled = open == null || open.getBoolean("enabled", true); | ||
| share = nonNegative(open == null ? DEFAULT_SHARE : open.getDouble("share", DEFAULT_SHARE), DEFAULT_SHARE); | ||
| keptPer1000 = kept(open == null | ||
| ? DEFAULT_KEPT : open.getDouble("kept-per-1000-blocks", DEFAULT_KEPT)); | ||
| double range = open == null ? DEFAULT_RANGE : open.getDouble("range-blocks", DEFAULT_RANGE); | ||
| rangeBlocks = Double.isFinite(range) && range > 0 ? range : 0; | ||
| } | ||
|
|
||
| public static void reset() { | ||
| enabled = true; | ||
| share = DEFAULT_SHARE; | ||
| keptPer1000 = DEFAULT_KEPT; | ||
| rangeBlocks = DEFAULT_RANGE; | ||
| } | ||
|
|
||
| public static boolean enabled() { | ||
| return enabled; | ||
| } | ||
|
|
||
| /** Multiplied by the rail trade share. The product is capped at {@link HubTransport#MAX_SHARE}. */ | ||
| public static double share() { | ||
| return share; | ||
| } | ||
|
|
||
| public static double keptPer1000() { | ||
| return keptPer1000; | ||
| } | ||
|
|
||
| public static double rangeBlocks() { | ||
| return rangeBlocks; | ||
| } | ||
|
|
||
| private static double nonNegative(double value, double fallback) { | ||
| if (!Double.isFinite(value) || value < 0) { | ||
| return fallback; | ||
| } | ||
| return value; | ||
| } | ||
|
|
||
| /** Same cap as a line, so distance loss cannot be turned off by config. */ | ||
| private static double kept(double value) { | ||
| if (!Double.isFinite(value) || value < 0) { | ||
| return DEFAULT_KEPT; | ||
| } | ||
| return Math.min(value, HubTransport.MAX_SHARE); | ||
| } | ||
| } |
62 changes: 62 additions & 0 deletions
62
src/main/java/net/tfminecraft/simplefactions/guild/hub/TrackReach.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,62 @@ | ||
| package net.tfminecraft.simplefactions.guild.hub; | ||
|
|
||
| import java.util.Arrays; | ||
| import java.util.Comparator; | ||
| import java.util.List; | ||
| import java.util.PriorityQueue; | ||
|
|
||
| /** Shortest distances along an undirected track graph, limited to a range. */ | ||
| public final class TrackReach { | ||
| public interface At { | ||
| int province(double x, double z); | ||
| } | ||
|
|
||
| record Edge(int to, double weight) { | ||
| } | ||
|
|
||
| private record Step(int node, double distance) { | ||
| } | ||
|
|
||
| private TrackReach() { | ||
| } | ||
|
|
||
| static void link(List<List<Edge>> adjacent, int from, int to, double weight) { | ||
| if (from == to || !Double.isFinite(weight) || weight < 0) { | ||
| return; | ||
| } | ||
| adjacent.get(from).add(new Edge(to, weight)); | ||
| adjacent.get(to).add(new Edge(from, weight)); | ||
| } | ||
|
|
||
| /** Unreached nodes stay at positive infinity. Nodes past {@code range} are not expanded. */ | ||
| static double[] travel(List<List<Edge>> adjacent, boolean[] source, double range) { | ||
| int count = adjacent.size(); | ||
| double[] distance = new double[count]; | ||
| Arrays.fill(distance, Double.POSITIVE_INFINITY); | ||
| if (!(range >= 0)) { | ||
| return distance; | ||
| } | ||
| PriorityQueue<Step> queue = new PriorityQueue<>(Comparator.comparingDouble(Step::distance)); | ||
| for (int index = 0; index < count && index < source.length; index++) { | ||
| if (!source[index]) { | ||
| continue; | ||
| } | ||
| distance[index] = 0; | ||
| queue.add(new Step(index, 0)); | ||
| } | ||
| while (!queue.isEmpty()) { | ||
| Step step = queue.poll(); | ||
| if (step.distance != distance[step.node] || step.distance > range) { | ||
| continue; | ||
| } | ||
| for (Edge edge : adjacent.get(step.node)) { | ||
| double next = step.distance + edge.weight; | ||
| if (next <= range && next < distance[edge.to]) { | ||
| distance[edge.to] = next; | ||
| queue.add(new Step(edge.to, next)); | ||
| } | ||
| } | ||
| } | ||
| return distance; | ||
| } | ||
| } |
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.