Skip to content

Commit

Permalink
PlayerController: add chooseSingleCardState
Browse files Browse the repository at this point in the history
  • Loading branch information
Hanmac committed Oct 3, 2024
1 parent ad923f2 commit 67f5153
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 19 deletions.
9 changes: 9 additions & 0 deletions forge-ai/src/main/java/forge/ai/PlayerControllerAi.java
Original file line number Diff line number Diff line change
Expand Up @@ -1539,6 +1539,15 @@ public ICardFace chooseSingleCardFace(SpellAbility sa, List<ICardFace> faces, St
return SpellApiToAi.Converter.get(api).chooseCardFace(player, sa, faces);
}

@Override
public CardState chooseSingleCardState(SpellAbility sa, List<CardState> states, String message) {
ApiType api = sa.getApi();
if (null == api) {
throw new InvalidParameterException("SA is not api-based, this is not supported yet");
}
return SpellApiToAi.Converter.get(api).chooseCardState(player, sa, states);
}

@Override
public Card chooseDungeon(Player ai, List<PaperCard> dungeonCards, String message) {
// TODO: improve the conditions that define which dungeon is a viable option to choose
Expand Down
7 changes: 7 additions & 0 deletions forge-ai/src/main/java/forge/ai/SpellAbilityAi.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import forge.card.mana.ManaCostParser;
import forge.game.GameEntity;
import forge.game.card.Card;
import forge.game.card.CardState;
import forge.game.card.CounterType;
import forge.game.cost.Cost;
import forge.game.mana.ManaCostBeingPaid;
Expand Down Expand Up @@ -371,6 +372,12 @@ public ICardFace chooseCardFace(Player ai, SpellAbility sa, List<ICardFace> face
return Iterables.getFirst(faces, null);
}

public CardState chooseCardState(Player ai, SpellAbility sa, List<CardState> faces) {
System.err.println("Warning: default (ie. inherited from base class) implementation of chooseCardState is used for " + this.getClass().getName() + ". Consider declaring an overloaded method");

return Iterables.getFirst(faces, null);
}

public int chooseNumber(Player player, SpellAbility sa, int min, int max, Map<String, Object> params) {
return max;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
package forge.game.ability.effects;

import java.util.Map;
import java.util.List;
import java.util.stream.Collectors;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

import forge.StaticData;
import forge.card.CardStateName;
import forge.card.ICardFace;
import forge.game.Game;
import forge.game.ability.SpellAbilityEffect;
import forge.game.card.Card;
import forge.game.card.CardCollection;
import forge.game.card.CardLists;
import forge.game.card.CardState;
import forge.game.player.Player;
import forge.game.spellability.SpellAbility;
import forge.game.zone.ZoneType;
Expand Down Expand Up @@ -49,25 +47,14 @@ public void resolve(SpellAbility sa) {
c.unlockRoom(activator, sa.getCardStateName());
break;
case "Unlock":
//List<ICardFace> faces = c.getLockedRoomNames().stream().map(face -> StaticData.instance().getCommonCards().getFaceByName(face)).collect(Collectors.toList());

Map<ICardFace, CardStateName> map = Maps.newHashMap();

for (CardStateName faceStateName : c.getLockedRooms()) {
if (!c.hasState(faceStateName)) {
continue;
}
String faceName = c.getState(faceStateName).getName();
ICardFace face = StaticData.instance().getCommonCards().getFaceByName(faceName);
map.put(face, faceStateName);
}
List<CardState> states = c.getLockedRooms().stream().map(stateName -> c.getState(stateName)).collect(Collectors.toList());

// need to choose Room Name
ICardFace chosen = activator.getController().chooseSingleCardFace(sa, Lists.newArrayList(map.keySet()), "Choose Room to unlock");
CardState chosen = activator.getController().chooseSingleCardState(sa, states, "Choose Room to unlock");
if (chosen == null) {
continue;
}
c.unlockRoom(activator, map.get(chosen));
c.unlockRoom(activator, chosen.getStateName());
break;
}
}
Expand Down
9 changes: 9 additions & 0 deletions forge-game/src/main/java/forge/game/card/CardView.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package forge.game.card;

import com.google.common.collect.Iterables;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import forge.ImageKeys;
import forge.StaticData;
Expand Down Expand Up @@ -40,6 +41,14 @@ public static CardStateView getState(Card c, CardStateName state) {
return s == null ? null : s.getView();
}

public static Map<CardStateView, CardState> getStateMap(Iterable<CardState> states) {
Map<CardStateView, CardState> stateViewCache = Maps.newLinkedHashMap();
for (CardState state : states) {
stateViewCache.put(state.getView(), state);
}
return stateViewCache;
}

public CardView getBackup() {
if (get(TrackableProperty.PaperCardBackup) == null)
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ public int chooseNumber(SpellAbility sa, String string, int min, int max, Map<St

public abstract ICardFace chooseSingleCardFace(SpellAbility sa, String message, Predicate<ICardFace> cpp, String name);
public abstract ICardFace chooseSingleCardFace(SpellAbility sa, List<ICardFace> faces, String message);
public abstract CardState chooseSingleCardState(SpellAbility sa, List<CardState> states, String message);
public abstract List<String> chooseColors(String message, SpellAbility sa, int min, int max, List<String> options);

public abstract CounterType chooseCounterType(List<CounterType> options, SpellAbility sa, String prompt, Map<String, Object> params);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -733,6 +733,12 @@ public ICardFace chooseSingleCardFace(SpellAbility sa, List<ICardFace> faces, St
return null;
}

@Override
public CardState chooseSingleCardState(SpellAbility sa, List<CardState> states, String message) {
// TODO Auto-generated method stub
return null;
}

@Override
public Card chooseDungeon(Player player, List<PaperCard> dungeonCards, String message) {
// TODO Auto-generated method stub
Expand Down
11 changes: 11 additions & 0 deletions forge-gui/src/main/java/forge/player/PlayerControllerHuman.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import forge.game.ability.AbilityUtils;
import forge.game.ability.ApiType;
import forge.game.card.*;
import forge.game.card.CardView.CardStateView;
import forge.game.card.token.TokenInfo;
import forge.game.combat.Combat;
import forge.game.combat.CombatUtil;
Expand Down Expand Up @@ -1843,6 +1844,16 @@ public CounterType chooseCounterType(final List<CounterType> options, final Spel
return getGui().one(prompt, options);
}

@Override
public CardState chooseSingleCardState(SpellAbility sa, List<CardState> states, String message) {
if (states.size() <= 1) {
return Iterables.getFirst(states, null);
}
Map<CardStateView, CardState> cache = CardView.getStateMap(states);
CardStateView chosen = getGui().one(message, Lists.newArrayList(cache.keySet()));
return cache.get(chosen);
}

@Override
public String chooseKeywordForPump(final List<String> options, final SpellAbility sa, final String prompt, final Card tgtCard) {
if (options.size() <= 1) {
Expand Down

0 comments on commit 67f5153

Please sign in to comment.