Skip to content

Commit

Permalink
Added npc encounter patch (#131) which allows npc encounters to spawn…
Browse files Browse the repository at this point in the history
… again
  • Loading branch information
thorwin99 committed Jan 14, 2022
1 parent 8c75209 commit 1215059
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
52 changes: 52 additions & 0 deletions SEWorldGenPlugin/Patches/PatchAsteroidGeneration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using HarmonyLib;
using Sandbox.Game.World.Generator;
using SEWorldGenPlugin.Session;
using VRage.Library.Utils;
using VRage.Noise;

namespace SEWorldGenPlugin.Patches
{
/// <summary>
/// Patch class to patch vanilla asteroid generation, so that encounter generation is still possible
/// </summary>
public class PatchAsteroidGeneration : HarmonyPatchBase
{
public PatchAsteroidGeneration() : base("Asteroid generator patch (encounter fix)")
{
}

/// <summary>
/// Prefix patch applied to <see cref="MyProceduralAsteroidCellGenerator.GenerateObjects(System.Collections.Generic.List{MyObjectSeed}, System.Collections.Generic.HashSet{VRage.Game.MyObjectSeedParams})"/>
/// </summary>
/// <param name="cell">Cell to generate objects for</param>
/// <param name="objectSeed">Object to generate for cell</param>
/// <param name="index"></param>
/// <param name="random"></param>
/// <param name="densityFunctionFilled"></param>
/// <param name="densityFunctionRemoved"></param>
/// <returns>True when original method should execute, false if skipped</returns>
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));
}
}
}
1 change: 1 addition & 0 deletions SEWorldGenPlugin/SEWorldGenPlugin.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@
<Compile Include="GUI\AdminMenu\MyAdminMenuExtension.cs" />
<Compile Include="GUI\AdminMenu\SubMenus\StarSystemDesigner\MyStarSystemDesignerOrbitMenu.cs" />
<Compile Include="GUI\AdminMenu\SubMenus\StarSystemDesigner\MyOrbitRenderObject.cs" />
<Compile Include="Patches\PatchAsteroidGeneration.cs" />
<Compile Include="Session\MyAdminMenuExtensionRegister.cs" />
<Compile Include="GUI\AdminMenu\MyPluginAdminMenuSubMenu.cs" />
<Compile Include="GUI\AdminMenu\SubMenus\MyPlanetSpawnMenu.cs" />
Expand Down
3 changes: 3 additions & 0 deletions SEWorldGenPlugin/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand All @@ -108,6 +109,8 @@ private void Patch()

var asteroidPatch = new PatchOverlapAllAsteroidSeedsInSphere();
asteroidPatch.ApplyPatch(harmony);
var encounterPatch = new PatchAsteroidGeneration();
encounterPatch.ApplyPatch(harmony);
}
}
}

0 comments on commit 1215059

Please sign in to comment.