refactor: Magic system (#1138)

Co-authored-by: Ed <96445749+TheShuEd@users.noreply.github.com>
This commit is contained in:
Tornado Tech
2025-04-15 00:32:41 +10:00
committed by GitHub
parent e7cd69e383
commit 52f31e4403
14 changed files with 266 additions and 262 deletions

View File

@@ -0,0 +1,5 @@
using Content.Shared._CP14.MagicEnergy;
namespace Content.Client._CP14.MagicEnergy;
public sealed class CP14MagicEnergyCrystalSlotSystem : SharedCP14MagicEnergyCrystalSlotSystem;

View File

@@ -0,0 +1,5 @@
using Content.Shared._CP14.MagicEnergy;
namespace Content.Client._CP14.MagicEnergy;
public sealed class CP14MagicEnergySystem : SharedCP14MagicEnergySystem;

View File

@@ -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<CP14MagicEnergySystem>();
var magicSystem = entityManager.System<CP14MagicEnergySystem>();
magicSystem.ChangeEnergy(args.TargetEntity, ManaDelta * scale, out var changed, out var overload, safe: Safe);
scale -= FixedPoint2.Abs(changed + overload);
if (args.EntityManager.TryGetComponent<CP14MagicEnergyCrystalSlotComponent>(args.TargetEntity,
out var crystalSlot))
{
var slotSystem = args.EntityManager.System<CP14MagicEnergyCrystalSlotSystem>();
slotSystem.TryChangeEnergy(args.TargetEntity, ManaDelta * scale, crystalSlot);
}
if (!args.EntityManager.TryGetComponent<CP14MagicEnergyCrystalSlotComponent>(args.TargetEntity, out var crystalSlot))
return;
var slotSystem = entityManager.System<CP14MagicEnergyCrystalSlotSystem>();
slotSystem.TryChangeEnergy((args.TargetEntity, crystalSlot), ManaDelta * scale);
}
}

View File

@@ -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<CP14MagicEnergyCrystalComponent, CP14MagicEnergyLevelChangeEvent>(OnEnergyChanged);
SubscribeLocalEvent<CP14MagicEnergyCrystalSlotComponent, ExaminedEvent>(OnExamined);
SubscribeLocalEvent<CP14MagicEnergyCrystalSlotComponent, CP14SlotCrystalChangedEvent>(OnCrystalChanged);
}
private void OnCrystalChanged(Entity<CP14MagicEnergyCrystalSlotComponent> 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<CP14MagicEnergyCrystalComponent> 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<CP14MagicEnergyCrystalSlotComponent> 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<CP14MagicEnergyCrystalSlotComponent?> ent,
[NotNullWhen(true)] out Entity<CP14MagicEnergyContainerComponent>? 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<CP14MagicEnergyContainerComponent>(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<CP14MagicEnergyCrystalSlotComponent?> 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<CP14MagicEnergyCrystalSlotComponent?> 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;
}
}

View File

@@ -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<CP14MagicEnergyPhotosynthesisComponent, CP14MagicEnergyContainerComponent>();
@@ -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);
}
}
}

View File

@@ -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);

View File

@@ -10,6 +10,8 @@ public sealed partial class CP14MagicEnergySystem : SharedCP14MagicEnergySystem
public override void Initialize()
{
base.Initialize();
InitializeDraw();
InitializePortRelay();
}

View File

@@ -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<CP14MagicEnergyContainerComponent>(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<CP14MagicEffectRequiredMusicToolComponent> 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();
}
}

View File

@@ -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<AlertPrototype>? MagicAlert = null;
public ProtoId<AlertPrototype>? MagicAlert;
/// <summary>
/// Does this container support unsafe energy manipulation?
/// </summary>
[DataField, AutoNetworkedField]
public bool UnsafeSupport = false;
public bool UnsafeSupport;
}

View File

