Remove damage flags (#4189)

This commit is contained in:
DrSmugleaf
2021-06-19 10:50:56 +02:00
committed by GitHub
parent 9b8185db23
commit 4093e2b5ba
9 changed files with 59 additions and 288 deletions

View File

@@ -1,28 +0,0 @@
#nullable enable
using Content.Server.Administration;
using Content.Shared.Administration;
using Robust.Server.Player;
using Robust.Shared.Console;
namespace Content.Server.Damage.Commands
{
[AdminCommand(AdminFlags.Fun)]
public class AddDamageFlagCommand : DamageFlagCommand
{
public override string Command => "adddamageflag";
public override string Description => "Adds a damage flag to your entity or another.";
public override string Help => $"Usage: {Command} <flag> / {Command} <entityUid> <flag>";
public override void Execute(IConsoleShell shell, string argStr, string[] args)
{
var player = shell.Player as IPlayerSession;
if (!TryGetEntity(shell, player, args, true, out var entity, out var flag, out var damageable))
{
return;
}
damageable.AddFlag(flag);
shell.WriteLine($"Added damage flag {flag} to entity {entity.Name}");
}
}
}

View File

@@ -1,116 +0,0 @@
#nullable enable
using System;
using System.Diagnostics.CodeAnalysis;
using Content.Shared.Damage;
using Content.Shared.Damage.Components;
using Robust.Server.Player;
using Robust.Shared.Console;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
namespace Content.Server.Damage.Commands
{
public abstract class DamageFlagCommand : IConsoleCommand
{
public abstract string Command { get; }
public abstract string Description { get; }
public abstract string Help { get; }
public abstract void Execute(IConsoleShell shell, string argStr, string[] args);
public bool TryGetEntity(
IConsoleShell shell,
IPlayerSession? player,
string[] args,
bool adding,
[NotNullWhen(true)] out IEntity? entity,
out DamageFlag flag,
[NotNullWhen(true)] out IDamageableComponent? damageable)
{
entity = null;
flag = DamageFlag.None;
damageable = null;
IEntity? parsedEntity;
DamageFlag parsedFlag;
IDamageableComponent? parsedDamageable;
switch (args.Length)
{
case 1:
{
if (player == null)
{
shell.WriteLine("An entity needs to be specified when the command isn't used by a player.");
return false;
}
if (player.AttachedEntity == null)
{
shell.WriteLine("An entity needs to be specified when you aren't attached to an entity.");
return false;
}
if (!Enum.TryParse(args[0], true, out parsedFlag))
{
shell.WriteLine($"{args[0]} is not a valid damage flag.");
return false;
}
parsedEntity = player.AttachedEntity;
flag = parsedFlag;
break;
}
case 2:
{
if (!EntityUid.TryParse(args[0], out var id))
{
shell.WriteLine($"{args[0]} isn't a valid entity id.");
return false;
}
var entityManager = IoCManager.Resolve<IEntityManager>();
if (!entityManager.TryGetEntity(id, out parsedEntity))
{
shell.WriteLine($"No entity found with id {id}.");
return false;
}
if (!Enum.TryParse(args[1], true, out parsedFlag))
{
shell.WriteLine($"{args[1]} is not a valid damage flag.");
return false;
}
break;
}
default:
shell.WriteLine(Help);
return false;
}
if (!parsedEntity.TryGetComponent(out parsedDamageable))
{
shell.WriteLine($"Entity {parsedEntity.Name} doesn't have a {nameof(IDamageableComponent)}");
return false;
}
if (parsedDamageable.HasFlag(parsedFlag) && adding)
{
shell.WriteLine($"Entity {parsedEntity.Name} already has damage flag {parsedFlag}.");
return false;
}
else if (!parsedDamageable.HasFlag(parsedFlag) && !adding)
{
shell.WriteLine($"Entity {parsedEntity.Name} doesn't have damage flag {parsedFlag}.");
return false;
}
entity = parsedEntity;
flag = parsedFlag;
damageable = parsedDamageable;
return true;
}
}
}

View File

@@ -1,28 +0,0 @@
#nullable enable
using Content.Server.Administration;
using Content.Shared.Administration;
using Robust.Server.Player;
using Robust.Shared.Console;
namespace Content.Server.Damage.Commands
{
[AdminCommand(AdminFlags.Fun)]
public class RemoveDamageFlagCommand : DamageFlagCommand
{
public override string Command => "removedamageflag";
public override string Description => "Removes a damage flag from your entity or another.";
public override string Help => $"Usage: {Command} <flag> / {Command} <entityUid> <flag>";
public override void Execute(IConsoleShell shell, string argStr, string[] args)
{
var player = shell.Player as IPlayerSession;
if (!TryGetEntity(shell, player, args, false, out var entity, out var flag, out var damageable))
{
return;
}
damageable.RemoveFlag(flag);
shell.WriteLine($"Removed damage flag {flag} from entity {entity.Name}");
}
}
}

View File

@@ -1,8 +1,10 @@
#nullable enable #nullable enable
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using Content.Server.GameObjects.Components.Atmos; using Content.Server.GameObjects.Components.Atmos;
using Content.Shared.Damage; using Content.Shared.Damage;
using Content.Shared.Damage.Components; using Content.Shared.Damage.Components;
using Content.Shared.Damage.Resistances;
using Content.Shared.GameTicking; using Content.Shared.GameTicking;
using JetBrains.Annotations; using JetBrains.Annotations;
using Robust.Shared.GameObjects; using Robust.Shared.GameObjects;
@@ -35,7 +37,8 @@ namespace Content.Server.Damage
if (entity.TryGetComponent(out IDamageableComponent? damageable)) if (entity.TryGetComponent(out IDamageableComponent? damageable))
{ {
damageable.AddFlag(DamageFlag.Invulnerable); damageable.SupportedTypes.Clear();
damageable.SupportedClasses.Clear();
} }
return true; return true;
@@ -60,7 +63,15 @@ namespace Content.Server.Damage
if (entity.TryGetComponent(out IDamageableComponent? damageable)) if (entity.TryGetComponent(out IDamageableComponent? damageable))
{ {
damageable.RemoveFlag(DamageFlag.Invulnerable); if (old.SupportedTypes != null)
{
damageable.SupportedTypes.UnionWith(old.SupportedTypes);
}
if (old.SupportedClasses != null)
{
damageable.SupportedClasses.UnionWith(old.SupportedClasses);
}
} }
return true; return true;
@@ -91,10 +102,21 @@ namespace Content.Server.Damage
{ {
Entity = entity; Entity = entity;
MovedByPressure = entity.IsMovedByPressure(); MovedByPressure = entity.IsMovedByPressure();
if (entity.TryGetComponent(out IDamageableComponent? damageable))
{
SupportedTypes = damageable.SupportedTypes.ToHashSet();
SupportedClasses = damageable.SupportedClasses.ToHashSet();
}
} }
public IEntity Entity { get; } public IEntity Entity { get; }
public bool MovedByPressure { get; } public bool MovedByPressure { get; }
public HashSet<DamageType>? SupportedTypes { get; }
public HashSet<DamageClass>? SupportedClasses { get; }
} }
} }
} }

