Ejemplo n.º 1
0
        public static CreateItemResult TryDropToCharacter(
            IReadOnlyDropItemsList dropItemsList,
            ICharacter character,
            bool sendNoFreeSpaceNotification,
            double probabilityMultiplier,
            DropItemContext context)
        {
            if (character == null)
            {
                return(new CreateItemResult()
                {
                    IsEverythingCreated = false
                });
            }

            var itemsService = Api.Server.Items;

            var result = dropItemsList.Execute(
                (protoItem, count) => itemsService.CreateItem(character, protoItem, count),
                context,
                probabilityMultiplier);

            if (sendNoFreeSpaceNotification &&
                !result.IsEverythingCreated)
            {
                NotificationSystem.ServerSendNotificationNoSpaceInInventory(character);
            }

            return(result);
        }
Ejemplo n.º 2
0
        private static void ExecuteEntry(
            Entry entry,
            DropItemContext dropItemContext,
            out CreateItemResult createItemResult,
            double probabilityMultiplier,
            DelegateSpawnDropItem delegateSpawnDropItem)
        {
            if (entry.Condition != null)
            {
                try
                {
                    if (!entry.Condition(dropItemContext))
                    {
                        // condition not match
                        createItemResult = null;
                        return;
                    }
                }
                catch (Exception ex)
                {
                    Api.Logger.Exception(ex, "Exception during checking condition for droplist item " + entry);
                    createItemResult = null;
                    return;
                }
            }

            probabilityMultiplier *= entry.Probability;
            createItemResult       = entry.EntryNestedList != null
                                   ? entry.EntryNestedList.Execute(delegateSpawnDropItem,
                                                                   dropItemContext,
                                                                   probabilityMultiplier)
                                   : ExecuteSpawnItem(entry.EntryItem,
                                                      delegateSpawnDropItem,
                                                      probabilityMultiplier);
        }
Ejemplo n.º 3
0
        public static CreateItemResult TryDropToGround(
            IReadOnlyDropItemsList dropItemsList,
            Vector2Ushort tilePosition,
            double probabilityMultiplier,
            DropItemContext context,
            [CanBeNull] out IItemsContainer groundContainer)
        {
            // obtain the ground container to drop the items into
            var tile = Api.Server.World.GetTile(tilePosition);

            groundContainer = ObjectGroundItemsContainer
                              .ServerTryGetOrCreateGroundContainerAtTileOrNeighbors(tile);

            if (groundContainer == null)
            {
                // cannot drop because there are no free space available on the ground
                return(new CreateItemResult()
                {
                    IsEverythingCreated = false
                });
            }

            var result = dropItemsList.TryDropToContainer(groundContainer, context, probabilityMultiplier);

            if (result.TotalCreatedCount == 0 &&
                groundContainer.OccupiedSlotsCount == 0)
            {
                // nothing is spawned, the ground container should be destroyed
                Api.Server.World.DestroyObject(groundContainer.OwnerAsStaticObject);
                groundContainer = null;
            }

            return(result);
        }
Ejemplo n.º 4
0
        public CreateItemResult Execute(
            DelegateSpawnDropItem delegateSpawnDropItem,
            DropItemContext dropItemContext,
            double probabilityMultiplier)
        {
            this.Freeze();

            var result = new CreateItemResult()
            {
                IsEverythingCreated = true
            };

            using var selectedEntries = Api.Shared.GetTempList <Entry>();
            this.SelectRandomEntries(selectedEntries);

            // execute selected entries
            foreach (var entry in selectedEntries)
            {
                ExecuteEntry(entry,
                             dropItemContext,
                             out var entryResult,
                             probabilityMultiplier,
                             delegateSpawnDropItem);
                result.MergeWith(entryResult);
            }

            return(result);
        }
Ejemplo n.º 5
0
 public CreateItemResult TryDropToContainer(
     IItemsContainer container,
     DropItemContext context,
     double probabilityMultiplier = 1.0)
 {
     return(ServerDroplistHelper.TryDropToContainer(
                this,
                container,
                probabilityMultiplier,
                context));
 }
Ejemplo n.º 6
0
 public CreateItemResult TryDropToGround(
     Vector2Ushort tilePosition,
     DropItemContext context,
     double probabilityMultiplier = 1.0)
 {
     return(ServerDroplistHelper.TryDropToGround(
                this,
                tilePosition,
                probabilityMultiplier,
                context));
 }
Ejemplo n.º 7
0
 public CreateItemResult TryDropToGround(
     Vector2Ushort tilePosition,
     DropItemContext context,
     [CanBeNull] out IItemsContainer groundContainer,
     double probabilityMultiplier = 1.0)
 {
     return(ServerDroplistHelper.TryDropToGround(
                this,
                tilePosition,
                probabilityMultiplier,
                context,
                out groundContainer));
 }
Ejemplo n.º 8
0
 public CreateItemResult TryDropToCharacter(
     ICharacter character,
     DropItemContext context,
     bool sendNoFreeSpaceNotification = true,
     double probabilityMultiplier     = 1.0)
 {
     return(ServerDroplistHelper.TryDropToCharacter(
                this,
                character,
                sendNoFreeSpaceNotification,
                probabilityMultiplier,
                context));
 }
