Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add_ingredient issue #146

Open
Nexela opened this issue Jan 23, 2020 · 8 comments
Open

add_ingredient issue #146

Nexela opened this issue Jan 23, 2020 · 8 comments
Assignees
Labels

Comments

@Nexela
Copy link
Collaborator

Nexela commented Jan 23, 2020

https://mods.factorio.com/mod/stdlib/discussion/5dc42f6770ee0d000c1786f4

@Nexela Nexela added the bug label Jan 23, 2020
@Nexela Nexela self-assigned this Jan 23, 2020
@fabriceni
Copy link

Hi, I have the same problem with the mods pypetroleumhandling_1.6.0 and aai-industry_0.4.1 in 0.18.1 despite the addition of the 3 functions. But no problem in 0.17.79.

@Mernom
Copy link

Mernom commented Apr 15, 2021

I've opened a pull request that should fix this issue.

@drdozer
Copy link

drdozer commented Sep 9, 2021

What is the 'correct' behaviour here? To not crash obviously, but what should add_ingredients ideally do if the ingredient already there? There are 2 situations. a) the ingredient is already there with the same number, or b) it's there with a different number. What's the logic for resolving a difference in the number of items? Do you replace it or raise an error or leave it unchanged?

@drdozer
Copy link

drdozer commented Sep 9, 2021

--- Add an ingredient to an ingredients table.
--- If the ingredient exists, replace the item count
-- @tparam table ingredients
-- @tparam Congepts.ingredient to add
-- @return bool true if replaced, false if added
local function add_or_replace(ingredients, ing)
    for i, ingredient in pairs(ingredients or {}) do
        if ing.name == ingredient.name then
            ingredient.amount = ing.amount
            return true
        end
    end

    ingredients[#ingredients + 1] = ingredient
    return false
end

--- Add an ingredient to a recipe.
-- @tparam string|Concepts.ingredient normal
-- @tparam[opt] string|Concepts.ingredient|boolean expensive
-- @treturn Recipe
function Recipe:add_ingredient(normal, expensive)
    if self:is_valid() then
        normal, expensive = get_difficulties(normal, expensive)

        if self.normal then
            if normal then
                add_or_replace(self.normal.ingredients, normal)
            end
            if expensive then
                add_or_replace(self.expensive.ingredients, expensive)
            end
        elseif normal then
            add_or_replace(self.ingredients, normal)
        end
    end
    return self
end
Recipe.add_ing = Recipe.add_ingredient

@Mernom
Copy link

Mernom commented Sep 9, 2021

There's also the situation of it being present as a catalyst. Should the catalyst property be inherited to the new entry?

@Nexela
Copy link
Collaborator Author

Nexela commented Sep 9, 2021 via email

@drdozer
Copy link

drdozer commented Sep 9, 2021

I think my code can be updated to handle catalysts fairly easily. Otherwise I think it does the job.

@Mernom
Copy link

Mernom commented Sep 9, 2021

--- Add an ingredient to a recipe.
-- @tparam string|Concepts.ingredient normal
-- @tparam[opt] string|Concepts.ingredient|boolean expensive
-- @treturn Recipe
function Recipe:add_ingredient(normal, expensive)
    if self:is_valid() then
        normal, expensive = get_difficulties(normal, expensive)

        if self.normal then
            if normal then
                add_or_replace(self.normal.ingredients, normal)
            end
            if expensive then
                add_or_replace(self.expensive.ingredients, expensive)
            end
        elseif normal then
            add_or_replace(self.ingredients, normal)
        end
    end
    return self
end
Recipe.add_ing = Recipe.add_ingredient

This whole thing can probably just be handled under something like this

for _, diff in pairs  (proto, proto.normal, proto.expensive) do
  if diff.ingredients then
    stuff
  end
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants