Skip to content

Commit

Permalink
Merge pull request #888 from Tachyonite/Additions
Browse files Browse the repository at this point in the history
Polishing and fixes
  • Loading branch information
FxWolfxe authored Jan 23, 2024
2 parents 39a838d + 16b3bcc commit f9a2f7e
Show file tree
Hide file tree
Showing 8 changed files with 157 additions and 103 deletions.
66 changes: 41 additions & 25 deletions Source/Pawnmorphs/Esoteria/Chambers/MutaChamber.cs
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,8 @@ private void SelectorComp_OnClick([NotNull] AnimalSelectorComp comp)
comp.SpeciesFilter = (x) => x.GetModExtension<ChamberAnimalTfController>()?.CanInitiateTransformation(pawn, x, this) ?? true;
}

// ThingDefs with classes derived from MutaChamber
private static IList<ThingDef> chamberDefs;

/// <summary>
/// Finds the Mutachamber casket for.
Expand All @@ -320,40 +322,54 @@ private void SelectorComp_OnClick([NotNull] AnimalSelectorComp comp)
/// <param name="ignoreOtherReservations">if set to <c>true</c> [ignore other reservations].</param>
/// <param name="use">The use.</param>
/// <returns></returns>
public static MutaChamber FindMutaChamberFor(Pawn p, Pawn traveler, bool ignoreOtherReservations = false,
ChamberUse? use = null)
public static MutaChamber FindMutaChamberFor(Pawn p, Pawn traveler, bool ignoreOtherReservations = false, ValueTuple<ChamberUse, ChamberUse>? use = null)
{
IEnumerable<ThingDef> enumerable = from def in DefDatabase<ThingDef>.AllDefs
where typeof(MutaChamber).IsAssignableFrom(def.thingClass)
select def;
foreach (ThingDef item in enumerable)
if (chamberDefs == null)
chamberDefs = DefDatabase<ThingDef>.AllDefs.Where(x => typeof(MutaChamber).IsAssignableFrom(x.thingClass)).ToList();


bool Validator(Thing x)
{
bool Validator(Thing x)
var mutaChamber = (MutaChamber)x;
ChamberUse currentUse = mutaChamber.CurrentUse;
if ((use == null || use.Value.Item1 == currentUse || use.Value.Item2 == currentUse)
&& mutaChamber.CanAcceptPawns
&& mutaChamber.Flickable.SwitchIsOn)
{
int result;
var mutaChamber = (MutaChamber)x;
if (mutaChamber.CanAcceptPawns
&& mutaChamber.Flickable.SwitchIsOn
&& (use == null || use == mutaChamber.CurrentUse))
{
Pawn p2 = traveler;
LocalTargetInfo target = x;
bool ignoreOtherReservations2 = ignoreOtherReservations;
result = p2.CanReserve(target, 1, -1, null, ignoreOtherReservations2) ? 1 : 0;
}
else
return traveler.CanReserve(x, 1, -1, null, ignoreOtherReservations);
}
return false;
}

// For each type of chamber defs.
for (int i = 0; i < chamberDefs.Count; i++)
{
ThingDef def = chamberDefs[i];

var chambers = p.Map.listerThings.ThingsOfDef(def);
int chambersCount = chambers.Count;
if (chambersCount == 0)
continue;

bool anyTargets = false;
for (int chamberIndex = chambers.Count - 1; chamberIndex >= 0; chamberIndex--)
{
// Check if any chambers are available before doing pathing.
if (Validator(chambers[chamberIndex]))
{
result = 0;
anyTargets = true;
break;
}

return result != 0;
}
if (anyTargets == false)
continue;

var building_MutagenChamber =
(MutaChamber)GenClosest.ClosestThingReachable(p.Position, p.Map, ThingRequest.ForDef(item),
var building_MutagenChamber = (MutaChamber)GenClosest.ClosestThingReachable(p.Position, p.Map, ThingRequest.ForDef(def),
PathEndMode.InteractionCell, TraverseParms.For(traveler),
9999f, Validator);
if (building_MutagenChamber != null) return building_MutagenChamber;

if (building_MutagenChamber != null)
return building_MutagenChamber;
}

return null;
Expand Down
114 changes: 54 additions & 60 deletions Source/Pawnmorphs/Esoteria/HPatches/FloatMenuMakerMapPatches.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,80 +24,74 @@ internal static class FloatMenuMakerMapPatches
[HarmonyPatch("AddHumanlikeOrders")]
internal static class AddHumanlikeOrdersPatch
{
private static ValueTuple<Pawn, bool, string, string> _pawnCanTransformCache = (null, false, string.Empty, string.Empty);

[HarmonyPrefix]
private static bool Prefix_AddHumanlikeOrders(Vector3 clickPos, Pawn pawn, List<FloatMenuOption> opts)
{
if (pawn.health.capacities.CapableOf(PawnCapacityDefOf.Manipulation))
foreach (LocalTargetInfo localTargetInfo3 in GenUI.TargetsAt(clickPos, TargetingParameters.ForRescue(pawn), true).MakeSafe())
{
IEnumerable<LocalTargetInfo> targets = GenUI.TargetsAt(clickPos, TargetingParameters.ForRescue(pawn), true);
if (targets != null)
{
LocalTargetInfo localTargetInfo4 = localTargetInfo3;
var victim = (Pawn)localTargetInfo4.Thing;
MutagenDef mutagen = MutagenDefOf.MergeMutagen;
if (mutagen.CanTransform(victim)
&& pawn.CanReserveAndReach(victim, PathEndMode.OnCell, Danger.Deadly, 1, -1, null, true))
foreach (LocalTargetInfo localTargetInfo3 in targets)
{
if (MutaChamber.FindMutaChamberFor(victim, pawn, true, ChamberUse.Tf) != null)
{
string text4 = "CarryToChamber".Translate(localTargetInfo4.Thing.LabelCap, localTargetInfo4.Thing);
JobDef jDef = Mutagen_JobDefOf.CarryToMutagenChamber;
Action action3 = delegate
{
var building_chamber =
MutaChamber.FindMutaChamberFor(victim, pawn);
if (building_chamber == null)
building_chamber = MutaChamber.FindMutaChamberFor(victim, pawn, true);
if (building_chamber == null)
{
Messages.Message("CannotCarryToChamber".Translate() + ": " + "NoChamber".Translate(), victim,
MessageTypeDefOf.RejectInput, false);
return;
}

var job = new Job(jDef, victim, building_chamber);
job.count = 1;
pawn.jobs.TryTakeOrderedJob(job);
};
string label = text4;
Action action2 = action3;
Pawn revalidateClickTarget = victim;
opts.Add(FloatMenuUtility
.DecoratePrioritizedTask(new FloatMenuOption(label, action2, MenuOptionPriority.Default, null, revalidateClickTarget),
pawn, victim));
}
if (MutaChamber.FindMutaChamberFor(victim, pawn, true, ChamberUse.Merge) != null)
LocalTargetInfo localTargetInfo4 = localTargetInfo3;
var victim = (Pawn)localTargetInfo4.Thing;
MutagenDef mutagen = MutagenDefOf.MergeMutagen;

// As long as the target remains unchanged, then cache mutability state.
if (_pawnCanTransformCache.Item1 != victim)
_pawnCanTransformCache = (victim,
mutagen.CanTransform(victim) && pawn.CanReserveAndReach(victim, PathEndMode.OnCell, Danger.Deadly, 1, -1, null, true),
"CarryToChamber".Translate(victim.LabelCap, victim),
"PMCarryToChamberMerge".Translate(victim.LabelCap, victim)
);

if (_pawnCanTransformCache.Item2)
{
string text4 = "PMCarryToChamberMerge".Translate(localTargetInfo4.Thing.LabelCap, localTargetInfo4.Thing);
JobDef jDef = Mutagen_JobDefOf.CarryToMutagenChamber;
Action action3 = delegate
{
var building_chamber =
MutaChamber.FindMutaChamberFor(victim, pawn);
if (building_chamber == null)
building_chamber = MutaChamber.FindMutaChamberFor(victim, pawn, true);
if (building_chamber == null)
{
Messages.Message("CannotCarryToChamber".Translate() + ": " + "NoChamber".Translate(), victim,
MessageTypeDefOf.RejectInput, false);
return;
}

var job = new Job(jDef, victim, building_chamber);
job.count = 1;
pawn.jobs.TryTakeOrderedJob(job);
};
string label = text4;
Action action2 = action3;
Pawn revalidateClickTarget = victim;
opts.Add(FloatMenuUtility
.DecoratePrioritizedTask(new FloatMenuOption(label, action2, MenuOptionPriority.Default, null, revalidateClickTarget),
pawn, victim));
CarryToChamber(pawn, opts, victim, _pawnCanTransformCache.Item3, ValueTuple.Create(ChamberUse.Mutation, ChamberUse.Tf));
CarryToChamber(pawn, opts, victim, _pawnCanTransformCache.Item4, ValueTuple.Create(ChamberUse.Merge, ChamberUse.Merge));
}
}
}
}

return true;
}

private static void CarryToChamber(Pawn pawn, List<FloatMenuOption> opts, Pawn victim, string label, ValueTuple<ChamberUse, ChamberUse> useType)
{
// Is there any chambers including reserved that fit the requirements?
var mergeChamber = MutaChamber.FindMutaChamberFor(victim, pawn, true, useType);
if (mergeChamber != null)
{
Action action = delegate
{
// Is there any chambers that isn't already reserved? then prioritise that.
var building_chamber = MutaChamber.FindMutaChamberFor(victim, pawn, false, useType);

// If none is found then check for any reserved again.
// Chambers might no longer be avaiable between right clicking and selecting the option.
if (building_chamber == null)
building_chamber = MutaChamber.FindMutaChamberFor(victim, pawn, true, useType);

if (building_chamber == null)
{
Messages.Message("CannotCarryToChamber".Translate() + ": " + "NoChamber".Translate(), victim, MessageTypeDefOf.RejectInput, false);
return;
}

var job = new Job(Mutagen_JobDefOf.CarryToMutagenChamber, victim, building_chamber);
job.count = 1;
pawn.jobs.TryTakeOrderedJob(job);
};
opts.Add(FloatMenuUtility.DecoratePrioritizedTask(new FloatMenuOption(label, action, MenuOptionPriority.Default, null, victim),
pawn, victim));
}
}
}

#if true
[HarmonyPatch(typeof(FloatMenuMakerMap))]
[HarmonyPatch("CanTakeOrder")]
Expand Down
8 changes: 6 additions & 2 deletions Source/Pawnmorphs/Esoteria/HPatches/GeneTrackerPatches.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,12 @@ static void EnsureCorrectSkinColorOverridePostfix(Pawn ___pawn)
GraphicsUpdaterComp graphicsComp = ___pawn.TryGetComp<GraphicsUpdaterComp>();
if (graphicsComp != null)
{
graphicsComp.GeneOverrideColor = ___pawn.story.skinColorOverride;
graphicsComp.RefreshGraphics();
UnityEngine.Color? currentColor = ___pawn.story.skinColorOverride;
if (currentColor != graphicsComp.GeneOverrideColor)
{
graphicsComp.GeneOverrideColor = currentColor;
graphicsComp.RefreshGraphics();
}
}
}

Expand Down
12 changes: 9 additions & 3 deletions Source/Pawnmorphs/Esoteria/HPatches/PawnPatches.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,24 @@ private static void OnClear(PawnmorphGameComp obj)
static void FixCombinedDisabledWorkTags(ref WorkTags __result, [NotNull] Pawn __instance)
{
var hediffs = __instance.health?.hediffSet?.hediffs;
if (hediffs == null) return;
if (hediffs == null)
return;

foreach (Hediff hediff in hediffs)
for (int i = hediffs.Count - 1; i >= 0; i--)
{
Hediff hediff = hediffs[i];
if (hediff is IWorkModifier wM)
{
__result |= ~wM.AllowedWorkTags;
}
else
{
foreach (HediffStage hediffStage in hediff.def.stages.MakeSafe())
if (hediff.def?.stages == null)
continue;

for (int stageIndex = hediff.def.stages.Count - 1; stageIndex >= 0; stageIndex--)
{
HediffStage hediffStage = hediff.def.stages[stageIndex];
if (hediffStage is IWorkModifier sWM)
{
__result |= ~sWM.AllowedWorkTags;
Expand Down
13 changes: 13 additions & 0 deletions Source/Pawnmorphs/Esoteria/MorphUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using Pawnmorph.DefExtensions;
using Pawnmorph.Hediffs;
using Pawnmorph.Hybrids;
using Pawnmorph.RecipeWorkers;
using Pawnmorph.Utilities;
using RimWorld;
using Verse;
Expand Down Expand Up @@ -69,6 +70,12 @@ public static void Initialize()
{
foreach (MorphDef morph in DefDatabase<MorphDef>.AllDefsListForReading)
{
if (morph.race == null)
{
Log.Error($"{morph.defName} has null animal assigned.");
continue;
}

//first check the race
if (!_associatedAnimalsLookup.TryGetValue(morph.race, out var lst))
{
Expand All @@ -81,6 +88,12 @@ public static void Initialize()
//now the associated animals
foreach (ThingDef aAnimal in morph.associatedAnimals.MakeSafe())
{
if (aAnimal == null)
{
Log.Error($"{morph.defName} has associated null animal.");
continue;
}

if (!_associatedAnimalsLookup.TryGetValue(aAnimal, out lst))
{
lst = new List<MorphDef>();
Expand Down
15 changes: 12 additions & 3 deletions Source/Pawnmorphs/Esoteria/PawnmorphPatches.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Reflection.Emit;
Expand All @@ -20,7 +21,6 @@
using Pawnmorph.Utilities;
using RimWorld;
using RimWorld.Planet;
using UnityEngine.SocialPlatforms;
using Verse;
using Verse.AI;

Expand All @@ -46,6 +46,10 @@ internal static class PawnmorphPatches

static PawnmorphPatches()
{
#if DEBUG
Stopwatch stopwatch = Stopwatch.StartNew();
#endif


_animalTabWorkerMethod =
typeof(PawnmorphPatches).GetMethod(nameof(AnimalTabWorkerMethod), BindingFlags.Static | BindingFlags.NonPublic);
Expand Down Expand Up @@ -123,7 +127,7 @@ static PawnmorphPatches()
}
catch (Exception e)
{
Log.Error($"Pawnmorpher cannot preform harmony patches! caught {e.GetType().Name}\n{e}");
Log.Error($"Pawnmorpher cannot perform harmony patches! caught {e.GetType().Name}\n{e}");
}

try
Expand All @@ -132,12 +136,17 @@ static PawnmorphPatches()
}
catch (Exception e)
{
Log.Error($"Pawnmorpher cannot preform debugging patches! caught {e.GetType().Name}\n{e}");
Log.Error($"Pawnmorpher cannot perform debugging patches! caught {e.GetType().Name}\n{e}");
}

ConversionUtilityPatches.PreformPatches(harmonyInstance);
ThoughtWorkerPatches.DoPatches(harmonyInstance);
InteractionPatches.PatchDelegateMethods(harmonyInstance);

#if DEBUG
stopwatch.Stop();
Log.Message($"[{DateTime.Now.TimeOfDay}][Pawnmorpher]: Harmony patching finished in {stopwatch.ElapsedMilliseconds}ms");
#endif
}

private static void PatchMods([NotNull] Harmony harmonyInstance)
Expand Down
Loading

0 comments on commit f9a2f7e

Please sign in to comment.