From 2692887684584b4f50a85dc45471a3d28da7fc8b Mon Sep 17 00:00:00 2001 From: LeonardoVAC Date: Fri, 16 Oct 2026 00:38:51 -0300 Subject: [PATCH] feat: Add extended zoom --- src/TerraSharp/Core/Hotkeys.cs | 1 + src/TerraSharp/Core/Settings.Json.cs | 1 + src/TerraSharp/Core/Settings.cs | 2 +- src/TerraSharp/Patches/VisionPatches.cs | 42 ++++++++++++++++++++++++ src/TerraSharp/UI/MainWindow.Pages.cs | 8 +++++ src/TerraSharp/UI/MainWindow.Tooltips.cs | 1 + src/TerraSharp/UI/MainWindow.cs | 2 +- 7 files changed, 55 insertions(+), 2 deletions(-) diff --git a/src/TerraSharp/Core/Hotkeys.cs b/src/TerraSharp/Core/Hotkeys.cs index 42d6022..cbe6467 100644 --- a/src/TerraSharp/Core/Hotkeys.cs +++ b/src/TerraSharp/Core/Hotkeys.cs @@ -71,6 +71,7 @@ public static class Hotkeys Toggle("Visuals", "NightOwl", "Night Owl", () => Settings.NightOwl, v => Settings.NightOwl = v), Toggle("Visuals", "Shine", "Shine", () => Settings.Shine, v => Settings.Shine = v), Toggle("Visuals", "FullBright", "Full Bright", () => Settings.FullBright, v => Settings.FullBright = v), + Toggle("Visuals", "ExtendedZoom", "Extended Zoom", () => Settings.ExtendedZoom, v => Settings.ExtendedZoom = v), Toggle("World", "FreezeTime", "Freeze Time", () => Settings.FreezeTime, v => Settings.FreezeTime = v), Toggle("World", "SpawnRate", "Spawn Rate", () => Settings.SpawnRateBoost, v => Settings.SpawnRateBoost = v), diff --git a/src/TerraSharp/Core/Settings.Json.cs b/src/TerraSharp/Core/Settings.Json.cs index 9b0e06d..29494e6 100644 --- a/src/TerraSharp/Core/Settings.Json.cs +++ b/src/TerraSharp/Core/Settings.Json.cs @@ -117,6 +117,7 @@ public static partial class Settings [nameof(DangerSense)] = "Vision.DangerSense", [nameof(BiomeSight)] = "Vision.BiomeSight", [nameof(FullBright)] = "Vision.FullBright", + [nameof(ExtendedZoom)] = "Vision.ExtendedZoom", [nameof(FreezeTime)] = "World.FreezeTime", [nameof(SpawnRateBoost)] = "World.SpawnRate.Enabled", diff --git a/src/TerraSharp/Core/Settings.cs b/src/TerraSharp/Core/Settings.cs index 375b656..812cd2a 100644 --- a/src/TerraSharp/Core/Settings.cs +++ b/src/TerraSharp/Core/Settings.cs @@ -48,7 +48,7 @@ public static partial class Settings public static Color ColLocal = Color.White, ColPlayers = new(70, 200, 255), ColHostile = new(235, 60, 60), ColCritters = new(200, 170, 120), ColTown = new(80, 220, 110), ColBoss = new(255, 120, 40), ColItems = new(255, 210, 70), ColProjHostile = new(255, 80, 200), ColProjFriendly = new(120, 160, 255), ColWaypoint = new(220, 90, 255); public static bool DamageMelee, DamageRanged, DamageMagic, DamageSummon; public static Color ColDamageMelee = new(255, 110, 60), ColDamageRanged = new(120, 230, 90), ColDamageMagic = new(90, 150, 255), ColDamageSummon = new(200, 110, 255); - public static bool Spelunker, Hunter, Shine, NightOwl, DangerSense, BiomeSight, FullBright; + public static bool Spelunker, Hunter, Shine, NightOwl, DangerSense, BiomeSight, FullBright, ExtendedZoom; public static bool FreezeTime, SpawnRateBoost; public static float SpawnRateMult = 5f; diff --git a/src/TerraSharp/Patches/VisionPatches.cs b/src/TerraSharp/Patches/VisionPatches.cs index c32fb4a..c0b9e6d 100644 --- a/src/TerraSharp/Patches/VisionPatches.cs +++ b/src/TerraSharp/Patches/VisionPatches.cs @@ -1,3 +1,4 @@ +using System; using Microsoft.Xna.Framework; using Mono.Cecil.Cil; using MonoMod.Cil; @@ -12,6 +13,7 @@ internal static class VisionPatches internal static void Install(HookRegistry hooks) { FullBrightPatch.Install(hooks); + ZoomPatch.Install(hooks); } } @@ -36,3 +38,43 @@ internal static class FullBrightPatch body.Target = cursor.Prev; } } + +// Main.DoDraw and Main.UpdateViewZoomKeys clamp Main.GameZoomTarget to [1, 2]. +internal static class ZoomPatch +{ + public const float MaxZoom = 8f; + const int SearchWindow = 8; + + // Tile and wall targets cover the screen plus Main.offScreenRange on each side. + public static float MinZoom => Math.Max(Main.screenWidth / (float)(Main.screenWidth + 2 * Main.offScreenRange), Main.screenHeight / (float)(Main.screenHeight + 2 * Main.offScreenRange)); + + internal static void Install(HookRegistry hooks) + { + hooks.Modify(HookRegistry.Method(typeof(Main), "DoDraw"), ReplaceClamps); + hooks.Modify(HookRegistry.Method(typeof(Main), nameof(Main.UpdateViewZoomKeys)), ReplaceClamps); + } + + public static float Clamp(float value, float minimum, float maximum) => Settings.ExtendedZoom && Plugin.Active ? MathHelper.Clamp(value, MinZoom, MaxZoom) : MathHelper.Clamp(value, minimum, maximum); + + static void ReplaceClamps(ILContext context) + { + var cursor = new ILCursor(context); + int replaced = 0; + while (cursor.TryGotoNext(MoveType.After, i => i.MatchLdsfld(typeof(Main), nameof(Main.GameZoomTarget)))) + { + Instruction? instruction = cursor.Next; + for (int i = 0; i < SearchWindow && instruction is not null; i++, instruction = instruction.Next) + { + if (instruction.OpCode != OpCodes.Call || instruction.Operand is not Mono.Cecil.MethodReference { Name: "Clamp" }) + continue; + + instruction.Operand = context.Import(typeof(ZoomPatch).GetMethod(nameof(Clamp))!); + replaced++; + break; + } + } + + if (replaced == 0) + throw new MissingMethodException(context.Method.FullName, "Clamp"); + } +} diff --git a/src/TerraSharp/UI/MainWindow.Pages.cs b/src/TerraSharp/UI/MainWindow.Pages.cs index db4921f..6ea1c47 100644 --- a/src/TerraSharp/UI/MainWindow.Pages.cs +++ b/src/TerraSharp/UI/MainWindow.Pages.cs @@ -7,6 +7,7 @@ using Terraria.GameContent.Events; using TerraSharp.Core; using TerraSharp.Features; using TerraSharp.Features.Combat; +using TerraSharp.Patches; using TerraSharp.Simulation; namespace TerraSharp.UI; @@ -188,6 +189,13 @@ public sealed partial class MainWindow Group("View"); Toggle("Full Bright", ref Settings.FullBright); + if (Toggle("Extended Zoom", ref Settings.ExtendedZoom)) + Sub(() => + { + float zoom = Main.GameZoomTarget; + Slider("Zoom", ref zoom, ZoomPatch.MinZoom, ZoomPatch.MaxZoom, 1f, "{0:0.00}x"); + Main.GameZoomTarget = zoom; + }); } void WorldPage() diff --git a/src/TerraSharp/UI/MainWindow.Tooltips.cs b/src/TerraSharp/UI/MainWindow.Tooltips.cs index 008d163..bdaf37d 100644 --- a/src/TerraSharp/UI/MainWindow.Tooltips.cs +++ b/src/TerraSharp/UI/MainWindow.Tooltips.cs @@ -56,6 +56,7 @@ public sealed partial class MainWindow ["Danger Sense"] = "Makes traps and hazards glow.", ["Biome Sight"] = "Makes Corruption, Crimson and Hallow blocks glow.", ["Full Bright"] = "Lights everything at full brightness.", + ["Extended Zoom"] = "Zoom in up to 8x and out until the edge of what the game draws.", ["Full Heal"] = "Refills health and mana.", ["TP to Spawn"] = "Recalls like a Magic Mirror: to your bed if set, otherwise the world's spawn point.", ["Reset Learned Ballistics"] = "Clears learned projectile data. Learns it again as you shoot.", diff --git a/src/TerraSharp/UI/MainWindow.cs b/src/TerraSharp/UI/MainWindow.cs index 3642df8..541d395 100644 --- a/src/TerraSharp/UI/MainWindow.cs +++ b/src/TerraSharp/UI/MainWindow.cs @@ -34,7 +34,7 @@ public sealed partial class MainWindow : Window new Page("Movement", MovementPage, () => Count(Settings.Fly, Settings.SuperSpeed, Settings.HighJump, Settings.GravityFlip, Settings.NoFallDamage, Settings.LavaImmune, Settings.WaterWalk, Settings.NoKnockback, Settings.ExtendedReach)), new Page("Combat", CombatPage, () => Count(Settings.RapidAttack, Settings.AutoSwing, Settings.DamageBoost, Settings.NoManaCost, Settings.NoSpread, Settings.Aimbot, Settings.AutoAttack, Settings.KillAura, Settings.HitMagnet, Settings.FreezeEnemies, Settings.WeakenEnemies)), new Page("Items", ItemsPage, () => Count(Settings.InstantBreak, Settings.InfItems, Settings.ItemVacuum, Settings.AutoFish, Settings.Sonar)), - new Page("Visuals", VisualsPage, () => Count(Settings.Esp, Settings.Spelunker, Settings.Hunter, Settings.Shine, Settings.NightOwl, Settings.DangerSense, Settings.BiomeSight, Settings.FullBright)), + new Page("Visuals", VisualsPage, () => Count(Settings.Esp, Settings.Spelunker, Settings.Hunter, Settings.Shine, Settings.NightOwl, Settings.DangerSense, Settings.BiomeSight, Settings.FullBright, Settings.ExtendedZoom)), new Page("World", WorldPage, () => Count(Settings.FreezeTime, Settings.SpawnRateBoost)), new Page("Keys", KeysPage, () => 0), ];