Example #1
0
        private void PacketHandler_0x07_PickupItem(Object obj, ClientPacket packet)
        {
            var user = (User) obj;
            var slot = packet.ReadByte();
            var x = packet.ReadInt16();
            var y = packet.ReadInt16();

            //var user = client.User;
            //var map = user.Map;

            // Is the player within PICKUP_DISTANCE tiles of what they're trying to pick up?
            if (Math.Abs(x - user.X) > Constants.PICKUP_DISTANCE ||
                Math.Abs(y - user.Y) > Constants.PICKUP_DISTANCE)
                return;

            // Check if inventory slot is valid and empty
            if (slot == 0 || slot > user.Inventory.Size || user.Inventory[slot] != null)
                return;

            // Find the items that are at the pickup area

            var tile = new Rectangle(x, y, 1, 1);

            // We don't want to pick up people
            var pickupObject = user.Map.EntityTree.GetObjects(tile).FindLast(i => i is Gold || i is Item);

            // If the add is successful, remove the item from the map quadtree
            if (pickupObject is Gold)
            {
                var gold = (Gold) pickupObject;
                if (user.AddGold(gold))
                {
                    Logger.DebugFormat("Removing {0}, qty {1} from {2}@{3},{4}",
                        gold.Name, gold.Amount, user.Map.Name, x, y);
                    user.Map.RemoveGold(gold);
                }
            }
            else if (pickupObject is Item)
            {
                var item = (Item) pickupObject;
                if (item.Stackable && user.Inventory.Contains(item.TemplateId))
                {
                    byte existingSlot = user.Inventory.SlotOf(item.TemplateId);
                    var existingItem = user.Inventory[existingSlot];

                    int maxCanGive = existingItem.MaximumStack - existingItem.Count;
                    int quantity = Math.Min(item.Count, maxCanGive);

                    item.Count -= quantity;
                    existingItem.Count += quantity;

                    Logger.DebugFormat("Removing {0}, qty {1} from {2}@{3},{4}",
                        item.Name, item.Count, user.Map.Name, x, y);
                    user.Map.Remove(item);
                    user.SendItemUpdate(existingItem, existingSlot);

                    if (item.Count == 0) Remove(item);
                    else
                    {
                        user.Map.Insert(item, user.X, user.Y);
                        user.SendMessage(string.Format("You can't carry any more {0}.", item.Name), 3);
                    }
                }
                else
                {
                    Logger.DebugFormat("Removing {0}, qty {1} from {2}@{3},{4}",
                        item.Name, item.Count, user.Map.Name, x, y);
                    user.Map.Remove(item);
                    user.AddItem(item, slot);
                }
            }
        }
Example #2
0
        private void PacketHandler_0x08_DropItem(Object obj, ClientPacket packet)
        {
            var user = (User) obj;
            var slot = packet.ReadByte();
            var x = packet.ReadInt16();
            var y = packet.ReadInt16();
            var count = packet.ReadInt32();

            Logger.DebugFormat("{0} {1} {2} {3}", slot, x, y, count);

            // Do a few sanity checks
            //
            // Is the distance valid? (Can't drop things beyond
            // MAXIMUM_DROP_DISTANCE tiles away)
            if (Math.Abs(x - user.X) > Constants.PICKUP_DISTANCE ||
                Math.Abs(y - user.Y) > Constants.PICKUP_DISTANCE)
            {
                Logger.ErrorFormat("Request to drop item exceeds maximum distance {0}",
                    Hybrasyl.Constants.MAXIMUM_DROP_DISTANCE);
                return;
            }

            // Is this a valid slot?
            if ((slot == 0) || (slot > Hybrasyl.Constants.MAXIMUM_INVENTORY))
            {
                Logger.ErrorFormat("Slot not valid. Aborting");
                return;
            }

            // Does the player actually have an item in the slot? Does the count in the packet exceed the 
            // count in the player's inventory?  Are they trying to drop the item on something that 
            // is impassable (i.e. a wall)?
            if ((user.Inventory[slot] == null) || (count > user.Inventory[slot].Count) ||
                (user.Map.IsWall[x, y] == true) || !user.Map.IsValidPoint(x, y))
            {
                Logger.ErrorFormat(
                    "Slot {0} is null, or count {1} exceeds count {2}, or {3},{4} is a wall, or {3},{4} is out of bounds",
                    slot, count, user.Inventory[slot].Count, x, y);
                return;
            }

            Item toDrop = user.Inventory[slot];

            if (toDrop.Stackable && count < toDrop.Count)
            {
                toDrop.Count -= count;
                user.SendItemUpdate(toDrop, slot);

                toDrop = new Item(toDrop);
                toDrop.Count = count;
                Insert(toDrop);
            }
            else
            {
                user.RemoveItem(slot);
            }

            // Are we dropping an item onto a reactor?
            Reactor reactor;
            var coordinates = new Tuple<byte, byte>((byte) x, (byte) y);
            if (user.Map.Reactors.TryGetValue(coordinates, out reactor))
            {
                reactor.OnDrop(user, toDrop);
            }
            else
                user.Map.AddItem(x, y, toDrop);
        }
Example #3
0
        private void PacketHandler_0x24_DropGold(Object obj, ClientPacket packet)
        {
            var user = (User) obj;
            var amount = packet.ReadUInt32();
            var x = packet.ReadInt16();
            var y = packet.ReadInt16();

            Logger.DebugFormat("{0} {1} {2}", amount, x, y);
            // Do a few sanity checks

            // Is the distance valid? (Can't drop things beyond
            // MAXIMUM_DROP_DISTANCE tiles away)
            if (Math.Abs(x - user.X) > Constants.PICKUP_DISTANCE ||
                Math.Abs(y - user.Y) > Constants.PICKUP_DISTANCE)
            {
                Logger.ErrorFormat("Request to drop gold exceeds maximum distance {0}",
                    Hybrasyl.Constants.MAXIMUM_DROP_DISTANCE);
                return;
            }

            // Does the amount in the packet exceed the 
            // amount of gold the player has?  Are they trying to drop the item on something that 
            // is impassable (i.e. a wall)?
            if ((amount > user.Gold) || (x >= user.Map.X) || (y >= user.Map.Y) ||
                (x < 0) || (y < 0) || (user.Map.IsWall[x, y]))
            {
                Logger.ErrorFormat("Amount {0} exceeds amount {1}, or {2},{3} is a wall, or {2},{3} is out of bounds",
                    amount, user.Gold, x, y);
                return;
            }

            var toDrop = new Gold(amount);
            user.RemoveGold(amount);

            Insert(toDrop);

            // Are we dropping an item onto a reactor?
            Reactor reactor;
            var coordinates = new Tuple<byte, byte>((byte)x, (byte)y);
            if (user.Map.Reactors.TryGetValue(coordinates, out reactor))
            {
                reactor.OnDrop(user, toDrop);
            }
            else
                user.Map.AddGold(x, y, toDrop);
        }