diff --git a/changelog.txt b/changelog.txt index 8358856c..39d2056e 100644 --- a/changelog.txt +++ b/changelog.txt @@ -2,11 +2,12 @@ Version: 0.10.6 Date: 23. 02. 2020 Changes: - - added all energy source prototypes - added a new block for energy consumption + - added all energy source prototypes - removed energy tab - modified property tab - changed fluid recipe, now there are hidden except water and steam + - removed ingredient for ressource recipes and water recipe Bugfixes: - fixed width internal panel of edit recipe --------------------------------------------------------------------------------------------------- diff --git a/controller/Controller.lua b/controller/Controller.lua index fe3f9401..dbb7cbb2 100644 --- a/controller/Controller.lua +++ b/controller/Controller.lua @@ -9,6 +9,7 @@ require "dialog.Calculator" require "dialog.RecipeExplorer" require "edition.RecipeEdition" require "edition.ProductEdition" +require "edition.EnergyEdition" require "edition.RuleEdition" require "edition.PreferenceEdition" require "selector.ContainerSelector" @@ -19,6 +20,7 @@ require "selector.TechnologySelector" require "selector.ItemSelector" require "selector.FluidSelector" +require "tab.EnergyTab" require "tab.ProductionBlockTab" require "tab.ProductionLineTab" require "tab.ResourceTab" @@ -79,6 +81,7 @@ function Controller:prepare() table.insert(forms, ProductionLineTab("HMProductionLineTab")) table.insert(forms, ProductionBlockTab("HMProductionBlockTab")) + --table.insert(forms, EnergyTab("HMEnergyTab")) table.insert(forms, ResourceTab("HMResourceTab")) table.insert(forms, SummaryTab("HMSummaryTab")) table.insert(forms, StatisticTab("HMStatisticTab")) @@ -96,6 +99,7 @@ function Controller:prepare() table.insert(forms, RecipeEdition("HMRecipeEdition")) table.insert(forms, ProductEdition("HMProductEdition")) + --table.insert(forms, EnergyEdition("HMEnergyEdition")) table.insert(forms, RuleEdition("HMRuleEdition")) table.insert(forms, PreferenceEdition("HMPreferenceEdition")) @@ -644,6 +648,11 @@ function Controller:onEventAccessWrite(event) local model = Model.getModel() local model_id = User.getParameter("model_id") local current_block = User.getParameter("current_block") + local block = model.blocks[current_block] or {} + local selector_name = "HMRecipeSelector" + if model.blocks[current_block] ~= nil and model.blocks[current_block].isEnergy then + selector_name = "HMEnergySelector" + end if event.action == "change-tab" then if event.item1 == "HMProductionBlockTab" and event.item2 == "new" then @@ -682,7 +691,7 @@ function Controller:onEventAccessWrite(event) if event.action == "product-edition" then if event.button == defines.mouse_button_type.right then - self:send("on_gui_open", event, "HMRecipeSelector") + self:send("on_gui_open", event, selector_name) else self:send("on_gui_open", event, "HMProductEdition") end @@ -696,7 +705,7 @@ function Controller:onEventAccessWrite(event) if event.action == "production-recipe-product-add" then if event.button == defines.mouse_button_type.right then - self:send("on_gui_open", event, "HMRecipeSelector") + self:send("on_gui_open", event, selector_name) else local recipes = Player.searchRecipe(event.item3, true) if #recipes == 1 then @@ -708,14 +717,14 @@ function Controller:onEventAccessWrite(event) else -- pour ouvrir avec le filtre ingredient event.button = defines.mouse_button_type.right - self:send("on_gui_open", event, "HMRecipeSelector") + self:send("on_gui_open", event, selector_name) end end end if event.action == "production-recipe-ingredient-add" then if event.button == defines.mouse_button_type.right then - self:send("on_gui_open", event, "HMRecipeSelector") + self:send("on_gui_open", event, selector_name) else local recipes = Player.searchRecipe(event.item3) if #recipes == 1 then @@ -725,7 +734,7 @@ function Controller:onEventAccessWrite(event) User.setParameter("scroll_element", new_recipe.id) self:send("on_gui_update", event) else - self:send("on_gui_open", event, "HMRecipeSelector") + self:send("on_gui_open", event, selector_name) end end end diff --git a/data/ModelBuilder.lua b/data/ModelBuilder.lua index be9d5766..f763f79f 100644 --- a/data/ModelBuilder.lua +++ b/data/ModelBuilder.lua @@ -270,6 +270,23 @@ function ModelBuilder.updateFactory(item, key, options) end end +------------------------------------------------------------------------------- +-- Update a factory +-- +-- @function [parent=#ModelBuilder] updateTemperatureFactory +-- +-- @param #string item block_id or resource +-- @param #string key object name +-- @param #table options +-- +function ModelBuilder.updateTemperatureFactory(item, key, options) + Logging:debug(ModelBuilder.classname, "updateTemperatureFactory()", item, key, options) + local object = Model.getObject(item, key) + if object ~= nil then + object.factory.target_temperature = options.target_temperature or 0 + end +end + ------------------------------------------------------------------------------- -- Update a factory -- diff --git a/data/ModelCompute.lua b/data/ModelCompute.lua index 5f0cefde..d9eb13c9 100644 --- a/data/ModelCompute.lua +++ b/data/ModelCompute.lua @@ -873,7 +873,7 @@ function ModelCompute.computeFactory(recipe) -- effet consumption local factory_prototype = EntityPrototype(recipe.factory) local energy_type = factory_prototype:getEnergyType() - recipe.factory.energy = factory_prototype:getMaxEnergyUsage() * (1 + recipe.factory.effects.consumption) + recipe.factory.energy = factory_prototype:getEnergyConsumption() * (1 + recipe.factory.effects.consumption) -- effet pollution recipe.factory.pollution = factory_prototype:getPollution() * (1 + recipe.factory.effects.pollution) @@ -898,7 +898,7 @@ function ModelCompute.computeFactory(recipe) recipe.factory.energy_total = 0 if energy_type == "burner" or energy_type == "fluid" then local energy_prototype = EntityPrototype(recipe.factory):getEnergySource() - local fuel_prototype = energy_prototype:getFuelPrototype(recipe.factory) + local fuel_prototype = energy_prototype:getFuelPrototype() fuel_emissions_multiplier = fuel_prototype:getFuelEmissionsMultiplier() end else @@ -924,6 +924,7 @@ end -- function ModelCompute.computeEnergyFactory(recipe) Logging:trace(ModelCompute.classname, "computeFactory()", recipe.name) + local model = Model.getModel() local recipe_prototype = RecipePrototype(recipe) local recipe_energy = recipe_prototype:getEnergy() -- effet speed @@ -934,14 +935,20 @@ function ModelCompute.computeEnergyFactory(recipe) -- effet consumption local factory_prototype = EntityPrototype(recipe.factory) + local energy_prototype = factory_prototype:getEnergySource() + local energy_type = factory_prototype:getEnergyType() - recipe.factory.energy = factory_prototype:getEnergyNominal() * (1 + recipe.factory.effects.consumption) + local gameDay = {day=12500,dust=5000,night=2500,dawn=2500} + if factory_prototype:getType() == EntityType.accumulator then + local dark_time = (gameDay.dust/2 + gameDay.night + gameDay.dawn / 2 ) + recipe_energy = dark_time/60 + end + recipe.factory.energy = factory_prototype:getEnergyConsumption() * (1 + recipe.factory.effects.consumption) -- effet pollution recipe.factory.pollution = factory_prototype:getPollution() * (1 + recipe.factory.effects.pollution) -- compte le nombre de machines necessaires - local model = Model.getModel() -- [ratio recipe] * [effort necessaire du recipe] / ([la vitesse de la factory] local count = recipe.count*recipe_energy/recipe.factory.speed if recipe.factory.speed == 0 then count = 0 end @@ -951,12 +958,12 @@ function ModelCompute.computeEnergyFactory(recipe) if energy_type ~= "electric" then recipe.factory.energy_total = 0 if energy_type == "burner" or energy_type == "fluid" then - local energy_prototype = EntityPrototype(recipe.factory):getEnergySource() - local fuel_prototype = energy_prototype:getFuelPrototype(recipe.factory) + local fuel_prototype = energy_prototype:getFuelPrototype() fuel_emissions_multiplier = fuel_prototype:getFuelEmissionsMultiplier() end else - recipe.factory.energy_total = math.ceil(recipe.factory.count*recipe.factory.energy) + recipe.factory.energy_total = 0 + --recipe.factory.energy_total = math.ceil(recipe.factory.count*recipe.factory.energy) end recipe.factory.pollution_total = recipe.factory.pollution * recipe.factory.count * model.time @@ -1186,12 +1193,12 @@ function ModelCompute.computePower(key) local secondary_prototype = EntityPrototype(power.secondary.name) if primary_prototype:getType() == EntityType.generator then -- calcul primary - local count = math.ceil( power.power / primary_prototype:getEnergyNominal() ) + local count = math.ceil( power.power / primary_prototype:getEnergyConsumption() ) power.primary.count = count or 0 -- calcul secondary if secondary_prototype:native() ~= nil and secondary_prototype:getType() == EntityType.boiler then local count = 0 - count = math.ceil( power.power / secondary_prototype:getEnergyNominal() ) + count = math.ceil( power.power / secondary_prototype:getEnergyConsumption() ) power.secondary.count = count or 0 else power.secondary.count = 0 @@ -1199,7 +1206,7 @@ function ModelCompute.computePower(key) end if primary_prototype:getType() == EntityType.solar_panel then -- calcul primary - local count = math.ceil( power.power / primary_prototype:getEnergyNominal() ) + local count = math.ceil( power.power / primary_prototype:getEnergyConsumption() ) power.primary.count = count or 0 -- calcul secondary if secondary_prototype:native() ~= nil and secondary_prototype:getType() == EntityType.accumulator then @@ -1209,10 +1216,11 @@ function ModelCompute.computePower(key) -- selon les aires il faut de l'accu en dehors du jour selon le trapese journalier local accu= (gameDay.dust/factor + gameDay.night + gameDay.dawn / factor ) / ( gameDay.day ) -- puissance nominale la nuit - local count1 = power.power/ secondary_prototype:getElectricOutputFlowLimit() + local energy_prototype = secondary_prototype:getEnergySource() + local count1 = power.power/ energy_prototype:getOutputFlowLimit() -- puissance durant la penombre -- formula (puissance*durree_penombre)/(60s*capacite) - local count2 = power.power*( gameDay.dust / factor + gameDay.night + gameDay.dawn / factor ) / ( 60 * secondary_prototype:getElectricBufferCapacity() ) + local count2 = power.power*( gameDay.dust / factor + gameDay.night + gameDay.dawn / factor ) / ( 60 * energy_prototype:getBufferCapacity() ) Logging:debug(ModelCompute.classname , "********** computePower result:", accu, count1, count2) if count1 > count2 then @@ -1242,7 +1250,7 @@ function ModelCompute.speedFactory(recipe) local factory_prototype = EntityPrototype(recipe.factory) -- info energy 1J=1W local power_extract = factory_prototype:getPowerExtract() - local power_usage = factory_prototype:getMaxEnergyUsage() + local power_usage = factory_prototype:getEnergyConsumption() Logging:debug(ModelCompute.classname, "power_extract", power_extract, "power_usage", power_usage, "fluid", power_usage/power_extract) return power_usage/power_extract elseif recipe.type == "resource" then diff --git a/edition/EnergyEdition.lua b/edition/EnergyEdition.lua new file mode 100644 index 00000000..42686921 --- /dev/null +++ b/edition/EnergyEdition.lua @@ -0,0 +1,558 @@ +------------------------------------------------------------------------------- +-- Class to build power edition dialog +-- +-- @module EnergyEdition +-- @extends #AbstractEdition +-- + +EnergyEdition = newclass(Form) + +local input_quantity = nil +------------------------------------------------------------------------------- +-- On initialization +-- +-- @function [parent=#EnergyEdition] onInit +-- +function EnergyEdition:onInit() + self.panelCaption = ({"helmod_energy-edition-panel.title"}) +end + +------------------------------------------------------------------------------- +-- Get or create panel +-- +-- @function [parent=#EnergyEdition] getPowerPanel +-- +function EnergyEdition:getPowerPanel() + local flow_panel, content_panel, menu_panel = self:getPanel() + if content_panel["power"] ~= nil and content_panel["power"].valid then + return content_panel["power"] + end + local panel = GuiElement.add(content_panel, GuiFrameH("power"):style(helmod_frame_style.panel)) + panel.style.horizontally_stretchable = true + return panel +end + +------------------------------------------------------------------------------- +-- Get or create generator panel +-- +-- @function [parent=#EnergyEdition] getPrimaryPanel +-- +function EnergyEdition:getPrimaryPanel() + local flow_panel, content_panel, menu_panel = self:getPanel() + if content_panel["Primary"] ~= nil and content_panel["Primary"].valid then + return content_panel["Primary"] + end + return GuiElement.add(content_panel, GuiTable("Primary"):column(2):style(helmod_table_style.panel)) +end + +------------------------------------------------------------------------------- +-- Get or create info panel +-- +-- @function [parent=#EnergyEdition] getPrimaryInfoPanel +-- +function EnergyEdition:getPrimaryInfoPanel() + local panel = self:getPrimaryPanel() + if panel["info"] ~= nil and panel["info"].valid then + return panel["info"] + end + local panel = GuiElement.add(panel, GuiFrameV("info"):style(helmod_frame_style.panel):caption({"helmod_common.primary-generator"})) + GuiElement.setStyle(panel, "power", "width") + GuiElement.setStyle(panel, "power", "height") + panel.style.horizontally_stretchable = true + return panel +end + +------------------------------------------------------------------------------- +-- Get or create selector panel +-- +-- @function [parent=#EnergyEdition] getPrimarySelectorPanel +-- +function EnergyEdition:getPrimarySelectorPanel() + local panel = self:getPrimaryPanel() + if panel["selector"] ~= nil and panel["selector"].valid then + return panel["selector"]["scroll-primary"] + end + local panel = GuiElement.add(panel, GuiFrameV("selector"):style(helmod_frame_style.panel):caption({"helmod_common.generator"})) + panel.style.horizontally_stretchable = true + GuiElement.setStyle(panel, "power", "width") + GuiElement.setStyle(panel, "power", "height") + local scroll_panel = GuiElement.add(panel, GuiScroll("scroll-primary"):style(helmod_frame_style.scroll_pane)) + scroll_panel.style.horizontally_stretchable = true + scroll_panel.style.vertically_stretchable = true + return scroll_panel +end + +------------------------------------------------------------------------------- +-- Build primary panel +-- +-- @function [parent=#EnergyEdition] buildPrimaryPanel +-- +function EnergyEdition:buildPrimaryPanel() + Logging:debug(self.classname, "buildPrimaryPanel()") + self:getPrimaryInfoPanel() + self:getPrimarySelectorPanel() +end + +------------------------------------------------------------------------------- +-- Get or create generator panel +-- +-- @function [parent=#EnergyEdition] getSecondaryPanel +-- +function EnergyEdition:getSecondaryPanel() + local flow_panel, content_panel, menu_panel = self:getPanel() + if content_panel["Secondary"] ~= nil and content_panel["Secondary"].valid then + return content_panel["Secondary"] + end + return GuiElement.add(content_panel, GuiTable("Secondary"):column(2):style(helmod_table_style.panel)) +end + +------------------------------------------------------------------------------- +-- Get or create info panel +-- +-- @function [parent=#EnergyEdition] getSecondaryInfoPanel +-- +function EnergyEdition:getSecondaryInfoPanel() + local panel = self:getSecondaryPanel() + if panel["info"] ~= nil and panel["info"].valid then + return panel["info"] + end + local panel = GuiElement.add(panel, GuiFrameV("info"):style(helmod_frame_style.panel):caption({"helmod_common.secondary-generator"})) + GuiElement.setStyle(panel, "power", "width") + GuiElement.setStyle(panel, "power", "height") + panel.style.horizontally_stretchable = true + return panel +end + +------------------------------------------------------------------------------- +-- Get or create selector panel +-- +-- @function [parent=#EnergyEdition] getSecondarySelectorPanel +-- +function EnergyEdition:getSecondarySelectorPanel() + local panel = self:getSecondaryPanel() + if panel["selector"] ~= nil and panel["selector"].valid then + return panel["selector"] + end + local panel = GuiElement.add(panel, GuiFrameV("selector"):style(helmod_frame_style.panel):caption({"helmod_common.generator"})) + panel.style.horizontally_stretchable = true + GuiElement.setStyle(panel, "power", "width") + GuiElement.setStyle(panel, "power", "height") + local scroll_panel = GuiElement.add(panel, GuiScroll("scroll-primary"):style(helmod_frame_style.scroll_pane)) + scroll_panel.style.horizontally_stretchable = true + scroll_panel.style.vertically_stretchable = true + return scroll_panel +end + +------------------------------------------------------------------------------- +-- Build Secondary panel +-- +-- @function [parent=#EnergyEdition] buildSecondaryPanel +-- +function EnergyEdition:buildSecondaryPanel() + Logging:debug(self.classname, "buildSecondaryPanel()") + self:getSecondaryInfoPanel() + self:getSecondarySelectorPanel() +end + +------------------------------------------------------------------------------- +-- Build header panel +-- +-- @function [parent=#EnergyEdition] buildHeaderPanel +-- +function EnergyEdition:buildHeaderPanel() + Logging:debug(self.classname, "buildHeaderPanel()") + self:getPowerPanel() +end + +------------------------------------------------------------------------------- +-- Get object +-- +-- @function [parent=#EnergyEdition] getObject +-- +-- @param #LuaEvent event +-- +function EnergyEdition:getObject(event) + local model = Model.getModel() + if model.powers ~= nil and model.powers[event.item1] ~= nil then + -- return power + return model.powers[event.item1] + end + return nil +end + +------------------------------------------------------------------------------- +-- On before open +-- +-- @function [parent=#EnergyEdition] onBeforeOpen +-- +-- @param #LuaEvent event +-- +function EnergyEdition:onBeforeOpen(event) + local model = Model.getModel() + model.primaryGroupSelected = nil + model.secondaryGroupSelected = nil +end + +------------------------------------------------------------------------------- +-- On event +-- +-- @function [parent=#EnergyEdition] onEvent +-- +-- @param #LuaEvent event +-- +function EnergyEdition:onEvent(event) + Logging:debug(self.classname, "onEvent()", event) + local model = Model.getModel() + + if event.action == "primary-group" then + model.primaryGroupSelected = event.item2 + self:updatePrimarySelector(event) + end + + if event.action == "secondary-group" then + model.secondaryGroupSelected = event.item2 + self:updateSecondarySelector(event) + end + + if User.isWriter() then + if event.action == "power-update" then + local options = {} + local operation = input_quantity.text + local ok , err = pcall(function() + local quantity = formula(operation) + if quantity == 0 then quantity = nil end + options["power"] = quantity + ModelBuilder.updatePower(event.item1, options) + self:updatePowerInfo(event) + Controller:send("on_gui_refresh", event) + end) + if not(ok) then + Player.print("Formula is not valid!") + end + end + + if event.action == "primary-select" then + local object = self:getObject(event) + if object ~= nil then + local power = ModelBuilder.addPrimaryPower(event.item1, event.item2) + else + local power = ModelBuilder.addPrimaryPower(nil, event.item2) + event.item1 = power.id + end + ModelCompute.computePower(event.item1) + Controller:send("on_gui_update", event) + end + + if event.action == "secondary-select" then + local object = self:getObject(event) + if object ~= nil then + local power = ModelBuilder.addSecondaryPower(event.item1, event.item2) + else + local power = ModelBuilder.addSecondaryPower(nil, event.item2) + event.item1 = power.id + end + ModelCompute.computePower(event.item1) + Controller:send("on_gui_update", event) + end + end +end + +------------------------------------------------------------------------------- +-- On update +-- +-- @function [parent=#EnergyEdition] onUpdate +-- +-- @param #LuaEvent event +-- +function EnergyEdition:onUpdate(event) + self:updatePowerInfo(event) + self:updatePrimary(event) + self:updateSecondary(event) +end + +------------------------------------------------------------------------------- +-- Update information +-- +-- @function [parent=#EnergyEdition] updatePowerInfo +-- +-- @param #LuaEvent event +-- + +function EnergyEdition:updatePowerInfo(event) + Logging:debug(self.classname, "updatePowerInfo()", event) + local power_panel = self:getPowerPanel() + local model = Model.getModel() + local default = Model.getDefault() + + local model = Model.getModel() + if model.powers ~= nil and model.powers[event.item1] ~= nil then + local power = self:getObject(event) + if power ~= nil then + Logging:debug(self.classname, "updatePowerInfo():power=",power) + for k,guiName in pairs(power_panel.children_names) do + power_panel[guiName].destroy() + end + + local table_panel = GuiElement.add(power_panel, GuiTable("table-input"):column(2)) + + GuiElement.add(table_panel, GuiLabel("label-power"):caption({"helmod_energy-edition-panel.power"})) + local cell, button + local power_value = math.ceil(power.power/1000)/1000 + cell, input_quantity, button = GuiCellInput(self.classname, "power-update", event.item1, power.id):text(power_value or 0):create(table_panel) + input_quantity.focus() + input_quantity.select_all() + end + end +end +------------------------------------------------------------------------------- +-- Update Primary +-- +-- @function [parent=#EnergyEdition] updatePrimary +-- +-- @param #LuaEvent event +-- +function EnergyEdition:updatePrimary(event) + Logging:debug(self.classname, "updatePrimary()", event) + local model = Model.getModel() + + self:updatePrimaryInfo(event) + self:updatePrimarySelector(event) +end + +------------------------------------------------------------------------------- +-- Update information +-- +-- @function [parent=#EnergyEdition] updatePrimaryInfo +-- +-- @param #LuaEvent event +-- +function EnergyEdition:updatePrimaryInfo(event) + Logging:debug(self.classname, "updatePrimaryInfo()", event) + local infoPanel = self:getPrimaryInfoPanel() + local object = self:getObject(event) + local model = Model.getModel() + + for k,guiName in pairs(infoPanel.children_names) do + infoPanel[guiName].destroy() + end + + if object ~= nil then + Logging:debug(self.classname, "updatePrimaryInfo():object:",object) + local primary = object.primary + if primary.name ~= nil then + + local headerPanel = GuiElement.add(infoPanel, GuiTable("table-header"):column(2)) + local tooltip = ({"tooltip.selector-module"}) + if model.module_panel == true then tooltip = ({"tooltip.selector-factory"}) end + GuiElement.add(headerPanel, GuiButtonSprite(self.classname, "do-nothing"):sprite(primary.type, primary.name):tooltip(tooltip)) + + local entity_prototype = EntityPrototype(primary.name) + if entity_prototype:native() ~= nil then + GuiElement.add(headerPanel, GuiLabel("label"):caption(entity_prototype:getLocalisedName())) + else + GuiElement.add(headerPanel, GuiLabel("label"):caption(primary.name)) + end + + local inputPanel = GuiElement.add(infoPanel, GuiTable("table-input"):column(2)) + + GuiElement.add(inputPanel, GuiLabel("label-energy-nominal"):caption({"helmod_label.energy-nominal"})) + GuiElement.add(inputPanel, GuiLabel("energy-nominal"):caption(Format.formatNumberKilo(entity_prototype:getEnergyConsumption(), "W"))) + + if entity_prototype:getType() == "generator" then + GuiElement.add(inputPanel, GuiLabel("label-maximum-temperature"):caption({"helmod_label.maximum-temperature"})) + GuiElement.add(inputPanel, GuiLabel("maximum-temperature"):caption(entity_prototype:getMaximumTemperature() or "NAN")) + + GuiElement.add(inputPanel, GuiLabel("label-fluid-usage"):caption({"helmod_label.fluid-usage"})) + GuiElement.add(inputPanel, GuiLabel("fluid-usage"):caption(entity_prototype:getFluidUsagePerTick() or "NAN")) + + GuiElement.add(inputPanel, GuiLabel("label-effectivity"):caption({"helmod_label.effectivity"})) + GuiElement.add(inputPanel, GuiLabel("effectivity"):caption(entity_prototype:getEffectivity() or "NAN")) + end + end + end +end + +------------------------------------------------------------------------------- +-- Update selector +-- +-- @function [parent=#EnergyEdition] updatePrimarySelector +-- +-- @param #LuaEvent event +-- +function EnergyEdition:updatePrimarySelector(event) + Logging:debug(self.classname, "updatePrimarySelector()", event) + local scroll_panel = self:getPrimarySelectorPanel() + local model = Model.getModel() + + scroll_panel.clear() + + local object = self:getObject(event) + + local groupsPanel = GuiElement.add(scroll_panel, GuiTable("primary-groups"):column(1)) + + local category = "primary" + if not(User.getModGlobalSetting("model_filter_generator")) then category = nil end + -- ajouter de la table des groupes de recipe + local factories = Player.getGenerators("primary") + Logging:debug(self.classname, "factories:",factories) + + + if category == nil then + local subgroups = {} + for key, factory in pairs(factories) do + local subgroup = factory.subgroup.name + if subgroup ~= nil then + if subgroups[subgroup] == nil then + subgroups[subgroup] = 1 + else + subgroups[subgroup] = subgroups[subgroup] + 1 + end + end + end + + for group, count in pairs(subgroups) do + -- set le groupe + if model.primaryGroupSelected == nil then model.primaryGroupSelected = group end + -- ajoute les icons de groupe + local action = GuiElement.add(groupsPanel, GuiButton(self.classname, "primary-group", event.item1, group):caption(group)) + end + end + + local tablePanel = GuiElement.add(scroll_panel, GuiTable("primary-table"):column(5)) + for key, element in pairs(factories) do + if category ~= nil or (element.subgroup ~= nil and element.subgroup.name == model.primaryGroupSelected) then + local localised_name = Player.getLocalisedName(element) + GuiElement.add(tablePanel, GuiButtonSelectSprite(self.classname, "primary-select", event.item1):sprite("entity", element.name):tooltip(localised_name)) + end + end +end + +------------------------------------------------------------------------------- +-- Update Secondary +-- +-- @function [parent=#EnergyEdition] updateSecondary +-- +-- @param #LuaEvent event +-- +function EnergyEdition:updateSecondary(event) + Logging:debug(self.classname, "updateSecondary()", event) + local model = Model.getModel() + + self:updateSecondaryInfo(event) + self:updateSecondarySelector(event) +end + +------------------------------------------------------------------------------- +-- Update information +-- +-- @function [parent=#EnergyEdition] updateSecondaryInfo +-- +-- @param #LuaEvent event +-- +function EnergyEdition:updateSecondaryInfo(event) + Logging:debug(self.classname, "updateSecondaryInfo()", event) + local infoPanel = self:getSecondaryInfoPanel() + local object = self:getObject(event) + local model = Model.getModel() + + for k,guiName in pairs(infoPanel.children_names) do + infoPanel[guiName].destroy() + end + + if object ~= nil then + Logging:debug(self.classname, "updateSecondaryInfo():object:",object) + local secondary = object.secondary + if secondary.name ~= nil then + + local headerPanel = GuiElement.add(infoPanel, GuiTable("table-header"):column(2)) + local tooltip = ({"tooltip.selector-module"}) + if model.module_panel == true then tooltip = ({"tooltip.selector-factory"}) end + GuiElement.add(headerPanel, GuiButtonSelectSprite(self.classname, "do-nothing"):sprite(secondary.type, secondary.name):tooltip(tooltip)) + + local entity_prototype = EntityPrototype(secondary.name) + if entity_prototype:native() ~= nil then + GuiElement.add(headerPanel, GuiLabel("label"):caption(entity_prototype:getLocalisedName())) + else + GuiElement.add(headerPanel, GuiLabel("label"):caption(secondary.name)) + end + + local inputPanel = GuiElement.add(infoPanel, GuiTable("table-input"):column(2)) + + if entity_prototype:getType() == EntityType.boiler then + GuiElement.add(inputPanel, GuiLabel("label-energy-nominal"):caption({"helmod_label.energy-nominal"})) + GuiElement.add(inputPanel, GuiLabel("energy-nominal"):caption(Format.formatNumberKilo(entity_prototype:getEnergyConsumption(), "W"))) + + GuiElement.add(inputPanel, GuiLabel("label-effectivity"):caption({"helmod_label.effectivity"})) + GuiElement.add(inputPanel, GuiLabel("effectivity"):caption(entity_prototype:getEffectivity())) + end + + if entity_prototype:getType() == EntityType.accumulator then + local energy_prototype = entity_prototype:getEnergySource() + GuiElement.add(inputPanel, GuiLabel("label-buffer-capacity"):caption({"helmod_label.buffer-capacity"})) + GuiElement.add(inputPanel, GuiLabel("buffer-capacity"):caption(Format.formatNumberKilo(energy_prototype:getBufferCapacity(), "J"))) + + GuiElement.add(inputPanel, GuiLabel("label-input_flow_limit"):caption({"helmod_label.input-flow-limit"})) + GuiElement.add(inputPanel, GuiLabel("input-flow-limit"):caption(Format.formatNumberKilo(energy_prototype:getInputFlowLimit(), "W"))) + + GuiElement.add(inputPanel, GuiLabel("label-output-flow-limit"):caption({"helmod_label.output-flow-limit"})) + GuiElement.add(inputPanel, GuiLabel("output-flow-limit"):caption(Format.formatNumberKilo(energy_prototype:getOutputFlowLimit(), "W"))) + end + + end + end +end + +------------------------------------------------------------------------------- +-- Update selector +-- +-- @function [parent=#EnergyEdition] updateSecondarySelector +-- +-- @param #LuaEvent event +-- +function EnergyEdition:updateSecondarySelector(event) + Logging:debug(self.classname, "updateSecondarySelector()", event) + local scroll_panel = self:getSecondarySelectorPanel() + local model = Model.getModel() + + scroll_panel.clear() + + local object = self:getObject(event) + + local groupsPanel = GuiElement.add(scroll_panel, GuiTable("secondary-groups"):column(1)) + + local category = "secondary" + if not(User.getModGlobalSetting("model_filter_generator")) then category = nil end + -- ajouter de la table des groupes de recipe + local factories = Player.getGenerators("secondary") + Logging:debug(self.classname, "factories:",factories) + + + if category == nil then + local subgroups = {} + for key, factory in pairs(factories) do + local subgroup = factory.subgroup.name + if subgroup ~= nil then + if subgroups[subgroup] == nil then + subgroups[subgroup] = 1 + else + subgroups[subgroup] = subgroups[subgroup] + 1 + end + end + end + + for group, count in pairs(subgroups) do + -- set le groupe + if model.secondaryGroupSelected == nil then model.secondaryGroupSelected = group end + -- ajoute les icons de groupe + local action = GuiElement.add(groupsPanel, GuiButton(self.classname, "secondary-group", event.item1, group):caption(group)) + end + end + + local tablePanel = GuiElement.add(scroll_panel, GuiTable("secondary-table"):column(5)) + for key, element in pairs(factories) do + if category ~= nil or (element.subgroup ~= nil and element.subgroup.name == model.secondaryGroupSelected) then + local localised_name = Player.getLocalisedName(element) + GuiElement.add(tablePanel, GuiButtonSelectSprite(self.classname, "secondary-select", event.item1):sprite("entity", element.name):tooltip(localised_name)) + end + end +end diff --git a/edition/ProductEdition.lua b/edition/ProductEdition.lua index b41a79a3..33a29b35 100644 --- a/edition/ProductEdition.lua +++ b/edition/ProductEdition.lua @@ -85,8 +85,8 @@ local product_count = 0 function ProductEdition:onUpdate(event) local model = Model.getModel() product = nil - if model.blocks[event.item1] ~= nil then - local block = model.blocks[event.item1] + local block = model.blocks[event.item1] + if block ~= nil then local block_elements = block.products if block.by_product == false then block_elements = block.ingredients @@ -99,8 +99,9 @@ function ProductEdition:onUpdate(event) end self:updateInfo(event) - self:updateTool(event) - --self:updateAction(event) + if block == nil or block.isEnergy ~= true then + self:updateTool(event) + end end ------------------------------------------------------------------------------- @@ -117,15 +118,26 @@ function ProductEdition:updateInfo(event) if product ~= nil then info_panel.clear() + local model = Model.getModel() + local block = model.blocks[event.item1] + local table_panel = GuiElement.add(info_panel, GuiTable("input-table"):column(2)) GuiElement.add(table_panel, GuiButtonSprite("product"):sprite(product.type, product.name)) GuiElement.add(table_panel, GuiLabel("product-label"):caption(Player.getLocalisedName(product))) - GuiElement.add(table_panel, GuiLabel("quantity-label"):caption({"helmod_common.quantity"})) + local caption = {"helmod_common.quantity"} + local count = product_count or 0 + if block.isEnergy then + caption = {"", {"helmod_common.quantity"}, "(MW)"} + count = count/1e6 + end + + GuiElement.add(table_panel, GuiLabel("quantity-label"):caption(caption)) local cell, button - cell, input_quantity, button = GuiCellInput(self.classname, "product-update", event.item1, product.name):text(product_count or 0):create(table_panel) + cell, input_quantity, button = GuiCellInput(self.classname, "product-update", event.item1, product.name):text(count):create(table_panel) input_quantity.focus() input_quantity.select_all() + input_quantity.style.minimal_width = 100 end end @@ -175,10 +187,14 @@ function ProductEdition:onEvent(event) if User.isWriter() then if event.action == "product-update" then local products = {} + local block = model.blocks[event.item1] local operation = input_quantity.text local ok , err = pcall(function() local quantity = formula(operation) + if block ~= nil and block.isEnergy then + quantity = quantity * 1e6 + end if quantity == 0 then quantity = nil end ModelBuilder.updateProduct(event.item1, event.item2, quantity) ModelCompute.update() diff --git a/edition/RecipeEdition.lua b/edition/RecipeEdition.lua index 118e936a..f5cc7414 100644 --- a/edition/RecipeEdition.lua +++ b/edition/RecipeEdition.lua @@ -140,12 +140,12 @@ function RecipeEdition:getFactoryTablePanel() local table_panel = GuiElement.add(content_panel, GuiTable(table_name):column(2)) table_panel.vertical_centering = false local factory_info_panel = GuiElement.add(table_panel, GuiFlowV(factory_info_name)) - factory_info_panel.style.width = 250 + factory_info_panel.style.minimal_width = 250 GuiElement.add(factory_info_panel, GuiLabel("factory_label"):caption({"helmod_common.factory"}):style("helmod_label_title_frame")) local factory_module_panel = GuiElement.add(table_panel, GuiFlowV(factory_module_name)) - factory_module_panel.style.width = 300 + factory_module_panel.style.minimal_width = 300 return factory_info_panel, factory_module_panel end @@ -200,9 +200,9 @@ function RecipeEdition:getBeaconPanel() local table_panel = GuiElement.add(content_panel, GuiTable(table_name):column(2)) table_panel.vertical_centering = false local beacon_info_panel = GuiElement.add(table_panel, GuiFlowV(beacon_info_name)) - beacon_info_panel.style.width = 250 + beacon_info_panel.style.minimal_width = 250 local beacon_module_panel = GuiElement.add(table_panel, GuiFlowV(beacon_module_name)) - beacon_module_panel.style.width = 300 + beacon_module_panel.style.minimal_width = 300 return beacon_info_panel, beacon_module_panel end @@ -284,6 +284,24 @@ function RecipeEdition:onEvent(event) end end + if event.action == "factory-temperature" then + local options = {} + + local text = event.element.text + local ok , err = pcall(function() + options["target_temperature"] = formula(text) or 0 + + ModelBuilder.updateTemperatureFactory(block.id, recipe.id, options) + ModelCompute.update() + self:updateFactoryInfo(event) + self:updateHeader(event) + Controller:send("on_gui_refresh", event) + end) + if not(ok) then + Player.print("Formula is not valid!") + end + end + if event.action == "factory-fuel-update" then local index = event.element.selected_index @@ -699,7 +717,8 @@ function RecipeEdition:updateFactoryInfo(event) local sign = "" if factory.effects.consumption > 0 then sign = "+" end GuiElement.add(input_panel, GuiLabel("energy"):caption(Format.formatNumberKilo(factory.energy, "W").." ("..sign..Format.formatPercent(factory.effects.consumption).."%)")) - -- solid burner + + -- burner local energy_type = factory_prototype:getEnergyType() if energy_type == "burner" or energy_type == "fluid" then local fuel_type = "item" @@ -709,13 +728,18 @@ function RecipeEdition:updateFactoryInfo(event) GuiElement.add(input_panel, GuiLabel("label-burner"):caption({"helmod_common.resource"})) local energy_prototype = factory_prototype:getEnergySource() local fuel_list = energy_prototype:getFuelPrototypes() - local factory_fuel = energy_prototype:getFuelPrototype(factory) + local factory_fuel = energy_prototype:getFuelPrototype() local items = {} for _,item in pairs(fuel_list) do table.insert(items,string.format("[%s=%s]", fuel_type, item.name)) end local default_fuel = string.format("[%s=%s]", fuel_type, factory_fuel:native().name) GuiElement.add(input_panel, GuiDropDown(self.classname, "factory-fuel-update", block.id, recipe.id, fuel_type):items(items, default_fuel)) + +-- if factory_fuel:native().name == "steam" then +-- GuiElement.add(input_panel, GuiLabel("label-temperature"):caption({"helmod_common.temperature"})) +-- GuiElement.add(input_panel, GuiTextField(self.classname, "factory-temperature", block.id, recipe.id):text(factory.target_temperature or 165):isNumeric()) +-- end end local sign = "" diff --git a/gui/GuiButton.lua b/gui/GuiButton.lua index cfb7dbd8..c1bd1dd2 100644 --- a/gui/GuiButton.lua +++ b/gui/GuiButton.lua @@ -38,6 +38,8 @@ function GuiButton:sprite(type, name, hovered) if hovered then self.options.hovered_sprite = GuiElement.getSprite(hovered) end + table.insert(self.name, name) + table.insert(self.name, type) else self.options.sprite = GuiElement.getSprite(type, name) if hovered then diff --git a/gui/GuiCell.lua b/gui/GuiCell.lua index 3f507a95..812de461 100644 --- a/gui/GuiCell.lua +++ b/gui/GuiCell.lua @@ -270,7 +270,9 @@ function GuiCellProductSm:create(parent) end local row3 = GuiElement.add(cell, GuiFrameH("row3"):style("helmod_frame_product", color, 3)) - GuiElement.add(row3, GuiLabel("label2", element.name):caption(Format.formatNumber(element.count, 5)):style("helmod_label_element_sm"):tooltip({"helmod_common.total"})) + local caption3 = Format.formatNumber(element.count, 5) + if element.type == "energy" then caption3 = Format.formatNumberKilo(element.count, "W") end + GuiElement.add(row3, GuiLabel("label2", element.name):caption(caption3):style("helmod_label_element_sm"):tooltip({"helmod_common.total"})) return cell end diff --git a/gui/GuiTextField.lua b/gui/GuiTextField.lua index d378719c..f5315319 100644 --- a/gui/GuiTextField.lua +++ b/gui/GuiTextField.lua @@ -26,4 +26,34 @@ end) function GuiTextField:text(text) self.options.text = text return self +end + +------------------------------------------------------------------------------- +-- +-- @function [parent=#GuiTextField] isNumeric +-- @return #GuiTextField +-- +function GuiTextField:isNumeric() + self.options.numeric = true + return self +end + +------------------------------------------------------------------------------- +-- +-- @function [parent=#GuiTextField] allowDecimal +-- @return #GuiTextField +-- +function GuiTextField:allowDecimal () + self.options.allow_decimal = true + return self +end + +------------------------------------------------------------------------------- +-- +-- @function [parent=#GuiTextField] allowNegative +-- @return #GuiTextField +-- +function GuiTextField:allowNegative () + self.options.allow_negative = true + return self end \ No newline at end of file diff --git a/gui/GuiTooltip.lua b/gui/GuiTooltip.lua index a202deb8..240c9f73 100644 --- a/gui/GuiTooltip.lua +++ b/gui/GuiTooltip.lua @@ -139,7 +139,11 @@ function GuiTooltipElement:create() if element ~= nil then local type = element.type if element == "resource" then type = "entity" end - table.insert(tooltip, {"", "\n", string.format("[%s=%s]", type, element.name), " ", helmod_tag.color.gold, helmod_tag.font.default_bold, Player.getLocalisedName({type=type, name=element.name}), helmod_tag.font.close, helmod_tag.color.close}) + local element_icon = string.format("[%s=%s]", type, element.name) + if type == "energy" and (element.name == "energy" or element.name == "steam-heat") then + element_icon = string.format("[img=helmod-%s-white]", type, element.name) + end + table.insert(tooltip, {"", "\n", element_icon, " ", helmod_tag.color.gold, helmod_tag.font.default_bold, Player.getLocalisedName({type=type, name=element.name}), helmod_tag.font.close, helmod_tag.color.close}) -- quantity local total_count = Format.formatNumberElement(element.count) if element.limit_count ~= nil then diff --git a/info.json b/info.json index 5425001f..a69676c0 100644 --- a/info.json +++ b/info.json @@ -1,11 +1,11 @@ { "name": "helmod", - "version": "0.10.5", + "version": "0.10.6", "title": "Helmod: assistant for planning your base.", "author": "Helfima", "contact": "Helfima", "homepage": "https://github.com/Helfima/helmod", "description": "Assistant to plan its base. calculates the needs, resources, factories, beacon or electric power.", - "dependencies": ["base >= 0.18"], + "dependencies": ["base >= 0.18.7"], "factorio_version": "0.18" } diff --git a/locale/en/helmod.cfg b/locale/en/helmod.cfg index b45b4918..cd3bd242 100644 --- a/locale/en/helmod.cfg +++ b/locale/en/helmod.cfg @@ -49,6 +49,7 @@ upload=Upload outflow=Outflow pollution=Pollution energy-consumption=Consumption +temperature=Temperature [helmod_button] save=Save diff --git a/locale/fr/helmod.cfg b/locale/fr/helmod.cfg index d0d20fe7..f2b688c0 100644 --- a/locale/fr/helmod.cfg +++ b/locale/fr/helmod.cfg @@ -41,6 +41,7 @@ upload=Export outflow=Débit pollution=Pollution energy-consumption=Consommation +temperature=Température [helmod_button] save=Enregistrer diff --git a/model/EnergySourcePrototype.lua b/model/EnergySourcePrototype.lua index b686c095..5d997a73 100644 --- a/model/EnergySourcePrototype.lua +++ b/model/EnergySourcePrototype.lua @@ -4,8 +4,9 @@ require "model.Prototype" -- -- @module EnergySourcePrototype -- -EnergySourcePrototype = newclass(Prototype,function(base,lua_prototype) +EnergySourcePrototype = newclass(Prototype,function(base, lua_prototype, factory) Prototype.init(base,lua_prototype) + base.factory = factory end) ------------------------------------------------------------------------------- -- Return emissions @@ -81,8 +82,8 @@ function ElectricSourcePrototype:getOutputFlowLimit() return 0 end -BurnerPrototype = newclass(EnergySourcePrototype,function(base,lua_prototype) - EnergySourcePrototype.init(base,lua_prototype) +BurnerPrototype = newclass(EnergySourcePrototype,function(base,lua_prototype, factory) + EnergySourcePrototype.init(base, lua_prototype, factory) end) ------------------------------------------------------------------------------- @@ -139,12 +140,10 @@ end -- -- @function [parent=#BurnerPrototype] getFuelPrototype -- --- @param #table factory --- -- @return #ItemPrototype item prototype -- -function BurnerPrototype:getFuelPrototype(factory) - local fuel = factory.fuel +function BurnerPrototype:getFuelPrototype() + local fuel = self.factory.fuel if fuel == nil then local first_fuel = self:getFirstFuelPrototype() fuel = first_fuel.name @@ -167,8 +166,8 @@ function BurnerPrototype:toString() return game.table_to_json(self:toData()) end -FluidSourcePrototype = newclass(EnergySourcePrototype,function(base,lua_prototype) - EnergySourcePrototype.init(base,lua_prototype) +FluidSourcePrototype = newclass(EnergySourcePrototype,function(base, lua_prototype, factory) + EnergySourcePrototype.init(base,lua_prototype, factory) end) ------------------------------------------------------------------------------- @@ -213,12 +212,10 @@ end -- -- @function [parent=#FluidSourcePrototype] getFuelPrototype -- --- @param #table factory --- -- @return #FluidPrototype item prototype -- -function FluidSourcePrototype:getFuelPrototype(factory) - local fuel = factory.fuel +function FluidSourcePrototype:getFuelPrototype() + local fuel = self.factory.fuel if fuel == nil then local first_fuel = self:getFirstFuelPrototype() fuel = first_fuel.name @@ -240,10 +237,10 @@ function FluidSourcePrototype:getFluidUsagePerTick() return 0 end -VoidSourcePrototype = newclass(FluidSourcePrototype,function(base,lua_prototype) - EnergySourcePrototype.init(base,lua_prototype) +VoidSourcePrototype = newclass(FluidSourcePrototype,function(base, lua_prototype, factory) + EnergySourcePrototype.init(base,lua_prototype, factory) end) -HeatSourcePrototype = newclass(EnergySourcePrototype,function(base,lua_prototype) - EnergySourcePrototype.init(base,lua_prototype) +HeatSourcePrototype = newclass(EnergySourcePrototype,function(base, lua_prototype, factory) + EnergySourcePrototype.init(base,lua_prototype, factory) end) diff --git a/model/EntityPrototype.lua b/model/EntityPrototype.lua index d62d3eba..9d3099f9 100644 --- a/model/EntityPrototype.lua +++ b/model/EntityPrototype.lua @@ -9,6 +9,7 @@ EntityPrototype = newclass(Prototype,function(base, object) elseif object ~= nil and object.name ~= nil then Prototype.init(base, Player.getEntityPrototype(object.name)) end + base.factory = object base.classname = "HMEntityPrototype" end) @@ -61,31 +62,23 @@ function EntityPrototype:getEnergyUsage() return 0 end -------------------------------------------------------------------------------- --- Return max energy usage per second --- --- @function [parent=#EntityPrototype] getMaxEnergyUsage --- --- @return #number default 0 --- -function EntityPrototype:getMaxEnergyUsage() - if self.lua_prototype ~= nil and self.lua_prototype.max_energy_usage ~= nil then - return self.lua_prototype.max_energy_usage*60 - end - return 0 -end - ------------------------------------------------------------------------------- -- Return extract power of fluid (boiler) in J -- -- @function [parent=#EntityPrototype] getPowerExtract -- +-- @param #number temperature +-- -- @return #number default 0 -- -function EntityPrototype:getPowerExtract() - if self.lua_prototype ~= nil and self.lua_prototype.target_temperature ~= nil then +function EntityPrototype:getPowerExtract(temperature) + if self.lua_prototype ~= nil then + local target_temperature = temperature or 165 + if self.lua_prototype.target_temperature ~= nil then + target_temperature = math.min(target_temperature , self.lua_prototype.target_temperature) + end -- [boiler.target_temperature]-15°c)x[200J/unit/°] - return (self.lua_prototype.target_temperature-15)*200 + return (target_temperature-15)*200 end return 0 end @@ -109,11 +102,11 @@ end -- @see https://wiki.factorio.com/Power_production -- @see https://wiki.factorio.com/Liquids/Hot -- --- @function [parent=#EntityPrototype] getEnergyNominal +-- @function [parent=#EntityPrototype] getEnergyConsumption -- -- @return #number default 0 -- -function EntityPrototype:getEnergyNominal() +function EntityPrototype:getEnergyConsumption() if self.lua_prototype ~= nil then if self.lua_prototype.type == EntityType.generator then local fluid_usage = self:getFluidUsagePerTick() @@ -123,12 +116,33 @@ function EntityPrototype:getEnergyNominal() -- @see https://wiki.factorio.com/Liquids/Hot return fluid_usage*60*effectivity*(maximum_temperature-15)*1000/5 end - if self.lua_prototype.type == EntityType.boiler then - return self:getMaxEnergyUsage() - end if self.lua_prototype.type == EntityType.solar_panel and self.lua_prototype.production ~= nil then return self.lua_prototype.production*60 or 0 end + local energy_type = self:getEnergyType() + if energy_type == "burner" or energy_type == "fluid" then + --return self:getFuelValue() + end + + return self:getMaxEnergyUsage() + end + return 0 +end + +------------------------------------------------------------------------------- +-- Return fuel value +-- +-- @function [parent=#EntityPrototype] getFuelValue +-- +-- @return #boolean +-- +function EntityPrototype:getFuelValue() + local energy_prototype = self:getEnergySource() + local factory_fuel = energy_prototype:getFuelPrototype() + if factory_fuel ~= nil then + local temperature = self.factory.target_temperature or self.lua_prototype.target_temperature + local fuel_value = factory_fuel:getFuelValue(temperature) + return fuel_value end return 0 end @@ -213,7 +227,7 @@ function EntityPrototype:getFluidConsumption() -- @see https://wiki.factorio.com/Heat_exchanger if self.lua_prototype ~= nil then local power_extract = self:getPowerExtract() - local power_usage = self:getMaxEnergyUsage() + local power_usage = self:getEnergyConsumption() -- fluid quantity = [boiler.max_energy_usage]*60/[boiler.target_temperature]-15°c)x[200J/unit/°] -- 1 water = 1 steam -- dans notre cas power_usage est deja en seconde (x60) @@ -320,11 +334,11 @@ end -- function EntityPrototype:getEnergySource() if self.lua_prototype ~= nil then - if self.lua_prototype.burner_prototype ~= nil then return BurnerPrototype(self.lua_prototype.burner_prototype) end - if self.lua_prototype.electric_energy_source_prototype ~= nil then return ElectricSourcePrototype(self.lua_prototype.electric_energy_source_prototype) end - if self.lua_prototype.heat_energy_source_prototype ~= nil then return HeatSourcePrototype(self.lua_prototype.heat_energy_source_prototype) end - if self.lua_prototype.fluid_energy_source_prototype ~= nil then return FluidSourcePrototype(self.lua_prototype.fluid_energy_source_prototype) end - if self.lua_prototype.void_energy_source_prototype ~= nil then return VoidSourcePrototype(self.lua_prototype.void_energy_source_prototype) end + if self.lua_prototype.burner_prototype ~= nil then return BurnerPrototype(self.lua_prototype.burner_prototype, self.factory) end + if self.lua_prototype.electric_energy_source_prototype ~= nil then return ElectricSourcePrototype(self.lua_prototype.electric_energy_source_prototype, self.factory) end + if self.lua_prototype.heat_energy_source_prototype ~= nil then return HeatSourcePrototype(self.lua_prototype.heat_energy_source_prototype, self.factory) end + if self.lua_prototype.fluid_energy_source_prototype ~= nil then return FluidSourcePrototype(self.lua_prototype.fluid_energy_source_prototype, self.factory) end + if self.lua_prototype.void_energy_source_prototype ~= nil then return VoidSourcePrototype(self.lua_prototype.void_energy_source_prototype, self.factory) end end return nil end @@ -498,10 +512,10 @@ end -- function EntityPrototype:getPollution() if self.lua_prototype ~= nil then - local energy_usage = self:getMaxEnergyUsage() + local energy_usage = self:getEnergyConsumption() local emission = 0 local energy_prototype = self:getEnergySource() - if energy_prototype ~= 0 then + if energy_prototype ~= nil then emission = energy_prototype:getEmissions() end return energy_usage * emission diff --git a/model/FluidPrototype.lua b/model/FluidPrototype.lua index 1b565f59..e034ec51 100644 --- a/model/FluidPrototype.lua +++ b/model/FluidPrototype.lua @@ -19,10 +19,11 @@ end) -- -- @return #boolean -- -function FluidPrototype:getFuelValue() +function FluidPrototype:getFuelValue(temperature) if self.lua_prototype == nil then return 0 end if self.lua_prototype.name == "steam" then - return (165-15)*200 + local target_temperature = temperature or 165 + return (target_temperature-15)*200 end return self.lua_prototype.fuel_value end diff --git a/model/Player.lua b/model/Player.lua index 42370498..919d5aff 100644 --- a/model/Player.lua +++ b/model/Player.lua @@ -734,7 +734,8 @@ function Player.getRecipeEntity(name) local prototype = entity_prototype:native() local type = "item" if name == "crude-oil" then type = "entity" end - local ingredients = {{name=prototype.name, type=type, amount=1}} + --local ingredients = {{name=prototype.name, type=type, amount=1}} + local ingredients = {} if entity_prototype:getMineableMiningFluidRequired() then local fluid_ingredient = {name=entity_prototype:getMineableMiningFluidRequired(), type="fluid", amount=entity_prototype:getMineableMiningFluidAmount()} table.insert(ingredients, fluid_ingredient) @@ -771,6 +772,9 @@ function Player.getRecipeFluid(name) local prototype = fluid_prototype:native() local products = {{name=prototype.name, type="fluid", amount=1}} local ingredients = {{name=prototype.name, type="fluid", amount=1}} + if prototype.name == "water" then + ingredients = {} + end if prototype.name == "steam" then ingredients = {{name="water", type="fluid", amount=1}} end diff --git a/model/RecipePrototype.lua b/model/RecipePrototype.lua index bb3d6ded..9ef63eb9 100644 --- a/model/RecipePrototype.lua +++ b/model/RecipePrototype.lua @@ -169,7 +169,7 @@ function RecipePrototype:getRawProducts() local products = {} local prototype = EntityPrototype(self.lua_prototype.name) if prototype:getType() == EntityType.solar_panel then - local amount = prototype:getEnergyNominal() + local amount = prototype:getEnergyConsumption() local product = {name="energy", type="energy", amount=amount} table.insert(products, product) end @@ -191,21 +191,19 @@ function RecipePrototype:getRawProducts() end if prototype:getType() == EntityType.accumulator then local energy_prototype = prototype:getEnergySource() - local gameDay = {day=12500,dust=5000,night=2500,dawn=2500} - local dark_ratio = (gameDay.dust / 2 + gameDay.night + gameDay.dawn / 2) - local amount = energy_prototype:getOutputFlowLimit()/60 + local amount = energy_prototype:getBufferCapacity() local product = {name="energy", type="energy", amount=amount} --Logging:debug("HMRecipePrototype", "capacity", energy_prototype:getBufferCapacity(), "dark", (gameDay.dust / 2 + gameDay.night + gameDay.dawn / 2)) --Logging:debug("HMRecipePrototype", "product", product) table.insert(products, product) end if prototype:getType() == EntityType.generator then - local amount = prototype:getEnergyNominal() + local amount = prototype:getEnergyConsumption() local product = {name="energy", type="energy", amount=amount} table.insert(products, product) end if prototype:getType() == EntityType.reactor then - local amount = prototype:getMaxEnergyUsage() + local amount = prototype:getEnergyConsumption() local product = {name="steam-heat", type="energy", amount=amount} table.insert(products, product) end @@ -250,7 +248,7 @@ function RecipePrototype:getRawIngredients() end end if prototype:getTargetTemperature() > 200 then - local amount = prototype:getMaxEnergyUsage() + local amount = prototype:getEnergyConsumption() local ingredient = {name="steam-heat", type="energy", amount=amount} table.insert(ingredients, ingredient) end @@ -258,8 +256,8 @@ function RecipePrototype:getRawIngredients() if prototype:getType() == EntityType.accumulator then local energy_prototype = prototype:getEnergySource() local gameDay = {day=12500,dust=5000,night=2500,dawn=2500} - local dark_ratio = (gameDay.dust / 2 + gameDay.night + gameDay.dawn / 2) - local amount = energy_prototype:getInputFlowLimit()/60 + local dark_ratio = (gameDay.dust/2 + gameDay.night + gameDay.dawn / 2 ) / ( gameDay.day ) + local amount = energy_prototype:getBufferCapacity() * (1+dark_ratio) local ingredient = {name="energy", type="energy", amount=amount} table.insert(ingredients, ingredient) end @@ -327,13 +325,15 @@ function RecipePrototype:getIngredients(factory) if energy_type == "burner" or energy_type == "fluid" then local energy_prototype = factory_prototype:getEnergySource() local burner_effectivity = energy_prototype:getEffectivity() - local factory_fuel = energy_prototype:getFuelPrototype(factory) - local fuel_value = (energy_usage/burner_effectivity)*(self:getEnergy()/speed_factory) - local burner_count = fuel_value/factory_fuel:getFuelValue() - local ingredient_type = "item" - if energy_type == "fluid" then ingredient_type = "fluid" end - local burner_ingredient = {name=factory_fuel:native().name, type=ingredient_type, amount=burner_count} - table.insert(raw_ingredients, burner_ingredient) + local factory_fuel = energy_prototype:getFuelPrototype() + if factory_fuel ~= nil then + local fuel_value = (energy_usage/burner_effectivity)*(self:getEnergy()/speed_factory) + local burner_count = fuel_value/factory_fuel:getFuelValue() + local ingredient_type = "item" + if energy_type == "fluid" then ingredient_type = "fluid" end + local burner_ingredient = {name=factory_fuel:native().name, type=ingredient_type, amount=burner_count} + table.insert(raw_ingredients, burner_ingredient) + end end end elseif self.lua_type == "resource" then @@ -355,15 +355,17 @@ function RecipePrototype:getIngredients(factory) if energy_type == "burner" or energy_type == "fluid" then local energy_prototype = factory_prototype:getEnergySource() local burner_effectivity = energy_prototype:getEffectivity() - local factory_fuel = energy_prototype:getFuelPrototype(factory) - local speed_factory = hardness * mining_speed / mining_time - --Logging:debug(RecipePrototype.classname, "resource burner", energy_usage,speed_factory,burner_effectivity,burner_emission) - local fuel_value = (energy_usage/burner_effectivity)*(1/speed_factory) - local burner_count = fuel_value/factory_fuel:getFuelValue() - local ingredient_type = "item" - if energy_type == "fluid" then ingredient_type = "fluid" end - local burner_ingredient = {name=factory_fuel:native().name, type=ingredient_type, amount=burner_count} - table.insert(raw_ingredients, burner_ingredient) + local factory_fuel = energy_prototype:getFuelPrototype() + if factory_fuel ~= nil then + local speed_factory = hardness * mining_speed / mining_time + --Logging:debug(RecipePrototype.classname, "resource burner", energy_usage,speed_factory,burner_effectivity,burner_emission) + local fuel_value = (energy_usage/burner_effectivity)*(1/speed_factory) + local burner_count = fuel_value/factory_fuel:getFuelValue() + local ingredient_type = "item" + if energy_type == "fluid" then ingredient_type = "fluid" end + local burner_ingredient = {name=factory_fuel:native().name, type=ingredient_type, amount=burner_count} + table.insert(raw_ingredients, burner_ingredient) + end end end elseif self.lua_type == "fluid" then @@ -374,34 +376,35 @@ function RecipePrototype:getIngredients(factory) if energy_type == "burner" or energy_type == "fluid" then local energy_prototype = factory_prototype:getEnergySource() local burner_effectivity = energy_prototype:getEffectivity() - local factory_fuel = energy_prototype:getFuelPrototype(factory) - -- source energy en kJ - local power_extract = factory_prototype:getPowerExtract() - local fuel_value = factory_fuel:getFuelValue() - local burner_count = power_extract/(fuel_value*burner_effectivity) - local ingredient_type = "item" - if energy_type == "fluid" then ingredient_type = "fluid" end - local burner_ingredient = {name=factory_fuel:native().name, type=ingredient_type, amount=burner_count} - table.insert(raw_ingredients, burner_ingredient) + local factory_fuel = energy_prototype:getFuelPrototype() + if factory_fuel ~= nil then + -- source energy en kJ + local burner_count = energy_prototype:getFluidUsagePerTick()*60 + local ingredient_type = "item" + if energy_type == "fluid" then ingredient_type = "fluid" end + local burner_ingredient = {name=factory_fuel:native().name, type=ingredient_type, amount=burner_count} + table.insert(raw_ingredients, burner_ingredient) + end end end end elseif self.lua_type == "energy" then local factory_prototype = EntityPrototype(factory) if factory ~= nil and factory_prototype:getEnergyType() ~= "electric" then - local energy_usage = factory_prototype:getMaxEnergyUsage() - + local energy_usage = factory_prototype:getEnergyConsumption() local energy_type = factory_prototype:getEnergyType() if energy_type == "burner" or energy_type == "fluid" then local energy_prototype = factory_prototype:getEnergySource() local burner_effectivity = energy_prototype:getEffectivity() - local factory_fuel = energy_prototype:getFuelPrototype(factory) - local fuel_value = factory_fuel:getFuelValue() - local burner_count = energy_usage/(fuel_value*burner_effectivity) - local ingredient_type = "item" - if energy_type == "fluid" then ingredient_type = "fluid" end - local burner_ingredient = {name=factory_fuel:native().name, type=ingredient_type, amount=burner_count * model.time} - table.insert(raw_ingredients, burner_ingredient) + local factory_fuel = energy_prototype:getFuelPrototype() + if factory_fuel ~= nil then + local fuel_value = factory_fuel:getFuelValue() + local burner_count = energy_usage/(fuel_value*burner_effectivity) + local ingredient_type = "item" + if energy_type == "fluid" then ingredient_type = "fluid" end + local burner_ingredient = {name=factory_fuel:native().name, type=ingredient_type, amount=burner_count * model.time} + table.insert(raw_ingredients, burner_ingredient) + end end end end diff --git a/tab/EnergyTab.lua b/tab/EnergyTab.lua new file mode 100644 index 00000000..0ff6e639 --- /dev/null +++ b/tab/EnergyTab.lua @@ -0,0 +1,129 @@ +require "tab.AbstractTab" +------------------------------------------------------------------------------- +-- Class to build tab +-- +-- @module EnergyTab +-- @extends #AbstractTab +-- + +EnergyTab = newclass(AbstractTab,function(base,classname) + AbstractTab.init(base,classname) +end) + +------------------------------------------------------------------------------- +-- Return button caption +-- +-- @function [parent=#EnergyTab] getButtonCaption +-- +-- @return #string +-- +function EnergyTab:getButtonCaption() + return {"helmod_result-panel.tab-button-energy"} +end + +------------------------------------------------------------------------------- +-- Get Button Sprites +-- +-- @function [parent=#EnergyTab] getButtonSprites +-- +-- @return boolean +-- +function EnergyTab:getButtonSprites() + return "nuclear-white","nuclear" +end + +------------------------------------------------------------------------------- +-- Update data +-- +-- @function [parent=#EnergyTab] updateData +-- +function EnergyTab:updateData() + Logging:debug(self.classname, "updatePowers()") + local model = Model.getModel() + local order = User.getParameter("order") + + -- data + local scroll_panel = self:getResultScrollPanel() + local menu_manel = GuiElement.add(scroll_panel, GuiFrameH("menu"):style(helmod_frame_style.hidden)) + GuiElement.add(menu_manel, GuiButton("HMEnergyEdition", "OPEN", "new"):caption({"helmod_result-panel.add-button-power"})) + + local countBlock = Model.countPowers() + if model.powers ~= nil and countBlock > 0 then + + local extra_cols = 0 + if User.getModGlobalSetting("display_data_col_id") then + extra_cols = extra_cols + 1 + end + local resultTable = GuiElement.add(scroll_panel, GuiTable("list-data"):column(4 + extra_cols):style("helmod_table-odd")) + + self:addTableHeader(resultTable) + + local i = 0 + for _, element in spairs(model.powers, function(t,a,b) if order.ascendant then return t[b][order.name] > t[a][order.name] else return t[b][order.name] < t[a][order.name] end end) do + self:addTableRow(resultTable, element) + end + + end +end + +------------------------------------------------------------------------------- +-- Add table header +-- +-- @function [parent=#EnergyTab] addTableHeader +-- +-- @param #LuaGuiElement itable container for element +-- +function EnergyTab:addTableHeader(itable) + Logging:debug(self.classname, "addTableHeader()", itable) + local model = Model.getModel() + + self:addCellHeader(itable, "action", {"helmod_result-panel.col-header-action"}) + -- optionnal columns + self:addCellHeader(itable, "id", {"helmod_result-panel.col-header-id"},"id") + -- data columns + self:addCellHeader(itable, "power", {"helmod_result-panel.col-header-energy"}) + self:addCellHeader(itable, "primary", {"helmod_result-panel.col-header-primary"}) + self:addCellHeader(itable, "secondary", {"helmod_result-panel.col-header-secondary"}) +end + +------------------------------------------------------------------------------- +-- Add row table +-- +-- @function [parent=#EnergyTab] addTableRow +-- +-- @param #LuaGuiElement itable container for element +-- @param #table power +-- +function EnergyTab:addTableRow(gui_table, power) + Logging:debug(self.classname, "addPowersRow()", gui_table, power) + local model = Model.getModel() + log(self.classname) + -- col action + local cell_action = GuiElement.add(gui_table, GuiFrameH("action", power.id):style(helmod_frame_style.hidden)) + GuiElement.add(cell_action, GuiButton(self.classname, "power-remove", power.id):sprite("menu", "delete-white", "delete"):style("helmod_button_menu_sm_red"):tooltip({"tooltip.remove-element"})) + log(self.classname) + + -- col id + if User.getModGlobalSetting("display_data_col_id") then + local cell_id = GuiElement.add(gui_table, GuiFrameH("id", power.id):style(helmod_frame_style.hidden)) + GuiElement.add(cell_id, GuiLabel("id"):caption(power.id)) + end + -- col power + local cell_power = GuiElement.add(gui_table, GuiFrameH("power", power.id):style(helmod_frame_style.hidden)) + GuiElement.add(cell_power, GuiLabel(power.id):caption(Format.formatNumberKilo(power.power, "W")):style("helmod_label_right_70")) + + -- col primary + local cell_primary = GuiElement.add(gui_table, GuiFrameH("primary",power.id):style(helmod_frame_style.hidden)) + local primary = power.primary + if primary.name ~= nil then + GuiElement.add(cell_primary, GuiLabel(primary.name):caption(Format.formatNumberFactory(primary.count)):style("helmod_label_right_60")) + GuiElement.add(cell_primary, GuiButtonSelectSprite("HMEnergyEdition", "OPEN", power.id):sprite(primary.type, primary.name):caption("X"..Format.formatNumberFactory(primary.count)):tooltip({"tooltip.edit-energy", Player.getLocalisedName(primary)})) + end + -- col secondary + local cell_secondary = GuiElement.add(gui_table, GuiFrameH("secondary", power.id):style(helmod_frame_style.hidden)) + local secondary = power.secondary + if secondary.name ~= nil then + GuiElement.add(cell_secondary, GuiLabel(secondary.name):caption(Format.formatNumberFactory(secondary.count)):style("helmod_label_right_60")) + GuiElement.add(cell_secondary, GuiButtonSelectSprite("HMEnergyEdition", "OPEN", power.id):sprite(secondary.type, secondary.name):caption("X"..Format.formatNumberFactory(secondary.count)):tooltip({"tooltip.edit-energy", Player.getLocalisedName(secondary)})) + end +end diff --git a/test_find.lua b/test_find.lua index 1f1b5d14..774f0dfd 100644 --- a/test_find.lua +++ b/test_find.lua @@ -1,2 +1,7 @@ local value = string.find("pure-natural-gas","pure-natural-gas",1,true) print(value) + + +local gameDay = {day=12500,dust=5000,night=2500,dawn=2500} +local dark_time = (gameDay.dust/2 + gameDay.night + gameDay.dawn / 2 ) +print(dark_time/60)