Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Spell scrolls #571

Merged
merged 5 commits into from
Nov 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public bool HasEnergy(EntityUid uid, FixedPoint2 energy, CP14MagicEnergyContaine
if (safe == false)
return true;

return component.Energy > energy;
return component.Energy >= energy;
}

public bool TryConsumeEnergy(EntityUid uid, FixedPoint2 energy, CP14MagicEnergyContainerComponent? component = null, bool safe = false)
Expand Down
25 changes: 16 additions & 9 deletions Content.Shared/_CP14/MagicSpell/CP14SharedMagicSystem.Actions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,18 @@ private void OnInstantAction(CP14DelayedInstantActionEvent args)
if (args is not ICP14DelayedMagicEffect delayedEffect)
return;

if (!CanCastSpell(args.Action, args.Performer))
if (!TryComp<CP14MagicEffectComponent>(args.Action, out var magicEffect))
return;

if (!TryComp<CP14MagicEffectComponent>(args.Action, out var magicEffect))
Entity<CP14MagicEffectComponent> spell = (args.Action, magicEffect);

if (!CanCastSpell(spell, args.Performer))
return;

if (args.CastDelay > 0)
{
var doAfter = new CP14DelayedInstantActionDoAfterEvent(args.Cooldown);
if (!TryCastSpellDelayed(delayedEffect, doAfter, args.Action, args.Performer))
if (!TryCastSpellDelayed(delayedEffect, doAfter, (args.Action, magicEffect), args.Performer))
return;
}

Expand Down Expand Up @@ -67,20 +69,23 @@ private void OnEntityWorldTargetAction(CP14DelayedEntityWorldTargetActionEvent a
if (args is not ICP14DelayedMagicEffect delayedEffect)
return;

if (!CanCastSpell(args.Action, args.Performer))
if (!TryComp<CP14MagicEffectComponent>(args.Action, out var magicEffect))
return;

if (!TryComp<CP14MagicEffectComponent>(args.Action, out var magicEffect))
Entity<CP14MagicEffectComponent> spell = (args.Action, magicEffect);

if (!CanCastSpell(spell, args.Performer))
return;


if (args.CastDelay > 0)
{
var doAfter = new CP14DelayedEntityWorldTargetActionDoAfterEvent(
EntityManager.GetNetCoordinates(args.Coords),
EntityManager.GetNetEntity(args.Entity),
args.Cooldown);

if (!TryCastSpellDelayed(delayedEffect, doAfter, args.Action, args.Performer))
if (!TryCastSpellDelayed(delayedEffect, doAfter, (args.Action, magicEffect), args.Performer))
return;
}

Expand Down Expand Up @@ -109,10 +114,12 @@ private void OnEntityTargetAction(CP14DelayedEntityTargetActionEvent args)
if (args is not ICP14DelayedMagicEffect delayedEffect)
return;

if (!CanCastSpell(args.Action, args.Performer))
if (!TryComp<CP14MagicEffectComponent>(args.Action, out var magicEffect))
return;

if (!TryComp<CP14MagicEffectComponent>(args.Action, out var magicEffect))
Entity<CP14MagicEffectComponent> spell = (args.Action, magicEffect);

if (!CanCastSpell(spell, args.Performer))
return;

if (args.CastDelay > 0)
Expand All @@ -121,7 +128,7 @@ private void OnEntityTargetAction(CP14DelayedEntityTargetActionEvent args)
EntityManager.GetNetEntity(args.Target),
args.Cooldown);

if (!TryCastSpellDelayed(delayedEffect, doAfter, args.Action, args.Performer))
if (!TryCastSpellDelayed(delayedEffect, doAfter, (args.Action, magicEffect), args.Performer))
return;
}

