diff --git a/Source/SmarterConstruction/Core/ClosedRegionDetector.cs b/Source/SmarterConstruction/Core/ClosedRegionDetector.cs index 008bd7d..8e780e5 100644 --- a/Source/SmarterConstruction/Core/ClosedRegionDetector.cs +++ b/Source/SmarterConstruction/Core/ClosedRegionDetector.cs @@ -41,7 +41,8 @@ public static EncloseThingsResult WouldEncloseThings(Thing target, Pawn ___pawn) var enclosedPlayerPawns = enclosedThings.Where(t => t is Pawn && t.Faction != null && t.Faction.IsPlayer).ToList(); retValue.EnclosesRegion = true; - retValue.EnclosesThings = enclosedUnacceptable.Count > 0 || enclosedPlayerPawns.Count > 0; + retValue.EnclosesThings = enclosedUnacceptable.Count > 0 || enclosedPlayerPawns.Count(p => p != ___pawn) > 0; + retValue.EnclosesSelf = enclosedPlayerPawns.Any(p => p == ___pawn); } WouldEncloseThingsCache[target] = new CachedEncloseThingsResult { @@ -121,5 +122,6 @@ public class EncloseThingsResult { public bool EnclosesRegion { get; set; } public bool EnclosesThings { get; set; } + public bool EnclosesSelf { get; set; } } } diff --git a/Source/SmarterConstruction/DebugUtils.cs b/Source/SmarterConstruction/DebugUtils.cs index 591d70a..d86123f 100644 --- a/Source/SmarterConstruction/DebugUtils.cs +++ b/Source/SmarterConstruction/DebugUtils.cs @@ -14,7 +14,7 @@ public static void DebugLog(string text) public static void VerboseLog(string text) { - Log.Message(Find.TickManager.TicksGame + ": " + text, true); + //Log.Message(Find.TickManager.TicksGame + ": " + text, true); } } } diff --git a/Source/SmarterConstruction/Patches/Patch_JobDriver_MakeNewToils.cs b/Source/SmarterConstruction/Patches/Patch_JobDriver_MakeNewToils.cs index 19dd298..dc38968 100644 --- a/Source/SmarterConstruction/Patches/Patch_JobDriver_MakeNewToils.cs +++ b/Source/SmarterConstruction/Patches/Patch_JobDriver_MakeNewToils.cs @@ -117,11 +117,12 @@ public static bool EndJobIfEnclosing(Frame target, Pawn pawn) pawn.jobs.EndCurrentJob(JobCondition.Incompletable); return true; } - //DebugUtils.VerboseLog(pawn.Label + " finished " + target.Label + " on coordinates " + target.Position); + DebugUtils.VerboseLog(pawn.Label + " finished " + target.Label + " on coordinates " + target.Position); var pawnsAtLocation = target.Position.GetThingList(target.Map) .Where(t => t is Pawn && t.Faction != null && t.Faction.IsPlayer) .ToList(); + if (wouldEnclose.EnclosesSelf) pawnsAtLocation.Add(pawn); if (pawnsAtLocation.Count > 0 && wouldEnclose.EnclosesRegion) { // move pawn to a safe location to avoid random movement diff --git a/Source/SmarterConstruction/Patches/Patch_WorkGiver_ConstructDeliverResources.cs b/Source/SmarterConstruction/Patches/Patch_WorkGiver_ConstructDeliverResources.cs deleted file mode 100644 index 88c99a4..0000000 --- a/Source/SmarterConstruction/Patches/Patch_WorkGiver_ConstructDeliverResources.cs +++ /dev/null @@ -1,20 +0,0 @@ -using HarmonyLib; -using RimWorld; -using SmarterConstruction.Core; -using Verse; -using Verse.AI; - -namespace SmarterConstruction.Patches -{ - // Pretend job doesn't exist if the building would enclose something - /*[HarmonyPatch(typeof(WorkGiver_ConstructDeliverResources), "ResourceDeliverJobFor")] - public class Patch_WorkGiver_ConstructDeliverResources - { - public static bool Prefix(Pawn pawn, IConstructible c, ref Job __result, WorkGiver_ConstructDeliverResources __instance) - { - return !(c is Blueprint_Install install - && install.ThingToInstall?.def?.passability == Traversability.Impassable - && ClosedRegionDetector.WouldEncloseThings(install, pawn)); - } - }*/ -} diff --git a/Source/SmarterConstruction/Patches/WorkGiver_ConstructFinishFrames_JobOnThing.cs b/Source/SmarterConstruction/Patches/WorkGiver_ConstructFinishFrames_JobOnThing.cs index 8d525e8..ded1a32 100644 --- a/Source/SmarterConstruction/Patches/WorkGiver_ConstructFinishFrames_JobOnThing.cs +++ b/Source/SmarterConstruction/Patches/WorkGiver_ConstructFinishFrames_JobOnThing.cs @@ -12,11 +12,17 @@ public class WorkGiver_ConstructFinishFrames_JobOnThing { public static void Postfix(Pawn pawn, Thing t, ref Job __result, bool forced, WorkGiver_ConstructFinishFrames __instance) { - if (t?.def?.entityDefToBuild?.passability == Traversability.Impassable - && ClosedRegionDetector.WouldEncloseThings(t, pawn).EnclosesThings - && !forced) + if (t?.def?.entityDefToBuild?.passability == Traversability.Impassable && !forced) { - __result = null; + var encloseData = ClosedRegionDetector.WouldEncloseThings(t, pawn); + if (encloseData.EnclosesSelf) + { + DebugUtils.VerboseLog($"Allowing self enclosing job on {t.Position} for pawn {pawn.Label} expecting a teleport."); + } + if (encloseData.EnclosesThings) + { + __result = null; + } } } } diff --git a/Source/SmarterConstruction/Properties/AssemblyInfo.cs b/Source/SmarterConstruction/Properties/AssemblyInfo.cs index b8ee9e4..60911d4 100644 --- a/Source/SmarterConstruction/Properties/AssemblyInfo.cs +++ b/Source/SmarterConstruction/Properties/AssemblyInfo.cs @@ -32,5 +32,5 @@ // You can specify all the values or you can default the Build and Revision Numbers // by using the '*' as shown below: // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("1.2.0.0")] -[assembly: AssemblyFileVersion("1.2.0.0")] +[assembly: AssemblyVersion("1.2.1.0")] +[assembly: AssemblyFileVersion("1.2.1.0")] diff --git a/Source/SmarterConstruction/SmarterConstruction.csproj b/Source/SmarterConstruction/SmarterConstruction.csproj index c34b5bc..c411262 100644 --- a/Source/SmarterConstruction/SmarterConstruction.csproj +++ b/Source/SmarterConstruction/SmarterConstruction.csproj @@ -60,7 +60,6 @@ <ItemGroup> <Compile Include="DebugUtils.cs" /> <Compile Include="Patches\Patch_JobDriver_MakeNewToils.cs" /> - <Compile Include="Patches\Patch_WorkGiver_ConstructDeliverResources.cs" /> <Compile Include="Patches\Patch_WorkGiver_Scanner_GetPriority.cs" /> <Compile Include="Patches\Patch_WorkGiver_Scanner_Prioritized.cs" /> <Compile Include="Patches\WorkGiver_ConstructFinishFrames_JobOnThing.cs" />