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

Fix 5596: Fix range updates and projections in space #5598

Merged
Show file tree
Hide file tree
Changes from 1 commit
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
9 changes: 9 additions & 0 deletions megamek/src/megamek/client/ui/swing/ClientGUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
import megamek.common.actions.WeaponAttackAction;
import megamek.common.annotations.Nullable;
import megamek.common.enums.GamePhase;
import megamek.common.equipment.AmmoMounted;
import megamek.common.equipment.WeaponMounted;
import megamek.common.event.*;
import megamek.common.icons.Camouflage;
Expand Down Expand Up @@ -2918,6 +2919,14 @@ public Optional<WeaponMounted> getDisplayedWeapon() {
}
}

public Optional<AmmoMounted> getDisplayedAmmo() {
AmmoMounted ammo = unitDisplay.wPan.getSelectedAmmo();
if (null == ammo) {
Sleet01 marked this conversation as resolved.
Show resolved Hide resolved
return Optional.empty();
}
return Optional.of(ammo);
}

@Override
public void weaponSelected(MechDisplayEvent b) {
setSelectedEntityNum(b.getEntityId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import megamek.client.ui.swing.*;
import megamek.common.*;
import megamek.common.annotations.Nullable;
import megamek.common.equipment.AmmoMounted;
import megamek.common.equipment.WeaponMounted;
import megamek.common.options.OptionsConstants;
import megamek.common.preference.IPreferenceChangeListener;
Expand Down Expand Up @@ -300,13 +301,20 @@ private void updateFacing(WeaponMounted weapon, int assumedFacing) {

private void findRanges(WeaponMounted weapon) {
WeaponType wtype = weapon.getType();
ranges[0] = wtype.getRanges(weapon);

AmmoType atype = null;
if ((weapon.getLinked() != null) && (weapon.getLinked().getType() instanceof AmmoType)) {
// Use the Weapon Panel's selected ammo to determine ranges, or the current linked ammo if not set
AmmoMounted ammoMounted = (clientGUI.getDisplayedAmmo().isPresent())
? clientGUI.getDisplayedAmmo().get() : weapon.getLinkedAmmo();

// Try to get the ammo type from the selected ammo if possible, or the current linked ammo if not
AmmoType atype = (ammoMounted != null) ? ammoMounted.getType() : null;
if (atype == null && (weapon.getLinked() != null) && (weapon.getLinked().getType() instanceof AmmoType)) {
atype = (AmmoType) weapon.getLinked().getType();
}

// Ranges set by weapon + ammo combination, but will be updated depending on selected unit
ranges[0] = wtype.getRanges(weapon, ammoMounted);

// gather underwater ranges
ranges[1] = wtype.getWRanges();
if (atype != null) {
Expand All @@ -317,9 +325,9 @@ private void findRanges(WeaponMounted weapon) {
|| (wtype.getAmmoType() == AmmoType.T_LRM_IMP)
|| (wtype.getAmmoType() == AmmoType.T_MML)) {
if (atype.getMunitionType().contains(AmmoType.Munitions.M_TORPEDO)) {
ranges[1] = wtype.getRanges(weapon);
ranges[1] = wtype.getRanges(weapon, ammoMounted);
} else if (atype.getMunitionType().contains(AmmoType.Munitions.M_MULTI_PURPOSE)) {
ranges[1] = wtype.getRanges(weapon);
ranges[1] = wtype.getRanges(weapon, ammoMounted);
}
}
}
Expand All @@ -340,12 +348,12 @@ private void findRanges(WeaponMounted weapon) {
// 6 to 17 in the other phases as it will be
// direct fire then
if (wtype.hasFlag(WeaponType.F_ARTILLERY)) {
boolean isADA = (weapon.getLinked() != null
&& ((AmmoType) weapon.getLinked().getType()).getMunitionType().contains(AmmoType.Munitions.M_ADA));
boolean isADA = (ammoMounted != null
&& ((AmmoType) ammoMounted.getType()).getMunitionType().contains(AmmoType.Munitions.M_ADA));
if (game.getPhase().isTargeting()) {
ranges[0] = (!isADA? new int[] { 0, 0, 0, 100, 0 } : new int[] { 0, 0, 0, 51, 0 });
} else {
ranges[0] = (!isADA? new int[] { 6, 0, 0, 17, 0 } : wtype.getRanges(weapon));
ranges[0] = (!isADA? new int[] { 6, 0, 0, 17, 0 } : wtype.getRanges(weapon, ammoMounted));
}
ranges[1] = new int[] { 0, 0, 0, 0, 0 };
}
Expand Down Expand Up @@ -388,9 +396,9 @@ private void findRanges(WeaponMounted weapon) {
// only works for the current player's units
if (!weapon.isBreached() && !weapon.isMissing()
&& !weapon.isDestroyed() && !weapon.isJammed()
&& ((weapon.getLinked() == null)
|| (weapon.getLinked().getUsableShotsLeft() > 0))) {
maxr = wtype.getMaxRange(weapon);
&& ((ammoMounted == null)
|| (ammoMounted.getUsableShotsLeft() > 0))) {
maxr = wtype.getMaxRange(weapon, ammoMounted);

// set the standard ranges, depending on capital or no
// boolean isCap = wtype.isCapital();
Expand All @@ -402,6 +410,8 @@ private void findRanges(WeaponMounted weapon) {
for (int rangeIndex = RangeType.RANGE_MINIMUM; rangeIndex <= RangeType.RANGE_EXTREME; rangeIndex++) {
if (maxr >= rangeIndex) {
ranges[0][rangeIndex] = WeaponType.AIRBORNE_WEAPON_RANGES[rangeIndex] * rangeMultiplier;
} else {
ranges[0][rangeIndex] = 0;
}
}
}
Expand Down
64 changes: 45 additions & 19 deletions megamek/src/megamek/client/ui/swing/unitDisplay/WeaponPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -1301,6 +1301,18 @@ public WeaponMounted getSelectedWeapon() {
return ((WeaponListModel) weaponList.getModel()).getWeaponAt(selected);
}

/**
*
* @return the AmmoMounted currently selected by the ammo selector combo box, not the linked ammo per se.
*/
public AmmoMounted getSelectedAmmo() {
int selected = m_chAmmo.getSelectedIndex();
if (selected == -1) {
return null;
}
return vAmmo.get(m_chAmmo.getSelectedIndex());
}

/**
* Returns the equipment ID number for the weapon currently selected
*/
Expand Down Expand Up @@ -1877,9 +1889,11 @@ private void displaySelected() {
wExtR.setText("" + extremeR);
}

// Update the range display to account for the weapon's loaded ammo.
if ((mounted.getLinked() != null) && (mounted.getLinked() instanceof AmmoMounted)) {
updateRangeDisplayForAmmo((AmmoMounted) mounted.getLinked());
// Update the range display to account for the selected ammo, or the loaded ammo if none is selected
int curDisplayed = m_chAmmo.getSelectedIndex();
AmmoMounted mAmmo = (curDisplayed != -1) ? vAmmo.get(curDisplayed) : mounted.getLinkedAmmo();
if (mAmmo != null) {
updateRangeDisplayForAmmo(mAmmo);
}

if (aerospaceAttack) {
Expand All @@ -1893,11 +1907,11 @@ private void displaySelected() {
// if this is a weapons bay, then I need to compile it to get
// accurate results
if (wtype instanceof BayWeapon) {
compileWeaponBay(mounted, wtype.isCapital());
compileWeaponBay(mounted, mAmmo, wtype.isCapital());
} else {
// otherwise I need to replace range display with standard
// ranges and attack values
updateAttackValues(mounted, (AmmoMounted) mounted.getLinked());
updateAttackValues(mounted, mAmmo);
}

}
Expand All @@ -1920,7 +1934,7 @@ private void displaySelected() {
}
}

// update ammo selector
// update ammo selector; reset to currently-displayed item if set.
((DefaultComboBoxModel<String>) m_chAmmo.getModel()).removeAllElements();
WeaponMounted oldmount = mounted;
if (wtype instanceof BayWeapon) {
Expand Down Expand Up @@ -2000,7 +2014,9 @@ private void displaySelected() {

vAmmo.add(mountedAmmo);
m_chAmmo.addItem(formatAmmo(mountedAmmo));
if ((mounted.getLinked() != null) &&
if (curDisplayed != -1) {
nCur = curDisplayed;
} else if ((mounted.getLinked() != null) &&
mounted.getLinked().equals(mountedAmmo)) {
nCur = i;
}
Expand Down Expand Up @@ -2375,7 +2391,7 @@ else if ((atype.getAmmoType() == AmmoType.T_LRM)

}

private void compileWeaponBay(WeaponMounted weapon, boolean isCapital) {
private void compileWeaponBay(WeaponMounted weapon, AmmoMounted mAmmo, boolean isCapital) {

List<WeaponMounted> bayWeapons = weapon.getBayWeapons();
WeaponType wtype = weapon.getType();
Expand All @@ -2402,8 +2418,7 @@ private void compileWeaponBay(WeaponMounted weapon, boolean isCapital) {
&& !m.isMissing()
&& !m.isDestroyed()
&& !m.isJammed()
&& ((m.getLinked() == null) || (m.getLinked()
.getUsableShotsLeft() > 0))) {
&& ((mAmmo == null) || (mAmmo.getUsableShotsLeft() > 0))) {
WeaponType bayWType = m.getType();
heat = heat + m.getCurrentHeat();
double mAVShort = bayWType.getShortAV();
Expand All @@ -2413,8 +2428,8 @@ private void compileWeaponBay(WeaponMounted weapon, boolean isCapital) {
int mMaxR = bayWType.getMaxRange(m);

// deal with any ammo adjustments
if (null != m.getLinked()) {
double[] changes = changeAttackValues((AmmoType) m.getLinked().getType(), mAVShort, mAVMed,
if (null != mAmmo) {
double[] changes = changeAttackValues((AmmoType) mAmmo.getType(), mAVShort, mAVMed,
mAVLong, mAVExt, mMaxR);
mAVShort = changes[0];
mAVMed = changes[1];
Expand Down Expand Up @@ -2558,19 +2573,30 @@ public void actionPerformed(ActionEvent ev) {
if (ev.getSource().equals(m_chAmmo)
&& (m_chAmmo.getSelectedIndex() != -1)
&& (clientgui != null)) {
// only change our own units
if (!clientgui.getClient().getLocalPlayer()
.equals(entity.getOwner())) {
return;
}
int n = weaponList.getSelectedIndex();
if (weaponList.getSelectedIndex() == -1) {
if (n == -1) {
return;
}

// We can update display values without changing the selected unit's ammo;
// this allows displaying selected unit's ammo-based ranges without owning it
WeaponMounted mWeap = ((WeaponListModel) weaponList.getModel()).getWeaponAt(n);
WeaponMounted oldWeap = mWeap;
AmmoMounted oldAmmo = mWeap.getLinkedAmmo();
AmmoMounted mAmmo = vAmmo.get(m_chAmmo.getSelectedIndex());

weaponList.setSelectedIndex(n);
weaponList.ensureIndexIsVisible(n);
displaySelected();
// Update once prior to changing any actual loaded ammo
updateRangeDisplayForAmmo(mAmmo);

// only change our own units
if (!clientgui.getClient().getLocalPlayer()
.equals(entity.getOwner())) {
return;
}

// if this is a weapon bay, then this is not what we want
boolean isBay = false;
if (mWeap.getType() instanceof BayWeapon) {
Expand Down Expand Up @@ -2623,7 +2649,7 @@ public void actionPerformed(ActionEvent ev) {
if (entity.isAirborne() || entity.usesWeaponBays()) {
WeaponType wtype = (WeaponType) mWeap.getType();
if (isBay) {
compileWeaponBay(oldWeap, wtype.isCapital());
compileWeaponBay(oldWeap, mAmmo, wtype.isCapital());
} else {
// otherwise I need to replace range display with
// standard ranges and attack values
Expand Down
6 changes: 4 additions & 2 deletions megamek/src/megamek/common/weapons/bayweapons/BayWeapon.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,13 @@ public int getMaxRange(WeaponMounted weapon) {
public int getMaxRange(WeaponMounted weapon, AmmoMounted ammo) {
int mrange = RANGE_SHORT;
Entity ae = weapon.getEntity();
AmmoMounted mAmmo;
if (null != ae) {
for (WeaponMounted bayW : weapon.getBayWeapons()) {
mAmmo = (ammo != null) ? ammo : bayW.getLinkedAmmo();
WeaponType bayWType = bayW.getType();
if (bayWType.getMaxRange(bayW) > mrange) {
mrange = bayWType.getMaxRange(bayW);
if (bayWType.getMaxRange(bayW, mAmmo) > mrange) {
mrange = bayWType.getMaxRange(bayW, mAmmo);
}
}
}
Expand Down
Loading