View File

@@ -35,67 +35,32 @@ namespace Content.Shared.Damage.Components
private readonly Dictionary<DamageType, int> _damageList = DamageTypeExtensions.ToNewDictionary(); private readonly Dictionary<DamageType, int> _damageList = DamageTypeExtensions.ToNewDictionary();
private readonly HashSet<DamageType> _supportedTypes = new();
private readonly HashSet<DamageClass> _supportedClasses = new();
[DataField("flags")]
private DamageFlag _flags;
[DataField("resistances")] public string ResistanceSetId = DefaultResistanceSet; [DataField("resistances")] public string ResistanceSetId = DefaultResistanceSet;
// TODO DAMAGE Use as default values, specify overrides in a separate property through yaml for better (de)serialization // TODO DAMAGE Use as default values, specify overrides in a separate property through yaml for better (de)serialization
[ViewVariables] [DataField("damageContainer")] public string DamageContainerId { get; set; } = DefaultDamageContainer; [ViewVariables] [DataField("damageContainer")] public string DamageContainerId { get; set; } = DefaultDamageContainer;
[ViewVariables] private ResistanceSet Resistances { get; set; } = new(); [ViewVariables] public ResistanceSet Resistances { get; set; } = new();
// TODO DAMAGE Cache this // TODO DAMAGE Cache this
[ViewVariables] public int TotalDamage => _damageList.Values.Sum(); [ViewVariables] public int TotalDamage => _damageList.Values.Sum();
[ViewVariables] [ViewVariables] public IReadOnlyDictionary<DamageClass, int> DamageClasses => _damageList.ToClassDictionary();
public IReadOnlyDictionary<DamageClass, int> DamageClasses =>
DamageTypeExtensions.ToClassDictionary(_damageList);
[ViewVariables] public IReadOnlyDictionary<DamageType, int> DamageTypes => _damageList; [ViewVariables] public IReadOnlyDictionary<DamageType, int> DamageTypes => _damageList;
public DamageFlag Flags [ViewVariables] public HashSet<DamageType> SupportedTypes { get; } = new();
{
get => _flags;
private set
{
if (_flags == value)
{
return;
}
_flags = value; [ViewVariables] public HashSet<DamageClass> SupportedClasses { get; } = new();
Dirty();
}
}
public void AddFlag(DamageFlag flag)
{
Flags |= flag;
}
public bool HasFlag(DamageFlag flag)
{
return Flags.HasFlag(flag);
}
public void RemoveFlag(DamageFlag flag)
{
Flags &= ~flag;
}
public bool SupportsDamageClass(DamageClass @class) public bool SupportsDamageClass(DamageClass @class)
{ {
return _supportedClasses.Contains(@class); return SupportedClasses.Contains(@class);
} }
public bool SupportsDamageType(DamageType type) public bool SupportsDamageType(DamageType type)
{ {
return _supportedTypes.Contains(type); return SupportedTypes.Contains(type);
} }
public override void Initialize() public override void Initialize()
@@ -107,12 +72,12 @@ namespace Content.Shared.Damage.Components
// TODO DAMAGE Serialize damage done and resistance changes // TODO DAMAGE Serialize damage done and resistance changes
var damagePrototype = prototypeManager.Index<DamageContainerPrototype>(DamageContainerId); var damagePrototype = prototypeManager.Index<DamageContainerPrototype>(DamageContainerId);
_supportedClasses.Clear(); SupportedClasses.Clear();
_supportedTypes.Clear(); SupportedTypes.Clear();
DamageContainerId = damagePrototype.ID; DamageContainerId = damagePrototype.ID;
_supportedClasses.UnionWith(damagePrototype.SupportedClasses); SupportedClasses.UnionWith(damagePrototype.SupportedClasses);
_supportedTypes.UnionWith(damagePrototype.SupportedTypes); SupportedTypes.UnionWith(damagePrototype.SupportedTypes);
var resistancePrototype = prototypeManager.Index<ResistanceSetPrototype>(ResistanceSetId); var resistancePrototype = prototypeManager.Index<ResistanceSetPrototype>(ResistanceSetId);
Resistances = new ResistanceSet(resistancePrototype); Resistances = new ResistanceSet(resistancePrototype);
@@ -127,7 +92,7 @@ namespace Content.Shared.Damage.Components
public override ComponentState GetComponentState(ICommonSession player) public override ComponentState GetComponentState(ICommonSession player)
{ {
return new DamageableComponentState(_damageList, _flags); return new DamageableComponentState(_damageList);
} }
public override void HandleComponentState(ComponentState? curState, ComponentState? nextState) public override void HandleComponentState(ComponentState? curState, ComponentState? nextState)
@@ -145,8 +110,6 @@ namespace Content.Shared.Damage.Components
{ {
_damageList[type] = damage; _damageList[type] = damage;
} }
_flags = state.Flags;
} }
public int GetDamage(DamageType type) public int GetDamage(DamageType type)
@@ -203,7 +166,7 @@ namespace Content.Shared.Damage.Components
var damageClass = type.ToClass(); var damageClass = type.ToClass();
if (_supportedClasses.Contains(damageClass)) if (SupportedClasses.Contains(damageClass))
{ {
var old = _damageList[type] = newValue; var old = _damageList[type] = newValue;
_damageList[type] = newValue; _damageList[type] = newValue;
@@ -227,7 +190,7 @@ namespace Content.Shared.Damage.Components
public void Heal() public void Heal()
{ {
foreach (var type in _supportedTypes) foreach (var type in SupportedTypes)
{ {
Heal(type); Heal(type);
} }
@@ -240,11 +203,6 @@ namespace Content.Shared.Damage.Components
IEntity? source = null, IEntity? source = null,
DamageChangeParams? extraParams = null) DamageChangeParams? extraParams = null)
{ {
if (amount > 0 && HasFlag(DamageFlag.Invulnerable))
{
return false;
}
if (!SupportsDamageType(type)) if (!SupportsDamageType(type))
{ {
return false; return false;
@@ -291,11 +249,6 @@ namespace Content.Shared.Damage.Components
IEntity? source = null, IEntity? source = null,
DamageChangeParams? extraParams = null) DamageChangeParams? extraParams = null)
{ {
if (amount > 0 && HasFlag(DamageFlag.Invulnerable))
{
return false;
}
if (!SupportsDamageClass(@class)) if (!SupportsDamageClass(@class))
{ {
return false; return false;
@@ -374,7 +327,7 @@ namespace Content.Shared.Damage.Components
public bool SetDamage(DamageType type, int newValue, IEntity? source = null, DamageChangeParams? extraParams = null) public bool SetDamage(DamageType type, int newValue, IEntity? source = null, DamageChangeParams? extraParams = null)
{ {
if (newValue >= TotalDamage && HasFlag(DamageFlag.Invulnerable)) if (newValue >= TotalDamage)
{ {
return false; return false;
} }
@@ -405,7 +358,7 @@ namespace Content.Shared.Damage.Components
{ {
var data = new List<DamageChangeData>(); var data = new List<DamageChangeData>();
foreach (var type in _supportedTypes) foreach (var type in SupportedTypes)
{ {
var damage = GetDamage(type); var damage = GetDamage(type);
var datum = new DamageChangeData(type, damage, 0); var datum = new DamageChangeData(type, damage, 0);
@@ -457,12 +410,10 @@ namespace Content.Shared.Damage.Components
public class DamageableComponentState : ComponentState public class DamageableComponentState : ComponentState
{ {
public readonly Dictionary<DamageType, int> DamageList; public readonly Dictionary<DamageType, int> DamageList;
public readonly DamageFlag Flags;
public DamageableComponentState(Dictionary<DamageType, int> damageList, DamageFlag flags) : base(ContentNetIDs.DAMAGEABLE) public DamageableComponentState(Dictionary<DamageType, int> damageList) : base(ContentNetIDs.DAMAGEABLE)
{ {
DamageList = damageList; DamageList = damageList;
Flags = flags;
} }
} }
} }

View File

@@ -1,6 +1,7 @@
#nullable enable #nullable enable
using System.Collections.Generic; using System.Collections.Generic;
using Content.Shared.Acts; using Content.Shared.Acts;
using Content.Shared.Damage.Resistances;
using Robust.Shared.GameObjects; using Robust.Shared.GameObjects;
namespace Content.Shared.Damage.Components namespace Content.Shared.Damage.Components
@@ -22,29 +23,14 @@ namespace Content.Shared.Damage.Components
/// </summary> /// </summary>
IReadOnlyDictionary<DamageType, int> DamageTypes { get; } IReadOnlyDictionary<DamageType, int> DamageTypes { get; }
/// <summary> HashSet<DamageType> SupportedTypes { get; }
/// The damage flags on this component.
/// </summary> HashSet<DamageClass> SupportedClasses { get; }
DamageFlag Flags { get; }
/// <summary> /// <summary>
/// Adds a flag to this component. /// The resistances of this component.
/// </summary> /// </summary>
/// <param name="flag">The flag to add.</param> ResistanceSet Resistances { get; }
void AddFlag(DamageFlag flag);
/// <summary>
/// Checks whether or not this component has a specific flag.
/// </summary>
/// <param name="flag">The flag to check for.</param>
/// <returns>True if it has the flag, false otherwise.</returns>
bool HasFlag(DamageFlag flag);
/// <summary>
/// Removes a flag from this component.
/// </summary>
/// <param name="flag">The flag to remove.</param>
void RemoveFlag(DamageFlag flag);
bool SupportsDamageClass(DamageClass @class); bool SupportsDamageClass(DamageClass @class);

View File

@@ -1,14 +0,0 @@
#nullable enable
using System;
using Robust.Shared.Serialization;
namespace Content.Shared.Damage
{
[Flags]
[Serializable, NetSerializable]
public enum DamageFlag
{
None = 0,
Invulnerable = 1 << 0
}
}

View File

@@ -42,7 +42,7 @@ namespace Content.Shared.Damage
return ToNewDictionary<int>(); return ToNewDictionary<int>();
} }
public static Dictionary<DamageClass, int> ToClassDictionary(IReadOnlyDictionary<DamageType, int> types) public static Dictionary<DamageClass, int> ToClassDictionary(this IReadOnlyDictionary<DamageType, int> types)
{ {
var classes = DamageClassExtensions.ToNewDictionary(); var classes = DamageClassExtensions.ToNewDictionary();

View File

@@ -11,30 +11,29 @@ namespace Content.Shared.Damage.Resistances
/// Each <see cref="DamageType"/> has a multiplier and flat damage /// Each <see cref="DamageType"/> has a multiplier and flat damage
/// reduction value. /// reduction value.
/// </summary> /// </summary>
[NetSerializable] [Serializable, NetSerializable]
[Serializable]
public class ResistanceSet public class ResistanceSet
{ {
[ViewVariables]
private Dictionary<DamageType, ResistanceSetSettings> _resistances =
new();
public ResistanceSet() public ResistanceSet()
{ {
foreach (var damageType in (DamageType[]) Enum.GetValues(typeof(DamageType))) foreach (var damageType in (DamageType[]) Enum.GetValues(typeof(DamageType)))
{ {
_resistances.Add(damageType, new ResistanceSetSettings(1f, 0)); Resistances.Add(damageType, new ResistanceSetSettings(1f, 0));
} }
} }
public ResistanceSet(ResistanceSetPrototype data) public ResistanceSet(ResistanceSetPrototype data)
{ {
ID = data.ID; ID = data.ID;
_resistances = data.Resistances; Resistances = data.Resistances;
} }
[ViewVariables]
public string ID { get; } = string.Empty; public string ID { get; } = string.Empty;
[ViewVariables]
public Dictionary<DamageType, ResistanceSetSettings> Resistances { get; } = new();
/// <summary> /// <summary>
/// Adjusts input damage with the resistance set values. /// Adjusts input damage with the resistance set values.
/// Only applies reduction if the amount is damage (positive), not /// Only applies reduction if the amount is damage (positive), not
@@ -46,7 +45,7 @@ namespace Content.Shared.Damage.Resistances
{ {
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; amount -= Resistances[damageType].FlatReduction;
if (amount <= 0) if (amount <= 0)
{ {
@@ -54,7 +53,7 @@ namespace Content.Shared.Damage.Resistances
} }
} }
amount = (int) Math.Ceiling(amount * _resistances[damageType].Coefficient); amount = (int) Math.Ceiling(amount * Resistances[damageType].Coefficient);
return amount; return amount;
} }
@@ -64,11 +63,10 @@ namespace Content.Shared.Damage.Resistances
/// Settings for a specific damage type in a resistance set. Flat reduction is applied before the coefficient. /// Settings for a specific damage type in a resistance set. Flat reduction is applied before the coefficient.
/// </summary> /// </summary>
[Serializable, NetSerializable] [Serializable, NetSerializable]
public struct ResistanceSetSettings public readonly struct ResistanceSetSettings
{ {
[ViewVariables] public float Coefficient { get; private set; } [ViewVariables] public readonly float Coefficient;
[ViewVariables] public readonly int FlatReduction;
[ViewVariables] public int FlatReduction { get; private set; }
public ResistanceSetSettings(float coefficient, int flatReduction) public ResistanceSetSettings(float coefficient, int flatReduction)
{ {