Skip to content

Commit

Permalink
Merge pull request #5195 from Sleet01/Fix_5193_weapon_bays_show_out_o…
Browse files Browse the repository at this point in the history
…f_range

Fix 5193: Weapon Bays only work at Short Range
  • Loading branch information
SJuliez authored Feb 26, 2024
2 parents d8fa093 + 75ca452 commit 727e62b
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 9 deletions.
17 changes: 8 additions & 9 deletions megamek/src/megamek/common/weapons/bayweapons/BayWeapon.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,7 @@
*/
package megamek.common.weapons.bayweapons;

import megamek.common.Entity;
import megamek.common.Game;
import megamek.common.Mounted;
import megamek.common.SimpleTechLevel;
import megamek.common.TechAdvancement;
import megamek.common.ToHitData;
import megamek.common.WeaponType;
import megamek.common.*;
import megamek.common.actions.WeaponAttackAction;
import megamek.common.weapons.AttackHandler;
import megamek.common.weapons.BayWeaponHandler;
Expand Down Expand Up @@ -56,7 +50,7 @@ public AttackHandler fire(WeaponAttackAction waa, Game game, GameManager manager

/*
* (non-Javadoc)
*
*
* @see
* megamek.common.weapons.Weapon#getCorrectHandler(megamek.common.ToHitData,
* megamek.common.actions.WeaponAttackAction, megamek.common.Game)
Expand All @@ -66,9 +60,14 @@ protected AttackHandler getCorrectHandler(ToHitData toHit,
WeaponAttackAction waa, Game game, GameManager manager) {
return new BayWeaponHandler(toHit, waa, game, manager);
}

@Override
public int getMaxRange(Mounted weapon) {
return getMaxRange(weapon, null);
}

@Override
public int getMaxRange(Mounted weapon, Mounted ammo) {
int mrange = RANGE_SHORT;
Entity ae = weapon.getEntity();
if (null != ae) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,11 @@ public int getRoundExtAV() {

@Override
public int getMaxRange(Mounted weapon) {
return getMaxRange(weapon, null);
}

@Override
public int getMaxRange(Mounted weapon, Mounted ammo) {
for (int range = RangeType.RANGE_EXTREME; range >= RangeType.RANGE_SHORT; range--) {
if (infantryRange * 3 > AIRBORNE_WEAPON_RANGES[range - 1]) {
return range;
Expand Down
51 changes: 51 additions & 0 deletions megamek/unittests/megamek/common/WeaponTypeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,31 @@
*/
package megamek.common;

import megamek.common.weapons.Weapon;
import megamek.common.weapons.bayweapons.BayWeapon;
import megamek.common.weapons.bayweapons.PPCBayWeapon;
import megamek.common.weapons.ppc.ISERPPC;
import megamek.common.weapons.ppc.PPCWeapon;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import java.util.Enumeration;
import java.util.Vector;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class WeaponTypeTest {

private Entity mockEntity = mock(Entity.class);

@BeforeAll
static void before(){
EquipmentType.initializeTypes();
}

@Test
public void testArtemisCompatibleFlag() {
for (Enumeration<EquipmentType> e = EquipmentType.getAllTypes(); e.hasMoreElements(); ) {
Expand All @@ -45,4 +63,37 @@ public void testArtemisCompatibleFlag() {
}
}
}

private Mounted setupBayWeapon(String name){
EquipmentType etype = EquipmentType.get(name);
Mounted weapon = new Mounted(mockEntity, etype);
Mounted bWeapon = new Mounted(mockEntity, ((WeaponType) weapon.getType()).getBayType());
bWeapon.addWeaponToBay(mockEntity.getEquipmentNum(weapon));
when(mockEntity.getEquipment(anyInt())).thenReturn(weapon);

return bWeapon;
}

@Test
public void testWeaponBaysGetCorrectMaxRanges() {
Mounted ppcbay = setupBayWeapon("ISERPPC");
WeaponType wtype = (WeaponType) ppcbay.getType();
assertEquals(RangeType.RANGE_LONG, wtype.getMaxRange(ppcbay));

Mounted erplasbay = setupBayWeapon("CLERLargePulseLaser");
wtype = (WeaponType) erplasbay.getType();
assertEquals(RangeType.RANGE_LONG, wtype.getMaxRange(erplasbay ));

Mounted islplasbay = setupBayWeapon("ISLargePulseLaser");
wtype = (WeaponType) islplasbay.getType();
assertEquals(RangeType.RANGE_MEDIUM, wtype.getMaxRange(islplasbay));

Mounted ersmlasbay = setupBayWeapon("CLERSmallLaser");
wtype = (WeaponType) ersmlasbay.getType();
assertEquals(RangeType.RANGE_SHORT, wtype.getMaxRange(ersmlasbay));

Mounted islgaussbay = setupBayWeapon("ISLightGaussRifle");
wtype = (WeaponType) islgaussbay .getType();
assertEquals(RangeType.RANGE_EXTREME, wtype.getMaxRange(islgaussbay ));
}
}

0 comments on commit 727e62b

Please sign in to comment.