Skip to content
This repository has been archived by the owner on Nov 3, 2022. It is now read-only.

Commit

Permalink
MVCF: Minor refactorings and two new features
Browse files Browse the repository at this point in the history
  • Loading branch information
legodude17 committed Mar 1, 2021
1 parent 39cef21 commit c14a830
Show file tree
Hide file tree
Showing 12 changed files with 112 additions and 124 deletions.
Binary file modified 1.2/Assemblies/MVCF.dll
Binary file not shown.
1 change: 0 additions & 1 deletion Source/MVCF/Base.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ public Base(ModContentPack content) : base(content)
if (ModLister.HasActiveModWithName("Prosthetic Combat Framework")) IgnoredFeatures.HediffVerbs = true;
}


public static void CollectFeatureData()
{
foreach (var def in DefDatabase<ModDef>.AllDefs)
Expand Down
7 changes: 7 additions & 0 deletions Source/MVCF/Comps/Properties.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ public CompProperties_VerbGiver()
}
}

public class Comp_VerbProps : ThingComp
{
public CompProperties_VerbProps Props => props as CompProperties_VerbProps;
}

public class CompProperties_VerbProps : CompProperties
{
public List<AdditionalVerbProps> verbProps;
Expand Down Expand Up @@ -70,6 +75,7 @@ public class AdditionalVerbProps
public static BodyTypeDef NA = new BodyTypeDef();
public bool canBeToggled;
public bool canFireIndependently;
public bool colonistOnly;
public DrawPosition defaultPosition;
public string description;
public bool draw;
Expand All @@ -86,6 +92,7 @@ public class AdditionalVerbProps
public string toggleDescription;
public string toggleIconPath;
public string toggleLabel;
public bool uniqueTargets;
public string visualLabel;
public Texture2D ToggleIcon { get; protected set; }
public Texture2D Icon { get; protected set; }
Expand Down
2 changes: 1 addition & 1 deletion Source/MVCF/Harmony/Compat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ public static bool CanRunAndGun(Pawn pawn, Verb verb)
// ReSharper disable once InconsistentNaming
public static void RunAndGunHasRangedWeapon(Pawn instance, ref bool __result)
{
if (!__result) __result = instance.Manager().ManagedVerbs.Any(mv => mv.Enabled && !mv.Verb.IsMeleeAttack);
if (!__result) __result = instance.Manager().CurrentlyUseableRangedVerbs.Any();
}

public static bool RunAndGunVerbCast(ref bool __result, Verb __0)
Expand Down
7 changes: 2 additions & 5 deletions Source/MVCF/Harmony/Hunting.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,8 @@ public static bool TrySetJobToUseAttackVerb(ref Toil __result, TargetIndex targe
}

var man = actor.Manager();
var verbs = man.ManagedVerbs.Where(mv =>
!mv.Verb.IsMeleeAttack && mv.Enabled &&
(!actor.IsColonist || !mv.Verb.verbProps.onlyManualCast) &&
(mv.Props == null || !mv.Props.canFireIndependently) && mv.Verb.Available());
var verb = actor.BestVerbForTarget(actor.jobs.curJob.GetTarget(targetInd), verbs, man) ??
var verb = actor.BestVerbForTarget(actor.jobs.curJob.GetTarget(targetInd),
man.CurrentlyUseableRangedVerbs, man) ??
actor.TryGetAttackVerb(actor.jobs.curJob.GetTarget(targetInd).Thing, !actor.IsColonist);
if (verb == null)
actor.jobs.EndCurrentJob(JobCondition.Incompletable);
Expand Down
4 changes: 1 addition & 3 deletions Source/MVCF/Harmony/Pawn_TryGetAttackVerb.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,7 @@ public static Verb AttackVerb(Pawn pawn, Thing target, bool allowManualCastWeapo
manager.CurrentVerb.CanHitTarget(job.targetA)))
return manager.CurrentVerb;

