示例#1
0
        public ProtoMsg ProtoToData <T>(T protoMsg)
        {
            Type       type = typeof(T);
            IProtoItem item = protoListSender.GetItemByType(type);

            if (item == null)
            {
                Loger.LogTag("Proto", "ProtoToData " + protoMsg + "没找到对应的协议");
                return(default(ProtoMsg));
            }


            MemoryStream stream = new MemoryStream();

            ProtoBuf.Serializer.Serialize <T>(stream, protoMsg);
            stream.Position = 0;

            ProtoMsg msg = new ProtoMsg();

            msg.protoId = item.opcode;
            msg.bytes   = stream.ToArray();

            stream.Dispose();

            return(msg);
        }
示例#2
0
        public static void ServerTrySpawnItemToCharacterOrGround(
            ICharacter character,
            IProtoItem protoItem,
            uint countToSpawn,
            ref IItemsContainer groundContainer)
        {
            var result = character.ProtoCharacter.ServerCreateItem(character, protoItem, countToSpawn);

            if (result.IsEverythingCreated)
            {
                // successfully spawned at character
                return;
            }

            countToSpawn -= result.TotalCreatedCount;

            // cannot spawn - try spawn to the ground
            if (groundContainer is null)
            {
                groundContainer = ObjectGroundItemsContainer.ServerTryGetOrCreateGroundContainerAtTileOrNeighbors(
                    character,
                    character.Tile);

                if (groundContainer is null)
                {
                    return;
                }
            }

            ServerItemsService.CreateItem(protoItem,
                                          groundContainer,
                                          count: countToSpawn);
        }
示例#3
0
 public void TryAdd(IProtoItem protoItem, double count)
 {
     if (protoItem is IProtoItemFood protoItemFood)
     {
         this.Add(protoItemFood, count);
     }
 }
        public static void OnFishCaughtOrFishingCancelled(
            ICharacter character,
            [CanBeNull] IProtoItem protoItemFishCaught,
            Vector2D fishingTargetPosition,
            float caughtFishSizeValue)
        {
            ClientTimersSystem.AddAction(
                FishingCompletedAnimationDelay,
                () =>
            {
                ComponentFishingVisualizer.TryGetFor(character, out var fishingVisualizer);
                if (fishingVisualizer is null)
                {
                    return;
                }

                if (fishingVisualizer.FishingCompletedVisualizer is null)
                {
                    Api.Client.Scene.CreateSceneObject(nameof(ComponentFishingCompletedVisualizer))
                    .AddComponent <ComponentFishingCompletedVisualizer>()
                    .Setup(fishingTargetPosition,
                           character,
                           protoItemFishCaught,
                           caughtFishSizeValue);
                }
                else if (protoItemFishCaught is not null)
                {
                    fishingVisualizer.FishingCompletedVisualizer
                    .SetupFish(protoItemFishCaught, caughtFishSizeValue);
                }
            });
        }
示例#5
0
 public static HUDItemNotificationControl Create(IProtoItem protoItem, int deltaCount)
 {
     return(new()
     {
         viewModel = new ViewModelHUDItemNotificationControl(protoItem, deltaCount)
     });
 }
示例#6
0
        public string Execute(IProtoItem protoItem)
        {
            using var tempListContainers = Api.Shared.GetTempList <IItemsContainer>();
            using var tempListItems      = Api.Shared.GetTempList <IItem>();
            var   containers          = tempListContainers.AsList();
            var   items               = tempListItems.AsList();
            ulong totalDestroyedCount = 0;

            foreach (var protoItemsContainer in Api.FindProtoEntities <IProtoItemsContainer>())
            {
                containers.Clear();
                protoItemsContainer.GetAllGameObjects(containers);

                foreach (var container in containers)
                {
                    items.Clear();
                    items.AddRange(container.Items);

                    foreach (var item in items)
                    {
                        if (item.ProtoItem != protoItem)
                        {
                            continue;
                        }

                        totalDestroyedCount += item.Count;
                        Server.Items.DestroyItem(item);
                    }
                }
            }

            return($"Destroyed {totalDestroyedCount} item(s).");
        }
