diff --git a/About/Manifest.xml b/About/Manifest.xml index 765ec06..6db632c 100644 --- a/About/Manifest.xml +++ b/About/Manifest.xml @@ -1,7 +1,7 @@  net.pardeike.rimworld.mod.achtung - 2.2.9.0 + 2.3.0.0
  • 0.19.0
  • 1.0.0
  • diff --git a/About/ModSync.xml b/About/ModSync.xml index ef32e42..69ce473 100644 --- a/About/ModSync.xml +++ b/About/ModSync.xml @@ -2,7 +2,7 @@ be673269-db56-463b-8c36-e074881e0d77 Achtung! - 2.2.9.0 + 2.3.0.0 False pardeike diff --git a/Assemblies/AchtungMod.dll b/Assemblies/AchtungMod.dll index 6154bd4..1a6f18e 100644 Binary files a/Assemblies/AchtungMod.dll and b/Assemblies/AchtungMod.dll differ diff --git a/Source/ForcedJob.cs b/Source/ForcedJob.cs index fbc77e7..46a07b5 100644 --- a/Source/ForcedJob.cs +++ b/Source/ForcedJob.cs @@ -18,12 +18,17 @@ public ForcedJobs() public void ExposeData() { + if (Scribe.mode == LoadSaveMode.Saving) + jobs.RemoveAll(job => job.IsEmpty()); + Scribe_Collections.Look(ref jobs, "jobs", LookMode.Deep); - if (Scribe.mode == LoadSaveMode.ResolvingCrossRefs) + if (Scribe.mode == LoadSaveMode.PostLoadInit) { if (jobs == null) jobs = new List(); + + jobs.RemoveAll(job => job.IsEmpty()); } } } @@ -35,10 +40,10 @@ public class ForcedJob : IExposable public Pawn pawn = null; public List workgiverDefs = new List(); + public Action cellChangeDelegate; public bool isThingJob = false; public bool initialized = false; public int cellRadius = 0; - static readonly Dictionary TypeScores = new Dictionary { { ThingDefOf.PowerConduit, 1000 }, @@ -83,6 +88,7 @@ public ForcedJob() public void AddTarget(LocalTargetInfo item) { targets.Add(new ForcedTarget(item, MaterialScore(item))); + cellChangeDelegate(); UpdateCells(); } @@ -128,11 +134,6 @@ public static int MaterialScore(LocalTargetInfo item) return new[] { scoreThing, scoreBlueprint, scoreFrame }.Max(); } - public void PrepareTargets() - { - return; - } - public IEnumerable GetUnsortedTargets() { var mapWidth = pawn.Map.Size.x; @@ -300,7 +301,7 @@ public void UpdateCells() var item = new LocalTargetInfo(thing); return new ForcedTarget(item, MaterialScore(item)); })); - PrepareTargets(); + cellChangeDelegate(); return; } @@ -328,11 +329,19 @@ public void UpdateCells() var item = new LocalTargetInfo(cell); return new ForcedTarget(item, MaterialScore(item)); })); - PrepareTargets(); + cellChangeDelegate(); + } + + public bool IsEmpty() + { + return targets.Count == 0; } public void ExposeData() { + if (Scribe.mode == LoadSaveMode.Saving) + targets.RemoveWhere(target => target.item == null || target.item.IsValid == false || target.item.ThingDestroyed); + Scribe_References.Look(ref pawn, "pawn"); Scribe_Collections.Look(ref workgiverDefs, "workgivers", LookMode.Def); Scribe_Collections.Look(ref targets, "targets", LookMode.Deep); @@ -340,11 +349,8 @@ public void ExposeData() Scribe_Values.Look(ref initialized, "inited", false, true); Scribe_Values.Look(ref cellRadius, "radius", 0, true); - if (Scribe.mode == LoadSaveMode.ResolvingCrossRefs) - { - targets.RemoveWhere(target => target.item.ThingDestroyed); - PrepareTargets(); - } + if (Scribe.mode == LoadSaveMode.PostLoadInit) + targets.RemoveWhere(target => target.item == null || target.item.IsValid == false || target.item.ThingDestroyed); } } diff --git a/Source/ForcedWork.cs b/Source/ForcedWork.cs index 9f4fbc6..b678a07 100644 --- a/Source/ForcedWork.cs +++ b/Source/ForcedWork.cs @@ -12,6 +12,7 @@ public class ForcedWork : WorldComponent Dictionary allForcedJobs = new Dictionary(); private List forcedJobsKeysWorkingList; private List forcedJobsValuesWorkingList; + private HashSet allForcedCells; readonly HashSet preparing = new HashSet(); readonly Dictionary> forbiddenLocations = new Dictionary>(); @@ -107,6 +108,7 @@ public bool AddForcedJob(Pawn pawn, List workgiverDefs, LocalTarge Unprepare(pawn); var forcedJob = new ForcedJob() { pawn = pawn, workgiverDefs = workgiverDefs, isThingJob = item.HasThing }; + forcedJob.cellChangeDelegate = () => allForcedCells = null; forcedJob.AddTarget(item); if (allForcedJobs.ContainsKey(pawn) == false) allForcedJobs[pawn] = new ForcedJobs(); @@ -147,6 +149,18 @@ public IEnumerable ForcedJobsForMap(Map map) .SelectMany(pair => pair.Value.jobs); } + public HashSet AllForcedCellsForMap(Map map) + { + if (allForcedCells == null) + { + allForcedCells = new HashSet(allForcedJobs + .Where(pair => pair.Key.Map == map) + .SelectMany(pair => pair.Value.jobs) + .SelectMany(forcedJob => forcedJob.AllCells(true))); + } + return allForcedCells; + } + public void AddForbiddenLocation(Pawn pawn, IntVec3 cell) { if (forbiddenLocations.TryGetValue(pawn, out var cells) == false) @@ -177,12 +191,29 @@ public bool IsForbiddenLocation(IntVec3 cell) public override void ExposeData() { + if (Scribe.mode == LoadSaveMode.Saving) + { + allForcedJobs + .Where(pair => pair.Value.jobs.Count == 0) + .Select(pair => pair.Key) + .Do(pawn => Remove(pawn)); + } + Scribe_Collections.Look(ref allForcedJobs, "joblist", LookMode.Reference, LookMode.Deep, ref forcedJobsKeysWorkingList, ref forcedJobsValuesWorkingList); - if (Scribe.mode == LoadSaveMode.ResolvingCrossRefs) + if (Scribe.mode == LoadSaveMode.PostLoadInit) { if (allForcedJobs == null) allForcedJobs = new Dictionary(); + + allForcedJobs + .Where(pair => pair.Value.jobs.Count == 0) + .Select(pair => pair.Key) + .Do(pawn => Remove(pawn)); + + allForcedJobs.Values + .SelectMany(forcedJobs => forcedJobs.jobs) + .Do(forcedJob => forcedJob.cellChangeDelegate = () => allForcedCells = null); } } } diff --git a/Source/Main.cs b/Source/Main.cs index 44f59df..1dec434 100644 --- a/Source/Main.cs +++ b/Source/Main.cs @@ -96,6 +96,26 @@ static bool Prefix(Pawn p, Thing t, bool forced, ref bool __result) } } + // allow for jobs outside allowed area + // + [HarmonyPatch(typeof(ForbidUtility))] + [HarmonyPatch(nameof(ForbidUtility.InAllowedArea))] + static class ForbidUtility_InAllowedArea_Patch + { + static void Postfix(IntVec3 c, Pawn forPawn, ref bool __result) + { + if (__result == false) + return; + var forcedWork = Find.World.GetComponent(); + if (forcedWork.HasForcedJob(forPawn)) + __result = true; + else + __result = forcedWork + .AllForcedCellsForMap(forPawn.Map) + .Contains(c) == false; + } + } + // forced repair outside of allowed area // [HarmonyPatch(typeof(WorkGiver_Repair))] diff --git a/Source/Properties/AssemblyInfo.cs b/Source/Properties/AssemblyInfo.cs index d1ac4c8..dffeb78 100644 --- a/Source/Properties/AssemblyInfo.cs +++ b/Source/Properties/AssemblyInfo.cs @@ -31,5 +31,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("2.2.9.0")] -[assembly: AssemblyFileVersion("2.2.9.0")] +[assembly: AssemblyVersion("2.3.0.0")] +[assembly: AssemblyFileVersion("2.3.0.0")] diff --git a/Source/Tools.cs b/Source/Tools.cs index 176054f..21cacea 100644 --- a/Source/Tools.cs +++ b/Source/Tools.cs @@ -309,7 +309,7 @@ public static int NeighbourScore(IntVec3 pos, PathGrid pathGrid, int mapWidth, H return -neighbourScore; } - public static IEnumerable AllCells(this LocalTargetInfo item) + /*public static IEnumerable AllCells(this LocalTargetInfo item) { if (item.HasThing) { @@ -323,10 +323,12 @@ public static IEnumerable AllCells(this LocalTargetInfo item) yield break; } yield return item.Cell; - } + }*/ public static IEnumerable AllCells(this Thing thing) { + if (thing == null) + yield break; var size = thing.def.size; if (size.x + size.z == 1) yield return thing.Position;