From 5f7e97726c984809494a4a1ec18be3b77ad29b6f Mon Sep 17 00:00:00 2001 From: MewMew Date: Fri, 3 Apr 2020 22:40:42 +0200 Subject: [PATCH] wip --- functions/random_room.lua | 165 +++++++++++++++++++++++++------------- maps/dungeons.lua | 69 ++++++++++++++++ 2 files changed, 177 insertions(+), 57 deletions(-) create mode 100644 maps/dungeons.lua diff --git a/functions/random_room.lua b/functions/random_room.lua index cfbbaaa2..4465f25c 100644 --- a/functions/random_room.lua +++ b/functions/random_room.lua @@ -1,14 +1,46 @@ +local Public = {} local table_shuffle_table = table.shuffle_table local table_insert = table.insert local math_random = math.random -local room_spacing = 2 +local room_spacing = 3 -local room_sizes = {} -for a = 3, 15, 2 do - table_insert(room_sizes, a) +local function build_room(surface, position, vector, room_center_position, room_radius) + local room = {} + + local a = room_radius - 1 + local room_area = { + left_top = {x = room_center_position.x - a, y = room_center_position.y - a}, + right_bottom = {x = room_center_position.x + room_radius, y = room_center_position.y + room_radius} + } + room.room_tiles = surface.find_tiles_filtered({area = room_area}) + + room.path_tiles = {} + for d = 1, room_spacing, 1 do + local p = {position.x + vector[1] * d, position.y + vector[2] * d} + local tile = surface.get_tile(p) + table_insert(room.path_tiles, tile) + end + + room.entrance_tile = surface.get_tile({position.x + vector[1] * (room_spacing + 1), position.y + vector[2] * (room_spacing + 1)}) + + room.room_border_tiles = {} + local left_top = {x = room_area.left_top.x - 1, y = room_area.left_top.y - 1} + local right_bottom = {x = room_area.right_bottom.x, y = room_area.right_bottom.y} + local t = room.room_border_tiles + for d = 1, room_radius * 2, 1 do + table_insert(t, surface.get_tile({left_top.x + d, left_top.y})) + table_insert(t, surface.get_tile({left_top.x, left_top.y + d})) + table_insert(t, surface.get_tile({right_bottom.x - d, right_bottom.y})) + table_insert(t, surface.get_tile({right_bottom.x, right_bottom.y - d})) + end + table_insert(t, surface.get_tile(left_top)) + table_insert(t, surface.get_tile(right_bottom)) + table_insert(t, surface.get_tile({left_top.x + room_radius * 2, left_top.y + room_radius * 2})) + table_insert(t, surface.get_tile({right_bottom.x - (room_radius * 2), right_bottom.y - (room_radius * 2)})) + + return room end -local size_of_room_sizes = #room_sizes local function scan_direction(surface, position, vector, room_radius) local valid_tile_count = 0 @@ -21,14 +53,14 @@ local function scan_direction(surface, position, vector, room_radius) end end - local a = room_radius + room_spacing * 2 - 1 + local a = room_radius + room_spacing + 1 local b = room_radius + room_spacing local room_center_position = {x = position.x + vector[1] * a, y = position.y + vector[2] * a} local search_area = { {x = room_center_position.x - b, y = room_center_position.y - b}, - {x = room_center_position.x + b, y = room_center_position.y + b} + {x = room_center_position.x + b + 1, y = room_center_position.y + b + 1} } local tiles = surface.find_tiles_filtered({area = search_area}) @@ -38,38 +70,7 @@ local function scan_direction(surface, position, vector, room_radius) end end - local room = {} - - local room_area = { - left_top = {x = room_center_position.x - room_radius, y = room_center_position.y - room_radius}, - right_bottom = {x = room_center_position.x + room_radius, y = room_center_position.y + room_radius} - } - local left_top = room_area.left_top - local right_bottom = room_area.right_bottom - - room.room_tiles = surface.find_tiles_filtered({area = room_area}) - - room.path_tiles = {} - for d = 1, room_spacing, 1 do - local p = {position.x + vector[1] * d, position.y + vector[2] * d} - local tile = surface.get_tile(p) - table_insert(room.path_tiles, tile) - end - - room.room_border_tiles = {} - local t = room.room_border_tiles - for d = 1, room_radius * 2, 1 do - table_insert(t, surface.get_tile({left_top.x + d, left_top.y})) - table_insert(t, surface.get_tile({left_top.x, left_top.y + d})) - table_insert(t, surface.get_tile({right_bottom.x - d, right_bottom.y})) - table_insert(t, surface.get_tile({right_bottom.x, right_bottom.y - d})) - end - table_insert(t, surface.get_tile(left_top)) - table_insert(t, surface.get_tile(right_bottom)) - table_insert(t, surface.get_tile({left_top.x + room_radius * 2, left_top.y + room_radius * 2})) - table_insert(t, surface.get_tile({right_bottom.x - (room_radius * 2), right_bottom.y - (room_radius * 2)})) - - return room + return build_room(surface, position, vector, room_center_position, room_radius) end local function get_room_tiles(surface, position, room_radius) @@ -81,33 +82,83 @@ local function get_room_tiles(surface, position, room_radius) if room then return room end + end + + for _, v in pairs(vectors) do + local room = scan_direction(surface, position, v, room_radius) + if room then + return room + end + end +end + +local function build_bridge(surface, position) + local vectors = {{0, -1}, {0, 1}, {1, 0}, {-1, 0}} + table_shuffle_table(vectors) + + local room = {} + room.path_tiles = {} + room.room_border_tiles = {} + room.room_tiles = {} + + local a = room_spacing * 2 + + for _, v in pairs(vectors) do + for d = 1, a, 1 do + local p = {position.x + v[1] * d, position.y + v[2] * d} + local tile = surface.get_tile(p) + if not tile.collides_with("resource-layer") then + break + end + table_insert(room.path_tiles, tile) + if d == a then room.path_tiles = {} end + end + if room.path_tiles[1] then return room end end end -local function get_room(surface, position) - local room_radius = room_sizes[math_random(1, size_of_room_sizes)] - - local room = get_room_tiles(surface, position, room_radius) - - return room -end - -function draw_random_room(surface, position) - local room = get_room(surface, position) - if not room then return end +function Public.get_room(surface, position) + local room_sizes = {} + for i = 1, 13, 1 do + room_sizes[i] = i + 1 + end + table_shuffle_table(room_sizes) - for _, tile in pairs(room.room_tiles) do - surface.set_tiles({{name = "grass-1", position = tile.position}}, true) + local last_size = room_sizes[1] + for i = 1, 13, 1 do + if room_sizes[i] <= last_size then + last_size = room_sizes[i] + local room = get_room_tiles(surface, position, last_size) + if room then + return room + end + end end + if math_random(1, 2) == 1 then return end + + local room = build_bridge(surface, position) + if room then return room end +end + +function Public.draw_random_room(surface, position) + local room = Public.get_room(surface, position) + if not room then return end + for _, tile in pairs(room.path_tiles) do - surface.set_tiles({{name = "dirt-1", position = tile.position}}, true) + surface.set_tiles({{name = "dirt-3", position = tile.position}}, true) end for _, tile in pairs(room.room_border_tiles) do - surface.set_tiles({{name = "concrete", position = tile.position}}, true) - if math_random(1, 4) == 1 then - surface.create_entity({name = "stone-wall", position = tile.position, force = "player"}) + surface.set_tiles({{name = "dirt-7", position = tile.position}}, true) + if math_random(1, 8) == 1 then + surface.create_entity({name = "rock-big", position = tile.position}) end end -end \ No newline at end of file + + for _, tile in pairs(room.room_tiles) do + surface.set_tiles({{name = "dirt-5", position = tile.position}}, true) + end +end + +return Public \ No newline at end of file diff --git a/maps/dungeons.lua b/maps/dungeons.lua new file mode 100644 index 00000000..61629469 --- /dev/null +++ b/maps/dungeons.lua @@ -0,0 +1,69 @@ +--lost-- mewmew made this -- + +local Rooms = require "functions.random_room" +local table_insert = table.insert +local math_random = math.random + +local disabled_for_deconstruction = { + ["fish"] = true, + ["rock-huge"] = true, + ["rock-big"] = true, + ["sand-rock-big"] = true, + ["mineable-wreckage"] = true + } + +local function on_chunk_generated(event) + local left_top = event.area.left_top + +end + +local function on_player_joined_game(event) + local player = game.players[event.player_index] + local surface = game.surfaces["dungeons"] + if player.online_time == 0 then + player.teleport(surface.find_non_colliding_position("character", {0, 0}, 50, 0.5), surface) + end +end + +local function on_player_mined_entity(event) + local entity = event.entity + if not entity.valid then return end + local surface = entity.surface + + Rooms.draw_random_room(surface, entity.position) +end + +local function on_marked_for_deconstruction(event) + if disabled_for_deconstruction[event.entity.name] then + event.entity.cancel_deconstruction(game.players[event.player_index].force.name) + end +end + +local function on_init() + local map_gen_settings = {} + map_gen_settings.height = 3 + map_gen_settings.width = 3 + local surface = game.create_surface("dungeons", map_gen_settings) + + surface.request_to_generate_chunks({0,0}, 2) + surface.force_generate_chunk_requests() + surface.create_entity({name = "rock-big", position = {0, -1}}) + + local surface = game.surfaces[1] + local map_gen_settings = surface.map_gen_settings + map_gen_settings.height = 3 + map_gen_settings.width = 3 + surface.map_gen_settings = map_gen_settings + for chunk in surface.get_chunks() do + surface.delete_chunk({chunk.x, chunk.y}) + end + + game.forces.player.manual_mining_speed_modifier = 10 +end + +local Event = require 'utils.event' +Event.on_init(on_init) +Event.add(defines.events.on_marked_for_deconstruction, on_marked_for_deconstruction) +Event.add(defines.events.on_player_joined_game, on_player_joined_game) +Event.add(defines.events.on_player_mined_entity, on_player_mined_entity) +Event.add(defines.events.on_chunk_generated, on_chunk_generated) \ No newline at end of file