From 12150598d740c22e2cf8e161498456efddc738c0 Mon Sep 17 00:00:00 2001 From: Thorwin Vogt Date: Fri, 14 Jan 2022 12:29:25 +0100 Subject: [PATCH] Added npc encounter patch (#131) which allows npc encounters to spawn again --- .../MyProceduralGeneratorComponent.cs | 2 +- .../Patches/PatchAsteroidGeneration.cs | 52 +++++++++++++++++++ SEWorldGenPlugin/SEWorldGenPlugin.csproj | 1 + SEWorldGenPlugin/Startup.cs | 3 ++ 4 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 SEWorldGenPlugin/Patches/PatchAsteroidGeneration.cs diff --git a/SEWorldGenPlugin/Generator/ProceduralGeneration/MyProceduralGeneratorComponent.cs b/SEWorldGenPlugin/Generator/ProceduralGeneration/MyProceduralGeneratorComponent.cs index cf47ee2..f85de11 100644 --- a/SEWorldGenPlugin/Generator/ProceduralGeneration/MyProceduralGeneratorComponent.cs +++ b/SEWorldGenPlugin/Generator/ProceduralGeneration/MyProceduralGeneratorComponent.cs @@ -90,7 +90,7 @@ public override void LoadData() m_procDensity = MySession.Static.Settings.ProceduralDensity; m_seed = MySession.Static.Settings.ProceduralSeed; - if (MySettingsSession.Static.Settings.GeneratorSettings.AsteroidGenerator == ObjectBuilders.AsteroidGenerationMethod.PLUGIN) + if (MySettingsSession.Static.Settings.GeneratorSettings.AsteroidGenerator == ObjectBuilders.AsteroidGenerationMethod.PLUGIN && !MySettings.Static.Settings.EnablePatching) { MySession.Static.Settings.ProceduralDensity = 0f; } diff --git a/SEWorldGenPlugin/Patches/PatchAsteroidGeneration.cs b/SEWorldGenPlugin/Patches/PatchAsteroidGeneration.cs new file mode 100644 index 0000000..a91155c --- /dev/null +++ b/SEWorldGenPlugin/Patches/PatchAsteroidGeneration.cs @@ -0,0 +1,52 @@ +using HarmonyLib; +using Sandbox.Game.World.Generator; +using SEWorldGenPlugin.Session; +using VRage.Library.Utils; +using VRage.Noise; + +namespace SEWorldGenPlugin.Patches +{ + /// + /// Patch class to patch vanilla asteroid generation, so that encounter generation is still possible + /// + public class PatchAsteroidGeneration : HarmonyPatchBase + { + public PatchAsteroidGeneration() : base("Asteroid generator patch (encounter fix)") + { + } + + /// + /// Prefix patch applied to + /// + /// Cell to generate objects for + /// Object to generate for cell + /// + /// + /// + /// + /// True when original method should execute, false if skipped + public static bool Prefix(MyProceduralCell cell, MyObjectSeed objectSeed, ref int index, MyRandom random, IMyModule densityFunctionFilled, IMyModule densityFunctionRemoved) + { + if (!MySettingsSession.Static.Settings.Enabled) return true; + + if (MySettingsSession.Static.Settings.GeneratorSettings.AsteroidGenerator != ObjectBuilders.AsteroidGenerationMethod.PLUGIN) return true; + + if(objectSeed.Params.Type == VRage.Game.MyObjectSeedType.Asteroid || objectSeed.Params.Type == VRage.Game.MyObjectSeedType.AsteroidCluster) + { + return false; // skip original method + } + + return true; + } + + public override void ApplyPatch(Harmony harmony) + { + base.ApplyPatch(harmony); + + var baseMethod = typeof(MyProceduralAsteroidCellGenerator).GetMethod("GenerateObject", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance); + var prefix = typeof(PatchAsteroidGeneration).GetMethod("Prefix"); + + harmony.Patch(baseMethod, prefix: new HarmonyMethod(prefix)); + } + } +} diff --git a/SEWorldGenPlugin/SEWorldGenPlugin.csproj b/SEWorldGenPlugin/SEWorldGenPlugin.csproj index 87acf46..4a567c4 100644 --- a/SEWorldGenPlugin/SEWorldGenPlugin.csproj +++ b/SEWorldGenPlugin/SEWorldGenPlugin.csproj @@ -201,6 +201,7 @@ + diff --git a/SEWorldGenPlugin/Startup.cs b/SEWorldGenPlugin/Startup.cs index 08cd31c..9d95f4a 100644 --- a/SEWorldGenPlugin/Startup.cs +++ b/SEWorldGenPlugin/Startup.cs @@ -90,6 +90,7 @@ private void TryEnablePatches() catch (Exception e) { MyPluginLog.Log("Something went wrong while patching: ", LogLevel.ERROR); + MyPluginLog.Log(e.Message, LogLevel.ERROR); MyPluginLog.Log(e.StackTrace, LogLevel.ERROR); } } @@ -108,6 +109,8 @@ private void Patch() var asteroidPatch = new PatchOverlapAllAsteroidSeedsInSphere(); asteroidPatch.ApplyPatch(harmony); + var encounterPatch = new PatchAsteroidGeneration(); + encounterPatch.ApplyPatch(harmony); } } }