demilich1 / metastone

Hearthstone simulator written in Java with full GUI support
GNU General Public License v2.0
132 stars 80 forks source link

Doomguard incorrectly summons Silverware Golem twice without discarding it #361

Open doctorpangloss opened 7 years ago

doctorpangloss commented 7 years ago

Doomguard summons a Silverware Golem twice from your hand, as long as there is one copy, and go about its business discarding cards otherwise. It's extremely mysterious to me why it is broken based on the code below:

    @Override
    protected boolean fire(GameEvent event, Entity host) {
        DiscardEvent discardEvent = (DiscardEvent) event;
        EntityReference target = (EntityReference) desc.get(EventTriggerArg.TARGET);
        if (target == EntityReference.SELF && discardEvent.getCard() != host) {
            return false;
        }

        return true;
    }

I changed the code to this, and fixed the bug.

    @Override
    protected boolean fire(GameEvent event, Entity host) {
        DiscardEvent discardEvent = (DiscardEvent) event;
        EntityReference target = (EntityReference) desc.get(EventTriggerArg.TARGET);
        TargetPlayer targetPlayer = (TargetPlayer) desc.get(EventTriggerArg.TARGET_PLAYER);

        final int owner = host.getOwner();

        boolean targetPlayerSatisfied = targetPlayer == null
                || (targetPlayer == TargetPlayer.SELF && owner == event.getTargetPlayerId())
                || (targetPlayer == TargetPlayer.OWNER && owner == event.getTargetPlayerId());

        boolean targetSatisfied = target == null;

        if (target != null) {
            List<Entity> resolvedTargets = event.getGameContext().resolveTarget(event.getGameContext().getPlayer(owner), host, target);
            targetSatisfied = resolvedTargets != null && resolvedTargets.stream().anyMatch(e -> e.getId() == discardEvent.getCard().getId());
        }

        return targetPlayerSatisfied && targetSatisfied;
    }
doctorpangloss commented 7 years ago

The code above was updated to pass all tests.