Files
crystall-punk-14/Content.Shared/Damage/DamageContainer/DamageContainer.cs

294 lines
9.0 KiB
C#
Raw Normal View History

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>
2020-08-17 01:42:42 +02:00
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);
}
}
}