Skip to content

Commit

Permalink
Game: use Table for getChangeZoneLKIInfo
Browse files Browse the repository at this point in the history
  • Loading branch information
Hanmac committed Jun 18, 2023
1 parent 09f6dfd commit 41ee8e5
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 14 deletions.
9 changes: 5 additions & 4 deletions forge-game/src/main/java/forge/game/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,15 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.IdentityHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import forge.game.card.*;

import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.tuple.Pair;

import com.google.common.base.Predicate;
Expand Down Expand Up @@ -242,18 +243,18 @@ public void addPlayer(int id, Player player) {
}

// methods that deal with saving, retrieving and clearing LKI information about cards on zone change
private final HashMap<Integer, Card> changeZoneLKIInfo = new HashMap<>();
private final Table<Integer, Long, Card> changeZoneLKIInfo = HashBasedTable.create();
public final void addChangeZoneLKIInfo(Card lki) {
if (lki == null) {
return;
}
changeZoneLKIInfo.put(lki.getId(), lki);
changeZoneLKIInfo.put(lki.getId(), lki.getGameTimestamp(), lki);
}
public final Card getChangeZoneLKIInfo(Card c) {
if (c == null) {
return null;
}
return changeZoneLKIInfo.getOrDefault(c.getId(), c);
return ObjectUtils.defaultIfNull(changeZoneLKIInfo.get(c.getId(), c.getGameTimestamp()), c);
}
public final void clearChangeZoneLKIInfo() {
changeZoneLKIInfo.clear();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,10 @@ public List<ReplacementEffect> getReplacementList(final ReplacementType event, f
public boolean visit(Card crd) {
Card c = preList.get(crd);
Zone cardZone = game.getZoneOf(c);
Zone lkiZone = game.getChangeZoneLKIInfo(c).getLastKnownZone();

// only when not prelist
boolean noLKIstate = c != crd || event != ReplacementType.Moved;
boolean noLKIstate = c != crd || event != ReplacementType.Moved || !ZoneType.Battlefield.equals(runParams.get(AbilityKey.Origin));
// might be inbound token
noLKIstate |= lkiZone == null || !lkiZone.is(ZoneType.Battlefield);
noLKIstate |= !runParams.containsKey(AbilityKey.LastStateBattlefield) || runParams.get(AbilityKey.LastStateBattlefield) == null;
if (!noLKIstate) {
Card lastState = ((CardCollectionView) runParams.get(AbilityKey.LastStateBattlefield)).get(crd);
Expand All @@ -139,7 +137,7 @@ public boolean visit(Card crd) {
}
// use the LKI because it has the right RE from the state before the effect started
c = lastState;
cardZone = lkiZone;
cardZone = lastState.getLastKnownZone();
}

for (final ReplacementEffect replacementEffect : c.getReplacementEffects()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1285,13 +1285,10 @@ public void testMassRemovalVsKalitas() {
Player opp = game.getPlayers().get(1);

addCardToZone("Kalitas, Traitor of Ghet", p, ZoneType.Battlefield);
for (int i = 0; i < 4; i++) {
addCardToZone("Plains", p, ZoneType.Battlefield);
}

for (int i = 0; i < 2; i++) {
addCardToZone("Aboroth", opp, ZoneType.Battlefield);
}
addCards("Plains", 4, p);

addCards("Aboroth", 2, opp);

Card wrathOfGod = addCardToZone("Wrath of God", p, ZoneType.Hand);
game.getPhaseHandler().devModeSet(PhaseType.MAIN2, p);
Expand Down

0 comments on commit 41ee8e5

Please sign in to comment.