Skip to content

Commit

Permalink
Fix recipe computing
Browse files Browse the repository at this point in the history
Added calculation with probability: amount = (amount_min + amount_max) *
probability / 2)
Added description on product of recipe edition (amount, amount_min,
maount max and probabilty)
Added cap 20% on speed and productivity effects
  • Loading branch information
Helfima committed Sep 9, 2016
1 parent e384c08 commit 011f395
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 16 deletions.
2 changes: 1 addition & 1 deletion helmod.lua
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Logging.console = false
--===========================
function helmod:on_init(event)
self.name = "helmod"
self.version = "0.2.9"
self.version = "0.2.10"
self.loaded = false;

self.playerController = PlayerController:new()
Expand Down
2 changes: 1 addition & 1 deletion info.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

{
"name": "helmod",
"version": "0.2.9",
"version": "0.2.10",
"title": "Helmod: Assistant to plan its base.",
"author": "Helfima",
"contact": "Helfima",
Expand Down
2 changes: 2 additions & 0 deletions locale/en/locale.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ remove-element=Remove this row
up-element=Up this row
down-element=Down this row
module-description=__1__\nConsumption: __2__%\nSpeed: __3__%\nProductivity: __4__%\nPollution: __5__%
element-amount=Amount: __1__
element-amount-probability=Amount min: __1__\nAmount max: __2__\nProbability: __3__

[controls] --Text for "game menu -> controls -> mods"
planner-hotkey-main-window=Open/Close planner panel
2 changes: 2 additions & 0 deletions locale/fr/locale.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ remove-element=Supprimer cette ligne
up-element=Monter cette ligne
down-element=Descendre cette ligne
module-description=__1__\nConsommation: __2__%\nVitesse: __3__%\nProductivité: __4__%\nPolution: __5__%
element-amount=Quantité: __1__
element-amount-probability=Quantité min: __1__\nQuantité max: __2__\nProbabilité: __3__

[controls] --Text for "game menu -> controls -> mods"
planner-hotkey-main-window=Open/Close planner panel
2 changes: 2 additions & 0 deletions locale/ru/local.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ remove-element=Удал.строку
up-element=Поднять строку
down-element=Опустить строку
module-description=__1__\nПотребление: __2__%\nСкорость: __3__%\nПродуктивность: __4__%\nЗагрязнение: __5__%
element-amount=Amount: __1__
element-amount-probability=Amount min: __1__\nAmount max: __2__\nProbability: __3__

[controls] --Text for "game menu -> controls -> mods"
planner-hotkey-main-window=Отк/закр окно планировщика
47 changes: 39 additions & 8 deletions planner/plannerModel.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ function PlannerModel.methods:init(parent)
self.parent = parent
self.player = self.parent.parent

self.capSpeed = 0.2
self.capEnergy = 0.2
self.capProductivity = 0.2
end

-------------------------------------------------------------------------------
Expand Down Expand Up @@ -945,6 +948,28 @@ function PlannerModel.methods:update(player, force)

end

-------------------------------------------------------------------------------
-- Get amount of element
--
-- @function [parent=#PlannerModel] getElementAmount
--
-- @param #table element
--
-- @return #number
--
-- @see http://lua-api.factorio.com/latest/Concepts.html#Product
--
function PlannerModel.methods:getElementAmount(element)
if element.amount ~= nil then
return element.amount
end

if element.probability ~= nil and element.amount_min ~= nil and element.amount_max ~= nil then
return ((element.amount_min + element.amount_max) * element.probability / 2)
end

return 0
end
-------------------------------------------------------------------------------
-- Prepare model
--
Expand Down Expand Up @@ -1011,10 +1036,11 @@ function PlannerModel.methods:computeProductionBlock(player, element, maxLoop, l
end
end
-- check produit pilotant
-- @see http://lua-api.factorio.com/latest/Concepts.html#Product
for _, product in pairs(recipe.products) do
if mainProduct == nil then
mainProduct = product
elseif product.count/product.amount > mainProduct.count/mainProduct.amount then
elseif product.count/self:getElementAmount(product) > mainProduct.count/self:getElementAmount(mainProduct) then
mainProduct = product
end
end
Expand All @@ -1023,16 +1049,16 @@ function PlannerModel.methods:computeProductionBlock(player, element, maxLoop, l
-- met a jour le produit
for index, product in pairs(recipe.products) do
if product.name ~= mainProduct.name then
product.count = (mainProduct.count*product.amount/mainProduct.amount)
product.count = (mainProduct.count*self:getElementAmount(product)/self:getElementAmount(mainProduct))
end
end
end


local pCount = mainProduct.count;
for k, ingredient in pairs(recipe.ingredients) do
local productNominal = mainProduct.amount
local productUsage = mainProduct.amount
local productNominal = self:getElementAmount(mainProduct)
local productUsage = self:getElementAmount(mainProduct)
-- calcul production module factory
for module, value in pairs(recipe.factory.modules) do
local bonus = self.player:getModuleBonus(module, "productivity")
Expand All @@ -1044,6 +1070,9 @@ function PlannerModel.methods:computeProductionBlock(player, element, maxLoop, l
productUsage = productUsage + productNominal * value * bonus * recipe.beacon.efficiency * recipe.beacon.combo
end
end
-- cap le productivity a self.capProductivity
if productUsage < productNominal*self.capProductivity then productUsage = productNominal*self.capProductivity end

local nextCount = math.ceil(pCount*(ingredient.amount/productUsage))
ingredient.count = nextCount

Expand All @@ -1069,7 +1098,7 @@ function PlannerModel.methods:computeProductionBlock(player, element, maxLoop, l
element.count = math.ceil(recipe.factory.count/recipe.factory.limit)
end
end

-- consomme les produits
for _, product in pairs(recipe.products) do
if ingredients ~= nil and ingredients[product.name] ~= nil then
Expand Down Expand Up @@ -1203,8 +1232,10 @@ function PlannerModel.methods:computeFactory(player, recipe)
end
end

-- cap l'energy a 20%
if recipe.factory.energy < recipe.factory.energy_nominal*0.2 then recipe.factory.energy = recipe.factory.energy_nominal*0.2 end
-- cap le speed a self.capSpeed
if recipe.factory.speed < recipe.factory.speed_nominal*self.capSpeed then recipe.factory.speed = recipe.factory.speed_nominal*self.capSpeed end
-- cap l'energy a self.capEnergy
if recipe.factory.energy < recipe.factory.energy_nominal*self.capEnergy then recipe.factory.energy = recipe.factory.energy_nominal*self.capEnergy end
-- compte le nombre de machines necessaires
local product = nil
for k, element in pairs(recipe.products) do
Expand All @@ -1214,7 +1245,7 @@ function PlannerModel.methods:computeFactory(player, recipe)
if product ~= nil then
local model = self:getModel(player)
-- [nombre d'item] * [effort necessaire du recipe] / ([la vitesse de la factory] * [nombre produit par le recipe] * [le temps en second])
local count = math.ceil(product.count*recipe.energy/(recipe.factory.speed*product.amount*model.time))
local count = math.ceil(product.count*recipe.energy/(recipe.factory.speed*self:getElementAmount(product)*model.time))
recipe.factory.count = count
if recipe.beacon.active then
recipe.beacon.count = math.ceil(count/recipe.beacon.factory)
Expand Down
10 changes: 7 additions & 3 deletions planner/plannerRecipeEdition.lua
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,7 @@ function PlannerRecipeEdition.methods:updateRecipeIngredients(player, element, a
end
local tablePanel= self:addGuiTable(ingredientsPanel, "table-ingredients", 6)
for key, ingredient in pairs(recipe.ingredients) do
self:addSpriteIconButton(tablePanel, "item=ID=", self.player:getIconType(ingredient), ingredient.name)
self:addSpriteIconButton(tablePanel, "item=ID=", self.player:getIconType(ingredient), ingredient.name, ingredient.name, ({"tooltip.element-amount", ingredient.amount}))
self:addGuiLabel(tablePanel, ingredient.name, ingredient.amount)
end
end
Expand Down Expand Up @@ -512,8 +512,12 @@ function PlannerRecipeEdition.methods:updateRecipeProducts(player, element, acti
end
local tablePanel= self:addGuiTable(productsPanel, "table-products", 6)
for key, product in pairs(recipe.products) do
self:addSpriteIconButton(tablePanel, "item=ID=", self.player:getIconType(product), product.name)
self:addGuiLabel(tablePanel, product.name, product.amount)
if product.amount ~= nil then
self:addSpriteIconButton(tablePanel, "item=ID=", self.player:getIconType(product), product.name, product.name, ({"tooltip.element-amount", product.amount}))
else
self:addSpriteIconButton(tablePanel, "item=ID=", self.player:getIconType(product), product.name, product.name, ({"tooltip.element-amount-probability", product.amount_min, product.amount_max, product.probability}))
end
self:addGuiLabel(tablePanel, product.name, self.model:getElementAmount(product))
end
end
end
Expand Down
6 changes: 3 additions & 3 deletions planner/plannerResult.lua
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,7 @@ function PlannerResult.methods:updateProductionBlock(player, item, item2, item3)
for r, product in pairs(element.products) do
-- product = {type="item", name="steel-plate", amount=8}
local cell = self:addGuiFlowH(outputTable,"cell_"..product.name)
self:addSelectSpriteIconButton(cell, "HMPlannerProductEdition=OPEN=ID="..element.id.."=", self.player:getIconType(product), product.name, "X"..product.amount, nil, ({"tooltip.edit-product"}))
self:addSelectSpriteIconButton(cell, "HMPlannerProductEdition=OPEN=ID="..element.id.."=", self.player:getIconType(product), product.name, "X"..self.model:getElementAmount(product), nil, ({"tooltip.edit-product"}))
self:addGuiLabel(cell, product.name, self:formatNumber(product.count), "helmod_label-right-60")
end
end
Expand Down Expand Up @@ -897,7 +897,7 @@ function PlannerResult.methods:addProductionBlockRow(player, guiTable, blockId,
for r, product in pairs(recipe.products) do
local cell = self:addGuiFlowH(tProducts,"cell_"..product.name)
-- product = {type="item", name="steel-plate", amount=8}
self:addSpriteIconButton(cell, "HMPlannerResourceInfo=OPEN=ID="..blockId.."="..recipe.name.."=", self.player:getIconType(product), product.name, "X"..product.amount)
self:addSpriteIconButton(cell, "HMPlannerResourceInfo=OPEN=ID="..blockId.."="..recipe.name.."=", self.player:getIconType(product), product.name, "X"..self.model:getElementAmount(product))

self:addGuiLabel(cell, product.name, self:formatNumber(product.count), "helmod_label-right-60")
end
Expand Down Expand Up @@ -974,7 +974,7 @@ function PlannerResult.methods:addProductionLineRow(player, guiTable, element)
for r, product in pairs(element.products) do
-- product = {type="item", name="steel-plate", amount=8}
local cell = self:addGuiFlowH(tProducts,"cell_"..product.name)
self:addSelectSpriteIconButton(cell, "HMPlannerProductEdition=OPEN=ID="..element.id.."=", self.player:getIconType(product), product.name, "X"..product.amount, nil, ({"tooltip.edit-product"}))
self:addSelectSpriteIconButton(cell, "HMPlannerProductEdition=OPEN=ID="..element.id.."=", self.player:getIconType(product), product.name, "X"..self.model:getElementAmount(product), nil, ({"tooltip.edit-product"}))

self:addGuiLabel(cell, product.name, self:formatNumber(product.count), "helmod_label-right-60")
end
Expand Down

0 comments on commit 011f395

Please sign in to comment.