Repository navigation
Build one shared graph of trade nodes and networks #120
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鈥檒l 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
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
14 changes: 2 additions & 12 deletions
14
src/main/java/net/tfminecraft/simplefactions/guild/hub/VehicleFrameworkTrackProvinces.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 |
|---|---|---|
| @@ -1,26 +1,16 @@ | ||
| package net.tfminecraft.simplefactions.guild.hub; | ||
|
|
||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Set; | ||
|
|
||
| import net.tfminecraft.simplefactions.map.ProvinceGrid; | ||
| import net.tfminecraft.simplefactions.map.infra.TrackProvinceLookup; | ||
| import net.tfminecraft.simplefactions.map.infra.TrackProvinceLookup.Point; | ||
| import net.tfminecraft.simplefactions.map.provinces.Province; | ||
| import net.tfminecraft.vehicleframework.VehicleFramework; | ||
| import net.tfminecraft.vehicleframework.tracks.TrackRegistry; | ||
| import net.tfminecraft.vehicleframework.tracks.TrackSamplePoint; | ||
|
|
||
| /** The only track sampler here that names VehicleFramework's sample point type. */ | ||
| /** Track infrastructure sampling uses the same VehicleFramework boundary as hub routes. */ | ||
| public final class VehicleFrameworkTrackProvinces { | ||
| private VehicleFrameworkTrackProvinces() {} | ||
|
|
||
| public static Set<Integer> sample(String world, ProvinceGrid grid, Map<Integer, Province> provinces) { | ||
| TrackRegistry tracks = VehicleFramework.getTrackRegistry(); | ||
| if (tracks == null || world == null) return Set.of(); | ||
| List<TrackSamplePoint> samples = tracks.sampleTrack(world, 8); | ||
| return TrackProvinceLookup.collect(grid, samples.stream() | ||
| .map(point -> new Point(point.x(), point.y(), point.z())).toList(), provinces); | ||
| return VehicleFrameworkTracks.sample(world, grid, provinces); | ||
| } | ||
| } |
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
83 changes: 83 additions & 0 deletions
83
src/main/java/net/tfminecraft/simplefactions/guild/network/LiveTradeGraph.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,83 @@ | ||
| package net.tfminecraft.simplefactions.guild.network; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.HashMap; | ||
| import java.util.IdentityHashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Optional; | ||
|
|
||
| import org.bukkit.Bukkit; | ||
|
|
||
| import net.tfminecraft.simplefactions.Cache; | ||
| import net.tfminecraft.simplefactions.SimpleFactions; | ||
| import net.tfminecraft.simplefactions.guild.hub.VehicleFrameworkTracks; | ||
| import net.tfminecraft.simplefactions.guild.network.RailRoutes.Route; | ||
| import net.tfminecraft.simplefactions.guild.network.TradeGraphBuilder.ProvinceData; | ||
| import net.tfminecraft.simplefactions.guild.network.TradeGraphBuilder.Site; | ||
| import net.tfminecraft.simplefactions.installation.Installation; | ||
| import net.tfminecraft.simplefactions.installation.InstallationKind; | ||
| import net.tfminecraft.simplefactions.loaders.InstallationConfigLoader; | ||
| import net.tfminecraft.simplefactions.managers.FactionManager; | ||
| import net.tfminecraft.simplefactions.managers.ProvinceManager; | ||
| import net.tfminecraft.simplefactions.map.ProvinceGrid; | ||
| import net.tfminecraft.simplefactions.map.provinces.Province; | ||
| import net.tfminecraft.simplefactions.objects.Faction; | ||
|
|
||
| /** Takes the server-thread data needed by the pure builder. */ | ||
| final class LiveTradeGraph { | ||
| private static final RailRouteCache routes = new RailRouteCache(System::currentTimeMillis); | ||
|
|
||
| private LiveTradeGraph() { } | ||
|
|
||
| static TradeGraph build(ProvinceManager provinces) { | ||
| List<Site> sites = new ArrayList<>(); | ||
| Map<Installation, String> identities = new IdentityHashMap<>(); | ||
| if (FactionManager.factions != null) { | ||
| for (Faction faction : FactionManager.factions) { | ||
| if (faction == null || faction.getId() == null || faction.getInstallationHandler() == null) continue; | ||
| // Pending construction is separate from getAll(), as in SupplyHubService.findInstallation. | ||
| for (Installation installation : faction.getInstallationHandler().getAll()) { | ||
| if (installation == null || installation.getKind() == InstallationKind.FORT) continue; | ||
| sites.add(new Site(faction.getId(), installation, InstallationConfigLoader.getHubSlots( | ||
| installation.getKind(), installation.getLevel()), true)); | ||
| identities.put(installation, | ||
| faction.getId() + ":" + installation.getProvince() + ":" + installation.getId()); | ||
| } | ||
| } | ||
| } | ||
| Map<Integer, ProvinceData> map = new HashMap<>(); | ||
| for (Province province : provinces.getProvinces()) { | ||
| if (province.isValid()) { | ||
| map.put(province.getId(), new ProvinceData(province.getTerrain(), province.getNeighbours())); | ||
| } | ||
| } | ||
| SimpleFactions plugin = SimpleFactions.getInstance(); | ||
| ProvinceGrid grid = plugin == null ? null : plugin.getProvinceGrid(); | ||
| String world = Cache.worldName; | ||
| return TradeGraphBuilder.build(sites, map, (from, to) -> { | ||
| String left = identities.get(from); | ||
| String right = identities.get(to); | ||
| if (left == null || right == null) return Optional.empty(); | ||
| return routes.route(left, right, () -> trackRoute(world, from, to)); | ||
| }, point -> grid == null ? 0 : grid.getAt( | ||
| (int) Math.floor(point.x()), (int) Math.floor(point.z()))); | ||
| } | ||
|
|
||
| /** | ||
| * The plugin check sits outside {@link VehicleFrameworkTracks}, matching hub distances. | ||
| * A missing or older plugin must not fail the refresh. | ||
| */ | ||
| private static Optional<Route> trackRoute(String world, Installation from, Installation to) { | ||
| try { | ||
| if (!Bukkit.getPluginManager().isPluginEnabled("VehicleFramework")) return Optional.empty(); | ||
| return VehicleFrameworkTracks.route(world, from, to); | ||
| } catch (RuntimeException | LinkageError e) { | ||
| return Optional.empty(); | ||
| } | ||
| } | ||
|
|
||
| static void forgetRoutes() { | ||
| routes.forget(); | ||
| } | ||
| } |
41 changes: 41 additions & 0 deletions
41
src/main/java/net/tfminecraft/simplefactions/guild/network/RailRouteCache.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,41 @@ | ||
| package net.tfminecraft.simplefactions.guild.network; | ||
|
|
||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
| import java.util.Optional; | ||
| import java.util.function.LongSupplier; | ||
| import java.util.function.Supplier; | ||
|
|
||
| import net.tfminecraft.simplefactions.guild.network.RailRoutes.Route; | ||
|
|
||
| /** Hits last until forgotten; misses are tried again after thirty seconds. */ | ||
| final class RailRouteCache { | ||
| private record Key(String first, String second) { } | ||
| private record Cached(Optional<Route> route, long expiresAt) { } | ||
|
|
||
| private final Map<Key, Cached> routes = new HashMap<>(); | ||
| private final LongSupplier clock; | ||
|
|
||
| RailRouteCache(LongSupplier clock) { | ||
| this.clock = clock; | ||
| } | ||
|
|
||
| Optional<Route> route(String from, String to, Supplier<Optional<Route>> source) { | ||
| if (from == null || to == null) return Optional.empty(); | ||
| boolean reverse = from.compareTo(to) > 0; | ||
| Key key = reverse ? new Key(to, from) : new Key(from, to); | ||
| long now = clock.getAsLong(); | ||
| Cached known = routes.get(key); | ||
| if (known != null && (known.route().isPresent() || known.expiresAt() > now)) { | ||
| return reverse ? known.route().map(Route::reversed) : known.route(); | ||
| } | ||
| Optional<Route> measured = source.get(); | ||
| routes.put(key, new Cached(reverse ? measured.map(Route::reversed) : measured, | ||
| measured.isPresent() ? Long.MAX_VALUE : now + 30_000L)); | ||
| return measured; | ||
| } | ||
|
|
||
| void forget() { | ||
| routes.clear(); | ||
| } | ||
| } |
23 changes: 23 additions & 0 deletions
23
src/main/java/net/tfminecraft/simplefactions/guild/network/RailRoutes.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,23 @@ | ||
| package net.tfminecraft.simplefactions.guild.network; | ||
|
|
||
| import java.util.List; | ||
| import java.util.Optional; | ||
|
|
||
| import net.tfminecraft.simplefactions.installation.Installation; | ||
| import net.tfminecraft.simplefactions.map.infra.TrackProvinceLookup.Point; | ||
|
|
||
| /** Routes contain points in order from the first installation to the second. */ | ||
| @FunctionalInterface | ||
| public interface RailRoutes { | ||
| Optional<Route> route(Installation from, Installation to); | ||
|
|
||
| record Route(double length, List<Point> points) { | ||
| public Route { | ||
| points = List.copyOf(points); | ||
| } | ||
|
|
||
| public Route reversed() { | ||
| return new Route(length, points.reversed()); | ||
| } | ||
| } | ||
| } |
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.