Generalize ReagentUnit into FixedPoint2 and use it for damage calculations (#5151)

* Damage units

* sum ext method
This commit is contained in:
mirrorcult
2021-11-03 16:48:03 -07:00
committed by GitHub
parent 8165d8f38c
commit 3ab4a30a0f
100 changed files with 730 additions and 601 deletions

View File

@@ -2,6 +2,7 @@ using Content.Client.Eui;
using Content.Shared.Administration;
using Content.Shared.Chemistry.Reagent;
using Content.Shared.Eui;
using Content.Shared.FixedPoint;
using JetBrains.Annotations;
using Robust.Client.UserInterface.Controls;
using Robust.Client.UserInterface.CustomControls;
@@ -40,7 +41,7 @@ namespace Content.Client.Administration.UI
_window.HandleState((AdminAddReagentEuiState) state);
}
private void DoAdd(bool close, string reagentId, ReagentUnit amount)
private void DoAdd(bool close, string reagentId, FixedPoint2 amount)
{
SendMessage(new AdminAddReagentEuiMsg.DoAdd
{
@@ -121,7 +122,7 @@ namespace Content.Client.Administration.UI
_eui.DoAdd(
close,
_reagentIdEdit.Text,
ReagentUnit.New(float.Parse(_amountEdit.Text)));
FixedPoint2.New(float.Parse(_amountEdit.Text)));
}
private void CheckErrors()

View File

@@ -3,6 +3,7 @@ using Content.Client.Message;
using Content.Client.Stylesheets;
using Content.Shared.Chemistry.Components;
using Content.Shared.Chemistry.Reagent;
using Content.Shared.FixedPoint;
using Robust.Client.UserInterface;
using Robust.Client.UserInterface.Controls;
using Robust.Shared.GameObjects;
@@ -15,8 +16,8 @@ namespace Content.Client.Chemistry.Components
[RegisterComponent]
public sealed class HyposprayComponent : SharedHyposprayComponent, IItemStatus
{
[ViewVariables] private ReagentUnit CurrentVolume { get; set; }
[ViewVariables] private ReagentUnit TotalVolume { get; set; }
[ViewVariables] private FixedPoint2 CurrentVolume { get; set; }
[ViewVariables] private FixedPoint2 TotalVolume { get; set; }
[ViewVariables(VVAccess.ReadWrite)] private bool _uiUpdateNeeded;
public override void HandleComponentState(ComponentState? curState, ComponentState? nextState)

View File

@@ -3,6 +3,7 @@ using Content.Client.Message;
using Content.Client.Stylesheets;
using Content.Shared.Chemistry.Components;
using Content.Shared.Chemistry.Reagent;
using Content.Shared.FixedPoint;
using Robust.Client.UserInterface;
using Robust.Client.UserInterface.Controls;
using Robust.Shared.GameObjects;
@@ -18,8 +19,8 @@ namespace Content.Client.Chemistry.Components
[RegisterComponent]
public class InjectorComponent : SharedInjectorComponent, IItemStatus
{
[ViewVariables] private ReagentUnit CurrentVolume { get; set; }
[ViewVariables] private ReagentUnit TotalVolume { get; set; }
[ViewVariables] private FixedPoint2 CurrentVolume { get; set; }
[ViewVariables] private FixedPoint2 TotalVolume { get; set; }
[ViewVariables] private InjectorToggleMode CurrentMode { get; set; }
[ViewVariables(VVAccess.ReadWrite)] private bool _uiUpdateNeeded;

View File

@@ -4,6 +4,7 @@ using Content.Client.Stylesheets;
using Content.Client.UserInterface;
using Content.Shared.Chemistry.Components;
using Content.Shared.Chemistry.Reagent;
using Content.Shared.FixedPoint;
using Robust.Client.AutoGenerated;
using Robust.Client.UserInterface;
using Robust.Client.UserInterface.Controls;
@@ -47,7 +48,7 @@ namespace Content.Client.Chemistry.UI
BottleAmount.InitDefaultButtons();
}
private ChemButton MakeChemButton(string text, ReagentUnit amount, string id, bool isBuffer, string styleClass)
private ChemButton MakeChemButton(string text, FixedPoint2 amount, string id, bool isBuffer, string styleClass)
{
var button = new ChemButton(text, amount, id, isBuffer, styleClass);
button.OnPressed += args
@@ -129,11 +130,11 @@ namespace Content.Client.Chemistry.UI
//Padding
new Control {HorizontalExpand = true},
MakeChemButton("1", ReagentUnit.New(1), reagent.ReagentId, false, StyleBase.ButtonOpenRight),
MakeChemButton("5", ReagentUnit.New(5), reagent.ReagentId, false, StyleBase.ButtonOpenBoth),
MakeChemButton("10", ReagentUnit.New(10), reagent.ReagentId, false, StyleBase.ButtonOpenBoth),
MakeChemButton("25", ReagentUnit.New(25), reagent.ReagentId, false, StyleBase.ButtonOpenBoth),
MakeChemButton(Loc.GetString("chem-master-window-buffer-all-amount"), ReagentUnit.New(-1), reagent.ReagentId, false, StyleBase.ButtonOpenLeft),
MakeChemButton("1", FixedPoint2.New(1), reagent.ReagentId, false, StyleBase.ButtonOpenRight),
MakeChemButton("5", FixedPoint2.New(5), reagent.ReagentId, false, StyleBase.ButtonOpenBoth),
MakeChemButton("10", FixedPoint2.New(10), reagent.ReagentId, false, StyleBase.ButtonOpenBoth),
MakeChemButton("25", FixedPoint2.New(25), reagent.ReagentId, false, StyleBase.ButtonOpenBoth),
MakeChemButton(Loc.GetString("chem-master-window-buffer-all-amount"), FixedPoint2.New(-1), reagent.ReagentId, false, StyleBase.ButtonOpenLeft),
}
});
}
@@ -189,11 +190,11 @@ namespace Content.Client.Chemistry.UI
//Padding
new Control {HorizontalExpand = true},
MakeChemButton("1", ReagentUnit.New(1), reagent.ReagentId, true, StyleBase.ButtonOpenRight),
MakeChemButton("5", ReagentUnit.New(5), reagent.ReagentId, true, StyleBase.ButtonOpenBoth),
MakeChemButton("10", ReagentUnit.New(10), reagent.ReagentId, true, StyleBase.ButtonOpenBoth),
MakeChemButton("25", ReagentUnit.New(25), reagent.ReagentId, true, StyleBase.ButtonOpenBoth),
MakeChemButton(Loc.GetString("chem-master-window-buffer-all-amount"), ReagentUnit.New(-1), reagent.ReagentId, true, StyleBase.ButtonOpenLeft),
MakeChemButton("1", FixedPoint2.New(1), reagent.ReagentId, true, StyleBase.ButtonOpenRight),
MakeChemButton("5", FixedPoint2.New(5), reagent.ReagentId, true, StyleBase.ButtonOpenBoth),
MakeChemButton("10", FixedPoint2.New(10), reagent.ReagentId, true, StyleBase.ButtonOpenBoth),
MakeChemButton("25", FixedPoint2.New(25), reagent.ReagentId, true, StyleBase.ButtonOpenBoth),
MakeChemButton(Loc.GetString("chem-master-window-buffer-all-amount"), FixedPoint2.New(-1), reagent.ReagentId, true, StyleBase.ButtonOpenLeft),
}
});
}
@@ -203,10 +204,10 @@ namespace Content.Client.Chemistry.UI
public class ChemButton : Button
{
public ReagentUnit Amount { get; set; }
public FixedPoint2 Amount { get; set; }
public bool isBuffer = true;
public string Id { get; set; }
public ChemButton(string _text, ReagentUnit _amount, string _id, bool _isBuffer, string _styleClass)
public ChemButton(string _text, FixedPoint2 _amount, string _id, bool _isBuffer, string _styleClass)
{
AddStyleClass(_styleClass);
Text = _text;

View File

@@ -1,5 +1,6 @@
using Content.Shared.Chemistry;
using Content.Shared.Chemistry.Reagent;
using Content.Shared.FixedPoint;
using JetBrains.Annotations;
using Robust.Client.GameObjects;
using Robust.Shared.GameObjects;
@@ -20,7 +21,7 @@ namespace Content.Client.Chemistry.UI
{
if (int.TryParse(_window.AmountLineEdit.Text, out var i))
{
SendMessage(new TransferAmountSetValueMessage(ReagentUnit.New(i)));
SendMessage(new TransferAmountSetValueMessage(FixedPoint2.New(i)));
_window.Close();
}
};

View File

@@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Linq;
using Content.Shared.Damage;
using Content.Shared.Damage.Prototypes;
using Content.Shared.FixedPoint;
using Robust.Client.GameObjects;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
@@ -58,7 +59,7 @@ namespace Content.Client.Damage
/// isn't required for it.
/// </remarks>
[DataField("thresholds", required: true)]
private List<int> _thresholds = new();
private List<FixedPoint2> _thresholds = new();
/// <summary>
/// Layers to target, by layerMapKey.
@@ -295,7 +296,7 @@ namespace Content.Client.Damage
|| !entity.TryGetComponent<AppearanceComponent>(out var appearanceComponent))
return;
_thresholds.Add(0);
_thresholds.Add(FixedPoint2.Zero);
_thresholds.Sort();
if (_thresholds[0] != 0)
@@ -324,7 +325,7 @@ namespace Content.Client.Damage
return;
}
damageData.LastThresholdPerGroup.Add(damageType, 0);
damageData.LastThresholdPerGroup.Add(damageType, FixedPoint2.Zero);
}
}
// Are we tracking a single damage group without overlay instead?
@@ -338,7 +339,7 @@ namespace Content.Client.Damage
return;
}
damageData.LastThresholdPerGroup.Add(_damageGroup, 0);
damageData.LastThresholdPerGroup.Add(_damageGroup, FixedPoint2.Zero);
}
}
// Ditto above, but instead we go through every group.
@@ -356,7 +357,7 @@ namespace Content.Client.Damage
damageData.Valid = false;
return;
}
damageData.LastThresholdPerGroup.Add(damageType, 0);
damageData.LastThresholdPerGroup.Add(damageType, FixedPoint2.Zero);
}
else if (_damageGroup != null)
{
@@ -367,7 +368,7 @@ namespace Content.Client.Damage
return;
}
damageData.LastThresholdPerGroup.Add(_damageGroup, 0);
damageData.LastThresholdPerGroup.Add(_damageGroup, FixedPoint2.Zero);
}
}
@@ -604,7 +605,7 @@ namespace Content.Client.Damage
{
foreach (var (damageGroup, sprite) in _damageOverlayGroups)
{
int threshold = damageData.LastThresholdPerGroup[damageGroup];
FixedPoint2 threshold = damageData.LastThresholdPerGroup[damageGroup];
ReorderOverlaySprite(spriteComponent,
damageData,
sprite,
@@ -625,12 +626,12 @@ namespace Content.Client.Damage
}
}
private void ReorderOverlaySprite(SpriteComponent spriteComponent, DamageVisualizerDataComponent damageData, DamageVisualizerSprite sprite, string key, string statePrefix, int threshold)
private void ReorderOverlaySprite(SpriteComponent spriteComponent, DamageVisualizerDataComponent damageData, DamageVisualizerSprite sprite, string key, string statePrefix, FixedPoint2 threshold)
{
spriteComponent.LayerMapTryGet(key, out int spriteLayer);
bool visibility = spriteComponent[spriteLayer].Visible;
spriteComponent.RemoveLayer(spriteLayer);
if (threshold == 0) // these should automatically be invisible
if (threshold == FixedPoint2.Zero) // these should automatically be invisible
threshold = _thresholds[1];
spriteLayer = spriteComponent.AddLayer(
new SpriteSpecifier.Rsi(
@@ -650,7 +651,7 @@ namespace Content.Client.Damage
/// </summary>
private void UpdateDamageVisuals(DamageableComponent damageComponent, SpriteComponent spriteComponent, DamageVisualizerDataComponent damageData)
{
if (!CheckThresholdBoundary(damageComponent.TotalDamage, damageData.LastDamageThreshold, out int threshold))
if (!CheckThresholdBoundary(damageComponent.TotalDamage, damageData.LastDamageThreshold, out FixedPoint2 threshold))
return;
damageData.LastDamageThreshold = threshold;
@@ -679,11 +680,11 @@ namespace Content.Client.Damage
continue;
if (!_prototypeManager.TryIndex<DamageGroupPrototype>(damageGroup, out var damageGroupPrototype)
|| !damageComponent.Damage.TryGetDamageInGroup(damageGroupPrototype, out int damageTotal))
|| !damageComponent.Damage.TryGetDamageInGroup(damageGroupPrototype, out FixedPoint2 damageTotal))
continue;
if (!damageData.LastThresholdPerGroup.TryGetValue(damageGroup, out int lastThreshold)
|| !CheckThresholdBoundary(damageTotal, lastThreshold, out int threshold))
if (!damageData.LastThresholdPerGroup.TryGetValue(damageGroup, out FixedPoint2 lastThreshold)
|| !CheckThresholdBoundary(damageTotal, lastThreshold, out FixedPoint2 threshold))
continue;
damageData.LastThresholdPerGroup[damageGroup] = threshold;
@@ -704,10 +705,10 @@ namespace Content.Client.Damage
/// <summary>
/// Checks if a threshold boundary was passed.
/// </summary>
private bool CheckThresholdBoundary(int damageTotal, int lastThreshold, out int threshold)
private bool CheckThresholdBoundary(FixedPoint2 damageTotal, FixedPoint2 lastThreshold, out FixedPoint2 threshold)
{
threshold = 0;
damageTotal = (int) Math.Floor(damageTotal / _divisor);
threshold = FixedPoint2.Zero;
damageTotal = damageTotal / _divisor;
int thresholdIndex = _thresholds.BinarySearch(damageTotal);
if (thresholdIndex < 0)
@@ -753,7 +754,7 @@ namespace Content.Client.Damage
/// it assumes you're updating a layer that is tracking all
/// damage.
/// </summary>
private void UpdateTargetLayer(SpriteComponent spriteComponent, DamageVisualizerDataComponent damageData, object layerMapKey, int threshold)
private void UpdateTargetLayer(SpriteComponent spriteComponent, DamageVisualizerDataComponent damageData, object layerMapKey, FixedPoint2 threshold)
{
if (_overlay && _damageOverlayGroups != null)
{
@@ -783,7 +784,7 @@ namespace Content.Client.Damage
/// <summary>
/// Updates a target layer by damage group.
/// </summary>
private void UpdateTargetLayer(SpriteComponent spriteComponent, DamageVisualizerDataComponent damageData, object layerMapKey, string damageGroup, int threshold)
private void UpdateTargetLayer(SpriteComponent spriteComponent, DamageVisualizerDataComponent damageData, object layerMapKey, string damageGroup, FixedPoint2 threshold)
{
if (_overlay && _damageOverlayGroups != null)
{
@@ -813,7 +814,7 @@ namespace Content.Client.Damage
/// <summary>
/// Updates an overlay that is tracking all damage.
/// </summary>
private void UpdateOverlay(SpriteComponent spriteComponent, int threshold)
private void UpdateOverlay(SpriteComponent spriteComponent, FixedPoint2 threshold)
{
spriteComponent.LayerMapTryGet($"DamageOverlay", out int spriteLayer);
@@ -826,7 +827,7 @@ namespace Content.Client.Damage
/// <summary>
/// Updates an overlay based on damage group.
/// </summary>
private void UpdateOverlay(SpriteComponent spriteComponent, string damageGroup, int threshold)
private void UpdateOverlay(SpriteComponent spriteComponent, string damageGroup, FixedPoint2 threshold)
{
if (_damageOverlayGroups != null)
{
@@ -848,7 +849,7 @@ namespace Content.Client.Damage
/// function calls it), and what threshold
/// was passed into it.
/// </summary>
private void UpdateDamageLayerState(SpriteComponent spriteComponent, int spriteLayer, string statePrefix, int threshold)
private void UpdateDamageLayerState(SpriteComponent spriteComponent, int spriteLayer, string statePrefix, FixedPoint2 threshold)
{
if (threshold == 0)
{

View File

@@ -1,4 +1,5 @@
using System.Collections.Generic;
using Content.Shared.FixedPoint;
using Robust.Shared.GameObjects;
namespace Content.Client.Damage
@@ -17,10 +18,10 @@ namespace Content.Client.Damage
public List<object> TargetLayerMapKeys = new();
public bool Disabled = false;
public bool Valid = true;
public int LastDamageThreshold = 0;
public FixedPoint2 LastDamageThreshold = FixedPoint2.Zero;
public Dictionary<object, bool> DisabledLayers = new();
public Dictionary<object, string> LayerMapKeyStates = new();
public Dictionary<string, int> LastThresholdPerGroup = new();
public Dictionary<string, FixedPoint2> LastThresholdPerGroup = new();
public string TopMostLayerKey = default!;
}
}

View File

@@ -1,6 +1,7 @@
using Content.Client.IoC;
using Content.Client.Resources;
using Content.Shared.Damage;
using Content.Shared.FixedPoint;
using Content.Shared.MobState;
using Robust.Client.Graphics;
using Robust.Client.UserInterface;
@@ -84,7 +85,7 @@ namespace Content.Client.HealthOverlay.UI
return;
}
int threshold;
FixedPoint2 threshold;
if (mobState.IsAlive())
{
@@ -97,7 +98,7 @@ namespace Content.Client.HealthOverlay.UI
CritBar.Ratio = 1;
CritBar.Visible = true;
HealthBar.Ratio = 1 - (float) damageable.TotalDamage / threshold;
HealthBar.Ratio = 1 - (damageable.TotalDamage / threshold).Float();
HealthBar.Visible = true;
}
else if (mobState.IsCritical())
@@ -113,9 +114,9 @@ namespace Content.Client.HealthOverlay.UI
}
CritBar.Visible = true;
CritBar.Ratio = 1 - (float)
(damageable.TotalDamage - critThreshold) /
(deadThreshold - critThreshold);
CritBar.Ratio = 1 -
((damageable.TotalDamage - critThreshold) /
(deadThreshold - critThreshold)).Float();
}
else if (mobState.IsDead())
{

View File

@@ -9,6 +9,7 @@ using Robust.Shared.Localization;
using Robust.Shared.Prototypes;
using static Content.Shared.MedicalScanner.SharedMedicalScannerComponent;
using Content.Shared.Damage.Prototypes;
using Content.Shared.FixedPoint;
using Robust.Client.AutoGenerated;
using Robust.Client.UserInterface.XAML;
@@ -39,6 +40,7 @@ namespace Content.Client.MedicalScanner.UI
text.Append($"{Loc.GetString("medical-scanner-window-entity-health-text", ("entityName", entity.Name))}\n");
var totalDamage = state.DamagePerType.Values.Sum();
text.Append($"{Loc.GetString("medical-scanner-window-entity-damage-total-text", ("amount", totalDamage))}\n");
HashSet<string> shownTypes = new();