Predicted armor (#20560)

This commit is contained in:
Nemanja
2023-09-28 06:48:50 -04:00
committed by GitHub
parent 9ad551e3dd
commit b052e0f242
7 changed files with 135 additions and 124 deletions

View File

@@ -0,0 +1,9 @@
using Content.Shared.Armor;
namespace Content.Client.Armor;
/// <inheritdoc/>
public sealed class ArmorSystem : SharedArmorSystem
{
}

View File

@@ -1,11 +0,0 @@
using Content.Shared.Damage;
namespace Content.Server.Armor
{
[RegisterComponent]
public sealed partial class ArmorComponent : Component
{
[DataField("modifiers", required: true)]
public DamageModifierSet Modifiers = default!;
}
}

View File

@@ -1,125 +1,34 @@
using Content.Shared.Damage;
using Content.Server.Examine;
using Content.Shared.Verbs;
using Robust.Shared.Utility;
using Content.Server.Cargo.Systems;
using Content.Shared.Armor;
using Robust.Shared.Prototypes;
using Content.Shared.Damage.Prototypes;
using Content.Shared.Inventory;
using Content.Shared.Silicons.Borgs;
namespace Content.Server.Armor
{
public sealed class ArmorSystem : EntitySystem
{
const double CoefDefaultPrice = 2; // default price of 1% protection against any type of damage
const double FlatDefaultPrice = 10; //default price of 1 damage protection against a certain type of damage
namespace Content.Server.Armor;
[Dependency] private readonly ExamineSystem _examine = default!;
/// <inheritdoc/>
public sealed class ArmorSystem : SharedArmorSystem
{
[Dependency] private readonly IPrototypeManager _protoManager = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<ArmorComponent, InventoryRelayedEvent<DamageModifyEvent>>(OnDamageModify);
SubscribeLocalEvent<ArmorComponent, BorgModuleRelayedEvent<DamageModifyEvent>>(OnBorgDamageModify);
SubscribeLocalEvent<ArmorComponent, GetVerbsEvent<ExamineVerb>>(OnArmorVerbExamine);
SubscribeLocalEvent<ArmorComponent, PriceCalculationEvent>(GetArmorPrice);
}
private void GetArmorPrice(EntityUid uid, ArmorComponent component, ref PriceCalculationEvent args)
{
if (component.Modifiers == null)
return;
double price = 0;
foreach (var modifier in component.Modifiers.Coefficients)
{
_protoManager.TryIndex(modifier.Key, out DamageTypePrototype? damageType);
var damageType = _protoManager.Index<DamageTypePrototype>(modifier.Key);
args.Price += damageType.ArmorPriceCoefficient * 100 * (1 - modifier.Value);
}
if (damageType != null)
{
price += damageType.ArmorPriceCoefficient * 100 * (1 - modifier.Value);
}
else
{
price += CoefDefaultPrice * 100 * (1 - modifier.Value);
}
}
foreach (var modifier in component.Modifiers.FlatReduction)
{
_protoManager.TryIndex(modifier.Key, out DamageTypePrototype? damageType);
if (damageType != null)
{
price += damageType.ArmorPriceFlat * modifier.Value;
}
else
{
price += FlatDefaultPrice * modifier.Value;
}
}
args.Price += price;
}
private void OnDamageModify(EntityUid uid, ArmorComponent component, InventoryRelayedEvent<DamageModifyEvent> args)
{
args.Args.Damage = DamageSpecifier.ApplyModifierSet(args.Args.Damage, component.Modifiers);
}
private void OnBorgDamageModify(EntityUid uid, ArmorComponent component, ref BorgModuleRelayedEvent<DamageModifyEvent> args)
{
args.Args.Damage = DamageSpecifier.ApplyModifierSet(args.Args.Damage, component.Modifiers);
}
private void OnArmorVerbExamine(EntityUid uid, ArmorComponent component, GetVerbsEvent<ExamineVerb> args)
{
if (!args.CanInteract || !args.CanAccess)
return;
var armorModifiers = component.Modifiers;
if (armorModifiers == null)
return;
var examineMarkup = GetArmorExamine(armorModifiers);
var ev = new ArmorExamineEvent(examineMarkup);
RaiseLocalEvent(uid, ref ev);
_examine.AddDetailedExamineVerb(args, component, examineMarkup, Loc.GetString("armor-examinable-verb-text"), "/Textures/Interface/VerbIcons/dot.svg.192dpi.png", Loc.GetString("armor-examinable-verb-message"));
}
private FormattedMessage GetArmorExamine(DamageModifierSet armorModifiers)
{
var msg = new FormattedMessage();
msg.AddMarkup(Loc.GetString("armor-examine"));
foreach (var coefficientArmor in armorModifiers.Coefficients)
{
msg.PushNewline();
msg.AddMarkup(Loc.GetString("armor-coefficient-value",
("type", coefficientArmor.Key),
("value", MathF.Round((1f - coefficientArmor.Value) * 100,1))
));
}
foreach (var flatArmor in armorModifiers.FlatReduction)
{
msg.PushNewline();
msg.AddMarkup(Loc.GetString("armor-reduction-value",
("type", flatArmor.Key),
("value", flatArmor.Value)
));
}
return msg;
var damageType = _protoManager.Index<DamageTypePrototype>(modifier.Key);
args.Price += damageType.ArmorPriceFlat * modifier.Value;
}
}
}
[ByRefEvent]
public record struct ArmorExamineEvent(FormattedMessage Msg);

