Skip to content

Commit

Permalink
Merge pull request #1350 from SJuliez/1334-remove-only-specific-OS-ammo
Browse files Browse the repository at this point in the history
Issue #1334: remove only specific OS/Infantry ammo
  • Loading branch information
neoancient authored Dec 26, 2023
2 parents fa3a351 + df62d12 commit c95e9cf
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,7 @@ protected void addEquipment(EquipmentType equip, int count) {
loc = Tank.LOC_BODY;
}
getTank().addEquipment(mount, loc, false);
if ((equip instanceof WeaponType) && equip.hasFlag(WeaponType.F_ONESHOT)) {
UnitUtil.removeOneShotAmmo(eSource.getEntity());
}
UnitUtil.removeHiddenAmmo(mount);
} catch (LocationFullException ignored) {
// this can't happen, we add to Entity.LOC_NONE
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,7 @@ protected void addEquipment(EquipmentType equip, int count) {
UnitUtil.setVariableSizeMiscTypeMinimumSize(mount);
int location = (equip instanceof AmmoType) ? Aero.LOC_FUSELAGE : Aero.LOC_NONE;
getAero().addEquipment(mount, location, false);
if ((equip instanceof WeaponType) && equip.hasFlag(WeaponType.F_ONESHOT)) {
UnitUtil.removeOneShotAmmo(getAero());
}
UnitUtil.removeHiddenAmmo(mount);
} catch (LocationFullException ignored) {
// location maximum is currently checked in menus and dragndrop
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,7 @@ protected void addEquipment(EquipmentType equip, int count) {
mount = new Mounted(getAero(), equip);
UnitUtil.setVariableSizeMiscTypeMinimumSize(mount);
getAero().addEquipment(mount, Entity.LOC_NONE, false);
if ((equip instanceof WeaponType) && equip.hasFlag(WeaponType.F_ONESHOT)) {
UnitUtil.removeOneShotAmmo(eSource.getEntity());
}
UnitUtil.removeHiddenAmmo(mount);
}
} catch (LocationFullException ignored) {
// Shouldn't happen when adding to LOC_NONE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,7 @@ protected void addEquipment(EquipmentType equip, int count) {
mount = new Mounted(getMech(), equip);
UnitUtil.setVariableSizeMiscTypeMinimumSize(mount);
getMech().addEquipment(mount, Entity.LOC_NONE, false);
if ((equip instanceof WeaponType) && equip.hasFlag(WeaponType.F_ONESHOT)) {
UnitUtil.removeOneShotAmmo(eSource.getEntity());
}
UnitUtil.removeHiddenAmmo(mount);
} catch (LocationFullException ignored) {
// this can't happen, we add to Entity.LOC_NONE
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,18 +49,15 @@ protected void addEquipment(EquipmentType equip, int count) {
if ((equip instanceof MiscType) && UnitUtil.isFixedLocationSpreadEquipment(equip)) {
int location = TestEntity.getSystemWideLocation(eSource.getEntity());
Mounted mount = new Mounted(eSource.getEntity(), equip);
eSource.getEntity().addEquipment(mount, location, false);
getEntity().addEquipment(mount, location, false);
} else {
if (equip instanceof AmmoType) {
addProtomechAmmo(equip, 1);
return;
}
Mounted mount = new Mounted(eSource.getEntity(), equip);
eSource.getEntity().addEquipment(mount, Entity.LOC_NONE, false);
if ((equip instanceof WeaponType) && (equip.hasFlag(WeaponType.F_ONESHOT)
|| (((WeaponType) equip).getAmmoType() == AmmoType.T_INFANTRY))) {
UnitUtil.removeOneShotAmmo(eSource.getEntity());
}
getEntity().addEquipment(mount, Entity.LOC_NONE, false);
UnitUtil.removeHiddenAmmo(mount);
}
} catch (LocationFullException ex) {
LogManager.getLogger().error("Location full while trying to add " + equip.getName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,40 +49,36 @@ protected void addEquipment(EquipmentType equip, int count) {
boolean isMisc = equip instanceof MiscType;
try {
if (isMisc && equip.hasFlag(MiscType.F_TARGCOMP)) {
if (!UnitUtil.hasTargComp(eSource.getEntity())) {
UnitUtil.updateTC(eSource.getEntity(), equip);
if (!UnitUtil.hasTargComp(getEntity())) {
UnitUtil.updateTC(getEntity(), equip);
}
} else if (isMisc && UnitUtil.isFixedLocationSpreadEquipment(equip)) {
int location = TestEntity.getSystemWideLocation(eSource.getEntity());
mount = new Mounted(eSource.getEntity(), equip);
eSource.getEntity().addEquipment(mount, location, false);
int location = TestEntity.getSystemWideLocation(getEntity());
mount = new Mounted(getEntity(), equip);
getEntity().addEquipment(mount, location, false);
} else {
if (equip instanceof AmmoType) {
if (eSource.getEntity().usesWeaponBays()) {
if (getEntity().usesWeaponBays()) {
addLargeCraftAmmo(equip, count);
return;
} else if (eSource.getEntity().isAero()) {
} else if (getEntity().isAero()) {
addBodyAmmo(equip, count, Aero.LOC_FUSELAGE);
return;
} else if (eSource.getEntity() instanceof Tank) {
} else if (getEntity() instanceof Tank) {
addBodyAmmo(equip, count, Tank.LOC_BODY);
return;
}
}
for (int i = 0; i < count; i++) {
mount = new Mounted(eSource.getEntity(), equip);
mount = new Mounted(getEntity(), equip);
UnitUtil.setVariableSizeMiscTypeMinimumSize(mount);
if ((eSource.getEntity().isFighter()
if ((getEntity().isFighter()
&& (equip instanceof MiscType)) && equip.hasFlag(MiscType.F_BLUE_SHIELD)) {
getAero().addEquipment(mount, Aero.LOC_FUSELAGE, false);
} else {
eSource.getEntity().addEquipment(mount, Entity.LOC_NONE, false);
}

if ((equip instanceof WeaponType) && (equip.hasFlag(WeaponType.F_ONESHOT)
|| (((WeaponType) equip).getAmmoType() == AmmoType.T_INFANTRY))) {
UnitUtil.removeOneShotAmmo(eSource.getEntity());
getEntity().addEquipment(mount, Entity.LOC_NONE, false);
}
UnitUtil.removeHiddenAmmo(mount);
}
}
} catch (LocationFullException ex) {
Expand Down
25 changes: 24 additions & 1 deletion megameklab/src/megameklab/util/UnitUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -3275,7 +3275,7 @@ public static void copyLocationEquipment(Entity entity, int fromLoc, int toLoc)

/**
* Makes the equipment mounted in one location identical to that in another location. Any equipment
* previously in the target location that is does not match the source location is removed and
* previously in the target location that does not match the source location is removed and
* assigned to Entity.LOC_NONE. This does not handle split location equipment.
*
* @param entity The unit being modified
Expand Down Expand Up @@ -3391,9 +3391,32 @@ private static Mounted copyEquipment(Entity entity, int toLoc, Mounted toCopy, L
}
entity.addEquipment(toAdd, toLoc, toCopy.isRearMounted());
changeMountStatus(entity, toAdd, toLoc, Entity.LOC_NONE, toCopy.isRearMounted());
removeHiddenAmmo(toAdd);
return toAdd;
}

/**
* If the given Mounted is a one-shot launcher or infantry weapon, this method removes the hidden
* ammo linked to it, if any. During construction, we have no use of hidden ammo. Cannot
* use {@link #removeOneShotAmmo(Entity)} here as it removes all ammo that has
* no location (which is how hidden ammo is kept when a unit is loaded from file) but during construction
* normal ammo may not yet have been allocated and also have no location.
*
* @param mounted The weapon to remove linked hidden ammo
*/
public static void removeHiddenAmmo(Mounted mounted) {
EquipmentType launcherType = mounted.getType();
if ((launcherType instanceof WeaponType) && (launcherType.hasFlag(WeaponType.F_ONESHOT)
|| (((WeaponType) launcherType).getAmmoType() == AmmoType.T_INFANTRY))) {
Mounted oneShotAmmo = mounted.getLinked();
if (oneShotAmmo != null) {
mounted.getEntity().getEquipment().remove(oneShotAmmo);
mounted.getEntity().getAmmo().remove(oneShotAmmo);
mounted.setLinked(null);
}
}
}

/**
* Checks whether the space has room for the equipment within the slot and weight limits.
*
Expand Down

0 comments on commit c95e9cf

Please sign in to comment.