From 5cc6ab82b6b7a58e0f2c3bc6d1e5c31ce009880f Mon Sep 17 00:00:00 2001 From: James Gillham Date: Sat, 12 Sep 2020 20:17:29 +0100 Subject: [PATCH] Change paint tool to use coloured concrete. --- config.lua | 5 +- features/gui/autofill.lua | 2 +- features/gui/paint.lua | 231 +++++++++++++++++++++++++--------- locale/en/redmew_features.cfg | 8 ++ locale/en/redmew_gui.cfg | 2 +- resources/market_items.lua | 1 + 6 files changed, 188 insertions(+), 61 deletions(-) diff --git a/config.lua b/config.lua index 5ba3699a..bea506cb 100644 --- a/config.lua +++ b/config.lua @@ -106,7 +106,10 @@ global.config = { }, -- adds a paint brush paint = { - enabled = true + enabled = true, + -- Sometimes the hidden tile information is lost, the fallback tile will be used when removing those tiles. + fallback_hidden_tile = 'dirt-6', + prevent_on_landfill = true }, -- autofill turrets with ammo autofill = { diff --git a/features/gui/autofill.lua b/features/gui/autofill.lua index 4b09e4e4..972ffe00 100644 --- a/features/gui/autofill.lua +++ b/features/gui/autofill.lua @@ -94,7 +94,7 @@ local function toggle_main_frame(event) Gui.set_data(button, name) end - frame.add {type = 'button', name = main_button_name, caption = 'Close'} + frame.add {type = 'button', name = main_button_name, caption = {'common.close_button'}} local data = { enabled_checkbox = enabled_checkbox, diff --git a/features/gui/paint.lua b/features/gui/paint.lua index 1699b4e7..4d1d4cab 100644 --- a/features/gui/paint.lua +++ b/features/gui/paint.lua @@ -2,32 +2,26 @@ local Event = require 'utils.event' local Gui = require 'utils.gui' local Global = require 'utils.global' -local brush_tool = 'refined-hazard-concrete' +local config = global.config.paint +local default_fallback_hidden_tile = 'dirt-6' + +local brush_tools = { + ['refined-concrete'] = true, + ['refined-hazard-concrete'] = true +} local valid_filters = { - 'dirt-1', - 'dirt-2', - 'dirt-3', - 'dirt-4', - 'dirt-5', - 'dirt-6', - 'dirt-7', - 'dry-dirt', - 'grass-1', - 'grass-2', - 'grass-3', - 'grass-4', - 'lab-dark-1', - 'lab-dark-2', - 'lab-white', - 'red-desert-0', - 'red-desert-1', - 'red-desert-2', - 'red-desert-3', - 'sand-1', - 'sand-2', - 'sand-3', - 'tutorial-grid' + ['acid-refined-concrete'] = true, + ['black-refined-concrete'] = true, + ['blue-refined-concrete'] = true, + ['brown-refined-concrete'] = true, + ['cyan-refined-concrete'] = true, + ['green-refined-concrete'] = true, + ['orange-refined-concrete'] = true, + ['pink-refined-concrete'] = true, + ['purple-refined-concrete'] = true, + ['red-refined-concrete'] = true, + ['yellow-refined-concrete'] = true } local main_button_name = Gui.uid_name() @@ -50,41 +44,146 @@ Global.register( end ) +local function refund_tiles(player, tiles) + local count = 0 + local set_hidden_tile = player.surface.set_hidden_tile + local fallback_tile = config.fallback_hidden_tile or default_fallback_hidden_tile + for i = 1, #tiles do + local tile_data = tiles[i] + if valid_filters[tile_data.old_tile.name] then + count = count + 1 + set_hidden_tile(tile_data.position, fallback_tile) + end + end + + if count > 0 then + player.insert {name = 'refined-concrete', count = count} + end +end + local function player_build_tile(event) local item = event.item if not item then return end - if item.name ~= brush_tool then + local item_name = item.name + if not brush_tools[item_name] then return end - local replace_tile = paint_brushes_by_player[event.player_index] + local player_index = event.player_index + local player = game.get_player(player_index) + if not player or not player.valid then + return + end + + local surface = game.surfaces[event.surface_index] + if not surface or not surface.valid then + return + end + + local replace_tile = paint_brushes_by_player[player_index] if not replace_tile then + refund_tiles(player, event.tiles) return end - local player = game.get_player(event.player_index) if not player.gui.left[main_frame_name] then + refund_tiles(player, event.tiles) + return + end + + local get_hidden_tile = surface.get_hidden_tile + local tile_name = event.tile.name + local tiles = event.tiles + local count = 0 + local hidden_tiles = {} + local prevent_on_landfill = config.prevent_on_landfill + local print_no_landfill_message = false + local fallback_tile = config.fallback_hidden_tile or default_fallback_hidden_tile + for i = 1, #tiles do + local tile_data = tiles[i] + + local hidden_tile = get_hidden_tile(tile_data.position) + if prevent_on_landfill and hidden_tile == 'landfill' then + tile_data.name = tile_name + print_no_landfill_message = true + goto continue + end + + tile_data.name = replace_tile + + if valid_filters[tile_data.old_tile.name] then + count = count + 1 + end + + if valid_filters[hidden_tile] then + hidden_tiles[#hidden_tiles + 1] = {position = tile_data.position, name = fallback_tile} + end + + ::continue:: + end + + surface.set_tiles(tiles) + + local set_hidden_tile = surface.set_hidden_tile + for i = 1, #hidden_tiles do + local tile = hidden_tiles[i] + set_hidden_tile(tile.position, tile.name) + end + + if count > 0 then + player.insert {name = item_name, count = count} + end + + if print_no_landfill_message then + player.print({'paint.no_place_landfill'}) + end +end + +local function robot_built_tile(event) + local item = event.item + if not item then + return + end + + local item_name = item.name + if not brush_tools[item_name] then + return + end + + local surface = game.surfaces[event.surface_index] + if not surface or not surface.valid then return end local tiles = event.tiles - local count = 0 + local hidden_tiles = {} + local fallback_tile = config.fallback_hidden_tile or default_fallback_hidden_tile for i = 1, #tiles do local tile_data = tiles[i] - tile_data.name = replace_tile + local hidden_tile = surface.get_hidden_tile(tile_data.position) - if tile_data.old_tile.name == replace_tile then - count = count + 1 + if valid_filters[hidden_tile] then + hidden_tiles[#hidden_tiles + 1] = {position = tile_data.position, name = fallback_tile} end end - game.surfaces[event.surface_index].set_tiles(tiles) + for i = 1, #hidden_tiles do + local tile = hidden_tiles[i] + surface.set_hidden_tile(tile.position, tile.name) + end +end - if count > 0 then - player.insert {name = brush_tool, count = count} +local function get_tile_localised_name(tile_name) + if not tile_name then + return + end + + local proto = game.tile_prototypes[tile_name] + if proto then + return proto.localised_name or proto.name end end @@ -115,22 +214,29 @@ local function draw_filters_table(event) return end - local frame = center.add {type = 'frame', name = filters_table_name, direction = 'vertical', caption = 'Palette'} + local frame = + center.add {type = 'frame', name = filters_table_name, direction = 'vertical', caption = {'paint.palette'}} - local t = frame.add {type = 'table', column_count = 10} + local t = frame.add {type = 'table', column_count = 6} t.style.horizontal_spacing = 0 t.style.vertical_spacing = 0 - for _, v in ipairs(valid_filters) do + for tile_name, _ in pairs(valid_filters) do local flow = t.add {type = 'flow'} - local b = flow.add {type = 'sprite-button', name = filter_element_name, sprite = 'tile/' .. v, tooltip = v} - Gui.set_data(b, frame) - b.style = 'slot_button' + local button = + flow.add { + type = 'sprite-button', + name = filter_element_name, + sprite = 'tile/' .. tile_name, + tooltip = get_tile_localised_name(tile_name) + } + Gui.set_data(button, {frame = frame, tile_name = tile_name}) + button.style = 'slot_button' end local flow = frame.add {type = 'flow'} - local close = flow.add {type = 'button', name = filter_table_close_button_name, caption = 'Close'} + local close = flow.add {type = 'button', name = filter_table_close_button_name, caption = {'common.close_button'}} Gui.set_data(close, frame) event.player.opened = frame @@ -159,25 +265,34 @@ local function toggle(event) type = 'frame', name = main_frame_name, direction = 'vertical', - caption = 'Paint Brush' + caption = {'paint.frame_name'} } - local tooltip = paint_brushes_by_player[event.player_index] or '' + local top_flow = main_frame.add {type = 'flow', direction = 'horizontal'} + + local tile_name = paint_brushes_by_player[event.player_index] local brush = - main_frame.add({type = 'flow'}).add { + top_flow.add({type = 'flow'}).add { type = 'sprite-button', name = filter_button_name, - tooltip = tooltip, - sprite = tooltip ~= '' and 'tile/' .. tooltip or nil + tooltip = get_tile_localised_name(tile_name) or {'paint.select_brush'}, + sprite = tile_name and 'tile/' .. tile_name } brush.style = 'slot_button' + local label = top_flow.add {type = 'label', caption = {'paint.instructions'}} + local label_style = label.style + label_style.font = 'default-bold' + label_style.single_line = false + label_style.left_padding = 10 + local buttons_flow = main_frame.add {type = 'flow', direction = 'horizontal'} - buttons_flow.add {type = 'button', name = main_button_name, caption = 'Close'} + buttons_flow.add {type = 'button', name = main_button_name, caption = {'common.close_button'}} - local clear_brush = buttons_flow.add {type = 'button', name = filter_clear_name, caption = 'Clear Brush'} + local clear_brush = + buttons_flow.add {type = 'button', name = filter_clear_name, caption = {'paint.clear_brush'}} Gui.set_data(clear_brush, brush) end end @@ -191,7 +306,7 @@ Gui.on_click( paint_brushes_by_player[event.player_index] = nil local element = event.element element.sprite = 'utility/pump_cannot_connect_icon' - element.tooltip = '' + element.tooltip = {'paint.select_brush'} else draw_filters_table(event) end @@ -204,7 +319,7 @@ Gui.on_click( local brush = Gui.get_data(event.element) brush.sprite = 'utility/pump_cannot_connect_icon' - brush.tooltip = '' + brush.tooltip = {'paint.select_brush'} paint_brushes_by_player[event.player_index] = nil end @@ -218,15 +333,16 @@ Gui.on_click( return end - local frame = Gui.get_data(element) + local data = Gui.get_data(element) + local frame = data.frame + local tile_name = data.tile_name local filter_button = Gui.get_data(frame) - paint_brushes_by_player[event.player_index] = element.tooltip + paint_brushes_by_player[event.player_index] = tile_name filter_button.sprite = element.sprite filter_button.tooltip = element.tooltip - Gui.remove_data_recursively(frame) - frame.destroy() + Gui.destroy(frame) end ) @@ -234,8 +350,7 @@ Gui.on_click( filter_table_close_button_name, function(event) local frame = Gui.get_data(event.element) - Gui.remove_data_recursively(frame) - frame.destroy() + Gui.destroy(frame) end ) @@ -243,8 +358,7 @@ Gui.on_custom_close( filters_table_name, function(event) local element = event.element - Gui.remove_data_recursively(element) - element.destroy() + Gui.destroy(element) end ) @@ -252,3 +366,4 @@ Gui.allow_player_to_toggle_top_element_visibility(main_button_name) Event.add(defines.events.on_player_joined_game, player_joined) Event.add(defines.events.on_player_built_tile, player_build_tile) +Event.add(defines.events.on_robot_built_tile, robot_built_tile) diff --git a/locale/en/redmew_features.cfg b/locale/en/redmew_features.cfg index a9702112..ff34b072 100644 --- a/locale/en/redmew_features.cfg +++ b/locale/en/redmew_features.cfg @@ -169,3 +169,11 @@ research_finished=[technology=__1__] has been researched. name=Snake spawn_snake_fail=Unable to spawn snake, please try again. snake_destroyed=__1__ has been destroyed with a score of __2__. + +[paint] +frame_name=Paint Brush +clear_brush=Clear Brush +palette=Palette +select_brush=Select Brush Tile. +instructions=Select a brush tile to replace [item=refined-concrete] and [item=refined-hazard-concrete].\nOnly works when Paint Brush window is open. +no_place_landfill=Coloured concrete can not be placed on landfill tiles. diff --git a/locale/en/redmew_gui.cfg b/locale/en/redmew_gui.cfg index 77bad3d9..e9054548 100644 --- a/locale/en/redmew_gui.cfg +++ b/locale/en/redmew_gui.cfg @@ -92,7 +92,7 @@ empty_cursor_error_message=Click the button with a blueprint or blueprint book. no_filters_error_message=No filters have been set. [paint] -tooltip=Landscape painting tool +tooltip=Refined concrete painting tool [tag_group] tooltip=Player tag group management diff --git a/resources/market_items.lua b/resources/market_items.lua index 7507ca04..2060c567 100644 --- a/resources/market_items.lua +++ b/resources/market_items.lua @@ -76,5 +76,6 @@ return { {price = 25, name = 'construction-robot'}, {price = 350, name = 'energy-shield-equipment'}, {price = 750, name = 'personal-laser-defense-equipment'}, + {price = 1, name = 'refined-concrete'}, {price = 1, name = 'refined-hazard-concrete'}, }