Custom Damage Calculator

Altruist's combat system uses IDamageCalculator to compute damage. The default formula is attackPower - defensePower (minimum 1). Replace it with your own.

Implement IDamageCalculator

[Service(typeof(IDamageCalculator))]
public class MyDamageCalculator : IDamageCalculator
{
    public int Calculate(ICombatEntity attacker, ICombatEntity target)
    {
        var attack = attacker.GetAttackPower();
        var defense = target.GetDefensePower();

        // Custom formula with variance
        var baseDamage = (int)(attack * 0.7f - defense * 0.3f);
        var variance = Random.Shared.Next(-5, 6);
        return Math.Max(1, baseDamage + variance);
    }
}

The [Service(typeof(IDamageCalculator))] attribute replaces the default calculator automatically.

Examples

// Level-scaled damage
public int Calculate(ICombatEntity attacker, ICombatEntity target)
{
    var levelBonus = attacker is PlayerEntity p ? p.Level * 2 : 0;
    return Math.Max(1, attacker.GetAttackPower() + levelBonus - target.GetDefensePower());
}

// Percentage-based (MMO style)
public int Calculate(ICombatEntity attacker, ICombatEntity target)
{
    var raw = attacker.GetAttackPower();
    var reduction = target.GetDefensePower() / (float)(target.GetDefensePower() + 100);
    return Math.Max(1, (int)(raw * (1f - reduction)));
}

Note:

See Combat System for the full combat API including AoE sweeps and combat events.