Skip to content

Commit

Permalink
Merge pull request #13 from alextd/master
Browse files Browse the repository at this point in the history
Queued hauling supports Extended Storage
  • Loading branch information
Mehni authored Oct 26, 2018
2 parents 903534b + be98072 commit 4ffadbb
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 5 deletions.
50 changes: 50 additions & 0 deletions Source/PickUpAndHaul/ExtendedStorage_Support.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Verse;
using RimWorld;
using UnityEngine;

namespace PickUpAndHaul
{
class ExtendedStorage_Support
{
public static bool CapacityAt(ThingDef def, IntVec3 storeCell, Map map, out int capacity)
{
if (ModCompatibilityCheck.ExtendedStorageIsActive)
{
try
{
return CapacityAtEx(def, storeCell, map, out capacity);
}
catch (Exception e)
{
Verse.Log.Warning($"Pick Up And Haul tried to get Extended Storage capacity but it failed: {e}");
}
}
capacity = 0;
return false;
}

public static bool CapacityAtEx(ThingDef def, IntVec3 storeCell, Map map, out int capacity)
{
foreach (Thing thing in storeCell.GetThingList(map))
{
//thing.Position seems to be the input cell, which can just be handled normally
if (thing.Position == storeCell) continue;

ExtendedStorage.Building_ExtendedStorage storage = thing as ExtendedStorage.Building_ExtendedStorage;

if(storage.StoredThingTotal == 0)
capacity = (int)(def.stackLimit * storage.GetStatValue(ExtendedStorage.DefReferences.Stat_ES_StorageFactor));
else
capacity = storage.ApparentMaxStorage - storage.StoredThingTotal;
Log.Message($"AT {storeCell} ES: {capacity} = {storage.ApparentMaxStorage} - {storage.StoredThingTotal}");
return true;
}
capacity = 0;
return false;
}
}
}
28 changes: 28 additions & 0 deletions Source/PickUpAndHaul/JobDriver_HaulToInventory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,34 @@ protected override IEnumerable<Toil> MakeNewToils()
yield return takeThing;
yield return Toils_Jump.JumpIf(nextTarget, () => !job.targetQueueA.NullOrEmpty<LocalTargetInfo>());

//Find more to haul, in case things spawned while this was in progess
yield return new Toil()
{
initAction = () =>
{
Pawn actor = pawn;
Job curJob = actor.jobs.curJob;
LocalTargetInfo storeCell = curJob.targetB;

List<Thing> haulables = actor.Map.listerHaulables.ThingsPotentiallyNeedingHauling();
WorkGiver_HaulToInventory haulMoreWork = actor.workSettings.WorkGiversInOrderNormal.First(wg => wg is WorkGiver_HaulToInventory) as WorkGiver_HaulToInventory;
Thing haulMoreThing = GenClosest.ClosestThing_Global(actor.Position, haulables, 12, t => haulMoreWork.HasJobOnThing(actor, t, false));

//WorkGiver_HaulToInventory found more work nearby
if (haulMoreThing != null)
{
Log.Message($"{actor} hauling again : {haulMoreThing}");
Job haulMoreJob = haulMoreWork.JobOnThing(actor, haulMoreThing);

if (haulMoreJob.TryMakePreToilReservations(actor, false))
{
actor.jobs.jobQueue.EnqueueFirst(haulMoreJob, new JobTag?(JobTag.Misc));
this.EndJobWith(JobCondition.Succeeded);
}
}
}
};

//maintain cell reservations on the trip back
//TODO: do that when we carry things
//I guess that means TODO: implement carrying the rest of the items in this job instead of falling back on HaulToStorageJob
Expand Down
4 changes: 3 additions & 1 deletion Source/PickUpAndHaul/ModCompatibilityCheck.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ public static bool ExtendedStorageIsActive
{
get
{
return (ModsConfig.ActiveModsInLoadOrder.Any(m => m.Name == "ExtendedStorageFluffyHarmonised") || ModsConfig.ActiveModsInLoadOrder.Any(m => m.Name == "Core SK"));
return (ModsConfig.ActiveModsInLoadOrder.Any(m => m.Name == "ExtendedStorageFluffyHarmonised")
|| ModsConfig.ActiveModsInLoadOrder.Any(m => m.Name == "Extended Storage")
|| ModsConfig.ActiveModsInLoadOrder.Any(m => m.Name == "Core SK"));
}
}

Expand Down
13 changes: 9 additions & 4 deletions Source/PickUpAndHaul/PickUpAndHaul.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@
<DebugSymbols>false</DebugSymbols>
<DebugType>none</DebugType>
<Optimize>true</Optimize>
<OutputPath>..\..\..\Assemblies\</OutputPath>
<OutputPath>..\..\Assemblies\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>none</DebugType>
<Optimize>true</Optimize>
<OutputPath>..\..\..\Assemblies\</OutputPath>
<OutputPath>..\..\Assemblies\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
Expand All @@ -35,7 +35,11 @@
<HintPath>..\..\..\..\..\..\..\..\..\..\Users\Maniak\Documents\RimWorld Mods\0Harmony.dll</HintPath>
</Reference>
<Reference Include="Assembly-CSharp">
<HintPath>..\..\..\..\..\..\..\..\Program Files (x86)\Steam\steamapps\common\RimWorld\RimWorldWin64_Data\Managed\Assembly-CSharp.dll</HintPath>
<HintPath>..\..\..\..\..\..\..\Program Files (x86)\Steam\steamapps\common\RimWorld\RimWorldWin64_Data\Managed\Assembly-CSharp.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="ExtendedStorage">
<HintPath>..\..\..\..\..\..\..\Program Files (x86)\Steam\steamapps\workshop\content\294100\731732064\Assemblies\ExtendedStorage.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="System" />
Expand All @@ -45,13 +49,14 @@
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
<Reference Include="UnityEngine">
<HintPath>..\..\..\..\..\..\..\..\Program Files (x86)\Steam\steamapps\common\RimWorld\RimWorldWin64_Data\Managed\UnityEngine.dll</HintPath>
<HintPath>..\..\..\..\..\..\..\Program Files (x86)\Steam\steamapps\common\RimWorld\RimWorldWin64_Data\Managed\UnityEngine.dll</HintPath>
<Private>False</Private>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="CompHauledToInventory.cs" />
<Compile Include="DebugLog.cs" />
<Compile Include="ExtendedStorage_Support.cs" />
<Compile Include="GearTabHighlight.cs" />
<Compile Include="HarmonyPatches.cs" />
<Compile Include="JobDriver_HaulToInventory.cs" />
Expand Down
3 changes: 3 additions & 0 deletions Source/PickUpAndHaul/WorkGiver_HaulToInventory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,9 @@ public CellAllocation(Thing a, int c)

public static int CapacityAt(ThingDef def, IntVec3 storeCell, Map map)
{
if (ExtendedStorage_Support.CapacityAt(def, storeCell, map, out int cap))
return cap;

int capacity = def.stackLimit;

Thing preExistingThing = map.thingGrid.ThingAt(storeCell, def);
Expand Down

0 comments on commit 4ffadbb

Please sign in to comment.