fix: Use pooled ref for corpse instanced loot (#1645)

This commit is contained in:
Kamron Batman 2023-12-26 10:51:58 -08:00 • committed by GitHub
parent 449dba3ac7
commit 96c3f2eb91
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,6 +1,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using ModernUO.Serialization; using ModernUO.Serialization;
using Server.Collections;
using Server.ContextMenus; using Server.ContextMenus;
using Server.Engines.PartySystem; using Server.Engines.PartySystem;
using Server.Engines.Quests.Doom; using Server.Engines.Quests.Doom;
@ -371,8 +372,8 @@ public partial class Corpse : Container, ICarvable
_instancedItems ??= new Dictionary<Item, InstancedItemInfo>(); _instancedItems ??= new Dictionary<Item, InstancedItemInfo>();
var m_Stackables = new List<Item>(); using var stackables = PooledRefQueue<Item>.Create();
var m_Unstackables = new List<Item>(); using var unstackables = PooledRefQueue<Item>.Create();
for (var i = 0; i < Items.Count; i++) for (var i = 0; i < Items.Count; i++)
{ {
@ -382,29 +383,23 @@ public partial class Corpse : Container, ICarvable
{ {
if (item.Stackable) if (item.Stackable)
{ {
m_Stackables.Add(item); stackables.Enqueue(item);
} }
else else
{ {
m_Unstackables.Add(item); unstackables.Enqueue(item);
} }
} }
} }
var attackers = new List<Mobile>(_aggressors); using var attackers = PooledRefList<Mobile>.Create(_aggressors.Count);
attackers.AddRange(_aggressors);
for (var i = 1; i < attackers.Count - 1; i++) // randomize attackers.Shuffle();
{
var rand = Utility.Random(i + 1);
(attackers[rand], attackers[i]) = (attackers[i], attackers[rand]);
}
// stackables first, for the remaining stackables, have those be randomly added after // stackables first, for the remaining stackables, have those be randomly added after
while (stackables.Count > 0)
for (var i = 0; i < m_Stackables.Count; i++)
{ {
var item = m_Stackables[i]; var item = stackables.Dequeue();
if (item.Amount >= attackers.Count) if (item.Amount >= attackers.Count)
{ {
@ -426,20 +421,21 @@ public partial class Corpse : Container, ICarvable
} }
else else
{ {
m_Unstackables.Add(item); unstackables.Enqueue(item);
} }
} }
else else
{ {
// What happens in this case? TEMP FOR NOW UNTIL OSI VERIFICATION: Treat as Single Item. // What happens in this case? TEMP FOR NOW UNTIL OSI VERIFICATION: Treat as Single Item.
m_Unstackables.Add(item); unstackables.Enqueue(item);
} }
} }
for (var i = 0; i < m_Unstackables.Count; i++) var index = 0;
while (unstackables.Count > 0)
{ {
var m = attackers[i % attackers.Count]; var m = attackers[index++ % attackers.Count];
var item = m_Unstackables[i]; var item = unstackables.Dequeue();
_instancedItems.Add(item, new InstancedItemInfo(item, m)); _instancedItems.Add(item, new InstancedItemInfo(item, m));
} }