Files
tbd-station-14/Content.Shared/Magic/SpellbookSystem.cs
Perry Fraser 6cefc412a3 fix: spellbooks can have infinite charges (#38376)
* fix: spellbooks can have infinite charges

* refactor: indicate infinite spellbook charges with null

Not sure if I like this much better...
2025-07-04 23:28:56 -07:00

99 lines
3.4 KiB
C#
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using Content.Shared.Actions;
using Content.Shared.Actions.Components;
using Content.Shared.Charges.Systems;
using Content.Shared.DoAfter;
using Content.Shared.Interaction.Events;
using Content.Shared.Magic.Components;
using Content.Shared.Mind;
using Robust.Shared.Network;
namespace Content.Shared.Magic;
public sealed class SpellbookSystem : EntitySystem
{
[Dependency] private readonly SharedChargesSystem _sharedCharges = default!;
[Dependency] private readonly SharedMindSystem _mind = default!;
[Dependency] private readonly SharedDoAfterSystem _doAfter = default!;
[Dependency] private readonly SharedActionsSystem _actions = default!;
[Dependency] private readonly ActionContainerSystem _actionContainer = default!;
[Dependency] private readonly INetManager _netManager = default!;
public override void Initialize()
{
SubscribeLocalEvent<SpellbookComponent, MapInitEvent>(OnInit, before: [typeof(SharedMagicSystem)]);
SubscribeLocalEvent<SpellbookComponent, UseInHandEvent>(OnUse);
SubscribeLocalEvent<SpellbookComponent, SpellbookDoAfterEvent>(OnDoAfter);
}
private void OnInit(Entity<SpellbookComponent> ent, ref MapInitEvent args)
{
foreach (var (id, charges) in ent.Comp.SpellActions)
{
var spell = _actionContainer.AddAction(ent, id);
if (spell == null)
continue;
// Null means infinite charges.
if (charges is { } count)
_sharedCharges.SetCharges(spell.Value, count);
ent.Comp.Spells.Add(spell.Value);
}
}
private void OnUse(Entity<SpellbookComponent> ent, ref UseInHandEvent args)
{
if (args.Handled)
return;
AttemptLearn(ent, args);
args.Handled = true;
}
private void OnDoAfter<T>(Entity<SpellbookComponent> ent, ref T args) where T : DoAfterEvent // Sometimes i despise this language
{
if (args.Handled || args.Cancelled)
return;
args.Handled = true;
if (!ent.Comp.LearnPermanently)
{
_actions.GrantActions(args.Args.User, ent.Comp.Spells, ent.Owner);
return;
}
if (_mind.TryGetMind(args.Args.User, out var mindId, out _))
{
var mindActionContainerComp = EnsureComp<ActionsContainerComponent>(mindId);
if (_netManager.IsServer)
_actionContainer.TransferAllActionsWithNewAttached(ent, mindId, args.Args.User, newContainer: mindActionContainerComp);
}
else
{
foreach (var (id, charges) in ent.Comp.SpellActions)
{
EntityUid? actionId = null;
if (_actions.AddAction(args.Args.User, ref actionId, id)
&& charges is { } count) // Null means infinite charges
_sharedCharges.SetCharges(actionId.Value, count);
}
}
ent.Comp.SpellActions.Clear();
}
private void AttemptLearn(Entity<SpellbookComponent> ent, UseInHandEvent args)
{
var doAfterEventArgs = new DoAfterArgs(EntityManager, args.User, ent.Comp.LearnTime, new SpellbookDoAfterEvent(), ent, target: ent)
{
BreakOnMove = true,
BreakOnDamage = true,
NeedHand = true, //What, are you going to read with your eyes only??
};
_doAfter.TryStartDoAfter(doAfterEventArgs);
}
}