Remove IRadiationAct (#7757)

* Move radiation collector to ECS

* Damagable system

* Remove IRadiationAct

* Add small helper field

* Update Content.Server/Radiation/Systems/RadiationSystem.cs

Co-authored-by: Leon Friedrich <60421075+ElectroJr@users.noreply.github.com>

* Delete comment

* Fixed total rads

Co-authored-by: Leon Friedrich <60421075+ElectroJr@users.noreply.github.com>
This commit is contained in:
Alex Evgrashin
2022-04-28 15:36:25 +03:00
committed by GitHub
parent 8be90ea53b
commit 4aa45dc695
9 changed files with 145 additions and 98 deletions

View File

@@ -1,5 +1,6 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using Content.Server.Radiation.Systems;
using Content.Shared.Radiation; using Content.Shared.Radiation;
using Content.Shared.Sound; using Content.Shared.Sound;
using JetBrains.Annotations; using JetBrains.Annotations;
@@ -12,8 +13,7 @@ namespace Content.Server.Radiation
[UsedImplicitly] [UsedImplicitly]
public sealed class RadiationPulseSystem : EntitySystem public sealed class RadiationPulseSystem : EntitySystem
{ {
[Dependency] private readonly IEntityManager _entMan = default!; [Dependency] private readonly RadiationSystem _radiation = default!;
[Dependency] private readonly EntityLookupSystem _lookup = default!;
private const float RadiationCooldown = 1.0f; private const float RadiationCooldown = 1.0f;
private float _accumulator; private float _accumulator;
@@ -36,18 +36,8 @@ namespace Content.Server.Radiation
if (Deleted(ent)) continue; if (Deleted(ent)) continue;
foreach (var entity in _lookup.GetEntitiesInRange(_entMan.GetComponent<TransformComponent>(ent).Coordinates, comp.Range)) var cords = Transform(ent).MapPosition;
{ _radiation.IrradiateRange(cords, comp.Range, comp.RadsPerSecond, RadiationCooldown);
// For now at least still need this because it uses a list internally then returns and this may be deleted before we get to it.
if ((!_entMan.EntityExists(entity) ? EntityLifeStage.Deleted : _entMan.GetComponent<MetaDataComponent>(entity).EntityLifeStage) >= EntityLifeStage.Deleted) continue;
// Note: Radiation is liable for a refactor (stinky Sloth coding a basic version when he did StationEvents)
// so this ToArray doesn't really matter.
foreach (var radiation in _entMan.GetComponents<IRadiationAct>(entity).ToArray())
{
radiation.RadiationAct(RadiationCooldown, comp);
}
}
} }
} }
} }

View File

@@ -0,0 +1,27 @@
using Content.Shared.Radiation.Events;
using Robust.Shared.Map;
namespace Content.Server.Radiation.Systems;
public sealed class RadiationSystem : EntitySystem
{
[Dependency] private readonly EntityLookupSystem _lookup = default!;
public void IrradiateRange(MapCoordinates coordinates, float range, float radsPerSecond, float time)
{
var lookUp = _lookup.GetEntitiesInRange(coordinates, range);
foreach (var uid in lookUp)
{
if (Deleted(uid))
continue;
IrradiateEntity(uid, radsPerSecond, time);
}
}
public void IrradiateEntity(EntityUid uid, float radsPerSecond, float time)
{
var msg = new OnIrradiatedEvent(time, radsPerSecond);
RaiseLocalEvent(uid, msg);
}
}

View File

