Skip to content

The hit and heal pipeline ​

Every hit and every heal goes through the same steps. CombatManager.apply_damage and apply_healing are the entry points; everything that damages or heals, from an ability to a damage reflection, ends there. They return a typed result, never a loose dictionary.

The result ​

DamageResult is made for the hit and filled in as the pipeline goes. HealingResult is its twin.

MemberMeaning
rawThe number the effect asked for
doneAfter the attacker's phase
takenAfter the target's phase, the number that goes to the pools
triggers_done, triggers_takenTriggerRecords that fired in each phase
steps_done, steps_takenThe ModifierSteps that changed the number, in order
redirected, absorbed, health_damageWhere the number went
outcomeHIT, AVOIDED, IMMUNE, FULLY_REDIRECTED, TARGET_DEAD, FAILED, NEGATED or MISSED
chain_depth0 for a normal hit, one more for damage caused by reacting to a hit
target_diedThe hit killed the target

Consumers choose which number they want with a DamageBasis: RAW, AFTER_DONE, AFTER_TAKEN or HEALTH_ONLY (what actually reached health). Threat, leech and damage reflection each have a setting for it, and result.get_amount(basis) returns the number.

A script error inside the target's processing is turned into a FAILED result by CombatManager, so a crash is never mistaken for a hit.

The hit ​

CombatManager.apply_damage(attacker, target, raw, damage_type, effect_instance, can_be_avoided, chain_depth)
  1  validate the participants (both need a StatsComponent)
  2  make the DamageResult
  3  PHASE 1, attacker: DamageDoneCalculation.apply_to(attacker stats, result)    -> result.done
  4  target.take_damage(result)                                                   -> StatsComponent.take_damage
       a  already dead?                         TARGET_DEAD
       b  redirection effects (guardians): redirect a share, reduce by a percentage
       c  nothing left?                         FULLY_REDIRECTED
       d  emit damage_incoming
       e  damage immunity?                      IMMUNE
       f  PHASE 2, target: DamageTakenCalculation.apply_to   -> result.taken
                avoid trigger fired?            AVOIDED
       g  project options: minimum damage, round damage, "zero is not a hit"  (NEGATED)
       h  pools take damage through the damage layers
       i  emit damage_taken, then the death check
  5  announce the result: damage_pipeline_completed, entity_hit_dealt, entity_hit_received

run_damage_done(attacker, raw, type, effect) runs only step 3 and returns the result. Effects that put a number somewhere other than on a target use it: the pool and stat modifier effects.

A calculation ​

Both phases are a CalculationBase subclass. run_phase returns a CalculationPhase (value, triggers, steps, avoid), which the subclass copies into the result.

run_phase(stats_component, start_value, context, formula_context, effect_instance)
  rules   = trigger rules of the effect causing the hit + every TriggerRuleStatEffect the entity has points in
  PHASE 1 triggers
      avoid triggers roll first (damage taken only). One fires: value = 0, the phase ends
      the other triggers roll, in stat id order, and set their tag in the context
      tags a rule forces ("always") fire without a stat
  PHASE 2 modifiers
      the universal order of every CalculationModifierStatEffect in the database,
      highest priority first. A fired tag with an Application takes its place in the same order
      a stat the entity has no points in is skipped; a modifier that requires a tag only runs when it fired
  value = max(value, 0)

The stat ids are sorted before every loop, so the result never depends on the order of a dictionary.

The universal order is built once by CombatCalculations from all the stats in the database, for each of the four calculations (build_order: priority, then stat id, then the position of the effect in the stat). It is a cache; the Calculations editor and any stat change invalidate it.

Boosts ​

After its modifiers, a done calculation (damage done, healing done) calls StatsComponent.apply_done_boosts: for every active AbilityBoostEffect instance on the doer whose lists match the ability_id and effect_ids of the context, the number becomes (number + flat x stacks) x (1 + percent x stacks / 100) and a ModifierStep named after the effect is recorded.

Rules ​

_collect_rules builds the TriggerRuleSet of a phase from three places:

  1. the trigger_rules of the effect that causes the hit or heal,
  2. the TriggerRuleStatEffects of the entity that runs the phase whose applies_to is OWNER,
  3. the TriggerRuleStatEffects of its opponent (context["opponent"]) with OPPONENT_ACTING_ON_ME when the phase is a done phase (Damage Done, Healing Done) or OPPONENT_DEFENDING_AGAINST_ME when it is a taken phase. The conditions of those effects are checked with the roles swapped: the opponent is the owner.

