Skip to content

Commit

Permalink
refactor: abstract pitch controller system
Browse files Browse the repository at this point in the history
  • Loading branch information
Octol1ttle committed Apr 15, 2024
1 parent 2995277 commit 86a120c
Show file tree
Hide file tree
Showing 22 changed files with 337 additions and 172 deletions.
19 changes: 4 additions & 15 deletions src/main/java/ru/octol1ttle/flightassistant/FACallbacks.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,13 @@
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.AirDataComputer;
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.computers.impl.safety.GroundProximityComputer;
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 @@ -34,7 +32,6 @@ public static void setup() {
setupWorldRender();
setupHudRender();
setupUseItem();
setupComputerRegistered();
}

private static void setupClientStart() {
Expand Down Expand Up @@ -71,7 +68,7 @@ private static void setupUseItem() {
UseItemCallback.EVENT.register((player, world, hand) -> {
ItemStack stack = player.getStackInHand(hand);
AirDataComputer data = ComputerRegistry.resolve(AirDataComputer.class);
GPWSComputer gpws = ComputerRegistry.resolve(GPWSComputer.class);
GroundProximityComputer gpws = ComputerRegistry.resolve(GroundProximityComputer.class);
FireworkController firework = ComputerRegistry.resolve(FireworkController.class);
TimeComputer time = ComputerRegistry.resolve(TimeComputer.class);

Expand All @@ -83,7 +80,7 @@ private static void setupUseItem() {
}

boolean gpwsLocksFireworks = FAConfig.computer().lockFireworksFacingTerrain;
boolean gpwsDanger = !ComputerRegistry.isFaulted(GPWSComputer.class) && gpwsLocksFireworks && (gpws.isInDanger() || !gpws.fireworkUseSafe);
boolean gpwsDanger = !ComputerRegistry.isFaulted(GroundProximityComputer.class) && gpwsLocksFireworks && (gpws.isInDanger() || !gpws.fireworkUseSafe);

boolean unsafeFireworks = FAConfig.computer().lockUnsafeFireworks && !firework.isFireworkSafe(player.getStackInHand(hand));

Expand All @@ -102,12 +99,4 @@ 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 @@ -337,10 +337,10 @@ private ConfigCategory computers(Text name, ComputerConfig config, ComputerConfi
.build())

.option(LabelOption.create(Text.translatable("config.flightassistant.computers.chunk_state")))
.option(Option.<ComputerConfig.ProtectionMode>createBuilder()
.name(Text.translatable("config.flightassistant.computers.chunk_state.protection"))
.binding(defaults.unloadedChunkProtection, () -> config.unloadedChunkProtection, o -> config.unloadedChunkProtection = o)
.controller(opt -> EnumControllerBuilder.create(opt).enumClass(ComputerConfig.ProtectionMode.class))
.option(Option.<Boolean>createBuilder()
.name(Text.translatable("config.flightassistant.computers.chunk_state.preserve_altitude"))
.binding(defaults.preserveAltitudeInUnloadedChunk, () -> config.preserveAltitudeInUnloadedChunk, o -> config.preserveAltitudeInUnloadedChunk = o)
.controller(TickBoxControllerBuilder::create)
.build())

.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import ru.octol1ttle.flightassistant.alerts.api.BaseAlert;
import ru.octol1ttle.flightassistant.alerts.api.IECAMAlert;
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 @@ -26,8 +27,20 @@ public boolean isTriggered() {
@Override
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
Expand Up @@ -32,7 +32,7 @@ public int render(TextRenderer textRenderer, DrawContext context, int x, int y,
Identifier id = entry.getKey();
if (HudDisplayRegistry.isFaulted(id)) {
i += DrawHelper.drawText(textRenderer, context,
Text.translatable("alerts.flightassistant.fault.hud." + id), x, y,
Text.translatable("alerts.flightassistant.fault.hud." + id.getPath()), x, y,
FAConfig.indicator().cautionColor);
y += 10;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import ru.octol1ttle.flightassistant.alerts.api.BaseAlert;
import ru.octol1ttle.flightassistant.alerts.api.ICenteredAlert;
import ru.octol1ttle.flightassistant.computers.impl.AirDataComputer;
import ru.octol1ttle.flightassistant.computers.impl.safety.GPWSComputer;
import ru.octol1ttle.flightassistant.computers.impl.safety.GroundProximityComputer;
import ru.octol1ttle.flightassistant.config.FAConfig;
import ru.octol1ttle.flightassistant.registries.ComputerRegistry;

Expand All @@ -18,7 +18,7 @@ public class ExcessiveDescentAlert extends BaseAlert implements ICenteredAlert {
private static final float SINK_RATE_THRESHOLD = 7.5f;
private static final float PULL_UP_THRESHOLD = 5.0f;
private final AirDataComputer data = ComputerRegistry.resolve(AirDataComputer.class);
private final GPWSComputer gpws = ComputerRegistry.resolve(GPWSComputer.class);
private final GroundProximityComputer gpws = ComputerRegistry.resolve(GroundProximityComputer.class);

@Override
public boolean isTriggered() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import ru.octol1ttle.flightassistant.alerts.api.BaseAlert;
import ru.octol1ttle.flightassistant.alerts.api.ICenteredAlert;
import ru.octol1ttle.flightassistant.computers.impl.TimeComputer;
import ru.octol1ttle.flightassistant.computers.impl.safety.GPWSComputer;
import ru.octol1ttle.flightassistant.computers.impl.safety.GroundProximityComputer;
import ru.octol1ttle.flightassistant.config.FAConfig;
import ru.octol1ttle.flightassistant.registries.ComputerRegistry;

Expand All @@ -20,7 +20,7 @@ public class ExcessiveTerrainClosureAlert extends BaseAlert implements ICentered

private static final float PULL_UP_THRESHOLD = 5.0f;
private static final float DELAY_ALERT_FOR = 0.5f;
private final GPWSComputer gpws = ComputerRegistry.resolve(GPWSComputer.class);
private final GroundProximityComputer gpws = ComputerRegistry.resolve(GroundProximityComputer.class);
private final TimeComputer time = ComputerRegistry.resolve(TimeComputer.class);
private boolean delayFull = false;
private float delay = 0.0f;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@
import ru.octol1ttle.flightassistant.alerts.api.BaseAlert;
import ru.octol1ttle.flightassistant.alerts.api.ICenteredAlert;
import ru.octol1ttle.flightassistant.computers.impl.navigation.FlightPlanner;
import ru.octol1ttle.flightassistant.computers.impl.safety.GPWSComputer;
import ru.octol1ttle.flightassistant.computers.impl.safety.GroundProximityComputer;
import ru.octol1ttle.flightassistant.config.FAConfig;
import ru.octol1ttle.flightassistant.registries.ComputerRegistry;

public class UnsafeTerrainClearanceAlert extends BaseAlert implements ICenteredAlert {
private final GPWSComputer gpws = ComputerRegistry.resolve(GPWSComputer.class);
private final GroundProximityComputer gpws = ComputerRegistry.resolve(GroundProximityComputer.class);
private final FlightPlanner plan = ComputerRegistry.resolve(FlightPlanner.class);

@Override
public boolean isTriggered() {
return gpws.landingClearanceStatus == GPWSComputer.LandingClearanceStatus.TOO_LOW;
return gpws.landingClearanceStatus == GroundProximityComputer.LandingClearanceStatus.TOO_LOW;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@
import ru.octol1ttle.flightassistant.computers.impl.autoflight.FireworkController;
import ru.octol1ttle.flightassistant.computers.impl.autoflight.PitchController;
import ru.octol1ttle.flightassistant.computers.impl.autoflight.YawController;
import ru.octol1ttle.flightassistant.computers.impl.autoflight.pitch.ProtectionsPitchController;
import ru.octol1ttle.flightassistant.computers.impl.navigation.FlightPlanner;
import ru.octol1ttle.flightassistant.computers.impl.safety.AlertController;
import ru.octol1ttle.flightassistant.computers.impl.safety.ChunkStatusComputer;
import ru.octol1ttle.flightassistant.computers.impl.safety.ElytraStateController;
import ru.octol1ttle.flightassistant.computers.impl.safety.GPWSComputer;
import ru.octol1ttle.flightassistant.computers.impl.safety.GroundProximityComputer;
import ru.octol1ttle.flightassistant.computers.impl.safety.PitchLimitComputer;
import ru.octol1ttle.flightassistant.computers.impl.safety.StallComputer;
import ru.octol1ttle.flightassistant.computers.impl.safety.VoidLevelComputer;
import ru.octol1ttle.flightassistant.registries.ComputerRegistry;
Expand All @@ -30,14 +32,16 @@ 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 PitchController());
ComputerRegistry.register(new ProtectionsPitchController());
ComputerRegistry.register(new ChunkStatusComputer());
ComputerRegistry.register(new StallComputer());
ComputerRegistry.register(new VoidLevelComputer());
ComputerRegistry.register(new FlightPlanner());
ComputerRegistry.register(new GPWSComputer());
ComputerRegistry.register(new GroundProximityComputer());
ComputerRegistry.register(new ElytraStateController());
ComputerRegistry.register(new YawController());
ComputerRegistry.register(new PitchController());
ComputerRegistry.register(new AutoFlightComputer());
ComputerRegistry.register(new AlertController(mc.getSoundManager()));

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package ru.octol1ttle.flightassistant.computers.api;

import net.minecraft.util.Pair;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public interface IPitchController {
/**
* Gets the target pitch that this controller wants
* @return a pair of two floats: the first one is the target pitch, second is the delta time multiplier. If 'null', the controller doesn't want any pitch at this moment
*/
@Nullable
Pair<@NotNull Float, @NotNull Float> getTargetPitch();

Priority getPriority();

enum Priority {
HIGHEST(0),
HIGH(1),
NORMAL(2),
LOW(3),
LOWEST(4);

public final int priority;

Priority(int priority) {
this.priority = priority;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,16 @@
import ru.octol1ttle.flightassistant.computers.impl.AirDataComputer;
import ru.octol1ttle.flightassistant.computers.api.ITickableComputer;
import ru.octol1ttle.flightassistant.computers.impl.navigation.FlightPlanner;
import ru.octol1ttle.flightassistant.computers.impl.safety.GPWSComputer;
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 {
private final AirDataComputer data = ComputerRegistry.resolve(AirDataComputer.class);
private final GPWSComputer gpws = ComputerRegistry.resolve(GPWSComputer.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 PitchController pitch = ComputerRegistry.resolve(PitchController.class);
private final YawController yaw = ComputerRegistry.resolve(YawController.class);

public boolean flightDirectorsEnabled = false;
Expand Down Expand Up @@ -48,7 +47,6 @@ public void tick() {
}
}

pitch.targetPitch = autoPilotEnabled ? getTargetPitch() : null;
yaw.targetHeading = autoPilotEnabled ? getTargetHeading() : null;
}

Expand Down Expand Up @@ -109,7 +107,6 @@ public void reset() {
disconnectAutoFirework(true);
disconnectAutopilot(true);

pitch.targetPitch = null;
yaw.targetHeading = null;
}
}
Loading

0 comments on commit 86a120c

Please sign in to comment.