Skip to content

Commit

Permalink
feat: finish custom thrust handlers, begin work on autopilot v4
Browse files Browse the repository at this point in the history
Signed-off-by: Octol1ttle <l1ttleofficial@outlook.com>
  • Loading branch information
Octol1ttle committed Jun 14, 2024
1 parent c4ec750 commit 95dbd64
Show file tree
Hide file tree
Showing 20 changed files with 167 additions and 121 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -22,13 +22,13 @@ private static void registerSpeed(LiteralArgumentBuilder<FabricClientCommandSour
select.then(literal("speed")
.then(literal("managed")
.executes(context -> {
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;
})
)
Expand All @@ -39,13 +39,13 @@ private static void registerAltitude(LiteralArgumentBuilder<FabricClientCommandS
select.then(literal("altitude")
.then(literal("managed")
.executes(context -> {
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;
})
)
Expand All @@ -56,13 +56,13 @@ private static void registerHeading(LiteralArgumentBuilder<FabricClientCommandSo
select.then(literal("heading")
.then(literal("managed")
.executes(context -> {
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;
})
)
Expand Down
Original file line number Diff line number Diff line change
@@ -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);

Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Original file line number Diff line number Diff line change
@@ -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) {
}
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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;
}
Expand All @@ -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;
Expand All @@ -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() {
Expand All @@ -100,8 +81,6 @@ public String getId() {
@Override
public void reset() {
phase = FlightPhase.UNKNOWN;
minimumHeight = Float.MAX_VALUE;
wasAutolandAllowed = true;
}

public enum FlightPhase {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down
Loading

0 comments on commit 95dbd64

Please sign in to comment.