From 96d579813a4634f7e2dfadea948373ffd1c79b88 Mon Sep 17 00:00:00 2001 From: Helfima Date: Tue, 30 May 2017 22:23:56 +0200 Subject: [PATCH] Fixed crash when clicking on icon to open Fixed crash when clicking on icon to open Fixed mining productivity Added item, recipe and technology selector for properties tab Fixed amout error when click computing by factory --- controller/Controller.lua | 4 +- controller/PlayerController.lua | 15 +++- edition/AbstractEdition.lua | 3 +- helmod.lua | 1 + info.json | 2 +- locale/en/helmod.cfg | 9 ++- locale/fr/helmod.cfg | 4 +- model/Model.lua | 5 +- selector/AbstractSelector.lua | 42 ++++++++--- selector/EntitySelector.lua | 2 +- selector/ItemSelector.lua | 129 ++++++++++++++++++++++++++++++++ selector/RecipeSelector.lua | 5 +- tab/MainTab.lua | 5 +- tab/PropertiesTab.lua | 42 ++++++++--- 14 files changed, 239 insertions(+), 29 deletions(-) create mode 100644 selector/ItemSelector.lua diff --git a/controller/Controller.lua b/controller/Controller.lua index 438c2ecc..96eceb37 100644 --- a/controller/Controller.lua +++ b/controller/Controller.lua @@ -9,6 +9,7 @@ require "edition.EnergyEdition" require "selector.EntitySelector" require "selector.RecipeSelector" require "selector.TechnologySelector" +require "selector.ItemSelector" require "tab.MainTab" @@ -49,6 +50,7 @@ function Controller.methods:init(parent) table.insert(controllers, EnergyEdition:new(self)) table.insert(controllers, PinPanel:new(self)) table.insert(controllers, TechnologySelector:new(self)) + table.insert(controllers, ItemSelector:new(self)) for _,controller in pairs(controllers) do self.controllers[controller:classname()] = controller @@ -97,7 +99,7 @@ end -- function Controller.methods:on_gui_click(event) Logging:debug(self:classname(), "on_gui_click(event)") - if event.element then + if event.element and event.element.valid then if event.element.name == PLANNER_COMMAND then local player = game.players[event.player_index] self:main(player) diff --git a/controller/PlayerController.lua b/controller/PlayerController.lua index 92555885..582e1641 100644 --- a/controller/PlayerController.lua +++ b/controller/PlayerController.lua @@ -761,6 +761,19 @@ function PlayerController.methods:getResources() return items end +------------------------------------------------------------------------------- +-- Return item prototypes +-- +-- @function [parent=#PlayerController] getItemPrototypes +-- +-- @param #string item name +-- +-- @return #LuaItemPrototype item prototype +-- +function PlayerController.methods:getItemPrototypes() + return game.item_prototypes +end + ------------------------------------------------------------------------------- -- Return item prototype -- @@ -880,7 +893,7 @@ end function PlayerController.methods:getItemIconType(element) local item = self:getItemPrototype(element.name) if item ~= nil then - return item.type + return "item" end local fluid = self:getFluidPrototype(element.name) if fluid ~= nil then diff --git a/edition/AbstractEdition.lua b/edition/AbstractEdition.lua index 209a78b3..c7211eca 100644 --- a/edition/AbstractEdition.lua +++ b/edition/AbstractEdition.lua @@ -536,11 +536,12 @@ function AbstractEdition.methods:updateFactoryModulesSelector(player, element, a local factory = object.factory for k, module in pairs(self.player:getModules()) do local allowed = true + local factory_type = self.player:getEntityProperty(factory.name, "type") local consumption = self:formatPercent(self.player:getModuleBonus(module.name, "consumption")) local speed = self:formatPercent(self.player:getModuleBonus(module.name, "speed")) local productivity = self:formatPercent(self.player:getModuleBonus(module.name, "productivity")) local pollution = self:formatPercent(self.player:getModuleBonus(module.name, "pollution")) - if productivity > 0 and item ~= "resource" and model_filter_factory_module == true then + if productivity > 0 and factory_type ~= "mining-drill" and model_filter_factory_module == true then if module.limitations[object.name] == nil then allowed = false end end if factory.module_slots == 0 then diff --git a/helmod.lua b/helmod.lua index 1ad91c67..2ea4c379 100644 --- a/helmod.lua +++ b/helmod.lua @@ -53,6 +53,7 @@ end -- } -- function helmod:on_configuration_changed(data) + log("helmod:on_configuration_changed(data)") Logging:trace("helmod", "helmod:on_configuration_changed(data)", data) if not data or not data.mod_changes then return diff --git a/info.json b/info.json index be19d3cf..f75d7d68 100644 --- a/info.json +++ b/info.json @@ -1,7 +1,7 @@ { "name": "helmod", - "version": "0.5.2", + "version": "0.5.3", "title": "Helmod: Assistant to plan its base.", "author": "Helfima", "contact": "Helfima", diff --git a/locale/en/helmod.cfg b/locale/en/helmod.cfg index 7b4dba95..c4907bca 100644 --- a/locale/en/helmod.cfg +++ b/locale/en/helmod.cfg @@ -74,6 +74,9 @@ compute-by-factory=Computing by factory factory-number=Factory number [helmod_selector-panel] +entity-title=Entity selector +item-title=Item selector +fluid-title=Fluid selector recipe-title=Recipe selector technology-title=Technology selector @@ -138,7 +141,11 @@ add-button-production-block=Add Production Block add-button-recipe=Add a recipe add-button-power=Add power add-button-technology=Add technology -add-button-entity=Add entity +select-button-entity=Select entity +select-button-item=Select item +select-button-fluid=Select fluid +select-button-recipe=Select recipe +select-button-technology=Select technology row-button-delete=X row-button-up=U row-button-down=D diff --git a/locale/fr/helmod.cfg b/locale/fr/helmod.cfg index 492e41c5..21f90084 100644 --- a/locale/fr/helmod.cfg +++ b/locale/fr/helmod.cfg @@ -74,6 +74,9 @@ compute-by-factory=Calcul par usine factory-number=Nombre d'usine [helmod_selector-panel] +entity-title=Entity selector +item-title=Item selector +fluid-title=Fluid selector recipe-title=Sélecteur de recette technology-title=Sélecteur de technologie @@ -138,7 +141,6 @@ add-button-production-block=Ajouter bloc de production add-button-recipe=Ajouter une recette add-button-power=Ajouter une energie add-button-technology=Ajouter une technologie -add-button-entity=Ajouter une entité row-button-delete=X row-button-up=U row-button-down=D diff --git a/model/Model.lua b/model/Model.lua index 0f0c2688..97b5e12f 100644 --- a/model/Model.lua +++ b/model/Model.lua @@ -1458,6 +1458,9 @@ end -- @see http://lua-api.factorio.com/latest/Concepts.html#Product -- function Model.methods:getElementAmount(element) + Logging:debug(self:classname(), "getElementAmount",element) + if element == nil then return 0 end + if element.amount ~= nil then return element.amount end @@ -1541,7 +1544,7 @@ function Model.methods:computeProductionBlock(player, element, maxLoop, level, p element.input = {} -- formula [product amount] * (1 + [productivity]) *[assembly speed]*[time]/[recipe energy] element.input.key = first_product.name - element.input.quantity = first_product.amount * (1 + first_recipe.factory.effects.productivity) * ( element.factory_number or 0 ) * first_recipe.factory.speed * model.time / first_recipe.energy + element.input.quantity = self:getElementAmount(first_product) * (1 + first_recipe.factory.effects.productivity) * ( element.factory_number or 0 ) * first_recipe.factory.speed * model.time / first_recipe.energy end -- initialise la premiere recette avec le input diff --git a/selector/AbstractSelector.lua b/selector/AbstractSelector.lua index 1b36481d..63cf89c3 100644 --- a/selector/AbstractSelector.lua +++ b/selector/AbstractSelector.lua @@ -226,6 +226,8 @@ function AbstractSelector.methods:after_open(player, element, action, item, item self.parent:send_event(player, "HMRecipeEdition", "CLOSE") self.parent:send_event(player, "HMProductEdition", "CLOSE") self.parent:send_event(player, "HMSettings", "CLOSE") + if self:classname() ~= "HMItemSelector" then self.parent:send_event(player, "HMItemSelector", "CLOSE") end + if self:classname() ~= "HMEntitySelector" then self.parent:send_event(player, "HMEntitySelector", "CLOSE") end if self:classname() ~= "HMRecipeSelector" then self.parent:send_event(player, "HMRecipeSelector", "CLOSE") end if self:classname() ~= "HMTechnologySelector" then self.parent:send_event(player, "HMTechnologySelector", "CLOSE") end end @@ -247,21 +249,41 @@ function AbstractSelector.methods:on_event(player, element, action, item, item2, local globalPlayer = self.player:getGlobal(player) local globalSettings = self.player:getGlobal(player, "settings") local defaultSettings = self.player:getDefaultSettings() + local globalGui = self.player:getGlobalGui(player) local model = self.model:getModel(player) if self.player:isAdmin(player) or model.owner == player.name or (model.share ~= nil and bit32.band(model.share, 2) > 0) then - if action == "recipe-select" then - local productionBlock = self.parent.model:addRecipeIntoProductionBlock(player, item) - self.parent.model:update(player) - self.parent:refreshDisplayData(player) - self:close(player) - end - if action == "entity-select" then - globalPlayer["entity-properties"] = item - self.parent:refreshDisplayData(player) - self:close(player) + if globalGui.currentTab == "HMPropertiesTab" then + if action == "recipe-select" then + globalPlayer["prototype-properties"] = {name = item, type = "recipe" } + self.parent:refreshDisplayData(player) + self:close(player) + end + if action == "entity-select" then + globalPlayer["prototype-properties"] = {name = item, type = "entity" } + self.parent:refreshDisplayData(player) + self:close(player) + end + if action == "item-select" then + globalPlayer["prototype-properties"] = {name = item, type = "item" } + self.parent:refreshDisplayData(player) + self:close(player) + end + if action == "technology-select" then + globalPlayer["prototype-properties"] = {name = item, type = "technology" } + self.parent:refreshDisplayData(player) + self:close(player) + end + else + if action == "recipe-select" then + local productionBlock = self.parent.model:addRecipeIntoProductionBlock(player, item) + self.parent.model:update(player) + self.parent:refreshDisplayData(player) + self:close(player) + end end end + if action == "recipe-group" then globalPlayer.recipeGroupSelected = item self:on_update(player, element, action, item, item2, item3) diff --git a/selector/EntitySelector.lua b/selector/EntitySelector.lua index 757316b4..87c01880 100644 --- a/selector/EntitySelector.lua +++ b/selector/EntitySelector.lua @@ -16,7 +16,7 @@ EntitySelector = setclass("HMEntitySelector", AbstractSelector) -- @param #Controller parent parent controller -- function EntitySelector.methods:getCaption(parent) - return {"helmod_selector-panel.technology-title"} + return {"helmod_selector-panel.entity-title"} end ------------------------------------------------------------------------------- diff --git a/selector/ItemSelector.lua b/selector/ItemSelector.lua new file mode 100644 index 00000000..165565a4 --- /dev/null +++ b/selector/ItemSelector.lua @@ -0,0 +1,129 @@ +require "selector.AbstractSelector" +------------------------------------------------------------------------------- +-- Class to build technology selector +-- +-- @module ItemSelector +-- @extends #AbstractSelector +-- + +ItemSelector = setclass("HMItemSelector", AbstractSelector) + +------------------------------------------------------------------------------- +-- Return caption +-- +-- @function [parent=#ItemSelector] getCaption +-- +-- @param #Controller parent parent controller +-- +function ItemSelector.methods:getCaption(parent) + return {"helmod_selector-panel.item-title"} +end + +------------------------------------------------------------------------------- +-- Update groups +-- +-- @function [parent=#ItemSelector] updateGroups +-- +-- @param #LuaPlayer player +-- @param #LuaGuiElement element button +-- @param #string action action name +-- @param #string item first item name +-- @param #string item2 second item name +-- @param #string item3 third item name +-- +-- @return {groupList, prototypeGroups} +-- +function ItemSelector.methods:updateGroups(player, element, action, item, item2, item3) + Logging:trace(self:classname(), "on_update():",player, element, action, item, item2, item3) + local globalPlayer = self.player:getGlobal(player) + -- recuperation recipes + local prototypeGroups = {} + local groupList = {} + local prototypeFilter = self:getFilter() + local prototypeFilterProduct = self:getProductFilter() + + local firstGroup = nil + for key, prototype in spairs(self.player:getItemPrototypes(),function(t,a,b) return t[b]["order"] > t[a]["order"] end) do + -- ne traite pas les entity sans name + if prototype.name ~= nil then + local find = false + if prototypeFilter ~= nil and prototypeFilter ~= "" then + if prototypeFilterProduct == true then + local search = prototype.name:lower():gsub("[-]"," ") + if string.find(search, prototypeFilter) then + find = true + end + end + else + find = true + end + + local filter_show_hidden = self.player:getGlobalSettings(player, "filter_show_hidden") + if find == true and (prototype.valid == true or filter_show_hidden == true) then + if firstGroup == nil then firstGroup = prototype.group.name end + groupList[prototype.group.name] = prototype.group + if prototypeGroups[prototype.group.name] == nil then prototypeGroups[prototype.group.name] = {} end + if prototypeGroups[prototype.group.name][prototype.subgroup.name] == nil then prototypeGroups[prototype.group.name][prototype.subgroup.name] = {} end + table.insert(prototypeGroups[prototype.group.name][prototype.subgroup.name], prototype) + end + end + end + + if prototypeGroups[globalPlayer.recipeGroupSelected] == nil then + globalPlayer.recipeGroupSelected = firstGroup + end + return groupList, prototypeGroups +end + +------------------------------------------------------------------------------- +-- Get item list +-- +-- @function [parent=#ItemSelector] getItemList +-- +-- @param #LuaPlayer player +-- @param #LuaGuiElement element button +-- @param #string action action name +-- @param #string item first item name +-- @param #string item2 second item name +-- @param #string item3 third item name +-- +function ItemSelector.methods:getItemList(player, element, action, item, item2, item3) + Logging:trace(self:classname(), "getItemList():",player, element, action, item, item2, item3) + local globalPlayer = self.player:getGlobal(player) + local list = {} + local prototypeGroups = self:getPrototypeGroups() + if prototypeGroups[globalPlayer.recipeGroupSelected] ~= nil then + list = prototypeGroups[globalPlayer.recipeGroupSelected] + end + return list +end + +------------------------------------------------------------------------------- +-- Build prototype tooltip +-- +-- @function [parent=#ItemSelector] buildPrototypeTooltip +-- +-- @param #LuaPlayer player +-- @param #LuaPrototype prototype +-- +function ItemSelector.methods:buildPrototypeTooltip(player, prototype) + Logging:trace(self:classname(), "buildPrototypeTooltip(player, prototype):",player, prototype) + -- initalize tooltip + local tooltip = self.player:getLocalisedName(player, prototype) + return tooltip +end + +------------------------------------------------------------------------------- +-- Build prototype icon +-- +-- @function [parent=#ItemSelector] buildPrototypeIcon +-- +-- @param #LuaPlayer player +-- +function ItemSelector.methods:buildPrototypeIcon(player, guiElement, prototype, tooltip) + Logging:trace(self:classname(), "buildPrototypeIcon(player, guiElement, prototype, tooltip:",player, guiElement, prototype, tooltip) + self:addGuiButtonSelectSprite(guiElement, self:classname().."=item-select=ID=", self.player:getItemIconType(prototype), prototype.name, prototype.name, tooltip) +end + + + diff --git a/selector/RecipeSelector.lua b/selector/RecipeSelector.lua index 8f0f1dcf..f752a99d 100644 --- a/selector/RecipeSelector.lua +++ b/selector/RecipeSelector.lua @@ -36,6 +36,7 @@ end function RecipeSelector.methods:updateGroups(player, element, action, item, item2, item3) Logging:trace(self:classname(), "updateGroups():",player, element, action, item, item2, item3) local globalPlayer = self.player:getGlobal(player) + local globalGui = self.player:getGlobalGui(player) -- recuperation recipes local prototypeGroups = {} local groupList = {} @@ -43,7 +44,9 @@ function RecipeSelector.methods:updateGroups(player, element, action, item, item local prototypeFilterProduct = self:getProductFilter() local firstGroup = nil - for key, prototype in spairs(self.player:getRecipes(player, true),function(t,a,b) return t[b]["subgroup"]["order"] > t[a]["subgroup"]["order"] end) do + local fake_recipe = true + if globalGui.currentTab == "HMPropertiesTab" then fake_recipe = false end + for key, prototype in spairs(self.player:getRecipes(player, fake_recipe),function(t,a,b) return t[b]["subgroup"]["order"] > t[a]["subgroup"]["order"] end) do local find = false if prototypeFilter ~= nil and prototypeFilter ~= "" then local elements = prototype.products diff --git a/tab/MainTab.lua b/tab/MainTab.lua index 57dbeb58..cc0645ed 100644 --- a/tab/MainTab.lua +++ b/tab/MainTab.lua @@ -607,7 +607,10 @@ function MainTab.methods:updateHeaderPanel(player, item, item2, item3) self:addGuiButton(tabPanel, self:classname().."=refresh-model=ID=", model.id, "helmod_button_default", ({"helmod_result-panel.refresh-button"})) elseif globalGui.currentTab == "HMPropertiesTab" then local tabPanel = self:addGuiFlowH(menuPanel, "tab", "helmod_flow_data_tab") - self:addGuiButton(tabPanel, "HMEntitySelector=", "OPEN", "helmod_button_default", ({"helmod_result-panel.add-button-entity"})) + self:addGuiButton(tabPanel, "HMEntitySelector=", "OPEN", "helmod_button_default", ({"helmod_result-panel.select-button-entity"})) + self:addGuiButton(tabPanel, "HMItemSelector=", "OPEN", "helmod_button_default", ({"helmod_result-panel.select-button-item"})) + self:addGuiButton(tabPanel, "HMRecipeSelector=", "OPEN", "helmod_button_default", ({"helmod_result-panel.select-button-recipe"})) + self:addGuiButton(tabPanel, "HMTechnologySelector=", "OPEN", "helmod_button_default", ({"helmod_result-panel.select-button-technology"})) self:addGuiButton(tabPanel, self:classname().."=change-tab=ID=", "HMProductionLineTab", "helmod_button_default", ({"helmod_result-panel.back-button-production-line"})) else -- action panel diff --git a/tab/PropertiesTab.lua b/tab/PropertiesTab.lua index b5cf1340..41b0722e 100644 --- a/tab/PropertiesTab.lua +++ b/tab/PropertiesTab.lua @@ -68,19 +68,43 @@ function PropertiesTab.methods:updateData(player) local model = self.model:getModel(player) local globalGui = self.player:getGlobalGui(player) -- data - local scrollPanel = self.parent:getResultScrollPanel(player, {"helmod_result-panel.tab-title-properties"}) + local resultPanel = self.parent:getResultPanel(player, {"helmod_result-panel.tab-title-properties"}) + local listPanel = self:addGuiFlowH(resultPanel, "list-element", "helmod_flow_full_resize_row") + local scrollPanel = self.parent:getResultScrollPanel(player) local globalPlayer = self.player:getGlobal(player) - if globalPlayer["entity-properties"] ~= nil then - local prototype = self.player:getEntityPrototype(globalPlayer["entity-properties"]) + if globalPlayer["prototype-properties"] ~= nil and globalPlayer["prototype-properties"].name ~= nil then + local prototype_name = globalPlayer["prototype-properties"].name + local prototype_type = globalPlayer["prototype-properties"].type + local prototype = nil + if prototype_type == "entity" then + prototype = self.player:getEntityPrototype(prototype_name) + if prototype ~= nil then + self:addGuiButtonSprite(listPanel, self:classname().."=entity-select=ID=", self.player:getEntityIconType(prototype), prototype.name, prototype.name, self.player:getLocalisedName(player, prototype)) + end + elseif prototype_type == "item" then + prototype = self.player:getItemPrototype(prototype_name) + if prototype ~= nil then + self:addGuiButtonSprite(listPanel, self:classname().."=item-select=ID=", self.player:getItemIconType(prototype), prototype.name, prototype.name, self.player:getLocalisedName(player, prototype)) + end + elseif prototype_type == "recipe" then + prototype = self.player:getRecipe(player, prototype_name) + if prototype ~= nil then + self:addGuiButtonSprite(listPanel, self:classname().."=recipe-select=ID=", self.player:getRecipeIconType(player, prototype), prototype.name, prototype.name, self.player:getRecipeLocalisedName(player, prototype)) + end + elseif prototype_type == "technology" then + prototype = self.player:getTechnology(player, prototype_name) + if prototype ~= nil then + self:addGuiButtonSprite(listPanel, self:classname().."=technology-select=ID=", "technology", prototype.name, prototype.name, self.player:getTechnologyLocalisedName(player, prototype)) + end + end if prototype ~= nil then - self:addGuiButtonSprite(scrollPanel, self:classname().."=entity-select=ID=", self.player:getEntityIconType(prototype), prototype.name, prototype.name, self.player:getLocalisedName(player, prototype)) - + self:addGuiLabel(listPanel, "type-label", prototype_type, "helmod_label_right_100") local resultTable = self:addGuiTable(scrollPanel,"table-resources",2) self:addTableHeader(player, resultTable) - local properties = self:parseProperties(prototype) + local properties = self:parseProperties(prototype, 0) for _, property in pairs(properties) do self:addTableRow(player, resultTable, property.name, property.value) @@ -96,7 +120,7 @@ end -- -- @param #LuaObject prototype -- -function PropertiesTab.methods:parseProperties(prototype) +function PropertiesTab.methods:parseProperties(prototype, level) local properties = {} local help_string = string.gmatch(prototype:help(),"(%S+) [[]R[]]") @@ -107,8 +131,8 @@ function PropertiesTab.methods:parseProperties(prototype) local type = type(prototype[i]) local value = tostring(prototype[i]) if type == "table" then - if pcall(function() prototype[i]:help() end) then - local result = PropertiesTab.methods:parseProperties(prototype[i]) + if level < 2 and pcall(function() prototype[i]:help() end) then + local result = PropertiesTab.methods:parseProperties(prototype[i], level + 1) value = "" for _, property in pairs(result) do value = value .. property.name .. " = " .. property.value .. "\n"