#nullable enable
using System;
using System.Collections.Generic;
using Robust.Shared.Serialization;
using Robust.Shared.ViewVariables;
namespace Content.Shared.Damage.Resistances
{
///
/// Set of resistances used by damageable objects.
/// Each has a multiplier and flat damage
/// reduction value.
///
[Serializable, NetSerializable]
public class ResistanceSet
{
public ResistanceSet()
{
foreach (var damageType in (DamageType[]) Enum.GetValues(typeof(DamageType)))
{
Resistances.Add(damageType, new ResistanceSetSettings(1f, 0));
}
}
public ResistanceSet(ResistanceSetPrototype data)
{
ID = data.ID;
Resistances = data.Resistances;
}
[ViewVariables]
public string ID { get; } = string.Empty;
[ViewVariables]
public Dictionary Resistances { get; } = new();
///
/// Adjusts input damage with the resistance set values.
/// Only applies reduction if the amount is damage (positive), not
/// healing (negative).
///
/// Type of damage.
/// Incoming amount of damage.
public int CalculateDamage(DamageType damageType, int amount)
{
if (amount > 0) // Only apply reduction if it's healing, not damage.
{
amount -= Resistances[damageType].FlatReduction;
if (amount <= 0)
{
return 0;
}
}
amount = (int) Math.Ceiling(amount * Resistances[damageType].Coefficient);
return amount;
}
}
///
/// Settings for a specific damage type in a resistance set. Flat reduction is applied before the coefficient.
///
[Serializable, NetSerializable]
public readonly struct ResistanceSetSettings
{
[ViewVariables] public readonly float Coefficient;
[ViewVariables] public readonly int FlatReduction;
public ResistanceSetSettings(float coefficient, int flatReduction)
{
Coefficient = coefficient;
FlatReduction = flatReduction;
}
}
}