From 719332d9671467e7638244dff2b8f78b3bae2545 Mon Sep 17 00:00:00 2001 From: MaxDorob Date: Tue, 19 Nov 2024 03:11:05 +0600 Subject: [PATCH] Hold fire for CIWS --- Defs/ThingDefs_Misc/Weapons_CIWS.xml | 6 ++ .../CombatExtended/Comps/CompVerbDisabler.cs | 38 +++++++++++ .../CombatExtended/Comps/VerbCIWS.cs | 66 +++++++++---------- .../Comps/VerbCIWSProjectile.cs | 2 - .../CombatExtended/Comps/VerbCIWSSkyfaller.cs | 2 - .../CombatExtended/IVerbDisableable.cs | 17 +++++ .../Things/Building_Turret_MultiVerbs.cs | 21 +++++- 7 files changed, 112 insertions(+), 40 deletions(-) create mode 100644 Source/CombatExtended/CombatExtended/Comps/CompVerbDisabler.cs create mode 100644 Source/CombatExtended/CombatExtended/IVerbDisableable.cs diff --git a/Defs/ThingDefs_Misc/Weapons_CIWS.xml b/Defs/ThingDefs_Misc/Weapons_CIWS.xml index 286db6fc1c..0e1ef49e59 100644 --- a/Defs/ThingDefs_Misc/Weapons_CIWS.xml +++ b/Defs/ThingDefs_Misc/Weapons_CIWS.xml @@ -47,6 +47,8 @@ GunTail_Heavy 9 Mounted + HoldCloseInProjectilesFire + HoldCloseInProjectilesFireDesc
  • CombatExtended.VerbCIWSSkyfaller @@ -61,6 +63,7 @@ GunTail_Heavy 9 Mounted + HoldCloseInSkyfallersFire
  • @@ -69,6 +72,9 @@ 7.8 AmmoSet_8x35mmCharged +
  • + CombatExtended.CompVerbDisabler +
  • diff --git a/Source/CombatExtended/CombatExtended/Comps/CompVerbDisabler.cs b/Source/CombatExtended/CombatExtended/Comps/CompVerbDisabler.cs new file mode 100644 index 0000000000..67466ae447 --- /dev/null +++ b/Source/CombatExtended/CombatExtended/Comps/CompVerbDisabler.cs @@ -0,0 +1,38 @@ +using RimWorld; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Verse; + +namespace CombatExtended +{ + public class CompVerbDisabler : ThingComp + { + public override IEnumerable CompGetGizmosExtra() + { + foreach (var gizmo in base.CompGetGizmosExtra()) + { + yield return gizmo; + } + var verbs = parent.GetComp()?.AllVerbs.OfType(); + if (verbs != null) + { + foreach (var verb in verbs) + { + var command = new Command_Toggle() + { + defaultDesc = verb.HoldFireDesc.Translate(), + defaultLabel = verb.HoldFireLabel.Translate(), + icon = verb.HoldFireIcon, + isActive = () => verb.HoldFire, + toggleAction = () => verb.HoldFire = !verb.HoldFire, + + }; + yield return command; + } + } + } + } +} diff --git a/Source/CombatExtended/CombatExtended/Comps/VerbCIWS.cs b/Source/CombatExtended/CombatExtended/Comps/VerbCIWS.cs index 10d563fdcc..85cbc5d380 100644 --- a/Source/CombatExtended/CombatExtended/Comps/VerbCIWS.cs +++ b/Source/CombatExtended/CombatExtended/Comps/VerbCIWS.cs @@ -6,49 +6,34 @@ using System.Threading.Tasks; using UnityEngine; using UnityEngine.SocialPlatforms; +using UnityEngine.UI; using Verse; using Verse.Sound; namespace CombatExtended { - public abstract class VerbCIWS : Verb_ShootCE_CIWS, ITargetSearcher + public abstract class VerbCIWS : Verb_ShootCE_CIWS, ITargetSearcher, IVerbDisableable { protected bool debug; - protected bool holdFire; + protected Texture2D icon; + public virtual bool HoldFire { get; set; } public VerbProperties_CIWS Props => verbProps as VerbProperties_CIWS; - protected abstract string HoldLabel { get; } - protected abstract string HoldDesc { get; } - protected virtual string HoldIcon => "UI/Commands/HoldFire"; - - //public override IEnumerable CompGetGizmosExtra() - //{ - // foreach (var gizmo in base.CompGetGizmosExtra()) - // { - // yield return gizmo; - // } - // if (Turret.CanToggleHoldFire) - // { - // yield return new Command_Toggle - // { - // defaultLabel = HoldLabel.Translate(), - // defaultDesc = HoldDesc.Translate(), - // icon = ContentFinder.Get(HoldIcon, true), - // hotKey = KeyBindingDefOf.Misc6, - // toggleAction = delegate () - // { - // this.holdFire = !this.holdFire; - // if (this.holdFire && HasTarget) - // { - // Turret.ResetForcedTarget(); - // } - // }, - // isActive = (() => this.holdFire) - // }; - // } - //} + public virtual string HoldFireLabel => Props.holdFireLabel; + public virtual string HoldFireDesc => Props.holdFireDesc; + public virtual Texture2D HoldFireIcon + { + get + { + if (icon == null) + { + icon = ContentFinder.Get(Props.holdFireIcon); + } + return icon; + } + } protected override bool ShouldAim => false; - public virtual bool Active => !holdFire && Turret.Active; + public virtual bool Active => !HoldFire && Turret.Active; protected override bool LockRotationAndAngle => false; public abstract bool TryFindNewTarget(out LocalTargetInfo target); public virtual void ShowTrajectories() @@ -102,14 +87,22 @@ protected override bool KeepBurstOnNoShootLine(bool suppressing, out ShootLine s { shootLine = lastShootLine.HasValue ? lastShootLine.Value : default; return !currentTarget.ThingDestroyed; - } - + } + public override bool Available() + { + return Active && base.Available(); + } } public abstract class VerbCIWS : VerbCIWS where TargetType : Thing { public override bool TryFindNewTarget(out LocalTargetInfo target) { + if (!Active) + { + target = LocalTargetInfo.Invalid; + return false; + } float range = this.verbProps.range; var _target = Targets.Where(x => Props.Interceptable(x.def) && !Turret.IgnoredDefs.Contains(x.def)).Where(x => !IsFriendlyTo(x)).FirstOrDefault(t => { @@ -229,6 +222,9 @@ public override bool TryFindCEShootLineFromTo(IntVec3 root, LocalTargetInfo targ public abstract class VerbProperties_CIWS : VerbPropertiesCE { + public string holdFireIcon = "UI/Commands/HoldFire"; + public string holdFireLabel = "HoldFire"; + public string holdFireDesc; public List ignored = new List(); public IEnumerable Ignored => ignored; public virtual bool Interceptable(ThingDef targetDef) => true; diff --git a/Source/CombatExtended/CombatExtended/Comps/VerbCIWSProjectile.cs b/Source/CombatExtended/CombatExtended/Comps/VerbCIWSProjectile.cs index 5a39faa553..5b2bbb1d11 100644 --- a/Source/CombatExtended/CombatExtended/Comps/VerbCIWSProjectile.cs +++ b/Source/CombatExtended/CombatExtended/Comps/VerbCIWSProjectile.cs @@ -11,8 +11,6 @@ namespace CombatExtended { public class VerbCIWSProjectile : VerbCIWS { - protected override string HoldDesc => "HoldCloseInProjectilesFireDesc"; - protected override string HoldLabel => "HoldCloseInProjectilesFire"; public new VerbProperties_CIWSProjectile Props => verbProps as VerbProperties_CIWSProjectile; public override IEnumerable Targets => Caster.Map?.listerThings.ThingsInGroup(ThingRequestGroup.Projectile).OfType() ?? Array.Empty(); diff --git a/Source/CombatExtended/CombatExtended/Comps/VerbCIWSSkyfaller.cs b/Source/CombatExtended/CombatExtended/Comps/VerbCIWSSkyfaller.cs index 8c19061f49..c6767da0e6 100644 --- a/Source/CombatExtended/CombatExtended/Comps/VerbCIWSSkyfaller.cs +++ b/Source/CombatExtended/CombatExtended/Comps/VerbCIWSSkyfaller.cs @@ -11,8 +11,6 @@ namespace CombatExtended { public class VerbCIWSSkyfaller : VerbCIWS { - protected override string HoldDesc => "HoldSkyfallerFireDesc"; - protected override string HoldLabel => "HoldSkyfallerFire"; public override IEnumerable Targets => Caster.Map?.listerThings.ThingsInGroup(Verse.ThingRequestGroup.ActiveDropPod).OfType(); diff --git a/Source/CombatExtended/CombatExtended/IVerbDisableable.cs b/Source/CombatExtended/CombatExtended/IVerbDisableable.cs new file mode 100644 index 0000000000..5077665fc1 --- /dev/null +++ b/Source/CombatExtended/CombatExtended/IVerbDisableable.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using UnityEngine; + +namespace CombatExtended +{ + public interface IVerbDisableable + { + bool HoldFire { get; set; } + string HoldFireLabel { get; } + string HoldFireDesc { get; } + Texture2D HoldFireIcon { get; } + } +} diff --git a/Source/CombatExtended/CombatExtended/Things/Building_Turret_MultiVerbs.cs b/Source/CombatExtended/CombatExtended/Things/Building_Turret_MultiVerbs.cs index 6c6a44416a..d8bc521758 100644 --- a/Source/CombatExtended/CombatExtended/Things/Building_Turret_MultiVerbs.cs +++ b/Source/CombatExtended/CombatExtended/Things/Building_Turret_MultiVerbs.cs @@ -1,4 +1,5 @@ -using System; +using RimWorld; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -67,5 +68,23 @@ public override void ResetForcedTarget() base.ResetForcedTarget(); activeVerb = null; } + public override IEnumerable GetGizmos() + { + foreach (var gizmo in base.GetGizmos()) + { + yield return gizmo; + } + if (PlayerControlled) + { + var disablerComp = Gun.TryGetComp(); + if (disablerComp != null) + { + foreach (var gizmo in disablerComp.CompGetGizmosExtra()) + { + yield return gizmo; + } + } + } + } } }