Bodysystem and damagesystem rework (#1544)

* Things and stuff with grids, unfinished w/ code debug changes.

* Updated submodule and also lost some progress cause I fucked it up xd

* First unfinished draft of the BodySystem. Doesn't compile.

* More changes to make it compile, but still just a framework. Doesn't do anything at the moment.

* Many cleanup changes.

* Revert "Merge branch 'master' of https://github.com/GlassEclipse/space-station-14 into body_system"

This reverts commit ddd4aebbc76cf2a0b7b102f72b93d55a0816c88c, reversing
changes made to 12d0dd752706bdda8879393bd8191a1199a0c978.

* Commit human.yml

* Updated a lot of things to be more classy, more progress overall, etc. etc.

* Latest update with many changes

* Minor changes

* Fixed Travis build bug

* Adds first draft of Body Scanner console, apparently I also forgot to tie Mechanisms into body parts so now a heart just sits in the Torso like a good boy :)

* Commit rest of stuff

* Latest changes

* Latest changes again

* 14 naked cowboys

* Yay!

* Latest changes (probably doesnt compile)

* Surgery!!!!!!!!!~1116y

* Cleaned some stuff up

* More cleanup

* Refactoring of code. Basic surgery path now done.

* Removed readme, has been added to HackMD

* Fixes typo (and thus test errors)

* WIP changes, committing so I can pull latest master changes

* Still working on that god awful merge

* Latest changes

* Latest changes!!

* Beginning of refactor to BoundUserInterface

* Surgery!

* Latest changes - fixes pr change requests and random fixes

* oops

* Fixes bodypart recursion

* Beginning of work on revamping the damage system.

* More latest changes

* Latest changes

* Finished merge

* Commit before removing old healthcode

* Almost done with removing speciescomponent...

* It compiles!!!

* yahoo more work

* Fixes to make it work

* Merge conflict fixes

* Deleting species visualizer was a mistake

* IDE warnings are VERBOTEN

* makes the server not kill itself on startup, some cleanup (#1)

* Namespaces, comments and exception fixes

* Fix conveyor and conveyor switch serialization

SS14 in reactive when

* Move damage, acts and body to shared

Damage cleanup
Comment cleanup

* Rename SpeciesComponent to RotationComponent and cleanup

Damage cleanup
Comment cleanup

* Fix nullable warnings

* Address old reviews

Fix off welder suicide damage type, deathmatch and suspicion

* Fix new test fail with units being able to accept items when unpowered

* Remove RotationComponent, change references to IBodyManagerComponent

* Add a bloodstream to humans

* More cleanups

* Add body conduits, connections, connectors substances and valves

* Revert "Add body conduits, connections, connectors substances and valves"

This reverts commit 9ab0b50e6b15fe98852d7b0836c0cdbf4bd76d20.

* Implement the heart mechanism behavior with the circulatory network

* Added network property to mechanism behaviors

* Changed human organ sprites and added missing ones

* Fix tests

* Add individual body part sprite rendering

* Fix error where dropped mechanisms are not initialized

* Implement client/server body damage

* Make DamageContainer take care of raising events

* Reimplement medical scanner with the new body system

* Improve the medical scanner ui

* Merge conflict fixes

* Fix crash when colliding with something

* Fix microwave suicides and eyes sprite rendering

* Fix nullable reference error

* Fix up surgery client side

* Fix missing using from merge conflict

* Add breathing

*inhale

* Merge conflict fixes

* Fix accumulatedframetime being reset to 0 instead of decreased by the threshold

https://github.com/space-wizards/space-station-14/pull/1617

* Use and add to the new AtmosHelpers

* Fix feet

* Add proper coloring to dropped body parts

* Fix Urist's lungs being too strong

* Merge conflict fixes

* Merge conflict fixes

* Merge conflict fixes

Co-authored-by: GlassEclipse <tsymall5@gmail.com>
Co-authored-by: Pieter-Jan Briers <pieterjan.briers+git@gmail.com>
Co-authored-by: AJCM-git <60196617+AJCM-git@users.noreply.github.com>
This commit is contained in:
DrSmugleaf
2020-08-17 01:42:42 +02:00
committed by GitHub
parent c17dd97383
commit b051261485
276 changed files with 7853 additions and 4737 deletions

View File

@@ -0,0 +1,41 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using Robust.Shared.Serialization;
namespace Content.Shared.Damage
{
[Serializable, NetSerializable]
public enum DamageClass
{
Brute,
Burn,
Toxin,
Airloss
}
public static class DamageClassExtensions
{
private static readonly ImmutableDictionary<DamageClass, List<DamageType>> ClassToType =
new Dictionary<DamageClass, List<DamageType>>
{
{DamageClass.Brute, new List<DamageType> {DamageType.Blunt, DamageType.Piercing}},
{DamageClass.Burn, new List<DamageType> {DamageType.Heat, DamageType.Disintegration}},
{DamageClass.Toxin, new List<DamageType> {DamageType.Cellular, DamageType.DNA}},
{DamageClass.Airloss, new List<DamageType> {DamageType.Asphyxiation}}
}.ToImmutableDictionary();
public static List<DamageType> ToTypes(this DamageClass @class)
{
return ClassToType[@class];
}
public static Dictionary<DamageClass, int> ToDictionary()
{
return Enum.GetValues(typeof(DamageClass))
.Cast<DamageClass>()
.ToDictionary(@class => @class, type => 0);
}
}
}

View File

@@ -0,0 +1,293 @@
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)
{
OnHealthChanged = onHealthChanged;
SupportedClasses = data.ActiveDamageClasses;
}
public DamageContainer(HealthChangedDelegate onHealthChanged, List<DamageClass> supportedClasses)
{
OnHealthChanged = onHealthChanged;
SupportedClasses = supportedClasses;
}
public DamageContainer(HealthChangedDelegate onHealthChanged)
{
OnHealthChanged = onHealthChanged;
}
[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

@@ -0,0 +1,32 @@
using System;
using System.Collections.Generic;
using Robust.Shared.Prototypes;
using Robust.Shared.Serialization;
using Robust.Shared.ViewVariables;
using YamlDotNet.RepresentationModel;
namespace Content.Shared.Damage.DamageContainer
{
/// <summary>
/// Prototype for the DamageContainer class.
/// </summary>
[Prototype("damageContainer")]
[NetSerializable]
[Serializable]
public class DamageContainerPrototype : IPrototype, IIndexedPrototype
{
private List<DamageClass> _activeDamageClasses;
private string _id;
[ViewVariables] public List<DamageClass> ActiveDamageClasses => _activeDamageClasses;
[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>());
}
}
}

