////////////////

        public static void Implant(Chest chest, ChestImplanterItemDefinition info)
        {
            int addedAmount = ChestImplanter.GetImplantQuantity(info);

            if (addedAmount == 0)
            {
                return;
            }

            int itemType = info.ChestItem.Type;

            if (itemType == 0)
            {
                LogHelpers.Alert("Invalid item key " + info.ChestItem);
                return;
            }

            // Add or remove quantity of item, according to implanter spec
            if (addedAmount > 0)
            {
                ChestImplanter.PrependItemToChest(chest, itemType, addedAmount, info);
            }
            else if (addedAmount < 0)
            {
                ChestImplanter.ExtractItemFromChest(chest, itemType, -addedAmount);
            }
            else
            {
                throw new ModHelpersException("Invalid quantity.");
            }
        }
        ////

        private static void ApplyRandomImplantsSetToChest(
            Chest chest,
            string currentChestType,
            ChestImplanterSetDefinition setDef)
        {
            ChestImplanterDefinition implantDef = ChestImplanter.GetRandomImplanterFromSet(setDef);

            if (implantDef == null)
            {
                return;
            }

            if (ChestImplantsConfig.Instance.DebugModeInfo)
            {
                LogHelpers.Log(
                    "ApplyConfiguredImplantsToChest RAND "
                    + "'" + chest.GetHashCode() + " " + currentChestType + "' at " + chest.x + "," + chest.y
                    //+ " - Set total: " + setDef.Value.Count + " - Items of set's pick: " + implantDef?.ItemDefinitions.Count
                    + " - " + string.Join(", ", implantDef?.ItemDefinitions.Select(
                                              itemDef => itemDef.ChestItem.ToString() + " (" + (int)(itemDef.ChancePerChest * 100f) + "%)"
                                              ))
                    );
            }

            ChestImplanter.ApplyImplantToChest(chest, implantDef, currentChestType);
        }
예제 #3
0
        ////////////////

        public override void Apply(GenerationProgress progress)
        {
            for (int i = 0; i < Main.chest.Length; i++)
            {
                Chest chest = Main.chest[i];
                if (chest == null || ChestImplanter.IsChestEmpty(chest))
                {
                    continue;
                }

                ChestImplanter.ApplyAllImplantsToChest(chest);

                progress.Value = (float)i / (float)Main.chest.Length;
            }
        }
        public static void PrependItemToChest(Chest chest, int itemType, int amount, ChestImplanterItemDefinition info)
        {
            ChestImplanter.PrependItemToChest(chest, itemType, amount, info.Prefix);

            if (ChestImplantsConfig.Instance.DebugModeInfo)
            {
                Tile mytile = Main.tile[chest.x, chest.y];

                string chestName;
                if (!TileFrameHelpers.VanillaChestTypeNamesByFrame.TryGetValue(mytile.frameX / 36, out chestName))
                {
                    chestName = "Unknown (modded?) chest";
                }

                LogHelpers.Log(
                    " Implanted " + chestName + " (" + chest.x + ", " + chest.y + ") with " + amount + " " + info.ChestItem.ToString());
            }
        }
        ////////////////

        public static void ApplyAllImplantsToChest(Chest chest)
        {
            var mymod  = ChestImplantsMod.Instance;
            var config = ChestImplantsConfig.Instance;

            // Get chest tile and name
            Tile   chestTile = Main.tile[chest.x, chest.y];
            string chestName;

            if (!TileFrameHelpers.VanillaChestTypeNamesByFrame.TryGetValue(chestTile.frameX / 36, out chestName))
            {
                throw new ModHelpersException("Could not find chest frame");
            }
//LogHelpers.Log("chest "+i+" pos:"+mychest.x+","+mychest.y+", frame:"+(mytile.frameX/36)+", wall:"+mytile.wall+" "+(mychest.item[0]!=null?mychest.item[0].Name:"..."));

            // Apply random implants
            foreach (ChestImplanterSetDefinition setDef in config.GetRandomImplanterSets())
            {
                ChestImplanter.ApplyRandomImplantsSetToChest(chest, chestName, setDef);
            }

            string propName         = nameof(ChestImplantsConfig.AllFromSetChestImplanterDefinitions);
            var    chestImplantDefs = config.Get <ChestImplanterSetDefinition>(propName);

            // Apply guaranteed implants
            foreach (Ref <ChestImplanterDefinition> implantDef in chestImplantDefs.Value)
            {
                ChestImplanter.ApplyImplantToChest(chest, implantDef.Value, chestName);
            }

            foreach ((string name, CustomChestImplanter customStuffer) in mymod.CustomImplanter)
            {
                if (ChestImplantsConfig.Instance.DebugModeInfo)
                {
                    LogHelpers.Log(
                        "ApplyAllImplantsToChest CUSTOM "
                        + "'" + chest.GetHashCode() + " " + chestName + "'"
                        + "- " + name + " at " + chest.x + "," + chest.y
                        );
                }

                customStuffer.Invoke(chestName, chest);
            }
        }
        private static void ApplyImplantToChest(Chest chest, ChestImplanterDefinition implantDef, string currentChestType)
        {
            Tile chestTile = Main.tile[chest.x, chest.y];

            // Check if chest type is recognized by the given implanter
            bool isMatched = false;

            foreach (Ref <string> checkChestType in implantDef.ChestTypes)
            {
                if (ChestImplanter.IsChestMatch(currentChestType, checkChestType.Value))
                {
                    isMatched = true;
                    break;
                }
            }

            // Guess not?
            if (!isMatched)
            {
                return;
            }

            // Implant each item (item-implanter willing)
            foreach (ChestImplanterItemDefinition itemImplantDef in implantDef.ItemDefinitions)
            {
                bool canImplant = ChestImplanter.CanChestAcceptImplantItem(chestTile, itemImplantDef);

                if (ChestImplantsConfig.Instance.DebugModeVerboseInfo)
                {
                    LogHelpers.Log(" ApplyImplantToChest "
                                   + chest.GetHashCode() + currentChestType
                                   + " - " + itemImplantDef.ToCustomString()
                                   + " - " + canImplant
                                   + " - " + ChestImplanter.GetImplantQuantity(itemImplantDef)
                                   );
                }

                if (canImplant)
                {
                    ChestImplanter.Implant(chest, itemImplantDef);
                }
            }
        }