fix: Cleans up LINQ calls. (#965)

- [X] Removes several `ToList()` uses with `PooledRefQueue`
- [X] Adds a `PeekRandom` to PooledRefQueue
- [X] Updates EV/BS so they dispel each other in a more efficient manner.
- [X] Fixes Firebomb so it works like a normal firefield.
- [X] Fixes field spells so they aren't unnecessarily using a Point3D ref more than necessary.
- [X] Removes extra allocation in campfire by using reverse loop.
- [X] Removes other LINQ calls that aren't needed.
This commit is contained in:
Kamron Batman 2022-03-20 19:20:54 -07:00 • committed by GitHub
parent daf89686d1
commit 23532db603
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
40 changed files with 585 additions and 447 deletions

View file

@ -256,14 +256,14 @@ namespace Server.Spells
return false;
}
public static bool GetEastToWest(IPoint3D from,IPoint3D target)
public static bool GetEastToWest(Point3D from, Point3D target)
{
var dx = from.X - target.X;
var dy = from.Y - target.Y;
var rx = (dx - dy) * 44;
var ry = (dx + dy) * 44;
return (rx >= 0 && ry < 0) || (ry >= 0 && rx < 0);
return rx >= 0 && ry < 0 || ry >= 0 && rx < 0;
}
public static bool CanRevealCaster(Mobile m) => m is BaseCreature { Controlled: false };

View file

@ -1,4 +1,5 @@
using System.Linq;
using Server.Collections;
namespace Server.Spells.Bushido
{
@ -21,12 +22,17 @@ namespace Server.Spells.Bushido
var weapon = attacker.Weapon;
var targets = attacker.GetMobilesInRange(weapon.MaxRange)
.Where(m => m != defender)
.Where(m => m.Combatant == attacker)
.ToList();
var eable = attacker.GetMobilesInRange(weapon.MaxRange);
using var queue = PooledRefQueue<Mobile>.Create();
foreach (var m in eable)
{
if (m != defender && m.Combatant == attacker)
{
queue.Enqueue(m);
}
}
if (targets.Count <= 0)
if (queue.Count <= 0)
{
attacker.SendLocalizedMessage(1063123); // There are no valid targets to attack!
return;
@ -37,7 +43,7 @@ namespace Server.Spells.Bushido
return;
}
var target = targets.RandomElement();
Mobile target = queue.PeekRandom();
var damageBonus = attacker.Skills.Bushido.Value / 100.0;
@ -47,7 +53,7 @@ namespace Server.Spells.Bushido
}
attacker.SendLocalizedMessage(1063171); // You transfer the momentum of your weapon into another enemy!
target.SendLocalizedMessage(1063172); // You were hit by the momentum of a Samurai's weapon!
target!.SendLocalizedMessage(1063172); // You were hit by the momentum of a Samurai's weapon!
target.FixedParticles(0x37B9, 1, 4, 0x251D, 0, 0, EffectLayer.Waist);

View file