A rule that names no tag can name a kind (TagKindFilter): it then matches every tag of that kind (TriggerRuleSet takes the kind of the tag it is asked about). A rule that names a tag matches only that tag.

The hit roll ​

A miss is not part of the hit. Before the pipeline above, an ability that can miss makes a hit roll (HitRules), once per use and enemy. EffectInstance.start_effect asks HitRules.needs_roll(instance), which is true for an effect of an ability (effect_owner.can_miss()) that is not a composite, not applied to the user, and aimed at an Entity that is hostile or neutral to the originator. HitRules.outcome_for then rolls the first time and keeps the outcome in the shared CastRecord under the id of the target, so every effect of the use (the children of a composite included) gets the same answer.

OutcomeResult
HITThe effect goes on
GLANCINGThe effect goes on with hit_multiplier set to the glancing multiplier (1 - reduction / 100). DamageEffect and HealEffect multiply their raw number by it. With glancing_other_effects_apply off, effects that are not damage or healing are rejected ("Glancing hit")
MISSThe instance is rejected ("Missed") and cleaned up, and CombatManager.announce_miss emits a DamageResult with outcome MISSED so the floating text, the log and the signals work as for any other result

The chance is HitRules.get_chance(attacker, target, ranged, effect_instance): the base chance of GameplayConfig for melee or ranged, plus the HitChanceStatEffects of the attacker with side ACCURACY, minus those of the target with side EVASION (the stat effect filters and conditions are checked with the context of the hit), plus get_level_gap_change, clamped to the minimum and maximum. ranged comes from AbilityInstance.is_ranged_attack(). GameplayConfig.level_gap_mode is NONE, PER_LEVEL, TABLE (the row with the largest key not above the gap) or FORMULA (a CalculationFormula that gets the gap). Dodge, parry and block stay in the damage taken calculation. See the Gameplay Config page for the settings.

Context and tags ​

The context dictionary holds what the stat effects look at:

KeyMeaning
damage_typeThe damage type id of the hit
calculation_target"Damage Done", "Damage Taken", ...
is_damage_calculation, can_be_avoided, in_combatThe kind of hit and the combat state
owner, opponentThe entity being calculated and the other side
schoolThe school id of the ability (else of the effect) that causes the hit, 0 for none
is_rangedTrue when the ability that causes the hit is a ranged attack (see HitRules)
ability_idThe id of the ability that causes the hit (the effect owner, when it is an ability), else 0
effect_idsThe id of the effect that causes the hit and of every effect around it (parent_instance chain, at most 16)
is_periodicTrue for a tick of a damage or healing over time (the effect instance has a tick interval)
the tagtrue when a trigger with this tag fired
"magnitude:" + tagThe magnitude of the fired tag

The tags the attacker fired on this hit are also visible to the defender's modifiers. Resilience can cut critical damage, armor can be pierced.

The heal ​

CombatManager.apply_healing(healer, target, raw, effect_instance, chain_depth, only_pool_id)
  1  validate, make the HealingResult
  2  PHASE 1, healer: HealingDoneCalculation                  -> result.done
  3  target.take_healing(result)
       PHASE 2, target: HealingTakenCalculation               -> result.taken
       heal absorbs soak up healing (HealAbsorbEffect)        -> result.absorbed
       pools that receive healing are filled                  -> result.applied
       death check: a heal can revive                         -> result.target_revived
  4  announce: healing_pipeline_completed, entity_heal_dealt, entity_heal_received

only_pool_id limits the heal to one pool (restore mana only).

Reactions ​

Leech and damage reflection are reactions to a resolved hit. CombatReactions implements both once: it reads one number of the hit (a DamageBasis), turns it into an amount and applies it through the real pipelines (apply_healing, apply_damage), so healing-done and damage-done modifiers, the log and the result signals all apply. The reaction is called by the effect classes and by the stat effects (PoolRestorationStatEffect, ReactiveDamageStatEffect).

A reaction hit has chain_depth + 1. The game settings set the longest chain (CombatOptions.max_reflect_chain); a hit at that depth does not react again, so two reflecting entities cannot bounce a hit for ever.

Project options ​

CombatOptions reads the game settings once for the pipeline: a minimum damage, whole numbers only, and whether a hit reduced to 0 still counts as a hit. They apply after the defender's phase.

Immunities and statuses ​

A damage immunity is checked before the defender's phase. Status effects go through StatsComponent.apply_status_effect, which asks for immunity, tenacity (the status duration gain channel) and diminishing returns, and answers {can_apply, effective_duration, ...}. School immunities are checked when an effect is started on a target (EffectInstance.start_effect). See Immunities: how they are built.