예제 #1
0
 public override void ForceAdd(MabiItem item)
 {
     _items.Add(item);
     item.Move(this.Pocket, 0, 0);
 }
예제 #2
0
        public override bool TryAdd(MabiItem newItem, byte targetX, byte targetY, out MabiItem collidingItem)
        {
            collidingItem = null;

            if (targetX + newItem.DataInfo.Width > _width || targetY + newItem.DataInfo.Height > _height)
                return false;

            this.TryGetCollidingItem(targetX, targetY, newItem, out collidingItem);

            if (collidingItem != null && ((collidingItem.StackType == BundleType.Sac && (collidingItem.StackItem == newItem.Info.Class || collidingItem.StackItem == newItem.StackItem)) || (newItem.StackType == BundleType.Stackable && newItem.Info.Class == collidingItem.Info.Class)))
            {
                if (collidingItem.Info.Amount < collidingItem.StackMax)
                {
                    var diff = (ushort)(collidingItem.StackMax - collidingItem.Info.Amount);

                    collidingItem.Info.Amount += Math.Min(diff, newItem.Info.Amount);
                    newItem.Info.Amount -= Math.Min(diff, newItem.Info.Amount);

                    return true;
                }
            }

            if (collidingItem != null)
            {
                _items.Remove(collidingItem.Id);
                collidingItem.Move(newItem.Pocket, newItem.Info.X, newItem.Info.Y);
                this.ClearFromMap(collidingItem);
            }

            _items.Add(newItem.Id, newItem);
            newItem.Move(this.Pocket, targetX, targetY);
            this.AddToMap(newItem);

            return true;
        }
예제 #3
0
        public override bool TryAdd(MabiItem item, byte targetX, byte targetY, out MabiItem collidingItem)
        {
            collidingItem = null;
            if (_item != null)
            {
                collidingItem = _item;
                collidingItem.Move(item.Pocket, item.Info.X, item.Info.Y);
            }

            _item = item;
            _item.Move(this.Pocket, 0, 0);

            return true;
        }
예제 #4
0
        /// <summary>
        /// Adds one or multiple items with the given id to the creature's
        /// inventory. Tries to fill sacs first, inventory afterwards, and
        /// all remaining will be added to the temp inventory.
        /// </summary>
        /// <param name="itemClass"></param>
        /// <param name="amount"></param>
        public MabiItem GiveItem(uint itemClass, uint amount, uint color1 = 0, uint color2 = 0, uint color3 = 0, bool useDBColors = true, bool drop = false)
        {
            MabiItem result = null;

            // Fill stacks and sacs
            foreach (var item in this.Items)
            {
                if ((item.Type == ItemType.Sac && item.StackItem == itemClass) || (item.Info.Class == itemClass && item.StackType == BundleType.Stackable))
                {
                    if (item.Info.Amount >= item.StackMax)
                        continue;

                    var prev = item.Info.Amount;
                    var diff = item.StackMax - item.Info.Amount;
                    if (diff >= amount)
                    {
                        item.Info.Amount += (ushort)amount;
                        amount = 0;
                    }
                    else
                    {
                        item.Info.Amount = item.StackMax;
                        amount -= (uint)diff;
                    }

                    if (prev != item.Info.Amount)
                    {
                        this.ItemUpdate(item);
                        result = item;
                    }
                }
            }

            // Add remaining to inv or temp inv.
            while (amount > 0)
            {
                var item = new MabiItem(itemClass);
                if (!useDBColors)
                {
                    item.Info.ColorA = color1;
                    item.Info.ColorB = color2;
                    item.Info.ColorC = color3;
                }
                var max = Math.Max((ushort)1, item.StackMax); // This way, we can't drag the server into an infinate loop
                if (amount <= max)
                {
                    item.Info.Amount = (ushort)amount;
                    amount = 0;
                }
                else
                {
                    item.Info.Amount = max;
                    amount -= max;
                }

                if (drop)
                {
                    var pos = this.GetPosition();
                    var rand = RandomProvider.Get();
                    var x = (uint)(pos.X + rand.Next(-100, 101));
                    var y = (uint)(pos.Y + rand.Next(-100, 101)); WorldManager.Instance.DropItem(item, this.Region, x, y);
                    EventManager.CreatureEvents.OnCreatureDropItem(this, item);
                }
                else
                {
                    var pocket = Pocket.Inventory;
                    var space = this.GetFreeItemSpace(item, pocket);
                    if (space == null)
                    {
                        pocket = Pocket.Temporary;
                        space = new MabiVertex(0, 0);
                    }

                    item.Move(pocket, space.X, space.Y);
                    this.Items.Add(item);

                    this.ItemUpdate(item, true);
                }

                result = item;
            }

            EventManager.CreatureEvents.OnCreatureItemAction(this, itemClass);

            return result;
        }