var verbs = manager.ManagedVerbs.Where(v =>
!v.Verb.IsMeleeAttack && (v.Props == null || !v.Props.canFireIndependently) && v.Enabled &&
v.Verb.Available());
var verbs = manager.CurrentlyUseableRangedVerbs;
if (!allowManualCastWeapons && job != null && job.def == JobDefOf.Wait_Combat)
verbs = verbs.Where(v => !v.Verb.verbProps.onlyManualCast);

Expand Down
61 changes: 3 additions & 58 deletions Source/MVCF/Harmony/Trackers.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System.Linq;
using HarmonyLib;
using MVCF.Comps;
using MVCF.Utilities;
Expand All @@ -20,25 +19,7 @@ public static void Apparel(HarmonyLib.Harmony harm)

public static void ApparelAdded_Postfix(Pawn_ApparelTracker __instance, Apparel apparel)
{
if (Base.IsIgnoredMod(apparel?.def?.modContentPack?.Name)) return;
if (Compat.ShouldIgnore(apparel)) return;
var comp = apparel.TryGetComp<Comp_VerbGiver>();
if (comp?.VerbTracker?.AllVerbs == null) return;
if (!Base.Features.ApparelVerbs && !Base.IgnoredFeatures.ApparelVerbs)
{
Log.ErrorOnce(
"[MVCF] Found apparel with a verb while that feature is not enabled. Enabling now. This is not recommend. Contact the author of " +
apparel?.def?.modContentPack?.Name + " and ask them to add a MVCF.ModDef.",
apparel?.def?.modContentPack?.Name?.GetHashCode() ?? -1);
Base.Features.ApparelVerbs = true;
Base.ApplyPatches();
}

comp.Notify_Worn(__instance.pawn);
var manager = __instance.pawn?.Manager();
if (manager == null) return;
foreach (var verb in comp.VerbTracker.AllVerbs)
manager.AddVerb(verb, VerbSource.Apparel, comp.PropsFor(verb));
__instance.pawn.Manager().AddVerbs(apparel);
}

public static void ApparelRemoved_Postfix(Apparel apparel, Pawn_ApparelTracker __instance)
Expand Down Expand Up @@ -67,25 +48,7 @@ public static void Hediffs(HarmonyLib.Harmony harm)

public static void AddHediff_Postfix(Hediff hediff, Pawn_HealthTracker __instance)
{
if (Base.IsIgnoredMod(hediff?.def?.modContentPack?.Name)) return;
var comp = hediff.TryGetComp<HediffComp_VerbGiver>();
if (comp?.VerbTracker?.AllVerbs == null) return;
if (!Base.Features.HediffVerbs && !Base.IgnoredFeatures.HediffVerbs &&
comp.VerbTracker.AllVerbs.Any(v => !v.IsMeleeAttack))
{
Log.ErrorOnce(
"[MVCF] Found a hediff with a ranged verb while that feature is not enabled. Enabling now. This is not recommend. Contant the author of " +
hediff?.def?.modContentPack?.Name + " and ask them to add a MVCF.ModDef.",
hediff?.def?.modContentPack?.Name?.GetHashCode() ?? -1);
Base.Features.HediffVerbs = true;
Base.ApplyPatches();
}

var manager = __instance?.hediffSet?.pawn?.Manager();
if (manager == null) return;
var extComp = comp as HediffComp_ExtendedVerbGiver;
foreach (var verb in comp.VerbTracker.AllVerbs)
manager.AddVerb(verb, VerbSource.Hediff, extComp?.PropsFor(verb));
__instance.hediffSet.pawn.Manager().AddVerbs(hediff);
}

public static void RemoveHediff_Postfix(Hediff hediff, Pawn_HealthTracker __instance)
Expand All @@ -109,25 +72,7 @@ public static void Equipment(HarmonyLib.Harmony harm)