View File

@@ -0,0 +1,66 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using Robust.Shared.Serialization;
namespace Content.Shared.Damage
{
[Serializable, NetSerializable]
public enum DamageType
{
Blunt,
Piercing,
Heat,
Disintegration,
Cellular,
DNA,
Asphyxiation
}
public static class DamageTypeExtensions
{
// TODO: Automatically generate this
private static readonly ImmutableDictionary<DamageType, DamageClass> TypeToClass =
new Dictionary<DamageType, DamageClass>
{
{DamageType.Blunt, DamageClass.Brute},
{DamageType.Piercing, DamageClass.Brute},
{DamageType.Heat, DamageClass.Burn},
{DamageType.Disintegration, DamageClass.Burn},
{DamageType.Cellular, DamageClass.Toxin},
{DamageType.DNA, DamageClass.Toxin},
{DamageType.Asphyxiation, DamageClass.Airloss}
}.ToImmutableDictionary();
public static DamageClass ToClass(this DamageType type)
{
return TypeToClass[type];
}
public static Dictionary<DamageType, int> ToDictionary()
{
return Enum.GetValues(typeof(DamageType))
.Cast<DamageType>()
.ToDictionary(type => type, type => 0);
}
public static Dictionary<DamageClass, int> ToClassDictionary(IReadOnlyDictionary<DamageType, int> types)
{
var classes = DamageClassExtensions.ToDictionary();
foreach (var @class in classes.Keys.ToList())
foreach (var type in @class.ToTypes())
{
if (!types.TryGetValue(type, out var damage))
{
continue;
}
classes[@class] += damage;
}
return classes;
}
}
}

View File

@@ -0,0 +1,73 @@
using System;
using System.Collections.Generic;
using Robust.Shared.Serialization;
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.
/// </summary>
[NetSerializable]
[Serializable]
public class ResistanceSet
{
[ViewVariables]
private Dictionary<DamageType, ResistanceSetSettings> _resistances =
new Dictionary<DamageType, ResistanceSetSettings>();
public ResistanceSet()
{
foreach (var damageType in (DamageType[]) Enum.GetValues(typeof(DamageType)))
{
_resistances.Add(damageType, new ResistanceSetSettings(1f, 0));
}
}
public ResistanceSet(ResistanceSetPrototype data)
{
_resistances = data.Resistances;
}
/// <summary>
/// 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.
{
amount -= _resistances[damageType].FlatReduction;
if (amount <= 0)
{
return 0;
}
}
amount = (int) Math.Ceiling(amount * _resistances[damageType].Coefficient);
return amount;
}
}
/// <summary>
/// Settings for a specific damage type in a resistance set. Flat reduction is applied before the coefficient.
/// </summary>
[NetSerializable]
[Serializable]
public struct ResistanceSetSettings
{
[ViewVariables] public float Coefficient { get; private set; }
[ViewVariables] public int FlatReduction { get; private set; }
public ResistanceSetSettings(float coefficient, int flatReduction)
{
Coefficient = coefficient;
FlatReduction = flatReduction;
}
}
}

View File

@@ -0,0 +1,46 @@
using System;
using System.Collections.Generic;
using Robust.Shared.Prototypes;
using Robust.Shared.Serialization;
using Robust.Shared.ViewVariables;
using YamlDotNet.RepresentationModel;
namespace Content.Shared.Damage.ResistanceSet
{
/// <summary>
/// Prototype for the BodyPart class.
/// </summary>
[Prototype("resistanceSet")]
[NetSerializable]
[Serializable]
public class ResistanceSetPrototype : IPrototype, IIndexedPrototype
{
private Dictionary<DamageType, float> _coefficients;
private Dictionary<DamageType, int> _flatReductions;
private string _id;
[ViewVariables] public Dictionary<DamageType, float> Coefficients => _coefficients;
[ViewVariables] public Dictionary<DamageType, int> FlatReductions => _flatReductions;
[ViewVariables] public Dictionary<DamageType, ResistanceSetSettings> Resistances { get; private set; }
[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 _coefficients, "coefficients", null);
serializer.DataField(ref _flatReductions, "flatReductions", null);
Resistances = new Dictionary<DamageType, ResistanceSetSettings>();
foreach (var damageType in (DamageType[]) Enum.GetValues(typeof(DamageType)))
{
Resistances.Add(damageType,
new ResistanceSetSettings(_coefficients[damageType], _flatReductions[damageType]));
}
}
}
}