Example #1
0
        public override void ApplyToComponent <T>(T component)
        {
            var comp = component as Preserver;

            if (this.NullifyPerish != null)
            {
                comp.NullifyPerishing = (bool)this.NullifyPerish;
            }

            if (this.PreservedElements != null)
            {
                var list = new List <Preserver.PreservedElement>();

                foreach (var ele in this.PreservedElements)
                {
                    list.Add(new Preserver.PreservedElement
                    {
                        Preservation = ele.Preservation,
                        Tag          = new TagSourceSelector(CustomTags.GetTag(ele.PreservedItemTag)),
                    });
                }

                At.SetField(comp, "m_preservedElements", list);
            }
        }
Example #2
0
        public override void ApplyToComponent <T>(T component)
        {
            var tag = CustomItems.GetTag(Stat_Tag);

            if (tag == Tag.None)
            {
                SL.Log("AffectStat: could not find tag of ID " + (this.Stat_Tag ?? ""));
                return;
            }

            var comp = component as AffectStat;

            comp.AffectedStat = new TagSourceSelector(tag);
            comp.Value        = this.AffectQuantity;
            comp.IsModifier   = this.IsModifier;

            if (this.Tags != null)
            {
                comp.Tags = this.Tags
                            .Select(it => new TagSourceSelector(CustomTags.GetTag(it)))
                            .ToArray();
            }
        }
        public TrapEffectRecipe Apply()
        {
            var recipe = new TrapEffectRecipe();

            At.SetField(recipe, "m_name", this.Name);

            SetLocalization();

            if (this.CompatibleItemTags != null)
            {
                var list = new List <TagSourceSelector>();
                foreach (var tagName in this.CompatibleItemTags)
                {
                    if (CustomTags.GetTag(tagName) is Tag tag && tag != Tag.None)
                    {
                        list.Add(new TagSourceSelector(tag));
                    }
                }
                At.SetField(recipe, "m_compatibleTags", list.ToArray());
            }

            if (this.CompatibleItemIDs != null)
            {
                var list = new List <Item>();
                foreach (var itemID in this.CompatibleItemIDs)
                {
                    if (ResourcesPrefabManager.Instance.GetItemPrefab(itemID) is Item item)
                    {
                        list.Add(item);
                    }
                }
                At.SetField(recipe, "m_compatibleItems", list.ToArray());
            }

            if (this.TrapEffects != null && this.TrapEffects.Count > 0)
            {
                var prefab = recipe.TrapEffectsPrefab;
                if (!prefab)
                {
                    prefab = new GameObject($"{this.Name}_NormalEffects").transform;
                    GameObject.DontDestroyOnLoad(prefab.gameObject);
                    recipe.TrapEffectsPrefab = prefab;
                }
                UnityHelpers.DestroyChildren(prefab);

                foreach (var effect in this.TrapEffects)
                {
                    effect.ApplyToTransform(prefab);
                }
            }

            if (this.HiddenEffects != null && this.HiddenEffects.Count > 0)
            {
                var prefab = recipe.HiddenTrapEffectsPrefab;
                if (!prefab)
                {
                    prefab = new GameObject($"{this.Name}_HiddenEffects").transform;
                    GameObject.DontDestroyOnLoad(prefab.gameObject);
                    recipe.HiddenTrapEffectsPrefab = prefab;
                }
                UnityHelpers.DestroyChildren(prefab);

                foreach (var effect in this.HiddenEffects)
                {
                    effect.ApplyToTransform(prefab);
                }
            }

            return(recipe);
        }
