Skip to content

Commit

Permalink
Fixed crash when clicking on icon to open
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Helfima committed May 30, 2017
1 parent c73e3b6 commit 96d5798
Show file tree
Hide file tree
Showing 14 changed files with 239 additions and 29 deletions.
4 changes: 3 additions & 1 deletion controller/Controller.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ require "edition.EnergyEdition"
require "selector.EntitySelector"
require "selector.RecipeSelector"
require "selector.TechnologySelector"
require "selector.ItemSelector"
require "tab.MainTab"


Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
15 changes: 14 additions & 1 deletion controller/PlayerController.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
--
Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion edition/AbstractEdition.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions helmod.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion info.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

{
"name": "helmod",
"version": "0.5.2",
"version": "0.5.3",
"title": "Helmod: Assistant to plan its base.",
"author": "Helfima",
"contact": "Helfima",
Expand Down
9 changes: 8 additions & 1 deletion locale/en/helmod.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion locale/fr/helmod.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion model/Model.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
42 changes: 32 additions & 10 deletions selector/AbstractSelector.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion selector/EntitySelector.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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

-------------------------------------------------------------------------------
Expand Down
129 changes: 129 additions & 0 deletions selector/ItemSelector.lua
Original file line number Diff line number Diff line change
@@ -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



5 changes: 4 additions & 1 deletion selector/RecipeSelector.lua
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,17 @@ 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 = {}
local prototypeFilter = self:getFilter()
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
Expand Down
5 changes: 4 additions & 1 deletion tab/MainTab.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading

0 comments on commit 96d5798

Please sign in to comment.