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

Quirks misc code changes #4944

Merged
merged 3 commits into from
Dec 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 1 addition & 2 deletions megamek/src/megamek/common/Entity.java
Original file line number Diff line number Diff line change
Expand Up @@ -12757,8 +12757,7 @@ public synchronized Quirks getQuirks() {
}

public boolean hasQuirk(String name) {
if ((null == game)
|| !game.getOptions().booleanOption(OptionsConstants.ADVANCED_STRATOPS_QUIRKS)) {
if ((game != null) && !game.getOptions().booleanOption(OptionsConstants.ADVANCED_STRATOPS_QUIRKS)) {
return false;
}
return quirks.booleanOption(name);
Expand Down
58 changes: 19 additions & 39 deletions megamek/src/megamek/common/MechView.java
Original file line number Diff line number Diff line change
Expand Up @@ -483,53 +483,28 @@ public MechView(final Entity entity, final boolean showDetail, final boolean use
Game game = entity.getGame();

if ((game == null) || game.getOptions().booleanOption(OptionsConstants.ADVANCED_STRATOPS_QUIRKS)) {
List<String> quirksList = new ArrayList<>();
Quirks quirks = entity.getQuirks();
List<String> activeUnitQuirksNames = entity.getQuirks().activeQuirks().stream()
.map(IOption::getDisplayableNameWithValue)
.collect(Collectors.toList());

for (Enumeration<IOptionGroup> optionGroups = quirks.getGroups(); optionGroups.hasMoreElements();) {
IOptionGroup group = optionGroups.nextElement();

if (quirks.count(group.getKey()) > 0) {
for (Enumeration<IOption> options = group.getOptions(); options.hasMoreElements();) {
IOption option = options.nextElement();

if (option != null && option.booleanValue()) {
quirksList.add(option.getDisplayableNameWithValue());
}
}
}
}
if (!quirksList.isEmpty()) {
if (!activeUnitQuirksNames.isEmpty()) {
sFluff.add(new SingleLine());
ItemList list = new ItemList(Messages.getString("MechView.Quirks"));
quirksList.forEach(list::addItem);
activeUnitQuirksNames.forEach(list::addItem);
sFluff.add(list);
}

List<String> wpQuirksList = new ArrayList<>();
for (Mounted weapon: entity.getWeaponList()) {
for (Enumeration<IOptionGroup> optionGroups = weapon.getQuirks().getGroups(); optionGroups.hasMoreElements();) {
IOptionGroup group = optionGroups.nextElement();

if (weapon.getQuirks().count(group.getKey()) > 0) {
String wq = "";

for (Enumeration<IOption> options = group.getOptions(); options.hasMoreElements(); ) {
IOption option = options.nextElement();

if (option != null && option.booleanValue()) {
wq += option.getDisplayableNameWithValue() + " \u2022 ";
}
}

if (!wq.isEmpty()) {
wq = weapon.getDesc() + ": " + wq.substring(0, wq.length() - 2);
wpQuirksList.add(wq);
}
}
List<String> activeWeaponQuirksNames = weapon.getQuirks().activeQuirks().stream()
.map(IOption::getDisplayableNameWithValue)
.collect(Collectors.toList());
if (!activeWeaponQuirksNames.isEmpty()) {
String wq = weapon.getDesc() + " (" + entity.getLocationAbbr(weapon.getLocation()) + "): ";
wq += String.join(", ", activeWeaponQuirksNames);
wpQuirksList.add(wq);
}
}

if (!wpQuirksList.isEmpty()) {
sFluff.add(new SingleLine());
ItemList list = new ItemList(Messages.getString("MechView.WeaponQuirks"));
Expand Down Expand Up @@ -853,7 +828,7 @@ private List<ViewElement> getWeapons(boolean showDetail) {
retVal.add(new SingleLine());
}

if (entity.getWeaponList().size() < 1) {
if (entity.getWeaponList().isEmpty()) {
return retVal;
}

Expand All @@ -862,7 +837,8 @@ private List<ViewElement> getWeapons(boolean showDetail) {
wpnTable.setJustification(TableElement.JUSTIFIED_LEFT, TableElement.JUSTIFIED_CENTER,
TableElement.JUSTIFIED_CENTER, TableElement.JUSTIFIED_CENTER);
for (Mounted mounted : entity.getWeaponList()) {
String[] row = { mounted.getDesc(), entity.joinLocationAbbr(mounted.allLocations(), 3), "", "" };
String[] row = { mounted.getDesc() + quirkMarker(mounted),
entity.joinLocationAbbr(mounted.allLocations(), 3), "", "" };
WeaponType wtype = (WeaponType) mounted.getType();

if (entity.isClan()
Expand Down Expand Up @@ -969,6 +945,10 @@ private List<ViewElement> getWeapons(boolean showDetail) {
return retVal;
}

private String quirkMarker(Mounted mounted) {
return (mounted.countQuirks() > 0) ? " (Q)" : "";
}

private ViewElement getAmmo() {
TableElement ammoTable = new TableElement(4);
ammoTable.setColNames("Ammo", "Loc", "Shots", entity.isOmni() ? "Omni" : "");
Expand Down
14 changes: 9 additions & 5 deletions megamek/src/megamek/common/Mounted.java
Original file line number Diff line number Diff line change
Expand Up @@ -1804,15 +1804,19 @@ public boolean hasQuirk(String name) {
}

/**
* Count all the quirks for this "mounted" object, positive and negative
* Count all the weapon quirks for this Mounted. Returns 0 when this Mounted is tied to an Entity and
* this Entity's game does not use quirks! In all other cases (e.g. outside of games), returns the quirk
* count of this Mounted.
*
* @return The number of active weapon quirks for this Mounted unless the game does not use quirks
*/
public int countQuirks() {
if ((null == entity) || (null == entity.game)
|| !entity.game.getOptions().booleanOption(OptionsConstants.ADVANCED_STRATOPS_QUIRKS)) {
if ((entity != null) && (entity.getGame() != null) &&
!entity.getGame().getOptions().booleanOption(OptionsConstants.ADVANCED_STRATOPS_QUIRKS)) {
return 0;
} else {
return quirks.count();
}

return quirks.count();
}

/**
Expand Down
2 changes: 1 addition & 1 deletion megamek/src/megamek/common/options/AbstractOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
*/
public abstract class AbstractOptions implements Serializable {
private static final long serialVersionUID = 6406883135074654379L;
private Hashtable<String, IOption> optionsHash = new Hashtable<>();
private final Hashtable<String, IOption> optionsHash = new Hashtable<>();

protected AbstractOptions() {
initialize();
Expand Down
16 changes: 16 additions & 0 deletions megamek/src/megamek/common/options/IBasicOption.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

package megamek.common.options;

import java.util.Arrays;

/**
* Basic option. It's just <code>String</code> name - <code>Object</code>
* value pair
Expand All @@ -34,4 +36,18 @@ public interface IBasicOption {
*/
public abstract Object getValue();

/** @return True when this Option's name is equal to the given Option name. */
default boolean is(String otherName) {
return isAnyOf(otherName);
}

/** @return True when this Option's name is equal to at least one of the given Option names. */
default boolean isAnyOf(String otherName, String... otherNames) {
return getName().equals(otherName) || Arrays.stream(otherNames).anyMatch(name -> getName().equals(name));
}

/** @return True when this Option's name is not equal to any of the given names. */
default boolean isNoneOf(String otherName, String... otherNames) {
return !isAnyOf(otherName, otherNames);
}
}
32 changes: 16 additions & 16 deletions megamek/src/megamek/common/options/IOption.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,27 +29,27 @@
/**
* Boolean option type
*/
public static final int BOOLEAN = 0;
int BOOLEAN = 0;
NickAragua marked this conversation as resolved.
Show resolved Hide resolved

/**
* Integer option type
*/
public static final int INTEGER = 1;
int INTEGER = 1;

/**
* Float option type
*/
public static final int FLOAT = 2;
int FLOAT = 2;

/**
* String option type
*/
public static final int STRING = 3;
int STRING = 3;

/**
* Choice option type
*/
public static final int CHOICE = 4;
int CHOICE = 4;

/**
* Returns this option container - GameOptions, PilotOptions etc
Expand All @@ -71,66 +71,66 @@
*
* @return default option value
*/
public abstract Object getDefault();
Object getDefault();

/**
* Return the value as the <code>boolean</code>
*/
public abstract boolean booleanValue();
boolean booleanValue();

/**
* Return the value as the <code>int</code>
*/
public abstract int intValue();
int intValue();

/**
* Return the value as the <code>float</code>
*/
public abstract float floatValue();
float floatValue();

/**
* Return the value as the <code>String</code>
*/
public abstract String stringValue();
String stringValue();

/**
* Sets the value
*
* @param value value to set
*/
public abstract void setValue(Object value);
void setValue(Object value);

Check notice

Code scanning / CodeQL

Confusing overloading of methods Note

Method IOption.setValue(..) could be confused with overloaded method
setValue
, since dispatch depends on static types.
Copy link
Member Author

Choose a reason for hiding this comment

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

legacy issue. Seems no problem at this moment. I tried, can't just remove it.


/**
* Sets the <code>String</code> value
*
* @param value value to set
*/
public abstract void setValue(String value);
void setValue(String value);

/**
* Sets the <code>boolean</code> value
*
* @param value value to set
*/
public abstract void setValue(boolean value);
void setValue(boolean value);

/**
* Sets the <code>int</code> value
*
* @param value value to set
*/
public abstract void setValue(int value);
void setValue(int value);

/**
* Sets the <code>float</code> value
*
* @param value value to set
*/
public abstract void setValue(float value);
void setValue(float value);

/**
* Resets the value to its "default" state
*/
public abstract void clearValue();
void clearValue();

}
Loading