Skip to content

Commit

Permalink
feat: extract ALTN LAW alert, add automatic AP disconnect on alternat…
Browse files Browse the repository at this point in the history
…e law trigger and autoflight system faults
  • Loading branch information
Octol1ttle committed Apr 21, 2024
1 parent ed57e27 commit f1089a2
Show file tree
Hide file tree
Showing 17 changed files with 118 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@
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.alerts.impl.AlertSoundData;
import ru.octol1ttle.flightassistant.computers.api.IComputer;
import ru.octol1ttle.flightassistant.computers.api.IPitchLimiter;
import ru.octol1ttle.flightassistant.config.FAConfig;
import ru.octol1ttle.flightassistant.registries.ComputerRegistry;

Expand All @@ -28,19 +27,8 @@ public boolean isTriggered() {
public int render(TextRenderer textRenderer, DrawContext context, int x, int y, boolean highlight) {
int i = 0;

boolean drawnAlternateLaw = false;
for (IComputer computer : ComputerRegistry.getComputers()) {
if (ComputerRegistry.isFaulted(computer.getClass())) {
if (!drawnAlternateLaw && computer instanceof IPitchLimiter) {
i += DrawHelper.drawHighlightedText(textRenderer, context,
Text.translatable("alerts.flightassistant.fault.computers.pitch_normal_law"), x, y,
FAConfig.indicator().warningColor, highlight
);
y += 10;

drawnAlternateLaw = true;
}

i += DrawHelper.drawHighlightedText(textRenderer, context,
Text.translatable("alerts.flightassistant.fault.computers." + computer.getId()), x, y,
FAConfig.indicator().warningColor, highlight
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package ru.octol1ttle.flightassistant.alerts.impl.other;

import net.minecraft.client.font.TextRenderer;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.text.Text;
import org.jetbrains.annotations.NotNull;
import ru.octol1ttle.flightassistant.DrawHelper;
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.safety.FlightProtectionsComputer;
import ru.octol1ttle.flightassistant.config.FAConfig;
import ru.octol1ttle.flightassistant.registries.ComputerRegistry;

public class ProtectionsLostAlert extends BaseAlert implements IECAMAlert {
private final FlightProtectionsComputer prot = ComputerRegistry.resolve(FlightProtectionsComputer.class);

@Override
public boolean isTriggered() {
return prot.law != FlightProtectionsComputer.FlightControlLaw.NORMAL;
}

@Override
public @NotNull AlertSoundData getSoundData() {
return AlertSoundData.MASTER_CAUTION;
}

@Override
public int render(TextRenderer textRenderer, DrawContext context, int x, int y, boolean highlight) {
return DrawHelper.drawHighlightedText(textRenderer, context,
Text.translatable("alerts.flightassistant.fault.computers.flight_prot"), x, y,
FAConfig.indicator().cautionColor, highlight
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import ru.octol1ttle.flightassistant.computers.impl.autoflight.PitchController;
import ru.octol1ttle.flightassistant.computers.impl.autoflight.HeadingController;
import ru.octol1ttle.flightassistant.computers.impl.autoflight.AutopilotControlComputer;
import ru.octol1ttle.flightassistant.computers.impl.autoflight.pitch.ProtectionsPitchController;
import ru.octol1ttle.flightassistant.computers.impl.safety.FlightProtectionsComputer;
import ru.octol1ttle.flightassistant.computers.impl.navigation.FlightPlanner;
import ru.octol1ttle.flightassistant.computers.impl.safety.AlertController;
import ru.octol1ttle.flightassistant.computers.impl.safety.ChunkStatusComputer;
Expand All @@ -36,7 +36,7 @@ public ComputerHost(@NotNull MinecraftClient mc) {
ComputerRegistry.register(new FireworkController(mc));
ComputerRegistry.register(new PitchLimitComputer());
ComputerRegistry.register(new PitchController());
ComputerRegistry.register(new ProtectionsPitchController());
ComputerRegistry.register(new FlightProtectionsComputer());
ComputerRegistry.register(new ChunkStatusComputer());
ComputerRegistry.register(new StallComputer());
ComputerRegistry.register(new VoidLevelComputer());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package ru.octol1ttle.flightassistant.computers.api;

public interface IAutopilotProvider {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package ru.octol1ttle.flightassistant.computers.api;

public interface INormalLawProvider {
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import net.minecraft.util.math.Direction;
import ru.octol1ttle.flightassistant.config.ComputerConfig;

public interface IPitchLimiter {
public interface IPitchLimiter extends INormalLawProvider {
default ComputerConfig.ProtectionMode getProtectionMode() {
return ComputerConfig.ProtectionMode.HARD;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
package ru.octol1ttle.flightassistant.computers.impl.autoflight;

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 {
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);
private final FlightProtectionsComputer prot = ComputerRegistry.resolve(FlightProtectionsComputer.class);

public boolean flightDirectorsEnabled = false;
public boolean autoFireworkEnabled = false;
Expand Down Expand Up @@ -42,6 +45,12 @@ public void tick() {
firework.activateFirework(false);
}
}

if (prot.law != FlightProtectionsComputer.FlightControlLaw.NORMAL
|| ComputerRegistry.anyFaulted(computer -> computer instanceof IAutopilotProvider)) {
disconnectAutoFirework(true);
disconnectAutopilot(true);
}
}

public @Nullable Integer getTargetSpeed() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.joml.Vector2d;
import ru.octol1ttle.flightassistant.FAMathHelper;
import ru.octol1ttle.flightassistant.computers.api.ControllerPriority;
import ru.octol1ttle.flightassistant.computers.api.IAutopilotProvider;
import ru.octol1ttle.flightassistant.computers.api.IPitchController;
import ru.octol1ttle.flightassistant.computers.api.ITickableComputer;
import ru.octol1ttle.flightassistant.computers.api.IHeadingController;
Expand All @@ -15,7 +16,7 @@
import ru.octol1ttle.flightassistant.computers.impl.navigation.FlightPlanner;
import ru.octol1ttle.flightassistant.registries.ComputerRegistry;

public class AutopilotControlComputer implements ITickableComputer, IPitchController, IHeadingController {
public class AutopilotControlComputer implements ITickableComputer, IPitchController, IHeadingController, IAutopilotProvider {
private final AirDataComputer data = ComputerRegistry.resolve(AirDataComputer.class);
private final AutoFlightComputer autoflight = ComputerRegistry.resolve(AutoFlightComputer.class);
private final FlightPhaseComputer phase = ComputerRegistry.resolve(FlightPhaseComputer.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@
import net.minecraft.nbt.NbtCompound;
import net.minecraft.nbt.NbtElement;
import net.minecraft.util.Hand;
import ru.octol1ttle.flightassistant.computers.api.IAutopilotProvider;
import ru.octol1ttle.flightassistant.computers.impl.AirDataComputer;
import ru.octol1ttle.flightassistant.computers.api.ITickableComputer;
import ru.octol1ttle.flightassistant.computers.impl.TimeComputer;
import ru.octol1ttle.flightassistant.registries.ComputerRegistry;

public class FireworkController implements ITickableComputer {
public class FireworkController implements ITickableComputer, IAutopilotProvider {
private final MinecraftClient mc;
private final AirDataComputer data = ComputerRegistry.resolve(AirDataComputer.class);
private final TimeComputer time = ComputerRegistry.resolve(TimeComputer.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@
import net.minecraft.util.math.MathHelper;
import org.jetbrains.annotations.NotNull;
import ru.octol1ttle.flightassistant.computers.api.ControllerPriority;
import ru.octol1ttle.flightassistant.computers.api.IAutopilotProvider;
import ru.octol1ttle.flightassistant.computers.api.IHeadingController;
import ru.octol1ttle.flightassistant.computers.impl.AirDataComputer;
import ru.octol1ttle.flightassistant.computers.api.ITickableComputer;
import ru.octol1ttle.flightassistant.computers.impl.TimeComputer;
import ru.octol1ttle.flightassistant.registries.ComputerRegistry;
import ru.octol1ttle.flightassistant.registries.events.ComputerRegisteredCallback;

public class HeadingController implements ITickableComputer {
public class HeadingController implements ITickableComputer, IAutopilotProvider {
private final AirDataComputer data = ComputerRegistry.resolve(AirDataComputer.class);
private final TimeComputer time = ComputerRegistry.resolve(TimeComputer.class);
private final List<IHeadingController> controllers = new ArrayList<>();
Expand All @@ -38,9 +39,9 @@ public void tick() {
break;
}

Pair<@NotNull Float, @NotNull Float> targetPitch = controller.getControlledHeading();
if (targetPitch != null) {
smoothSetHeading(targetPitch.getLeft(), MathHelper.clamp(time.deltaTime * targetPitch.getRight(), 0.001f, 1.0f));
Pair<@NotNull Float, @NotNull Float> targetHeading = controller.getControlledHeading();
if (targetHeading != null) {
smoothSetHeading(targetHeading.getLeft(), MathHelper.clamp(time.deltaTime * targetHeading.getRight(), 0.001f, 1.0f));
lastPriority = controller.getPriority();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import net.minecraft.util.math.MathHelper;
import org.jetbrains.annotations.NotNull;
import ru.octol1ttle.flightassistant.computers.api.ControllerPriority;
import ru.octol1ttle.flightassistant.computers.api.INormalLawProvider;
import ru.octol1ttle.flightassistant.computers.api.IPitchController;
import ru.octol1ttle.flightassistant.computers.api.ITickableComputer;
import ru.octol1ttle.flightassistant.computers.impl.AirDataComputer;
Expand All @@ -15,7 +16,7 @@
import ru.octol1ttle.flightassistant.registries.ComputerRegistry;
import ru.octol1ttle.flightassistant.registries.events.ComputerRegisteredCallback;

public class PitchController implements ITickableComputer {
public class PitchController implements ITickableComputer, INormalLawProvider {
public static final float CLIMB_PITCH = 55.0f;
public static final float ALTITUDE_PRESERVE_PITCH = 15.0f;
public static final float GLIDE_PITCH = -2.2f;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
import java.util.List;
import net.minecraft.client.sound.SoundManager;
import ru.octol1ttle.flightassistant.AlertSoundInstance;
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.alerts.impl.AlertSoundData;
import ru.octol1ttle.flightassistant.alerts.impl.autoflight.AutoFireworkOffAlert;
import ru.octol1ttle.flightassistant.alerts.impl.autoflight.AutopilotOffAlert;
import ru.octol1ttle.flightassistant.alerts.impl.fault.ComputerFaultAlert;
Expand All @@ -21,9 +21,10 @@
import ru.octol1ttle.flightassistant.alerts.impl.nav.gpws.ExcessiveTerrainClosureAlert;
import ru.octol1ttle.flightassistant.alerts.impl.nav.gpws.UnsafeTerrainClearanceAlert;
import ru.octol1ttle.flightassistant.alerts.impl.other.ElytraHealthLowAlert;
import ru.octol1ttle.flightassistant.alerts.impl.other.ProtectionsLostAlert;
import ru.octol1ttle.flightassistant.alerts.impl.other.StallAlert;
import ru.octol1ttle.flightassistant.computers.impl.AirDataComputer;
import ru.octol1ttle.flightassistant.computers.api.ITickableComputer;
import ru.octol1ttle.flightassistant.computers.impl.AirDataComputer;
import ru.octol1ttle.flightassistant.registries.AlertRegistry;
import ru.octol1ttle.flightassistant.registries.ComputerRegistry;
import ru.octol1ttle.flightassistant.registries.events.CustomAlertRegistrationCallback;
Expand All @@ -37,19 +38,20 @@ public AlertController(SoundManager manager) {
this.manager = manager;
// TODO: ECAM actions
AlertRegistry.register(new StallAlert());
AlertRegistry.register(new UnloadedChunkAlert());
AlertRegistry.register(new ExcessiveDescentAlert());
AlertRegistry.register(new ExcessiveTerrainClosureAlert());
AlertRegistry.register(new UnsafeTerrainClearanceAlert());
AlertRegistry.register(new AutopilotOffAlert());
AlertRegistry.register(new ProtectionsLostAlert());
AlertRegistry.register(new UnloadedChunkAlert());
AlertRegistry.register(new ComputerFaultAlert());
AlertRegistry.register(new IndicatorFaultAlert());
AlertRegistry.register(new AutoFireworkOffAlert());
AlertRegistry.register(new MinimumsAlert());
AlertRegistry.register(new ApproachingVoidDamageLevelAlert());
AlertRegistry.register(new ElytraHealthLowAlert());
AlertRegistry.register(new FireworkUnsafeAlert());
AlertRegistry.register(new FireworkNoResponseAlert());
AlertRegistry.register(new IndicatorFaultAlert());

CustomAlertRegistrationCallback.EVENT.invoker().registerCustomAlerts();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,30 @@
package ru.octol1ttle.flightassistant.computers.impl.autoflight.pitch;
package ru.octol1ttle.flightassistant.computers.impl.safety;

import net.minecraft.util.Pair;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import ru.octol1ttle.flightassistant.computers.api.ControllerPriority;
import ru.octol1ttle.flightassistant.computers.api.IComputer;
import ru.octol1ttle.flightassistant.computers.api.INormalLawProvider;
import ru.octol1ttle.flightassistant.computers.api.IPitchController;
import ru.octol1ttle.flightassistant.computers.api.ITickableComputer;
import ru.octol1ttle.flightassistant.computers.impl.AirDataComputer;
import ru.octol1ttle.flightassistant.computers.impl.safety.PitchLimitComputer;
import ru.octol1ttle.flightassistant.config.ComputerConfig;
import ru.octol1ttle.flightassistant.registries.ComputerRegistry;

public class ProtectionsPitchController implements IComputer, IPitchController {
public class FlightProtectionsComputer implements ITickableComputer, IPitchController, INormalLawProvider {
private final AirDataComputer data = ComputerRegistry.resolve(AirDataComputer.class);
private final PitchLimitComputer limit = ComputerRegistry.resolve(PitchLimitComputer.class);
public FlightControlLaw law = FlightControlLaw.NORMAL;

@Override
public void tick() {
if (ComputerRegistry.anyFaulted(computer -> computer instanceof INormalLawProvider)) {
law = FlightControlLaw.ALTERNATE;
return;
}

law = FlightControlLaw.NORMAL;
}

@Override
public @Nullable Pair<@NotNull Float, @NotNull Float> getControlledPitch() {
Expand All @@ -35,10 +46,16 @@ public ControllerPriority getPriority() {

@Override
public String getId() {
return "pitch_normal_law";
return "flight_prot";
}

@Override
public void reset() {
law = FlightControlLaw.NORMAL;
}

public enum FlightControlLaw {
NORMAL,
ALTERNATE
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@

import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
import java.util.function.Predicate;
import net.minecraft.util.Pair;
import net.minecraft.util.math.Direction;
import ru.octol1ttle.flightassistant.computers.api.IComputer;
import ru.octol1ttle.flightassistant.computers.api.INormalLawProvider;
import ru.octol1ttle.flightassistant.computers.api.IPitchLimiter;
import ru.octol1ttle.flightassistant.computers.api.ITickableComputer;
import ru.octol1ttle.flightassistant.config.ComputerConfig;
import ru.octol1ttle.flightassistant.registries.ComputerRegistry;
import ru.octol1ttle.flightassistant.registries.events.ComputerRegisteredCallback;

public class PitchLimitComputer implements ITickableComputer {
public class PitchLimitComputer implements ITickableComputer, INormalLawProvider {
private final List<IPitchLimiter> limiters = new ArrayList<>();
public float minimumSafePitch = -90.0f;
public float maximumSafePitch = 90.0f;
Expand Down Expand Up @@ -40,14 +41,14 @@ public void tick() {

/**
* Gets the pitches which are considered safe by pitch limiters satisfying a condition.
* @param condition The condition that pitch limiters' protection mode must satisfy in order to be considered
* @param predicate The condition that pitch limiters' protection mode must satisfy in order to be considered
* @return a pair of two floats: the first one is the minimum safe pitch, second is the maximum safe pitch
*/
public Pair<Float, Float> getSafePitches(Function<ComputerConfig.ProtectionMode, Boolean> condition) {
public Pair<Float, Float> getSafePitches(Predicate<ComputerConfig.ProtectionMode> predicate) {
float minimum = -90.0f;
float maximum = 90.0f;
for (IPitchLimiter limiter : limiters) {
if (!condition.apply(limiter.getProtectionMode()) || limiter instanceof IComputer computer && ComputerRegistry.isFaulted(computer.getClass())) {
if (!predicate.test(limiter.getProtectionMode()) || limiter instanceof IComputer computer && ComputerRegistry.isFaulted(computer.getClass())) {
continue;
}

Expand All @@ -58,9 +59,9 @@ public Pair<Float, Float> getSafePitches(Function<ComputerConfig.ProtectionMode,
return new Pair<>(minimum, maximum);
}

public boolean blockPitchChange(Direction direction, Function<ComputerConfig.ProtectionMode, Boolean> condition) {
public boolean blockPitchChange(Direction direction, Predicate<ComputerConfig.ProtectionMode> predicate) {
for (IPitchLimiter limiter : limiters) {
if (!condition.apply(limiter.getProtectionMode()) || limiter instanceof IComputer computer && ComputerRegistry.isFaulted(computer.getClass())) {
if (!predicate.test(limiter.getProtectionMode()) || limiter instanceof IComputer computer && ComputerRegistry.isFaulted(computer.getClass())) {
continue;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Objects;
import java.util.function.Predicate;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.Nullable;
import ru.octol1ttle.flightassistant.FlightAssistant;
Expand Down Expand Up @@ -49,6 +50,16 @@ public static boolean anyFaulted() {
return !faulted.isEmpty();
}

public static boolean anyFaulted(Predicate<IComputer> predicate) {
for (Class<IComputer> clazz : faulted) {
if (predicate.test(instances.get(clazz))) {
return true;
}
}

return false;
}

public static void resetFaulted() {
faulted.clear();
}
Expand Down
Loading

0 comments on commit f1089a2

Please sign in to comment.