Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

List based ammo user #3317

Open
wants to merge 15 commits into
base: Development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions Defs/ThingDefs_Buildings/AutoLoaderExample.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<Defs>
<!--
<!--

===Auto loader example def

Expand All @@ -18,11 +18,15 @@
===This Ammo user is the autoloader's ammo capacity and reload time===

<comps>
<li Class="CombatExtended.CompProperties_AmmoUser">
<magazineSize>400</magazineSize>
<reloadTime>7.8</reloadTime>
<ammoSet>AmmoSet_145x114mm</ammoSet>
</li>
<li Class="CombatExtended.CompProperties_AmmoListUser">
<compClass>CombatExtended.CompAmmoListUserGeneric</compClass>
<magazineSize>400</magazineSize>
<reloadTime>7.8</reloadTime>
<ammoSet>AmmoSet_762x51mmNATO</ammoSet>
<additionalAmmoSets>
<li>AmmoSet_44Magnum</li>
</additionalAmmoSets>
</li>

===If CompMannable and CompPowerTrader are present, they'll be taken into account too, same goes for CompInitiatable and CompCanBeDormant===

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,14 +107,14 @@ 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)
{
base.SpawnSetup(map, respawningAfterLoad);
Map.GetComponent<AutoLoaderTracker>().Register(this);
CompAmmoUser = GetComp<CompAmmoUser>();
CompAmmoUser = this.TryGetComp<CompAmmoUser>();

dormantComp = GetComp<CompCanBeDormant>();
initiatableComp = GetComp<CompInitiatable>();
Expand Down Expand Up @@ -252,6 +252,10 @@ public override IEnumerable<Gizmo> GetGizmos()
Messages.Message(string.Format("CE_AutoLoader_NoTurretToReload".Translate(), Label, CompAmmoUser.Props.ammoSet.label), this, MessageTypeDefOf.RejectInput, historical: false);
}
}
if (!success)
{
Messages.Message(string.Format("CE_AutoLoader_NoTurretToReload".Translate(), Label, CompAmmoUser.CurAmmoSet.label), this, MessageTypeDefOf.RejectInput, historical: false);
}
};
yield return reload;
}
Expand Down Expand Up @@ -375,18 +379,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;
}
}
}
}
Expand Down
36 changes: 36 additions & 0 deletions Source/CombatExtended/CombatExtended/Comps/CompAmmoListUser.cs
Original file line number Diff line number Diff line change
@@ -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<AmmoSetDef> UsableAmmoSets
{
get
{
if (usableAmmoSets.NullOrEmpty())
{
foreach (var def in Props.additionalAmmoSets)
{
if (!def.ammoTypes.First().ammo.menuHidden && !IsIdenticalToAny(def))
{
usableAmmoSets.Add(def);
}
}
}
return usableAmmoSets;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -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<AmmoSetDef> UsableAmmoSets
{
get
{
List<AmmoSetDef> tempGenericAmmoSet = new List<AmmoSetDef>
{
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<AmmoSetDef>.AllDefs)
{
if (def.similarTo == null)
{
continue;
}
if (tempGenericAmmoSet.Contains(def.similarTo) && !def.ammoTypes.First().ammo.menuHidden && !IsIdenticalToAny(def))
{
usableAmmoSets.Add(def);
}
}
}
return usableAmmoSets;
}
}
}
}
28 changes: 15 additions & 13 deletions Source/CombatExtended/CombatExtended/Comps/CompAmmoUser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);
}
Expand Down Expand Up @@ -139,7 +139,7 @@ public bool UseAmmo
{
get
{
return Props.ammoSet != null && AmmoUtility.IsAmmoSystemActive(Props.ammoSet);
return CurAmmoSet != null && AmmoUtility.IsAmmoSystemActive(CurAmmoSet);
}
}
public bool IsAOEWeapon
Expand Down Expand Up @@ -174,7 +174,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;
Expand Down Expand Up @@ -219,7 +219,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
Expand Down Expand Up @@ -269,7 +271,7 @@ private Map Map
}
public bool ShouldThrowMote => Props.throwMote && MagSize > 1;

public AmmoDef SelectedAmmo
public virtual AmmoDef SelectedAmmo
{
get
{
Expand Down Expand Up @@ -299,15 +301,15 @@ 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");
}
else
{
if (currentAmmoInt == null)
{
currentAmmoInt = (AmmoDef)Props.ammoSet.ammoTypes[0].ammo;
currentAmmoInt = (AmmoDef)CurAmmoSet.ammoTypes[0].ammo;
}
if (selectedAmmo == null)
{
Expand Down Expand Up @@ -683,7 +685,7 @@ public bool TryPickupAmmo()
{
return false;
}
IEnumerable<AmmoDef> supportedAmmo = Props.ammoSet.ammoTypes.Select(a => a.ammo);
IEnumerable<AmmoDef> 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)))))
Expand Down Expand Up @@ -843,7 +845,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)
Expand Down Expand Up @@ -939,7 +941,7 @@ public override IEnumerable<Gizmo> CompGetGizmosExtra()

public override string TransformLabel(string label)
{
string ammoSet = UseAmmo && Controller.settings.ShowCaliberOnGuns ? " (" + (string)Props.ammoSet.LabelCap + ") " : "";
string ammoSet = UseAmmo && Controller.settings.ShowCaliberOnGuns ? " (" + (string)CurAmmoSet.LabelCap + ") " : "";
return label + ammoSet;
}

Expand Down
37 changes: 37 additions & 0 deletions Source/CombatExtended/CombatExtended/Comps/CompAmmoUserGeneric.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
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<AmmoSetDef> UsableAmmoSets
{
get
{
if (usableAmmoSets.NullOrEmpty())
{
foreach (var def in DefDatabase<AmmoSetDef>.AllDefs)
{
if (def.similarTo == null)
{
continue;
}
if (def.similarTo == UsedGenericAmmoSet && !def.ammoTypes.First().ammo.menuHidden && !IsIdenticalToAny(def))
{
usableAmmoSets.Add(def);
}
}
}
return usableAmmoSets;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System.Collections.Generic;

namespace CombatExtended
{
public class CompProperties_AmmoListUser : CompProperties_AmmoUser
{
public List<AmmoSetDef> additionalAmmoSets = new List<AmmoSetDef>();

public CompProperties_AmmoListUser()
{
compClass = typeof(CompAmmoUser);
}
}
}
Loading
Loading