From 1f0ca98420991420599ab4b714d36e53da62b865 Mon Sep 17 00:00:00 2001 From: MewMew Date: Mon, 7 Oct 2019 07:19:41 +0200 Subject: [PATCH] basic terrain --- maps/mountain_fortress/locomotive.lua | 2 +- maps/mountain_fortress/main.lua | 19 ++++++ maps/mountain_fortress/terrain.lua | 90 ++++++++++++++++++++++----- 3 files changed, 94 insertions(+), 17 deletions(-) diff --git a/maps/mountain_fortress/locomotive.lua b/maps/mountain_fortress/locomotive.lua index 8905b73d..8f2dcaaa 100644 --- a/maps/mountain_fortress/locomotive.lua +++ b/maps/mountain_fortress/locomotive.lua @@ -3,7 +3,7 @@ function locomotive_spawn(surface, position) surface.create_entity({name = "straight-rail", position = {position.x, position.y + y}, force = "player", direction = 0}) end global.locomotive = surface.create_entity({name = "locomotive", position = {position.x, position.y + -3}, force = "player"}) - global.locomotive.get_inventory(defines.inventory.fuel).insert({name = "coal", count = 50}) + global.locomotive.get_inventory(defines.inventory.fuel).insert({name = "wood", count = 100}) global.locomotive_cargo = surface.create_entity({name = "cargo-wagon", position = {position.x, position.y + 3}, force = "player"}) global.locomotive_cargo.get_inventory(defines.inventory.cargo_wagon).insert({name = "raw-fish", count = 4000}) diff --git a/maps/mountain_fortress/main.lua b/maps/mountain_fortress/main.lua index b4e94667..7fda3786 100644 --- a/maps/mountain_fortress/main.lua +++ b/maps/mountain_fortress/main.lua @@ -31,12 +31,28 @@ local function on_entity_died(event) end end +local function on_entity_damaged(event) + if not event.entity.valid then return end + if event.entity.force.index ~= 1 then return end + if event.entity == global.locomotive or event.entity == global.locomotive_cargo then + if event.cause then + if event.cause.force.index == 2 then + return + end + end + event.entity.health = event.entity.health + event.final_damage_amount + end +end + local function on_player_joined_game(event) local surface = game.surfaces["mountain_fortress"] local player = game.players[event.player_index] if player.online_time == 0 then player.teleport(surface.find_non_colliding_position("character", game.forces.player.get_spawn_position(surface), 3, 0.5), surface) + player.insert({name = 'pistol', count = 1}) + player.insert({name = 'firearm-magazine', count = 16}) + player.insert({name = 'rail', count = 16}) end end @@ -59,6 +75,8 @@ local function on_init(surface) game.map_settings.enemy_expansion.settler_group_min_size = 16 game.map_settings.pollution.enabled = false + game.forces.player.technologies["steel-axe"].researched = true + game.forces.player.technologies["railway"].researched = true game.forces.player.set_spawn_position({-2, 16}, surface) locomotive_spawn(surface, {x = 0, y = 16}) @@ -69,5 +87,6 @@ end local event = require 'utils.event' event.on_init(on_init) +event.add(defines.events.on_entity_damaged, on_entity_damaged) 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/mountain_fortress/terrain.lua b/maps/mountain_fortress/terrain.lua index 4dc5293e..843e8d26 100644 --- a/maps/mountain_fortress/terrain.lua +++ b/maps/mountain_fortress/terrain.lua @@ -1,25 +1,82 @@ local math_random = math.random +local simplex_noise = require "utils.simplex_noise".d2 local rock_raffle = {"sand-rock-big","sand-rock-big","rock-big","rock-big","rock-big","rock-big","rock-big","rock-big","rock-huge"} local spawner_raffle = {"biter-spawner", "biter-spawner", "biter-spawner", "spitter-spawner"} +local noises = { + ["large_caves"] = {{modifier = 0.0033, weight = 1}, {modifier = 0.01, weight = 0.22}, {modifier = 0.05, weight = 0.05}, {modifier = 0.1, weight = 0.04}}, + ["small_caves"] = {{modifier = 0.008, weight = 1}, {modifier = 0.03, weight = 0.15}, {modifier = 0.25, weight = 0.05}}, +} +local caves_start = -360 -local function process_position(surface, p) - local distance_to_center = math.sqrt(p.x^2 + p.y^2) - local index = math.floor((distance_to_center / 16) % 18) + 1 - --if index == 7 then surface.create_entity({name = "rock-big", position = p}) return end - if index % 2 == 1 then - if math.random(1, 3) == 1 then - surface.create_entity({name = "rock-big", position = p}) - else - surface.create_entity({name = "tree-0" .. math.ceil(index * 0.5), position = p}) - end - return +local function get_noise(name, pos, seed) + local noise = 0 + local d = 0 + for _, n in pairs(noises[name]) do + noise = noise + simplex_noise(pos.x * n.modifier, pos.y * n.modifier, seed) * n.weight + d = d + n.weight + seed = seed + 10000 end + noise = noise / d + return noise +end + +function get_cave_density_modifer(y) + if y < caves_start then y = y - 2048 end + local m = 1 + ((y) * 0.000175) + if m < 0.10 then m = 0.10 end + return m +end + +local function process_rock_chunk_position(p, seed, tiles, entities) + local m = get_cave_density_modifer(p.y) + local noise = get_noise("large_caves", p, seed) + if noise > m * -1 and noise < m then + tiles[#tiles + 1] = {name = "dirt-7", position = p} + if math_random(1,3) > 1 then entities[#entities + 1] = {name = rock_raffle[math_random(1, #rock_raffle)], position = p} end + return + end + + if math.abs(noise) > m * 7 then + tiles[#tiles + 1] = {name = "water", position = p} + return + end + if math.abs(noise) > m * 6.5 then + if math_random(1,16) == 1 then entities[#entities + 1] = {name="tree-02", position=p} end + end + if math.abs(noise) > m * 5 then + tiles[#tiles + 1] = {name = "grass-2", position = p} + if math_random(1,128) == 1 then entities[#entities + 1] = {name=spawner_raffle[math_random(1, #spawner_raffle)], position=p} end + return + end + + local noise = get_noise("small_caves", p, seed) + if noise > (m + 0.05) * -1 and noise < m - 0.05 then + tiles[#tiles + 1] = {name = "dirt-6", position = p} + if math_random(1,5) > 1 then entities[#entities + 1] = {name = rock_raffle[math_random(1, #rock_raffle)], position = p} end + return + end + + tiles[#tiles + 1] = {name = "out-of-map", position = p} end local function rock_chunk(surface, left_top) - for x = 0, 31, 1 do - for y = 0, 31, 1 do - surface.set_tiles({{name = "dirt-7", position = {x = left_top.x + x, y = left_top.y + y}}}) + local tiles = {} + local entities = {} + local seed = game.surfaces[1].map_gen_settings.seed + for y = 0, 31, 1 do + for x = 0, 31, 1 do + local p = {x = left_top.x + x, y = left_top.y + y} + process_rock_chunk_position(p, seed, tiles, entities) + end + end + surface.set_tiles(tiles, true) + for _, e in pairs(entities) do + if game.entity_prototypes[e.name].type == "simple-entity" then + surface.create_entity(e) + else + if surface.can_place_entity(e) then + surface.create_entity(e) + end end end end @@ -80,7 +137,7 @@ local function biter_chunk(surface, left_top) for x = 0, 31, 1 do for y = 0, 31, 1 do local p = {x = left_top.x + x, y = left_top.y + y} - surface.set_tiles({{name = "sand-3", position = p}}) + surface.set_tiles({{name = "dirt-3", position = p}}) tile_positions[#tile_positions + 1] = p end end @@ -102,6 +159,7 @@ end local function process_chunk(left_top) local surface = game.surfaces["mountain_fortress"] + game.forces.player.chart(surface, {{left_top.x, left_top.y},{left_top.x + 31, left_top.y + 31}}) if left_top.y == 96 and left_top.x == 96 then local p = global.locomotive.position for _, entity in pairs(surface.find_entities_filtered({area = {{p.x - 3, p.y - 4},{p.x + 3, p.y + 8}}, force = "neutral"})) do entity.destroy() end @@ -132,5 +190,5 @@ local function on_chunk_generated(event) end local event = require 'utils.event' -event.on_nth_tick(60, process_chunk_queue) +event.on_nth_tick(1, process_chunk_queue) event.add(defines.events.on_chunk_generated, on_chunk_generated) \ No newline at end of file