Skip to content

Commit

Permalink
feat: begin work on custom thrust handlers
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 d451d4d commit c4ec750
Show file tree
Hide file tree
Showing 29 changed files with 259 additions and 78 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ public static void setup() {
}

while (toggleAutoFirework.wasPressed()) {
if (!autoflight.autoFireworkEnabled) {
autoflight.autoFireworkEnabled = true;
if (!autoflight.autoThrustEnabled) {
autoflight.autoThrustEnabled = true;
} else {
autoflight.disconnectAutoFirework(false);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
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.impl.autoflight.AutoFlightComputer;
import ru.octol1ttle.flightassistant.config.FAConfig;
import ru.octol1ttle.flightassistant.registries.ComputerRegistry;
Expand All @@ -21,18 +21,19 @@ public AutoFireworkOffAlert() {

@Override
public boolean isTriggered() {
return !autoflight.autoFireworkEnabled;
return !autoflight.autoThrustEnabled;
}

@Override
public @NotNull AlertSoundData getSoundData() {
return autoflight.afrwkDisconnectionForced ? AlertSoundData.MASTER_CAUTION : AlertSoundData.EMPTY;
return autoflight.athrDisconnectionForced ? AlertSoundData.MASTER_CAUTION : AlertSoundData.EMPTY;
}

@Override
public int render(TextRenderer textRenderer, DrawContext context, int x, int y, boolean highlight) {
return DrawHelper.drawHighlightedText(textRenderer, context, Text.translatable("alerts.flightassistant.autoflight.auto_firework_off"), x, y,
FAConfig.indicator().cautionColor,
highlight && autoflight.afrwkDisconnectionForced);
highlight && autoflight.athrDisconnectionForced
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package ru.octol1ttle.flightassistant.compatibility.doabarrelroll;

import nl.enjarai.doabarrelroll.api.event.ThrustEvents;
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.ThrustController;
import ru.octol1ttle.flightassistant.registries.ComputerRegistry;

public class DaBRThrustHandler implements IThrustHandler {
private final AutoFlightComputer autoflight = ComputerRegistry.resolve(AutoFlightComputer.class);
private final ThrustController thrust = ComputerRegistry.resolve(ThrustController.class);
private final TimeComputer time = ComputerRegistry.resolve(TimeComputer.class);

public DaBRThrustHandler() {
ThrustEvents.MODIFY_THRUST_INPUT.register(v -> {
if (Math.abs(v) > 0.001f) {
autoflight.disconnectAutoFirework(true);
}
return thrust.targetThrust += (float) (v * time.deltaTime * 0.5f);
});
}

@Override
public void reset() {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import ru.octol1ttle.flightassistant.computers.impl.autoflight.HeadingController;
import ru.octol1ttle.flightassistant.computers.impl.autoflight.PitchController;
import ru.octol1ttle.flightassistant.computers.impl.autoflight.RollController;
import ru.octol1ttle.flightassistant.computers.impl.autoflight.ThrustController;
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,8 +37,8 @@ public ComputerHost(@NotNull MinecraftClient mc) {
ComputerRegistry.register(new TimeComputer(mc));
ComputerRegistry.register(new FireworkController(mc));
ComputerRegistry.register(new PitchLimitComputer());
ComputerRegistry.register(new PitchController());
ComputerRegistry.register(new FlightProtectionsComputer());
ComputerRegistry.register(new PitchController());
ComputerRegistry.register(new ChunkStatusComputer());
ComputerRegistry.register(new StallComputer());
ComputerRegistry.register(new VoidLevelComputer());
Expand All @@ -49,6 +50,7 @@ public ComputerHost(@NotNull MinecraftClient mc) {
ComputerRegistry.register(new AutoFlightComputer());
ComputerRegistry.register(new FlightPhaseComputer());
ComputerRegistry.register(new AutopilotControlComputer());
ComputerRegistry.register(new ThrustController());
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,7 @@
public interface IHeadingController extends IComputer {
/**
* Gets the target heading that this controller wants
* @return a {@link ControlInput} with {@link ControlInput#target} being the target heading
* @return a {@link ControlInput} with {@link ControlInput#target()} being the target heading
*/
@Nullable
ControlInput getHeadingInput();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
public interface IPitchController extends IComputer {
/**
* Gets the target pitch that this controller wants
* @return a {@link ControlInput} with {@link ControlInput#target} being the target pitch
* @return a {@link ControlInput} with {@link ControlInput#target()} being the target pitch
*/
@Nullable
ControlInput getPitchInput();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ public interface IRollController extends IComputer {
/**
* Gets the target roll that this controller wants
*
* @return a {@link ControlInput} with {@link ControlInput#target} being the target roll
* @return a {@link ControlInput} with {@link ControlInput#target()} being the target roll
*/
@Nullable
ControlInput getRollInput();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package ru.octol1ttle.flightassistant.computers.api;

public interface IThrustController extends IComputer {
/**
* Gets the target thrust that this controller wants
*
* @return a {@link ThrustControlInput} with {@link ThrustControlInput#target()} being the target thrust
*/
ThrustControlInput getThrustInput();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package ru.octol1ttle.flightassistant.computers.api;

import ru.octol1ttle.flightassistant.computers.impl.autoflight.ThrustController;

/**
* 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
*/
public interface IThrustHandler extends IComputer {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package ru.octol1ttle.flightassistant.computers.api;

public record ThrustControlInput(float target, float deltaTimeMultiplier, InputPriority priority) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ public class AutoFlightComputer implements ITickableComputer, IAutopilotProvider
private final FlightProtectionsComputer prot = ComputerRegistry.resolve(FlightProtectionsComputer.class);

public boolean flightDirectorsEnabled = false;
public boolean autoFireworkEnabled = false;
public boolean autoThrustEnabled = false;
public boolean autoPilotEnabled = false;

public boolean afrwkDisconnectionForced = false;
public boolean athrDisconnectionForced = false;
public boolean apDisconnectionForced = false;

public Integer selectedSpeed;
Expand All @@ -31,7 +31,7 @@ public class AutoFlightComputer implements ITickableComputer, IAutopilotProvider

@Override
public void tick() {
if (autoFireworkEnabled && data.isCurrentChunkLoaded && gpws.fireworkUseSafe && gpws.getGPWSLampColor() == FAConfig.indicator().frameColor) {
if (autoThrustEnabled && data.isCurrentChunkLoaded && gpws.fireworkUseSafe && gpws.getGPWSLampColor() == FAConfig.indicator().frameColor) {
Integer targetSpeed = getTargetSpeed();
Integer targetAltitude = getTargetAltitude();
if (targetSpeed != null) {
Expand Down Expand Up @@ -73,9 +73,9 @@ public void disconnectAutopilot(boolean force) {
}

public void disconnectAutoFirework(boolean force) {
if (autoFireworkEnabled) {
autoFireworkEnabled = false;
afrwkDisconnectionForced = force;
if (autoThrustEnabled) {
autoThrustEnabled = false;
athrDisconnectionForced = force;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,19 @@
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.api.IThrustHandler;
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.registries.ComputerRegistry;

public class FireworkController implements ITickableComputer, IAutopilotProvider {
public class FireworkController implements ITickableComputer, IThrustHandler {
public static final float FIREWORK_SPEED = 33.62f;

private final MinecraftClient mc;
private final AirDataComputer data = ComputerRegistry.resolve(AirDataComputer.class);
private final TimeComputer time = ComputerRegistry.resolve(TimeComputer.class);
private final ThrustController thrust = ComputerRegistry.resolve(ThrustController.class);

public int safeFireworkCount = Integer.MAX_VALUE;
public boolean fireworkResponded = true;
Expand Down Expand Up @@ -52,6 +55,10 @@ public void tick() {

i++;
}

if (data.speed() / FIREWORK_SPEED < thrust.currentThrust) {
activateFirework(false);
}
}

private int countSafeFireworks() {
Expand All @@ -68,7 +75,7 @@ private int countSafeFireworks() {
return i;
}

public void activateFirework(boolean force) {
private void activateFirework(boolean force) {
if (!data.canAutomationsActivate() || lastUseTime > 0 && time.millis != null && time.millis - lastUseTime < 1000) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,19 @@
import ru.octol1ttle.flightassistant.computers.impl.AirDataComputer;
import ru.octol1ttle.flightassistant.computers.impl.TimeComputer;
import ru.octol1ttle.flightassistant.registries.ComputerRegistry;
import ru.octol1ttle.flightassistant.registries.events.ComputerRegisteredCallback;
import ru.octol1ttle.flightassistant.registries.events.AllowComputerRegisterCallback;

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<>();

public HeadingController() {
ComputerRegisteredCallback.EVENT.register((computer -> {
AllowComputerRegisterCallback.EVENT.register((computer -> {
if (computer instanceof IHeadingController controller) {
controllers.add(controller);
}
return true;
}));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import ru.octol1ttle.flightassistant.computers.impl.TimeComputer;
import ru.octol1ttle.flightassistant.computers.impl.safety.PitchLimitComputer;
import ru.octol1ttle.flightassistant.registries.ComputerRegistry;
import ru.octol1ttle.flightassistant.registries.events.ComputerRegisteredCallback;
import ru.octol1ttle.flightassistant.registries.events.AllowComputerRegisterCallback;

public class PitchController implements ITickableComputer, INormalLawProvider {
public static final float CLIMB_PITCH = 55.0f;
Expand All @@ -26,10 +26,11 @@ public class PitchController implements ITickableComputer, INormalLawProvider {
private final PitchLimitComputer limit = ComputerRegistry.resolve(PitchLimitComputer.class);

public PitchController() {
ComputerRegisteredCallback.EVENT.register((computer -> {
AllowComputerRegisterCallback.EVENT.register((computer -> {
if (computer instanceof IPitchController controller) {
controllers.add(controller);
}
return true;
}));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import ru.octol1ttle.flightassistant.computers.impl.AirDataComputer;
import ru.octol1ttle.flightassistant.computers.impl.TimeComputer;
import ru.octol1ttle.flightassistant.registries.ComputerRegistry;
import ru.octol1ttle.flightassistant.registries.events.ComputerRegisteredCallback;
import ru.octol1ttle.flightassistant.registries.events.AllowComputerRegisterCallback;
import ru.octol1ttle.flightassistant.registries.events.CustomComputerRegistrationCallback;

public class RollController implements ITickableComputer, INormalLawProvider {
Expand All @@ -32,18 +32,20 @@ public RollController() {
ComputerRegistry.register(new DaBRRollHandler());
}
});
ComputerRegisteredCallback.EVENT.register(computer -> {
AllowComputerRegisterCallback.EVENT.register(computer -> {
if (computer instanceof IRollController controller) {
controllers.add(controller);
}
if (computer instanceof IRollHandler handler) {
if (rollHandler != null) {
FlightAssistant.LOGGER.warn("Multiple roll handlers found! Discarding handler %s".formatted(handler.getClass().getName()));
FlightAssistant.LOGGER.error("Multiple roll handlers found! Discarding handler %s".formatted(handler.getClass().getName()));
return false;
} else {
rollHandler = handler;
FlightAssistant.LOGGER.info("Active roll handler is %s".formatted(rollHandler.getClass().getName()));
}
}
return true;
});
}

Expand Down Expand Up @@ -111,4 +113,4 @@ public String getId() {
@Override
public void reset() {
}
}
}
Loading

0 comments on commit c4ec750

Please sign in to comment.