1
0
mirror of https://github.com/Refactorio/RedMew.git synced 2024-12-14 10:13:13 +02:00
RedMew/map_gen/presets/crash_site/outpost_builder.lua

1279 lines
31 KiB
Lua
Raw Normal View History

2018-07-13 12:47:38 +02:00
local Random = require 'map_gen.shared.random'
2018-11-26 03:07:03 +02:00
local Token = require 'utils.token'
2018-07-20 23:01:34 +02:00
local Global = require 'utils.global'
local Event = require 'utils.event'
2019-01-04 22:02:55 +02:00
local Task = require 'utils.task'
local Retailer = require 'features.retailer'
2018-11-11 09:20:07 +02:00
local PlayerStats = require 'features.player_stats'
2018-07-20 23:01:34 +02:00
2018-07-14 11:33:06 +02:00
local b = require 'map_gen.shared.builders'
2018-07-13 12:47:38 +02:00
local direction_bit_mask = 0xc0000000
local section_bit_mask = 0x30000000
local level_bit_mask = 0x0fffffff
2018-07-13 17:36:43 +02:00
local not_level_bit_mask = 0xf0000000
local direction_bit_shift = 30
local section_bit_shift = 28
local section_straight = 0
local section_outer_corner = 1
local section_inner_corner = 2
local wall_north_straight = 0x00000001
local wall_east_straight = 0x40000001
local wall_south_straight = 0x80000001
local wall_west_straight = 0xc0000001
local wall_north_outer = 0x10000001
local wall_east_outer = 0x50000001
local wall_south_outer = 0x90000001
local wall_west_outer = 0xd0000001
local wall_north_inner = 0x20000001
local wall_east_inner = 0x60000001
local wall_south_inner = 0xa0000001
local wall_west_inner = 0xe0000001
2019-01-10 00:29:51 +02:00
local default_part_size = 6
--local inv_part_size = 1 / part_size
2018-07-14 11:33:06 +02:00
2018-07-20 23:01:34 +02:00
local refill_turrets = {index = 1}
local power_sources = {}
local magic_crafters = {index = 1}
Global.register(
{
refil_turrets = refill_turrets,
power_sources = power_sources,
magic_crafters = magic_crafters
},
function(tbl)
refill_turrets = tbl.refil_turrets
power_sources = tbl.power_sources
magic_crafters = tbl.magic_crafters
end
)
2018-07-13 17:36:43 +02:00
local function get_direction(part)
local dir = bit32.band(part, direction_bit_mask)
return bit32.rshift(dir, direction_bit_shift - 1)
end
2018-07-14 11:33:06 +02:00
local function get_4_way_direction(part)
local dir = bit32.band(part, direction_bit_mask)
return bit32.rshift(dir, direction_bit_shift)
end
2018-07-13 17:36:43 +02:00
local function get_section(part)
local sec = bit32.band(part, section_bit_mask)
return bit32.rshift(sec, section_bit_shift)
end
local function get_level(part)
return bit32.band(part, level_bit_mask)
end
local function set_level(part, level)
local not_level = bit32.band(part)
return not_level + level
end
2018-07-13 12:47:38 +02:00
local function set_block(tbl, x, y, value)
tbl[(y - 1) * tbl.size + x] = value
end
local function get_block(tbl, x, y)
local size = tbl.size
if x < 1 or x > size or y < 1 or y > size then
return 0
end
return tbl[(y - 1) * size + x] or 0
end
2018-07-23 22:50:11 +02:00
local function fast_remove(tbl, index)
local count = #tbl
if index > count then
return
elseif index < count then
tbl[index] = tbl[count]
end
tbl[count] = nil
end
2018-07-13 12:47:38 +02:00
local Public = {}
Public.__index = Public
2018-07-20 23:01:34 +02:00
Public.empty_template = {}
2018-07-28 14:04:14 +02:00
function Public.new(random)
local obj = {random = random}
2018-07-13 12:47:38 +02:00
return setmetatable(obj, Public)
end
local function do_walls(self, blocks, outpost_variance, outpost_min_step)
local size = blocks.size
local random = self.random
local max_variance = size - outpost_variance + 1
local variance_step = outpost_variance + outpost_min_step
local x = random:next_int(1, outpost_variance)
local y = random:next_int(1, outpost_variance)
local start_x, start_y = x, y
2018-07-13 17:36:43 +02:00
local i = (y - 1) * size + x
local pv = -1
-- top
2018-07-13 12:47:38 +02:00
while x < size do
local tx = x + random:next_int(outpost_min_step, variance_step)
2018-07-13 17:36:43 +02:00
tx = math.min(tx, size)
if pv == 0 then
blocks[i] = wall_north_straight
elseif pv == -1 then
blocks[i] = wall_north_outer
else
2018-07-14 11:33:06 +02:00
blocks[i] = wall_east_inner
2018-07-13 17:36:43 +02:00
end
2018-07-13 12:47:38 +02:00
2018-07-13 17:36:43 +02:00
x = x + 1
i = i + 1
2018-07-13 12:47:38 +02:00
2018-07-13 17:36:43 +02:00
while x < tx do
blocks[i] = wall_north_straight
x = x + 1
i = i + 1
end
if x < size - outpost_min_step then
local ty = random:next_int(1, outpost_variance)
if y == ty then
pv = 0
elseif y < ty then
pv = 1
2018-07-14 11:33:06 +02:00
blocks[i] = wall_east_outer
2018-07-13 17:36:43 +02:00
y = y + 1
i = i + size
while y < ty do
blocks[i] = wall_east_straight
y = y + 1
i = i + size
end
else
pv = -1
blocks[i] = wall_north_inner
y = y - 1
i = i - size
while y > ty do
blocks[i] = wall_west_straight
y = y - 1
i = i - size
end
end
else
pv = 0
end
2018-07-13 12:47:38 +02:00
end
2018-07-13 17:36:43 +02:00
pv = 1
-- right
2018-07-13 12:47:38 +02:00
while y < size do
local ty = y + random:next_int(outpost_min_step, variance_step)
2018-07-13 17:36:43 +02:00
ty = math.min(ty, size)
if pv == 0 then
blocks[i] = wall_east_straight
elseif pv == -1 then
2018-07-14 11:33:06 +02:00
blocks[i] = wall_south_inner
2018-07-13 17:36:43 +02:00
else
blocks[i] = wall_east_outer
end
2018-07-13 12:47:38 +02:00
2018-07-13 17:36:43 +02:00
y = y + 1
i = i + size
2018-07-13 12:47:38 +02:00
2018-07-13 17:36:43 +02:00
while y < ty do
blocks[i] = wall_east_straight
y = y + 1
i = i + size
end
if y < size - outpost_min_step then
local tx = random:next_int(max_variance, size)
if x == tx then
pv = 0
elseif x < tx then
pv = 1
blocks[i] = wall_east_inner
x = x + 1
i = i + 1
while x < tx do
blocks[i] = wall_north_straight
x = x + 1
i = i + 1
end
else
pv = -1
2018-07-14 11:33:06 +02:00
blocks[i] = wall_south_outer
2018-07-13 17:36:43 +02:00
x = x - 1
i = i - 1
while x > tx do
blocks[i] = wall_south_straight
x = x - 1
i = i - 1
end
end
else
pv = 0
end
2018-07-13 12:47:38 +02:00
end
2018-07-13 17:36:43 +02:00
pv = 1
-- bottom
2018-07-13 12:47:38 +02:00
while x > 1 do
local tx = x - random:next_int(outpost_min_step, variance_step)
2018-07-13 17:36:43 +02:00
tx = math.max(tx, 1)
if pv == 0 then
blocks[i] = wall_south_straight
elseif pv == -1 then
2018-07-14 11:33:06 +02:00
blocks[i] = wall_west_inner
2018-07-13 17:36:43 +02:00
else
blocks[i] = wall_south_outer
end
x = x - 1
i = i - 1
2018-07-13 12:47:38 +02:00
2018-07-13 17:36:43 +02:00
while x > tx do
blocks[i] = wall_south_straight
x = x - 1
i = i - 1
end
2018-07-13 12:47:38 +02:00
2018-07-13 17:36:43 +02:00
if x > outpost_min_step + 1 then
local ty = random:next_int(max_variance, size)
if y == ty then
pv = 0
elseif y < ty then
pv = 1
blocks[i] = wall_south_inner
y = y + 1
i = i + size
while y < ty do
blocks[i] = wall_east_straight
y = y + 1
i = i + size
end
else
pv = -1
2018-07-14 11:33:06 +02:00
blocks[i] = wall_west_outer
2018-07-13 17:36:43 +02:00
y = y - 1
i = i - size
while y > ty do
blocks[i] = wall_west_straight
y = y - 1
i = i - size
end
end
else
pv = 0
end
2018-07-13 12:47:38 +02:00
end
2018-07-13 17:36:43 +02:00
pv = -1
-- left
local bottom_left_y = y
while y > start_y + variance_step do
2018-07-13 12:47:38 +02:00
local ty = y - random:next_int(outpost_min_step, variance_step)
2018-07-13 17:36:43 +02:00
ty = math.max(ty, start_y)
if pv == 0 then
blocks[i] = wall_west_straight
elseif pv == -1 then
blocks[i] = wall_west_outer
else
2018-07-14 11:33:06 +02:00
blocks[i] = wall_north_inner
2018-07-13 12:47:38 +02:00
end
2018-07-13 17:36:43 +02:00
y = y - 1
i = i - size
while y > ty do
blocks[i] = wall_west_straight
y = y - 1
i = i - size
end
2018-07-13 12:47:38 +02:00
2018-07-13 17:36:43 +02:00
if y > start_y + variance_step + outpost_min_step then
local tx = random:next_int(1, outpost_variance)
if x == tx then
pv = 0
elseif x < tx then
pv = 1
2018-07-30 17:42:14 +02:00
--blocks[i] = wall_west_outer
blocks[i] = wall_north_outer
2018-07-13 17:36:43 +02:00
x = x + 1
i = i + 1
while x < tx do
blocks[i] = wall_north_straight
x = x + 1
i = i + 1
end
else
pv = -1
blocks[i] = wall_west_inner
x = x - 1
i = i - 1
while x > tx do
blocks[i] = wall_south_straight
x = x - 1
i = i - 1
end
end
else
pv = 0
end
2018-07-13 12:47:38 +02:00
end
2018-07-13 17:36:43 +02:00
-- final connection
if y == bottom_left_y then
blocks[i] = wall_west_outer
2018-07-13 12:47:38 +02:00
2018-07-13 17:36:43 +02:00
y = y - 1
i = i - size
2018-07-13 12:47:38 +02:00
2018-07-13 17:36:43 +02:00
while y > bottom_left_y - outpost_min_step do
blocks[i] = wall_west_straight
y = y - 1
i = i - size
2018-07-13 12:47:38 +02:00
end
2018-07-13 17:36:43 +02:00
end
if x == start_x then
pv = 0
elseif x < start_x then
pv = 1
2018-07-14 11:33:06 +02:00
blocks[i] = wall_north_outer
2018-07-13 17:36:43 +02:00
x = x + 1
i = i + 1
while x < start_x do
blocks[i] = wall_north_straight
x = x + 1
i = i + 1
2018-07-13 12:47:38 +02:00
end
2018-07-13 17:36:43 +02:00
else
pv = -1
blocks[i] = wall_west_inner
x = x - 1
i = i - 1
while x > start_x do
blocks[i] = wall_south_straight
x = x - 1
i = i - 1
2018-07-13 12:47:38 +02:00
end
2018-07-13 17:36:43 +02:00
end
2018-07-13 12:47:38 +02:00
2018-07-13 17:36:43 +02:00
if pv == 0 then
blocks[i] = wall_west_straight
elseif pv == -1 then
blocks[i] = wall_west_outer
else
2018-07-14 11:33:06 +02:00
blocks[i] = wall_north_inner
2018-07-13 17:36:43 +02:00
end
y = y - 1
i = i - size
while y > start_y do
blocks[i] = wall_west_straight
y = y - 1
i = i - size
2018-07-13 12:47:38 +02:00
end
end
local function fill(blocks)
local size = blocks.size
local anti_set = {size = size}
local anti_stack = {}
local y_offset = (size - 1) * size
for x = 1, size do
2018-07-13 17:36:43 +02:00
if blocks[x] == nil then
2018-07-13 12:47:38 +02:00
table.insert(anti_stack, {x = x, y = 1})
end
2018-07-13 17:36:43 +02:00
if blocks[x + y_offset] == nil then
2018-07-13 12:47:38 +02:00
table.insert(anti_stack, {x = x, y = size})
end
end
for y = 2, size do
y_offset = (y - 1) * size
2018-07-13 17:36:43 +02:00
if blocks[y_offset + 1] == nil then
2018-07-13 12:47:38 +02:00
table.insert(anti_stack, {x = 1, y = y})
end
2018-07-13 17:36:43 +02:00
if blocks[y_offset + size] == nil then
2018-07-13 12:47:38 +02:00
table.insert(anti_stack, {x = size, y = y})
end
end
while #anti_stack > 0 do
local point = table.remove(anti_stack)
local x, y = point.x, point.y
local offset = (y - 1) * size + x
2018-07-13 17:36:43 +02:00
anti_set[offset] = true
2018-07-13 12:47:38 +02:00
if x > 1 then
local x2 = x - 1
local offset2 = offset - 1
2018-07-13 17:36:43 +02:00
if not anti_set[offset2] and not blocks[offset2] then
2018-07-13 12:47:38 +02:00
table.insert(anti_stack, {x = x2, y = y})
end
end
if x < size then
local x2 = x + 1
local offset2 = offset + 1
2018-07-13 17:36:43 +02:00
if not anti_set[offset2] and not blocks[offset2] then
2018-07-13 12:47:38 +02:00
table.insert(anti_stack, {x = x2, y = y})
end
end
if y > 1 then
local y2 = y - 1
local offset2 = offset - size
2018-07-13 17:36:43 +02:00
if not anti_set[offset2] and not blocks[offset2] then
2018-07-13 12:47:38 +02:00
table.insert(anti_stack, {x = x, y = y2})
end
end
if y < size then
local y2 = y + 1
local offset2 = offset + size
2018-07-13 17:36:43 +02:00
if not anti_set[offset2] and not blocks[offset2] then
2018-07-13 12:47:38 +02:00
table.insert(anti_stack, {x = x, y = y2})
end
end
end
for y = 1, size do
local offset = (y - 1) * size
for x = 1, size do
local i = offset + x
2018-07-13 17:36:43 +02:00
if not anti_set[i] and not blocks[i] then
blocks[i] = 2
2018-07-13 12:47:38 +02:00
end
end
end
end
local function do_levels(blocks, max_level)
local size = blocks.size
2018-07-13 17:36:43 +02:00
local level = 2
2018-07-13 12:47:38 +02:00
2018-07-24 01:45:38 +02:00
while level < max_level do
2018-07-13 17:36:43 +02:00
local next_level = level + 1
2018-07-20 23:01:34 +02:00
2018-07-13 12:47:38 +02:00
for y = 1, size do
local offset = (y - 1) * size
for x = 1, size do
local i = offset + x
2018-07-13 17:36:43 +02:00
if get_level(blocks[i] or 0) >= level then
2018-07-13 12:47:38 +02:00
local count = 0
2018-07-13 17:36:43 +02:00
if x > 1 and get_level(blocks[i - 1] or 0) >= level then
2018-07-13 12:47:38 +02:00
count = count + 1
end
2018-07-13 17:36:43 +02:00
if x < size and get_level(blocks[i + 1] or 0) >= level then
2018-07-13 12:47:38 +02:00
count = count + 1
end
2018-07-13 17:36:43 +02:00
if y > 1 and get_level(blocks[i - size] or 0) >= level then
2018-07-13 12:47:38 +02:00
count = count + 1
end
2018-07-13 17:36:43 +02:00
if y < size and get_level(blocks[i + size] or 0) >= level then
2018-07-13 12:47:38 +02:00
count = count + 1
end
if count == 4 then
blocks[i] = next_level
end
end
end
end
level = level + 1
2018-07-24 01:45:38 +02:00
end
2018-07-13 12:47:38 +02:00
2018-07-20 23:01:34 +02:00
local levels = {}
blocks.levels = levels
2018-07-13 12:47:38 +02:00
2018-07-20 23:01:34 +02:00
for i = 1, max_level do
levels[i] = {}
end
2018-07-14 11:33:06 +02:00
2018-07-20 23:01:34 +02:00
for y = 1, size do
local offset = (y - 1) * size
for x = 1, size do
local i = offset + x
local block = blocks[i]
if block then
local l = get_level(block)
table.insert(levels[l], i)
end
2018-07-14 11:33:06 +02:00
end
2018-07-20 23:01:34 +02:00
end
end
2018-07-14 11:33:06 +02:00
2018-07-23 22:50:11 +02:00
local function get_template(random, templates, templates_count, counts)
local template
if templates_count == 0 then
return nil
elseif templates_count == 1 then
template = templates[1]
else
local ti = random:next_int(1, templates_count)
template = templates[ti]
end
if template == Public.empty_template then
return nil
end
local count = counts[template] or 0
local max_count = template.max_count
while count == max_count do
template = template.fallback
if template == nil then
return nil
end
count = counts[template] or 0
max_count = template.max_count
end
counts[template] = count + 1
return template
end
2018-07-24 01:45:38 +02:00
local function make_blocks(self, blocks, template)
2018-07-20 23:01:34 +02:00
local random = self.random
2019-01-10 00:29:51 +02:00
local counts = {}
2018-07-14 11:33:06 +02:00
2018-07-20 23:01:34 +02:00
local levels = blocks.levels
local wall_level = levels[1]
2018-07-14 11:33:06 +02:00
2018-07-24 01:45:38 +02:00
local walls = template.walls
2018-07-20 23:01:34 +02:00
local wall_template_count = #walls
2019-01-10 00:29:51 +02:00
while #wall_level > 0 do
local index = random:next_int(1, #wall_level)
local i = wall_level[index]
fast_remove(wall_level, index)
local block = get_template(random, walls, wall_template_count, counts)
2018-07-14 11:33:06 +02:00
2018-07-24 01:45:38 +02:00
if block == Public.empty_template then
2018-07-20 23:01:34 +02:00
blocks[i] = nil
else
2018-07-24 01:45:38 +02:00
local block_data = blocks[i]
2018-07-14 11:33:06 +02:00
2018-07-24 01:45:38 +02:00
local section = get_section(block_data)
local dir = get_4_way_direction(block_data)
2018-07-14 11:33:06 +02:00
2018-07-24 01:45:38 +02:00
local new_block = block[section + 1][dir + 1]
2018-07-20 23:01:34 +02:00
blocks[i] = new_block
end
end
2018-07-24 01:45:38 +02:00
local bases = template.bases
2018-07-23 22:50:11 +02:00
2018-07-20 23:01:34 +02:00
for l = 2, #levels do
local level = levels[l]
2018-07-24 01:45:38 +02:00
local base_templates = bases[l - 1]
2018-07-20 23:01:34 +02:00
if base_templates then
local base_template_count = #base_templates
2018-07-23 22:50:11 +02:00
while #level > 0 do
local index = random:next_int(1, #level)
local i = level[index]
2018-07-14 11:33:06 +02:00
2018-07-23 22:50:11 +02:00
fast_remove(level, index)
2018-07-24 01:45:38 +02:00
blocks[i] = get_template(random, base_templates, base_template_count, counts)
2018-07-20 23:01:34 +02:00
end
else
for _, i in ipairs(level) do
blocks[i] = nil
2018-07-14 11:33:06 +02:00
end
2018-07-20 23:01:34 +02:00
end
end
end
2018-07-14 11:33:06 +02:00
2018-07-23 22:50:11 +02:00
local remove_entity_types = {'tree', 'simple-entity'}
2019-01-10 00:29:51 +02:00
local function to_shape(blocks, part_size)
part_size = part_size or default_part_size
local inv_part_size = 1 / part_size
2018-07-20 23:01:34 +02:00
local size = blocks.size
local t_size = size * part_size
local half_t_size = t_size * 0.5
2018-07-14 11:33:06 +02:00
2018-07-23 22:50:11 +02:00
return function(x, y, world)
2018-07-20 23:01:34 +02:00
x, y = math.floor(x + half_t_size), math.floor(y + half_t_size)
if x < 0 or y < 0 or x >= t_size or y >= t_size then
2018-07-28 14:04:14 +02:00
return false
2018-07-20 23:01:34 +02:00
end
2018-07-14 11:33:06 +02:00
2018-07-20 23:01:34 +02:00
local x2, y2 = math.floor(x * inv_part_size), math.floor(y * inv_part_size)
2018-07-14 11:33:06 +02:00
2018-07-20 23:01:34 +02:00
local template = blocks[y2 * size + x2 + 1]
if not template then
2018-07-28 14:04:14 +02:00
return false
2018-07-20 23:01:34 +02:00
end
2018-07-14 11:33:06 +02:00
2018-07-23 22:50:11 +02:00
local wx, wy = world.x, world.y
for _, e in ipairs(
world.surface.find_entities_filtered(
{
area = {{wx, wy}, {wx + 1, wy + 1}},
type = remove_entity_types
}
)
) do
e.destroy()
end
2018-07-20 23:01:34 +02:00
local x3, y3 = x - x2 * part_size, y - y2 * part_size
2018-07-14 11:33:06 +02:00
2018-07-20 23:01:34 +02:00
local i = y3 * part_size + x3 + 1
local entry = template[i]
if not entry then
2018-07-28 14:04:14 +02:00
return false
2018-07-20 23:01:34 +02:00
end
2018-07-14 11:33:06 +02:00
2018-07-20 23:01:34 +02:00
local entity = entry.entity
local tile = entry.tile or true
if entity then
local data
local callback = entity.callback
if callback then
local cd = template[callback]
callback = cd.callback
data = cd.data
2018-07-14 11:33:06 +02:00
end
2018-07-20 23:01:34 +02:00
return {
tile = tile,
entities = {
{
name = entity.name,
direction = entity.direction,
2019-01-10 00:29:51 +02:00
force = entity.force or template.force,
2018-07-20 23:01:34 +02:00
callback = callback,
2018-07-23 22:50:11 +02:00
data = data,
always_place = true
2018-07-20 23:01:34 +02:00
}
}
}
2018-07-14 11:33:06 +02:00
end
2018-07-20 23:01:34 +02:00
return tile
2018-07-14 11:33:06 +02:00
end
end
local path_tiles = b.path_tiles
local function collides(world)
local gen_tile = world.surface.get_tile(world.x, world.y)
return gen_tile.collides_with('water-tile')
end
local function set_hidden_tiles(shape)
return function(x, y, world)
local tile = shape(x, y, world)
if type(tile) == 'table' then
if path_tiles[tile.tile] and collides(world) then
tile.hidden_tile = 'grass-1'
end
elseif path_tiles[tile] and collides(world) then
tile = {tile = tile, hidden_tile = 'grass-1'}
end
return tile
end
end
2018-07-24 01:45:38 +02:00
function Public:do_outpost(template)
local settings = template.settings
local blocks = {size = settings.blocks}
2018-07-13 12:47:38 +02:00
2018-07-24 01:45:38 +02:00
do_walls(self, blocks, settings.variance, settings.min_step)
2018-07-13 12:47:38 +02:00
fill(blocks)
2018-07-24 01:45:38 +02:00
do_levels(blocks, settings.max_level)
make_blocks(self, blocks, template)
2018-07-20 23:01:34 +02:00
2019-01-10 00:29:51 +02:00
local shape = to_shape(blocks, settings.part_size)
return set_hidden_tiles(shape)
2018-07-20 23:01:34 +02:00
end
2019-01-10 00:29:51 +02:00
function Public.to_shape(blocks, part_size)
return to_shape(blocks, part_size)
2018-07-20 23:01:34 +02:00
end
2018-07-23 22:50:11 +02:00
local function change_direction(entity, new_dir)
2018-07-20 23:01:34 +02:00
local copy = {}
2018-07-23 22:50:11 +02:00
for k, v in pairs(entity) do
copy[k] = v
2018-07-20 23:01:34 +02:00
end
2018-07-23 22:50:11 +02:00
copy.direction = new_dir
2018-07-20 23:01:34 +02:00
return copy
end
2018-07-13 12:47:38 +02:00
2018-07-20 23:01:34 +02:00
function Public.make_1_way(data)
data.__index = data
return data
2018-07-14 11:33:06 +02:00
end
2018-07-13 17:36:43 +02:00
2018-07-23 22:50:11 +02:00
local function set_tile(tbl, index, tile)
local entry = tbl[index]
if entry then
entry.tile = tile
else
tbl[index] = {tile = tile}
end
end
local function set_entity(tbl, index, entity)
local entry = tbl[index]
if entry then
entry.entity = entity
else
tbl[index] = {entity = entity}
end
end
2018-07-14 11:33:06 +02:00
function Public.make_4_way(data)
2019-01-10 00:29:51 +02:00
local part_size = data.part_size or default_part_size
local inv_part_size = 1 / part_size
2018-07-20 23:01:34 +02:00
local props = {}
2018-07-14 11:33:06 +02:00
local north = {}
local east = {}
local south = {}
local west = {}
local res = {north, east, south, west}
for i, entry in pairs(data) do
2018-07-20 23:01:34 +02:00
if type(i) == 'string' then
props[i] = entry
else
local y = math.ceil(i * inv_part_size)
local x = i - (y - 1) * part_size
local x2 = part_size - y + 1
local y2 = x
local x3 = part_size - x + 1
local y3 = part_size - y + 1
local x4 = y
local y4 = part_size - x + 1
local i2 = (y2 - 1) * part_size + x2
local i3 = (y3 - 1) * part_size + x3
local i4 = (y4 - 1) * part_size + x4
2018-07-23 22:50:11 +02:00
local tile = entry.tile
if tile then
set_tile(north, i, tile)
set_tile(east, i2, tile)
set_tile(south, i3, tile)
set_tile(west, i4, tile)
2018-07-20 23:01:34 +02:00
end
2018-07-23 22:50:11 +02:00
local entity = entry.entity
if entity then
local offset = entity.offset
if offset == 3 then
2019-01-10 00:29:51 +02:00
i = i + part_size + 1
i2 = i2 + part_size
2018-07-23 22:50:11 +02:00
i4 = i4 + 1
elseif offset == 1 then
i = i + 1
2019-01-10 00:29:51 +02:00
i2 = i2 + part_size
2018-07-23 22:50:11 +02:00
elseif offset == 2 then
2019-01-10 00:29:51 +02:00
i = i + part_size
2018-07-27 01:36:25 +02:00
i4 = i4 + 1
2018-07-23 22:50:11 +02:00
end
local dir = entity.direction or 0
2018-07-20 23:01:34 +02:00
2018-07-23 22:50:11 +02:00
set_entity(north, i, entity)
set_entity(east, i2, change_direction(entity, (dir + 2) % 8))
set_entity(south, i3, change_direction(entity, (dir + 4) % 8))
set_entity(west, i4, change_direction(entity, (dir + 6) % 8))
end
2018-07-13 17:36:43 +02:00
end
2018-07-20 23:01:34 +02:00
end
2018-07-13 17:36:43 +02:00
2018-07-20 23:01:34 +02:00
north.__index = north
east.__index = east
south.__index = south
west.__index = west
for k, v in pairs(props) do
north[k] = v
east[k] = v
south[k] = v
west[k] = v
2018-07-13 12:47:38 +02:00
end
2018-07-14 11:33:06 +02:00
return res
2018-07-13 12:47:38 +02:00
end
2019-01-10 00:29:51 +02:00
function Public.make_walls(data)
data.__index = data
return data
end
2018-07-20 23:01:34 +02:00
local function shallow_copy(tbl)
local copy = {}
for k, v in pairs(tbl) do
copy[k] = v
end
return copy
end
function Public.extend_1_way(data, tbl)
return setmetatable(shallow_copy(tbl), data)
end
function Public.extend_4_way(data, tbl)
return {
setmetatable(shallow_copy(tbl), data[1]),
setmetatable(shallow_copy(tbl), data[2]),
setmetatable(shallow_copy(tbl), data[3]),
setmetatable(shallow_copy(tbl), data[4])
}
end
function Public.extend_walls(data, tbl)
2019-01-10 00:29:51 +02:00
local copy = shallow_copy(tbl)
local base = {
Public.extend_4_way(data[1], copy),
Public.extend_4_way(data[2], copy),
Public.extend_4_way(data[3], copy)
2018-07-20 23:01:34 +02:00
}
2019-01-10 00:29:51 +02:00
base.__index = base
return setmetatable(copy, base)
2018-07-20 23:01:34 +02:00
end
local function do_refill_turrets()
local index = refill_turrets.index
if index > #refill_turrets then
refill_turrets.index = 1
return
end
local data = refill_turrets[index]
local turret = data.turret
if not turret.valid then
fast_remove(refill_turrets, index)
return
end
refill_turrets.index = index + 1
local ammo = data.ammo
if data.liquid then
turret.fluidbox[1] = ammo
elseif ammo then
turret.insert(ammo)
end
end
local function do_magic_crafters()
local index = magic_crafters.index
if index > #magic_crafters then
magic_crafters.index = 1
return
end
local data = magic_crafters[index]
local entity = data.entity
if not entity.valid then
fast_remove(magic_crafters, index)
return
end
magic_crafters.index = index + 1
local tick = game.tick
local last_tick = data.last_tick
local rate = data.rate
local count = (tick - last_tick) * rate
local fcount = math.floor(count)
if fcount > 0 then
local fluidbox_index = data.fluidbox_index
if fluidbox_index then
local fb = entity.fluidbox
local fb_data = fb[fluidbox_index] or {name = data.item, amount = 0}
fb_data.amount = fb_data.amount + fcount
fb[fluidbox_index] = fb_data
else
entity.get_output_inventory().insert {name = data.item, count = fcount}
end
data.last_tick = tick - (count - fcount) / rate
end
end
local function tick()
do_refill_turrets()
do_magic_crafters()
end
Public.refill_turret_callback =
Token.register(
function(turret, ammo)
table.insert(refill_turrets, {turret = turret, ammo = ammo})
end
)
Public.refill_liquid_turret_callback =
Token.register(
function(turret, ammo)
table.insert(refill_turrets, {turret = turret, ammo = ammo, liquid = true})
end
)
Public.power_source_callback =
Token.register(
function(entity, data)
local power_source =
entity.surface.create_entity {name = 'hidden-electric-energy-interface', position = entity.position}
power_source.electric_buffer_size = data.buffer_size
power_source.power_production = data.power_production
power_sources[entity.unit_number] = power_source
end
)
2018-08-01 17:35:48 +02:00
Public.power_source_player_callback =
Token.register(
function(entity, data)
local power_source =
entity.surface.create_entity {name = 'hidden-electric-energy-interface', position = entity.position}
power_source.electric_buffer_size = data.buffer_size
power_source.power_production = data.power_production
power_sources[entity.unit_number] = power_source
entity.minable = false
entity.operable = false
entity.destructible = false
end
)
2018-07-20 23:01:34 +02:00
local function add_magic_crafter_output(entity, output, distance)
local rate = output.min_rate + output.distance_factor * distance
table.insert(
magic_crafters,
{
entity = entity,
last_tick = game.tick,
rate = rate,
item = output.item,
fluidbox_index = output.fluidbox_index
}
)
end
local set_inactive_token =
Token.register(
function(entity)
if entity.valid then
entity.active = false
end
end
)
Public.magic_item_crafting_callback =
Token.register(
function(entity, data)
entity.minable = false
entity.destructible = false
2018-07-24 01:45:38 +02:00
entity.operable = false
2018-07-20 23:01:34 +02:00
local recipe = data.recipe
if recipe then
entity.set_recipe(recipe)
else
local furance_item = data.furance_item
if furance_item then
local inv = entity.get_inventory(2) -- defines.inventory.furnace_source
inv.insert(furance_item)
end
end
local p = entity.position
2018-07-23 22:50:11 +02:00
local x, y = p.x, p.y
local distance = math.sqrt(x * x + y * y)
2018-07-20 23:01:34 +02:00
local output = data.output
if #output == 0 then
add_magic_crafter_output(entity, output, distance)
else
for _, o in ipairs(data.output) do
add_magic_crafter_output(entity, o, distance)
end
end
2018-07-28 14:04:14 +02:00
if not data.keep_active then
Task.set_timeout_in_ticks(2, set_inactive_token, entity) -- causes problems with refineries.
end
2018-07-20 23:01:34 +02:00
end
)
Public.deactivate_callback =
Token.register(
function(entity)
entity.active = false
entity.operable = false
entity.destructible = false
end
)
local function remove_power_source(event)
local entity = event.entity
if not entity or not entity.valid then
return
end
local number = entity.unit_number
2018-07-23 22:50:11 +02:00
if not number then
return
end
2018-07-20 23:01:34 +02:00
local ps = power_sources[number]
power_sources[number] = nil
if ps and ps.valid then
ps.destroy()
end
end
2018-07-23 22:50:11 +02:00
Public.market_set_items_callback =
Token.register(
function(entity, data)
if not entity.valid then
return
end
entity.destructible = false
local market_id = Retailer.generate_group_id()
Retailer.add_market(market_id, entity)
2019-01-10 03:22:48 +02:00
Retailer.set_market_group_label(market_id, data.market_name)
2018-07-23 22:50:11 +02:00
local p = entity.position
local x, y = p.x, p.y
local d = math.sqrt(x * x + y * y)
for i = 1, #data do
local item = data[i]
2018-07-23 22:50:11 +02:00
local price = item.price
2018-08-01 17:35:48 +02:00
local df = item.distance_factor
if df then
local min_price = item.min_price or 1
price = item.price - d * df
price = math.max(price, min_price)
end
2018-07-23 22:50:11 +02:00
Retailer.set_item(market_id, {name = item.name, price = price})
2018-07-23 22:50:11 +02:00
end
end
)
2018-07-20 23:01:34 +02:00
Public.firearm_magazine_ammo = {name = 'firearm-magazine', count = 200}
Public.piercing_rounds_magazine_ammo = {name = 'piercing-rounds-magazine', count = 200}
Public.uranium_rounds_magazine_ammo = {name = 'uranium-rounds-magazine', count = 200}
2018-07-27 01:36:25 +02:00
Public.artillery_shell_ammo = {name = 'artillery-shell', count = 15}
2018-07-20 23:01:34 +02:00
Public.light_oil_ammo = {name = 'light-oil', amount = 100}
Public.laser_turrent_power_source = {buffer_size = 2400000, power_production = 40000}
2018-07-23 22:50:11 +02:00
function Public.prepare_weighted_loot(loot)
local total = 0
local weights = {}
for _, v in ipairs(loot) do
total = total + v.weight
table.insert(weights, total)
end
weights.total = total
return weights
end
function Public.do_random_loot(entity, weights, loot)
if not entity.valid then
return
end
entity.operable = false
entity.destructible = false
local i = math.random() * weights.total
local index = table.binary_search(weights, i)
if (index < 0) then
index = bit32.bnot(index)
end
local stack = loot[index].stack
if not stack then
return
end
local df = stack.distance_factor
local count
if df then
local p = entity.position
local x, y = p.x, p.y
local d = math.sqrt(x * x + y * y)
count = stack.count + d * df
else
count = stack.count
end
entity.insert {name = stack.name, count = count}
end
2018-07-28 14:04:14 +02:00
function Public.do_random_fluid_loot(entity, weights, loot)
if not entity.valid then
return
end
entity.operable = false
entity.destructible = false
local i = math.random() * weights.total
local index = table.binary_search(weights, i)
if (index < 0) then
index = bit32.bnot(index)
end
local stack = loot[index].stack
if not stack then
return
end
local df = stack.distance_factor
local count
if df then
local p = entity.position
local x, y = p.x, p.y
local d = math.sqrt(x * x + y * y)
count = stack.count + d * df
else
count = stack.count
end
entity.fluidbox[1] = {name = stack.name, amount = count}
end
2019-01-10 00:29:51 +02:00
function Public.do_factory_loot(entity, weights, loot)
if not entity.valid then
return
end
entity.operable = false
entity.destructible = false
local i = math.random() * weights.total
local index = table.binary_search(weights, i)
if (index < 0) then
index = bit32.bnot(index)
end
local stack = loot[index].stack
if not stack then
return
end
local df = stack.distance_factor
local count
if df then
local p = entity.position
local x, y = p.x, p.y
local d = math.sqrt(x * x + y * y)
count = stack.count + d * df
else
count = stack.count
end
local name = stack.name
entity.set_recipe(name)
entity.get_output_inventory().insert {name = name, count = count}
end
2018-08-01 17:35:48 +02:00
local function coin_mined(event)
local stack = event.item_stack
if stack.name == 'coin' then
PlayerStats.change_coin_earned(event.player_index, stack.count)
end
end
2018-07-20 23:01:34 +02:00
Event.add(defines.events.on_tick, tick)
Event.add(defines.events.on_entity_died, remove_power_source)
2018-07-24 01:45:38 +02:00
Event.on_init(
function()
game.forces.neutral.recipes['steel-plate'].enabled = true
end
)
2018-08-01 17:35:48 +02:00
Event.add(defines.events.on_player_mined_item, coin_mined)
2018-07-13 12:47:38 +02:00
return Public