Updates rest of the random list (#197)

This commit is contained in:
Kamron Batman 2020-08-22 00:23:51 -07:00 committed by GitHub
parent 4403d6c1c7
commit 8caf5a422f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
18 changed files with 116 additions and 79 deletions

View file

@ -1077,7 +1077,7 @@ namespace Server
surface = id.Surface;
impassable = id.Impassable;
if ((surface || impassable || (checkBlocksFit && item.BlocksFit)) && item.Z + id.CalcHeight > z &&
if ((surface || impassable || checkBlocksFit && item.BlocksFit) && item.Z + id.CalcHeight > z &&
z + height > item.Z)
return false;
@ -1148,7 +1148,7 @@ namespace Server
var end = dest;
if (org.X > dest.X || (org.X == dest.X && org.Y > dest.Y) || (org.X == dest.X && org.Y == dest.Y && org.Z > dest.Z))
if (org.X > dest.X || org.X == dest.X && org.Y > dest.Y || org.X == dest.X && org.Y == dest.Y && org.Z > dest.Z)
{
var swap = org;
org = dest;

View file

@ -849,23 +849,80 @@ namespace Server
return total + bonus;
}
public static void Shuffle<T>(IList<T> list)
public static void Shuffle<T>(this IList<T> list)
{
var count = list.Count;
for (var i = count - 1; i > 0; i--)
for (var i = 0; i < count; i++)
{
var r = RandomSources.Source.Next(count);
var r = RandomMinMax(i, count - 1);
var swap = list[r];
list[r] = list[i];
list[i] = swap;
}
}
public static void Shuffle<T>(this Span<T> list)
{
var count = list.Length;
for (var i = 0; i < count; i++)
{
var r = RandomMinMax(i, count - 1);
var swap = list[r];
list[r] = list[i];
list[i] = swap;
}
}
/**
* Gets a random sample from the source list.
* Not meant for unbounded lists. Does not shuffle or modify source.
*/
public static T[] RandomSample<T>(this T[] source, int count)
{
if (count <= 0) return Array.Empty<T>();
var length = source.Length;
Span<bool> list = stackalloc bool[length];
var sampleList = new T[count];
int i = 0;
do
{
var rand = Random(length);
if (!(list[rand] && (list[rand] = true)))
sampleList[i++] = source[rand];
} while (i < count);
return sampleList;
}
public static List<T> RandomSample<T>(this List<T> source, int count)
{
if (count <= 0) return new List<T>();
var length = source.Count;
Span<bool> list = stackalloc bool[length];
var sampleList = new List<T>(count);
int i = 0;
do
{
var rand = Random(length);
if (!(list[rand] && (list[rand] = true)))
sampleList[i++] = source[rand];
} while (i < count);
return sampleList;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static T RandomList<T>(params T[] list) => list.RandomElement();
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static T RandomElement<T>(this IList<T> list) => list.Count == 0 ? default : list[Random(list.Count)];
public static T RandomElement<T>(this IList<T> list) => list.RandomElement(default);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static T RandomElement<T>(this IList<T> list, T valueIfZero) => list.Count == 0 ? valueIfZero : list[Random(list.Count)];
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool RandomBool() => RandomSources.Source.NextBool();

View file

@ -346,34 +346,26 @@ namespace Server.Engines.Doom
ResetLevers();
}
public virtual void GenKey() /* Shuffle & build key */
public virtual void GenKey()
{
ushort[] CA = { 1, 2, 4, 8 };
for (int i = 0; i < 4; i++)
{
int n = (n = Utility.Random(0, 3)) == i ? n & ~i : n;
ushort tmp = CA[i];
CA[i] = CA[n];
CA[n] = tmp;
}
Span<ushort> ca = stackalloc ushort[]{ 1, 2, 4, 8 };
ca.Shuffle();
for (int i = 0; i < 4; MyKey = (ushort)(CA[i++] | (MyKey <<= 4)))
{
}
for (int i = 0; i < 4; i++) MyKey = (ushort)(ca[i] | (MyKey <<= 4));
}
private static bool IsValidDamagable(Mobile m) =>
m?.Deleted == false &&
((m.Player && m.Alive) ||
(m is BaseCreature bc && (bc.Controlled || bc.Summoned) && !bc.IsDeadBondedPet));
(m.Player && m.Alive ||
m is BaseCreature bc && (bc.Controlled || bc.Summoned) && !bc.IsDeadBondedPet);
public static void MoveMobileOut(Mobile m)
{
if (m != null)
{
if (m is PlayerMobile && !m.Alive)
if (m.Corpse?.Deleted == false)
m.Corpse.MoveToWorld(lr_Exit, Map.Malas);
if (m is PlayerMobile && !m.Alive && m.Corpse?.Deleted == false)
m.Corpse.MoveToWorld(lr_Exit, Map.Malas);
BaseCreature.TeleportPets(m, lr_Exit, Map.Malas);
m.Location = lr_Exit;
m.ProcessDelta();

View file

@ -335,8 +335,7 @@ namespace Server.Engines.Harvest
public virtual void DoHarvestingSound(Mobile from, Item tool, HarvestDefinition def, object toHarvest)
{
if (def.EffectSounds.Length > 0)
from.PlaySound(Utility.RandomList(def.EffectSounds));
from.PlaySound(def.EffectSounds.RandomElement(-1));
}
public virtual void DoHarvestingEffect(Mobile from, Item tool, HarvestDefinition def, Map map, Point3D loc)
@ -344,7 +343,7 @@ namespace Server.Engines.Harvest
from.Direction = from.GetDirectionTo(loc);
if (!from.Mounted)
from.Animate(Utility.RandomList(def.EffectActions), 5, 1, true, false, 0);
from.Animate(def.EffectActions.RandomElement(), 5, 1, true, false, 0);
}
public virtual HarvestDefinition GetDefinition() => Definitions.First();

View file

@ -22,7 +22,7 @@ namespace Server.Engines.Harvest
m_Definition = def;
m_ToHarvest = toHarvest;
m_Locked = locked;
m_Count = Utility.RandomList(def.EffectCounts);
m_Count = def.EffectCounts.RandomElement();
}
protected override void OnTick()

View file

@ -205,7 +205,7 @@ namespace Server.Engines.Harvest
0x1CE2, 0x1CEC // leg
};
preLoot = new ShipwreckedItem(Utility.RandomList(list));
preLoot = new ShipwreckedItem(list.RandomElement());
break;
}
case 1: // Bone parts
@ -217,7 +217,7 @@ namespace Server.Engines.Harvest
0x1B15, 0x1B16 // pelvis bones
};
preLoot = new ShipwreckedItem(Utility.RandomList(list));
preLoot = new ShipwreckedItem(list.RandomElement());
break;
}
case 2: // Paintings and portraits
@ -258,7 +258,7 @@ namespace Server.Engines.Harvest
if (Utility.Random(list.Length + 1) == 0)
preLoot = new Candelabra();
else
preLoot = new ShipwreckedItem(Utility.RandomList(list));
preLoot = new ShipwreckedItem(list.RandomElement());
break;
}