public static void EquipmentAdded_Postfix(ThingWithComps eq, Pawn_EquipmentTracker __instance)
{
if (Base.IsIgnoredMod(eq?.def?.modContentPack?.Name)) return;
if (Compat.ShouldIgnore(eq)) return;
var comp = eq.TryGetComp<CompEquippable>();
if (comp?.VerbTracker?.AllVerbs == null) return;
var manager = __instance.pawn?.Manager();
if (manager == null) return;
if (!Base.Features.ExtraEquipmentVerbs && !Base.IgnoredFeatures.ExtraEquipmentVerbs &&
comp.VerbTracker.AllVerbs.Count(v => !v.IsMeleeAttack) > 1)
{
Log.ErrorOnce(
"[MVCF] Found equipment with more than one ranged attack while that feature is not enabled. Enabling now. This is not recommend. Contact the author of " +
eq?.def?.modContentPack?.Name + " and ask them to add a MVCF.ModDef.",
eq?.def?.modContentPack?.Name?.GetHashCode() ?? -1);
Base.Features.ExtraEquipmentVerbs = true;
Base.ApplyPatches();
}

foreach (var verb in comp.VerbTracker.AllVerbs)
manager.AddVerb(verb, VerbSource.Equipment, (comp.props as CompProperties_VerbProps)?.PropsFor(verb));
__instance.pawn.Manager()?.AddVerbs(eq);
}

public static void EquipmentRemoved_Postfix(ThingWithComps eq, Pawn_EquipmentTracker __instance)
Expand Down
1 change: 1 addition & 0 deletions Source/MVCF/MVCF.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
<Compile Include="ManagedVerb.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="TurretVerb.cs" />
<Compile Include="Utilities\VerbManagerUtility.cs" />
<Compile Include="VerbManager.cs" />
<Compile Include="Verbs\Verb_Jump.cs" />
<Compile Include="Verbs\Verb_SmokePop.cs" />
Expand Down
11 changes: 10 additions & 1 deletion Source/MVCF/TurretVerb.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
//
// Created 2021-02-02

using System;
using System.Linq;
using MVCF.Comps;
using MVCF.Harmony;
using RimWorld;
Expand Down Expand Up @@ -119,7 +121,14 @@ protected virtual LocalTargetInfo TryFindNewTarget()
return AttackTargetFinder.BestShootTargetFromCurrentPosition(
man.Pawn,
TargetScanFlags.NeedActiveThreat | TargetScanFlags.NeedLOSToAll |
TargetScanFlags.NeedAutoTargetable)?.Thing ?? LocalTargetInfo.Invalid;
TargetScanFlags.NeedAutoTargetable,
Props.uniqueTargets
? new Predicate<Thing>(thing =>
man.Pawn.mindState.enemyTarget != thing &&
man.ManagedVerbs.All(verb =>
verb.Verb.CurrentTarget.Thing != thing &&
(verb as TurretVerb)?.currentTarget.Thing != thing))
: null)?.Thing ?? LocalTargetInfo.Invalid;
}
}

