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; surface = id.Surface;
impassable = id.Impassable; 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) z + height > item.Z)
return false; return false;
@ -1148,7 +1148,7 @@ namespace Server
var end = dest; 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; var swap = org;
org = dest; org = dest;

View file

@ -849,23 +849,80 @@ namespace Server
return total + bonus; return total + bonus;
} }
public static void Shuffle<T>(IList<T> list) public static void Shuffle<T>(this IList<T> list)
{ {
var count = list.Count; 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]; var swap = list[r];
list[r] = list[i]; list[r] = list[i];
list[i] = swap; 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)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static T RandomList<T>(params T[] list) => list.RandomElement(); public static T RandomList<T>(params T[] list) => list.RandomElement();
[MethodImpl(MethodImplOptions.AggressiveInlining)] [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)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool RandomBool() => RandomSources.Source.NextBool(); public static bool RandomBool() => RandomSources.Source.NextBool();

View file

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

View file

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

View file

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

View file

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

View file

@ -1,5 +1,4 @@
using System; using System;
using System.Linq;
using Server.Mobiles; using Server.Mobiles;
namespace Server.Engines.Quests.Collector namespace Server.Engines.Quests.Collector
@ -56,8 +55,6 @@ namespace Server.Engines.Quests.Collector
new ImageTypeInfo(9746, typeof(Juggernaut), 55, 38) new ImageTypeInfo(9746, typeof(Juggernaut), 55, 38)
}; };
private static ImageType[] m_ImageTypeList;
public ImageTypeInfo(int figurine, Type type, int x, int y) public ImageTypeInfo(int figurine, Type type, int x, int y)
{ {
Figurine = figurine; Figurine = figurine;
@ -66,6 +63,8 @@ namespace Server.Engines.Quests.Collector
Y = y; Y = y;
} }
public ImageType Image { get; }
public int Figurine { get; } public int Figurine { get; }
public Type Type { get; } public Type Type { get; }
@ -82,16 +81,21 @@ namespace Server.Engines.Quests.Collector
public static ImageType[] RandomList(int count) public static ImageType[] RandomList(int count)
{ {
if (m_ImageTypeList == null) if (count <= 0) return Array.Empty<ImageType>();
{
m_ImageTypeList = new ImageType[m_Table.Length];
for (int i = 0; i < m_Table.Length; i++)
m_ImageTypeList[i] = (ImageType)i;
}
ImageType[] array = m_ImageTypeList.ToArray(); var length = m_Table.Length;
Utility.Shuffle(array); Span<bool> list = stackalloc bool[length];
return array.Take(count).ToArray(); 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] [Constructible]
public HiddenFigure() => Message = Utility.RandomList(Messages); public HiddenFigure() => Message = Messages.RandomElement();
public HiddenFigure(Serial serial) : base(serial) public HiddenFigure(Serial serial) : base(serial)
{ {
@ -83,4 +83,4 @@ namespace Server.Engines.Quests.Ninja
Message = reader.ReadInt(); Message = reader.ReadInt();
} }
} }
} }

View file

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

View file

@ -17,10 +17,10 @@ namespace Server.Items
private string m_Name; private string m_Name;
[Constructible] [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; m_Name = name;
Hue = Utility.RandomList(m_Hues); Hue = m_Hues.RandomElement();
Fill(level); Fill(level);
} }
@ -207,4 +207,4 @@ namespace Server.Items
m_Name = Utility.Intern(reader.ReadString()); m_Name = Utility.Intern(reader.ReadString());
} }
} }
} }

View file

@ -11,7 +11,7 @@ namespace Server.Items
}; };
[Constructible] [Constructible]
public ShimmeringCrystals() : base(Utility.RandomList(m_ItemIDs)) public ShimmeringCrystals() : base(m_ItemIDs.RandomElement())
{ {
} }
@ -36,4 +36,4 @@ namespace Server.Items
int version = reader.ReadInt(); int version = reader.ReadInt();
} }
} }
} }

View file

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

View file

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

View file

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

View file

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

View file

@ -903,7 +903,7 @@ namespace Server.Items
0x2F58, 0x2F59, 0x2F5A, 0x2F5B 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 = private static readonly Type[] m_Summons =
{ {

View file

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

View file

@ -248,14 +248,7 @@ namespace Server.Mobiles
if (toGive.Count == 0) if (toGive.Count == 0)
return; return;
// Randomize toGive.Shuffle();
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;
}
for (int i = 0; i < 16; ++i) for (int i = 0; i < 16; ++i)
{ {