diff --git a/classes/TerraFarm.lua b/classes/TerraFarm.lua index bea812c..871cd82 100644 --- a/classes/TerraFarm.lua +++ b/classes/TerraFarm.lua @@ -34,48 +34,6 @@ TerraFarm.NAME_TO_MODE = { ['FLATTEN'] = TerraFarm.MODE.FLATTEN, ['PAINT'] = TerraFarm.MODE.PAINT } -TerraFarm.PAINT_LAYERS = { - ['dirt'] = 1, - ['gravel'] = 2, - ['concrete'] = 3, - ['asphalt'] = 4, - ['grass'] = 5, - ['sand'] = 6, - ['animalMud'] = 7, - ['plates'] = 8, - ['cobblestones'] = 9, - ['flagstones'] = 10, - ['forestGround'] = 11, - ['rock'] = 12 -} -TerraFarm.PAINT_LAYER_TEXTS = { - 'Dirt', - 'Gravel', - 'Concrete', - 'Asphalt', - 'Grass', - 'Sand', - 'Animal mud', - 'Plates', - 'Cobblestones', - 'Flagstones', - 'Forest ground', - 'Rock' -} -TerraFarm.TEXT_TO_PAINT_LAYER = { - ['Dirt'] = 'dirt', - ['Gravel'] = 'gravel', - ['Concrete'] = 'concrete', - ['Asphalt'] = 'asphalt', - ['Grass'] = 'grass', - ['Sand'] = 'sand', - ['Animal mud'] = 'animalMud', - ['Plates'] = 'plates', - ['Cobblestones'] = 'cobblestones', - ['Flagstones'] = 'flagstones', - ['Forest ground'] = 'forestGround', - ['Rock'] = 'rock' -} ---@return TerraFarm function TerraFarm.new() @@ -112,7 +70,7 @@ function TerraFarm:setPaintLayer(name, textIndex) if name then self.config.terraformPaintLayer = name elseif textIndex then - self:setPaintLayer(TerraFarm.TEXT_TO_PAINT_LAYER[TerraFarm.PAINT_LAYER_TEXTS[textIndex]]) + self:setPaintLayer(TerraFarmGroundTypes:getNameByIndex(textIndex)) end end @@ -142,7 +100,7 @@ function TerraFarm:setDischargePaintLayer(name, textIndex) if name then self.config.dischargePaintLayer = name elseif textIndex then - self:setDischargePaintLayer(TerraFarm.TEXT_TO_PAINT_LAYER[TerraFarm.PAINT_LAYER_TEXTS[textIndex]]) + self:setDischargePaintLayer(TerraFarmGroundTypes:getNameByIndex(textIndex)) end end @@ -162,6 +120,11 @@ function TerraFarm:updateFillTypeData() self:setFillType(self.config.fillTypeName) end +function TerraFarm:updatePaintLayerData() + self:setPaintLayer(nil, TerraFarmGroundTypes:getIndexByName(self.config.terraformPaintLayer)) + self:setDischargePaintLayer(nil, TerraFarmGroundTypes:getIndexByName(self.config.dischargePaintLayer)) +end + function TerraFarm:registerCustomFillType() local name = 'DIRT' diff --git a/classes/TerraFarmGroundTypes.lua b/classes/TerraFarmGroundTypes.lua new file mode 100644 index 0000000..680d385 --- /dev/null +++ b/classes/TerraFarmGroundTypes.lua @@ -0,0 +1,58 @@ +---@class TerraFarmGroundTypes +---@field items GroundTypeItem[] +TerraFarmGroundTypes = {} + +TerraFarmGroundTypes.items = {} +TerraFarmGroundTypes.TYPES_LIST = {} + +local function isValidGroundTypeName(name) + return name == string.upper(name) +end + +function TerraFarmGroundTypes:init() + for name, layerId in pairs(g_groundTypeManager.terrainLayerMapping) do + if isValidGroundTypeName(name) then + local index = #self.items + 1 + local item = { + index = index, + name = name, + layerId = layerId + } + table.insert(self.items, item) + table.insert(self.TYPES_LIST, name) + end + end + + g_terraFarm:updatePaintLayerData() +end + +function TerraFarmGroundTypes:getIndexByName(name) + for index, item in ipairs(self.items) do + if item.name == name then + return index + end + end + return 1 +end + +function TerraFarmGroundTypes:getNameByIndex(index) + if self.items[index] ~= nil then + return self.items[index].name + end +end + +function TerraFarmGroundTypes:getLayerByName(name) + for _, item in ipairs(self.items) do + if item.name == name then + return item.layerId + end + end + + return 0 +end + +GroundTypeManager.initTerrain = Utils.appendedFunction(GroundTypeManager.initTerrain, + function () + TerraFarmGroundTypes:init() + end +) \ No newline at end of file diff --git a/classes/events/TerraFarmLandscapingEvent.lua b/classes/events/TerraFarmLandscapingEvent.lua index fcc0f2b..e8e8f1b 100644 --- a/classes/events/TerraFarmLandscapingEvent.lua +++ b/classes/events/TerraFarmLandscapingEvent.lua @@ -44,7 +44,7 @@ function TerraFarmLandscapingEvent.new(machineType, mode, object, operation, pos self.isDischarging = isDischarging or false if type(paintLayer) == 'string' then - self.paintLayer = g_groundTypeManager:getTerrainLayerByType(paintLayer) + self.paintLayer = TerraFarmGroundTypes:getLayerByName(paintLayer) end return self diff --git a/classes/gui/TerraFarmSettingsMainFrame.lua b/classes/gui/TerraFarmSettingsMainFrame.lua index d5a9c90..cffd7ed 100644 --- a/classes/gui/TerraFarmSettingsMainFrame.lua +++ b/classes/gui/TerraFarmSettingsMainFrame.lua @@ -57,8 +57,8 @@ function TerraFarmSettingsMainFrame:updateSettings() self.enableDebug:setIsChecked(g_terraFarm.config.debug) self.disableDischarge:setIsChecked(g_terraFarm.config.disableDischarge) self.disablePaint:setIsChecked(g_terraFarm.config.terraformDisablePaint) - self.paintLayer:setState(TerraFarm.PAINT_LAYERS[g_terraFarm.config.terraformPaintLayer]) - self.dischargePaintLayer:setState(TerraFarm.PAINT_LAYERS[g_terraFarm.config.dischargePaintLayer]) + self.paintLayer:setState(TerraFarmGroundTypes:getIndexByName(g_terraFarm.config.terraformPaintLayer)) + self.dischargePaintLayer:setState(TerraFarmGroundTypes:getIndexByName(g_terraFarm.config.dischargePaintLayer)) self.fillType:setState(TerraFarmFillTypes.NAME_TO_INDEX[g_terraFarm.config.fillTypeName]) end @@ -92,7 +92,7 @@ end ---@param element MultiTextOptionElement function TerraFarmSettingsMainFrame:onCreateDischargePaintLayer(element) - element:setTexts(TerraFarm.PAINT_LAYER_TEXTS) + element:setTexts(TerraFarmGroundTypes.TYPES_LIST) end ---@param state number @@ -103,7 +103,7 @@ end ---@param element MultiTextOptionElement function TerraFarmSettingsMainFrame:onCreatePaintLayer(element) - element:setTexts(TerraFarm.PAINT_LAYER_TEXTS) + element:setTexts(TerraFarmGroundTypes.TYPES_LIST) end ---@param state number diff --git a/definitions.lua b/definitions.lua index 887a9c3..ab65ce3 100644 --- a/definitions.lua +++ b/definitions.lua @@ -15,4 +15,10 @@ Position = { ---@field filePath string ---@field actionEvents table ---@field machine TerraFarmMachine -MachineSpec = {} \ No newline at end of file +MachineSpec = {} + +---@class GroundTypeItem +---@field index number +---@field name string +---@field layerId number +GroundTypeItem = {} \ No newline at end of file diff --git a/script.lua b/script.lua index ba231d3..b357fd0 100644 --- a/script.lua +++ b/script.lua @@ -5,6 +5,7 @@ source(modFolder .. 'classes/TerraFarm.lua') source(modFolder .. 'classes/debug/TerraFarmDebug.lua') source(modFolder .. 'classes/TerraFarmFillTypes.lua') +source(modFolder .. 'classes/TerraFarmGroundTypes.lua') source(modFolder .. 'classes/TerraFarmLandscaping.lua') source(modFolder .. 'classes/events/TerraFarmLandscapingEvent.lua')