// Takes ItemIds of available objects, and itemId of target object
    // If targetItem can be made, return corresponding GameObjectEntry
    public GameObjectEntry GetBlueprint(List <RecipeElement> availableItems, int targetItemId)
    {
        // Obtain blueprint from targetItemId
        GameObjectEntry goe = GameObjs.items.Find(item => item.item_id == targetItemId);

        // ForEach item in the blueprint, check the required items are available
        foreach (RecipeElement item in goe.blueprint)
        {
            // Find recipe element in provided available elements
            RecipeElement available = availableItems.Find(itemToFind => itemToFind.item_id == item.item_id);

            // If item is present
            if (available != null)
            {
                // Is the required quantity available?
                if (available.quantity < item.quantity)
                {
                    return(null);
                }
            }
            else
            {
                // If item is not present
                return(null);
            }
        }

        return(goe);
    }
예제 #2
0
 public Recipe(int id, List <RecipeElement> ingredients, RecipeElement product, List <int> requireTech, int craftLevel)
 {
     this.id          = id;
     this.ingredients = new List <RecipeElement>(ingredients);
     this.product     = product;
     this.requireTech = requireTech;
     this.craftLevel  = craftLevel;
 }
예제 #3
0
    public static void init(TextAsset reader)
    {
        recipeList = new List <Recipe>();

        try {
            string[] lines;

            lines = reader.text.Split('\n');
            foreach (string line in lines)
            {
                // for database comment
                if (line[0] == '#')
                {
                    continue;
                }

                string[] words = line.Split(' ');

                // RECIPE DB : 'ID ING1 NUM1 ING2 NUM2 ING3 NUM3 ... | PRODUCT_NUM'
                int section = Array.IndexOf(words, "|");
                List <RecipeElement> ingredients = new List <RecipeElement>();
                for (int i = 1; i < section; i += 2)
                {
                    ingredients.Add(new RecipeElement(int.Parse(words[i]), int.Parse(words[i + 1])));
                }

                // if recipe can generate multiple product, code below need to be fixed.
                RecipeElement product = new RecipeElement(int.Parse(words[section + 1]), int.Parse(words[section + 2]));

                // ADDITIONAL RECIPE : '~ REQUIRED TECH'
                if (section + 3 < words.Length)
                {
                    List <int> requiredTech = new List <int>();
                    for (int i = section + 4; !words[i].Equals("|"); i++)
                    {
                        requiredTech.Add(int.Parse(words[i]));
                    }
                    recipeList.Add(new Recipe(
                                       int.Parse(words[0]),
                                       ingredients,
                                       product,
                                       requiredTech,
                                       int.Parse(words[words.Length - 1])
                                       ));
                }
                else
                {
                    recipeList.Add(new Recipe(
                                       int.Parse(words[0]),
                                       ingredients,
                                       product
                                       ));
                }
            }
        } catch (System.Exception e) {
            Debug.Log("Wrong File " + e);
        }
    }
예제 #4
0
        public void ZeroAmountsFailTest()
        {
            void TestCodeZero()
            {
                var _ = new RecipeElement("test", 0);
            }

            Assert.Throws <ArgumentOutOfRangeException>(TestCodeZero);
        }
예제 #5
0
        public void NegativeeAountsFailTest()
        {
            void TestCodeNegative()
            {
                var _ = new RecipeElement("test", -1);
            }

            Assert.Throws <ArgumentOutOfRangeException>(TestCodeNegative);
        }
예제 #6
0
 /// <summary>
 /// Resets the UI each time the dialogue box is loaded.
 /// </summary>
 /// <param name="inSender">The originator of the event.</param>
 /// <param name="inEventArguments">Additional event data.</param>
 private void AddRecipeElementBox_Load(object inSender, EventArgs inEventArguments)
 {
     if (ReturnNewRecipeElement is not null)
     {
         ReturnNewRecipeElement = null;
         newTag    = "";
         newAmount = 0;
         ElementAmountTextBox.Text = "";
         ElementTagTextBox.Text    = "";
     }
     ApplyCurrentTheme();
     ElementTagTextBox.Select();
 }
