Re-organize all projects (#4166)

This commit is contained in:
DrSmugleaf
2021-06-09 22:19:39 +02:00
committed by GitHub
parent 9f50e4061b
commit ff1a2d97ea
1773 changed files with 5258 additions and 5508 deletions

View File

@@ -0,0 +1,468 @@
#nullable enable
using System;
using System.Collections.Generic;
using System.Linq;
using Content.Shared.Acts;
using Content.Shared.Damage.Container;
using Content.Shared.Damage.Resistances;
using Content.Shared.NetIDs;
using Content.Shared.Radiation;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Players;
using Robust.Shared.Prototypes;
using Robust.Shared.Serialization;
using Robust.Shared.Serialization.Manager.Attributes;
using Robust.Shared.ViewVariables;
namespace Content.Shared.Damage.Components
{
/// <summary>
/// Component that allows attached entities to take damage.
/// This basic version never dies (thus can take an indefinite amount of damage).
/// </summary>
[RegisterComponent]
[ComponentReference(typeof(IDamageableComponent))]
public class DamageableComponent : Component, IDamageableComponent, IRadiationAct, ISerializationHooks
{
public override string Name => "Damageable";
public override uint? NetID => ContentNetIDs.DAMAGEABLE;
// TODO define these in yaml?
public const string DefaultResistanceSet = "defaultResistances";
public const string DefaultDamageContainer = "metallicDamageContainer";
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;
// 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] private ResistanceSet Resistances { get; set; } = new();
// TODO DAMAGE Cache this
[ViewVariables] public int TotalDamage => _damageList.Values.Sum();
[ViewVariables]
public IReadOnlyDictionary<DamageClass, int> DamageClasses =>
DamageTypeExtensions.ToClassDictionary(_damageList);
[ViewVariables] public IReadOnlyDictionary<DamageType, int> DamageTypes => _damageList;
public DamageFlag Flags
{
get => _flags;
private set
{
if (_flags == value)
{
return;
}
_flags = value;
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)
{
return _supportedClasses.Contains(@class);
}
public bool SupportsDamageType(DamageType type)
{
return _supportedTypes.Contains(type);
}
public override void Initialize()
{
base.Initialize();
var prototypeManager = IoCManager.Resolve<IPrototypeManager>();
// TODO DAMAGE Serialize damage done and resistance changes
var damagePrototype = prototypeManager.Index<DamageContainerPrototype>(DamageContainerId);
_supportedClasses.Clear();
_supportedTypes.Clear();
DamageContainerId = damagePrototype.ID;
_supportedClasses.UnionWith(damagePrototype.SupportedClasses);
_supportedTypes.UnionWith(damagePrototype.SupportedTypes);
var resistancePrototype = prototypeManager.Index<ResistanceSetPrototype>(ResistanceSetId);
Resistances = new ResistanceSet(resistancePrototype);
}
protected override void Startup()
{
base.Startup();
ForceHealthChangedEvent();
}
public override ComponentState GetComponentState(ICommonSession player)
{
return new DamageableComponentState(_damageList, _flags);
}
public override void HandleComponentState(ComponentState? curState, ComponentState? nextState)
{
base.HandleComponentState(curState, nextState);
if (!(curState is DamageableComponentState state))
{
return;
}
_damageList.Clear();
foreach (var (type, damage) in state.DamageList)
{
_damageList[type] = damage;
}
_flags = state.Flags;
}
public int GetDamage(DamageType type)
{
return _damageList.GetValueOrDefault(type);
}
public bool TryGetDamage(DamageType type, out int damage)
{
return _damageList.TryGetValue(type, out damage);
}
public int GetDamage(DamageClass @class)
{
if (!SupportsDamageClass(@class))
{
return 0;
}
var damage = 0;
foreach (var type in @class.ToTypes())
{
damage += GetDamage(type);
}
return damage;
}
public bool TryGetDamage(DamageClass @class, out int damage)
{
if (!SupportsDamageClass(@class))
{
damage = 0;
return false;
}
damage = GetDamage(@class);
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 TrySetDamage(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 DamageChangeData(type, newValue, delta);
var data = new List<DamageChangeData> {datum};
OnHealthChanged(data);
return true;
}
return false;
}
public void Heal(DamageType type)
{
SetDamage(type, 0);
}
public void Heal()
{
foreach (var type in _supportedTypes)
{
Heal(type);
}
}
public bool ChangeDamage(
DamageType type,
int amount,
bool ignoreResistances,
IEntity? source = null,
DamageChangeParams? extraParams = null)
{
if (amount > 0 && HasFlag(DamageFlag.Invulnerable))
{
return false;
}
if (!SupportsDamageType(type))
{
return false;
}
var finalDamage = amount;
if (!ignoreResistances)
{
finalDamage = Resistances.CalculateDamage(type, amount);
}
if (finalDamage == 0)
return false;
if (!_damageList.TryGetValue(type, out var current))
{
return false;
}
if (current + finalDamage < 0)
{
if (current == 0)
return false;
_damageList[type] = 0;
finalDamage = -current;
}
else
{
_damageList[type] = current + finalDamage;
}
current = _damageList[type];
var datum = new DamageChangeData(type, current, finalDamage);
var data = new List<DamageChangeData> {datum};
OnHealthChanged(data);
return true;
}
public bool ChangeDamage(DamageClass @class, int amount, bool ignoreResistances,
IEntity? source = null,
DamageChangeParams? extraParams = null)
{
if (amount > 0 && HasFlag(DamageFlag.Invulnerable))
{
return false;
}
if (!SupportsDamageClass(@class))
{
return false;
}
var types = @class.ToTypes();
if (amount < 0)
{
// 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 healThisCycle = 1;
// While we have healing left...
while (healingLeft > 0 && healThisCycle != 0)
{
// Infinite loop fallback, if no healing was done in a cycle
// then exit
healThisCycle = 0;
int healPerType;
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;
}
else
{
// Say we were to distribute 62 healing between 3
// this will distribute 20 to each, leaving 2 for next loop
healPerType = healingLeft / types.Count;
}
foreach (var type in types)
{
var damage = GetDamage(type);
var healAmount = Math.Min(healingLeft, damage);
healAmount = Math.Min(healAmount, healPerType);
ChangeDamage(type, -healAmount, true);
healThisCycle += healAmount;
healingLeft -= healAmount;
}
}
return true;
}
var damageLeft = amount;
while (damageLeft > 0)
{
int damagePerType;
if (damageLeft < types.Count)
{
damagePerType = 1;
}
else
{
damagePerType = damageLeft / types.Count;
}
foreach (var type in types)
{
var damageAmount = Math.Min(damagePerType, damageLeft);
ChangeDamage(type, damageAmount, true);
damageLeft -= damageAmount;
}
}
return true;
}
public bool SetDamage(DamageType type, int newValue, IEntity? source = null, DamageChangeParams? extraParams = null)
{
if (newValue >= TotalDamage && HasFlag(DamageFlag.Invulnerable))
{
return false;
}
if (newValue < 0)
{
return false;
}
if (!_damageList.ContainsKey(type))
{
return false;
}
var old = _damageList[type];
_damageList[type] = newValue;
var delta = newValue - old;
var datum = new DamageChangeData(type, 0, delta);
var data = new List<DamageChangeData> {datum};
OnHealthChanged(data);
return true;
}
public void ForceHealthChangedEvent()
{
var data = new List<DamageChangeData>();
foreach (var type in _supportedTypes)
{
var damage = GetDamage(type);
var datum = new DamageChangeData(type, damage, 0);
data.Add(datum);
}
OnHealthChanged(data);
}
private void OnHealthChanged(List<DamageChangeData> changes)
{
var args = new DamageChangedEventArgs(this, changes);
OnHealthChanged(args);
}
protected virtual void OnHealthChanged(DamageChangedEventArgs e)
{
Owner.EntityManager.EventBus.RaiseEvent(EventSource.Local, e);
var message = new DamageChangedMessage(this, e.Data);
SendMessage(message);
Dirty();
}
void IRadiationAct.RadiationAct(float frameTime, SharedRadiationPulseComponent radiation)
{
var totalDamage = Math.Max((int)(frameTime * radiation.RadsPerSecond), 1);
ChangeDamage(DamageType.Radiation, totalDamage, false, radiation.Owner);
}
public void OnExplosion(ExplosionEventArgs eventArgs)
{
var damage = eventArgs.Severity switch
{
ExplosionSeverity.Light => 20,
ExplosionSeverity.Heavy => 60,
ExplosionSeverity.Destruction => 250,
_ => throw new ArgumentOutOfRangeException()
};
ChangeDamage(DamageType.Piercing, damage, false);
ChangeDamage(DamageType.Heat, damage, false);
}
}
[Serializable, NetSerializable]
public class DamageableComponentState : ComponentState
{
public readonly Dictionary<DamageType, int> DamageList;
public readonly DamageFlag Flags;
public DamageableComponentState(Dictionary<DamageType, int> damageList, DamageFlag flags) : base(ContentNetIDs.DAMAGEABLE)
{
DamageList = damageList;
Flags = flags;
}
}
}

View File

@@ -0,0 +1,163 @@
#nullable enable
using System.Collections.Generic;
using Content.Shared.Acts;
using Robust.Shared.GameObjects;
namespace Content.Shared.Damage.Components
{
public interface IDamageableComponent : IComponent, IExAct
{
/// <summary>
/// Sum of all damages taken.
/// </summary>
int TotalDamage { get; }
/// <summary>
/// The amount of damage mapped by <see cref="DamageClass"/>.
/// </summary>
IReadOnlyDictionary<DamageClass, int> DamageClasses { get; }
/// <summary>
/// The amount of damage mapped by <see cref="DamageType"/>.
/// </summary>
IReadOnlyDictionary<DamageType, int> DamageTypes { get; }
/// <summary>
/// The damage flags on this component.
/// </summary>
DamageFlag Flags { get; }
/// <summary>
/// Adds a flag to this component.
/// </summary>
/// <param name="flag">The flag to add.</param>
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 SupportsDamageType(DamageType type);
/// <summary>
/// Gets the amount of damage of a type.
/// </summary>
/// <param name="type">The type to get the damage of.</param>
/// <param name="damage">The amount of damage of that type.</param>
/// <returns>
/// True if the given <see cref="type"/> is supported, false otherwise.
/// </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.
/// </summary>
/// <param name="type">Type of damage being changed.</param>
/// <param name="amount">
/// Amount of damage being received (positive for damage, negative for heals).
/// </param>
/// <param name="ignoreResistances">
/// Whether or not to ignore resistances.
/// Healing always ignores resistances, regardless of this input.
/// </param>
/// <param name="source">
/// The entity that dealt or healed the damage, if any.
/// </param>
/// <param name="extraParams">
/// Extra parameters that some components may require, such as a specific limb to target.
/// </param>
/// <returns>
/// False if the given type is not supported or improper
/// <see cref="DamageChangeParams"/> were provided; true otherwise.
/// </returns>
bool ChangeDamage(
DamageType type,
int amount,
bool ignoreResistances,
IEntity? source = null,
DamageChangeParams? extraParams = null);
/// <summary>
/// Changes the specified <see cref="DamageClass"/>, applying
/// resistance values only if it is damage.
/// Spreads amount evenly between the <see cref="DamageType"></see>s
/// represented by that class.
/// </summary>
/// <param name="class">Class of damage being changed.</param>
/// <param name="amount">
/// Amount of damage being received (positive for damage, negative for heals).
/// </param>
/// <param name="ignoreResistances">
/// Whether to ignore resistances.
/// Healing always ignores resistances, regardless of this input.
/// </param>
/// <param name="source">Entity that dealt or healed the damage, if any.</param>
/// <param name="extraParams">
/// Extra parameters that some components may require,
/// such as a specific limb to target.
/// </param>
/// <returns>
/// Returns false if the given class is not supported or improper
/// <see cref="DamageChangeParams"/> were provided; true otherwise.
/// </returns>
bool ChangeDamage(
DamageClass @class,
int amount,
bool ignoreResistances,
IEntity? source = null,
DamageChangeParams? extraParams = null);
/// <summary>
/// Forcefully sets the specified <see cref="DamageType"/> to the given
/// value, ignoring resistance values.
/// </summary>
/// <param name="type">Type of damage being changed.</param>
/// <param name="newValue">New damage value to be set.</param>
/// <param name="source">Entity that set the new damage value.</param>
/// <param name="extraParams">
/// Extra parameters that some components may require,
/// such as a specific limb to target.
/// </param>
/// <returns>
/// Returns false if the given type is not supported or improper
/// <see cref="DamageChangeParams"/> were provided; true otherwise.
/// </returns>
bool SetDamage(
DamageType type,
int newValue,
IEntity? source = null,
DamageChangeParams? extraParams = null);
/// <summary>
/// Sets all damage values to zero.
/// </summary>
void Heal();
/// <summary>
/// Invokes the HealthChangedEvent with the current values of health.
/// </summary>
void ForceHealthChangedEvent();
}
}

View File

@@ -6,7 +6,7 @@ using Robust.Shared.Serialization;
using Robust.Shared.Serialization.Manager.Attributes;
using Robust.Shared.ViewVariables;
namespace Content.Shared.Damage.DamageContainer
namespace Content.Shared.Damage.Container
{
/// <summary>
/// Prototype for the DamageContainer class.

View File

@@ -0,0 +1,32 @@
#nullable enable
namespace Content.Shared.Damage
{
/// <summary>
/// Data class with information on how the value of a
/// single <see cref="DamageType"/> has changed.
/// </summary>
public struct DamageChangeData
{
/// <summary>
/// Type of damage that changed.
/// </summary>
public DamageType Type;
/// <summary>
/// The new current value for that damage.
/// </summary>
public int NewValue;
/// <summary>
/// How much the health value changed from its last value (negative is heals, positive is damage).
/// </summary>
public int Delta;
public DamageChangeData(DamageType type, int newValue, int delta)
{
Type = type;
NewValue = newValue;
Delta = delta;
}
}
}

View File

@@ -0,0 +1,18 @@
#nullable enable
using System;
using Content.Shared.Body.Components;
using Content.Shared.Damage.Components;
namespace Content.Shared.Damage
{
/// <summary>
/// Data class with information on how to damage a
/// <see cref="IDamageableComponent"/>.
/// While not necessary to damage for all instances, classes such as
/// <see cref="SharedBodyComponent"/> may require it for extra data
/// (such as selecting which limb to target).
/// </summary>
public class DamageChangeParams : EventArgs
{
}
}

View File

@@ -0,0 +1,36 @@
#nullable enable
using System;
using System.Collections.Generic;
using Content.Shared.Damage.Components;
namespace Content.Shared.Damage
{
public class DamageChangedEventArgs : EventArgs
{
public DamageChangedEventArgs(IDamageableComponent damageable, IReadOnlyList<DamageChangeData> data)
{
Damageable = damageable;
Data = data;
}
public DamageChangedEventArgs(IDamageableComponent damageable, DamageType type, int newValue, int delta)
{
Damageable = damageable;
var datum = new DamageChangeData(type, newValue, delta);
var data = new List<DamageChangeData> {datum};
Data = data;
}
/// <summary>
/// Reference to the <see cref="IDamageableComponent"/> that invoked the event.
/// </summary>
public IDamageableComponent Damageable { get; }
/// <summary>
/// List containing data on each <see cref="DamageType"/> that was changed.
/// </summary>
public IReadOnlyList<DamageChangeData> Data { get; }
}
}

View File

@@ -0,0 +1,52 @@
#nullable enable
using System.Collections.Generic;
using Content.Shared.Damage.Components;
using Robust.Shared.GameObjects;
namespace Content.Shared.Damage
{
public class DamageChangedMessage : ComponentMessage
{
public DamageChangedMessage(IDamageableComponent damageable, IReadOnlyList<DamageChangeData> data)
{
Damageable = damageable;
Data = data;
}
public DamageChangedMessage(IDamageableComponent damageable, DamageType type, int newValue, int delta)
{
Damageable = damageable;
var datum = new DamageChangeData(type, newValue, delta);
var data = new List<DamageChangeData> {datum};
Data = data;
}
/// <summary>
/// Reference to the <see cref="IDamageableComponent"/> that invoked the event.
/// </summary>
public IDamageableComponent Damageable { get; }
/// <summary>
/// List containing data on each <see cref="DamageType"/> that was changed.
/// </summary>
public IReadOnlyList<DamageChangeData> Data { get; }
public bool TookDamage
{
get
{
foreach (var datum in Data)
{
if (datum.Delta > 0)
{
return true;
}
}
return false;
}
}
}
}

View File

@@ -0,0 +1,14 @@
#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

@@ -0,0 +1,67 @@
#nullable enable
using System.Collections.Generic;
using System.Collections.Immutable;
using JetBrains.Annotations;
using Robust.Shared.GameObjects;
namespace Content.Shared.Damage
{
[UsedImplicitly]
public class DamageSystem : EntitySystem
{
public static ImmutableDictionary<DamageClass, ImmutableList<DamageType>> ClassToType { get; } = DefaultClassToType();
public static ImmutableDictionary<DamageType, DamageClass> TypeToClass { get; } = DefaultTypeToClass();
private static ImmutableDictionary<DamageClass, ImmutableList<DamageType>> DefaultClassToType()
{
return new Dictionary<DamageClass, ImmutableList<DamageType>>
{
[DamageClass.Brute] = new List<DamageType>
{
DamageType.Blunt,
DamageType.Slash,
DamageType.Piercing
}.ToImmutableList(),
[DamageClass.Burn] = new List<DamageType>
{
DamageType.Heat,
DamageType.Shock,
DamageType.Cold
}.ToImmutableList(),
[DamageClass.Toxin] = new List<DamageType>
{
DamageType.Poison,
DamageType.Radiation
}.ToImmutableList(),
[DamageClass.Airloss] = new List<DamageType>
{
DamageType.Asphyxiation,
DamageType.Bloodloss
}.ToImmutableList(),
[DamageClass.Genetic] = new List<DamageType>
{
DamageType.Cellular
}.ToImmutableList()
}.ToImmutableDictionary();
}
private static ImmutableDictionary<DamageType, DamageClass> DefaultTypeToClass()
{
return new Dictionary<DamageType, DamageClass>
{
{DamageType.Blunt, DamageClass.Brute},
{DamageType.Slash, DamageClass.Brute},
{DamageType.Piercing, DamageClass.Brute},
{DamageType.Heat, DamageClass.Burn},
{DamageType.Shock, DamageClass.Burn},
{DamageType.Cold, DamageClass.Burn},
{DamageType.Poison, DamageClass.Toxin},
{DamageType.Radiation, DamageClass.Toxin},
{DamageType.Asphyxiation, DamageClass.Airloss},
{DamageType.Bloodloss, DamageClass.Airloss},
{DamageType.Cellular, DamageClass.Genetic}
}.ToImmutableDictionary();
}
}
}

View File

@@ -4,7 +4,7 @@ using System.Collections.Generic;
using Robust.Shared.Serialization;
using Robust.Shared.ViewVariables;
namespace Content.Shared.Damage.ResistanceSet
namespace Content.Shared.Damage.Resistances
{
/// <summary>
/// Set of resistances used by damageable objects.

View File

@@ -6,7 +6,7 @@ using Robust.Shared.Serialization;
using Robust.Shared.Serialization.Manager.Attributes;
using Robust.Shared.ViewVariables;
namespace Content.Shared.Damage.ResistanceSet
namespace Content.Shared.Damage.Resistances
{
/// <summary>
/// Prototype for the BodyPart class.