Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
14 changes: 8 additions & 6 deletions Source/CombatExtended/CombatExtended/CollisionVertical.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
using Verse;

namespace CombatExtended;
public class ModExtensionCover : DefModExtension
{
public float heightOffset = 0f;
}
59 changes: 59 additions & 0 deletions Source/CombatExtended/Compatibility/Trenches.cs
Original file line number Diff line number Diff line change
@@ -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<Thing> thingList = cell.GetThingList(map);
foreach (Thing thing in thingList)
{
ModExtensionCover modExtProperties = thing.def.GetModExtension<ModExtensionCover>();
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;
}
}