예제 #7
0
    public void UpdateShopResourcesAndItemsAmounts()
    {
        foreach (GameObject obj in _recipePrefabs)
        {
            RecipeElement recipeElement = obj.GetComponent <RecipeElement>();
            foreach (Resource resource in Player.AllResources)
            {
                if (recipeElement.IsResouce && recipeElement.ResourceType == resource.Type)
                {
                    obj.transform.GetChild(1).GetComponent <TextMeshProUGUI>().text = resource.Amount.ToString() + " / " + recipeElement.AmountNeeded.ToString();
                }
            }

            int itemsNeededNumber = 0;
            if (recipeElement.IsItem)
            {
                itemsNeededNumber = recipeElement.AmountNeeded;
            }
            for (int i = 0; i < itemsNeededNumber; i++)
            {
                int itemNeededInInventory = 0;
                foreach (Item item in Player.AllItems)
                {
                    if (recipeElement.IsItem && recipeElement.ItemType == item.Type)
                    {
                        itemNeededInInventory++;
                    }
                }
                if (itemNeededInInventory > 0)
                {
                    obj.transform.GetChild(1).GetComponent <TextMeshProUGUI>().text = (itemNeededInInventory - itemsNeededNumber).ToString() + " / " + recipeElement.AmountNeeded.ToString();
                    //return;
                }
            }


            /*foreach (Item item in Player.AllItems)
             * {
             *  //recipeElement.Amount--;
             *  if (recipeElement.IsItem && recipeElement.ItemType == item.Type)
             *  {
             *      obj.transform.GetChild(1).GetComponent<TextMeshProUGUI>().text = recipeElement.Amount.ToString() + " / " + recipeElement.AmountNeeded.ToString();
             *  }
             * }*/
        }
    }
    // Given available items and a machineId
    // Returns possible object
    public GameObjectEntry GetRecipe(List <RecipeElement> inputItems, int machineId)
    {
        // Find objects that can be produced by machineId
        List <GameObjectEntry> objects = GameObjs.items.FindAll(item => item.machine_id == machineId);

        // Find the correct output item for given input items
        foreach (GameObjectEntry obj in objects)
        {
            Boolean correctItem = true;

            // For each item in the object's recipe
            foreach (RecipeElement recipeItem in obj.recipe)
            {
                // Is the required item present?
                RecipeElement available = inputItems.Find(itemToFind => itemToFind.item_id == recipeItem.item_id);

                // If present
                if (available != null)
                {
                    // Is the required quantity available?
                    if (available.quantity < recipeItem.quantity)
                    {
                        // Not enough item available
                        correctItem = false;
                    }
                }
                else
                {
                    // If item is not present
                    correctItem = false;
                }
            }

            // Success case
            if (correctItem)
            {
                return(obj);
            }
        }

        // Failure case, no valid outputs
        return(null);
    }
예제 #9
0
    // craft Item
    public bool craft(int recipeId, int craftCount)
    {
        Recipe recipe = getRecipe(recipeId);
        List <RecipeElement> ingredients  = recipe.getIngredients();
        List <int>           requirements = recipe.getRequireTech();
        RecipeElement        product      = recipe.getProduct();

        foreach (RecipeElement e in ingredients)
        {
            // player don't have enough item
            if (!player.checkItemPossession(e.item.getId(), e.count * craftCount))
            {
                Debug.Log("NOT ENOUGH ITEM T.T");
                return(false);
            }
        }

        foreach (int id in requirements)
        {
            if (!rm.checkTechDone(id))
            {
                Debug.Log("TECHNOLOGY NOT SATISFIED");
                return(false);
            }
        }

        foreach (RecipeElement e in ingredients)
        {
            player.removeItem(e.item.getId(), e.count * craftCount);
        }

        if (product.item.getType() == ITEMTYPE.BUILDING)
        {
            return(true);
        }

        player.changeSatiety(SATIETYPOINTS.CRAFTING);
        player.addItem(product.item.getId(), product.count * craftCount);
        Debug.Log("Craft Done");
        return(true);
    }
