Skip to content

Commit

Permalink
fix error with some nil value when missing recipe
Browse files Browse the repository at this point in the history
  • Loading branch information
Helfima committed Jun 20, 2024
1 parent 95fb702 commit 2c7ffc9
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 41 deletions.
2 changes: 2 additions & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ Date: 2024-06-09
- Fix flow value per minute in tooltip of product
- Fix remove linked product if not exist in block products
- Fix compute unliked block in parent block
- Fix apply module priority when change beacon
- Fix modules comparation to check if the factory is the default
---------------------------------------------------------------------------------------------------
Version: 1.0.11
Date: 2024-05-27
Expand Down
5 changes: 4 additions & 1 deletion data/Model.lua
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,9 @@ function Model.setBeacon(recipe, index, name, combo, per_factory, per_factory_co
beacon.per_factory = per_factory or User.getPreferenceSetting("beacon_by_factory")
beacon.per_factory_constant = per_factory_constant or User.getPreferenceSetting("beacon_constant")
if recipe.beacons[index] ~= nil then
local old_beacon = recipe.beacons[index]
local module_priority = old_beacon.module_priority
beacon.module_priority = module_priority
recipe.beacons[index] = beacon
end
end
Expand Down Expand Up @@ -620,7 +623,7 @@ end
function Model.factoryHasModule(factory)
if factory == nil then return false end
if factory.modules == nil then return false end
if factory.modules ~= nil and #factory.modules then return false end
if factory.modules ~= nil and table.size(factory.modules) == 0 then return false end
return true
end

Expand Down
25 changes: 5 additions & 20 deletions data/ModelBuilder.lua
Original file line number Diff line number Diff line change
Expand Up @@ -403,33 +403,18 @@ end
function ModelBuilder.applyFactoryModulePriority(recipe)
if recipe ~= nil then
local module_priority = recipe.factory.module_priority
if module_priority == nil then
recipe.factory.modules = {}
else
local first = true
for i, priority in pairs(module_priority) do
local module = ItemPrototype(priority.name)
if Player.checkFactoryLimitationModule(module:native(), recipe) == true then
if first then
ModelBuilder.setModuleModel(recipe.factory, priority.name, priority.value)
first = false
else
ModelBuilder.appendModuleModel(recipe.factory, priority.name, priority.value)
end
end
end
end
ModelBuilder.setFactoryModulePriority(recipe, module_priority)
end
end

---Set a module priority in beacons
-------------------------------------------------------------------------------
---Apply a module priority in factory
---@param recipe RecipeData
---@param modules_priority {[uint] : {[uint] : ModulePriorityData}}
function ModelBuilder.setBeaconsModulesPriority(recipe, modules_priority)
function ModelBuilder.applyBeaconModulePriority(recipe)
if recipe ~= nil then
local beacons = recipe.beacons
for index, beacon in ipairs(beacons) do
local module_priority = modules_priority[index]
local module_priority = beacon.module_priority
ModelBuilder.setBeaconModulePriority(beacon, recipe, module_priority)
end
end
Expand Down
23 changes: 10 additions & 13 deletions dialog/ProductionPanel.lua
Original file line number Diff line number Diff line change
Expand Up @@ -592,8 +592,8 @@ function ProductionPanel:updateInputBlock(model, block)
local ingredient = Product(lua_ingredient):clone()
ingredient.time = model.time
ingredient.count = lua_ingredient.amount
ingredient.count_limit = lua_ingredient.amount * block.count_limit
ingredient.count_deep = lua_ingredient.amount * block.count_deep
ingredient.count_limit = lua_ingredient.amount * (block.count_limit or 0)
ingredient.count_deep = lua_ingredient.amount * (block.count_deep or 0)

local button_action = "production-recipe-ingredient-add"
local button_tooltip = "tooltip.ingredient"
Expand Down Expand Up @@ -681,8 +681,8 @@ function ProductionPanel:updateOutputBlock(model, block)
local product = Product(lua_product):clone()
product.time = model.time
product.count = lua_product.amount
product.count_limit = lua_product.amount * block.count_limit
product.count_deep = lua_product.amount * block.count_deep
product.count_limit = lua_product.amount * (block.count_limit or 0)
product.count_deep = lua_product.amount * (block.count_deep or 0)

local button_action = "production-recipe-product-add"
local button_tooltip = "tooltip.product"
Expand Down Expand Up @@ -1166,9 +1166,6 @@ function ProductionPanel:addTableRowRecipe(gui_table, model, block, recipe)
if ingredient.constant == true then
ingredient.count = ingredient_prototype:countProduct(recipe)
end
if block.by_limit == true and block.count > 1 then
ingredient.limit_count = ingredient.count / block.count
end
if block.by_product == false and recipe.contraint ~= nil and recipe.contraint.name == ingredient.name then
contraint_type = recipe.contraint.type
end
Expand Down Expand Up @@ -1281,9 +1278,9 @@ function ProductionPanel:addTableRowBlock(gui_table, model, parent, block)
local product_prototype = Product(lua_product)
local product = product_prototype:clone()
product.time = model.time
product.count = lua_product.amount * block.count
product.count_limit = lua_product.amount * block.count_limit
product.count_deep = lua_product.amount * block.count_deep
product.count = lua_product.amount * (block.count or 0)
product.count_limit = lua_product.amount * (block.count_limit or 0)
product.count_deep = lua_product.amount * (block.count_deep or 0)

if not (block_by_product) or not (block.unlinked) or block.by_factory == true then
button_action = "production-recipe-product-add"
Expand Down Expand Up @@ -1334,9 +1331,9 @@ function ProductionPanel:addTableRowBlock(gui_table, model, parent, block)
local ingredient_prototype = Product(lua_ingredient)
local ingredient = ingredient_prototype:clone()
ingredient.time = model.time
ingredient.count = lua_ingredient.amount * block.count
ingredient.count_limit = lua_ingredient.amount * block.count_limit
ingredient.count_deep = lua_ingredient.amount * block.count_deep
ingredient.count = lua_ingredient.amount * (block.count or 0)
ingredient.count_limit = lua_ingredient.amount * (block.count_limit or 0)
ingredient.count_deep = lua_ingredient.amount * (block.count_deep or 0)

if block_by_product then
button_action = "production-recipe-ingredient-add"
Expand Down
3 changes: 2 additions & 1 deletion edition/RecipeEdition.lua
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,7 @@ function RecipeEdition:onEvent(event)
if event.action == "beacon-choose" then
local current_beacon_selection = User.getParameter("current_beacon_selection") or 1
Model.setBeacon(recipe, current_beacon_selection, event.item4)
ModelBuilder.applyBeaconModulePriority(recipe)
ModelCompute.update(model)
self:update(event)
Controller:send("on_gui_recipe_update", event)
Expand Down Expand Up @@ -760,7 +761,7 @@ function RecipeEdition:updateFactoryInfo(event)
default_fuel = string.format("[%s=%s] %s", fuel_type, factory_fuel:native().name, Format.formatNumberKilo(factory_fuel:getFuelValue(), "J"))
end
GuiElement.add(input_panel, GuiLabel("label-burner"):caption({"helmod_common.resource"}))
GuiElement.add(input_panel, GuiDropDown(self.classname, "factory-fuel-update", model.id, block.id, recipe.id, fuel_type):items(items, default_fuel))
GuiElement.add(input_panel, GuiDropDown(self.classname, "factory-fuel-update", model.id, block.id, recipe.id, fuel_type):items(items, default_fuel):tooltip(factory_fuel:native().localised_name))
end
end

Expand Down
3 changes: 3 additions & 0 deletions math/SolverLinkedMatrix.lua
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,9 @@ end
---@param block any
---@param matrix any
function SolverLinkedMatrix:solve_block_finalize(block, matrix)
if matrix == nil or matrix.states == nil then
return
end
-- state = 0 => produit
-- state = 1 => produit pilotant
-- state = 2 => produit restant
Expand Down
12 changes: 6 additions & 6 deletions model/Product.lua
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ end
---@return number
function Product:countProduct(recipe)
local amount = self:getAmount(recipe)
return amount * recipe.count
return amount * (recipe.count or 0)
end

-------------------------------------------------------------------------------
Expand All @@ -181,7 +181,7 @@ end
---@return number
function Product:countLimitProduct(recipe)
local amount = self:getAmount(recipe)
return amount * recipe.count_limit
return amount * (recipe.count_limit or 0)
end

-------------------------------------------------------------------------------
Expand All @@ -190,7 +190,7 @@ end
---@return number
function Product:countDeepProduct(recipe)
local amount = self:getAmount(recipe)
return amount * recipe.count_deep
return amount * (recipe.count_deep or 0)
end

-------------------------------------------------------------------------------
Expand All @@ -199,7 +199,7 @@ end
---@return number
function Product:countIngredient(recipe)
local amount = self:getElementAmount()
return amount * recipe.count
return amount * (recipe.count or 0)
end

-------------------------------------------------------------------------------
Expand All @@ -208,7 +208,7 @@ end
---@return number
function Product:countLimitIngredient(recipe)
local amount = self:getElementAmount()
return amount * recipe.count_limit
return amount * (recipe.count_limit or 0)
end

-------------------------------------------------------------------------------
Expand All @@ -217,7 +217,7 @@ end
---@return number
function Product:countDeepIngredient(recipe)
local amount = self:getElementAmount()
return amount * recipe.count_deep
return amount * (recipe.count_deep or 0)
end

-------------------------------------------------------------------------------
Expand Down

0 comments on commit 2c7ffc9

Please sign in to comment.