From 8c579b208408244cc4c76c1228955962dd8cadac Mon Sep 17 00:00:00 2001 From: MewMew Date: Mon, 30 Mar 2020 20:10:21 +0200 Subject: [PATCH] players no longer get murdered by water --- modules/sticky_landfill.lua | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/modules/sticky_landfill.lua b/modules/sticky_landfill.lua index 4c570390..096b47aa 100644 --- a/modules/sticky_landfill.lua +++ b/modules/sticky_landfill.lua @@ -1,5 +1,7 @@ -- landfill is sticky, making it difficult to isolate +local math_sqrt = math.sqrt +local math_floor = math.floor local table_insert = table.insert local vectors = {} @@ -16,7 +18,6 @@ local vectors_2 = {{0, -1}, {0, 1}, {-1, 0}, {1, 0}} local function is_position_sticky(surface, position) local tile = surface.get_tile(position) if not tile.collides_with("resource-layer") then return end - for _, v in pairs(vectors_2) do tile = surface.get_tile({position.x + v[1], position.y + v[2]}) if not tile.collides_with("resource-layer") then @@ -28,14 +29,27 @@ end local function move_tile(surface, tile_name, position) for key, v in pairs(vectors) do local p = {x = position.x + v[1], y = position.y + v[2]} - if is_position_sticky(surface, p) then surface.set_tiles({{name = tile_name, position = p}}, true) return end end end - + +local function safe_players_from_drowning(surface, tiles) + local a = math_floor(math_sqrt(#tiles)) + 2 + local left_top = {x = tiles[1].position.x - 1, y = tiles[1].position.y - 1} + local area = {{left_top.x, left_top.y}, {left_top.x + a + 2, left_top.y + a + 2}} + local players = {} + for _, character in pairs(surface.find_entities_filtered({area = area, name = "character"})) do + if character.player then + table_insert(players, {character.player, {character.player.position.x, character.player.position.y}}) + character.player.teleport({0, 0}, surface) + end + end + return players +end + local function sticky(surface, tiles, tile_name) local revert_tiles = {} local revert_entities = {} @@ -51,6 +65,9 @@ local function sticky(surface, tiles, tile_name) end i = i + 1 end + + local players = safe_players_from_drowning(surface, tiles) + surface.set_tiles(revert_tiles, true) for _, placed_tile in pairs(tiles) do @@ -60,6 +77,10 @@ local function sticky(surface, tiles, tile_name) for _, entity in pairs(revert_entities) do surface.create_entity(entity) end + + for _, v in pairs(players) do + v[1].teleport(v[2], surface) + end end local function on_player_built_tile(event)