From 71dac9c559ae1c2eb17574a91647b1a785ac419d Mon Sep 17 00:00:00 2001 From: CMDR-Bill-Doors Date: Wed, 13 Dec 2023 20:29:55 +0000 Subject: [PATCH 01/11] init commit --- .../Building_AutoloaderCE.cs | 4 +- .../CombatExtended/Comps/CompAmmoUser.cs | 28 ++++--- .../Comps/CompAmmoUserGeneric.cs | 79 +++++++++++++++++++ .../Jobs/JobGiver_CheckReload.cs | 2 +- .../Jobs/JobGiver_ManTurretsNearPointCE.cs | 2 +- .../Jobs/JobGiver_ManTurretsNearSelfCE.cs | 2 +- 6 files changed, 99 insertions(+), 18 deletions(-) create mode 100644 Source/CombatExtended/CombatExtended/Comps/CompAmmoUserGeneric.cs diff --git a/Source/CombatExtended/CombatExtended/Building_AmmoContainerCE/Building_AutoloaderCE.cs b/Source/CombatExtended/CombatExtended/Building_AmmoContainerCE/Building_AutoloaderCE.cs index 23132c7ba5..8ad053ce9e 100644 --- a/Source/CombatExtended/CombatExtended/Building_AmmoContainerCE/Building_AutoloaderCE.cs +++ b/Source/CombatExtended/CombatExtended/Building_AmmoContainerCE/Building_AutoloaderCE.cs @@ -88,7 +88,7 @@ public override Graphic Graphic public bool CanReplaceAmmo(CompAmmoUser ammoUser) { - return shouldReplaceAmmo && ammoUser.Props.ammoSet == CompAmmoUser.Props.ammoSet && ammoUser.CurrentAmmo != CompAmmoUser.CurrentAmmo; + return shouldReplaceAmmo && ammoUser.CurAmmoSet == CompAmmoUser.CurAmmoSet && ammoUser.CurrentAmmo != CompAmmoUser.CurrentAmmo; } public override void SpawnSetup(Map map, bool respawningAfterLoad) @@ -228,7 +228,7 @@ public override IEnumerable GetGizmos() } if (!success) { - Messages.Message(string.Format("CE_AutoLoader_NoTurretToReload".Translate(), Label, CompAmmoUser.Props.ammoSet.label), this, MessageTypeDefOf.RejectInput, historical: false); + Messages.Message(string.Format("CE_AutoLoader_NoTurretToReload".Translate(), Label, CompAmmoUser.CurAmmoSet.label), this, MessageTypeDefOf.RejectInput, historical: false); } }; yield return reload; diff --git a/Source/CombatExtended/CombatExtended/Comps/CompAmmoUser.cs b/Source/CombatExtended/CombatExtended/Comps/CompAmmoUser.cs index 1a13c15d15..1af6e3a714 100644 --- a/Source/CombatExtended/CombatExtended/Comps/CompAmmoUser.cs +++ b/Source/CombatExtended/CombatExtended/Comps/CompAmmoUser.cs @@ -17,9 +17,9 @@ public class CompAmmoUser : CompRangedGizmoGiver { #region Fields - private int curMagCountInt = 0; - private AmmoDef currentAmmoInt = null; - private AmmoDef selectedAmmo; + protected int curMagCountInt = 0; + protected AmmoDef currentAmmoInt = null; + protected AmmoDef selectedAmmo; private Thing ammoToBeDeleted; @@ -46,7 +46,7 @@ public int MagsLeft { CompInventory.UpdateInventory(); int count = 0; - foreach (AmmoLink link in Props.ammoSet.ammoTypes) + foreach (AmmoLink link in CurAmmoSet.ammoTypes) { count += CompInventory.AmmoCountOfDef(link.ammo); } @@ -137,7 +137,7 @@ public bool UseAmmo { get { - return Props.ammoSet != null && AmmoUtility.IsAmmoSystemActive(Props.ammoSet); + return CurAmmoSet != null && AmmoUtility.IsAmmoSystemActive(CurAmmoSet); } } public bool IsAOEWeapon @@ -172,7 +172,7 @@ public bool HasAmmo { get { - return CompInventory != null && CompInventory.ammoList.Any(x => Props.ammoSet.ammoTypes.Any(a => a.ammo == x.def)); + return CompInventory != null && CompInventory.ammoList.Any(x => CurAmmoSet.ammoTypes.Any(a => a.ammo == x.def)); } } public bool HasMagazine => MagSize > 0; @@ -217,7 +217,9 @@ public bool FullMagazine } } - public ThingDef CurAmmoProjectile => Props.ammoSet?.ammoTypes?.FirstOrDefault(x => x.ammo == CurrentAmmo)?.projectile ?? parent.def.Verbs.FirstOrDefault().defaultProjectile; + public virtual AmmoSetDef CurAmmoSet => Props.ammoSet; + + public virtual ThingDef CurAmmoProjectile => CurAmmoSet?.ammoTypes?.FirstOrDefault(x => x.ammo == CurrentAmmo)?.projectile ?? parent.def.Verbs.FirstOrDefault().defaultProjectile; public CompInventory CompInventory { get @@ -267,7 +269,7 @@ private Map Map } public bool ShouldThrowMote => Props.throwMote && MagSize > 1; - public AmmoDef SelectedAmmo + public virtual AmmoDef SelectedAmmo { get { @@ -297,7 +299,7 @@ public override void Initialize(CompProperties vprops) // Initialize ammo with default if none is set if (UseAmmo) { - if (Props.ammoSet.ammoTypes.NullOrEmpty()) + if (CurAmmoSet.ammoTypes.NullOrEmpty()) { Log.Error(parent.Label + " has no available ammo types"); } @@ -305,7 +307,7 @@ public override void Initialize(CompProperties vprops) { if (currentAmmoInt == null) { - currentAmmoInt = (AmmoDef)Props.ammoSet.ammoTypes[0].ammo; + currentAmmoInt = (AmmoDef)CurAmmoSet.ammoTypes[0].ammo; } if (selectedAmmo == null) { @@ -671,7 +673,7 @@ public bool TryPickupAmmo() { return false; } - IEnumerable supportedAmmo = Props.ammoSet.ammoTypes.Select(a => a.ammo); + IEnumerable supportedAmmo = CurAmmoSet.ammoTypes.Select(a => a.ammo); foreach (Thing thing in Holder.Position.AmmoInRange(Holder.Map, 6).Where(t => t is AmmoThing ammo && supportedAmmo.Contains(ammo.AmmoDef) && (!Holder.IsColonist || (!ammo.IsForbidden(Holder) && ammo.Position.AdjacentTo8WayOrInside(Holder))))) @@ -831,7 +833,7 @@ public bool TryFindAmmoInInventory(out Thing ammoThing) } // Try finding ammo from different type - foreach (AmmoLink link in Props.ammoSet.ammoTypes) + foreach (AmmoLink link in CurAmmoSet.ammoTypes) { ammoThing = CompInventory.ammoList.Find(thing => thing.def == link.ammo); if (ammoThing != null) @@ -927,7 +929,7 @@ public override IEnumerable CompGetGizmosExtra() public override string TransformLabel(string label) { - string ammoSet = UseAmmo && Controller.settings.ShowCaliberOnGuns ? " (" + (string)Props.ammoSet.LabelCap + ") " : ""; + string ammoSet = UseAmmo && Controller.settings.ShowCaliberOnGuns ? " (" + (string)SelectedAmmo.LabelCap + ") " : ""; return label + ammoSet; } diff --git a/Source/CombatExtended/CombatExtended/Comps/CompAmmoUserGeneric.cs b/Source/CombatExtended/CombatExtended/Comps/CompAmmoUserGeneric.cs new file mode 100644 index 0000000000..d5e47a3dfd --- /dev/null +++ b/Source/CombatExtended/CombatExtended/Comps/CompAmmoUserGeneric.cs @@ -0,0 +1,79 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using UnityEngine; +using Verse; + +namespace CombatExtended.CombatExtended.Comps +{ + public class CompAmmoUserGeneric : CompAmmoUser + { + private List usableAmmoSets = new List(); + + public AmmoSetDef SelectedAmmoSet; + + public List UsableAmmoSets + { + get + { + if (usableAmmoSets.NullOrEmpty()) + { + foreach (var def in DefDatabase.AllDefs.Where((AmmoSetDef def) => def.generated)) + { + if (def.similarTo == null) + { + continue; + } + if (def.similarTo == this.Props.ammoSet) + { + usableAmmoSets.Add(def); + } + } + } + return usableAmmoSets; + } + } + + public override AmmoSetDef CurAmmoSet => SelectedAmmoSet; + + public override void PostExposeData() + { + base.PostExposeData(); + Scribe_Defs.Look(ref SelectedAmmoSet, "SelectedAmmoSet"); + } + + public override void Initialize(CompProperties vprops) + { + SelectedAmmoSet = UsableAmmoSets.First(); + base.Initialize(vprops); + } + + public override IEnumerable CompGetGizmosExtra() + { + foreach (var gizmo in base.CompGetGizmosExtra()) + { + yield return gizmo; + } + Command_Action command_Action = new Command_Action(); + command_Action.defaultLabel = "CE_SelectAmmoSet".Translate(); + command_Action.defaultDesc = "CommandSelectMineralToScanForDesc".Translate(); + command_Action.icon = ContentFinder.Get("UI/Buttons/Reload", reportFailure: false); + command_Action.action = delegate + { + List list = new List(); + foreach (AmmoSetDef caliber in usableAmmoSets) + { + FloatMenuOption item = new FloatMenuOption(caliber.LabelCap, delegate + { + this.SelectedAmmoSet = caliber; + }, MenuOptionPriority.Default, null, null); + list.Add(item); + } + Find.WindowStack.Add(new FloatMenu(list)); + }; + yield return command_Action; + } + } +} diff --git a/Source/CombatExtended/CombatExtended/Jobs/JobGiver_CheckReload.cs b/Source/CombatExtended/CombatExtended/Jobs/JobGiver_CheckReload.cs index 57c4446115..00de2a9ff6 100644 --- a/Source/CombatExtended/CombatExtended/Jobs/JobGiver_CheckReload.cs +++ b/Source/CombatExtended/CombatExtended/Jobs/JobGiver_CheckReload.cs @@ -140,7 +140,7 @@ private bool DoReloadCheck(Pawn pawn, out ThingWithComps reloadWeapon, out AmmoD if (tmpComp.UseAmmo && pawnHasLoadout && !TrackingSatisfied(pawn, ammoType, magazineSize)) { // Do we have ammo in the inventory that the gun uses which satisfies requirements? (expensive) - AmmoDef matchAmmo = tmpComp.Props.ammoSet.ammoTypes + AmmoDef matchAmmo = tmpComp.CurAmmoSet.ammoTypes .Where(al => al.ammo != ammoType) .Select(al => al.ammo) .FirstOrDefault(ad => TrackingSatisfied(pawn, ad, magazineSize) diff --git a/Source/CombatExtended/CombatExtended/Jobs/JobGiver_ManTurretsNearPointCE.cs b/Source/CombatExtended/CombatExtended/Jobs/JobGiver_ManTurretsNearPointCE.cs index c9448e901a..228f64d9f0 100644 --- a/Source/CombatExtended/CombatExtended/Jobs/JobGiver_ManTurretsNearPointCE.cs +++ b/Source/CombatExtended/CombatExtended/Jobs/JobGiver_ManTurretsNearPointCE.cs @@ -54,7 +54,7 @@ private static Thing FindAmmoForTurret(Pawn pawn, Thing turret) 40f, t => !t.IsForbidden(pawn) && pawn.CanReserve(t, 10, 1) && - compAmmo.Props.ammoSet.ammoTypes.Any(l => l.ammo == t.def)); + compAmmo.CurAmmoSet.ammoTypes.Any(l => l.ammo == t.def)); } } } diff --git a/Source/CombatExtended/CombatExtended/Jobs/JobGiver_ManTurretsNearSelfCE.cs b/Source/CombatExtended/CombatExtended/Jobs/JobGiver_ManTurretsNearSelfCE.cs index b02bca7243..1129cfa3d5 100644 --- a/Source/CombatExtended/CombatExtended/Jobs/JobGiver_ManTurretsNearSelfCE.cs +++ b/Source/CombatExtended/CombatExtended/Jobs/JobGiver_ManTurretsNearSelfCE.cs @@ -54,7 +54,7 @@ private static Thing FindAmmoForTurret(Pawn pawn, Thing turret) 40f, t => !t.IsForbidden(pawn) && pawn.CanReserve(t, 10, 1) && - compAmmo.Props.ammoSet.ammoTypes.Any(l => l.ammo == t.def)); + compAmmo.CurAmmoSet.ammoTypes.Any(l => l.ammo == t.def)); } } } From bfa3f7f33102155a071fd9f794b232c042b41826 Mon Sep 17 00:00:00 2001 From: CMDR-Bill-Doors Date: Thu, 14 Dec 2023 12:31:36 +0000 Subject: [PATCH 02/11] this shit actually works now --- .../ThingDefs_Buildings/AutoLoaderExample.xml | 212 ++++++++---------- .../Building_AutoloaderCE.cs | 24 +- .../CombatExtended/Comps/CompAmmoUser.cs | 2 +- .../Comps/CompAmmoUserGeneric.cs | 65 ++++-- .../CombatExtended/Gizmos/Command_Reload.cs | 2 +- .../Jobs/Utils/JobGiverUtils_Reload.cs | 1 + 6 files changed, 153 insertions(+), 153 deletions(-) diff --git a/Defs/ThingDefs_Buildings/AutoLoaderExample.xml b/Defs/ThingDefs_Buildings/AutoLoaderExample.xml index 1ca7370284..05f540a5b1 100644 --- a/Defs/ThingDefs_Buildings/AutoLoaderExample.xml +++ b/Defs/ThingDefs_Buildings/AutoLoaderExample.xml @@ -1,144 +1,110 @@ - + 0.5 + false + 42 + (2,1) + + 200 + + + BuildingDestroyed_Metal_Small + + + 100 + 1800 + 20 + 0.5 + + 0.5 + + Misc + MinifiedThing + +
  • BuildingsMisc
  • +
    +
    \ No newline at end of file diff --git a/Source/CombatExtended/CombatExtended/Building_AmmoContainerCE/Building_AutoloaderCE.cs b/Source/CombatExtended/CombatExtended/Building_AmmoContainerCE/Building_AutoloaderCE.cs index 8ad053ce9e..df39eba5fc 100644 --- a/Source/CombatExtended/CombatExtended/Building_AmmoContainerCE/Building_AutoloaderCE.cs +++ b/Source/CombatExtended/CombatExtended/Building_AmmoContainerCE/Building_AutoloaderCE.cs @@ -95,7 +95,7 @@ public override void SpawnSetup(Map map, bool respawningAfterLoad) { base.SpawnSetup(map, respawningAfterLoad); Map.GetComponent().Register(this); - CompAmmoUser = GetComp(); + CompAmmoUser = this.TryGetComp(); dormantComp = GetComp(); initiatableComp = GetComp(); @@ -349,18 +349,24 @@ public bool StartReload(CompAmmoUser TurretMagazine, bool continued = false) //if this is the right turret to reload if (graphicsExt != null) { - //if def exists and match - bool tagMatch = graphicsExt.allowedTurrets.Any() && graphicsExt.allowedTurrets.Contains(turret.def.defName); + //if turret type restriction is in place, if both are null, tag chech automatically pass + bool tagMatch = graphicsExt.allowedTurrets.NullOrEmpty() && graphicsExt.allowedTurretTags.NullOrEmpty(); - //if tag exists and match - if (!tagMatch && graphicsExt.allowedTurretTags.Any()) + if (!tagMatch) { - foreach (string loadertag in graphicsExt.allowedTurretTags) + //if def dont exist or match + tagMatch = graphicsExt.allowedTurrets.NullOrEmpty() || graphicsExt.allowedTurrets.Contains(turret.def.defName); + + //if tag exists and match + if (!tagMatch && graphicsExt.allowedTurretTags.Any()) { - if (turret.def.building.buildingTags.NotNullAndContains(loadertag)) + foreach (string loadertag in graphicsExt.allowedTurretTags) { - tagMatch = true; - break; + if (turret.def.building.buildingTags.NotNullAndContains(loadertag)) + { + tagMatch = true; + break; + } } } } diff --git a/Source/CombatExtended/CombatExtended/Comps/CompAmmoUser.cs b/Source/CombatExtended/CombatExtended/Comps/CompAmmoUser.cs index 1af6e3a714..dc10f1d2d7 100644 --- a/Source/CombatExtended/CombatExtended/Comps/CompAmmoUser.cs +++ b/Source/CombatExtended/CombatExtended/Comps/CompAmmoUser.cs @@ -929,7 +929,7 @@ public override IEnumerable CompGetGizmosExtra() public override string TransformLabel(string label) { - string ammoSet = UseAmmo && Controller.settings.ShowCaliberOnGuns ? " (" + (string)SelectedAmmo.LabelCap + ") " : ""; + string ammoSet = UseAmmo && Controller.settings.ShowCaliberOnGuns ? " (" + (string)CurAmmoSet.LabelCap + ") " : ""; return label + ammoSet; } diff --git a/Source/CombatExtended/CombatExtended/Comps/CompAmmoUserGeneric.cs b/Source/CombatExtended/CombatExtended/Comps/CompAmmoUserGeneric.cs index d5e47a3dfd..8f1a5cbaf1 100644 --- a/Source/CombatExtended/CombatExtended/Comps/CompAmmoUserGeneric.cs +++ b/Source/CombatExtended/CombatExtended/Comps/CompAmmoUserGeneric.cs @@ -6,7 +6,7 @@ using UnityEngine; using Verse; -namespace CombatExtended.CombatExtended.Comps +namespace CombatExtended { public class CompAmmoUserGeneric : CompAmmoUser { @@ -14,19 +14,21 @@ public class CompAmmoUserGeneric : CompAmmoUser public AmmoSetDef SelectedAmmoSet; + public AmmoSetDef UsedGenericAmmoSet => Props.ammoSet.similarTo ?? Props.ammoSet; + public List UsableAmmoSets { get { if (usableAmmoSets.NullOrEmpty()) { - foreach (var def in DefDatabase.AllDefs.Where((AmmoSetDef def) => def.generated)) + foreach (var def in DefDatabase.AllDefs) { if (def.similarTo == null) { continue; } - if (def.similarTo == this.Props.ammoSet) + if (def.similarTo == UsedGenericAmmoSet && !def.ammoTypes.First().ammo.menuHidden) { usableAmmoSets.Add(def); } @@ -46,8 +48,30 @@ public override void PostExposeData() public override void Initialize(CompProperties vprops) { - SelectedAmmoSet = UsableAmmoSets.First(); base.Initialize(vprops); + SelectedAmmoSet = Props.ammoSet; + RegenSelectedAmmo(); + } + public void RegenSelectedAmmo() + { + if (currentAmmoInt == null) + { + currentAmmoInt = (AmmoDef)CurAmmoSet.ammoTypes[0].ammo; + } + if (selectedAmmo == null) + { + selectedAmmo = currentAmmoInt; + } + else + { + selectedAmmo = CurAmmoSet.ammoTypes.Where(l => l.ammo.ammoClass == currentAmmoInt.ammoClass).First().ammo; + } + } + + [Compatibility.Multiplayer.SyncMethod] + private void SyncedSelectAmmoSet(AmmoSetDef caliber) + { + SelectedAmmoSet = caliber; RegenSelectedAmmo(); } public override IEnumerable CompGetGizmosExtra() @@ -56,24 +80,27 @@ public override IEnumerable CompGetGizmosExtra() { yield return gizmo; } - Command_Action command_Action = new Command_Action(); - command_Action.defaultLabel = "CE_SelectAmmoSet".Translate(); - command_Action.defaultDesc = "CommandSelectMineralToScanForDesc".Translate(); - command_Action.icon = ContentFinder.Get("UI/Buttons/Reload", reportFailure: false); - command_Action.action = delegate + if (!Controller.settings.GenericAmmo) { - List list = new List(); - foreach (AmmoSetDef caliber in usableAmmoSets) + Command_Action command_Action = new Command_Action(); + command_Action.defaultLabel = "CE_SelectAmmoSet".Translate(); + command_Action.defaultDesc = "CommandSelectMineralToScanForDesc".Translate(); + command_Action.icon = ContentFinder.Get("UI/Buttons/Reload", reportFailure: false); + command_Action.action = delegate { - FloatMenuOption item = new FloatMenuOption(caliber.LabelCap, delegate + List list = new List(); + foreach (AmmoSetDef caliber in UsableAmmoSets) { - this.SelectedAmmoSet = caliber; - }, MenuOptionPriority.Default, null, null); - list.Add(item); - } - Find.WindowStack.Add(new FloatMenu(list)); - }; - yield return command_Action; + FloatMenuOption item = new FloatMenuOption(caliber.LabelCap, delegate + { + SyncedSelectAmmoSet(caliber); + }, MenuOptionPriority.Default, null, null); + list.Add(item); + } + Find.WindowStack.Add(new FloatMenu(list)); + }; + yield return command_Action; + } } } } diff --git a/Source/CombatExtended/CombatExtended/Gizmos/Command_Reload.cs b/Source/CombatExtended/CombatExtended/Gizmos/Command_Reload.cs index d8a83d5f0f..d93368b433 100644 --- a/Source/CombatExtended/CombatExtended/Gizmos/Command_Reload.cs +++ b/Source/CombatExtended/CombatExtended/Gizmos/Command_Reload.cs @@ -119,7 +119,7 @@ private List BuildAmmoOptions() { var user = other.compAmmo; - foreach (AmmoLink link in user.Props.ammoSet.ammoTypes) + foreach (AmmoLink link in user.CurAmmoSet.ammoTypes) { var ammoDef = link.ammo; var ammoClass = ammoDef.ammoClass; diff --git a/Source/CombatExtended/CombatExtended/Jobs/Utils/JobGiverUtils_Reload.cs b/Source/CombatExtended/CombatExtended/Jobs/Utils/JobGiverUtils_Reload.cs index 70f5faa4a5..6f099ad83a 100644 --- a/Source/CombatExtended/CombatExtended/Jobs/Utils/JobGiverUtils_Reload.cs +++ b/Source/CombatExtended/CombatExtended/Jobs/Utils/JobGiverUtils_Reload.cs @@ -165,6 +165,7 @@ public static Thing FindBestAmmo(Pawn pawn, Building_AutoloaderCE AutoLoader) { var ammoComp = AutoLoader.CompAmmoUser; AmmoDef requestedAmmo = ammoComp.SelectedAmmo; + Log.Message(requestedAmmo.defName); var bestAmmo = FindBestAmmo(pawn, requestedAmmo); // try to find currently selected ammo first if (bestAmmo == null && ammoComp.EmptyMagazine && requestedAmmo.AmmoSetDefs != null && AutoLoader.Faction != Faction.OfPlayer) { From e4b675c46ff5a8c67875284ca06fa73c9d0d71c2 Mon Sep 17 00:00:00 2001 From: CMDR-Bill-Doors Date: Tue, 9 Jul 2024 17:34:32 +0100 Subject: [PATCH 03/11] Removed the debug log I forgot to remove --- .../CombatExtended/Jobs/Utils/JobGiverUtils_Reload.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/Source/CombatExtended/CombatExtended/Jobs/Utils/JobGiverUtils_Reload.cs b/Source/CombatExtended/CombatExtended/Jobs/Utils/JobGiverUtils_Reload.cs index 6f099ad83a..70f5faa4a5 100644 --- a/Source/CombatExtended/CombatExtended/Jobs/Utils/JobGiverUtils_Reload.cs +++ b/Source/CombatExtended/CombatExtended/Jobs/Utils/JobGiverUtils_Reload.cs @@ -165,7 +165,6 @@ public static Thing FindBestAmmo(Pawn pawn, Building_AutoloaderCE AutoLoader) { var ammoComp = AutoLoader.CompAmmoUser; AmmoDef requestedAmmo = ammoComp.SelectedAmmo; - Log.Message(requestedAmmo.defName); var bestAmmo = FindBestAmmo(pawn, requestedAmmo); // try to find currently selected ammo first if (bestAmmo == null && ammoComp.EmptyMagazine && requestedAmmo.AmmoSetDefs != null && AutoLoader.Faction != Faction.OfPlayer) { From c2304a99c7366060c9e095cc1f096c05d38483ee Mon Sep 17 00:00:00 2001 From: CMDR-Bill-Doors Date: Mon, 15 Jul 2024 06:57:37 +0100 Subject: [PATCH 04/11] test def, pawn reload fix --- Defs/TempTestDef.xml | 177 ++++++++++++++++++ .../ThingDefs_Buildings/AutoLoaderExample.xml | 19 +- .../Comps/CompAmmoUserGeneric.cs | 14 +- .../Jobs/JobGiver_CheckReload.cs | 2 +- 4 files changed, 204 insertions(+), 8 deletions(-) create mode 100644 Defs/TempTestDef.xml diff --git a/Defs/TempTestDef.xml b/Defs/TempTestDef.xml new file mode 100644 index 0000000000..9e7ac47f58 --- /dev/null +++ b/Defs/TempTestDef.xml @@ -0,0 +1,177 @@ + + + + Turret_GenericAmmoUserTest + + 6 + + Things/Building/Turrets/MachineGunBase + + (0.27,0.25,0.27) + (0,0,0) + + + UI/Icons/Turrets/MediumAutoTurret_uiIcon + + 20000 + 200 + 0.6 + 30 + 40 + 0.5 + 0.75 + + Automatic turret equiped with a full powered cartridge machine gun. Fairly resistant to damage. + + 150 + 8 + + + true + Gun_GenericAmmoUserTest + + +
  • PlaceWorker_TurretTop
  • +
  • PlaceWorker_ShowTurretRadius
  • +
    + +
  • + CompPowerTrader + 150 +
  • +
    + MinifiedThing + +
  • GunTurrets
  • +
  • PrecisionRifling
  • +
    +
    + + + Gun_GenericAmmoUserTest + + + Things/Building/Turrets/MediumTurret_Top + Graphic_Single + + Full powered cartridge gun on a turret mount. + + 1 + 0.05 + 0.84 + 0.36 + 10 + + +
  • + 0.95 + CombatExtended.Verb_ShootCE + true + Bullet_762x51mmNATO_FMJ + 1.3 + 55 + 6 + 10 + MediumMG + GunTail_Medium + 11 + Mounted +
  • +
    + +
  • + CombatExtended.CompAmmoUserGeneric + 80 + 7.8 + AmmoSet_762x51mmNATO +
  • +
    +
    + + + Turret_GenericAmmoUserTestII + + 6 + + Things/Building/Turrets/MachineGunBase + + (0.27,0.25,0.27) + (0,0,0) + + + UI/Icons/Turrets/MediumAutoTurret_uiIcon + + 20000 + 200 + 0.6 + 30 + 40 + 0.5 + 0.75 + + Automatic turret equiped with a full powered cartridge machine gun. Fairly resistant to damage. + + 150 + 8 + + + true + Gun_GenericAmmoUserTestII + + +
  • PlaceWorker_TurretTop
  • +
  • PlaceWorker_ShowTurretRadius
  • +
    + +
  • + CompPowerTrader + 150 +
  • +
    + MinifiedThing + +
  • GunTurrets
  • +
  • PrecisionRifling
  • +
    +
    + + + Gun_GenericAmmoUserTestII + + + Things/Building/Turrets/MediumTurret_Top + Graphic_Single + + Full powered cartridge gun on a turret mount. + + 1 + 0.05 + 0.84 + 0.36 + 10 + + +
  • + 0.95 + CombatExtended.Verb_ShootCE + true + Bullet_762x51mmNATO_FMJ + 1.3 + 55 + 6 + 10 + MediumMG + GunTail_Medium + 11 + Mounted +
  • +
    + +
  • + 80 + 7.8 + AmmoSet_303British +
  • +
    +
    +
    \ No newline at end of file diff --git a/Defs/ThingDefs_Buildings/AutoLoaderExample.xml b/Defs/ThingDefs_Buildings/AutoLoaderExample.xml index 337d45271d..3484c6b900 100644 --- a/Defs/ThingDefs_Buildings/AutoLoaderExample.xml +++ b/Defs/ThingDefs_Buildings/AutoLoaderExample.xml @@ -1,9 +1,9 @@ - - BDsAutoLoaderTest - - An ammo crate with magical autoloaders. + + BDsAutoLoaderTest + + An ammo crate with magical autoloaders. CombatExtended.Building_AutoLoaderCE Normal @@ -42,7 +42,16 @@ - + Things/Building/Misc/ToolCabinet + Graphic_Multi + (2,1) + (0,114,0) + + Damage/Corner + Damage/Corner + Damage/Corner + Damage/Corner + diff --git a/Source/CombatExtended/CombatExtended/Comps/CompAmmoUserGeneric.cs b/Source/CombatExtended/CombatExtended/Comps/CompAmmoUserGeneric.cs index 8f1a5cbaf1..dd98d79e94 100644 --- a/Source/CombatExtended/CombatExtended/Comps/CompAmmoUserGeneric.cs +++ b/Source/CombatExtended/CombatExtended/Comps/CompAmmoUserGeneric.cs @@ -64,14 +64,24 @@ public void RegenSelectedAmmo() } else { - selectedAmmo = CurAmmoSet.ammoTypes.Where(l => l.ammo.ammoClass == currentAmmoInt.ammoClass).First().ammo; + //turned out it was bold to assume similar ammosets always have same ammo types + var tempAmmoLink = CurAmmoSet.ammoTypes.Where(l => l.ammo.ammoClass == currentAmmoInt.ammoClass); + if (tempAmmoLink.Any()) + { + selectedAmmo = tempAmmoLink.First().ammo; + } + else + { + selectedAmmo = CurAmmoSet.ammoTypes.First().ammo; + } } } [Compatibility.Multiplayer.SyncMethod] private void SyncedSelectAmmoSet(AmmoSetDef caliber) { - SelectedAmmoSet = caliber; RegenSelectedAmmo(); + SelectedAmmoSet = caliber; + RegenSelectedAmmo(); } public override IEnumerable CompGetGizmosExtra() diff --git a/Source/CombatExtended/CombatExtended/Jobs/JobGiver_CheckReload.cs b/Source/CombatExtended/CombatExtended/Jobs/JobGiver_CheckReload.cs index 00de2a9ff6..0a5af7ae86 100644 --- a/Source/CombatExtended/CombatExtended/Jobs/JobGiver_CheckReload.cs +++ b/Source/CombatExtended/CombatExtended/Jobs/JobGiver_CheckReload.cs @@ -132,7 +132,7 @@ private bool DoReloadCheck(Pawn pawn, out ThingWithComps reloadWeapon, out AmmoD { // Get key stats of the weapon. tmpComp = gun.TryGetComp(); - AmmoDef ammoType = tmpComp.CurrentAmmo; + AmmoDef ammoType = tmpComp.SelectedAmmo; int ammoAmount = tmpComp.CurMagCount; int magazineSize = tmpComp.MagSize; From 66109b78a1297fdd2ab62eb6c6846119cce29f9b Mon Sep 17 00:00:00 2001 From: CMDR-Bill-Doors Date: Wed, 17 Jul 2024 15:25:49 +0100 Subject: [PATCH 05/11] hide identical ammosets, contained ammo tip hide ammosets with completely identical ammo. Hovering mouse over a caliber shows contained ammo's label --- .../Comps/CompAmmoUserGeneric.cs | 79 ++++++++++++++++++- 1 file changed, 75 insertions(+), 4 deletions(-) diff --git a/Source/CombatExtended/CombatExtended/Comps/CompAmmoUserGeneric.cs b/Source/CombatExtended/CombatExtended/Comps/CompAmmoUserGeneric.cs index dd98d79e94..e792a46783 100644 --- a/Source/CombatExtended/CombatExtended/Comps/CompAmmoUserGeneric.cs +++ b/Source/CombatExtended/CombatExtended/Comps/CompAmmoUserGeneric.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Security.Cryptography; using System.Text; using System.Threading.Tasks; using UnityEngine; @@ -10,13 +11,15 @@ namespace CombatExtended { public class CompAmmoUserGeneric : CompAmmoUser { - private List usableAmmoSets = new List(); + protected List usableAmmoSets = new List(); public AmmoSetDef SelectedAmmoSet; public AmmoSetDef UsedGenericAmmoSet => Props.ammoSet.similarTo ?? Props.ammoSet; - public List UsableAmmoSets + const float margin = 4f; + + public virtual List UsableAmmoSets { get { @@ -28,7 +31,7 @@ public List UsableAmmoSets { continue; } - if (def.similarTo == UsedGenericAmmoSet && !def.ammoTypes.First().ammo.menuHidden) + if (def.similarTo == UsedGenericAmmoSet && !def.ammoTypes.First().ammo.menuHidden && !IsIdenticalToAny(def)) { usableAmmoSets.Add(def); } @@ -38,6 +41,45 @@ public List UsableAmmoSets } } + //Merge ammosets with identical ammo usage. I'm worried about its performance and I might want to caculate this during game start up. + protected bool IsIdenticalToAny(AmmoSetDef def) + { + if (usableAmmoSets.NullOrEmpty()) + { + return false; + } + foreach (var v in usableAmmoSets) + { + if (IsIdenticalTo(v, def)) + { + return true; + } + } + return false; + } + + protected bool IsIdenticalTo(AmmoSetDef a, AmmoSetDef b) + { + if (a.ammoTypes.Count != b.ammoTypes.Count) + { + return false; + } + HashSet list = new HashSet(); + foreach (var v in a.ammoTypes) + { + list.Add(v.ammo); + } + foreach (var n in b.ammoTypes) + { + if (!list.Contains(n.ammo)) + { + return false; + } + } + return true; + } + + public override AmmoSetDef CurAmmoSet => SelectedAmmoSet; public override void PostExposeData() @@ -104,7 +146,12 @@ public override IEnumerable CompGetGizmosExtra() FloatMenuOption item = new FloatMenuOption(caliber.LabelCap, delegate { SyncedSelectAmmoSet(caliber); - }, MenuOptionPriority.Default, null, null); + }, MenuOptionPriority.Default, + delegate (Rect rect) + { + ContainedAmmoPopOut(rect, caliber); + } + , null); list.Add(item); } Find.WindowStack.Add(new FloatMenu(list)); @@ -112,5 +159,29 @@ public override IEnumerable CompGetGizmosExtra() yield return command_Action; } } + + AmmoSetDef currentlyHoveredOverAmmoSet = null; + + string ammoSetContentDescCache; + + public void ContainedAmmoPopOut(Rect rect, AmmoSetDef ammoSet) + { + if (ammoSet != currentlyHoveredOverAmmoSet) + { + BuildAmmosetString(ammoSet); + } + TooltipHandler.TipRegion(rect, ammoSetContentDescCache); + + } + + public void BuildAmmosetString(AmmoSetDef ammoSetDef) + { + StringBuilder stringBuilder = new StringBuilder(); + foreach (var v in ammoSetDef.ammoTypes) + { + stringBuilder.AppendLine(v.ammo.label); + } + ammoSetContentDescCache = stringBuilder.ToString(); + } } } From 574827956e14e8bdaeaddb655edea2c01e696d62 Mon Sep 17 00:00:00 2001 From: CMDR-Bill-Doors Date: Tue, 30 Jul 2024 13:15:20 +0100 Subject: [PATCH 06/11] List based ammo user --- .../CombatExtended/Comps/CompAmmoListUser.cs | 36 ++++ .../Comps/CompAmmoListUserGeneric.cs | 53 ++++++ .../Comps/CompAmmoUserGeneric.cs | 156 +--------------- .../Comps/CompProperties_AmmoListUser.cs | 14 ++ .../Comps/CompVariableAmmoUser.cs | 167 ++++++++++++++++++ 5 files changed, 273 insertions(+), 153 deletions(-) create mode 100644 Source/CombatExtended/CombatExtended/Comps/CompAmmoListUser.cs create mode 100644 Source/CombatExtended/CombatExtended/Comps/CompAmmoListUserGeneric.cs create mode 100644 Source/CombatExtended/CombatExtended/Comps/CompProperties_AmmoListUser.cs create mode 100644 Source/CombatExtended/CombatExtended/Comps/CompVariableAmmoUser.cs diff --git a/Source/CombatExtended/CombatExtended/Comps/CompAmmoListUser.cs b/Source/CombatExtended/CombatExtended/Comps/CompAmmoListUser.cs new file mode 100644 index 0000000000..aec11bece0 --- /dev/null +++ b/Source/CombatExtended/CombatExtended/Comps/CompAmmoListUser.cs @@ -0,0 +1,36 @@ +using System.Collections.Generic; +using System.Linq; +using Verse; + +namespace CombatExtended +{ + public class CompAmmoListUser : CompVariableAmmoUser + { + public new CompProperties_AmmoListUser Props + { + get + { + return (CompProperties_AmmoListUser)props; + } + } + + + public override List UsableAmmoSets + { + get + { + if (usableAmmoSets.NullOrEmpty()) + { + foreach (var def in Props.additionalAmmoSets) + { + if (!def.ammoTypes.First().ammo.menuHidden && !IsIdenticalToAny(def)) + { + usableAmmoSets.Add(def); + } + } + } + return usableAmmoSets; + } + } + } +} diff --git a/Source/CombatExtended/CombatExtended/Comps/CompAmmoListUserGeneric.cs b/Source/CombatExtended/CombatExtended/Comps/CompAmmoListUserGeneric.cs new file mode 100644 index 0000000000..5b3c9857d9 --- /dev/null +++ b/Source/CombatExtended/CombatExtended/Comps/CompAmmoListUserGeneric.cs @@ -0,0 +1,53 @@ +using System.Collections.Generic; +using System.Linq; +using Verse; + +namespace CombatExtended +{ + public class CompAmmoListUserGeneric : CompAmmoUserGeneric + { + public new CompProperties_AmmoListUser Props + { + get + { + return (CompProperties_AmmoListUser)props; + } + } + + + public override List UsableAmmoSets + { + get + { + List tempGenericAmmoSet = new List + { + Props.ammoSet.similarTo ?? Props.ammoSet + }; + foreach (var addAmmo in Props.additionalAmmoSets) + { + AmmoSetDef tempAmmoSet = addAmmo.similarTo ?? addAmmo; + if (!tempGenericAmmoSet.Contains(tempAmmoSet)) + { + tempGenericAmmoSet.Add(tempAmmoSet); + } + } + + if (usableAmmoSets.NullOrEmpty()) + { + foreach (var def in DefDatabase.AllDefs) + { + if (def.similarTo == null) + { + continue; + } + if (tempGenericAmmoSet.Contains(def.similarTo) && !def.ammoTypes.First().ammo.menuHidden && !IsIdenticalToAny(def)) + { + usableAmmoSets.Add(def); + } + } + } + return usableAmmoSets; + } + } + } +} diff --git a/Source/CombatExtended/CombatExtended/Comps/CompAmmoUserGeneric.cs b/Source/CombatExtended/CombatExtended/Comps/CompAmmoUserGeneric.cs index e792a46783..ef76b098c1 100644 --- a/Source/CombatExtended/CombatExtended/Comps/CompAmmoUserGeneric.cs +++ b/Source/CombatExtended/CombatExtended/Comps/CompAmmoUserGeneric.cs @@ -2,24 +2,17 @@ using System.Collections.Generic; using System.Linq; using System.Security.Cryptography; -using System.Text; using System.Threading.Tasks; -using UnityEngine; using Verse; namespace CombatExtended { - public class CompAmmoUserGeneric : CompAmmoUser - { - protected List usableAmmoSets = new List(); - - public AmmoSetDef SelectedAmmoSet; + public class CompAmmoUserGeneric : CompVariableAmmoUser + { public AmmoSetDef UsedGenericAmmoSet => Props.ammoSet.similarTo ?? Props.ammoSet; - const float margin = 4f; - - public virtual List UsableAmmoSets + public override List UsableAmmoSets { get { @@ -40,148 +33,5 @@ public virtual List UsableAmmoSets return usableAmmoSets; } } - - //Merge ammosets with identical ammo usage. I'm worried about its performance and I might want to caculate this during game start up. - protected bool IsIdenticalToAny(AmmoSetDef def) - { - if (usableAmmoSets.NullOrEmpty()) - { - return false; - } - foreach (var v in usableAmmoSets) - { - if (IsIdenticalTo(v, def)) - { - return true; - } - } - return false; - } - - protected bool IsIdenticalTo(AmmoSetDef a, AmmoSetDef b) - { - if (a.ammoTypes.Count != b.ammoTypes.Count) - { - return false; - } - HashSet list = new HashSet(); - foreach (var v in a.ammoTypes) - { - list.Add(v.ammo); - } - foreach (var n in b.ammoTypes) - { - if (!list.Contains(n.ammo)) - { - return false; - } - } - return true; - } - - - public override AmmoSetDef CurAmmoSet => SelectedAmmoSet; - - public override void PostExposeData() - { - base.PostExposeData(); - Scribe_Defs.Look(ref SelectedAmmoSet, "SelectedAmmoSet"); - } - - public override void Initialize(CompProperties vprops) - { - base.Initialize(vprops); - SelectedAmmoSet = Props.ammoSet; - RegenSelectedAmmo(); - } - public void RegenSelectedAmmo() - { - if (currentAmmoInt == null) - { - currentAmmoInt = (AmmoDef)CurAmmoSet.ammoTypes[0].ammo; - } - if (selectedAmmo == null) - { - selectedAmmo = currentAmmoInt; - } - else - { - //turned out it was bold to assume similar ammosets always have same ammo types - var tempAmmoLink = CurAmmoSet.ammoTypes.Where(l => l.ammo.ammoClass == currentAmmoInt.ammoClass); - if (tempAmmoLink.Any()) - { - selectedAmmo = tempAmmoLink.First().ammo; - } - else - { - selectedAmmo = CurAmmoSet.ammoTypes.First().ammo; - } - } - } - - [Compatibility.Multiplayer.SyncMethod] - private void SyncedSelectAmmoSet(AmmoSetDef caliber) - { - SelectedAmmoSet = caliber; - RegenSelectedAmmo(); - } - - public override IEnumerable CompGetGizmosExtra() - { - foreach (var gizmo in base.CompGetGizmosExtra()) - { - yield return gizmo; - } - if (!Controller.settings.GenericAmmo) - { - Command_Action command_Action = new Command_Action(); - command_Action.defaultLabel = "CE_SelectAmmoSet".Translate(); - command_Action.defaultDesc = "CommandSelectMineralToScanForDesc".Translate(); - command_Action.icon = ContentFinder.Get("UI/Buttons/Reload", reportFailure: false); - command_Action.action = delegate - { - List list = new List(); - foreach (AmmoSetDef caliber in UsableAmmoSets) - { - FloatMenuOption item = new FloatMenuOption(caliber.LabelCap, delegate - { - SyncedSelectAmmoSet(caliber); - }, MenuOptionPriority.Default, - delegate (Rect rect) - { - ContainedAmmoPopOut(rect, caliber); - } - , null); - list.Add(item); - } - Find.WindowStack.Add(new FloatMenu(list)); - }; - yield return command_Action; - } - } - - AmmoSetDef currentlyHoveredOverAmmoSet = null; - - string ammoSetContentDescCache; - - public void ContainedAmmoPopOut(Rect rect, AmmoSetDef ammoSet) - { - if (ammoSet != currentlyHoveredOverAmmoSet) - { - BuildAmmosetString(ammoSet); - } - TooltipHandler.TipRegion(rect, ammoSetContentDescCache); - - } - - public void BuildAmmosetString(AmmoSetDef ammoSetDef) - { - StringBuilder stringBuilder = new StringBuilder(); - foreach (var v in ammoSetDef.ammoTypes) - { - stringBuilder.AppendLine(v.ammo.label); - } - ammoSetContentDescCache = stringBuilder.ToString(); - } } } diff --git a/Source/CombatExtended/CombatExtended/Comps/CompProperties_AmmoListUser.cs b/Source/CombatExtended/CombatExtended/Comps/CompProperties_AmmoListUser.cs new file mode 100644 index 0000000000..1cb9f1606a --- /dev/null +++ b/Source/CombatExtended/CombatExtended/Comps/CompProperties_AmmoListUser.cs @@ -0,0 +1,14 @@ +using System.Collections.Generic; + +namespace CombatExtended +{ + public class CompProperties_AmmoListUser : CompProperties_AmmoUser + { + public List additionalAmmoSets = new List(); + + public CompProperties_AmmoListUser() + { + compClass = typeof(CompAmmoUser); + } + } +} diff --git a/Source/CombatExtended/CombatExtended/Comps/CompVariableAmmoUser.cs b/Source/CombatExtended/CombatExtended/Comps/CompVariableAmmoUser.cs new file mode 100644 index 0000000000..5c9e685e80 --- /dev/null +++ b/Source/CombatExtended/CombatExtended/Comps/CompVariableAmmoUser.cs @@ -0,0 +1,167 @@ +using System.Collections.Generic; +using System.Linq; +using System.Text; +using UnityEngine; +using Verse; + +namespace CombatExtended +{ + public abstract class CompVariableAmmoUser : CompAmmoUser + { + protected List usableAmmoSets = new List(); + + public AmmoSetDef SelectedAmmoSet; + + public virtual List UsableAmmoSets + { + get + { + if (usableAmmoSets.NullOrEmpty()) + { + usableAmmoSets.Add(Props.ammoSet); + } + return usableAmmoSets; + } + } + + //Merge ammosets with identical ammo usage. I'm worried about its performance and I might want to caculate this during game start up. + protected bool IsIdenticalToAny(AmmoSetDef def) + { + if (usableAmmoSets.NullOrEmpty()) + { + return false; + } + foreach (var v in usableAmmoSets) + { + if (IsIdenticalTo(v, def)) + { + return true; + } + } + return false; + } + + protected bool IsIdenticalTo(AmmoSetDef a, AmmoSetDef b) + { + if (a.ammoTypes.Count != b.ammoTypes.Count) + { + return false; + } + HashSet list = new HashSet(); + foreach (var v in a.ammoTypes) + { + list.Add(v.ammo); + } + foreach (var n in b.ammoTypes) + { + if (!list.Contains(n.ammo)) + { + return false; + } + } + return true; + } + + + public override AmmoSetDef CurAmmoSet => SelectedAmmoSet; + + public override void PostExposeData() + { + base.PostExposeData(); + Scribe_Defs.Look(ref SelectedAmmoSet, "SelectedAmmoSet"); + } + + public override void Initialize(CompProperties vprops) + { + base.Initialize(vprops); + SelectedAmmoSet = Props.ammoSet; + RegenSelectedAmmo(); + } + public void RegenSelectedAmmo() + { + if (currentAmmoInt == null) + { + currentAmmoInt = (AmmoDef)CurAmmoSet.ammoTypes[0].ammo; + } + if (selectedAmmo == null) + { + selectedAmmo = currentAmmoInt; + } + else + { + //turned out it was bold to assume similar ammosets always have same ammo types + var tempAmmoLink = CurAmmoSet.ammoTypes.Where(l => l.ammo.ammoClass == currentAmmoInt.ammoClass); + if (tempAmmoLink.Any()) + { + selectedAmmo = tempAmmoLink.First().ammo; + } + else + { + selectedAmmo = CurAmmoSet.ammoTypes.First().ammo; + } + } + } + + [Compatibility.Multiplayer.SyncMethod] + private void SyncedSelectAmmoSet(AmmoSetDef caliber) + { + SelectedAmmoSet = caliber; + RegenSelectedAmmo(); + } + + public override IEnumerable CompGetGizmosExtra() + { + foreach (var gizmo in base.CompGetGizmosExtra()) + { + yield return gizmo; + } + if (!Controller.settings.GenericAmmo) + { + Command_Action command_Action = new Command_Action(); + command_Action.defaultLabel = "CE_SelectAmmoSet".Translate(); + command_Action.defaultDesc = "CommandSelectMineralToScanForDesc".Translate(); + command_Action.icon = ContentFinder.Get("UI/Buttons/Reload", reportFailure: false); + command_Action.action = delegate + { + List list = new List(); + foreach (AmmoSetDef caliber in UsableAmmoSets) + { + FloatMenuOption item = new FloatMenuOption( + caliber.LabelCap, + delegate { SyncedSelectAmmoSet(caliber); }, + caliber.ammoTypes.First().ammo, + priority: MenuOptionPriority.Default, + mouseoverGuiAction: delegate (Rect rect) { ContainedAmmoPopOut(rect, caliber); }); + list.Add(item); + } + Find.WindowStack.Add(new FloatMenu(list)); + }; + yield return command_Action; + } + } + + AmmoSetDef currentlyHoveredOverAmmoSet = null; + + string ammoSetContentDescCache; + + public void ContainedAmmoPopOut(Rect rect, AmmoSetDef ammoSet) + { + if (ammoSet != currentlyHoveredOverAmmoSet) + { + BuildAmmosetString(ammoSet); + } + TooltipHandler.TipRegion(rect, ammoSetContentDescCache); + + } + + public void BuildAmmosetString(AmmoSetDef ammoSetDef) + { + StringBuilder stringBuilder = new StringBuilder(); + foreach (var v in ammoSetDef.ammoTypes) + { + stringBuilder.AppendLine(v.ammo.label); + } + ammoSetContentDescCache = stringBuilder.ToString(); + } + } +} From 4eb8f8cac1d42d6ec1f7bf7b3627227499c50fef Mon Sep 17 00:00:00 2001 From: CMDR-Bill-Doors Date: Wed, 28 Aug 2024 06:30:59 +0100 Subject: [PATCH 07/11] removing test defs and edit example def --- Defs/TempTestDef.xml | 177 -------------- .../ThingDefs_Buildings/AutoLoaderExample.xml | 223 ++++++++++-------- 2 files changed, 126 insertions(+), 274 deletions(-) delete mode 100644 Defs/TempTestDef.xml diff --git a/Defs/TempTestDef.xml b/Defs/TempTestDef.xml deleted file mode 100644 index 9e7ac47f58..0000000000 --- a/Defs/TempTestDef.xml +++ /dev/null @@ -1,177 +0,0 @@ - - - - Turret_GenericAmmoUserTest - - 6 - - Things/Building/Turrets/MachineGunBase - - (0.27,0.25,0.27) - (0,0,0) - - - UI/Icons/Turrets/MediumAutoTurret_uiIcon - - 20000 - 200 - 0.6 - 30 - 40 - 0.5 - 0.75 - - Automatic turret equiped with a full powered cartridge machine gun. Fairly resistant to damage. - - 150 - 8 - - - true - Gun_GenericAmmoUserTest - - -
  • PlaceWorker_TurretTop
  • -
  • PlaceWorker_ShowTurretRadius
  • -
    - -
  • - CompPowerTrader - 150 -
  • -
    - MinifiedThing - -
  • GunTurrets
  • -
  • PrecisionRifling
  • -
    -
    - - - Gun_GenericAmmoUserTest - - - Things/Building/Turrets/MediumTurret_Top - Graphic_Single - - Full powered cartridge gun on a turret mount. - - 1 - 0.05 - 0.84 - 0.36 - 10 - - -
  • - 0.95 - CombatExtended.Verb_ShootCE - true - Bullet_762x51mmNATO_FMJ - 1.3 - 55 - 6 - 10 - MediumMG - GunTail_Medium - 11 - Mounted -
  • -
    - -
  • - CombatExtended.CompAmmoUserGeneric - 80 - 7.8 - AmmoSet_762x51mmNATO -
  • -
    -
    - - - Turret_GenericAmmoUserTestII - - 6 - - Things/Building/Turrets/MachineGunBase - - (0.27,0.25,0.27) - (0,0,0) - - - UI/Icons/Turrets/MediumAutoTurret_uiIcon - - 20000 - 200 - 0.6 - 30 - 40 - 0.5 - 0.75 - - Automatic turret equiped with a full powered cartridge machine gun. Fairly resistant to damage. - - 150 - 8 - - - true - Gun_GenericAmmoUserTestII - - -
  • PlaceWorker_TurretTop
  • -
  • PlaceWorker_ShowTurretRadius
  • -
    - -
  • - CompPowerTrader - 150 -
  • -
    - MinifiedThing - -
  • GunTurrets
  • -
  • PrecisionRifling
  • -
    -
    - - - Gun_GenericAmmoUserTestII - - - Things/Building/Turrets/MediumTurret_Top - Graphic_Single - - Full powered cartridge gun on a turret mount. - - 1 - 0.05 - 0.84 - 0.36 - 10 - - -
  • - 0.95 - CombatExtended.Verb_ShootCE - true - Bullet_762x51mmNATO_FMJ - 1.3 - 55 - 6 - 10 - MediumMG - GunTail_Medium - 11 - Mounted -
  • -
    - -
  • - 80 - 7.8 - AmmoSet_303British -
  • -
    -
    -
    \ No newline at end of file diff --git a/Defs/ThingDefs_Buildings/AutoLoaderExample.xml b/Defs/ThingDefs_Buildings/AutoLoaderExample.xml index 3484c6b900..4a95a970f1 100644 --- a/Defs/ThingDefs_Buildings/AutoLoaderExample.xml +++ b/Defs/ThingDefs_Buildings/AutoLoaderExample.xml @@ -1,119 +1,148 @@ - - BDsAutoLoaderTest - - An ammo crate with magical autoloaders. + \ No newline at end of file From 98fd72ab637b4c7b66c295a48e22a3562f78428c Mon Sep 17 00:00:00 2001 From: CMDR-Bill-Doors Date: Mon, 23 Sep 2024 05:51:22 +0100 Subject: [PATCH 08/11] Update CompVariableAmmoUser.cs --- .../CombatExtended/Comps/CompVariableAmmoUser.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Source/CombatExtended/CombatExtended/Comps/CompVariableAmmoUser.cs b/Source/CombatExtended/CombatExtended/Comps/CompVariableAmmoUser.cs index 5c9e685e80..6c960a3a38 100644 --- a/Source/CombatExtended/CombatExtended/Comps/CompVariableAmmoUser.cs +++ b/Source/CombatExtended/CombatExtended/Comps/CompVariableAmmoUser.cs @@ -12,6 +12,10 @@ public abstract class CompVariableAmmoUser : CompAmmoUser public AmmoSetDef SelectedAmmoSet; + private AmmoSetDef currentlyHoveredOverAmmoSet = null; + + private string ammoSetContentDescCache; + public virtual List UsableAmmoSets { get @@ -140,10 +144,6 @@ public override IEnumerable CompGetGizmosExtra() } } - AmmoSetDef currentlyHoveredOverAmmoSet = null; - - string ammoSetContentDescCache; - public void ContainedAmmoPopOut(Rect rect, AmmoSetDef ammoSet) { if (ammoSet != currentlyHoveredOverAmmoSet) From ed8641faf0aff68b10860525ad0e03d9c4cf36c4 Mon Sep 17 00:00:00 2001 From: n7huntsman Date: Sun, 29 Sep 2024 16:09:08 -0400 Subject: [PATCH 09/11] Add translation keys --- Languages/English/Keyed/AmmoContainerKeyed.xml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Languages/English/Keyed/AmmoContainerKeyed.xml b/Languages/English/Keyed/AmmoContainerKeyed.xml index 2231753f1b..7fa39b5522 100644 --- a/Languages/English/Keyed/AmmoContainerKeyed.xml +++ b/Languages/English/Keyed/AmmoContainerKeyed.xml @@ -23,4 +23,7 @@ Autoloader is reloading or being reloaded. + Select ammoset + Select an ammoset to fill the autoloader. + \ No newline at end of file From 83cc2a7202c59a4bb181704412806692773b6093 Mon Sep 17 00:00:00 2001 From: n7huntsman Date: Sun, 29 Sep 2024 16:19:38 -0400 Subject: [PATCH 10/11] Replace placeholder desc text --- .../CombatExtended/CombatExtended/Comps/CompVariableAmmoUser.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Source/CombatExtended/CombatExtended/Comps/CompVariableAmmoUser.cs b/Source/CombatExtended/CombatExtended/Comps/CompVariableAmmoUser.cs index 6c960a3a38..dcf5e783ab 100644 --- a/Source/CombatExtended/CombatExtended/Comps/CompVariableAmmoUser.cs +++ b/Source/CombatExtended/CombatExtended/Comps/CompVariableAmmoUser.cs @@ -123,7 +123,7 @@ public override IEnumerable CompGetGizmosExtra() { Command_Action command_Action = new Command_Action(); command_Action.defaultLabel = "CE_SelectAmmoSet".Translate(); - command_Action.defaultDesc = "CommandSelectMineralToScanForDesc".Translate(); + command_Action.defaultDesc = "CE_SelectAmmoSetDesc".Translate(); command_Action.icon = ContentFinder.Get("UI/Buttons/Reload", reportFailure: false); command_Action.action = delegate { From 117f621c8e1a1d8a3ad41eae6f025d1bfd8786de Mon Sep 17 00:00:00 2001 From: mszabo Date: Sun, 8 Dec 2024 16:23:19 +0100 Subject: [PATCH 11/11] Simplify generic ammo handling for autoloaders --- .../CombatExtended/Comps/CompAmmoListUser.cs | 85 ++++++++++++++++++- .../Comps/CompAmmoListUserGeneric.cs | 53 ------------ .../Comps/CompAmmoUserGeneric.cs | 37 -------- .../Comps/CompProperties_AmmoListUser.cs | 2 +- .../Comps/CompVariableAmmoUser.cs | 79 +++++------------ 5 files changed, 103 insertions(+), 153 deletions(-) delete mode 100644 Source/CombatExtended/CombatExtended/Comps/CompAmmoListUserGeneric.cs delete mode 100644 Source/CombatExtended/CombatExtended/Comps/CompAmmoUserGeneric.cs diff --git a/Source/CombatExtended/CombatExtended/Comps/CompAmmoListUser.cs b/Source/CombatExtended/CombatExtended/Comps/CompAmmoListUser.cs index aec11bece0..bf9797a0db 100644 --- a/Source/CombatExtended/CombatExtended/Comps/CompAmmoListUser.cs +++ b/Source/CombatExtended/CombatExtended/Comps/CompAmmoListUser.cs @@ -21,9 +21,44 @@ public override List UsableAmmoSets { if (usableAmmoSets.NullOrEmpty()) { - foreach (var def in Props.additionalAmmoSets) + var allowedAmmoSets = new List { Props.ammoSet }; + allowedAmmoSets.AddRange(Props.additionalAmmoSets); + + // Generalize allowed ammosets in generic ammo mode, unless they were already a generic ammoset. + if (Controller.settings.GenericAmmo) + { + foreach (var def in allowedAmmoSets.Select(def => def.similarTo ?? def)) + { + if (IsUsableAmmoSet(def)) + { + usableAmmoSets.Add(def); + } + } + + return usableAmmoSets; + } + + foreach (var def in allowedAmmoSets) { - if (!def.ammoTypes.First().ammo.menuHidden && !IsIdenticalToAny(def)) + var similarAmmoSets = DefDatabase.AllDefs + .Where(other => other.similarTo == def) + .ToList(); + + // Allow specifying a generic ammoset directly, and allow its constituent ammosets in this case. + if (similarAmmoSets.Any()) + { + foreach (var similarAmmoSet in similarAmmoSets) + { + if (IsUsableAmmoSet(similarAmmoSet)) + { + usableAmmoSets.Add(similarAmmoSet); + } + } + + continue; + } + + if (IsUsableAmmoSet(def)) { usableAmmoSets.Add(def); } @@ -32,5 +67,51 @@ public override List UsableAmmoSets return usableAmmoSets; } } + + + //Merge ammosets with identical ammo usage. I'm worried about its performance and I might want to caculate this during game start up. + private bool IsUsableAmmoSet(AmmoSetDef def) + { + if (def.ammoTypes.First().ammo.menuHidden) + { + return false; + } + + if (usableAmmoSets.NullOrEmpty()) + { + return true; + } + + foreach (var v in usableAmmoSets) + { + if (IsIdenticalTo(v, def)) + { + return false; + } + } + + return true; + } + + private bool IsIdenticalTo(AmmoSetDef a, AmmoSetDef b) + { + if (a.ammoTypes.Count != b.ammoTypes.Count) + { + return false; + } + HashSet list = new HashSet(); + foreach (var v in a.ammoTypes) + { + list.Add(v.ammo); + } + foreach (var n in b.ammoTypes) + { + if (!list.Contains(n.ammo)) + { + return false; + } + } + return true; + } } } diff --git a/Source/CombatExtended/CombatExtended/Comps/CompAmmoListUserGeneric.cs b/Source/CombatExtended/CombatExtended/Comps/CompAmmoListUserGeneric.cs deleted file mode 100644 index 5b3c9857d9..0000000000 --- a/Source/CombatExtended/CombatExtended/Comps/CompAmmoListUserGeneric.cs +++ /dev/null @@ -1,53 +0,0 @@ -using System.Collections.Generic; -using System.Linq; -using Verse; - -namespace CombatExtended -{ - public class CompAmmoListUserGeneric : CompAmmoUserGeneric - { - public new CompProperties_AmmoListUser Props - { - get - { - return (CompProperties_AmmoListUser)props; - } - } - - - public override List UsableAmmoSets - { - get - { - List tempGenericAmmoSet = new List - { - Props.ammoSet.similarTo ?? Props.ammoSet - }; - foreach (var addAmmo in Props.additionalAmmoSets) - { - AmmoSetDef tempAmmoSet = addAmmo.similarTo ?? addAmmo; - if (!tempGenericAmmoSet.Contains(tempAmmoSet)) - { - tempGenericAmmoSet.Add(tempAmmoSet); - } - } - - if (usableAmmoSets.NullOrEmpty()) - { - foreach (var def in DefDatabase.AllDefs) - { - if (def.similarTo == null) - { - continue; - } - if (tempGenericAmmoSet.Contains(def.similarTo) && !def.ammoTypes.First().ammo.menuHidden && !IsIdenticalToAny(def)) - { - usableAmmoSets.Add(def); - } - } - } - return usableAmmoSets; - } - } - } -} diff --git a/Source/CombatExtended/CombatExtended/Comps/CompAmmoUserGeneric.cs b/Source/CombatExtended/CombatExtended/Comps/CompAmmoUserGeneric.cs deleted file mode 100644 index ef76b098c1..0000000000 --- a/Source/CombatExtended/CombatExtended/Comps/CompAmmoUserGeneric.cs +++ /dev/null @@ -1,37 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Security.Cryptography; -using System.Threading.Tasks; -using Verse; - -namespace CombatExtended -{ - - public class CompAmmoUserGeneric : CompVariableAmmoUser - { - public AmmoSetDef UsedGenericAmmoSet => Props.ammoSet.similarTo ?? Props.ammoSet; - - public override List UsableAmmoSets - { - get - { - if (usableAmmoSets.NullOrEmpty()) - { - foreach (var def in DefDatabase.AllDefs) - { - if (def.similarTo == null) - { - continue; - } - if (def.similarTo == UsedGenericAmmoSet && !def.ammoTypes.First().ammo.menuHidden && !IsIdenticalToAny(def)) - { - usableAmmoSets.Add(def); - } - } - } - return usableAmmoSets; - } - } - } -} diff --git a/Source/CombatExtended/CombatExtended/Comps/CompProperties_AmmoListUser.cs b/Source/CombatExtended/CombatExtended/Comps/CompProperties_AmmoListUser.cs index 1cb9f1606a..f913b7cde9 100644 --- a/Source/CombatExtended/CombatExtended/Comps/CompProperties_AmmoListUser.cs +++ b/Source/CombatExtended/CombatExtended/Comps/CompProperties_AmmoListUser.cs @@ -8,7 +8,7 @@ public class CompProperties_AmmoListUser : CompProperties_AmmoUser public CompProperties_AmmoListUser() { - compClass = typeof(CompAmmoUser); + compClass = typeof(CompAmmoListUser); } } } diff --git a/Source/CombatExtended/CombatExtended/Comps/CompVariableAmmoUser.cs b/Source/CombatExtended/CombatExtended/Comps/CompVariableAmmoUser.cs index dcf5e783ab..c6316e5f21 100644 --- a/Source/CombatExtended/CombatExtended/Comps/CompVariableAmmoUser.cs +++ b/Source/CombatExtended/CombatExtended/Comps/CompVariableAmmoUser.cs @@ -28,45 +28,6 @@ public virtual List UsableAmmoSets } } - //Merge ammosets with identical ammo usage. I'm worried about its performance and I might want to caculate this during game start up. - protected bool IsIdenticalToAny(AmmoSetDef def) - { - if (usableAmmoSets.NullOrEmpty()) - { - return false; - } - foreach (var v in usableAmmoSets) - { - if (IsIdenticalTo(v, def)) - { - return true; - } - } - return false; - } - - protected bool IsIdenticalTo(AmmoSetDef a, AmmoSetDef b) - { - if (a.ammoTypes.Count != b.ammoTypes.Count) - { - return false; - } - HashSet list = new HashSet(); - foreach (var v in a.ammoTypes) - { - list.Add(v.ammo); - } - foreach (var n in b.ammoTypes) - { - if (!list.Contains(n.ammo)) - { - return false; - } - } - return true; - } - - public override AmmoSetDef CurAmmoSet => SelectedAmmoSet; public override void PostExposeData() @@ -119,29 +80,27 @@ public override IEnumerable CompGetGizmosExtra() { yield return gizmo; } - if (!Controller.settings.GenericAmmo) + + Command_Action command_Action = new Command_Action(); + command_Action.defaultLabel = "CE_SelectAmmoSet".Translate(); + command_Action.defaultDesc = "CE_SelectAmmoSetDesc".Translate(); + command_Action.icon = ContentFinder.Get("UI/Buttons/Reload", reportFailure: false); + command_Action.action = delegate { - Command_Action command_Action = new Command_Action(); - command_Action.defaultLabel = "CE_SelectAmmoSet".Translate(); - command_Action.defaultDesc = "CE_SelectAmmoSetDesc".Translate(); - command_Action.icon = ContentFinder.Get("UI/Buttons/Reload", reportFailure: false); - command_Action.action = delegate + List list = new List(); + foreach (AmmoSetDef caliber in UsableAmmoSets) { - List list = new List(); - foreach (AmmoSetDef caliber in UsableAmmoSets) - { - FloatMenuOption item = new FloatMenuOption( - caliber.LabelCap, - delegate { SyncedSelectAmmoSet(caliber); }, - caliber.ammoTypes.First().ammo, - priority: MenuOptionPriority.Default, - mouseoverGuiAction: delegate (Rect rect) { ContainedAmmoPopOut(rect, caliber); }); - list.Add(item); - } - Find.WindowStack.Add(new FloatMenu(list)); - }; - yield return command_Action; - } + FloatMenuOption item = new FloatMenuOption( + caliber.LabelCap, + delegate { SyncedSelectAmmoSet(caliber); }, + caliber.ammoTypes.First().ammo, + priority: MenuOptionPriority.Default, + mouseoverGuiAction: delegate (Rect rect) { ContainedAmmoPopOut(rect, caliber); }); + list.Add(item); + } + Find.WindowStack.Add(new FloatMenu(list)); + }; + yield return command_Action; } public void ContainedAmmoPopOut(Rect rect, AmmoSetDef ammoSet)