From 59f8f5cd11221ff374cc380355b4e895db87d3e0 Mon Sep 17 00:00:00 2001 From: Anuken Date: Wed, 3 Sep 2025 16:28:50 -0400 Subject: [PATCH] Choosing of Erekir launch sector based on loadout cost --- .../maps/generators/PlanetGenerator.java | 49 +++++++++++++------ .../maps/planet/SerpuloPlanetGenerator.java | 2 +- .../ui/dialogs/LaunchLoadoutDialog.java | 1 - .../mindustry/ui/dialogs/PlanetDialog.java | 5 +- 4 files changed, 39 insertions(+), 18 deletions(-) diff --git a/core/src/mindustry/maps/generators/PlanetGenerator.java b/core/src/mindustry/maps/generators/PlanetGenerator.java index 20b90590f2..e8ab32da68 100644 --- a/core/src/mindustry/maps/generators/PlanetGenerator.java +++ b/core/src/mindustry/maps/generators/PlanetGenerator.java @@ -20,6 +20,8 @@ import mindustry.world.*; import static mindustry.Vars.*; public abstract class PlanetGenerator extends BasicGenerator implements HexMesher{ + protected static final ItemSeq tmpItems = new ItemSeq(); + public int baseSeed = 0; public int seed = 0; @@ -54,23 +56,42 @@ public abstract class PlanetGenerator extends BasicGenerator implements HexMeshe return sector.planet.allowLaunchToNumbered && (sector.hasBase() || sector.near().contains(Sector::hasBase)); } - public Sector findLaunchCandidate(Sector destination, Sector selected){ - Sector launchSector = selected != null && selected.planet == destination.planet && selected.hasBase() ? selected: null; - //directly nearby. - if(destination.near().contains(launchSector)) return launchSector; + public @Nullable Sector findLaunchCandidate(Sector destination, @Nullable Sector selected){ + if(!destination.allowLaunchLoadout() && destination.preset != null){ + tmpItems.clear(); - Sector launchFrom = launchSector; - if(launchFrom == null || (destination.preset == null && !destination.near().contains(launchSector))){ - //TODO pick one with the most resources - launchFrom = destination.near().find(Sector::hasBase); - if(launchFrom == null && destination.preset != null){ - if(launchSector != null) return launchSector; - launchFrom = destination.planet.sectors.min(s -> !s.hasBase() ? Float.MAX_VALUE : s.tile.v.dst2(destination.tile.v)); - if(!launchFrom.hasBase()) launchFrom = null; + var rules = destination.preset.generator.map.rules(); + for(var stack : rules.loadout){ + if(stack.item.isOnPlanet(destination.planet)){ + tmpItems.add(stack.item, stack.amount); + } } - } - return launchFrom; + //currently played (selected) sector has all the resources + if(selected != null && selected.planet == destination.planet && selected.hasBase() && selected.items().has(tmpItems)){ + return selected; + }else{ + //find the closest sector that has resources (ranked by distance, not item quantity) + return destination.planet.sectors.min(s -> s.hasBase() && s.items().has(tmpItems), s -> s.tile.v.dst(destination.tile.v)); + } + }else{ + Sector launchSector = selected != null && selected.planet == destination.planet && selected.hasBase() ? selected : null; + //directly nearby. + if(destination.near().contains(launchSector)) return launchSector; + + Sector launchFrom = launchSector; + if(launchFrom == null || destination.preset == null){ + //TODO pick one with the most resources + launchFrom = destination.near().find(Sector::hasBase); + if(launchFrom == null && destination.preset != null){ + if(launchSector != null) return launchSector; + launchFrom = destination.planet.sectors.min(s -> !s.hasBase() ? Float.MAX_VALUE : s.tile.v.dst2(destination.tile.v)); + if(!launchFrom.hasBase()) launchFrom = null; + } + } + + return launchFrom; + } } /** @return whether to allow landing on the specified procedural sector */ diff --git a/core/src/mindustry/maps/planet/SerpuloPlanetGenerator.java b/core/src/mindustry/maps/planet/SerpuloPlanetGenerator.java index f037c56768..1e760976eb 100644 --- a/core/src/mindustry/maps/planet/SerpuloPlanetGenerator.java +++ b/core/src/mindustry/maps/planet/SerpuloPlanetGenerator.java @@ -97,7 +97,7 @@ public class SerpuloPlanetGenerator extends PlanetGenerator{ } @Override - public Sector findLaunchCandidate(Sector destination, Sector selected){ + public @Nullable Sector findLaunchCandidate(Sector destination, @Nullable Sector selected){ if(destination.preset == null || !destination.preset.requireUnlock){ if(selected != null && selected.isNear(destination) && allowNumberedLaunch(selected)){ return selected; diff --git a/core/src/mindustry/ui/dialogs/LaunchLoadoutDialog.java b/core/src/mindustry/ui/dialogs/LaunchLoadoutDialog.java index 082b8c134e..2124ceed09 100644 --- a/core/src/mindustry/ui/dialogs/LaunchLoadoutDialog.java +++ b/core/src/mindustry/ui/dialogs/LaunchLoadoutDialog.java @@ -68,7 +68,6 @@ public class LaunchLoadoutDialog extends BaseDialog{ if(!destination.allowLaunchLoadout()){ resources.clear(); - //TODO this should be set to a proper loadout based on sector. if(destination.preset != null){ var rules = destination.preset.generator.map.rules(); for(var stack : rules.loadout){ diff --git a/core/src/mindustry/ui/dialogs/PlanetDialog.java b/core/src/mindustry/ui/dialogs/PlanetDialog.java index d0fb0ac21d..3dc0c63bc3 100644 --- a/core/src/mindustry/ui/dialogs/PlanetDialog.java +++ b/core/src/mindustry/ui/dialogs/PlanetDialog.java @@ -430,10 +430,11 @@ public class PlanetDialog extends BaseDialog implements PlanetInterfaceRenderer{ return mode == planetLaunch ? sector.planet.generator.allowAcceleratorLanding(sector) : sector.planet.generator.allowLanding(sector); } - Sector findLauncher(Sector to){ + @Nullable Sector findLauncher(Sector to){ if(mode == planetLaunch || to.planet.generator == null) return launchSector; - return to.planet.generator.findLaunchCandidate(to, launchSector); + Sector candidate = to.planet.generator.findLaunchCandidate(to, launchSector); + return candidate == null ? launchSector : candidate; } boolean showing(){