Skip to content

Commit

Permalink
Merge pull request #800 from zymex22/miscPerformenceUpdate
Browse files Browse the repository at this point in the history
StartJob and SetPos Performance Improvements
  • Loading branch information
Sn1p3rr3c0n authored Jul 18, 2024
2 parents 7d69014 + e9578df commit ff7111f
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,15 @@ namespace ProjectRimFactory.Common.HarmonyPatches
[HarmonyPatch(typeof(Verse.AI.Pawn_JobTracker), "StartJob")]
class Patch_Pawn_JobTracker_StartJob
{


/// <summary>
/// Returns the Target Position of a Job
/// </summary>
/// <param name="targetPos"></param>
/// <param name="isHaulJobType"></param>
/// <param name="newJob"></param>
/// <param name="pawnPosition"></param>
/// <returns></returns>
private static bool TryGetTargetPos(ref IntVec3 targetPos, bool isHaulJobType, Job newJob, IntVec3 pawnPosition)
{
if (isHaulJobType)
Expand All @@ -28,13 +36,19 @@ private static bool TryGetTargetPos(ref IntVec3 targetPos, bool isHaulJobType, J
}
else
{
//Bill Type Jon
//Bill Type Job
targetPos = newJob.targetA.Thing?.Position ?? newJob.targetA.Cell;
if (newJob.targetB == IntVec3.Invalid && (newJob.targetQueueB == null || newJob.targetQueueB.Count == 0)) return false;
}
return true;
}


/// <summary>
/// Returns a list of LocalTargetInfos via <para>TargetItems</para> depending on the Job type
/// </summary>
/// <param name="TargetItems"></param>
/// <param name="isHaulJobType"></param>
/// <param name="newJob"></param>
private static void GetTargetItems(ref List<LocalTargetInfo> TargetItems, bool isHaulJobType, Job newJob)
{
if (isHaulJobType)
Expand All @@ -55,22 +69,31 @@ private static void GetTargetItems(ref List<LocalTargetInfo> TargetItems, bool i
}


public static bool Prefix(Job newJob, ref Pawn ___pawn, JobCondition lastJobEndCondition = JobCondition.None, ThinkNode jobGiver = null, bool resumeCurJobAfterwards = false, bool cancelBusyStances = true
public static bool Prefix(Job newJob, ref Pawn ___pawn, JobCondition lastJobEndCondition = JobCondition.None,
ThinkNode jobGiver = null, bool resumeCurJobAfterwards = false, bool cancelBusyStances = true
, ThinkTreeDef thinkTree = null, JobTag? tag = null, bool fromQueue = false, bool canReturnCurJobToPool = false)
{
//No random moths eating my cloths
// No random moths eating my cloths
if (___pawn?.Faction == null || !___pawn.Faction.IsPlayer) return true;
var prfmapcomp = PatchStorageUtil.GetPRFMapComponent(___pawn.Map);

//PickUpAndHaul "Compatibility" (by not messing with it)
// PickUpAndHaul "Compatibility" (by not messing with it)
if (newJob.def.defName == "HaulToInventory") return true;

// Cache Variables for Performance
var pawnMap = ___pawn.Map;
var pawnPos = ___pawn.Position;
var prfMapComponent = PatchStorageUtil.GetPRFMapComponent(pawnMap);

//This is the Position where we need the Item to be at

// This is the Position where we need the Item to be at
IntVec3 targetPos = IntVec3.Invalid;
var usHaulJobType = newJob.targetA.Thing?.def?.category == ThingCategory.Item;
if (!TryGetTargetPos(ref targetPos, usHaulJobType, newJob, ___pawn.Position)) return true;
// Check if this is a Haul Job
bool usHaulJobType = newJob.targetA.Thing?.def?.category == ThingCategory.Item;

// Get Target Position, Exit on fail
if (!TryGetTargetPos(ref targetPos, usHaulJobType, newJob, pawnPos)) return true;

List<KeyValuePair<float, Building_AdvancedStorageUnitIOPort>> Ports = AdvancedIO_PatchHelper.GetOrderdAdvancedIOPorts(___pawn.Map, ___pawn.Position, targetPos);
List<KeyValuePair<float, Building_AdvancedStorageUnitIOPort>> Ports =
AdvancedIO_PatchHelper.GetOrderdAdvancedIOPorts(pawnMap, pawnPos, targetPos);
List<LocalTargetInfo> TargetItems = null;
GetTargetItems(ref TargetItems, usHaulJobType, newJob);
foreach (var target in TargetItems)
Expand All @@ -81,17 +104,21 @@ public static bool Prefix(Job newJob, ref Pawn ___pawn, JobCondition lastJobEndC
continue;
}

var DistanceToTarget = AdvancedIO_PatchHelper.CalculatePath(___pawn.Position, target.Cell, targetPos);
var DistanceToTarget = AdvancedIO_PatchHelper.CalculatePath(pawnPos, target.Cell, targetPos);

//Quick check if the Item could be in a DSU
//Might have false Positives They are then filterd by AdvancedIO_PatchHelper.CanMoveItem
//But should not have false Negatives
if (prfmapcomp.ShouldHideItemsAtPos(target.Cell))
if (prfMapComponent.ShouldHideItemsAtPos(target.Cell))
{
foreach (var port in Ports)
{
var PortIsCloser = port.Key < DistanceToTarget;
if (PortIsCloser || (ConditionalPatchHelper.Patch_Reachability_CanReach.Status && ___pawn.Map.reachability.CanReach(___pawn.Position, target.Thing, Verse.AI.PathEndMode.Touch, TraverseParms.For(___pawn)) && Patch_Reachability_CanReach.CanReachThing(target.Thing)))
if (PortIsCloser || (ConditionalPatchHelper.Patch_Reachability_CanReach.Status
&& pawnMap.reachability.CanReach(pawnPos,
target.Thing, Verse.AI.PathEndMode.Touch,
TraverseParms.For(___pawn))
&& Patch_Reachability_CanReach.CanReachThing(target.Thing)))
{
if (AdvancedIO_PatchHelper.CanMoveItem(port.Value, target.Cell))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,23 @@

namespace ProjectRimFactory.Common.HarmonyPatches
{
/// <summary>
/// Notifies Building_MassStorageUnit via Notify_LostThing(t) if a contained Item was moved away
/// </summary>
[HarmonyPatch(typeof(Thing), "set_Position")]
public static class SetPositionPatch
{
public static void Prefix(Thing __instance, out Building_MassStorageUnit __state)
{
__state = null;
IntVec3 pos = __instance.Position;
if (__instance.def.category == ThingCategory.Item && pos.IsValid && __instance.Map != null)
if (__instance.def.category == ThingCategory.Item && pos.IsValid)
{
__state = pos.GetFirst<Building_MassStorageUnit>(__instance.Map);
var map = __instance.Map;
if (map != null)
{
__state = pos.GetFirst<Building_MassStorageUnit>(map);
}
}
}
public static void Postfix(Thing __instance, Building_MassStorageUnit __state)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ private Thing GetstoredItem()
Log.Error($"PRF GetstoredItem @{this.Position} map is null");
return null;
}
return WorkPosition.GetFirstItem(Map);
return WorkPosition.GetFirstItem(map);
}

public bool CanGetNewItem => GetstoredItem() == null && (powerComp?.PowerOn ?? false);
Expand Down
10 changes: 6 additions & 4 deletions Source/ProjectRimFactory/Storage/Building_MassStorageUnit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -247,17 +247,19 @@ public virtual void RefreshStorage()
{
items.Clear();
ItemCounts.Clear();

var thisPos = Position;
var thisMap = Map;
if (!Spawned) return; // don't want to try getting lists of things when not on a map (see 155)
foreach (var cell in AllSlotCells())
{
var things = new List<Thing>(cell.GetThingList(Map));
for (var i = 0; i < things.Count; i++)
var things = new List<Thing>(cell.GetThingList(thisMap));
int cnt = things.Count;
for (var i = 0; i < cnt; i++)
{
var item = things[i];
if (item.def.category == ThingCategory.Item)
{
if (cell != Position)
if (cell != thisPos)
{
HandleNewItem(item);
}
Expand Down

0 comments on commit ff7111f

Please sign in to comment.