View file

@ -1,5 +1,4 @@
using System;
using System.Linq;
using Server.Mobiles;
namespace Server.Engines.Quests.Collector
@ -56,8 +55,6 @@ namespace Server.Engines.Quests.Collector
new ImageTypeInfo(9746, typeof(Juggernaut), 55, 38)
};
private static ImageType[] m_ImageTypeList;
public ImageTypeInfo(int figurine, Type type, int x, int y)
{
Figurine = figurine;
@ -66,6 +63,8 @@ namespace Server.Engines.Quests.Collector
Y = y;
}
public ImageType Image { get; }
public int Figurine { get; }
public Type Type { get; }
@ -82,16 +81,21 @@ namespace Server.Engines.Quests.Collector
public static ImageType[] RandomList(int count)
{
if (m_ImageTypeList == null)
{
m_ImageTypeList = new ImageType[m_Table.Length];
for (int i = 0; i < m_Table.Length; i++)
m_ImageTypeList[i] = (ImageType)i;
}
if (count <= 0) return Array.Empty<ImageType>();
ImageType[] array = m_ImageTypeList.ToArray();
Utility.Shuffle(array);
return array.Take(count).ToArray();
var length = m_Table.Length;
Span<bool> list = stackalloc bool[length];
var imageTypes = new ImageType[count];
int i = 0;
do
{
var rand = Utility.Random(length);
if (!(list[rand] && (list[rand] = true)))
imageTypes[i++] = (ImageType)rand;
} while (i < count);
return imageTypes;
}
}
}

View file

