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

Weapon attack cleanup #5922

Open
wants to merge 20 commits into
base: master
Choose a base branch
from
Open

Conversation

Saklad5
Copy link
Contributor

@Saklad5 Saklad5 commented Aug 18, 2024

This is a series of refactors centered around eliminating unnecessary raw loops from WeaponAttackAction.

@Saklad5 Saklad5 force-pushed the weapon-attack-cleanup branch from a4d6fee to 6902929 Compare August 18, 2024 17:08
@HammerGS
Copy link
Member

This has a ton of data stuff attached to it. I'm assuming you didn't update your branch to master before doing the PR.

@Saklad5
Copy link
Contributor Author

Saklad5 commented Aug 18, 2024

This has a ton of data stuff attached to it. I'm assuming you didn't update your branch to master before doing the PR.

Mistakes were made. I'm working on it.

@Saklad5 Saklad5 force-pushed the weapon-attack-cleanup branch 5 times, most recently from ceec3db to 262c4ca Compare August 18, 2024 17:28
@Saklad5 Saklad5 force-pushed the weapon-attack-cleanup branch 2 times, most recently from d7d9074 to fd5dc0e Compare August 18, 2024 17:59
WeaponMounted.getBayWeapons() iterates through the entire bay before
returning, which isn't always necessary. It has been refactored to allow
for the stream to be used directly.
@Saklad5 Saklad5 force-pushed the weapon-attack-cleanup branch 2 times, most recently from 9f6bd6e to 175a3f1 Compare August 18, 2024 18:09
Copy link

codecov bot commented Aug 18, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 28.92%. Comparing base (36e8cab) to head (014cb8f).
Report is 41 commits behind head on master.

Additional details and impacted files
@@             Coverage Diff              @@
##             master    #5922      +/-   ##
============================================
+ Coverage     28.90%   28.92%   +0.01%     
- Complexity    13926    13943      +17     
============================================
  Files          2539     2539              
  Lines        268363   268319      -44     
  Branches      47933    47872      -61     
============================================
+ Hits          77578    77606      +28     
+ Misses       186817   186748      -69     
+ Partials       3968     3965       -3     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@Saklad5 Saklad5 force-pushed the weapon-attack-cleanup branch 6 times, most recently from f1df6d9 to f168263 Compare August 18, 2024 19:27
@Saklad5 Saklad5 force-pushed the weapon-attack-cleanup branch from f168263 to 22eef5c Compare August 18, 2024 19:31
@Saklad5 Saklad5 force-pushed the weapon-attack-cleanup branch from 22eef5c to 29643a5 Compare August 18, 2024 19:37
@Saklad5 Saklad5 force-pushed the weapon-attack-cleanup branch from 8132fd6 to 34ecf28 Compare August 18, 2024 20:02
megamek/src/megamek/common/actions/WeaponAttackAction.java Outdated Show resolved Hide resolved
}
}
if (!usable) {
if (!weapon.streamBayWeapons()
Copy link
Member

Choose a reason for hiding this comment

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

Request to keep streams out of if statements. I'd rather have an extra line and boolean assignment and a variable name that says what this test is about.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't see how having a distinct boolean would add clarity beyond what the existing comment provides.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Do you want me to change the comment to be an initialized boolean anyway?

Copy link
Collaborator

Choose a reason for hiding this comment

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

I agree with @SJuliez. It makes the code harder to read.

spotter = game.getEntity(ti.attackerId);
}
}
final Targetable currentTarget = target; // Required for concurrency reasons
Copy link
Member

Choose a reason for hiding this comment

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

For readability and code length, this does not feel like an improvement to me.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

target really should be final, but the current implementation for Swarm LRMs prevents that.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I find this more readable because it makes it much easier to tell that it is simply getting any entity that successfully applied TAG to the target.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If this is an issue, I could just drop this commit. I'd rather not hold up further review of this pull request due to a few controversial changes.

Copy link
Collaborator

Choose a reason for hiding this comment

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

This changes the code and may have other impacts as the old code picked the last target that matched where as the new code picks a random one.

Depending upon the rules, this may not be a valid change.

Copy link
Contributor Author

@Saklad5 Saklad5 Sep 13, 2024

Choose a reason for hiding this comment

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

This will literally never make a difference. The source of the TAG does not matter.

