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

Mounted rework #5406

Merged
merged 17 commits into from
Apr 25, 2024
Merged

Mounted rework #5406

merged 17 commits into from
Apr 25, 2024

Conversation

neoancient
Copy link
Member

This overhaul of Mounted is the next step in my equipment overhaul. The vast majority of the changes involve type checking, and errors would be caught by the compiler.

What it does:

  1. Creates subclasses of Mounted and moves functionality that only applies to one subclass of EquipmentType into the appropriate Mounted subclass.
  2. Adds a generic type parameter to Mounted to improve type safety working with mounted equipment.
  3. Adds a few convenience methods.

Benefits:

  1. Reduces the size of Mounted by removing code that's only used for some type of equipment.
  2. Removes much of the need for type checking and casting of equipment. For example, Entity::getWeapons returns List, and WeaponMounted::getType returns WeaponType. No need for wtype = (WeaponType) mount.getType().
  3. Prepares the way for moving code from Entity to a Mounted subclass. A step in the near future is an ArmorMounted, which will handle the armor fields and weight calculations, and deal with patchwork more elegantly.

Note that the calculation of ammo shots is still in the base class rather than AmmoMounted because it is also used by mines, which are MiscType. It can probably be simplified but I left that for the future, as this is already very extensive.

Also I handled the movement of bay weapons and ammo to WeaponMounted in a separate PR that builds on this one, since that change also touches a lot of code.

# Conflicts:
#	megamek/src/megamek/client/bot/princess/ArtilleryTargetingControl.java
#	megamek/src/megamek/client/bot/princess/FireControl.java
#	megamek/src/megamek/client/bot/princess/MultiTargetFireControl.java
#	megamek/src/megamek/client/bot/princess/Princess.java
#	megamek/src/megamek/client/bot/princess/WeaponFireInfo.java
#	megamek/src/megamek/common/Aero.java
#	megamek/src/megamek/common/Compute.java
#	megamek/src/megamek/common/Entity.java
#	megamek/src/megamek/common/Mounted.java
#	megamek/src/megamek/common/WeaponType.java
#	megamek/src/megamek/common/actions/WeaponAttackAction.java
#	megamek/src/megamek/common/weapons/AmmoWeapon.java
#	megamek/src/megamek/common/weapons/bayweapons/BayWeapon.java
#	megamek/src/megamek/common/weapons/infantry/InfantryWeapon.java
#	megamek/src/megamek/server/GameManager.java
#	megamek/unittests/megamek/client/bot/princess/FireControlTest.java
Copy link
Member

@SJuliez SJuliez left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Considering how central Mounted is, the amount of change is rather lower than I had expected. We have some equipment with non-obvious type, e.g. C3M is a weapon and (iirc) Radical Heat Sinks are an AmmoType but I imagine if it worked before then it'll work now.

@SJuliez
Copy link
Member

SJuliez commented Apr 23, 2024

But there's a test failure :)

@neoancient
Copy link
Member Author

I ran the tests but now I suspect that I actually had master checked out at the time.

