Skip to content

Commit

Permalink
Abstract pitch limiters
Browse files Browse the repository at this point in the history
  • Loading branch information
Octol1ttle committed Apr 14, 2024
1 parent 921e99a commit 0fc98aa
Show file tree
Hide file tree
Showing 16 changed files with 205 additions and 34 deletions.
11 changes: 11 additions & 0 deletions src/main/java/ru/octol1ttle/flightassistant/FACallbacks.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@
import ru.octol1ttle.flightassistant.commands.FlightPlanCommand;
import ru.octol1ttle.flightassistant.commands.ResetCommand;
import ru.octol1ttle.flightassistant.commands.SelectCommand;
import ru.octol1ttle.flightassistant.computers.api.IPitchLimiter;
import ru.octol1ttle.flightassistant.computers.impl.AirDataComputer;
import ru.octol1ttle.flightassistant.computers.ComputerHost;
import ru.octol1ttle.flightassistant.computers.impl.TimeComputer;
import ru.octol1ttle.flightassistant.computers.impl.autoflight.FireworkController;
import ru.octol1ttle.flightassistant.computers.impl.safety.GPWSComputer;
import ru.octol1ttle.flightassistant.config.FAConfig;
import ru.octol1ttle.flightassistant.registries.ComputerRegistry;
import ru.octol1ttle.flightassistant.registries.events.ComputerRegisteredCallback;

import static net.fabricmc.fabric.api.client.command.v2.ClientCommandManager.literal;

Expand All @@ -32,6 +34,7 @@ public static void setup() {
setupWorldRender();
setupHudRender();
setupUseItem();
setupComputerRegistered();
}

private static void setupClientStart() {
Expand Down Expand Up @@ -99,4 +102,12 @@ private static void setupUseItem() {
return TypedActionResult.pass(stack);
});
}

