diff --git a/build.gradle b/build.gradle index 3041eb90..14f93bf8 100644 --- a/build.gradle +++ b/build.gradle @@ -1,5 +1,5 @@ plugins { - id 'fabric-loom' version '1.2-SNAPSHOT' + id 'fabric-loom' version '1.4-SNAPSHOT' id 'maven-publish' } diff --git a/gradle.properties b/gradle.properties index 72a7c8a8..25c904b3 100644 --- a/gradle.properties +++ b/gradle.properties @@ -14,6 +14,6 @@ loader_version=0.14.7 fabric_version=0.83.0 immediatelyfast_version=1.2.0 # Mod Properties -mod_version=1.20.1-fabric-8 +mod_version=2.0.0-alpha.1+mc1.20.1 maven_group=net.torocraft archives_base_name=flighthud diff --git a/src/main/java/net/torocraft/flighthud/AutoFlightManager.java b/src/main/java/net/torocraft/flighthud/AutoFlightManager.java deleted file mode 100644 index a8a4441a..00000000 --- a/src/main/java/net/torocraft/flighthud/AutoFlightManager.java +++ /dev/null @@ -1,106 +0,0 @@ -package net.torocraft.flighthud; - -import net.minecraft.client.MinecraftClient; -import net.minecraft.entity.player.PlayerEntity; -import net.minecraft.util.Util; -import net.minecraft.util.math.MathHelper; - -import static net.torocraft.flighthud.FlightSafetyMonitor.correctThreshold; -import static net.torocraft.flighthud.FlightSafetyMonitor.gpwsLampColor; -import static net.torocraft.flighthud.FlightSafetyMonitor.lastFireworkActivationTimeMs; -import static net.torocraft.flighthud.FlightSafetyMonitor.maximumSafePitch; -import static net.torocraft.flighthud.FlightSafetyMonitor.secondsUntilTerrainImpact; -import static net.torocraft.flighthud.FlightSafetyMonitor.thrustLocked; -import static net.torocraft.flighthud.FlightSafetyMonitor.thrustSet; -import static net.torocraft.flighthud.FlightSafetyMonitor.usableFireworkHand; -import static net.torocraft.flighthud.HudComponent.CONFIG; -import static net.torocraft.flighthud.HudComponent.wrapHeading; - -public class AutoFlightManager { - public static Long lastUpdateTimeMs = null; - public static Float deltaTime; - - public static boolean flightDirectorsEnabled = false; - public static boolean autoThrustEnabled = false; - public static boolean autoPilotEnabled = false; - - public static Integer destinationX = null; - public static Integer destinationZ = null; - public static Integer targetAltitude = null; - - public static Float targetPitch = null; - public static Float targetYaw = null; - public static Float targetHeading = null; - public static Double distanceToTarget = null; - - public static String statusString = ""; - - public static void update(MinecraftClient mc, FlightComputer computer) { - statusString = ""; - long currentTimeMs = Util.getMeasuringTimeMs(); - if (lastUpdateTimeMs != null) - deltaTime = Math.min(1, (currentTimeMs - lastUpdateTimeMs) * 0.001f); - else - deltaTime = 1f / mc.options.getMaxFps().getValue(); - lastUpdateTimeMs = currentTimeMs; - - if (CONFIG == null || mc.player == null || !mc.player.isFallFlying() || mc.interactionManager == null) - return; - boolean approachingDestination = distanceToTarget != null && distanceToTarget < Math.max(40, computer.velocityPerSecond.horizontalLength()); - - if (computer.speed > 30) thrustSet = true; - - if (autoThrustEnabled && usableFireworkHand != null) { - if (!thrustLocked && gpwsLampColor == CONFIG.color && computer.velocityPerSecond.horizontalLength() > 0.01 && computer.pitch > (autoPilotEnabled ? 0 : 10) && !approachingDestination) { - if (thrustSet && (computer.speed < 28 || computer.velocityPerSecond.y < -8)) { - mc.interactionManager.interactItem(mc.player, usableFireworkHand); - lastFireworkActivationTimeMs = lastUpdateTimeMs; - } - statusString += "THR MCT"; - } else statusString += "THR IDLE"; - } - - targetPitch = null; - if (AutoFlightManager.autoPilotEnabled && (secondsUntilTerrainImpact <= correctThreshold || computer.velocityPerSecond.horizontalLength() < 0.01)) { - targetPitch = -maximumSafePitch; - statusString += "".equals(statusString) ? "GPWS" : " | GPWS"; - } else if (approachingDestination || FlightSafetyMonitor.fireworkCount <= 0 || (AutoFlightManager.autoThrustEnabled && usableFireworkHand == null)) { - targetPitch = 2.2f; - statusString += "".equals(statusString) ? "OPT GLD" : " | OPT GLD"; - } else if (targetAltitude != null) { - float pitchLimiter = (float) (Math.abs(targetAltitude - mc.player.getY()) + computer.velocityPerSecond.length()); - targetPitch = (float) Math.max(-maximumSafePitch, Math.toDegrees(-Math.asin((targetAltitude - mc.player.getY()) / pitchLimiter))); - statusString += "".equals(statusString) ? "ALT" : " | ALT"; - } - - - if (destinationX != null && destinationZ != null) { - targetYaw = (float) Math.toDegrees(Math.atan2(-(destinationX - mc.player.getX()), destinationZ - mc.player.getZ())); - targetHeading = (targetYaw + 180) % 360; - distanceToTarget = Math.sqrt(mc.player.getPos().squaredDistanceTo(destinationX, mc.player.getY(), destinationZ)); - statusString += "".equals(statusString) ? "NAV" : " | NAV"; - } else { - targetYaw = null; - targetHeading = null; - distanceToTarget = null; - } - - if (autoPilotEnabled) { - float deltaHeading = targetHeading != null ? wrapHeading(targetHeading) - wrapHeading(computer.heading) : 0.0f; - if (deltaHeading < -180) { - deltaHeading += 360; - } - - changeLookDirection(mc.player, targetPitch != null ? (targetPitch + computer.pitch) * deltaTime : 0.0f, - deltaHeading * deltaTime * 1.25f); - statusString += "".equals(statusString) ? "A/P" : " | A/P"; - } - } - - public static void changeLookDirection(PlayerEntity player, float pitch, float yaw) { - player.setPitch(MathHelper.clamp(player.getPitch() + pitch, -90.0F, 90.0F)); - player.setYaw(player.getYaw() + yaw); - player.prevPitch = MathHelper.clamp(player.prevPitch + pitch, -90.0F, 90.0F); - player.prevYaw += yaw; - } -} diff --git a/src/main/java/net/torocraft/flighthud/FlightHud.java b/src/main/java/net/torocraft/flighthud/FlightHud.java index f2babde3..e0b87a32 100644 --- a/src/main/java/net/torocraft/flighthud/FlightHud.java +++ b/src/main/java/net/torocraft/flighthud/FlightHud.java @@ -1,21 +1,13 @@ package net.torocraft.flighthud; -import com.mojang.brigadier.arguments.IntegerArgumentType; import com.mojang.brigadier.tree.LiteralCommandNode; import net.fabricmc.api.ClientModInitializer; import net.fabricmc.fabric.api.client.command.v2.ClientCommandRegistrationCallback; import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource; import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents; -import net.fabricmc.fabric.api.client.keybinding.v1.KeyBindingHelper; import net.minecraft.client.option.KeyBinding; import net.minecraft.client.util.InputUtil; -import net.torocraft.flighthud.alerts.Alert; -import net.torocraft.flighthud.commands.AltitudeResetCommand; -import net.torocraft.flighthud.commands.AltitudeSelectCommand; -import net.torocraft.flighthud.commands.DestinationResetCommand; -import net.torocraft.flighthud.commands.DestinationSelectCommand; import net.torocraft.flighthud.commands.SwitchDisplayModeCommand; -import net.torocraft.flighthud.components.FlightStatusIndicator; import net.torocraft.flighthud.config.HudConfig; import net.torocraft.flighthud.config.SettingsConfig; import net.torocraft.flighthud.config.loader.ConfigLoader; @@ -23,7 +15,6 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import static net.fabricmc.fabric.api.client.command.v2.ClientCommandManager.argument; import static net.fabricmc.fabric.api.client.command.v2.ClientCommandManager.literal; public class FlightHud implements ClientModInitializer { @@ -52,91 +43,16 @@ public class FlightHud implements ClientModInitializer { config -> FlightHud.CONFIG_MIN = config); private static KeyBinding keyBinding; - private static KeyBinding hideAlertSwitch; - private static KeyBinding showAlertSwitch; - private static KeyBinding flightLawSwitch; - private static KeyBinding gpwsSwitch; - private static KeyBinding fdSwitch; - private static KeyBinding aThrSwitch; - private static KeyBinding apSwitch; private static void setupKeycCode() { keyBinding = new KeyBinding("key.flighthud.toggleDisplayMode", InputUtil.Type.KEYSYM, GLFW.GLFW_KEY_GRAVE_ACCENT, "category.flighthud.toggleDisplayMode"); - hideAlertSwitch = new KeyBinding("key.flighthud.hideAlertSwitch", InputUtil.Type.KEYSYM, - GLFW.GLFW_KEY_KP_0, "category.flighthud.toggleDisplayMode"); - - showAlertSwitch = new KeyBinding("key.flighthud.showAlertSwitch", InputUtil.Type.KEYSYM, - GLFW.GLFW_KEY_KP_DECIMAL, "category.flighthud.toggleDisplayMode"); - - flightLawSwitch = new KeyBinding("key.flighthud.flightLawSwitch", InputUtil.Type.KEYSYM, - GLFW.GLFW_KEY_KP_ENTER, "category.flighthud.toggleDisplayMode"); - - gpwsSwitch = new KeyBinding("key.flighthud.gpwsSwitch", InputUtil.Type.KEYSYM, - GLFW.GLFW_KEY_KP_ADD, "category.flighthud.toggleDisplayMode"); - - fdSwitch = new KeyBinding("key.flighthud.fdSwitch", InputUtil.Type.KEYSYM, - GLFW.GLFW_KEY_KP_1, "category.flighthud.toggleDisplayMode"); - - aThrSwitch = new KeyBinding("key.flighthud.aThrSwitch", InputUtil.Type.KEYSYM, - GLFW.GLFW_KEY_KP_2, "category.flighthud.toggleDisplayMode"); - - apSwitch = new KeyBinding("key.flighthud.apSwitch", InputUtil.Type.KEYSYM, - GLFW.GLFW_KEY_KP_3, "category.flighthud.toggleDisplayMode"); - - KeyBindingHelper.registerKeyBinding(keyBinding); - KeyBindingHelper.registerKeyBinding(hideAlertSwitch); - KeyBindingHelper.registerKeyBinding(showAlertSwitch); - KeyBindingHelper.registerKeyBinding(flightLawSwitch); - KeyBindingHelper.registerKeyBinding(gpwsSwitch); - KeyBindingHelper.registerKeyBinding(fdSwitch); - KeyBindingHelper.registerKeyBinding(aThrSwitch); - KeyBindingHelper.registerKeyBinding(apSwitch); ClientTickEvents.END_CLIENT_TICK.register(client -> { while (keyBinding.wasPressed()) { CONFIG_SETTINGS.toggleDisplayMode(); } - - while (hideAlertSwitch.wasPressed()) { - for (Alert alert : FlightStatusIndicator.activeAlerts) { - if (alert.hidden) continue; - alert.hidden = true; - break; - } - } - - while (showAlertSwitch.wasPressed()) { - for (int i = FlightStatusIndicator.activeAlerts.size() - 1; i >= 0; i--) { - Alert alert = FlightStatusIndicator.activeAlerts.get(i); - if (!alert.hidden) continue; - alert.hidden = false; - break; - } - } - - while (flightLawSwitch.wasPressed()) { - FlightSafetyMonitor.flightProtectionsEnabled = !FlightSafetyMonitor.flightProtectionsEnabled; - } - - while (gpwsSwitch.wasPressed()) { - CONFIG_SETTINGS.gpws = !CONFIG_SETTINGS.gpws; - } - - while (fdSwitch.wasPressed()) { - AutoFlightManager.flightDirectorsEnabled = !AutoFlightManager.flightDirectorsEnabled; - } - - while (aThrSwitch.wasPressed()) { - AutoFlightManager.autoThrustEnabled = !AutoFlightManager.autoThrustEnabled; - if (!AutoFlightManager.autoThrustEnabled) - FlightSafetyMonitor.thrustLocked = false; - } - - while (apSwitch.wasPressed()) { - AutoFlightManager.autoPilotEnabled = !AutoFlightManager.autoPilotEnabled; - } }); } @@ -145,17 +61,7 @@ private static void setupCommand() { LiteralCommandNode node = dispatcher.register(literal("flighthud") .then(literal("toggle").executes(new SwitchDisplayModeCommand())) - .then(literal("nav") - .then(argument("destinationX", IntegerArgumentType.integer(-30_000_000, 30_000_000)) - .then(argument("destinationZ", IntegerArgumentType.integer(-30_000_000, 30_000_000)) - .executes(new DestinationSelectCommand()))) - .then(literal("reset") - .executes(new DestinationResetCommand()))) - .then(literal("alt") - .then(argument("targetAltitude", IntegerArgumentType.integer(0, 640)) - .executes(new AltitudeSelectCommand())) - .then(literal("reset") - .executes(new AltitudeResetCommand())))); + ); dispatcher.register(literal("fhud").redirect(node)); dispatcher.register(literal("fh").redirect(node)); }); diff --git a/src/main/java/net/torocraft/flighthud/FlightSafetyMonitor.java b/src/main/java/net/torocraft/flighthud/FlightSafetyMonitor.java deleted file mode 100644 index eb597bb9..00000000 --- a/src/main/java/net/torocraft/flighthud/FlightSafetyMonitor.java +++ /dev/null @@ -1,182 +0,0 @@ -package net.torocraft.flighthud; - -import it.unimi.dsi.fastutil.objects.ObjectArrayList; -import java.util.List; -import net.minecraft.client.MinecraftClient; -import net.minecraft.entity.Entity; -import net.minecraft.entity.player.PlayerEntity; -import net.minecraft.item.FireworkRocketItem; -import net.minecraft.item.ItemStack; -import net.minecraft.item.Items; -import net.minecraft.nbt.NbtCompound; -import net.minecraft.util.Hand; -import net.minecraft.util.hit.BlockHitResult; -import net.minecraft.util.hit.HitResult; -import net.minecraft.util.math.Direction; -import net.minecraft.util.math.Vec3d; -import net.minecraft.world.RaycastContext; - -import static net.torocraft.flighthud.AutoFlightManager.changeLookDirection; -import static net.torocraft.flighthud.AutoFlightManager.deltaTime; -import static net.torocraft.flighthud.FlightHud.CONFIG_SETTINGS; -import static net.torocraft.flighthud.HudComponent.CONFIG; - -public class FlightSafetyMonitor { - public static List unsafeFireworkHands = new ObjectArrayList<>(2); - public static Hand usableFireworkHand = null; - - public static boolean isElytraLow = false; - - public static boolean isStalling = false; - public static float maximumSafePitch = 0.0f; - - public static float secondsUntilGroundImpact = 0.0f; - public static float secondsUntilTerrainImpact = 0.0f; - public static float terrainDetectionTimer = 0.0f; - public static int gpwsLampColor; - - public static float lampThreshold = 0.0f; - public static float cautionThreshold = 0.0f; - public static float warningThreshold = 0.0f; - public static float correctThreshold = 0.0f; - - public static boolean flightProtectionsEnabled = true; - - public static boolean thrustSet = true; - public static boolean havePassengersDismounted = false; - public static long lastFireworkActivationTimeMs = 0; - - public static int fireworkCount = Integer.MAX_VALUE; - public static boolean thrustLocked = false; - private static int lastPassengers = 0; - - public static void update(MinecraftClient mc, FlightComputer computer) { - if (CONFIG == null || mc.world == null || mc.player == null || !mc.player.isFallFlying()) { - flightProtectionsEnabled = thrustSet = true; - thrustLocked = havePassengersDismounted = false; - lastPassengers = 0; - return; - } - maximumSafePitch = updateMaximumSafePitch(computer); - secondsUntilGroundImpact = updateUnsafeSinkrate(computer); - - boolean hasCeiling = mc.player.getWorld().getDimension().hasCeiling(); - lampThreshold = hasCeiling ? 7.5f : 10.0f; - cautionThreshold = hasCeiling ? 5.0f : 7.5f; - warningThreshold = hasCeiling ? 2.5f : 5.0f; - correctThreshold = hasCeiling ? 1.0f : 2.5f; - - if (secondsUntilGroundImpact <= warningThreshold || secondsUntilTerrainImpact <= warningThreshold) - gpwsLampColor = CONFIG.alertColor; - else if (secondsUntilGroundImpact <= lampThreshold || secondsUntilTerrainImpact <= lampThreshold) - gpwsLampColor = CONFIG.amberColor; - else - gpwsLampColor = CONFIG.color; - - isStalling = updateStallStatus(computer); - isElytraLow = updateElytraLow(computer); - secondsUntilTerrainImpact = updateUnsafeTerrainClearance(mc.player, computer); - updateUnsafeFireworks(mc.player); - fireworkCount = countSafeFireworks(mc.player); - - havePassengersDismounted = updatePassengersDismounted(mc); - - if (flightProtectionsEnabled) { // Make corrections to flight path to ensure safety - float delta = deltaTime / Math.min(1, secondsUntilGroundImpact); - if (computer.distanceFromGround > 3 && computer.pitch > maximumSafePitch) - changeLookDirection(mc.player, Math.max(0, computer.pitch - maximumSafePitch) * delta, 0); - - else if (secondsUntilGroundImpact <= correctThreshold) - changeLookDirection(mc.player, Math.min(0, computer.pitch) * delta, 0); - } - } - - private static boolean updatePassengersDismounted(MinecraftClient mc) { - int currentPassengers = (int) mc.player.getPassengerList().stream().flatMap(Entity::streamSelfAndPassengers).count(); - boolean b = currentPassengers < lastPassengers; - lastPassengers = currentPassengers; - return b; - } - - private static void updateUnsafeFireworks(PlayerEntity player) { - unsafeFireworkHands.clear(); - if (!CONFIG_SETTINGS.unsafeFireworksAlert) return; - - ItemStack main = player.getMainHandStack(); - ItemStack off = player.getOffHandStack(); - - usableFireworkHand = null; - if (off.getItem() instanceof FireworkRocketItem) { - NbtCompound nbtCompound = off.getSubNbt("Fireworks"); - if (nbtCompound != null && !nbtCompound.getList("Explosions", 10).isEmpty()) - unsafeFireworkHands.add(Hand.OFF_HAND); - else usableFireworkHand = Hand.OFF_HAND; - } - if (main.getItem() instanceof FireworkRocketItem) { - NbtCompound nbtCompound = main.getSubNbt("Fireworks"); - if (nbtCompound != null && !nbtCompound.getList("Explosions", 10).isEmpty()) - unsafeFireworkHands.add(Hand.MAIN_HAND); - else usableFireworkHand = Hand.MAIN_HAND; - } - } - - private static boolean updateElytraLow(FlightComputer computer) { - return CONFIG_SETTINGS.lowElytraHealthAlarm && computer.elytraHealth != null && computer.elytraHealth <= CONFIG_SETTINGS.lowElytraHealthAlarmThreshold; - } - - private static boolean updateStallStatus(FlightComputer computer) { - return computer.pitch > 0 && computer.distanceFromGround > 3 && computer.velocity.horizontalLength() < -computer.velocity.y; - } - - private static float updateMaximumSafePitch(FlightComputer computer) { - return isStalling && computer.velocityPerSecond.y <= -10 ? 0.0f : computer.speed * 3; - } - - private static float updateUnsafeSinkrate(FlightComputer computer) { - if (!CONFIG_SETTINGS.gpws || isStalling || computer.distanceFromGround <= 3 || computer.velocityPerSecond.y > -10) - return Float.MAX_VALUE; - return (float) (computer.distanceFromGround / -computer.velocityPerSecond.y); - } - - private static float updateUnsafeTerrainClearance(PlayerEntity player, FlightComputer computer) { - if (!CONFIG_SETTINGS.gpws || isStalling || computer.velocityPerSecond.horizontalLength() <= 15) - return Float.MAX_VALUE; - Vec3d vec = raycast(player, computer, 10); - float f = vec == null ? Float.MAX_VALUE : (float) (vec.subtract(player.getPos()).horizontalLength() / computer.velocityPerSecond.horizontalLength()); - if (f <= 10.0f) { - f = Math.min(f, secondsUntilTerrainImpact); - terrainDetectionTimer = Math.min(0.5f, terrainDetectionTimer + deltaTime); - } else { - terrainDetectionTimer = Math.max(0.0f, terrainDetectionTimer - deltaTime); - if (terrainDetectionTimer > 0.0f) - f = Math.min(f, secondsUntilTerrainImpact); - } - - return f > 10.0f || terrainDetectionTimer >= Math.min(0.5f, f * 0.2f) ? f : Float.MAX_VALUE; - } - - private static int countSafeFireworks(PlayerEntity player) { - int i = 0; - - for (int j = 0; j < player.getInventory().size(); j++) { - ItemStack stack = player.getInventory().getStack(j); - if (stack.getItem().equals(Items.FIREWORK_ROCKET)) { - NbtCompound nbtCompound = stack.getSubNbt("Fireworks"); - if (nbtCompound == null || nbtCompound.getList("Explosions", 10).isEmpty()) - i += stack.getCount(); - } - } - - return i; - } - - public static Vec3d raycast(PlayerEntity player, FlightComputer computer, int seconds) { - Vec3d vel = computer.velocityPerSecond; - Vec3d end = player.getPos().add(vel.multiply(seconds)); - - BlockHitResult result = player.getWorld().raycast(new RaycastContext(player.getPos(), end, RaycastContext.ShapeType.COLLIDER, RaycastContext.FluidHandling.ANY, player)); - if (result.getType() != HitResult.Type.BLOCK || result.getSide() == Direction.UP || result.getSide() == Direction.DOWN) - return null; - return result.getPos(); - } -} diff --git a/src/main/java/net/torocraft/flighthud/HudRenderer.java b/src/main/java/net/torocraft/flighthud/HudRenderer.java index d76f0ed9..9fd1bb8f 100644 --- a/src/main/java/net/torocraft/flighthud/HudRenderer.java +++ b/src/main/java/net/torocraft/flighthud/HudRenderer.java @@ -6,24 +6,30 @@ import net.torocraft.flighthud.components.AltitudeIndicator; import net.torocraft.flighthud.components.ElytraHealthIndicator; import net.torocraft.flighthud.components.FlightPathIndicator; -import net.torocraft.flighthud.components.FlightStatusIndicator; import net.torocraft.flighthud.components.HeadingIndicator; import net.torocraft.flighthud.components.LocationIndicator; import net.torocraft.flighthud.components.PitchIndicator; import net.torocraft.flighthud.components.SpeedIndicator; +import net.torocraft.flighthud.computers.FlightComputer; import net.torocraft.flighthud.config.SettingsConfig.DisplayMode; public class HudRenderer extends HudComponent { - public static final HudRenderer INSTANCE = new HudRenderer(); private static final String FULL = DisplayMode.FULL.toString(); private static final String MIN = DisplayMode.MIN.toString(); - public final FlightComputer computer = new FlightComputer(); + public static HudRenderer INSTANCE; + public final FlightComputer computer; private final Dimensions dim = new Dimensions(); - private final HudComponent[] components = - new HudComponent[]{new FlightPathIndicator(computer, dim), new LocationIndicator(dim), - new HeadingIndicator(computer, dim), new SpeedIndicator(computer, dim), - new AltitudeIndicator(computer, dim), new PitchIndicator(computer, dim), - new ElytraHealthIndicator(computer, dim), new FlightStatusIndicator(computer, dim)}; + private final HudComponent[] components; + + public HudRenderer(MinecraftClient mc) { + this.computer = new FlightComputer(mc); + this.components = new HudComponent[]{ + new FlightPathIndicator(computer, dim), new LocationIndicator(dim), + new HeadingIndicator(computer, dim), new SpeedIndicator(computer, dim), + new AltitudeIndicator(computer, dim), new PitchIndicator(computer, dim), + new ElytraHealthIndicator(computer, dim) + }; + } private void setupConfig(MinecraftClient client) { HudComponent.CONFIG = null; @@ -45,7 +51,6 @@ private void setupConfig(MinecraftClient client) { @Override public void render(DrawContext context, MinecraftClient client) { setupConfig(client); - ((FlightStatusIndicator) components[components.length - 1]).tryStopEvents(client.player, client.getSoundManager()); if (HudComponent.CONFIG == null) { return; diff --git a/src/main/java/net/torocraft/flighthud/Util.java b/src/main/java/net/torocraft/flighthud/Util.java new file mode 100644 index 00000000..2c69487d --- /dev/null +++ b/src/main/java/net/torocraft/flighthud/Util.java @@ -0,0 +1,9 @@ +package net.torocraft.flighthud; + +import net.minecraft.util.math.Vec3d; + +public class Util { + public static Vec3d copyVec3d(Vec3d from) { + return new Vec3d(from.x, from.y, from.z); + } +} diff --git a/src/main/java/net/torocraft/flighthud/alerts/AutoThrustLimitedAlert.java b/src/main/java/net/torocraft/flighthud/alerts/AutoThrustLimitedAlert.java deleted file mode 100644 index a3b50331..00000000 --- a/src/main/java/net/torocraft/flighthud/alerts/AutoThrustLimitedAlert.java +++ /dev/null @@ -1,24 +0,0 @@ -package net.torocraft.flighthud.alerts; - -import net.minecraft.client.MinecraftClient; -import net.minecraft.client.gui.DrawContext; -import net.torocraft.flighthud.AutoFlightManager; -import net.torocraft.flighthud.FlightSafetyMonitor; -import net.torocraft.flighthud.HudComponent; - -import static net.torocraft.flighthud.HudComponent.CONFIG; - -public class AutoThrustLimitedAlert extends Alert { - @Override - public boolean shouldActivate() { - return AutoFlightManager.autoThrustEnabled && FlightSafetyMonitor.usableFireworkHand == null; - } - - @Override - public int drawText(MinecraftClient mc, DrawContext context, float x, float y, boolean highlight) { - int i = drawCaution(mc, context, x, y, highlight, "AUTO THRUST LIMITED"); - if (FlightSafetyMonitor.fireworkCount > 0) - i += HudComponent.drawFont(mc, context, " -FRWKS: SELECT", x, y + 9, CONFIG.adviceColor); - return i; - } -} diff --git a/src/main/java/net/torocraft/flighthud/alerts/Alert.java b/src/main/java/net/torocraft/flighthud/alerts/ECAMAlert.java similarity index 63% rename from src/main/java/net/torocraft/flighthud/alerts/Alert.java rename to src/main/java/net/torocraft/flighthud/alerts/ECAMAlert.java index 203098ba..0c17a25c 100644 --- a/src/main/java/net/torocraft/flighthud/alerts/Alert.java +++ b/src/main/java/net/torocraft/flighthud/alerts/ECAMAlert.java @@ -6,7 +6,7 @@ import static net.torocraft.flighthud.HudComponent.CONFIG; -public abstract class Alert { +public abstract class ECAMAlert implements IAlert { public boolean hidden; public static int drawWarning(MinecraftClient mc, DrawContext context, float x, float y, boolean highlight, String text) { @@ -20,16 +20,8 @@ public static int drawWarning(MinecraftClient mc, DrawContext context, float x, } public static int drawCaution(MinecraftClient mc, DrawContext context, float x, float y, boolean highlight, String text) { - if (highlight) { - HudComponent.drawTextHighlight(mc.textRenderer, context, x, y, text, CONFIG.amberColor); - HudComponent.drawFont(mc, context, text, x, y, CONFIG.black); - return 1; - } - HudComponent.drawFont(mc, context, text, x, y, CONFIG.amberColor); + HudComponent.drawTextHighlight(mc.textRenderer, context, x, y, text, CONFIG.amberColor); + HudComponent.drawFont(mc, context, text, x, y, CONFIG.black); return 1; } - - public abstract boolean shouldActivate(); - - public abstract int drawText(MinecraftClient mc, DrawContext context, float x, float y, boolean highlight); } diff --git a/src/main/java/net/torocraft/flighthud/alerts/FireworkActivationFailureAlert.java b/src/main/java/net/torocraft/flighthud/alerts/FireworkActivationFailureAlert.java deleted file mode 100644 index a2406a0b..00000000 --- a/src/main/java/net/torocraft/flighthud/alerts/FireworkActivationFailureAlert.java +++ /dev/null @@ -1,32 +0,0 @@ -package net.torocraft.flighthud.alerts; - -import net.minecraft.client.MinecraftClient; -import net.minecraft.client.gui.DrawContext; -import net.torocraft.flighthud.AutoFlightManager; -import net.torocraft.flighthud.FlightSafetyMonitor; -import net.torocraft.flighthud.HudComponent; - -import static net.torocraft.flighthud.AutoFlightManager.lastUpdateTimeMs; -import static net.torocraft.flighthud.HudComponent.CONFIG; - -public class FireworkActivationFailureAlert extends Alert { - @Override - public boolean shouldActivate() { - return !FlightSafetyMonitor.thrustSet && lastUpdateTimeMs - FlightSafetyMonitor.lastFireworkActivationTimeMs > 1000; - } - - @Override - public int drawText(MinecraftClient mc, DrawContext context, float x, float y, boolean highlight) { - int i = drawWarning(mc, context, x, y, highlight, "FRWK ACTIVATION FAIL"); - if (AutoFlightManager.flightDirectorsEnabled) - i += HudComponent.drawFont(mc, context, " -FD: OFF", x, y += 9, CONFIG.adviceColor); - if (AutoFlightManager.autoThrustEnabled) - i += HudComponent.drawFont(mc, context, " -A/THR: OFF", x, y += 9, CONFIG.adviceColor); - if (AutoFlightManager.autoPilotEnabled) - i += HudComponent.drawFont(mc, context, " -A/P: OFF", x, y += 9, CONFIG.adviceColor); - i += HudComponent.drawFont(mc, context, " -PITCH: MAX SAFE UP", x, y += 9, CONFIG.adviceColor); - i += HudComponent.drawFont(mc, context, " -FRWKS: DO NOT USE", x, y += 9, CONFIG.adviceColor); - i += HudComponent.drawFont(mc, context, " SPD MAY BE UNRELIABLE", x, y + 9, CONFIG.adviceColor); - return i; - } -} diff --git a/src/main/java/net/torocraft/flighthud/alerts/FlightProtectionsOffAlert.java b/src/main/java/net/torocraft/flighthud/alerts/FlightProtectionsOffAlert.java deleted file mode 100644 index 5960f932..00000000 --- a/src/main/java/net/torocraft/flighthud/alerts/FlightProtectionsOffAlert.java +++ /dev/null @@ -1,25 +0,0 @@ -package net.torocraft.flighthud.alerts; - -import net.minecraft.client.MinecraftClient; -import net.minecraft.client.gui.DrawContext; -import net.torocraft.flighthud.FlightSafetyMonitor; -import net.torocraft.flighthud.HudComponent; - -import static net.torocraft.flighthud.HudComponent.CONFIG; - -public class FlightProtectionsOffAlert extends Alert { - @Override - public boolean shouldActivate() { - return !FlightSafetyMonitor.flightProtectionsEnabled; - } - - @Override - public int drawText(MinecraftClient mc, DrawContext context, float x, float y, boolean highlight) { - int i = drawCaution(mc, context, x, y, highlight, "FLIGHT PROTECTIONS OFF"); - i += HudComponent.drawFont(mc, context, " MAX PITCH: 40* UP", x, y += 9, CONFIG.adviceColor); - i += HudComponent.drawFont(mc, context, " MIN V/S: -8 BPS", x, y += 9, CONFIG.adviceColor); - i += HudComponent.drawFont(mc, context, " -FRWKS: CONFIRM SAFE", x, y += 9, CONFIG.adviceColor); - i += HudComponent.drawFont(mc, context, " MANEUVER WITH CARE", x, y + 9, CONFIG.adviceColor); - return i; - } -} diff --git a/src/main/java/net/torocraft/flighthud/alerts/GPWSOffAlert.java b/src/main/java/net/torocraft/flighthud/alerts/GPWSOffAlert.java deleted file mode 100644 index d2cc3c58..00000000 --- a/src/main/java/net/torocraft/flighthud/alerts/GPWSOffAlert.java +++ /dev/null @@ -1,25 +0,0 @@ -package net.torocraft.flighthud.alerts; - -import net.minecraft.client.MinecraftClient; -import net.minecraft.client.gui.DrawContext; -import net.torocraft.flighthud.HudComponent; - -import static net.torocraft.flighthud.FlightHud.CONFIG_SETTINGS; -import static net.torocraft.flighthud.HudComponent.CONFIG; - -public class GPWSOffAlert extends Alert { - @Override - public boolean shouldActivate() { - return !CONFIG_SETTINGS.gpws; - } - - @Override - public int drawText(MinecraftClient mc, DrawContext context, float x, float y, boolean highlight) { - int i = drawCaution(mc, context, x, y, highlight, "GPWS OFF"); - i += HudComponent.drawFont(mc, context, " BELOW 320M", x, y += 9, CONFIG.adviceColor); - i += HudComponent.drawFont(mc, context, " -A/P: DO NOT USE", x, y += 9, CONFIG.adviceColor); - i += HudComponent.drawFont(mc, context, " BELOW 320M", x, y += 9, CONFIG.adviceColor); - i += HudComponent.drawFont(mc, context, " -A/THR: DO NOT USE", x, y + 9, CONFIG.adviceColor); - return i; - } -} diff --git a/src/main/java/net/torocraft/flighthud/alerts/IAlert.java b/src/main/java/net/torocraft/flighthud/alerts/IAlert.java new file mode 100644 index 00000000..00984aec --- /dev/null +++ b/src/main/java/net/torocraft/flighthud/alerts/IAlert.java @@ -0,0 +1,12 @@ +package net.torocraft.flighthud.alerts; + +import net.minecraft.client.MinecraftClient; +import net.minecraft.client.gui.DrawContext; + +public interface IAlert { + boolean isTriggered(); + + void tick(); + + int drawText(MinecraftClient mc, DrawContext context, float x, float y, boolean highlight); +} diff --git a/src/main/java/net/torocraft/flighthud/alerts/LowElytraHealthAlert.java b/src/main/java/net/torocraft/flighthud/alerts/LowElytraHealthAlert.java deleted file mode 100644 index dc12e516..00000000 --- a/src/main/java/net/torocraft/flighthud/alerts/LowElytraHealthAlert.java +++ /dev/null @@ -1,17 +0,0 @@ -package net.torocraft.flighthud.alerts; - -import net.minecraft.client.MinecraftClient; -import net.minecraft.client.gui.DrawContext; -import net.torocraft.flighthud.FlightSafetyMonitor; - -public class LowElytraHealthAlert extends Alert { - @Override - public boolean shouldActivate() { - return FlightSafetyMonitor.isElytraLow; - } - - @Override - public int drawText(MinecraftClient mc, DrawContext context, float x, float y, boolean highlight) { - return drawWarning(mc, context, x, y, highlight, "ELYTRA HEALTH LOW"); - } -} diff --git a/src/main/java/net/torocraft/flighthud/alerts/LowFireworksAlert.java b/src/main/java/net/torocraft/flighthud/alerts/LowFireworksAlert.java deleted file mode 100644 index c50c665c..00000000 --- a/src/main/java/net/torocraft/flighthud/alerts/LowFireworksAlert.java +++ /dev/null @@ -1,25 +0,0 @@ -package net.torocraft.flighthud.alerts; - -import net.minecraft.client.MinecraftClient; -import net.minecraft.client.gui.DrawContext; -import net.torocraft.flighthud.AutoFlightManager; -import net.torocraft.flighthud.FlightSafetyMonitor; -import net.torocraft.flighthud.HudComponent; - -import static net.torocraft.flighthud.HudComponent.CONFIG; - -public class LowFireworksAlert extends Alert { - @Override - public boolean shouldActivate() { - return FlightSafetyMonitor.fireworkCount > 0 && FlightSafetyMonitor.fireworkCount < 24; - } - - @Override - public int drawText(MinecraftClient mc, DrawContext context, float x, float y, boolean highlight) { - int i = drawCaution(mc, context, x, y, highlight, "FRWK CNT BELOW 24"); - if (AutoFlightManager.autoThrustEnabled) - i += HudComponent.drawFont(mc, context, " -A/THR: OFF", x, y += 9, CONFIG.adviceColor); - i += HudComponent.drawFont(mc, context, " -CLIMB: INITIATE", x, y + 9, CONFIG.adviceColor); - return i; - } -} diff --git a/src/main/java/net/torocraft/flighthud/alerts/NoFireworksAlert.java b/src/main/java/net/torocraft/flighthud/alerts/NoFireworksAlert.java deleted file mode 100644 index e0e81e99..00000000 --- a/src/main/java/net/torocraft/flighthud/alerts/NoFireworksAlert.java +++ /dev/null @@ -1,28 +0,0 @@ -package net.torocraft.flighthud.alerts; - -import net.minecraft.client.MinecraftClient; -import net.minecraft.client.gui.DrawContext; -import net.torocraft.flighthud.AutoFlightManager; -import net.torocraft.flighthud.FlightSafetyMonitor; -import net.torocraft.flighthud.HudComponent; - -import static net.torocraft.flighthud.HudComponent.CONFIG; - -public class NoFireworksAlert extends Alert { - @Override - public boolean shouldActivate() { - return FlightSafetyMonitor.fireworkCount <= 0; - } - - @Override - public int drawText(MinecraftClient mc, DrawContext context, float x, float y, boolean highlight) { - int i = drawWarning(mc, context, x, y, highlight, "FRWK CNT ZERO"); - if (AutoFlightManager.autoThrustEnabled) - i += HudComponent.drawFont(mc, context, " -A/THR: OFF", x, y += 9, CONFIG.adviceColor); - if (AutoFlightManager.targetAltitude != null) - i += HudComponent.drawFont(mc, context, " -A/P ALT: RESET", x, y += 9, CONFIG.adviceColor); - i += HudComponent.drawFont(mc, context, " GLDG DIST:", x, y += 9, CONFIG.adviceColor); - i += HudComponent.drawFont(mc, context, " 100 BLKS/10 M", x, y + 9, CONFIG.adviceColor); - return i; - } -} diff --git a/src/main/java/net/torocraft/flighthud/alerts/PassengerDismountedAlert.java b/src/main/java/net/torocraft/flighthud/alerts/PassengerDismountedAlert.java deleted file mode 100644 index e8e1a85f..00000000 --- a/src/main/java/net/torocraft/flighthud/alerts/PassengerDismountedAlert.java +++ /dev/null @@ -1,17 +0,0 @@ -package net.torocraft.flighthud.alerts; - -import net.minecraft.client.MinecraftClient; -import net.minecraft.client.gui.DrawContext; -import net.torocraft.flighthud.FlightSafetyMonitor; - -public class PassengerDismountedAlert extends Alert { - @Override - public boolean shouldActivate() { - return FlightSafetyMonitor.havePassengersDismounted; - } - - @Override - public int drawText(MinecraftClient mc, DrawContext context, float x, float y, boolean highlight) { - return drawWarning(mc, context, x, y, highlight, "PAX DISMOUNTED"); - } -} \ No newline at end of file diff --git a/src/main/java/net/torocraft/flighthud/alerts/ThrustLockedAlert.java b/src/main/java/net/torocraft/flighthud/alerts/ThrustLockedAlert.java deleted file mode 100644 index ff68611e..00000000 --- a/src/main/java/net/torocraft/flighthud/alerts/ThrustLockedAlert.java +++ /dev/null @@ -1,32 +0,0 @@ -package net.torocraft.flighthud.alerts; - -import net.minecraft.client.MinecraftClient; -import net.minecraft.client.gui.DrawContext; -import net.torocraft.flighthud.AutoFlightManager; -import net.torocraft.flighthud.FlightSafetyMonitor; -import net.torocraft.flighthud.HudComponent; - -import static net.torocraft.flighthud.FlightHud.CONFIG_SETTINGS; -import static net.torocraft.flighthud.HudComponent.CONFIG; - -public class ThrustLockedAlert extends Alert { - @Override - public boolean shouldActivate() { - return FlightSafetyMonitor.thrustLocked; - } - - @Override - public int drawText(MinecraftClient mc, DrawContext context, float x, float y, boolean highlight) { - int i = drawCaution(mc, context, x, y, highlight, "THRUST LOCKED"); - if (!CONFIG_SETTINGS.gpws) - i += HudComponent.drawFont(mc, context, "-GPWS: ON", x, y += 9, CONFIG.adviceColor); - if (!AutoFlightManager.autoPilotEnabled) - i += HudComponent.drawFont(mc, context, "-A/P: ON", x, y += 9, CONFIG.adviceColor); - if (!AutoFlightManager.autoThrustEnabled) { - i += HudComponent.drawFont(mc, context, "-A/THR: ON THEN OFF", x, y + 9, CONFIG.adviceColor); - return i; - } - i += HudComponent.drawFont(mc, context, "-A/THR: OFF", x, y + 9, CONFIG.adviceColor); - return i; - } -} diff --git a/src/main/java/net/torocraft/flighthud/alerts/UnsafeFireworksAlert.java b/src/main/java/net/torocraft/flighthud/alerts/UnsafeFireworksAlert.java deleted file mode 100644 index acc42258..00000000 --- a/src/main/java/net/torocraft/flighthud/alerts/UnsafeFireworksAlert.java +++ /dev/null @@ -1,42 +0,0 @@ -package net.torocraft.flighthud.alerts; - -import net.minecraft.client.MinecraftClient; -import net.minecraft.client.gui.DrawContext; -import net.minecraft.util.Hand; -import net.torocraft.flighthud.FlightSafetyMonitor; -import net.torocraft.flighthud.HudComponent; - -import static net.torocraft.flighthud.HudComponent.CONFIG; - -public class UnsafeFireworksAlert extends Alert { - - @Override - public boolean shouldActivate() { - return !FlightSafetyMonitor.unsafeFireworkHands.isEmpty(); - } - - @Override - public int drawText(MinecraftClient mc, DrawContext context, float x, float y, boolean highlight) { - int linesDrawn = 0; - if (highlight) { - for (Hand hand : FlightSafetyMonitor.unsafeFireworkHands) { - String handStr = hand.toString().replace('_', ' '); - String text = handStr + " FRWKS UNSAFE"; - HudComponent.drawTextHighlight(mc.textRenderer, context, x, y, text, CONFIG.alertColor); - HudComponent.drawFont(mc, context, text, x, y, CONFIG.white); - y += 9; - linesDrawn++; - } - return linesDrawn; - } - - for (Hand hand : FlightSafetyMonitor.unsafeFireworkHands) { - String handStr = hand.toString().replace('_', ' '); - String result = handStr + " FRWKS UNSAFE"; - HudComponent.drawFont(mc, context, result, x, y, CONFIG.alertColor); - y += 9; - linesDrawn++; - } - return linesDrawn; - } -} diff --git a/src/main/java/net/torocraft/flighthud/alerts/nav/gpws/UnsafeSinkrateAlert.java b/src/main/java/net/torocraft/flighthud/alerts/nav/gpws/UnsafeSinkrateAlert.java new file mode 100644 index 00000000..d79f06e4 --- /dev/null +++ b/src/main/java/net/torocraft/flighthud/alerts/nav/gpws/UnsafeSinkrateAlert.java @@ -0,0 +1,34 @@ +package net.torocraft.flighthud.alerts.nav.gpws; + +import net.minecraft.client.MinecraftClient; +import net.minecraft.client.gui.DrawContext; +import net.torocraft.flighthud.alerts.IAlert; +import net.torocraft.flighthud.computers.FlightComputer; + +public class UnsafeSinkrateAlert implements IAlert { + private final FlightComputer computer; + + public UnsafeSinkrateAlert(FlightComputer computer) { + this.computer = computer; + } + + @Override + public boolean isTriggered() { + return computer.gpws.getImpactTime() >= 0; + } + + @Override + public void tick() { + boolean shouldLevelOff = computer.gpws.getImpactTime() <= 2.5f; + if (shouldLevelOff) { + // Looks like it's trying to kill us. + computer.autoflight.disconnectAutopilot(true); + } + computer.pitchController.forceLevelOff = shouldLevelOff; + } + + @Override + public int drawText(MinecraftClient mc, DrawContext context, float x, float y, boolean highlight) { + return 0; + } +} diff --git a/src/main/java/net/torocraft/flighthud/commands/AltitudeResetCommand.java b/src/main/java/net/torocraft/flighthud/commands/AltitudeResetCommand.java deleted file mode 100644 index aebc9ed9..00000000 --- a/src/main/java/net/torocraft/flighthud/commands/AltitudeResetCommand.java +++ /dev/null @@ -1,14 +0,0 @@ -package net.torocraft.flighthud.commands; - -import com.mojang.brigadier.Command; -import com.mojang.brigadier.context.CommandContext; -import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource; -import net.torocraft.flighthud.AutoFlightManager; - -public class AltitudeResetCommand implements Command { - @Override - public int run(CommandContext context) { - AutoFlightManager.targetAltitude = null; - return 0; - } -} diff --git a/src/main/java/net/torocraft/flighthud/commands/AltitudeSelectCommand.java b/src/main/java/net/torocraft/flighthud/commands/AltitudeSelectCommand.java deleted file mode 100644 index 8de69f10..00000000 --- a/src/main/java/net/torocraft/flighthud/commands/AltitudeSelectCommand.java +++ /dev/null @@ -1,15 +0,0 @@ -package net.torocraft.flighthud.commands; - -import com.mojang.brigadier.Command; -import com.mojang.brigadier.arguments.IntegerArgumentType; -import com.mojang.brigadier.context.CommandContext; -import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource; -import net.torocraft.flighthud.AutoFlightManager; - -public class AltitudeSelectCommand implements Command { - @Override - public int run(CommandContext context) { - AutoFlightManager.targetAltitude = IntegerArgumentType.getInteger(context, "targetAltitude"); - return 0; - } -} diff --git a/src/main/java/net/torocraft/flighthud/commands/DestinationResetCommand.java b/src/main/java/net/torocraft/flighthud/commands/DestinationResetCommand.java deleted file mode 100644 index f3c3f049..00000000 --- a/src/main/java/net/torocraft/flighthud/commands/DestinationResetCommand.java +++ /dev/null @@ -1,15 +0,0 @@ -package net.torocraft.flighthud.commands; - -import com.mojang.brigadier.Command; -import com.mojang.brigadier.context.CommandContext; -import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource; -import net.torocraft.flighthud.AutoFlightManager; - -public class DestinationResetCommand implements Command { - @Override - public int run(CommandContext context) { - AutoFlightManager.destinationX = null; - AutoFlightManager.destinationZ = null; - return 0; - } -} diff --git a/src/main/java/net/torocraft/flighthud/commands/DestinationSelectCommand.java b/src/main/java/net/torocraft/flighthud/commands/DestinationSelectCommand.java deleted file mode 100644 index 0925d7be..00000000 --- a/src/main/java/net/torocraft/flighthud/commands/DestinationSelectCommand.java +++ /dev/null @@ -1,16 +0,0 @@ -package net.torocraft.flighthud.commands; - -import com.mojang.brigadier.Command; -import com.mojang.brigadier.arguments.IntegerArgumentType; -import com.mojang.brigadier.context.CommandContext; -import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource; -import net.torocraft.flighthud.AutoFlightManager; - -public class DestinationSelectCommand implements Command { - @Override - public int run(CommandContext context) { - AutoFlightManager.destinationX = IntegerArgumentType.getInteger(context, "destinationX"); - AutoFlightManager.destinationZ = IntegerArgumentType.getInteger(context, "destinationZ"); - return 0; - } -} diff --git a/src/main/java/net/torocraft/flighthud/components/AltitudeIndicator.java b/src/main/java/net/torocraft/flighthud/components/AltitudeIndicator.java index 7ca083f5..dd173f0b 100644 --- a/src/main/java/net/torocraft/flighthud/components/AltitudeIndicator.java +++ b/src/main/java/net/torocraft/flighthud/components/AltitudeIndicator.java @@ -3,8 +3,8 @@ import net.minecraft.client.MinecraftClient; import net.minecraft.client.gui.DrawContext; import net.torocraft.flighthud.Dimensions; -import net.torocraft.flighthud.FlightComputer; import net.torocraft.flighthud.HudComponent; +import net.torocraft.flighthud.computers.FlightComputer; public class AltitudeIndicator extends HudComponent { private final Dimensions dim; diff --git a/src/main/java/net/torocraft/flighthud/components/ElytraHealthIndicator.java b/src/main/java/net/torocraft/flighthud/components/ElytraHealthIndicator.java index edbfd7af..59162802 100644 --- a/src/main/java/net/torocraft/flighthud/components/ElytraHealthIndicator.java +++ b/src/main/java/net/torocraft/flighthud/components/ElytraHealthIndicator.java @@ -3,10 +3,8 @@ import net.minecraft.client.MinecraftClient; import net.minecraft.client.gui.DrawContext; import net.torocraft.flighthud.Dimensions; -import net.torocraft.flighthud.FlightComputer; import net.torocraft.flighthud.HudComponent; - -import static net.torocraft.flighthud.FlightHud.CONFIG_SETTINGS; +import net.torocraft.flighthud.computers.FlightComputer; public class ElytraHealthIndicator extends HudComponent { @@ -24,7 +22,7 @@ public void render(DrawContext context, MinecraftClient mc) { float y = dim.hScreen * CONFIG.elytra_y; if (CONFIG.elytra_showHealth && computer.elytraHealth != null) { - int color = computer.elytraHealth <= CONFIG_SETTINGS.lowElytraHealthAlarmThreshold ? CONFIG.alertColor : CONFIG.color; + int color = CONFIG.color; drawBox(context, x - 3.5f, y - 1.5f, 30, color); drawFont(mc, context, "E", x - 10, y, color); drawFont(mc, context, String.format("%d", i(computer.elytraHealth)) + "%", x, y, color); diff --git a/src/main/java/net/torocraft/flighthud/components/FlightPathIndicator.java b/src/main/java/net/torocraft/flighthud/components/FlightPathIndicator.java index 88aa1309..a624ec48 100644 --- a/src/main/java/net/torocraft/flighthud/components/FlightPathIndicator.java +++ b/src/main/java/net/torocraft/flighthud/components/FlightPathIndicator.java @@ -3,10 +3,8 @@ import net.minecraft.client.MinecraftClient; import net.minecraft.client.gui.DrawContext; import net.torocraft.flighthud.Dimensions; -import net.torocraft.flighthud.FlightComputer; import net.torocraft.flighthud.HudComponent; - -import static net.torocraft.flighthud.FlightSafetyMonitor.gpwsLampColor; +import net.torocraft.flighthud.computers.FlightComputer; public class FlightPathIndicator extends HudComponent { private final Dimensions dim; @@ -45,14 +43,14 @@ public void render(DrawContext context, MinecraftClient client) { float t = y - 3 - CONFIG.halfThickness; float b = y + 3 - CONFIG.halfThickness; - drawVerticalLine(context, l, t, b, gpwsLampColor); - drawVerticalLine(context, r, t, b, gpwsLampColor); + drawVerticalLine(context, l, t, b, CONFIG.color); + drawVerticalLine(context, r, t, b, CONFIG.color); - drawHorizontalLine(context, l, r, t, gpwsLampColor); - drawHorizontalLine(context, l, r, b, gpwsLampColor); + drawHorizontalLine(context, l, r, t, CONFIG.color); + drawHorizontalLine(context, l, r, b, CONFIG.color); - drawVerticalLine(context, x, t - 5, t, gpwsLampColor); - drawHorizontalLine(context, l - 4, l, y - CONFIG.halfThickness, gpwsLampColor); - drawHorizontalLine(context, r, r + 4, y - CONFIG.halfThickness, gpwsLampColor); + drawVerticalLine(context, x, t - 5, t, CONFIG.color); + drawHorizontalLine(context, l - 4, l, y - CONFIG.halfThickness, CONFIG.color); + drawHorizontalLine(context, r, r + 4, y - CONFIG.halfThickness, CONFIG.color); } } diff --git a/src/main/java/net/torocraft/flighthud/components/FlightStatusIndicator.java b/src/main/java/net/torocraft/flighthud/components/FlightStatusIndicator.java deleted file mode 100644 index 19d12fe7..00000000 --- a/src/main/java/net/torocraft/flighthud/components/FlightStatusIndicator.java +++ /dev/null @@ -1,218 +0,0 @@ -package net.torocraft.flighthud.components; - -import it.unimi.dsi.fastutil.objects.ObjectArrayList; -import java.util.ArrayList; -import java.util.List; -import net.minecraft.client.MinecraftClient; -import net.minecraft.client.gui.DrawContext; -import net.minecraft.client.sound.SoundManager; -import net.minecraft.entity.player.PlayerEntity; -import net.minecraft.sound.SoundCategory; -import net.minecraft.sound.SoundEvent; -import net.minecraft.util.Identifier; -import net.torocraft.flighthud.AlertSoundInstance; -import net.torocraft.flighthud.AutoFlightManager; -import net.torocraft.flighthud.Dimensions; -import net.torocraft.flighthud.FlightComputer; -import net.torocraft.flighthud.FlightSafetyMonitor; -import net.torocraft.flighthud.HudComponent; -import net.torocraft.flighthud.alerts.Alert; -import net.torocraft.flighthud.alerts.AutoThrustLimitedAlert; -import net.torocraft.flighthud.alerts.FireworkActivationFailureAlert; -import net.torocraft.flighthud.alerts.FlightProtectionsOffAlert; -import net.torocraft.flighthud.alerts.GPWSOffAlert; -import net.torocraft.flighthud.alerts.LowElytraHealthAlert; -import net.torocraft.flighthud.alerts.LowFireworksAlert; -import net.torocraft.flighthud.alerts.NoFireworksAlert; -import net.torocraft.flighthud.alerts.PassengerDismountedAlert; -import net.torocraft.flighthud.alerts.ThrustLockedAlert; -import net.torocraft.flighthud.alerts.UnsafeFireworksAlert; - -import static net.torocraft.flighthud.AutoFlightManager.deltaTime; -import static net.torocraft.flighthud.AutoFlightManager.lastUpdateTimeMs; -import static net.torocraft.flighthud.FlightHud.CONFIG_SETTINGS; - -public class FlightStatusIndicator extends HudComponent { - public static final SoundEvent ALERT = SoundEvent.of(new Identifier("flighthud:alert")); - public static final SoundEvent STICK_SHAKER = SoundEvent.of(new Identifier("flighthud:stick_shaker")); - public static final SoundEvent STALL_WARNING = SoundEvent.of(new Identifier("flighthud:stall_warning")); - public static final SoundEvent SINKRATE = SoundEvent.of(new Identifier("flighthud:sinkrate")); - public static final SoundEvent TERRAIN = SoundEvent.of(new Identifier("flighthud:terrain")); - public static final SoundEvent PULL_UP = SoundEvent.of(new Identifier("flighthud:pull_up")); - public static final SoundEvent AUTOPILOT_DISCONNECT = SoundEvent.of(new Identifier("flighthud:autopilot_disconnect")); - public static final Alert[] registeredAlerts = { - new AutoThrustLimitedAlert(), new ThrustLockedAlert(), - new FlightProtectionsOffAlert(), new GPWSOffAlert(), - new LowElytraHealthAlert(), - new LowFireworksAlert(), new NoFireworksAlert(), - new UnsafeFireworksAlert(), new FireworkActivationFailureAlert(), - new PassengerDismountedAlert() - }; - public static final List activeAlerts = new ArrayList<>(); - private static long lastHighlightTimeMs = 0L; - - private final List activeEvents = new ObjectArrayList<>(2); - private final Dimensions dim; - private final FlightComputer computer; - public boolean highlight = true; - private boolean lastAutopilotState = false; - private AlertSoundInstance stickShakerInstance; - - public FlightStatusIndicator(FlightComputer computer, Dimensions dim) { - this.dim = dim; - this.computer = computer; - } - - @Override - public void render(DrawContext context, MinecraftClient mc) { - if (mc.world == null || mc.player == null || !mc.player.isFallFlying()) return; - if (lastUpdateTimeMs - lastHighlightTimeMs >= 500) { - highlight = !highlight; - lastHighlightTimeMs = lastUpdateTimeMs; - } - - float x = dim.lFrame + 5; - float xRight = dim.rFrame - 5; - float y = dim.tFrame + 15; - float yRight = y - 10; - - for (Alert alert : registeredAlerts) { - if (!activeAlerts.contains(alert) && alert.shouldActivate()) { - playOnce(mc, ALERT, 0.75f, false); - alert.hidden = false; - activeAlerts.add(alert); - } - } - - for (Alert alert : activeAlerts) { - if (alert.hidden) continue; - y += 9 * alert.drawText(mc, context, x, y, highlight); - } - - int fwobLampColor = FlightSafetyMonitor.fireworkCount > 0 - ? (FlightSafetyMonitor.fireworkCount < 24 ? CONFIG.amberColor : CONFIG.color) - : CONFIG.alertColor; - drawRightAlignedFont(mc, context, "FRWK CNT: " + FlightSafetyMonitor.fireworkCount, xRight, yRight += 9, fwobLampColor); - // Right-side ECAM - if (AutoFlightManager.flightDirectorsEnabled) { - drawRightAlignedFont(mc, context, "FD", xRight, yRight += 9, CONFIG.color); - - // Flight directors - if (AutoFlightManager.targetPitch != null) { - float deltaPitch = computer.pitch + AutoFlightManager.targetPitch; - float fdY = Math.max(dim.tFrame, Math.min(dim.bFrame, dim.yMid + i(deltaPitch * dim.degreesPerPixel))); - drawHorizontalLine(context, dim.xMid - dim.wFrame * 0.15f, dim.xMid + dim.wFrame * 0.15f, fdY, CONFIG.adviceColor); - } - - if (AutoFlightManager.targetHeading != null) { - float deltaHeading = wrapHeading(AutoFlightManager.targetHeading) - wrapHeading(computer.heading); - if (deltaHeading < -180) { - deltaHeading += 360; - } - - float fdX = Math.max(dim.lFrame, Math.min(dim.rFrame, dim.xMid + i(deltaHeading * dim.degreesPerPixel))); - drawVerticalLine(context, fdX, dim.yMid - dim.hFrame * 0.15f, dim.yMid + dim.hFrame * 0.15f, CONFIG.adviceColor); - } - } - if (AutoFlightManager.distanceToTarget != null) { - drawRightAlignedFont(mc, context, String.format("DIST: %.0f", AutoFlightManager.distanceToTarget), xRight, yRight + 9, CONFIG.color); - } - drawCenteredFont(mc, context, AutoFlightManager.statusString, dim.wScreen, dim.tFrame + 15, CONFIG.color); - - if (lastAutopilotState) { - if (!AutoFlightManager.autoPilotEnabled) - playOnce(mc, AUTOPILOT_DISCONNECT, 1f, false); - else stopEvent(mc, AUTOPILOT_DISCONNECT); - } - lastAutopilotState = AutoFlightManager.autoPilotEnabled; - - tryPlayStickShaker(mc, context); - - if (FlightSafetyMonitor.secondsUntilGroundImpact <= FlightSafetyMonitor.warningThreshold || FlightSafetyMonitor.secondsUntilTerrainImpact <= FlightSafetyMonitor.warningThreshold) { - playOnce(mc, PULL_UP, 0.75f, true); - drawCenteredWarning(mc, context, dim.wScreen, dim.hScreen / 2 + 10, highlight, "PULL UP"); - } else if (FlightSafetyMonitor.secondsUntilTerrainImpact <= FlightSafetyMonitor.cautionThreshold) - playOnce(mc, TERRAIN, 0.5f, true); - else if (FlightSafetyMonitor.secondsUntilGroundImpact <= FlightSafetyMonitor.cautionThreshold) - playOnce(mc, SINKRATE, 0.5f, true); - else - stopGpwsEvents(mc); - } - - public void drawCenteredWarning(MinecraftClient mc, DrawContext context, float width, float y, boolean highlight, String text) { - float x = (width - mc.textRenderer.getWidth(text)) / 2; - if (highlight) { - HudComponent.drawUnbatched(drawContext -> { - HudComponent.drawTextHighlight(mc.textRenderer, drawContext, x, y, text, CONFIG.alertColor); - HudComponent.drawCenteredFont(mc, drawContext, text, width, y, CONFIG.white); - }, context); - return; - } - HudComponent.drawCenteredFont(mc, context, text, width, y, CONFIG.alertColor); - } - - public void tryStopEvents(PlayerEntity player, SoundManager manager) { - if (!player.isFallFlying()) { - if (!activeEvents.isEmpty()) { - for (SoundEvent event : activeEvents) - manager.stopSounds(event.getId(), SoundCategory.MASTER); - activeEvents.clear(); - } - if (stickShakerInstance != null) { - manager.stop(stickShakerInstance); - stickShakerInstance = null; - } - } - activeAlerts.removeIf(alert -> !alert.shouldActivate()); - } - - private void playOnce(MinecraftClient mc, SoundEvent event, float volume, boolean limit) { - if (!limit || !activeEvents.contains(event)) { - mc.getSoundManager().play(new AlertSoundInstance(event, volume, mc.player, false)); - if (limit) - activeEvents.add(event); - } - } - - private void tryPlayStickShaker(MinecraftClient mc, DrawContext context) { - if (FlightSafetyMonitor.isStalling) { - if (CONFIG_SETTINGS.stickShaker) { - if (stickShakerInstance == null) { - mc.getSoundManager().play(stickShakerInstance = new AlertSoundInstance(STICK_SHAKER, 0.0f, mc.player, true)); - } - - stickShakerInstance.setVolume(stickShakerInstance.getVolume() + deltaTime * 2.0f); - } - - if (computer.velocityPerSecond.y <= -10) { - playOnce(mc, STALL_WARNING, 1.0f, true); - } else - stopEvent(mc, STALL_WARNING); - drawCenteredWarning(mc, context, dim.wScreen, dim.hScreen / 2 + 10, highlight, "STALL"); - return; - } - - stopEvent(mc, STALL_WARNING); - - if (stickShakerInstance == null) { - return; - } - - stickShakerInstance.setVolume(stickShakerInstance.getVolume() - deltaTime * 2.0f); - if (stickShakerInstance.getVolume() <= 0.0f) { - mc.getSoundManager().stop(stickShakerInstance); - stickShakerInstance = null; - } - } - - private void stopGpwsEvents(MinecraftClient mc) { - stopEvent(mc, TERRAIN); - stopEvent(mc, SINKRATE); - stopEvent(mc, PULL_UP); - } - - private void stopEvent(MinecraftClient mc, SoundEvent event) { - mc.getSoundManager().stopSounds(event.getId(), SoundCategory.MASTER); - activeEvents.remove(event); - } -} diff --git a/src/main/java/net/torocraft/flighthud/components/HeadingIndicator.java b/src/main/java/net/torocraft/flighthud/components/HeadingIndicator.java index 4bd8aff1..0457ad41 100644 --- a/src/main/java/net/torocraft/flighthud/components/HeadingIndicator.java +++ b/src/main/java/net/torocraft/flighthud/components/HeadingIndicator.java @@ -3,8 +3,8 @@ import net.minecraft.client.MinecraftClient; import net.minecraft.client.gui.DrawContext; import net.torocraft.flighthud.Dimensions; -import net.torocraft.flighthud.FlightComputer; import net.torocraft.flighthud.HudComponent; +import net.torocraft.flighthud.computers.FlightComputer; public class HeadingIndicator extends HudComponent { diff --git a/src/main/java/net/torocraft/flighthud/components/PitchIndicator.java b/src/main/java/net/torocraft/flighthud/components/PitchIndicator.java index 09d0c575..3fa58340 100644 --- a/src/main/java/net/torocraft/flighthud/components/PitchIndicator.java +++ b/src/main/java/net/torocraft/flighthud/components/PitchIndicator.java @@ -4,9 +4,8 @@ import net.minecraft.client.gui.DrawContext; import net.minecraft.util.math.RotationAxis; import net.torocraft.flighthud.Dimensions; -import net.torocraft.flighthud.FlightComputer; -import net.torocraft.flighthud.FlightSafetyMonitor; import net.torocraft.flighthud.HudComponent; +import net.torocraft.flighthud.computers.FlightComputer; public class PitchIndicator extends HudComponent { private final Dimensions dim; @@ -43,7 +42,7 @@ public void render(DrawContext context, MinecraftClient mc) { drawReferenceMark(context, yHorizon, CONFIG.pitchLadder_optimumClimbAngle, CONFIG.color); drawReferenceMark(context, yHorizon, CONFIG.pitchLadder_optimumGlideAngle, CONFIG.color); - drawReferenceMark(context, yHorizon, FlightSafetyMonitor.maximumSafePitch, CONFIG.alertColor); + //drawReferenceMark(context, yHorizon, FlightSafetyMonitor.maximumSafePitch, CONFIG.alertColor); if (CONFIG.pitchLadder_showHorizon) { pitchData.l1 -= pitchData.margin; diff --git a/src/main/java/net/torocraft/flighthud/components/SpeedIndicator.java b/src/main/java/net/torocraft/flighthud/components/SpeedIndicator.java index 85916e9c..dbc01749 100644 --- a/src/main/java/net/torocraft/flighthud/components/SpeedIndicator.java +++ b/src/main/java/net/torocraft/flighthud/components/SpeedIndicator.java @@ -3,10 +3,8 @@ import net.minecraft.client.MinecraftClient; import net.minecraft.client.gui.DrawContext; import net.torocraft.flighthud.Dimensions; -import net.torocraft.flighthud.FlightComputer; import net.torocraft.flighthud.HudComponent; - -import static net.torocraft.flighthud.FlightSafetyMonitor.isStalling; +import net.torocraft.flighthud.computers.FlightComputer; public class SpeedIndicator extends HudComponent { private final Dimensions dim; @@ -32,9 +30,8 @@ public void render(DrawContext context, MinecraftClient mc) { float xSpeedText = left - 5; if (CONFIG.speed_showReadout) { - int color = isStalling ? CONFIG.alertColor : CONFIG.color; - drawRightAlignedFont(mc, context, String.format("%.2f", computer.speed), xSpeedText, dim.yMid - 3, color); - drawBox(context, xSpeedText - 29.5f, dim.yMid - 4.5f, 30, color); + drawRightAlignedFont(mc, context, String.format("%.2f", computer.speed), xSpeedText, dim.yMid - 3, CONFIG.color); + drawBox(context, xSpeedText - 29.5f, dim.yMid - 4.5f, 30, CONFIG.color); float frameWidth = dim.rFrame - dim.lFrame; drawFont(mc, context, String.format("G/S: %.2f", computer.velocityPerSecond.horizontalLength()), dim.lFrame + frameWidth * 0.25f, dim.hScreen * 0.8f, CONFIG.color); diff --git a/src/main/java/net/torocraft/flighthud/computers/AutoFlightComputer.java b/src/main/java/net/torocraft/flighthud/computers/AutoFlightComputer.java new file mode 100644 index 00000000..15685334 --- /dev/null +++ b/src/main/java/net/torocraft/flighthud/computers/AutoFlightComputer.java @@ -0,0 +1,26 @@ +package net.torocraft.flighthud.computers; + +public class AutoFlightComputer { + private final FlightComputer computer; + private float targetPitch; + + public AutoFlightComputer(FlightComputer computer) { + this.computer = computer; + } + + public void setTargetPitch(float pitch) { + this.targetPitch = pitch; + } + + public void disconnectAutopilot(boolean force) { + + } + + public void disconnectAutoThrust(boolean force) { + + } + + public void tick() { + + } +} diff --git a/src/main/java/net/torocraft/flighthud/FlightComputer.java b/src/main/java/net/torocraft/flighthud/computers/FlightComputer.java similarity index 52% rename from src/main/java/net/torocraft/flighthud/FlightComputer.java rename to src/main/java/net/torocraft/flighthud/computers/FlightComputer.java index 90578531..68398780 100644 --- a/src/main/java/net/torocraft/flighthud/FlightComputer.java +++ b/src/main/java/net/torocraft/flighthud/computers/FlightComputer.java @@ -1,21 +1,33 @@ -package net.torocraft.flighthud; +package net.torocraft.flighthud.computers; import net.minecraft.block.BlockState; import net.minecraft.client.MinecraftClient; -import net.minecraft.entity.Entity; import net.minecraft.entity.EquipmentSlot; +import net.minecraft.entity.player.PlayerEntity; import net.minecraft.item.ItemStack; import net.minecraft.item.Items; import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.Direction; import net.minecraft.util.math.Vec3d; +import net.torocraft.flighthud.FlightHud; +import org.jetbrains.annotations.NotNull; import org.joml.Matrix3f; -public class FlightComputer { - private static final float TICKS_PER_SECOND = 20; +import static net.minecraft.SharedConstants.TICKS_PER_SECOND; +public class FlightComputer { + public final GPWSComputer gpws = new GPWSComputer(this); + @NotNull + private final MinecraftClient mc; + @NotNull + private final PlayerEntity player; + public AutoFlightComputer autoflight = new AutoFlightComputer(this); + public PitchController pitchController = new PitchController(); + + public Vec3d position; public Vec3d velocity; public Vec3d velocityPerSecond; + public Vec3d acceleration; public float speed; public float pitch; public float heading; @@ -28,32 +40,44 @@ public class FlightComputer { public float distanceFromGround; public Float elytraHealth; - public static boolean isGround(BlockPos pos, MinecraftClient client) { - BlockState block = client.world.getBlockState(pos); + public FlightComputer(MinecraftClient mc) { + this.mc = mc; + assert mc.player != null; + this.player = mc.player; + } + + public boolean isGround(BlockPos pos) { + assert mc.world != null; + BlockState block = mc.world.getBlockState(pos); return !block.isAir(); } - public void update(MinecraftClient client, Matrix3f normal) { - velocity = client.player.getVelocity(); + public void tick() { + position = player.getPos(); + acceleration = player.getVelocity().subtract(velocity); + velocity = player.getVelocity(); velocityPerSecond = velocity.multiply(TICKS_PER_SECOND); - pitch = computePitch(client); - speed = computeSpeed(client); - roll = computeRoll(normal); - heading = computeHeading(client); - altitude = computeAltitude(client); - groundLevel = computeGroundLevel(client); + pitch = computePitch(); + speed = computeSpeed(); + heading = computeHeading(); + altitude = computeAltitude(); + groundLevel = computeGroundLevel(); distanceFromGround = computeDistanceFromGround(altitude, groundLevel); flightPitch = computeFlightPitch(velocity, pitch); - flightYaw = computeFlightYaw(velocity, client.player.getYaw()); + flightYaw = computeFlightYaw(velocity, player.getYaw()); flightHeading = toHeading(flightYaw); - elytraHealth = computeElytraHealth(client); + elytraHealth = computeElytraHealth(); - AutoFlightManager.update(client, this); - FlightSafetyMonitor.update(client, this); + gpws.tick(); + autoflight.tick(); } - private Float computeElytraHealth(MinecraftClient client) { - ItemStack stack = client.player.getEquippedStack(EquipmentSlot.CHEST); + public void updateRoll(Matrix3f normal) { + roll = computeRoll(normal); + } + + private Float computeElytraHealth() { + ItemStack stack = player.getEquippedStack(EquipmentSlot.CHEST); if (stack != null && stack.getItem() == Items.ELYTRA) { float remain = ((float) stack.getMaxDamage() - (float) stack.getDamage()) / (float) stack.getMaxDamage(); return remain * 100f; @@ -86,23 +110,23 @@ private float computeRoll(Matrix3f normalMatrix) { return (float) Math.toDegrees(Math.atan2(y, x)); } - private float computePitch(MinecraftClient client) { - return -client.player.getPitch(); + private float computePitch() { + return -player.getPitch(); } - public BlockPos findGround(MinecraftClient client) { - BlockPos.Mutable pos = client.player.getBlockPos().mutableCopy(); + public BlockPos findGround() { + BlockPos.Mutable pos = player.getBlockPos().mutableCopy(); while (pos.getY() >= -64) { - if (isGround(pos.move(Direction.DOWN), client)) { + if (isGround(pos.move(Direction.DOWN))) { return pos; } } return null; } - private int computeGroundLevel(MinecraftClient client) { - BlockPos ground = findGround(client); - return ground == null ? Math.min(client.player.getBlockY() + 4, -50) : ground.getY(); + private int computeGroundLevel() { + BlockPos ground = findGround(); + return ground == null ? Math.min(player.getBlockY() + 4, -50) : ground.getY(); } private float computeDistanceFromGround(float altitude, @@ -110,27 +134,23 @@ private float computeDistanceFromGround(float altitude, return Math.max(-64f, altitude - groundLevel); } - private float computeAltitude(MinecraftClient client) { - return (float) client.player.getPos().y - 1; + private float computeAltitude() { + return (float) player.getPos().y - 1; } - private float computeHeading(MinecraftClient client) { - return toHeading(client.player.getYaw()); + private float computeHeading() { + return toHeading(player.getYaw()); } - private float computeSpeed(MinecraftClient client) { - float speed; - var player = client.player; - if (player.hasVehicle()) { - Entity entity = player.getVehicle(); - speed = (float) entity.getVelocity().length() * TICKS_PER_SECOND; - } else { - speed = (float) client.player.getVelocity().length() * TICKS_PER_SECOND; - } - return speed; + private float computeSpeed() { + return (float) velocityPerSecond.length(); } private float toHeading(float yawDegrees) { return (yawDegrees + 180) % 360; } + + public void tickPitchController(float tickDelta) { + pitchController.tick(player, tickDelta); + } } diff --git a/src/main/java/net/torocraft/flighthud/computers/GPWSComputer.java b/src/main/java/net/torocraft/flighthud/computers/GPWSComputer.java new file mode 100644 index 00000000..c41549dc --- /dev/null +++ b/src/main/java/net/torocraft/flighthud/computers/GPWSComputer.java @@ -0,0 +1,50 @@ +package net.torocraft.flighthud.computers; + +import net.minecraft.util.math.Vec3d; +import net.torocraft.flighthud.Util; + +import static net.minecraft.SharedConstants.TICKS_PER_SECOND; + +public class GPWSComputer { + private static final int MAX_SAFE_SINKRATE = -10; + private static final int STATUS_VERTICAL_SPEED_SAFE = -1; + private static final int MAX_SIMULATION_TICKS = 10 * TICKS_PER_SECOND; + private static final int STATUS_SIMULATION_TIME_EXCEEDED = -2; + private static final float PITCH_CORRECT_THRESHOLD = 2.5f; + private final FlightComputer computer; + private float impactTime; + + public GPWSComputer(FlightComputer computer) { + this.computer = computer; + } + + public void tick() { + impactTime = this.computeImpactTime(); + } + + public float getImpactTime() { + return impactTime; + } + + public boolean shouldCorrectPitch() { + return getImpactTime() <= PITCH_CORRECT_THRESHOLD; + } + + private float computeImpactTime() { + if (computer.velocity.y > MAX_SAFE_SINKRATE) { + return STATUS_VERTICAL_SPEED_SAFE; + } + + Vec3d position = Util.copyVec3d(computer.position); + Vec3d velocity = Util.copyVec3d(computer.velocity); + for (int ticks = 0; ticks < MAX_SIMULATION_TICKS; ticks++) { + velocity.add(computer.acceleration); + position.add(velocity); + if (position.y <= computer.groundLevel) { + return (float) ticks / TICKS_PER_SECOND; + } + } + + return STATUS_SIMULATION_TIME_EXCEEDED; + } +} diff --git a/src/main/java/net/torocraft/flighthud/computers/PitchController.java b/src/main/java/net/torocraft/flighthud/computers/PitchController.java new file mode 100644 index 00000000..1d18eddb --- /dev/null +++ b/src/main/java/net/torocraft/flighthud/computers/PitchController.java @@ -0,0 +1,21 @@ +package net.torocraft.flighthud.computers; + +import net.minecraft.entity.player.PlayerEntity; + +public class PitchController { + + /** + * USE MINECRAFT PITCH (minus is up and plus is down) + **/ + public float targetPitch; + public boolean forceLevelOff = false; + + public void tick(PlayerEntity player, float tickDelta) { + if (forceLevelOff) { + player.setPitch(player.getPitch() - player.getPitch() * tickDelta); + return; + } + + player.setPitch(player.getPitch() - (player.getPitch() - targetPitch) * tickDelta); + } +} diff --git a/src/main/java/net/torocraft/flighthud/config/HudConfig.java b/src/main/java/net/torocraft/flighthud/config/HudConfig.java index 510fdc13..383650c2 100644 --- a/src/main/java/net/torocraft/flighthud/config/HudConfig.java +++ b/src/main/java/net/torocraft/flighthud/config/HudConfig.java @@ -1,13 +1,12 @@ package net.torocraft.flighthud.config; -import net.torocraft.flighthud.config.loader.IConfig; - import java.awt.*; +import net.torocraft.flighthud.config.loader.IConfig; public class HudConfig implements IConfig { public transient int color = Color.GREEN.getRGB(); public transient int alertColor = Color.RED.getRGB(); - public transient int amberColor = Color.YELLOW.getRGB(); + public transient int amberColor = Color.ORANGE.getRGB(); public transient int adviceColor = Color.CYAN.getRGB(); public transient int black = Color.BLACK.getRGB(); public transient int white = Color.WHITE.getRGB(); diff --git a/src/main/java/net/torocraft/flighthud/config/SettingsConfig.java b/src/main/java/net/torocraft/flighthud/config/SettingsConfig.java index 4a803b9f..15902a2e 100644 --- a/src/main/java/net/torocraft/flighthud/config/SettingsConfig.java +++ b/src/main/java/net/torocraft/flighthud/config/SettingsConfig.java @@ -10,11 +10,6 @@ public class SettingsConfig implements IConfig { public String displayModeWhenFlying = DisplayMode.FULL.toString(); public String displayModeWhenNotFlying = DisplayMode.NONE.toString(); public boolean calculateRoll = true; - public boolean gpws = true; - public boolean stickShaker = true; - public boolean lowElytraHealthAlarm = true; - public int lowElytraHealthAlarmThreshold = 10; - public boolean unsafeFireworksAlert = true; private static String toggle(String curr) { DisplayMode m = parseDisplayMode(curr); diff --git a/src/main/java/net/torocraft/flighthud/mixin/ClientPlayerInteractionManagerMixin.java b/src/main/java/net/torocraft/flighthud/mixin/ClientPlayerInteractionManagerMixin.java index 3e51e5be..2958b189 100644 --- a/src/main/java/net/torocraft/flighthud/mixin/ClientPlayerInteractionManagerMixin.java +++ b/src/main/java/net/torocraft/flighthud/mixin/ClientPlayerInteractionManagerMixin.java @@ -2,11 +2,8 @@ import net.minecraft.client.network.ClientPlayerInteractionManager; import net.minecraft.entity.player.PlayerEntity; -import net.minecraft.item.FireworkRocketItem; import net.minecraft.util.ActionResult; import net.minecraft.util.Hand; -import net.minecraft.util.Util; -import net.torocraft.flighthud.FlightSafetyMonitor; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Inject; @@ -16,7 +13,7 @@ public class ClientPlayerInteractionManagerMixin { @Inject(method = "interactItem", at = @At("HEAD"), cancellable = true) public void disallowUnsafeFireworks(PlayerEntity player, Hand hand, CallbackInfoReturnable cir) { - if (!player.isFallFlying()) return; + /*if (!player.isFallFlying()) return; if (FlightSafetyMonitor.flightProtectionsEnabled && FlightSafetyMonitor.unsafeFireworkHands.contains(hand)) cir.setReturnValue(ActionResult.FAIL); @@ -28,6 +25,6 @@ else if (player.getStackInHand(hand).getItem() instanceof FireworkRocketItem) { FlightSafetyMonitor.lastFireworkActivationTimeMs = Util.getMeasuringTimeMs(); FlightSafetyMonitor.thrustSet = false; } - } + }*/ } } diff --git a/src/main/java/net/torocraft/flighthud/mixin/EntityMixin.java b/src/main/java/net/torocraft/flighthud/mixin/EntityMixin.java index fcc5a9f8..35d4828a 100644 --- a/src/main/java/net/torocraft/flighthud/mixin/EntityMixin.java +++ b/src/main/java/net/torocraft/flighthud/mixin/EntityMixin.java @@ -1,8 +1,6 @@ package net.torocraft.flighthud.mixin; -import net.minecraft.client.network.ClientPlayerEntity; import net.minecraft.entity.Entity; -import net.torocraft.flighthud.FlightSafetyMonitor; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Inject; @@ -12,13 +10,13 @@ public abstract class EntityMixin { @Inject(method = "setPitch", at = @At("HEAD"), cancellable = true) public void preventUpsetPitch(float pitch, CallbackInfo ci) { - Entity that = (Entity) (Object) this; + /*Entity that = (Entity) (Object) this; if (that instanceof ClientPlayerEntity cpe && cpe.isFallFlying() && FlightSafetyMonitor.flightProtectionsEnabled) { boolean approachingStall = pitch < that.getPitch() && pitch < -FlightSafetyMonitor.maximumSafePitch; boolean highSinkRate = pitch > that.getPitch() && FlightSafetyMonitor.secondsUntilGroundImpact <= 5.0f; if (approachingStall || highSinkRate) ci.cancel(); - } + }*/ } } diff --git a/src/main/java/net/torocraft/flighthud/mixin/GameRendererMixin.java b/src/main/java/net/torocraft/flighthud/mixin/GameRendererMixin.java index 1e9f6ae6..b54c85c5 100644 --- a/src/main/java/net/torocraft/flighthud/mixin/GameRendererMixin.java +++ b/src/main/java/net/torocraft/flighthud/mixin/GameRendererMixin.java @@ -1,24 +1,17 @@ package net.torocraft.flighthud.mixin; import com.mojang.blaze3d.systems.RenderSystem; -import net.minecraft.client.MinecraftClient; import net.minecraft.client.render.GameRenderer; import net.minecraft.client.util.math.MatrixStack; import net.torocraft.flighthud.HudRenderer; import org.joml.Matrix3f; -import org.spongepowered.asm.mixin.Final; import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.Shadow; import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; @Mixin(GameRenderer.class) public class GameRendererMixin { - @Shadow - @Final - MinecraftClient client; - @Inject( method = "renderWorld", at = @At( @@ -33,6 +26,8 @@ private void renderWorld(float tickDelta, CallbackInfo ci ) { Matrix3f inverseViewRotationMatrix = RenderSystem.getInverseViewRotationMatrix(); - HudRenderer.INSTANCE.computer.update(client, inverseViewRotationMatrix.invert()); + if (HudRenderer.INSTANCE != null) { + HudRenderer.INSTANCE.computer.updateRoll(inverseViewRotationMatrix); + } } } diff --git a/src/main/java/net/torocraft/flighthud/mixin/InGameHudMixin.java b/src/main/java/net/torocraft/flighthud/mixin/InGameHudMixin.java index 950ad43d..321d3cb6 100644 --- a/src/main/java/net/torocraft/flighthud/mixin/InGameHudMixin.java +++ b/src/main/java/net/torocraft/flighthud/mixin/InGameHudMixin.java @@ -17,8 +17,19 @@ public class InGameHudMixin { @Shadow private MinecraftClient client; - @Inject(method = "render", at = @At("RETURN")) + @Inject(method = "render", at = @At("TAIL")) private void render(DrawContext context, float tickDelta, CallbackInfo ci) { - HudRenderer.INSTANCE.render(context, client); + if (HudRenderer.INSTANCE != null) { + HudRenderer.INSTANCE.render(context, client); + HudRenderer.INSTANCE.computer.tickPitchController(tickDelta); + } + } + + @Inject(method = "tick()V", at = @At("TAIL")) + private void tick(CallbackInfo ci) { + if (HudRenderer.INSTANCE == null) { + HudRenderer.INSTANCE = new HudRenderer(client); + } + HudRenderer.INSTANCE.computer.tick(); } } diff --git a/src/main/java/net/torocraft/flighthud/mixin/LivingEntityMixin.java b/src/main/java/net/torocraft/flighthud/mixin/LivingEntityMixin.java index e9975ddf..f41246b7 100644 --- a/src/main/java/net/torocraft/flighthud/mixin/LivingEntityMixin.java +++ b/src/main/java/net/torocraft/flighthud/mixin/LivingEntityMixin.java @@ -1,10 +1,7 @@ package net.torocraft.flighthud.mixin; -import net.minecraft.client.network.ClientPlayerEntity; import net.minecraft.entity.LivingEntity; import net.minecraft.entity.damage.DamageSource; -import net.minecraft.entity.damage.DamageTypes; -import net.torocraft.flighthud.FlightSafetyMonitor; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Inject; @@ -14,9 +11,9 @@ public class LivingEntityMixin { @Inject(method = "onDamaged", at = @At("HEAD")) public void detectWallCollision(DamageSource damageSource, CallbackInfo ci) { - LivingEntity $this = (LivingEntity) (Object) this; + /*LivingEntity $this = (LivingEntity) (Object) this; if ($this instanceof ClientPlayerEntity && damageSource.isOf(DamageTypes.FLY_INTO_WALL)) { FlightSafetyMonitor.thrustLocked = true; - } + }*/ } } diff --git a/src/main/resources/assets/flighthud/sounds.json b/src/main/resources/assets/flighthud/sounds.json index 0d159459..1a17085a 100644 --- a/src/main/resources/assets/flighthud/sounds.json +++ b/src/main/resources/assets/flighthud/sounds.json @@ -1,17 +1,17 @@ { - "alert": { + "autopilot_disconnect": { "sounds": [ - "flighthud:alert" + "flighthud:autopilot_disconnect" ] }, - "stick_shaker": { + "caution": { "sounds": [ - "flighthud:stick_shaker" + "flighthud:caution" ] }, - "stall_warning": { + "pull_up": { "sounds": [ - "flighthud:stall_warning" + "flighthud:pull_up" ] }, "sinkrate": { @@ -24,14 +24,9 @@ "flighthud:terrain" ] }, - "pull_up": { + "warning": { "sounds": [ - "flighthud:pull_up" - ] - }, - "autopilot_disconnect": { - "sounds": [ - "flighthud:autopilot_disconnect" + "flighthud:warning" ] } } diff --git a/src/main/resources/assets/flighthud/sounds/alert.ogg b/src/main/resources/assets/flighthud/sounds/alert.ogg deleted file mode 100644 index e0c84f52..00000000 Binary files a/src/main/resources/assets/flighthud/sounds/alert.ogg and /dev/null differ diff --git a/src/main/resources/assets/flighthud/sounds/caution.wav b/src/main/resources/assets/flighthud/sounds/caution.wav new file mode 100644 index 00000000..cc3d88bb Binary files /dev/null and b/src/main/resources/assets/flighthud/sounds/caution.wav differ diff --git a/src/main/resources/assets/flighthud/sounds/stall_warning.ogg b/src/main/resources/assets/flighthud/sounds/stall_warning.ogg deleted file mode 100644 index e69de29b..00000000 diff --git a/src/main/resources/assets/flighthud/sounds/stick_shaker.ogg b/src/main/resources/assets/flighthud/sounds/stick_shaker.ogg deleted file mode 100644 index eace9615..00000000 Binary files a/src/main/resources/assets/flighthud/sounds/stick_shaker.ogg and /dev/null differ diff --git a/src/main/resources/assets/flighthud/sounds/warning.wav b/src/main/resources/assets/flighthud/sounds/warning.wav new file mode 100644 index 00000000..0cb51e66 Binary files /dev/null and b/src/main/resources/assets/flighthud/sounds/warning.wav differ