Skip to content

Commit

Permalink
Improved door handling with zombies on the other side
Browse files Browse the repository at this point in the history
  • Loading branch information
pardeike committed Sep 21, 2019
1 parent 53f33bb commit 5b587c1
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 7 deletions.
Binary file modified Assemblies/ZombieLand.dll
Binary file not shown.
59 changes: 56 additions & 3 deletions Source/Patches.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,14 +125,14 @@ static void Postfix()
}

// patch for debugging: show zombie pathing grid around the mouse
/*
//
[HarmonyPatch(typeof(MapInterface))]
[HarmonyPatch("MapInterfaceOnGUI_AfterMainTabs")]
class MapInterface_MapInterfaceOnGUI_AfterMainTabs_Patch
{
static void Postfix()
{
if (DebugViewSettings.writePathCosts == false) return;
if (Constants.DEBUGGRID == false || DebugViewSettings.writeBeauty == false) return;
if (Event.current.type != EventType.Repaint) return;

var map = Find.CurrentMap;
Expand All @@ -158,7 +158,6 @@ static void Postfix()
});
}
}
*/

// patch to show zombieland version and total number of zombies
//
Expand Down Expand Up @@ -1056,6 +1055,60 @@ static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> inst
}
}

// do not open doors when not drafted and they are marked by the avoid grid
//
[HarmonyPatch]
static class Building_Door_PawnCanOpen_Patch
{
static void Postfix(Building_Door __instance, Pawn p, ref bool __result)
{
if (__result == false)
return;

if (p == null || p.Map == null || p.Drafted)
return;

if (__instance.FreePassage)
return;

if (p.CurJob?.playerForced ?? false)
return;

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

var map = p.Map;

var tickManager = map.GetComponent<TickManager>();
if (tickManager == null)
return;

var avoidGrid = tickManager.avoidGrid;

var size = __instance.def.size;
if (size.x == 1 && size.z == 1)
{
if (avoidGrid.ShouldAvoid(map, __instance.Position))
__result = false;
}
else
{
var cells = __instance.OccupiedRect().Cells;
if (cells.Any(cell => avoidGrid.ShouldAvoid(map, cell)))
__result = false;
}
}

static IEnumerable<MethodBase> TargetMethods()
{
return GenTypes.AllSubclasses(typeof(Building_Door))
.Union(new List<Type>() { typeof(Building_Door) })
.Select(type => type.GetMethod("PawnCanOpen", AccessTools.all | BindingFlags.DeclaredOnly))
.Where(method => method != null)
.Cast<MethodBase>();
}
}

// patch to stop jobs when zombies have to be avoided
//
[HarmonyPatch(typeof(JobDriver))]
Expand Down
7 changes: 3 additions & 4 deletions Source/WorkGivers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,10 @@ public override bool HasJobOnThing(Pawn pawn, Thing t, bool forced = false)
if (forced == false && ColonistSettings.Values.ConfigFor(pawn).autoExtractZombieSerum == false)
return false;

if (pawn.CanReach(corpse, PathEndMode.ClosestTouch, pawn.NormalMaxDanger(), false, TraverseMode.ByPawn) == false)
if (pawn.CanReach(corpse, PathEndMode.ClosestTouch, forced ? Danger.Deadly : Danger.None) == false)
return false;

return pawn.CanReserve(corpse, 1, -1, null, forced);
/*var result = pawn.CanReserve(corpse, 1, -1, null, forced);
var result = pawn.CanReserve(corpse, 1, -1, null, forced);
if (result && forced == false && ZombieSettings.Values.betterZombieAvoidance)
{
var map = pawn.Map;
Expand All @@ -56,7 +55,7 @@ public override bool HasJobOnThing(Pawn pawn, Thing t, bool forced = false)
return false;
}
}
return result;*/
return result;
}

public override Job JobOnThing(Pawn pawn, Thing t, bool forced = false)
Expand Down

0 comments on commit 5b587c1

Please sign in to comment.