Skip to content
This repository was archived by the owner on Apr 14, 2020. It is now read-only.

Commit 5aeba13

Browse files
committed
Destroy the GenClosestAmmo class
- Mechanoids now reload turrets with the JobGiver_DefenderReloadTurret thinknode, patched into the Defend DutyDef. - Turrets now only order reloads directly for manned pawns (i.e. mortars)
1 parent 6ee7f00 commit 5aeba13

File tree

12 files changed

+164
-459
lines changed

12 files changed

+164
-459
lines changed

Assemblies/CombatExtended.dll

-3.5 KB
Binary file not shown.
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<LanguageData>
3-
<CE_NoAmmoAvailable>No viable ammo found nearby.</CE_NoAmmoAvailable>
3+
<CE_NoAmmoAvailable>no viable ammo found nearby</CE_NoAmmoAvailable>
4+
<CE_TurretFull>turret magazine is fully loaded</CE_TurretFull>
5+
<CE_TurretNonAllied>turret belongs to a hostile faction</CE_TurretNonAllied>
6+
<CE_TurretAlreadyReloading>already reloading turret</CE_TurretAlreadyReloading>
7+
<CE_TurretIsBurning>turret is on fire</CE_TurretIsBurning>
48
</LanguageData>

Patches/Core/ThinkTreeDefs/Humanlike.xml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,15 @@
4242
</value>
4343
</Operation>
4444

45+
<!-- =========== Add turret reloading to mechanoids ===========-->
46+
<Operation Class="PatchOperationAdd">
47+
<xpath>Defs/DutyDef[defName="Defend"]/thinkNode/subNodes</xpath>
48+
<value>
49+
<li Class="CombatExtended.JobGiver_DefenderReloadTurret"/>
50+
</value>
51+
<order>Prepend</order>
52+
</Operation>
53+
4554
<!--<Operation Class="PatchOperationAttributeSet">
4655
<xpath>Defs/ThinkTreeDef[defName="HumanlikeConstant"]/thinkRoot/subNodes/li[@Class="ThinkNode_ConditionalCanDoConstantThinkTreeJobNow"]/subNodes/li[@Class="JobGiver_ConfigurableHostilityResponse"]</xpath>
4756
<attribute>Class</attribute>

Royalty/Defs/ThingDefs_Buildings/Buildings_Mech_Spawners.xml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@
1414
<MaxHitPoints>100</MaxHitPoints>
1515
<Flammability>0</Flammability>
1616
</statBases>
17-
<building>
17+
<building>
1818
<buildingTags>
1919
<li>MechClusterMember</li>
2020
</buildingTags>
2121
</building>
2222
<graphicData>
23-
<texPath>Things/Building/Mech/MechDropBeacon</texPath> <!-- Perhaps add a custom texture? -->
23+
<texPath>Things/Building/Mech/MechDropBeacon</texPath> <!-- Perhaps add a custom texture? -->
2424
<graphicClass>Graphic_Single</graphicClass>
2525
<drawSize>(1,1)</drawSize>
2626
<shadowData>
@@ -36,9 +36,9 @@
3636
<dormantStateLabelKey>DormantCompInactive</dormantStateLabelKey>
3737
</li>
3838
<li Class="CompProperties_WakeUpDormant"/>
39-
<li Class="CombatExtended.CompProperties_AmmoResupplyOnWakeup">
39+
<li Class="CombatExtended.CompProperties_AmmoResupplyOnWakeup">
4040
<dropInPods>True</dropInPods>
41-
</li>
41+
</li>
4242
</comps>
4343
</ThingDef>
4444

