Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shields and suppresion #2809

Merged
merged 22 commits into from
Jan 1, 2024
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Verse.AI;
using UnityEngine;
using CombatExtended.AI;
using CombatExtended.Compatibility;

namespace CombatExtended
{
Expand Down Expand Up @@ -243,6 +244,10 @@ public void AddSuppression(float amount, IntVec3 origin)
}
}
}
public bool IgnoreSuppresion(IntVec3 origin)
{
return BlockerRegistry.PawnUnsuppresableFromCallback(parent as Pawn, origin) || SuppressionUtility.InterceptorZonesFor((Pawn)parent).Where(x => x.Contains(parent.Position)).Any(x => !x.Contains(origin));
}

public override void CompTick()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1074,7 +1074,8 @@ protected void ApplySuppression(Pawn pawn, float suppressionMultiplier = 1f)
var compSuppressable = pawn.TryGetComp<CompSuppressable>();
if (compSuppressable != null
&& pawn.Faction != launcher?.Faction
&& (shield == null || shield.ShieldState == ShieldState.Resetting))
&& (shield == null || shield.ShieldState == ShieldState.Resetting)
&& !compSuppressable.IgnoreSuppresion(OriginIV3))
{
suppressionAmount = def.projectile.damageAmountBase * suppressionMultiplier;

Expand Down
29 changes: 18 additions & 11 deletions Source/CombatExtended/CombatExtended/SuppressionUtility.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Collections.Generic;
using System.Linq;
using CombatExtended.AI;
using CombatExtended.Compatibility;
using CombatExtended.Utilities;
using RimWorld;
using UnityEngine;
Expand All @@ -23,7 +24,7 @@ public static class SuppressionUtility

private static DangerTracker dangerTracker;

private static List<CompProjectileInterceptor> interceptors;
private static IEnumerable<CompProjectileInterceptor> Interceptors(Thing pawn) => pawn.Map.listerThings.ThingsInGroup(ThingRequestGroup.ProjectileInterceptor).Select(t => t.TryGetComp<CompProjectileInterceptor>());
perkinslr marked this conversation as resolved.
Show resolved Hide resolved

public static bool TryRequestHelp(Pawn pawn)
{
Expand Down Expand Up @@ -91,7 +92,6 @@ private static bool GetCoverPositionFrom(Pawn pawn, IntVec3 fromPosition, float
{
List<IntVec3> cellList = new List<IntVec3>(GenRadial.RadialCellsAround(pawn.Position, maxDist, true));
IntVec3 bestPos = pawn.Position;
interceptors = pawn.Map.listerThings.ThingsInGroup(ThingRequestGroup.ProjectileInterceptor).Select(t => t.TryGetComp<CompProjectileInterceptor>()).ToList();
lightingTracker = pawn.Map.GetLightingTracker();
dangerTracker = pawn.Map.GetDangerTracker();

Expand Down Expand Up @@ -203,14 +203,8 @@ private static float GetCellCoverRatingForPawn(Pawn pawn, IntVec3 cell, IntVec3

cellRating += 10f - (bonusCellRating * 10f);

for (int i = 0; i < interceptors.Count; i++)
{
CompProjectileInterceptor interceptor = interceptors[i];
if (interceptor.Active && interceptor.parent.Position.DistanceTo(cell) < interceptor.Props.radius)
{
cellRating += 15f;
}
}
// If the cell is covered by a shield and there are no enemies inside, then increases by 15 (for each such shield)
cellRating += InterceptorZonesFor(pawn).Where(x => !IsOccupiedByEnemies(x, pawn)).Count(x => x.Contains(cell)) * 15;

// Avoid bullets and other danger sources;
// Yet do not discard cover that is extremely good, even if it may be dangerous
Expand Down Expand Up @@ -246,7 +240,20 @@ private static float GetCellCoverRatingForPawn(Pawn pawn, IntVec3 cell, IntVec3
}
return cellRating;
}

public static IEnumerable<IEnumerable<IntVec3>> InterceptorZonesFor(Pawn pawn)
{
var result = Interceptors(pawn).Where(x => x.Active).Select(x => GenRadial.RadialCellsAround(x.parent.Position, x.Props.radius, true));
var compatibilityZones = BlockerRegistry.ShieldZonesCallback(pawn);
if (compatibilityZones != null)
{
result = result.Union(compatibilityZones);
}
return result;
}
private static bool IsOccupiedByEnemies(IEnumerable<IntVec3> cells, Pawn pawn)
{
return cells.Any(cell => pawn.Map.thingGrid.ThingsListAt(cell).Any(thing => (thing.HostileTo(pawn))));
}
private static float GetCoverRating(Thing cover)
{
// Higher values mean more effective at being considered cover.
Expand Down
57 changes: 54 additions & 3 deletions Source/CombatExtended/Compatibility/BlockerRegistry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,14 @@ public static class BlockerRegistry
private static bool enabledCFC = false;
private static bool enabledIS = false;
private static bool enabledBCW = false;
private static bool enabledPUF = false;
private static bool enabledSZ = false;
private static List<Func<ProjectileCE, Vector3, Vector3, bool>> checkForCollisionBetweenCallbacks;
private static List<Func<ProjectileCE, IntVec3, Thing, bool>> checkCellForCollisionCallbacks;
private static List<Func<ProjectileCE, Thing, bool>> impactSomethingCallbacks;
private static List<Func<ProjectileCE, Thing, bool>> beforeCollideWithCallbacks;
private static List<Func<Pawn, IntVec3, bool>> pawnUnsuppresableFromCallback;
private static List<Func<Thing, IEnumerable<IEnumerable<IntVec3>>>> shieldZonesCallback;

private static void EnableCB()
{
Expand All @@ -35,6 +39,16 @@ private static void EnableCFC()
enabledCFC = true;
checkCellForCollisionCallbacks = new List<Func<ProjectileCE, IntVec3, Thing, bool>>();
}
private static void EnableSZ()
{
enabledSZ = true;
shieldZonesCallback = new List<Func<Thing, IEnumerable<IEnumerable<IntVec3>>>>();
}
private static void EnablePUF()
{
enabledPUF = true;
pawnUnsuppresableFromCallback = new List<Func<Pawn, IntVec3, bool>>();
}
private static void EnableBCW()
{
enabledBCW = true;
Expand Down Expand Up @@ -90,6 +104,23 @@ public static bool CheckForCollisionBetweenCallback(ProjectileCE projectile, Vec
return false;
}

public static void RegisterShieldZonesCallback(Func<Thing, IEnumerable<IEnumerable<IntVec3>>> f)
{
if (!enabledSZ)
{
EnableSZ();
}
shieldZonesCallback.Add(f);
}
public static void RegisterUnsuppresableFromCallback(Func<Pawn, IntVec3, bool> f)
{
if (!enabledPUF)
{
EnablePUF();
}
pawnUnsuppresableFromCallback.Add(f);
}

public static bool CheckCellForCollisionCallback(ProjectileCE projectile, IntVec3 cell, Thing launcher)
{
if (!enabledCFC)
Expand All @@ -105,7 +136,6 @@ public static bool CheckCellForCollisionCallback(ProjectileCE projectile, IntVec
}
return false;
}

public static bool ImpactSomethingCallback(ProjectileCE projectile, Thing launcher)
{
if (!enabledIS)
Expand All @@ -121,7 +151,29 @@ public static bool ImpactSomethingCallback(ProjectileCE projectile, Thing launch
}
return false;
}

public static IEnumerable<IEnumerable<IntVec3>> ShieldZonesCallback(Thing thing)
{
if (!enabledSZ)
{
return null;
}
return shieldZonesCallback.SelectMany(cb => cb(thing));
}
public static bool PawnUnsuppresableFromCallback(Pawn pawn, IntVec3 origin)
{
if (!enabledPUF)
{
return false;
}
foreach (var cb in pawnUnsuppresableFromCallback)
{
if (cb(pawn, origin))
{
return true;
}
}
return false;
}
public static bool BeforeCollideWithCallback(ProjectileCE projectile, Thing collideWith)
{
if (!enabledBCW)
Expand All @@ -137,7 +189,6 @@ public static bool BeforeCollideWithCallback(ProjectileCE projectile, Thing coll
}
return false;
}

public static Vector3 GetExactPosition(Vector3 origin, Vector3 curPosition, Vector3 shieldPosition, float radiusSq)
{
Vector3 velocity = curPosition - origin;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,21 @@ public IEnumerable<string> GetCompatList()
}
public void Install()
{

BlockerRegistry.RegisterImpactSomethingCallback(ImpactSomething); //temp commented
BlockerRegistry.RegisterBeforeCollideWithCallback(BeforeCollideWith);
BlockerRegistry.RegisterCheckForCollisionCallback(Hediff_Overshield_InterceptCheck);
BlockerRegistry.RegisterCheckForCollisionBetweenCallback(AOE_CheckIntercept);
BlockerRegistry.RegisterShieldZonesCallback(ShieldZones);
BlockerRegistry.RegisterUnsuppresableFromCallback(Unsuppresable);
}
private static IEnumerable<IEnumerable<IntVec3>> ShieldZones(Thing thing)
{
perkinslr marked this conversation as resolved.
Show resolved Hide resolved
return thing.Map.listerThings.ThingsInGroup(ThingRequestGroup.Pawn).Cast<Pawn>().SelectMany(x => x.health.hediffSet.hediffs).OfType<Hediff_Overshield>().Select(x => GenRadial.RadialCellsAround(x.pawn.Position, x.OverlaySize, true));
}

private static bool Unsuppresable(Pawn pawn, IntVec3 origin) => pawn.health.hediffSet.hediffs.Any(x => x.GetType() == typeof(Hediff_Overshield));

private static bool BeforeCollideWith(ProjectileCE projectile, Thing collideWith)
{
if (collideWith is Pawn pawn)
Expand Down