Here's how I'd go about it...
Basically, the way to organize this is with a (slightly modified) stack: Last in, first out. (There's a reason M:tG uses the term "stack" to describe its timing system--it's based on the computer science term, a fact I just now realized. 4E works much the same way.) Our stack will consist of an array of in-game events, plus four methods:
AddInterrupt, AddReaction, ResolveEvent, and
ExecuteStack.
AddInterrupt takes an event as parameter. It inserts that event at the
end of the array. We will use this method when an immediate interrupt or opportunity action is declared, or when an action is declared on an empty stack.
AddReaction takes an event as parameter. It inserts that event in the
second-to-last spot in the array. We will use this method when an immediate reaction is declared, or when an event creates another event (e.g., a successful attack roll creates a damage roll).
ResolveEvent looks at the
last event in the array and does the following:
- If event has not been resolved: Resolve it (but do not apply the result), then check to see if anyone has a response. If someone responds, exit ResolveEvent (do not do step 2).
- Apply the result of the event and remove the event from the array.
ExecuteStack checks to see if anyone wishes to add an event with AddInterrupt or AddReaction. It continues until no one wishes to add an event; then call ResolveEvent. Repeat until the array is empty.
A key thing to note is that I say "event" rather than "action." This is going to be an important distinction, because a lot of actions involve multiple events--typically an attack roll and a damage roll, the latter being contingent on the result of the first.
So, with the example above, we start with an empty array. Since the wizard is casting into an empty stack, we call AddInterrupt to put the spell in the array:
{ Spell }
Then ExecuteStack. Two enemies respond with opportunity attacks, #1 and #2. (As to which of them declares first, I'd go in reverse initiative order.) These are treated as interrupts, so we use AddInterrupt again:
{ Spell, OA #1 }
{ Spell, OA #1, OA #2 }
Nobody else has anything to declare at this time, so we proceed to ResolveEvent. The event at the end of the array is OA #2.
- OA #2 has not been resolved, so we resolve it by making an attack roll (say it hits). Then check to see if anyone has a response. No one does.
- Apply the result. This produces a damage event, so we call AddReaction, resulting in this:
{ Spell, OA #1, OA #2 Damage Roll, OA #2 }
Then remove OA #2 from the stack:
{ Spell, OA #1, OA #2 Damage Roll }
ResolveEvent finishes. We're still in the middle of ExecuteStack, and the array is not empty, so we check for new events (none) and ResolveEvent again. This time it's the damage roll. We resolve it (say, 6 damage); check to see if there's a response (no); apply the damage (reduce caster's hit points by 6); and remove the event.
{ Spell, OA #1 }
Go through the same process with OA #1. Maybe this one misses, so we never get a damage roll event; we just move straight on to the spell.
{ Spell }
Check for new events, then ResolveEvent once more. This is where things get tricky. We resolve the spell; the attack misses. Then we check to see if there's a response--and get a yes! The warlord wishes to respond with an interrupt. So we call AddInterrupt and exit ResolveEvent.
{ Spell, Warlord's Reroll }
Notice that at this point,
the spell event is resolved. The attack roll has already been made and its value is sitting in Spell. This is important, because if Warlord's Reroll is somehow prevented, the original attack roll should apply--we don't want to come back to Spell and start all over!
Anyhow, we're still in ExecuteStack and the array is
still not empty. So we look for new events (no) and ResolveEvent yet again. This time it's Warlord's Reroll. We resolve it by re-doing the attack roll for Spell. Then we check to see if there are any responses and get none. Next, apply the result; go into Spell and change that attack value sitting there to whatever Warlord's Reroll produced. Finally, remove Warlord's Reroll from the array.
{ Spell }
Back here again. We call ResolveEvent. It looks at Spell and sees that
this event is already resolved; there's no need to do it over. So it proceeds directly to applying the result. Say the spell hits, we have to call AddReaction to insert a damage roll:
{ Spell Damage Roll, Spell }
And then remove Spell from the array:
{ Spell Damage Roll }
ResolveEvent one more time, to deal with the damage roll--it should be pretty clear by now how this goes. ResolveEvent finishes, Spell Damage Roll is removed from the array, and the array is empty. ExecuteStack ends.
(And
damn, that was a lot more complicated than I thought it would be when I started writing it. I don't even want to think about what happens if the spell has multiple targets.)