View File

@@ -6,6 +6,7 @@ using Content.Server.Chat.Managers;
using Content.Server.Explosion.Components;
using Content.Server.NodeContainer.EntitySystems;
using Content.Server.NPC.Pathfinding;
using Content.Shared.Armor;
using Content.Shared.Camera;
using Content.Shared.CCVar;
using Content.Shared.Damage;

View File

@@ -0,0 +1,25 @@
using Content.Shared.Damage;
using Robust.Shared.GameStates;
using Robust.Shared.Utility;
namespace Content.Shared.Armor;
/// <summary>
/// Used for clothing that reduces damage when worn.
/// </summary>
[RegisterComponent, NetworkedComponent, Access(typeof(SharedArmorSystem))]
public sealed partial class ArmorComponent : Component
{
/// <summary>
/// The damage reduction
/// </summary>
[DataField(required: true)]
public DamageModifierSet Modifiers = default!;
}
/// <summary>
/// Event raised on an armor entity to get additional examine text relating to its armor.
/// </summary>
/// <param name="Msg"></param>
[ByRefEvent]
public record struct ArmorExamineEvent(FormattedMessage Msg);

View File

@@ -0,0 +1,78 @@
using Content.Shared.Damage;
using Content.Shared.Examine;
using Content.Shared.Inventory;
using Content.Shared.Silicons.Borgs;
using Content.Shared.Verbs;
using Robust.Shared.Utility;
namespace Content.Shared.Armor;
/// <summary>
/// This handles logic relating to <see cref="ArmorComponent"/>
/// </summary>
public abstract class SharedArmorSystem : EntitySystem
{
[Dependency] private readonly ExamineSystemShared _examine = default!;
/// <inheritdoc/>
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<ArmorComponent, InventoryRelayedEvent<DamageModifyEvent>>(OnDamageModify);
SubscribeLocalEvent<ArmorComponent, BorgModuleRelayedEvent<DamageModifyEvent>>(OnBorgDamageModify);
SubscribeLocalEvent<ArmorComponent, GetVerbsEvent<ExamineVerb>>(OnArmorVerbExamine);
}
private void OnDamageModify(EntityUid uid, ArmorComponent component, InventoryRelayedEvent<DamageModifyEvent> args)
{
args.Args.Damage = DamageSpecifier.ApplyModifierSet(args.Args.Damage, component.Modifiers);
}
private void OnBorgDamageModify(EntityUid uid, ArmorComponent component, ref BorgModuleRelayedEvent<DamageModifyEvent> args)
{
args.Args.Damage = DamageSpecifier.ApplyModifierSet(args.Args.Damage, component.Modifiers);
}
private void OnArmorVerbExamine(EntityUid uid, ArmorComponent component, GetVerbsEvent<ExamineVerb> args)
{
if (!args.CanInteract || !args.CanAccess)
return;
var examineMarkup = GetArmorExamine(component.Modifiers);
var ev = new ArmorExamineEvent(examineMarkup);
RaiseLocalEvent(uid, ref ev);
_examine.AddDetailedExamineVerb(args, component, examineMarkup,
Loc.GetString("armor-examinable-verb-text"), "/Textures/Interface/VerbIcons/dot.svg.192dpi.png",
Loc.GetString("armor-examinable-verb-message"));
}
private FormattedMessage GetArmorExamine(DamageModifierSet armorModifiers)
{
var msg = new FormattedMessage();
msg.AddMarkup(Loc.GetString("armor-examine"));
foreach (var coefficientArmor in armorModifiers.Coefficients)
{
msg.PushNewline();
msg.AddMarkup(Loc.GetString("armor-coefficient-value",
("type", coefficientArmor.Key),
("value", MathF.Round((1f - coefficientArmor.Value) * 100,1))
));
}
foreach (var flatArmor in armorModifiers.FlatReduction)
{
msg.PushNewline();
msg.AddMarkup(Loc.GetString("armor-reduction-value",
("type", flatArmor.Key),
("value", flatArmor.Value)
));
}
return msg;
}
}

View File

@@ -198,7 +198,7 @@
baseSprintSpeed: 6
- type: Armor
modifiers:
coeffecients:
coefficients:
Blunt: 0.8
Slash: 0.6
Piercing: 0.85
@@ -289,7 +289,7 @@
maxBuckleDistance: 1
- type: Armor
modifiers:
coeffecients:
coefficients:
Blunt: 0.8
Slash: 0.6
Piercing: 0.85