Mechanics of Thornmail, Armor, Magic Resistance

The description of Thornmail says "On being hit by basic attacks, returns 30% of damage taken as magic damage." I'd like to know how the defender's armor and the attacker's magic resist affect that number. For extra credit, do the defender's magic pen or attacker's armor pen factor in as well?

Damage | DefArmor | AttMagRes | DefMagPen | AttArmPen | ThornmailDamage
-------|----------|-----------|-----------|-----------|----------------
400    | 0        | 0         | 0         | 0         | 120¹
400    | 100      | 0         | 0         | 0         | 120? 60?
400    | 0        | 100       | 0         | 0         | 120? 60?²
400    | 100      | 100       | 0         | 0         | 120? 60? 30?
400    | 100      | 100       | 50        | 0         | ?????
400    | 100      | 100       | 0         | 50        | ?????
400    | 100      | 100       | 50        | 50        | ?????

¹ I know this example is dumb, since Thornmail grants +100 armor. But it's a simple base case

² Again, 0 armor is nonsensical. But I wanted to isolate the attacker's MR


EDIT:

I understand how armor (and MR) affect damage: (effectiveDamage = rawDamage * (100/(100+armor)). This question is not about how to make each calculation; it's about the order in which the calculations are derived. For example, I know that 400 raw damage on 100 armor gives 200 effective damage. I also know that 400 raw damage with 50 armor pen on 100 armor gives 300 penetrated damage. But which value does thornmail use? Is it .3 * effective or .3 * raw or .3 * penetrated? And from that point, does the attacker's magic resistance reduce incoming thornmail damage? If so, does the defender's magic pen reduce the attacker's MR?


Solution 1:

It's rather simple, actually.

Consider a hypothetical scenario where Ashe attacks Garen (who is wearing Thornmail). Ashe has 200 attack damage and 30 MR (Ashe has no Magic Resistance items), and Garen has an effective armor of 100 (Because Ashe has armor penetration runes).

Case 1 - 100 Armor Garen, 30 MR Ashe

Ashe shoots an arrow, which would deal 200 damage. Thornmail uses this base damage to calculate the 30% retaliation. 30% of 200 is 60, so Ashe takes 46.15 damage (30 MR mitigates this to 77% of the base) from the Thornmail and deals 100 damage to Garen (100 armor reduces physical damage by 50%).

Case 2 - 200 Armor Garen, 30 MR Ashe

Garen wises up, and buys another 100 armor, and Ashe shoots him with another arrow. Ashe still takes 46.15 damage (same AD, same MR), but Garen only takes 66.67 damage.

Case 3 - 200 Armor Garen, 0 MR Ashe

But Garen is still not happy, so he buys a "Hypothetical 30 MR penetration item". Thornmail damage treats Garen as the origin, so it uses his magic penetration for the attack (same as proc items). The next time he runs into Ashe, Thornmail means Garen takes 66.67, and Ashe takes the full 60!

So in summary:

Thornmail Damage Return is based on the attacker's effective AD (can potentially be higher than just their AD for On-Hit attacks, like Mystic Shot or Parrrley, which will proc Thornmail), and the attacker's MR (reducible by the defender's magic pen).

The exact formula is pretty easy: Attack Damage * 30% * (1-MR%Reduction)