private static void setupComputerRegistered() {
ComputerRegisteredCallback.EVENT.register(computer -> {
if (computer instanceof IPitchLimiter limiter) {
IPitchLimiter.instances.add(limiter);
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import ru.octol1ttle.flightassistant.computers.impl.safety.StallComputer;
import ru.octol1ttle.flightassistant.computers.impl.safety.VoidLevelComputer;
import ru.octol1ttle.flightassistant.registries.ComputerRegistry;
import ru.octol1ttle.flightassistant.registries.events.CustomComputerRegistrationCallback;

public class ComputerHost {
public static ComputerHost instance() {
Expand All @@ -39,6 +40,8 @@ public ComputerHost(@NotNull MinecraftClient mc) {
ComputerRegistry.register(new PitchController());
ComputerRegistry.register(new AutoFlightComputer());
ComputerRegistry.register(new AlertController(mc.getSoundManager()));

CustomComputerRegistrationCallback.EVENT.invoker().registerCustomComputers();
}

public void tick() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
package ru.octol1ttle.flightassistant.computers.api;

import java.util.ArrayList;
import java.util.List;
import net.minecraft.util.math.Direction;
import ru.octol1ttle.flightassistant.config.ComputerConfig;

public interface IPitchLimiter {
List<IPitchLimiter> instances = new ArrayList<>();

default ComputerConfig.ProtectionMode getProtectionMode() {
return ComputerConfig.ProtectionMode.HARD;
}
default float getMinimumPitch() {
return -90.0f;
}
default float getMaximumPitch() {
return 90.0f;
}
default boolean blockPitchChange(Direction direction) {
return false;
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package ru.octol1ttle.flightassistant.computers.impl.autoflight;

import net.minecraft.util.math.MathHelper;
import ru.octol1ttle.flightassistant.computers.impl.AirDataComputer;
import ru.octol1ttle.flightassistant.computers.api.IPitchLimiter;
import ru.octol1ttle.flightassistant.computers.api.ITickableComputer;
import ru.octol1ttle.flightassistant.computers.impl.AirDataComputer;
import ru.octol1ttle.flightassistant.computers.impl.TimeComputer;
import ru.octol1ttle.flightassistant.computers.impl.safety.ChunkStatusComputer;
import ru.octol1ttle.flightassistant.computers.impl.safety.GPWSComputer;
import ru.octol1ttle.flightassistant.computers.impl.safety.StallComputer;
import ru.octol1ttle.flightassistant.computers.impl.safety.VoidLevelComputer;
import ru.octol1ttle.flightassistant.config.FAConfig;
import ru.octol1ttle.flightassistant.registries.ComputerRegistry;

public class PitchController implements ITickableComputer {
Expand All @@ -30,10 +30,19 @@ public void tick() {
return;
}

if (FAConfig.computer().stallProtection.recover() && data.pitch() > 0.0f && data.pitch() > stall.maximumSafePitch) {
smoothSetPitch(stall.maximumSafePitch, time.deltaTime);
} else if (FAConfig.computer().voidProtection.recover() && data.pitch() < voidLevel.minimumSafePitch) {
smoothSetPitch(voidLevel.minimumSafePitch, time.deltaTime);
float maximumSafePitch = 90.0f;
float minimumSafePitch = -90.0f;
for (IPitchLimiter limiter : IPitchLimiter.instances) {
if (limiter.getProtectionMode().recover()) {
maximumSafePitch = Math.min(maximumSafePitch, limiter.getMaximumPitch());
minimumSafePitch = Math.max(minimumSafePitch, limiter.getMinimumPitch());
}
}

if (data.pitch() > maximumSafePitch) {
smoothSetPitch(maximumSafePitch, time.deltaTime);
} else if (data.pitch() < minimumSafePitch) {
smoothSetPitch(minimumSafePitch, time.deltaTime);
}

if (gpws.shouldCorrectSinkrate()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import ru.octol1ttle.flightassistant.computers.api.ITickableComputer;
import ru.octol1ttle.flightassistant.registries.AlertRegistry;
import ru.octol1ttle.flightassistant.registries.ComputerRegistry;
import ru.octol1ttle.flightassistant.registries.events.CustomAlertRegistrationCallback;

public class AlertController implements ITickableComputer {
public final List<BaseAlert> activeAlerts = new ArrayList<>();
Expand All @@ -49,6 +50,8 @@ public AlertController(SoundManager manager) {
AlertRegistry.register(new ElytraHealthLowAlert());
AlertRegistry.register(new FireworkUnsafeAlert());
AlertRegistry.register(new FireworkNoResponseAlert());

CustomAlertRegistrationCallback.EVENT.invoker().registerCustomAlerts();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@
import net.minecraft.util.math.Direction;
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.RaycastContext;
import ru.octol1ttle.flightassistant.computers.impl.AirDataComputer;
import ru.octol1ttle.flightassistant.computers.api.IPitchLimiter;
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.config.FAConfig;
import ru.octol1ttle.flightassistant.registries.ComputerRegistry;

public class GPWSComputer implements ITickableComputer {
public class GPWSComputer implements ITickableComputer, IPitchLimiter {
private static final int STATUS_PLAYER_INVULNERABLE = -1;
private static final int STATUS_FALL_DISTANCE_TOO_LOW = -2;
private static final int STATUS_SPEED_SAFE = -3;
Expand Down Expand Up @@ -65,11 +66,6 @@ public Color getGPWSLampColor() {
return FAConfig.indicator().frameColor;
}

public boolean shouldBlockPitchChanges() {
return FAConfig.computer().sinkrateProtection.override() && positiveLessOrEquals(descentImpactTime, PULL_UP_THRESHOLD)
|| FAConfig.computer().terrainProtection.override() && positiveLessOrEquals(terrainImpactTime, PULL_UP_THRESHOLD);
}

private float computeDescentImpactTime() {
if (!data.isFlying() || data.player().isTouchingWater()) {
return STATUS_UNKNOWN;
Expand Down Expand Up @@ -170,6 +166,12 @@ private BlockPos findHighest(BlockPos.Mutable at) {
return at;
}

@Override
public boolean blockPitchChange(Direction direction) {
return FAConfig.computer().sinkrateProtection.override() && positiveLessOrEquals(descentImpactTime, PULL_UP_THRESHOLD)
|| FAConfig.computer().terrainProtection.override() && positiveLessOrEquals(terrainImpactTime, PULL_UP_THRESHOLD);
}

@Override
public String getId() {
return "gpws";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package ru.octol1ttle.flightassistant.computers.impl.safety;

import net.minecraft.util.math.Direction;
import net.minecraft.util.math.MathHelper;
import ru.octol1ttle.flightassistant.computers.impl.AirDataComputer;
import ru.octol1ttle.flightassistant.computers.api.ITickableComputer;
import ru.octol1ttle.flightassistant.computers.api.IPitchLimiter;
import ru.octol1ttle.flightassistant.computers.impl.AirDataComputer;
import ru.octol1ttle.flightassistant.computers.impl.autoflight.FireworkController;
import ru.octol1ttle.flightassistant.config.ComputerConfig;
import ru.octol1ttle.flightassistant.config.FAConfig;
import ru.octol1ttle.flightassistant.registries.ComputerRegistry;

public class StallComputer implements ITickableComputer {
public class StallComputer implements ITickableComputer, IPitchLimiter {
private final AirDataComputer data = ComputerRegistry.resolve(AirDataComputer.class);
private final FireworkController firework = ComputerRegistry.resolve(FireworkController.class);
public StallStatus status = StallStatus.UNKNOWN;
Expand Down Expand Up @@ -51,8 +54,19 @@ private float computeMaximumSafePitch() {
return status == StallStatus.FULL_STALL ? -90.0f : MathHelper.clamp(data.speed() * 3.0f, 0.0f, 90.0f);
}

public boolean isPitchUnsafe(float newPitch) {
return newPitch > maximumSafePitch || status == StallStatus.FULL_STALL;
@Override
public float getMaximumPitch() {
return maximumSafePitch;
}

@Override
public boolean blockPitchChange(Direction direction) {
return direction == Direction.UP && status == StallStatus.FULL_STALL;
}

@Override
public ComputerConfig.ProtectionMode getProtectionMode() {
return FAConfig.computer().stallProtection;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package ru.octol1ttle.flightassistant.computers.impl.safety;

import net.minecraft.util.math.Direction;
import ru.octol1ttle.flightassistant.computers.api.IPitchLimiter;
import ru.octol1ttle.flightassistant.computers.impl.AirDataComputer;
import ru.octol1ttle.flightassistant.computers.api.ITickableComputer;
import ru.octol1ttle.flightassistant.computers.impl.autoflight.FireworkController;
import ru.octol1ttle.flightassistant.computers.impl.autoflight.PitchController;
import ru.octol1ttle.flightassistant.config.ComputerConfig;
import ru.octol1ttle.flightassistant.config.FAConfig;
import ru.octol1ttle.flightassistant.registries.ComputerRegistry;

public class VoidLevelComputer implements ITickableComputer {
public class VoidLevelComputer implements ITickableComputer, IPitchLimiter {
private final AirDataComputer data = ComputerRegistry.resolve(AirDataComputer.class);
private final FireworkController firework = ComputerRegistry.resolve(FireworkController.class);
private final StallComputer stall = ComputerRegistry.resolve(StallComputer.class);
Expand Down Expand Up @@ -65,8 +68,19 @@ public boolean approachingOrReachedDamageLevel() {
return status == VoidLevelStatus.APPROACHING_DAMAGE_LEVEL || status == VoidLevelStatus.REACHED_DAMAGE_LEVEL;
}

public boolean shouldBlockPitchChange(float newPitch) {
return FAConfig.computer().voidProtection.override() && newPitch < minimumSafePitch;
@Override
public float getMinimumPitch() {
return minimumSafePitch;
}

@Override
public boolean blockPitchChange(Direction direction) {
return direction == Direction.DOWN && status == VoidLevelStatus.REACHED_DAMAGE_LEVEL;
}

@Override
public ComputerConfig.ProtectionMode getProtectionMode() {
return FAConfig.computer().voidProtection;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import ru.octol1ttle.flightassistant.hud.impl.VerticalSpeedDisplay;
import ru.octol1ttle.flightassistant.mixin.GameRendererInvoker;
import ru.octol1ttle.flightassistant.registries.HudDisplayRegistry;
import ru.octol1ttle.flightassistant.registries.events.CustomHudDisplayRegistrationCallback;

public class HudDisplayHost {
private final Dimensions dim = new Dimensions();
Expand All @@ -45,6 +46,8 @@ public HudDisplayHost() {
HudDisplayRegistry.register(FlightAssistant.id("speed"), new SpeedDisplay(dim));
HudDisplayRegistry.register(FlightAssistant.id("status"), new StatusDisplay(dim));
HudDisplayRegistry.register(FlightAssistant.id("vertical_speed"), new VerticalSpeedDisplay(dim));

CustomHudDisplayRegistrationCallback.EVENT.invoker().registerCustomDisplays();
}

public void render(MinecraftClient mc, DrawContext context, float tickDelta) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import net.minecraft.util.math.RotationAxis;
import ru.octol1ttle.flightassistant.Dimensions;
import ru.octol1ttle.flightassistant.DrawHelper;
import ru.octol1ttle.flightassistant.computers.api.IPitchLimiter;
import ru.octol1ttle.flightassistant.computers.impl.AirDataComputer;
import ru.octol1ttle.flightassistant.computers.impl.autoflight.PitchController;
import ru.octol1ttle.flightassistant.computers.impl.safety.StallComputer;
Expand Down Expand Up @@ -48,10 +49,17 @@ public void render(DrawContext context, TextRenderer textRenderer) {

drawLadder(textRenderer, context, yHorizon);

drawPushArrows(textRenderer, context, stall.maximumSafePitch, yHorizon, FAConfig.indicator().warningColor);
float maximumSafePitch = 90.0f;
float minimumSafePitch = -90.0f;
for (IPitchLimiter limiter : IPitchLimiter.instances) {
maximumSafePitch = Math.min(maximumSafePitch, limiter.getMaximumPitch());
minimumSafePitch = Math.max(minimumSafePitch, limiter.getMinimumPitch());
}

drawPushArrows(textRenderer, context, maximumSafePitch, yHorizon, FAConfig.indicator().warningColor);
drawReferenceMark(context, PitchController.CLIMB_PITCH, yHorizon, getPitchColor(PitchController.CLIMB_PITCH));
drawReferenceMark(context, PitchController.GLIDE_PITCH, yHorizon, getPitchColor(PitchController.GLIDE_PITCH));
drawPullArrows(textRenderer, context, Math.max(PitchController.DESCEND_PITCH, voidLevel.minimumSafePitch), yHorizon, FAConfig.indicator().warningColor);
drawPullArrows(textRenderer, context, Math.max(PitchController.DESCEND_PITCH, Math.min(maximumSafePitch, minimumSafePitch)), yHorizon, FAConfig.indicator().warningColor);

pitchData.l1 -= pitchData.margin;
pitchData.r2 += pitchData.margin;
Expand Down
25 changes: 13 additions & 12 deletions src/main/java/ru/octol1ttle/flightassistant/mixin/EntityMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@

import net.minecraft.client.network.ClientPlayerEntity;
import net.minecraft.entity.Entity;
import net.minecraft.util.math.Direction;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.ModifyVariable;
import ru.octol1ttle.flightassistant.computers.api.IComputer;
import ru.octol1ttle.flightassistant.computers.api.IPitchLimiter;
import ru.octol1ttle.flightassistant.computers.impl.AirDataComputer;
import ru.octol1ttle.flightassistant.computers.impl.safety.GPWSComputer;
import ru.octol1ttle.flightassistant.computers.impl.safety.StallComputer;
import ru.octol1ttle.flightassistant.computers.impl.safety.VoidLevelComputer;
import ru.octol1ttle.flightassistant.config.FAConfig;
import ru.octol1ttle.flightassistant.registries.ComputerRegistry;

@SuppressWarnings("UnreachableCode")
Expand All @@ -24,15 +23,17 @@ public float preventUpsetPitch(float pitchDelta) {
float oldPitch = data.pitch();
float newPitch = oldPitch + (-pitchDelta);

boolean isStalling = !ComputerRegistry.isFaulted(StallComputer.class) && ComputerRegistry.resolve(StallComputer.class).isPitchUnsafe(newPitch);
boolean stallLock = FAConfig.computer().stallProtection.override() && isStalling;
for (IPitchLimiter limiter : IPitchLimiter.instances) {
if (!limiter.getProtectionMode().override()
|| limiter instanceof IComputer computer && ComputerRegistry.isFaulted(computer.getClass())) {
continue;
}

boolean gpwsLock = !isStalling && !ComputerRegistry.isFaulted(GPWSComputer.class) && ComputerRegistry.resolve(GPWSComputer.class).shouldBlockPitchChanges();
boolean voidLevelLock = !ComputerRegistry.isFaulted(VoidLevelComputer.class) && ComputerRegistry.resolve(VoidLevelComputer.class).shouldBlockPitchChange(newPitch);

if (stallLock && newPitch > oldPitch ||
(gpwsLock || voidLevelLock) && newPitch < oldPitch) {
return 0.0f;
if (limiter.blockPitchChange(newPitch > oldPitch ? Direction.UP : Direction.DOWN)
|| newPitch > oldPitch && limiter.getMaximumPitch() < newPitch
|| newPitch < oldPitch && limiter.getMinimumPitch() > newPitch) {
return 0.0f;
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.jetbrains.annotations.Nullable;
import ru.octol1ttle.flightassistant.FlightAssistant;
import ru.octol1ttle.flightassistant.computers.api.IComputer;
import ru.octol1ttle.flightassistant.registries.events.ComputerRegisteredCallback;

@SuppressWarnings("unchecked")
public abstract class ComputerRegistry {
Expand All @@ -22,6 +23,7 @@ public static void register(IComputer computer) {
}

instances.put(clazz, computer);
ComputerRegisteredCallback.EVENT.invoker().onComputerRegistered(computer);
}

public static <T extends IComputer> T resolve(Class<T> clazz) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package ru.octol1ttle.flightassistant.registries.events;

import net.fabricmc.fabric.api.event.Event;
import net.fabricmc.fabric.api.event.EventFactory;
import ru.octol1ttle.flightassistant.computers.api.IComputer;

public interface ComputerRegisteredCallback {
Event<ComputerRegisteredCallback> EVENT = EventFactory.createArrayBacked(
ComputerRegisteredCallback.class,
(listeners) -> (computer) -> {
for (ComputerRegisteredCallback event : listeners) {
event.onComputerRegistered(computer);
}
}
);

/**
* Called after a computer has been registered.
*
* @param computer the {@link IComputer} instance
*/
void onComputerRegistered(IComputer computer);
}
Loading

0 comments on commit 0fc98aa

Please sign in to comment.