diff --git a/Content.Client/_CP14/MagicEnergy/CP14MagicEnergyCrystalSlotSystem.cs b/Content.Client/_CP14/MagicEnergy/CP14MagicEnergyCrystalSlotSystem.cs new file mode 100644 index 0000000000..e885d79747 --- /dev/null +++ b/Content.Client/_CP14/MagicEnergy/CP14MagicEnergyCrystalSlotSystem.cs @@ -0,0 +1,5 @@ +using Content.Shared._CP14.MagicEnergy; + +namespace Content.Client._CP14.MagicEnergy; + +public sealed class CP14MagicEnergyCrystalSlotSystem : SharedCP14MagicEnergyCrystalSlotSystem; diff --git a/Content.Client/_CP14/MagicEnergy/CP14MagicEnergySystem.cs b/Content.Client/_CP14/MagicEnergy/CP14MagicEnergySystem.cs new file mode 100644 index 0000000000..10848f4b68 --- /dev/null +++ b/Content.Client/_CP14/MagicEnergy/CP14MagicEnergySystem.cs @@ -0,0 +1,5 @@ +using Content.Shared._CP14.MagicEnergy; + +namespace Content.Client._CP14.MagicEnergy; + +public sealed class CP14MagicEnergySystem : SharedCP14MagicEnergySystem; diff --git a/Content.Server/_CP14/Chemistry/ReagentEffect/CP14ManaChange.cs b/Content.Server/_CP14/Chemistry/ReagentEffect/CP14ManaChange.cs index a35eff0575..9fc5ecd652 100644 --- a/Content.Server/_CP14/Chemistry/ReagentEffect/CP14ManaChange.cs +++ b/Content.Server/_CP14/Chemistry/ReagentEffect/CP14ManaChange.cs @@ -15,12 +15,12 @@ public sealed partial class CP14ManaChange : EntityEffect public float ManaDelta = 1; [DataField] - public bool Safe = false; + public bool Safe; [DataField] public bool ScaleByQuantity; - protected override string? ReagentEffectGuidebookText(IPrototypeManager prototype, IEntitySystemManager entSys) + protected override string ReagentEffectGuidebookText(IPrototypeManager prototype, IEntitySystemManager entSys) { return Loc.GetString(ManaDelta >= 0 ? "cp14-reagent-effect-guidebook-mana-add" : "cp14-reagent-effect-guidebook-mana-remove", ("chance", Probability), @@ -29,23 +29,21 @@ public sealed partial class CP14ManaChange : EntityEffect public override void Effect(EntityEffectBaseArgs args) { + var entityManager = args.EntityManager; var scale = FixedPoint2.New(1); if (args is EntityEffectReagentArgs reagentArgs) - { scale = ScaleByQuantity ? reagentArgs.Quantity * reagentArgs.Scale : reagentArgs.Scale; - } - var magicSystem = args.EntityManager.System(); + var magicSystem = entityManager.System(); magicSystem.ChangeEnergy(args.TargetEntity, ManaDelta * scale, out var changed, out var overload, safe: Safe); scale -= FixedPoint2.Abs(changed + overload); - if (args.EntityManager.TryGetComponent(args.TargetEntity, - out var crystalSlot)) - { - var slotSystem = args.EntityManager.System(); - slotSystem.TryChangeEnergy(args.TargetEntity, ManaDelta * scale, crystalSlot); - } + if (!args.EntityManager.TryGetComponent(args.TargetEntity, out var crystalSlot)) + return; + + var slotSystem = entityManager.System(); + slotSystem.TryChangeEnergy((args.TargetEntity, crystalSlot), ManaDelta * scale); } } diff --git a/Content.Server/_CP14/MagicEnergy/CP14MagicEnergyCrystalSlotSystem.cs b/Content.Server/_CP14/MagicEnergy/CP14MagicEnergyCrystalSlotSystem.cs index 6e80d37aa8..c911a04d90 100644 --- a/Content.Server/_CP14/MagicEnergy/CP14MagicEnergyCrystalSlotSystem.cs +++ b/Content.Server/_CP14/MagicEnergy/CP14MagicEnergyCrystalSlotSystem.cs @@ -9,7 +9,7 @@ using Robust.Shared.Containers; namespace Content.Server._CP14.MagicEnergy; -public sealed partial class CP14MagicEnergyCrystalSlotSystem : SharedCP14MagicEnergyCrystalSlotSystem +public sealed class CP14MagicEnergyCrystalSlotSystem : SharedCP14MagicEnergyCrystalSlotSystem { [Dependency] private readonly ItemSlotsSystem _itemSlots = default!; [Dependency] private readonly CP14MagicEnergySystem _magicEnergy = default!; @@ -19,125 +19,120 @@ public sealed partial class CP14MagicEnergyCrystalSlotSystem : SharedCP14MagicEn public override void Initialize() { + base.Initialize(); + SubscribeLocalEvent(OnEnergyChanged); + SubscribeLocalEvent(OnExamined); SubscribeLocalEvent(OnCrystalChanged); } private void OnCrystalChanged(Entity ent, ref CP14SlotCrystalChangedEvent args) { - var realPowered = TryGetEnergyCrystalFromSlot(ent, out var energyEnt, out var energyComp); + var realPowered = TryGetEnergyCrystalFromSlot((ent, ent), out var energyComp); + if (energyComp is not null) + realPowered = energyComp.Value.Comp.Energy > 0; - if (energyComp != null) - realPowered = energyComp.Energy > 0; + if (ent.Comp.Powered == realPowered) + return; - if (ent.Comp.Powered != realPowered) - { - ent.Comp.Powered = realPowered; - _appearance.SetData(ent, CP14MagicSlotVisuals.Powered, realPowered); - RaiseLocalEvent(ent, new CP14SlotCrystalPowerChangedEvent(realPowered)); - } + ent.Comp.Powered = realPowered; + _appearance.SetData(ent, CP14MagicSlotVisuals.Powered, realPowered); + + RaiseLocalEvent(ent, new CP14SlotCrystalPowerChangedEvent(realPowered)); } private void OnEnergyChanged(Entity crystal, ref CP14MagicEnergyLevelChangeEvent args) { - if (_container.TryGetContainingContainer(crystal, out var container) - && TryComp(container.Owner, out CP14MagicEnergyCrystalSlotComponent? slot) - && _itemSlots.TryGetSlot(container.Owner, slot.SlotId, out var itemSlot)) - { - if (itemSlot.Item == crystal) - { - RaiseLocalEvent(container.Owner, new CP14SlotCrystalChangedEvent(false)); - } - } + if (!_container.TryGetContainingContainer((crystal.Owner, null, null), out var container)) + return; + + if (!TryComp(container.Owner, out CP14MagicEnergyCrystalSlotComponent? slot)) + return; + + if (!_itemSlots.TryGetSlot(container.Owner, slot.SlotId, out var itemSlot)) + return; + + if (itemSlot.Item != crystal) + return; + + RaiseLocalEvent(container.Owner, new CP14SlotCrystalChangedEvent(false)); } private void OnExamined(Entity ent, ref ExaminedEvent args) { - if (!TryGetEnergyCrystalFromSlot(ent, out var crystalUid, out var crystalComp, ent.Comp)) + if (!TryGetEnergyCrystalFromSlot((ent, ent), out var energyEnt)) return; if (!args.IsInDetailsRange) return; - //var scanEvent = new CP14MagicEnergyScanEvent(); - //RaiseLocalEvent(args.Examiner, scanEvent); -// - //if (!scanEvent.CanScan) + // TODO: scan energy ability + // var scanEvent = new CP14MagicEnergyScanEvent(); + // RaiseLocalEvent(args.Examiner, scanEvent); + // + // if (!scanEvent.CanScan) // return; - args.PushMarkup(_magicEnergy.GetEnergyExaminedText(crystalUid.Value, crystalComp)); + args.PushMarkup(_magicEnergy.GetEnergyExaminedText((energyEnt.Value, energyEnt))); } - public bool TryGetEnergyCrystalFromSlot(EntityUid uid, - [NotNullWhen(true)] out CP14MagicEnergyContainerComponent? energyComp, - CP14MagicEnergyCrystalSlotComponent? component = null) + public bool TryGetEnergyCrystalFromSlot(Entity ent, + [NotNullWhen(true)] out Entity? energyEnt) { - return TryGetEnergyCrystalFromSlot(uid, out _, out energyComp, component); + energyEnt = null; + + if (!Resolve(ent, ref ent.Comp, false)) + return false; + + if (!_itemSlots.TryGetSlot(ent, ent.Comp.SlotId, out var slot)) + return false; + + if (slot.Item is null) + return false; + + if (!TryComp(slot.Item, out var energyComp)) + return false; + + energyEnt = (slot.Item.Value, energyComp); + return true; } - public bool TryGetEnergyCrystalFromSlot(EntityUid uid, - [NotNullWhen(true)] out EntityUid? energyEnt, - [NotNullWhen(true)] out CP14MagicEnergyContainerComponent? energyComp, - CP14MagicEnergyCrystalSlotComponent? component = null) + public bool HasEnergy(Entity ent, + FixedPoint2 energy, + EntityUid? user = null) { - if (!Resolve(uid, ref component, false)) + if (!TryGetEnergyCrystalFromSlot(ent, out var energyEnt)) { - energyEnt = null; - energyComp = null; + if (user is not null) + _popup.PopupEntity(Loc.GetString("cp14-magic-energy-no-crystal"), ent,user.Value); + return false; } - if (_itemSlots.TryGetSlot(uid, component.SlotId, out ItemSlot? slot)) - { - energyEnt = slot.Item; - return TryComp(slot.Item, out energyComp); - } + if (energyEnt.Value.Comp.Energy >= energy) + return true; + + if (user is not null) + _popup.PopupEntity(Loc.GetString("cp14-magic-energy-insufficient"), ent, user.Value); - energyEnt = null; - energyComp = null; return false; } - public bool HasEnergy(EntityUid uid, + public bool TryChangeEnergy(Entity ent, FixedPoint2 energy, - CP14MagicEnergyCrystalSlotComponent? component = null, - EntityUid? user = null) - { - if (!TryGetEnergyCrystalFromSlot(uid, out var energyEnt, out var energyComp, component)) - { - if (user != null) - _popup.PopupEntity(Loc.GetString("cp14-magic-energy-no-crystal"), uid,user.Value); - - return false; - } - - if (energyComp.Energy < energy) - { - if (user != null) - _popup.PopupEntity(Loc.GetString("cp14-magic-energy-insufficient"), uid, user.Value); - - return false; - } - - return true; - } - - public bool TryChangeEnergy(EntityUid uid, - FixedPoint2 energy, - CP14MagicEnergyCrystalSlotComponent? component = null, EntityUid? user = null, bool safe = false) { - if (!TryGetEnergyCrystalFromSlot(uid, out var energyEnt, out var energyComp, component)) + if (!TryGetEnergyCrystalFromSlot(ent, out var energyEnt)) { - if (user != null) - _popup.PopupEntity(Loc.GetString("cp14-magic-energy-no-crystal"), uid, user.Value); + if (user is not null) + _popup.PopupEntity(Loc.GetString("cp14-magic-energy-no-crystal"), ent, user.Value); return false; } - _magicEnergy.ChangeEnergy(energyEnt.Value, energy, out _, out _, energyComp, safe); + _magicEnergy.ChangeEnergy((energyEnt.Value, energyEnt.Value), energy, out _, out _, safe); return true; } } diff --git a/Content.Server/_CP14/MagicEnergy/CP14MagicEnergySystem.Draw.cs b/Content.Server/_CP14/MagicEnergy/CP14MagicEnergySystem.Draw.cs index 6d69512801..3fd5710bf5 100644 --- a/Content.Server/_CP14/MagicEnergy/CP14MagicEnergySystem.Draw.cs +++ b/Content.Server/_CP14/MagicEnergy/CP14MagicEnergySystem.Draw.cs @@ -30,7 +30,7 @@ public partial class CP14MagicEnergySystem if (!ent.Comp.Damage.TryGetValue(dict.Key, out var modifier)) continue; - ChangeEnergy(ent, modifier * dict.Value, out _, out _, safe: true); + ChangeEnergy(ent.Owner, modifier * dict.Value, out _, out _, safe: true); } } @@ -61,7 +61,7 @@ public partial class CP14MagicEnergySystem draw.NextUpdateTime = _gameTiming.CurTime + TimeSpan.FromSeconds(draw.Delay); - ChangeEnergy(uid, draw.Energy, out _, out _, magicContainer, draw.Safe); + ChangeEnergy((uid, magicContainer), draw.Energy, out _, out _, draw.Safe); } var query2 = EntityQueryEnumerator(); @@ -86,7 +86,7 @@ public partial class CP14MagicEnergySystem // daylight = true; //} - ChangeEnergy(uid, daylight ? draw.DaylightEnergy : draw.DarknessEnergy, out _, out _, magicContainer, true); + ChangeEnergy((uid, magicContainer), daylight ? draw.DaylightEnergy : draw.DarknessEnergy, out _, out _, true); } } @@ -103,10 +103,10 @@ public partial class CP14MagicEnergySystem draw.NextUpdateTime = _gameTiming.CurTime + TimeSpan.FromSeconds(draw.Delay); - if (!_magicSlot.TryGetEnergyCrystalFromSlot(uid, out var energyEnt, out var energyComp)) + if (!_magicSlot.TryGetEnergyCrystalFromSlot(uid, out var energyEnt)) continue; - ChangeEnergy(energyEnt.Value, draw.Energy, out _, out _, energyComp, draw.Safe); + ChangeEnergy((energyEnt.Value, energyEnt.Value), draw.Energy, out _, out _, draw.Safe); } } } diff --git a/Content.Server/_CP14/MagicEnergy/CP14MagicEnergySystem.PortRelay.cs b/Content.Server/_CP14/MagicEnergy/CP14MagicEnergySystem.PortRelay.cs index 245b544623..48785e8ae4 100644 --- a/Content.Server/_CP14/MagicEnergy/CP14MagicEnergySystem.PortRelay.cs +++ b/Content.Server/_CP14/MagicEnergy/CP14MagicEnergySystem.PortRelay.cs @@ -41,9 +41,7 @@ public partial class CP14MagicEnergySystem } if (passed) - { - TransferEnergy(uid, sinkUid, relay.Energy, out _, out _, container, safe: relay.Safe); - } + TransferEnergy((uid, container), sinkUid, relay.Energy, out _, out _, safe: relay.Safe); } relay.NextUpdateTime = _gameTiming.CurTime + TimeSpan.FromSeconds(relay.Delay); diff --git a/Content.Server/_CP14/MagicEnergy/CP14MagicEnergySystem.cs b/Content.Server/_CP14/MagicEnergy/CP14MagicEnergySystem.cs index 2de80a3a67..541ef6a77f 100644 --- a/Content.Server/_CP14/MagicEnergy/CP14MagicEnergySystem.cs +++ b/Content.Server/_CP14/MagicEnergy/CP14MagicEnergySystem.cs @@ -10,6 +10,8 @@ public sealed partial class CP14MagicEnergySystem : SharedCP14MagicEnergySystem public override void Initialize() { + base.Initialize(); + InitializeDraw(); InitializePortRelay(); } diff --git a/Content.Server/_CP14/MagicSpell/CP14MagicSystem.cs b/Content.Server/_CP14/MagicSpell/CP14MagicSystem.cs index 29cde0a6a2..a9b951cb99 100644 --- a/Content.Server/_CP14/MagicSpell/CP14MagicSystem.cs +++ b/Content.Server/_CP14/MagicSpell/CP14MagicSystem.cs @@ -18,7 +18,7 @@ using Robust.Shared.Random; namespace Content.Server._CP14.MagicSpell; -public sealed partial class CP14MagicSystem : CP14SharedMagicSystem +public sealed class CP14MagicSystem : CP14SharedMagicSystem { [Dependency] private readonly ChatSystem _chat = default!; [Dependency] private readonly TransformSystem _transform = default!; @@ -26,7 +26,6 @@ public sealed partial class CP14MagicSystem : CP14SharedMagicSystem [Dependency] private readonly EntityLookupSystem _lookup = default!; [Dependency] private readonly EntityWhitelistSystem _whitelist = default!; [Dependency] private readonly IRobustRandom _random = default!; - [Dependency] private readonly SharedActionsSystem _action = default!; public override void Initialize() { @@ -132,16 +131,14 @@ public sealed partial class CP14MagicSystem : CP14SharedMagicSystem var spellEv = new CP14SpellFromSpellStorageUsedEvent(args.Performer, (ent, magicEffect), requiredMana); RaiseLocalEvent(magicEffect.SpellStorage.Value, ref spellEv); - _magicEnergy.ChangeEnergy(magicEffect.SpellStorage.Value, -requiredMana, out var changedEnergy, out var overloadedEnergy, magicStorage, safe: false); + _magicEnergy.ChangeEnergy((magicEffect.SpellStorage.Value, magicStorage), -requiredMana, out var changedEnergy, out var overloadedEnergy, safe: false); requiredMana -= FixedPoint2.Abs(changedEnergy + overloadedEnergy); } //Second - action user if (requiredMana > 0 && TryComp(args.Performer, out var playerMana)) - { - _magicEnergy.ChangeEnergy(args.Performer.Value, -requiredMana, out _, out _, playerMana, safe: false); - } + _magicEnergy.ChangeEnergy((args.Performer.Value, playerMana), -requiredMana, out _, out _, safe: false); } private void OnMusicCheck(Entity ent, ref CP14CastMagicEffectAttemptEvent args) @@ -160,10 +157,10 @@ public sealed partial class CP14MagicSystem : CP14SharedMagicSystem break; } - if (!passed) - { - args.PushReason(Loc.GetString("cp14-magic-music-aspect")); - args.Cancel(); - } + if (passed) + return; + + args.PushReason(Loc.GetString("cp14-magic-music-aspect")); + args.Cancel(); } } diff --git a/Content.Shared/_CP14/MagicEnergy/Components/CP14MagicEnergyContainerComponent.cs b/Content.Shared/_CP14/MagicEnergy/Components/CP14MagicEnergyContainerComponent.cs index cd30c86a4d..37ab1dab3a 100644 --- a/Content.Shared/_CP14/MagicEnergy/Components/CP14MagicEnergyContainerComponent.cs +++ b/Content.Shared/_CP14/MagicEnergy/Components/CP14MagicEnergyContainerComponent.cs @@ -13,17 +13,17 @@ namespace Content.Shared._CP14.MagicEnergy.Components; public sealed partial class CP14MagicEnergyContainerComponent : Component { [DataField, AutoNetworkedField] - public FixedPoint2 Energy = 0f; + public FixedPoint2 Energy = 0; [DataField, AutoNetworkedField] - public FixedPoint2 MaxEnergy = 100f; + public FixedPoint2 MaxEnergy = 100; [DataField, AutoNetworkedField] - public ProtoId? MagicAlert = null; + public ProtoId? MagicAlert; /// /// Does this container support unsafe energy manipulation? /// [DataField, AutoNetworkedField] - public bool UnsafeSupport = false; + public bool UnsafeSupport; } diff --git a/Content.Shared/_CP14/MagicEnergy/Components/CP14MagicEnergyCrystalSlotComponent.cs b/Content.Shared/_CP14/MagicEnergy/Components/CP14MagicEnergyCrystalSlotComponent.cs index 728a53b011..558229e091 100644 --- a/Content.Shared/_CP14/MagicEnergy/Components/CP14MagicEnergyCrystalSlotComponent.cs +++ b/Content.Shared/_CP14/MagicEnergy/Components/CP14MagicEnergyCrystalSlotComponent.cs @@ -1,11 +1,13 @@ +using Robust.Shared.GameStates; using Robust.Shared.Serialization; namespace Content.Shared._CP14.MagicEnergy.Components; /// -/// Allows you to examine how much energy is in that object +/// Allows you to examine how much energy is in that object. /// -[RegisterComponent, Access(typeof(SharedCP14MagicEnergyCrystalSlotSystem))] +[RegisterComponent, NetworkedComponent] +[Access(typeof(SharedCP14MagicEnergyCrystalSlotSystem))] public sealed partial class CP14MagicEnergyCrystalSlotComponent : Component { [DataField(required: true)] @@ -18,7 +20,7 @@ public sealed partial class CP14MagicEnergyCrystalSlotComponent : Component public enum CP14MagicSlotVisuals : byte { Inserted, - Powered + Powered, } /// @@ -40,6 +42,7 @@ public sealed class CP14SlotCrystalChangedEvent : EntityEventArgs public sealed class CP14SlotCrystalPowerChangedEvent : EntityEventArgs { public readonly bool Powered; + public CP14SlotCrystalPowerChangedEvent(bool powered) { Powered = powered; diff --git a/Content.Shared/_CP14/MagicEnergy/Components/CP14MagicEnergyExaminableComponent.cs b/Content.Shared/_CP14/MagicEnergy/Components/CP14MagicEnergyExaminableComponent.cs index 398e429997..eaa1bcc486 100644 --- a/Content.Shared/_CP14/MagicEnergy/Components/CP14MagicEnergyExaminableComponent.cs +++ b/Content.Shared/_CP14/MagicEnergy/Components/CP14MagicEnergyExaminableComponent.cs @@ -1,9 +1,10 @@ +using Robust.Shared.GameStates; + namespace Content.Shared._CP14.MagicEnergy.Components; /// -/// Allows you to examine how much energy is in that object +/// Allows you to examine how much energy is in that object. /// -[RegisterComponent, Access(typeof(SharedCP14MagicEnergySystem))] -public sealed partial class CP14MagicEnergyExaminableComponent : Component -{ -} +[RegisterComponent, NetworkedComponent] +[Access(typeof(SharedCP14MagicEnergySystem))] +public sealed partial class CP14MagicEnergyExaminableComponent : Component; diff --git a/Content.Shared/_CP14/MagicEnergy/SharedCP14MagicEnergyCrystalSlotSystem.cs b/Content.Shared/_CP14/MagicEnergy/SharedCP14MagicEnergyCrystalSlotSystem.cs index be0974ad07..164fee8ce9 100644 --- a/Content.Shared/_CP14/MagicEnergy/SharedCP14MagicEnergyCrystalSlotSystem.cs +++ b/Content.Shared/_CP14/MagicEnergy/SharedCP14MagicEnergyCrystalSlotSystem.cs @@ -4,11 +4,9 @@ using Robust.Shared.Containers; namespace Content.Shared._CP14.MagicEnergy; -public partial class SharedCP14MagicEnergyCrystalSlotSystem : EntitySystem +public abstract class SharedCP14MagicEnergyCrystalSlotSystem : EntitySystem { - [Dependency] private readonly ItemSlotsSystem _itemSlots = default!; [Dependency] private readonly SharedAppearanceSystem _appearance = default!; - [Dependency] private readonly SharedContainerSystem _containerSystem = default!; public override void Initialize() { diff --git a/Content.Shared/_CP14/MagicEnergy/SharedCP14MagicEnergySystem.cs b/Content.Shared/_CP14/MagicEnergy/SharedCP14MagicEnergySystem.cs index bc02167aad..5c1d43347c 100644 --- a/Content.Shared/_CP14/MagicEnergy/SharedCP14MagicEnergySystem.cs +++ b/Content.Shared/_CP14/MagicEnergy/SharedCP14MagicEnergySystem.cs @@ -8,27 +8,18 @@ using Content.Shared.Rounding; namespace Content.Shared._CP14.MagicEnergy; -public partial class SharedCP14MagicEnergySystem : EntitySystem +public abstract class SharedCP14MagicEnergySystem : EntitySystem { [Dependency] private readonly AlertsSystem _alerts = default!; [Dependency] private readonly SharedAmbientSoundSystem _ambient = default!; public override void Initialize() { - SubscribeLocalEvent(OnSlotPowerChanged); - SubscribeLocalEvent(OnComponentStartup); SubscribeLocalEvent(OnComponentShutdown); SubscribeLocalEvent(OnExamined); - } - - private void OnSlotPowerChanged(Entity ent, ref CP14SlotCrystalPowerChangedEvent args) - { - if (TryComp(ent, out var ambient)) - { - _ambient.SetAmbience(ent, args.Powered); - } + SubscribeLocalEvent(OnSlotPowerChanged); } private void OnComponentStartup(Entity ent, ref ComponentStartup args) @@ -38,135 +29,12 @@ public partial class SharedCP14MagicEnergySystem : EntitySystem private void OnComponentShutdown(Entity ent, ref ComponentShutdown args) { - if (ent.Comp.MagicAlert == null) + if (ent.Comp.MagicAlert is null) return; _alerts.ClearAlert(ent, ent.Comp.MagicAlert.Value); } - public string GetEnergyExaminedText(EntityUid uid, CP14MagicEnergyContainerComponent ent) - { - var power = (int)(ent.Energy / ent.MaxEnergy * 100); - - var color = "#3fc488"; - if (power < 66) - color = "#f2a93a"; - if (power < 33) - color = "#c23030"; - - return Loc.GetString("cp14-magic-energy-scan-result", - ("item", MetaData(uid).EntityName), - ("power", power), - ("color", color)); - } - - public void ChangeEnergy(EntityUid uid, - FixedPoint2 energy, - out FixedPoint2 changedEnergy, - out FixedPoint2 overloadEnergy, - CP14MagicEnergyContainerComponent? component = null, - bool safe = false) - { - changedEnergy = 0; - overloadEnergy = 0; - - if (!Resolve(uid, ref component, false)) - return; - - if (!safe) - { - //Overload - if (component.Energy + energy > component.MaxEnergy && component.UnsafeSupport) - { - overloadEnergy = (component.Energy + energy) - component.MaxEnergy; - RaiseLocalEvent(uid, - new CP14MagicEnergyOverloadEvent() - { - OverloadEnergy = (component.Energy + energy) - component.MaxEnergy, - }); - } - - //Burn out - if (component.Energy + energy < 0 && component.UnsafeSupport) - { - overloadEnergy = component.Energy + energy; - RaiseLocalEvent(uid, - new CP14MagicEnergyBurnOutEvent() - { - BurnOutEnergy = -energy - component.Energy - }); - } - } - - var oldEnergy = component.Energy; - var newEnergy = Math.Clamp((float)component.Energy + (float)energy, 0, (float)component.MaxEnergy); - - changedEnergy = newEnergy - oldEnergy; - component.Energy = newEnergy; - - if (oldEnergy != newEnergy) - { - RaiseLocalEvent(uid, - new CP14MagicEnergyLevelChangeEvent() - { - OldValue = oldEnergy, - NewValue = newEnergy, - MaxValue = component.MaxEnergy, - }); - } - - UpdateMagicAlert((uid, component)); - } - - public void TransferEnergy(EntityUid from, - EntityUid to, - FixedPoint2 energy, - out FixedPoint2 changedEnergy, - out FixedPoint2 overloadEnergy, - CP14MagicEnergyContainerComponent? fromComponent = null, - CP14MagicEnergyContainerComponent? toComponent = null, - bool safe = false) - { - changedEnergy = 0; - overloadEnergy = 0; - - if (!Resolve(from, ref fromComponent) || !Resolve(to, ref toComponent)) - return; - - var transferEnergy = energy; - //We check how much space is left in the container so as not to overload it, but only if it does not support overloading - if (!toComponent.UnsafeSupport || safe) - { - var freeSpace = toComponent.MaxEnergy - toComponent.Energy; - transferEnergy = FixedPoint2.Min(freeSpace, energy); - } - - ChangeEnergy(from, -transferEnergy, out var change, out var overload, fromComponent, safe); - ChangeEnergy(to , -(change + overload), out changedEnergy, out overloadEnergy, toComponent, safe); - } - - public bool HasEnergy(EntityUid uid, FixedPoint2 energy, CP14MagicEnergyContainerComponent? component = null, bool safe = false) - { - if (!Resolve(uid, ref component)) - return false; - - if (safe == false && component.UnsafeSupport) - return true; - - return component.Energy >= energy; - } - - private void UpdateMagicAlert(Entity ent) - { - if (ent.Comp.MagicAlert == null) - return; - - var level = ContentHelpers.RoundToLevels(MathF.Max(0f, (float)ent.Comp.Energy), - (float)ent.Comp.MaxEnergy, - _alerts.GetMaxSeverity(ent.Comp.MagicAlert.Value)); - _alerts.ShowAlert(ent, ent.Comp.MagicAlert.Value, (short)level); - } - private void OnExamined(Entity ent, ref ExaminedEvent args) { if (!TryComp(ent, out var magicContainer)) @@ -175,7 +43,121 @@ public partial class SharedCP14MagicEnergySystem : EntitySystem if (!args.IsInDetailsRange) return; - args.PushMarkup(GetEnergyExaminedText(ent, magicContainer)); + args.PushMarkup(GetEnergyExaminedText((ent, magicContainer))); + } + + private void OnSlotPowerChanged(Entity ent, ref CP14SlotCrystalPowerChangedEvent args) + { + _ambient.SetAmbience(ent, args.Powered); + } + + public void ChangeEnergy(Entity ent, + FixedPoint2 energy, + out FixedPoint2 deltaEnergy, + out FixedPoint2 overloadEnergy, + bool safe = false) + { + deltaEnergy = 0; + overloadEnergy = 0; + + if (!Resolve(ent, ref ent.Comp, false)) + return; + + if (!safe) + { + // Overload + if (ent.Comp.Energy + energy > ent.Comp.MaxEnergy && ent.Comp.UnsafeSupport) + { + overloadEnergy = ent.Comp.Energy + energy - ent.Comp.MaxEnergy; + RaiseLocalEvent(ent, new CP14MagicEnergyOverloadEvent(overloadEnergy)); + } + + // Burn out + if (ent.Comp.Energy + energy < 0 && ent.Comp.UnsafeSupport) + { + overloadEnergy = ent.Comp.Energy + energy; + RaiseLocalEvent(ent, new CP14MagicEnergyBurnOutEvent(-energy - ent.Comp.Energy)); + } + } + + var oldEnergy = ent.Comp.Energy; + var newEnergy = Math.Clamp((float) ent.Comp.Energy + (float) energy, 0, (float) ent.Comp.MaxEnergy); + + deltaEnergy = newEnergy - oldEnergy; + ent.Comp.Energy = newEnergy; + + if (oldEnergy != newEnergy) + RaiseLocalEvent(ent, new CP14MagicEnergyLevelChangeEvent(oldEnergy, newEnergy, ent.Comp.MaxEnergy)); + + UpdateMagicAlert((ent, ent.Comp)); + } + + public void TransferEnergy(Entity sender, + Entity receiver, + FixedPoint2 energy, + out FixedPoint2 deltaEnergy, + out FixedPoint2 overloadEnergy, + bool safe = false) + { + deltaEnergy = 0; + overloadEnergy = 0; + + if (!Resolve(sender, ref sender.Comp) || !Resolve(receiver, ref receiver.Comp)) + return; + + var transferEnergy = energy; + //We check how much space is left in the container so as not to overload it, but only if it does not support overloading + if (!receiver.Comp.UnsafeSupport || safe) + { + var freeSpace = receiver.Comp.MaxEnergy - receiver.Comp.Energy; + transferEnergy = FixedPoint2.Min(freeSpace, energy); + } + + ChangeEnergy(sender, -transferEnergy, out var change, out var overload, safe); + ChangeEnergy(receiver , -(change + overload), out deltaEnergy, out overloadEnergy, safe); + } + + public bool HasEnergy(EntityUid uid, FixedPoint2 energy, CP14MagicEnergyContainerComponent? component = null, bool safe = false) + { + if (!Resolve(uid, ref component)) + return false; + + if (!safe && component.UnsafeSupport) + return true; + + return component.Energy >= energy; + } + + public string GetEnergyExaminedText(Entity ent) + { + var power = (int) (ent.Comp.Energy / ent.Comp.MaxEnergy * 100); + + // TODO: customization for examined + + var color = "#3fc488"; + if (power < 66) + color = "#f2a93a"; + + if (power < 33) + color = "#c23030"; + + return Loc.GetString("cp14-magic-energy-scan-result", + ("item", MetaData(ent).EntityName), + ("power", power), + ("color", color)); + } + + private void UpdateMagicAlert(Entity ent) + { + if (ent.Comp.MagicAlert is null) + return; + + var level = ContentHelpers.RoundToLevels( + MathF.Max(0f, (float) ent.Comp.Energy), + (float) ent.Comp.MaxEnergy, + _alerts.GetMaxSeverity(ent.Comp.MagicAlert.Value)); + + _alerts.ShowAlert(ent, ent.Comp.MagicAlert.Value, (short) level); } } @@ -184,9 +166,16 @@ public partial class SharedCP14MagicEnergySystem : EntitySystem /// public sealed class CP14MagicEnergyLevelChangeEvent : EntityEventArgs { - public FixedPoint2 OldValue; - public FixedPoint2 NewValue; - public FixedPoint2 MaxValue; + public readonly FixedPoint2 OldValue; + public readonly FixedPoint2 NewValue; + public readonly FixedPoint2 MaxValue; + + public CP14MagicEnergyLevelChangeEvent(FixedPoint2 oldValue, FixedPoint2 newValue, FixedPoint2 maxValue) + { + OldValue = oldValue; + NewValue = newValue; + MaxValue = maxValue; + } } /// @@ -194,7 +183,12 @@ public sealed class CP14MagicEnergyLevelChangeEvent : EntityEventArgs /// public sealed class CP14MagicEnergyOverloadEvent : EntityEventArgs { - public FixedPoint2 OverloadEnergy; + public readonly FixedPoint2 OverloadEnergy; + + public CP14MagicEnergyOverloadEvent(FixedPoint2 overloadEnergy) + { + OverloadEnergy = overloadEnergy; + } } /// @@ -202,5 +196,10 @@ public sealed class CP14MagicEnergyOverloadEvent : EntityEventArgs /// public sealed class CP14MagicEnergyBurnOutEvent : EntityEventArgs { - public FixedPoint2 BurnOutEnergy; + public readonly FixedPoint2 BurnOutEnergy; + + public CP14MagicEnergyBurnOutEvent(FixedPoint2 burnOutEnergy) + { + BurnOutEnergy = burnOutEnergy; + } } diff --git a/Content.Shared/_CP14/MagicEssence/CP14MagicEssenceSystem.cs b/Content.Shared/_CP14/MagicEssence/CP14MagicEssenceSystem.cs index 256c3814f3..2b979ce31d 100644 --- a/Content.Shared/_CP14/MagicEssence/CP14MagicEssenceSystem.cs +++ b/Content.Shared/_CP14/MagicEssence/CP14MagicEssenceSystem.cs @@ -47,9 +47,12 @@ public partial class CP14MagicEssenceSystem : EntitySystem private void OnPowerChanged(Entity ent, ref CP14SlotCrystalPowerChangedEvent args) { if (args.Powered) + { EnsureComp(ent); - else - RemCompDeferred(ent); + return; + } + + RemCompDeferred(ent); } private void OnCollectorCollide(Entity ent, ref StartCollideEvent args) @@ -86,7 +89,7 @@ public partial class CP14MagicEssenceSystem : EntitySystem if (!TryComp(ent, out var energyContainer)) return; - _magicEnergy.ChangeEnergy(ent, -energyContainer.Energy, out _, out _, energyContainer, safe: true); + _magicEnergy.ChangeEnergy((ent, energyContainer), -energyContainer.Energy, out _, out _, safe: true); //TODO move to server if (_net.IsClient)