Skip to content

Commit

Permalink
Merge pull request #3206 from CombatExtended-Continued/eyetoneck
Browse files Browse the repository at this point in the history
Use Neck BodyPart for melee targeting
  • Loading branch information
N7Huntsman authored Jun 24, 2024
2 parents 22b5e76 + 6097982 commit ff9c874
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,19 @@ static MeleeTargettingAdd()
}
}
}

[StaticConstructorOnStartup]
public class CompMeleeTargettingGizmo : ThingComp
{
static CompMeleeTargettingGizmo()
{
priorityList = new List<BodyPartDef>() {
CE_BodyPartDefOf.Neck,
BodyPartDefOf.Eye,
BodyPartDefOf.Head
};
}
private static List<BodyPartDef> priorityList = null;
#region Fields
public Pawn PawnParent => (Pawn)this.parent;

Expand Down Expand Up @@ -92,26 +103,26 @@ public BodyPartHeight finalHeight(Pawn target)

if (PawnParent.skills.GetSkill(SkillDefOf.Melee).Level >= 16)
{
//TODO: 1.5 Should be neck?
targetBodyPart = BodyPartDefOf.Eye;

var neck = target.health.hediffSet.GetNotMissingParts(BodyPartHeight.Top).Where(y => y.def == BodyPartDefOf.Eye).FirstOrFallback();

if (neck != null)
foreach (var bpd in priorityList)
{
var neckApparel = target.apparel?.WornApparel?.Find(x => x.def.apparel.CoversBodyPart(neck));
targetBodyPart = bpd;
var bp = target.health.hediffSet.GetNotMissingParts(BodyPartHeight.Top).Where(y => y.def == bpd).FirstOrFallback();

if (neckApparel != null && maxWeaponPen < neckApparel.GetStatValue(StatDefOf.ArmorRating_Sharp))
{
targetBodyPart = null;
return BodyPartHeight.Bottom;
}
else
if (bp != null)
{
targetBodyPart = neck.def;
var bpApparel = target.apparel?.WornApparel?.Find(x => x.def.apparel.CoversBodyPart(bp));

if (bpApparel != null && maxWeaponPen < bpApparel.GetStatValue(StatDefOf.ArmorRating_Sharp))
{
targetBodyPart = null;
return BodyPartHeight.Bottom;
}
else
{
targetBodyPart = bp.def;
}
}
}

return BodyPartHeight.Top;
}

Expand Down
12 changes: 12 additions & 0 deletions Source/CombatExtended/CombatExtended/DefOfs/CE_BodyPartDefOf.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;
using Verse;
using RimWorld;

namespace CombatExtended
{
[DefOf]
public static class CE_BodyPartDefOf
{
public static BodyPartDef Neck;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,7 @@ private void RebuildArmorCache(Dictionary<BodyPartRecord, float> armorCache, Sta
foreach (BodyPartRecord part in SelPawnForGear.RaceProps.body.AllParts)
{
//TODO: 1.5 should be Neck
if (part.depth == BodyPartDepth.Outside && (part.coverage >= 0.1 || (part.def == BodyPartDefOf.Eye || part.def == BodyPartDefOf.Eye)))
if (part.depth == BodyPartDepth.Outside && (part.coverage >= 0.1 || (part.def == CE_BodyPartDefOf.Neck || part.def == CE_BodyPartDefOf.Neck)))
{
float armorValue = part.IsInGroup(CE_BodyPartGroupDefOf.CoveredByNaturalArmor) ? naturalArmor : 0f;
if (wornApparel != null)
Expand Down
17 changes: 13 additions & 4 deletions Source/CombatExtended/CombatExtended/Verbs/Verb_MeleeAttackCE.cs
Original file line number Diff line number Diff line change
Expand Up @@ -400,9 +400,14 @@ private IEnumerable<DamageInfo> DamageInfosToApply(LocalTargetInfo target, bool
if (caster.def.race.predator && IsTargetImmobile(target))
{
//TODO: 1.5 Should be neck?
var neck = pawn.health.hediffSet.GetNotMissingParts(BodyPartHeight.Top, BodyPartDepth.Outside)
.FirstOrDefault(r => r.def == BodyPartDefOf.Eye);
damageInfo.SetHitPart(neck);
var hp = pawn.health.hediffSet.GetNotMissingParts(BodyPartHeight.Top, BodyPartDepth.Outside)
.FirstOrDefault(r => r.def == CE_BodyPartDefOf.Neck);
if (hp == null)
{
hp = pawn.health.hediffSet.GetNotMissingParts(BodyPartHeight.Top, BodyPartDepth.Outside)
.FirstOrDefault(r => r.def == BodyPartDefOf.Head);
}
damageInfo.SetHitPart(hp);
}
//for some reason, when all parts of height are missing their incode count is 3
if (pawn.health.hediffSet.GetNotMissingParts(bodyRegion).Count() <= 3)
Expand Down Expand Up @@ -451,7 +456,11 @@ private IEnumerable<DamageInfo> DamageInfosToApply(LocalTargetInfo target, bool

if (damageInfo.HitPart != null)
{
damageInfo.SetHitPart(damageInfo.HitPart.GetDirectChildParts().RandomElementByWeight(x => x.coverage));
var children = damageInfo.HitPart.GetDirectChildParts();
if (children.Count() > 0)
{
damageInfo.SetHitPart(children.RandomElementByWeight(x => x.coverage));
}
}
}
}
Expand Down

0 comments on commit ff9c874

Please sign in to comment.