Skip to content

Commit

Permalink
v3.4 speeds up spawing zombies by 6000% and adds reentrant safe grid …
Browse files Browse the repository at this point in the history
…code to be compatible with optimizers
  • Loading branch information
pardeike committed Dec 27, 2022
1 parent 0cae426 commit c3d56b9
Show file tree
Hide file tree
Showing 8 changed files with 145 additions and 114 deletions.
Binary file modified 1.4/Assemblies/ZombieLand.dll
Binary file not shown.
2 changes: 1 addition & 1 deletion About/Manifest.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Manifest>
<identifier>net.pardeike.rimworld.mod.zombieland</identifier>
<version>3.3.1.0</version>
<version>3.4.0.0</version>
<targetVersions>
<li>1.0.0</li>
<li>1.1.0</li>
Expand Down
66 changes: 43 additions & 23 deletions Source/TickManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,24 @@ public static class ZombieTicker
public static void DoSingleTick()
{
if (RimThreaded == null)
managers.Do(tickManager => tickManager.ZombieTicking());
managers.Do(tickManager =>
{
switch (tickManager.isInitialized)
{
case 0:
Log.Error("Fatal error! Zombieland's TickManager is not initialized. This should never happen unless you're using another mod that caused an error in MapComponent.FinalizeInit");
break;
case 1:
Log.Error("Fatal error! Zombieland's TickManager is not initialized. The base implementation never returned which means another mod is causing an error in MapComponent.FinalizeInit");
break;
case 2:
Log.Error("Fatal error! Zombieland's TickManager is not initialized because its FinalizeInit method caused an error. Maybe another mod caused this error indirectly, you should report this.");
break;
case 3:
tickManager.ZombieTicking();
break;
}
});
}

public static float PercentTicking
Expand All @@ -47,6 +64,7 @@ public static float PercentTicking

public class TickManager : MapComponent
{
public int isInitialized = 0;
int populationSpawnCounter;

int nextVisibleGridUpdate;
Expand All @@ -71,18 +89,18 @@ public class TickManager : MapComponent
Sustainer zombiesAmbientSound;
float zombiesAmbientSoundVolume;

public readonly HashSet<Zombie> hummingZombies = new HashSet<Zombie>();
public readonly HashSet<Zombie> hummingZombies = new();
Sustainer electricSustainer;

public Queue<ThingWithComps> colonistsConverter = new Queue<ThingWithComps>();
public Queue<Action<Map>> rimConnectActions = new Queue<Action<Map>>();
public Queue<ThingWithComps> colonistsConverter = new();
public Queue<Action<Map>> rimConnectActions = new();

public List<IntVec3> explosions = new List<IntVec3>();
public IncidentInfo incidentInfo = new IncidentInfo();
public List<IntVec3> explosions = new();
public IncidentInfo incidentInfo = new();
public ZombiePathing zombiePathing;

public List<SoSTools.Floater> floatingSpaceZombiesBack = new List<SoSTools.Floater>();
public List<SoSTools.Floater> floatingSpaceZombiesFore = new List<SoSTools.Floater>();
public List<SoSTools.Floater> floatingSpaceZombiesBack = new();
public List<SoSTools.Floater> floatingSpaceZombiesFore = new();

public TickManager(Map map) : base(map)
{
Expand All @@ -109,7 +127,9 @@ public TickManager(Map map) : base(map)

public override void FinalizeInit()
{
isInitialized = 1;
base.FinalizeInit();
isInitialized = 2;

Tools.nextPlayerReachableRegionsUpdate = 0;

Expand Down Expand Up @@ -159,6 +179,8 @@ public override void FinalizeInit()
taskTicker = TickTasks();
while (taskTicker.Current as string != "end")
_ = taskTicker.MoveNext();

isInitialized = 3;
}

public override void MapRemoved()
Expand All @@ -185,17 +207,14 @@ public override void ExposeData()

if (Scribe.mode == LoadSaveMode.PostLoadInit)
{
if (allZombiesCached == null)
allZombiesCached = new HashSet<Zombie>();
allZombiesCached ??= new HashSet<Zombie>();
allZombiesCached = allZombiesCached.Where(zombie => zombie != null && zombie.Spawned && zombie.Dead == false).ToHashSet();

if (allZombieCorpses == null)
allZombieCorpses = new List<ZombieCorpse>();
allZombieCorpses ??= new List<ZombieCorpse>();
allZombieCorpses = allZombieCorpses.Where(corpse => corpse.DestroyedOrNull() == false && corpse.Spawned).ToList();

runZombiesForNewIncident = true;
if (explosions == null)
explosions = new List<IntVec3>();
explosions ??= new List<IntVec3>();
}
}

Expand Down Expand Up @@ -410,8 +429,7 @@ void FetchAvoidGrid()
{
if (Tools.ShouldAvoidZombies() == false)
{
if (emptyAvoidGrid == null)
emptyAvoidGrid = new AvoidGrid(map);
emptyAvoidGrid ??= new AvoidGrid(map);
avoidGrid = emptyAvoidGrid;
return;
}
Expand Down Expand Up @@ -521,8 +539,7 @@ public void UpdateElectricalHumming()
return;
}

if (electricSustainer == null)
electricSustainer = CustomDefs.ZombieElectricHum.TrySpawnSustainer(SoundInfo.OnCamera(MaintenanceType.None));
electricSustainer ??= CustomDefs.ZombieElectricHum.TrySpawnSustainer(SoundInfo.OnCamera(MaintenanceType.None));

if (hummingZombies.Count == 0)
{
Expand Down Expand Up @@ -590,8 +607,7 @@ IEnumerator TickTasks()
yield return null;
if (Constants.USE_SOUND && ZombieSettings.Values.playCreepyAmbientSound)
{
if (zombiesAmbientSound == null)
zombiesAmbientSound = CustomDefs.ZombiesClosingIn.TrySpawnSustainer(SoundInfo.OnCamera(MaintenanceType.None));
zombiesAmbientSound ??= CustomDefs.ZombiesClosingIn.TrySpawnSustainer(SoundInfo.OnCamera(MaintenanceType.None));

if (volume < zombiesAmbientSoundVolume)
zombiesAmbientSoundVolume -= 0.0001f;
Expand All @@ -600,21 +616,25 @@ IEnumerator TickTasks()
zombiesAmbientSound.info.volumeFactor = zombiesAmbientSoundVolume;
}
else
{
StopAmbientSound();
yield return null;
}

yield return null;
if (colonistsConverter.Count > 0 && map != null)
{
var pawn = colonistsConverter.Dequeue();
Tools.ConvertToZombie(pawn, map);
yield return null;
}
yield return null;
if (rimConnectActions.Count > 0 && map != null)
{
var action = rimConnectActions.Dequeue();
action(map);
yield return null;
}
yield return "end";

yield return "end"; // must be called "end"!
}
}