@Saklad5 Saklad5 force-pushed the weapon-attack-cleanup branch 4 times, most recently from 4df8c8c to e2bacbf Compare August 22, 2024 14:15
@HammerGS HammerGS added For New Dev Cycle This PR should be merged at the beginning of a dev cycle Refactoring labels Aug 22, 2024
This section of code specifically checks whether a BA squad has already
fired a taser or Narc at a different target.
It may be possible to optimize this further if it can be assumed that
all CI attacks either have the infantry flag or are field guns, but I've
settled for replicating the previous logic for now.
@Saklad5 Saklad5 force-pushed the weapon-attack-cleanup branch from e066406 to bdfb73b Compare August 24, 2024 16:26
@Saklad5 Saklad5 force-pushed the weapon-attack-cleanup branch from 0e3fc44 to d16dce5 Compare August 24, 2024 16:46
@Saklad5 Saklad5 force-pushed the weapon-attack-cleanup branch from 332a3fd to 891a17d Compare August 24, 2024 19:06
@Saklad5 Saklad5 force-pushed the weapon-attack-cleanup branch 2 times, most recently from e6131e9 to 5b4ce3f Compare August 24, 2024 21:27
@Saklad5 Saklad5 force-pushed the weapon-attack-cleanup branch 2 times, most recently from 3b96361 to 33a1c1f Compare August 24, 2024 22:01
@Saklad5 Saklad5 force-pushed the weapon-attack-cleanup branch from 33a1c1f to 014cb8f Compare August 24, 2024 22:06
@Saklad5 Saklad5 marked this pull request as ready for review August 24, 2024 23:24
@HammerGS
Copy link
Member

First I want to say thank you for the PR and you're interest to improve MegaMek. However, we have some concerns about this refactor, particularly given its scope and the core area it affects.

Our main points of concern are:

Readability: We strive to keep our code easy to understand. While streams and optionals are useful, overusing them can make the code less accessible, especially for newcomers or those with different skill levels. I'm not a coder but years of working with the code I've learned to read it, but I don't fully understand what I'm looking it here. Running it with the debugger hasn't helped me.

Consistency: MegaMek's code has developed over many years, establishing its own style and conventions. This creates a scenario where we need to refactor and roll out changes in a consistent pattern, and behind the scenes discussions around this happen and having refactoring PR's can disrupt plans.

Performance: Although it's not our top priority in most areas, unnecessary use of streams and optionals can slow things down. We prefer changes that offer clear performance benefits.

Risk management: Refactoring critical parts of the code can lead to unexpected issues. MegaMek is complex, so we need to carefully consider and justify changes. Smaller, focused updates are easier to review and less likely to cause problems.

Collaboration: We value feedback and teamwork in our development process. If concerns raised by Devs (or the tools) aren't addressed, it's unlikely we'll accept a pull request. (You can find our guidelines for developer and contributor expectations on the MegaMek Wiki.) Likewise, after discussions with Devs and getting their feedback if that feedback is ignored it does reduce the likelyhood of a PR being accepted.

Given these concerns, we're going to hold off on merging this PR while we evaluate its overall impact. Please address the existing comments on the PR first. Once that's done, we'll discuss the next steps.

For future reference, it's a good idea to chat with the Dev team before starting any major refactoring work. We are open to using these but want to start small and in specific areas of the code.

As I mentioned in my recent Sarna interview, "You need to earn some street cred by showing you understand our 20-year-old codebase. It needs careful handling, like dealing with a 680-year-old Icarus. You've got to really learn the code, not just know how to code."

@SJuliez SJuliez removed the For New Dev Cycle This PR should be merged at the beginning of a dev cycle label Sep 4, 2024
Comment on lines +1299 to +1303
if (ae.hasC3() || ae.hasC3i() || ae.hasActiveNovaCEWS()
&& game.getEntitiesVector().stream().anyMatch(en ->
!en.isEnemyOf(ae)
&& en.onSameC3NetworkAs(ae)
&& Compute.canSee(game, en, target))) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Please don't do this inside of a conditional block. If you're going to do a stream, please do so either inside or outside of it.

Copy link
Collaborator

@Scoppio Scoppio left a comment

Choose a reason for hiding this comment

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

I'd not do this kind of code change without also adding tests to make sure they keep doing what they were supposed to do.

And hard agree with others, streams are great and I do like them alot, but they are to be used to manipulate data, not to make flow control, they should be used to remove code branching and are great at that, using it in conditionals is really a bad form of its use.

     if (ae.hasC3() || ae.hasC3i() || ae.hasActiveNovaCEWS() && 
                    game.getEntitiesVector().stream().anyMatch(en ->
                    !en.isEnemyOf(ae)
                    && en.onSameC3NetworkAs(ae)
                    && Compute.canSee(game, en, target))) 
     { 

Should be something instead like this:

var foobar =  game.getEntitiesVector().stream().filter(en ->
   !en.isEnemyOf(ae)
   && en.onSameC3NetworkAs(ae)
   && Compute.canSee(game, en, target));
   
if (ae.hasC3() || ae.hasC3i() || ae.hasActiveNovaCEWS() && foobar)  {

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants