From 95dbd64ecb30afd65b51be8e13d22e48cf50c9d7 Mon Sep 17 00:00:00 2001 From: Octol1ttle Date: Fri, 14 Jun 2024 22:05:58 +0500 Subject: [PATCH] feat: finish custom thrust handlers, begin work on autopilot v4 Signed-off-by: Octol1ttle --- .../flightassistant/FAKeyBindings.java | 6 +- .../impl/autoflight/AutoFireworkOffAlert.java | 4 +- .../impl/autoflight/AutopilotOffAlert.java | 6 +- .../commands/SelectCommand.java | 14 +-- .../doabarrelroll/DaBRThrustHandler.java | 12 ++- .../computers/ComputerHost.java | 17 ++-- .../computers/api/IThrustHandler.java | 3 +- .../computers/api/ThrustControlInput.java | 2 +- .../computers/impl/FlightPhaseComputer.java | 39 ++------ ...omputer.java => AutoFlightController.java} | 25 +----- ...olComputer.java => AutopilotComputer.java} | 89 +++++++++++++++---- .../impl/autoflight/FireworkController.java | 12 ++- .../impl/autoflight/ThrustController.java | 8 +- .../hud/impl/AltitudeDisplay.java | 4 +- .../hud/impl/FlightDirectorsDisplay.java | 16 ++-- .../hud/impl/FlightModeDisplay.java | 4 +- .../hud/impl/HeadingDisplay.java | 4 +- .../registries/ComputerRegistry.java | 7 +- .../assets/flightassistant/lang/en_us.yml | 10 ++- .../assets/flightassistant/lang/ru_ru.yml | 6 +- 20 files changed, 167 insertions(+), 121 deletions(-) rename src/main/java/ru/octol1ttle/flightassistant/computers/impl/autoflight/{AutoFlightComputer.java => AutoFlightController.java} (64%) rename src/main/java/ru/octol1ttle/flightassistant/computers/impl/autoflight/{AutopilotControlComputer.java => AutopilotComputer.java} (61%) diff --git a/src/main/java/ru/octol1ttle/flightassistant/FAKeyBindings.java b/src/main/java/ru/octol1ttle/flightassistant/FAKeyBindings.java index c6b23c4f..78337e0a 100644 --- a/src/main/java/ru/octol1ttle/flightassistant/FAKeyBindings.java +++ b/src/main/java/ru/octol1ttle/flightassistant/FAKeyBindings.java @@ -5,7 +5,7 @@ import net.minecraft.client.option.KeyBinding; import net.minecraft.client.util.InputUtil; import org.lwjgl.glfw.GLFW; -import ru.octol1ttle.flightassistant.computers.impl.autoflight.AutoFlightComputer; +import ru.octol1ttle.flightassistant.computers.impl.autoflight.AutoFlightController; import ru.octol1ttle.flightassistant.computers.impl.autoflight.FireworkController; import ru.octol1ttle.flightassistant.computers.impl.safety.AlertController; import ru.octol1ttle.flightassistant.registries.ComputerRegistry; @@ -46,8 +46,8 @@ public static void setup() { KeyBindingHelper.registerKeyBinding(lockManualFireworks); ClientTickEvents.END_CLIENT_TICK.register(client -> { - if (!ComputerRegistry.isFaulted(AutoFlightComputer.class)) { - AutoFlightComputer autoflight = ComputerRegistry.resolve(AutoFlightComputer.class); + if (!ComputerRegistry.isFaulted(AutoFlightController.class)) { + AutoFlightController autoflight = ComputerRegistry.resolve(AutoFlightController.class); while (toggleFlightDirectors.wasPressed()) { autoflight.flightDirectorsEnabled = !autoflight.flightDirectorsEnabled; } diff --git a/src/main/java/ru/octol1ttle/flightassistant/alerts/impl/autoflight/AutoFireworkOffAlert.java b/src/main/java/ru/octol1ttle/flightassistant/alerts/impl/autoflight/AutoFireworkOffAlert.java index 9e46872a..b8703763 100644 --- a/src/main/java/ru/octol1ttle/flightassistant/alerts/impl/autoflight/AutoFireworkOffAlert.java +++ b/src/main/java/ru/octol1ttle/flightassistant/alerts/impl/autoflight/AutoFireworkOffAlert.java @@ -8,12 +8,12 @@ import ru.octol1ttle.flightassistant.alerts.api.BaseAlert; import ru.octol1ttle.flightassistant.alerts.api.IECAMAlert; import ru.octol1ttle.flightassistant.alerts.impl.AlertSoundData; -import ru.octol1ttle.flightassistant.computers.impl.autoflight.AutoFlightComputer; +import ru.octol1ttle.flightassistant.computers.impl.autoflight.AutoFlightController; import ru.octol1ttle.flightassistant.config.FAConfig; import ru.octol1ttle.flightassistant.registries.ComputerRegistry; public class AutoFireworkOffAlert extends BaseAlert implements IECAMAlert { - private final AutoFlightComputer autoflight = ComputerRegistry.resolve(AutoFlightComputer.class); + private final AutoFlightController autoflight = ComputerRegistry.resolve(AutoFlightController.class); public AutoFireworkOffAlert() { this.hidden = true; diff --git a/src/main/java/ru/octol1ttle/flightassistant/alerts/impl/autoflight/AutopilotOffAlert.java b/src/main/java/ru/octol1ttle/flightassistant/alerts/impl/autoflight/AutopilotOffAlert.java index fc9f486e..44f88d8f 100644 --- a/src/main/java/ru/octol1ttle/flightassistant/alerts/impl/autoflight/AutopilotOffAlert.java +++ b/src/main/java/ru/octol1ttle/flightassistant/alerts/impl/autoflight/AutopilotOffAlert.java @@ -5,15 +5,15 @@ import net.minecraft.text.Text; import org.jetbrains.annotations.NotNull; import ru.octol1ttle.flightassistant.DrawHelper; -import ru.octol1ttle.flightassistant.alerts.impl.AlertSoundData; import ru.octol1ttle.flightassistant.alerts.api.BaseAlert; import ru.octol1ttle.flightassistant.alerts.api.IECAMAlert; -import ru.octol1ttle.flightassistant.computers.impl.autoflight.AutoFlightComputer; +import ru.octol1ttle.flightassistant.alerts.impl.AlertSoundData; +import ru.octol1ttle.flightassistant.computers.impl.autoflight.AutoFlightController; import ru.octol1ttle.flightassistant.config.FAConfig; import ru.octol1ttle.flightassistant.registries.ComputerRegistry; public class AutopilotOffAlert extends BaseAlert implements IECAMAlert { - private final AutoFlightComputer autoflight = ComputerRegistry.resolve(AutoFlightComputer.class); + private final AutoFlightController autoflight = ComputerRegistry.resolve(AutoFlightController.class); public AutopilotOffAlert() { this.hidden = true; diff --git a/src/main/java/ru/octol1ttle/flightassistant/commands/SelectCommand.java b/src/main/java/ru/octol1ttle/flightassistant/commands/SelectCommand.java index 43bfeba1..fff95290 100644 --- a/src/main/java/ru/octol1ttle/flightassistant/commands/SelectCommand.java +++ b/src/main/java/ru/octol1ttle/flightassistant/commands/SelectCommand.java @@ -3,7 +3,7 @@ import com.mojang.brigadier.arguments.IntegerArgumentType; import com.mojang.brigadier.builder.LiteralArgumentBuilder; import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource; -import ru.octol1ttle.flightassistant.computers.impl.autoflight.AutoFlightComputer; +import ru.octol1ttle.flightassistant.computers.impl.autoflight.AutoFlightController; import ru.octol1ttle.flightassistant.registries.ComputerRegistry; import static net.fabricmc.fabric.api.client.command.v2.ClientCommandManager.argument; @@ -22,13 +22,13 @@ private static void registerSpeed(LiteralArgumentBuilder { - ComputerRegistry.resolve(AutoFlightComputer.class).selectedSpeed = null; + ComputerRegistry.resolve(AutoFlightController.class).selectedSpeed = null; return 0; }) ) .then(argument("target", IntegerArgumentType.integer(0, 30)) .executes(context -> { - ComputerRegistry.resolve(AutoFlightComputer.class).selectedSpeed = IntegerArgumentType.getInteger(context, "target"); + ComputerRegistry.resolve(AutoFlightController.class).selectedSpeed = IntegerArgumentType.getInteger(context, "target"); return 0; }) ) @@ -39,13 +39,13 @@ private static void registerAltitude(LiteralArgumentBuilder { - ComputerRegistry.resolve(AutoFlightComputer.class).selectedAltitude = null; + ComputerRegistry.resolve(AutoFlightController.class).selectedAltitude = null; return 0; }) ) .then(argument("target", IntegerArgumentType.integer(-120, 1200)) .executes(context -> { - ComputerRegistry.resolve(AutoFlightComputer.class).selectedAltitude = IntegerArgumentType.getInteger(context, "target"); + ComputerRegistry.resolve(AutoFlightController.class).selectedAltitude = IntegerArgumentType.getInteger(context, "target"); return 0; }) ) @@ -56,13 +56,13 @@ private static void registerHeading(LiteralArgumentBuilder { - ComputerRegistry.resolve(AutoFlightComputer.class).selectedHeading = null; + ComputerRegistry.resolve(AutoFlightController.class).selectedHeading = null; return 0; }) ) .then(argument("target", IntegerArgumentType.integer(0, 360)) .executes(context -> { - ComputerRegistry.resolve(AutoFlightComputer.class).selectedHeading = IntegerArgumentType.getInteger(context, "target"); + ComputerRegistry.resolve(AutoFlightController.class).selectedHeading = IntegerArgumentType.getInteger(context, "target"); return 0; }) ) diff --git a/src/main/java/ru/octol1ttle/flightassistant/compatibility/doabarrelroll/DaBRThrustHandler.java b/src/main/java/ru/octol1ttle/flightassistant/compatibility/doabarrelroll/DaBRThrustHandler.java index 921654ad..9d111c41 100644 --- a/src/main/java/ru/octol1ttle/flightassistant/compatibility/doabarrelroll/DaBRThrustHandler.java +++ b/src/main/java/ru/octol1ttle/flightassistant/compatibility/doabarrelroll/DaBRThrustHandler.java @@ -1,14 +1,15 @@ package ru.octol1ttle.flightassistant.compatibility.doabarrelroll; import nl.enjarai.doabarrelroll.api.event.ThrustEvents; +import nl.enjarai.doabarrelroll.config.ModConfig; import ru.octol1ttle.flightassistant.computers.api.IThrustHandler; import ru.octol1ttle.flightassistant.computers.impl.TimeComputer; -import ru.octol1ttle.flightassistant.computers.impl.autoflight.AutoFlightComputer; +import ru.octol1ttle.flightassistant.computers.impl.autoflight.AutoFlightController; import ru.octol1ttle.flightassistant.computers.impl.autoflight.ThrustController; import ru.octol1ttle.flightassistant.registries.ComputerRegistry; public class DaBRThrustHandler implements IThrustHandler { - private final AutoFlightComputer autoflight = ComputerRegistry.resolve(AutoFlightComputer.class); + private final AutoFlightController autoflight = ComputerRegistry.resolve(AutoFlightController.class); private final ThrustController thrust = ComputerRegistry.resolve(ThrustController.class); private final TimeComputer time = ComputerRegistry.resolve(TimeComputer.class); @@ -18,7 +19,12 @@ public DaBRThrustHandler() { autoflight.disconnectAutoFirework(true); } return thrust.targetThrust += (float) (v * time.deltaTime * 0.5f); - }); + }, 10); + } + + @Override + public boolean canBeUsed() { + return ModConfig.INSTANCE.getEnableThrust(); } @Override diff --git a/src/main/java/ru/octol1ttle/flightassistant/computers/ComputerHost.java b/src/main/java/ru/octol1ttle/flightassistant/computers/ComputerHost.java index 0ca389c7..e4ae943d 100644 --- a/src/main/java/ru/octol1ttle/flightassistant/computers/ComputerHost.java +++ b/src/main/java/ru/octol1ttle/flightassistant/computers/ComputerHost.java @@ -8,8 +8,8 @@ import ru.octol1ttle.flightassistant.computers.impl.AirDataComputer; import ru.octol1ttle.flightassistant.computers.impl.FlightPhaseComputer; import ru.octol1ttle.flightassistant.computers.impl.TimeComputer; -import ru.octol1ttle.flightassistant.computers.impl.autoflight.AutoFlightComputer; -import ru.octol1ttle.flightassistant.computers.impl.autoflight.AutopilotControlComputer; +import ru.octol1ttle.flightassistant.computers.impl.autoflight.AutoFlightController; +import ru.octol1ttle.flightassistant.computers.impl.autoflight.AutopilotComputer; import ru.octol1ttle.flightassistant.computers.impl.autoflight.FireworkController; import ru.octol1ttle.flightassistant.computers.impl.autoflight.HeadingController; import ru.octol1ttle.flightassistant.computers.impl.autoflight.PitchController; @@ -35,22 +35,25 @@ public static ComputerHost instance() { public ComputerHost(@NotNull MinecraftClient mc) { ComputerRegistry.register(new AirDataComputer(mc)); ComputerRegistry.register(new TimeComputer(mc)); + ComputerRegistry.register(new FireworkController(mc)); ComputerRegistry.register(new PitchLimitComputer()); ComputerRegistry.register(new FlightProtectionsComputer()); - ComputerRegistry.register(new PitchController()); ComputerRegistry.register(new ChunkStatusComputer()); ComputerRegistry.register(new StallComputer()); ComputerRegistry.register(new VoidLevelComputer()); ComputerRegistry.register(new FlightPlanner()); ComputerRegistry.register(new GroundProximityComputer()); ComputerRegistry.register(new ElytraStateController()); - ComputerRegistry.register(new HeadingController()); - ComputerRegistry.register(new RollController()); - ComputerRegistry.register(new AutoFlightComputer()); + + ComputerRegistry.register(new AutoFlightController()); ComputerRegistry.register(new FlightPhaseComputer()); - ComputerRegistry.register(new AutopilotControlComputer()); ComputerRegistry.register(new ThrustController()); + ComputerRegistry.register(new AutopilotComputer()); + ComputerRegistry.register(new PitchController()); + ComputerRegistry.register(new HeadingController()); + ComputerRegistry.register(new RollController()); + ComputerRegistry.register(new AlertController(mc.getSoundManager())); CustomComputerRegistrationCallback.EVENT.invoker().registerCustomComputers(); diff --git a/src/main/java/ru/octol1ttle/flightassistant/computers/api/IThrustHandler.java b/src/main/java/ru/octol1ttle/flightassistant/computers/api/IThrustHandler.java index ffc34a18..64b6a071 100644 --- a/src/main/java/ru/octol1ttle/flightassistant/computers/api/IThrustHandler.java +++ b/src/main/java/ru/octol1ttle/flightassistant/computers/api/IThrustHandler.java @@ -5,7 +5,8 @@ /** * Implementing classes should inject ThrustController and use {@link ThrustController#currentThrust} and {@link ThrustController#targetThrust} themselves as needed. * Implementing this interface is required to resolve any conflicts between multiple thrust handlers. - * In case of multiple thrust handlers being present, only the first one registered + * In case of multiple thrust handlers being present, only the first one is registered */ public interface IThrustHandler extends IComputer { + boolean canBeUsed(); } diff --git a/src/main/java/ru/octol1ttle/flightassistant/computers/api/ThrustControlInput.java b/src/main/java/ru/octol1ttle/flightassistant/computers/api/ThrustControlInput.java index 0dcb90b4..d3272cb3 100644 --- a/src/main/java/ru/octol1ttle/flightassistant/computers/api/ThrustControlInput.java +++ b/src/main/java/ru/octol1ttle/flightassistant/computers/api/ThrustControlInput.java @@ -1,4 +1,4 @@ package ru.octol1ttle.flightassistant.computers.api; -public record ThrustControlInput(float target, float deltaTimeMultiplier, InputPriority priority) { +public record ThrustControlInput(float target, InputPriority priority) { } diff --git a/src/main/java/ru/octol1ttle/flightassistant/computers/impl/FlightPhaseComputer.java b/src/main/java/ru/octol1ttle/flightassistant/computers/impl/FlightPhaseComputer.java index 1d436979..8f244290 100644 --- a/src/main/java/ru/octol1ttle/flightassistant/computers/impl/FlightPhaseComputer.java +++ b/src/main/java/ru/octol1ttle/flightassistant/computers/impl/FlightPhaseComputer.java @@ -1,19 +1,16 @@ package ru.octol1ttle.flightassistant.computers.impl; import net.minecraft.text.Text; -import net.minecraft.util.math.MathHelper; import ru.octol1ttle.flightassistant.computers.api.ITickableComputer; -import ru.octol1ttle.flightassistant.computers.impl.autoflight.AutoFlightComputer; +import ru.octol1ttle.flightassistant.computers.impl.autoflight.AutoFlightController; import ru.octol1ttle.flightassistant.computers.impl.navigation.FlightPlanner; import ru.octol1ttle.flightassistant.registries.ComputerRegistry; public class FlightPhaseComputer implements ITickableComputer { private final AirDataComputer data = ComputerRegistry.resolve(AirDataComputer.class); - private final AutoFlightComputer autoflight = ComputerRegistry.resolve(AutoFlightComputer.class); + private final AutoFlightController autoflight = ComputerRegistry.resolve(AutoFlightController.class); private final FlightPlanner plan = ComputerRegistry.resolve(FlightPlanner.class); public FlightPhase phase = FlightPhase.UNKNOWN; - private float minimumHeight = Float.MAX_VALUE; - private boolean wasAutolandAllowed = true; @SuppressWarnings("DataFlowIssue") @Override @@ -22,21 +19,10 @@ public void tick() { phase = FlightPhase.ON_GROUND; } - if (!isAboutToLand()) { - minimumHeight = Float.MAX_VALUE; - } - if (!data.isFlying()) { return; } - Integer cruiseAltitude = plan.getCruiseAltitude(); - Integer targetAltitude = autoflight.getTargetAltitude(); - if (cruiseAltitude == null || targetAltitude == null) { - phase = FlightPhase.UNKNOWN; - return; - } - if (phase == FlightPhase.ON_GROUND) { phase = FlightPhase.TAKEOFF; } @@ -45,6 +31,13 @@ public void tick() { return; } + Integer cruiseAltitude = plan.getCruiseAltitude(); + Integer targetAltitude = autoflight.getTargetAltitude(); + if (cruiseAltitude == null || targetAltitude == null) { + phase = FlightPhase.UNKNOWN; + return; + } + if (!isNearDestination()) { if (data.altitude() - targetAltitude <= 5.0f) { phase = FlightPhase.CLIMB; @@ -70,18 +63,6 @@ public void tick() { phase = FlightPhase.LAND; } } - - if (isAboutToLand() && plan.landAltitude != null) { - float heightAboveDestination = data.altitude() - plan.landAltitude; - minimumHeight = MathHelper.clamp(heightAboveDestination, 0.0f, minimumHeight); - - if (heightAboveDestination - minimumHeight >= 5.0f - || wasAutolandAllowed && !plan.autolandAllowed && plan.getDistanceToWaypoint() > 10.0f && heightAboveDestination > 2.0f) { - phase = FlightPhase.GO_AROUND; - } - } - - wasAutolandAllowed = plan.autolandAllowed; } private boolean isAboutToLand() { @@ -100,8 +81,6 @@ public String getId() { @Override public void reset() { phase = FlightPhase.UNKNOWN; - minimumHeight = Float.MAX_VALUE; - wasAutolandAllowed = true; } public enum FlightPhase { diff --git a/src/main/java/ru/octol1ttle/flightassistant/computers/impl/autoflight/AutoFlightComputer.java b/src/main/java/ru/octol1ttle/flightassistant/computers/impl/autoflight/AutoFlightController.java similarity index 64% rename from src/main/java/ru/octol1ttle/flightassistant/computers/impl/autoflight/AutoFlightComputer.java rename to src/main/java/ru/octol1ttle/flightassistant/computers/impl/autoflight/AutoFlightController.java index a7f31939..5d4220fc 100644 --- a/src/main/java/ru/octol1ttle/flightassistant/computers/impl/autoflight/AutoFlightComputer.java +++ b/src/main/java/ru/octol1ttle/flightassistant/computers/impl/autoflight/AutoFlightController.java @@ -3,20 +3,14 @@ import org.jetbrains.annotations.Nullable; import ru.octol1ttle.flightassistant.computers.api.IAutopilotProvider; import ru.octol1ttle.flightassistant.computers.api.ITickableComputer; -import ru.octol1ttle.flightassistant.computers.impl.AirDataComputer; import ru.octol1ttle.flightassistant.computers.impl.navigation.FlightPlanner; import ru.octol1ttle.flightassistant.computers.impl.safety.FlightProtectionsComputer; -import ru.octol1ttle.flightassistant.computers.impl.safety.GroundProximityComputer; -import ru.octol1ttle.flightassistant.config.FAConfig; import ru.octol1ttle.flightassistant.registries.ComputerRegistry; -public class AutoFlightComputer implements ITickableComputer, IAutopilotProvider { - private final AirDataComputer data = ComputerRegistry.resolve(AirDataComputer.class); - private final GroundProximityComputer gpws = ComputerRegistry.resolve(GroundProximityComputer.class); - private final FlightPlanner plan = ComputerRegistry.resolve(FlightPlanner.class); - private final FireworkController firework = ComputerRegistry.resolve(FireworkController.class); +public class AutoFlightController implements ITickableComputer, IAutopilotProvider { private final FlightProtectionsComputer prot = ComputerRegistry.resolve(FlightProtectionsComputer.class); + private final FlightPlanner plan = ComputerRegistry.resolve(FlightPlanner.class); public boolean flightDirectorsEnabled = false; public boolean autoThrustEnabled = false; @@ -31,21 +25,6 @@ public class AutoFlightComputer implements ITickableComputer, IAutopilotProvider @Override public void tick() { - if (autoThrustEnabled && data.isCurrentChunkLoaded && gpws.fireworkUseSafe && gpws.getGPWSLampColor() == FAConfig.indicator().frameColor) { - Integer targetSpeed = getTargetSpeed(); - Integer targetAltitude = getTargetAltitude(); - if (targetSpeed != null) { - if (data.speed() < targetSpeed) { - firework.activateFirework(false); - } - } else if (targetAltitude != null && targetAltitude > data.altitude() - && data.speed() < 30 - && data.velocity.y < 0.0f - && data.pitch() > 0) { - firework.activateFirework(false); - } - } - if (prot.law != FlightProtectionsComputer.FlightControlLaw.NORMAL || ComputerRegistry.anyFaulted(computer -> computer instanceof IAutopilotProvider)) { disconnectAutoFirework(true); diff --git a/src/main/java/ru/octol1ttle/flightassistant/computers/impl/autoflight/AutopilotControlComputer.java b/src/main/java/ru/octol1ttle/flightassistant/computers/impl/autoflight/AutopilotComputer.java similarity index 61% rename from src/main/java/ru/octol1ttle/flightassistant/computers/impl/autoflight/AutopilotControlComputer.java rename to src/main/java/ru/octol1ttle/flightassistant/computers/impl/autoflight/AutopilotComputer.java index ace8cb54..ffe193fc 100644 --- a/src/main/java/ru/octol1ttle/flightassistant/computers/impl/autoflight/AutopilotControlComputer.java +++ b/src/main/java/ru/octol1ttle/flightassistant/computers/impl/autoflight/AutopilotComputer.java @@ -1,6 +1,8 @@ package ru.octol1ttle.flightassistant.computers.impl.autoflight; +import net.minecraft.text.Text; import net.minecraft.util.math.MathHelper; +import org.apache.commons.lang3.NotImplementedException; import org.jetbrains.annotations.Nullable; import org.joml.Vector2d; import ru.octol1ttle.flightassistant.FAMathHelper; @@ -9,33 +11,63 @@ import ru.octol1ttle.flightassistant.computers.api.IHeadingController; import ru.octol1ttle.flightassistant.computers.api.IPitchController; import ru.octol1ttle.flightassistant.computers.api.IRollController; +import ru.octol1ttle.flightassistant.computers.api.IThrustController; import ru.octol1ttle.flightassistant.computers.api.ITickableComputer; import ru.octol1ttle.flightassistant.computers.api.InputPriority; +import ru.octol1ttle.flightassistant.computers.api.ThrustControlInput; import ru.octol1ttle.flightassistant.computers.impl.AirDataComputer; import ru.octol1ttle.flightassistant.computers.impl.FlightPhaseComputer; import ru.octol1ttle.flightassistant.computers.impl.navigation.FlightPlanner; import ru.octol1ttle.flightassistant.registries.ComputerRegistry; -public class AutopilotControlComputer implements ITickableComputer, IAutopilotProvider, IPitchController, IHeadingController, IRollController { +public class AutopilotComputer implements ITickableComputer, IAutopilotProvider, IPitchController, IHeadingController, IRollController, IThrustController { private final AirDataComputer data = ComputerRegistry.resolve(AirDataComputer.class); - private final AutoFlightComputer autoflight = ComputerRegistry.resolve(AutoFlightComputer.class); + private final AutoFlightController autoflight = ComputerRegistry.resolve(AutoFlightController.class); private final FlightPhaseComputer phase = ComputerRegistry.resolve(FlightPhaseComputer.class); private final FlightPlanner plan = ComputerRegistry.resolve(FlightPlanner.class); - public Float targetPitch; - public Float targetHeading; + private final ThrustController thrust = ComputerRegistry.resolve(ThrustController.class); + public Text verticalMode; + public Text lateralMode; + public Text thrustMode; + private Float targetPitch; + private Float targetHeading; + private Float targetThrust; @Override public void tick() { - targetPitch = computeTargetPitch(); - targetHeading = computeTargetHeading(); - } - - private Float computeTargetPitch() { if (phase.phase == FlightPhaseComputer.FlightPhase.TAKEOFF || phase.phase == FlightPhaseComputer.FlightPhase.GO_AROUND && data.heightAboveGround() < 15.0f) { - return PitchController.CLIMB_PITCH; + setTargetThrust(1.0f, Text.translatable("mode.flightassistant.thrust.toga")); + setTargetPitch(PitchController.CLIMB_PITCH, Text.translatable("mode.flightassistant.vert.climb.optimum")); + setTargetHeading(data.heading(), Text.translatable("mode.flightassistant.lat.current")); + + return; } + tickLateral(); + + Integer targetAltitude = autoflight.getTargetAltitude(); + if (targetAltitude == null) { + targetPitch = null; + targetThrust = null; + return; + } + + boolean useThrustConservatively = thrust.thrustHandler instanceof FireworkController || !thrust.thrustHandler.canBeUsed(); + // TODO + throw new NotImplementedException(); + } + + private void tickLateral() { + Vector2d planPos = plan.getTargetPosition(); + if (autoflight.selectedHeading != null) { + setTargetHeading(Float.valueOf(autoflight.selectedHeading), Text.translatable("mode.flightassistant.lat.selected", autoflight.selectedHeading)); + } else if (planPos != null) { + setTargetHeading(plan.getManagedHeading(), Text.translatable("mode.flightassistant.lat.managed", (int) planPos.x, (int) planPos.y)); + } + } + + private Float computeTargetPitch() { Integer targetAltitude = autoflight.getTargetAltitude(); if (targetAltitude == null) { return null; @@ -95,12 +127,27 @@ private float computeClimbPitch(float diff, Vector2d target) { return degrees; } - private Float computeTargetHeading() { - if (phase.phase == FlightPhaseComputer.FlightPhase.TAKEOFF || phase.phase == FlightPhaseComputer.FlightPhase.GO_AROUND) { - return data.heading(); - } + public Float getTargetPitch() { + return targetPitch; + } + + public void setTargetPitch(Float targetPitch, Text mode) { + this.targetPitch = targetPitch; + this.verticalMode = mode; + } + + public Float getTargetHeading() { + return targetHeading; + } - return autoflight.getTargetHeading(); + public void setTargetHeading(Float targetHeading, Text mode) { + this.targetHeading = targetHeading; + this.lateralMode = mode; + } + + public void setTargetThrust(Float targetThrust, Text mode) { + this.targetThrust = targetThrust; + this.thrustMode = mode; } @Override @@ -130,6 +177,15 @@ private Float computeTargetHeading() { return new ControlInput(0.0f, 2.0f, InputPriority.NORMAL); } + @Override + public ThrustControlInput getThrustInput() { + if (!autoflight.autoThrustEnabled || targetThrust == null) { + return null; + } + + return new ThrustControlInput(targetThrust, InputPriority.NORMAL); + } + @Override public String getId() { return "autopilot_ctl"; @@ -138,7 +194,10 @@ public String getId() { @Override public void reset() { targetPitch = null; + verticalMode = null; targetHeading = null; + lateralMode = null; + targetThrust = null; autoflight.disconnectAutoFirework(true); autoflight.disconnectAutopilot(true); diff --git a/src/main/java/ru/octol1ttle/flightassistant/computers/impl/autoflight/FireworkController.java b/src/main/java/ru/octol1ttle/flightassistant/computers/impl/autoflight/FireworkController.java index e372b406..2ea8ab60 100644 --- a/src/main/java/ru/octol1ttle/flightassistant/computers/impl/autoflight/FireworkController.java +++ b/src/main/java/ru/octol1ttle/flightassistant/computers/impl/autoflight/FireworkController.java @@ -56,8 +56,11 @@ public void tick() { i++; } - if (data.speed() / FIREWORK_SPEED < thrust.currentThrust) { - activateFirework(false); + + if (thrust.thrustHandler == this || !thrust.thrustHandler.canBeUsed()) { + if (data.speed() / FIREWORK_SPEED < thrust.currentThrust) { + activateFirework(false); + } } } @@ -136,6 +139,11 @@ public boolean isFireworkSafe(ItemStack stack) { return nbtCompound == null || nbtCompound.getList("Explosions", NbtElement.COMPOUND_TYPE).isEmpty(); } + @Override + public boolean canBeUsed() { + return true; + } + @Override public String getId() { return "frwk_ctl"; diff --git a/src/main/java/ru/octol1ttle/flightassistant/computers/impl/autoflight/ThrustController.java b/src/main/java/ru/octol1ttle/flightassistant/computers/impl/autoflight/ThrustController.java index 9ab60ff3..db88877a 100644 --- a/src/main/java/ru/octol1ttle/flightassistant/computers/impl/autoflight/ThrustController.java +++ b/src/main/java/ru/octol1ttle/flightassistant/computers/impl/autoflight/ThrustController.java @@ -22,9 +22,9 @@ public class ThrustController implements ITickableComputer, INormalLawProvider { private static final float MIN_TO_MAX_SPOOL_UP_TIME = 5.0f; private final TimeComputer time = ComputerRegistry.resolve(TimeComputer.class); private final List controllers = new ArrayList<>(); + public IThrustHandler thrustHandler; public float currentThrust = 0.0f; public float targetThrust = 0.0f; - private boolean thrustHandlerRegistered = false; public ThrustController() { CustomComputerRegistrationCallback.EVENT.register(() -> { @@ -37,12 +37,12 @@ public ThrustController() { controllers.add(controller); } if (computer instanceof IThrustHandler handler) { - if (thrustHandlerRegistered) { + if (thrustHandler != null && !(thrustHandler instanceof FireworkController)) { FlightAssistant.LOGGER.error("Multiple thrust handlers found! Discarding handler %s".formatted(handler.getClass().getName())); return false; } - if (!(handler instanceof FireworkController)) { - thrustHandlerRegistered = true; + thrustHandler = handler; + if (!(thrustHandler instanceof FireworkController)) { FlightAssistant.LOGGER.info("Active thrust handler is %s".formatted(handler.getClass().getName())); } } diff --git a/src/main/java/ru/octol1ttle/flightassistant/hud/impl/AltitudeDisplay.java b/src/main/java/ru/octol1ttle/flightassistant/hud/impl/AltitudeDisplay.java index 4f57c800..aeab8e56 100644 --- a/src/main/java/ru/octol1ttle/flightassistant/hud/impl/AltitudeDisplay.java +++ b/src/main/java/ru/octol1ttle/flightassistant/hud/impl/AltitudeDisplay.java @@ -10,7 +10,7 @@ import ru.octol1ttle.flightassistant.Dimensions; import ru.octol1ttle.flightassistant.DrawHelper; import ru.octol1ttle.flightassistant.computers.impl.AirDataComputer; -import ru.octol1ttle.flightassistant.computers.impl.autoflight.AutoFlightComputer; +import ru.octol1ttle.flightassistant.computers.impl.autoflight.AutoFlightController; import ru.octol1ttle.flightassistant.computers.impl.navigation.FlightPlanner; import ru.octol1ttle.flightassistant.config.FAConfig; import ru.octol1ttle.flightassistant.hud.api.IHudDisplay; @@ -19,7 +19,7 @@ public class AltitudeDisplay implements IHudDisplay { private final Dimensions dim; private final AirDataComputer data = ComputerRegistry.resolve(AirDataComputer.class); - private final AutoFlightComputer autoflight = ComputerRegistry.resolve(AutoFlightComputer.class); + private final AutoFlightController autoflight = ComputerRegistry.resolve(AutoFlightController.class); private final FlightPlanner plan = ComputerRegistry.resolve(FlightPlanner.class); public AltitudeDisplay(Dimensions dim) { diff --git a/src/main/java/ru/octol1ttle/flightassistant/hud/impl/FlightDirectorsDisplay.java b/src/main/java/ru/octol1ttle/flightassistant/hud/impl/FlightDirectorsDisplay.java index 293ba106..e3bf6a8e 100644 --- a/src/main/java/ru/octol1ttle/flightassistant/hud/impl/FlightDirectorsDisplay.java +++ b/src/main/java/ru/octol1ttle/flightassistant/hud/impl/FlightDirectorsDisplay.java @@ -7,8 +7,8 @@ import ru.octol1ttle.flightassistant.Dimensions; import ru.octol1ttle.flightassistant.DrawHelper; import ru.octol1ttle.flightassistant.computers.impl.AirDataComputer; -import ru.octol1ttle.flightassistant.computers.impl.autoflight.AutoFlightComputer; -import ru.octol1ttle.flightassistant.computers.impl.autoflight.AutopilotControlComputer; +import ru.octol1ttle.flightassistant.computers.impl.autoflight.AutoFlightController; +import ru.octol1ttle.flightassistant.computers.impl.autoflight.AutopilotComputer; import ru.octol1ttle.flightassistant.config.FAConfig; import ru.octol1ttle.flightassistant.hud.api.IHudDisplay; import ru.octol1ttle.flightassistant.registries.ComputerRegistry; @@ -16,8 +16,8 @@ public class FlightDirectorsDisplay implements IHudDisplay { private final Dimensions dim; private final AirDataComputer data = ComputerRegistry.resolve(AirDataComputer.class); - private final AutoFlightComputer autoflight = ComputerRegistry.resolve(AutoFlightComputer.class); - private final AutopilotControlComputer autopilot = ComputerRegistry.resolve(AutopilotControlComputer.class); + private final AutoFlightController autoflight = ComputerRegistry.resolve(AutoFlightController.class); + private final AutopilotComputer autopilot = ComputerRegistry.resolve(AutopilotComputer.class); public FlightDirectorsDisplay(Dimensions dim) { this.dim = dim; @@ -29,14 +29,14 @@ public void render(DrawContext context, TextRenderer textRenderer) { return; } - if (autopilot.targetPitch != null) { - float deltaPitch = autopilot.targetPitch - data.pitch(); + if (autopilot.getTargetPitch() != null) { + float deltaPitch = autopilot.getTargetPitch() - data.pitch(); int fdY = MathHelper.clamp(Math.round(dim.yMid - deltaPitch * dim.degreesPerPixel), dim.tFrame + 10, dim.bFrame - 20); DrawHelper.drawHorizontalLine(context, dim.xMid - dim.wFrame / 10, dim.xMid + dim.wFrame / 10, fdY, FAConfig.indicator().advisoryColor); } - if (autopilot.targetHeading != null) { - float deltaHeading = autopilot.targetHeading - data.heading(); + if (autopilot.getTargetHeading() != null) { + float deltaHeading = autopilot.getTargetHeading() - data.heading(); if (deltaHeading < -180.0f) { deltaHeading += 360.0f; } diff --git a/src/main/java/ru/octol1ttle/flightassistant/hud/impl/FlightModeDisplay.java b/src/main/java/ru/octol1ttle/flightassistant/hud/impl/FlightModeDisplay.java index 9156944e..45ebd52e 100644 --- a/src/main/java/ru/octol1ttle/flightassistant/hud/impl/FlightModeDisplay.java +++ b/src/main/java/ru/octol1ttle/flightassistant/hud/impl/FlightModeDisplay.java @@ -10,7 +10,7 @@ import ru.octol1ttle.flightassistant.DrawHelper; import ru.octol1ttle.flightassistant.computers.impl.AirDataComputer; import ru.octol1ttle.flightassistant.computers.impl.TimeComputer; -import ru.octol1ttle.flightassistant.computers.impl.autoflight.AutoFlightComputer; +import ru.octol1ttle.flightassistant.computers.impl.autoflight.AutoFlightController; import ru.octol1ttle.flightassistant.computers.impl.autoflight.FireworkController; import ru.octol1ttle.flightassistant.computers.impl.navigation.FlightPlanner; import ru.octol1ttle.flightassistant.config.FAConfig; @@ -22,7 +22,7 @@ public class FlightModeDisplay implements IHudDisplay { private final AirDataComputer data = ComputerRegistry.resolve(AirDataComputer.class); private final TimeComputer time = ComputerRegistry.resolve(TimeComputer.class); private final FireworkController firework = ComputerRegistry.resolve(FireworkController.class); - private final AutoFlightComputer autoflight = ComputerRegistry.resolve(AutoFlightComputer.class); + private final AutoFlightController autoflight = ComputerRegistry.resolve(AutoFlightController.class); private final FlightPlanner plan = ComputerRegistry.resolve(FlightPlanner.class); private final FlightMode fireworkMode; diff --git a/src/main/java/ru/octol1ttle/flightassistant/hud/impl/HeadingDisplay.java b/src/main/java/ru/octol1ttle/flightassistant/hud/impl/HeadingDisplay.java index c2ab84d8..feec9cc7 100644 --- a/src/main/java/ru/octol1ttle/flightassistant/hud/impl/HeadingDisplay.java +++ b/src/main/java/ru/octol1ttle/flightassistant/hud/impl/HeadingDisplay.java @@ -8,7 +8,7 @@ import ru.octol1ttle.flightassistant.Dimensions; import ru.octol1ttle.flightassistant.DrawHelper; import ru.octol1ttle.flightassistant.computers.impl.AirDataComputer; -import ru.octol1ttle.flightassistant.computers.impl.autoflight.AutoFlightComputer; +import ru.octol1ttle.flightassistant.computers.impl.autoflight.AutoFlightController; import ru.octol1ttle.flightassistant.config.FAConfig; import ru.octol1ttle.flightassistant.hud.api.IHudDisplay; import ru.octol1ttle.flightassistant.registries.ComputerRegistry; @@ -17,7 +17,7 @@ public class HeadingDisplay implements IHudDisplay { private final Dimensions dim; private final AirDataComputer data = ComputerRegistry.resolve(AirDataComputer.class); - private final AutoFlightComputer autoflight = ComputerRegistry.resolve(AutoFlightComputer.class); + private final AutoFlightController autoflight = ComputerRegistry.resolve(AutoFlightController.class); public HeadingDisplay(Dimensions dim) { this.dim = dim; diff --git a/src/main/java/ru/octol1ttle/flightassistant/registries/ComputerRegistry.java b/src/main/java/ru/octol1ttle/flightassistant/registries/ComputerRegistry.java index 73f57c69..862b1b99 100644 --- a/src/main/java/ru/octol1ttle/flightassistant/registries/ComputerRegistry.java +++ b/src/main/java/ru/octol1ttle/flightassistant/registries/ComputerRegistry.java @@ -23,8 +23,11 @@ public static void register(IComputer computer) { throw new IllegalStateException("Computer already registered with class: %s".formatted(clazz)); } - instances.put(clazz, computer); - AllowComputerRegisterCallback.EVENT.invoker().allowRegister(computer); + if (AllowComputerRegisterCallback.EVENT.invoker().allowRegister(computer)) { + instances.put(clazz, computer); + } else { + FlightAssistant.LOGGER.info("Denied registration of computer %s".formatted(computer.getClass().getName())); + } } public static T resolve(Class clazz) { diff --git a/src/main/resources/assets/flightassistant/lang/en_us.yml b/src/main/resources/assets/flightassistant/lang/en_us.yml index 15d59ce2..c4c760b3 100644 --- a/src/main/resources/assets/flightassistant/lang/en_us.yml +++ b/src/main/resources/assets/flightassistant/lang/en_us.yml @@ -216,6 +216,10 @@ mode.flightassistant: absolute: MINIM %s relative: MINIM +%s not_set: NO MINIMS + thrust: + toga: + auto: AUTO TOGA + manual: MAN TOGA firework: none_in_hotbar: NO FRWKS locked: A/FRWK ONLY @@ -229,6 +233,7 @@ mode.flightassistant: managed: P. SPD %s vert: climb: + optimum: OPT CLB selected: CLB %s managed: P. CLB %s descend: @@ -239,8 +244,9 @@ mode.flightassistant: managed: P. ALT %s land: LAND %s lat: - heading: HDG %s - position: POS %s %s + current: CUR HDG + selected: HDG %s + managed: POS %s %s approach: APPR %s %s auto: flight_directors: FD diff --git a/src/main/resources/assets/flightassistant/lang/ru_ru.yml b/src/main/resources/assets/flightassistant/lang/ru_ru.yml index 7974d524..20e585f3 100644 --- a/src/main/resources/assets/flightassistant/lang/ru_ru.yml +++ b/src/main/resources/assets/flightassistant/lang/ru_ru.yml @@ -229,6 +229,7 @@ mode.flightassistant: managed: П. СКОР %s vert: climb: + optimum: ОПТ НАБОР selected: НАБОР %s managed: П. НАБОР %s descend: @@ -239,8 +240,9 @@ mode.flightassistant: managed: П. ВЫС %s land: ПОСАДКА %s lat: - heading: КУРС %s - position: ПОЗ %s %s + current: ТЕКУЩ КУРС + selected: КУРС %s + managed: ПОЗ %s %s approach: ЗАХОД %s %s auto: flight_directors: УК