From 8dbf4e0920cbdc8ebed7dea6fd31ab6bea08da7a Mon Sep 17 00:00:00 2001 From: Lynn Date: Sun, 7 Oct 2018 13:50:25 +0200 Subject: [PATCH] grilledham+valansch changes without initialize --- control.lua | 2 +- map_gen/Diggy/Config.lua | 6 - map_gen/Diggy/Debug.lua | 13 +- map_gen/Diggy/Feature/AlienSpawner.lua | 9 - map_gen/Diggy/Feature/DiggyCaveCollapse.lua | 652 +++++++++++++----- map_gen/Diggy/Feature/DiggyHole.lua | 9 - map_gen/Diggy/Feature/MarketExchange.lua | 39 +- map_gen/Diggy/Feature/MiningEfficiency.lua | 45 +- map_gen/Diggy/Feature/RefreshMap.lua | 9 - map_gen/Diggy/Feature/ScatteredResources.lua | 7 - map_gen/Diggy/Feature/SetupPlayer.lua | 9 - map_gen/Diggy/Feature/SimpleRoomGenerator.lua | 8 - map_gen/Diggy/Feature/StartingZone.lua | 61 +- map_gen/Diggy/Scenario.lua | 67 +- map_gen/Diggy/Template.lua | 70 +- map_layout.lua | 2 +- 16 files changed, 609 insertions(+), 399 deletions(-) diff --git a/control.lua b/control.lua index 98a1e3f6..01d9884d 100644 --- a/control.lua +++ b/control.lua @@ -14,7 +14,7 @@ require 'follow' require 'autodeconstruct' require 'corpse_util' --require 'infinite_storage_chest' -require 'fish_market' +--require 'fish_market' require 'reactor_meltdown' require 'train_saviour' require 'map_gen.shared.perlin_noise' diff --git a/map_gen/Diggy/Config.lua b/map_gen/Diggy/Config.lua index e23820e0..31cc9774 100644 --- a/map_gen/Diggy/Config.lua +++ b/map_gen/Diggy/Config.lua @@ -84,12 +84,10 @@ local Config = { RefreshMap = { enabled = true, register = require 'map_gen.Diggy.Feature.RefreshMap'.register, - initialize = require 'map_gen.Diggy.Feature.RefreshMap'.initialize, }, SimpleRoomGenerator = { enabled = true, register = require 'map_gen.Diggy.Feature.SimpleRoomGenerator'.register, - initialize = require 'map_gen.Diggy.Feature.SimpleRoomGenerator'.initialize, -- value between 0 and 1, higher value means stronger variance between coordinates noise_variance = 0.089, @@ -109,7 +107,6 @@ local Config = { ScatteredResources = { enabled = true, register = require 'map_gen.Diggy.Feature.ScatteredResources'.register, - initialize = require 'map_gen.Diggy.Feature.ScatteredResources'.initialize, -- percentage of resource added to the sum. 100 tiles means -- 10% more resources with a distance_richness_modifier of 10 @@ -172,7 +169,6 @@ local Config = { AlienSpawner = { enabled = true, register = require 'map_gen.Diggy.Feature.AlienSpawner'.register, - initialize = require 'map_gen.Diggy.Feature.AlienSpawner'.initialize, -- minimum distance from spawn before aliens can spawn alien_minimum_distance = 35, @@ -183,7 +179,6 @@ local Config = { MiningEfficiency = { enabled = true, register = require 'map_gen.Diggy.Feature.MiningEfficiency'.register, - initialize = require 'map_gen.Diggy.Feature.MiningEfficiency'.initialize, -- percentage * mining productivity level gets added to mining speed mining_speed_productivity_multiplier = 15, @@ -194,7 +189,6 @@ local Config = { MarketExchange = { enabled = true, register = require 'map_gen.Diggy.Feature.MarketExchange'.register, - initialize = require 'map_gen.Diggy.Feature.MarketExchange'.initialize, stone_to_surface_amount = 50, currency_item = 'stone', diff --git a/map_gen/Diggy/Debug.lua b/map_gen/Diggy/Debug.lua index 95234218..66066113 100644 --- a/map_gen/Diggy/Debug.lua +++ b/map_gen/Diggy/Debug.lua @@ -23,7 +23,7 @@ function Debug.disable_cheats() cheats = true end -local message_count = 0 +global.message_count = 0 --[[-- Shows the given message if _DEBUG == true. @@ -32,10 +32,11 @@ local message_count = 0 ]] function Debug.print(message) if type(message) ~= 'string' and type(message) ~= 'number' and type(message) ~= 'boolean' then message = type(message) end - message_count = message_count + 1 + global.message_count = global.message_count + 1 if (debug) then - game.print('[' .. message_count .. '] ' .. tostring(message)) - log('[' .. message_count .. '] ' .. tostring(message)) + game.print('[' .. global.message_count .. '] ' .. tostring(message)) + log('[' .. global.message_count .. '] ' .. tostring(message)) + game.write_file("desync", '[' .. global.message_count .. '] ' .. tostring(message) .. '\n', true) end end @@ -49,9 +50,9 @@ end function Debug.printPosition(position, message) message = message or '' if type(message) ~= 'string' and type(message) ~= 'number' and type(message) ~= 'boolean' then message = type(message) end - message_count = message_count + 1 + global.message_count = global.message_count + 1 if (debug) then - game.print('[' .. message_count .. '] {x=' .. position.x .. ', y=' .. position.y .. '} ' .. tostring(message)) + game.print('[' .. global.message_count .. '] {x=' .. position.x .. ', y=' .. position.y .. '} ' .. tostring(message)) end end diff --git a/map_gen/Diggy/Feature/AlienSpawner.lua b/map_gen/Diggy/Feature/AlienSpawner.lua index f8d7a52c..c2fd65a2 100644 --- a/map_gen/Diggy/Feature/AlienSpawner.lua +++ b/map_gen/Diggy/Feature/AlienSpawner.lua @@ -48,13 +48,4 @@ function AlienSpawner.register(cfg) end) end ---[[-- - Initializes the Feature. - - @param config Table {@see Diggy.Config}. -]] -function AlienSpawner.initialize(config) - -end - return AlienSpawner diff --git a/map_gen/Diggy/Feature/DiggyCaveCollapse.lua b/map_gen/Diggy/Feature/DiggyCaveCollapse.lua index c6f5fb16..bf0e3359 100644 --- a/map_gen/Diggy/Feature/DiggyCaveCollapse.lua +++ b/map_gen/Diggy/Feature/DiggyCaveCollapse.lua @@ -1,15 +1,12 @@ --[[-- info Provides the ability to collapse caves when digging. ]] - -- dependencies require 'utils.list_utils' local Event = require 'utils.event' local Template = require 'map_gen.Diggy.Template' -local Mask = require 'map_gen.Diggy.Mask' -local StressMap = require 'map_gen.Diggy.StressMap' -local Debug = require'map_gen.Diggy.Debug' +local Debug = require 'map_gen.Diggy.Debug' local Task = require 'utils.Task' local Token = require 'utils.global_token' @@ -18,6 +15,28 @@ local DiggyCaveCollapse = {} local config = {} +local n = 9 +local radius = 0 +local radius_sq = 0 +local center_radius_sq = 0 +local disc_radius_sq = 0 + +local center_weight +local disc_weight +local ring_weight + +local disc_blur_sum = 0 + +local center_value = 0 +local disc_value = 0 +local ring_value = 0 + +local enable_stress_grid = 0 +local stress_map_blur_add = nil +local mask_disc_blur = nil +local stress_map_check_stress_in_threshold = nil +local support_beam_entities = nil + DiggyCaveCollapse.events = { --[[-- When stress at certain position is above the collapse threshold @@ -30,64 +49,76 @@ DiggyCaveCollapse.events = { local function create_collapse_template(positions, surface) local entities = {} local tiles = {} + local map = {} for _, position in pairs(positions) do - table.insert(entities, {position = {x = position.x, y = position.y - 1}, name = 'sand-rock-big'}) - table.insert(entities, {position = {x = position.x + 1, y = position.y}, name = 'sand-rock-big'}) - table.insert(entities, {position = {x = position.x, y = position.y + 1}, name = 'sand-rock-big'}) - table.insert(entities, {position = {x = position.x - 1, y = position.y}, name = 'sand-rock-big'}) + map[position.x] = map[position.x] or {} + map[position.x][position.y] = map[position.x][position.y] or true table.insert(tiles, {position = {x = position.x, y = position.y}, name = 'out-of-map'}) end - for _, new_spawn in pairs({entities, tiles}) do - for _, tile in pairs(new_spawn) do - for _, entity in pairs(surface.find_entities_filtered({position = tile.position})) do - pcall(function() entity.die() end) - pcall(function() entity.destroy() end) + + for x, y_tbl in pairs(map) do + for y, _ in pairs(y_tbl) do + if not map[x] or not map[x][y - 1] then + table.insert(entities, {position = {x = x, y = y - 1}, name = 'sand-rock-big'}) + end + if not map[x] or not map[x][y + 1] then + table.insert(entities, {position = {x = x, y = y + 1}, name = 'sand-rock-big'}) + end + if not map[x - 1] or not map[x - 1][y] then + table.insert(entities, {position = {x = x - 1, y = y}, name = 'sand-rock-big'}) + end + if not map[x + 1] or not map[x + 1][y] then + table.insert(entities, {position = {x = x + 1, y = y}, name = 'sand-rock-big'}) end end end - for key,entity in pairs(entities) do - if not entity.valid then - entities[key] = nil + + for _, new_spawn in pairs({entities, tiles}) do + for _, tile in pairs(new_spawn) do + for _, entity in pairs(surface.find_entities_filtered({position = tile.position})) do + pcall( + function() + entity.die() + end + ) + pcall( + function() + entity.destroy() + end + ) + end end end + return tiles, entities end ---[[-- - @param surface LuaSurface - @param position Position with x and y - @param strength positive increases stress, negative decreases stress -]] -local function update_stress_map(surface, position, strength) - local max_value - Mask.disc_blur(position.x, position.y, strength, function (x, y, fraction) - max_value = max_value or StressMap.add(surface, {x = x, y = y}, fraction) - end) - - if max_value then - script.raise_event(DiggyCaveCollapse.events.on_collapse_triggered, {surface = surface, position = position}) - end -end - -local function collapse(surface, position) +local function collapse(args, surface, position) + local position = args.position + local surface = args.surface local positions = {} - - Mask.disc_blur(position.x, position.y, config.collapse_threshold_total_strength, function(x, y, value) - StressMap.check_stress_in_threshold(surface, {x = x, y = y}, value, function(_, position) - table.insert(positions, position) - end) - - end) + mask_disc_blur( + position.x, + position.y, + config.collapse_threshold_total_strength, + function(x, y, value) + stress_map_check_stress_in_threshold( + surface, + {x = x, y = y}, + value, + function(_, position) + table.insert(positions, position) + end + ) + end + ) local tiles, entities = create_collapse_template(positions, surface) Template.insert(surface, tiles, entities) end -local on_collapse_timeout_finished = Token.register(function(params) - collapse(params.surface, params.position) -end) +local on_collapse_timeout_finished = Token.register(collapse) - -function spawn_cracking_sound_text(surface, position) +local function spawn_cracking_sound_text(surface, position) local text = config.cracking_sounds[math.random(1, #config.cracking_sounds)] local color = { @@ -97,14 +128,98 @@ function spawn_cracking_sound_text(surface, position) } for i = 1, #text do - local x_offset = (i - #text / 2 - 1) / 3 - local char = text:sub(i, i) - surface.create_entity{ - name = 'flying-text', - color = color, - text = char , - position = {x = position.x + x_offset, y = position.y - ((i + 1) % 2) / 4} - }.active = true + local x_offset = (i - #text / 2 - 1) / 3 + local char = text:sub(i, i) + surface.create_entity { + name = 'flying-text', + color = color, + text = char, + position = {x = position.x + x_offset, y = position.y - ((i + 1) % 2) / 4} + }.active = true + end +end + +local function on_collapse_triggered(event) + spawn_cracking_sound_text(event.surface, event.position) + Task.set_timeout( + math.random(config.collapse_delay_min * 10, config.collapse_delay_max * 10) / 10, + on_collapse_timeout_finished, + {surface = event.surface, position = event.position} + ) +end + +local function on_built_tile(event) + local strength = support_beam_entities[event.item.name] + + if strength then + local surface = game.surfaces[event.surface_index] + for _, tile in pairs(event.tiles) do + stress_map_blur_add(surface, tile.position, -1 * strength, 'on_built_tile') + end + end +end + +local function on_robot_mined_tile(event) + for _, tile in pairs(event.tiles) do + local strength = support_beam_entities[tile.old_tile.name] + if strength then + stress_map_blur_add(event.robot.surface, tile.position, strength, 'on_robot_mined_tile') + end + end +end + +local function on_player_mined_tile(event) + local surface = game.surfaces[event.surface_index] + for _, tile in pairs(event.tiles) do + local strength = support_beam_entities[tile.old_tile.name] + + if strength then + stress_map_blur_add(surface, tile.position, strength, 'on_player_mined_tile') + end + end +end + +local function on_mined_entity(event) + local strength = support_beam_entities[event.entity.name] + + if strength then + stress_map_blur_add(event.entity.surface, event.entity.position, strength, 'on_mined_entity') + end +end + +local function on_built_entity(event) + local strength = support_beam_entities[event.created_entity.name] + + if strength then + stress_map_blur_add( + event.created_entity.surface, + event.created_entity.position, + -1 * strength, + 'on_built_entity' + ) + end +end + +local function on_placed_entity(event) + local strength = support_beam_entities[event.entity.name] + + if strength then + stress_map_blur_add(event.entity.surface, event.entity.position, -1 * strength, 'on_placed_entity') + end +end + +local function on_void_removed(event) + local strength = support_beam_entities['out-of-map'] + + if strength then + stress_map_blur_add(event.surface, event.old_tile.position, strength, 'on_void_removed') + end +end + +local function on_void_added(event) + local strength = support_beam_entities['out-of-map'] + if strength then + stress_map_blur_add(event.surface, event.old_tile.position, -1 * strength, 'on_void_added') end end @@ -115,156 +230,323 @@ end ]] function DiggyCaveCollapse.register(global_config) config = global_config.features.DiggyCaveCollapse - local support_beam_entities = config.support_beam_entities; + support_beam_entities = config.support_beam_entities - if (config.enable_stress_grid) then - Event.add(StressMap.events.on_stress_changed, function(event) - Debug.print_grid_value(event.value, event.surface, event.position) - end) + Event.add(DiggyCaveCollapse.events.on_collapse_triggered, on_collapse_triggered) + Event.add(defines.events.on_robot_built_entity, on_built_entity) + Event.add(defines.events.on_robot_built_tile, on_built_tile) + Event.add(defines.events.on_player_built_tile, on_built_tile) + Event.add(defines.events.on_robot_mined_tile, on_robot_mined_tile) + Event.add(defines.events.on_player_mined_tile, on_player_mined_tile) + Event.add(defines.events.on_robot_mined_entity, on_mined_entity) + Event.add(defines.events.on_built_entity, on_built_entity) + Event.add(Template.events.on_placed_entity, on_placed_entity) + Event.add(defines.events.on_entity_died, on_mined_entity) + Event.add(defines.events.on_player_mined_entity, on_mined_entity) + Event.add(Template.events.on_void_removed, on_void_removed) + Event.add(Template.events.on_void_added, on_void_added) + + enable_stress_grid = config.enable_stress_grid + + mask_init(config) + if (config.enable_mask_debug) then + local surface = game.surfaces.nauvis + mask_disc_blur( + 0, + 0, + 10, + function(x, y, fraction) + Debug.print_grid_value(fraction, surface, {x = x, y = y}) + end + ) + end +end + +-- +--STRESS MAP +-- +local stress_threshold_causing_collapse = 0.9 + +-- main block +global.stress_map_storage = {} + +local defaultValue = 0 + +--[[-- + Adds a fraction to a given location on the stress_map. Returns the new + fraction value of that position. + + @param stress_map Table of {@see get_stress_map} + @param position Table with x and y + @param number fraction + + @return number sum of old fraction + new fraction +]] +local function add_fraction(stress_map, x, y, fraction) + local quadrant = 1 + if x < 0 then + quadrant = quadrant + 1 + x = -x + end + if y < 0 then + quadrant = quadrant + 2 + y = -y end - Event.add(DiggyCaveCollapse.events.on_collapse_triggered, function(event) - spawn_cracking_sound_text(event.surface, event.position) + local quad_t = stress_map[quadrant] - Task.set_timeout( - math.random(config.collapse_delay_min * 10, config.collapse_delay_max * 10) / 10, - on_collapse_timeout_finished, - {surface = event.surface, position = event.position} + local x_t = quad_t[x] + if not x_t then + x_t = {} + quad_t[x] = x_t + end + + local value = x_t[y] + if not value then + value = defaultValue + end + + value = value + fraction + + x_t[y] = value + + if (value > stress_threshold_causing_collapse) then + if quadrant > 2 then + y = -y + end + if quadrant % 2 == 0 then + x = -x + end + script.raise_event( + DiggyCaveCollapse.events.on_collapse_triggered, + {surface = game.surfaces[stress_map.surface_index], position = {x = x, y = y}} ) - end) - - Event.add(defines.events.on_robot_built_entity, function(event) - local strength = support_beam_entities[event.created_entity.name] - - if (not strength) then - return + end + if (enable_stress_grid) then + local surface = game.surfaces[stress_map.surface_index] + if quadrant > 2 then + y = -y end - - update_stress_map(event.created_entity.surface, event.created_entity.position, -1 * strength) - end) - - Event.add(defines.events.on_robot_built_tile, function(event) - local strength = support_beam_entities[event.item.name] - - if (not strength) then - return + if quadrant % 2 == 0 then + x = -x end + Debug.print_grid_value(value, surface, {x = x, y = y}) + end + return value +end - for _, tile in pairs(event.tiles) do - update_stress_map(event.robot.surface, tile.position, -1 * strength) +local function add_fraction_by_quadrant(stress_map, x, y, fraction, quadrant) + local quad_t = stress_map[quadrant] + + local x_t = quad_t[x] + if not x_t then + x_t = {} + quad_t[x] = x_t + end + + local value = x_t[y] + if not value then + value = defaultValue + end + + value = value + fraction + + x_t[y] = value + + if (value > stress_threshold_causing_collapse) then + if quadrant > 2 then + y = -y end - end) - - Event.add(defines.events.on_player_built_tile, function(event) - local strength = support_beam_entities[event.item.name] - - if (not strength) then - return + if quadrant % 2 == 0 then + x = -x end - local surface = game.surfaces[event.surface_index] - for _, tile in pairs(event.tiles) do - update_stress_map(surface, tile.position, -1 * strength) + script.raise_event( + DiggyCaveCollapse.events.on_collapse_triggered, + {surface = game.surfaces[stress_map.surface_index], position = {x = x, y = y}} + ) + end + if (enable_stress_grid) then + local surface = game.surfaces[stress_map.surface_index] + if quadrant > 2 then + y = -y end - end) - - Event.add(defines.events.on_robot_mined_tile, function(event) - for _, tile in pairs(event.tiles) do - local strength = support_beam_entities[tile.old_tile.name] - - if (strength) then - update_stress_map(event.robot.surface, tile.position, strength) - end + if quadrant % 2 == 0 then + x = -x end - end) - - Event.add(defines.events.on_player_mined_tile, function(event) - local surface = game.surfaces[event.surface_index] - for _, tile in pairs(event.tiles) do - local strength = support_beam_entities[tile.old_tile.name] - - if (strength) then - update_stress_map(surface, tile.position, strength) - end - end - end) - - Event.add(defines.events.on_robot_mined_entity, function(event) - local strength = support_beam_entities[event.entity.name] - - if (not strength) then - return - end - - update_stress_map(event.entity.surface, event.entity.position, strength) - end) - - Event.add(defines.events.on_built_entity, function(event) - local strength = support_beam_entities[event.created_entity.name] - - if (not strength) then - return - end - - update_stress_map(event.created_entity.surface, event.created_entity.position, -1 * strength) - end) - - Event.add(Template.events.on_placed_entity, function(event) - local strength = support_beam_entities[event.entity.name] - - if (not strength) then - return - end - - update_stress_map(event.entity.surface, event.entity.position, -1 * strength) - end) - - Event.add(defines.events.on_entity_died, function(event) - local strength = support_beam_entities[event.entity.name] - - if (not strength) then - return - end - - update_stress_map(event.entity.surface, event.entity.position, strength) - end) - - Event.add(defines.events.on_player_mined_entity, function(event) - local strength = support_beam_entities[event.entity.name] - - if (not strength) then - return - end - - update_stress_map(event.entity.surface, event.entity.position, strength) - end) - - Event.add(Template.events.on_void_removed, function(event) - local strength = support_beam_entities['out-of-map'] - - update_stress_map(event.surface, event.old_tile.position, strength) - end) - - Event.add(Template.events.on_void_added, function(event) - local strength = support_beam_entities['out-of-map'] - - update_stress_map(event.surface, event.old_tile.position, -1 * strength) - end) + Debug.print_grid_value(value, surface, {x = x, y = y}) + end + return value end --[[-- - Initializes the Feature. + Creates a new stress map if it doesn't exist yet and returns it. - @param config Table {@see Diggy.Config}. + @param surface LuaSurface + @return Table [1,2,3,4] containing the quadrants ]] -function DiggyCaveCollapse.initialize(global_config) - config = global_config.features.DiggyCaveCollapse +local function get_stress_map(surface) + if not global.stress_map_storage[surface.index] then + global.stress_map_storage[surface.index] = {} - Mask.init(config) - if (config.enable_mask_debug) then - local surface = game.surfaces.nauvis - Mask.disc_blur(0, 0, 10, function (x, y, fraction) - Debug.print_grid_value(fraction, surface, {x=x, y=y}) - end) + local map = global.stress_map_storage[surface.index] + + map['surface_index'] = surface.index + map[1] = {} + map[2] = {} + map[3] = {} + map[4] = {} + end + + return global.stress_map_storage[surface.index] +end + +--[[-- + Checks whether a tile's pressure is within a given threshold and calls the handler if not. + @param surface LuaSurface + @param position Position with x and y + @param number threshold + @param callback + +]] +stress_map_check_stress_in_threshold = function(surface, position, threshold, callback) + local stress_map = get_stress_map(surface) + local value = add_fraction(stress_map, position.x, position.y, 0) + + if (value >= stress_threshold_causing_collapse - threshold) then + callback(surface, position) + end +end + +stress_map_blur_add = function(surface, position, factor, caller) + if global.disable_cave_collapse then + return + end + + caller = caller or 'unknown' + + --Debug.print(caller .. ' before') + local x_start = math.floor(position.x) + local y_start = math.floor(position.y) + + local stress_map = get_stress_map(game.surfaces[1]) + + if radius > math.abs(x_start) or radius > math.abs(y_start) then + for x = -radius, radius do + for y = -radius, radius do + local value = 0 + local distance_sq = x * x + y * y + if distance_sq <= center_radius_sq then + value = center_value + elseif distance_sq <= disc_radius_sq then + value = disc_value + elseif distance_sq <= radius_sq then + value = ring_value + end + if math.abs(value) > 0.001 then + add_fraction(stress_map, x + x_start, y + y_start, value * factor) + end + end + end + else + --ALL VALUES IN SAME QUADRANT SO WE CAN OPTIMIZE THIS! + local quadrant = 1 + if x_start < 0 then + quadrant = quadrant + 1 + x_start = -x_start + end + if y_start < 0 then + quadrant = quadrant + 2 + y_start = -y_start + end + + for x = -radius, radius do + for y = -radius, radius do + local value = 0 + local distance_sq = x * x + y * y + if distance_sq <= center_radius_sq then + value = center_value + elseif distance_sq <= disc_radius_sq then + value = disc_value + elseif distance_sq <= radius_sq then + value = ring_value + end + if math.abs(value) > 0.001 then + add_fraction_by_quadrant(stress_map, x + x_start, y + y_start, value * factor, quadrant) + end + end + end + end + --Debug.print(caller .. ' after') +end + +DiggyCaveCollapse.stress_map_blur_add = stress_map_blur_add + +-- +-- MASK +-- + +function mask_init(config) + n = config.mask_size + + ring_weight = config.mask_relative_ring_weights[1] + disc_weight = config.mask_relative_ring_weights[2] + center_weight = config.mask_relative_ring_weights[3] + + radius = math.floor(n / 2) + + radius_sq = (radius + 0.2) * (radius + 0.2) + center_radius_sq = radius_sq / 9 + disc_radius_sq = radius_sq * 4 / 9 + + for x = -radius, radius do + for y = -radius, radius do + local distance_sq = x * x + y * y + if distance_sq <= center_radius_sq then + disc_blur_sum = disc_blur_sum + center_weight + elseif distance_sq <= disc_radius_sq then + disc_blur_sum = disc_blur_sum + disc_weight + elseif distance_sq <= radius_sq then + disc_blur_sum = disc_blur_sum + ring_weight + end + end + end + center_value = center_weight / disc_blur_sum + disc_value = disc_weight / disc_blur_sum + ring_value = ring_weight / disc_blur_sum +end + +--[[-- + Applies a blur + Applies the disc in 3 discs: center, (middle) disc and (outer) ring. + The relative weights for tiles in a disc are: + center: 3/3 + disc: 2/3 + ring: 1/3 + The sum of all values is 1 + + @param x_start number center point + @param y_start number center point + @param factor the factor to multiply the cell value with (value = cell_value * factor) + @param callback function to execute on each tile within the mask callback(x, y, value) +]] +mask_disc_blur = function(x_start, y_start, factor, callback) + x_start = math.floor(x_start) + y_start = math.floor(y_start) + for x = -radius, radius do + for y = -radius, radius do + local value = 0 + local distance_sq = x * x + y * y + if distance_sq <= center_radius_sq then + value = center_value + elseif distance_sq <= disc_radius_sq then + value = disc_value + elseif distance_sq <= radius_sq then + value = ring_value + end + if math.abs(value) > 0.001 then + callback(x_start + x, y_start + y, value * factor) + end + end end end diff --git a/map_gen/Diggy/Feature/DiggyHole.lua b/map_gen/Diggy/Feature/DiggyHole.lua index 111a43b8..3a87caca 100644 --- a/map_gen/Diggy/Feature/DiggyHole.lua +++ b/map_gen/Diggy/Feature/DiggyHole.lua @@ -92,13 +92,4 @@ function DiggyHole.register(cfg) end) end ---[[-- - Initializes the Feature. - - @param config Table {@see Diggy.Config}. -]] -function DiggyHole.initialize(config) - -end - return DiggyHole diff --git a/map_gen/Diggy/Feature/MarketExchange.lua b/map_gen/Diggy/Feature/MarketExchange.lua index 3bd0a9c4..012d96b5 100644 --- a/map_gen/Diggy/Feature/MarketExchange.lua +++ b/map_gen/Diggy/Feature/MarketExchange.lua @@ -21,22 +21,7 @@ global.MarketExchange = { ]] function MarketExchange.register(cfg) local config = cfg.features.MarketExchange - Event.add(defines.events.on_market_item_purchased, function (event) - if (1 ~= event.offer_index) then - return - end - global.MarketExchange.stone_sent_to_surface = global.MarketExchange.stone_sent_to_surface + config.stone_to_surface_amount - end) -end - ---[[-- - Initializes the Feature. - - @param cfg Table {@see Diggy.Config}. -]] -function MarketExchange.initialize(cfg) - local config = cfg.features.MarketExchange local market_items = { {price = {{config.currency_item, 50}}, offer = {type = 'nothing', effect_description = 'Send ' .. config.stone_to_surface_amount .. ' stone to the surface'}}, } @@ -52,16 +37,24 @@ function MarketExchange.initialize(cfg) end local on_market_timeout_finished = Token.register(function(params) - Template.market(params.surface, params.position, params.force, params.currency_item, params.market_items) + Template.market(game.surfaces.nauvis, {x = 0, y = -5}, game.forces.player, params.currency_item, params.market_items) end) - Task.set_timeout_in_ticks(40, on_market_timeout_finished, { - surface = game.surfaces.nauvis, - position = {x = 0, y = -5}, - force = game.forces.player, - currency_item = config.currency_item, - market_items = market_items, - }) + Event.on_init(function() + Task.set_timeout_in_ticks(360, on_market_timeout_finished, { + currency_item = config.currency_item, + market_items = market_items, + }) + end) + + + Event.add(defines.events.on_market_item_purchased, function (event) + if (1 ~= event.offer_index) then + return + end + + global.MarketExchange.stone_sent_to_surface = global.MarketExchange.stone_sent_to_surface + config.stone_to_surface_amount + end) end return MarketExchange diff --git a/map_gen/Diggy/Feature/MiningEfficiency.lua b/map_gen/Diggy/Feature/MiningEfficiency.lua index d40f2ddf..7904bcba 100644 --- a/map_gen/Diggy/Feature/MiningEfficiency.lua +++ b/map_gen/Diggy/Feature/MiningEfficiency.lua @@ -17,16 +17,19 @@ global.MiningEfficiency = { local function update_mining_speed(player_force) -- recalculate current modifier - global.MiningEfficiency.current_modifier = global.MiningEfficiency.default_mining_speed + global.MiningEfficiency.research_modifier + global.MiningEfficiency.current_modifier = + global.MiningEfficiency.default_mining_speed + global.MiningEfficiency.research_modifier -- remove the current buff - player_force.manual_mining_speed_modifier = player_force.manual_mining_speed_modifier - global.MiningEfficiency.active_modifier + player_force.manual_mining_speed_modifier = + player_force.manual_mining_speed_modifier - global.MiningEfficiency.active_modifier -- update the active modifier global.MiningEfficiency.active_modifier = global.MiningEfficiency.current_modifier -- add the new active modifier - player_force.manual_mining_speed_modifier = player_force.manual_mining_speed_modifier + global.MiningEfficiency.active_modifier + player_force.manual_mining_speed_modifier = + player_force.manual_mining_speed_modifier + global.MiningEfficiency.active_modifier end --[[-- @@ -35,25 +38,25 @@ end function MiningEfficiency.register(cfg) local config = cfg.features.MiningEfficiency - Event.add(defines.events.on_research_finished, function(event) - local player_force = game.forces.player - - global.MiningEfficiency.research_modifier = player_force.mining_drill_productivity_bonus * config.mining_speed_productivity_multiplier / 2 - - update_mining_speed(player_force) - end) -end - ---[[-- - Initializes the Feature. - - @param config Table {@see Diggy.Config}. -]] -function MiningEfficiency.initialize(cfg) - local config = cfg.features.MiningEfficiency - global.MiningEfficiency.default_mining_speed = config.default_mining_speed - update_mining_speed(game.forces.player) + + Event.add( + defines.events.on_research_finished, + function(event) + local player_force = game.forces.player + + global.MiningEfficiency.research_modifier = + player_force.mining_drill_productivity_bonus * config.mining_speed_productivity_multiplier / 2 + + update_mining_speed(player_force) + end + ) end +Event.on_init( + function() + update_mining_speed(game.forces.player) + end +) + return MiningEfficiency diff --git a/map_gen/Diggy/Feature/RefreshMap.lua b/map_gen/Diggy/Feature/RefreshMap.lua index 5d750ba3..809d32ed 100644 --- a/map_gen/Diggy/Feature/RefreshMap.lua +++ b/map_gen/Diggy/Feature/RefreshMap.lua @@ -36,13 +36,4 @@ function RefreshMap.register(config) end) end ---[[-- - Initializes the Feature. - - @param config Table {@see Diggy.Config}. -]] -function RefreshMap.initialize(config) - -end - return RefreshMap diff --git a/map_gen/Diggy/Feature/ScatteredResources.lua b/map_gen/Diggy/Feature/ScatteredResources.lua index d03cf113..7d5d1ef1 100644 --- a/map_gen/Diggy/Feature/ScatteredResources.lua +++ b/map_gen/Diggy/Feature/ScatteredResources.lua @@ -91,14 +91,7 @@ function ScatteredResources.register(cfg) spawn_resource(config, event.surface, x, y, distance) end end) -end ---[[-- - Initializes the Feature. - - @param config Table {@see Diggy.Config}. -]] -function ScatteredResources.initialize(config) global.ScatteredResources.can_spawn_resources = true end diff --git a/map_gen/Diggy/Feature/SetupPlayer.lua b/map_gen/Diggy/Feature/SetupPlayer.lua index e01bfac3..f2d09a88 100644 --- a/map_gen/Diggy/Feature/SetupPlayer.lua +++ b/map_gen/Diggy/Feature/SetupPlayer.lua @@ -28,13 +28,4 @@ function SetupPlayer.register(config) end) end ---[[-- - Initializes the Feature. - - @param config Table {@see Diggy.Config}. -]] -function SetupPlayer.initialize(config) - -end - return SetupPlayer diff --git a/map_gen/Diggy/Feature/SimpleRoomGenerator.lua b/map_gen/Diggy/Feature/SimpleRoomGenerator.lua index a3cad0b3..9f746d71 100644 --- a/map_gen/Diggy/Feature/SimpleRoomGenerator.lua +++ b/map_gen/Diggy/Feature/SimpleRoomGenerator.lua @@ -90,12 +90,4 @@ function SimpleRoomGenerator.register(cfg) end end ---[[-- - Initializes the Feature. - - @param config Table {@see Diggy.Config}. -]] -function SimpleRoomGenerator.initialize(config) -end - return SimpleRoomGenerator diff --git a/map_gen/Diggy/Feature/StartingZone.lua b/map_gen/Diggy/Feature/StartingZone.lua index 7db47d8d..b4b31d1a 100644 --- a/map_gen/Diggy/Feature/StartingZone.lua +++ b/map_gen/Diggy/Feature/StartingZone.lua @@ -1,18 +1,18 @@ --[[-- info Provides the ability to create a pre-configured starting zone. ]] - -- dependencies local Event = require 'utils.event' local Token = require 'utils.global_token' -local Mask = require 'map_gen.Diggy.Mask' -local StressMap = require 'map_gen.Diggy.StressMap' local Template = require 'map_gen.Diggy.Template' local Debug = require 'map_gen.Diggy.Debug' +local DiggyCaveCollapse = require 'map_gen.Diggy.Feature.DiggyCaveCollapse' -- this local StartingZone = {} +local daytime + --[[-- Registers all event handlers. ]] @@ -24,7 +24,7 @@ function StartingZone.register(config) local start_point_area = {{-1, -1}, {0, 0}} -- hack to figure out whether the important chunks are generated via Diggy.Feature.RefreshMap. - if (4 ~= event.surface.count_tiles_filtered({start_point_area, name='lab-dark-1'})) then + if (4 ~= event.surface.count_tiles_filtered({start_point_area, name = 'lab-dark-1'})) then return end @@ -36,23 +36,28 @@ function StartingZone.register(config) local tiles = {} local rocks = {} - Mask.circle(0, 0, starting_zone_size, function(x, y, tile_distance_to_center) - if (tile_distance_to_center > math.floor(starting_zone_size / 2)) then - table.insert(tiles, {name = 'dirt-' .. math.random(1, 7), position = {x = x, y = y}}) - else - table.insert(tiles, {name = 'stone-path', position = {x = x, y = y}}) - end + for x = -starting_zone_size, starting_zone_size do + for y = -starting_zone_size, starting_zone_size do + local distance = math.floor(math.sqrt(x * x + y * y)) - if (tile_distance_to_center > starting_zone_size - 2) then - table.insert(rocks, {name = 'sand-rock-big', position = {x = x, y = y}}) - end + if (distance < starting_zone_size) then + if (distance > math.floor(starting_zone_size / 2)) then + table.insert(tiles, {name = 'dirt-' .. math.random(1, 7), position = {x = x, y = y}}) + else + table.insert(tiles, {name = 'stone-path', position = {x = x, y = y}}) + end - if (tile_distance_to_center > math.floor(starting_zone_size / 10)) then - Mask.blur(x, y, -0.3, function (x, y, fraction) - StressMap.add(event.surface, {x = x, y = y}, fraction) - end) + if (distance > starting_zone_size - 2) then + table.insert(rocks, {name = 'sand-rock-big', position = {x = x, y = y}}) + end + + -- hack to avoid starting area from collapsing + if (distance > math.floor(starting_zone_size / 10)) then + DiggyCaveCollapse.stress_map_blur_add(event.surface, {x = x, y = y}, -0.3) + end + end end - end) + end Template.insert(event.surface, tiles, rocks) @@ -62,19 +67,17 @@ function StartingZone.register(config) callback_token = Token.register(on_chunk_generated) Event.add_removable(defines.events.on_chunk_generated, callback_token) + + daytime = config.features.StartingZone.daytime end ---[[-- - Initializes the Feature. +Event.on_init( + function() + local surface = game.surfaces.nauvis - @param config Table {@see Diggy.Config}. -]] -function StartingZone.initialize(config) - local surface = game.surfaces.nauvis - - surface.daytime = config.features.StartingZone.daytime - surface.freeze_daytime = 1 - -end + surface.daytime = daytime + surface.freeze_daytime = 1 + end +) return StartingZone diff --git a/map_gen/Diggy/Scenario.lua b/map_gen/Diggy/Scenario.lua index ea9bf3ac..8a0a73fe 100644 --- a/map_gen/Diggy/Scenario.lua +++ b/map_gen/Diggy/Scenario.lua @@ -1,5 +1,4 @@ -- dependencies -local Event = require 'utils.event' local Config = require 'map_gen.Diggy.Config' local Debug = require 'map_gen.Diggy.Debug' @@ -11,8 +10,6 @@ local Scenario = {} -- private state local scenario_registered = false -local scenario_initialized = false - --[[-- Allows calling a callback for each enabled feature. @@ -47,53 +44,29 @@ function Scenario.register(debug) return end - -- using the on_player_created to initialize all the features - Event.add(defines.events.on_player_created, function (event) - if ('boolean' == type(debug)) then - Config.Debug = debug + if ('boolean' == type(debug)) then + Config.Debug = debug + end + + if (Config.debug) then + Debug.enable_debug() + end + + if (Config.cheats) then + Debug.enable_cheats() + end + + each_enabled_feature( + function(feature_name, feature_data) + if ('function' ~= type(feature_data.register)) then + error('Feature ' .. feature_name .. ' did not define a register function.') + end + + feature_data.register(Config) end - - if (Config.debug) then - Debug.enable_debug() - end - - if (Config.cheats) then - Debug.enable_cheats() - end - - Scenario.initialize(Config) - end) - - each_enabled_feature(function(feature_name, feature_data) - if ('function' ~= type(feature_data.register)) then - error('Feature ' .. feature_name .. ' did not define a register function.') - end - - feature_data.register(Config) - end) + ) scenario_registered = true end ---[[-- - Initializes the starting position. - - @param config Table {@see Diggy.Config}. -]] -function Scenario.initialize(config) - if scenario_initialized then - return - end - - each_enabled_feature(function(feature_name, feature_data) - if ('function' ~= type(feature_data.initialize)) then - error('Feature ' .. feature_name .. ' did not define an initialize function.') - end - - feature_data.initialize(config) - end) - - scenario_initialized = true -end - return Scenario diff --git a/map_gen/Diggy/Template.lua b/map_gen/Diggy/Template.lua index 4b141e50..895af2ee 100644 --- a/map_gen/Diggy/Template.lua +++ b/map_gen/Diggy/Template.lua @@ -7,7 +7,7 @@ local Debug = require 'map_gen.Diggy.Debug' local Template = {} local tiles_per_call = 5 --how many tiles are inserted with each call of insert_action -local entities_per_call = 5 --how many entities are inserted with each call of insert_action +local entities_per_call = 5 --how many entities are inserted with each call of insert_action Template.events = { --[[-- @@ -37,23 +37,32 @@ local function insert_next_tiles(data) local surface = data.surface local tiles = {} - pcall(function() --use pcall to assure tile_iterator is always incremented, to avoid endless loops - for i = data.tile_iterator, math.min(data.tile_iterator + tiles_per_call - 1, data.tiles_n) do - local new_tile = data.tiles[i] - table.insert(tiles, new_tile) - local current_tile = surface.get_tile(new_tile.position.x, new_tile.position.y) - local current_is_void = current_tile.name == 'out-of-map' - local new_is_void = new_tile.name == 'out-of-map' + pcall( + function() + --use pcall to assure tile_iterator is always incremented, to avoid endless loops + for i = data.tile_iterator, math.min(data.tile_iterator + tiles_per_call - 1, data.tiles_n) do + local new_tile = data.tiles[i] + table.insert(tiles, new_tile) + local current_tile = surface.get_tile(new_tile.position.x, new_tile.position.y) + local current_is_void = current_tile.name == 'out-of-map' + local new_is_void = new_tile.name == 'out-of-map' - if (current_is_void and not new_is_void) then - table.insert(void_removed, {surface = surface, old_tile = {name = current_tile.name, position = current_tile.position}}) - end + if (current_is_void and not new_is_void) then + table.insert( + void_removed, + {surface = surface, old_tile = {name = current_tile.name, position = current_tile.position}} + ) + end - if (new_is_void and not current_is_void) then - table.insert(void_added, {surface = surface, old_tile = {name = current_tile.name, position = current_tile.position}}) + if (new_is_void and not current_is_void) then + table.insert( + void_added, + {surface = surface, old_tile = {name = current_tile.name, position = current_tile.position}} + ) + end end end - end) + ) data.tile_iterator = data.tile_iterator + tiles_per_call @@ -72,17 +81,20 @@ local function insert_next_entities(data) local created_entities = {} local surface = data.surface - pcall(function() --use pcall to assure tile_iterator is always incremented, to avoid endless loops - for i = data.entity_iterator, math.min(data.entity_iterator + entities_per_call - 1, data.entities_n) do - local entity = data.entities[i] - created_entity = surface.create_entity(entity) - if (nil == created_entity) then - error('Failed creating entity ' .. entity.name .. ' on surface.') - end + pcall( + function() + --use pcall to assure tile_iterator is always incremented, to avoid endless loops + for i = data.entity_iterator, math.min(data.entity_iterator + entities_per_call - 1, data.entities_n) do + local entity = data.entities[i] + local created_entity = surface.create_entity(entity) + if (nil == created_entity) then + error('Failed creating entity ' .. entity.name .. ' on surface.') + end - table.insert(created_entities, created_entity) + table.insert(created_entities, created_entity) + end end - end) + ) data.entity_iterator = data.entity_iterator + entities_per_call @@ -133,11 +145,11 @@ function Template.insert(surface, tiles, entities) } local continue = true - for i=1,4 do - continue = insert_action(data) - if not continue then - return - end + for i = 1, 4 do + continue = insert_action(data) + if not continue then + return + end end if continue then Task.queue_task(insert_token, data, total_calls - 4) @@ -162,7 +174,7 @@ function Template.units(surface, units, non_colliding_distance) entity.position = position surface.create_entity(entity) else - Debug.print('Failed to spawn \'' .. entity.name .. '\' at \'' .. serpent.line(entity.position) .. '\'') + Debug.print("Failed to spawn '" .. entity.name .. "' at '" .. serpent.line(entity.position) .. "'") end end end diff --git a/map_layout.lua b/map_layout.lua index 77c99d34..4c4e9304 100644 --- a/map_layout.lua +++ b/map_layout.lua @@ -20,7 +20,7 @@ local tiles_per_tick = 32 --require "map_gen.combined.dagobah_swamp" --require "map_gen.combined.meteor_strike" --unfinished --require 'map_gen.combined.cave_miner.cave_miner' ---require "map_gen.combined.diggy" +require "map_gen.combined.diggy" --presets--