Skip to content

Commit

Permalink
Fix VEF issue with thing spawning on map generation (including Vanill…
Browse files Browse the repository at this point in the history
…a Vehicles Tier 3 wrecks) (#502)

This should fix issues with things being spawned on map generation (ObjectSpawnsDef) by seeding the relevant method.

The issue is that a method passed to `LongEventHandler.ExecuteWhenFinished` during a `MapGenerator:GenerateMap` call (perhaps others as well?) would have a different seed between players. Should this be something we fix in MP? It doesn't affect vanilla only game, as (I believe) every affected `ExecuteWhenFinished` call seeds the RNG. And almost every `ExecuteWhenFinished` call is affecting graphics/audio, so it doesn't affect Multiplayer.
  • Loading branch information
SokyranTheDragon authored Dec 11, 2024
1 parent 35df976 commit 0eb9cad
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions Source/Mods/VanillaExpandedFramework.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public VanillaExpandedFramework(ModContentPack mod)
(PatchStaticCaches, "Static caches", false),
(PatchGraphicCustomizationDialog, "Graphic Customization Dialog", true),
(PatchDraftedAi, "Drafted AI", true),
(PatchMapObjectGeneration, "Thing spawning on map generation (ObjectSpawnsDef)", false),
];

foreach (var (patchMethod, componentName, latePatch) in patches)
Expand Down Expand Up @@ -1701,5 +1702,31 @@ private static void SyncDraftedActionData(SyncWorker sync, ref object draftedAct
}

#endregion

#region Thing spawning on map generation (ObjectSpawnsDef)

private static void PatchMapObjectGeneration()
{
// When calling "LongEventHandler.ExecuteWhenFinished" inside a
// "MapGenerator:GenerateMap" call the seed will differ between
// players. Is this something we should look into in MP itself?
MpCompat.harmony.Patch(AccessTools.DeclaredMethod("VFECore.MapGenerator_GenerateMap_Patch:DoMapSpawns"),
prefix: new HarmonyMethod(PreDoSpawns),
finalizer: new HarmonyMethod(PostDoSpawns));
}

private static void PreDoSpawns(Map map)
{
if (MP.IsInMultiplayer)
Rand.PushState(Gen.HashCombineInt(Gen.HashCombineInt(map.uniqueID, map.Tile), Find.TickManager.TicksGame));
}

private static void PostDoSpawns()
{
if (MP.IsInMultiplayer)
Rand.PopState();
}

#endregion
}
}

0 comments on commit 0eb9cad

Please sign in to comment.