From 13c33fc86a56378661f30ac3d901d0b4fd63b5ea Mon Sep 17 00:00:00 2001 From: Helfima Date: Sun, 3 Jun 2018 18:12:13 +0200 Subject: [PATCH] Added flare stack/clarifier consumption recipes Added flare stack/clarifier consumption recipes Fixed show assembly for recipe Fixed product name for resource recipe --- controller/Controller.lua | 10 +++--- core/MainPanel.lua | 4 +-- core/ModelCompute.lua | 49 +++++++++++++++---------- core/defines.lua | 1 + edition/ProductEdition.lua | 11 ++++-- edition/RuleEdition.lua | 2 +- info.json | 2 +- model/EntityPrototype.lua | 14 ++++++++ model/Model.lua | 6 ++-- model/Player.lua | 15 +++++--- model/Product.lua | 14 ++++++++ model/RecipePrototype.lua | 74 +++++++++++++++++++++++++++++--------- 12 files changed, 148 insertions(+), 54 deletions(-) diff --git a/controller/Controller.lua b/controller/Controller.lua index 2271dcbf..1d96950d 100644 --- a/controller/Controller.lua +++ b/controller/Controller.lua @@ -163,9 +163,7 @@ end -- function Controller.onGuiClosed(event) Logging:trace(Controller.classname, "onGuiClosed(event)", event) - if event.element ~= nil and event.element.name == "helmod_main_panel" then - event.element.destroy() - end + Controller.cleanController(Player.native()) end ------------------------------------------------------------------------------- @@ -185,8 +183,7 @@ function Controller.onGuiClick(event) end if event.element.name == Controller.classname.."=CLOSE" then - local player = game.players[event.player_index] - Controller.cleanController(player) + Controller.cleanController(Player.native()) end Controller.parseEvent(event) end @@ -213,6 +210,9 @@ function Controller.parseEvent(event, type) if main_panel:isOpened() then main_panel:main() main_panel:main() + else + -- prevent change location + Controller.cleanController(Player.native()) end end -- hotkey action diff --git a/core/MainPanel.lua b/core/MainPanel.lua index 1d76ead7..3b6a7a4a 100644 --- a/core/MainPanel.lua +++ b/core/MainPanel.lua @@ -61,8 +61,8 @@ function MainPanel.methods:main() local lua_player = Player.native() local location = Player.getSettings("display_location") local gui_main = lua_player.gui[location] - if gui_main["helmod_main_panel"] ~= nil and gui_main["helmod_main_panel"].valid then - gui_main["helmod_main_panel"].destroy() + if self:isOpened() then + Controller.cleanController(Player.native()) else -- interessant mais genere une fausse UI ouverte --Player.native().opened = self:getMainPanel() diff --git a/core/ModelCompute.lua b/core/ModelCompute.lua index d7315cfb..189411c7 100644 --- a/core/ModelCompute.lua +++ b/core/ModelCompute.lua @@ -184,23 +184,29 @@ function ModelCompute.computeBlockRecipe(block, recipe) local production = 1 if recipe.production ~= nil then production = recipe.production end - -- recipe classique - -- prepare le recipe - for _, lua_product in pairs(RecipePrototype.getProducts()) do - if block.ingredients[lua_product.name] ~= nil then - local product = Product.load(lua_product).new() - local p_amount = Product.getAmount(recipe) - local count = block.ingredients[lua_product.name].count*production / p_amount - -- for void in angel mod - if p_amount == 0 then - local lua_ingredient = RecipePrototype.getIngredients(recipe.factory)[1] - local ingredient = Product.load(lua_ingredient).new() - p_amount = ingredient.amount - count = block.products[lua_ingredient.name].count*production / p_amount + -- for void in angel mod + if RecipePrototype.isVoid() then + Logging:debug(ModelCompute.classname, "isvoid", RecipePrototype.isVoid(), "recipe name", recipe.name) + local lua_ingredient = RecipePrototype.getIngredients(recipe.factory)[1] + local ingredient = Product.load(lua_ingredient).new() + local p_amount = ingredient.amount + local count = 0 + if block.products[lua_ingredient.name] ~= nil then + count = block.products[lua_ingredient.name].count*production / p_amount + end + if recipe.count < count then recipe.count = count end + else + -- recipe classique + -- prepare le recipe + for _, lua_product in pairs(RecipePrototype.getProducts()) do + if block.ingredients[lua_product.name] ~= nil then + local product = Product.load(lua_product).new() + local p_amount = Product.getAmount(recipe) + local count = block.ingredients[lua_product.name].count*production / p_amount + Logging:debug(ModelCompute.classname, "count", count) + Logging:debug(ModelCompute.classname, "p_amount", p_amount) + if recipe.count < count then recipe.count = count end end - Logging:debug(ModelCompute.classname, "count", count) - Logging:debug(ModelCompute.classname, "p_amount", p_amount) - if recipe.count < count then recipe.count = count end end end Logging:debug(ModelCompute.classname, "recipe.count=", recipe.count) @@ -302,7 +308,7 @@ end -- @param #table block block of model -- function ModelCompute.prepareBlock(block) - Logging:debug(ModelCompute.classname, "computeBlock():", block.name) + Logging:debug(ModelCompute.classname, "prepareBlock", block.name) local model = Model.getModel() local recipes = block.recipes @@ -330,6 +336,7 @@ function ModelCompute.prepareBlock(block) block.ingredients[lua_ingredient.name].count = 0 end -- construit la list des produits + -- si c'est un voider la liste est vide for _, lua_product in pairs(RecipePrototype.getProducts()) do if block.products[lua_product.name] == nil then block.products[lua_product.name] = Product.load(lua_product).new() @@ -641,7 +648,7 @@ end -- @param #table block block of model -- function ModelCompute.computeBlock(block) - Logging:debug(Model.classname, "computeBlock():", block.name) + Logging:debug(ModelCompute.classname, "********** computeBlock **********") local model = Model.getModel() local recipes = block.recipes @@ -746,6 +753,9 @@ function ModelCompute.computeBlock(block) -- exclus le type ressource ou fluid if recipe.type ~= "resource" and recipe.type ~= "fluid" and block.products[lua_ingredient.name] ~= nil and block.mining_ingredient ~= lua_ingredient.name then block.products[lua_ingredient.name].count = block.products[lua_ingredient.name].count - count + if RecipePrototype.isVoid() then + block.ingredients[lua_ingredient.name].count = block.ingredients[lua_ingredient.name].count - count + end end end Logging:debug(ModelCompute.classname , "********** Compute after clean ingredient:", block) @@ -762,7 +772,8 @@ function ModelCompute.computeBlock(block) -- reduit les produits du block for _, product in pairs(block.products) do - if block.ingredients[product.name] ~= nil and block.mining_ingredient ~= product.name then + -- change le satus si exedant + if block.ingredients[product.name] ~= nil and block.mining_ingredient ~= product.name and not(RecipePrototype.isVoid()) then product.state = 2 end if block.products[product.name].count < 0.01 and not(bit32.band(product.state, 1) > 0) then diff --git a/core/defines.lua b/core/defines.lua index afab2fe9..3004dd96 100644 --- a/core/defines.lua +++ b/core/defines.lua @@ -100,6 +100,7 @@ helmod_settings_mod = { "HMRemote", "HMResourceEdition", "HMResourceTab", + "HMRuleEdition", "HMSatisticTab", "HMSummaryTab", "HMTechnology", diff --git a/edition/ProductEdition.lua b/edition/ProductEdition.lua index 70c06732..469efee1 100644 --- a/edition/ProductEdition.lua +++ b/edition/ProductEdition.lua @@ -123,6 +123,7 @@ function ProductEdition.methods:after_open(event, action, item, item2, item3) end local product = nil +local product_count = 0 ------------------------------------------------------------------------------- -- On update @@ -139,9 +140,15 @@ function ProductEdition.methods:onUpdate(event, action, item, item2, item3) local model = Model.getModel() product = nil if model.blocks[item] ~= nil then - for _, element in pairs(model.blocks[item].products) do + local block = model.blocks[item] + for _, element in pairs(block.products) do if element.name == item2 then product = element + if block.input[product.name] then + product_count = block.input[product.name] + else + product_count = product.count + end end end end @@ -172,7 +179,7 @@ function ProductEdition.methods:updateInfo(item, item2, item3) ElementGui.addGuiLabel(tablePanel, "product-label", Player.getLocalisedName(product)) ElementGui.addGuiLabel(tablePanel, "quantity-label", ({"helmod_common.quantity"})) - ElementGui.addGuiText(tablePanel, "quantity", product.count) + ElementGui.addGuiText(tablePanel, "quantity", product_count or 0) end end diff --git a/edition/RuleEdition.lua b/edition/RuleEdition.lua index 40dc0253..91f841ba 100644 --- a/edition/RuleEdition.lua +++ b/edition/RuleEdition.lua @@ -102,7 +102,7 @@ function RuleEdition.methods:updateRule(item, item2, item3) end if rule_mod == nil then rule_mod = mod_list[1] end ElementGui.addGuiLabel(rule_table, "label-mod", ({"helmod_rule-edition-panel.mod"})) - ElementGui.addGuiDropDown(rule_table, "dropdown=ID=", "mod", mod_list, rule_mod) + ElementGui.addGuiDropDown(rule_table, self:classname().."=dropdown=ID=", "mod", mod_list, rule_mod) -- name local helmod_rule_manes = {} diff --git a/info.json b/info.json index c24fc3a4..a41357c7 100644 --- a/info.json +++ b/info.json @@ -1,7 +1,7 @@ { "name": "helmod", - "version": "0.7.8", + "version": "0.7.10", "title": "Helmod: assistant for planning your base.", "author": "Helfima", "contact": "Helfima", diff --git a/model/EntityPrototype.lua b/model/EntityPrototype.lua index cb062201..1859631c 100644 --- a/model/EntityPrototype.lua +++ b/model/EntityPrototype.lua @@ -356,6 +356,20 @@ function EntityPrototype.getMineableMiningFluidAmount() return 0 end +------------------------------------------------------------------------------- +-- Return mineable property products +-- +-- @function [parent=#EntityPrototype] getMineableMiningProducts +-- +-- @return #string +-- +function EntityPrototype.getMineableMiningProducts() + if lua_entity_prototype ~= nil and lua_entity_prototype.mineable_properties ~= nil then + return lua_entity_prototype.mineable_properties.products + end + return {} +end + ------------------------------------------------------------------------------- -- Return electric energy source prototype buffer capacity -- diff --git a/model/Model.lua b/model/Model.lua index 019f7d06..69024592 100644 --- a/model/Model.lua +++ b/model/Model.lua @@ -85,8 +85,10 @@ function Model.resetRules() table.insert(global.rules, {index=7, mod="DyWorld", name="production-crafting", category="extraction-machine", type="entity-subgroup", value="dyworld-extraction-burner", excluded = false}) table.insert(global.rules, {index=8, mod="DyWorld", name="production-crafting", category="extraction-machine", type="entity-subgroup", value="dyworld-drills-electric", excluded = false}) table.insert(global.rules, {index=9, mod="DyWorld", name="production-crafting", category="extraction-machine", type="entity-subgroup", value="dyworld-drills-burner", excluded = false}) - table.insert(global.rules, {index=10, mod="DyWorld", name="production-crafting", category="standard", type="entity-group", value="production", excluded = true}) - table.insert(global.rules, {index=11, mod="DyWorld", name="production-crafting", category="extraction-machine", type="entity-group", value="production", excluded = true}) + table.insert(global.rules, {index=10, mod="DyWorld", name="production-crafting", category="standard", type="entity-name", value="assembling-machine-1", excluded = true}) + table.insert(global.rules, {index=11, mod="DyWorld", name="production-crafting", category="standard", type="entity-name", value="assembling-machine-2", excluded = true}) + table.insert(global.rules, {index=12, mod="DyWorld", name="production-crafting", category="standard", type="entity-name", value="assembling-machine-3", excluded = true}) + table.insert(global.rules, {index=13, mod="DyWorld", name="production-crafting", category="extraction-machine", type="entity-group", value="production", excluded = true}) end ------------------------------------------------------------------------------- diff --git a/model/Player.lua b/model/Player.lua index d37d524c..371c5f4e 100644 --- a/model/Player.lua +++ b/model/Player.lua @@ -506,7 +506,7 @@ end ------------------------------------------------------------------------------- -- Return rule -- --- @function [parent=#Player] getRules +-- @function [parent=#Player] checkRules -- -- @param #boolean check -- @param #table rules @@ -602,17 +602,18 @@ function Player.getProductionsCrafting(category, lua_recipe) else for key, lua_entity in pairs(game.entity_prototypes) do if lua_entity.type ~= nil and lua_entity.type ~= "offshore-pump" and lua_entity.name ~= nil and lua_entity.name ~= "player" then - Logging:debug(Player.classname, "getProductionsCrafting(category):item", lua_entity.name, lua_entity.type, lua_entity.group.name, lua_entity.subgroup.name, lua_entity.crafting_categories) + Logging:trace(Player.classname, "loop production machines", lua_entity.name, lua_entity.type, lua_entity.group.name, lua_entity.subgroup.name, lua_entity.crafting_categories) local check = false if category ~= nil then if not(rules_included[category]) and not(rules_included[category]) then -- standard recipe if lua_entity.crafting_categories ~= nil and lua_entity.crafting_categories[category] then - local recipe_ingredient_count = #RecipePrototype.load(lua_recipe, "recipe").getIngredients(lua_entity) + local recipe_ingredient_count = RecipePrototype.load(lua_recipe, "recipe").getIngredientCount(lua_entity) local factory_ingredient_count = EntityPrototype.load(lua_entity).getIngredientCount() Logging:debug(Player.classname, "crafting", recipe_ingredient_count, factory_ingredient_count) if factory_ingredient_count >= recipe_ingredient_count then check = true + Logging:debug(Player.classname, "allowed machine", lua_entity.name) end -- resolve rule excluded check = Player.checkRules(check, rules_excluded, "standard", lua_entity, false) @@ -721,8 +722,12 @@ function Player.searchRecipe(name) end -- recherche dans les resource for key, resource in pairs(Player.getResources()) do - if resource.name == name then - table.insert(recipes,{name=resource.name, type="resource"}) + local products = EntityPrototype.load(resource).getMineableMiningProducts() + for key, product in pairs(products) do + if product.name == name then + table.insert(recipes,{name=resource.name, type="resource"}) + break + end end end -- recherche dans les fluids diff --git a/model/Product.lua b/model/Product.lua index 523d1966..38ee71c3 100644 --- a/model/Product.lua +++ b/model/Product.lua @@ -109,6 +109,19 @@ function Product.getElementAmount(element) return 0 end +------------------------------------------------------------------------------- +-- Get type of element (item or fluid) +-- +-- @function [parent=#Product] getType +-- +-- @return #string +-- +function Product.getType() + Logging:debug(Product.classname, "getType()",lua_product) + if lua_product.type == 1 or lua_product.type == "fluid" then return "fluid" end + return "item" +end + ------------------------------------------------------------------------------- -- Get amount of element -- @@ -206,6 +219,7 @@ end -- function Product.getProductivityBonus(recipe) Logging:debug(Product.classname, "getProductivityBonus(recipe)", lua_product) + if recipe.isluaobject or recipe.factory == nil or recipe.factory.effects == nil then return 1 end local productivity = recipe.factory.effects.productivity if recipe.type == "resource" then productivity = productivity + Player.getForce().mining_drill_productivity_bonus diff --git a/model/RecipePrototype.lua b/model/RecipePrototype.lua index 5f447c77..0344bd43 100644 --- a/model/RecipePrototype.lua +++ b/model/RecipePrototype.lua @@ -9,6 +9,7 @@ local RecipePrototype = { local lua_prototype = nil local lua_type = nil +local is_voider = nil ------------------------------------------------------------------------------- -- Load factorio RecipePrototype @@ -23,6 +24,7 @@ local lua_type = nil function RecipePrototype.load(object, object_type) Logging:debug(RecipePrototype.classname, "load(object, object_type)", object, object_type) local object_name = nil + is_voider = nil if type(object) == "string" then object_name = object lua_type = object_type @@ -124,6 +126,18 @@ function RecipePrototype.type() return lua_type end +------------------------------------------------------------------------------- +-- Return if recipe void ingredient +-- for flare stack/clarifier ect... +-- @function [parent=#RecipePrototype] isVoid +-- +-- @return #boolean +-- +function RecipePrototype.isVoid() + if is_voider == nil then RecipePrototype.getProducts() end + return is_voider +end + ------------------------------------------------------------------------------- -- Return localised name of Prototype -- @@ -172,30 +186,37 @@ end -- @return #table -- function RecipePrototype.getProducts() - Logging:debug(RecipePrototype.classname, "getProducts()", lua_prototype, lua_type) + Logging:debug(RecipePrototype.classname, "getProducts()", lua_prototype.name, lua_type) local raw_products = RecipePrototype.getRawProducts() + --Logging:debug(RecipePrototype.classname, "raw_products", raw_products) + -- if recipe is a voider + if #raw_products == 1 and Product.getElementAmount(raw_products[1]) == 0 then + Logging:debug(RecipePrototype.classname, "is_voider", lua_prototype.name) + is_voider = true + return {} + else + is_voider = false + end local lua_products = {} for r, raw_product in pairs(RecipePrototype.getRawProducts()) do local product_id = raw_product.type .. "/" .. raw_product.name if lua_products[product_id] ~= nil then -- make a new product table for the combined result - local new_product = {} - for k, v in pairs(lua_products[product_id]) do - new_product[k] = v - end -- combine product amounts, averaging in variable and probabilistic outputs - local amount_a = Product.getElementAmount(new_product) + local amount_a = Product.getElementAmount(lua_products[product_id]) local amount_b = Product.getElementAmount(raw_product) - new_product.amount = amount_a + amount_b - new_product.min_amount = nil - new_product.max_amount = nil - new_product.probability = nil - lua_products[product_id] = new_product + lua_products[product_id] = {type=raw_product.type,name=raw_product.name,amount=amount_a + amount_b} else lua_products[product_id] = raw_product end end - return lua_products + -- convert map to array + local raw_products = {} + for _, lua_product in pairs(lua_products) do + table.insert(raw_products,lua_product) + end + + return raw_products end ------------------------------------------------------------------------------- @@ -206,12 +227,12 @@ end -- @return #table -- function RecipePrototype.getRawProducts() - Logging:debug(RecipePrototype.classname, "getRawProducts()", lua_prototype, lua_type) + Logging:debug(RecipePrototype.classname, "getRawProducts()", lua_prototype.name, lua_type) if lua_prototype ~= nil then if lua_type == "recipe" then return lua_prototype.products elseif lua_type == "resource" then - return {{name=lua_prototype.name, type="item", amount=1}} + return EntityPrototype.load(lua_prototype).getMineableMiningProducts() elseif lua_type == "fluid" then return {{name=lua_prototype.name, type="fluid", amount=1}} elseif lua_type == "technology" then @@ -221,6 +242,25 @@ function RecipePrototype.getRawProducts() return {} end +------------------------------------------------------------------------------- +-- Return solid ingredient number of Prototype +-- +-- @function [parent=#RecipePrototype] getIngredientCount +-- +-- @return #number +-- +function RecipePrototype.getIngredientCount(factory) + Logging:debug(RecipePrototype.classname, "getIngredientCount()", lua_prototype, lua_type) + local count = 0 + if lua_prototype ~= nil and RecipePrototype.getIngredients(factory) ~= nil then + for _,lua_ingredient in pairs(RecipePrototype.getIngredients(factory)) do + if Product.load(lua_ingredient).getType() == "item" then + count = count + 1 + end + end + end + return count +end ------------------------------------------------------------------------------- -- Return ingredients array of Prototype -- @@ -248,15 +288,15 @@ function RecipePrototype.getIngredients(factory) local energy_coal = 8000000 local hardness = EntityPrototype.getMineableHardness() local mining_time = EntityPrototype.getMineableMiningTime() - Logging:debug(RecipePrototype.classname, "mining properties", "hardness", hardness, "mining_time", mining_time) + Logging:debug(RecipePrototype.classname, "mining properties", "hardness", hardness, "mining_time", mining_time) EntityPrototype.load(factory) if factory ~= nil and EntityPrototype.getEnergyType() == "burner" then local energy_usage = EntityPrototype.getEnergyUsage() local burner_effectivity = EntityPrototype.getBurnerEffectivity() local mining_speed = EntityPrototype.getMiningSpeed() local mining_power = EntityPrototype.getMiningPower() - Logging:debug(RecipePrototype.classname, "factory properties", "energy_usage", energy_usage, "burner_effectivity", burner_effectivity, "mining_speed", mining_speed, "mining_power", mining_power) - + Logging:debug(RecipePrototype.classname, "factory properties", "energy_usage", energy_usage, "burner_effectivity", burner_effectivity, "mining_speed", mining_speed, "mining_power", mining_power) + local speed_factory = (mining_power - hardness) * mining_speed / mining_time local fuel_value = energy_usage*speed_factory*12.5 local burner_count = fuel_value/energy_coal