Fix healing damage classes and damageable serialization and add test (#2727)

* Fix healing damage classes and damageable serialization and add test

* The fall of an empire

* Fix healPerType being -1 instead of 1
This commit is contained in:
DrSmugleaf
2020-12-11 02:09:43 +01:00
committed by GitHub
parent bb905c9628
commit adc972f9d3
7 changed files with 198 additions and 21 deletions

View File

@@ -15,6 +15,7 @@ namespace Content.Shared.Damage.DamageContainer
[Serializable, NetSerializable]
public class DamageContainerPrototype : IPrototype, IIndexedPrototype
{
private bool _supportAll;
private HashSet<DamageClass> _supportedClasses;
private HashSet<DamageType> _supportedTypes;
private string _id;
@@ -31,18 +32,26 @@ namespace Content.Shared.Damage.DamageContainer
var serializer = YamlObjectSerializer.NewReader(mapping);
serializer.DataField(ref _id, "id", string.Empty);
serializer.DataField(ref _supportAll, "supportAll", false);
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())
if (_supportAll)
{
_supportedTypes.Add(supportedType);
_supportedClasses.UnionWith(Enum.GetValues<DamageClass>());
_supportedTypes.UnionWith(Enum.GetValues<DamageType>());
return;
}
foreach (var originalType in originalTypes)
foreach (var supportedClass in _supportedClasses)
{
foreach (var supportedType in supportedClass.ToTypes())
{
_supportedTypes.Add(supportedType);
}
}
foreach (var originalType in _supportedTypes)
{
_supportedClasses.Add(originalType.ToClass());
}

View File

@@ -58,14 +58,16 @@ namespace Content.Shared.Damage
var classes = DamageClassExtensions.ToDictionary();
foreach (var @class in classes.Keys.ToList())
foreach (var type in @class.ToTypes())
{
if (!types.TryGetValue(type, out var damage))
foreach (var type in @class.ToTypes())
{
continue;
}
if (!types.TryGetValue(type, out var damage))
{
continue;
}
classes[@class] += damage;
classes[@class] += damage;
}
}
return classes;

View File

@@ -135,7 +135,7 @@ namespace Content.Shared.GameObjects.Components.Damage
// TODO DAMAGE Serialize damage done and resistance changes
serializer.DataReadWriteFunction(
"damagePrototype",
"damageContainer",
DefaultDamageContainer,
prototype =>
{
@@ -151,7 +151,7 @@ namespace Content.Shared.GameObjects.Components.Damage
() => DamageContainerId);
serializer.DataReadWriteFunction(
"resistancePrototype",
"resistances",
DefaultResistanceSet,
prototype =>
{
@@ -343,7 +343,7 @@ namespace Content.Shared.GameObjects.Components.Damage
{
// Changing multiple types is a bit more complicated. Might be a better way (formula?) to do this,
// but essentially just loops between each damage category until all healing is used up.
var healingLeft = amount;
var healingLeft = -amount;
var healThisCycle = 1;
// While we have healing left...
@@ -354,11 +354,11 @@ namespace Content.Shared.GameObjects.Components.Damage
healThisCycle = 0;
int healPerType;
if (healingLeft > -types.Count)
if (healingLeft < types.Count)
{
// Say we were to distribute 2 healing between 3
// this will distribute 1 to each (and stop after 2 are given)
healPerType = -1;
healPerType = 1;
}
else
{
@@ -369,10 +369,11 @@ namespace Content.Shared.GameObjects.Components.Damage
foreach (var type in types)
{
var healAmount =
Math.Max(Math.Max(healPerType, -GetDamage(type)), healingLeft);
var damage = GetDamage(type);
var healAmount = Math.Min(healingLeft, damage);
healAmount = Math.Min(healAmount, healPerType);
ChangeDamage(type, healAmount, true);
ChangeDamage(type, -healAmount, true);
healThisCycle += healAmount;
healingLeft -= healAmount;
}

View File

@@ -70,6 +70,16 @@ namespace Content.Shared.GameObjects.Components.Damage
/// </returns>
bool TryGetDamage(DamageType type, out int damage);
/// <summary>
/// Gets the amount of damage of a class.
/// </summary>
/// <param name="class">The class to get the damage of.</param>
/// <param name="damage">The amount of damage of that class.</param>
/// <returns>
/// True if the given <see cref="@class"/> is supported, false otherwise.
/// </returns>
bool TryGetDamage(DamageClass @class, out int damage);
/// <summary>
/// Changes the specified <see cref="DamageType"/>, applying
/// resistance values only if it is damage.