Skip to content

Commit

Permalink
More fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
pardeike committed Nov 6, 2023
1 parent 7a8e192 commit c2323ff
Show file tree
Hide file tree
Showing 12 changed files with 170 additions and 38 deletions.
Binary file modified Assemblies/ZombieLand.dll
Binary file not shown.
13 changes: 13 additions & 0 deletions Languages/English/Keyed/Text.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,19 @@
<Difficulty_ZombielandThreatScale_Info>Controls the difficulty of almost all things in Zombieland like zombie abilities, threat forecast and much more.</Difficulty_ZombielandThreatScale_Info>
<Difficulty_ZombielandThreatScale_Label>Zombieland threat scale</Difficulty_ZombielandThreatScale_Label>

<ZombieSuicideBomber>Suicide bomber zombie</ZombieSuicideBomber>
<ZombieToxicSplasher>Toxic splasher zombie</ZombieToxicSplasher>
<ZombieTanky>Tank breacher zombie</ZombieTanky>
<ZombieMiner>Miner zombie</ZombieMiner>
<ZombieElectrifier>Electrifier zombie</ZombieElectrifier>
<ZombieAlbino>Albino zombie</ZombieAlbino>
<ZombieDarkSlimer>Dark slimer zombie</ZombieDarkSlimer>
<ZombieHealer>Healer zombie</ZombieHealer>
<ZombieChild>Zombie child</ZombieChild>
<ZombieWeak>Weak zombie</ZombieWeak>
<ZombieStrong>Strong zombie</ZombieStrong>
<ZombieSimple>Simple zombie</ZombieSimple>

<LetterLabelZombiesRising>Zombie attack</LetterLabelZombiesRising>
<ZombiesRising>A group of zombies appears</ZombiesRising>

Expand Down
16 changes: 8 additions & 8 deletions Source/CETools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,16 @@ public class CETools
public static void Init(Harmony harmony)
{
latePatching = true;
_ = new PatchClassProcessor(harmony, typeof(Patch1)).Patch();
_ = new PatchClassProcessor(harmony, typeof(Patch2)).Patch();
_ = new PatchClassProcessor(harmony, typeof(Patch3)).Patch();
_ = new PatchClassProcessor(harmony, typeof(Patch4)).Patch();
_ = new PatchClassProcessor(harmony, typeof(CETools_Patch1)).Patch();
_ = new PatchClassProcessor(harmony, typeof(CETools_Patch2)).Patch();
_ = new PatchClassProcessor(harmony, typeof(CETools_Patch3)).Patch();
_ = new PatchClassProcessor(harmony, typeof(CETools_Patch4)).Patch();
latePatching = false;
}
}

[HarmonyPatch]
class Patch1
class CETools_Patch1
{
static bool Prepare() => CETools.latePatching && TargetMethod() != null;
static MethodInfo TargetMethod()
Expand All @@ -48,7 +48,7 @@ static bool Prefix(ref DamageInfo dinfo)
}

[HarmonyPatch]
static class Patch2
static class CETools_Patch2
{
static bool Prepare() => CETools.latePatching && TargetMethod() != null;
static MethodBase TargetMethod()
Expand Down Expand Up @@ -95,7 +95,7 @@ public static float GetDistanceTraveled(float velocity, float angle, float shotH
}

[HarmonyPatch]
static class Patch3
static class CETools_Patch3
{
static bool Prepare() => CETools.latePatching && TargetMethod() != null;
static MethodBase TargetMethod()
Expand Down Expand Up @@ -152,7 +152,7 @@ static bool Prefix(ref DamageInfo originalDinfo, Pawn pawn, BodyPartRecord hitPa
}

[HarmonyPatch]
static class Patch4
static class CETools_Patch4
{
static bool Prepare() => CETools.latePatching && TargetMethod() != null;
static MethodBase TargetMethod()
Expand Down
13 changes: 6 additions & 7 deletions Source/CountingCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,16 @@ public CountingCache(int maxAccessCount)
cache = new Dictionary<K, (V, int)>();
}

public V Get(K key, Func<K, V> valueFetcher)
public V Get(K key, Func<K, V> valueFetcher, Action<V> clearer)
{
if (cache.TryGetValue(key, out var cacheEntry))
if (cache.TryGetValue(key, out var cacheEntry) && cacheEntry.Count < maxAccessCount)
{
if (cacheEntry.Count < maxAccessCount)
{
cache[key] = (cacheEntry.Value, cacheEntry.Count + 1);
return cacheEntry.Value;
}
cache[key] = (cacheEntry.Value, cacheEntry.Count + 1);
return cacheEntry.Value;
}
var value = valueFetcher(key);
if (cache.ContainsKey(key))
clearer(cache[key].Value);
cache[key] = (value, 1);
return value;
}
Expand Down
29 changes: 29 additions & 0 deletions Source/DubsTools.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using HarmonyLib;
using Verse;
using static HarmonyLib.AccessTools;

namespace ZombieLand
{
public class DubsTools
{
public static void Init()
{
var harmony = new Harmony("net.pardeike.zombieland.dubs");
var method = Method("Analyzer.Fixes.H_DrawNamesFix:Prefix");
if (method != null)
{
var prefix = SymbolExtensions.GetMethodInfo((bool b) => Prefix(default, ref b));
harmony.Patch(method, prefix: new HarmonyMethod(prefix));
}
}

static bool Prefix([HarmonyArgument("__instance")] PawnUIOverlay instance, ref bool __result)
{
if (instance.pawn is not Zombie)
return true;

__result = true;
return false;
}
}
}
13 changes: 10 additions & 3 deletions Source/Patches.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
using System.Reflection.Emit;
using System.Text;
using UnityEngine;
using UnityEngine.UIElements.Experimental;
using Verse;
using Verse.AI;
using Verse.Sound;
Expand Down Expand Up @@ -52,6 +51,7 @@ static Patches()
AlienTools.Init();
VehicleTools.Init();
Customization.Init();
DubsTools.Init();
});