示例#7
0
        private static void ClientShowItemNotification(IProtoItem protoItem, int deltaCount)
        {
            Api.ValidateIsClient();
            var notificationControl = HUDItemsNotificationsPanelControl.Show(protoItem, deltaCount);

            Api.SafeInvoke(() => ClientItemNotificationDisplayed?.Invoke(notificationControl));
        }
        private static bool SharedTryFindItemsOfType(
            IItemsContainerProvider containers,
            IProtoItem requiredProtoItem,
            uint count,
            out List <IItem> result)
        {
            var countToFindRemains = (int)count;

            result = new List <IItem>();
            foreach (var container in containers.ItemsContainers)
            {
                foreach (var item in container.Items)
                {
                    if (item.ProtoItem != requiredProtoItem)
                    {
                        continue;
                    }

                    result.Add(item);
                    countToFindRemains -= item.Count;
                    if (countToFindRemains <= 0)
                    {
                        break;
                    }
                }

                if (countToFindRemains <= 0)
                {
                    break;
                }
            }

            return(countToFindRemains <= 0);
        }
示例#9
0
        public override byte?FindSlotForItem(IItemsContainer container, IProtoItem protoItem)
        {
            var allowedSlotsIds = this.GetAllowedSlotsIds(protoItem);

            if (allowedSlotsIds is null)
            {
                return(null);
            }

            if (allowedSlotsIds.Length == 1)
            {
                // return only one appropriate slot
                return(allowedSlotsIds[0]);
            }

            // this equipment type allows placing to multiple slots
            // find an empty one of them
            foreach (var allowedSlotsId in allowedSlotsIds)
            {
                if (!container.IsSlotOccupied(allowedSlotsId))
                {
                    // empty slot found
                    return(allowedSlotsId);
                }
            }

            // no empty slots found, return first allowed slot id
            return(allowedSlotsIds[0]);
        }
 protected override void PrepareSystem()
 {
     if (IsServer)
     {
         protoItemFoodRotten = Api.GetProtoEntity <ItemFoodRotten>();
     }
 }
示例#11
0
        private static void CreateItem(
            ICharacter player,
            IProtoItem item,
            uint count    = 0,
            bool atHotbar = false)
        {
            if (atHotbar)
            {
                // try to add at hotbar
                var result = Server.Items.CreateItem(protoItem: item,
                                                     container: player.SharedGetPlayerContainerHotbar(),
                                                     count: count == 0 ? (uint)item.MaxItemsPerStack : count);
                if (result.IsEverythingCreated)
                {
                    return;
                }

                result.Rollback();
            }

            Server.Items.CreateItem(item,
                                    player,
                                    count: count == 0
                                               ? item.MaxItemsPerStack
                                               : count);
        }
        public static void ClientShowNotificationNotEnoughEnergyCharge(IProtoItem itemProto)
        {
            var playNotificationSound = true;

            if (itemProto is IProtoItemWeapon protoItemWeapon &&
                protoItemWeapon.SoundPresetWeapon.HasSound(WeaponSound.Empty))
            {
                protoItemWeapon.SoundPresetWeapon.PlaySound(WeaponSound.Empty,
                                                            volume: SoundConstants.VolumeWeapon);
                playNotificationSound = false;
            }

            if (playNotificationSound)
            {
                var itemSoundPreset = itemProto.SharedGetItemSoundPreset();
                if (itemSoundPreset.HasSound(ItemSound.CannotSelect))
                {
                    itemSoundPreset.PlaySound(ItemSound.CannotSelect);
                    playNotificationSound = false;
                }
            }

            var hasPowerBanks = ClientCalculateTotalEnergyCapacity() > 0;

            NotificationSystem.ClientShowNotification(
                string.Format(NotificationCannotUse_Title, itemProto.Name),
                hasPowerBanks
                    ? NotificationCannotUse_NotEnoughCharge
                    : NotificationCannotUse_NoPowerBanksEquipped,
                NotificationColor.Bad,
                itemProto.Icon,
                playSound: playNotificationSound);
        }
