Resistance System
This commit is contained in:
@@ -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; }
|
||||
}
|
||||
@@ -273,8 +273,18 @@ public class DungeonSimulation
|
||||
|
||||
private void MobAttackPlayer(MobInstance mob, PlayerInstance player)
|
||||
{
|
||||
int damage = mob.Stats.PhysicalDamage + mob.Stats.MagicDamage;
|
||||
player.Health -= damage;
|
||||
// Determine damage type (physical if mob has no magic damage)
|
||||
var damageType = mob.Stats.MagicDamage > mob.Stats.PhysicalDamage
|
||||
? DataServer.Data.Enums.DamageType.Fire // Use fire for magic mobs (could be configured per mob)
|
||||
: DataServer.Data.Enums.DamageType.Physical;
|
||||
|
||||
int baseDamage = mob.Stats.PhysicalDamage + mob.Stats.MagicDamage;
|
||||
|
||||
// Calculate damage with resistances
|
||||
var result = CombatCalculator.CalculateMobToPlayerDamage(
|
||||
mob, player, damageType, baseDamage, player.Resistances);
|
||||
|
||||
player.Health -= result.FinalDamage;
|
||||
player.IsInCombat = true;
|
||||
player.LastCombatTick = CurrentTick;
|
||||
|
||||
@@ -284,7 +294,9 @@ public class DungeonSimulation
|
||||
SourceId = mob.Id,
|
||||
TargetType = EntityType.Player,
|
||||
TargetId = (int)player.CharacterId,
|
||||
Damage = damage
|
||||
Damage = result.FinalDamage,
|
||||
WasResisted = result.WasResisted,
|
||||
WasWeakness = result.WasWeakness
|
||||
});
|
||||
|
||||
if (player.Health <= 0)
|
||||
@@ -347,10 +359,30 @@ public class DungeonSimulation
|
||||
float dist = Distance(player.X, player.Y, mob.X, mob.Y);
|
||||
if (dist > 60) return; // Out of range
|
||||
|
||||
// Calculate damage
|
||||
int damage = (int)(10 * (1 + player.Strength * 0.05f));
|
||||
// Base damage from stats (would be enhanced by equipped weapon in full implementation)
|
||||
int baseDamage = 10 + (int)(player.Strength * 0.5f);
|
||||
var damageType = DataServer.Data.Enums.DamageType.Physical;
|
||||
|
||||
mob.Health -= damage;
|
||||
// Calculate damage with mob resistances
|
||||
var result = CombatCalculator.CalculatePlayerToMobDamage(
|
||||
player, mob, damageType, baseDamage);
|
||||
|
||||
if (result.WasImmune)
|
||||
{
|
||||
// Mob is immune, still add event for client feedback
|
||||
_pendingDamageEvents.Add(new DamageEvent
|
||||
{
|
||||
SourceType = EntityType.Player,
|
||||
SourceId = (int)player.CharacterId,
|
||||
TargetType = EntityType.Mob,
|
||||
TargetId = mob.Id,
|
||||
Damage = 0,
|
||||
WasImmune = true
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
mob.Health -= result.FinalDamage;
|
||||
player.IsInCombat = true;
|
||||
player.LastCombatTick = CurrentTick;
|
||||
|
||||
@@ -360,7 +392,9 @@ public class DungeonSimulation
|
||||
SourceId = (int)player.CharacterId,
|
||||
TargetType = EntityType.Mob,
|
||||
TargetId = mob.Id,
|
||||
Damage = damage
|
||||
Damage = result.FinalDamage,
|
||||
WasResisted = result.WasResisted,
|
||||
WasWeakness = result.WasWeakness
|
||||
});
|
||||
|
||||
if (mob.Health <= 0)
|
||||
|
||||
@@ -22,6 +22,18 @@ public class PlayerInstance
|
||||
public float AttackSpeed { get; set; } = 1f;
|
||||
public int Strength { get; set; }
|
||||
public int Intelligence { get; set; }
|
||||
public int Wisdom { get; set; }
|
||||
public int Endurance { get; set; }
|
||||
public int Dexterity { get; set; }
|
||||
|
||||
// Defensive stats (from gear)
|
||||
public int Armor { get; set; }
|
||||
public int MagicResist { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Combined resistances from innate + gear
|
||||
/// </summary>
|
||||
public Dictionary<string, float> Resistances { get; set; } = new();
|
||||
|
||||
public bool IsAlive { get; set; } = true;
|
||||
public bool IsInCombat { get; set; }
|
||||
|
||||
@@ -17,6 +17,21 @@ public class DamageEvent
|
||||
public int TargetId { get; set; }
|
||||
public int Damage { get; set; }
|
||||
public DamageType DamageType { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// True if damage was reduced by resistance
|
||||
/// </summary>
|
||||
public bool WasResisted { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// True if damage was increased by weakness
|
||||
/// </summary>
|
||||
public bool WasWeakness { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// True if target was immune (0 damage)
|
||||
/// </summary>
|
||||
public bool WasImmune { get; set; }
|
||||
}
|
||||
|
||||
public class DeathEvent
|
||||
|
||||
Reference in New Issue
Block a user