@@ -1,11 +1,13 @@
using Robust.Shared.GameStates;
using Robust.Shared.Serialization;
namespace Content.Shared._CP14.MagicEnergy.Components;
/// <summary>
/// Allows you to examine how much energy is in that object
/// Allows you to examine how much energy is in that object.
/// </summary>
[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,
}
/// <summary>
@@ -40,6 +42,7 @@ public sealed class CP14SlotCrystalChangedEvent : EntityEventArgs
public sealed class CP14SlotCrystalPowerChangedEvent : EntityEventArgs
{
public readonly bool Powered;
public CP14SlotCrystalPowerChangedEvent(bool powered)
{
Powered = powered;

View File

@@ -1,9 +1,10 @@
using Robust.Shared.GameStates;
namespace Content.Shared._CP14.MagicEnergy.Components;
/// <summary>
/// Allows you to examine how much energy is in that object
/// Allows you to examine how much energy is in that object.
/// </summary>
[RegisterComponent, Access(typeof(SharedCP14MagicEnergySystem))]
public sealed partial class CP14MagicEnergyExaminableComponent : Component
{
}
[RegisterComponent, NetworkedComponent]
[Access(typeof(SharedCP14MagicEnergySystem))]
public sealed partial class CP14MagicEnergyExaminableComponent : Component;

View File

@@ -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()
{

View File

@@ -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<CP14MagicEnergyAmbientSoundComponent, CP14SlotCrystalPowerChangedEvent>(OnSlotPowerChanged);
SubscribeLocalEvent<CP14MagicEnergyContainerComponent, ComponentStartup>(OnComponentStartup);
SubscribeLocalEvent<CP14MagicEnergyContainerComponent, ComponentShutdown>(OnComponentShutdown);
SubscribeLocalEvent<CP14MagicEnergyExaminableComponent, ExaminedEvent>(OnExamined);
}
private void OnSlotPowerChanged(Entity<CP14MagicEnergyAmbientSoundComponent> ent, ref CP14SlotCrystalPowerChangedEvent args)
{
if (TryComp<AmbientSoundComponent>(ent, out var ambient))
{
_ambient.SetAmbience(ent, args.Powered);
}
SubscribeLocalEvent<CP14MagicEnergyAmbientSoundComponent, CP14SlotCrystalPowerChangedEvent>(OnSlotPowerChanged);
}
private void OnComponentStartup(Entity<CP14MagicEnergyContainerComponent> ent, ref ComponentStartup args)
@@ -38,135 +29,12 @@ public partial class SharedCP14MagicEnergySystem : EntitySystem
private void OnComponentShutdown(Entity<CP14MagicEnergyContainerComponent> 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<CP14MagicEnergyContainerComponent> 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<CP14MagicEnergyExaminableComponent> ent, ref ExaminedEvent args)
{
if (!TryComp<CP14MagicEnergyContainerComponent>(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<CP14MagicEnergyAmbientSoundComponent> ent, ref CP14SlotCrystalPowerChangedEvent args)
{
_ambient.SetAmbience(ent, args.Powered);
}
public void ChangeEnergy(Entity<CP14MagicEnergyContainerComponent?> 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<CP14MagicEnergyContainerComponent?> sender,
Entity<CP14MagicEnergyContainerComponent?> 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<CP14MagicEnergyContainerComponent> 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<CP14MagicEnergyContainerComponent> 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
/// </summary>
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;
}
}
/// <summary>
@@ -194,7 +183,12 @@ public sealed class CP14MagicEnergyLevelChangeEvent : EntityEventArgs
/// </summary>
public sealed class CP14MagicEnergyOverloadEvent : EntityEventArgs
{
public FixedPoint2 OverloadEnergy;
public readonly FixedPoint2 OverloadEnergy;
public CP14MagicEnergyOverloadEvent(FixedPoint2 overloadEnergy)
{
OverloadEnergy = overloadEnergy;
}
}
/// <summary>
@@ -202,5 +196,10 @@ public sealed class CP14MagicEnergyOverloadEvent : EntityEventArgs
/// </summary>
public sealed class CP14MagicEnergyBurnOutEvent : EntityEventArgs
{
public FixedPoint2 BurnOutEnergy;
public readonly FixedPoint2 BurnOutEnergy;
public CP14MagicEnergyBurnOutEvent(FixedPoint2 burnOutEnergy)
{
BurnOutEnergy = burnOutEnergy;
}
}

View File

@@ -47,9 +47,12 @@ public partial class CP14MagicEssenceSystem : EntitySystem
private void OnPowerChanged(Entity<CP14MagicEssenceCollectorComponent> ent, ref CP14SlotCrystalPowerChangedEvent args)
{
if (args.Powered)
{
EnsureComp<CP14MagicEssenceAttractorComponent>(ent);
else
RemCompDeferred<CP14MagicEssenceAttractorComponent>(ent);
return;
}
RemCompDeferred<CP14MagicEssenceAttractorComponent>(ent);
}
private void OnCollectorCollide(Entity<CP14MagicEssenceCollectorComponent> ent, ref StartCollideEvent args)
@@ -86,7 +89,7 @@ public partial class CP14MagicEssenceSystem : EntitySystem
if (!TryComp<CP14MagicEnergyContainerComponent>(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)