예제 #10
0
    public void FillInRecipeContainer()
    {
        TextAsset itemRecipes     = Resources.Load <TextAsset>("Item Recipes");
        JsonData  itemRecipesJson = JsonMapper.ToObject(itemRecipes.text);

        for (int i = 0; i < itemRecipesJson["Recipes"].Count; i++)
        {
            if (SelectedItem.Name == itemRecipesJson["Recipes"][i]["Name"].ToString())
            {
                for (int j = 0; j < itemRecipesJson["Recipes"][i]["RequieredResources"].Count; j++)
                {
                    JsonData      ItemInfo      = itemRecipesJson["Recipes"][i]["RequieredResources"][j];
                    RecipeElement recipeElement = Instantiate(RecipeElementPrefab, RecipeContainer).GetComponent <RecipeElement>();
                    _recipePrefabs.Add(recipeElement.gameObject);

                    recipeElement.IsResouce = true;
                    Resource.ResourceType resourceType = (Resource.ResourceType)System.Enum.Parse(typeof(Resource.ResourceType), ItemInfo["ResourceType"].ToString());
                    recipeElement.ResourceType = resourceType;

                    recipeElement.transform.Find("Recipe Element Icon").gameObject.GetComponent <Image>().sprite = Resources.Load <Sprite>("Sprites/" + ItemInfo["ResourceIcon"].ToString());

                    foreach (Resource resource in Player.AllResources)
                    {
                        if (resource.Type == resourceType)
                        {
                            recipeElement.Amount = resource.Amount;
                            break; // Not sure
                        }
                    }
                    recipeElement.AmountNeeded = int.Parse(itemRecipesJson["Recipes"][i]["RequieredResources"][j]["Amount"].ToString());
                    recipeElement.transform.Find("Recipe Element Amount").GetComponent <TextMeshProUGUI>().text = recipeElement.Amount.ToString() + " / " + recipeElement.AmountNeeded;
                }
                for (int k = 0; k < itemRecipesJson["Recipes"][i]["RequieredItems"].Count; k++)
                {
                    JsonData      ItemInfo      = itemRecipesJson["Recipes"][i]["RequieredItems"][k];
                    RecipeElement recipeElement = Instantiate(RecipeElementPrefab, RecipeContainer).GetComponent <RecipeElement>();
                    _recipePrefabs.Add(recipeElement.gameObject);

                    recipeElement.GetComponent <RecipeElement>().IsItem = true;
                    Item.ItemType itemType = (Item.ItemType)System.Enum.Parse(typeof(Item.ItemType), ItemInfo["ItemType"].ToString());
                    recipeElement.GetComponent <RecipeElement>().ItemType = itemType;

                    recipeElement.transform.Find("Recipe Element Icon").gameObject.GetComponent <Image>().sprite = Resources.Load <Sprite>("Sprites/" + ItemInfo["ItemIcon"].ToString());

                    int matchedItemsCount = 0;
                    foreach (Item item in Player.AllItems)
                    {
                        if (item.Type == itemType)
                        {
                            matchedItemsCount++;
                        }
                    }
                    recipeElement.AmountNeeded = int.Parse(itemRecipesJson["Recipes"][i]["RequieredItems"][k]["Amount"].ToString());
                    recipeElement.Amount       = matchedItemsCount;

                    recipeElement.transform.Find("Recipe Element Amount").GetComponent <TextMeshProUGUI>().text = recipeElement.Amount.ToString() + " / " + recipeElement.AmountNeeded.ToString();
                    /*recipeElement.transform.Find("Recipe Element Amount Needed").GetComponent<TextMeshProUGUI>().text = itemRecipesJson["Recipes"][i]["RequieredItems"][k]["Amount"].ToString();*/
                }
            }
        }
    }
예제 #11
0
 /// <summary>
 /// Closes the <see cref="AddTagBox"/>, signaling to abandon any entered tag text.
 /// </summary>
 /// <param name="inSender">The originator of the event.</param>
 /// <param name="inEventArguments">Additional event data.</param>
 private void CancelButtonControl_Click(object inSender, EventArgs inEventArguments)
 {
     ReturnNewRecipeElement = null;
     DialogResult           = DialogResult.Cancel;
     Close();
 }