205 lines
6.7 KiB
C#
205 lines
6.7 KiB
C#
using DataServer.Data.Entities;
|
|
|
|
namespace GameServer.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,
|
|
string damageTypeKey,
|
|
int baseDamage)
|
|
{
|
|
// 1. Base damage modified by player stats
|
|
float damage = baseDamage;
|
|
|
|
// Physical damage scales with STR, all other types scale with INT
|
|
if (damageTypeKey == "physical")
|
|
{
|
|
damage *= 1 + (player.Strength * 0.05f); // 5% per STR
|
|
}
|
|
else
|
|
{
|
|
damage *= 1 + (player.Intelligence * 0.05f); // 5% per INT
|
|
}
|
|
|
|
// 2. Apply mob's armor reduction (flat mitigation for all damage types)
|
|
float armorMitigation = mob.Stats.Armor / (float)(mob.Stats.Armor + 100);
|
|
damage *= (1 - armorMitigation);
|
|
|
|
// 3. Apply mob's elemental resistances
|
|
float mobResist = 0f;
|
|
if (mob.Stats.Resistances.TryGetValue(damageTypeKey, out float resist))
|
|
{
|
|
mobResist = resist;
|
|
// Positive = resistance (reduces damage), Negative = weakness (increases damage)
|
|
// 0.5 = 50% resistance, -0.25 = 25% weakness, 1.0 = immune
|
|
if (mobResist >= 1.0f)
|
|
{
|
|
return new DamageResult
|
|
{
|
|
FinalDamage = 0,
|
|
WasResisted = true,
|
|
WasImmune = true,
|
|
DamageTypeKey = damageTypeKey
|
|
};
|
|
}
|
|
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,
|
|
DamageTypeKey = damageTypeKey
|
|
};
|
|
}
|
|
|
|
/// <summary>
|
|
/// Calculate damage dealt from mob to player
|
|
/// </summary>
|
|
public static DamageResult CalculateMobToPlayerDamage(
|
|
MobInstance mob,
|
|
PlayerInstance player,
|
|
string damageTypeKey,
|
|
int baseDamage,
|
|
Dictionary<string, float> playerTotalResistances)
|
|
{
|
|
float damage = baseDamage;
|
|
|
|
// 1. Apply player's armor reduction (flat mitigation for all damage types)
|
|
float armorMitigation = player.Armor / (float)(player.Armor + 100);
|
|
damage *= (1 - armorMitigation);
|
|
|
|
// 2. Apply player's elemental resistances (from gear + innate)
|
|
float playerResist = 0f;
|
|
if (playerTotalResistances.TryGetValue(damageTypeKey, 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 percentage damage reduction (capped at 50%)
|
|
// Formula: END / (END + 100) gives diminishing returns
|
|
float enduranceReduction = player.Endurance / (float)(player.Endurance + 100);
|
|
enduranceReduction = Math.Min(enduranceReduction, 0.50f); // Cap at 50% reduction
|
|
damage *= (1.0f - enduranceReduction);
|
|
|
|
int finalDamage = Math.Max(1, (int)damage);
|
|
|
|
return new DamageResult
|
|
{
|
|
FinalDamage = finalDamage,
|
|
WasResisted = playerResist > 0,
|
|
WasWeakness = playerResist < 0,
|
|
DamageTypeKey = damageTypeKey
|
|
};
|
|
}
|
|
|
|
/// <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 total armor for a player from gear
|
|
/// </summary>
|
|
public static int CalculatePlayerTotalArmor(List<DungeonItemDefinition> equippedItems)
|
|
{
|
|
return equippedItems.Sum(item => item.Armor);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Calculate player weapon damage range
|
|
/// </summary>
|
|
public static (int min, int max, string damageTypeKey) 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, "physical");
|
|
}
|
|
|
|
// Safety: if both min and max are 0, use 1 as fallback
|
|
int minDmg = weapon.MinDamage;
|
|
int maxDmg = weapon.MaxDamage;
|
|
if (minDmg == 0 && maxDmg == 0)
|
|
{
|
|
minDmg = 1;
|
|
maxDmg = 1;
|
|
}
|
|
|
|
return (minDmg, maxDmg, weapon.DamageTypeKey);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Check if player meets equipment requirements
|
|
/// </summary>
|
|
public static (bool canEquip, string? reason) CanEquipItem(
|
|
DungeonCharacter character,
|
|
DungeonItemDefinition item)
|
|
{
|
|
if (character.Level < item.RequiredLevel)
|
|
return (false, $"Requires level {item.RequiredLevel}");
|
|
if (character.Strength < item.RequiredStrength)
|
|
return (false, $"Requires {item.RequiredStrength} STR");
|
|
if (character.Intelligence < item.RequiredIntelligence)
|
|
return (false, $"Requires {item.RequiredIntelligence} INT");
|
|
if (character.Wisdom < item.RequiredWisdom)
|
|
return (false, $"Requires {item.RequiredWisdom} WIS");
|
|
if (character.Dexterity < item.RequiredDexterity)
|
|
return (false, $"Requires {item.RequiredDexterity} DEX");
|
|
|
|
return (true, null);
|
|
}
|
|
}
|
|
|
|
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 string DamageTypeKey { get; set; } = "physical";
|
|
}
|