1
0
mirror of https://github.com/Refactorio/RedMew.git synced 2025-03-19 21:18:01 +02:00
RedMew/utils/test/helper.lua

102 lines
2.9 KiB
Lua
Raw Permalink Normal View History

2020-10-04 12:10:27 +01:00
local Global = require 'utils.global'
2020-09-26 20:02:53 +01:00
local Public = {}
local surface_count = 0
Global.register({surface_count = surface_count}, function(tbl)
surface_count = tbl.surface_count
end)
2020-10-04 12:10:27 +01:00
2020-09-26 20:02:53 +01:00
local function get_surface_name()
surface_count = surface_count + 1
return 'test_surface' .. surface_count
end
local autoplace_settings = {
tile = {treat_missing_as_default = false, settings = {['grass-1'] = {frequency = 1, size = 1, richness = 1}}}
2020-09-26 20:02:53 +01:00
}
local autoplace_controls = {
trees = {frequency = 1, richness = 1, size = 0},
['enemy-base'] = {frequency = 1, richness = 1, size = 0},
coal = {frequency = 1, richness = 1, size = 0},
['copper-ore'] = {frequency = 1, richness = 1, size = 0},
['crude-oil'] = {frequency = 1, richness = 1, size = 0},
['iron-ore'] = {frequency = 1, richness = 1, size = 0},
stone = {frequency = 1, richness = 1, size = 0},
['uranium-ore'] = {frequency = 1, richness = 1, size = 0}
2020-09-26 20:02:53 +01:00
}
local cliff_settings = {cliff_elevation_0 = 1024, cliff_elevation_interval = 10, name = 'cliff'}
2020-09-26 20:02:53 +01:00
2020-09-28 20:40:47 +01:00
function Public.startup_test_surface(context, options)
2020-09-26 20:02:53 +01:00
options = options or {}
local name = options.name or get_surface_name()
local area = options.area or {64, 64}
2020-09-28 20:40:47 +01:00
local player = context.player
2020-09-26 20:02:53 +01:00
local old_surface = player.surface
local old_position = player.position
2020-09-28 21:03:31 +01:00
local old_character = player.character
2020-09-26 20:02:53 +01:00
local surface = game.create_surface(name, {
width = area.x or area[1],
height = area.y or area[2],
autoplace_settings = autoplace_settings,
autoplace_controls = autoplace_controls,
cliff_settings = cliff_settings
})
2020-09-26 20:02:53 +01:00
surface.request_to_generate_chunks({0, 0}, 32)
surface.force_generate_chunk_requests()
player.force.chart_all(surface)
2020-09-26 20:02:53 +01:00
context:next(function()
for k, v in pairs(surface.find_entities()) do
v.destroy()
end
2020-09-26 20:02:53 +01:00
surface.destroy_decoratives {area = {{-32, -32}, {32, 32}}}
2020-09-26 20:02:53 +01:00
player.character = nil
player.teleport({0, 0}, surface)
player.create_character()
end)
2020-09-26 20:02:53 +01:00
return function()
2020-09-29 20:50:07 +01:00
player.character = nil
2020-09-26 20:02:53 +01:00
player.teleport(old_position, old_surface)
2020-09-28 21:03:31 +01:00
if old_character and old_character.valid then
player.character = old_character
end
2020-09-26 20:02:53 +01:00
game.delete_surface(surface)
end
end
function Public.wait_for_chunk_to_be_charted(context, force, surface, chunk_position, next)
if not force.is_chunk_charted(surface, chunk_position) then
context:next(function()
Public.wait_for_chunk_to_be_charted(context, force, surface, chunk_position)
end)
return
end
if next then
context:next(next)
end
end
function Public.modify_lua_object(context, object, key, value)
local old_value = object[key]
rawset(object, key, value)
context:add_teardown(function()
rawset(object, key, old_value)
end)
end
2020-09-26 20:02:53 +01:00
return Public