From f149e446a17fbc06b970c27e0d754fc9c99c10b4 Mon Sep 17 00:00:00 2001 From: badgamernl Date: Thu, 3 Sep 2020 03:33:39 +0200 Subject: [PATCH] Fixed heavy repitition - Introduced EmmyLua documentation (can be removed if needed) - Remamed type to inv --- config/gui/autofill.lua | 207 ++++++++++++++++----------------------- modules/gui/autofill.lua | 14 +-- 2 files changed, 90 insertions(+), 131 deletions(-) diff --git a/config/gui/autofill.lua b/config/gui/autofill.lua index e9d6c6da..c2a892c6 100644 --- a/config/gui/autofill.lua +++ b/config/gui/autofill.lua @@ -1,11 +1,35 @@ --- This file contains all the different settings for the autofill system and gui -- @config Autofill -local table = require 'overrides.table' --- @dep overrides.table +local table = require 'overrides.table' -- @dep overrides.table + +---@class AutofillEntity +---@field entity string +---@field enabled boolean +---@field items AutofillItem[] + +---@class AutofillItem +---@field entity string +---@field category string +---@field inv number +---@field name string +---@field amount number +---@field enabled boolean + +---@class DefaultItem +---@field name string +---@field amount number +---@field enabled boolean + +---@class DefaultCategory +---@field category string +---@field entity string[] +---@field inv number[] +---@field items DefaultItem[] local config = { -- General config - icon = 'item/piercing-rounds-magazine', --- @setting icon that will be used for the toolbar + icon = 'item/piercing-rounds-magazine', -- @setting icon that will be used for the toolbar categories = { ammo = 'ammo', fuel = 'fuel', @@ -21,162 +45,100 @@ local config = { stone_furnace = 'stone-furnace', steel_furnace = 'steel-furnace' }, + ---@type AutofillEntity[] default_entities = {} } -local default_autofill_item_settings = { +---@type DefaultCategory[] +local default_categories = { { category = config.categories.ammo, entity = {config.entities.car, config.entities.tank, config.entities.gun_turret}, - type = {defines.inventory.car_ammo, defines.inventory.turret_ammo}, - name = 'uranium-rounds-magazine', - amount = 10, - enabled = false - }, - { - category = config.categories.ammo, - entity = {config.entities.car, config.entities.tank, config.entities.gun_turret}, - type = {defines.inventory.car_ammo, defines.inventory.turret_ammo}, - name = 'piercing-rounds-magazine', - amount = 10, - enabled = false - }, - { - category = config.categories.ammo, - entity = {config.entities.car, config.entities.tank, config.entities.gun_turret}, - type = {defines.inventory.car_ammo, defines.inventory.turret_ammo}, - name = 'firearm-magazine', - amount = 10, - enabled = false + inv = {defines.inventory.car_ammo, defines.inventory.turret_ammo}, + items = { + { name = 'uranium-rounds-magazine', amount = 10, enabled = false }, + { name = 'piercing-rounds-magazine', amount = 10, enabled = false }, + { name = 'firearm-magazine', amount = 10, enabled = false }, + } }, { category = config.categories.ammo, entity = {config.entities.tank}, - type = {defines.inventory.car_ammo}, - name = 'flamethrower-ammo', - amount = 10, - enabled = false + inv = {defines.inventory.car_ammo}, + items = { + { name = 'flamethrower-ammo', amount = 10, enabled = false }, + } }, { category = config.categories.shell, entity = {config.entities.tank}, - type = {defines.inventory.car_ammo}, - name = 'cannon-shell', - amount = 10, - enabled = false - }, - { - category = config.categories.shell, - entity = {config.entities.tank}, - type = {defines.inventory.car_ammo}, - name = 'explosive-cannon-shell', - amount = 10, - enabled = false - }, - { - category = config.categories.shell, - entity = {config.entities.tank}, - type = {defines.inventory.car_ammo}, - name = 'uranium-cannon-shell', - amount = 10, - enabled = false - }, - { - category = config.categories.shell, - entity = {config.entities.tank}, - type = {defines.inventory.car_ammo}, - name = 'explosive-uranium-cannon-shell', - amount = 10, - enabled = false + inv = {defines.inventory.car_ammo}, + items = { + { name = 'cannon-shell', amount = 10, enabled = false }, + { name = 'explosive-cannon-shell', amount = 10, enabled = false }, + { name = 'uranium-cannon-shell', amount = 10, enabled = false }, + { name = 'explosive-uranium-cannon-shell', amount = 10, enabled = false }, + } }, { category = config.categories.ammo, entity = {config.entities.spidertron}, - type = {defines.inventory.car_ammo}, - name = 'rocket', - amount = 10, - enabled = false - }, - { - category = config.categories.ammo, - entity = {config.entities.spidertron}, - type = {defines.inventory.car_ammo}, - name = 'explosive-rocket', - amount = 10, - enabled = false - }, - { - category = config.categories.ammo, - entity = {config.entities.spidertron}, - type = {defines.inventory.car_ammo}, - name = 'atomic-bomb', - amount = 10, - enabled = false + inv = {defines.inventory.car_ammo}, + items = { + { name = 'rocket', amount = 10, enabled = false }, + { name = 'explosive-rocket', amount = 10, enabled = false }, + { name = 'atomic-bomb', amount = 10, enabled = false }, + } }, { category = config.categories.fuel, entity = {config.entities.car, config.entities.tank, config.entities.locomotive, config.entities.burner_mining_drill, config.entities.stone_furnace, config.entities.steel_furnace}, - type = {defines.inventory.fuel}, - name = 'nuclear-fuel', - amount = 1, - enabled = false - }, - { - category = config.categories.fuel, - entity = {config.entities.car, config.entities.tank, config.entities.locomotive, config.entities.burner_mining_drill, config.entities.stone_furnace, config.entities.steel_furnace}, - type = {defines.inventory.fuel}, - name = 'rocket-fuel', - amount = 10, - enabled = false - }, - { - category = config.categories.fuel, - entity = {config.entities.car, config.entities.tank, config.entities.locomotive, config.entities.burner_mining_drill, config.entities.stone_furnace, config.entities.steel_furnace}, - type = {defines.inventory.fuel}, - name = 'solid-fuel', - amount = 10, - enabled = false - }, - { - category = config.categories.fuel, - entity = {config.entities.car, config.entities.tank, config.entities.locomotive, config.entities.burner_mining_drill, config.entities.stone_furnace, config.entities.steel_furnace}, - type = {defines.inventory.fuel}, - name = 'coal', - amount = 10, - enabled = false + inv = {defines.inventory.fuel}, + items = { + { name = 'nuclear-fuel', amount = 10, enabled = false }, + { name = 'rocket-fuel', amount = 10, enabled = false }, + { name = 'solid-fuel', amount = 10, enabled = false }, + { name = 'coal', amount = 10, enabled = false }, + } } } -local function get_items_by_type(entity, type) +---@param entity AutofillEntity +---@param inv string +---@return AutofillItem[] +local function get_items_by_inv(entity, inv) local items = entity.items - for _, item in pairs(default_autofill_item_settings) do - if table.contains(item.entity, entity.entity) then - if table.contains(item.type, type) then - items[item.name] = { - entity = entity.entity, - category = item.category, - type = type, - name = item.name, - amount = item.amount, - enabled = item.enabled - } + for _, category in pairs(default_categories) do + if table.contains(category.entity, entity.entity) then + if table.contains(category.inv, inv) then + for _, item in pairs(category.items) do + items[item.name] = { + entity = entity.entity, + category = category.category, + inv = inv, + name = item.name, + amount = item.amount, + enabled = item.enabled + } + end end end end return items end -local default_entities = config.default_entities - -local function generate_default_setting(entity_name, type, enabled) - if not default_entities[entity_name] then - default_entities[entity_name] = { +---@param entity_name string +---@param inv number +---@param enabled boolean +local function generate_default_setting(entity_name, inv, enabled) + if not config.default_entities[entity_name] then + config.default_entities[entity_name] = { entity = entity_name, enabled = enabled, items = {} } end - get_items_by_type(default_entities[entity_name], type) + get_items_by_inv(config.default_entities[entity_name], inv) end generate_default_setting(config.entities.car, defines.inventory.fuel, true) @@ -197,7 +159,4 @@ generate_default_setting(config.entities.stone_furnace, defines.inventory.fuel, generate_default_setting(config.entities.steel_furnace, defines.inventory.fuel, true) --- Cleanup temporary table -default_autofill_item_settings = nil - return config \ No newline at end of file diff --git a/modules/gui/autofill.lua b/modules/gui/autofill.lua index cfbda6fe..51ec376d 100644 --- a/modules/gui/autofill.lua +++ b/modules/gui/autofill.lua @@ -4,12 +4,12 @@ @alias autofill ]] -local Game = require 'utils.game' --- @dep utils.game -local Gui = require 'expcore.gui' --- @dep expcore.gui -local Global = require 'utils.global' --- @dep utils.global -local config = require 'config.gui.autofill' --- @dep config.gui.autofill -local Event = require 'utils.event' --- @dep utils.event -local table = require 'overrides.table' --- @dep overrides.table +local Game = require 'utils.game' -- @dep utils.game +local Gui = require 'expcore.gui' -- @dep expcore.gui +local Global = require 'utils.global' -- @dep utils.global +local config = require 'config.gui.autofill' -- @dep config.gui.autofill +local Event = require 'utils.event' -- @dep utils.event +local table = require 'overrides.table' -- @dep overrides.table local print_text = Game.print_floating_text -- (surface, position, text, color) @@ -333,7 +333,7 @@ local function entity_build(event) if not item.enabled then goto end_item end -- Get the inventory of the entity or goto next item - local entity_inventory = entity.get_inventory(item.type) + local entity_inventory = entity.get_inventory(item.inv) if not entity_inventory then goto end_item end local preferd_amount = item.amount