// for debugging
Expand Down Expand Up @@ -1752,14 +1752,18 @@ static bool ShouldAvoid(Pawn pawn, Thing thing, bool forced)
if (forced || pawn.ActivePartOfColony() == false)
return false;

var map = thing?.Map ?? pawn.Map;
if (map == null || thing.Position.InBounds(map) == false)
return false;

if (Tools.ShouldAvoidZombies(pawn) == false)
return false;

var tickManager = pawn.Map?.GetComponent<TickManager>();
var tickManager = map.GetComponent<TickManager>();
if (tickManager == null)
return false;

return tickManager.avoidGrid.ShouldAvoid(thing.Map, thing.Position);
return tickManager.avoidGrid.ShouldAvoid(map, thing.Position);
}

static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions, ILGenerator generator)
Expand Down Expand Up @@ -1966,6 +1970,9 @@ static void DebugGrid(StringBuilder builder)
else
idx = -1;
_ = builder.AppendLine($"Smart wandering index: {idx}");

var pathGrid = map.pathing.For(MapInfo.traverseParms).pathGrid;
_ = builder.AppendLine($"Smart wandering walkable: {region.Cells.Count(pathGrid.WalkableFast)} of {region.Cells.Count()}");
}
var destination = pathing.GetWanderDestination(pos);
var fromStr = from.IsValid ? from.ToString() : "null";
Expand Down
6 changes: 6 additions & 0 deletions Source/Tools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1188,6 +1188,12 @@ public static void Continue(this Stopwatch sw)
sw.Start();
}

public static void LogSelected(this Zombie zombie, string text)
{
if (UI.MouseCell() == zombie.Position)
Log.Warning($"{zombie.Position.SimplePos()}: {text}");
}

public static string ToHourString(this int ticks, bool relativeToAbsoluteGameTime = true)
{
var t = relativeToAbsoluteGameTime ? ticks - GenTicks.TicksAbs : ticks;
Expand Down
45 changes: 45 additions & 0 deletions Source/Zombie.cs
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,51 @@ public void Dispose()
GC.SuppressFinalize(this);
}

string ZombieType
{
get
{
if (IsSuicideBomber)
return "ZombieSuicideBomber".Translate();
if (isToxicSplasher)
return "ZombieToxicSplasher".Translate();
if (IsTanky)
return "ZombieTanky".Translate();
if (isMiner)
return "ZombieMiner".Translate();
if (isElectrifier)
return "ZombieElectrifier".Translate();
if (isAlbino)
return "ZombieAlbino".Translate();
if (isDarkSlimer)
return "ZombieDarkSlimer".Translate();
if (isHealer)
return "ZombieHealer".Translate();
if (story.bodyType == BodyTypeDefOf.Child)
return "ZombieChild".Translate();
if (story.bodyType == BodyTypeDefOf.Thin)
return "ZombieWeak".Translate();
if (story.bodyType == BodyTypeDefOf.Fat)
return "ZombieStrong".Translate();
return "ZombieSimple".Translate();
}
}

public override string LabelMouseover
{
get
{
if (Name is NameTriple nameTriple)
{
var last = nameTriple.Last;
if (last.StartsWith("#"))
return $"{ZombieType} {last.Substring(1)}";
return $"{ZombieType} {last}";
}
return ZombieType;
}
}

