public override bool TryAdd(Item newItem, byte targetX, byte targetY, out Item collidingItem)
{
collidingItem = null;
if (targetX + newItem.Data.Width > _width || targetY + newItem.Data.Height > _height)
return false;
var collidingItems = this.GetCollidingItems(targetX, targetY, newItem);
if (collidingItems.Count > 1)
return false;
if (collidingItems.Count > 0)
collidingItem = collidingItems[0];
if (collidingItem != null && (
// Colliding item is sac and new item can fill be put into it
(collidingItem.Data.StackType == StackType.Sac && collidingItem.Data.StackItemId != 0 && (collidingItem.Data.StackItemId == newItem.Info.Id || collidingItem.Data.StackItemId == newItem.Data.StackItemId)) ||
// Colliding item is a quiver (general arrow sac) that
// a regular arrow can be put into.
// Corner case, due to quiver being a sac without stack item id,
// instead of a stackable for some reason. They possibly wanted
// to be able to put different kinds of arrows into it,
// otherwise they probably would have made it stackable or
// specified a stack item id.
(collidingItem.HasTag("/largearrowsac/") && newItem.HasTag("/arrow_bag_bundle/")) ||
// Item is stackable and can be put into the colliding stack
(newItem.Data.StackType == StackType.Stackable && newItem.Info.Id == collidingItem.Info.Id))
)
{
if (collidingItem.Info.Amount < collidingItem.Data.StackMax)
{
var diff = (ushort)(collidingItem.Data.StackMax - collidingItem.Info.Amount);
var amount = collidingItem.Info.Amount;
collidingItem.Info.Amount += Math.Min(diff, newItem.Info.Amount);
newItem.Info.Amount -= Math.Min(diff, newItem.Info.Amount);
if (amount != collidingItem.Info.Amount)
return true;
}
}
if (collidingItem != null)
{
_items.Remove(collidingItem.EntityId);
collidingItem.Move(newItem.Info.Pocket, newItem.Info.X, newItem.Info.Y);
this.ClearFromMap(collidingItem);
}
_items.Add(newItem.EntityId, newItem);
newItem.Move(this.Pocket, targetX, targetY);
this.AddToMap(newItem);
return true;
}