@@ -1,52 +1,37 @@
using Content.Server.Power.Components; using Content.Server.Singularity.EntitySystems;
using Content.Shared.Radiation;
using Content.Shared.Singularity.Components;
namespace Content.Server.Singularity.Components namespace Content.Server.Singularity.Components
{ {
/// <summary>
/// Generates electricity from radiation.
/// </summary>
[RegisterComponent] [RegisterComponent]
public sealed class RadiationCollectorComponent : Component, IRadiationAct [Friend(typeof(RadiationCollectorSystem))]
public sealed class RadiationCollectorComponent : Component
{ {
[Dependency] private readonly IEntityManager _entMan = default!; /// <summary>
/// How much joules will collector generate for each rad.
public bool Enabled; /// </summary>
public TimeSpan CoolDownEnd; [DataField("chargeModifier")]
[ViewVariables(VVAccess.ReadWrite)]
public bool Collecting {
get => Enabled;
set
{
if (Enabled == value) return;
Enabled = value;
SetAppearance(Enabled ? RadiationCollectorVisualState.Activating : RadiationCollectorVisualState.Deactivating);
}
}
[ViewVariables(VVAccess.ReadWrite)] [ViewVariables(VVAccess.ReadWrite)]
public float ChargeModifier = 30000f; public float ChargeModifier = 30000f;
void IRadiationAct.RadiationAct(float frameTime, SharedRadiationPulseComponent radiation) /// <summary>
{ /// Cooldown time between users interaction.
if (!Enabled) return; /// </summary>
[DataField("cooldown")]
[ViewVariables(VVAccess.ReadWrite)]
public TimeSpan Cooldown = TimeSpan.FromSeconds(0.81f);
// No idea if this is even vaguely accurate to the previous logic. /// <summary>
// The maths is copied from that logic even though it works differently. /// Was machine activated by user?
// But the previous logic would also make the radiation collectors never ever stop providing energy. /// </summary>
// And since frameTime was used there, I'm assuming that this is what the intent was. [ViewVariables(VVAccess.ReadOnly)]
// This still won't stop things being potentially hilarously unbalanced though. public bool Enabled;
if (_entMan.TryGetComponent<BatteryComponent>(Owner, out var batteryComponent))
{
batteryComponent.CurrentCharge += frameTime * radiation.RadsPerSecond * ChargeModifier;
}
}
public void SetAppearance(RadiationCollectorVisualState state) /// <summary>
{ /// Timestamp when machine can be deactivated again.
if (IoCManager.Resolve<IEntityManager>().TryGetComponent<AppearanceComponent?>(Owner, out var appearance)) /// </summary>
{ public TimeSpan CoolDownEnd;
appearance.SetData(RadiationCollectorVisuals.VisualState, state);
}
}
} }
} }

View File

@@ -2,6 +2,8 @@ using Content.Server.Singularity.Components;
using Content.Shared.Interaction; using Content.Shared.Interaction;
using Content.Shared.Singularity.Components; using Content.Shared.Singularity.Components;
using Content.Server.Popups; using Content.Server.Popups;
using Content.Server.Power.Components;
using Content.Shared.Radiation.Events;
using Robust.Shared.Timing; using Robust.Shared.Timing;
using Robust.Shared.Player; using Robust.Shared.Player;
@@ -15,6 +17,7 @@ namespace Content.Server.Singularity.EntitySystems
{ {
base.Initialize(); base.Initialize();
SubscribeLocalEvent<RadiationCollectorComponent, InteractHandEvent>(OnInteractHand); SubscribeLocalEvent<RadiationCollectorComponent, InteractHandEvent>(OnInteractHand);
SubscribeLocalEvent<RadiationCollectorComponent, OnIrradiatedEvent>(OnRadiation);
} }
private void OnInteractHand(EntityUid uid, RadiationCollectorComponent component, InteractHandEvent args) private void OnInteractHand(EntityUid uid, RadiationCollectorComponent component, InteractHandEvent args)
@@ -24,18 +27,58 @@ namespace Content.Server.Singularity.EntitySystems
if(curTime < component.CoolDownEnd) if(curTime < component.CoolDownEnd)
return; return;
if (!component.Enabled) ToggleCollector(uid, args.User, component);
component.CoolDownEnd = curTime + component.Cooldown;
}
private void OnRadiation(EntityUid uid, RadiationCollectorComponent component, OnIrradiatedEvent args)
{
if (!component.Enabled) return;
// No idea if this is even vaguely accurate to the previous logic.
// The maths is copied from that logic even though it works differently.
// But the previous logic would also make the radiation collectors never ever stop providing energy.
// And since frameTime was used there, I'm assuming that this is what the intent was.
// This still won't stop things being potentially hilariously unbalanced though.
if (TryComp<BatteryComponent>(uid, out var batteryComponent))
{ {
_popupSystem.PopupEntity(Loc.GetString("radiation-collector-component-use-on"), uid, Filter.Pvs(args.User)); var charge = args.TotalRads * component.ChargeModifier;
component.Collecting = true; batteryComponent.CurrentCharge += charge;
} }
else }
public void ToggleCollector(EntityUid uid, EntityUid? user = null, RadiationCollectorComponent? component = null)
{
if (!Resolve(uid, ref component))
return;
SetCollectorEnabled(uid, !component.Enabled, user, component);
}
public void SetCollectorEnabled(EntityUid uid, bool enabled, EntityUid? user = null, RadiationCollectorComponent? component = null)
{
if (!Resolve(uid, ref component))
return;
component.Enabled = enabled;
// Show message to the player
if (user != null)
{ {
_popupSystem.PopupEntity(Loc.GetString("radiation-collector-component-use-off"), uid, Filter.Pvs(args.User)); var msg = component.Enabled ? "radiation-collector-component-use-on" : "radiation-collector-component-use-off";
component.Collecting = false; _popupSystem.PopupEntity(Loc.GetString(msg), uid, Filter.Pvs(user.Value));
} }
component.CoolDownEnd = curTime + TimeSpan.FromSeconds(0.81f); // Update appearance
UpdateAppearance(uid, component);
}
private void UpdateAppearance(EntityUid uid, RadiationCollectorComponent? component, AppearanceComponent? appearance = null)
{
if (!Resolve(uid, ref component, ref appearance))
return;
var state = component.Enabled ? RadiationCollectorVisualState.Active : RadiationCollectorVisualState.Deactive;
appearance.SetData(RadiationCollectorVisuals.VisualState, state);
} }
} }
} }

