Resistance System

This commit is contained in:
dingo
2025-12-26 10:40:09 -05:00
parent 2d847c91dd
commit fd056692df
16 changed files with 2862 additions and 8 deletions
+191
View File
@@ -0,0 +1,191 @@
using DataServer.Data.Entities;
using DataServer.Data.Enums;
namespace Server.Game.Dungeon;
/// <summary>
/// Handles damage calculations with stats, gear, and resistances
/// </summary>
public static class CombatCalculator
{
/// <summary>
/// Calculate damage dealt from player to mob
/// </summary>
public static DamageResult CalculatePlayerToMobDamage(
PlayerInstance player,
MobInstance mob,
DamageType damageType,
int baseDamage,
Dictionary<string, float>? playerGearResistances = null)
{
// 1. Base damage modified by player stats
float damage = baseDamage;
// Physical damage scales with STR
if (damageType == DamageType.Physical)
{
damage *= 1 + (player.Strength * 0.05f); // 5% per STR
}
// Magical damage scales with INT
else
{
damage *= 1 + (player.Intelligence * 0.05f); // 5% per INT
}
// 2. Apply mob's armor/magic resist reduction
float mitigation = 0f;
if (damageType == DamageType.Physical)
{
// Armor formula: damage reduction = armor / (armor + 100)
mitigation = mob.Stats.Armor / (float)(mob.Stats.Armor + 100);
}
else
{
// Magic resist formula
mitigation = mob.Stats.MagicResist / (float)(mob.Stats.MagicResist + 100);
}
damage *= (1 - mitigation);
// 3. Apply mob's elemental resistances
string resistKey = damageType.ToString().ToLower();
if (mob.Stats.Resistances.TryGetValue(resistKey, out float mobResist))
{
// Positive = resistance (reduces damage), Negative = weakness (increases damage)
// 0.5 = 50% resistance, -0.25 = 25% weakness, 1.0 = immune
if (mobResist >= 1.0f)
{
// Immune
return new DamageResult
{
FinalDamage = 0,
WasResisted = true,
WasImmune = true,
DamageType = damageType
};
}
damage *= (1 - mobResist);
}
// 4. Ensure minimum damage of 1
int finalDamage = Math.Max(1, (int)damage);
return new DamageResult
{
FinalDamage = finalDamage,
WasResisted = mobResist > 0,
WasWeakness = mobResist < 0,
DamageType = damageType
};
}
/// <summary>
/// Calculate damage dealt from mob to player
/// </summary>
public static DamageResult CalculateMobToPlayerDamage(
MobInstance mob,
PlayerInstance player,
DamageType damageType,
int baseDamage,
Dictionary<string, float> playerTotalResistances)
{
float damage = baseDamage;
// 1. Apply player's armor/magic resist reduction
float mitigation = 0f;
if (damageType == DamageType.Physical)
{
// Player armor from gear + endurance bonus
int playerArmor = player.Armor;
mitigation = playerArmor / (float)(playerArmor + 100);
}
else
{
int playerMagicResist = player.MagicResist;
mitigation = playerMagicResist / (float)(playerMagicResist + 100);
}
damage *= (1 - mitigation);
// 2. Apply player's elemental resistances (from gear + innate)
string resistKey = damageType.ToString().ToLower();
float playerResist = 0f;
if (playerTotalResistances.TryGetValue(resistKey, out float resist))
{
playerResist = resist;
}
// Cap resistance at 75% to prevent immunity stacking
playerResist = Math.Min(playerResist, 0.75f);
if (playerResist != 0)
{
damage *= (1 - playerResist);
}
// 3. Endurance provides flat damage reduction
float enduranceReduction = player.Endurance * 0.5f; // 0.5 flat reduction per END
damage = Math.Max(1, damage - enduranceReduction);
int finalDamage = Math.Max(1, (int)damage);
return new DamageResult
{
FinalDamage = finalDamage,
WasResisted = playerResist > 0,
WasWeakness = playerResist < 0,
DamageType = damageType
};
}
/// <summary>
/// Calculate total resistances for a player from innate + gear
/// </summary>
public static Dictionary<string, float> CalculatePlayerTotalResistances(
Dictionary<string, float> innateResistances,
List<DungeonItemDefinition> equippedItems)
{
var total = new Dictionary<string, float>(innateResistances);
foreach (var item in equippedItems)
{
foreach (var resist in item.Resistances)
{
if (total.ContainsKey(resist.Key))
total[resist.Key] += resist.Value;
else
total[resist.Key] = resist.Value;
}
}
return total;
}
/// <summary>
/// Calculate player weapon damage range
/// </summary>
public static (int min, int max, DamageType type) GetPlayerWeaponDamage(
PlayerInstance player,
DungeonItemDefinition? weapon)
{
if (weapon == null)
{
// Unarmed: base damage + STR
int unarmedMin = 1 + (int)(player.Strength * 0.5f);
int unarmedMax = 3 + (int)(player.Strength * 0.5f);
return (unarmedMin, unarmedMax, DamageType.Physical);
}
return (weapon.MinDamage, weapon.MaxDamage, weapon.DamageType);
}
}
public class DamageResult
{
public int FinalDamage { get; set; }
public bool WasResisted { get; set; }
public bool WasWeakness { get; set; }
public bool WasImmune { get; set; }
public bool WasCritical { get; set; }
public DamageType DamageType { get; set; }
}