Combat

You can engage in combat with Monsters and, if you are in a pvp-enabled area, other players. You can not attack NPCs and NPCs never attack you.
Attacks against an enemy (either Monsters och another player) is done by issuing commands that affect HP of the enemy, which means that if you are in Smalltown, where PvP is disabled, you cannot heal other players.

When issuing an attack against a monster or player the following phases are gone through on the server, in order:

* Command comes in
* Hit Chance and Damage are calculated
* If the attack hits, damage is applied

The damage application phase might seem weird to have it specially, but there are situations when this separation of phases are meaningful, since some damage might be reduced a specific phase or non-obvious situations might happen that is explained by this.

## Damage Types

All commands have a specific damage type that classifies the type of the damage that will be dealt. There are six types of damage that are divided in two types of groups, melee and magic.

* **Melee**
	+ *Blunt*
	+ *Slashing*
	+ *Pierce*
* **Magic**
	+ *Light*
	+ *Dark*
	+ *Nature*

Armor can either protect against '**all damage**', '**melee**' or '**magic**' which means that they will protect against both groups, just the damage types under melee or magic respectively. The difference between them is mainly Monsters weakness and resistance to them and that Melee is reduced by def-stat, while magic is reduced by int-stat.

## Accuracy

All attacks have chance to hit which is based on the accuracy of the command use, the dex stat of the attacker and the agi stat of the defender. Equipped items can also have a raw accuracy or evade bonus that will affect the accuracy greatly. In general you can add up the Skill base accuracy with any eventual accuracy bonuses and withdraw the eventual evade bonuses of the defender. After that dex of the attacker and the agi of the defender are calculated as the square root of the stat times 40. The value of the defender is then withdrawn from the attackers and the resulting value is added to the hit chance. The value might be negative if the defenders agi is higher than attackers dex.

The final value is then clamped between 1% to 99% so there is always a chance to hit, or miss, no matter how large the difference is.

The full algorithm with examples can be seen here:

* Take the base accuracy from the skill used, normally 80 but might vary.
* To this you add accuracy bonus of any items for the attacker.
* Then you add the following using the attackers dex to this: `sqrt(1+((dex-10)*40))`
* Multiply the accuracy with the grace factor, if there is one. Motivate and Grace affects this.
* This is the attackers accuracy.

* From this you withdraw the defenders evade.
* Then, just like the attackers dex is used, we withdraw based on defenders agi: `sqrt(1+((agi-10)*40))`
* If the hit chance would be below 1%, then it is set to 1% and if the hit chance is above 99% it is set to 99%.
* This is the final hit chance percentage.

**Example 1**:
Player1, dex 15, with a Shortsword attacks a Slime (agi 10) with Slash:

This is the accuracy of Player1 and from that we withdraw 1, based on the slimes agi (`sqrt(1) = 1`) so the final hit chance will be 88%.

**Example 2**:
The same Player1 from example 1 now attacks a Harpy (agi 50) with Slash.
The attack accuracy is the same but the Harpy withdraw `sqrt(1+((50-10)*40)) = sqrt(1601) = 40` from that and the hit chance ends up at 89-40=49%.

**Example 3**:
Player2 has dex 100, used Precision Strike carrying a Bloodfang and a Ranger Cape against Player3, with agi 130 wearing Spectral Cloak and Spectral Hood.

## Attack damage

If an attack hits, it will do damage to the defender based on the skill used. The attack damage can be reduced by armor and the defenders Int or Def stats. The attack damage is then applied to the defender in the next step where further reduction might happen or it could be avoided altogether.

The short version is that attack damage is calculated based on:

`(base damages - resistance) * sqrt(attacker scaling factor / defenders scaling factor)`

Armour usually protects against a specific Damage Type and will not give any protection against other. Most attacks have a scaling factor that is multiplied with base damage of the Skill together with the base damage of the eventual weapon used. **Note that the scaling factor is only applied if weapon used has Influence on the stat used by the Skill**, otherwise only the Skill base damage is used. Unfortunately Influence is not shown anywhere in game but it is listed in the wiki for weapons. The attack scaling factor is countered by the defenders Def or Int, depending on if the attack deals melee damage or magic damage.

