Skip to content

Commit

Permalink
why is this SO DIFFICULT
Browse files Browse the repository at this point in the history
Signed-off-by: Octol1ttle <l1ttleofficial@outlook.com>
  • Loading branch information
Octol1ttle committed Jun 15, 2024
1 parent 95dbd64 commit 24fee22
Show file tree
Hide file tree
Showing 15 changed files with 164 additions and 74 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,15 @@ public DaBRThrustHandler() {
}

@Override
public boolean canBeUsed() {
public boolean available() {
return ModConfig.INSTANCE.getEnableThrust();
}

@Override
public boolean isFireworkLike() {
return true;
}

@Override
public void reset() {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,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;
import ru.octol1ttle.flightassistant.registries.events.RegisterCustomComputersCallback;

public class ComputerHost {
public static ComputerHost instance() {
Expand All @@ -36,7 +36,6 @@ 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 ChunkStatusComputer());
Expand All @@ -54,9 +53,11 @@ public ComputerHost(@NotNull MinecraftClient mc) {
ComputerRegistry.register(new HeadingController());
ComputerRegistry.register(new RollController());

ComputerRegistry.register(new FireworkController(mc));

ComputerRegistry.register(new AlertController(mc.getSoundManager()));

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

public void tick() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,15 @@
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 classes should inject ThrustController and use {@link ThrustController#currentThrust} and {@link ThrustController#targetThrust} in {@link IThrustHandler#tickThrust()}.
* 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 is registered
*/
public interface IThrustHandler extends IComputer {
boolean canBeUsed();
default void tickThrust() {
}

boolean available();

boolean isFireworkLike();
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,67 +10,67 @@ public class FlightPhaseComputer implements ITickableComputer {
private final AirDataComputer data = ComputerRegistry.resolve(AirDataComputer.class);
private final AutoFlightController autoflight = ComputerRegistry.resolve(AutoFlightController.class);
private final FlightPlanner plan = ComputerRegistry.resolve(FlightPlanner.class);
public FlightPhase phase = FlightPhase.UNKNOWN;
public Phase phase = Phase.UNKNOWN;

@SuppressWarnings("DataFlowIssue")
@Override
public void tick() {
if (data.player().isOnGround()) {
phase = FlightPhase.ON_GROUND;
phase = Phase.ON_GROUND;
}

if (!data.isFlying()) {
return;
}

if (phase == FlightPhase.ON_GROUND) {
phase = FlightPhase.TAKEOFF;
if (phase == Phase.ON_GROUND) {
phase = Phase.TAKEOFF;
}

if (phase == FlightPhase.TAKEOFF && data.heightAboveGround() <= 10.0f) {
if (phase == Phase.TAKEOFF && data.heightAboveGround() <= 15.0f) {
return;
}

Integer cruiseAltitude = plan.getCruiseAltitude();
Integer targetAltitude = autoflight.getTargetAltitude();
if (cruiseAltitude == null || targetAltitude == null) {
phase = FlightPhase.UNKNOWN;
phase = Phase.UNKNOWN;
return;
}

if (!isNearDestination()) {
if (data.altitude() - targetAltitude <= 5.0f) {
phase = FlightPhase.CLIMB;
phase = Phase.CLIMB;
} else {
phase = FlightPhase.DESCENT;
phase = Phase.DESCENT;
}

if (targetAltitude.equals(cruiseAltitude) && Math.abs(cruiseAltitude - data.altitude()) <= 5.0f) {
phase = FlightPhase.CRUISE;
phase = Phase.CRUISE;
}
}

if (phase == FlightPhase.GO_AROUND) {
if (phase == Phase.GO_AROUND) {
if (plan.getDistanceToWaypoint() > 150.0f) {
phase = FlightPhase.APPROACH;
phase = Phase.APPROACH;
}
} else {
if (plan.isOnApproach()) {
phase = FlightPhase.APPROACH;
phase = Phase.APPROACH;
}

if (phase == FlightPhase.APPROACH && plan.autolandAllowed) {
phase = FlightPhase.LAND;
if (phase == Phase.APPROACH && plan.autolandAllowed) {
phase = Phase.LAND;
}
}
}

private boolean isAboutToLand() {
return phase == FlightPhase.APPROACH || phase == FlightPhase.LAND;
return phase == Phase.APPROACH || phase == Phase.LAND;
}

public boolean isNearDestination() {
return isAboutToLand() || phase == FlightPhase.GO_AROUND;
return isAboutToLand() || phase == Phase.GO_AROUND;
}

@Override
Expand All @@ -80,10 +80,10 @@ public String getId() {

@Override
public void reset() {
phase = FlightPhase.UNKNOWN;
phase = Phase.UNKNOWN;
}

public enum FlightPhase {
public enum Phase {
ON_GROUND("on_ground"),
TAKEOFF("takeoff"),
CLIMB("climb"),
Expand All @@ -96,11 +96,11 @@ public enum FlightPhase {

public final Text text;

FlightPhase(Text text) {
Phase(Text text) {
this.text = text;
}

FlightPhase(String key) {
Phase(String key) {
this(Text.translatable("status.flightassistant.phase." + key));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import net.minecraft.text.Text;
import net.minecraft.util.math.MathHelper;
import org.apache.commons.lang3.NotImplementedException;
import org.jetbrains.annotations.Nullable;
import org.joml.Vector2d;
import ru.octol1ttle.flightassistant.FAMathHelper;
Expand All @@ -17,45 +16,102 @@
import ru.octol1ttle.flightassistant.computers.api.ThrustControlInput;
import ru.octol1ttle.flightassistant.computers.impl.AirDataComputer;
import ru.octol1ttle.flightassistant.computers.impl.FlightPhaseComputer;
import ru.octol1ttle.flightassistant.computers.impl.FlightPhaseComputer.Phase;
import ru.octol1ttle.flightassistant.computers.impl.navigation.FlightPlanner;
import ru.octol1ttle.flightassistant.registries.ComputerRegistry;

public class AutopilotComputer implements ITickableComputer, IAutopilotProvider, IPitchController, IHeadingController, IRollController, IThrustController {
private static final float THRUST_CLIMB = 0.9f;
private static final float THRUST_CLIMB_REDUCED = 0.75f;

private final AirDataComputer data = ComputerRegistry.resolve(AirDataComputer.class);
private final AutoFlightController autoflight = ComputerRegistry.resolve(AutoFlightController.class);
private final FlightPhaseComputer phase = ComputerRegistry.resolve(FlightPhaseComputer.class);
private final FlightPlanner plan = ComputerRegistry.resolve(FlightPlanner.class);
private final ThrustController thrust = ComputerRegistry.resolve(ThrustController.class);

public Text verticalMode;
public Text lateralMode;
public Text thrustMode;

private Float targetPitch;
private Float targetHeading;
private Float targetThrust;

private Float togaHeading;

@Override
public void tick() {
if (phase.phase == FlightPhaseComputer.FlightPhase.TAKEOFF
|| phase.phase == FlightPhaseComputer.FlightPhase.GO_AROUND && data.heightAboveGround() < 15.0f) {
if (phase.phase == Phase.TAKEOFF
|| phase.phase == Phase.GO_AROUND && data.heightAboveGround() < 15.0f) {
if (togaHeading == null) {
togaHeading = data.heading();
}

setTargetThrust(1.0f, Text.translatable("mode.flightassistant.thrust.toga"));
setTargetPitch(PitchController.CLIMB_PITCH, Text.translatable("mode.flightassistant.vert.climb.optimum"));
setTargetHeading(data.heading(), Text.translatable("mode.flightassistant.lat.current"));
setTargetPitch(55.0f, Text.translatable("mode.flightassistant.vert.climb.optimum"));

String lat = phase.phase == Phase.TAKEOFF ? ".takeoff" : ".go_around";
setTargetHeading(togaHeading, Text.translatable("mode.flightassistant.lat" + lat, togaHeading.intValue()));

return;
}

togaHeading = null;
setTargetHeading(null, Text.empty());
tickLateral();

Integer targetSpeed = autoflight.getTargetSpeed();
setTargetThrust(null, Text.empty());

setTargetPitch(null, Text.empty());
Integer targetAltitude = autoflight.getTargetAltitude();
if (targetAltitude == null) {
targetPitch = null;
targetThrust = null;
return;
}

boolean useThrustConservatively = thrust.thrustHandler instanceof FireworkController || !thrust.thrustHandler.canBeUsed();
// TODO
throw new NotImplementedException();
float diff = targetAltitude - data.altitude();

boolean useReducedThrust = thrust.getThrustHandler().isFireworkLike(); // I wish I didn't have to account for this
float climbThrust = useReducedThrust ? THRUST_CLIMB_REDUCED : THRUST_CLIMB;
String thrustSuffix = useReducedThrust ? "_reduced" : "";

if (autoflight.selectedAltitude != null) {
float speedAdjustment = targetSpeed != null ? data.speed() - targetSpeed : 0.0f;
if (targetAltitude >= data.altitude()) {
setTargetThrust(climbThrust, Text.translatable("mode.flightassistant.thrust.climb" + thrustSuffix));
float pitch;
if (useReducedThrust) {
pitch = 47.5f - 47.5f * (Math.max(20.0f - diff, 0.0f) / 15.0f) + 7.5f;
} else {
pitch = 55.0f - 55.0f * (Math.max(15.0f - diff, 0.0f) / 15.0f) + speedAdjustment;
}
setTargetPitch(pitch, Text.translatable("mode.flightassistant.vert.climb.selected"));
} else {
setTargetThrust(climbThrust, Text.translatable("mode.flightassistant.thrust.descend" + thrustSuffix));
}
}

if (targetAltitude > data.altitude()) {

}

// overwrite any thrust setting if we have a target speed
tickSpeed(targetSpeed);
}

private void tickSpeed(Integer targetSpeed) {
if (targetSpeed == null) {
return;
}

float diff = Math.abs(targetSpeed - data.speed());

float thr = targetSpeed > data.speed() ? THRUST_CLIMB : -0.5f;
if (thrust.getThrustHandler().isFireworkLike()) {
thr = targetSpeed / FireworkController.FIREWORK_SPEED;
}
setTargetThrust(thr * Math.min(diff * 0.1f, 1.0f), Text.translatable("mode.flightassistant.thrust.speed.selected", targetSpeed));
}

private void tickLateral() {
Expand All @@ -75,7 +131,7 @@ private Float computeTargetPitch() {
Vector2d planPos = plan.getTargetPosition();

float diff = targetAltitude - data.altitude();
boolean landing = phase.phase == FlightPhaseComputer.FlightPhase.LAND;
boolean landing = phase.phase == Phase.LAND;
if (!landing && diff > -10.0f && diff < 5.0f) {
return (PitchController.GLIDE_PITCH + PitchController.ALTITUDE_PRESERVE_PITCH) * 0.5f;
}
Expand Down Expand Up @@ -188,16 +244,19 @@ public ThrustControlInput getThrustInput() {

@Override
public String getId() {
return "autopilot_ctl";
return "autopilot";
}

@Override
public void reset() {
targetPitch = null;
verticalMode = null;
targetHeading = null;
lateralMode = null;
thrustMode = null;

targetPitch = null;
targetHeading = null;
targetThrust = null;
togaHeading = null;

autoflight.disconnectAutoFirework(true);
autoflight.disconnectAutopilot(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,12 @@ public void tick() {

i++;
}
}


if (thrust.thrustHandler == this || !thrust.thrustHandler.canBeUsed()) {
if (data.speed() / FIREWORK_SPEED < thrust.currentThrust) {
activateFirework(false);
}
@Override
public void tickThrust() {
if (data.speed() / FIREWORK_SPEED < thrust.currentThrust) {
activateFirework(false);
}
}

Expand Down Expand Up @@ -140,10 +140,15 @@ public boolean isFireworkSafe(ItemStack stack) {
}

@Override
public boolean canBeUsed() {
public boolean available() {
return true;
}

@Override
public boolean isFireworkLike() {
return false;
}

@Override
public String getId() {
return "frwk_ctl";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
import ru.octol1ttle.flightassistant.registries.events.AllowComputerRegisterCallback;

public class HeadingController implements ITickableComputer, IAutopilotProvider {
private static final List<IHeadingController> controllers = new ArrayList<>();
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() {
static {
AllowComputerRegisterCallback.EVENT.register((computer -> {
if (computer instanceof IHeadingController controller) {
controllers.add(controller);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ public class PitchController implements ITickableComputer, INormalLawProvider {
public static final float ALTITUDE_PRESERVE_PITCH = 15.0f;
public static final float GLIDE_PITCH = -2.2f;
public static final float DESCEND_PITCH = -35.0f;
private final List<IPitchController> controllers = new ArrayList<>();
private static final List<IPitchController> controllers = new ArrayList<>();
private final AirDataComputer data = ComputerRegistry.resolve(AirDataComputer.class);
private final TimeComputer time = ComputerRegistry.resolve(TimeComputer.class);
private final PitchLimitComputer limit = ComputerRegistry.resolve(PitchLimitComputer.class);

public PitchController() {
static {
AllowComputerRegisterCallback.EVENT.register((computer -> {
if (computer instanceof IPitchController controller) {
controllers.add(controller);
Expand Down
Loading

0 comments on commit 24fee22

Please sign in to comment.