Skip to content

Commit

Permalink
v2.1 adds setting for colonist cell count limit
Browse files Browse the repository at this point in the history
  • Loading branch information
pardeike committed Dec 9, 2020
1 parent 7020819 commit 51156b5
Show file tree
Hide file tree
Showing 10 changed files with 3,134 additions and 32 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.samespot</identifier>
<version>2.0.2.0</version>
<version>2.1.0.0</version>
<targetVersions>
<li>1.0.0</li>
<li>1.1.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>da39eefe-71cc-4419-a91c-884eb1c6ea39</ID>
<ModName>SameSpot</ModName>
<Version>2.0.2.0</Version>
<Version>2.1.0.0</Version>
<SaveBreaking>False</SaveBreaking>
<Host name="Github">
<Owner>pardeike</Owner>
Expand Down
Binary file added Current/Assemblies/0Harmony.dll
Binary file not shown.
3,085 changes: 3,085 additions & 0 deletions Current/Assemblies/0Harmony.xml

Large diffs are not rendered by default.

Binary file modified Current/Assemblies/SameSpot.dll
Binary file not shown.
52 changes: 30 additions & 22 deletions Source/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ public static class Main
public static IntVec3 dragStart = IntVec3.Invalid;
public static List<Colonist> draggedColonists = new List<Colonist>();

public static Material markerMaterial = MaterialPool.MatFrom("SameSpotMarker");
public static readonly Material markerMaterial = MaterialPool.MatFrom("SameSpotMarker");
static readonly AccessTools.FieldRef<PawnDestinationReservationManager, Dictionary<Faction, PawnDestinationReservationManager.PawnDestinationSet>> reservedDestinationsRef = AccessTools.FieldRefAccess<PawnDestinationReservationManager, Dictionary<Faction, PawnDestinationReservationManager.PawnDestinationSet>>("reservedDestinations");
public static readonly FastInvokeHandler SelectUnderMouseDelegate = MethodInvoker.GetHandler(AccessTools.Method(typeof(Selector), "SelectUnderMouse"));

public static bool CustomStandable(this IntVec3 c, Map map)
{
Expand All @@ -51,24 +53,19 @@ public static bool CustomStandable(this IntVec3 c, Map map)
return edifice == null || (edifice as Building_Door) != null;
}

public static bool IsReserved(this PawnDestinationReservationManager instance, IntVec3 loc)
public static bool CustomIsReserved(this PawnDestinationReservationManager instance, IntVec3 loc)
{
if (Find.Selector.SelectedObjects.Count == 1) return false;
if (Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift)) return false;
return instance.IsReserved(loc);
if (SameSpotMod.Settings.colonistsPerCell == 0) return false;
var count = reservedDestinationsRef(instance).SelectMany(pair => pair.Value.list).Count(res => res.obsolete == false && res.target == loc);
return count >= SameSpotMod.Settings.colonistsPerCell;
}

public static bool CanReserve(this PawnDestinationReservationManager instance, IntVec3 c, Pawn searcher, bool draftedOnly)
public static bool CustomCanReserve(this PawnDestinationReservationManager instance, IntVec3 c, Pawn searcher, bool draftedOnly)
{
_ = draftedOnly;
if (Find.Selector.SelectedObjects.Count == 1) return true;
if (Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift)) return true;
return instance.CanReserve(c, searcher);
}

public static bool Standable(this IntVec3 c, Map map)
{
return c.CustomStandable(map);
if (SameSpotMod.Settings.colonistsPerCell == 0) return true;
var count = reservedDestinationsRef(instance).SelectMany(pair => pair.Value.list).Count(res => res.obsolete == false && res.claimant != searcher && res.target == c);
return count < SameSpotMod.Settings.colonistsPerCell;
}

public static List<Thing> GetThingList(this IntVec3 c, Map map)
Expand Down Expand Up @@ -118,6 +115,17 @@ public static bool Prefix(ref Pawn __result, Pawn forPawn)
}
return true;
}

public static void Postfix(ref Pawn __result, IntVec3 c, Pawn forPawn)
{
if (__result != null || SameSpotMod.Settings.colonistsPerCell == 0)
return;

var map = forPawn.Map;
var otherPawns = map.thingGrid.ThingsListAtFast(c).OfType<Pawn>().Where(pawn => pawn != forPawn && (pawn.IsColonist || SameSpotMod.Settings.hardcoreMode));
if (otherPawns.Count() >= SameSpotMod.Settings.colonistsPerCell)
__result = otherPawns.First();
}
}

