mirror of
https://github.com/ComfyFactory/ComfyFactorio.git
synced 2025-01-06 00:23:49 +02:00
players no longer get murdered by water
This commit is contained in:
parent
eb1762973c
commit
8c579b2084
@ -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)
|
||||
|
Loading…
Reference in New Issue
Block a user