The counterspell ability is giving me trouble. I've gotten it to behave relatively well. If you remove the lua condition, then it behaves like this:-Spell is cast, ability becomes activable.-Activate ability-Choose target-Choose card with target's CMC to discard-Card is discarded-Timer-Spell is countered.

That's almost perfect, except that if you don't have a matching card in hand, it can still be activated and you can still target the spell. The ability never goes on the stack, because as soon as it gets to the cost, it registers that you can't pay it, and it invalidates.

I added the lua condition to try and make the spell only able to target spells whose CMC matches a card in your hand. This worked fine, and now that's exactly what it does. It now works like this:-Spell is cast, ability becomes activable IF it's a valid target-Activate ability-Choose valid target-Choose matching card to discard-Card is discarded-Timer-Spell resolves with no effect

I put in a debug message (which you'll need to replace, remove, or define, if you don't have the CW, it's just a shorthand message function), and it never runs. I even removed the entire code block in the RTA and replaced it with "MTG():MessageAllPlayers("RTA")", but it didn't appear. I'm not sure what would cause that to happen, and now I'm at a loss. Any advice?

The problem is probably that you are clearing the filter inside of a filter. You will probably have to rewrite your Lua condition to use a loop instead of a filter. There is only a single filter for a single card at any given time, when you call ClearFilter() it clears the current filter (regardless of where it is) and returns a new filter. The ClearFilter() function is essentially and alias for:

<FILTER_CONDITION id="1"> local Spell = FilteredCard() if Spell ~= nil then SpellCMC = Spell:GetConvertedManaCost() local bFoundValid = false local nCount = EffectController():Hand_Count() local i = 0 for i = 0, nCount-1 do local oCard = EffectController():Hand_GetNth(i) if (oCard ~= nil) then if (oCard:GetConvertedManaCost() == SpellCMC) then bFoundValid = true break end end end return bFoundValid end return false </FILTER_CONDITION>

RiiakShiNal wrote:The problem is probably that you are clearing the filter inside of a filter. You will probably have to rewrite your Lua condition to use a loop instead of a filter. There is only a single filter for a single card at any given time, when you call ClearFilter() it clears the current filter (regardless of where it is) and returns a new filter. The ClearFilter() function is essentially and alias for:

<FILTER_CONDITION id="1"> local Spell = FilteredCard() if Spell ~= nil then SpellCMC = Spell:GetConvertedManaCost() local bFoundValid = false local nCount = EffectController():Hand_Count() local i = 0 for i = 0, nCount-1 do local oCard = EffectController():Hand_GetNth(i) if (oCard ~= nil) then if (oCard:GetConvertedManaCost() == SpellCMC) then bFoundValid = true break end end end return bFoundValid end return false </FILTER_CONDITION>

I tried your suggestion, but no change. The RTA still doesn't run. I removed the lua condition just to make sure that was in fact the source of the problem, and it worked. I then realized what the issue is and tested to confirm it. The problem is that once you discard the card from you hand to pay the cost, the target is checked for validity when the ability resolves, but now you don't have any cards for that condition to return true, so the target is invalid, and the ability fizzles.

I'm thinking that since the ability is successfully put onto the stack, a PTA should fire. So, an EffectDC int that informs the lua condition to return true because the cost was already paid should be enough to fix it.

EDIT: Tested with the PTA and it now works like it should. I didn't notice before that it only failed when the discarded card was the only one with that CMC in my hand. Kozilek, the Great Distortion is now working. Thank you.

You know, this is interesting: Magic Duels has Kozilek as an official card and it uses a different approach, but now that I think about it, it probably has a flaw.Instead of using a standard discard cost, it uses a generic cost that allows you to choose only cards from your hand that have the same cmc as your target. The PREREQUISITE block of the generic cost checks that you have at least a card in your hand with cmc that matches a spell on the stack.But what happens when there are multiple spells on the stack? Let's say that there's a spell with cmc=3 and another one with cmc=4. You have only one card in hand with cmc=4: the PREREQUISITE block returns true. There are no restrictions on the TARGET block so you are allowed to choose the spell with cmc=3... boom! I haven't tested what happens, but I'd guess it would end countering the spell without discarding any card.

Here's the official implementation, for reference:

Official implementation of Kozilek's activated ability (probably flawed) | Open

In case someone wants to test the code in a previous installment, the only non-retro-compatible part should be QUERY_FLAG_DISCARD, which in Magic Duels instructs the game on making the cards with madness glow blue instead of red (or at least that's what I think it does).

thefiremind wrote:You know, this is interesting: Magic Duels has Kozilek as an official card and it uses a different approach, but now that I think about it, it probably has a flaw.Instead of using a standard discard cost, it uses a generic cost that allows you to choose only cards from your hand that have the same cmc as your target. The PREREQUISITE block of the generic cost checks that you have at least a card in your hand with cmc that matches a spell on the stack.But what happens when there are multiple spells on the stack? Let's say that there's a spell with cmc=3 and another one with cmc=4. You have only one card in hand with cmc=4: the PREREQUISITE block returns true. There are no restrictions on the TARGET block so you are allowed to choose the spell with cmc=3... boom! I haven't tested what happens, but I'd guess it would end countering the spell without discarding any card.

Here's the official implementation, for reference:

Official implementation of Kozilek's activated ability (probably flawed) | Open

In case someone wants to test the code in a previous installment, the only non-retro-compatible part should be QUERY_FLAG_DISCARD, which in Magic Duels instructs the game on making the cards with madness glow blue instead of red (or at least that's what I think it does).

Getting a head start on a Magic Duels Core Fixes mod?

The code they used suffers from the issue I was having before I added in the target lua condition. You're allowed to choose a target for which you can't discard a matching card, but upon doing so, the ability exits. So, it's not bugged such that you can counter illegal spells, but it is a bit off. There are no spells that I know of that act like illusions on the stack (something happens as a result of merely being targeted), so I can't easily test if /that/ would behave wrongly.

Interesting to know I got mine working better than theirs thanks to Riiak (while his suggestion didn't fix the issue I was seeing, it was still the right way to go).

Awhile back, I got annoyed at having 6 different soldier tokens that are identical except for their art and their filename, so I made CW_Tokens(). It produces random tokens from the base name given. Feed in "SOLDIER_C_1_1_W" and it'll make a random token from TOKEN_SOLDIER_C_1_1_W_CW_1 to TOKEN_SOLDIER_C_1_1_W_CW_6 (there's a table lookup for how many there are).

However, this left a different minor annoyance: stacking. Because they aren't identical, they don't stack. I thought today I'd give a go at solving the issue. I decided to try making TOKEN_SOLDIER_C_1_1_W_CW_1 (I'd make it something slightly different in the end, but this was for testing), and that token would spawn a random token and the use its copiable values instead. I was hoping that the fact that they're the same card (same filename) and everything else EXCEPT art, that they'd stack up. But, after some time getting it to work right and copy the image, I discovered that it didn't work. They still won't stack.

Can anyone think of a way to make them have different art but still stack? I'm out of ideas...