use of megamek.common.AmmoType in project megameklab by MegaMek.
the class BayWeaponCriticalTree method removeEquipment.
/**
* Removes equipment node.
*
* @param node The node to remove
* @param shouldRefresh If false, will not trigger refreshes. This is used when removing
* all equipment in a bay to hold the refresh until the end.
* @param updateMount If true, the mount location will be set to LOC_NONE. The transfer handler
* takes care of this separately to keep from unallocating equipment that
* has been transferred to another bay.
*/
private void removeEquipment(final EquipmentNode node, boolean shouldRefresh, boolean updateMount) {
model.removeNodeFromParent(node);
setRootVisible(((TreeNode) model.getRoot()).getChildCount() == 0);
final Mounted mounted = node.getMounted();
// process of removing a bay.
if ((node.getParent() instanceof BayNode) && (node.getParent().getParent() != null)) {
Mounted bay = ((BayNode) node.getParent()).getMounted();
if (mounted.getType() instanceof WeaponType) {
bay.getBayWeapons().removeElement(eSource.getEntity().getEquipmentNum(mounted));
} else if (mounted.getType() instanceof AmmoType) {
bay.getBayAmmo().removeElement(eSource.getEntity().getEquipmentNum(mounted));
}
if ((node.getMounted().getType() instanceof WeaponType) && bay.getBayWeapons().size() == 0) {
removeBay((EquipmentNode) node.getParent(), false, true);
}
}
if (updateMount) {
Mounted moveTo = null;
if (mounted.getType() instanceof AmmoType) {
moveTo = UnitUtil.findUnallocatedAmmo(eSource.getEntity(), mounted.getType());
if (null != moveTo) {
moveTo.setShotsLeft(moveTo.getBaseShotsLeft() + mounted.getBaseShotsLeft());
UnitUtil.removeCriticals(eSource.getEntity(), mounted);
UnitUtil.removeMounted(eSource.getEntity(), mounted);
}
}
if (null == moveTo) {
UnitUtil.removeCriticals(eSource.getEntity(), mounted);
UnitUtil.changeMountStatus(eSource.getEntity(), mounted, Entity.LOC_NONE, Entity.LOC_NONE, false);
if ((node.getMounted().getType() instanceof WeaponType) && (node.getMounted().getLinkedBy() != null)) {
UnitUtil.removeCriticals(eSource.getEntity(), node.getMounted().getLinkedBy());
UnitUtil.changeMountStatus(eSource.getEntity(), node.getMounted().getLinkedBy(), Entity.LOC_NONE, Entity.LOC_NONE, false);
}
}
UnitUtil.compactCriticals(eSource.getEntity());
}
if (shouldRefresh) {
refresh.refreshEquipment();
refresh.refreshBuild();
refresh.refreshPreview();
refresh.refreshStatus();
refresh.refreshSummary();
}
}
use of megamek.common.AmmoType in project megameklab by MegaMek.
the class BayWeaponCriticalTree method addAmmoToBay.
public void addAmmoToBay(Mounted bay, Mounted eq, int shots) {
AmmoType at = (AmmoType) eq.getType();
eq.setShotsLeft(eq.getBaseShotsLeft() - shots);
if (eq.getBaseShotsLeft() <= 0) {
UnitUtil.removeMounted(eSource.getEntity(), eq);
}
Optional<Mounted> addMount = bay.getBayAmmo().stream().map(n -> eSource.getEntity().getEquipment(n)).filter(m -> m.getType() == at).findFirst();
if (addMount.isPresent()) {
addMount.get().setShotsLeft(addMount.get().getBaseShotsLeft() + shots);
refresh.refreshEquipment();
refresh.refreshBuild();
refresh.refreshPreview();
refresh.refreshStatus();
refresh.refreshSummary();
} else {
try {
Mounted m = eSource.getEntity().addEquipment(at, bay.getLocation());
m.setShotsLeft(shots);
addToBay(bay, m);
} catch (LocationFullException e) {
}
}
}
use of megamek.common.AmmoType in project megameklab by MegaMek.
the class BayWeaponCriticalTree method removeAmmo.
/**
* Moves one or more shots of ammo from this location to unallocated. This should not be used if
* there is only one slot of ammo left in the location;
* use {@link #removeEquipment(EquipmentNode) removeEquipment}
* instead.
*
* @param ammo The allocated ammo to remove.
* @param shots The number of shots to remove.
*/
private void removeAmmo(final Mounted ammo, int shots) {
AmmoType at = (AmmoType) ammo.getType();
ammo.setShotsLeft(ammo.getBaseShotsLeft() - shots);
Mounted unallocated = UnitUtil.findUnallocatedAmmo(eSource.getEntity(), at);
for (Mounted m : eSource.getEntity().getAmmo()) {
if ((m.getLocation() == Entity.LOC_NONE) && (m.getType() == at)) {
unallocated = m;
break;
}
}
if (null != unallocated) {
unallocated.setShotsLeft(ammo.getBaseShotsLeft() + shots);
} else {
unallocated = new Mounted(eSource.getEntity(), at);
unallocated.setShotsLeft(shots);
try {
eSource.getEntity().addEquipment(unallocated, Entity.LOC_NONE, false);
} catch (LocationFullException e) {
}
}
refresh.refreshEquipment();
refresh.refreshBuild();
refresh.refreshPreview();
refresh.refreshStatus();
refresh.refreshSummary();
}
use of megamek.common.AmmoType in project megameklab by MegaMek.
the class BayWeaponCriticalTree method isValidDropLocation.
/**
* Determines whether the equipment can be dropped here. In the case of ammo or weapon
* enhancement, there must be a matching weapon in the bay. A weapon that doesn't fit the
* bay will be placed in a new bay.
*
* @param loc The drop location from the TransferSupport object passed to the TransferHandler
* @param eq The equipment to be dropped
* @return Whether the equipment can be dropped in the location
*/
public boolean isValidDropLocation(JTree.DropLocation loc, Mounted eq) {
if (!eSource.getEntity().usesWeaponBays()) {
return true;
}
Mounted bay = null;
TreePath path = loc.getPath();
if (null != path) {
// without a capacitor.
if ((eq.getType() instanceof MiscType) && eq.getType().hasFlag(MiscType.F_PPC_CAPACITOR) && (path.getLastPathComponent() instanceof EquipmentNode)) {
EquipmentNode node = (EquipmentNode) path.getLastPathComponent();
if (node.getMounted().getType() instanceof PPCWeapon) {
return node.getMounted().getLinkedBy() == null;
} else if (node.getMounted().getType() instanceof PPCBayWeapon) {
for (Integer eqNum : node.getMounted().getBayWeapons()) {
if (eSource.getEntity().getEquipment(eqNum).getLinkedBy() == null) {
return true;
}
}
return false;
}
}
bay = getBayFromPath(path);
}
// disallow dropping a bay into its current arc or a weapon or ammo into its current bay
if (null != bay) {
if ((eq == bay) || ((eq.getType() instanceof WeaponType) && (bay.getBayWeapons().contains(eSource.getEntity().getEquipmentNum(eq)))) || ((eq.getType() instanceof AmmoType) && (bay.getBayAmmo().contains(eSource.getEntity().getEquipmentNum(eq))))) {
return false;
}
}
if (eq.getType() instanceof AmmoType) {
return (null != bay) && canTakeEquipment(bay, eq);
}
if (UnitUtil.isWeaponEnhancement(eq.getType())) {
if ((null != bay) && canTakeEquipment(bay, eq)) {
for (Integer eqNum : bay.getBayWeapons()) {
if (eSource.getEntity().getEquipment(eqNum) == eq.getLinked()) {
return false;
}
}
return true;
}
return false;
}
return true;
}
Aggregations