Skip to content

Commit

Permalink
Fixed an issue with vehicle framework
Browse files Browse the repository at this point in the history
  • Loading branch information
kbatbouta committed Oct 1, 2023
1 parent aaaee10 commit 4fba3f0
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 12 deletions.
Binary file modified 1.4/Assemblies/CombatAI.dll
Binary file not shown.
6 changes: 3 additions & 3 deletions Source/Rule56/CombatAI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@
<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">
<PackageReference Include="Microsoft.NETFramework.ReferenceAssemblies.net472" Version="1.0.3">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
Expand All @@ -120,7 +120,7 @@
<TextTransform>$(PkgMono_TextTransform)\tools\TextTransform.exe</TextTransform>
</PropertyGroup>
<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')" />
<!-- <Exec WorkingDirectory="$(ProjectDir)" Command="@(TTFiles->'$(TextTransform) &#45;&#45;out=T4/Outputs/%(FileName).generated.cs T4/Sources/%(FileName).tt')" />-->
</Target>
<!-- ................ -->
<!-- Publisize -->
Expand Down Expand Up @@ -163,7 +163,7 @@
</ItemGroup>
</Target>
<PropertyGroup>
<TransformOnBuild>false</TransformOnBuild>
<TransformOnBuild>true</TransformOnBuild>
</PropertyGroup>
<ProjectExtensions>
<MonoDevelop>
Expand Down
82 changes: 74 additions & 8 deletions Source/Rule56/Comps/ThingComp_CombatAI.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using CombatAI.Abilities;
using CombatAI.R;
using CombatAI.Squads;
using CombatAI.Utilities;
using RimWorld;
using UnityEngine;
using Verse;
Expand Down Expand Up @@ -337,7 +339,8 @@ public void OnScanStarted()
/// If OnScanStarted is not called before then this will result in an error.
/// Should only be called from the main thread.
/// </summary>
public void OnScanFinished()
/// <param name="progress">Used to debug where an exception was thrown</param>
public void OnScanFinished(ref int progress)
{
if (scanning == false)
{
Expand All @@ -351,6 +354,8 @@ public void OnScanFinished()
data.ReSetAllies(allAllies);
// update when this pawn last saw enemies
data.LastSawEnemies = data.NumEnemies > 0 ? GenTicks.TicksGame : -1;
// For debugging and logging.
progress = 1;
// skip for animals.
if (selPawn.mindState == null || selPawn.RaceProps.Animal || IsDeadOrDowned)
{
Expand All @@ -366,6 +371,8 @@ public void OnScanFinished()
{
return;
}
// For debugging and logging.
progress = 2;
#if DEBUG_REACTION
if (Finder.Settings.Debug && Finder.Settings.Debug_ValidateSight)
{
Expand All @@ -384,6 +391,8 @@ public void OnScanFinished()
_visibleEnemies.Add(pawn);
}
}
// For debugging and logging.
progress = 21;
if (_path.Count == 0 || _path.Last() != parent.Position)
{
_path.Add(parent.Position);
Expand All @@ -405,8 +414,12 @@ public void OnScanFinished()
_colors.RemoveAt(0);
}
}
// For debugging and logging.
progress = 22;
}
#endif
// For debugging and logging.
progress = 3;
List<Thing> targetedBy = data.BeingTargetedBy;
// update when last saw enemies
data.LastSawEnemies = data.NumEnemies > 0 ? GenTicks.TicksGame : data.LastSawEnemies;
Expand All @@ -415,6 +428,8 @@ public void OnScanFinished()
{
return;
}
// For debugging and logging.
progress = 4;
// check if the TPS is good enough.
// reduce cooldown if the pawn hasn't seen enemies for a few ticks
if (!Finder.Performance.TpsCriticallyLow)
Expand All @@ -431,6 +446,8 @@ public void OnScanFinished()
}
lastSawEnemies = GenTicks.TicksGame;
}
// For debugging and logging.
progress = 5;
// get body size and use it in cooldown math.
float bodySize = selPawn.RaceProps.baseBodySize;
// pawn reaction cooldown changes with their body size.
Expand All @@ -448,6 +465,8 @@ public void OnScanFinished()
{
selPawn.jobs.StopAll();
}
// For debugging and logging.
progress = 6;
// Skip if some vanilla duties are active.
PawnDuty duty = selPawn.mindState.duty;
if (duty.Is(DutyDefOf.Build) || duty.Is(DutyDefOf.SleepForever) || duty.Is(DutyDefOf.TravelOrLeave))
Expand All @@ -472,7 +491,6 @@ void UpdateNearestEnemy(Thing enemy)
nearestEnemy = enemy;
}
}