示例#13
0
        private static IGrouping <IProtoItemAmmo, IItem> SharedFindNextAmmoGroup(
            List <IGrouping <IProtoItemAmmo, IItem> > compatibleAmmoGroups,
            IProtoItem currentProtoItemAmmo)
        {
            var foundProtoItemIndex = -1;

            for (var index = 0; index < compatibleAmmoGroups.Count; index++)
            {
                var compatibleAmmoItem = compatibleAmmoGroups[index];
                if (compatibleAmmoItem.Key == currentProtoItemAmmo)
                {
                    // found current proto item, select next item prototype
                    foundProtoItemIndex = index;
                    break;
                }
            }

            if (foundProtoItemIndex < 0)
            {
                // current proto item ammo is not found
                return(compatibleAmmoGroups.FirstOrDefault());
            }

            // select next proto ammo group
            if (foundProtoItemIndex + 1 < compatibleAmmoGroups.Count)
            {
                return(compatibleAmmoGroups[foundProtoItemIndex + 1]);
            }

            return(null);
        }
示例#14
0
        public override byte?FindSlotForItem(IItemsContainer container, IProtoItem protoItem)
        {
            if (protoItem is not IProtoItemEquipment protoEquipment)
            {
                // not an equipment - cannot be placed here
                return(null);
            }

            var allowedSlotsIds = protoEquipment.CompatibleContainerSlotsIds;

            if (allowedSlotsIds.Length == 1)
            {
                // return only one appropriate slot
                return(allowedSlotsIds[0]);
            }

            // this equipment type allows placing to multiple slots
            // find an empty one of them
            foreach (var allowedSlotsId in allowedSlotsIds)
            {
                if (!container.IsSlotOccupied(allowedSlotsId))
                {
                    // empty slot found
                    return(allowedSlotsId);
                }
            }

            // no empty slots found, return first allowed slot id
            return(allowedSlotsIds[0]);
        }
示例#15
0
        private void ServerRemote_SetTradingLot(
            IStaticWorldObject tradingStation,
            byte lotIndex,
            IProtoItem protoItem,
            ushort lotQuantity,
            ushort priceCoinPenny,
            ushort priceCoinShiny)
        {
            var character = ServerRemoteContext.Character;

            ValidateCanAdminAndInteract(character, tradingStation);

            var publicState = GetPublicState(tradingStation);
            var lot         = publicState.Lots[lotIndex];

            lot.ProtoItem = protoItem;
            lot.SetLotQuantity(lotQuantity);
            lot.SetPrices(priceCoinPenny, priceCoinShiny);
            // the state will be set automatically during the refresh
            lot.State = TradingStationLotState.Available;

            Logger.Important($"Successfully modified trading lot #{lotIndex} on {tradingStation}", character);
            ServerRefreshTradingStationLots(tradingStation,
                                            GetPrivateState(tradingStation),
                                            publicState);
        }
示例#16
0
        public static bool ServerIsWateringRequired(
            IWorldObject objectPlant,
            ICharacter character,
            IProtoItem protoItem,
            IProtoObjectPlant protoPlant,
            double proposedWateringDuration)
        {
            var plantPrivateState = objectPlant.GetPrivateState <PlantPrivateState>();

            if (plantPrivateState.ServerTimeWateringEnds >= double.MaxValue ||
                (proposedWateringDuration < double.MaxValue &&
                 (plantPrivateState.ServerTimeWateringEnds
                  >= Server.Game.FrameTime + proposedWateringDuration - 60)))
            {
                // the plant is already watered enough
                Instance.CallClient(character, _ => _.ClientRemote_CannotWaterAlreadyWatered(protoItem));
                return(false);
            }

            if (!protoPlant.ServerCanBeWatered((IStaticWorldObject)objectPlant))
            {
                // no need to water the plant
                Instance.CallClient(character, _ => _.ClientRemote_CannotWaterLastHarvestOrRotten(protoItem));
                return(false);
            }

            return(true);
        }
示例#17
0
 private void ClientRemote_CannotWaterAlreadyWatered(IProtoItem protoItem)
 {
     NotificationSystem.ClientShowNotification(
         NotificationCannotWater_Title,
         NotificationAlreadyWatered,
         NotificationColor.Bad,
         protoItem.Icon);
 }
示例#18
0
 private void ClientRemote_CannotWaterLastHarvestOrRotten(IProtoItem protoItem)
 {
     NotificationSystem.ClientShowNotification(
         NotificationCannotWater_Title,
         NotificationCannotWater_MessageHarvestReady,
         NotificationColor.Bad,
         protoItem.Icon);
 }
示例#19
0
 private void ClientRemote_CannotWaterSpoiled(IProtoItem protoItem)
 {
     NotificationSystem.ClientShowNotification(
         NotificationCannotWater_Title,
         CoreStrings.NotificationPlantSpoiled_Message,
         NotificationColor.Bad,
         protoItem.Icon);
 }
