Skip to content

Commit

Permalink
feat: special negative-conditional cache for two properties that are …
Browse files Browse the repository at this point in the history
…very important for Princess calculations
  • Loading branch information
Scoppio committed Jan 30, 2025
1 parent 2015dac commit 18a8f95
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
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

0 comments on commit 18a8f95

Please sign in to comment.