Skip to content

Commit

Permalink
v1.1
Browse files Browse the repository at this point in the history
  • Loading branch information
XeoNovaDan committed Sep 25, 2018
1 parent 7dd7710 commit d1a4246
Show file tree
Hide file tree
Showing 22 changed files with 240 additions and 27 deletions.
2 changes: 1 addition & 1 deletion About/About.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
<author>XeoNovaDan</author>
<url>https://ludeon.com/forums/index.php?topic=29503.0</url>
<targetVersion>0.19.0</targetVersion>
<description>Current Version: v1.0.6 (24th September 2018)\n\nV.A.T.S for RimWorld! Allow your colonists, obedient animals and even some raiders to prioritize targeting certain body part groups of their enemies. Shoot those heads off in style!</description>
<description>Current Version: v1.1 (25th September 2018)\n\nV.A.T.S for RimWorld! Allow your colonists, obedient animals and even some raiders to prioritize targeting certain body part groups of their enemies. Shoot those heads off in style!</description>
</ModMetaData>
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>6ff764b2-16e6-400f-be30-c004b847f984</ID>
<ModName>[XND] Targeting Modes</ModName>
<Version>1.0.6</Version>
<Version>1.1</Version>
<SaveBreaking>False</SaveBreaking>
<Host name="Github">
<Owner>XeoNovaDan</Owner>
Expand Down
Binary file added Assemblies/1SettingsHelper.dll
Binary file not shown.
Binary file modified Assemblies/TargetingModes.dll
Binary file not shown.
7 changes: 0 additions & 7 deletions Languages/English/Keyed/GameplayCommands.xml

This file was deleted.

27 changes: 27 additions & 0 deletions Languages/English/Keyed/Keys.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<LanguageData>

<!-- Settings -->
<TargetingModesSettingsCategory>Targeting Modes</TargetingModesSettingsCategory>
<Settings_AccuracyPenalties>Accuracy penalties</Settings_AccuracyPenalties>
<Settings_AccuracyPenalties_Tooltip>Whether or not targeting modes have an effect on the accuracy of a pawn's attacks.</Settings_AccuracyPenalties_Tooltip>

<Settings_TargModeResetFrequency>Targeting mode resetting frequency</Settings_TargModeResetFrequency>
<Settings_TargModeResetFrequency_0>Never</Settings_TargModeResetFrequency_0>
<Settings_TargModeResetFrequency_1>1d</Settings_TargModeResetFrequency_1>
<Settings_TargModeResetFrequency_2>12h</Settings_TargModeResetFrequency_2>
<Settings_TargModeResetFrequency_3>6h</Settings_TargModeResetFrequency_3>
<Settings_TargModeResetFrequency_4>3h</Settings_TargModeResetFrequency_4>
<Settings_TargModeResetFrequency_5>1h</Settings_TargModeResetFrequency_5>

<Settings_RaidersUseTargetingModes>Hostile NPCs use Targeting Modes</Settings_RaidersUseTargetingModes>
<Settings_RaidersUseTargetingModes_Tooltip>Whether or not any hostile NPCs are able to use targeting modes other than general.</Settings_RaidersUseTargetingModes_Tooltip>
<Settings_MinRaiderWeaponSkill>Minimum weapon skill for raiders</Settings_MinRaiderWeaponSkill>
<Settings_MechTargModeChance>Chance for mechanoids</Settings_MechTargModeChance>
<Settings_BaseManhunterTargModeChance>Base chance for manhunters</Settings_BaseManhunterTargModeChance>

<!-- GameplayCommands -->
<CommandSetTargetingModeMulti>Set targeting mode</CommandSetTargetingModeMulti>
<CommandSetTargetingMode>Targeting: {0}</CommandSetTargetingMode>
<CommandSetTargetingModeDesc>Determine which targeting mode the pawn should use.</CommandSetTargetingModeDesc>

</LanguageData>
Binary file modified Source/TargetingModes/.vs/TargetingModes/v15/.suo
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
4 changes: 2 additions & 2 deletions Source/TargetingModes/Command_SetTargetingMode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ public override void ProcessInput(Event ev)
private string FloatMenuLabel(TargetingModeDef targetingMode)
{
string label = targetingMode.LabelCap;
if (targetingMode.hitChanceFactor != 1f)
label += $" (x{targetingMode.hitChanceFactor.ToStringPercent()} Acc)";
if (targetingMode.HitChanceFactor != 1f)
label += $" (x{targetingMode.HitChanceFactor.ToStringPercent()} Acc)";
return label;
}

