Example #1
0
        /// <summary>
        /// Handles the contents of a network message.
        /// </summary>
        /// <param name="message">The message to handle.</param>
        /// <param name="connection">A reference to the connection from where this message is comming from, for context.</param>
        /// <returns>A collection of <see cref="IResponsePacket"/>s that compose that synchronous response, if any.</returns>
        public override IEnumerable <IResponsePacket> HandleRequest(INetworkMessage message, IConnection connection)
        {
            var itemUseOnInfo = message.ReadItemUseOnInfo();

            if (!(this.Context.CreatureFinder.FindCreatureById(connection.PlayerId) is IPlayer player))
            {
                return(null);
            }

            // Before actually using the item, check if we're close enough to use it.
            if (itemUseOnInfo.FromLocation.Type == LocationType.Map)
            {
                // Turn to the item if it's not exactly the location.
                if (player.Location != itemUseOnInfo.FromLocation)
                {
                    var directionToThing = player.Location.DirectionTo(itemUseOnInfo.FromLocation);

                    this.ScheduleNewOperation(
                        this.Context.OperationFactory.Create(
                            OperationType.Turn,
                            new TurnToDirectionOperationCreationArguments(player, directionToThing)));
                }

                var locationDiff = itemUseOnInfo.FromLocation - player.Location;

                if (locationDiff.Z != 0)
                {
                    // it's on a different floor...
                    return(new TextMessagePacket(MessageType.StatusSmall, "There is no way.").YieldSingleItem());
                }
            }

            return(this.UseItemAtOn(player, itemUseOnInfo.FromItemClientId, itemUseOnInfo.FromLocation, itemUseOnInfo.FromIndex, itemUseOnInfo.ToItemClientId, itemUseOnInfo.ToLocation, itemUseOnInfo.ToIndex));
        }
Example #2
0
        /// <summary>
        /// Handles the contents of a network message.
        /// </summary>
        /// <param name="message">The message to handle.</param>
        /// <param name="connection">A reference to the connection from where this message is comming from, for context.</param>
        public override void HandleRequest(INetworkMessage message, IConnection connection)
        {
            var useOnInfo = message.ReadItemUseOnInfo();

            if (!(this.Game.GetCreatureWithId(connection.PlayerId) is IPlayer player))
            {
                return;
            }

            player.ClearPendingActions();

            // Before actually using the item, check if we're close enough to use it.
            if (useOnInfo.FromLocation.Type == LocationType.Ground)
            {
                var locationDiff = useOnInfo.FromLocation - player.Location;

                if (locationDiff.Z != 0) // it's on a different floor...
                {
                    this.ResponsePackets.Add(new TextMessagePacket(MessageType.StatusSmall, "There is no way."));

                    return;
                }

                if (locationDiff.MaxValueIn2D > 1)
                {
                    // Too far away to use it.
                    var directions = this.Game.Pathfind(player.Location, useOnInfo.FromLocation, out Location retryLoc).ToArray();

                    player.SetPendingAction(new UseItemOnPlayerAction(player, useOnInfo, retryLoc));

                    if (directions.Length > 0)
                    {
                        player.AutoWalk(directions);
                    }
                    else // we found no way...
                    {
                        this.ResponsePackets.Add(new TextMessagePacket(MessageType.StatusSmall, "There is no way."));
                    }

                    return;
                }
            }

            new UseItemOnPlayerAction(player, useOnInfo, useOnInfo.FromLocation).Perform();
        }