@ -1,5 +1,5 @@
using System;
using System.Linq;
using Server.Collections;
using Server.Items;
using Server.Mobiles;
using Server.Spells.Necromancy;
@ -46,11 +46,21 @@ namespace Server.Spells.Chivalry
var chiv = Caster.Skills.Chivalry.Value;
var targets = Caster.GetMobilesInRange(8)
.Where(m => Caster != m && SpellHelper.ValidIndirectTarget(Caster, m) && Caster.CanBeHarmful(m, false));
foreach (var m in targets)
var eable = Caster.GetMobilesInRange(8);
using var queue = PooledRefQueue<Mobile>.Create();
foreach (var m in eable)
{
if (Caster != m && SpellHelper.ValidIndirectTarget(Caster, m) && Caster.CanBeHarmful(m, false))
{
queue.Enqueue(m);
}
}
eable.Free();
while (queue.Count > 0)
{
var m = queue.Dequeue();
if (m is BaseCreature bc)
{
if (bc.Summoned && !bc.IsAnimatedDead)

View file

@ -1,5 +1,5 @@
using System;
using System.Linq;
using Server.Collections;
namespace Server.Spells.Eighth
{
@ -37,14 +37,21 @@ namespace Server.Spells.Eighth
return;
}
var targets = Caster.GetMobilesInRange(1 + (int)(Caster.Skills.Magery.Value / 15.0))
.Where(
m => Caster != m && SpellHelper.ValidIndirectTarget(Caster, m) && Caster.CanBeHarmful(m, false) &&
(!Core.AOS || Caster.InLOS(m))
);
foreach (var m in targets)
var eable = Caster.GetMobilesInRange(1 + (int)(Caster.Skills.Magery.Value / 15.0));
using var queue = PooledRefQueue<Mobile>.Create();
foreach (var m in eable)
{
if (Caster != m && SpellHelper.ValidIndirectTarget(Caster, m) && Caster.CanBeHarmful(m, false) &&
(!Core.AOS || Caster.InLOS(m)))
{
queue.Enqueue(m);
}
}
while (queue.Count > 0)
{
var m = queue.Dequeue();
int damage;
if (Core.AOS)

View file

@ -30,22 +30,21 @@ namespace Server.Spells.Fifth
if (SpellHelper.CheckTown(p, Caster) && CheckSequence())
{
SpellHelper.Turn(Caster, p);
SpellHelper.GetSurfaceTop(ref p);
var eastToWest = SpellHelper.GetEastToWest(Caster.Location, p);
var loc = new Point3D(p);
var eastToWest = SpellHelper.GetEastToWest(Caster.Location, loc);
Effects.PlaySound(new Point3D(p), Caster.Map, 0x20B);
Effects.PlaySound(loc, Caster.Map, 0x20B);
var itemID = eastToWest ? 0x3915 : 0x3922;
var duration = TimeSpan.FromSeconds(3 + Caster.Skills.Magery.Fixed / 25);
for (var i = -2; i <= 2; ++i)
{
var loc = new Point3D(eastToWest ? p.X + i : p.X, eastToWest ? p.Y : p.Y + i, p.Z);
var targetLoc = new Point3D(eastToWest ? loc.X + i : loc.X, eastToWest ? loc.Y : loc.Y + i, loc.Z);
new InternalItem(itemID, loc, Caster, Caster.Map, duration, i);
new InternalItem(itemID, targetLoc, Caster, Caster.Map, duration, i);
}
}

View file

@ -33,28 +33,23 @@ namespace Server.Spells.Fourth
SpellHelper.GetSurfaceTop(ref p);
var eastToWest = SpellHelper.GetEastToWest(Caster.Location, p);
var loc = new Point3D(p);
Effects.PlaySound(new Point3D(p), Caster.Map, 0x20C);
var eastToWest = SpellHelper.GetEastToWest(Caster.Location, loc);
Effects.PlaySound(loc, Caster.Map, 0x20C);
var itemID = eastToWest ? 0x398C : 0x3996;
TimeSpan duration;
if (Core.AOS)
{
duration = TimeSpan.FromSeconds((15 + Caster.Skills.Magery.Fixed / 5.0) / 4.0);
}
else
{
duration = TimeSpan.FromSeconds(4.0 + Caster.Skills.Magery.Value * 0.5);
}
var duration = Core.AOS
? TimeSpan.FromSeconds((15 + Caster.Skills.Magery.Fixed / 5.0) / 4.0)
: TimeSpan.FromSeconds(4.0 + Caster.Skills.Magery.Value * 0.5);
for (var i = -2; i <= 2; ++i)
{
var loc = new Point3D(eastToWest ? p.X + i : p.X, eastToWest ? p.Y : p.Y + i, p.Z);
var targetLoc = new Point3D(eastToWest ? loc.X + i : loc.X, eastToWest ? loc.Y : loc.Y + i, loc.Z);
new FireFieldItem(itemID, loc, Caster, Caster.Map, duration, i);
new FireFieldItem(itemID, targetLoc, Caster, Caster.Map, duration, i);
}
}

View file

@ -30,42 +30,35 @@ namespace Server.Spells.Seventh
if (SpellHelper.CheckTown(p, Caster) && CheckSequence())
{
SpellHelper.Turn(Caster, p);
SpellHelper.GetSurfaceTop(ref p);
var eastToWest = SpellHelper.GetEastToWest(Caster.Location, p);
var loc = new Point3D(p);
Effects.PlaySound(new Point3D(p), Caster.Map, 0x20B);
var eastToWest = SpellHelper.GetEastToWest(Caster.Location, loc);
TimeSpan duration;
Effects.PlaySound(loc, Caster.Map, 0x20B);
if (Core.AOS)
{
duration = TimeSpan.FromSeconds((15 + Caster.Skills.Magery.Fixed / 5) / 7.0);
}
else
{
// (28% of magery) + 2.0 seconds
duration = TimeSpan.FromSeconds(Caster.Skills.Magery.Value * 0.28 + 2.0);
}
TimeSpan duration = Core.AOS
? TimeSpan.FromSeconds((15 + Caster.Skills.Magery.Fixed / 5) / 7.0)
: TimeSpan.FromSeconds(Caster.Skills.Magery.Value * 0.28 + 2.0);
var itemID = eastToWest ? 0x3946 : 0x3956;
for (var i = -2; i <= 2; ++i)
{
var loc = new Point3D(eastToWest ? p.X + i : p.X, eastToWest ? p.Y : p.Y + i, p.Z);
var canFit = SpellHelper.AdjustField(ref loc, Caster.Map, 12, false);
var targetLoc = new Point3D(eastToWest ? loc.X + i : loc.X, eastToWest ? loc.Y : loc.Y + i, loc.Z);
var canFit = SpellHelper.AdjustField(ref targetLoc, Caster.Map, 12, false);
if (!canFit)
{
continue;
}
Item item = new InternalItem(loc, Caster.Map, duration, itemID, Caster);
Item item = new InternalItem(targetLoc, Caster.Map, duration, itemID, Caster);
item.ProcessDelta();
Effects.SendLocationParticles(
EffectItem.Create(loc, Caster.Map, EffectItem.DefaultDuration),
EffectItem.Create(targetLoc, Caster.Map, EffectItem.DefaultDuration),
0x376A,
9,
10,

View file

@ -29,12 +29,12 @@ namespace Server.Spells.Sixth
if (SpellHelper.CheckTown(p, Caster) && CheckSequence())
{
SpellHelper.Turn(Caster, p);
SpellHelper.GetSurfaceTop(ref p);
var eastToWest = SpellHelper.GetEastToWest(Caster.Location, p);
var loc = new Point3D(p);
var eastToWest = SpellHelper.GetEastToWest(Caster.Location, loc);
Effects.PlaySound(new Point3D(p), Caster.Map, 0x20B);
Effects.PlaySound(loc, Caster.Map, 0x20B);
var itemID = eastToWest ? 0x3967 : 0x3979;
@ -42,18 +42,18 @@ namespace Server.Spells.Sixth
for (var i = -2; i <= 2; ++i)
{
var loc = new Point3D(eastToWest ? p.X + i : p.X, eastToWest ? p.Y : p.Y + i, p.Z);
var targetLoc = new Point3D(eastToWest ? loc.X + i : loc.X, eastToWest ? loc.Y : loc.Y + i, loc.Z);
if (!SpellHelper.AdjustField(ref loc, Caster.Map, 12, false))
if (!SpellHelper.AdjustField(ref targetLoc, Caster.Map, 12, false))
{
continue;
}
Item item = new InternalItem(Caster, itemID, loc, Caster.Map, duration);
Item item = new InternalItem(Caster, itemID, targetLoc, Caster.Map, duration);
item.ProcessDelta();
Effects.SendLocationParticles(
EffectItem.Create(loc, Caster.Map, EffectItem.DefaultDuration),
EffectItem.Create(targetLoc, Caster.Map, EffectItem.DefaultDuration),
0x376A,
9,
10,

View file

@ -30,14 +30,16 @@ namespace Server.Spells.Third
SpellHelper.GetSurfaceTop(ref p);
var eastToWest = SpellHelper.GetEastToWest(Caster.Location, p);
var loc = new Point3D(p);
Effects.PlaySound(new Point3D(p), Caster.Map, 0x1F6);
var eastToWest = SpellHelper.GetEastToWest(Caster.Location, loc);
Effects.PlaySound(loc, Caster.Map, 0x1F6);
for (var i = -1; i <= 1; ++i)
{
var loc = new Point3D(eastToWest ? p.X + i : p.X, eastToWest ? p.Y : p.Y + i, p.Z);
var canFit = SpellHelper.AdjustField(ref loc, Caster.Map, 22, true);
var targetLoc = new Point3D(eastToWest ? p.X + i : p.X, eastToWest ? p.Y : p.Y + i, p.Z);
var canFit = SpellHelper.AdjustField(ref targetLoc, Caster.Map, 22, true);
// Effects.SendLocationParticles( EffectItem.Create( loc, Caster.Map, EffectItem.DefaultDuration ), 0x376A, 9, 10, 5025 );
@ -46,7 +48,7 @@ namespace Server.Spells.Third
continue;
}
Item item = new InternalItem(loc, Caster.Map, Caster);
Item item = new InternalItem(targetLoc, Caster.Map, Caster);
Effects.SendLocationParticles(item, 0x376A, 9, 10, 5025);