示例#20
0
 public override CreateItemResult ServerCreateItem(
     ICharacter character,
     IProtoItem protoItem,
     uint countToSpawn = 1)
 {
     // no containers - cannot create items
     throw new NotImplementedException();
 }
示例#21
0
 public static void ServerSendNotificationNoSpaceInInventoryItemsDroppedToGround(
     ICharacter character,
     [CanBeNull] IProtoItem protoItemForIcon)
 {
     Instance.CallClient(
         character,
         _ => _.ClientRemote_ShowNotificationNoSpaceInInventoryItemsDroppedToGround(protoItemForIcon));
 }
示例#22
0
 public static void ClientShowNotificationNoSpaceInInventoryItemsDroppedToGround(
     [CanBeNull] IProtoItem protoItemForIcon)
 {
     ClientShowNotification(title: NotificationNoFreeSpace,
                            message: NotificationSomeItemsDropped,
                            color: NotificationColor.Bad,
                            icon: protoItemForIcon?.Icon);
 }
示例#23
0
 public DropItem(
     IProtoItem protoItem,
     ushort count,
     ushort countRandom)
     : base(protoItem,
            count)
 {
     this.CountRandom = countRandom;
 }
示例#24
0
 /// <summary>
 /// Add item to the output items list.
 /// </summary>
 /// <param name="protoItem">Item prototype instance.</param>
 /// <param name="count">Minimal amount to spawn (if spawning/adding occurs).</param>
 /// <param name="countRandom">
 /// Additional amount to spawn (value selected randomly from 0 to the specified max value
 /// (inclusive) when spawning).
 /// </param>
 /// <param name="probability">Probability of spawn/add at all.</param>
 public OutputItems Add(
     IProtoItem protoItem,
     ushort count       = 1,
     ushort countRandom = 0,
     double probability = 1)
 {
     this.items.Add(new OutputItem(protoItem, count, countRandom, probability));
     return(this);
 }
 public DataEntryRequiredItemProto(
     IProtoItem protoItem,
     [CanBeNull] TechNode techNode,
     bool hasNextEntry)
 {
     this.protoItem    = protoItem;
     this.techNode     = techNode;
     this.hasNextEntry = hasNextEntry;
 }
示例#26
0
 protected override void SetupRecipe(
     StationsList stations,
     out TimeSpan duration,
     InputItems inputItems,
     OutputItems outputItems)
 {
     this.SetupRecipe(out duration, out var protoItemFuel, outputItems);
     Api.Assert(protoItemFuel is not null, "Crafting byproduct recipe requires proto item fuel.");
     this.ProtoItemFuel = protoItemFuel;
 }
示例#27
0
        private void ClientRemote_ItemRepairedCompletely(IProtoItem protoItem)
        {
            var skill = Api.GetProtoEntity <SkillMaintenance>();

            NotificationSystem.ClientShowNotification(
                Notification_PerfectRepair_Title,
                string.Format(Notification_PerfectRepair_Format, protoItem.Name),
                NotificationColor.Good,
                skill.Icon);
        }
示例#28
0
            static void SortItems(IProtoItem protoItem, List <IItem> items)
            {
                if (protoItem is not IProtoItemWithFreshness protoItemWithFreshness ||
                    protoItemWithFreshness.FreshnessMaxValue == 0)
                {
                    return;
                }

                // sort by freshness
                items.Sort(ItemFreshnessSystem.SharedCompareFreshnessReverse);
            }
示例#29
0
        private void ClientRemote_ItemBroke(IProtoItem protoItem)
        {
            NotificationSystem.ClientShowNotification(
                NotificationItemBroke_Title,
                string.Format(NotificationItemBroke_MessageFormat, protoItem.Name),
                color: NotificationColor.Bad,
                icon: protoItem.Icon,
                playSound: false);

            protoItem.SharedGetItemSoundPreset().PlaySound(ItemSound.Broken);
        }
        public Recipe.RecipeForManufacturingByproduct MatchRecipeForByproduct(IProtoItem protoItemFuel)
        {
            foreach (var recipe in this.RecipesForByproducts)
            {
                if (recipe.CanBeCrafted(protoItemFuel))
                {
                    return(recipe);
                }
            }

            return(null);
        }