[HarmonyPatch(typeof(Pawn_PathFollower))]
Expand Down Expand Up @@ -159,7 +167,7 @@ static class JobDriver_Goto_TryMakePreToilReservations_Patch
{
public static bool Prefix(JobDriver_Goto __instance, ref bool __result)
{
if (__instance.pawn.IsColonist || SameSpotMod.Settings.hardcoreMode)
if (SameSpotMod.Settings.colonistsPerCell == 0 && (__instance.pawn.IsColonist || SameSpotMod.Settings.hardcoreMode))
{
__result = true;
return false;
Expand All @@ -176,7 +184,7 @@ public static MethodBase TargetMethod()
return typeof(RCellFinder)
.GetNestedTypes(AccessTools.all)
.SelectMany(t => AccessTools.GetDeclaredMethods(t))
.First(m => m.Name.Contains("<" + nameof(RCellFinder.BestOrderedGotoDestNear)));
.First(m => m.Name.Contains($"<{nameof(RCellFinder.BestOrderedGotoDestNear)}"));
}

public static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions)
Expand All @@ -185,12 +193,12 @@ public static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstructio

.MethodReplacer(
AccessTools.Method(typeof(PawnDestinationReservationManager), nameof(PawnDestinationReservationManager.CanReserve)),
AccessTools.Method(typeof(Main), nameof(Main.CanReserve))
AccessTools.Method(typeof(Main), nameof(Main.CustomCanReserve))
)

.MethodReplacer(
AccessTools.Method(typeof(GenGrid), nameof(GenGrid.Standable)),
AccessTools.Method(typeof(Main), nameof(Main.Standable))
AccessTools.Method(typeof(Main), nameof(Main.CustomStandable))
)

.MethodReplacer(
Expand Down Expand Up @@ -224,12 +232,12 @@ public static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstructio

.MethodReplacer(
AccessTools.Method(typeof(PawnDestinationReservationManager), nameof(PawnDestinationReservationManager.IsReserved), new Type[] { typeof(IntVec3) }),
AccessTools.Method(typeof(Main), nameof(Main.IsReserved))
AccessTools.Method(typeof(Main), nameof(Main.CustomIsReserved))
)

.MethodReplacer(
AccessTools.Method(typeof(GenGrid), nameof(GenGrid.Standable)),
AccessTools.Method(typeof(Main), nameof(Main.Standable))
AccessTools.Method(typeof(Main), nameof(Main.CustomStandable))
);
}
}
Expand Down Expand Up @@ -327,7 +335,7 @@ static void MouseDown()
selector.dragBox.active = false;

if (colonistsUnderMouse.Any(colonist => selector.IsSelected(colonist)) == false)
_ = Traverse.Create(selector).Method("SelectUnderMouse").GetValue();
_ = Main.SelectUnderMouseDelegate(selector);

Event.current.Use();
}
Expand Down Expand Up @@ -380,4 +388,4 @@ static void MouseUp()
Event.current.Use();
}
}
}
}
4 changes: 2 additions & 2 deletions Source/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@

[assembly: Guid("a9ece402-5da4-41b1-b8f4-72856986cff6")]

[assembly: AssemblyVersion("2.0.2.0")]
[assembly: AssemblyFileVersion("2.0.2.0")]
[assembly: AssemblyVersion("2.1.0.0")]
[assembly: AssemblyFileVersion("2.1.0.0")]
5 changes: 2 additions & 3 deletions Source/SameSpot.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,8 @@
<Compile Include="Settings.cs" />
</ItemGroup>
<ItemGroup>
<Reference Include="0Harmony, Version=2.0.2.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Lib.Harmony.2.0.2\lib\net472\0Harmony.dll</HintPath>
<Private>False</Private>
<Reference Include="0Harmony, Version=2.0.4.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Lib.Harmony.2.0.4\lib\net472\0Harmony.dll</HintPath>
</Reference>
<Reference Include="Assembly-CSharp">
<HintPath>..\..\..\..\..\..\Program Files (x86)\Steam\steamapps\common\RimWorld\RimWorldWin64_Data\Managed\Assembly-CSharp.dll</HintPath>
Expand Down
14 changes: 12 additions & 2 deletions Source/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,34 @@ public class SameSpotModSettings : ModSettings
public bool enableDragDrop = true;
public bool hardcoreMode = false;
public bool walkableMode = false;
public int colonistsPerCell = 0;

public override void ExposeData()
{
base.ExposeData();
Scribe_Values.Look(ref enableDragDrop, "enableDragDrop", true);
Scribe_Values.Look(ref hardcoreMode, "hardcoreMode", false);
Scribe_Values.Look(ref walkableMode, "walkableMode", false);
Scribe_Values.Look(ref colonistsPerCell, "colonistsPerCell", 0);
}

void NumberEntryLabeled(Listing_Standard list, string label, ref int num, int lineCount = 1)
{
var txt = list.TextEntryLabeled(label, "" + num, lineCount);
try { num = int.Parse(txt); } catch { }
}

public void DoWindowContents(Rect inRect)
{
var list = new Listing_Standard { ColumnWidth = (inRect.width - 34f) / 2f };
var list = new Listing_Standard { ColumnWidth = inRect.width / 2f };
list.Begin(inRect);
list.Gap(12f);
list.CheckboxLabeled("Enable Drag'n Drop", ref enableDragDrop);
list.CheckboxLabeled("SameSpot also for enemies", ref hardcoreMode);
list.CheckboxLabeled("Make walkable also standable", ref walkableMode);
_ = list.Label($"Maximum colonists per cell: {(colonistsPerCell == 0 ? "unlimited" : "" + colonistsPerCell)}");
colonistsPerCell = (int)Mathf.Min(20, list.Slider(colonistsPerCell + 0.5f, 0, 21));
list.End();
}
}
}
}
2 changes: 1 addition & 1 deletion Source/packages.config
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Lib.Harmony" version="2.0.2" targetFramework="net472" />
<package id="Lib.Harmony" version="2.0.4" targetFramework="net472" />
</packages>

0 comments on commit 51156b5

Please sign in to comment.