From b8a11d850dcf3618dc3800a37db1ba770f96b58c Mon Sep 17 00:00:00 2001 From: DominicKarma <51094361+DominicKarma@users.noreply.github.com> Date: Thu, 8 Oct 2020 16:47:40 -0500 Subject: [PATCH] Town NPC spawn manipulation changes --- CalamityConfig.cs | 18 ++++++++++++++++++ ILEditing/ILChanges.cs | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+) diff --git a/CalamityConfig.cs b/CalamityConfig.cs index 86eda6b763..02f132d1c7 100644 --- a/CalamityConfig.cs +++ b/CalamityConfig.cs @@ -128,6 +128,24 @@ namespace CalamityMod [Tooltip("Sets Reactive Boss DR to always be full strength, even if the boss has already been defeated.\nIf disabled, the effect is only 66% as powerful after the boss has been defeated.\n\nReactive Boss DR makes bosses smoothly take less damage if they are being killed very quickly.\nIn most cases, the system has no noticeable effect.")] public bool FullPowerReactiveBossDR { get; set; } + [Label("Let Town NPCs spawn at night.")] + [BackgroundColor(192, 54, 64, 192)] + [DefaultValue(false)] + [Tooltip("Allows you to determine if town NPCs (including the Old Man) can spawn at night.")] + public bool CanTownNPCsSpawnAtNight { get; set; } + + private const int MinTownNPCSpawnMultiplier = 1; + private const int MaxTownNPCSpawnMultiplier = 10; + + [Label("Town NPC Spawn Rate Multiplier")] + [BackgroundColor(192, 54, 64, 192)] + [Range(MinTownNPCSpawnMultiplier, MaxTownNPCSpawnMultiplier)] + [Increment(1)] + [DrawTicks] + [DefaultValue(MinTownNPCSpawnMultiplier)] + [Tooltip("Makes town NPCs spawn more quickly, the higher this value is.")] + public int TownNPCSpawnRateMultiplier { get; set; } + private const float MinBossHealthBoost = 0f; private const float MaxBossHealthBoost = 900f; diff --git a/ILEditing/ILChanges.cs b/ILEditing/ILChanges.cs index a80c62f190..b4056fd154 100644 --- a/ILEditing/ILChanges.cs +++ b/ILEditing/ILChanges.cs @@ -3,6 +3,7 @@ using Mono.Cecil.Cil; using MonoMod.Cil; using System; using System.Collections.Generic; +using System.Reflection; using Terraria; using Terraria.GameInput; using Terraria.ID; @@ -27,9 +28,13 @@ namespace CalamityMod.ILEditing /// public static void Initialize() { + var spawnTownNPCMethod = typeof(Main).GetMethod("UpdateTime_SpawnTownNPCs", BindingFlags.Static | BindingFlags.NonPublic); + cachedUpdateTime_SpawnTownNPCs = Delegate.CreateDelegate(typeof(Action), spawnTownNPCMethod) as Action; + On.Terraria.NPC.SlimeRainSpawns += PreventBossSlimeRainSpawns; ApplyLifeBytesChanges(); SlideDungeonOver(); + AlterTownNPCSpawnRate(); LabDoorFixes(); PreventStupidTreesNearOcean(); } @@ -79,6 +84,42 @@ namespace CalamityMod.ILEditing if (!Main.player[plr].Calamity().bossZen) orig(plr); } + + private static Action cachedUpdateTime_SpawnTownNPCs; + + private static void AlterTownNPCSpawnRate() + { + IL.Terraria.Main.UpdateTime += (il) => + { + // Don't do town NPC spawning at the end (after a !Main.dayTime return). + // Do it at the beginning in spite of time. + + var cursor = new ILCursor(il); + cursor.EmitDelegate(() => + { + // A cached delegate is used here instead of direct reflection for performance reasons + // since UpdateTime is called every frame. + if (Main.dayTime || CalamityConfig.Instance.CanTownNPCsSpawnAtNight) + cachedUpdateTime_SpawnTownNPCs(); + }); + + if (!cursor.TryGotoNext(MoveType.Before, i => i.MatchCallOrCallvirt
("UpdateTime_SpawnTownNPCs"))) + { + CalamityMod.Instance.Logger.Warn("Town NPC spawn editing code failed."); + return; + } + + cursor.Emit(OpCodes.Ret); + }; + + On.Terraria.Main.UpdateTime_SpawnTownNPCs += (orig) => + { + int oldWorldRate = Main.worldRate; + Main.worldRate *= CalamityConfig.Instance.TownNPCSpawnRateMultiplier; + orig(); + Main.worldRate = oldWorldRate; + }; + } #endregion #region IL Editing Injection Functions