From 6442eab80cc005b39e2ac150e770a502cf30c06d Mon Sep 17 00:00:00 2001 From: James Gillham Date: Wed, 31 Mar 2021 15:24:01 +0100 Subject: [PATCH 1/2] Require auto trusted to use landfill remover. --- features/landfill_remover.lua | 14 +++++++++ features/landfill_remover_tests.lua | 44 +++++++++++++++++++++++++++++ locale/en/redmew_features.cfg | 3 ++ utils/test/assert.lua | 10 +++++++ 4 files changed, 71 insertions(+) diff --git a/features/landfill_remover.lua b/features/landfill_remover.lua index 19b1cbf4..806b820b 100644 --- a/features/landfill_remover.lua +++ b/features/landfill_remover.lua @@ -3,10 +3,17 @@ -- Use the decon planner within range of the landfill to remove it local Event = require 'utils.event' +local Rank = require 'features.rank_system' +local Ranks = require 'resources.ranks' local table = require 'utils.table' local math = require 'utils.math' local config = global.config.landfill_remover +local Public = {} + +local rank_too_low_message = {'landfill_remover.rank_too_low'} +Public.rank_too_low_message =rank_too_low_message + local floor = math.floor local ceil = math.ceil local fast_remove = table.fast_remove @@ -153,6 +160,11 @@ Event.add( return end + if Rank.less_than(player.name, Ranks.auto_trusted) then + player.print(rank_too_low_message) + return + end + local count_entities_filtered = surface.count_entities_filtered local tiles_to_add = {} local revert_tile = config.revert_tile or 'water-mud' @@ -179,3 +191,5 @@ Event.add( end end ) + +return Public diff --git a/features/landfill_remover_tests.lua b/features/landfill_remover_tests.lua index c125d2c1..ae5a125a 100644 --- a/features/landfill_remover_tests.lua +++ b/features/landfill_remover_tests.lua @@ -2,6 +2,9 @@ local Declare = require 'utils.test.declare' local EventFactory = require 'utils.test.event_factory' local Assert = require 'utils.test.assert' local Helper = require 'utils.test.helper' +local Rank = require 'features.rank_system' +local Ranks = require 'resources.ranks' +local LandfillRemover = require 'features.landfill_remover' local main_inventory = defines.inventory.character_main local config = global.config.landfill_remover @@ -697,5 +700,46 @@ Declare.module( end end ) + + Declare.test( + 'can not remove landfill when guest', + function(context) + -- Arrange + local player = context.player + local surface = player.surface + local cursor = setup_player_with_valid_deconstruction_planner(player) + local position = {2, 2} + local area = {{2.1, 2.1}, {2.9, 2.9}} + surface.set_tiles({{name = 'landfill', position = position}}) + + player.admin = false + local old_rank = Rank.get_player_rank(player.name) + Rank.set_player_rank(player.name, Ranks.guest) + + context:add_teardown(function() + player.admin = true + Rank.set_player_rank(player.name, old_rank) + end) + + local messages = {} + + Helper.modify_lua_object(context, player, 'print', function(text) + messages[#messages+1] = text + end) + + Helper.modify_lua_object(context, game, 'get_player', function() + return player + end) + + -- Act + EventFactory.do_player_deconstruct_area(cursor, player, area) + + -- Assert + local tile = surface.get_tile(position[1], position[2]) + Assert.equal('landfill', tile.name) + + Assert.array_contains(messages, LandfillRemover.rank_too_low_message) + end + ) end ) diff --git a/locale/en/redmew_features.cfg b/locale/en/redmew_features.cfg index e4a5c075..72bc67ce 100644 --- a/locale/en/redmew_features.cfg +++ b/locale/en/redmew_features.cfg @@ -186,3 +186,6 @@ empty_corpse=[color=red][Corpse][/color]Your corpse was empty and has been remov [dump_offline_inventories] inventory_location=[color=blue][Corpse][/color] __1__ has been offline __2__ minutes. Their inventory is now available at [gps=__3__,__4__,__5__] + +[landfill_remover] +rank_too_low=You must be auto trusted or above to use the deconstruction planner on landfill. diff --git a/utils/test/assert.lua b/utils/test/assert.lua index 122a7cb6..6f4772f5 100644 --- a/utils/test/assert.lua +++ b/utils/test/assert.lua @@ -48,6 +48,16 @@ function Public.table_equal(a, b) end end +function Public.array_contains(array, item) + for _, v in pairs(array) do + if v == item then + return + end + end + + error('array does not contain ' .. _G.dump(item), 2) +end + function Public.is_true(condition, optional_message) if not condition then error(optional_message or 'condition was not true', 2) From ef4be55e1a8a77fc7cbf51445c09d5aaca8bd854 Mon Sep 17 00:00:00 2001 From: James Gillham Date: Wed, 31 Mar 2021 16:19:19 +0100 Subject: [PATCH 2/2] Add resource-layer to landfill remover collision mask. --- features/landfill_remover.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/features/landfill_remover.lua b/features/landfill_remover.lua index 806b820b..ec9c9552 100644 --- a/features/landfill_remover.lua +++ b/features/landfill_remover.lua @@ -18,7 +18,7 @@ local floor = math.floor local ceil = math.ceil local fast_remove = table.fast_remove -local collision_mask = {'floor-layer', 'object-layer'} +local collision_mask = {'floor-layer', 'object-layer', 'resource-layer'} local whitelist = defines.deconstruction_item.tile_filter_mode.whitelist local entity_whitelist = defines.deconstruction_item.entity_filter_mode.whitelist