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

feat: special negative-conditional cache for two properties that are very important for princess calculations #6463

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
7 changes: 6 additions & 1 deletion megamek/src/megamek/common/Entity.java
Original file line number Diff line number Diff line change
Expand Up @@ -899,6 +899,7 @@ public enum InvalidSourceBuildReason {

private boolean hasFleeZone = false;
private HexArea fleeZone = HexArea.EMPTY_AREA;
private transient Boolean cacheHasNoModularArmor = null;

/**
* Generates a new, blank, entity.
Expand Down Expand Up @@ -6447,6 +6448,7 @@ public void newRound(int roundNumber) {
isJumpingNow = false;
convertingNow = false;
damageThisRound = 0;
cacheHasNoModularArmor = null;
if (assaultDropInProgress == 2) {
assaultDropInProgress = 0;
}
Expand Down Expand Up @@ -12131,7 +12133,10 @@ public boolean isAeroSensorDestroyed() {
}

public boolean hasModularArmor() {
return hasModularArmor(-1);
if (cacheHasNoModularArmor == null || cacheHasNoModularArmor) {
cacheHasNoModularArmor = hasModularArmor(-1);
}
return cacheHasNoModularArmor;
}

public boolean hasModularArmor(int loc) {
Expand Down
17 changes: 13 additions & 4 deletions megamek/src/megamek/common/Mek.java
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,8 @@ public abstract class Mek extends Entity {
// Cooling System Flaws quirk
private boolean coolingFlawActive = false;

private transient Boolean cacheHasNoIndustrialTSM = null;

// QuadVees, LAMs, and tracked 'Meks can change movement mode.
protected EntityMovementMode originalMovementMode = EntityMovementMode.BIPED;

Expand Down Expand Up @@ -610,6 +612,7 @@ public void newRound(int roundNumber) {
m.setMode("Off");
}
} // Check the next piece of equipment.
cacheHasNoIndustrialTSM = null;

super.newRound(roundNumber);
incrementMASCAndSuperchargerLevels();
Expand Down Expand Up @@ -885,13 +888,19 @@ public boolean hasActiveTSM(boolean includeIndustrial) {
* @return
*/
public boolean hasIndustrialTSM() {
for (Mounted<?> m : getEquipment()) {
if ((m.getType() instanceof MiscType)
// This is a special cache that returns immediatelly if the value is false
// but checks for the real value if it is not (because it may lose the state, but cannot gain during normal gameplay)
if (cacheHasNoIndustrialTSM == null || cacheHasNoIndustrialTSM) {
for (Mounted<?> m : getEquipment()) {
if ((m.getType() instanceof MiscType)
&& m.getType().hasFlag(MiscType.F_INDUSTRIAL_TSM)) {
return true;
cacheHasNoIndustrialTSM = true;
return true;
}
}
cacheHasNoIndustrialTSM = false;
}
return false;
return cacheHasNoIndustrialTSM;
}

/**
Expand Down