2016-12-11 19:24:52 +00:00
|
|
|
local class = require('class')
|
|
|
|
local DEFLATE = require('deflatelua')
|
|
|
|
local UI = require('ui')
|
|
|
|
local Point = require('point')
|
|
|
|
|
|
|
|
--[[
|
|
|
|
Loading and manipulating a schematic
|
|
|
|
--]]
|
|
|
|
|
|
|
|
local schematicMagic = 0x0a00
|
|
|
|
local gzipMagic = 0x1f8b
|
|
|
|
|
|
|
|
local Schematic = class()
|
|
|
|
function Schematic:init(args)
|
|
|
|
self.blocks = { }
|
|
|
|
self.damages = { }
|
|
|
|
self.originalBlocks = { }
|
|
|
|
self.x, self.y, self.z = 0, 0, 0
|
|
|
|
self.height = 0
|
|
|
|
self.index = 1
|
|
|
|
end
|
|
|
|
|
|
|
|
--[[
|
|
|
|
Credit to Orwell for the schematic file reader code
|
|
|
|
http://www.computercraft.info/forums2/index.php?/topic/1949-turtle-schematic-file-builder/
|
|
|
|
|
|
|
|
Some parts of the file reader code was modified from the original
|
|
|
|
--]]
|
2017-06-15 07:25:55 +00:00
|
|
|
|
2016-12-11 19:24:52 +00:00
|
|
|
function Schematic:discardBytes(h, n, spinner)
|
|
|
|
for i = 1,n do
|
|
|
|
h:readbyte()
|
|
|
|
if (i % 1000) == 0 then
|
|
|
|
spinner:spin()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-06-15 07:25:55 +00:00
|
|
|
|
2016-12-11 19:24:52 +00:00
|
|
|
function Schematic:readname(h)
|
|
|
|
local n1 = h:readbyte(h)
|
|
|
|
local n2 = h:readbyte(h)
|
2017-06-15 07:25:55 +00:00
|
|
|
|
2016-12-11 19:24:52 +00:00
|
|
|
if(n1 == nil or n2 == nil) then
|
|
|
|
return ""
|
|
|
|
end
|
2017-06-15 07:25:55 +00:00
|
|
|
|
2016-12-11 19:24:52 +00:00
|
|
|
local n = n1*256 + n2
|
2017-06-15 07:25:55 +00:00
|
|
|
|
2016-12-11 19:24:52 +00:00
|
|
|
local str = ""
|
|
|
|
for i=1,n do
|
|
|
|
local c = h:readbyte(h)
|
|
|
|
if c == nil then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
str = str .. string.char(c)
|
|
|
|
end
|
|
|
|
return str
|
|
|
|
end
|
2017-06-15 07:25:55 +00:00
|
|
|
|
2016-12-11 19:24:52 +00:00
|
|
|
function Schematic:parse(a, h, containsName, spinner)
|
2017-06-15 07:25:55 +00:00
|
|
|
|
2016-12-11 19:24:52 +00:00
|
|
|
if a==0 then
|
|
|
|
return
|
|
|
|
end
|
2017-06-15 07:25:55 +00:00
|
|
|
|
2016-12-11 19:24:52 +00:00
|
|
|
local name
|
|
|
|
if containsName then
|
|
|
|
name = self:readname(h)
|
|
|
|
end
|
|
|
|
|
|
|
|
if a==1 then
|
|
|
|
self:discardBytes(h, 1, spinner)
|
|
|
|
elseif a==2 then
|
|
|
|
local i1 = h:readbyte(h)
|
|
|
|
local i2 = h:readbyte(h)
|
|
|
|
local i = i1*256 + i2
|
|
|
|
if(name=="Height") then
|
|
|
|
--self.height = i
|
|
|
|
elseif (name=="Length") then
|
|
|
|
self.length = i
|
|
|
|
elseif (name=="Width") then
|
|
|
|
self.width = i
|
|
|
|
end
|
|
|
|
return 2
|
|
|
|
elseif a==3 then
|
|
|
|
self:discardBytes(h, 4, spinner)
|
|
|
|
return 4
|
|
|
|
elseif a==4 then
|
|
|
|
self:discardBytes(h,8, spinner)
|
|
|
|
return 8
|
|
|
|
elseif a==5 then
|
|
|
|
self:discardBytes(h,4, spinner)
|
|
|
|
return 4
|
|
|
|
elseif a==6 then
|
|
|
|
self:discardBytes(h,8, spinner)
|
|
|
|
elseif a==7 then
|
|
|
|
local i1 = h:readbyte(h)
|
|
|
|
local i2 = h:readbyte(h)
|
|
|
|
local i3 = h:readbyte(h)
|
|
|
|
local i4 = h:readbyte(h)
|
|
|
|
local i = bit.blshift(i1, 24) + bit.blshift(i2, 16) + bit.blshift(i3, 8) + i4
|
|
|
|
|
2017-07-24 02:37:07 +00:00
|
|
|
if name == "Blocks" then
|
2016-12-11 19:24:52 +00:00
|
|
|
for i = 1, i do
|
|
|
|
local id = h:readbyte(h)
|
2017-07-24 02:37:07 +00:00
|
|
|
if id > 0 then
|
|
|
|
table.insert(self.blocks, {
|
|
|
|
id = id,
|
|
|
|
index = i,
|
|
|
|
})
|
|
|
|
end
|
2016-12-11 19:24:52 +00:00
|
|
|
if (i % 1000) == 0 then
|
|
|
|
spinner:spin()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
elseif name == "Data" then
|
|
|
|
for i = 1, i do
|
|
|
|
local dmg = h:readbyte(h)
|
|
|
|
if dmg > 0 then
|
|
|
|
self.damages[i] = dmg
|
|
|
|
end
|
|
|
|
if (i % 1000) == 0 then
|
|
|
|
spinner:spin()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
else
|
|
|
|
self:discardBytes(h,i, spinner)
|
|
|
|
end
|
|
|
|
elseif a==8 then
|
|
|
|
local i1 = h:readbyte(h)
|
|
|
|
local i2 = h:readbyte(h)
|
|
|
|
local i = i1*256 + i2
|
|
|
|
self:discardBytes(h,i, spinner)
|
|
|
|
elseif a==9 then
|
|
|
|
local type = h:readbyte(h)
|
|
|
|
local i1 = h:readbyte(h)
|
|
|
|
local i2 = h:readbyte(h)
|
|
|
|
local i3 = h:readbyte(h)
|
|
|
|
local i4 = h:readbyte(h)
|
|
|
|
local i = bit.blshift(i1, 24) + bit.blshift(i2, 16) + bit.blshift(i3, 8) + i4
|
|
|
|
|
|
|
|
for j=1,i do
|
|
|
|
self:parse(type, h, false, spinner)
|
|
|
|
end
|
|
|
|
elseif a > 11 then
|
|
|
|
error('invalid tag')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
-- end http://www.computercraft.info/forums2/index.php?/topic/1949-turtle-schematic-file-builder/
|
2017-06-15 07:25:55 +00:00
|
|
|
|
2017-06-23 06:04:56 +00:00
|
|
|
function Schematic:copyBlocks(iblocks, oblocks, throttle)
|
2016-12-11 19:24:52 +00:00
|
|
|
for k,b in ipairs(iblocks) do
|
|
|
|
oblocks[k] = Util.shallowCopy(b)
|
2017-06-23 06:04:56 +00:00
|
|
|
if (k % 1000) == 0 then
|
|
|
|
throttle()
|
2016-12-11 19:24:52 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-06-15 07:25:55 +00:00
|
|
|
|
2017-06-23 06:04:56 +00:00
|
|
|
function Schematic:reload(throttle)
|
2016-12-11 19:24:52 +00:00
|
|
|
self.blocks = { }
|
2017-06-23 06:04:56 +00:00
|
|
|
self:copyBlocks(self.originalBlocks, self.blocks, throttle)
|
2017-06-15 07:25:55 +00:00
|
|
|
|
|
|
|
for _,ri in pairs(self.rowIndex) do
|
|
|
|
ri.loaded = false
|
2016-12-11 19:24:52 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function Schematic:getMagic(fh)
|
|
|
|
fh:open()
|
2017-06-15 07:25:55 +00:00
|
|
|
|
2016-12-11 19:24:52 +00:00
|
|
|
local magic = fh:readbyte() * 256 + fh:readbyte()
|
|
|
|
|
|
|
|
fh:close()
|
|
|
|
|
|
|
|
return magic
|
|
|
|
end
|
|
|
|
|
|
|
|
function Schematic:isCompressed(filename)
|
2017-06-15 07:25:55 +00:00
|
|
|
local h = fs.open(filename, "rb")
|
|
|
|
|
2016-12-11 19:24:52 +00:00
|
|
|
if not h then
|
|
|
|
error('unable to open: ' .. filename)
|
|
|
|
end
|
2017-06-15 07:25:55 +00:00
|
|
|
|
2016-12-11 19:24:52 +00:00
|
|
|
local magic = h.read() * 256 + h.read()
|
|
|
|
|
|
|
|
h.close()
|
|
|
|
|
|
|
|
return magic == gzipMagic
|
|
|
|
end
|
|
|
|
|
|
|
|
function Schematic:checkFileType(fh)
|
2017-06-15 07:25:55 +00:00
|
|
|
|
2016-12-11 19:24:52 +00:00
|
|
|
local magic = self:getMagic(fh)
|
|
|
|
if magic ~= schematicMagic then
|
|
|
|
error('Unknown file type')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local DiskFile = class()
|
|
|
|
function DiskFile:init(args)
|
|
|
|
Util.merge(self, args)
|
|
|
|
end
|
|
|
|
|
|
|
|
function DiskFile:open()
|
|
|
|
self.h = fs.open(self.filename, "rb")
|
|
|
|
if not self.h then
|
|
|
|
error('unable to open: ' .. self.filename)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
function DiskFile:readbyte()
|
|
|
|
return self.h.read()
|
|
|
|
end
|
|
|
|
function DiskFile:close()
|
|
|
|
self.h.close()
|
|
|
|
end
|
|
|
|
|
|
|
|
local MemoryFile = class()
|
|
|
|
function MemoryFile:init(args)
|
|
|
|
self.s = { }
|
|
|
|
self.i = 1
|
|
|
|
end
|
|
|
|
function MemoryFile:open(filename)
|
|
|
|
self.i = 1
|
|
|
|
end
|
|
|
|
function MemoryFile:close() end
|
|
|
|
|
|
|
|
function MemoryFile:readbyte()
|
|
|
|
local b = self.s[self.i]
|
|
|
|
self.i = self.i + 1
|
|
|
|
return b
|
|
|
|
end
|
|
|
|
|
|
|
|
function MemoryFile:write(b)
|
|
|
|
self.s[#self.s + 1] = b
|
|
|
|
end
|
|
|
|
|
|
|
|
function Schematic:decompress(ifname, spinner)
|
|
|
|
|
|
|
|
local ifh = fs.open(ifname, "rb")
|
|
|
|
if not ifh then
|
|
|
|
error('Unable to open ' .. ifname)
|
|
|
|
end
|
|
|
|
|
|
|
|
local mh = MemoryFile()
|
|
|
|
|
|
|
|
DEFLATE.gunzip({
|
|
|
|
input=function(...) spinner:spin() return ifh.read() end,
|
|
|
|
output=function(b) mh:write(b) end,
|
|
|
|
disable_crc=true
|
|
|
|
})
|
|
|
|
|
|
|
|
ifh.close()
|
|
|
|
|
|
|
|
spinner:stop()
|
|
|
|
|
|
|
|
return mh
|
|
|
|
end
|
2017-06-15 07:25:55 +00:00
|
|
|
|
2016-12-11 19:24:52 +00:00
|
|
|
function Schematic:loadpass(fh, spinner)
|
|
|
|
|
|
|
|
fh:open()
|
2017-06-15 07:25:55 +00:00
|
|
|
|
2016-12-11 19:24:52 +00:00
|
|
|
while true do
|
|
|
|
local a = fh:readbyte()
|
2017-06-15 07:25:55 +00:00
|
|
|
|
2016-12-11 19:24:52 +00:00
|
|
|
if not a then
|
|
|
|
break
|
|
|
|
end
|
|
|
|
self:parse(a, fh, true, spinner)
|
|
|
|
|
|
|
|
spinner:spin()
|
|
|
|
end
|
|
|
|
|
|
|
|
fh:close()
|
2017-06-15 07:25:55 +00:00
|
|
|
|
2017-07-24 02:37:07 +00:00
|
|
|
print('Assigning coords ')
|
|
|
|
local index = 1
|
|
|
|
for _, b in ipairs(self.blocks) do
|
|
|
|
while index < b.index do
|
|
|
|
self.x = self.x + 1
|
|
|
|
if self.x >= self.width then
|
|
|
|
self.x = 0
|
|
|
|
self.z = self.z + 1
|
|
|
|
end
|
|
|
|
if self.z >= self.length then
|
|
|
|
self.z = 0
|
|
|
|
self.y = self.y + 1
|
|
|
|
end
|
|
|
|
if self.y >= self.height then
|
|
|
|
self.height = self.y + 1
|
|
|
|
end
|
|
|
|
index = index + 1
|
|
|
|
end
|
|
|
|
b.x = self.x
|
|
|
|
b.y = self.y
|
|
|
|
b.z = self.z
|
|
|
|
spinner:spin()
|
|
|
|
end
|
|
|
|
|
2016-12-11 19:24:52 +00:00
|
|
|
self:assignDamages(spinner)
|
|
|
|
self.damages = nil
|
2017-06-15 07:25:55 +00:00
|
|
|
|
2017-06-23 06:04:56 +00:00
|
|
|
self:copyBlocks(self.blocks, self.originalBlocks, function() spinner:spin() end)
|
2016-12-11 19:24:52 +00:00
|
|
|
|
|
|
|
spinner:stop()
|
|
|
|
end
|
|
|
|
|
|
|
|
function Schematic:load(filename)
|
|
|
|
|
|
|
|
local cursorX, cursorY = term.getCursorPos()
|
|
|
|
local spinner = UI.Spinner({
|
|
|
|
x = UI.term.width,
|
|
|
|
y = cursorY - 1
|
|
|
|
})
|
|
|
|
local f
|
2017-06-15 07:25:55 +00:00
|
|
|
|
2016-12-11 19:24:52 +00:00
|
|
|
if self:isCompressed(filename) then
|
|
|
|
local originalFile = filename
|
|
|
|
filename = originalFile .. '.uncompressed'
|
|
|
|
|
|
|
|
if not fs.exists(filename) then
|
|
|
|
print('Decompressing')
|
|
|
|
f = self:decompress(originalFile, spinner)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
self.filename = string.match(filename, '([^/]+)$')
|
|
|
|
|
|
|
|
if not f then
|
|
|
|
f = DiskFile({ filename = filename })
|
|
|
|
end
|
|
|
|
|
|
|
|
self:checkFileType(f)
|
|
|
|
|
2017-07-24 02:37:07 +00:00
|
|
|
print('Loading blocks ')
|
2016-12-11 19:24:52 +00:00
|
|
|
self:loadpass(f, spinner)
|
|
|
|
|
2017-06-15 07:25:55 +00:00
|
|
|
self.rowIndex = { }
|
|
|
|
for k,b in ipairs(self.blocks) do
|
|
|
|
local ri = self.rowIndex[b.y]
|
|
|
|
if not ri then
|
|
|
|
self.rowIndex[b.y] = { s = k, e = k }
|
|
|
|
else
|
|
|
|
ri.e = k
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
self.cache = Util.readTable('usr/builder/' .. self.filename .. '.cache') or { }
|
2016-12-11 19:24:52 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function Schematic:assignDamages(spinner)
|
|
|
|
print('Assigning damages')
|
|
|
|
|
|
|
|
for _,b in pairs(self.blocks) do
|
|
|
|
b.dmg = self.damages[b.index] or 0
|
2017-07-24 02:37:07 +00:00
|
|
|
spinner:spin()
|
2016-12-11 19:24:52 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function Schematic:findIndexAt(x, z, y)
|
|
|
|
if y < 0 then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
local ri = self.rowIndex[y]
|
|
|
|
if ri then
|
|
|
|
for i = ri.s, ri.e do
|
|
|
|
local b = self.blocks[i]
|
|
|
|
if b.x == x and b.z == z and b.y == y then
|
|
|
|
if b.id == 'minecraft:air' then
|
|
|
|
-- this will definitely screw up placement order if a substition is made with air after starting
|
|
|
|
-- as blocks will be placed differently and could have a different heading
|
|
|
|
break
|
|
|
|
end
|
|
|
|
return i, b
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function Schematic:findBlockAtSide(b, side)
|
|
|
|
local hi = turtle.getHeadingInfo(side)
|
|
|
|
local index = self:findIndexAt(b.x + hi.xd, b.z + hi.zd, b.y + hi.yd)
|
|
|
|
if index then
|
|
|
|
return self.blocks[index] -- could be better
|
|
|
|
end
|
|
|
|
end
|
2017-06-15 07:25:55 +00:00
|
|
|
|
|
|
|
function Schematic:addPlacementChain(chains, chain)
|
2016-12-11 19:24:52 +00:00
|
|
|
local t = { }
|
|
|
|
for _,v in ipairs(chain) do
|
|
|
|
local k = self:findIndexAt(v.x, v.z, v.y)
|
|
|
|
if k then
|
|
|
|
local b = self.blocks[k] -- could be better
|
|
|
|
b.index = v.y * self.width * self.length + v.z * self.width + v.x + 1
|
|
|
|
table.insert(t, b)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if #t > 1 then
|
|
|
|
local keys = { }
|
|
|
|
for _,b in pairs(t) do
|
|
|
|
keys[b.index] = true
|
|
|
|
end
|
2017-06-15 07:25:55 +00:00
|
|
|
table.insert(chains, {
|
2016-12-11 19:24:52 +00:00
|
|
|
blocks = t,
|
|
|
|
keys = keys
|
|
|
|
})
|
|
|
|
end
|
|
|
|
end
|
2017-06-15 07:25:55 +00:00
|
|
|
|
|
|
|
function Schematic:bestSide(b, chains, ...)
|
2016-12-11 19:24:52 +00:00
|
|
|
local directions = { ... }
|
|
|
|
local blocks = { }
|
2017-06-15 07:25:55 +00:00
|
|
|
|
2016-12-11 19:24:52 +00:00
|
|
|
for k,d in pairs(directions) do
|
|
|
|
local hi = turtle.getHeadingInfo(d)
|
|
|
|
local sb = self:findIndexAt(b.x - hi.xd, b.z - hi.zd, b.y)
|
|
|
|
if not sb then
|
|
|
|
b.heading = turtle.getHeadingInfo(d).heading
|
|
|
|
b.direction = d .. '-block'
|
|
|
|
return
|
|
|
|
end
|
|
|
|
blocks[k] = {
|
|
|
|
b = self.blocks[sb],
|
|
|
|
hi = hi,
|
|
|
|
d = d
|
|
|
|
}
|
|
|
|
end
|
2017-06-15 07:25:55 +00:00
|
|
|
|
2016-12-11 19:24:52 +00:00
|
|
|
local bestBlock
|
|
|
|
for _,sb in ipairs(blocks) do
|
|
|
|
if not sb.b.direction then -- could be better
|
|
|
|
bestBlock = sb
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
2017-06-15 07:25:55 +00:00
|
|
|
|
2016-12-11 19:24:52 +00:00
|
|
|
if not bestBlock then
|
|
|
|
local sideDirections = {
|
|
|
|
[ 'east-block' ] = 'east',
|
|
|
|
[ 'south-block' ] = 'south',
|
|
|
|
[ 'west-block' ] = 'west',
|
|
|
|
[ 'north-block' ] = 'north'
|
|
|
|
}
|
|
|
|
for _,sb in ipairs(blocks) do
|
|
|
|
if not bestBlock then
|
|
|
|
bestBlock = sb
|
|
|
|
end
|
|
|
|
if not sideDirections[sb.b.direction] then
|
|
|
|
bestBlock = sb
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-06-15 07:25:55 +00:00
|
|
|
|
2016-12-11 19:24:52 +00:00
|
|
|
local hi = bestBlock.hi
|
|
|
|
b.heading = hi.heading -- ?????????????????????????????????
|
|
|
|
b.direction = bestBlock.d .. '-block'
|
2017-06-15 07:25:55 +00:00
|
|
|
self:addPlacementChain(chains, {
|
2016-12-11 19:24:52 +00:00
|
|
|
{ x = b.x, z = b.z, y = b.y },
|
|
|
|
{ x = b.x - hi.xd, z = b.z - hi.zd, y = b.y }
|
|
|
|
})
|
|
|
|
end
|
2017-06-15 07:25:55 +00:00
|
|
|
|
2017-06-23 06:04:56 +00:00
|
|
|
function Schematic:bestFlipSide(b, chains)
|
|
|
|
-- If there is a block to place this one against
|
|
|
|
|
|
|
|
local directions = {
|
|
|
|
[ 'east-block-flip' ] = 'east',
|
|
|
|
[ 'west-block-flip' ] = 'west',
|
|
|
|
[ 'north-block-flip' ] = 'north',
|
|
|
|
[ 'south-block-flip' ] = 'south',
|
|
|
|
}
|
|
|
|
|
|
|
|
local d = directions[b.direction]
|
|
|
|
local hi = turtle.getHeadingInfo(d)
|
|
|
|
local _, fb = self:findIndexAt(b.x + hi.xd, b.z + hi.zd, b.y)
|
|
|
|
|
|
|
|
if fb then
|
|
|
|
self:addPlacementChain(chains, {
|
|
|
|
{ x = b.x + hi.xd, z = b.z + hi.zd, y = b.y }, -- block we are placing against
|
|
|
|
{ x = b.x, z = b.z, y = b.y }, -- the block (or torch, etc)
|
|
|
|
{ x = b.x - hi.xd, z = b.z - hi.zd, y = b.y }, -- room for the turtle
|
|
|
|
})
|
|
|
|
b.direction = d .. '-block'
|
|
|
|
else
|
|
|
|
self:addPlacementChain(chains, {
|
|
|
|
{ x = b.x, z = b.z, y = b.y }, -- the block (or torch, etc)
|
|
|
|
{ x = b.x - hi.xd, z = b.z - hi.zd, y = b.y }, -- room for the turtle
|
|
|
|
{ x = b.x + hi.xd, z = b.z + hi.zd, y = b.y }, -- block we are placing against
|
|
|
|
})
|
|
|
|
b.direction = turtle.getHeadingInfo((hi.heading + 2) % 4).direction .. '-block'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-06-15 07:25:55 +00:00
|
|
|
function Schematic:bestOfTwoSides(b, chains, side1, side2) -- could be better
|
2016-12-11 19:24:52 +00:00
|
|
|
|
|
|
|
local sb
|
|
|
|
local fb = b -- first block
|
|
|
|
local lb = b -- last block
|
|
|
|
local od = b.direction -- original direction
|
|
|
|
|
|
|
|
-- find the last block in the row with the same two-sided direction
|
|
|
|
while true do
|
|
|
|
sb = self:findBlockAtSide(lb, side2)
|
|
|
|
if not sb or sb.direction ~= b.direction then
|
|
|
|
break
|
|
|
|
end
|
|
|
|
lb = sb
|
|
|
|
end
|
|
|
|
|
|
|
|
-- find the first block
|
|
|
|
while true do
|
|
|
|
sb = self:findBlockAtSide(fb, side1)
|
|
|
|
if not sb or sb.direction ~= b.direction then
|
|
|
|
break
|
|
|
|
end
|
|
|
|
fb = sb
|
|
|
|
end
|
|
|
|
|
|
|
|
-- set the placement order to side1 -> side2
|
|
|
|
if fb ~= lb then -- only 1 block
|
|
|
|
|
|
|
|
local pc = { } -- placementChain
|
|
|
|
b = fb
|
|
|
|
|
|
|
|
while true do
|
|
|
|
|
|
|
|
table.insert(pc, { x = b.x, z = b.z, y = b.y })
|
|
|
|
|
|
|
|
b.direction = side1 .. '-block'
|
|
|
|
b.heading = turtle.getHeadingInfo(side1).heading
|
|
|
|
|
|
|
|
if b == lb then
|
|
|
|
break
|
|
|
|
end
|
|
|
|
|
|
|
|
b = self:findBlockAtSide(b, side2)
|
|
|
|
end
|
|
|
|
|
2017-06-15 07:25:55 +00:00
|
|
|
self:addPlacementChain(chains, pc)
|
2016-12-11 19:24:52 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
-- can we place the first block from the side (instead of using piston) ?
|
|
|
|
sb = self:findBlockAtSide(fb, side1)
|
|
|
|
if not sb then
|
|
|
|
local ub = self:findBlockAtSide(fb, 'down')
|
|
|
|
if not ub then
|
|
|
|
fb.direction = side1 .. '-block'
|
|
|
|
fb.heading = turtle.getHeadingInfo(side1).heading
|
|
|
|
else
|
|
|
|
fb.direction = od
|
|
|
|
end
|
|
|
|
else -- really should use placement chain
|
|
|
|
fb.direction = od
|
|
|
|
end
|
|
|
|
|
|
|
|
-- can we place the last block from the side (instead of using piston) ?
|
|
|
|
sb = self:findBlockAtSide(lb, side2)
|
|
|
|
if not sb then
|
|
|
|
local ub = self:findBlockAtSide(lb, 'down')
|
|
|
|
if not ub then
|
|
|
|
lb.direction = side1 .. '-block'
|
|
|
|
lb.heading = turtle.getHeadingInfo(side1).heading
|
|
|
|
else
|
|
|
|
fb.direction = od
|
|
|
|
end
|
|
|
|
else
|
|
|
|
lb.direction = od
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
2017-06-15 07:25:55 +00:00
|
|
|
|
2016-12-11 19:24:52 +00:00
|
|
|
-- Determine the best way to place each block
|
2017-06-15 07:25:55 +00:00
|
|
|
function Schematic:determineBlockPlacement(y)
|
2016-12-11 19:24:52 +00:00
|
|
|
|
|
|
|
-- NOTE: blocks are evaluated top to bottom
|
|
|
|
|
2017-06-15 07:25:55 +00:00
|
|
|
print('Processing level ' .. y)
|
|
|
|
|
2016-12-11 19:24:52 +00:00
|
|
|
local spinner = UI.Spinner({
|
|
|
|
x = 1,
|
|
|
|
spinSymbols = { 'o.....', '.o....', '..o...', '...o..', '....o.', '.....o' }
|
|
|
|
})
|
|
|
|
local stairDownDirections = {
|
|
|
|
[ 'north-down' ] = 'north',
|
|
|
|
[ 'south-down' ] = 'south',
|
|
|
|
[ 'east-down' ] = 'east',
|
|
|
|
[ 'west-down' ] = 'west'
|
|
|
|
}
|
|
|
|
local stairUpDirections = {
|
|
|
|
[ 'east-up' ] = { 'east', 'east-block', 1, 0, 'west-block' },
|
|
|
|
[ 'west-up' ] = { 'west', 'west-block', -1, 0, 'east-block' },
|
|
|
|
[ 'north-up' ] = { 'north', 'north-block', 0, -1, 'south-block' },
|
|
|
|
[ 'south-up' ] = { 'south', 'south-block', 0, 1, 'north-block' }
|
|
|
|
}
|
|
|
|
local twoSideDirections = {
|
|
|
|
[ 'east-west-block' ] = true,
|
|
|
|
[ 'north-south-block' ] = true,
|
|
|
|
}
|
|
|
|
local directions = {
|
|
|
|
[ 'north' ] = 'north',
|
|
|
|
[ 'south' ] = 'south',
|
|
|
|
[ 'east' ] = 'east',
|
|
|
|
[ 'west' ] = 'west',
|
|
|
|
}
|
|
|
|
local blockDirections = {
|
|
|
|
[ 'east-block' ] = 'east',
|
|
|
|
[ 'south-block' ] = 'south',
|
|
|
|
[ 'west-block' ] = 'west',
|
|
|
|
[ 'north-block' ] = 'north',
|
|
|
|
}
|
|
|
|
local doorDirections = {
|
|
|
|
[ 'east-door' ] = 'east',
|
|
|
|
[ 'south-door' ] = 'south',
|
|
|
|
[ 'west-door' ] = 'west',
|
|
|
|
[ 'north-door' ] = 'north',
|
|
|
|
}
|
|
|
|
local vineDirections = {
|
|
|
|
[ 'east-block-vine' ] = 'east-block',
|
|
|
|
[ 'south-block-vine' ] = 'south-block',
|
|
|
|
[ 'west-block-vine' ] = 'west-block',
|
|
|
|
[ 'north-block-vine' ] = 'north-block'
|
|
|
|
}
|
2017-06-23 06:04:56 +00:00
|
|
|
local flipDirections = {
|
|
|
|
[ 'east-block-flip' ] = 'east-block',
|
|
|
|
[ 'south-block-flip' ] = 'south-block',
|
|
|
|
[ 'west-block-flip' ] = 'west-block',
|
|
|
|
[ 'north-block-flip' ] = 'north-block'
|
|
|
|
}
|
2017-06-15 07:25:55 +00:00
|
|
|
|
2016-12-11 19:24:52 +00:00
|
|
|
local dirtyBlocks = {}
|
|
|
|
local dirtyBlocks2 = {}
|
2017-06-15 07:25:55 +00:00
|
|
|
local chains = {}
|
2016-12-11 19:24:52 +00:00
|
|
|
|
2017-06-15 07:25:55 +00:00
|
|
|
local ri = self.rowIndex[y]
|
2017-06-23 06:04:56 +00:00
|
|
|
if not ri then
|
|
|
|
ri = { s = -1, e = -2 }
|
|
|
|
self.rowIndex[y] = ri
|
|
|
|
end
|
|
|
|
|
2017-06-15 07:25:55 +00:00
|
|
|
for k = ri.s, ri.e do
|
|
|
|
local b = self.blocks[k]
|
2016-12-11 19:24:52 +00:00
|
|
|
local d = b.direction
|
|
|
|
|
|
|
|
if d then
|
|
|
|
if vineDirections[d] then
|
|
|
|
local _, aboveBlock = self:findIndexAt(b.x, b.z, b.y+1)
|
|
|
|
|
|
|
|
if aboveBlock and aboveBlock.id == b.id and aboveBlock.dmg == b.dmg and aboveBlock.direction == d then
|
|
|
|
-- only need to place top vine
|
|
|
|
b.id = 'minecraft:air'
|
|
|
|
b.dmg = 0
|
|
|
|
b.direction = nil
|
|
|
|
else
|
|
|
|
b.direction = vineDirections[d]
|
|
|
|
table.insert(dirtyBlocks, b)
|
|
|
|
end
|
|
|
|
elseif twoSideDirections[d] then
|
|
|
|
table.insert(dirtyBlocks2, b)
|
|
|
|
else
|
|
|
|
table.insert(dirtyBlocks, b)
|
|
|
|
end
|
|
|
|
spinner:spin(#dirtyBlocks + #dirtyBlocks2 .. ' blocks remaining ')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Util.filterInplace(dirtyBlocks, function(b) return b.id ~= 'minecraft:air' end)
|
|
|
|
|
|
|
|
-- remove directional info from slabs where possible
|
|
|
|
-- iterate backwards to process top planes first
|
|
|
|
for k = #dirtyBlocks, 1, -1 do
|
|
|
|
local b = dirtyBlocks[k]
|
|
|
|
local d = b.direction
|
|
|
|
|
|
|
|
if d == 'top' then
|
|
|
|
-- slab occupying top of voxel
|
|
|
|
-- can be placed from the top if there is no block below
|
|
|
|
local belowBlock = self:findIndexAt(b.x, b.z, b.y-1)
|
|
|
|
if not belowBlock then
|
|
|
|
b.direction = nil
|
|
|
|
table.remove(dirtyBlocks, k)
|
|
|
|
end
|
|
|
|
elseif d == 'bottom' then
|
2017-07-02 14:05:10 +00:00
|
|
|
b.bottom = true -- flag this as a bottom block
|
2016-12-11 19:24:52 +00:00
|
|
|
local _,db = self:findIndexAt(b.x, b.z, b.y-1)
|
|
|
|
if db then
|
|
|
|
if not db.direction or db.direction ~= 'bottom' then
|
|
|
|
-- not a slab below, ok to place from above
|
2017-07-02 14:05:10 +00:00
|
|
|
if not db.bottom then
|
|
|
|
b.direction = nil
|
|
|
|
end
|
2016-12-11 19:24:52 +00:00
|
|
|
end
|
|
|
|
-- it is a slab below - must be pistoned
|
|
|
|
table.remove(dirtyBlocks, k)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
spinner:spin(#dirtyBlocks + #dirtyBlocks2 .. ' blocks remaining ')
|
|
|
|
end
|
|
|
|
|
|
|
|
-- iterate through the directional blocks setting the placement strategy
|
|
|
|
while #dirtyBlocks > 0 do
|
|
|
|
local b = table.remove(dirtyBlocks)
|
|
|
|
local d = b.direction or ''
|
|
|
|
|
|
|
|
spinner:spin(#dirtyBlocks + #dirtyBlocks2 .. ' blocks remaining ')
|
2017-06-15 07:25:55 +00:00
|
|
|
|
2016-12-11 19:24:52 +00:00
|
|
|
if directions[d] then
|
|
|
|
b.heading = turtle.getHeadingInfo(directions[d]).heading
|
|
|
|
end
|
2017-06-15 07:25:55 +00:00
|
|
|
|
2016-12-11 19:24:52 +00:00
|
|
|
if doorDirections[d] then
|
2017-06-15 07:25:55 +00:00
|
|
|
|
2016-12-11 19:24:52 +00:00
|
|
|
local hi = turtle.getHeadingInfo(doorDirections[d])
|
|
|
|
b.heading = hi.heading
|
|
|
|
b.twoHigh = true
|
2017-06-15 07:25:55 +00:00
|
|
|
|
|
|
|
self:addPlacementChain(chains, {
|
2016-12-11 19:24:52 +00:00
|
|
|
{ x = b.x, z = b.z, y = b.y },
|
|
|
|
{ x = b.x - hi.xd, z = b.z - hi.zd, y = b.y },
|
|
|
|
})
|
|
|
|
end
|
2017-06-15 07:25:55 +00:00
|
|
|
|
2016-12-11 19:24:52 +00:00
|
|
|
if stairDownDirections[d] then
|
|
|
|
if not self:findIndexAt(b.x, b.z, b.y-1) then
|
|
|
|
b.direction = stairDownDirections[b.direction]
|
|
|
|
b.heading = turtle.getHeadingInfo(b.direction).heading
|
|
|
|
else
|
|
|
|
b.heading = turtle.getHeadingInfo(stairDownDirections[b.direction]).heading
|
|
|
|
end
|
|
|
|
end
|
2017-06-15 07:25:55 +00:00
|
|
|
|
2016-12-11 19:24:52 +00:00
|
|
|
if d == 'bottom' then
|
|
|
|
-- slab occupying bottom of voxel
|
|
|
|
-- can be placed from top if a block is below
|
|
|
|
-- otherwise, needs to be placed from side
|
|
|
|
|
|
|
|
-- except... if the block below is a slab :(
|
|
|
|
--local _,db = self:findIndexAt(b.x, b.z, b.y-1)
|
|
|
|
--if not db then
|
|
|
|
-- no block below, place from side
|
|
|
|
|
|
|
|
-- took care of all other cases above
|
2017-06-15 07:25:55 +00:00
|
|
|
self:bestSide(b, chains, 'east', 'south', 'west', 'north')
|
2016-12-11 19:24:52 +00:00
|
|
|
|
|
|
|
-- elseif not db.direction or db.direction ~= 'bottom' then
|
|
|
|
-- not a slab below, ok to place from above
|
|
|
|
-- b.direction = nil
|
|
|
|
--end
|
|
|
|
-- otherwise, builder will piston it in from above
|
|
|
|
|
|
|
|
elseif stairUpDirections[d] then
|
|
|
|
-- a directional stair
|
|
|
|
-- turtle can place correctly from above if there is a block below
|
|
|
|
-- otherwise, the turtle must place the block from the same plane
|
|
|
|
-- against another block
|
|
|
|
-- if no block to place against (from side) then the turtle must place from
|
|
|
|
-- the other side
|
|
|
|
--
|
|
|
|
-- Stair bug in 1.7 - placing a stair southward doesn't respect the turtle's direction
|
|
|
|
-- all other directions are fine
|
|
|
|
-- any stair southwards that can't be placed against another block must be pistoned
|
|
|
|
local sd = stairUpDirections[d]
|
2017-06-15 07:25:55 +00:00
|
|
|
|
2016-12-11 19:24:52 +00:00
|
|
|
if self:findIndexAt(b.x, b.z, b.y-1) then
|
|
|
|
-- there's a block below
|
|
|
|
b.direction = sd[1]
|
|
|
|
b.heading = turtle.getHeadingInfo(b.direction).heading
|
|
|
|
else
|
|
|
|
local _,pb = self:findIndexAt(b.x + sd[3], b.z + sd[4], b.y)
|
|
|
|
if pb and pb.direction ~= sd[5] then
|
|
|
|
-- place stair against another block (that's not relying on this block to be down first)
|
|
|
|
d = sd[2] -- fall through to the blockDirections code below
|
|
|
|
b.direction = sd[2]
|
|
|
|
else
|
|
|
|
b.heading = (turtle.getHeadingInfo(sd[1]).heading + 2) % 4
|
|
|
|
end
|
|
|
|
end
|
2017-06-23 06:04:56 +00:00
|
|
|
elseif flipDirections[d] then
|
|
|
|
self:bestFlipSide(b, chains)
|
2016-12-11 19:24:52 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
if blockDirections[d] then
|
|
|
|
-- placing a block from the side
|
|
|
|
local hi = turtle.getHeadingInfo(blockDirections[d])
|
|
|
|
b.heading = hi.heading
|
2017-06-15 07:25:55 +00:00
|
|
|
self:addPlacementChain(chains, {
|
2016-12-11 19:24:52 +00:00
|
|
|
{ x = b.x + hi.xd, z = b.z + hi.zd, y = b.y }, -- block we are placing against
|
|
|
|
{ x = b.x, z = b.z, y = b.y }, -- the block (or torch, etc)
|
|
|
|
{ x = b.x - hi.xd, z = b.z - hi.zd, y = b.y } -- room for the turtle
|
|
|
|
})
|
|
|
|
end
|
|
|
|
end
|
2017-06-15 07:25:55 +00:00
|
|
|
|
2016-12-11 19:24:52 +00:00
|
|
|
-- pass 3
|
|
|
|
while #dirtyBlocks2 > 0 do
|
|
|
|
local b = table.remove(dirtyBlocks2)
|
|
|
|
local d = b.direction
|
|
|
|
|
|
|
|
spinner:spin(#dirtyBlocks2 .. ' blocks remaining ')
|
|
|
|
|
|
|
|
if d == 'east-west-block' then
|
2017-06-15 07:25:55 +00:00
|
|
|
self:bestOfTwoSides(b, chains, 'east', 'west')
|
2016-12-11 19:24:52 +00:00
|
|
|
elseif d == 'north-south-block' then
|
2017-06-15 07:25:55 +00:00
|
|
|
self:bestOfTwoSides(b, chains, 'north', 'south')
|
2016-12-11 19:24:52 +00:00
|
|
|
end
|
|
|
|
end
|
2017-06-15 07:25:55 +00:00
|
|
|
|
2016-12-11 19:24:52 +00:00
|
|
|
term.clearLine()
|
2017-06-15 07:25:55 +00:00
|
|
|
|
|
|
|
self:setPlacementOrder(spinner, chains)
|
|
|
|
local plane = self:optimizeRoute(spinner, y)
|
|
|
|
|
2017-06-23 06:04:56 +00:00
|
|
|
term.clearLine()
|
2017-06-15 07:25:55 +00:00
|
|
|
spinner:stop()
|
|
|
|
|
|
|
|
for k,b in ipairs(plane) do
|
|
|
|
self.blocks[ri.s + k - 1] = b
|
|
|
|
end
|
2016-12-11 19:24:52 +00:00
|
|
|
end
|
2017-06-15 07:25:55 +00:00
|
|
|
|
|
|
|
function Schematic:getComputedBlock(i)
|
|
|
|
local b = self.blocks[i]
|
|
|
|
|
|
|
|
-- has this level been computed ?
|
|
|
|
if not self.rowIndex[b.y].loaded then
|
|
|
|
-- compute each level up til this one (unless saved in cache)
|
2017-07-24 02:37:07 +00:00
|
|
|
|
2017-06-15 07:25:55 +00:00
|
|
|
for y = 0, b.y - 1 do
|
|
|
|
if not self.cache[y] then
|
|
|
|
self:determineBlockPlacement(y)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
self:determineBlockPlacement(b.y)
|
|
|
|
-- get the block now at the computed location
|
|
|
|
b = self.blocks[i]
|
|
|
|
end
|
|
|
|
return b
|
|
|
|
end
|
|
|
|
|
2016-12-11 19:24:52 +00:00
|
|
|
-- set the order for block dependencies
|
2017-06-15 07:25:55 +00:00
|
|
|
function Schematic:setPlacementOrder(spinner, placementChains)
|
|
|
|
|
2016-12-11 19:24:52 +00:00
|
|
|
local cursorX, cursorY = term.getCursorPos()
|
2017-06-15 07:25:55 +00:00
|
|
|
|
2016-12-11 19:24:52 +00:00
|
|
|
-- optimize for overlapping check
|
2017-06-15 07:25:55 +00:00
|
|
|
for _,chain in pairs(placementChains) do
|
2016-12-11 19:24:52 +00:00
|
|
|
for index,_ in pairs(chain.keys) do
|
|
|
|
if not chain.startRow or (index < chain.startRow) then
|
|
|
|
chain.startRow = index
|
|
|
|
end
|
|
|
|
if not chain.endRow or (index > chain.endRow) then
|
|
|
|
chain.endRow = index
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-06-15 07:25:55 +00:00
|
|
|
|
2016-12-11 19:24:52 +00:00
|
|
|
local function groupOverlappingChains(t, groupedChain, chain, spinner)
|
|
|
|
local found = true
|
2017-06-15 07:25:55 +00:00
|
|
|
|
2016-12-11 19:24:52 +00:00
|
|
|
local function overlaps(chain1, chain2)
|
|
|
|
if chain1.startRow > chain2.endRow or
|
|
|
|
chain2.startRow > chain1.endRow then
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
for k,_ in pairs(chain1.keys) do
|
|
|
|
if chain2.keys[k] then
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-06-15 07:25:55 +00:00
|
|
|
|
2016-12-11 19:24:52 +00:00
|
|
|
while found do
|
|
|
|
found = false
|
|
|
|
for k, v in pairs(t) do
|
|
|
|
local o = overlaps(chain, v)
|
|
|
|
if o then
|
|
|
|
table.remove(t, k)
|
|
|
|
table.insert(groupedChain, v)
|
|
|
|
groupOverlappingChains(t, groupedChain, v, spinner)
|
|
|
|
spinner:spin()
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- group together any placement chains with overlapping blocks
|
|
|
|
local groupedChains = {}
|
2017-06-15 07:25:55 +00:00
|
|
|
while #placementChains > 0 do
|
2016-12-11 19:24:52 +00:00
|
|
|
local groupedChain = {}
|
2017-06-15 07:25:55 +00:00
|
|
|
local chain = table.remove(placementChains)
|
2016-12-11 19:24:52 +00:00
|
|
|
table.insert(groupedChain, chain)
|
|
|
|
table.insert(groupedChains, groupedChain)
|
2017-06-15 07:25:55 +00:00
|
|
|
groupOverlappingChains(placementChains, groupedChain, chain, spinner)
|
|
|
|
spinner:spin('chains: ' .. #groupedChains .. ' ' .. #placementChains .. ' ')
|
2016-12-11 19:24:52 +00:00
|
|
|
end
|
2017-06-15 07:25:55 +00:00
|
|
|
|
2016-12-11 19:24:52 +00:00
|
|
|
--Logger.log('schematic', 'groups: ' .. #groupedChains)
|
|
|
|
--Logger.setFileLogging('chains')
|
|
|
|
|
|
|
|
local function mergeChains(chains)
|
|
|
|
|
|
|
|
--[[
|
|
|
|
Logger.debug('---------------')
|
|
|
|
Logger.log('schematic', 'mergeChains: ' .. #chains)
|
|
|
|
for _,chain in ipairs(chains) do
|
|
|
|
Logger.log('schematic', chain)
|
|
|
|
for _,e in ipairs(chain) do
|
|
|
|
Logger.log('schematic', string.format('%d:%d:%d %s %d:%d',
|
|
|
|
e.block.x, e.block.z, e.block.y, tostring(e.block.direction), e.block.id, e.block.dmg))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
]]--
|
|
|
|
|
|
|
|
local masterChain = table.remove(chains)
|
|
|
|
|
|
|
|
--[[ it's something like this:
|
|
|
|
|
|
|
|
A chain B chain result
|
|
|
|
1 1
|
|
|
|
2 -------- 2 2
|
|
|
|
3 3
|
|
|
|
4 4
|
|
|
|
5 5
|
|
|
|
6 -------- 6 6
|
|
|
|
7 7
|
|
|
|
--]]
|
|
|
|
local function splice(chain1, chain2)
|
|
|
|
for k,v in ipairs(chain1) do
|
|
|
|
for k2,v2 in ipairs(chain2) do
|
|
|
|
if v == v2 then
|
|
|
|
local index = k
|
|
|
|
local dupe
|
|
|
|
for i = k2-1, 1, -1 do
|
|
|
|
dupe = false
|
|
|
|
-- traverse back through the first chain aligning on matches
|
|
|
|
for j = index-1, 1, -1 do
|
|
|
|
if chain1[j] == chain2[i] then
|
|
|
|
index = j
|
|
|
|
dupe = true
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if not dupe then
|
|
|
|
table.insert(chain1, index, chain2[i])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
index = k+1
|
|
|
|
for i = k2+1, #chain2, 1 do
|
|
|
|
dupe = false
|
|
|
|
for j = index, #chain1, 1 do
|
|
|
|
if chain1[j] == chain2[i] then
|
|
|
|
index = j
|
|
|
|
dupe = true
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if not dupe then
|
|
|
|
table.insert(chain1, index, chain2[i])
|
|
|
|
end
|
|
|
|
index = index + 1
|
|
|
|
end
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-06-15 07:25:55 +00:00
|
|
|
|
2016-12-11 19:24:52 +00:00
|
|
|
while #chains > 0 do
|
|
|
|
for k,chain in pairs(chains) do
|
|
|
|
if splice(masterChain.blocks, chain.blocks) then
|
|
|
|
table.remove(chains, k)
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
--[[
|
|
|
|
Logger.log('schematic', 'master chain: ')
|
|
|
|
Logger.log('schematic', masterChain)
|
|
|
|
Logger.log('schematic', '---------------')
|
|
|
|
for _,e in ipairs(masterChain.blocks) do
|
|
|
|
Logger.log('schematic', string.format('%d:%d:%d %s %s:%d',
|
|
|
|
e.x, e.z, e.y, tostring(e.direction), e.id, e.dmg))
|
|
|
|
end
|
|
|
|
--]]
|
|
|
|
|
|
|
|
return masterChain
|
|
|
|
end
|
2017-06-15 07:25:55 +00:00
|
|
|
|
2016-12-11 19:24:52 +00:00
|
|
|
-- combine the individual overlapping placement chains into 1 long master chain
|
|
|
|
local masterChains = {}
|
|
|
|
for _,group in pairs(groupedChains) do
|
|
|
|
spinner:spin('chains: ' .. #masterChains)
|
|
|
|
table.insert(masterChains, mergeChains(group))
|
|
|
|
end
|
2017-06-15 07:25:55 +00:00
|
|
|
|
2016-12-11 19:24:52 +00:00
|
|
|
local function removeDuplicates(chain)
|
|
|
|
for k,v in ipairs(chain) do
|
|
|
|
for i = #chain, k+1, -1 do
|
|
|
|
if v == chain[i] then
|
|
|
|
v.info = 'Unplaceable'
|
|
|
|
table.remove(chain, i)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-06-15 07:25:55 +00:00
|
|
|
|
2016-12-11 19:24:52 +00:00
|
|
|
-- any chains with duplicates cannot be placed correctly
|
|
|
|
-- there are some cases where a turtle cannot place blocks the same as a player
|
|
|
|
for _,chain in pairs(masterChains) do
|
|
|
|
removeDuplicates(chain.blocks)
|
|
|
|
spinner:spin('chains: ' .. #masterChains)
|
|
|
|
|
|
|
|
--[[
|
|
|
|
Logger.log('schematic', "MASTER CHAIN")
|
|
|
|
for _,e in ipairs(chain) do
|
|
|
|
Logger.log('schematic', string.format('%d:%d:%d %s %d:%d',
|
|
|
|
e.block.x, e.block.z, e.block.y, tostring(e.block.direction), e.block.id, e.block.dmg))
|
|
|
|
end
|
|
|
|
--]]
|
|
|
|
|
|
|
|
end
|
|
|
|
term.clearLine()
|
2017-06-15 07:25:55 +00:00
|
|
|
|
|
|
|
-- set dependent blocks for optimize routine
|
2016-12-11 19:24:52 +00:00
|
|
|
for k,chain in pairs(masterChains) do
|
|
|
|
spinner:spin('chains: ' .. #masterChains - k)
|
|
|
|
|
2017-06-15 07:25:55 +00:00
|
|
|
local prev
|
|
|
|
for k,b in ipairs(chain.blocks) do
|
|
|
|
b.prev = prev
|
|
|
|
b.next = chain.blocks[k + 1]
|
|
|
|
prev = b
|
2016-12-11 19:24:52 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
term.clearLine()
|
2017-06-15 07:25:55 +00:00
|
|
|
|
|
|
|
return t
|
2016-12-11 19:24:52 +00:00
|
|
|
end
|
2017-06-15 07:25:55 +00:00
|
|
|
|
|
|
|
function Schematic:optimizeRoute(spinner, y)
|
2016-12-11 19:24:52 +00:00
|
|
|
|
|
|
|
local function getNearestNeighbor(p, pt, maxDistance)
|
|
|
|
local key, block, heading
|
|
|
|
local moves = maxDistance
|
|
|
|
|
|
|
|
local function getMoves(b, k)
|
|
|
|
local distance = math.abs(pt.x - b.x) + math.abs(pt.z - b.z)
|
|
|
|
|
|
|
|
if distance < moves then
|
|
|
|
-- this operation is expensive - only run if distance is close
|
|
|
|
local c, h = Point.calculateMoves(pt, b, distance)
|
|
|
|
if c < moves then
|
|
|
|
block = b
|
|
|
|
key = k
|
|
|
|
moves = c
|
|
|
|
heading = h
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-06-15 07:25:55 +00:00
|
|
|
|
|
|
|
local function blockReady(b)
|
|
|
|
if b.u then
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
if b.prev and not b.prev.u then
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
2016-12-11 19:24:52 +00:00
|
|
|
local mid = pt.index
|
|
|
|
local forward = mid + 1
|
|
|
|
local backward = mid - 1
|
|
|
|
while forward <= #p or backward > 0 do
|
|
|
|
if forward <= #p then
|
|
|
|
local b = p[forward]
|
2017-06-15 07:25:55 +00:00
|
|
|
if blockReady(b) then
|
2016-12-11 19:24:52 +00:00
|
|
|
getMoves(b, forward)
|
|
|
|
if moves <= 1 then
|
|
|
|
break
|
|
|
|
end
|
|
|
|
if moves < maxDistance and math.abs(b.z - pt.z) > moves and pt.index > 0 then
|
|
|
|
forward = #p
|
|
|
|
end
|
|
|
|
end
|
|
|
|
forward = forward + 1
|
|
|
|
end
|
|
|
|
if backward > 0 then
|
|
|
|
local b = p[backward]
|
2017-06-15 07:25:55 +00:00
|
|
|
if blockReady(b) then
|
2016-12-11 19:24:52 +00:00
|
|
|
getMoves(b, backward)
|
|
|
|
if moves <= 1 then
|
|
|
|
break
|
|
|
|
end
|
|
|
|
if moves < maxDistance and math.abs(pt.z - b.z) > moves then
|
|
|
|
backward = 0
|
|
|
|
end
|
|
|
|
end
|
|
|
|
backward = backward - 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
pt.x = block.x
|
|
|
|
pt.z = block.z
|
|
|
|
pt.y = block.y
|
|
|
|
pt.heading = heading
|
|
|
|
pt.index = key
|
|
|
|
block.u = true
|
|
|
|
return block
|
|
|
|
end
|
2017-06-15 07:25:55 +00:00
|
|
|
|
|
|
|
local pt = Util.shallowCopy(self.cache[y - 1] or turtle.point)
|
2016-12-11 19:24:52 +00:00
|
|
|
local t = {}
|
2017-06-15 07:25:55 +00:00
|
|
|
local ri = self.rowIndex[y]
|
|
|
|
local blockCount = ri.e - ri.s + 1
|
|
|
|
|
|
|
|
local function extractPlane()
|
2016-12-11 19:24:52 +00:00
|
|
|
local t = {}
|
|
|
|
local dt = {}
|
2017-06-15 07:25:55 +00:00
|
|
|
for i = ri.s, ri.e do
|
|
|
|
local b = self.blocks[i]
|
|
|
|
if b.twoHigh then
|
|
|
|
b.last = true
|
|
|
|
while b.next do
|
|
|
|
b = b.next
|
|
|
|
b.last = true
|
2016-12-11 19:24:52 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-06-15 07:25:55 +00:00
|
|
|
for i = ri.s, ri.e do
|
|
|
|
local b = self.blocks[i]
|
|
|
|
if b.last then
|
|
|
|
table.insert(dt, b)
|
|
|
|
else
|
|
|
|
table.insert(t, b)
|
|
|
|
end
|
|
|
|
end
|
2016-12-11 19:24:52 +00:00
|
|
|
return t, dt
|
|
|
|
end
|
2017-06-15 07:25:55 +00:00
|
|
|
|
2016-12-11 19:24:52 +00:00
|
|
|
local maxDistance = self.width*self.length
|
2017-06-15 07:25:55 +00:00
|
|
|
local plane, doors = extractPlane(y)
|
|
|
|
spinner:spin(percent)
|
|
|
|
pt.index = 0
|
|
|
|
for i = 1, #plane do
|
|
|
|
local b = getNearestNeighbor(plane, pt, maxDistance)
|
|
|
|
table.insert(t, b)
|
|
|
|
local percent = math.floor(#t * 100 / blockCount) .. '%'
|
|
|
|
spinner:spin(percent .. ' ' .. blockCount - i .. ' ')
|
|
|
|
end
|
|
|
|
-- all two high blocks are placed last on each plane
|
|
|
|
pt.index = 0
|
|
|
|
for i = 1, #doors do
|
|
|
|
local b = getNearestNeighbor(doors, pt, maxDistance)
|
|
|
|
table.insert(t, b)
|
|
|
|
local percent = math.floor(#t * 100 / blockCount) .. '%'
|
|
|
|
spinner:spin(percent .. ' ' .. blockCount - #plane - i .. ' ')
|
|
|
|
end
|
|
|
|
|
|
|
|
self.rowIndex[y].loaded = true
|
|
|
|
if not self.cache[y] then
|
|
|
|
self.cache[y] = Util.shallowCopy(pt)
|
|
|
|
Util.writeTable('usr/builder/' .. self.filename .. '.cache', self.cache)
|
2016-12-11 19:24:52 +00:00
|
|
|
end
|
|
|
|
|
2017-06-15 07:25:55 +00:00
|
|
|
return t
|
2016-12-11 19:24:52 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
return Schematic
|