2019-10-07 04:37:23 +02:00
|
|
|
local math_random = math.random
|
2019-10-07 07:19:41 +02:00
|
|
|
local simplex_noise = require "utils.simplex_noise".d2
|
2019-10-07 04:37:23 +02:00
|
|
|
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"}
|
2019-10-07 07:19:41 +02:00
|
|
|
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}},
|
2019-10-07 09:48:17 +02:00
|
|
|
["cave_ponds"] = {{modifier = 0.01, weight = 1}, {modifier = 0.1, weight = 0.06}},
|
2019-10-07 07:19:41 +02:00
|
|
|
}
|
|
|
|
local caves_start = -360
|
2019-10-07 04:37:23 +02:00
|
|
|
|
2019-10-07 07:19:41 +02:00
|
|
|
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
|
2019-10-06 18:16:32 +02:00
|
|
|
end
|
2019-10-07 07:19:41 +02:00
|
|
|
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
|
|
|
|
|
2019-10-07 09:48:17 +02:00
|
|
|
local function process_rock_chunk_position(p, seed, tiles, entities, markets, treasure)
|
2019-10-07 07:19:41 +02:00
|
|
|
local m = get_cave_density_modifer(p.y)
|
2019-10-08 05:50:32 +02:00
|
|
|
|
|
|
|
local noise_large_caves = get_noise("large_caves", p, seed)
|
|
|
|
local noise_cave_ponds = get_noise("cave_ponds", p, seed)
|
|
|
|
local small_caves = get_noise("small_caves", p, seed)
|
|
|
|
|
|
|
|
if noise_cave_ponds < 0.08 and noise_cave_ponds > -0.08 then
|
|
|
|
if small_caves > 0.45 then
|
|
|
|
tiles[#tiles + 1] = {name = "out-of-map", position = p}
|
|
|
|
return
|
|
|
|
end
|
|
|
|
if small_caves < -0.45 then
|
|
|
|
tiles[#tiles + 1] = {name = "out-of-map", position = p}
|
|
|
|
return
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if noise_large_caves > m * -1 and noise_large_caves < m then
|
2019-10-07 09:48:17 +02:00
|
|
|
if noise_cave_ponds > 0.80 then
|
|
|
|
tiles[#tiles + 1] = {name = "deepwater-green", position = p}
|
|
|
|
if math_random(1,16) == 1 then entities[#entities + 1] = {name="fish", position=p} end
|
|
|
|
return
|
|
|
|
end
|
|
|
|
if noise_cave_ponds < -0.80 then
|
|
|
|
tiles[#tiles + 1] = {name = "grass-2", position = p}
|
|
|
|
if math_random(1,64) == 1 then markets[#markets + 1] = p end
|
|
|
|
if math_random(1,96) == 1 then entities[#entities + 1] = {name = "crude-oil", position = p, amount = math.abs(p.y) * 250} end
|
|
|
|
return
|
|
|
|
end
|
2019-10-07 22:40:05 +02:00
|
|
|
|
2019-10-07 07:19:41 +02:00
|
|
|
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
|
2019-10-07 09:48:17 +02:00
|
|
|
if math_random(1,2048) == 1 then treasure[#treasure + 1] = p end
|
2019-10-07 07:19:41 +02:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2019-10-08 05:50:32 +02:00
|
|
|
if math.abs(noise_large_caves) > m * 7 then
|
2019-10-07 07:19:41 +02:00
|
|
|
tiles[#tiles + 1] = {name = "water", position = p}
|
2019-10-07 09:48:17 +02:00
|
|
|
if math_random(1,16) == 1 then entities[#entities + 1] = {name="fish", position=p} end
|
2019-10-07 07:19:41 +02:00
|
|
|
return
|
|
|
|
end
|
2019-10-08 05:50:32 +02:00
|
|
|
if math.abs(noise_large_caves) > m * 6.5 then
|
2019-10-07 07:19:41 +02:00
|
|
|
if math_random(1,16) == 1 then entities[#entities + 1] = {name="tree-02", position=p} end
|
2019-10-07 09:48:17 +02:00
|
|
|
if math_random(1,128) == 1 then markets[#markets + 1] = p end
|
2019-10-07 07:19:41 +02:00
|
|
|
end
|
2019-10-08 05:50:32 +02:00
|
|
|
if math.abs(noise_large_caves) > m * 5 then
|
2019-10-07 07:19:41 +02:00
|
|
|
tiles[#tiles + 1] = {name = "grass-2", position = p}
|
2019-10-07 22:40:05 +02:00
|
|
|
--if math_random(1,128) == 1 then entities[#entities + 1] = {name=spawner_raffle[math_random(1, #spawner_raffle)], position=p} end
|
2019-10-07 07:19:41 +02:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2019-10-08 05:50:32 +02:00
|
|
|
if small_caves > (m + 0.05) * -1 and small_caves < m - 0.05 then
|
2019-10-07 07:19:41 +02:00
|
|
|
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
|
2019-10-07 09:48:17 +02:00
|
|
|
if math_random(1, 512) == 1 then treasure[#treasure + 1] = p end
|
2019-10-07 07:19:41 +02:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
tiles[#tiles + 1] = {name = "out-of-map", position = p}
|
2019-10-06 18:16:32 +02:00
|
|
|
end
|
|
|
|
|
2019-10-07 04:37:23 +02:00
|
|
|
local function rock_chunk(surface, left_top)
|
2019-10-07 07:19:41 +02:00
|
|
|
local tiles = {}
|
|
|
|
local entities = {}
|
2019-10-07 09:48:17 +02:00
|
|
|
local markets = {}
|
|
|
|
local treasure = {}
|
2019-10-07 07:19:41 +02:00
|
|
|
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}
|
2019-10-07 09:48:17 +02:00
|
|
|
process_rock_chunk_position(p, seed, tiles, entities, markets, treasure)
|
2019-10-07 07:19:41 +02:00
|
|
|
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
|
2019-10-07 04:37:23 +02:00
|
|
|
end
|
|
|
|
end
|
2019-10-08 05:50:32 +02:00
|
|
|
--if #markets > 0 then secret_shop(markets[math_random(1, #markets)], surface) end
|
|
|
|
if #markets > 0 then
|
|
|
|
local position = markets[math_random(1, #markets)]
|
|
|
|
if surface.count_entities_filtered{area = {{position.x - 128, position.y - 128}, {position.x + 128, position.y + 128}}, name = "market", limit = 1} == 0 then
|
|
|
|
local market = random_type_market(surface, position, math.abs(position.y) * 0.004)
|
|
|
|
end
|
|
|
|
end
|
2019-10-07 09:48:17 +02:00
|
|
|
for _, p in pairs(treasure) do treasure_chest(surface, p) end
|
2019-10-07 04:37:23 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
local function border_chunk(surface, left_top)
|
|
|
|
local trees = {"dead-grey-trunk", "dead-grey-trunk", "dry-tree"}
|
|
|
|
for x = 0, 31, 1 do
|
|
|
|
for y = 5, 31, 1 do
|
|
|
|
local pos = {x = left_top.x + x, y = left_top.y + y}
|
|
|
|
if math_random(1, math.ceil(pos.y + pos.y) + 64) == 1 then
|
|
|
|
surface.create_entity({name = trees[math_random(1, #trees)], position = pos})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
for x = 0, 31, 1 do
|
|
|
|
for y = 0, 31, 1 do
|
|
|
|
local pos = {x = left_top.x + x, y = left_top.y + y}
|
|
|
|
if math_random(1, pos.y + 2) == 1 then
|
|
|
|
surface.create_decoratives{
|
|
|
|
check_collision=false,
|
|
|
|
decoratives={
|
|
|
|
{name = "rock-medium", position = pos, amount = math_random(1, 1 + math.ceil(20 - y / 2))}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
if math_random(1, pos.y + 2) == 1 then
|
|
|
|
surface.create_decoratives{
|
|
|
|
check_collision=false,
|
|
|
|
decoratives={
|
|
|
|
{name = "rock-small", position = pos, amount = math_random(1, 1 + math.ceil(20 - y / 2))}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
if math_random(1, pos.y + 2) == 1 then
|
|
|
|
surface.create_decoratives{
|
|
|
|
check_collision=false,
|
|
|
|
decoratives={
|
|
|
|
{name = "rock-tiny", position = pos, amount = math_random(1, 1 + math.ceil(20 - y / 2))}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
if math_random(1, math.ceil(pos.y + pos.y) + 2) == 1 then
|
|
|
|
surface.create_entity({name = rock_raffle[math_random(1, #rock_raffle)], position = pos})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2019-10-07 09:48:17 +02:00
|
|
|
|
|
|
|
for _, e in pairs(surface.find_entities_filtered({area = {{left_top.x, left_top.y},{left_top.x + 32, left_top.y + 32}}, type = "cliff"})) do e.destroy() end
|
2019-10-07 04:37:23 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
local function biter_chunk(surface, left_top)
|
|
|
|
local tile_positions = {}
|
|
|
|
for x = 0, 31, 1 do
|
|
|
|
for y = 0, 31, 1 do
|
|
|
|
local p = {x = left_top.x + x, y = left_top.y + y}
|
|
|
|
tile_positions[#tile_positions + 1] = p
|
|
|
|
end
|
|
|
|
end
|
|
|
|
for i = 1, 4, 1 do
|
|
|
|
local position = surface.find_non_colliding_position("biter-spawner", tile_positions[math_random(1, #tile_positions)], 16, 2)
|
|
|
|
if position then
|
|
|
|
surface.create_entity({name = spawner_raffle[math_random(1, #spawner_raffle)], position = position})
|
|
|
|
end
|
|
|
|
end
|
2019-10-07 09:48:17 +02:00
|
|
|
for _, e in pairs(surface.find_entities_filtered({area = {{left_top.x, left_top.y},{left_top.x + 32, left_top.y + 32}}, type = "cliff"})) do e.destroy() end
|
2019-10-07 04:37:23 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
local function out_of_map(surface, left_top)
|
|
|
|
for x = 0, 31, 1 do
|
|
|
|
for y = 0, 31, 1 do
|
|
|
|
surface.set_tiles({{name = "out-of-map", position = {x = left_top.x + x, y = left_top.y + y}}})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-10-07 16:40:52 +02:00
|
|
|
local function process_chunk(surface, left_top)
|
2019-10-07 09:48:17 +02:00
|
|
|
--game.forces.player.chart(surface, {{left_top.x, left_top.y},{left_top.x + 31, left_top.y + 31}})
|
2019-10-07 16:40:52 +02:00
|
|
|
if left_top.y == 64 and left_top.x == 64 then
|
2019-10-07 04:37:23 +02:00
|
|
|
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
|
|
|
|
end
|
|
|
|
if left_top.y < 0 then rock_chunk(surface, left_top) return end
|
|
|
|
if left_top.y > 128 then out_of_map(surface, left_top) return end
|
|
|
|
if left_top.y > 64 then biter_chunk(surface, left_top) return end
|
|
|
|
if left_top.y >= 0 then border_chunk(surface, left_top) return end
|
|
|
|
end
|
|
|
|
|
|
|
|
local function process_chunk_queue()
|
2019-10-07 16:40:52 +02:00
|
|
|
for k, chunk in pairs(global.chunk_queue) do
|
|
|
|
if chunk.surface then
|
|
|
|
if chunk.surface.valid then
|
|
|
|
process_chunk(chunk.surface, chunk.left_top)
|
|
|
|
end
|
|
|
|
end
|
2019-10-07 04:37:23 +02:00
|
|
|
global.chunk_queue[k] = nil
|
|
|
|
return
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-10-06 18:16:32 +02:00
|
|
|
local function on_chunk_generated(event)
|
2019-10-07 16:40:52 +02:00
|
|
|
if event.surface.index == 1 then return end
|
|
|
|
global.chunk_queue[#global.chunk_queue + 1] = {left_top = event.area.left_top, surface = event.surface}
|
2019-10-06 18:16:32 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
local event = require 'utils.event'
|
2019-10-07 22:40:05 +02:00
|
|
|
event.on_nth_tick(1, process_chunk_queue)
|
2019-10-06 18:16:32 +02:00
|
|
|
event.add(defines.events.on_chunk_generated, on_chunk_generated)
|