**Currently, the exact algorithm how to calculate the damage is not fully known since there is a small difference between the local server and the official server used by online and steam client. The difference is minuscule and could be due to a raise of base damage for all str and int based skills with 1, but it is currently unknown. Extensive testing has been done to confirm the results below though so it should be a fairly good guide.**

To calculate the damage dealt, follow the steps below by first calculating base damage:

* Start with the skill base damage.
* If the attacker is a Monster, add power level.
* Multiply the damage with the Grace factor, if there is one. Motivate and Grace affects this.
* If the skill scales with a stat AND the weapon has that stat as an Influence, add the weapon base damage.
* For each equipment protecting against the Damage Type of the attack, remove the equipment resistance.
* Adjust the base damage if attacker wears a Shadow Mask or uses Smite.

Then calculate the scaling factor:

* For each scaling stat, sum them up as `(stat-10)*0.1` and then divide by the nr of scaling stats and add 1.
* For the defender, calculate the value of defending stat (def or int) by `1+((stat-10)*0.1)`.
* Divide the scaling factor stat of the attacker with the scaling stat of the defender, based on calculations above.
* If the scaling factor is below 0.1, set it to 0.1.
* Take the square root of the result from above and use that as scaling factor.

Then to get the final result:

* Multiply the base damage with the scaling factor and round it to nearest integer.
* If the defender is weak against against the Damage Type, multiply the final damage by the weakness factor.
* If the defender is strong against Damage Type, divide the final damage by the resist factor.

**Example 1**:
Player1, str 15, with a Shortsword hits a Slime (def 10) with Slash.

First we calculate the base damage:

 15 | Shortsword has Influence Str, Slash scales with str so add Shortsword base damage of 7

Base damage is 15, since the player is not graced. Now we calculate the scaling factor:

Then for final step we multiply the scaling factor with the base damage and round it, 15\*1.23=18.

Player1 will deal 18 damage to the slime since Slimes are not weak or strong against any damage.

**Example 2**:

The same Player1 from example 1 now attacks a Pirate (def 12, Strong against slashing (x1.5)) with Slash.
The base damage is the same but the scaling factor changes:

Finally we calculate base damage \* scaling factor and get 15\*1.12=16.8 that is rounded to 17.
We now have the final damage done, but since Pirates are strong against slashing damage, we divide 17/1.5=11 after rounding and this is the actual damage dealt to the Pirate.

**example 3**:

Player2 has dex 100, used Precision Strike carrying a Bloodfang and a Ranger Cape against Player3, with def 50, wearing Spectral Cloak, Spectral Hood and Bone Slippers. Player2 is lucky and get 80 base damage from Precision Strike instead of 20. Neither player is graced.

First we calculate the base damage:

 89 | Player two has Spectral Cloak (4), Spectral Hood (3) and Bone Slippers (8 for melee) equipped. Remove 4+3+8=15 damage.

Now we calculate the scaling factor:

Finally we calculate base damage \* scaling factor and get 89\*1.41=125.49 that is rounded to 125 in final damage.
If player3 had a shield and managed to Block the damage, the resulting damage would be 125\*0.2=25 which would be withdrawn from Player3s hit points.

Note that if Player2 used Slash instead of Precision Strike the weapon would not contribute to the base damage, since Bloodfang only has Influence on dex, and Slash scales with str.

## Damage Application

After the attack has been determined to hit and damage has been calculated it is applied to the defender. Now checks for the following is done in order:

* If damage is applied to a Player:
	+ If damage is 0, the attack is counted as a miss.
	+ If the attacker causing the damage is a player, and the area is guarded, Guards will spawn
	+ Dodge is checked, if triggered
	+ Counter is checked, if triggered and comes from an enemy
	+ Winter Boots negates status effect Freezing if equipped
	+ Reduction from Block if triggered
	+ Reduction from Barrier, provided by Protect or Motivate
* If damage is applied to a Monster:
	+ Carnivorous Worm duplication in case of Slashing damage
	+ Multiply damage with enemy weakness or strong against factor

Finally the damage is drawn from the health of the defender, if it has not been cancelled by the previous steps.

This means that, for example, the following non-obvious situations can occur:

* Counter will not trigger if an attack is Dodged, and will actually do more damage to the attacker if the defender is unarmored and have bad defensive scaling
* A attack that was Countered in a Guarded zone, will spawn Guards on both the attacker and defender, instead of just the attacker
* Status Effects, that goes directly to this phase can be Dodged.