BL Coding What does this damage calculation code do?

正在查看此主题的用户

Ingolifs

Grandmaster Knight
I've been looking through the Bannerlord damage calculation code (CombatStatCalculator.cs) and while I don't know C#, most of the code is fairly self-explanatory. However, I don't have the knowledge to interpret the blunt case in either ComputeRawDamageOld and ComputeRawDamageNew (it's basically the same code so I'm only showing one example here).

The code I don't understand is everything from label_5: downwards.


插入代码块:
    public static float ComputeRawDamageNew(
      DamageTypes damageType,
      float magnitude,
      float armorEffectiveness,
      float absorbedDamageRatio)
    {
      float num1 = 0.0f;
      float factorByDamageType = CombatStatCalculator.GetBluntDamageFactorByDamageType(damageType);
      float num2 = magnitude * factorByDamageType;
      float num3 = (float) (100.0 / (100.0 + (double) armorEffectiveness));
      float num4 = num1 + num2 * num3;
      float num5;
      switch (damageType)
      {
        case DamageTypes.Cut:
          num5 = Math.Max(0.0f, (float) ((double) magnitude * (double) num3 - (double) armorEffectiveness * 0.5));
          break;
        case DamageTypes.Pierce:
          num5 = Math.Max(0.0f, (float) ((double) magnitude * (double) num3 - (double) armorEffectiveness * 0.330000013113022));
          break;
        case DamageTypes.Blunt:
label_5:
          return num4 * absorbedDamageRatio;
        default:
          return 0.0f;
      }
      num4 += num5 * (1f - factorByDamageType);
      goto label_5;
    }

So how is the blunt damage being calculated here? I'm confused about the purpose of the goto and why it is outside the curly brackets.
 
解决方案
This code is optimized by the compiler. Un-optimized code will look like this:
插入代码块:
    public static float ComputeRawDamageNew(
      DamageTypes damageType,
      float magnitude,
      float armorEffectiveness,
      float absorbedDamageRatio)
    {
      float num1 = 0.0f;
      float factorByDamageType = CombatStatCalculator.GetBluntDamageFactorByDamageType(damageType);
      float num2 = magnitude * factorByDamageType;
      float num3 = (float) (100.0 / (100.0 + (double) armorEffectiveness));
      float num4 = num1 + num2 * num3;
      float num5;
      switch (damageType)
      {
        case DamageTypes.Cut:
          num5 = Math.Max(0.0f, (float) ((double) magnitude * (double) num3 - (double) armorEffectiveness * 0.5))...
This code is optimized by the compiler. Un-optimized code will look like this:
插入代码块:
    public static float ComputeRawDamageNew(
      DamageTypes damageType,
      float magnitude,
      float armorEffectiveness,
      float absorbedDamageRatio)
    {
      float num1 = 0.0f;
      float factorByDamageType = CombatStatCalculator.GetBluntDamageFactorByDamageType(damageType);
      float num2 = magnitude * factorByDamageType;
      float num3 = (float) (100.0 / (100.0 + (double) armorEffectiveness));
      float num4 = num1 + num2 * num3;
      float num5;
      switch (damageType)
      {
        case DamageTypes.Cut:
          num5 = Math.Max(0.0f, (float) ((double) magnitude * (double) num3 - (double) armorEffectiveness * 0.5));
          num4 += num5 * (1f - factorByDamageType);
          return num4 * absorbedDamageRatio;
        case DamageTypes.Pierce:
          num5 = Math.Max(0.0f, (float) ((double) magnitude * (double) num3 - (double) armorEffectiveness * 0.330000013113022));
          num4 += num5 * (1f - factorByDamageType);
          return num4 * absorbedDamageRatio;
        case DamageTypes.Blunt:
          return num4 * absorbedDamageRatio;
        default:
          return 0.0f;
      }
    }
 
点赞 0
解决方案
后退
顶部 底部