public void Randomize8()
{
var nextIndex = Constants.random.Next(8);
Expand Down
49 changes: 33 additions & 16 deletions Source/ZombieAreaManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,23 @@ namespace ZombieLand
{
public static class ZombieAreaManager
{
public static Map lastMap = null;
public static Dictionary<Pawn, Area> pawnsInDanger = new();
public static bool warningShowing = false;
public static IEnumerator stateUpdater = StateUpdater();

static Zombie[] AllZombies(Map map)
{
try
{
return map.GetComponent<TickManager>().allZombiesCached.ToArray();
}
catch (Exception)
{
return new Zombie[0];
}
}

static IEnumerator StateUpdater()
{
while (true)
Expand All @@ -26,9 +39,18 @@ static IEnumerator StateUpdater()
if (map == null)
continue;

if (lastMap != map)
{
lastMap = map;
pawnsInDanger.Clear();
}

var areas = ZombieSettings.Values.dangerousAreas.Where(pair => pair.Key.Map == map && pair.Value != AreaRiskMode.Ignore).ToArray();
if (areas.Length == 0)
{
pawnsInDanger.Clear();
continue;
}

var pawns = map.mapPawns.FreeColonistsSpawned.Where(pawn => pawn.InfectionState() < InfectionState.Infecting).ToArray();
yield return null;
Expand Down Expand Up @@ -58,8 +80,9 @@ static IEnumerator StateUpdater()
if (map == null)
continue;

var zombies = map.GetComponent<TickManager>().allZombiesCached.ToArray();
var zombies = AllZombies(map);
yield return null;

for (int zIdx = 0; zIdx < zombies.Length; zIdx++)
{
var zombie = zombies[zIdx];
Expand Down Expand Up @@ -152,7 +175,7 @@ public static void DrawDangerous()
RenderTexture.active = null;
RenderTexture.ReleaseTemporary(renderTexture);
return tex;
});
}, UnityEngine.Object.Destroy);
headsToDraw.Add((pawn, texture));
}
else
Expand Down Expand Up @@ -465,31 +488,25 @@ public static string ToStringHuman(this AreaRiskMode mode)
};
}

public static AreaRiskMode GetMode(Area area)
{
if (ZombieSettings.Values.dangerousAreas.TryGetValue(area, out var mode))
return mode;
return AreaRiskMode.Ignore;
}
public static AreaRiskMode GetMode(Area area) => ZombieSettings.Values.dangerousAreas.TryGetValue(area, AreaRiskMode.Ignore);

public static void ZombieMode(Rect rect)
{
var mode = GetMode(selected);
if (Widgets.ButtonText(rect, mode.ToStringHuman()))
var currentMode = GetMode(selected);
if (Widgets.ButtonText(rect, currentMode.ToStringHuman()))
{
var options = new List<FloatMenuOption>();
foreach (var choice in Enum.GetValues(typeof(AreaRiskMode)))
{
var localPmode2 = (AreaRiskMode)choice;
var localPmode = localPmode2;
options.Add(new FloatMenuOption(localPmode.ToStringHuman(), delegate ()
var newMode = (AreaRiskMode)choice;
options.Add(new FloatMenuOption(newMode.ToStringHuman(), delegate ()
{
if (localPmode != mode)
if (newMode != currentMode)
{
if (localPmode == AreaRiskMode.Ignore)
if (newMode == AreaRiskMode.Ignore)
_ = ZombieSettings.Values.dangerousAreas.Remove(selected);
else
ZombieSettings.Values.dangerousAreas[selected] = localPmode;
ZombieSettings.Values.dangerousAreas[selected] = newMode;
}
},
MenuOptionPriority.Default, null, null, 0f, null, null, true, 0));
Expand Down
7 changes: 6 additions & 1 deletion Source/ZombiePathing.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Collections.Generic;
using System.Linq;
using Verse;
using Verse.AI;

namespace ZombieLand
{
Expand Down Expand Up @@ -33,12 +34,14 @@ public class ZombiePathing
{
public bool running = true;
readonly Map map;
readonly PathGrid pathGrid;
public Dictionary<Region, int> backpointingRegionsIndices = new();
public List<BackpointingRegion> backpointingRegions = new();

public ZombiePathing(Map map)
{
this.map = map;
pathGrid = map.pathing.For(MapInfo.traverseParms).pathGrid;
}

public IntVec3 GetWanderDestination(IntVec3 cell)
Expand All @@ -63,6 +66,8 @@ bool InvalidRegion(Region region)
{
if (region == null || region.valid == false || (region.type & RegionType.Set_Passable) == 0)
return true;
if (region.Cells.Any(pathGrid.WalkableFast) == false)
return true;
return finalRegionIndices.ContainsKey(region);
}

Expand All @@ -76,7 +81,7 @@ void Add(Region region, int parentIdx)
map.regionGrid.allRooms
.Where(r => r.IsDoorway == false && r.Fogged == false && r.IsHuge == false && r.ProperRoom)
.SelectMany(r => r.Regions)
.Where(r => r != null && r.valid)
.Where(r => r != null && r.valid && r.Cells.Any(cell => pathGrid.WalkableFast(cell)))
.Distinct()
.Do(region => Add(region, -1));

Expand Down
Loading

0 comments on commit c2323ff

Please sign in to comment.