View File

@@ -32,7 +32,7 @@ namespace Content.Server.Singularity
} }
foreach (var comp in entityManager.EntityQuery<RadiationCollectorComponent>()) foreach (var comp in entityManager.EntityQuery<RadiationCollectorComponent>())
{ {
comp.Collecting = true; EntitySystem.Get<RadiationCollectorSystem>().SetCollectorEnabled(comp.Owner, true, null, comp);
} }
foreach (var comp in entityManager.EntityQuery<ParticleAcceleratorControlBoxComponent>()) foreach (var comp in entityManager.EntityQuery<ParticleAcceleratorControlBoxComponent>())
{ {

View File

@@ -1,17 +1,9 @@
using System;
using System.Collections.Generic;
using Content.Shared.Acts;
using Content.Shared.Damage.Prototypes; using Content.Shared.Damage.Prototypes;
using Content.Shared.FixedPoint; using Content.Shared.FixedPoint;
using Content.Shared.Radiation;
using Robust.Shared.Analyzers;
using Robust.Shared.GameObjects;
using Robust.Shared.GameStates; using Robust.Shared.GameStates;
using Robust.Shared.Serialization; using Robust.Shared.Serialization;
using Robust.Shared.Serialization.Manager.Attributes;
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype; using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype;
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype.List; using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype.List;
using Robust.Shared.ViewVariables;
namespace Content.Shared.Damage namespace Content.Shared.Damage
{ {
@@ -25,7 +17,7 @@ namespace Content.Shared.Damage
[RegisterComponent] [RegisterComponent]
[NetworkedComponent()] [NetworkedComponent()]
[Friend(typeof(DamageableSystem))] [Friend(typeof(DamageableSystem))]
public sealed class DamageableComponent : Component, IRadiationAct public sealed class DamageableComponent : Component
{ {
/// <summary> /// <summary>
/// This <see cref="DamageContainerPrototype"/> specifies what damage types are supported by this component. /// This <see cref="DamageContainerPrototype"/> specifies what damage types are supported by this component.
@@ -77,21 +69,6 @@ namespace Content.Shared.Damage
[ViewVariables] [ViewVariables]
[DataField("explosionDamageTypes", customTypeSerializer: typeof(PrototypeIdListSerializer<DamageTypePrototype>))] [DataField("explosionDamageTypes", customTypeSerializer: typeof(PrototypeIdListSerializer<DamageTypePrototype>))]
public List<string> ExplosionDamageTypeIDs = new() { "Piercing", "Heat" }; public List<string> ExplosionDamageTypeIDs = new() { "Piercing", "Heat" };
// TODO RADIATION Remove this.
void IRadiationAct.RadiationAct(float frameTime, SharedRadiationPulseComponent radiation)
{
var damageValue = FixedPoint2.New(MathF.Max((frameTime * radiation.RadsPerSecond), 1));
// Radiation should really just be a damage group instead of a list of types.
DamageSpecifier damage = new();
foreach (var typeID in RadiationDamageTypeIDs)
{
damage.DamageDict.Add(typeID, damageValue);
}
EntitySystem.Get<DamageableSystem>().TryChangeDamage(Owner, damage);
}
} }
[Serializable, NetSerializable] [Serializable, NetSerializable]

View File

@@ -2,6 +2,7 @@ using System.Linq;
using Content.Shared.Damage.Prototypes; using Content.Shared.Damage.Prototypes;
using Content.Shared.FixedPoint; using Content.Shared.FixedPoint;
using Content.Shared.Inventory; using Content.Shared.Inventory;
using Content.Shared.Radiation.Events;
using Robust.Shared.GameObjects; using Robust.Shared.GameObjects;
using Robust.Shared.GameStates; using Robust.Shared.GameStates;
using Robust.Shared.IoC; using Robust.Shared.IoC;
@@ -19,6 +20,7 @@ namespace Content.Shared.Damage
SubscribeLocalEvent<DamageableComponent, ComponentInit>(DamageableInit); SubscribeLocalEvent<DamageableComponent, ComponentInit>(DamageableInit);
SubscribeLocalEvent<DamageableComponent, ComponentHandleState>(DamageableHandleState); SubscribeLocalEvent<DamageableComponent, ComponentHandleState>(DamageableHandleState);
SubscribeLocalEvent<DamageableComponent, ComponentGetState>(DamageableGetState); SubscribeLocalEvent<DamageableComponent, ComponentGetState>(DamageableGetState);
SubscribeLocalEvent<DamageableComponent, OnIrradiatedEvent>(OnIrradiated);
} }
/// <summary> /// <summary>
@@ -195,12 +197,26 @@ namespace Content.Shared.Damage
Dirty(comp); Dirty(comp);
} }
private void DamageableGetState(EntityUid uid, DamageableComponent component, ref ComponentGetState args) private void DamageableGetState(EntityUid uid, DamageableComponent component, ref ComponentGetState args)
{ {
args.State = new DamageableComponentState(component.Damage.DamageDict, component.DamageModifierSetId); args.State = new DamageableComponentState(component.Damage.DamageDict, component.DamageModifierSetId);
} }
private void OnIrradiated(EntityUid uid, DamageableComponent component, OnIrradiatedEvent args)
{
var damageValue = FixedPoint2.New(args.TotalRads);
// Radiation should really just be a damage group instead of a list of types.
DamageSpecifier damage = new();
foreach (var typeId in component.RadiationDamageTypeIDs)
{
damage.DamageDict.Add(typeId, damageValue);
}
TryChangeDamage(uid, damage);
}
private void DamageableHandleState(EntityUid uid, DamageableComponent component, ref ComponentHandleState args) private void DamageableHandleState(EntityUid uid, DamageableComponent component, ref ComponentHandleState args)
{ {
if (args.Current is not DamageableComponentState state) if (args.Current is not DamageableComponentState state)

View File

@@ -0,0 +1,20 @@
namespace Content.Shared.Radiation.Events;
/// <summary>
/// Raised on entity when it was irradiated
/// by some radiation source.
/// </summary>
public sealed class OnIrradiatedEvent : EntityEventArgs
{
public readonly float FrameTime;
public readonly float RadsPerSecond;
public float TotalRads => RadsPerSecond * FrameTime;
public OnIrradiatedEvent(float frameTime, float radsPerSecond)
{
FrameTime = frameTime;
RadsPerSecond = radsPerSecond;
}
}

View File

@@ -1,11 +0,0 @@
using Robust.Shared.Analyzers;
using Robust.Shared.GameObjects;
namespace Content.Shared.Radiation
{
[RequiresExplicitImplementation]
public interface IRadiationAct : IComponent
{
void RadiationAct(float frameTime, SharedRadiationPulseComponent radiation);
}
}