From 6a39f6ab62dd3d0e8b21c5bde1e0a35f8af460e5 Mon Sep 17 00:00:00 2001 From: MewMew Date: Tue, 17 Sep 2019 20:04:47 +0200 Subject: [PATCH 1/2] map wip --- functions/noise_vector_path.lua | 26 +++++++++++++-- maps/island_troopers/main.lua | 5 +++ maps/island_troopers/terrain.lua | 57 ++++++++++++++++++++++++++++++++ 3 files changed, 85 insertions(+), 3 deletions(-) create mode 100644 maps/island_troopers/main.lua create mode 100644 maps/island_troopers/terrain.lua diff --git a/functions/noise_vector_path.lua b/functions/noise_vector_path.lua index 6a83dc03..39f57040 100644 --- a/functions/noise_vector_path.lua +++ b/functions/noise_vector_path.lua @@ -2,6 +2,18 @@ local simplex_noise = require "utils.simplex_noise".d2 +local function get_brush(size) + local vectors = {} + for x = size * -1, size, 1 do + for y = size * -1, size, 1 do + if math.sqrt(y ^ 2 + x ^ 2) <= size then + vectors[#vectors + 1] = {x, y} + end + end + end + return vectors +end + function noise_vector_entity_path(surface, entity_name, position, base_vector, length, collision) local seed_1 = math.random(1, 10000000) local seed_2 = math.random(1, 10000000) @@ -40,14 +52,20 @@ function noise_vector_entity_path(surface, entity_name, position, base_vector, l return entities end -function noise_vector_tile_path(surface, tile_name, position, base_vector, length) +function noise_vector_tile_path(surface, tile_name, position, base_vector, length, brush_size) local seed_1 = math.random(1, 10000000) local seed_2 = math.random(1, 10000000) local vector = {} - local minimal_movement = 0.5 + local tiles = {} + local minimal_movement = 0.75 + local brush_vectors = get_brush(brush_size) for a = 1, length, 1 do - surface.set_tiles({{name = tile_name, position = position}}, true) + tiles[#tiles + 1] = {name = tile_name, position = position} + for _, v in pairs(brush_vectors) do + surface.set_tiles({{name = tile_name, position = {position.x + v[1], position.y + v[2]}}}, true) + end + local noise = simplex_noise(position.x * 0.1, position.y * 0.1, seed_1) local noise_2 = simplex_noise(position.x * 0.1, position.y * 0.1, seed_2) @@ -65,6 +83,8 @@ function noise_vector_tile_path(surface, tile_name, position, base_vector, lengt position = {x = position.x + vector[1], y = position.y + vector[2]} end + + return tiles end --/c noise_vector_path(game.player.surface, "tree-04", game.player.position, {0,0}) \ No newline at end of file diff --git a/maps/island_troopers/main.lua b/maps/island_troopers/main.lua new file mode 100644 index 00000000..72d70360 --- /dev/null +++ b/maps/island_troopers/main.lua @@ -0,0 +1,5 @@ +require "functions.noise_vector_path" +require "maps.island_troopers.terrain" + + +--event.on_init(on_init) \ No newline at end of file diff --git a/maps/island_troopers/terrain.lua b/maps/island_troopers/terrain.lua new file mode 100644 index 00000000..f6e6b636 --- /dev/null +++ b/maps/island_troopers/terrain.lua @@ -0,0 +1,57 @@ +local map_functions = require "tools.map_functions" + +local function get_vector() + local v = {} + v[1] = (-100 + math.random(0, 200)) * 0.01 + v[2] = (-100 + math.random(0, 50)) * 0.01 + return v +end + +local function draw_island(surface, position, size) + map_functions.draw_noise_tile_circle(position, "grass-2", surface, size) +end + +local function draw_path(surface, position, length) + local tiles = noise_vector_tile_path(surface, "grass-1", position, get_vector(), length, math.random(2, 5)) + return tiles +end + +function kill_level(surface) + for _, t in pairs(surface.find_tiles_filtered({area = {{-256, -256}, {256, -32}}, name = {"grass-1", "grass-2"}})) do + surface.set_tiles({{name = "water", position = t.position}}, true) + end +end + +local function wipe_vision(surface) + for chunk in surface.get_chunks() do + if chunk.y < 0 then game.forces.player.unchart_chunk(chunk, surface) end + end +end + +function draw_level(surface) + kill_level(surface) + wipe_vision(surface) + local position = {x = 0, y = -32} + for a = 1, 3, 1 do + local size = math.random(20, 48) + local length = size * 3 + local t = draw_path(surface, position, length) + draw_island(surface, t[#t].position, size) + position = t[#t].position + end +end + +local function on_chunk_generated(event) + local left_top = event.area.left_top + local surface = event.surface + for x = 0, 31, 1 do + for y = 0, 31, 1 do + local p = {x = left_top.x + x, y = left_top.y + y} + if left_top.y < -32 then surface.set_tiles({{name = "water", position = p}}, true) end + if left_top.y > 32 then surface.set_tiles({{name = "water-green", position = p}}, true) end + end + end +end + +local event = require 'utils.event' +event.add(defines.events.on_chunk_generated, on_chunk_generated) \ No newline at end of file From 0aa0253d501c2dd1624ae3afc7b61c44f0bb7f11 Mon Sep 17 00:00:00 2001 From: MewMew Date: Wed, 18 Sep 2019 16:00:52 +0200 Subject: [PATCH 2/2] progress --- functions/noise_vector_path.lua | 16 +++- maps/island_troopers/main.lua | 84 ++++++++++++++++++- maps/island_troopers/terrain.lua | 140 +++++++++++++++++++++++++------ 3 files changed, 210 insertions(+), 30 deletions(-) diff --git a/functions/noise_vector_path.lua b/functions/noise_vector_path.lua index 39f57040..9a13e51a 100644 --- a/functions/noise_vector_path.lua +++ b/functions/noise_vector_path.lua @@ -52,18 +52,26 @@ function noise_vector_entity_path(surface, entity_name, position, base_vector, l return entities end -function noise_vector_tile_path(surface, tile_name, position, base_vector, length, brush_size) +function noise_vector_tile_path(surface, tile_name, position, base_vector, length, brush_size, whitelist) local seed_1 = math.random(1, 10000000) local seed_2 = math.random(1, 10000000) local vector = {} local tiles = {} - local minimal_movement = 0.75 + local minimal_movement = 0.65 local brush_vectors = get_brush(brush_size) for a = 1, length, 1 do - tiles[#tiles + 1] = {name = tile_name, position = position} for _, v in pairs(brush_vectors) do - surface.set_tiles({{name = tile_name, position = {position.x + v[1], position.y + v[2]}}}, true) + local p = {x = position.x + v[1], y = position.y + v[2]} + if whitelist then + if whitelist[surface.get_tile(p).name] then + surface.set_tiles({{name = tile_name, position = p}}, true) + tiles[#tiles + 1] = {name = tile_name, position = p} + end + else + surface.set_tiles({{name = tile_name, position = p}}, true) + tiles[#tiles + 1] = {name = tile_name, position = p} + end end local noise = simplex_noise(position.x * 0.1, position.y * 0.1, seed_1) diff --git a/maps/island_troopers/main.lua b/maps/island_troopers/main.lua index 72d70360..3985072a 100644 --- a/maps/island_troopers/main.lua +++ b/maps/island_troopers/main.lua @@ -1,5 +1,87 @@ require "functions.noise_vector_path" +require "functions.boss_unit" require "maps.island_troopers.terrain" +local function set_next_level() + global.alive_enemies = 0 + global.current_stage = 1 + global.current_level = global.current_level + 1 + + global.path_tiles = nil + global.stages = {} + global.stages[1] = { + path_length = 32 + global.current_level * 5, + size = math.random(global.current_level * 2, global.current_level * 5), + } + for i = 1, global.current_level, 1 do + global.stages[#global.stages + 1] = { + path_length = 24 + global.current_level * 5, + size = math.random(global.current_level * 2, global.current_level * 5), + } + end + global.stages[#global.stages + 1] = { + path_length = 128 + global.current_level * 5, + size = false, + } + + game.print("Level " .. global.current_level .. " has begun!") + + global.gamestate = 2 +end ---event.on_init(on_init) \ No newline at end of file +local function wait_until_stage_is_beaten() + if global.alive_enemies > 0 then return end + if global.stages[global.current_stage].size then + global.current_stage = global.current_stage + 1 + global.gamestate = 2 + return + end + + game.print("Level " .. global.current_level .. " complete!!") + global.gamestate = 5 +end + +local function on_player_joined_game(event) + local player = game.players[event.player_index] + player.insert({name = "pistol", count = 1}) + player.insert({name = "uranium-rounds-magazine", count = 128}) + player.insert({name = "firearm-magazine", count = 32}) +end + +local function on_init() + local surface = game.surfaces[1] + surface.request_to_generate_chunks({x = 0, y = 0}, 8) + surface.force_generate_chunk_requests() + + --global.level_tiles = {} + global.level_vectors = {} + global.current_level = 0 + global.gamestate = 1 + + game.forces.player.set_spawn_position({0, 2}, surface) +end + +local function on_entity_died(event) + if not event.entity.valid then return end + if event.entity.force.name == "enemy" then global.alive_enemies = global.alive_enemies - 1 end +end + +local gamestate_functions = { + [1] = set_next_level, + [2] = draw_path_to_next_stage, + [3] = draw_the_island, + [4] = wait_until_stage_is_beaten, + [5] = kill_the_level, +} + +local function on_tick() + if game.tick % 2 == 0 then + gamestate_functions[global.gamestate]() + end +end + +local event = require 'utils.event' +event.on_init(on_init) +event.add(defines.events.on_tick, on_tick) +event.add(defines.events.on_entity_died, on_entity_died) +event.add(defines.events.on_player_joined_game, on_player_joined_game) \ No newline at end of file diff --git a/maps/island_troopers/terrain.lua b/maps/island_troopers/terrain.lua index f6e6b636..a70110ea 100644 --- a/maps/island_troopers/terrain.lua +++ b/maps/island_troopers/terrain.lua @@ -1,24 +1,80 @@ local map_functions = require "tools.map_functions" +local math_random = math.random local function get_vector() - local v = {} - v[1] = (-100 + math.random(0, 200)) * 0.01 - v[2] = (-100 + math.random(0, 50)) * 0.01 - return v + if global.current_stage == 1 then return {0, -1} end + if global.current_stage == #global.stages then return {0, 1} end + return {1, 0} end -local function draw_island(surface, position, size) - map_functions.draw_noise_tile_circle(position, "grass-2", surface, size) +local function add_enemies(surface, position) + local radius = global.stages[global.current_stage].size + local amount = math.ceil(((global.current_level * 10) / #global.stages) * global.current_stage) + for a = 1, amount, 1 do + local p = {x = position.x + (radius - math.random(0, radius * 2)), y = position.y + (radius - math.random(0, radius * 2))} + if surface.can_place_entity({name = "small-biter", position = p, force = enemy}) then + surface.create_entity({name = "small-biter", position = p, force = enemy}) + global.alive_enemies = global.alive_enemies + 1 + end + end + + if global.current_stage == #global.stages - 1 then + local unit = surface.create_entity({name = "medium-spitter", position = position, force = enemy}) + add_boss_unit(unit, global.current_level * 2, 0.55) + global.alive_enemies = global.alive_enemies + 1 + end end -local function draw_path(surface, position, length) - local tiles = noise_vector_tile_path(surface, "grass-1", position, get_vector(), length, math.random(2, 5)) - return tiles +function draw_the_island() + if not global.stages[global.current_stage].size then global.gamestate = 4 return end + local surface = game.surfaces[1] + local position = global.path_tiles[#global.path_tiles].position + map_functions.draw_noise_tile_circle(position, "grass-2", surface, global.stages[global.current_stage].size) + add_enemies(surface, position) + global.gamestate = 4 end -function kill_level(surface) - for _, t in pairs(surface.find_tiles_filtered({area = {{-256, -256}, {256, -32}}, name = {"grass-1", "grass-2"}})) do - surface.set_tiles({{name = "water", position = t.position}}, true) +local draw_path_tile_whitelist = { + ["water"] = true, +} + +function draw_path_to_next_stage() + local surface = game.surfaces[1] + + if global.current_stage ~= #global.stages then + if global.current_stage == #global.stages - 1 then + game.print("--Final Stage--") + else + game.print("--Stage " .. global.current_stage .. "--") + end + end + + local position = {x = 0, y = 0} + if global.path_tiles then position = global.path_tiles[#global.path_tiles].position end + --game.print(get_vector()[1] .. " " .. get_vector()[2]) + global.path_tiles = noise_vector_tile_path(surface, "grass-1", position, get_vector(), global.stages[global.current_stage].path_length, math.random(2, 5), draw_path_tile_whitelist) + + --for _, t in pairs(global.path_tiles) do + -- global.level_tiles[#global.level_tiles + 1] = t + --end + + global.gamestate = 3 +end + +local tile_whitelist = {"grass-1", "grass-2", "grass-3", "grass-4"} +local function get_level_tiles(surface) + global.level_tiles = {} + for chunk in surface.get_chunks() do + if chunk.y < 0 and game.forces.player.is_chunk_charted(surface, chunk) then + for _, tile in pairs(surface.find_tiles_filtered({area = {{chunk.x * 32, chunk.y * 32}, {chunk.x * 32 + 32, chunk.y * 32 + 32}}, name = tile_whitelist})) do + local index = math.abs(tile.position.y) + if not global.level_tiles[index] then global.level_tiles[index] = {} end + global.level_tiles[index][#global.level_tiles[index] + 1] = tile + end + end + end + for k, tile_row in pairs(global.level_tiles) do + table.shuffle_table(global.level_tiles[k]) end end @@ -28,27 +84,61 @@ local function wipe_vision(surface) end end -function draw_level(surface) - kill_level(surface) - wipe_vision(surface) - local position = {x = 0, y = -32} - for a = 1, 3, 1 do - local size = math.random(20, 48) - local length = size * 3 - local t = draw_path(surface, position, length) - draw_island(surface, t[#t].position, size) - position = t[#t].position +local particles = {"coal-particle", "copper-ore-particle", "iron-ore-particle", "stone-particle"} +local function create_particles(surface, position) + local particle = particles[math_random(1, #particles)] + local m = math_random(10, 30) + local m2 = m * 0.005 + for i = 1, 25, 1 do + surface.create_entity({ + name = particle, + position = position, + frame_speed = 0.1, + vertical_speed = 0.1, + height = 0.1, + movement = {m2 - (math_random(0, m) * 0.01), m2 - (math_random(0, m) * 0.01)} + }) end end +function kill_the_level() + local surface = game.surfaces[1] + if not global.level_tiles then get_level_tiles(surface) end + + local amount = global.current_level + for i = #global.level_tiles, 1, -1 do + if global.level_tiles[i] then + for k, tile in pairs(global.level_tiles[i]) do + surface.set_tiles({{name = "water", position = tile.position}}, true) + create_particles(surface, tile.position) + global.level_tiles[i][k] = nil + amount = amount - 1 + if amount <= 0 then return end + end + global.level_tiles[i] = nil + end + end + + if #global.level_tiles == 0 then + wipe_vision(surface) + global.level_tiles = nil + global.gamestate = 1 + end +end + +local function process_tile(surface, position) + if position.y < 0 then surface.set_tiles({{name = "water", position = position}}, true) return end + if position.y > 4 then surface.set_tiles({{name = "water-green", position = position}}, true) return end + surface.set_tiles({{name = "sand-1", position = position}}, true) +end + local function on_chunk_generated(event) local left_top = event.area.left_top local surface = event.surface for x = 0, 31, 1 do for y = 0, 31, 1 do - local p = {x = left_top.x + x, y = left_top.y + y} - if left_top.y < -32 then surface.set_tiles({{name = "water", position = p}}, true) end - if left_top.y > 32 then surface.set_tiles({{name = "water-green", position = p}}, true) end + local position = {x = left_top.x + x, y = left_top.y + y} + process_tile(surface, position) end end end