Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added tile_picker feature for better dirt generation in diggy #894

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions map_gen/maps/diggy/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,17 @@ local Config = {
['behemoth-spitter'] = 7,
},
},
-- perlin noise for dirt generation
tile_picker = {
-- uses perlin noise when true, random when false
enabled = true,

-- tiles to pick from
tiles = {'dirt-1', 'dirt-2', 'dirt-3', 'dirt-4', 'dirt-5', 'dirt-6', 'dirt-7'},

-- value between 0 and 1, higher values means smaller areas with same tile
noise_variance = 0.03
}
},
}

Expand Down
10 changes: 7 additions & 3 deletions map_gen/maps/diggy/feature/diggy_hole.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ local ScoreTable = require 'map_gen.maps.diggy.score_table'
local Command = require 'utils.command'
local CreateParticles = require 'features.create_particles'
local Ranks = require 'resources.ranks'
local TilePicker = require 'map_gen.maps.diggy.feature.tile_picker'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you disable the feature, what happens?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It generates terrain like it used to if not enabled then return 'dirt-' .. random(1, 7) end


local random = math.random
local tonumber = tonumber
Expand Down Expand Up @@ -89,7 +90,8 @@ local function diggy_hole(entity)

for i = #out_of_map_found, 1, -1 do
local void_position = out_of_map_found[i]
tiles[i] = {name = 'dirt-' .. random(1, 7), position = void_position}
local tile_name = TilePicker.get_tile(void_position.x, void_position.y)
tiles[i] = {name = tile_name, position = void_position}
local predicted = random()
if predicted < 0.2 then
rocks[i] = {name = 'rock-huge', position = void_position}
Expand Down Expand Up @@ -120,7 +122,8 @@ local function on_mined_tile(surface, tiles)
for _, tile in pairs(tiles) do
if (artificial_tiles[tile.old_tile.name]) then
count = count + 1
new_tiles[count] = {name = 'dirt-' .. random(1, 7), position = tile.position}
local tile_name = TilePicker.get_tile(tile.position.x, tile.position.y)
new_tiles[count] = {name = tile_name, position = tile.position}
end
end

Expand All @@ -141,7 +144,8 @@ Command.add('diggy-clear-void', {
for x = 0, width do
for y = 0, height do
count = count + 1
tiles[count] = {name = 'dirt-' .. random(1, 7), position = {x = x + left_top_x, y = y + left_top_y}}
local tile_name = TilePicker.get_tile(x + left_top_x, y + left_top_y)
tiles[count] = {name = tile_name, position = {x = x + left_top_x, y = y + left_top_y}}
end
end

Expand Down
9 changes: 7 additions & 2 deletions map_gen/maps/diggy/feature/starting_zone.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ local Template = require 'map_gen.maps.diggy.template'
local Retailer = require 'features.retailer'
local DiggyCaveCollapse = require 'map_gen.maps.diggy.feature.diggy_cave_collapse'
local RS = require 'map_gen.shared.redmew_surface'
local TilePicker = require 'map_gen.maps.diggy.feature.tile_picker'

local insert = table.insert
local random = math.random
local sqrt = math.sqrt
local floor = math.floor
local pairs = pairs
Expand Down Expand Up @@ -51,13 +51,18 @@ function StartingZone.register(config)
local rock_range = starting_zone_size - 2
local stress_hack = floor(starting_zone_size * 0.1)

-- init tile picker
local seed = surface.map_gen_settings.seed + surface.index + 300
TilePicker.set_seed(seed)

for x = -starting_zone_size, starting_zone_size do
for y = -starting_zone_size, starting_zone_size do
local distance = floor(sqrt(x * x + y * y))

if (distance < starting_zone_size) then
if (distance > dirt_range) then
insert(tiles, {name = 'dirt-' .. random(1, 7), position = {x = x, y = y}})
local tile_name = TilePicker.get_tile(x, y)
insert(tiles, {name = tile_name, position = {x = x, y = y}})
else
insert(tiles, {name = 'stone-path', position = {x = x, y = y}})
end
Expand Down
78 changes: 78 additions & 0 deletions map_gen/maps/diggy/feature/tile_picker.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
--[[-- info
Provides nicer tile picking
]]

-- dependencies
local Event = require 'utils.event'
local perlin_noise = require 'map_gen.shared.perlin_noise'.noise

-- this
local Picker = {}

local floor = math.floor
local random = math.random

local enabled = false
local replacement_tiles
local tile_count
local noise_variance
local seed

-- should be called once before calling get_tile
function Picker.set_seed(new_seed)
seed = new_seed
end

-- returns tile generated by perlin noise from coordinates
function Picker.get_tile(x, y)
if not enabled then
return 'dirt-' .. random(1, 7)
end

-- value in range [-1, 1]
local noise
if seed then
noise = perlin_noise(x * noise_variance, y * noise_variance, seed)
else
noise = perlin_noise(x * noise_variance, y * noise_variance)
end

-- value in range [0, 1]
local corrected_noise = (noise + 1) / 2

-- value in range [1, tile_count]
local tile_number = floor(corrected_noise * tile_count) + 1

-- just in case perlin_noise() returns '1', not even sure if it can
if tile_number > tile_count then
tile_number = tile_count
end

return replacement_tiles[tile_number]
end

function Picker.register(config)
enabled = true
replacement_tiles = config.tiles or {'dirt-1', 'dirt-2', 'dirt-3', 'dirt-4', 'dirt-5', 'dirt-6', 'dirt-7'}
tile_count = #replacement_tiles
noise_variance = config.noise_variance or 0.03

-- add landfill event
Event.add(defines.events.on_player_built_tile, function(event)
local item = event.item
if not item or item.name ~= 'landfill' then
return
end

local tiles = event.tiles
for i = 1, #tiles do
local tile_name = Picker.get_tile(tiles[i].position.x, tiles[i].position.y)
tiles[i].name = tile_name
end

local surface = game.surfaces[event.surface_index]
surface.set_tiles(tiles)
end)
end

return Picker
7 changes: 5 additions & 2 deletions map_gen/maps/diggy/scenario.lua
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,11 @@ function Scenario.register()
end
)

local landfill_tiles = {'dirt-1','dirt-2','dirt-3','dirt-4','dirt-5','dirt-6','dirt-7'}
require ('map_gen.shared.change_landfill_tile')(landfill_tiles)
-- tile_picker adds its own landfill event
if not Config.features.tile_picker.enabled then
local landfill_tiles = {'dirt-1','dirt-2','dirt-3','dirt-4','dirt-5','dirt-6','dirt-7'}
require ('map_gen.shared.change_landfill_tile')(landfill_tiles)
end

ScenarioInfo.set_map_name('Diggy')
ScenarioInfo.set_map_description('Dig your way through!')
Expand Down