Designing a reaction stack in a tactical battle system

Hello everyone!

So, today I want to talk about my tbs script (vx ace) once again as I'm currently designing a stack of reactions (quite similar to how Magic The Gathering works and probably games like Baldur's Gate 3).
I'm currently thinking about the best way of designing one in my script. Especially defining when should reactions be allowed during the process of an action.



To explain quickly the tbs if you are not familiar, and the script itself.
- It's a turn-based battle system: a turn may be shared among allies in which case they decide in which order they act.
- actions are used instantly: so there is no gap between deciding something and applying said thing.
- position matters: battlers are occupying cells on the map, a cell cannot be shared by two battlers at the same time. Actions must be cast on a target cell, which can restrict the actions, the cell must be at a specific distance from the user and may be blocked by obstacles. Actions may affect multiple cells around their target cell which may affect multiple battlers.

Since position matters, then battlers may move, with move points refreshing at the end of their turn, cells crossed may cost different move points and effects may move the battlers too (push them, teleport them, swap them etc.).

With this, there are three kind of effects going on during the battle:
-normal actions: use of a skill or an item on a target by a battler
-movements: a list of steps that will make a battler reach another cell
-special effects: usually event processing or other scripted effects that trigger under some conditions

Now, the question is how to represent reactions to effects with other effects:
Spoiler: Example
(The light blue words are triggers, I will cover them afterwards)

1767977442455.png

Take Ralph

1767977150041.png

Ralph may attack his enemies

1767977503675.png

Or he may decide to go to the closest town

1767977483233.png

But on his path, there may be a swamp that will trigger an event when he walks over it.

1767977845968.png

Or even worse, he might get ambushed by Ulrika, and her attack would probably kill him.

1767977903438.png

On the other hand, Bennett can cast a protective spell on Ralph right before Ulrika's attack reaches his neck.

1767977957670.png

But Ylva's seems very eager to cast a counterspell on Bennett's magic.

1767978036184.png

Did I mention Oscar ready to jump attack any of the two magic users?

Sometimes, an effect may trigger while one is not over, for instance, Ralph is walking but the swamp event should trigger before Ralph finishes his walk.
To implement this interruption system, I use a stack of effects.

A stack or LIFO (for Last In First Out), is a storing object such that when we want to get an object back, it removes and returns the last object stored in it, in other words, the latest entries are handled first while the earliest are handled last.
So with my stack of effects, I could have at some point this situation:
Code:
------------------------------------------------------------------------------------------------------------------------------------------------
Head of the stack | Ralph's movement | Swamp Event | Ulrika's Attack | Bennett's Protection | Ylva's Counterspell | Oscar's Jump Attack | <- newer reactions
------------------------------------------------------------------------------------------------------------------------------------------------

Spoiler: Here is an detailed explanation of how the stack elements happened
--------------------
Head of the stack |
--------------------
This is my stack at the beginning of the scene. When Ralph start walking, we add the movement block.

Once Ralph reaches the swamp tile, he triggers both the event and Ulrika's attack so a choice based on the fastest move must be done, here we suppose that the attack was faster than the event and so it should resolve first by taking the priority:
--------------------------------------------------------------------------
Head of the stack | Ralph's movement | Swamp Event | Ulrika's Attack |
--------------------------------------------------------------------------

Since Ulrika's attack triggers Bennett's Protection, then we get this stack:
--------------------------------------------------------------------------------------------------
Head of the stack | Ralph's movement | Swamp Event | Ulrika's Attack | Bennett's Protection |
--------------------------------------------------------------------------------------------------

(In another universe, the swamp would have resolved first and we would have gotten this stack instead: )
Spoiler
---------------------------------------------------------------------------------
Head of the stack | Ralph's movement | Ulrika's Attack | Bennett's Protection |
---------------------------------------------------------------------------------

We once again get another ambiguity if Ylva and Oscar reacted to Bennett's cast, in which case the faster of the two will take priority (here let's say, Oscar's jump attack).

And then... well we just resolve the effects one by one from the latest entry. But it is important to understand that some effects started before being interrupted by others, like Ralph's movement that walked two tiles before being stopped by the swamp and Ulrika.
Or Ulrika's attack which was cast: it's cost was paid, and it was even assumed that her attack's hit had been decided before Bennett's interruption.
Similarly, Bennett's
spell cast assumed to have been paid before Ylva's decision to counter it along with Oscar jump attack.
What I want to highlight is that each action can be interrupted at many moments with triggers:
-the movement could be interrupted when it
starts, when it reaches a cell, when it leaves it or when it's over. (here, the swamp triggered when Ralph reached its cell, and in this universe, Ulrika also attack at this moment, but we could have decided to perform Ulrika's attack when Ralph aimed to leave the cell)
-an event can be interrupted any time it does something that creates another system effect, like
forcing an action, changing states, adding or removing battlers. It's very large, but in general it can be assumed that a trigger occurs only at the end of an interpreter command.
-the
normal actions, however, are a little bit more complicated to handle for me. They are meant by default to be used as a whole before eventually calling a common event or triggering the troop's interpreter pages. My description, however, implies the creation of more triggers. And every time a trigger is made, I must set some stop and preservation of the action on the stack. In other words, I have to create, between each interruption, a function processing the actions.

That's where I have a problem as rewriting methods contradicts one of the goals of my script which was vanilla-friendliness: since I might have to rewrite most of the action processing, it can break compatibility with many scripts.

For reference, here is the default damage flow of ace and later engines (with which I am not really ok to change).
And here is the pseudo-code of the vanilla action processing (which I must rewrite anyway because I made my own battle scene):
Code:
def perform_action(user)
  prepare_action #only handles confused action selection
  if action.valid?
    display_action_cast
    user.use_item #pay its cost
    targets = make_targets #decide which battlers are affected by action, also handles random choices
    show_animation(targets) #display one or multiple animations
    for t in target
      invoke_item * item.repetitions
    end
  end
end

My questions are:
Should I go for this stack system? (actually it's already done, except for the actions which currently can only be interrupted before being cast, but I'm open to better ideas ahah)
How much should I embrace it? By that I mean, where should I put the triggers for the actions to maximize possibilities while still maintaining as much vanilla code as possible? I'm thinking of triggers that can happen for each target of an action (for area of effects or random attacks).
Small bump as I'm still looking for suggestions, especially if you already have a good idea of possible reaction moments to skills like in Baldur's Gate 3 or similar games.

For now I think I will opt for a split of the code:
-I will keep in the core my current reaction system with no possibility to react to skills during their effects (but it remains possible to react to them before the targets are made and after all effects), this will preserve compatibility or at least easy patching with other scripts.
-I will make yet another addon to break the actions into smaller parts and to allow a more precise reaction system, thus putting breaks during the damage flow and between each target of the action: if I do it correctly, I could even change a bit how the damage flow is handled. This addon would then need high patching with other scripts to ensure compatibility.

Original thread: https://forums.rpgmakerweb.com/threads/designing-a-reaction-stack-in-a-tactical-battle-system.181860/