Source/CombatExtended/CombatExtended.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,6 @@
409409
<Compile Include="CombatExtended\Enums\LoadoutCountType.cs" />
410410
<Compile Include="CombatExtended\ExplosionCE.cs" />
411411
<Compile Include="CombatExtended\GenExplosionCE.cs" />
412-
<Compile Include="CombatExtended\GenClosestAmmo.cs" />
413412
<Compile Include="CombatExtended\Graphic_StackCountRanged.cs" />
414413
<Compile Include="CombatExtended\Graphic_Smoke.cs" />
415414
<Compile Include="CombatExtended\Jobs\JobDriver_Hunt.cs" />
@@ -419,6 +418,7 @@
419418
<Compile Include="CombatExtended\Jobs\JobDriver_WaitKnockdown.cs" />
420419
<Compile Include="CombatExtended\Jobs\JobGiver_CheckReload.cs" />
421420
<Compile Include="CombatExtended\Jobs\JobGiver_ConfigurableHostilityResponse.cs" />
421+
<Compile Include="CombatExtended\Jobs\JobGiver_DefenderReloadTurret.cs" />
422422
<Compile Include="CombatExtended\Jobs\JobGiver_ManTurretsNearPointCE.cs" />
423423
<Compile Include="CombatExtended\Jobs\JobGiver_ManTurretsNearSelfCE.cs" />
424424
<Compile Include="CombatExtended\Jobs\JobGiver_ReactToSuppression.cs" />