Expand Down
7 changes: 4 additions & 3 deletions Source/VariableGraphic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public IEnumerator InitIterativ(GraphicRequest req, int n, int points)
var stain = ZombieStains.GetRandom(points, req.path.Contains("Naked"));
var it = data.ApplyStainsIterativ(stain.Key, Rand.Bool, Rand.Bool);
while (it.MoveNext())
yield return null;
yield return it.Current;
points -= stain.Value;

hash = Gen.HashCombine(hash, stain);
Expand All @@ -48,7 +48,7 @@ public IEnumerator InitIterativ(GraphicRequest req, int n, int points)
mats[n] = new VariableMaterial(request, data);
}

public static GraphicRequest minimal = new GraphicRequest();
public static GraphicRequest minimal = new();
public override void Init(GraphicRequest req)
{
if (req == minimal)
Expand All @@ -67,7 +67,8 @@ public override void Init(GraphicRequest req)
for (var i = 0; i < 4; i++)
{
var iterator = InitIterativ(req, i, ZombieStains.maxStainPoints);
while (iterator.MoveNext()) ;
while (iterator.MoveNext())
;
}
}

Expand Down
11 changes: 7 additions & 4 deletions Source/ZombieAvoider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,15 @@ public class ZombieAvoider

ConcurrentQueue<AvoidGrid> QueueForMap(Map map)
{
if (resultQueues.TryGetValue(map, out var queue) == false)
lock (requestQueue)
{
queue = new ConcurrentQueue<AvoidGrid>(true);
resultQueues.Add(map, queue);
if (resultQueues.TryGetValue(map, out var queue) == false)
{
queue = new ConcurrentQueue<AvoidGrid>(true);
resultQueues.Add(map, queue);
}
return queue;
}
return queue;
}

public ZombieAvoider()
Expand Down
Loading

0 comments on commit c3d56b9

Please sign in to comment.