1
0
mirror of https://github.com/Refactorio/RedMew.git synced 2024-12-20 19:52:58 +02:00
RedMew/map_gen/combined/meteor_strike.lua

172 lines
4.4 KiB
Lua
Raw Normal View History

2018-05-23 23:34:47 +02:00
local perlin = require 'map_gen.shared.perlin_noise'
local Task = require 'utils.Task'
local Event = require 'utils.event'
2018-01-21 17:30:42 +02:00
local block_size = 1 -- in tiles
local start_size = 64 -- in blocks
local strike_time = 1 -- in ticks
-- dud blocks don't spawn meteors, with a block_weight = 1 and dud weight = 3, every 3 out of 4 blocks will be a dud block
local block_weight = 1
2018-05-23 23:34:47 +02:00
local dud_weight = 0
2018-01-21 17:30:42 +02:00
local min_blocks_in_list = 10 -- no dud meteors if the number of blocks in the list is less than or equal to this
global.blocks = nil
global.used_blocks = nil
global.strike_time = strike_time
global.weight_count = 0
local half_start_size = (start_size * block_size) / 2
local total_weight = block_weight + dud_weight
local function init_blocks()
local blocks = {}
local used_blocks = {}
2018-05-23 23:34:47 +02:00
local half = start_size / 2
2018-01-21 17:30:42 +02:00
for i = -half, half - 1 do
2018-05-23 23:34:47 +02:00
table.insert(blocks, {x = i, y = -half - 1})
used_blocks[i .. ',' .. (-half - 1)] = true
table.insert(blocks, {x = i, y = half})
used_blocks[i .. ',' .. half] = true
table.insert(blocks, {x = -half - 1, y = i})
used_blocks[(-half - 1) .. ',' .. i] = true
table.insert(blocks, {x = half, y = i})
used_blocks[half .. ',' .. i] = true
for j = -half, half - 1 do
used_blocks[i .. ',' .. j] = true
end
2018-01-21 17:30:42 +02:00
end
global.blocks = blocks
global.used_blocks = used_blocks
end
2018-05-23 23:34:47 +02:00
local function get_resource(x, y)
2018-06-12 17:57:39 +02:00
local value = perlin.noise(x / 16, y / 16)
2018-01-21 17:30:42 +02:00
value = value + 1
value = value * 500
2018-05-23 23:34:47 +02:00
local name = ''
2018-01-21 17:30:42 +02:00
if value < 450 then
2018-05-23 23:34:47 +02:00
return nil
2018-01-21 17:30:42 +02:00
elseif value < 550 then
2018-05-23 23:34:47 +02:00
name = 'iron-ore'
2018-01-21 17:30:42 +02:00
elseif value < 650 then
2018-05-23 23:34:47 +02:00
name = 'copper-ore'
2018-01-21 17:30:42 +02:00
elseif value < 750 then
2018-05-23 23:34:47 +02:00
name = 'coal'
2018-01-21 17:30:42 +02:00
elseif value < 850 then
2018-05-23 23:34:47 +02:00
name = 'stone'
else
2018-01-21 17:30:42 +02:00
return nil
end
2018-06-12 17:57:39 +02:00
value = perlin.noise(y / 64, x / 64)
2018-01-21 17:30:42 +02:00
value = value + 1
value = value * 500
2018-05-23 23:34:47 +02:00
return {name = name, position = {x, y}, amount = value}
2018-01-21 17:30:42 +02:00
end
function run_combined_module(event)
if not global.blocks then
init_blocks()
end
local area = event.area
local surface = event.surface
local top_x = area.left_top.x
2018-05-23 23:34:47 +02:00
local top_y = area.left_top.y
2018-01-21 17:30:42 +02:00
local tiles = {}
2018-05-23 23:34:47 +02:00
local entities = {}
2018-01-21 17:30:42 +02:00
for y = top_y, top_y + 31 do
2018-05-23 23:34:47 +02:00
for x = top_x, top_x + 31 do
2018-01-21 17:30:42 +02:00
if -x > half_start_size or x >= half_start_size or -y > half_start_size or y >= half_start_size then
2018-05-23 23:34:47 +02:00
table.insert(tiles, {name = 'out-of-map', position = {x, y}})
2018-01-21 17:30:42 +02:00
end
2018-05-23 23:34:47 +02:00
local e = get_resource(x, y)
2018-01-21 17:30:42 +02:00
if e then
table.insert(entities, e)
end
end
end
2018-05-23 23:34:47 +02:00
2018-01-21 17:30:42 +02:00
surface.set_tiles(tiles, false)
for _, e in ipairs(entities) do
if surface.can_place_entity(e) then
2018-05-23 23:34:47 +02:00
surface.create_entity(e)
2018-01-21 17:30:42 +02:00
end
end
end
local function get_block()
local blocks = global.blocks
local count = global.weight_count
while count >= block_weight and count < total_weight and #blocks > min_blocks_in_list do
local index = math.random(#blocks)
2018-05-23 23:34:47 +02:00
table.remove(blocks, index)
2018-01-21 17:30:42 +02:00
count = count + 1
end
if count < block_weight then
count = count + 1
end
2018-05-23 23:34:47 +02:00
if count == total_weight then
2018-01-21 17:30:42 +02:00
global.weight_count = 0
else
global.weight_count = count
end
local index = math.random(#blocks)
2018-05-23 23:34:47 +02:00
return table.remove(blocks, index)
2018-01-21 17:30:42 +02:00
end
2018-05-23 23:34:47 +02:00
local function do_strike()
local block = get_block()
2018-01-21 17:30:42 +02:00
2018-05-23 23:34:47 +02:00
function add(x, y)
local key = x .. ',' .. y
2018-01-21 17:30:42 +02:00
if not global.used_blocks[key] then
table.insert(global.blocks, {x = x, y = y})
2018-05-23 23:34:47 +02:00
global.used_blocks[key] = true
2018-01-21 17:30:42 +02:00
end
end
add(block.x, block.y - 1)
add(block.x + 1, block.y)
add(block.x, block.y + 1)
2018-05-23 23:34:47 +02:00
add(block.x - 1, block.y)
2018-01-21 17:30:42 +02:00
local tiles = {}
local bx = block.x * block_size
2018-05-23 23:34:47 +02:00
local by = block.y * block_size
2018-01-21 17:30:42 +02:00
for x = bx, bx + block_size - 1 do
for y = by, by + block_size - 1 do
2018-05-23 23:34:47 +02:00
table.insert(tiles, {name = 'dry-dirt', position = {x, y}})
2018-01-21 17:30:42 +02:00
end
end
local surface = game.surfaces[1]
surface.set_tiles(tiles, false)
2018-05-23 23:34:47 +02:00
game.forces.player.chart(surface, {{bx, by}, {bx + block_size, by + block_size}})
2018-01-21 17:30:42 +02:00
end
2018-05-23 23:34:47 +02:00
local function on_tick()
2018-01-21 17:30:42 +02:00
if global.strike_time == 0 then
do_strike()
global.strike_time = strike_time
2018-05-23 23:34:47 +02:00
else
2018-01-21 17:30:42 +02:00
global.strike_time = global.strike_time - 1
end
end
2018-05-23 23:34:47 +02:00
Event.add(defines.events.on_tick, on_tick)