gas grid dispersoin

This commit is contained in:
2026-10-07 02:08:17 -04:00
parent b6f255ebbb
commit 063d5641d0
5 changed files with 112 additions and 2 deletions
+111 -1
View File
@@ -72,13 +72,89 @@ public sealed class GasGridOptimization : ClassWithFishPrepatches
}
}
#if V1_6
public abstract class PackedGasPatch : FishPrepatch
{
public override bool ShowSettings => false;
public override bool Enabled
{
get => base.Enabled && Get<TickPatch>().Enabled;
set => base.Enabled = value;
}
protected static uint PackedDensityAt(GasGrid grid, int index)
{
var grids = grid.ParallelGasGrids();
return ((uint)grids[3].DensityAt(index) << 24)
| ((uint)grids[2].DensityAt(index) << 16)
| ((uint)grids[1].DensityAt(index) << 8)
| grids[0].DensityAt(index);
}
}
public sealed class GetDirectIndexPatch : PackedGasPatch
{
public override MethodBase TargetMethodBase { get; }
= AccessTools.DeclaredMethod(typeof(GasGrid), nameof(GasGrid.GetDirect), [typeof(int)]);
public override void Transpiler(ILProcessor ilProcessor, ModuleDefinition module)
=> ilProcessor.ReplaceBodyWith(ReplacementBody);
public static uint ReplacementBody(GasGrid __instance, int index)
=> PackedDensityAt(__instance, index);
}
public sealed class GetDirectCellPatch : PackedGasPatch
{
public override MethodBase TargetMethodBase { get; }
= AccessTools.DeclaredMethod(typeof(GasGrid), nameof(GasGrid.GetDirect), [typeof(IntVec3)]);
public override void Transpiler(ILProcessor ilProcessor, ModuleDefinition module)
=> ilProcessor.ReplaceBodyWith(ReplacementBody);
public static uint ReplacementBody(GasGrid __instance, IntVec3 c)
=> PackedDensityAt(__instance, c.CellToIndex(__instance.map));
}
public sealed class SetPackedDirectPatch : PackedGasPatch
{
public override MethodBase TargetMethodBase { get; }
= AccessTools.DeclaredMethod(typeof(GasGrid), nameof(GasGrid.SetDirect),
[typeof(int), typeof(uint)]);
public static void Postfix(GasGrid __instance, int index)
{
var density = __instance.gasDensity[index];
SetDirectPatch.ReplacementBody(__instance, index, (byte)density, (byte)(density >> 8),
(byte)(density >> 16), (byte)(density >> 24));
}
}
public sealed class ClearCellUnsafePatch : PackedGasPatch
{
public override MethodBase TargetMethodBase { get; }
= AccessTools.DeclaredMethod(typeof(GasGrid), nameof(GasGrid.ClearCellUnsafe));
public static void Postfix(GasGrid __instance, IntVec3 cell)
{
var index = cell.CellToIndex(__instance.map);
var grids = __instance.ParallelGasGrids();
for (var i = 0; i < grids.Length; i++)
grids[i].SetDirect(index, 0);
}
}
#endif
public sealed class TickPatch : FishPrepatch
{
public override List<Type> LinkedPatches { get; } =
[
typeof(SetDirectPatch),
#if V1_4
typeof(CalculateGasEffectsPatch),
typeof(ColorAtPatch),
#if V1_6
typeof(GetDirectIndexPatch), typeof(GetDirectCellPatch),
typeof(SetPackedDirectPatch), typeof(ClearCellUnsafePatch),
#endif
typeof(AddGasPatch), typeof(AnyGasAtPatch),
typeof(DensityAtPatch), typeof(Debug_ClearAllPatch), typeof(Debug_FillAllPatch),
@@ -174,6 +250,35 @@ public sealed class GasGridOptimization : ClassWithFishPrepatches
#endif
}
public sealed class CalculateGasEffectsPatch : FishPrepatch
{
public override bool ShowSettings => false;
public override bool Enabled
{
get => base.Enabled && Get<TickPatch>().Enabled;
set => base.Enabled = value;
}
public override MethodBase TargetMethodBase { get; }
= AccessTools.PropertyGetter(typeof(GasGrid), nameof(GasGrid.CalculateGasEffects));
public override void Transpiler(ILProcessor ilProcessor, ModuleDefinition module)
=> ilProcessor.ReplaceBodyWith(ReplacementBody);
public static bool ReplacementBody(GasGrid __instance)
{
// Vanilla uses this to decide whether to equalize gas through buildings.
var gasGrids = __instance.ParallelGasGrids();
for (var i = 0; i < gasGrids.Length; i++)
{
if (gasGrids[i].CalculateGasEffects)
return true;
}
return false;
}
}
#if !V1_4
public class ParallelJob : IJobParallelFor
{
@@ -186,6 +291,11 @@ public sealed class GasGridOptimization : ClassWithFishPrepatches
public sealed class ColorAtPatch : FishPrepatch
{
public override bool ShowSettings => false;
public override bool Enabled
{
get => base.Enabled && Get<TickPatch>().Enabled;
set => base.Enabled = value;
}
public override MethodBase TargetMethodBase { get; }
#if V1_4