@ -13,7 +13,7 @@ namespace Server.Engines.Quests.Ninja
};
[Constructible]
public HiddenFigure() => Message = Utility.RandomList(Messages);
public HiddenFigure() => Message = Messages.RandomElement();
public HiddenFigure(Serial serial) : base(serial)
{

View file

@ -599,8 +599,7 @@ namespace Server.Items
Item item = Loot.Construct(m_Types);
if (item is Key key)
key.ItemID = Utility.RandomList((int)KeyType.Copper, (int)KeyType.Gold, (int)KeyType.Iron,
(int)KeyType.Rusty);
key.ItemID = Utility.RandomList((int)KeyType.Copper, (int)KeyType.Gold, (int)KeyType.Iron, (int)KeyType.Rusty);
else if (item is Arrow || item is Bolt)
item.Amount = Utility.RandomMinMax(2, 6);
else if (item is Bandage || item is Lockpick)

View file

@ -17,10 +17,10 @@ namespace Server.Items
private string m_Name;
[Constructible]
public ParagonChest(string name, int level) : base(Utility.RandomList(m_ItemIDs))
public ParagonChest(string name, int level) : base(m_ItemIDs.RandomElement())
{
m_Name = name;
Hue = Utility.RandomList(m_Hues);
Hue = m_Hues.RandomElement();
Fill(level);
}

View file

@ -11,7 +11,7 @@ namespace Server.Items
};
[Constructible]
public ShimmeringCrystals() : base(Utility.RandomList(m_ItemIDs))
public ShimmeringCrystals() : base(m_ItemIDs.RandomElement())
{
}

View file

@ -43,7 +43,7 @@ namespace Server.Items
Weight = 1.0;
if (Utility.RandomDouble() < 0.01)
Hue = Utility.RandomList(m_Hues);
Hue = m_Hues.RandomElement();
else
Hue = 0x8A0;
}

View file

@ -227,7 +227,7 @@ namespace Server.Items
public override void Initialize()
{
Hue = Utility.RandomList(m_Hues);
Hue = m_Hues.RandomElement();
AddComponent(new PlagueBeastComponent(0x3BB, Hue), 0, 0);
AddComponent(new PlagueBeastComponent(0x3BA, Hue), 4, 6);
@ -264,13 +264,13 @@ namespace Server.Items
Opened = true;
}
private static int RandomHue(int exculde)
private static int RandomHue(int exclude)
{
for (int i = 0; i < 20; i++)
{
int hue = Utility.RandomList(m_Hues);
int hue = m_Hues.RandomElement();
if (hue != exculde)
if (hue != exclude)
return hue;
}

View file

@ -17,7 +17,7 @@ namespace Server.Items
private bool m_IsRewardItem;
[Constructible]
public RewardBrazier() : this(Utility.RandomList(m_Art))
public RewardBrazier() : this(m_Art.RandomElement())
{
}

View file

@ -208,14 +208,13 @@ namespace Server.Items
return;
Effects.PlaySound(target, map, Utility.RandomList(0x11B, 0x11C, 0x11D));
Effects.SendLocationEffect(target, map, Utility.RandomList(m_Effects), 16, 1);
Effects.SendLocationEffect(target, map, m_Effects.RandomElement(), 16, 1);
for (int count = Utility.Random(3); count > 0; count--)
{
IPoint3D location = new Point3D(target.X + Utility.RandomMinMax(-1, 1),
target.Y + Utility.RandomMinMax(-1, 1), target.Z);
int effect = Utility.RandomList(m_Effects);
Effects.SendLocationEffect(location, map, effect, 16, 1);
Effects.SendLocationEffect(location, map, m_Effects.RandomElement(), 16, 1);
}
Charges -= 1;

View file

@ -903,7 +903,7 @@ namespace Server.Items
0x2F58, 0x2F59, 0x2F5A, 0x2F5B
};
public static int GetRandomItemID() => Utility.RandomList(m_ItemIDs);
public static int GetRandomItemID() => m_ItemIDs.RandomElement();
private static readonly Type[] m_Summons =
{

View file

@ -129,13 +129,7 @@ namespace Server.Mobiles
}
// Randomize
for (int i = 0; i < toGive.Count; ++i)
{
int rand = Utility.Random(toGive.Count);
Mobile hold = toGive[i];
toGive[i] = toGive[rand];
toGive[rand] = hold;
}
toGive.Shuffle();
for (int i = 0; i < 6; ++i)
{

View file

@ -248,14 +248,7 @@ namespace Server.Mobiles
if (toGive.Count == 0)
return;
// Randomize
for (int i = 0; i < toGive.Count; ++i)
{
int rand = Utility.Random(toGive.Count);
Mobile hold = toGive[i];
toGive[i] = toGive[rand];
toGive[rand] = hold;
}
toGive.Shuffle();
for (int i = 0; i < 16; ++i)
{