diff --git a/Source/CombatExtended/CombatExtended/CollisionVertical.cs b/Source/CombatExtended/CombatExtended/CollisionVertical.cs index 0f624f6424..5d7bdc4795 100755 --- a/Source/CombatExtended/CombatExtended/CollisionVertical.cs +++ b/Source/CombatExtended/CombatExtended/CollisionVertical.cs @@ -19,11 +19,11 @@ public struct CollisionVertical private readonly FloatRange heightRange; public readonly float shotHeight; - public FloatRange HeightRange => new FloatRange(heightRange.min, heightRange.max); + public FloatRange HeightRange => new FloatRange(Mathf.Max(0, heightRange.min), Mathf.Max(0.1f, heightRange.max)); public float Min => heightRange.min; public float Max => heightRange.max; - public float BottomHeight => Max * BodyRegionBottomHeight; - public float MiddleHeight => Max * BodyRegionMiddleHeight; + public float BottomHeight => Max - (heightRange.Span * (1 - BodyRegionBottomHeight)); + public float MiddleHeight => Max - (heightRange.Span * (1 - BodyRegionMiddleHeight)); public CollisionVertical(Thing thing) { @@ -73,7 +73,7 @@ private static void CalculateHeightRange(Thing thing, out FloatRange heightRange } float collisionHeight = 0f; float shotHeightOffset = 0; - float heightAdjust = 0; + float heightAdjust = CETrenches.GetHeightAdjust(thing.Position, thing.Map); var pawn = thing as Pawn; if (pawn != null) @@ -127,8 +127,10 @@ private static void CalculateHeightRange(Thing thing, out FloatRange heightRange } } float fillPercent2 = collisionHeight; - heightRange = new FloatRange(Mathf.Min(edificeHeight, edificeHeight + fillPercent2) + heightAdjust, Mathf.Max(edificeHeight, edificeHeight + fillPercent2) + heightAdjust); - shotHeight = heightRange.max - shotHeightOffset; + heightRange = new FloatRange( + Mathf.Min(edificeHeight, edificeHeight + fillPercent2) + heightAdjust, + Mathf.Max(0.1f, Mathf.Max(edificeHeight, edificeHeight + fillPercent2) + heightAdjust)); + shotHeight = Mathf.Max(0.1f, heightRange.max - shotHeightOffset); } /// diff --git a/Source/CombatExtended/CombatExtended/Defs/ModExtensionCover.cs b/Source/CombatExtended/CombatExtended/Defs/ModExtensionCover.cs new file mode 100644 index 0000000000..6c7bb0f3f7 --- /dev/null +++ b/Source/CombatExtended/CombatExtended/Defs/ModExtensionCover.cs @@ -0,0 +1,7 @@ +using Verse; + +namespace CombatExtended; +public class ModExtensionCover : DefModExtension +{ + public float heightOffset = 0f; +} diff --git a/Source/CombatExtended/Compatibility/Trenches.cs b/Source/CombatExtended/Compatibility/Trenches.cs new file mode 100755 index 0000000000..e34af4c629 --- /dev/null +++ b/Source/CombatExtended/Compatibility/Trenches.cs @@ -0,0 +1,59 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Reflection; +using CombatExtended.CombatExtended.LoggerUtils; +using UnityEngine; +using Verse; +using VFESecurity; + +namespace CombatExtended.Compatibility; +[StaticConstructorOnStartup] +public class CETrenches +{ + private static bool CheckTrench(IntVec3 cell, Map map, out float heightAdjust) + { + heightAdjust = 0f; + + //consider swimming pawns to be partially underground + TerrainDef terrain = cell.GetTerrain(map); + if (terrain != null && terrain.IsWater) + { + heightAdjust = terrain.passability == Traversability.Impassable ? -0.5f : -0.25f; + return true; + } + + //find trench building + List thingList = cell.GetThingList(map); + foreach (Thing thing in thingList) + { + ModExtensionCover modExtProperties = thing.def.GetModExtension(); + if (modExtProperties == null || modExtProperties.heightOffset == 0f) + { + continue; + } + + heightAdjust = modExtProperties.heightOffset; + return true; + } + + return false; + } + + public static float GetHeightAdjust(IntVec3 cell, Map map) + { + if (cell == null || map == null) + { + return 0; + } + + float heightAdjust = 0; + + if (CheckTrench(cell, map, out heightAdjust)) + { + return heightAdjust; + } + + return 0f; + } +}