Middlewares

Description

The middlewares.lua file specifies which functions are executed before and after a formula.

There are a total of 4 middleware groups per function group (OnCast / OnHit).

Order
Name
Description
1

OnPre

Setup formula processing structures and apply certain conditions before any formula is executed (e.g. if target has reflect, redirect action).

2

OnMain

Plan changes that should be applied by a formula (e.g. 4 for Cure). Unlike other middleware groups, these functions are listed in formulas.lua.

3

OnPost

Apply certain conditions after any formula is executed (e.g. damage spikes augment).

4

OnEnd

Summarize all planned changes and update the caster's and target's properties (e.g. hp, mp, ...).

There are special cases where only OnHit functions are executed, while in others, they are not executed at all. Additionally, some middleware groups are unique to specific action types, like Quickenings. These details are further explained in the later pages (Formula Workflow).

If the formula processing is skipped within a function, then all upcoming functions of the same middleware group will also be skipped.

Furthermore, if the skipped function was part of the OnCastPre middleware group, then all proceeding formula functions of the OnCastMain middleware group will also be skipped. The same applies to the OnHitPre middleware group and its proceeding formula functions (OnHitMain).

Layout

local middlewares = {
  --more groups
  onHitPre = {220, 221, 223, 224}
}

return middlewares

Every table entry uses a group identifier (e.g. onHitPre ) as the key and a table of formula functions (e.g. {240, 241}) as the value.

Usage

Let's say you wanted to implement a feature where if a character casts an action, but falls asleep before it hits the target, it should have no effect.

You could accomplish this by creating the following function:

local function func(formula, caster, target, functions, classes, helpers)
  if caster.temporaryStatusEffects.sleep == 1 or caster.permanentStatusEffects.sleep == 1 then
    formula.skipState = 1
  end
end

return func

Afterwards, you would need to give it an identifier (e.g. 210), move it in the functions directory and finally add it to the function list of all on hit formulas in the formulas.lua file.

local onCastFormulas = {
  --no changes needed
}

local onHitFormulas = {
  [1] = {210, ...}
  [2] = {210, ...}
  --remaining functions
}

return onCastFormulas, onHitFormulas

As you can see, doing that for all formulas would be really troublesome. That's where middlewares come in handy.

Now instead of doing the latter, you can simply add the function's identifier to the function list of the OnHitPre middleware group in the middlewares.lua file.

local middlewares = {
  onHitPre = {..., 210}
}

return middlewares

Keep in mind that you want to expand the current list with an additional function, not completely replace it.

That's pretty much it. So whenever you want to apply a function on a global scale, you can use middlewares.

Last updated