1
0
mirror of https://github.com/ComfyFactory/ComfyFactorio.git synced 2025-01-04 00:15:45 +02:00

improvements

This commit is contained in:
MewMew 2019-11-14 23:06:15 +01:00
parent 5d2bb86e8f
commit ca9ef3c2cc
2 changed files with 97 additions and 62 deletions

View File

@ -256,7 +256,6 @@ local function on_entity_died(event)
if entity.type ~= "unit-spawner" then return end
local str
if entity.force.name == "east" then
game.print("East lost their Hatchery.", {100, 100, 100})
game.forces.east.play_sound{path="utility/game_lost", volume_modifier=0.85}

View File

@ -3,11 +3,12 @@ local simplex_noise = require "utils.simplex_noise".d2
local math_random = math.random
local math_abs = math.abs
local math_sqrt = math.sqrt
local level_depth = require "maps.mountain_fortress_v2.terrain"
local math_floor = math.floor
local table_remove = table.remove
local table_insert = table.insert
local table_shuffle_table = table.shuffle_table
local chart_radius = 30
local start_chunk_y = 5
local tile_conversion = {
["concrete"] = "stone-path",
["hazard-concrete-left"] = "stone-path",
@ -18,14 +19,14 @@ local tile_conversion = {
["stone-path"] = "landfill",
}
local function get_collapse_vectors(radius)
local size_of_vector_list = 128
local function get_collapse_vectors(radius, seed)
local vectors = {}
local i = 1
local m = 1 / (radius * 2)
local seed = math_random(1, 9999999)
for x = radius * -1, radius, 1 do
for y = radius * -1, radius, 1 do
local noise = math_abs(simplex_noise(x * m, y * m, seed) * radius * 1.3)
local noise = math_abs(simplex_noise(x * m, y * m, seed) * radius * 1.2)
local d = math_sqrt(x ^ 2 + y ^ 2)
if d + noise < radius then
vectors[i] = {x, y}
@ -33,85 +34,116 @@ local function get_collapse_vectors(radius)
end
end
end
return vectors
end
local function set_x_positions()
local x_positions = global.map_collapse.x_positions
for x = level_depth * -1, level_depth - 1, 1 do
table_insert(x_positions, x)
local sorted_vectors = {}
for _, vector in pairs(vectors) do
local index = math_floor(math_sqrt(vector[1] ^ 2 + vector[2] ^ 2)) + 1
if not sorted_vectors[index] then sorted_vectors[index] = {} end
sorted_vectors[index][#sorted_vectors[index] + 1] = vector
end
table.shuffle_table(x_positions)
end
local function get_position(surface)
local x_positions = global.map_collapse.x_positions
if #x_positions == 0 then set_x_positions() end
local x = x_positions[1]
local position = false
for y = 160, -100000, -1 do
y = y + math_random(1, 16)
local tile = surface.get_tile({x, y})
if tile.valid then
if tile.name ~= "out-of-map" then
position = {x = x, y = y}
break
else
y = y + math_random(8, 32)
end
else
y = y + 96
end
end
table_remove(x_positions, 1)
return position
end
local function sort_list_by_distance(center_position, list)
local sorted_list = {}
for _, item in pairs(list) do
local d = math_floor(math_sqrt((item.position.x - center_position.x)^2 + (item.position.y - center_position.y)^2)) + 1
if not sorted_list[d] then sorted_list[d] = {} end
sorted_list[d][#sorted_list[d] + 1] = item
end
local final_list = {}
for _, row in pairs(sorted_list) do
table.shuffle_table(row)
local final_list = {}
for _, row in pairs(sorted_vectors) do
table_shuffle_table(row)
for _, tile in pairs(row) do
table_insert(final_list, tile)
end
end
return final_list
end
local function set_positions(surface)
local level_width = surface.map_gen_settings.width
local row_count = level_width * 32
local chunk_x = math.floor((level_width * -0.5) / 32)
local chunk_y = false
local position_x = level_width * -0.5
local position_y = false
local area = false
for y = start_chunk_y, -1024, -1 do
if surface.is_chunk_generated({chunk_x, y}) then
position_y = y * 32
area = {{position_x, position_y},{position_x + level_width, position_y + 32}}
if surface.count_tiles_filtered({name = "out-of-map", area = area}) < row_count then
break
else
area = false
end
end
end
if not area then return end
local tile_positions = {}
local i = 1
for _, tile in pairs(surface.find_tiles_filtered({area = area})) do
if tile.valid then
if tile.name ~= "out-of-map" then
tile_positions[i] = {tile.position.x, tile.position.y}
i = i + 1
end
end
end
if #tile_positions > 1 then table_shuffle_table(tile_positions) end
for k, p in pairs(tile_positions) do
global.map_collapse.positions[k] = {p[1], p[2]}
if k > 256 then break end
end
return true
end
local function set_collapse_tiles(surface, position, vectors)
local tiles = {}
local i = 1
for _, vector in pairs(vectors) do
local position = {x = position.x + vector[1], y = position.y + vector[2]}
local position = {x = position[1] + vector[1], y = position[2] + vector[2]}
local tile = surface.get_tile(position)
if tile.valid then
tiles[i] = tile
i = i + 1
end
end
local sorted_tiles = sort_list_by_distance(position, tiles)
table_insert(global.map_collapse.processing, sorted_tiles)
table_insert(global.map_collapse.processing, tiles)
end
local function collapse_map()
local function setup_next_collapse()
local surface = game.surfaces[global.active_surface_index]
local vectors = get_collapse_vectors(math_random(8, 24))
local position = get_position(surface)
if not position then return end
local map_collapse = global.map_collapse
if not map_collapse.vector_list then
map_collapse.vector_list = {}
for _ = 1, size_of_vector_list, 1 do
table_insert(map_collapse.vector_list, get_collapse_vectors(math_random(16, 24), math_random(1, 9999999)))
end
end
local size_of_positions = #map_collapse.positions
if size_of_positions == 0 then
if not set_positions(surface) then return end
end
local vectors = map_collapse.vector_list[math_random(1, size_of_vector_list)]
local position = map_collapse.positions[size_of_positions]
if not position then return end
local tile = surface.get_tile(position)
if not tile.valid then map_collapse.positions[size_of_positions] = nil return end
if tile.name == "out-of-map" then map_collapse.positions[size_of_positions] = nil return end
set_collapse_tiles(surface, position, vectors)
local last_position = global.map_collapse.last_position
game.forces.player.chart(surface, {{last_position.x - chart_radius, last_position.y - chart_radius},{last_position.x + chart_radius, last_position.y + chart_radius}})
global.map_collapse.last_position = {x = position.x, y = position.y}
game.forces.player.chart(surface, {{position.x - chart_radius, position.y - chart_radius},{position.x + chart_radius, position.y + chart_radius}})
set_collapse_tiles(surface, position, vectors)
global.map_collapse.last_position = {x = position[1], y = position[2]}
game.forces.player.chart(surface, {{position[1] - chart_radius, position[2] - chart_radius},{position[1] + chart_radius, position[2] + chart_radius}})
end
function Public.delete_out_of_map_chunks(surface)
@ -126,7 +158,11 @@ end
function Public.process()
local processing = global.map_collapse.processing
if #processing == 0 then collapse_map() return end
if #processing == 0 then
global.map_collapse.positions[#global.map_collapse.positions] = nil
setup_next_collapse()
return
end
local surface = game.surfaces[global.active_surface_index]
for k1, tile_set in pairs(processing) do
for k2, tile in pairs(tile_set) do
@ -146,7 +182,7 @@ end
function Public.init()
global.map_collapse = {}
global.map_collapse.x_positions = {}
global.map_collapse.positions = {}
global.map_collapse.processing = {}
global.map_collapse.last_position = {x = 0, y = 0}
end