Expand Down
79 changes: 57 additions & 22 deletions Content.Shared/_CP14/MagicSpell/CP14SharedMagicSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,10 @@ private void OnMagicEffectInit(Entity<CP14MagicEffectComponent> ent, ref MapInit
/// <summary>
/// Checking to see if the spell can be used at all
/// </summary>
private bool CanCastSpell(EntityUid spell, EntityUid performer)
private bool CanCastSpell(Entity<CP14MagicEffectComponent> ent, EntityUid performer)
{
var ev = new CP14CastMagicEffectAttemptEvent(performer);
RaiseLocalEvent(spell, ref ev);
RaiseLocalEvent(ent, ref ev);
if (ev.Reason != string.Empty && _net.IsServer)
{
_popup.PopupEntity(ev.Reason, performer, performer);
Expand All @@ -92,22 +92,41 @@ private bool CanCastSpell(EntityUid spell, EntityUid performer)
/// </summary>
private void OnBeforeCastMagicEffect(Entity<CP14MagicEffectComponent> ent, ref CP14CastMagicEffectAttemptEvent args)
{
if (!TryComp<CP14MagicEnergyContainerComponent>(args.Performer, out var magicContainer))
if (ent.Comp.SpellStorage is null) //Dont have spellStorage, we use mana from caster
{
args.Cancel();
return;
}
if (!TryComp<CP14MagicEnergyContainerComponent>(args.Performer, out var magicContainer))
{
args.Cancel();
return;
}

var manaCost = CalculateManacost(ent, args.Performer);
var manaCost = CalculateManacost(ent, args.Performer);

if (!_magicEnergy.HasEnergy(args.Performer, manaCost, magicContainer, ent.Comp.Safe))
{
args.PushReason(Loc.GetString("cp14-magic-spell-not-enough-mana"));
args.Cancel();
if (!_magicEnergy.HasEnergy(args.Performer, manaCost, magicContainer, ent.Comp.Safe))
{
args.PushReason(Loc.GetString("cp14-magic-spell-not-enough-mana"));
args.Cancel();
}
else if(!_magicEnergy.HasEnergy(args.Performer, manaCost, magicContainer, true) && _net.IsServer) //фу какой некрасивый хардкод
{ // \/
_popup.PopupEntity(Loc.GetString("cp14-magic-spell-not-enough-mana-cast-warning-"+_random.Next(5)), args.Performer, args.Performer, PopupType.SmallCaution);
}
}
else if(!_magicEnergy.HasEnergy(args.Performer, manaCost, magicContainer, true) && _net.IsServer) //фу какой некрасивый | хардкод
{ // \/
_popup.PopupEntity(Loc.GetString("cp14-magic-spell-not-enough-mana-cast-warning-"+_random.Next(5)), args.Performer, args.Performer, PopupType.SmallCaution);
else //We HAVE SpellStorage, use mana from spellStorage
{
if (!TryComp<CP14MagicEnergyContainerComponent>(ent.Comp.SpellStorage, out var magicContainer))
{
args.Cancel();
return;
}

var manaCost = CalculateManacost(ent, ent.Comp.SpellStorage.Value);

if (!_magicEnergy.HasEnergy(ent.Comp.SpellStorage.Value, manaCost, magicContainer, true))
{
args.PushReason(Loc.GetString("cp14-magic-spell-not-enough-mana-item", ("item", MetaData(ent.Comp.SpellStorage.Value).EntityName)));
args.Cancel();
}
}
}

Expand All @@ -122,16 +141,18 @@ private void CastTelegraphy(Entity<CP14MagicEffectComponent> ent, CP14SpellEffec
}
}

private bool TryCastSpellDelayed(ICP14DelayedMagicEffect delayedEffect, DoAfterEvent doAfter, EntityUid action, EntityUid performer)
private bool TryCastSpellDelayed(ICP14DelayedMagicEffect delayedEffect, DoAfterEvent doAfter, Entity<CP14MagicEffectComponent> action, EntityUid performer)
{
var doAfterEventArgs = new DoAfterArgs(EntityManager, performer, delayedEffect.CastDelay, doAfter, action)
var doAfterEventArgs = new DoAfterArgs(EntityManager, performer, delayedEffect.CastDelay, doAfter, action, used: action.Comp.SpellStorage)
{
BreakOnMove = delayedEffect.BreakOnMove,
BreakOnDamage = delayedEffect.BreakOnDamage,
Hidden = delayedEffect.Hidden,
DistanceThreshold = 100f,
CancelDuplicate = true,
BlockDuplicate = true,
BreakOnDropItem = true,
NeedHand = true,
};

return _doAfter.TryStartDoAfter(doAfterEventArgs);
Expand All @@ -158,11 +179,25 @@ private void OnAfterCastMagicEffect(Entity<CP14MagicEffectComponent> ent, ref CP
if (_net.IsClient)
return;

if (!HasComp<CP14MagicEnergyContainerComponent>(args.Performer))
return;
if (ent.Comp.SpellStorage is null) //We pickup mana from player
{
if (!HasComp<CP14MagicEnergyContainerComponent>(args.Performer))
return;

var manaCost = CalculateManacost(ent, args.Performer.Value);
_magicEnergy.TryConsumeEnergy(args.Performer.Value, manaCost, safe: ent.Comp.Safe);
var manaCost = CalculateManacost(ent, args.Performer.Value);
_magicEnergy.TryConsumeEnergy(args.Performer.Value, manaCost, safe: ent.Comp.Safe);
}
else //We pickup mana from SpellStorage
{
if (!HasComp<CP14MagicEnergyContainerComponent>(ent.Comp.SpellStorage))
return;

var manaCost = CalculateManacost(ent, ent.Comp.SpellStorage.Value);
_magicEnergy.TryConsumeEnergy(ent.Comp.SpellStorage.Value, manaCost, safe: ent.Comp.Safe);

var spellEv = new CP14SpellFromSpellStorageUsedEvent(args.Performer, ent, manaCost);
RaiseLocalEvent(ent.Comp.SpellStorage.Value, ref spellEv);
}
}

private FixedPoint2 CalculateManacost(Entity<CP14MagicEffectComponent> ent, EntityUid caster)
Expand All @@ -174,8 +209,8 @@ private FixedPoint2 CalculateManacost(Entity<CP14MagicEffectComponent> ent, Enti
var manaEv = new CP14CalculateManacostEvent(caster, ent.Comp.ManaCost, ent.Comp.MagicType);
RaiseLocalEvent(caster, manaEv);

if (TryComp<CP14ProvidedBySpellStorageComponent>(ent, out var provided) && provided.SpellStorage is not null)
RaiseLocalEvent(provided.SpellStorage.Value, manaEv);
if (ent.Comp.SpellStorage is not null)
RaiseLocalEvent(ent.Comp.SpellStorage.Value, manaEv);

manaCost = manaEv.GetManacost();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Content.Shared._CP14.MagicRitual.Prototypes;
using Content.Shared._CP14.MagicSpell.Spells;
using Content.Shared._CP14.MagicSpellStorage;
using Content.Shared.FixedPoint;
using Robust.Shared.Prototypes;

Expand All @@ -8,9 +9,15 @@ namespace Content.Shared._CP14.MagicSpell.Components;
/// <summary>
/// Restricts the use of this action, by spending mana or user requirements.
/// </summary>
[RegisterComponent, Access(typeof(CP14SharedMagicSystem))]
[RegisterComponent, Access(typeof(CP14SharedMagicSystem), typeof(CP14SpellStorageSystem))]
public sealed partial class CP14MagicEffectComponent : Component
{
/// <summary>
/// if this effect was provided by an spellstorage, it will be recorded here automatically.
/// </summary>
[DataField]
public Entity<CP14SpellStorageComponent>? SpellStorage;

[DataField]
public FixedPoint2 ManaCost = 0f;

Expand Down
16 changes: 16 additions & 0 deletions Content.Shared/_CP14/MagicSpell/Events/CP14CastMagicEffectEvent.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Content.Shared._CP14.MagicRitual.Prototypes;
using Content.Shared._CP14.MagicSpell.Components;
using Content.Shared.FixedPoint;
using Content.Shared.Inventory;
using Robust.Shared.Prototypes;
Expand Down Expand Up @@ -97,3 +98,18 @@ public CP14AfterCastMagicEffectEvent(EntityUid? performer)
Performer = performer;
}
}

[ByRefEvent]
public sealed class CP14SpellFromSpellStorageUsedEvent : EntityEventArgs
{
public EntityUid? Performer { get; init; }
public Entity<CP14MagicEffectComponent> Action { get; init; }
public FixedPoint2 Manacost { get; init; }

public CP14SpellFromSpellStorageUsedEvent(EntityUid? performer, Entity<CP14MagicEffectComponent> action, FixedPoint2 manacost)
{
Performer = performer;
Action = action;
Manacost = manacost;
}
}

This file was deleted.

13 changes: 12 additions & 1 deletion Content.Shared/_CP14/MagicSpellStorage/CP14SpellStorageSystem.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
using Content.Shared._CP14.MagicAttuning;
using Content.Shared._CP14.MagicSpell.Components;
using Content.Shared._CP14.MagicSpell.Events;
using Content.Shared.Actions;
using Content.Shared.Clothing;
using Content.Shared.Clothing.Components;
using Content.Shared.Damage;
using Content.Shared.Hands;
using Content.Shared.Hands.EntitySystems;
using Content.Shared.Mind;
Expand All @@ -20,12 +23,15 @@ public sealed partial class CP14SpellStorageSystem : EntitySystem
[Dependency] private readonly CP14SharedMagicAttuningSystem _attuning = default!;
[Dependency] private readonly SharedHandsSystem _hands = default!;
[Dependency] private readonly INetManager _net = default!;
[Dependency] private readonly DamageableSystem _damageable = default!;

public override void Initialize()
{
SubscribeLocalEvent<CP14SpellStorageComponent, MapInitEvent>(OnMagicStorageInit);
SubscribeLocalEvent<CP14SpellStorageComponent, ComponentShutdown>(OnMagicStorageShutdown);

SubscribeLocalEvent<CP14SpellStorageUseDamageComponent, CP14SpellFromSpellStorageUsedEvent>(OnSpellUsed);

SubscribeLocalEvent<CP14SpellStorageAccessHoldingComponent, GotEquippedHandEvent>(OnEquippedHand);
SubscribeLocalEvent<CP14SpellStorageAccessHoldingComponent, AddedAttuneToMindEvent>(OnHandAddedAttune);

Expand All @@ -36,6 +42,11 @@ public override void Initialize()
SubscribeLocalEvent<CP14SpellStorageRequireAttuneComponent, RemovedAttuneFromMindEvent>(OnRemovedAttune);
}

private void OnSpellUsed(Entity<CP14SpellStorageUseDamageComponent> ent, ref CP14SpellFromSpellStorageUsedEvent args)
{
_damageable.TryChangeDamage(ent, ent.Comp.DamagePerMana * args.Manacost);
}

/// <summary>
/// When we initialize, we create action entities, and add them to this item.
/// </summary>
Expand All @@ -50,7 +61,7 @@ private void OnMagicStorageInit(Entity<CP14SpellStorageComponent> mStorage, ref
if (spellEnt is null)
continue;

var provided = EntityManager.EnsureComponent<CP14ProvidedBySpellStorageComponent>(spellEnt.Value);
var provided = EntityManager.EnsureComponent<CP14MagicEffectComponent>(spellEnt.Value);
provided.SpellStorage = mStorage;

mStorage.Comp.SpellEntities.Add(spellEnt.Value);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using Content.Shared.Damage;

namespace Content.Shared._CP14.MagicSpellStorage;

/// <summary>
/// Causes damage to the Spell storage when spells from it are used
/// </summary>
[RegisterComponent, Access(typeof(CP14SpellStorageSystem))]
public sealed partial class CP14SpellStorageUseDamageComponent : Component
{
/// <summary>
/// the amount of damage this entity will take per unit manacost of the spell used
/// </summary>
[DataField(required: true)]
public DamageSpecifier DamagePerMana = default!;
}
2 changes: 2 additions & 0 deletions Resources/Locale/en-US/_CP14/magicEnergy/magic-spells.ftl
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
cp14-magic-spell-not-enough-mana = Not enough mana!
cp14-magic-spell-not-enough-mana-item = Not enough mana in {$item}!


cp14-magic-spell-not-enough-mana-cast-warning-0 = Everything inside you starts to whine unpleasantly...
cp14-magic-spell-not-enough-mana-cast-warning-1 = Your head starts to buzz...
Expand Down
1 change: 1 addition & 0 deletions Resources/Locale/ru-RU/_CP14/magicEnergy/magic-spells.ftl
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
cp14-magic-spell-not-enough-mana = Недостаточно магической энергии!
cp14-magic-spell-not-enough-mana-item = Недостаточно магической энергии в {$item}!

cp14-magic-spell-not-enough-mana-cast-warning-0 = Внутри вас все начинает неприятно ныть...
cp14-magic-spell-not-enough-mana-cast-warning-1 = Голова начинает шуметь...
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@
sprite: _CP14/Effects/Magic/spells_icons.rsi
state: mana_gift
event: !type:CP14DelayedEntityTargetActionEvent
cooldown: 5
castDelay: 2
cooldown: 1
castDelay: 1

- type: entity
id: CP14RuneManaGift
Expand Down
5 changes: 5 additions & 0 deletions Resources/Prototypes/_CP14/Entities/Clothing/Rings/ring.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@
- ring
- type: Sprite
sprite: _CP14/Clothing/Rings/rings.rsi
- type: CP14MagicEnergyExaminable
- type: CP14MagicEnergyContainer
energy: 50
maxEnergy: 50
- type: CP14MagicUnsafeDamage

- type: entity
id: CP14ClothingRingIceDagger
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@
weight: 0.5
- id: CP14CopperCoin1
weight: 1
- !type:GroupSelector
children:
- id: CP14SpellScrollFireball
- id: CP14SpellScrollCureWounds
- id: CP14SpellScrollIceShards
- id: CP14SpellScrollManaGift
- !type:GroupSelector
children:
- id: CP14DyeRed
Expand Down
Loading
Loading