From fab41fbf8b3ffca8aa8feee79c074dad12d34a91 Mon Sep 17 00:00:00 2001 From: grilledham Date: Mon, 2 Jul 2018 10:51:22 +0100 Subject: [PATCH 1/2] line and square map --- config.lua | 2 +- map_gen/presets/lines_and_squares.lua | 62 +++++++++++++++++++++++++++ map_gen/shared/builders.lua | 26 +++++++++++ map_layout.lua | 5 ++- 4 files changed, 92 insertions(+), 3 deletions(-) create mode 100644 map_gen/presets/lines_and_squares.lua diff --git a/config.lua b/config.lua index 52935178..a4429ba9 100644 --- a/config.lua +++ b/config.lua @@ -1,4 +1,4 @@ -_DEBUG = false +_DEBUG = true global.scenario = {} global.spys = {"valansch", "air20"} diff --git a/map_gen/presets/lines_and_squares.lua b/map_gen/presets/lines_and_squares.lua new file mode 100644 index 00000000..2e7ec34f --- /dev/null +++ b/map_gen/presets/lines_and_squares.lua @@ -0,0 +1,62 @@ +local b = require 'map_gen.shared.builders' +local Random = require 'map_gen.shared.random' + +local track_seed1 = 1000 +local track_seed2 = 2000 + +local track_block_size = 30 +local track_lines = 50 +local track_chance = 6 -- 1 in x + +local h_track = { + b.line_x(2), + b.translate(b.line_x(2), 0, -3), + b.translate(b.line_x(2), 0, 3), + b.rectangle(2, 10), + b.translate(b.rectangle(2, 10), 15, 0), + b.translate(b.rectangle(2, 10), -15, 0) +} + +h_track = b.any(h_track) +h_track = b.single_x_pattern(h_track, 30) + +local v_track = { + b.line_y(2), + b.translate(b.line_y(2), -3, 0), + b.translate(b.line_y(2), 3, 0), + b.rectangle(10, 2), + b.translate(b.rectangle(10, 2), 0, 15), + b.translate(b.rectangle(10, 2), 0, -15) +} + +v_track = b.any(v_track) +v_track = b.single_y_pattern(v_track, 30) + +local random = Random.new(track_seed1, track_seed2) + +local function do_track_lines(track_shape) + local track_pattern = {} + + for _ = 1, track_lines do + local shape + if random:next_int(1, track_chance) == 1 then + shape = track_shape + else + shape = b.empty_shape() + end + + table.insert(track_pattern, shape) + end + + return track_pattern +end + +local h_tracks = do_track_lines(h_track) +h_tracks = b.grid_y_pattern(h_tracks, track_lines, track_block_size) + +local v_tracks = do_track_lines(v_track) +v_tracks = b.grid_x_pattern(v_tracks, track_lines, track_block_size) + +local tracks = b.any {h_tracks, v_tracks} + +return tracks diff --git a/map_gen/shared/builders.lua b/map_gen/shared/builders.lua index 3423b236..0e5a26d7 100644 --- a/map_gen/shared/builders.lua +++ b/map_gen/shared/builders.lua @@ -693,6 +693,32 @@ function Builders.single_y_pattern(shape, height) end end +function Builders.grid_x_pattern(pattern, columns, width) + local half_width = width / 2 + + return function(x, y, world) + local x2 = ((x + half_width) % width) - half_width + local columns_pos = math.floor(x / width + 0.5) + local column_i = columns_pos % columns + 1 + local shape = pattern[column_i] or Builders.empty_shape + + return shape(x2, y, world) + end +end + +function Builders.grid_y_pattern(pattern, rows, height) + local half_height = height / 2 + + return function(x, y, world) + local y2 = ((y + half_height) % height) - half_height + local row_pos = math.floor(y / height + 0.5) + local row_i = row_pos % rows + 1 + local shape = pattern[row_i] or Builders.empty_shape + + return shape(x, y2, world) + end +end + function Builders.grid_pattern(pattern, columns, rows, width, height) local half_width = width / 2 local half_height = height / 2 diff --git a/map_layout.lua b/map_layout.lua index bcdef45d..0d4ddcd3 100644 --- a/map_layout.lua +++ b/map_layout.lua @@ -67,6 +67,7 @@ local tiles_per_tick = 32 --shape = require "map_gen.presets.factory" --shape = require "map_gen.presets.triangle_of_death" --shape = require "map_gen.presets.world_map" +shape = require "map_gen.presets.lines_and_squares" --shape = require "map_gen.presets.test" --shapes-- @@ -136,6 +137,6 @@ if shape then ['nauvis'] = shape, } - require('map_gen.shared.generate')({surfaces = surfaces, regen_decoratives = regen_decoratives, tiles_per_tick = tiles_per_tick}) - --require ("map_gen.shared.generate_not_threaded")({surfaces = surfaces, regen_decoratives = regen_decoratives}) + --require('map_gen.shared.generate')({surfaces = surfaces, regen_decoratives = regen_decoratives, tiles_per_tick = tiles_per_tick}) + require ("map_gen.shared.generate_not_threaded")({surfaces = surfaces, regen_decoratives = regen_decoratives}) end From 39fd9a7e06959f07f01831ddda9c12b12ae27d50 Mon Sep 17 00:00:00 2001 From: grilledham Date: Sat, 7 Jul 2018 12:24:09 +0100 Subject: [PATCH 2/2] new maps --- map_gen/presets/lines_and_squares.lua | 309 ++++++++++++++++++++++++-- map_gen/presets/spiral_of_spirals.lua | 12 + map_gen/presets/test.lua | 65 +----- map_gen/shared/builders.lua | 82 +++++++ map_layout.lua | 7 +- 5 files changed, 391 insertions(+), 84 deletions(-) create mode 100644 map_gen/presets/spiral_of_spirals.lua diff --git a/map_gen/presets/lines_and_squares.lua b/map_gen/presets/lines_and_squares.lua index 2e7ec34f..d11273fc 100644 --- a/map_gen/presets/lines_and_squares.lua +++ b/map_gen/presets/lines_and_squares.lua @@ -1,62 +1,323 @@ local b = require 'map_gen.shared.builders' local Random = require 'map_gen.shared.random' -local track_seed1 = 1000 -local track_seed2 = 2000 +local track_seed1 = 33000 +local track_seed2 = track_seed1 * 2 +local ore_seed1 = 11000 +local ore_seed2 = ore_seed1 * 2 -local track_block_size = 30 -local track_lines = 50 -local track_chance = 6 -- 1 in x +local block_size = 30 * 1 +local track_lines = 32 +local track_chance = 1 / 3 +local block_chance = 1 / 5 +local number_blocks = 25 + +local ore_blocks = 32 +local ore_block_size = 30 + +local blocks_size = track_lines * block_size +local offset = (track_lines * 0.5 + 0.5) * block_size local h_track = { b.line_x(2), b.translate(b.line_x(2), 0, -3), b.translate(b.line_x(2), 0, 3), - b.rectangle(2, 10), - b.translate(b.rectangle(2, 10), 15, 0), - b.translate(b.rectangle(2, 10), -15, 0) + b.rectangle(2, 10) } h_track = b.any(h_track) -h_track = b.single_x_pattern(h_track, 30) +h_track = b.single_x_pattern(h_track, 15) local v_track = { b.line_y(2), b.translate(b.line_y(2), -3, 0), b.translate(b.line_y(2), 3, 0), - b.rectangle(10, 2), - b.translate(b.rectangle(10, 2), 0, 15), - b.translate(b.rectangle(10, 2), 0, -15) + b.rectangle(10, 2) } v_track = b.any(v_track) -v_track = b.single_y_pattern(v_track, 30) +v_track = b.single_y_pattern(v_track, 15) + +local v_line_left = b.translate(v_track, -12, 0) +local v_line_right = b.translate(v_track, 18, 0) +local h_line_top = b.translate(h_track, 0, -12) +local h_line_bottom = b.translate(h_track, 0, 18) local random = Random.new(track_seed1, track_seed2) +local random_ore = Random.new(ore_seed1, ore_seed2) + +local function do_track_lines(lines, track_shape, first_track, last_track) + if #lines == 0 then + return + end -local function do_track_lines(track_shape) local track_pattern = {} - for _ = 1, track_lines do + table.insert(track_pattern, first_track) + + local n_i = 2 + local n = lines[n_i] + + for i = 2, track_lines - 1 do local shape - if random:next_int(1, track_chance) == 1 then + if i == n then shape = track_shape + n_i = n_i + 1 + n = lines[n_i] else - shape = b.empty_shape() + shape = b.empty_shape end - table.insert(track_pattern, shape) + track_pattern[i] = shape end + table.insert(track_pattern, last_track) + return track_pattern end -local h_tracks = do_track_lines(h_track) -h_tracks = b.grid_y_pattern(h_tracks, track_lines, track_block_size) +local squares = { + {shape = b.rectangle(16), weight = 3}, + {shape = b.rectangle(32), weight = 2}, + {shape = b.rectangle(48), weight = 1} +} -local v_tracks = do_track_lines(v_track) -v_tracks = b.grid_x_pattern(v_tracks, track_lines, track_block_size) +local total_square_weights = {} +local square_t = 0 +for _, v in ipairs(squares) do + square_t = square_t + v.weight + table.insert(total_square_weights, square_t) +end -local tracks = b.any {h_tracks, v_tracks} +local function value(base, mult, pow) + return function(x, y) + local d = math.sqrt(x * x + y * y) + return base + mult * d ^ pow + end +end -return tracks +local function non_transform(shape) + return shape +end + +local function uranium_transform(shape) + return b.scale(shape, 0.5) +end + +local function oil_transform(shape) + shape = b.scale(shape, 0.5) + return b.throttle_world_xy(shape, 1, 4, 1, 4) +end + +local function empty_transform() + return b.empty_shape +end + +local ores = { + {transform = non_transform, resource = 'iron-ore', value = value(500, 0.75, 1.1), weight = 16}, + {transform = non_transform, resource = 'copper-ore', value = value(400, 0.75, 1.1), weight = 10}, + {transform = non_transform, resource = 'stone', value = value(250, 0.3, 1.05), weight = 3}, + {transform = non_transform, resource = 'coal', value = value(400, 0.8, 1.075), weight = 5}, + {transform = uranium_transform, resource = 'uranium-ore', value = value(200, 0.3, 1.025), weight = 3}, + {transform = oil_transform, resource = 'crude-oil', value = value(100000, 50, 1.025), weight = 6}, + {transform = empty_transform, weight = 300} +} + +local total_ore_weights = {} +local ore_t = 0 +for _, v in ipairs(ores) do + ore_t = ore_t + v.weight + table.insert(total_ore_weights, ore_t) +end + +local function do_resources() + local pattern = {} + + for r = 1, ore_blocks do + local row = {} + pattern[r] = row + for c = 1, ore_blocks do + local shape + local i = random_ore:next_int(1, square_t) + local index = table.binary_search(total_square_weights, i) + if (index < 0) then + index = bit32.bnot(index) + end + shape = squares[index].shape + + local ore_data + i = random_ore:next_int(1, ore_t) + index = table.binary_search(total_ore_weights, i) + if (index < 0) then + index = bit32.bnot(index) + end + ore_data = ores[index] + + shape = ore_data.transform(shape) + local ore = b.resource(shape, ore_data.resource, ore_data.value) + + row[c] = ore + end + end + + local ore_shape = b.grid_pattern_full_overlap(pattern, ore_blocks, ore_blocks, ore_block_size, ore_block_size) + return ore_shape +end + +local worm_names = {'small-worm-turret', 'medium-worm-turret', 'big-worm-turret'} +local safe_d = 300 +local half_spawn_d = 50000 -- distance at which there is a half chance of a worm spawning +local max_spawn_rate = 1 / 12 +local level_factor = 32 -- higher factor -> more likly to spawn higher level worms +local min_big_worm_d = 900 + +local hd = 1 / (2 * half_spawn_d) +local inv_level_factor = 1 / level_factor + +local function worms(_, _, world) + local x, y = world.x, world.y + local d = math.sqrt(x * x + y * y) + + d = d - safe_d + if d <= 0 then + return nil + end + + local chance = d * hd + if math.random() > math.min(chance, max_spawn_rate) then + return nil + end + + local lf = inv_level_factor / chance + + local lvl = math.floor(math.random() ^ lf * 3) + 1 + + if d < min_big_worm_d and lvl == 3 then + lvl = 2 + end + + return {name = worm_names[lvl]} +end + +local empty = b.empty_shape +local water = b.tile('water') + +local function do_blocks() + local vertical_lines = {1} + local horizontal_lines = {1} + + local prev_v, prev_h = false, false + + for i = 3, track_lines - 2 do + if prev_v then + prev_v = false + else + if random:next() < track_chance then + prev_v = true + table.insert(vertical_lines, i) + end + end + if prev_h then + prev_h = false + else + if random:next() < track_chance then + prev_h = true + table.insert(horizontal_lines, i) + end + end + end + + if #vertical_lines == 1 then + table.insert(vertical_lines, track_lines - 2) + end + if #horizontal_lines == 1 then + table.insert(horizontal_lines, track_lines - 2) + end + + local block_pattern = {} + for _ = 1, track_lines do + table.insert(block_pattern, {}) + end + + for hi = 1, #horizontal_lines - 1 do + local h = horizontal_lines[hi] + local h_next = horizontal_lines[hi + 1] + + for vi = 1, #vertical_lines - 1 do + local v = vertical_lines[vi] + local v_next = vertical_lines[vi + 1] + + local shape + if random:next() < block_chance then + shape = b.full_shape + else + shape = empty + end + + for row_i = h, h_next do + local row = block_pattern[row_i] + for col_i = v, v_next do + if row[col_i] ~= b.full_shape then + row[col_i] = shape + end + end + end + end + end + + local v_last = vertical_lines[#vertical_lines] + local h_last = horizontal_lines[#horizontal_lines] + for h = 1, track_lines do + local row = block_pattern[h] + for _ = v_last, track_lines do + table.insert(row, empty) + end + end + + for _ = 1, track_lines - 1 do + for h = h_last, track_lines do + local row = block_pattern[h] + + table.insert(row, empty) + end + end + + local blocks = b.grid_pattern(block_pattern, track_lines, track_lines, block_size, block_size) + local resources = do_resources() + blocks = b.apply_entity(blocks, resources) + blocks = b.apply_entity(blocks, worms) + + local h_tracks = do_track_lines(horizontal_lines, h_track, h_line_top, h_line_bottom) + h_tracks = b.grid_y_pattern(h_tracks, track_lines, block_size) + + local v_tracks = do_track_lines(vertical_lines, v_track, v_line_left, v_line_right) + v_tracks = b.grid_x_pattern(v_tracks, track_lines, block_size) + + local tracks = b.any {h_tracks, v_tracks} + + local map = b.any {blocks, tracks} + + map = b.if_else(map, water) + + map = b.translate(map, offset, offset) + + return map +end + +local blocks_pattern = {} + +for _ = 1, number_blocks do + local row = {} + for _ = 1, number_blocks do + table.insert(row, do_blocks()) + end + table.insert(blocks_pattern, row) +end + +local map = b.grid_pattern(blocks_pattern, number_blocks, number_blocks, blocks_size, blocks_size) +map = b.change_map_gen_collision_tile(map, 'water-tile', 'grass-1') +map = b.fish(map, 0.00125) + +map = b.translate(map, 1151, 703) + +return map diff --git a/map_gen/presets/spiral_of_spirals.lua b/map_gen/presets/spiral_of_spirals.lua new file mode 100644 index 00000000..db9504d9 --- /dev/null +++ b/map_gen/presets/spiral_of_spirals.lua @@ -0,0 +1,12 @@ +local b = require 'map_gen.shared.builders' + +local spiral = b.rectangular_spiral(1) + +local factor = 9 +local shape = b.single_spiral_rotate_pattern(spiral, factor, factor) +factor = factor * factor +shape = b.single_spiral_rotate_pattern(shape, factor, factor) + +shape = b.scale(shape, 64) + +return shape diff --git a/map_gen/presets/test.lua b/map_gen/presets/test.lua index d770edad..a98f9c64 100644 --- a/map_gen/presets/test.lua +++ b/map_gen/presets/test.lua @@ -1,62 +1,13 @@ local b = require 'map_gen.shared.builders' -local tile_map = { - [1] = false, - [2] = true, - [3] = 'concrete', - [4] = 'deepwater-green', - [5] = 'deepwater', - [6] = 'dirt-1', - [7] = 'dirt-2', - [8] = 'dirt-3', - [9] = 'dirt-4', - [10] = 'dirt-5', - [11] = 'dirt-6', - [12] = 'dirt-7', - [13] = 'dry-dirt', - [14] = 'grass-1', - [15] = 'grass-2', - [16] = 'grass-3', - [17] = 'grass-4', - [18] = 'hazard-concrete-left', - [19] = 'hazard-concrete-right', - [20] = 'lab-dark-1', - [21] = 'lab-dark-2', - [22] = 'lab-white', - [23] = 'out-of-map', - [24] = 'red-desert-0', - [25] = 'red-desert-1', - [26] = 'red-desert-2', - [27] = 'red-desert-3', - [28] = 'sand-1', - [29] = 'sand-2', - [30] = 'sand-3', - [31] = 'stone-path', - [32] = 'water-green', - [33] = 'water' -} +local spiral = b.rectangular_spiral(1) -local data = {} +local factor = 9 +local f = factor +local shape = b.single_spiral_rotate_pattern(spiral, f, f) +f = f * factor +shape = b.single_spiral_rotate_pattern(shape, f, f) -for count = 1, 33 do - local r = count % 10 +shape = b.scale(shape, 64) - local row - if r == 1 then - row = {} - table.insert(data, row) - else - row = data[#data] - end - - row[#row + 1] = tile_map[count] -end - -local tiles = {} -tiles.width = 10 -tiles.height = 4 -tiles.data = data - -local pic = b.picture(tiles) - -return b.scale(pic, 4) +return shape diff --git a/map_gen/shared/builders.lua b/map_gen/shared/builders.lua index 0e5a26d7..34e9b5f4 100644 --- a/map_gen/shared/builders.lua +++ b/map_gen/shared/builders.lua @@ -865,6 +865,88 @@ function Builders.grid_pattern_full_overlap(pattern, columns, rows, width, heigh end end +local function is_spiral(x, y) + local a = -math.max(math.abs(x), math.abs(y)) + + if a % 2 == 0 then + return y ~= a or x == a + else + return y == a and x ~= a + end +end + +function Builders.single_spiral_pattern(shape, width, height) + local inv_width = 1 / width + local inv_height = 1 / height + return function(x, y, world) + local x1 = math.floor(x * inv_width + 0.5) + local y1 = math.floor(y * inv_height + 0.5) + + if is_spiral(x1, y1) then + x1 = x - x1 * width + y1 = y - y1 * height + return shape(x1, y1, world) + else + return false + end + end +end + +local function rotate_0(x, y) + return x, y +end + +local function rotate_90(x, y) + return y, -x +end + +local function rotate_180(x, y) + return -x, -y +end + +local function rotate_270(x, y) + return -y, x +end + +local function spiral_rotation(x, y) + local a = -math.max(math.abs(x), math.abs(y)) + + if a % 2 == 0 then + if y ~= a or x == a then + if x == a then + return rotate_0 + elseif y >= x then + return rotate_270 + else + return rotate_180 + end + end + else + if y == a and x ~= a then + return rotate_90 + end + end +end + +function Builders.single_spiral_rotate_pattern(shape, width, height) + local inv_width = 1 / width + local inv_height = 1 / height + return function(x, y, world) + local x1 = math.floor(x * inv_width + 0.5) + local y1 = math.floor(y * inv_height + 0.5) + + local t = spiral_rotation(x1, y1) + if t then + x1 = x - x1 * width + y1 = y - y1 * height + x1, y1 = t(x1, y1) + return shape(x1, y1, world) + else + return false + end + end +end + function Builders.segment_pattern(pattern) local count = #pattern diff --git a/map_layout.lua b/map_layout.lua index 0d4ddcd3..ca8721d7 100644 --- a/map_layout.lua +++ b/map_layout.lua @@ -67,7 +67,8 @@ local tiles_per_tick = 32 --shape = require "map_gen.presets.factory" --shape = require "map_gen.presets.triangle_of_death" --shape = require "map_gen.presets.world_map" -shape = require "map_gen.presets.lines_and_squares" +--shape = require "map_gen.presets.lines_and_squares" +--shape = require "map_gen.presets.spiral_of_spirals" --shape = require "map_gen.presets.test" --shapes-- @@ -137,6 +138,6 @@ if shape then ['nauvis'] = shape, } - --require('map_gen.shared.generate')({surfaces = surfaces, regen_decoratives = regen_decoratives, tiles_per_tick = tiles_per_tick}) - require ("map_gen.shared.generate_not_threaded")({surfaces = surfaces, regen_decoratives = regen_decoratives}) + require('map_gen.shared.generate')({surfaces = surfaces, regen_decoratives = regen_decoratives, tiles_per_tick = tiles_per_tick}) + --require ("map_gen.shared.generate_not_threaded")({surfaces = surfaces, regen_decoratives = regen_decoratives}) end