Expand Down
1 change: 1 addition & 0 deletions Source/MVCF/Utilities/PawnVerbUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public static class PawnVerbUtility
{
public static VerbManager Manager(this Pawn p, bool createIfMissing = true)
{
if (p == null) return null;
return Base.Prepatcher
? PrepatchedVerbManager(p, createIfMissing)
: WorldComponent_MVCF.GetComp().GetManagerFor(p, createIfMissing);
Expand Down
79 changes: 79 additions & 0 deletions Source/MVCF/Utilities/VerbManagerUtility.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
using System.Linq;
using MVCF.Comps;
using MVCF.Harmony;
using RimWorld;
using Verse;

namespace MVCF.Utilities
{
public static class VerbManagerUtility
{
public static void AddVerbs(this VerbManager man, ThingWithComps eq)
{
if (man == null) return;
if (Base.IsIgnoredMod(eq?.def?.modContentPack?.Name)) return;
if (Compat.ShouldIgnore(eq)) return;
var comp = eq.TryGetComp<CompEquippable>();
if (comp?.VerbTracker?.AllVerbs == null) return;
if (!Base.Features.ExtraEquipmentVerbs && !Base.IgnoredFeatures.ExtraEquipmentVerbs &&
comp.VerbTracker.AllVerbs.Count(v => !v.IsMeleeAttack) > 1)
{
Log.ErrorOnce(
"[MVCF] Found equipment with more than one ranged attack while that feature is not enabled. Enabling now. This is not recommend. Contact the author of " +
eq?.def?.modContentPack?.Name + " and ask them to add a MVCF.ModDef.",
eq?.def?.modContentPack?.Name?.GetHashCode() ?? -1);
Base.Features.ExtraEquipmentVerbs = true;
Base.ApplyPatches();
}

foreach (var verb in comp.VerbTracker.AllVerbs)
man.AddVerb(verb, VerbSource.Equipment, comp.props is CompProperties_VerbProps props
? props.PropsFor(verb)
: eq.TryGetComp<Comp_VerbProps>()?.Props?.PropsFor(verb));
}

public static void AddVerbs(this VerbManager man, Apparel apparel)
{
if (man == null) return;
if (Base.IsIgnoredMod(apparel?.def?.modContentPack?.Name)) return;
if (Compat.ShouldIgnore(apparel)) return;
var comp = apparel.TryGetComp<Comp_VerbGiver>();
if (comp?.VerbTracker?.AllVerbs == null) return;
if (!Base.Features.ApparelVerbs && !Base.IgnoredFeatures.ApparelVerbs)
{
Log.ErrorOnce(
"[MVCF] Found apparel with a verb while that feature is not enabled. Enabling now. This is not recommend. Contact the author of " +
apparel?.def?.modContentPack?.Name + " and ask them to add a MVCF.ModDef.",
apparel?.def?.modContentPack?.Name?.GetHashCode() ?? -1);
Base.Features.ApparelVerbs = true;
Base.ApplyPatches();
}

comp.Notify_Worn(man.Pawn);
foreach (var verb in comp.VerbTracker.AllVerbs)
man.AddVerb(verb, VerbSource.Apparel, comp.PropsFor(verb));
}

public static void AddVerbs(this VerbManager man, Hediff hediff)
{
if (man == null) return;
if (Base.IsIgnoredMod(hediff?.def?.modContentPack?.Name)) return;
var comp = hediff.TryGetComp<HediffComp_VerbGiver>();
if (comp?.VerbTracker?.AllVerbs == null) return;
if (!Base.Features.HediffVerbs && !Base.IgnoredFeatures.HediffVerbs &&
comp.VerbTracker.AllVerbs.Any(v => !v.IsMeleeAttack))
{
Log.ErrorOnce(
"[MVCF] Found a hediff with a ranged verb while that feature is not enabled. Enabling now. This is not recommend. Contant the author of " +
hediff?.def?.modContentPack?.Name + " and ask them to add a MVCF.ModDef.",
hediff?.def?.modContentPack?.Name?.GetHashCode() ?? -1);
Base.Features.HediffVerbs = true;
Base.ApplyPatches();
}

var extComp = comp as HediffComp_ExtendedVerbGiver;
foreach (var verb in comp.VerbTracker.AllVerbs)
man.AddVerb(verb, VerbSource.Hediff, extComp?.PropsFor(verb));
}
}
}
62 changes: 7 additions & 55 deletions Source/MVCF/VerbManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ public class VerbManager : IVerbOwner
public Verb SearchVerb;
public bool NeedsTicking { get; private set; }

public IEnumerable<ManagedVerb> CurrentlyUseableRangedVerbs => verbs.Where(v =>
!v.Verb.IsMeleeAttack && (v.Props == null || !v.Props.canFireIndependently) && v.Enabled &&
v.Verb.Available() && !(!Pawn.IsColonist && v.Props != null && !v.Props.colonistOnly));

public IEnumerable<Verb> AllVerbs => verbs.Select(mv => mv.Verb);
public IEnumerable<Verb> AllRangedVerbs => verbs.Select(mv => mv.Verb).Where(verb => !verb.IsMeleeAttack);

Expand Down Expand Up @@ -102,69 +106,17 @@ public void Initialize(Pawn pawn)

if (pawn?.health?.hediffSet?.hediffs != null && !Base.IgnoredFeatures.HediffVerbs)
foreach (var hediff in pawn.health.hediffSet.hediffs)
{
if (Base.IsIgnoredMod(hediff?.def?.modContentPack?.Name)) return;
var comp = hediff.TryGetComp<HediffComp_VerbGiver>();
if (comp?.VerbTracker?.AllVerbs == null) continue;
if (!Base.Features.HediffVerbs &&
comp.VerbTracker.AllVerbs.Any(v => !v.IsMeleeAttack))
{
Log.ErrorOnce(
"[MVCF] Found a hediff with a ranged verb while that feature is not enabled. Enabling now. This is not recommend. Contant the author of " +
hediff?.def?.modContentPack?.Name + " and ask them to add a MVCF.ModDef.",
hediff?.def?.modContentPack?.Name?.GetHashCode() ?? -1);
Base.Features.HediffVerbs = true;
Base.ApplyPatches();
}

var extComp = comp as HediffComp_ExtendedVerbGiver;
foreach (var verb in comp.VerbTracker.AllVerbs)
AddVerb(verb, VerbSource.Hediff, extComp?.PropsFor(verb));
}
this.AddVerbs(hediff);

if (pawn?.apparel?.WornApparel != null && !Base.IgnoredFeatures.ApparelVerbs)
foreach (var apparel in pawn.apparel.WornApparel)
{
if (Base.IsIgnoredMod(apparel?.def?.modContentPack?.Name)) return;
var comp = apparel.TryGetComp<Comp_VerbGiver>();
if (comp?.VerbTracker?.AllVerbs == null) continue;
if (!Base.Features.ApparelVerbs)
{
Log.ErrorOnce(
"[MVCF] Found apparel with a verb while that feature is not enabled. Enabling now. This is not recommend. Contact the author of " +
apparel?.def?.modContentPack?.Name + " and ask them to add a MVCF.ModDef.",
apparel?.def?.modContentPack?.Name?.GetHashCode() ?? -1);
Base.Features.ApparelVerbs = true;
Base.ApplyPatches();
}

foreach (var verb in comp.VerbTracker.AllVerbs)
AddVerb(verb, VerbSource.Apparel, comp.PropsFor(verb));
}
this.AddVerbs(apparel);

if (pawn?.equipment?.AllEquipmentListForReading != null && !Base.IgnoredFeatures.ExtraEquipmentVerbs)
foreach (var eq in pawn.equipment.AllEquipmentListForReading)
{
if (Base.IsIgnoredMod(eq?.def?.modContentPack?.Name)) return;
var comp = eq.TryGetComp<CompEquippable>();
if (comp?.VerbTracker?.AllVerbs == null) continue;
if (!Base.Features.ExtraEquipmentVerbs &&
comp.VerbTracker.AllVerbs.Count(v => !v.IsMeleeAttack) > 1)
{
Log.ErrorOnce(
"[MVCF] Found equipment with more than one ranged attack while that feature is not enabled. Enabling now. This is not recommend. Contact the author of " +
eq?.def?.modContentPack?.Name + " and ask them to add a MVCF.ModDef.",
eq?.def?.modContentPack?.Name?.GetHashCode() ?? -1);
Base.Features.ExtraEquipmentVerbs = true;
Base.ApplyPatches();
}

foreach (var verb in comp.VerbTracker.AllVerbs)
AddVerb(verb, VerbSource.Equipment, (comp.props as CompProperties_VerbProps)?.PropsFor(verb));
}
this.AddVerbs(eq);
}


public void AddVerb(Verb verb, VerbSource source, AdditionalVerbProps props)
{
if (debugOpts.VerbLogging) Log.Message("Adding " + verb + " from " + source + " with props " + props);
Expand Down

0 comments on commit c14a830

Please sign in to comment.