1
0
mirror of https://github.com/ComfyFactory/ComfyFactorio.git synced 2025-01-10 00:43:27 +02:00
ComfyFactorio/maps/biter_battles_v2/pregenerate_chunks.lua

88 lines
3.1 KiB
Lua
Raw Normal View History

2019-03-14 23:50:09 +02:00
local event = require 'utils.event'
2019-03-17 03:35:58 +02:00
local function set_chunk_coords(radius)
global.chunk_gen_coords = {}
2019-03-14 23:50:09 +02:00
for r = radius, 1, -1 do
2019-03-14 19:06:39 +02:00
for x = r * -1, r - 1, 1 do
2019-03-14 23:50:09 +02:00
local pos = {x = x, y = r * -1}
2019-03-17 03:35:58 +02:00
if math.sqrt(pos.x ^ 2 + pos.y ^ 2) <= radius then table.insert(global.chunk_gen_coords, pos) end
2019-03-14 19:06:39 +02:00
end
for y = r * -1, r - 1, 1 do
2019-03-14 23:50:09 +02:00
local pos = {x = r, y = y}
2019-03-17 03:35:58 +02:00
if math.sqrt(pos.x ^ 2 + pos.y ^ 2) <= radius then table.insert(global.chunk_gen_coords, pos) end
2019-03-14 19:06:39 +02:00
end
for x = r, r * -1 + 1, -1 do
2019-03-14 23:50:09 +02:00
local pos = {x = x, y = r}
2019-03-17 03:35:58 +02:00
if math.sqrt(pos.x ^ 2 + pos.y ^ 2) <= radius then table.insert(global.chunk_gen_coords, pos) end
2019-03-14 19:06:39 +02:00
end
for y = r, r * -1 + 1, -1 do
2019-03-14 23:50:09 +02:00
local pos = {x = r * -1, y = y}
2019-03-17 03:35:58 +02:00
if math.sqrt(pos.x ^ 2 + pos.y ^ 2) <= radius then table.insert(global.chunk_gen_coords, pos) end
2019-03-14 19:06:39 +02:00
end
end
end
2019-03-17 03:35:58 +02:00
local function draw_gui()
for _, player in pairs(game.connected_players) do
if global.map_generation_complete then
if player.gui.left["map_pregen"] then player.gui.left["map_pregen"].destroy() end
2019-03-14 23:50:09 +02:00
else
2019-03-18 04:24:13 +02:00
local caption = "Map is generating... " .. #global.chunk_gen_coords .. " chunks left. Please get comfy."
2019-03-17 03:35:58 +02:00
if player.gui.left["map_pregen"] then
player.gui.left["map_pregen"].caption = caption
else
local frame = player.gui.left.add({
type = "frame",
caption = caption,
name = "map_pregen"
})
frame.style.font_color = {r = 150, g = 0, b = 255}
2019-03-18 04:24:13 +02:00
frame.style.font = "heading-1"
frame.style.maximal_height = 42
2019-03-17 03:35:58 +02:00
end
end
2019-03-14 19:06:39 +02:00
end
end
2019-03-17 19:19:40 +02:00
local function process_chunk(surface)
2019-03-17 03:35:58 +02:00
if global.map_generation_complete then return end
2019-03-18 04:24:13 +02:00
if game.tick < 300 then return end
2019-07-29 15:24:02 +02:00
if not global.chunk_gen_coords then
set_chunk_coords(bb_config.map_pregeneration_radius)
--table.shuffle_table(global.chunk_gen_coords)
end
2019-03-17 03:35:58 +02:00
if #global.chunk_gen_coords == 0 then
global.map_generation_complete = true
draw_gui()
2019-03-21 11:06:12 +02:00
for _, player in pairs(game.connected_players) do
player.play_sound{path="utility/new_objective", volume_modifier=0.75}
end
2019-03-17 03:35:58 +02:00
return
2019-03-14 19:06:39 +02:00
end
2019-03-16 21:25:21 +02:00
2019-03-17 03:35:58 +02:00
if not game then return end
local surface = game.surfaces["biter_battles"]
if not surface then return end
2019-06-24 18:54:29 +02:00
local force_chunk_requests = 2
if bb_config.fast_pregen then force_chunk_requests = 16 end
2019-04-28 19:38:44 +02:00
2019-03-17 03:35:58 +02:00
for i = #global.chunk_gen_coords, 1, -1 do
if surface.is_chunk_generated(global.chunk_gen_coords[i]) then
2019-07-29 15:24:02 +02:00
--game.forces.player.chart(surface, {{(global.chunk_gen_coords[i].x * 32), (global.chunk_gen_coords[i].y * 32)}, {(global.chunk_gen_coords[i].x * 32) + 32, (global.chunk_gen_coords[i].y * 32) + 32}})
2019-03-17 03:35:58 +02:00
global.chunk_gen_coords[i] = nil
2019-03-16 21:25:21 +02:00
else
2019-07-29 15:24:02 +02:00
--game.forces.player.chart(surface, {{(global.chunk_gen_coords[i].x * 32), (global.chunk_gen_coords[i].y * 32)}, {(global.chunk_gen_coords[i].x * 32) + 32, (global.chunk_gen_coords[i].y * 32) + 32}})
surface.request_to_generate_chunks({x = (global.chunk_gen_coords[i].x * 32), y = (global.chunk_gen_coords[i].y * 32)}, 1)
2019-03-17 03:35:58 +02:00
surface.force_generate_chunk_requests()
global.chunk_gen_coords[i] = nil
2019-04-28 19:38:44 +02:00
force_chunk_requests = force_chunk_requests - 1
if force_chunk_requests <= 0 then
break
end
2019-03-17 03:35:58 +02:00
end
2019-03-16 21:25:21 +02:00
end
2019-03-17 03:35:58 +02:00
draw_gui()
2019-03-14 23:50:09 +02:00
end
2019-03-17 03:35:58 +02:00
return process_chunk