2022-05-14 19:15:51 +02:00
|
|
|
local Event = require 'utils.event'
|
2020-04-29 13:12:42 +02:00
|
|
|
local Global = require 'utils.global'
|
|
|
|
local Public = {}
|
|
|
|
|
|
|
|
local math_floor = math.floor
|
|
|
|
local table_shuffle_table = table.shuffle_table
|
|
|
|
|
2022-05-14 19:15:51 +02:00
|
|
|
local this = {
|
2020-06-03 20:09:37 +02:00
|
|
|
debug = false
|
|
|
|
}
|
2020-04-29 13:12:42 +02:00
|
|
|
Global.register(
|
2022-05-14 19:15:51 +02:00
|
|
|
this,
|
2020-04-29 13:12:42 +02:00
|
|
|
function(tbl)
|
2022-05-14 19:15:51 +02:00
|
|
|
this = tbl
|
2020-04-29 13:12:42 +02:00
|
|
|
end
|
|
|
|
)
|
|
|
|
|
|
|
|
local directions = {
|
2020-05-09 10:04:24 +02:00
|
|
|
['north'] = function(position)
|
2022-05-14 19:15:51 +02:00
|
|
|
local surface_index = this.surface_index
|
|
|
|
if not surface_index then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
local surface = game.get_surface(surface_index)
|
|
|
|
if not surface.valid then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
local width = surface.map_gen_settings.width
|
|
|
|
if width > this.max_line_size then
|
|
|
|
width = this.max_line_size
|
2020-05-09 10:04:24 +02:00
|
|
|
end
|
|
|
|
local a = width * 0.5 + 1
|
2022-05-14 19:15:51 +02:00
|
|
|
this.vector = {0, -1}
|
|
|
|
this.area = {{position.x - a, position.y - 1}, {position.x + a, position.y}}
|
2020-05-09 10:04:24 +02:00
|
|
|
end,
|
|
|
|
['south'] = function(position)
|
2022-05-14 19:15:51 +02:00
|
|
|
local surface_index = this.surface_index
|
|
|
|
if not surface_index then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
local surface = game.get_surface(surface_index)
|
|
|
|
if not surface.valid then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
local width = surface.map_gen_settings.width
|
|
|
|
if width > this.max_line_size then
|
|
|
|
width = this.max_line_size
|
2020-05-09 10:04:24 +02:00
|
|
|
end
|
|
|
|
local a = width * 0.5 + 1
|
2022-05-14 19:15:51 +02:00
|
|
|
this.vector = {0, 1}
|
|
|
|
this.area = {{position.x - a, position.y}, {position.x + a, position.y + 1}}
|
2020-05-09 10:04:24 +02:00
|
|
|
end,
|
|
|
|
['west'] = function(position)
|
2022-05-14 19:15:51 +02:00
|
|
|
local surface_index = this.surface_index
|
|
|
|
if not surface_index then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
local surface = game.get_surface(surface_index)
|
|
|
|
if not surface.valid then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
local width = surface.map_gen_settings.height
|
|
|
|
if width > this.max_line_size then
|
|
|
|
width = this.max_line_size
|
2020-05-09 10:04:24 +02:00
|
|
|
end
|
|
|
|
local a = width * 0.5 + 1
|
2022-05-14 19:15:51 +02:00
|
|
|
this.vector = {-1, 0}
|
|
|
|
this.area = {{position.x - 1, position.y - a}, {position.x, position.y + a}}
|
2020-05-09 10:04:24 +02:00
|
|
|
end,
|
|
|
|
['east'] = function(position)
|
2022-05-14 19:15:51 +02:00
|
|
|
local surface_index = this.surface_index
|
|
|
|
if not surface_index then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
local surface = game.get_surface(surface_index)
|
|
|
|
if not surface.valid then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
local width = surface.map_gen_settings.height
|
|
|
|
if width > this.max_line_size then
|
|
|
|
width = this.max_line_size
|
2020-05-09 10:04:24 +02:00
|
|
|
end
|
|
|
|
local a = width * 0.5 + 1
|
2022-05-14 19:15:51 +02:00
|
|
|
this.vector = {1, 0}
|
|
|
|
this.area = {{position.x, position.y - a}, {position.x + 1, position.y + a}}
|
2020-05-09 10:04:24 +02:00
|
|
|
end
|
2020-04-29 13:12:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
local function print_debug(a)
|
2022-05-14 19:15:51 +02:00
|
|
|
if not this.debug then
|
2020-06-03 20:09:37 +02:00
|
|
|
return
|
|
|
|
end
|
2020-05-09 10:04:24 +02:00
|
|
|
print('Collapse error #' .. a)
|
2020-04-29 13:12:42 +02:00
|
|
|
end
|
|
|
|
|
2020-04-29 15:56:57 +02:00
|
|
|
local function set_collapse_tiles(surface)
|
2020-06-03 20:09:37 +02:00
|
|
|
if not surface or surface.valid then
|
|
|
|
print_debug(45)
|
|
|
|
end
|
2022-05-14 19:15:51 +02:00
|
|
|
game.forces.player.chart(surface, this.area)
|
|
|
|
this.tiles = surface.find_tiles_filtered({area = this.area})
|
|
|
|
if not this.tiles then
|
2020-05-09 10:04:24 +02:00
|
|
|
return
|
|
|
|
end
|
2022-05-14 19:15:51 +02:00
|
|
|
this.size_of_tiles = #this.tiles
|
|
|
|
if this.size_of_tiles > 0 then
|
|
|
|
table_shuffle_table(this.tiles)
|
2020-05-09 10:04:24 +02:00
|
|
|
end
|
2022-05-14 19:15:51 +02:00
|
|
|
this.position = {x = this.position.x + this.vector[1], y = this.position.y + this.vector[2]}
|
|
|
|
local v = this.vector
|
|
|
|
local area = this.area
|
|
|
|
this.area = {{area[1][1] + v[1], area[1][2] + v[2]}, {area[2][1] + v[1], area[2][2] + v[2]}}
|
|
|
|
game.forces.player.chart(surface, this.area)
|
2020-04-29 13:12:42 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
local function progress()
|
2022-05-14 19:15:51 +02:00
|
|
|
local surface_index = this.surface_index
|
|
|
|
if not surface_index then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
local surface = game.get_surface(surface_index)
|
|
|
|
if not surface.valid then
|
|
|
|
return
|
|
|
|
end
|
2020-05-09 10:04:24 +02:00
|
|
|
|
2022-05-14 19:15:51 +02:00
|
|
|
if not this.start_now then
|
|
|
|
this.tiles = nil
|
2020-05-09 10:04:24 +02:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2022-05-14 19:15:51 +02:00
|
|
|
local tiles = this.tiles
|
2020-05-09 10:04:24 +02:00
|
|
|
if not tiles then
|
|
|
|
set_collapse_tiles(surface)
|
2022-05-14 19:15:51 +02:00
|
|
|
tiles = this.tiles
|
2020-05-09 10:04:24 +02:00
|
|
|
end
|
|
|
|
if not tiles then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2022-05-14 19:15:51 +02:00
|
|
|
for _ = 1, this.amount, 1 do
|
|
|
|
local tile = tiles[this.size_of_tiles]
|
2020-05-09 10:04:24 +02:00
|
|
|
if not tile then
|
2022-05-14 19:15:51 +02:00
|
|
|
this.tiles = nil
|
2020-05-09 10:04:24 +02:00
|
|
|
return
|
|
|
|
end
|
2022-05-14 19:15:51 +02:00
|
|
|
this.size_of_tiles = this.size_of_tiles - 1
|
2020-05-09 10:04:24 +02:00
|
|
|
if not tile.valid then
|
|
|
|
return
|
|
|
|
end
|
2022-05-14 19:15:51 +02:00
|
|
|
if this.specific_entities.enabled then
|
2020-05-09 10:04:24 +02:00
|
|
|
local position = {tile.position.x + 0.5, tile.position.y + 0.5}
|
2022-05-14 19:15:51 +02:00
|
|
|
local entities = this.specific_entities.entities
|
2021-01-12 13:55:09 +02:00
|
|
|
for _, e in pairs(surface.find_entities_filtered({area = {{position[1] - 2, position[2] - 2}, {position[1] + 2, position[2] + 2}}})) do
|
2020-09-02 08:33:13 +02:00
|
|
|
if entities[e.name] and e.valid and e.health then
|
2020-05-09 10:04:24 +02:00
|
|
|
e.die()
|
2020-08-31 20:37:27 +02:00
|
|
|
elseif e.valid then
|
|
|
|
e.destroy()
|
2020-05-09 10:04:24 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2022-05-14 19:15:51 +02:00
|
|
|
if this.kill then
|
2020-09-02 08:33:13 +02:00
|
|
|
local position = {tile.position.x + 0.5, tile.position.y + 0.5}
|
2021-01-12 13:55:09 +02:00
|
|
|
for _, e in pairs(surface.find_entities_filtered({area = {{position[1] - 2, position[2] - 2}, {position[1] + 2, position[2] + 2}}})) do
|
2020-09-02 08:33:13 +02:00
|
|
|
if e.valid and e.health then
|
|
|
|
e.die()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2020-05-09 10:04:24 +02:00
|
|
|
surface.set_tiles({{name = 'out-of-map', position = tile.position}}, true)
|
|
|
|
end
|
2020-04-29 13:12:42 +02:00
|
|
|
end
|
|
|
|
|
2022-05-14 19:15:51 +02:00
|
|
|
function Public.set_surface_index(surface_index)
|
|
|
|
if not surface_index then
|
2020-05-09 10:04:24 +02:00
|
|
|
print_debug(1)
|
|
|
|
return
|
|
|
|
end
|
2022-05-14 19:15:51 +02:00
|
|
|
|
|
|
|
local surface = game.get_surface(surface_index)
|
2020-05-09 10:04:24 +02:00
|
|
|
if not surface.valid then
|
|
|
|
print_debug(2)
|
|
|
|
return
|
|
|
|
end
|
2022-05-14 19:15:51 +02:00
|
|
|
|
|
|
|
this.surface_index = surface_index
|
2020-04-29 13:12:42 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
function Public.set_direction(direction)
|
2020-05-09 10:04:24 +02:00
|
|
|
if not directions[direction] then
|
|
|
|
print_debug(11)
|
|
|
|
return
|
|
|
|
end
|
2022-05-14 19:15:51 +02:00
|
|
|
directions[direction](this.position)
|
2020-04-29 13:12:42 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
function Public.set_speed(speed)
|
2020-05-09 10:04:24 +02:00
|
|
|
if not speed then
|
|
|
|
print_debug(8)
|
|
|
|
return
|
|
|
|
end
|
|
|
|
speed = math_floor(speed)
|
|
|
|
if speed < 1 then
|
|
|
|
speed = 1
|
|
|
|
end
|
2022-05-14 19:15:51 +02:00
|
|
|
this.speed = speed
|
2020-04-29 13:12:42 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
function Public.set_amount(amount)
|
2020-05-09 10:04:24 +02:00
|
|
|
if not amount then
|
|
|
|
print_debug(9)
|
|
|
|
return
|
|
|
|
end
|
|
|
|
amount = math_floor(amount)
|
|
|
|
if amount < 0 then
|
|
|
|
amount = 0
|
|
|
|
end
|
2022-05-14 19:15:51 +02:00
|
|
|
this.amount = amount
|
2020-04-29 13:12:42 +02:00
|
|
|
end
|
|
|
|
|
2020-04-29 14:25:03 +02:00
|
|
|
function Public.set_position(position)
|
2020-05-09 10:04:24 +02:00
|
|
|
if not position then
|
|
|
|
print_debug(4)
|
|
|
|
return
|
|
|
|
end
|
|
|
|
if not position.x and not position[1] then
|
|
|
|
print_debug(5)
|
|
|
|
return
|
|
|
|
end
|
|
|
|
if not position.y and not position[2] then
|
|
|
|
print_debug(6)
|
|
|
|
return
|
|
|
|
end
|
|
|
|
local x = 0
|
|
|
|
local y = 0
|
|
|
|
if position[1] then
|
|
|
|
x = position[1]
|
|
|
|
end
|
|
|
|
if position[2] then
|
|
|
|
y = position[2]
|
|
|
|
end
|
|
|
|
if position.x then
|
|
|
|
x = position.x
|
|
|
|
end
|
|
|
|
if position.y then
|
|
|
|
y = position.y
|
|
|
|
end
|
2022-05-14 19:15:51 +02:00
|
|
|
this.position = {x = x, y = y}
|
2020-04-29 13:12:42 +02:00
|
|
|
end
|
|
|
|
|
2020-04-29 14:25:03 +02:00
|
|
|
function Public.get_position()
|
2022-05-14 19:15:51 +02:00
|
|
|
return this.position
|
2020-05-09 10:04:24 +02:00
|
|
|
end
|
|
|
|
|
2021-02-26 01:04:20 +02:00
|
|
|
function Public.get_amount()
|
2022-05-14 19:15:51 +02:00
|
|
|
return this.amount
|
2021-02-26 01:04:20 +02:00
|
|
|
end
|
|
|
|
|
2021-02-26 01:05:32 +02:00
|
|
|
function Public.get_speed()
|
2022-05-14 19:15:51 +02:00
|
|
|
return this.speed
|
2021-02-26 01:05:32 +02:00
|
|
|
end
|
|
|
|
|
2020-05-09 10:04:24 +02:00
|
|
|
function Public.start_now(status)
|
|
|
|
if status == true then
|
2022-05-14 19:15:51 +02:00
|
|
|
this.start_now = true
|
2020-05-09 10:04:24 +02:00
|
|
|
elseif status == false then
|
2022-05-14 19:15:51 +02:00
|
|
|
this.start_now = false
|
2020-05-09 10:04:24 +02:00
|
|
|
end
|
2022-05-14 19:15:51 +02:00
|
|
|
return this.start_now
|
2020-04-29 14:25:03 +02:00
|
|
|
end
|
|
|
|
|
2020-04-29 13:12:42 +02:00
|
|
|
function Public.set_max_line_size(size)
|
2020-05-09 10:04:24 +02:00
|
|
|
if not size then
|
|
|
|
print_debug(22)
|
|
|
|
return
|
|
|
|
end
|
|
|
|
size = math_floor(size)
|
|
|
|
if size <= 0 then
|
|
|
|
print_debug(21)
|
|
|
|
return
|
|
|
|
end
|
2022-05-14 19:15:51 +02:00
|
|
|
this.max_line_size = size
|
2020-04-29 13:12:42 +02:00
|
|
|
end
|
|
|
|
|
2020-04-29 14:25:03 +02:00
|
|
|
function Public.set_kill_entities(a)
|
2022-05-14 19:15:51 +02:00
|
|
|
this.kill = a
|
2020-04-29 13:12:42 +02:00
|
|
|
end
|
|
|
|
|
2020-09-02 08:33:13 +02:00
|
|
|
function Public.set_kill_specific_entities(tbl)
|
|
|
|
if tbl then
|
2022-05-14 19:15:51 +02:00
|
|
|
this.specific_entities = tbl
|
2020-09-02 08:33:13 +02:00
|
|
|
else
|
2022-05-14 19:15:51 +02:00
|
|
|
this.specific_entities = {
|
2020-09-02 08:33:13 +02:00
|
|
|
enabled = false
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-04-29 13:12:42 +02:00
|
|
|
local function on_init()
|
2022-05-14 19:15:51 +02:00
|
|
|
Public.set_surface_index(game.surfaces.nauvis.index)
|
2020-05-09 10:04:24 +02:00
|
|
|
Public.set_position({0, 32})
|
|
|
|
Public.set_max_line_size(256)
|
|
|
|
Public.set_direction('north')
|
|
|
|
Public.set_kill_entities(true)
|
2020-09-02 08:33:13 +02:00
|
|
|
Public.set_kill_specific_entities()
|
2022-05-14 19:15:51 +02:00
|
|
|
this.tiles = nil
|
|
|
|
this.speed = 1
|
|
|
|
this.amount = 8
|
|
|
|
this.start_now = false
|
2020-04-29 13:12:42 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
local function on_tick()
|
2022-05-14 19:15:51 +02:00
|
|
|
local tick = game.tick
|
|
|
|
if tick % this.speed ~= 0 then
|
2020-05-09 10:04:24 +02:00
|
|
|
return
|
|
|
|
end
|
2022-05-14 19:15:51 +02:00
|
|
|
|
2020-05-09 10:04:24 +02:00
|
|
|
progress()
|
2020-04-29 13:12:42 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
Event.on_init(on_init)
|
|
|
|
Event.add(defines.events.on_tick, on_tick)
|
|
|
|
|
2020-05-09 10:04:24 +02:00
|
|
|
return Public
|