Expand Down
58 changes: 55 additions & 3 deletions Source/TargetingModes/CompTargetingMode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Text;
using UnityEngine;
using Verse;
using Verse.AI;
using RimWorld;

namespace TargetingModes
Expand All @@ -25,13 +26,64 @@ public override void CompTick()
{
base.CompTick();
// Debugging
//if (Find.TickManager.TicksGame % (GenTicks.TicksPerRealSecond * 4) == 0)
// Log.Message(this.ToString());
if (Find.TickManager.TicksGame % (GenTicks.TicksPerRealSecond * 4) == 0)
Log.Message(this.ToString());
// For compatibility with existing saves
if (_targetingMode == null)
if (_targetingMode == null || CanResetTargetingMode())
_targetingMode = TargetingModesUtility.DefaultTargetingMode;
}

public bool CanResetTargetingMode()
{
// Non-player world pawns are exempt to player restrictions
if (parent.Faction != Faction.OfPlayer && parent.Map == null)
return Find.TickManager.TicksGame % TargModeResetAttemptInterval(3) == 0;

// If player's set it to never reset, if it isn't time to update or if it's already been reset
if (TargetingModesSettings.TargModeResetFrequencyInt == 0 ||
Find.TickManager.TicksGame % TargModeResetAttemptInterval(TargetingModesSettings.TargModeResetFrequencyInt) != 0 ||
_targetingMode == TargetingModesUtility.DefaultTargetingMode)
return false;

// Not super efficient code, but legibility's the priority
if (Pawn != null)
{
if (Pawn.drafter?.Drafted == false)
return true;
if (!GenAI.InDangerousCombat(Pawn))
return true;
}
if (parent is Building_TurretGun turret)
{
if (turret.CurrentTarget == null)
return true;
}

return false;
}

private static int TargModeResetAttemptInterval(int freqInt)
{
switch (freqInt)
{
// 1 = 1d
case 1:
return GenDate.TicksPerDay;
// 2 = 12h
case 2:
return GenDate.TicksPerHour * 12;
// 3 = 6h
case 3:
return GenDate.TicksPerHour * 6;
// 4 = 3h
case 4:
return GenDate.TicksPerHour * 3;
// other (5) = 1h
default:
return GenDate.TicksPerHour;
};
}

