Skip to content

Commit

Permalink
v2.3.0 makes other colonists ignore already forced work unless you as…
Browse files Browse the repository at this point in the history
…sign them too
  • Loading branch information
pardeike committed Nov 3, 2018
1 parent fd93056 commit 1035364
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 21 deletions.
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.achtung</identifier>
<version>2.2.9.0</version>
<version>2.3.0.0</version>
<targetVersions>
<li>0.19.0</li>
<li>1.0.0</li>
Expand Down
2 changes: 1 addition & 1 deletion About/ModSync.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<ModSyncNinjaData>
<ID>be673269-db56-463b-8c36-e074881e0d77</ID>
<ModName>Achtung!</ModName>
<Version>2.2.9.0</Version>
<Version>2.3.0.0</Version>
<SaveBreaking>False</SaveBreaking>
<Host name="Github">
<Owner>pardeike</Owner>
Expand Down
Binary file modified Assemblies/AchtungMod.dll
Binary file not shown.
34 changes: 20 additions & 14 deletions Source/ForcedJob.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ForcedJob>();

jobs.RemoveAll(job => job.IsEmpty());
}
}
}
Expand All @@ -35,10 +40,10 @@ public class ForcedJob : IExposable

public Pawn pawn = null;
public List<WorkGiverDef> workgiverDefs = new List<WorkGiverDef>();
public Action cellChangeDelegate;
public bool isThingJob = false;
public bool initialized = false;
public int cellRadius = 0;

static readonly Dictionary<BuildableDef, int> TypeScores = new Dictionary<BuildableDef, int>
{
{ ThingDefOf.PowerConduit, 1000 },
Expand Down Expand Up @@ -83,6 +88,7 @@ public ForcedJob()
public void AddTarget(LocalTargetInfo item)
{
targets.Add(new ForcedTarget(item, MaterialScore(item)));
cellChangeDelegate();
UpdateCells();
}

Expand Down Expand Up @@ -128,11 +134,6 @@ public static int MaterialScore(LocalTargetInfo item)
return new[] { scoreThing, scoreBlueprint, scoreFrame }.Max();
}

public void PrepareTargets()
{
return;
}

public IEnumerable<Thing> GetUnsortedTargets()
{
var mapWidth = pawn.Map.Size.x;
Expand Down Expand Up @@ -300,7 +301,7 @@ public void UpdateCells()
var item = new LocalTargetInfo(thing);
return new ForcedTarget(item, MaterialScore(item));
}));
PrepareTargets();
cellChangeDelegate();

return;
}
Expand Down Expand Up @@ -328,23 +329,28 @@ 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);
Scribe_Values.Look(ref isThingJob, "thingJob", false, true);
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);
}
}

Expand Down
33 changes: 32 additions & 1 deletion Source/ForcedWork.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public class ForcedWork : WorldComponent
Dictionary<Pawn, ForcedJobs> allForcedJobs = new Dictionary<Pawn, ForcedJobs>();
private List<Pawn> forcedJobsKeysWorkingList;
private List<ForcedJobs> forcedJobsValuesWorkingList;
private HashSet<IntVec3> allForcedCells;

readonly HashSet<Pawn> preparing = new HashSet<Pawn>();
readonly Dictionary<Pawn, HashSet<IntVec3>> forbiddenLocations = new Dictionary<Pawn, HashSet<IntVec3>>();
Expand Down Expand Up @@ -107,6 +108,7 @@ public bool AddForcedJob(Pawn pawn, List<WorkGiverDef> 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();
Expand Down Expand Up @@ -147,6 +149,18 @@ public IEnumerable<ForcedJob> ForcedJobsForMap(Map map)
.SelectMany(pair => pair.Value.jobs);
}

public HashSet<IntVec3> AllForcedCellsForMap(Map map)
{
if (allForcedCells == null)
{
allForcedCells = new HashSet<IntVec3>(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)
Expand Down Expand Up @@ -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<Pawn, ForcedJobs>();

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);
}
}
}
Expand Down
20 changes: 20 additions & 0 deletions Source/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ForcedWork>();
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))]
Expand Down
4 changes: 2 additions & 2 deletions Source/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
6 changes: 4 additions & 2 deletions Source/Tools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ public static int NeighbourScore(IntVec3 pos, PathGrid pathGrid, int mapWidth, H
return -neighbourScore;
}

public static IEnumerable<IntVec3> AllCells(this LocalTargetInfo item)
/*public static IEnumerable<IntVec3> AllCells(this LocalTargetInfo item)
{
if (item.HasThing)
{
Expand All @@ -323,10 +323,12 @@ public static IEnumerable<IntVec3> AllCells(this LocalTargetInfo item)
yield break;
}
yield return item.Cell;
}
}*/

public static IEnumerable<IntVec3> AllCells(this Thing thing)
{
if (thing == null)
yield break;
var size = thing.def.size;
if (size.x + size.z == 1)
yield return thing.Position;
Expand Down

0 comments on commit 1035364

Please sign in to comment.