@@ -309,17 +311,11 @@
boolean isPointblankShot, List<ECMInfo> allECMInfo, boolean evenIfAlreadyFired,
int ammoId) {
final Entity ae = game.getEntity(attackerId);
final Mounted weapon = ae.getEquipment(weaponId);
final Mounted linkedAmmo = (ammoId == -1) ? weapon.getLinked() : ae.getEquipment(ammoId);
final WeaponMounted weapon = (WeaponMounted) ae.getEquipment(weaponId);

Check warning

Code scanning / CodeQL

Dereferenced variable may be null Warning

Variable
ae
may be null at this access as suggested by
this
null guard.
@@ -4361,7 +4357,7 @@
*/
private static ToHitData compileTerrainAndLosToHitMods(Game game, Entity ae, Targetable target, int ttype, int aElev, int tElev,
int targEl, int distance, LosEffects los, ToHitData toHit, ToHitData losMods, int toSubtract, int eistatus,
WeaponType wtype, Mounted weapon, int weaponId, AmmoType atype, Mounted ammo, EnumSet<AmmoType.Munitions> munition, boolean isAttackerInfantry,
WeaponType wtype, WeaponMounted weapon, int weaponId, AmmoType atype, AmmoMounted ammo, EnumSet<AmmoType.Munitions> munition, boolean isAttackerInfantry,

Check notice

Code scanning / CodeQL

Useless parameter Note

The parameter 'munition' is never used.
@@ -2724,8 +2727,8 @@
continue;
}

final Mounted suggestedAmmo = info.getAmmo();
final Mounted mountedAmmo = getPreferredAmmo(shooter, info.getTarget(), currentWeapon, suggestedAmmo);
final AmmoMounted suggestedAmmo = info.getAmmo();

Check notice

Code scanning / CodeQL

Unread local variable Note

Variable 'AmmoMounted suggestedAmmo' is never read.
@@ -4238,17 +4251,18 @@
// One entry per ammo type that is currently usable by this weapon.
return ammos;
}
public ArrayList<Mounted> getMisc() {

public List<MiscMounted> getMisc() {

Check notice

Code scanning / CodeQL

Exposing internal representation Note

getMisc exposes the internal representation stored in field miscList. The value may be modified
after this call to getMisc
.
getMisc exposes the internal representation stored in field miscList. The value may be modified
after this call to getMisc
.
getMisc exposes the internal representation stored in field miscList. The value may be modified
after this call to getMisc
.
getMisc exposes the internal representation stored in field miscList. The value may be modified
after this call to getMisc
.
getMisc exposes the internal representation stored in field miscList. The value may be modified
after this call to getMisc
.
@@ -4214,22 +4228,21 @@
return false;
}

public ArrayList<Mounted> getAmmo() {
public List<AmmoMounted> getAmmo() {

Check notice

Code scanning / CodeQL

Exposing internal representation Note

getAmmo exposes the internal representation stored in field ammoList. The value may be modified
after this call to getAmmo
.
return weaponList;
}

public ArrayList<Mounted> getWeaponList() {
public List<WeaponMounted> getWeaponList() {

Check notice

Code scanning / CodeQL

Exposing internal representation Note

getWeaponList exposes the internal representation stored in field weaponGroupList. The value may be modified
after this call to getWeaponList
.
getWeaponList exposes the internal representation stored in field weaponGroupList. The value may be modified
after this call to getWeaponList
.
getWeaponList exposes the internal representation stored in field weaponBayList. The value may be modified
after this call to getWeaponList
.
getWeaponList exposes the internal representation stored in field weaponBayList. The value may be modified
after this call to getWeaponList
.
getWeaponList exposes the internal representation stored in field weaponList. The value may be modified
after this call to getWeaponList
.
getWeaponList exposes the internal representation stored in field weaponList. The value may be modified
after this call to getWeaponList
.
@@ -3853,26 +3854,41 @@
/**
* Returns an enumeration of all equipment
*/
public ArrayList<Mounted> getEquipment() {
public List<Mounted<?>> getEquipment() {

Check notice

Code scanning / CodeQL

Exposing internal representation Note

getEquipment exposes the internal representation stored in field equipmentList. The value may be modified
after this call to getEquipment
.
getEquipment exposes the internal representation stored in field equipmentList. The value may be modified
after this call to getEquipment
.
getEquipment exposes the internal representation stored in field equipmentList. The value may be modified
after this call to getEquipment
.
getEquipment exposes the internal representation stored in field equipmentList. The value may be modified
after this call to getEquipment
.
getEquipment exposes the internal representation stored in field equipmentList. The value may be modified
after this call to getEquipment
.
getEquipment exposes the internal representation stored in field equipmentList. The value may be modified
after this call to getEquipment
.
getEquipment exposes the internal representation stored in field equipmentList. The value may be modified
after this call to getEquipment
.
getEquipment exposes the internal representation stored in field equipmentList. The value may be modified
after this call to getEquipment
.
}

@Override
public List<Integer> getBayWeapons() {

Check notice

Code scanning / CodeQL

Exposing internal representation Note

getBayWeapons exposes the internal representation stored in field bayWeapons. The value may be modified
after this call to getBayWeapons
.
@neoancient neoancient merged commit f6af4e3 into master Apr 25, 2024
3 checks passed
@neoancient neoancient deleted the generic_mounted branch April 25, 2024 02:23
@neoancient neoancient mentioned this pull request Apr 25, 2024
@neoancient neoancient restored the generic_mounted branch April 28, 2024 20:55
@SJuliez SJuliez deleted the generic_mounted branch November 1, 2024 16:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants