2019-01-19 18:34:29 +02:00
|
|
|
local Command = require 'utils.command'
|
2019-01-30 05:52:43 +02:00
|
|
|
local Ranks = require 'resources.ranks'
|
2019-01-19 18:34:29 +02:00
|
|
|
|
|
|
|
local insert = table.insert
|
|
|
|
|
2018-07-14 11:33:06 +02:00
|
|
|
local function get_mins(entities, tiles)
|
|
|
|
local min_x, min_y = math.huge, math.huge
|
|
|
|
|
|
|
|
for _, e in ipairs(entities) do
|
|
|
|
local p = e.position
|
|
|
|
local x, y = p.x, p.y
|
|
|
|
|
|
|
|
if x < min_x then
|
|
|
|
min_x = x
|
|
|
|
end
|
|
|
|
if y < min_y then
|
|
|
|
min_y = y
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
for _, e in ipairs(tiles) do
|
|
|
|
local p = e.position
|
|
|
|
local x, y = p.x, p.y
|
|
|
|
|
|
|
|
if x < min_x then
|
|
|
|
min_x = x
|
|
|
|
end
|
|
|
|
if y < min_y then
|
|
|
|
min_y = y
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return min_x, min_y
|
|
|
|
end
|
|
|
|
|
2019-01-19 18:34:29 +02:00
|
|
|
local function output(result, prepend, size, player)
|
2018-07-20 23:01:34 +02:00
|
|
|
local str = {prepend}
|
2019-01-19 18:34:29 +02:00
|
|
|
insert(str, '{\npart_size = ')
|
|
|
|
insert(str, size)
|
|
|
|
insert(str, ',\n')
|
2018-07-14 11:33:06 +02:00
|
|
|
|
|
|
|
for i, entry in pairs(result) do
|
2019-01-19 18:34:29 +02:00
|
|
|
insert(str, '[')
|
|
|
|
insert(str, i)
|
|
|
|
insert(str, '] = {')
|
2018-07-14 11:33:06 +02:00
|
|
|
|
|
|
|
local e = entry.entity
|
|
|
|
if e then
|
2019-01-19 18:34:29 +02:00
|
|
|
insert(str, 'entity = {')
|
2018-07-14 11:33:06 +02:00
|
|
|
|
2019-01-19 18:34:29 +02:00
|
|
|
insert(str, "name = '")
|
|
|
|
insert(str, e.name)
|
|
|
|
insert(str, "'")
|
2018-07-14 11:33:06 +02:00
|
|
|
|
|
|
|
local dir = e.direction
|
|
|
|
if dir then
|
2019-01-19 18:34:29 +02:00
|
|
|
insert(str, ', direction = ')
|
|
|
|
insert(str, dir)
|
2018-07-14 11:33:06 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
local offset = e.offset
|
|
|
|
if offset then
|
2019-01-19 18:34:29 +02:00
|
|
|
insert(str, ', offset = ')
|
|
|
|
insert(str, offset)
|
2018-07-14 11:33:06 +02:00
|
|
|
end
|
|
|
|
|
2019-01-19 18:34:29 +02:00
|
|
|
insert(str, '}')
|
2018-07-14 11:33:06 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
local t = entry.tile
|
|
|
|
if t then
|
|
|
|
if e then
|
2019-01-19 18:34:29 +02:00
|
|
|
insert(str, ', ')
|
2018-07-14 11:33:06 +02:00
|
|
|
end
|
2019-01-19 18:34:29 +02:00
|
|
|
insert(str, "tile = '")
|
|
|
|
insert(str, t.name)
|
|
|
|
insert(str, "'")
|
2018-07-14 11:33:06 +02:00
|
|
|
end
|
|
|
|
|
2019-01-19 18:34:29 +02:00
|
|
|
insert(str, '}')
|
|
|
|
insert(str, ',\n')
|
2018-07-14 11:33:06 +02:00
|
|
|
end
|
|
|
|
table.remove(str)
|
|
|
|
|
2019-01-19 18:34:29 +02:00
|
|
|
insert(str, '\n}')
|
2018-07-14 11:33:06 +02:00
|
|
|
|
|
|
|
str = table.concat(str)
|
|
|
|
|
2019-01-19 18:34:29 +02:00
|
|
|
game.write_file('bp.lua', str, false, player.index)
|
|
|
|
player.print('bp.lua written')
|
2018-07-14 11:33:06 +02:00
|
|
|
end
|
|
|
|
|
2019-01-19 18:34:29 +02:00
|
|
|
local function extract1(args, player)
|
|
|
|
local size = args.size
|
|
|
|
local cs = player.cursor_stack
|
2018-07-14 11:33:06 +02:00
|
|
|
|
|
|
|
if not (cs.valid_for_read and cs.name == 'blueprint' and cs.is_blueprint_setup()) then
|
2019-01-19 18:34:29 +02:00
|
|
|
player.print('invalid blueprint')
|
2018-07-14 11:33:06 +02:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
size = size or 6
|
|
|
|
|
|
|
|
local es = cs.get_blueprint_entities() or {}
|
|
|
|
local ts = cs.get_blueprint_tiles() or {}
|
|
|
|
|
|
|
|
local min_x, min_y = get_mins(es, ts)
|
|
|
|
|
|
|
|
min_x = 1 - math.ceil(min_x)
|
|
|
|
min_y = 1 - math.ceil(min_y)
|
|
|
|
|
|
|
|
local result = {}
|
|
|
|
for _, e in ipairs(es) do
|
|
|
|
local p = e.position
|
|
|
|
local x, y = p.x + min_x, p.y + min_y
|
|
|
|
local x2, y2 = math.ceil(x), math.ceil(y)
|
|
|
|
local i = (y2 - 1) * size + x2
|
|
|
|
|
|
|
|
local entry = result[i]
|
|
|
|
if not entry then
|
|
|
|
entry = {}
|
|
|
|
result[i] = entry
|
|
|
|
end
|
|
|
|
|
|
|
|
entry.entity = e
|
|
|
|
end
|
|
|
|
|
|
|
|
for _, e in ipairs(ts) do
|
|
|
|
local p = e.position
|
|
|
|
local x, y = p.x + min_x, p.y + min_y
|
|
|
|
x, y = math.ceil(x), math.ceil(y)
|
|
|
|
local i = (y - 1) * size + x
|
|
|
|
|
|
|
|
local entry = result[i]
|
|
|
|
if not entry then
|
|
|
|
entry = {}
|
|
|
|
result[i] = entry
|
|
|
|
end
|
|
|
|
|
|
|
|
entry.tile = e
|
|
|
|
end
|
2019-01-19 18:34:29 +02:00
|
|
|
output(result, 'ob.make_1_way', size, player)
|
2018-07-14 11:33:06 +02:00
|
|
|
end
|
|
|
|
|
2019-01-19 18:34:29 +02:00
|
|
|
local function extract4(args, player)
|
|
|
|
local size = args.size
|
|
|
|
local cs = player.cursor_stack
|
2018-07-14 11:33:06 +02:00
|
|
|
|
|
|
|
if not (cs.valid_for_read and cs.name == 'blueprint' and cs.is_blueprint_setup()) then
|
2019-01-19 18:34:29 +02:00
|
|
|
player.print('invalid blueprint')
|
2018-07-14 11:33:06 +02:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
size = size or 6
|
|
|
|
|
|
|
|
local es = cs.get_blueprint_entities() or {}
|
|
|
|
local ts = cs.get_blueprint_tiles() or {}
|
|
|
|
|
|
|
|
local min_x, min_y = get_mins(es, ts)
|
|
|
|
|
|
|
|
min_x = 1 - math.floor(min_x)
|
|
|
|
min_y = 1 - math.floor(min_y)
|
|
|
|
|
|
|
|
local result = {}
|
|
|
|
for _, e in ipairs(es) do
|
|
|
|
local p = e.position
|
|
|
|
local x, y = p.x + min_x, p.y + min_y
|
|
|
|
local x2, y2 = math.floor(x), math.floor(y)
|
|
|
|
local i = (y2 - 1) * size + x2
|
|
|
|
|
|
|
|
local offset = 0
|
|
|
|
if x2 ~= x then
|
|
|
|
offset = offset + 1
|
|
|
|
end
|
|
|
|
if y2 ~= y then
|
|
|
|
offset = offset + 2
|
|
|
|
end
|
|
|
|
|
|
|
|
if offset ~= 0 then
|
|
|
|
e.offset = offset
|
|
|
|
end
|
|
|
|
|
|
|
|
local entry = result[i]
|
|
|
|
if not entry then
|
|
|
|
entry = {}
|
|
|
|
result[i] = entry
|
|
|
|
end
|
|
|
|
|
|
|
|
entry.entity = e
|
|
|
|
end
|
|
|
|
|
2018-07-23 22:50:11 +02:00
|
|
|
for _, t in ipairs(ts) do
|
|
|
|
local p = t.position
|
2018-07-14 11:33:06 +02:00
|
|
|
local x, y = p.x + min_x, p.y + min_y
|
|
|
|
x, y = math.ceil(x), math.ceil(y)
|
|
|
|
local i = (y - 1) * size + x
|
|
|
|
|
|
|
|
local entry = result[i]
|
|
|
|
if not entry then
|
|
|
|
entry = {}
|
|
|
|
result[i] = entry
|
|
|
|
end
|
|
|
|
|
2018-07-23 22:50:11 +02:00
|
|
|
entry.tile = t
|
2018-07-14 11:33:06 +02:00
|
|
|
end
|
2019-01-19 18:34:29 +02:00
|
|
|
output(result, 'ob.make_4_way', size, player)
|
2018-07-14 11:33:06 +02:00
|
|
|
end
|
2019-01-19 18:34:29 +02:00
|
|
|
|
|
|
|
Command.add(
|
|
|
|
'extract1',
|
|
|
|
{
|
|
|
|
arguments = {'size'},
|
|
|
|
default_values = {size = 6},
|
2019-01-30 05:52:43 +02:00
|
|
|
required_rank = Ranks.admin
|
2019-01-19 18:34:29 +02:00
|
|
|
},
|
|
|
|
extract1
|
|
|
|
)
|
|
|
|
Command.add(
|
|
|
|
'extract4',
|
|
|
|
{
|
|
|
|
arguments = {'size'},
|
|
|
|
default_values = {size = 6},
|
2019-01-30 05:52:43 +02:00
|
|
|
required_rank = Ranks.admin
|
2019-01-19 18:34:29 +02:00
|
|
|
},
|
|
|
|
extract4
|
|
|
|
)
|