-
Notifications
You must be signed in to change notification settings - Fork 259
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2515 from AluminumAlman/SRTSComp
SRTS Expanded compatibility
- Loading branch information
Showing
10 changed files
with
465 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
using HarmonyLib; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Reflection; | ||
using Verse; | ||
using Verse.Sound; | ||
using System.Reflection.Emit; | ||
using System; | ||
using UnityEngine; | ||
using RimWorld; | ||
|
||
namespace CombatExtended.Compatibility | ||
{ | ||
public class SRTS : IPatch | ||
{ | ||
public bool CanInstall() | ||
{ | ||
Log.Message("Combat Extended :: Checking SRTS Expanded"); | ||
if (!ModLister.HasActiveModWithName("SRTS Expanded")) | ||
{ | ||
return false; | ||
} | ||
return true; | ||
} | ||
public IEnumerable<string> GetCompatList() | ||
{ | ||
yield return "SRTSCompat"; | ||
} | ||
|
||
public void Install() | ||
{ | ||
Log.Message("Combat Extended :: Installing SRTS Expanded"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<PropertyGroup> | ||
<TargetFramework>net48</TargetFramework> | ||
<AssemblyName>SRTSCompat</AssemblyName> | ||
<AssemblyVersion>1.1.2.0</AssemblyVersion> | ||
<FileVersion>1.1.2.0</FileVersion> | ||
<Version>1.1.2</Version> | ||
<Company /> | ||
<Authors /> | ||
<Copyright>CC BY-NC-SA 4.0 2022</Copyright> | ||
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath> | ||
</PropertyGroup> | ||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'"> | ||
<OutputPath>..\..\AssembliesCompat\</OutputPath> | ||
<DebugType>portable</DebugType> | ||
<DebugSymbols>true</DebugSymbols> | ||
<TreatWarningsAsErrors>true</TreatWarningsAsErrors> | ||
<LangVersion>8.0</LangVersion> | ||
<AllowUnsafeBlocks>true</AllowUnsafeBlocks> | ||
</PropertyGroup> | ||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'"> | ||
<OutputPath>..\..\AssembliesCompat\</OutputPath> | ||
<TreatWarningsAsErrors>true</TreatWarningsAsErrors> | ||
<DebugType>none</DebugType> | ||
<DebugSymbols>false</DebugSymbols> | ||
<LangVersion>8.0</LangVersion> | ||
<AllowUnsafeBlocks>true</AllowUnsafeBlocks> | ||
</PropertyGroup> | ||
<Target Name="PostBuild" AfterTargets="PostBuildEvent" Condition="Exists('C:\Program Files (x86)\Mono\bin\pdb2mdb.bat')"> | ||
<Exec Command="if $(ConfigurationName) == Debugger call "C:\Program Files (x86)\Mono\bin\pdb2mdb.bat" $(TargetDir)$(TargetName)$(TargetExt)" /> | ||
</Target> | ||
|
||
<ItemGroup> | ||
<Folder Include="SRTSCompat" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<ProjectReference Include="..\CombatExtended\CombatExtended.csproj"> | ||
<Private>False</Private> | ||
</ProjectReference> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<Reference Include="0CombatExtendedLoader"> | ||
<HintPath>..\..\Assemblies\0CombatExtendedLoader.dll</HintPath> | ||
<Private>False</Private> | ||
</Reference> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<Reference Include="SRTS"> | ||
<HintPath>..\packages\SRTS-reference.dll</HintPath> | ||
<Private>False</Private> | ||
</Reference> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<PackageReference Include="Krafs.Rimworld.Ref" Version="1.4.3704" GeneratePathProperty="true" /> | ||
<PackageReference Include="Lib.Harmony" Version="2.2.2" ExcludeAssets="runtime" /> | ||
</ItemGroup> | ||
</Project> |
140 changes: 140 additions & 0 deletions
140
Source/SRTSCompat/SRTSCompat/Harmony/Harmony_BomberSkyfaller.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
using System; | ||
using System.Diagnostics; | ||
using System.Linq; | ||
using System.Reflection; | ||
using Verse; | ||
using CombatExtended.Loader; | ||
using RimWorld; | ||
using System.Collections.Generic; | ||
using HarmonyLib; | ||
using SRTS; | ||
using SPExtended; | ||
using UnityEngine; | ||
|
||
namespace CombatExtended.Compatibility.SRTSCompat | ||
{ | ||
[HarmonyPatch(typeof(BomberSkyfaller), | ||
"DropBomb", | ||
new Type[] { })] | ||
[HarmonyPriority(Priority.First)] | ||
public static class Harmony_BomberSkyfaller_DropBomb | ||
{ | ||
private static MethodInfo GetCurrentTargetingRadiusInfo = AccessTools.DeclaredMethod( | ||
typeof(BomberSkyfaller), "GetCurrentTargetingRadius", new Type[] { }); | ||
|
||
private static float shotHeight = 5f; | ||
|
||
// A destructive prefix so as to force SRTS bombers to use our projectile system, | ||
// unless the bomb isn't an AmmoThing, if so, fall back to SRTS's bombing system. | ||
public static bool Prefix(BomberSkyfaller __instance) | ||
{ | ||
ActiveDropPod srts = (ActiveDropPod)__instance.innerContainer.First(); | ||
if (srts == null) | ||
{ | ||
goto FunctionEnd; | ||
} | ||
|
||
for (int i = 0; i < (__instance.bombType == BombingType.precise | ||
? __instance.precisionBombingNumBombs : 1); ++i) | ||
{ | ||
Thing bombStack = srts.Contents.innerContainer | ||
.FirstOrDefault(thing => SRTSMod.mod.settings.allowedBombs.Contains(thing.def.defName)); | ||
if (bombStack == null) | ||
{ | ||
goto FunctionEnd; | ||
} | ||
|
||
Thing bombThing = srts.Contents.innerContainer.Take(bombStack, 1); | ||
|
||
IntVec3 bombPos = __instance.bombCells[0]; | ||
if (__instance.bombType == BombingType.carpet) | ||
{ | ||
__instance.bombCells.RemoveAt(0); | ||
} | ||
|
||
object targetingRadiusNullable = GetCurrentTargetingRadiusInfo.Invoke(__instance, null); | ||
if (targetingRadiusNullable == null) | ||
{ | ||
Log.Error("Combat Extended :: SRTSCompat BomberSkyfaller.DropBomb() - " | ||
+ "could not get SRTS dropship's targeting radius"); | ||
throw new Exception(); | ||
} | ||
float targetingRadius = (float)(int)targetingRadiusNullable; | ||
|
||
if (bombThing is AmmoThing) | ||
{ | ||
// Combat Extended projectile system | ||
ThingDef bombProjectileThingDef = (bombThing.def as AmmoDef)?.AmmoSetDefs | ||
.Find(ammoSet => ammoSet.ammoTypes?.Any() ?? false)? | ||
.ammoTypes | ||
.FirstOrDefault(ammoLink => ammoLink.ammo == bombThing.def)? | ||
.projectile; | ||
if (bombProjectileThingDef == null) | ||
{ | ||
Log.Error("Combat Extended :: SRTSCompat Harmony_BomberSkyfaller_DropBomb Prefix - " | ||
+ $"AmmoDef {bombThing.def} doesn't have a projectile"); | ||
throw new Exception(); | ||
} | ||
|
||
ProjectilePropertiesCE bombPropsCE = bombProjectileThingDef.projectile as ProjectilePropertiesCE; | ||
if (bombPropsCE == null) | ||
{ | ||
Log.Error("Combat Extended :: SRTSCompat Harmony_BomberSkyfaller_DropBomb Prefix - " | ||
+ $"AmmoDef {bombThing.def} projectile doesn't have ProjectilePropsCE"); | ||
throw new Exception(); | ||
} | ||
|
||
ProjectileCE bombProjectileCE = ThingMaker.MakeThing(bombProjectileThingDef) as ProjectileCE; | ||
|
||
GenSpawn.Spawn(bombProjectileCE, __instance.DrawPosCell, __instance.Map); | ||
bombProjectileCE.canTargetSelf = false; | ||
bombProjectileCE.minCollisionDistance = 1; | ||
bombProjectileCE.intendedTarget = null; | ||
bombProjectileCE.AccuracyFactor = 1f; | ||
|
||
float freefallTime = Mathf.Sqrt(2 * shotHeight / bombPropsCE.Gravity); | ||
float maxShotSpeed = targetingRadius / freefallTime; | ||
|
||
bombProjectileCE.Launch(launcher: __instance, | ||
origin: __instance.DrawPosCell.ToIntVec2.ToVector2(), | ||
shotAngle: 0f, | ||
shotRotation: Rand.Range(-180f, 180f), | ||
shotHeight: shotHeight, | ||
shotSpeed: Rand.Range(0f, maxShotSpeed), | ||
equipment: __instance); | ||
// Adjust shot speed after because of | ||
// this.shotSpeed = Math.Max(shotSpeed, def.projectile.speed); | ||
bombProjectileCE.shotSpeed = Rand.Range(0f, maxShotSpeed); | ||
} | ||
else | ||
{ | ||
// Original SRTS bomb dropping system | ||
int timerTickExplode = 20 + Rand.Range(0, 5); | ||
|
||
FallingBomb fallingBombThing = new FallingBomb(bombThing, | ||
bombThing.TryGetComp<CompExplosive>(), | ||
__instance.Map, | ||
__instance.def.skyfaller.shadow); | ||
fallingBombThing.HitPoints = int.MaxValue; | ||
fallingBombThing.ticksRemaining = timerTickExplode; | ||
|
||
IntVec3 targetCell = GenRadial.RadialCellsAround(bombPos, targetingRadius, true) | ||
.Where(cell => cell.InBounds(__instance.Map)) | ||
.RandomElementByWeight(cell => 1f - Mathf.Min(cell.DistanceTo(__instance.Position) / targetingRadius, 1f) + 0.05f); | ||
|
||
fallingBombThing.angle = __instance.angle + (SPTrig.LeftRightOfLine(__instance.DrawPosCell, __instance.Position, targetCell) * -10); | ||
fallingBombThing.speed = (float)SPExtra.Distance(__instance.DrawPosCell, targetCell) / fallingBombThing.ticksRemaining; | ||
Thing t = GenSpawn.Spawn(fallingBombThing, targetCell, __instance.Map); | ||
GenExplosion.NotifyNearbyPawnsOfDangerousExplosive(t, bombThing.TryGetComp<CompExplosive>().Props.explosiveDamageType, null); | ||
} | ||
} | ||
|
||
FunctionEnd: | ||
if (__instance.bombType == BombingType.precise && __instance.bombCells.Any()) | ||
{ | ||
__instance.bombCells.Clear(); | ||
} | ||
return false; | ||
} | ||
} | ||
} |
Oops, something went wrong.