Skip to content

Commit

Permalink
Merge pull request #2899 from CombatExtended-Continued/EMPdontStunMan…
Browse files Browse the repository at this point in the history
…nedTurrets

EMP don't stun non-powered and manned turrets
  • Loading branch information
N7Huntsman authored Dec 6, 2023
2 parents 5251fdc + 5736364 commit b041a18
Showing 1 changed file with 44 additions and 38 deletions.
82 changes: 44 additions & 38 deletions Source/CombatExtended/Harmony/Harmony_StunHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ public static class Harmony_StunHandler_Notify_DamageApplied
{
public static bool Prefix(StunHandler __instance, DamageInfo dinfo, ref int ___EMPAdaptedTicksLeft, ref int ___stunTicksLeft, ref bool ___stunFromEMP)
{

Pawn pawn = __instance.parent as Pawn;
float bodySize = 1.0f;
if (pawn != null)
Expand All @@ -26,42 +25,24 @@ public static bool Prefix(StunHandler __instance, DamageInfo dinfo, ref int ___E
bodySize = pawn.BodySize;
}

if (dinfo.Def == DamageDefOf.EMP && __instance.parent is Pawn p && !(p.RaceProps?.IsFlesh ?? false))
if (dinfo.Def == DamageDefOf.EMP)
{
if (___EMPAdaptedTicksLeft > 0)
if (__instance.parent.TryGetComp<CompPower>() == null && __instance.parent.TryGetComp<CompMannable>() != null)
{
int newStunAdaptedTicks = Mathf.RoundToInt(dinfo.Amount * 45 * bodySize);
int newStunTicks = Mathf.RoundToInt(dinfo.Amount * 30);

float stunResistChance = ((float)___EMPAdaptedTicksLeft / (float)newStunAdaptedTicks) * 15;

if (Rand.Value > stunResistChance)
return false;
}
if (pawn != null && !(pawn.RaceProps?.IsFlesh ?? false))
{
if (___EMPAdaptedTicksLeft > 0)
{
___EMPAdaptedTicksLeft += Mathf.RoundToInt(dinfo.Amount * 45 * bodySize);
int newStunAdaptedTicks = Mathf.RoundToInt(dinfo.Amount * 45 * bodySize);
int newStunTicks = Mathf.RoundToInt(dinfo.Amount * 30);

if (___stunTicksLeft > 0 && newStunTicks > ___stunTicksLeft)
{
___stunTicksLeft = newStunTicks;
}
else
{
__instance.StunFor(newStunTicks, dinfo.Instigator, true, true);
}
}
else
{
MoteMakerCE.ThrowText(new Vector3((float)__instance.parent.Position.x + 1f, (float)__instance.parent.Position.y, (float)__instance.parent.Position.z + 1f), __instance.parent.Map, "Adapted".Translate(), Color.white, -1f);
int adaptationReduction = Mathf.RoundToInt(Mathf.Sqrt(dinfo.Amount * 45));
float stunResistChance = ((float)___EMPAdaptedTicksLeft / (float)newStunAdaptedTicks) * 15;

if (adaptationReduction < ___EMPAdaptedTicksLeft)
{
___EMPAdaptedTicksLeft -= adaptationReduction;
}
else
if (Rand.Value > stunResistChance)
{
float adaptationReductionRatio = (adaptationReduction - ___EMPAdaptedTicksLeft) / adaptationReduction;
newStunAdaptedTicks = Mathf.RoundToInt(newStunAdaptedTicks * adaptationReductionRatio);
newStunTicks = Mathf.RoundToInt(newStunTicks * adaptationReductionRatio);
___EMPAdaptedTicksLeft += Mathf.RoundToInt(dinfo.Amount * 45 * bodySize);

if (___stunTicksLeft > 0 && newStunTicks > ___stunTicksLeft)
{
Expand All @@ -72,15 +53,40 @@ public static bool Prefix(StunHandler __instance, DamageInfo dinfo, ref int ___E
__instance.StunFor(newStunTicks, dinfo.Instigator, true, true);
}
}
}
else
{
MoteMakerCE.ThrowText(new Vector3((float)__instance.parent.Position.x + 1f, (float)__instance.parent.Position.y, (float)__instance.parent.Position.z + 1f), __instance.parent.Map, "Adapted".Translate(), Color.white, -1f);
int adaptationReduction = Mathf.RoundToInt(Mathf.Sqrt(dinfo.Amount * 45));

}
else
{
__instance.StunFor(Mathf.RoundToInt(dinfo.Amount * 30f), dinfo.Instigator, true, true);
___EMPAdaptedTicksLeft = Mathf.RoundToInt(dinfo.Amount * 45 * bodySize);
___stunFromEMP = true;
if (adaptationReduction < ___EMPAdaptedTicksLeft)
{
___EMPAdaptedTicksLeft -= adaptationReduction;
}
else
{
float adaptationReductionRatio = (adaptationReduction - ___EMPAdaptedTicksLeft) / adaptationReduction;
newStunAdaptedTicks = Mathf.RoundToInt(newStunAdaptedTicks * adaptationReductionRatio);
newStunTicks = Mathf.RoundToInt(newStunTicks * adaptationReductionRatio);

if (___stunTicksLeft > 0 && newStunTicks > ___stunTicksLeft)
{
___stunTicksLeft = newStunTicks;
}
else
{
__instance.StunFor(newStunTicks, dinfo.Instigator, true, true);
}
}
}

}
else
{
__instance.StunFor(Mathf.RoundToInt(dinfo.Amount * 30f), dinfo.Instigator, true, true);
___EMPAdaptedTicksLeft = Mathf.RoundToInt(dinfo.Amount * 45 * bodySize);
___stunFromEMP = true;

}
}
}
return true;
Expand Down

0 comments on commit b041a18

Please sign in to comment.