Ejemplo n.º 9
0
        public static CreateItemResult TryDropToContainer(
            IReadOnlyDropItemsList dropItemsList,
            IItemsContainer toContainer,
            double probabilityMultiplier,
            DropItemContext context)
        {
            var result = dropItemsList.Execute(
                (protoItem, count) => Items.CreateItem(protoItem, toContainer, count),
                context,
                probabilityMultiplier);

            return(result);
        }
Ejemplo n.º 10
0
        public static CreateItemResult TryDropToCharacterOrGround(
            IReadOnlyDropItemsList dropItemsList,
            ICharacter toCharacter,
            Vector2Ushort tilePosition,
            bool sendNotificationWhenDropToGround,
            double probabilityMultiplier,
            DropItemContext context,
            out IItemsContainer groundContainer)
        {
            var containersProvider = new CharacterAndGroundContainersProvider(toCharacter, tilePosition);

            var result = dropItemsList.Execute(
                (protoItem, count) => Items.CreateItem(protoItem, containersProvider, count),
                context,
                probabilityMultiplier);

            groundContainer = containersProvider.GroundContainer;
            if (groundContainer is null)
            {
                return(result);
            }

            if (groundContainer.OccupiedSlotsCount == 0)
            {
                // nothing is spawned, the ground container should be destroyed
                World.DestroyObject(groundContainer.OwnerAsStaticObject);
                groundContainer = null;
            }
            else
            {
                if (sendNotificationWhenDropToGround && result.TotalCreatedCount > 0)
                {
                    // notify player that there were not enough space in inventory so the items were dropped to the ground
                    NotificationSystem.ServerSendNotificationNoSpaceInInventoryItemsDroppedToGround(
                        toCharacter,
                        protoItemForIcon: result.ItemAmounts
                        .Keys
                        .FirstOrDefault(
                            k => k.Container == containersProvider.GroundContainer)?
                        .ProtoItem);
                }

                WorldObjectClaimSystem.ServerTryClaim(groundContainer.OwnerAsStaticObject,
                                                      toCharacter,
                                                      WorldObjectClaimDuration.GroundItems);
            }

            return(result);
        }
Ejemplo n.º 11
0
 public CreateItemResult TryDropToCharacterOrGround(
     ICharacter character,
     Vector2Ushort tilePosition,
     DropItemContext context,
     bool sendNotificationWhenDropToGround = true,
     double probabilityMultiplier          = 1.0)
 {
     return(ServerDroplistHelper.TryDropToCharacterOrGround(
                this,
                character,
                tilePosition,
                sendNotificationWhenDropToGround,
                probabilityMultiplier,
                context));
 }
Ejemplo n.º 12
0
        public static CreateItemResult TryDropToCharacterOrGround(
            IReadOnlyDropItemsList dropItemsList,
            ICharacter toCharacter,
            Vector2Ushort tilePosition,
            bool sendNotificationWhenDropToGround,
            double probabilityMultiplier,
            DropItemContext context,
            out IItemsContainer groundContainer)
        {
            CreateItemResult result;

            if (toCharacter != null)
            {
                result = TryDropToCharacter(
                    dropItemsList,
                    toCharacter,
                    sendNoFreeSpaceNotification: false,
                    probabilityMultiplier,
                    context);
                if (result.IsEverythingCreated)
                {
                    groundContainer = null;
                    return(result);
                }

                // cannot add to character - rollback and drop on ground instead
                result.Rollback();
            }

            result = TryDropToGround(dropItemsList,
                                     tilePosition,
                                     probabilityMultiplier,
                                     context,
                                     out groundContainer);
            if (sendNotificationWhenDropToGround &&
                result.TotalCreatedCount > 0)
            {
                // notify player that there were not enough space in inventory so the items were dropped to the ground
                NotificationSystem.ServerSendNotificationNoSpaceInInventoryItemsDroppedToGround(
                    toCharacter,
                    result.ItemAmounts.FirstOrDefault().Key?.ProtoItem);
            }

            return(result);
        }
Ejemplo n.º 13
0
        private void SelectRandomEntries(ITempList <Entry> result, DropItemContext dropItemContext)
        {
            var countToSelect = this.Outputs + RandomHelper.Next(0, this.OutputsRandom + 1);

            if (countToSelect >= this.frozenEntries.Count)
            {
                // simply return all the entries
                foreach (var entry in this.entries)
                {
                    result.Add(entry.Value);
                }

                return;
            }

            if (countToSelect == 1)
            {
                // simply return a single random selected entry that is satisfying the condition
                Entry entry = default;
                for (var attempt = 0; attempt < 100; attempt++)
                {
                    entry = this.frozenEntries.GetSingleRandomElement();
                    if (entry.Condition is null)
                    {
                        break;
                    }

                    try
                    {
                        if (entry.Condition(dropItemContext))
                        {
                            break;
                        }

                        // condition not match, select another entry
                        continue;
                    }
                    catch (Exception ex)
                    {
                        Api.Logger.Exception(ex, "Exception during checking condition for droplist item " + entry);
                        break;
                    }
                }

                if (!entry.Equals(default))