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

Check if Edge is enabled in Game Options before using it #5855

Merged
merged 6 commits into from
Aug 5, 2024
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
51 changes: 34 additions & 17 deletions megamek/src/megamek/common/Mech.java
Original file line number Diff line number Diff line change
Expand Up @@ -1971,7 +1971,8 @@ public HitData rollHitLocation(int table, int side, int aimedLocation, AimingMod
// normal front hits
switch (roll) {
case 2:
if ((getCrew().hasEdgeRemaining()
if (game.getOptions().booleanOption(OptionsConstants.EDGE)
&& (getCrew().hasEdgeRemaining()
Copy link
Collaborator

Choose a reason for hiding this comment

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

It looks like we just have a bunch of this conditional repeated throughout Mech.java (and its inheritors), just with different boolean options.
Would it be viable to add, e.g.

public boolean shouldUseEdge(String option) {
    return (
                      game.getOptions().booleanOption(OptionsConstants.EDGE)
                                && getCrew().hasEdgeRemaining()
                                && getCrew().getOptions().booleanOption(option)
                );

or something? Then each check becomes something like this:

    case 2:
        if (shouldUseEdge(OptionsConstants.EDGE_WHEN_TAC)
        && !game.getOptions().booleanOption(OptionsConstants.ADVCOMBAT_NO_TAC))

Which is somewhat cleaner when spread across all these cases.

Copy link
Collaborator Author

@Algebro7 Algebro7 Aug 5, 2024

Choose a reason for hiding this comment

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

Would it make sense to implement that method in the Entity base class all the various Mech and Aero units extend? Looks like it has a crew member so I think it should have all the information it needs, but I'm not sure if any other objects extend Entity that shouldn't have this method.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I think that's acceptable; the downside is you would also have to check that getCrew() doesn't return null prior to calling .hasEdgeRemaining() or .getOptions(), because while an Entity subclass like Mech must have crew, there's no guarantee that a bare Entity will.

&& getCrew().getOptions().booleanOption(OptionsConstants.EDGE_WHEN_TAC))
&& !game.getOptions().booleanOption(OptionsConstants.ADVCOMBAT_NO_TAC)) {
getCrew().decreaseEdge();
Expand All @@ -1997,7 +1998,8 @@ && getCrew().getOptions().booleanOption(OptionsConstants.EDGE_WHEN_TAC))
case 11:
return new HitData(Mech.LOC_LARM);
case 12:
if (getCrew().hasEdgeRemaining()
if (game.getOptions().booleanOption(OptionsConstants.EDGE)
&& getCrew().hasEdgeRemaining()
&& getCrew().getOptions().booleanOption(
OptionsConstants.EDGE_WHEN_HEADHIT)) {
getCrew().decreaseEdge();
Expand All @@ -2012,7 +2014,8 @@ && getCrew().getOptions().booleanOption(
// normal left side hits
switch (roll) {
case 2:
if ((getCrew().hasEdgeRemaining() && getCrew()
if (game.getOptions().booleanOption(OptionsConstants.EDGE)
&& (getCrew().hasEdgeRemaining() && getCrew()
.getOptions().booleanOption(OptionsConstants.EDGE_WHEN_TAC))
&& !game.getOptions().booleanOption(OptionsConstants.ADVCOMBAT_NO_TAC)) {
getCrew().decreaseEdge();
Expand Down Expand Up @@ -2049,7 +2052,8 @@ && getCrew().getOptions().booleanOption(
case 11:
return new HitData(Mech.LOC_RLEG);
case 12:
if (getCrew().hasEdgeRemaining()
if (game.getOptions().booleanOption(OptionsConstants.EDGE)
&& getCrew().hasEdgeRemaining()
&& getCrew().getOptions().booleanOption(
OptionsConstants.EDGE_WHEN_HEADHIT)) {
getCrew().decreaseEdge();
Expand All @@ -2064,7 +2068,8 @@ && getCrew().getOptions().booleanOption(
// normal right side hits
switch (roll) {
case 2:
if ((getCrew().hasEdgeRemaining() && getCrew()
if (game.getOptions().booleanOption(OptionsConstants.EDGE)
&& (getCrew().hasEdgeRemaining() && getCrew()
.getOptions().booleanOption(OptionsConstants.EDGE_WHEN_TAC))
&& !game.getOptions().booleanOption(OptionsConstants.ADVCOMBAT_NO_TAC)) {
getCrew().decreaseEdge();
Expand Down Expand Up @@ -2101,7 +2106,8 @@ && getCrew().getOptions().booleanOption(
case 11:
return new HitData(Mech.LOC_LLEG);
case 12:
if (getCrew().hasEdgeRemaining()
if (game.getOptions().booleanOption(OptionsConstants.EDGE)
&& getCrew().hasEdgeRemaining()
&& getCrew().getOptions().booleanOption(
OptionsConstants.EDGE_WHEN_HEADHIT)) {
getCrew().decreaseEdge();
Expand All @@ -2119,7 +2125,8 @@ && getCrew().getOptions().booleanOption(
&& isProne()) {
switch (roll) {
case 2:
if ((getCrew().hasEdgeRemaining() && getCrew()
if (game.getOptions().booleanOption(OptionsConstants.EDGE)
&& (getCrew().hasEdgeRemaining() && getCrew()
.getOptions()
.booleanOption(OptionsConstants.EDGE_WHEN_TAC))
&& !game.getOptions().booleanOption(
Expand Down Expand Up @@ -2149,7 +2156,8 @@ && isProne()) {
case 11:
return new HitData(Mech.LOC_LARM, true);
case 12:
if (getCrew().hasEdgeRemaining()
if (game.getOptions().booleanOption(OptionsConstants.EDGE)
&& getCrew().hasEdgeRemaining()
&& getCrew().getOptions().booleanOption(
OptionsConstants.EDGE_WHEN_HEADHIT)) {
getCrew().decreaseEdge();
Expand All @@ -2164,7 +2172,8 @@ && getCrew().getOptions().booleanOption(
} else {
switch (roll) {
case 2:
if ((getCrew().hasEdgeRemaining() && getCrew()
if (game.getOptions().booleanOption(OptionsConstants.EDGE)
&& (getCrew().hasEdgeRemaining() && getCrew()
.getOptions()
.booleanOption(OptionsConstants.EDGE_WHEN_TAC))
&& !game.getOptions().booleanOption(
Expand Down Expand Up @@ -2194,7 +2203,8 @@ && getCrew().getOptions().booleanOption(
case 11:
return new HitData(Mech.LOC_LARM, true);
case 12:
if (getCrew().hasEdgeRemaining()
if (game.getOptions().booleanOption(OptionsConstants.EDGE)
&& getCrew().hasEdgeRemaining()
&& getCrew().getOptions().booleanOption(
OptionsConstants.EDGE_WHEN_HEADHIT)) {
getCrew().decreaseEdge();
Expand Down Expand Up @@ -2239,7 +2249,8 @@ && getCrew().getOptions().booleanOption(
case 5:
return new HitData(Mech.LOC_RARM);
case 6:
if (getCrew().hasEdgeRemaining()
if (game.getOptions().booleanOption(OptionsConstants.EDGE)
&& getCrew().hasEdgeRemaining()
&& getCrew().getOptions().booleanOption(
OptionsConstants.EDGE_WHEN_HEADHIT)) {
getCrew().decreaseEdge();
Expand All @@ -2263,7 +2274,8 @@ && getCrew().getOptions().booleanOption(
case 5:
return new HitData(Mech.LOC_LARM);
case 6:
if (getCrew().hasEdgeRemaining()
if (game.getOptions().booleanOption(OptionsConstants.EDGE)
&& getCrew().hasEdgeRemaining()
&& getCrew().getOptions().booleanOption(
OptionsConstants.EDGE_WHEN_HEADHIT)) {
getCrew().decreaseEdge();
Expand All @@ -2287,7 +2299,8 @@ && getCrew().getOptions().booleanOption(
case 5:
return new HitData(Mech.LOC_RARM);
case 6:
if (getCrew().hasEdgeRemaining()
if (game.getOptions().booleanOption(OptionsConstants.EDGE)
&& getCrew().hasEdgeRemaining()
&& getCrew().getOptions().booleanOption(
OptionsConstants.EDGE_WHEN_HEADHIT)) {
getCrew().decreaseEdge();
Expand All @@ -2313,7 +2326,8 @@ && getCrew().getOptions().booleanOption(
case 5:
return new HitData(Mech.LOC_RARM, true);
case 6:
if (getCrew().hasEdgeRemaining()
if (game.getOptions().booleanOption(OptionsConstants.EDGE)
&& getCrew().hasEdgeRemaining()
&& getCrew().getOptions().booleanOption(
OptionsConstants.EDGE_WHEN_HEADHIT)) {
getCrew().decreaseEdge();
Expand Down Expand Up @@ -2392,7 +2406,8 @@ && getCrew().getOptions().booleanOption(
// Swarm attack locations.
switch (roll) {
case 2:
if (getCrew().hasEdgeRemaining()
if (game.getOptions().booleanOption(OptionsConstants.EDGE)
&& getCrew().hasEdgeRemaining()
&& getCrew().getOptions().booleanOption(OptionsConstants.EDGE_WHEN_HEADHIT)) {
getCrew().decreaseEdge();
HitData result = rollHitLocation(table, side, aimedLocation, aimingMode, cover);
Expand All @@ -2419,7 +2434,8 @@ && getCrew().getOptions().booleanOption(OptionsConstants.EDGE_WHEN_HEADHIT)) {
case 11:
return new HitData(Mech.LOC_CT, true, effects);
case 12:
if (getCrew().hasEdgeRemaining()
if (game.getOptions().booleanOption(OptionsConstants.EDGE)
&& getCrew().hasEdgeRemaining()
&& getCrew().getOptions().booleanOption(
OptionsConstants.EDGE_WHEN_HEADHIT)) {
getCrew().decreaseEdge();
Expand Down Expand Up @@ -2460,7 +2476,8 @@ && getCrew().getOptions().booleanOption(
case 5:
return new HitData(Mech.LOC_RARM, (side == ToHitData.SIDE_REAR));
case 6:
if (getCrew().hasEdgeRemaining()
if (game.getOptions().booleanOption(OptionsConstants.EDGE)
&& getCrew().hasEdgeRemaining()
&& getCrew().getOptions().booleanOption(
OptionsConstants.EDGE_WHEN_HEADHIT)) {
getCrew().decreaseEdge();
Expand Down
Loading