Source/CombatExtended/CombatExtended/Comps/CompAmmoUser.cs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ public Pawn Wielder
6868
return CompEquippable.PrimaryVerb.CasterPawn;
6969
}
7070
}
71+
public bool IsEquippedGun => Wielder != null;
7172
public Pawn Holder
7273
{
7374
get
@@ -124,7 +125,7 @@ public int MissingToFullMagazine
124125
{
125126
get
126127
{
127-
if (HasMagazine) { return -1; }
128+
if (!HasMagazine) { return 0; }
128129
if (SelectedAmmo == CurrentAmmo) { return Props.magazineSize - CurMagCount; }
129130
return Props.magazineSize;
130131
}
@@ -144,7 +145,7 @@ private IntVec3 Position
144145
{
145146
get
146147
{
147-
if (Wielder != null) return Wielder.Position;
148+
if (IsEquippedGun) return Wielder.Position;
148149
else if (turret != null) return turret.Position;
149150
else if (Holder != null) return Holder.Position;
150151
else return parent.Position;
@@ -185,7 +186,7 @@ public override void Initialize(CompProperties vprops)
185186
{
186187
base.Initialize(vprops);
187188

188-
//spawnUnloaded checks have all been moved to methods calling ResetAmmoCount.
189+
//spawnUnloaded checks have all been moved to methods calling ResetAmmoCount.
189190
//curMagCountInt = Props.spawnUnloaded && UseAmmo ? 0 : Props.magazineSize;
190191

191192
// Initialize ammo with default if none is set
@@ -258,7 +259,7 @@ public bool TryReduceAmmoCount(int ammoConsumedPerShot = 1)
258259
{
259260
ammoConsumedPerShot = (ammoConsumedPerShot > 0) ? ammoConsumedPerShot : 1;
260261

261-
if (Wielder == null && turret == null)
262+
if (!IsEquippedGun && turret == null)
262263
{
263264
Log.Error(parent.ToString() + " tried reducing its ammo count without a wielder");
264265
}
@@ -321,7 +322,7 @@ public void TryStartReload()
321322
}
322323
return;
323324
}
324-
if (Wielder == null && turret == null)
325+
if (!IsEquippedGun && turret == null)
325326
return;
326327

327328
// secondary branch for if we ended up being called up by a turret somehow...
@@ -340,7 +341,7 @@ public void TryStartReload()
340341
TryUnload();
341342

342343
// Check for ammo
343-
if (Wielder != null && !HasAmmo)
344+
if (IsEquippedGun && !HasAmmo)
344345
{
345346
DoOutOfAmmoAction();
346347
return;
@@ -354,7 +355,7 @@ public void TryStartReload()
354355
}
355356

356357
// Issue reload job
357-
if (Wielder != null)
358+
if (IsEquippedGun)
358359
{
359360
Job reloadJob = TryMakeReloadJob();
360361
if (reloadJob == null)
@@ -441,7 +442,7 @@ private void DoOutOfAmmoAction()
441442
{
442443
MoteMaker.ThrowText(Position.ToVector3Shifted(), Find.CurrentMap, "CE_OutOfAmmo".Translate() + "!");
443444
}
444-
if (Wielder != null && CompInventory != null && (Wielder.CurJob == null || Wielder.CurJob.def != JobDefOf.Hunt)) CompInventory.SwitchToNextViableWeapon();
445+
if (IsEquippedGun && CompInventory != null && (Wielder.CurJob == null || Wielder.CurJob.def != JobDefOf.Hunt)) CompInventory.SwitchToNextViableWeapon();
445446
}
446447

447448
public void LoadAmmo(Thing ammo = null)
@@ -566,10 +567,10 @@ public override IEnumerable<Gizmo> CompGetGizmosExtra()
566567
GizmoAmmoStatus ammoStatusGizmo = new GizmoAmmoStatus { compAmmo = this };
567568
yield return ammoStatusGizmo;
568569

569-
if ((Wielder != null && Wielder.Faction == Faction.OfPlayer) || (turret != null && turret.Faction == Faction.OfPlayer && (turret.MannableComp != null || UseAmmo)))
570+
if ((IsEquippedGun && Wielder.Faction == Faction.OfPlayer) || (turret != null && turret.Faction == Faction.OfPlayer && (turret.MannableComp != null || UseAmmo)))
570571
{
571572
Action action = null;
572-
if (Wielder != null) action = TryStartReload;
573+
if (IsEquippedGun) action = TryStartReload;
573574
else if (turret?.MannableComp != null) action = turret.TryForceReload;
574575

575576
// Check for teaching opportunities

Source/CombatExtended/CombatExtended/GenClosestAmmo.cs

Lines changed: 0 additions & 163 deletions
This file was deleted.
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
using CombatExtended.CombatExtended.Jobs.Utils;
2+
using CombatExtended.CombatExtended.LoggerUtils;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using System.Text;
7+
using System.Threading.Tasks;
8+
using Verse;
9+
using Verse.AI;
10+
11+
namespace CombatExtended
12+
{
13+
public class JobGiver_DefenderReloadTurret : ThinkNode_JobGiver
14+
{
15+
/// <summary>
16+
/// How low can ammo get before we want to reload the turret?
17+
/// Set arbitrarily, balance if needed.
18+
/// </summary>
19+
private const float ammoReloadThreshold = .5f;
20+
protected override Job TryGiveJob(Pawn pawn)
21+
{
22+
var turret = TryFindTurretWhichNeedsReloading(pawn);
23+
if (turret == null)
24+
{
25+
return null; // signals ThinkResult.NoJob.
26+
}
27+
return JobGiverUtils_Reload.MakeReloadJob(pawn, turret);
28+
}
29+
30+
private Building_TurretGunCE TryFindTurretWhichNeedsReloading(Pawn pawn)
31+
{
32+
bool _isTurretThatNeedsReloadingNow(Thing t)
33+
{
34+
var turret = t as Building_TurretGunCE;
35+
if (turret == null) { return false; }
36+
if (!JobGiverUtils_Reload.CanReload(pawn, turret, forced: false, emergency: true)) { return false; }
37+
38+
return turret.CompAmmo.CurMagCount <= turret.CompAmmo.Props.magazineSize / ammoReloadThreshold;
39+
};
40+
Thing hopefullyTurret = GenClosest.ClosestThingReachable(pawn.Position,
41+
pawn.Map,
42+
ThingRequest.ForGroup(ThingRequestGroup.BuildingArtificial),
43+
PathEndMode.Touch,
44+
TraverseParms.For(pawn),
45+
100f,
46+
_isTurretThatNeedsReloadingNow);
47+
48+
var actuallyTurret = hopefullyTurret as Building_TurretGunCE;
49+
if (actuallyTurret == null) { return null; }
50+
return actuallyTurret;
51+
52+
}
53+
54+
55+
}
56+
}

0 commit comments

Comments
 (0)