// used to update nearest melee pawn
void UpdateNearestEnemyMelee(Thing enemy)
{
Expand All @@ -486,7 +504,8 @@ void UpdateNearestEnemyMelee(Thing enemy)
}
}
}

// For debugging and logging.
progress = 7;
// check if the chance of survivability is high enough
// defensive actions
Verb verb = selPawn.CurrentEffectiveVerb;
Expand All @@ -498,7 +517,8 @@ void UpdateNearestEnemyMelee(Thing enemy)
float possibleDmgWarmup = 0f;
float possibleDmg = 0f;
AIEnvThings enemies = data.AllEnemies;

// For debugging and logging.
progress = 8;
rangedEnemiesTargetingSelf.Clear();
if (Finder.Settings.Retreat_Enabled && (bodySize < 2 || selPawn.RaceProps.Humanlike))
{
Expand All @@ -512,6 +532,8 @@ void UpdateNearestEnemyMelee(Thing enemy)
continue;
}
#endif
// For debugging and logging.
progress = 80;
if (GetEnemyAttackTargetId(enemy) == selPawn.thingIDNumber)
{
DamageReport damageReport = DamageUtility.GetDamageReport(enemy);
Expand All @@ -522,6 +544,7 @@ void UpdateNearestEnemyMelee(Thing enemy)
{
UpdateNearestEnemyMelee(enemy);
}
progress = 81;
float damage = damageReport.SimulatedDamage(armor);
if (!damageReport.primaryIsRanged)
{
Expand All @@ -535,16 +558,21 @@ void UpdateNearestEnemyMelee(Thing enemy)
possibleDmgWarmup += damageReport.primaryVerbProps.warmupTime;
rangedEnemiesTargetingSelf.Add(enemy);
}
progress = 82;
}
}
}
// For debugging and logging.
progress = 9;
if (rangedEnemiesTargetingSelf.Count > 0 && !selPawn.mindState.MeleeThreatStillThreat && !selPawn.IsApproachingMeleeTarget(8, false))
{
float retreatRoll = 15 + Rand.Range(0, 15 * rangedEnemiesTargetingSelf.Count) + data.NumAllies * 15;
if (Finder.Settings.Debug_LogJobs)
{
MoteMaker.ThrowText(selPawn.DrawPos, selPawn.Map, $"r:{Math.Round(retreatRoll)},d:{possibleDmg}", Color.white);
}
// For debugging and logging.
progress = 91;
// major retreat attempt if the pawn is doomed
if (possibleDmg * personality.retreat - retreatRoll > 0.001f && possibleDmg * personality.retreat >= 50)
{
Expand All @@ -558,6 +586,8 @@ void UpdateNearestEnemyMelee(Thing enemy)
request.checkBlockChance = true;
if (CoverPositionFinder.TryFindRetreatPosition(request, out IntVec3 cell))
{
// For debugging and logging.
progress = 911;
if (ShouldMoveTo(cell))
{
if (cell != selPos)
Expand All @@ -584,6 +614,8 @@ void UpdateNearestEnemyMelee(Thing enemy)
}
}
}
// For debugging and logging.
progress = 92;
// try minor retreat (duck for cover fast)
if (possibleDmg * personality.duck - retreatRoll * 0.5f > 0.001f && possibleDmg * personality.duck >= 30)
{
Expand Down Expand Up @@ -627,16 +659,22 @@ void UpdateNearestEnemyMelee(Thing enemy)
}
}
}
// For debugging and logging.
progress = 100;
if (duty.Is(DutyDefOf.ExitMapRandom))
{
return;
}
// For debugging and logging.
progress = 200;
// offensive actions
if (verb != null)
{
// if the pawn is retreating and the pawn is still in danger or recently took damage, skip any offensive reaction.
if (verb.IsMeleeAttack)
{
// For debugging and logging.
progress = 201;
if ((selPawn.CurJob.Is(CombatAI_JobDefOf.CombatAI_Goto_Retreat) || selPawn.CurJob.Is(CombatAI_JobDefOf.CombatAI_Goto_Cover)) && (rangedEnemiesTargetingSelf.Count == 0 || possibleDmg < 2.5f))
{
_last = 30;
Expand All @@ -647,6 +685,8 @@ void UpdateNearestEnemyMelee(Thing enemy)
bool bestEnemyIsMeleeAttackingAlly = false;
// TODO create melee reactions.
IEnumerator<AIEnvAgentInfo> enumeratorEnemies = data.EnemiesWhere(AIEnvAgentState.nearby);
// For debugging and logging.
progress = 202;
while (enumeratorEnemies.MoveNext())
{
AIEnvAgentInfo info = enumeratorEnemies.Current;
Expand All @@ -657,6 +697,8 @@ void UpdateNearestEnemyMelee(Thing enemy)
continue;
}
#endif
// For debugging and logging.
progress = 203;
if (info.thing.Spawned && selPawn.CanReach(info.thing, PathEndMode.Touch, Danger.Deadly))
{
Verb enemyVerb = info.thing.TryGetAttackVerb();
Expand Down Expand Up @@ -688,7 +730,11 @@ void UpdateNearestEnemyMelee(Thing enemy)
}
}
}
// For debugging and logging.
progress = 204;
}
// For debugging and logging.
progress = 205;
if (nearestEnemy == null)
{
nearestEnemy = selPawn.mindState.enemyTarget;
Expand All @@ -697,6 +743,8 @@ void UpdateNearestEnemyMelee(Thing enemy)
{
return;
}
// For debugging and logging.
progress = 206;
_bestEnemy = nearestEnemy;
if (!selPawn.mindState.MeleeThreatStillThreat || selPawn.stances?.stagger?.Staggered == false)
{
Expand All @@ -712,10 +760,14 @@ void UpdateNearestEnemyMelee(Thing enemy)
// no enemy can be approached solo
// TODO
}
// For debugging and logging.
progress = 207;
}
// ranged
else
{
// For debugging and logging.
progress = 208;
if (selPawn.CurJob.Is(CombatAI_JobDefOf.CombatAI_Goto_Cover) && GenTicks.TicksGame - selPawn.CurJob.startTick < 60)
{
return;
Expand All @@ -725,6 +777,8 @@ void UpdateNearestEnemyMelee(Thing enemy)
{
return;
}
// For debugging and logging.
progress = 209;
// check if the verb is available.
if (!verb.Available() || Mod_CE.active && Mod_CE.IsAimingCE(verb))
{
Expand All @@ -733,11 +787,14 @@ void UpdateNearestEnemyMelee(Thing enemy)
bool bestEnemyVisibleNow = false;
bool bestEnemyVisibleSoon = false;
ulong selFlags = selPawn.GetThingFlags();
// For debugging and logging.
progress = 210;
// A not fast check will check for retreat and for reactions to enemies that are visible or soon to be visible.
// A fast check will check only for retreat.
IEnumerator<AIEnvAgentInfo> enumerator = data.Enemies();
while (enumerator.MoveNext())
{
progress = 301;
AIEnvAgentInfo info = enumerator.Current;
#if DEBUG_REACTION
if (info.thing == null)
Expand All @@ -746,11 +803,15 @@ void UpdateNearestEnemyMelee(Thing enemy)
continue;
}
#endif
// For debugging and logging.
progress = 302;
if (info.thing.Spawned)
{
Pawn enemyPawn = info.thing as Pawn;
if ((sightReader.GetDynamicFriendlyFlags(info.thing.Position) & selFlags) != 0 && verb.CanHitTarget(info.thing))
{
// For debugging and logging.
progress = 311;
if (!bestEnemyVisibleNow)
{
nearestEnemy = null;
Expand All @@ -761,6 +822,8 @@ void UpdateNearestEnemyMelee(Thing enemy)
}
else if (enemyPawn != null && !bestEnemyVisibleNow)
{
// For debugging and logging.
progress = 312;
IntVec3 temp = PawnPathUtility.GetMovingShiftedPosition(enemyPawn, 120);
if ((sightReader.GetDynamicFriendlyFlags(temp) & selFlags) != 0 && verb.CanHitTarget(temp))
{
Expand All @@ -777,13 +840,15 @@ void UpdateNearestEnemyMelee(Thing enemy)
UpdateNearestEnemy(info.thing);
}
}
if (enemyPawn != null && enemyPawn.CurrentEffectiveVerb.IsMeleeAttack)
// For debugging and logging.
progress = 303;
if (enemyPawn != null && (enemyPawn.CurrentEffectiveVerb?.IsMeleeAttack ?? false))
{
UpdateNearestEnemyMelee(enemyPawn);
}
}
}

progress = 400;
void StartOrQueueCoverJob(IntVec3 cell, int codeOffset)
{
Job curJob = selPawn.CurJob;
Expand Down Expand Up @@ -853,6 +918,7 @@ void StartOrQueueCoverJob(IntVec3 cell, int codeOffset)
{
rangedEnemiesTargetingSelf.Remove(nearestEnemy);
}
progress = 500;
bool retreatMeleeThreat = nearestMeleeEnemy != null && verb.EffectiveRange * personality.retreat > 16 && nearestMeleeEnemyDist < Maths.Max(verb.EffectiveRange * personality.retreat / 3f, 9) && 0.25f * data.NumAllies < data.NumEnemies;
bool retreatThreat = !retreatMeleeThreat && nearestEnemy != null && nearestEnemyDist < Maths.Max(verb.EffectiveRange * personality.retreat / 4f, 5);
_bestEnemy = retreatMeleeThreat ? nearestMeleeEnemy : nearestEnemy;
Expand Down Expand Up @@ -1422,7 +1488,7 @@ public void ReleaseEscorts(bool success)
}
return false;
};
GenClosest.RegionwiseBFSWorker(selPawn.Position, selPawn.Map, ThingRequest.ForGroup(ThingRequestGroup.Pawn), PathEndMode.InteractionCell, TraverseParms.For(selPawn), validator, null, 1, 4, 15, out int _);
Verse.GenClosest.RegionwiseBFSWorker(selPawn.Position, selPawn.Map, ThingRequest.ForGroup(ThingRequestGroup.Pawn), PathEndMode.InteractionCell, TraverseParms.For(selPawn), validator, null, 1, 4, 15, out int _);
}
escorts.Clear();
}
Expand Down Expand Up @@ -1614,7 +1680,7 @@ private void TryStartSapperJob()
}
return false;
};
GenClosest.RegionwiseBFSWorker(selPawn.Position, map, ThingRequest.ForGroup(ThingRequestGroup.Pawn), PathEndMode.InteractionCell, TraverseParms.For(selPawn), validator, null, 1, 10, 40, out int _);
Verse.GenClosest.RegionwiseBFSWorker(selPawn.Position, map, ThingRequest.ForGroup(ThingRequestGroup.Pawn), PathEndMode.InteractionCell, TraverseParms.For(selPawn), validator, null, 1, 10, 40, out int _);
}
if (!Mod_CE.active && (escorts.Count >= 2 || miningSkill == 0))
{
Expand Down
4 changes: 3 additions & 1 deletion Source/Rule56/SightGrid.cs
Original file line number Diff line number Diff line change
Expand Up @@ -633,14 +633,16 @@ private bool TryCastSight(IBucketableThing item)
}
}
}
int p = 0;
//
// notify the pawn so they can start processing targets.
try
{
item.ai.OnScanFinished();
item.ai.OnScanFinished(ref p);
}
catch (Exception er)
{
Log.Error($"progress:{p} {item.thing?.thingIDNumber} {item.thing?.ToString()} encountered an error while doing OnScanFinished");
er.ShowExceptionGui();
}
item.spottings.Clear();
Expand Down

0 comments on commit 4fba3f0

Please sign in to comment.