ModernUO/Projects/UOContent/Mobiles/Monsters/ML/Humanoid/Magic/MLDryad.cs
Kamron Batman 582e1877b8
feat(core): Makes spanwriter resizable (#376)
SpanWriter now has an argument that allows it to be resizable.
```cs
public SpanWriter(Span<byte> initialBuffer, bool resize = false)
public SpanWriter(int initialCapacity, bool resize = false)
```

If a SpanWriter is set to be resizable, or given an initial capacity instead of a buffer, it must be disposed:
```cs
public static void SomeMethod()
{
    using var writer = new SpanWriter(stackalloc byte[512], true);
    // stuff
    
    // Automatic Dispose of writer
}
```

This is because the SpanWriter uses `ArrayPool<byte>.Shared` _rented buffers_ to resize. If the writer is not disposed, those buffers will never be reused resulting in a _memory leak_.

It is possible that the SpanWriter will outright ditch the initial buffer if resize is set to true and growing is needed. To check/account for that we can do the following:

```cs
public static void SomeMethod()
{
    Span<byte> span = stackalloc byte[512];
    using var writer = new SpanWriter(span, true);
    // write some stuff that causes the span to grow
    
    span = writer.RawBuffer;
    // Do stuff with the span
}
```

Make sure you don't accidentally `Dispose()` the writer or use the `RawBuffer` outside of the `using` block. If you do, bad things will happen! (NullPointerException, or a fresh SpanWriter with no buffer, depending on the situation).

SpanWriter can also now be used with a fixed statement since it has a `PinnableReference()` function.
2020-12-31 19:57:02 -08:00

160 lines
4.7 KiB
C#

using System;
using Server.Engines.Plants;
namespace Server.Mobiles
{
public class MLDryad : BaseCreature
{
private DateTime m_NextPeace;
private DateTime m_NextUndress;
[Constructible]
public MLDryad() : base(AIType.AI_Mage, FightMode.Evil, 10, 1, 0.2, 0.4)
{
Body = 266;
BaseSoundID = 0x57B;
SetStr(132, 149);
SetDex(152, 168);
SetInt(251, 280);
SetHits(304, 321);
SetDamage(11, 20);
SetDamageType(ResistanceType.Physical, 100);
SetResistance(ResistanceType.Physical, 40, 50);
SetResistance(ResistanceType.Fire, 15, 25);
SetResistance(ResistanceType.Cold, 40, 45);
SetResistance(ResistanceType.Poison, 30, 40);
SetResistance(ResistanceType.Energy, 25, 35);
SetSkill(SkillName.Meditation, 80.0, 90.0);
SetSkill(SkillName.EvalInt, 70.0, 80.0);
SetSkill(SkillName.Magery, 70.0, 80.0);
SetSkill(SkillName.Anatomy, 0);
SetSkill(SkillName.MagicResist, 100.0, 120.0);
SetSkill(SkillName.Tactics, 70.0, 80.0);
SetSkill(SkillName.Wrestling, 70.0, 80.0);
Fame = 5000;
Karma = 5000;
VirtualArmor = 28; // Don't know what it should be
if (Core.ML && Utility.RandomDouble() < .60)
{
PackItem(Seed.RandomPeculiarSeed(1));
}
PackArcanceScroll(0.05);
}
public MLDryad(Serial serial) : base(serial)
{
}
public override string CorpseName => "a dryad's corpse";
public override bool InitialInnocent => true;
public override OppositionGroup OppositionGroup => OppositionGroup.FeyAndUndead;
public override string DefaultName => "a dryad";
public override int Meat => 1;
public override void GenerateLoot()
{
AddLoot(LootPack.MlRich);
}
public override void OnThink()
{
base.OnThink();
AreaPeace();
AreaUndress();
}
public override void Serialize(IGenericWriter writer)
{
base.Serialize(writer);
writer.Write(0); // version
}
public override void Deserialize(IGenericReader reader)
{
base.Deserialize(reader);
var version = reader.ReadInt();
}
public void AreaPeace()
{
if (Combatant == null || Deleted || !Alive || m_NextPeace > DateTime.UtcNow || Utility.RandomDouble() > 0.1)
{
return;
}
var duration = TimeSpan.FromSeconds(Utility.RandomMinMax(20, 80));
foreach (var m in GetMobilesInRange(RangePerception))
{
if (m is PlayerMobile pm && IsValidTarget(pm))
{
pm.PeacedUntil = DateTime.UtcNow + duration;
m.SendLocalizedMessage(1072065); // You gaze upon the dryad's beauty, and forget to continue battling!
m.FixedParticles(0x376A, 1, 20, 0x7F5, EffectLayer.Waist);
m.Combatant = null;
}
}
m_NextPeace = DateTime.UtcNow + TimeSpan.FromSeconds(10);
PlaySound(0x1D3);
}
public bool IsValidTarget(PlayerMobile m) =>
m?.PeacedUntil < DateTime.UtcNow && !m.Hidden && m.AccessLevel == AccessLevel.Player &&
CanBeHarmful(m);
public void AreaUndress()
{
if (Combatant == null || Deleted || !Alive || m_NextUndress > DateTime.UtcNow || Utility.RandomDouble() > 0.005)
{
return;
}
foreach (var m in GetMobilesInRange(RangePerception))
{
if (m?.Player == true && !m.Female && !m.Hidden && m.AccessLevel == AccessLevel.Player &&
CanBeHarmful(m))
{
UndressItem(m, Layer.OuterTorso);
UndressItem(m, Layer.InnerTorso);
UndressItem(m, Layer.MiddleTorso);
UndressItem(m, Layer.Pants);
UndressItem(m, Layer.Shirt);
m.SendLocalizedMessage(
1072197
); // The dryad's beauty makes your blood race. Your clothing is too confining.
}
}
m_NextUndress = DateTime.UtcNow + TimeSpan.FromMinutes(1);
}
public void UndressItem(Mobile m, Layer layer)
{
var item = m.FindItemOnLayer(layer);
if (item?.Movable == true)
{
m.PlaceInBackpack(item);
}
}
}
}