Skip to content

Commit

Permalink
Merge pull request #2515 from AluminumAlman/SRTSComp
Browse files Browse the repository at this point in the history
SRTS Expanded compatibility
  • Loading branch information
N7Huntsman authored Jun 22, 2023
2 parents 655c7c1 + 16cb23e commit bea5032
Show file tree
Hide file tree
Showing 10 changed files with 465 additions and 4 deletions.
2 changes: 0 additions & 2 deletions Defs/ThingDefs_Items/Items_Resource_Ammo.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
<!--=============== Ammo resources ====================-->

<ThingDef ParentName="ResourceBase">
<thingClass>CombatExtended.AmmoThing</thingClass>
<defName>Prometheum</defName>
<label>prometheum</label>
<description>Military-grade incendiary agent, ignites on contact with oxygen. The raw resource is used to craft incendiary ammo of all kinds.</description>
Expand Down Expand Up @@ -46,7 +45,6 @@
</ThingDef>

<ThingDef ParentName="ResourceBase">
<thingClass>CombatExtended.AmmoThing</thingClass>
<defName>FSX</defName>
<label>FSX</label>
<description>High-explosive chemical extracted from Boomalope sacks and synthesized from chemfuel, it is used in a variety of industrial and military applications.</description>
Expand Down
9 changes: 8 additions & 1 deletion Source/CombatExtended.sln
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ Project("{E9B3A09D-DD32-4429-ABCC-64A2EA58B0B3}") = "ArtilleryCompat", "Artiller
EndProject
Project("{B55F2CBF-ABFF-4E01-813B-79DAAAC20203}") = "VehiclesCompat", "VehiclesCompat\VehiclesCompat.csproj", "{06833316-A463-4044-BAA7-993683F9810B}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SRTSCompat", "SRTSCompat\SRTSCompat.csproj", "{2ECDEC68-5878-41C7-B494-E189B8C5C33E}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -58,7 +60,12 @@ Global
{06833316-A463-4044-BAA7-993683F9810B}.Debugger|Any CPU.Build.0 = Debug|Any CPU
{06833316-A463-4044-BAA7-993683F9810B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{06833316-A463-4044-BAA7-993683F9810B}.Release|Any CPU.Build.0 = Release|Any CPU

{2ECDEC68-5878-41C7-B494-E189B8C5C33E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2ECDEC68-5878-41C7-B494-E189B8C5C33E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2ECDEC68-5878-41C7-B494-E189B8C5C33E}.Debugger|Any CPU.ActiveCfg = Debug|Any CPU
{2ECDEC68-5878-41C7-B494-E189B8C5C33E}.Debugger|Any CPU.Build.0 = Debug|Any CPU
{2ECDEC68-5878-41C7-B494-E189B8C5C33E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2ECDEC68-5878-41C7-B494-E189B8C5C33E}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
35 changes: 35 additions & 0 deletions Source/CombatExtended/Compatibility/SRTS.cs
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");
}
}
}
58 changes: 58 additions & 0 deletions Source/SRTSCompat/SRTSCompat.csproj
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 &quot;C:\Program Files (x86)\Mono\bin\pdb2mdb.bat&quot; $(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 Source/SRTSCompat/SRTSCompat/Harmony/Harmony_BomberSkyfaller.cs
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;
}
}
}
Loading

0 comments on commit bea5032

Please sign in to comment.