Damage rework (#2525)

* Make damage work through messages and events, make destructible not inherit ruinable or reference damageable

* Copy sound logic to destructible component for now

* Fix typo

* Fix prototype error

* Remove breakable component damageable reference

* Remove breakable construction reference

* Remove ruinable component

* Move thresholds to individual components and away from damageable

* Add threshold property to damageable component code

* Add thresholds to destructible component, add states to damageable, remove damage container, fix up mob states

* Being alive isn't normal

* Fix not reading the id

* Merge fixes

* YAML fixes

* Grammar moment

* Remove unnecessary dependency

* Update thresholds doc

* Change naming of thresholds to states in MobStateComponent

* Being alive is once again normal

* Make DamageState a byte

* Bring out classes structs and enums from DestructibleComponent

* Add test for destructible thresholds

* Merge fixes

* More merge fixes and fix rejuvenate test

* Remove IMobState.IsConscious

* More merge fixes someone please god review this shit already

* Fix rejuvenate test

* Update outdated destructible in YAML

* Fix repeatedly entering the current state

* Fix repeatedly entering the current state, add Threshold.TriggersOnce and expand test

* Update saltern
This commit is contained in:
DrSmugleaf
2020-12-07 14:52:55 +01:00
committed by GitHub
parent 9a187629ba
commit 02bca4c0d8
133 changed files with 3195 additions and 5897 deletions

View File

@@ -18,6 +18,7 @@ namespace Content.Shared.Damage
public static class DamageClassExtensions
{
// TODO DAMAGE This but not hardcoded
private static readonly ImmutableDictionary<DamageClass, List<DamageType>> ClassToType =
new Dictionary<DamageClass, List<DamageType>>
{

View File

@@ -1,285 +0,0 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using Content.Shared.GameObjects.Components.Damage;
using Robust.Shared.Serialization;
using Robust.Shared.ViewVariables;
namespace Content.Shared.Damage.DamageContainer
{
/// <summary>
/// Holds the information regarding the various forms of damage an object has
/// taken (i.e. brute, burn, or toxic damage).
/// </summary>
[Serializable, NetSerializable]
public class DamageContainer
{
private Dictionary<DamageType, int> _damageList = DamageTypeExtensions.ToDictionary();
public delegate void HealthChangedDelegate(List<HealthChangeData> changes);
[NonSerialized] public readonly HealthChangedDelegate OnHealthChanged;
public DamageContainer(HealthChangedDelegate onHealthChanged, DamageContainerPrototype data)
{
ID = data.ID;
OnHealthChanged = onHealthChanged;
SupportedClasses = data.ActiveDamageClasses;
}
public string ID { get; }
[ViewVariables] public virtual List<DamageClass> SupportedClasses { get; }
[ViewVariables]
public virtual List<DamageType> SupportedTypes
{
get
{
var toReturn = new List<DamageType>();
foreach (var @class in SupportedClasses)
{
toReturn.AddRange(@class.ToTypes());
}
return toReturn;
}
}
/// <summary>
/// Sum of all damages kept on record.
/// </summary>
[ViewVariables]
public int TotalDamage => _damageList.Values.Sum();
public IReadOnlyDictionary<DamageClass, int> DamageClasses =>
DamageTypeExtensions.ToClassDictionary(DamageTypes);
public IReadOnlyDictionary<DamageType, int> DamageTypes => _damageList;
public bool SupportsDamageClass(DamageClass @class)
{
return SupportedClasses.Contains(@class);
}
public bool SupportsDamageType(DamageType type)
{
return SupportedClasses.Contains(type.ToClass());
}
/// <summary>
/// Attempts to grab the damage value for the given <see cref="DamageType"/>.
/// </summary>
/// <returns>
/// False if the container does not support that type, true otherwise.
/// </returns>
public bool TryGetDamageValue(DamageType type, [NotNullWhen(true)] out int damage)
{
return _damageList.TryGetValue(type, out damage);
}
/// <summary>
/// Grabs the damage value for the given <see cref="DamageType"/>.
/// </summary>
public int GetDamageValue(DamageType type)
{
return TryGetDamageValue(type, out var damage) ? damage : 0;
}
/// <summary>
/// Attempts to grab the sum of damage values for the given
/// <see cref="DamageClasses"/>.
/// </summary>
/// <param name="class">The class to get the sum for.</param>
/// <param name="damage">The resulting amount of damage, if any.</param>
/// <returns>
/// True if the class is supported in this container, false otherwise.
/// </returns>
public bool TryGetDamageClassSum(DamageClass @class, [NotNullWhen(true)] out int damage)
{
damage = 0;
if (SupportsDamageClass(@class))
{
foreach (var type in @class.ToTypes())
{
damage += GetDamageValue(type);
}
return true;
}
return false;
}
/// <summary>
/// Grabs the sum of damage values for the given <see cref="DamageClasses"/>.
/// </summary>
public int GetDamageClassSum(DamageClass damageClass)
{
var sum = 0;
foreach (var type in damageClass.ToTypes())
{
sum += GetDamageValue(type);
}
return sum;
}
/// <summary>
/// Attempts to change the damage value for the given
/// <see cref="DamageType"/>
/// </summary>
/// <returns>
/// True if successful, false if this container does not support that type.
/// </returns>
public bool TryChangeDamageValue(DamageType type, int delta)
{
var damageClass = type.ToClass();
if (SupportsDamageClass(damageClass))
{
var current = _damageList[type];
current = _damageList[type] = current + delta;
if (_damageList[type] < 0)
{
_damageList[type] = 0;
delta = -current;
current = 0;
}
var datum = new HealthChangeData(type, current, delta);
var data = new List<HealthChangeData> {datum};
OnHealthChanged(data);
return true;
}
return false;
}
/// <summary>
/// Changes the damage value for the given <see cref="DamageType"/>.
/// </summary>
/// <param name="type">The type of damage to change.</param>
/// <param name="delta">The amount to change it by.</param>
/// <param name="quiet">
/// Whether or not to suppress the health change event.
/// </param>
/// <returns>
/// True if successful, false if this container does not support that type.
/// </returns>
public bool ChangeDamageValue(DamageType type, int delta, bool quiet = false)
{
if (!_damageList.TryGetValue(type, out var current))
{
return false;
}
_damageList[type] = current + delta;
if (_damageList[type] < 0)
{
_damageList[type] = 0;
delta = -current;
}
current = _damageList[type];
var datum = new HealthChangeData(type, current, delta);
var data = new List<HealthChangeData> {datum};
OnHealthChanged(data);
return true;
}
/// <summary>
/// Attempts to set the damage value for the given <see cref="DamageType"/>.
/// </summary>
/// <returns>
/// True if successful, false if this container does not support that type.
/// </returns>
public bool TrySetDamageValue(DamageType type, int newValue)
{
if (newValue < 0)
{
return false;
}
var damageClass = type.ToClass();
if (SupportedClasses.Contains(damageClass))
{
var old = _damageList[type] = newValue;
_damageList[type] = newValue;
var delta = newValue - old;
var datum = new HealthChangeData(type, newValue, delta);
var data = new List<HealthChangeData> {datum};
OnHealthChanged(data);
return true;
}
return false;
}
/// <summary>
/// Tries to set the damage value for the given <see cref="DamageType"/>.
/// </summary>
/// <param name="type">The type of damage to set.</param>
/// <param name="newValue">The value to set it to.</param>
/// <param name="quiet">
/// Whether or not to suppress the health changed event.
/// </param>
/// <returns>True if successful, false otherwise.</returns>
public bool SetDamageValue(DamageType type, int newValue, bool quiet = false)
{
if (newValue < 0)
{
return false;
}
if (!_damageList.ContainsKey(type))
{
return false;
}
var old = _damageList[type];
_damageList[type] = newValue;
if (!quiet)
{
var delta = newValue - old;
var datum = new HealthChangeData(type, 0, delta);
var data = new List<HealthChangeData> {datum};
OnHealthChanged(data);
}
return true;
}
public void Heal()
{
var data = new List<HealthChangeData>();
foreach (var type in SupportedTypes)
{
var delta = -GetDamageValue(type);
var datum = new HealthChangeData(type, 0, delta);
data.Add(datum);
SetDamageValue(type, 0, true);
}
OnHealthChanged(data);
}
}
}

View File

@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Robust.Shared.Prototypes;
using Robust.Shared.Serialization;
using Robust.Shared.ViewVariables;
@@ -14,18 +15,37 @@ namespace Content.Shared.Damage.DamageContainer
[Serializable, NetSerializable]
public class DamageContainerPrototype : IPrototype, IIndexedPrototype
{
private List<DamageClass> _activeDamageClasses;
private HashSet<DamageClass> _supportedClasses;
private HashSet<DamageType> _supportedTypes;
private string _id;
[ViewVariables] public List<DamageClass> ActiveDamageClasses => _activeDamageClasses;
// TODO NET 5 IReadOnlySet
[ViewVariables] public IReadOnlyCollection<DamageClass> SupportedClasses => _supportedClasses;
[ViewVariables] public IReadOnlyCollection<DamageType> SupportedTypes => _supportedTypes;
[ViewVariables] public string ID => _id;
public virtual void LoadFrom(YamlMappingNode mapping)
{
var serializer = YamlObjectSerializer.NewReader(mapping);
serializer.DataField(ref _id, "id", string.Empty);
serializer.DataField(ref _activeDamageClasses, "activeDamageClasses", new List<DamageClass>());
serializer.DataField(ref _supportedClasses, "supportedClasses", new HashSet<DamageClass>());
serializer.DataField(ref _supportedTypes, "supportedTypes", new HashSet<DamageType>());
var originalTypes = _supportedTypes.ToArray();
foreach (var supportedClass in _supportedClasses)
foreach (var supportedType in supportedClass.ToTypes())
{
_supportedTypes.Add(supportedType);
}
foreach (var originalType in originalTypes)
{
_supportedClasses.Add(originalType.ToClass());
}
}
}
}

View File

@@ -38,8 +38,7 @@ namespace Content.Shared.Damage
{DamageType.Radiation, DamageClass.Toxin},
{DamageType.Asphyxiation, DamageClass.Airloss},
{DamageType.Bloodloss, DamageClass.Airloss},
{DamageType.Cellular, DamageClass.Genetic }
{DamageType.Cellular, DamageClass.Genetic}
}.ToImmutableDictionary();
public static DamageClass ToClass(this DamageType type)

View File

@@ -6,7 +6,9 @@ using Robust.Shared.ViewVariables;
namespace Content.Shared.Damage.ResistanceSet
{
/// <summary>
/// Set of resistances used by damageable objects. Each DamageType has a multiplier and flat damage reduction value.
/// Set of resistances used by damageable objects.
/// Each <see cref="DamageType"/> has a multiplier and flat damage
/// reduction value.
/// </summary>
[NetSerializable]
[Serializable]
@@ -33,14 +35,15 @@ namespace Content.Shared.Damage.ResistanceSet
public string ID { get; }
/// <summary>
/// Adjusts input damage with the resistance set values. Only applies reduction if the amount is damage (positive), not
/// Adjusts input damage with the resistance set values.
/// Only applies reduction if the amount is damage (positive), not
/// healing (negative).
/// </summary>
/// <param name="damageType">Type of damage.</param>
/// <param name="amount">Incoming amount of damage.</param>
public int CalculateDamage(DamageType damageType, int amount)
{
if (amount > 0) //Only apply reduction if it's healing, not damage.
if (amount > 0) // Only apply reduction if it's healing, not damage.
{
amount -= _resistances[damageType].FlatReduction;
@@ -59,8 +62,7 @@ namespace Content.Shared.Damage.ResistanceSet
/// <summary>
/// Settings for a specific damage type in a resistance set. Flat reduction is applied before the coefficient.
/// </summary>
[NetSerializable]
[Serializable]
[Serializable, NetSerializable]
public struct ResistanceSetSettings
{
[ViewVariables] public float Coefficient { get; private set; }