Skip to content

Commit

Permalink
v3.8.2 RC2
Browse files Browse the repository at this point in the history
  • Loading branch information
pardeike committed Sep 10, 2023
1 parent db8b1fa commit 1816f72
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 14 deletions.
Binary file modified 1.4/Assemblies/AchtungMod.dll
Binary file not shown.
1 change: 0 additions & 1 deletion Source/ForcedExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System.Linq;
using Verse.AI;
using Verse;
using UnityEngine.VR;

namespace AchtungMod
{
Expand Down
26 changes: 14 additions & 12 deletions Source/ForcedJob.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ public class ForcedJob : IExposable
public HashSet<ForcedTarget> targets = new();
public Pawn pawn = null;
public List<WorkGiverDef> workgiverDefs = new();
public List<WorkGiver_Scanner> workgiverScanners = new();
public static readonly QuotaCache<Thing, bool> getThingJobCache = new(10);
public static readonly QuotaCache<IntVec3, bool> getCellJobCache = new(10);
public bool isThingJob = false;
public bool reentranceFlag = false;
public XY lastLocation = XY.Invalid;
Expand Down Expand Up @@ -58,6 +61,7 @@ public class ForcedJob : IExposable
public ForcedJob()
{
workgiverDefs = new List<WorkGiverDef>();
workgiverScanners = new List<WorkGiver_Scanner>();
targets = new HashSet<ForcedTarget>();
buildSmart = Achtung.Settings.buildingSmartDefault;
lastLocation = XY.Invalid;
Expand All @@ -68,6 +72,7 @@ public ForcedJob(Pawn pawn, LocalTargetInfo item, List<WorkGiverDef> workgiverDe
{
this.pawn = pawn;
this.workgiverDefs = workgiverDefs.Where(wgd => wgd?.giverClass != null).ToList();
workgiverScanners = workgiverDefs.Select(wgd => wgd.Worker).OfType<WorkGiver_Scanner>().ToList();
targets = new HashSet<ForcedTarget>() { new ForcedTarget(item, MaterialScore(item)) };
buildSmart = Achtung.Settings.buildingSmartDefault;

Expand All @@ -92,9 +97,6 @@ public IEnumerable<XY> AllCells(bool onlyValid = false)
return validTargets.Select(target => target.XY);
}

public IEnumerable<WorkGiver_Scanner> WorkGiverScanners => workgiverDefs
.Select(wgd => wgd.Worker).OfType<WorkGiver_Scanner>();

public static int MaterialScore(LocalTargetInfo item)
{
var scoreThing = 0;
Expand Down Expand Up @@ -195,7 +197,7 @@ public bool GetNextJob(out Job job)
{
job = null;

var workGiversByPrio = WorkGiverScanners.OrderBy(worker =>
var workGiversByPrio = workgiverScanners.OrderBy(worker =>
{
if (worker is WorkGiver_ConstructDeliverResourcesToBlueprints)
return 1;
Expand Down Expand Up @@ -314,13 +316,13 @@ public void ToggleSmartBuilding()
buildSmart = !buildSmart;
}

public bool HasJob(Thing thing)
public bool ThingHasJob(Thing thing)
{
try
{
lock (pawn.Map)
{
return WorkGiverScanners.Any(scanner => thing.GetThingJob(pawn, scanner, true) != null);
return getThingJobCache.Get(thing, t => workgiverScanners.Any(scanner => t.GetThingJob(pawn, scanner, true) != null));
}
}
catch
Expand All @@ -329,13 +331,13 @@ public bool HasJob(Thing thing)
}
}

public bool HasJob(XY cell)
public bool CellHasJob(XY cell)
{
try
{
lock (pawn.Map)
{
return WorkGiverScanners.Any(scanner => ((IntVec3)cell).GetCellJob(pawn, scanner) != null);
return getCellJobCache.Get(cell, c => workgiverScanners.Any(scanner => ((IntVec3)c).GetCellJob(pawn, scanner) != null));
}
}
catch
Expand Down Expand Up @@ -368,7 +370,7 @@ public IEnumerator ExpandThingTargets(Map map)
for (var i = 0; i < newThings.Length && cancelled == false && maxCountVerifier(); i++)
{
var newThing = newThings[i];
if (things.Contains(newThing) == false && HasJob(newThing))
if (things.Contains(newThing) == false && ThingHasJob(newThing))
{
LocalTargetInfo item = newThing;
_ = targets.Add(new ForcedTarget(item, MaterialScore(item)));
Expand All @@ -395,7 +397,7 @@ public IEnumerator ExpandCellTargets(Map map)
for (var i = 0; i < newCells.Length && cancelled == false && maxCountVerifier(); i++)
{
var cell = newCells[i];
if (HasJob(cell))
if (CellHasJob(cell))
{
LocalTargetInfo item = cell;
_ = targets.Add(new ForcedTarget(item, 0));
Expand All @@ -419,8 +421,8 @@ public IEnumerator ContractTargets(Map map)
for (var i = 0; i < cells.Length && cancelled == false; i++)
{
var cell = cells[i];
if (cell.InBounds(map) && HasJob(cell) == false)
if (map.thingGrid.ThingsListAtFast(cell).All(thing => thing.Spawned == false || HasJob(thing) == false))
if (cell.InBounds(map) && CellHasJob(cell) == false)
if (map.thingGrid.ThingsListAtFast(cell).All(thing => thing.Spawned == false || ThingHasJob(thing) == false))
_ = targets.RemoveWhere(target => target.XY == cell || (target.item.thingInt?.AllCells().Contains(cell) ?? false));
yield return null;
}
Expand Down
1 change: 0 additions & 1 deletion Source/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO.Ports;
using System.Linq;
using System.Reflection;
using System.Reflection.Emit;
Expand Down
35 changes: 35 additions & 0 deletions Source/QuotaCache.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System;
using System.Collections.Generic;

namespace AchtungMod
{
public class QuotaCache<S, T>
{
private readonly Dictionary<S, (T value, int count)> cache = new();
private readonly int maxRetrievals;

public QuotaCache(int maxRetrievals)
{
this.maxRetrievals = maxRetrievals;
}

private void Add(S key, T value)
{
cache[key] = (value, 0);
}

public T Get(S key, Func<S, T> fetchCallback)
{
if (!cache.ContainsKey(key) || cache[key].count >= maxRetrievals)
{
T newValue = fetchCallback(key);
Add(key, newValue);
}

var (value, count) = cache[key];
cache[key] = (value, count + 1);

return value;
}
}
}

0 comments on commit 1816f72

Please sign in to comment.