Skip to content

Commit

Permalink
Merge pull request #28 from kbatbouta/development-tactics
Browse files Browse the repository at this point in the history
Now raiders know how pawns are available to target a cell
  • Loading branch information
kbatbouta authored Feb 3, 2023
2 parents 3410ef6 + 2b7f238 commit d8201ee
Show file tree
Hide file tree
Showing 19 changed files with 514 additions and 221 deletions.
Binary file modified 1.4/Assemblies/CombatAI.dll
Binary file not shown.
90 changes: 45 additions & 45 deletions Source/Rule56/CombatAI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -32,35 +32,35 @@
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
</PropertyGroup>
<ItemGroup>
<Folder Include="T4\"/>
<Folder Include="T4\Outputs\"/>
<Folder Include="Patches\"/>
<Folder Include="Collections\"/>
<Folder Include="Comps\"/>
<Folder Include="Statistics\"/>
<Folder Include="Deprecated\"/>
<Folder Include="Deprecated\Utilities\"/>
<Folder Include="Defs\"/>
<Folder Include="DefOfs\"/>
<Folder Include="Defs\Extensions\"/>
<Folder Include="Cache\"/>
<Folder Include="Comps\Deprecated\"/>
<Folder Include="T4\" />
<Folder Include="T4\Outputs\" />
<Folder Include="Patches\" />
<Folder Include="Collections\" />
<Folder Include="Comps\" />
<Folder Include="Statistics\" />
<Folder Include="Deprecated\" />
<Folder Include="Deprecated\Utilities\" />
<Folder Include="Defs\" />
<Folder Include="DefOfs\" />
<Folder Include="Defs\Extensions\" />
<Folder Include="Cache\" />
<Folder Include="Comps\Deprecated\" />
</ItemGroup>
<ItemGroup>
<None Remove="Gui\Core\GUIUtility.ScrollViews"/>
<None Remove="Patches\"/>
<None Remove="Collections\"/>
<None Remove="Comps\"/>
<None Remove="Statistics\"/>
<None Remove="Statistics\DataWriter_Path.config"/>
<None Remove="Statistics\DataWriter_Path.cs"/>
<None Remove="Deprecated\"/>
<None Remove="Deprecated\Utilities\"/>
<None Remove="Defs\"/>
<None Remove="DefOfs\"/>
<None Remove="Defs\Extensions\"/>
<None Remove="Cache\"/>
<None Remove="Comps\Deprecated\"/>
<None Remove="Gui\Core\GUIUtility.ScrollViews" />
<None Remove="Patches\" />
<None Remove="Collections\" />
<None Remove="Comps\" />
<None Remove="Statistics\" />
<None Remove="Statistics\DataWriter_Path.config" />
<None Remove="Statistics\DataWriter_Path.cs" />
<None Remove="Deprecated\" />
<None Remove="Deprecated\Utilities\" />
<None Remove="Defs\" />
<None Remove="DefOfs\" />
<None Remove="Defs\Extensions\" />
<None Remove="Cache\" />
<None Remove="Comps\Deprecated\" />
</ItemGroup>
<ItemGroup>
<None Include="..\..\1.4\**\*.xml" CopyToOutputDirectory="Never">
Expand Down Expand Up @@ -94,11 +94,11 @@
<!-- ................ -->
<!-- Packages -->
<ItemGroup>
<PackageReference Include="Krafs.Rimworld.Ref" Version="1.4.3580-beta" GeneratePathProperty="true"/>
<PackageReference Include="TaskPubliciser" Version="1.0.3"/>
<PackageReference Include="Mono.TextTransform" Version="1.0.0" GeneratePathProperty="true" ExcludeAssets="runtime"/>
<PackageReference Include="Lib.Harmony" Version="2.2.2" ExcludeAssets="runtime"/>
<PackageReference Include="Microsoft.NETCore.Platforms" Version="5.0.3"/>
<PackageReference Include="Krafs.Rimworld.Ref" Version="1.4.3580-beta" GeneratePathProperty="true" />
<PackageReference Include="TaskPubliciser" Version="1.0.3" />
<PackageReference Include="Mono.TextTransform" Version="1.0.0" GeneratePathProperty="true" ExcludeAssets="runtime" />
<PackageReference Include="Lib.Harmony" Version="2.2.2" ExcludeAssets="runtime" />
<PackageReference Include="Microsoft.NETCore.Platforms" Version="5.0.3" />
<PackageReference Include="Microsoft.NETFramework.ReferenceAssemblies.net472" Version="1.0.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
Expand All @@ -107,17 +107,17 @@
<!-- ................ -->
<!-- Process T4 files -->
<ItemGroup>
<Compile Remove="ShadowCastingUtility.old.cs"/>
<Compile Remove="ShadowCastingUtility.old.cs" />
</ItemGroup>
<Target Name="ProcessTT" BeforeTargets="BeforeBuild">
<ItemGroup>
<TTFiles Include="T4\Sources\*.tt" CopyToOutputDirectory="Never"/>
<TTFiles Include="T4\Sources\*.tt" CopyToOutputDirectory="Never" />
</ItemGroup>
<PropertyGroup>
<TextTransform>$(PkgMono_TextTransform)\tools\TextTransform.exe</TextTransform>
</PropertyGroup>
<Message Text="Processing T4 files: @(TTFiles-&gt;'%(Identity)')"/>
<Exec WorkingDirectory="$(ProjectDir)" Command="@(TTFiles-&gt;'pwd; mono $(TextTransform) --out=T4/Outputs/%(FileName).generated.cs T4/Sources/%(FileName).tt')"/>
<Message Text="Processing T4 files: @(TTFiles->'%(Identity)')" />
<Exec WorkingDirectory="$(ProjectDir)" Command="@(TTFiles->'pwd; mono $(TextTransform) --out=T4/Outputs/%(FileName).generated.cs T4/Sources/%(FileName).tt')" />
</Target>
<!-- ................ -->
<!-- Publisize -->
Expand All @@ -131,12 +131,12 @@
<Unity_CoreModule_Publicised>$(PubliciseOutputPath)UnityEngine.CoreModule_publicised.dll</Unity_CoreModule_Publicised>
</PropertyGroup>
<!-- Publicise the dlls (if required) -->
<Message Importance="High" Text="2. Publicising Rimworld Assembly ..."/>
<Message Importance="High" Text="2.1 AssemblyCSharp Path ($(AssemblyCSharp))"/>
<Publicise TargetAssemblyPath="$(AssemblyCSharp)" OutputPath="$(PubliciseOutputPath)" Condition="Exists('$(AssemblyCSharp)')"/>
<Publicise TargetAssemblyPath="$(Unity_CoreModule)" OutputPath="$(PubliciseOutputPath)" Condition="Exists('$(Unity_CoreModule)')"/>
<Message Importance="High" Text="2. Publicising Rimworld Assembly ..." />
<Message Importance="High" Text="2.1 AssemblyCSharp Path ($(AssemblyCSharp))" />
<Publicise TargetAssemblyPath="$(AssemblyCSharp)" OutputPath="$(PubliciseOutputPath)" Condition="Exists('$(AssemblyCSharp)')" />
<Publicise TargetAssemblyPath="$(Unity_CoreModule)" OutputPath="$(PubliciseOutputPath)" Condition="Exists('$(Unity_CoreModule)')" />
<!-- Add references to the new publicised dlls -->
<Message Importance="High" Text="Replacing reference to un-publicised assemblies with publicised equivalents ..."/>
<Message Importance="High" Text="Replacing reference to un-publicised assemblies with publicised equivalents ..." />
<ItemGroup>
<Reference Include="$(AssemblyCSharp_Publicised)">
<SpecificVersion>false</SpecificVersion>
Expand All @@ -153,10 +153,10 @@
</ItemGroup>
</Target>
<Target Name="UpdateReferences" AfterTargets="ResolveLockFileReferences">
<Message Importance="High" Text="3. Remove References at ($(PkgKrafs_Rimworld_Ref))"/>
<Message Importance="High" Text="3. Remove References at ($(PkgKrafs_Rimworld_Ref))" />
<ItemGroup>
<Reference Remove="$(PkgKrafs_Rimworld_Ref)\ref\net472\Assembly-CSharp.dll"/>
<Reference Remove="$(PkgKrafs_Rimworld_Ref)\ref\net472\UnityEngine.CoreModule.dll"/>
<Reference Remove="$(PkgKrafs_Rimworld_Ref)\ref\net472\Assembly-CSharp.dll" />
<Reference Remove="$(PkgKrafs_Rimworld_Ref)\ref\net472\UnityEngine.CoreModule.dll" />
</ItemGroup>
</Target>
<PropertyGroup>
Expand All @@ -166,7 +166,7 @@
<MonoDevelop>
<Properties>
<Policies>
<StandardHeader Text="&#xA;${FileName}&#xA; &#xA;Author:&#xA; ${AuthorName} &lt;${AuthorEmail}&gt;&#xA;&#xA;Copyright (c) ${Year} ${CopyrightHolder}&#xA;&#xA;Permission is hereby granted, free of charge, to any person obtaining a copy&#xA;of this software and associated documentation files (the &quot;Software&quot;), to deal&#xA;in the Software without restriction, including without limitation the rights&#xA;to use, copy, modify, merge, publish, distribute, sublicense, and/or sell&#xA;copies of the Software, and to permit persons to whom the Software is&#xA;furnished to do so, subject to the following conditions:&#xA;&#xA;The above copyright notice and this permission notice shall be included in&#xA;all copies or substantial portions of the Software.&#xA;&#xA;THE SOFTWARE IS PROVIDED &quot;AS IS&quot;, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR&#xA;IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,&#xA;FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE&#xA;AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER&#xA;LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,&#xA;OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN&#xA;THE SOFTWARE." IncludeInNewFiles="False"/>
<StandardHeader Text="&#xA;${FileName}&#xA; &#xA;Author:&#xA; ${AuthorName} &lt;${AuthorEmail}&gt;&#xA;&#xA;Copyright (c) ${Year} ${CopyrightHolder}&#xA;&#xA;Permission is hereby granted, free of charge, to any person obtaining a copy&#xA;of this software and associated documentation files (the &quot;Software&quot;), to deal&#xA;in the Software without restriction, including without limitation the rights&#xA;to use, copy, modify, merge, publish, distribute, sublicense, and/or sell&#xA;copies of the Software, and to permit persons to whom the Software is&#xA;furnished to do so, subject to the following conditions:&#xA;&#xA;The above copyright notice and this permission notice shall be included in&#xA;all copies or substantial portions of the Software.&#xA;&#xA;THE SOFTWARE IS PROVIDED &quot;AS IS&quot;, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR&#xA;IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,&#xA;FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE&#xA;AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER&#xA;LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,&#xA;OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN&#xA;THE SOFTWARE." IncludeInNewFiles="False" />
</Policies>
</Properties>
</MonoDevelop>
Expand Down
10 changes: 5 additions & 5 deletions Source/Rule56/CombatAIMod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ public class CombatAIMod : Mod
private bool collapsibleGroupInited;
public CombatAIMod(ModContentPack contentPack) : base(contentPack)
{
Finder.Mod = this;
Finder.Harmony = new Harmony("Krkr.Rule56");
Finder.Harmony.PatchAll();
Finder.Mod = this;
Finder.Settings = GetSettings<Settings>();
if (Finder.Settings == null)
{
Expand Down Expand Up @@ -100,7 +100,7 @@ private void FillCollapsible_Basic(Listing_Collapsible collapsible)

Finder.Settings.Enable_Sprinting = false;
Finder.Settings.Enable_Groups = false;
Finder.Settings.Pathfinding_SappingMul = 0.5f;
Finder.Settings.Pathfinding_SappingMul = 1.5f;

Finder.Settings.SightSettings_FriendliesAndRaiders.interval = 3;
if (Current.ProgramState != ProgramState.Playing)
Expand Down Expand Up @@ -135,7 +135,7 @@ private void FillCollapsible_Basic(Listing_Collapsible collapsible)
Finder.Settings.Flank_Enabled = true;
Finder.Settings.Enable_Sprinting = false;
Finder.Settings.Enable_Groups = true;
Finder.Settings.Pathfinding_SappingMul = 1.0f;
Finder.Settings.Pathfinding_SappingMul = 1.3f;
Finder.Settings.SightSettings_FriendliesAndRaiders.interval = 3;
if (Current.ProgramState != ProgramState.Playing)
{
Expand Down Expand Up @@ -170,7 +170,7 @@ private void FillCollapsible_Basic(Listing_Collapsible collapsible)

Finder.Settings.Enable_Sprinting = false;
Finder.Settings.Enable_Groups = true;
Finder.Settings.Pathfinding_SappingMul = 0.9f;
Finder.Settings.Pathfinding_SappingMul = 1.0f;

Finder.Settings.SightSettings_FriendliesAndRaiders.interval = 2;
if (Current.ProgramState != ProgramState.Playing)
Expand Down Expand Up @@ -207,7 +207,7 @@ private void FillCollapsible_Basic(Listing_Collapsible collapsible)

Finder.Settings.Enable_Sprinting = true;
Finder.Settings.Enable_Groups = true;
Finder.Settings.Pathfinding_SappingMul = 0.85f;
Finder.Settings.Pathfinding_SappingMul = 1.0f;

Finder.Settings.SightSettings_FriendliesAndRaiders.interval = 1;
if (Current.ProgramState != ProgramState.Playing)
Expand Down
3 changes: 2 additions & 1 deletion Source/Rule56/CombatAI_Utility.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using RimWorld;
using System.Collections.Generic;
using RimWorld;
using UnityEngine;
using Verse;
using Verse.AI;
Expand Down
32 changes: 24 additions & 8 deletions Source/Rule56/Comps/ThingComp_CombatAI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,11 @@ public void OnScanFinished()
/// <param name="dInfo">Damage info</param>
public void Notify_TookDamage(DamageInfo dInfo)
{
// notify the custom duty manager that this pawn took damage.
if (duties != null)
{
duties.Notify_TookDamage();
}
// if the pawn is tanky enough skip.
if (Finder.Settings.Retreat_Enabled && parent.Spawned && GenTicks.TicksGame - lastScanned < 90 && parent is Pawn pawn && !pawn.Dead && !pawn.Downed && armor.TankInt < 0.4f)
{
Expand Down Expand Up @@ -582,6 +587,12 @@ public void StartSapper(List<IntVec3> blocked, IntVec3 cellBefore, bool findEsco
sapperNodes.AddRange(blocked);
_sap = 0;
TryStartSapperJob();
// if ((pawn.needs?.food?.CurCategory != HungerCategory.Fed) && pawn.Position.DistanceToSquared(cellBefore) < 13f)
// {
// List<Pawn> pawns = escorts.ToList();
// pawns.Add(pawn);
// SuppliesUtility.FulfillFoodSupplies(pawns, pawn.Map);
// }
}

/// <summary>
Expand Down Expand Up @@ -694,10 +705,10 @@ private void TryStartSapperJob()
job.maxNumMeleeAttacks = 300;
pawn.jobs.StopAll();
pawn.jobs.StartJob(job, JobCondition.InterruptForced);
if (findEscorts)
if (findEscorts && Rand.Chance(1 - Maths.Max(1f / (escorts.Count + 1f), 0.85f)))
{
int count = escorts.Count;
int countTarget = Rand.Int % 6 + 4 + Maths.Min(sapperNodes.Count, 10);
int countTarget = Rand.Int % 4 + 3 + Maths.Min(sapperNodes.Count, 10);
Faction faction = pawn.Faction;
Predicate<Thing> validator = t =>
{
Expand All @@ -708,16 +719,21 @@ private void TryStartSapperJob()
&& ally.skills?.GetSkill(SkillDefOf.Mining).Level < 10)
{
ThingComp_CombatAI comp = ally.GetComp_Fast<ThingComp_CombatAI>();
if (comp?.duties != null && comp.duties?.Any(DutyDefOf.Escort) == false && !comp.IsSapping && GenTicks.TicksGame - comp.releasedTick < 600)
if (comp?.duties != null && comp.duties?.Any(DutyDefOf.Escort) == false && !comp.IsSapping && GenTicks.TicksGame - comp.releasedTick > 600)
{
Pawn_CustomDutyTracker.CustomPawnDuty custom = CustomDutyUtility.Escort(ally, pawn, 20, 100, 500 * sapperNodes.Count + Rand.Int % 1000);
if (custom != null)
Pawn_CustomDutyTracker.CustomPawnDuty custom = CustomDutyUtility.Escort(pawn, 20, 100, (500 * sapperNodes.Count) / (escorts.Count + 1) + Rand.Int % 500);
if (ally.TryStartCustomDuty(custom))
{
custom.duty.locomotion = Finder.Settings.Enable_Sprinting ? LocomotionUrgency.Sprint : LocomotionUrgency.Jog;
comp.duties.StartDuty(custom);
escorts.Add(ally);
}
count++;
if (comp.duties.curCustomDuty?.duty != duties.curCustomDuty?.duty)
{
count += 3;
}
else
{
count++;
}
}
return count == countTarget;
}
Expand Down
102 changes: 102 additions & 0 deletions Source/Rule56/CustomDuties/CustomDutyUtility.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
using System.Runtime.CompilerServices;
using CombatAI.Comps;
using RimWorld;
using Verse;
using Verse.AI;
namespace CombatAI
{
public static class CustomDutyUtility
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Pawn_CustomDutyTracker GetPawnCustomDutyTracker(this Pawn pawn)
{
return pawn.GetComp_Fast<ThingComp_CombatAI>()?.duties ?? null;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool AnyCustomDuties(this Pawn pawn, DutyDef def)
{
return pawn.GetPawnCustomDutyTracker()?.Any(def) ?? false;
}

public static bool TryStartCustomDuty(this Pawn pawn, Pawn_CustomDutyTracker.CustomPawnDuty duty, bool returnCurDutyToQueue = true)
{
ThingComp_CombatAI comp = pawn.GetComp_Fast<ThingComp_CombatAI>();
if (comp == null)
{
return false;
}
comp.duties.StartDuty(duty, returnCurDutyToQueue: returnCurDutyToQueue);
return true;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void EnqueueFirstCustomDuty(this Pawn pawn, Pawn_CustomDutyTracker.CustomPawnDuty duty)
{
ThingComp_CombatAI comp = pawn.GetComp_Fast<ThingComp_CombatAI>();
if (comp?.duties == null)
{
comp.duties.EnqueueFirst(duty);
}
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void EnqueueCustomDuty(this Pawn pawn, Pawn_CustomDutyTracker.CustomPawnDuty duty)
{
ThingComp_CombatAI comp = pawn.GetComp_Fast<ThingComp_CombatAI>();
if (comp?.duties == null)
{
comp.duties.Enqueue(duty);
}
}

public static Pawn_CustomDutyTracker.CustomPawnDuty Escort(Pawn escortee, int radius = -1, int failOnDist = 0, int expireAfter = 0, int startAfter = 0, bool failOnFocusDowned = true, DutyDef failOnFocusDutyNot = null)
{
Pawn_CustomDutyTracker.CustomPawnDuty custom = new Pawn_CustomDutyTracker.CustomPawnDuty
{
duty = new PawnDuty(DutyDefOf.Escort, escortee, radius)
{
locomotion = LocomotionUrgency.Sprint
},
endOnDistToFocusLarger = failOnDist,
expireAfter = expireAfter,
startAfter = startAfter,
endOnFocusDowned = failOnFocusDowned,
endOnFocusDutyNot = failOnFocusDutyNot,
endOnFocusDestroyed = true,
endOnFocusDeath = true
};
return custom;
}

public static Pawn_CustomDutyTracker.CustomPawnDuty AssaultPoint(IntVec3 dest, int switchAssaultRadius = 15, int expireAfter = 0, int startAfter = 0)
{
Pawn_CustomDutyTracker.CustomPawnDuty custom = new Pawn_CustomDutyTracker.CustomPawnDuty
{
duty = new PawnDuty(DutyDefOf.Defend, dest, switchAssaultRadius)
{
locomotion = LocomotionUrgency.Sprint
},
endOnDistToFocusLess = switchAssaultRadius,
expireAfter = expireAfter,
startAfter = startAfter,
};
return custom;
}

public static Pawn_CustomDutyTracker.CustomPawnDuty DefendPoint(IntVec3 dest, int radius, bool endOnTookDamage, int expireAfter, int startAfter = 0)
{
Pawn_CustomDutyTracker.CustomPawnDuty custom = new Pawn_CustomDutyTracker.CustomPawnDuty
{
duty = new PawnDuty(DutyDefOf.Defend, dest, radius)
{
locomotion = LocomotionUrgency.Sprint
},
expireAfter = expireAfter,
startAfter = startAfter,
endOnTookDamage = endOnTookDamage
};
return custom;
}
}
}
Loading

0 comments on commit d8201ee

Please sign in to comment.