Example #1
0
        private void MoveContainerToSlot()
        {
            IItem addedItem;
            var   updateItem = Thing as IItem;

            if (FromContainer == null || updateItem == null || Requestor == null)
            {
                return;
            }

            // attempt to remove from the source container
            if (!FromContainer.RemoveContent(updateItem.Type.TypeId, FromIndex, Count, out addedItem))
            {
                return;
            }

            if (addedItem != null)
            {
                updateItem = addedItem;
            }

            IThing currentThing = null;

            // attempt to place the intended item at the slot.
            if (!Requestor.Inventory.Add(updateItem, out addedItem, ToSlot, Count))
            {
                // Something went wrong, add back to the source container...
                if (FromContainer.AddContent(updateItem, 0xFF))
                {
                    return;
                }

                // and we somehow failed to re-add it to the source container...
                // throw to the ground.
                currentThing = updateItem;
            }
            else
            {
                // added the new item to the slot
                if (addedItem == null)
                {
                    return;
                }

                // we exchanged or got some leftover item, place back in the source container at any index.
                if (FromContainer.AddContent(addedItem, 0xFF))
                {
                    return;
                }

                // and we somehow failed to re-add it to the source container...
                // throw to the ground.
                currentThing = addedItem;
            }

            Requestor.Tile.AddThing(ref currentThing, currentThing.Count);

            // notify all spectator players of that tile.
            Game.Instance.NotifySpectatingPlayers(conn => new TileUpdatedNotification(conn, Requestor.Location, Game.Instance.GetMapTileDescription(conn.PlayerId, Requestor.Location)), Requestor.Location);
        }
Example #2
0
        private void MoveBetweenContainers()
        {
            IItem extraItem;
            var   updatedItem = Thing as IItem;

            if (FromContainer == null || ToContainer == null || updatedItem == null || Requestor == null)
            {
                return;
            }

            // attempt to remove from the source container
            if (!FromContainer.RemoveContent(updatedItem.Type.TypeId, FromIndex, Count, out extraItem))
            {
                return;
            }

            if (extraItem != null)
            {
                updatedItem = extraItem;
            }

            // successfully removed thing from the source container
            // attempt to add to the dest container
            if (ToContainer.AddContent(updatedItem, ToIndex))
            {
                return;
            }

            // failed to add to the dest container (whole or partial)
            // attempt to add again to the source at any index.
            if (FromContainer.AddContent(updatedItem, 0xFF))
            {
                return;
            }

            // and we somehow failed to re-add it to the source container...
            // throw to the ground.
            IThing thing = updatedItem;

            Requestor.Tile.AddThing(ref thing, updatedItem.Count);

            // notify all spectator players of that tile.
            Game.Instance.NotifySpectatingPlayers(conn => new TileUpdatedNotification(conn, Requestor.Location, Game.Instance.GetMapTileDescription(conn.PlayerId, Requestor.Location)), Requestor.Location);

            // and handle collision.
            if (!Requestor.Tile.HandlesCollision)
            {
                return;
            }

            foreach (var itemWithCollision in Requestor.Tile.ItemsWithCollision)
            {
                var collisionEvents = Game.Instance.EventsCatalog[ItemEventType.Collision].Cast <CollisionItemEvent>();

                var candidate = collisionEvents.FirstOrDefault(e => e.ThingIdOfCollision == itemWithCollision.Type.TypeId && e.Setup(itemWithCollision, thing) && e.CanBeExecuted);

                // Execute all actions.
                candidate?.Execute();
            }
        }
Example #3
0
        /// <inheritdoc/>
        public bool Evaluate()
        {
            if (ItemToCheck == null || FromContainer == null)
            {
                return(false);
            }

            return(FromContainer.CountContentAmountAt(FromIndex, ItemToCheck.Type.TypeId) >= Count);
        }
        private void MoveContainerToGround()
        {
            IItem extraItem;
            var   itemToUpdate = Thing as IItem;

            if (FromContainer == null || ToTile == null || itemToUpdate == null)
            {
                return;
            }

            // attempt to remove from the source container
            if (!FromContainer.RemoveContent(itemToUpdate.Type.TypeId, FromIndex, Count, out extraItem))
            {
                return;
            }

            if (extraItem != null)
            {
                itemToUpdate = extraItem;
            }

            // add the remaining item to the destination tile.
            IThing thing = itemToUpdate;

            ToTile.AddThing(ref thing, itemToUpdate.Count);

            // notify all spectator players of that tile.
            Game.Instance.NotifySpectatingPlayers(conn => new TileUpdatedNotification(conn, ToTile.Location, Game.Instance.GetMapTileDescription(conn.PlayerId, ToTile.Location)), ToTile.Location);

            // and handle collision.
            if (!ToTile.HandlesCollision)
            {
                return;
            }

            foreach (var itemWithCollision in ToTile.ItemsWithCollision)
            {
                var collisionEvents = Game.Instance.EventsCatalog[ItemEventType.Collision].Cast <CollisionItemEvent>();

                var candidate = collisionEvents.FirstOrDefault(e => e.ThingIdOfCollision == itemWithCollision.Type.TypeId && e.Setup(itemWithCollision, thing, Requestor as IPlayer) && e.CanBeExecuted);

                // Execute all actions.
                candidate?.Execute();
            }
        }