public override void PostExposeData()
{
base.PostExposeData();
Expand Down
33 changes: 29 additions & 4 deletions Source/TargetingModes/HarmonyPatches.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,13 @@ static HarmonyPatches()
{
HarmonyInstance h = HarmonyInstance.Create("XeoNovaDan.TargetingModes");

#region Patches
h.Patch(AccessTools.Method(typeof(PawnGroupMakerUtility), nameof(PawnGroupMakerUtility.GeneratePawns)),
postfix: new HarmonyMethod(patchType, nameof(Postfix_GeneratePawns)));

h.Patch(AccessTools.Method(typeof(ManhunterPackIncidentUtility), nameof(ManhunterPackIncidentUtility.GenerateAnimals)),
postfix: new HarmonyMethod(patchType, nameof(Postfix_GenerateAnimals)));

h.Patch(AccessTools.Method(typeof(Pawn), nameof(Pawn.GetGizmos)),
postfix: new HarmonyMethod(patchType, nameof(Postfix_GetGizmos)));

Expand Down Expand Up @@ -54,6 +58,7 @@ static HarmonyPatches()

h.Patch(AccessTools.Method(typeof(CompSpawnerMechanoidsOnDamaged), "TrySpawnMechanoids"),
transpiler: new HarmonyMethod(patchType, nameof(Transpile_TrySpawnMechanoids)));
#endregion

}

Expand All @@ -69,7 +74,7 @@ private static IEnumerable<Pawn> ModifiedPawnGroup(IEnumerable<Pawn> result, Paw
foreach (Pawn pawn in result)
{
if ((pawn.RaceProps.Humanlike && parms.raidStrategy == TM_RaidStrategyDefOf.ImmediateAttackSmart && pawn.IsCompetentWithWeapon()) ||
(pawn.RaceProps.IsMechanoid && Rand.Chance(TargetingModesUtility.MechanoidRandomTargetingModeChance)))
(pawn.RaceProps.IsMechanoid && Rand.Chance(TargetingModesSettings.mechanoidTargModeChance)))
{
// Validation for CompTargetingMode is done within this method
pawn.TryAssignRandomTargetingMode();
Expand All @@ -79,6 +84,26 @@ private static IEnumerable<Pawn> ModifiedPawnGroup(IEnumerable<Pawn> result, Paw
}
#endregion

#region Postfix_GenerateAnimals
public static void Postfix_GenerateAnimals(ref List<Pawn> __result)
{
__result = ModifiedAnimalGroup(__result).ToList();
}

private static IEnumerable<Pawn> ModifiedAnimalGroup(List<Pawn> result)
{
if (!result.NullOrEmpty())
foreach (Pawn pawn in result)
{
if (Rand.Chance(TargetingModesUtility.AdjustedChanceForAnimal(pawn)))
{
pawn.TryAssignRandomTargetingMode();
}
yield return pawn;
}
}
#endregion

#region Postfix_GetGizmos
public static void Postfix_GetGizmos(Pawn __instance, ref IEnumerable<Gizmo> __result)
{
Expand All @@ -103,13 +128,13 @@ private static bool IsPlayerControlledAnimal(this Pawn pawn) =>
public static void Postfix_HitFactorFromShooter(ref float __result, Thing caster)
{
if (caster.TryGetComp<CompTargetingMode>() is CompTargetingMode targetingComp)
__result = Mathf.Max(__result * targetingComp.GetTargetingMode().hitChanceFactor, ShootTuning.MinAccuracyFactorFromShooterAndDistance);
__result = Mathf.Max(__result * targetingComp.GetTargetingMode().HitChanceFactor, ShootTuning.MinAccuracyFactorFromShooterAndDistance);
}

public static void Postfix_GetNonMissChance(Verb_MeleeAttack __instance, ref float __result)
{
if (__instance.caster is Thing caster && caster.TryGetComp<CompTargetingMode>() is CompTargetingMode targetingComp && __result == caster.GetStatValue(StatDefOf.MeleeHitChance))
__result *= __result * targetingComp.GetTargetingMode().hitChanceFactor;
__result *= __result * targetingComp.GetTargetingMode().HitChanceFactor;
}
#endregion

Expand Down Expand Up @@ -196,7 +221,7 @@ private static bool IsTheInstructionIAmLookingFor(this CodeInstruction instructi

private static void TryAssignRandomTargetingModeMechanoid(this Pawn pawn)
{
if (Rand.Chance(TargetingModesUtility.MechanoidRandomTargetingModeChance))
if (Rand.Chance(TargetingModesSettings.mechanoidTargModeChance))
pawn.TryAssignRandomTargetingMode();
}
#endregion
Expand Down
2 changes: 1 addition & 1 deletion Source/TargetingModes/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.6.0")]
[assembly: AssemblyVersion("1.1.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
3 changes: 3 additions & 0 deletions Source/TargetingModes/TargetingModeDef.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ public class TargetingModeDef : Def
public bool HasNoSpecifiedPartDetails =>
parts.NullOrEmpty() && partsOrAnyChildren.NullOrEmpty() && tags.NullOrEmpty();

public float HitChanceFactor =>
(TargetingModesSettings.accuracyPenalties) ? hitChanceFactor : 1f;

public bool PartsListContains(BodyPartDef def) => !parts.NullOrEmpty() && parts.Contains(def);

public bool PartsOrAnyChildrenListContains(BodyPartRecord part)
Expand Down
6 changes: 6 additions & 0 deletions Source/TargetingModes/TargetingModes.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@
<HintPath>..\..\Assemblies\0Harmony.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="1SettingsHelper, Version=0.19.0.28153, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\Assemblies\1SettingsHelper.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="AbilityUser">
<HintPath>references\AbilityUser.dll</HintPath>
<Private>False</Private>
Expand Down Expand Up @@ -61,6 +66,7 @@
<Compile Include="TargetingModeDef.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="TargetingModeDefOf.cs" />
<Compile Include="TargetingModesSettings.cs" />
<Compile Include="TargetingModesUtility.cs" />
<Compile Include="TargetModeRerollFactors.cs" />
<Compile Include="TM_RaidStrategyDefOf.cs" />
Expand Down
99 changes: 99 additions & 0 deletions Source/TargetingModes/TargetingModesSettings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
using Verse;
using RimWorld;
using SettingsHelper;

namespace TargetingModes
{
public class TargetingModesSettings : ModSettings
{

public static bool accuracyPenalties = true;
#region TargModeResetFrequencyInt
private static float targModeResetFrequencyInt = 3f;
public static int TargModeResetFrequencyInt =>
(int)targModeResetFrequencyInt;
#endregion
public static bool raidersUseTargModes = true;
#region MinimumSkillForRandomTargetingMode
private static float raiderMinSkillForTargMode = 8f;
public static int MinimumSkillForRandomTargetingMode =>
(int)raiderMinSkillForTargMode;
#endregion
public static float mechanoidTargModeChance = 0.35f;
public static float baseManhunterTargModeChance = 0.2f;

public void DoWindowContents(Rect wrect)
{
Listing_Standard options = new Listing_Standard();
Color defaultColor = GUI.color;
options.Begin(wrect);

GUI.color = defaultColor;
Text.Font = GameFont.Small;
Text.Anchor = TextAnchor.UpperLeft;
options.Gap();
// General settings
options.CheckboxLabeled("Settings_AccuracyPenalties".Translate(), ref accuracyPenalties, "Settings_AccuracyPenalties_Tooltip".Translate());
options.Gap();
options.AddLabeledSlider("Settings_TargModeResetFrequency".Translate(), ref targModeResetFrequencyInt, 0, 5,
rightAlignedLabel: $"Settings_TargModeResetFrequency_{targModeResetFrequencyInt}".Translate(), roundTo: 1);
options.GapLine(24);

// Settings for AI
options.CheckboxLabeled("Settings_RaidersUseTargetingModes".Translate(), ref raidersUseTargModes, "Settings_RaidersUseTargetingModes_Tooltip".Translate());
options.Gap();

// Grey out this section if raiders can't use targeting modes
if (!raidersUseTargModes)
GUI.color = Color.grey;

options.AddLabeledSlider("Settings_MinRaiderWeaponSkill".Translate(), ref raiderMinSkillForTargMode, 0, 20,
rightAlignedLabel: raiderMinSkillForTargMode.ToString(), roundTo: 1);
options.Gap();
options.AddLabeledSlider("Settings_MechTargModeChance".Translate(), ref mechanoidTargModeChance, 0, 1,
rightAlignedLabel: mechanoidTargModeChance.ToStringPercent(), roundTo: 0.01f);
options.Gap();
options.AddLabeledSlider("Settings_BaseManhunterTargModeChance".Translate(), ref baseManhunterTargModeChance, 0, 1,
rightAlignedLabel: baseManhunterTargModeChance.ToStringPercent(), roundTo: 0.01f);
// End of section
GUI.color = defaultColor;

options.End();

Mod.GetSettings<TargetingModesSettings>().Write();

}

public override void ExposeData()
{
Scribe_Values.Look(ref accuracyPenalties, "accuracyPenalties", true);
Scribe_Values.Look(ref raidersUseTargModes, "raidersUseTargModes", true);
Scribe_Values.Look(ref raiderMinSkillForTargMode, "raiderMinSkillForTargMode", 8f);
Scribe_Values.Look(ref mechanoidTargModeChance, "mechanoidTargModeChance", 0.35f);
Scribe_Values.Look(ref baseManhunterTargModeChance, "baseManhunterTargModeChance", 0.2f);
}

}

public class TargetingModes : Mod
{
public TargetingModesSettings settings;

public TargetingModes(ModContentPack content) : base(content)
{
GetSettings<TargetingModesSettings>();
}

public override string SettingsCategory() => "TargetingModesSettingsCategory".Translate();

public override void DoSettingsWindowContents(Rect inRect)
{
GetSettings<TargetingModesSettings>().DoWindowContents(inRect);
}
}
}
Loading

0 comments on commit d1a4246

Please sign in to comment.