Example #4
0
        internal void Internal_ApplyRecipe()
        {
            try
            {
                SL.Log("Defining recipe UID: " + this.UID);

                if (string.IsNullOrEmpty(this.UID))
                {
                    SL.LogWarning("No UID was set! Please set a UID, for example 'myname.myrecipe'. Aborting.");
                    return;
                }

                if (m_applied)
                {
                    SL.Log("Trying to apply an SL_Recipe that is already applied! This is not allowed.");
                    return;
                }

                var results = new List <ItemReferenceQuantity>();
                foreach (var result in this.Results)
                {
                    var resultItem = ResourcesPrefabManager.Instance.GetItemPrefab(result.ItemID);
                    if (!resultItem)
                    {
                        SL.Log("Error: Could not get recipe result id : " + result.ItemID);
                        return;
                    }
                    results.Add(new ItemReferenceQuantity(resultItem, result.Quantity));
                }

                var ingredients = new List <RecipeIngredient>();
                foreach (var ingredient in this.Ingredients)
                {
                    // legacy support
                    if (!string.IsNullOrEmpty(ingredient.Ingredient_Tag))
                    {
                        ingredient.SelectorValue = ingredient.Ingredient_Tag;
                    }
                    else if (ingredient.Ingredient_ItemID != 0)
                    {
                        ingredient.SelectorValue = ingredient.Ingredient_ItemID.ToString();
                    }

                    if (ingredient.Type == RecipeIngredient.ActionTypes.AddGenericIngredient)
                    {
                        var tag = CustomTags.GetTag(ingredient.SelectorValue);
                        if (tag == Tag.None)
                        {
                            SL.LogWarning("Could not get a tag by the name of '" + ingredient.SelectorValue);
                            return;
                        }

                        ingredients.Add(new RecipeIngredient
                        {
                            ActionType          = ingredient.Type,
                            AddedIngredientType = new TagSourceSelector(tag)
                        });
                    }
                    else
                    {
                        int.TryParse(ingredient.SelectorValue, out int id);
                        if (id == 0)
                        {
                            SL.LogWarning("Picking an Ingredient based on Item ID, but no ID was set. Check your XML and make sure there are no logical errors. Aborting");
                            return;
                        }

                        var ingredientItem = ResourcesPrefabManager.Instance.GetItemPrefab(id);
                        if (!ingredientItem)
                        {
                            SL.Log("Error: Could not get ingredient id : " + id);
                            return;
                        }

                        // The item needs the station type tag in order to be used in a manual recipe on that station
                        var tag = TagSourceManager.GetCraftingIngredient(StationType);
                        if (!ingredientItem.HasTag(tag))
                        {
                            //SL.Log($"Adding tag {tag.TagName} to " + ingredientItem.name);

                            if (!ingredientItem.GetComponent <TagSource>())
                            {
                                ingredientItem.gameObject.AddComponent <TagSource>();
                            }

                            ((List <TagSourceSelector>)At.GetField <TagListSelectorComponent>(ingredientItem.GetComponent <TagSource>(), "m_tagSelectors"))
                            .Add(new TagSourceSelector(tag));
                        }

                        ingredients.Add(new RecipeIngredient()
                        {
                            ActionType      = RecipeIngredient.ActionTypes.AddSpecificIngredient,
                            AddedIngredient = ingredientItem,
                        });
                    }
                }

                var recipe = ScriptableObject.CreateInstance <Recipe>();

                recipe.SetCraftingType(this.StationType);

                At.SetField(recipe, "m_results", results.ToArray());
                recipe.SetRecipeIngredients(ingredients.ToArray());

                // set or generate UID
                if (string.IsNullOrEmpty(this.UID))
                {
                    var uid = $"{recipe.Results[0].ItemID}{recipe.Results[0].Quantity}";
                    foreach (var ing in recipe.Ingredients)
                    {
                        if (ing.AddedIngredient != null)
                        {
                            uid += $"{ing.AddedIngredient.ItemID}";
                        }
                        else if (ing.AddedIngredientType != null)
                        {
                            uid += $"{ing.AddedIngredientType.Tag.TagName}";
                        }
                    }
                    this.UID = uid;
                    At.SetField(recipe, "m_uid", new UID(uid));
                }
                else
                {
                    At.SetField(recipe, "m_uid", new UID(this.UID));
                }

                recipe.Init();

                // fix Recipe Manager dictionaries to contain our recipe
                var dict  = References.ALL_RECIPES;
                var dict2 = References.RECIPES_PER_UTENSIL;

                if (dict.ContainsKey(recipe.UID))
                {
                    dict[recipe.UID] = recipe;
                }
                else
                {
                    dict.Add(recipe.UID, recipe);
                }

                if (!dict2.ContainsKey(recipe.CraftingStationType))
                {
                    dict2.Add(recipe.CraftingStationType, new List <UID>());
                }

                if (!dict2[recipe.CraftingStationType].Contains(recipe.UID))
                {
                    dict2[recipe.CraftingStationType].Add(recipe.UID);
                }

                m_applied = true;
            }
            catch (Exception e)
            {
                SL.LogWarning("Error applying recipe!");
                SL.LogInnerException(e);
            }
        }
        // ================ TAGS ================ //

        /// <summary>
        /// Gets a tag from a string tag name. Note: This just calls CustomTags.GetTag(tagName, logging).
        /// </summary>
        public static Tag GetTag(string tagName, bool logging = true)
        {
            return(CustomTags.GetTag(tagName, logging));
        }
        internal void Internal_ApplyTemplate()
        {
            SL.Log($"Applying Enchantment Recipe, ID: {this.EnchantmentID}, Name: {this.Name}");

            var recipe = ScriptableObject.CreateInstance <EnchantmentRecipe>();

            recipe.RecipeID = this.EnchantmentID;
            recipe.ResultID = this.EnchantmentID;

            if (ResourcesPrefabManager.Instance.GetItemPrefab(this.IncenseItemID) is Item incense)
            {
                var list = new List <EnchantmentRecipe.PillarData>();
                foreach (var pillar in this.PillarDatas)
                {
                    list.Add(new EnchantmentRecipe.PillarData
                    {
                        Direction             = (UICardinalPoint_v2.CardinalPoint)pillar.Direction,
                        IsFar                 = pillar.IsFar,
                        CompatibleIngredients = new EnchantmentRecipe.IngredientData[]
                        {
                            new EnchantmentRecipe.IngredientData
                            {
                                Type = EnchantmentRecipe.IngredientData.IngredientType.Specific,
                                SpecificIngredient = incense
                            }
                        }
                    });
                }
                recipe.PillarDatas = list.ToArray();
            }

            recipe.CompatibleEquipments = new EnchantmentRecipe.EquipmentData
            {
                EquipmentTag = new TagSourceSelector(CustomTags.GetTag(this.CompatibleEquipment.RequiredTag)),
            };
            if (this.CompatibleEquipment.Equipments != null)
            {
                var equipList = new List <EnchantmentRecipe.IngredientData>();
                foreach (var ingData in this.CompatibleEquipment.Equipments)
                {
                    var data = new EnchantmentRecipe.IngredientData();
                    if (ingData.SelectorType == IngredientTypes.Tag)
                    {
                        data.Type          = EnchantmentRecipe.IngredientData.IngredientType.Generic;
                        data.IngredientTag = new TagSourceSelector(CustomTags.GetTag(ingData.SelectorValue));
                    }
                    else if (ingData.SelectorType == IngredientTypes.SpecificItem)
                    {
                        data.Type = EnchantmentRecipe.IngredientData.IngredientType.Specific;
                        data.SpecificIngredient = ResourcesPrefabManager.Instance.GetItemPrefab(ingData.SelectorValue);
                    }
                    equipList.Add(data);
                }
                recipe.CompatibleEquipments.CompatibleEquipments = equipList.ToArray();
            }
            else
            {
                recipe.CompatibleEquipments.CompatibleEquipments = new EnchantmentRecipe.IngredientData[0];
            }

            recipe.TimeOfDay          = this.TimeOfDay ?? new Vector2[0];
            recipe.Region             = this.Areas ?? new AreaManager.AreaEnum[0];
            recipe.Temperature        = this.Temperature ?? new TemperatureSteps[0];
            recipe.WindAltarActivated = this.WindAltarActivated;

            if (!string.IsNullOrEmpty(this.QuestEventUID))
            {
                recipe.QuestEvent = new QuestEventReference()
                {
                    m_eventUID = QuestEventUID, UID = QuestEventUID
                };
            }

            if (this.WeatherConditions != null)
            {
                var list = new List <EnchantmentRecipe.WeaterCondition>();
                foreach (var condition in this.WeatherConditions)
                {
                    list.Add(new EnchantmentRecipe.WeaterCondition
                    {
                        Invert  = condition.Invert,
                        Weather = (EnchantmentRecipe.WeaterType)condition.WeatherType
                    });
                }
                recipe.Weather = list.ToArray();
            }
            else
            {
                recipe.Weather = new EnchantmentRecipe.WeaterCondition[0];
            }

            recipe.TableIsInBuilding = this.IsEnchantingGuildRecipe;

            // ========== Create actual Enchantment effects prefab ==========

            var enchantmentObject = new GameObject(this.EnchantmentID + "_" + this.Name);

            GameObject.DontDestroyOnLoad(enchantmentObject);
            var enchantment = enchantmentObject.AddComponent <Enchantment>();

            At.SetField <EffectPreset>(enchantment, "m_StatusEffectID", this.EnchantmentID);

            SetLocalization(this, out enchantment.CustomDescLocKey);

            enchantment.EnchantTime = this.EnchantTime;

            if (this.Effects != null)
            {
                SL_EffectTransform.ApplyTransformList(enchantment.transform, this.Effects?.ToArray(), EditBehaviours.Override);
            }

            if (this.AddedDamages != null)
            {
                var list = new List <Enchantment.AdditionalDamage>();
                foreach (var dmg in this.AddedDamages)
                {
                    list.Add(new Enchantment.AdditionalDamage
                    {
                        BonusDamageType  = dmg.AddedDamageType,
                        ConversionRatio  = dmg.ConversionRatio,
                        SourceDamageType = dmg.SourceDamageType
                    });
                }
                enchantment.AdditionalDamages = list.ToArray();
            }
            if (this.StatModifications != null)
            {
                var list = new Enchantment.StatModificationList();
                foreach (var mod in this.StatModifications)
                {
                    list.Add(new Enchantment.StatModification
                    {
                        Name  = mod.Stat,
                        Type  = mod.Type,
                        Value = mod.Value
                    });
                }
                enchantment.StatModifications = list;
            }
            if (this.FlatDamageAdded != null)
            {
                enchantment.DamageBonus = SL_Damage.GetDamageList(this.FlatDamageAdded);
            }
            if (this.DamageModifierBonus != null)
            {
                enchantment.DamageModifier = SL_Damage.GetDamageList(this.DamageModifierBonus);
            }
            if (this.DamageResistanceBonus != null)
            {
                enchantment.ElementalResistances = SL_Damage.GetDamageList(this.DamageResistanceBonus);
            }

            enchantment.HealthAbsorbRatio  = this.HealthAbsorbRatio;
            enchantment.StaminaAbsorbRatio = this.StaminaAbsorbRatio;
            enchantment.ManaAbsorbRatio    = this.ManaAbsorbRatio;

            enchantment.Indestructible   = this.Indestructible;
            enchantment.TrackDamageRatio = this.TrackDamageRatio;

            enchantment.GlobalStatusResistance = this.GlobalStatusResistance;

            // =========== SET DICTIONARY REFS ============

            // Recipe dict
            if (References.ENCHANTMENT_RECIPES.ContainsKey(this.EnchantmentID))
            {
                References.ENCHANTMENT_RECIPES[this.EnchantmentID] = recipe;
            }
            else
            {
                References.ENCHANTMENT_RECIPES.Add(this.EnchantmentID, recipe);
            }

            // Enchantment dict
            if (References.ENCHANTMENT_PREFABS.ContainsKey(this.EnchantmentID))
            {
                References.ENCHANTMENT_PREFABS[this.EnchantmentID] = enchantment;
            }
            else
            {
                References.ENCHANTMENT_PREFABS.Add(this.EnchantmentID, enchantment);
            }
        }