package management
This commit is contained in:
9
builder/.package
Normal file
9
builder/.package
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
required = {
|
||||
'opus-develop-1.8',
|
||||
},
|
||||
title = 'Schematic Builder',
|
||||
repository = 'kepler155c/opus-apps/develop1.8/builder',
|
||||
description = [[ ... ]],
|
||||
licence = 'MIT',
|
||||
}
|
||||
135
builder/apis/base64.lua
Normal file
135
builder/apis/base64.lua
Normal file
@@ -0,0 +1,135 @@
|
||||
-- Base64 Encoder / Decoder
|
||||
-- By KillaVanilla
|
||||
-- see: http://www.computercraft.info/forums2/index.php?/topic/12450-killavanillas-various-apis/
|
||||
|
||||
Base64 = { }
|
||||
|
||||
local alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
|
||||
|
||||
local function sixBitToBase64(input)
|
||||
return string.sub(alphabet, input+1, input+1)
|
||||
end
|
||||
|
||||
local function base64ToSixBit(input)
|
||||
for i=1, 64 do
|
||||
if input == string.sub(alphabet, i, i) then
|
||||
return i-1
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local function octetToBase64(o1, o2, o3)
|
||||
local i1 = sixBitToBase64(bit.brshift(bit.band(o1, 0xFC), 2))
|
||||
local i2 = "A"
|
||||
local i3 = "="
|
||||
local i4 = "="
|
||||
if o2 then
|
||||
i2 = sixBitToBase64(bit.bor( bit.blshift(bit.band(o1, 3), 4), bit.brshift(bit.band(o2, 0xF0), 4) ))
|
||||
if not o3 then
|
||||
i3 = sixBitToBase64(bit.blshift(bit.band(o2, 0x0F), 2))
|
||||
else
|
||||
i3 = sixBitToBase64(bit.bor( bit.blshift(bit.band(o2, 0x0F), 2), bit.brshift(bit.band(o3, 0xC0), 6) ))
|
||||
end
|
||||
else
|
||||
i2 = sixBitToBase64(bit.blshift(bit.band(o1, 3), 4))
|
||||
end
|
||||
if o3 then
|
||||
i4 = sixBitToBase64(bit.band(o3, 0x3F))
|
||||
end
|
||||
|
||||
return i1..i2..i3..i4
|
||||
end
|
||||
|
||||
-- octet 1 needs characters 1/2
|
||||
-- octet 2 needs characters 2/3
|
||||
-- octet 3 needs characters 3/4
|
||||
|
||||
local function base64ToThreeOctet(s1)
|
||||
local c1 = base64ToSixBit(string.sub(s1, 1, 1))
|
||||
local c2 = base64ToSixBit(string.sub(s1, 2, 2))
|
||||
local c3 = 0
|
||||
local c4 = 0
|
||||
local o1 = 0
|
||||
local o2 = 0
|
||||
local o3 = 0
|
||||
if string.sub(s1, 3, 3) == "=" then
|
||||
c3 = nil
|
||||
c4 = nil
|
||||
elseif string.sub(s1, 4, 4) == "=" then
|
||||
c3 = base64ToSixBit(string.sub(s1, 3, 3))
|
||||
c4 = nil
|
||||
else
|
||||
c3 = base64ToSixBit(string.sub(s1, 3, 3))
|
||||
c4 = base64ToSixBit(string.sub(s1, 4, 4))
|
||||
end
|
||||
o1 = bit.bor( bit.blshift(c1, 2), bit.brshift(bit.band( c2, 0x30 ), 4) )
|
||||
if c3 then
|
||||
o2 = bit.bor( bit.blshift(bit.band(c2, 0x0F), 4), bit.brshift(bit.band( c3, 0x3C ), 2) )
|
||||
else
|
||||
o2 = nil
|
||||
end
|
||||
if c4 then
|
||||
o3 = bit.bor( bit.blshift(bit.band(c3, 3), 6), c4 )
|
||||
else
|
||||
o3 = nil
|
||||
end
|
||||
return o1, o2, o3
|
||||
end
|
||||
|
||||
local function splitIntoBlocks(bytes)
|
||||
local blockNum = 1
|
||||
local blocks = {}
|
||||
for i=1, #bytes, 3 do
|
||||
blocks[blockNum] = {bytes[i], bytes[i+1], bytes[i+2]}
|
||||
--[[
|
||||
if #blocks[blockNum] < 3 then
|
||||
for j=#blocks[blockNum]+1, 3 do
|
||||
table.insert(blocks[blockNum], 0)
|
||||
end
|
||||
end
|
||||
]]
|
||||
blockNum = blockNum+1
|
||||
end
|
||||
return blocks
|
||||
end
|
||||
|
||||
function Base64.encode(bytes)
|
||||
local blocks = splitIntoBlocks(bytes)
|
||||
local output = ""
|
||||
for i=1, #blocks do
|
||||
output = output..octetToBase64( unpack(blocks[i]) )
|
||||
end
|
||||
return output
|
||||
end
|
||||
|
||||
function Base64.decode(str)
|
||||
local bytes = {}
|
||||
local blocks = {}
|
||||
local blockNum = 1
|
||||
for i=1, #str, 4 do
|
||||
blocks[blockNum] = string.sub(str, i, i+3)
|
||||
blockNum = blockNum+1
|
||||
end
|
||||
for i=1, #blocks do
|
||||
local o1, o2, o3 = base64ToThreeOctet(blocks[i])
|
||||
table.insert(bytes, o1)
|
||||
table.insert(bytes, o2)
|
||||
table.insert(bytes, o3)
|
||||
if (i % 1000) == 0 then
|
||||
os.sleep(0)
|
||||
end
|
||||
end
|
||||
-- Remove padding:
|
||||
--[[
|
||||
for i=#bytes, 1, -1 do
|
||||
if bytes[i] ~= 0 then
|
||||
break
|
||||
else
|
||||
bytes[i] = nil
|
||||
end
|
||||
end
|
||||
]]
|
||||
return bytes
|
||||
end
|
||||
|
||||
return Base64
|
||||
549
builder/apis/builder/blocks.lua
Normal file
549
builder/apis/builder/blocks.lua
Normal file
@@ -0,0 +1,549 @@
|
||||
local class = require('class')
|
||||
local Util = require('util')
|
||||
local TableDB = require('tableDB')
|
||||
local JSON = require('json')
|
||||
|
||||
-- see https://github.com/Khroki/MCEdit-Unified/blob/master/pymclevel/minecraft.yaml
|
||||
-- see https://github.com/Khroki/MCEdit-Unified/blob/master/Items/minecraft/blocks.json
|
||||
|
||||
--[[-- blockDB --]]--
|
||||
local blockDB = TableDB()
|
||||
|
||||
function blockDB:load()
|
||||
local blocks = JSON.decodeFromFile('usr/etc/names/minecraft.json')
|
||||
|
||||
if not blocks then
|
||||
error('Unable to read blocks.json')
|
||||
end
|
||||
|
||||
for strId, block in pairs(blocks) do
|
||||
strId = 'minecraft:' .. strId
|
||||
if type(block.name) == 'string' then
|
||||
self:add(block.id, 0, block.name, strId, block.place)
|
||||
else
|
||||
for nid,name in pairs(block.name) do
|
||||
self:add(block.id, nid - 1, name, strId, block.place)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function blockDB:lookup(id, dmg)
|
||||
if not id then
|
||||
return
|
||||
end
|
||||
|
||||
return self.data[id .. ':' .. dmg]
|
||||
end
|
||||
|
||||
function blockDB:add(id, dmg, name, strId, place)
|
||||
local key = id .. ':' .. dmg
|
||||
|
||||
TableDB.add(self, key, {
|
||||
id = id,
|
||||
dmg = dmg,
|
||||
name = name,
|
||||
strId = strId,
|
||||
place = place,
|
||||
})
|
||||
end
|
||||
|
||||
--[[-- placementDB --]]--
|
||||
-- in memory table that expands the standardBlock and blockType tables for each item/dmg/placement combination
|
||||
local placementDB = TableDB()
|
||||
|
||||
function placementDB:load(sbDB, btDB)
|
||||
for k,v in pairs(sbDB.data) do
|
||||
if v.place then
|
||||
local bt = btDB.data[v.place]
|
||||
if not bt then
|
||||
error('missing block type: ' .. v.place)
|
||||
end
|
||||
local id, dmg = string.match(k, '(%d+):*(%d+)')
|
||||
self:addSubsForBlockType(tonumber(id), tonumber(dmg), bt)
|
||||
end
|
||||
end
|
||||
|
||||
-- special case for quartz pillars
|
||||
self:addSubsForBlockType(155, 2, btDB.data['quartz-pillar'])
|
||||
end
|
||||
|
||||
function placementDB:addSubsForBlockType(id, dmg, bt)
|
||||
for _,sub in pairs(bt) do
|
||||
local odmg = sub.odmg
|
||||
if type(sub.odmg) == 'string' then
|
||||
odmg = dmg + tonumber(string.match(odmg, '+(%d+)'))
|
||||
end
|
||||
|
||||
local b = blockDB:lookup(id, dmg)
|
||||
local strId = tostring(id)
|
||||
if b then
|
||||
strId = b.strId
|
||||
end
|
||||
|
||||
self:add(
|
||||
id,
|
||||
odmg,
|
||||
sub.sid or strId,
|
||||
sub.sdmg or dmg,
|
||||
sub.dir,
|
||||
sub.extra)
|
||||
end
|
||||
end
|
||||
|
||||
function placementDB:add(id, dmg, sid, sdmg, direction, extra)
|
||||
if direction and #direction == 0 then
|
||||
direction = nil
|
||||
end
|
||||
|
||||
local entry = {
|
||||
oid = id, -- numeric ID
|
||||
odmg = dmg, -- dmg with placement info
|
||||
id = sid, -- string ID
|
||||
dmg = sdmg, -- dmg without placement info
|
||||
direction = direction,
|
||||
}
|
||||
if extra then
|
||||
Util.merge(entry, extra)
|
||||
end
|
||||
|
||||
self.data[id .. ':' .. dmg] = entry
|
||||
end
|
||||
|
||||
--[[-- BlockTypeDB --]]--
|
||||
local blockTypeDB = TableDB()
|
||||
|
||||
function blockTypeDB:addTemp(blockType, subs)
|
||||
local bt = self.data[blockType]
|
||||
if not bt then
|
||||
bt = { }
|
||||
self.data[blockType] = bt
|
||||
end
|
||||
for _,sub in pairs(subs) do
|
||||
table.insert(bt, {
|
||||
odmg = sub[1],
|
||||
sid = sub[2],
|
||||
sdmg = sub[3],
|
||||
dir = sub[4],
|
||||
extra = sub[5]
|
||||
})
|
||||
end
|
||||
self.dirty = true
|
||||
end
|
||||
|
||||
function blockTypeDB:load()
|
||||
blockTypeDB:addTemp('stairs', {
|
||||
{ 0, nil, 0, 'east-up' },
|
||||
{ 1, nil, 0, 'west-up' },
|
||||
{ 2, nil, 0, 'south-up' },
|
||||
{ 3, nil, 0, 'north-up' },
|
||||
{ 4, nil, 0, 'east-down' },
|
||||
{ 5, nil, 0, 'west-down' },
|
||||
{ 6, nil, 0, 'south-down' },
|
||||
{ 7, nil, 0, 'north-down' },
|
||||
})
|
||||
blockTypeDB:addTemp('gate', {
|
||||
{ 0, nil, 0, 'north' },
|
||||
{ 1, nil, 0, 'east' },
|
||||
{ 2, nil, 0, 'south' },
|
||||
{ 3, nil, 0, 'west' },
|
||||
{ 4, nil, 0, 'north' },
|
||||
{ 5, nil, 0, 'east' },
|
||||
{ 6, nil, 0, 'south' },
|
||||
{ 7, nil, 0, 'west' },
|
||||
})
|
||||
blockTypeDB:addTemp('pumpkin', {
|
||||
{ 0, nil, 0, 'north-block' },
|
||||
{ 1, nil, 0, 'east-block' },
|
||||
{ 2, nil, 0, 'south-block' },
|
||||
{ 3, nil, 0, 'west-block' },
|
||||
{ 4, nil, 0, 'north-block' },
|
||||
{ 5, nil, 0, 'east-block' },
|
||||
{ 6, nil, 0, 'south-block' },
|
||||
{ 7, nil, 0, 'west-block' },
|
||||
})
|
||||
blockTypeDB:addTemp('anvil', {
|
||||
{ 0, nil, 0, 'south' },
|
||||
{ 1, nil, 0, 'east' },
|
||||
{ 2, nil, 0, 'south'},
|
||||
{ 3, nil, 0, 'east' },
|
||||
{ 4, nil, 0, 'south' },
|
||||
{ 5, nil, 0, 'east' },
|
||||
{ 6, nil, 0, 'east' },
|
||||
{ 7, nil, 0, 'south' },
|
||||
{ 8, nil, 0, 'south' },
|
||||
{ 9, nil, 0, 'east' },
|
||||
{ 10, nil, 0, 'east' },
|
||||
{ 11, nil, 0, 'south' },
|
||||
{ 12, nil, 0 },
|
||||
{ 13, nil, 0 },
|
||||
{ 14, nil, 0 },
|
||||
{ 15, nil, 0 },
|
||||
})
|
||||
blockTypeDB:addTemp('bed', {
|
||||
{ 0, nil, 0, 'south' },
|
||||
{ 1, nil, 0, 'west' },
|
||||
{ 2, nil, 0, 'north' },
|
||||
{ 3, nil, 0, 'east' },
|
||||
{ 4, nil, 0, 'south' },
|
||||
{ 5, nil, 0, 'west' },
|
||||
{ 6, nil, 0, 'north' },
|
||||
{ 7, nil, 0, 'east' },
|
||||
{ 8, 'minecraft:air', 0 },
|
||||
{ 9, 'minecraft:air', 0 },
|
||||
{ 10, 'minecraft:air', 0 },
|
||||
{ 11, 'minecraft:air', 0 },
|
||||
{ 12, 'minecraft:air', 0 },
|
||||
{ 13, 'minecraft:air', 0 },
|
||||
{ 14, 'minecraft:air', 0 },
|
||||
{ 15, 'minecraft:air', 0 },
|
||||
})
|
||||
blockTypeDB:addTemp('comparator', {
|
||||
{ 0, nil, 0, 'south' },
|
||||
{ 1, nil, 0, 'west' },
|
||||
{ 2, nil, 0, 'north' },
|
||||
{ 3, nil, 0, 'east' },
|
||||
{ 4, nil, 0, 'south' },
|
||||
{ 5, nil, 0, 'west' },
|
||||
{ 6, nil, 0, 'north' },
|
||||
{ 7, nil, 0, 'east' },
|
||||
{ 8, nil, 0, 'south' },
|
||||
{ 9, nil, 0, 'west' },
|
||||
{ 10, nil, 0, 'north' },
|
||||
{ 11, nil, 0, 'east' },
|
||||
{ 12, nil, 0, 'south' },
|
||||
{ 13, nil, 0, 'west' },
|
||||
{ 14, nil, 0, 'north' },
|
||||
{ 15, nil, 0, 'east' },
|
||||
})
|
||||
blockTypeDB:addTemp('quartz-pillar', {
|
||||
{ 2, nil, 2 },
|
||||
{ 3, nil, 2, 'north-south-block' },
|
||||
{ 4, nil, 2, 'east-west-block' }, -- should be east-west-block
|
||||
})
|
||||
blockTypeDB:addTemp('hay-bale', {
|
||||
{ 0, nil, 0 },
|
||||
{ 4, nil, 0, 'east-west-block' }, -- should be east-west-block
|
||||
{ 8, nil, 0, 'north-south-block' },
|
||||
})
|
||||
blockTypeDB:addTemp('button', {
|
||||
{ 1, nil, 0, 'west-block' },
|
||||
{ 2, nil, 0, 'east-block' },
|
||||
{ 3, nil, 0, 'north-block' },
|
||||
{ 4, nil, 0, 'south-block' },
|
||||
{ 5, nil, 0 }, -- block top
|
||||
})
|
||||
blockTypeDB:addTemp('cauldron', {
|
||||
{ 0, nil, 0 },
|
||||
{ 1, nil, 0 },
|
||||
{ 2, nil, 0 },
|
||||
{ 3, nil, 0 },
|
||||
})
|
||||
blockTypeDB:addTemp('dispenser', {
|
||||
{ 0, nil, 0, 'wrench-down' },
|
||||
{ 1, nil, 0, 'wrench-up' },
|
||||
{ 2, nil, 0, 'south' },
|
||||
{ 3, nil, 0, 'north' },
|
||||
{ 4, nil, 0, 'east' },
|
||||
{ 5, nil, 0, 'west' },
|
||||
{ 9, nil, 0 },
|
||||
})
|
||||
blockTypeDB:addTemp('end_rod', {
|
||||
{ 0, nil, 0, 'wrench-down' },
|
||||
{ 1, nil, 0, 'wrench-up' },
|
||||
{ 2, nil, 0, 'south-block-flip' },
|
||||
{ 3, nil, 0, 'north-block-flip' },
|
||||
{ 4, nil, 0, 'east-block-flip' },
|
||||
{ 5, nil, 0, 'west-block-flip' },
|
||||
{ 9, nil, 0 },
|
||||
})
|
||||
blockTypeDB:addTemp('hopper', {
|
||||
{ 0, nil, 0 },
|
||||
{ 1, nil, 0 },
|
||||
{ 2, nil, 0, 'south-block' },
|
||||
{ 3, nil, 0, 'north-block' },
|
||||
{ 4, nil, 0, 'east-block' },
|
||||
{ 5, nil, 0, 'west-block' },
|
||||
{ 8, nil, 0 },
|
||||
{ 9, nil, 0 },
|
||||
{ 10, nil, 0 },
|
||||
{ 11, nil, 0, 'south-block' },
|
||||
{ 12, nil, 0, 'north-block' },
|
||||
{ 13, nil, 0, 'east-block' },
|
||||
{ 14, nil, 0, 'west-block' },
|
||||
})
|
||||
blockTypeDB:addTemp('mobhead', {
|
||||
{ 0, nil, 0 },
|
||||
{ 1, nil, 0 },
|
||||
{ 2, nil, 0, 'south-block' },
|
||||
{ 3, nil, 0, 'north-block' },
|
||||
{ 4, nil, 0, 'west-block' },
|
||||
{ 5, nil, 0, 'east-block' },
|
||||
})
|
||||
blockTypeDB:addTemp('rail', {
|
||||
{ 0, nil, 0, 'south' },
|
||||
{ 1, nil, 0, 'east' },
|
||||
{ 2, nil, 0, 'east' },
|
||||
{ 3, nil, 0, 'east' },
|
||||
{ 4, nil, 0, 'south' },
|
||||
{ 5, nil, 0, 'south' },
|
||||
{ 6, nil, 0, 'east' },
|
||||
{ 7, nil, 0, 'south' },
|
||||
{ 8, nil, 0, 'east' },
|
||||
{ 9, nil, 0, 'south' },
|
||||
})
|
||||
blockTypeDB:addTemp('adp-rail', {
|
||||
{ 0, nil, 0, 'south' },
|
||||
{ 1, nil, 0, 'east' },
|
||||
{ 2, nil, 0, 'east' },
|
||||
{ 3, nil, 0, 'east' },
|
||||
{ 4, nil, 0, 'south' },
|
||||
{ 5, nil, 0, 'south' },
|
||||
{ 8, nil, 0, 'south' },
|
||||
{ 9, nil, 0, 'east' },
|
||||
{ 10, nil, 0, 'east' },
|
||||
{ 11, nil, 0, 'east' },
|
||||
{ 12, nil, 0, 'south' },
|
||||
{ 13, nil, 0, 'south' },
|
||||
})
|
||||
blockTypeDB:addTemp('signpost', {
|
||||
{ 0, nil, 0, 'north' },
|
||||
{ 1, nil, 0, 'north', { facing = 1 } },
|
||||
{ 2, nil, 0, 'north', { facing = 2 } },
|
||||
{ 3, nil, 0, 'north', { facing = 3 } },
|
||||
{ 4, nil, 0, 'east' },
|
||||
{ 5, nil, 0, 'east', { facing = 1 } },
|
||||
{ 6, nil, 0, 'east', { facing = 2 } },
|
||||
{ 7, nil, 0, 'east', { facing = 3 } },
|
||||
{ 8, nil, 0, 'south' },
|
||||
{ 9, nil, 0, 'south', { facing = 1 } },
|
||||
{ 10, nil, 0, 'south', { facing = 2 } },
|
||||
{ 11, nil, 0, 'south', { facing = 3 } },
|
||||
{ 12, nil, 0, 'west' },
|
||||
{ 13, nil, 0, 'west', { facing = 1 } },
|
||||
{ 14, nil, 0, 'west', { facing = 2 } },
|
||||
{ 15, nil, 0, 'west', { facing = 3 } },
|
||||
})
|
||||
blockTypeDB:addTemp('vine', {
|
||||
{ 0, nil, 0 },
|
||||
{ 1, nil, 0, 'south-block-vine' },
|
||||
{ 2, nil, 0, 'west-block-vine' },
|
||||
{ 3, nil, 0, 'south-block-vine' },
|
||||
{ 4, nil, 0, 'north-block-vine' },
|
||||
{ 5, nil, 0, 'south-block-vine' },
|
||||
{ 6, nil, 0, 'north-block-vine' },
|
||||
{ 7, nil, 0, 'south-block-vine' },
|
||||
{ 8, nil, 0, 'east-block-vine' },
|
||||
{ 9, nil, 0, 'south-block-vine' },
|
||||
{ 10, nil, 0, 'east-block-vine' },
|
||||
{ 11, nil, 0, 'east-block-vine' },
|
||||
{ 12, nil, 0, 'east-block-vine' },
|
||||
{ 13, nil, 0, 'east-block-vine' },
|
||||
{ 14, nil, 0, 'east-block-vine' },
|
||||
{ 15, nil, 0, 'east-block-vine' },
|
||||
})
|
||||
blockTypeDB:addTemp('torch', {
|
||||
{ 0, nil, 0 },
|
||||
{ 1, nil, 0, 'west-block' },
|
||||
{ 2, nil, 0, 'east-block' },
|
||||
{ 3, nil, 0, 'north-block' },
|
||||
{ 4, nil, 0, 'south-block' },
|
||||
{ 5, nil, 0 },
|
||||
})
|
||||
blockTypeDB:addTemp('tripwire', {
|
||||
{ 0, nil, 0, 'north-block' },
|
||||
{ 1, nil, 0, 'east-block' },
|
||||
{ 2, nil, 0, 'south-block' },
|
||||
{ 3, nil, 0, 'west-block' },
|
||||
})
|
||||
blockTypeDB:addTemp('trapdoor', {
|
||||
{ 0, nil, 0, 'south-block' },
|
||||
{ 1, nil, 0, 'north-block' },
|
||||
{ 2, nil, 0, 'east-block' },
|
||||
{ 3, nil, 0, 'west-block' },
|
||||
{ 4, nil, 0, 'south-block' },
|
||||
{ 5, nil, 0, 'north-block' },
|
||||
{ 6, nil, 0, 'east-block' },
|
||||
{ 7, nil, 0, 'west-block' },
|
||||
{ 8, nil, 0, 'south' },
|
||||
{ 9, nil, 0, 'north' },
|
||||
{ 10, nil, 0, 'east' },
|
||||
{ 11, nil, 0, 'west' },
|
||||
{ 12, nil, 0, 'south' },
|
||||
{ 13, nil, 0, 'north' },
|
||||
{ 14, nil, 0, 'east' },
|
||||
{ 15, nil, 0, 'west' },
|
||||
})
|
||||
blockTypeDB:addTemp('piston', {
|
||||
{ 0, nil, 0, 'piston-down' },
|
||||
{ 1, nil, 0, 'piston-up' },
|
||||
{ 2, nil, 0, 'piston-north' },
|
||||
{ 3, nil, 0, 'piston-south' },
|
||||
{ 4, nil, 0, 'piston-west' },
|
||||
{ 5, nil, 0, 'piston-east' },
|
||||
{ 8, nil, 0, 'piston-down' },
|
||||
{ 9, nil, 0, 'piston-up' },
|
||||
{ 10, nil, 0, 'piston-north' },
|
||||
{ 11, nil, 0, 'piston-south' },
|
||||
{ 12, nil, 0, 'piston-west' },
|
||||
{ 13, nil, 0, 'piston-east' },
|
||||
})
|
||||
blockTypeDB:addTemp('lever', {
|
||||
{ 0, nil, 0, 'up' },
|
||||
{ 1, nil, 0, 'west-block' },
|
||||
{ 2, nil, 0, 'east-block' },
|
||||
{ 3, nil, 0, 'north-block' },
|
||||
{ 4, nil, 0, 'south-block' },
|
||||
{ 5, nil, 0, 'north' },
|
||||
{ 6, nil, 0, 'west' },
|
||||
{ 7, nil, 0, 'up' },
|
||||
{ 8, nil, 0, 'up' },
|
||||
{ 9, nil, 0, 'west-block' },
|
||||
{ 10, nil, 0, 'east-block' },
|
||||
{ 11, nil, 0, 'north-block' },
|
||||
{ 12, nil, 0, 'south-block' },
|
||||
{ 13, nil, 0, 'north' },
|
||||
{ 14, nil, 0, 'west' },
|
||||
{ 15, nil, 0, 'up' },
|
||||
})
|
||||
blockTypeDB:addTemp('wallsign-ladder', {
|
||||
{ 0, nil, 0 },
|
||||
{ 1, nil, 0 },
|
||||
{ 2, nil, 0, 'south-block' },
|
||||
{ 3, nil, 0, 'north-block' },
|
||||
{ 4, nil, 0, 'east-block' },
|
||||
{ 5, nil, 0, 'west-block' },
|
||||
})
|
||||
blockTypeDB:addTemp('chest-furnace', {
|
||||
{ 0, nil, 0 },
|
||||
{ 2, nil, 0, 'south' },
|
||||
{ 3, nil, 0, 'north' },
|
||||
{ 4, nil, 0, 'east' },
|
||||
{ 5, nil, 0, 'west' },
|
||||
})
|
||||
blockTypeDB:addTemp('repeater', {
|
||||
{ 0, nil, 0, 'north' },
|
||||
{ 1, nil, 0, 'east' },
|
||||
{ 2, nil, 0, 'south' },
|
||||
{ 3, nil, 0, 'west' },
|
||||
{ 4, nil, 0, 'north' },
|
||||
{ 5, nil, 0, 'east' },
|
||||
{ 6, nil, 0, 'south' },
|
||||
{ 7, nil, 0, 'west' },
|
||||
{ 8, nil, 0, 'north' },
|
||||
{ 9, nil, 0, 'east' },
|
||||
{ 10, nil, 0, 'south' },
|
||||
{ 11, nil, 0, 'west' },
|
||||
{ 12, nil, 0, 'north' },
|
||||
{ 13, nil, 0, 'east' },
|
||||
{ 14, nil, 0, 'south' },
|
||||
{ 15, nil, 0, 'west' },
|
||||
})
|
||||
blockTypeDB:addTemp('flatten', {
|
||||
{ 0, nil, 0 },
|
||||
{ 1, nil, 0 },
|
||||
{ 2, nil, 0 },
|
||||
{ 3, nil, 0 },
|
||||
{ 4, nil, 0 },
|
||||
{ 5, nil, 0 },
|
||||
{ 6, nil, 0 },
|
||||
{ 7, nil, 0 },
|
||||
{ 8, nil, 0 },
|
||||
{ 9, nil, 0 },
|
||||
{ 10, nil, 0 },
|
||||
{ 11, nil, 0 },
|
||||
{ 12, nil, 0 },
|
||||
{ 13, nil, 0 },
|
||||
{ 14, nil, 0 },
|
||||
{ 15, nil, 0 },
|
||||
})
|
||||
blockTypeDB:addTemp('sapling', {
|
||||
{ '+0', nil, nil },
|
||||
{ '+8', nil, nil },
|
||||
})
|
||||
blockTypeDB:addTemp('leaves', {
|
||||
{ '+0', nil, nil },
|
||||
{ '+4', nil, nil },
|
||||
{ '+8', nil, nil },
|
||||
{ '+12', nil, nil },
|
||||
})
|
||||
blockTypeDB:addTemp('slab', {
|
||||
{ '+0', nil, nil, 'bottom' },
|
||||
{ '+8', nil, nil, 'top' },
|
||||
})
|
||||
blockTypeDB:addTemp('largeplant', {
|
||||
{ '+0', nil, nil, 'east-door', { twoHigh = true } }, -- should use a generic double tall keyword
|
||||
{ '+8', 'minecraft:air', 0 },
|
||||
})
|
||||
blockTypeDB:addTemp('wood', {
|
||||
{ '+0', nil, nil },
|
||||
{ '+4', nil, nil, 'east-west-block' },
|
||||
{ '+8', nil, nil, 'north-south-block' },
|
||||
{ '+12', nil, nil },
|
||||
})
|
||||
blockTypeDB:addTemp('door', {
|
||||
{ 0, nil, 0, 'east-door', { twoHigh = true } },
|
||||
{ 1, nil, 0, 'south-door', { twoHigh = true } },
|
||||
{ 2, nil, 0, 'west-door', { twoHigh = true } },
|
||||
{ 3, nil, 0, 'north-door', { twoHigh = true } },
|
||||
{ 4, nil, 0, 'east-door', { twoHigh = true } },
|
||||
{ 5, nil, 0, 'south-door', { twoHigh = true } },
|
||||
{ 6, nil, 0, 'west-door', { twoHigh = true } },
|
||||
{ 7, nil, 0, 'north-door', { twoHigh = true } },
|
||||
{ 8,'minecraft:air', 0 },
|
||||
{ 9,'minecraft:air', 0 },
|
||||
{ 10,'minecraft:air', 0 },
|
||||
{ 11,'minecraft:air', 0 },
|
||||
{ 12,'minecraft:air', 0 },
|
||||
{ 13,'minecraft:air', 0 },
|
||||
{ 14,'minecraft:air', 0 },
|
||||
{ 15,'minecraft:air', 0 },
|
||||
})
|
||||
blockTypeDB:addTemp('cocoa', {
|
||||
{ 0, nil, 0, 'south-block' },
|
||||
{ 1, nil, 0, 'west-block' },
|
||||
{ 2, nil, 0, 'north-block' },
|
||||
{ 3, nil, 0, 'east-block' },
|
||||
{ 4, nil, 0, 'south-block' },
|
||||
{ 5, nil, 0, 'west-block' },
|
||||
{ 6, nil, 0, 'north-block' },
|
||||
{ 7, nil, 0, 'east-block' },
|
||||
{ 8, nil, 0, 'south-block' },
|
||||
{ 9, nil, 0, 'west-block' },
|
||||
{ 10, nil, 0, 'north-block' },
|
||||
{ 11, nil, 0, 'east-block' },
|
||||
})
|
||||
end
|
||||
|
||||
local Blocks = class()
|
||||
function Blocks:init(args)
|
||||
Util.merge(self, args)
|
||||
self.blockDB = blockDB
|
||||
|
||||
blockDB:load()
|
||||
blockTypeDB:load()
|
||||
placementDB:load(blockDB, blockTypeDB)
|
||||
end
|
||||
|
||||
-- for an ID / dmg (with placement info)
|
||||
-- return the correct block (without the placment info embedded in the dmg)
|
||||
function Blocks:getPlaceableBlock(id, dmg)
|
||||
local p = placementDB:get({id, dmg})
|
||||
if p then
|
||||
return Util.shallowCopy(p)
|
||||
end
|
||||
|
||||
local b = blockDB:get({id, dmg})
|
||||
if b then
|
||||
return { id = b.strId, dmg = b.dmg }
|
||||
end
|
||||
|
||||
b = blockDB:get({id, 0})
|
||||
if b then
|
||||
return { id = b.strId, dmg = b.dmg }
|
||||
end
|
||||
|
||||
return { id = id, dmg = dmg }
|
||||
end
|
||||
|
||||
return Blocks
|
||||
101
builder/apis/builder/builder.lua
Normal file
101
builder/apis/builder/builder.lua
Normal file
@@ -0,0 +1,101 @@
|
||||
local Blocks = require('builder.blocks')
|
||||
local class = require('class')
|
||||
local Message = require('message')
|
||||
local Util = require('util')
|
||||
|
||||
local device = _G.device
|
||||
local fs = _G.fs
|
||||
local turtle = _G.turtle
|
||||
|
||||
local Builder = class()
|
||||
Util.merge(Builder, {
|
||||
isCommandComputer = not turtle,
|
||||
loc = { },
|
||||
index = 1,
|
||||
mode = 'build',
|
||||
})
|
||||
|
||||
local BUILDER_DIR = 'usr/builder'
|
||||
|
||||
local blockInfo = Blocks()
|
||||
|
||||
function Builder:getBlockCounts()
|
||||
local blocks = { }
|
||||
|
||||
for k = self.index, #self.schematic.blocks do
|
||||
local b = self.schematic.blocks[k]
|
||||
local key = tostring(b.id) .. ':' .. b.dmg
|
||||
local block = blocks[key]
|
||||
if not block then
|
||||
block = Util.shallowCopy(b)
|
||||
block.qty = 0
|
||||
block.need = 0
|
||||
blocks[key] = block
|
||||
end
|
||||
block.need = block.need + 1
|
||||
end
|
||||
|
||||
return blocks
|
||||
end
|
||||
|
||||
function Builder:substituteBlocks(throttle)
|
||||
for _,b in pairs(self.schematic.blocks) do
|
||||
|
||||
-- replace schematic block type with substitution
|
||||
local pb = blockInfo:getPlaceableBlock(b.id, b.dmg)
|
||||
|
||||
Util.merge(b, pb)
|
||||
|
||||
b.odmg = pb.odmg or pb.dmg
|
||||
|
||||
local sub = self.subDB:get({ b.id, b.dmg })
|
||||
if sub then
|
||||
b.id, b.dmg = self.subDB:extract(sub)
|
||||
end
|
||||
throttle()
|
||||
end
|
||||
end
|
||||
|
||||
function Builder:reloadSchematic(throttle)
|
||||
self.schematic:reload(throttle)
|
||||
self:substituteBlocks(throttle)
|
||||
end
|
||||
|
||||
function Builder:log(...)
|
||||
Util.print(...)
|
||||
end
|
||||
|
||||
function Builder:dumpInventory()
|
||||
end
|
||||
|
||||
function Builder:logBlock(index, b)
|
||||
local bdir = b.direction or ''
|
||||
local logText = string.format('%d %s:%d (x:%d,z:%d:y:%d) %s',
|
||||
index, b.id, b.dmg, b.x, b.z, b.y, bdir)
|
||||
self:log(logText)
|
||||
-- self:log(b.index) -- unique identifier of block
|
||||
|
||||
if device.wireless_modem then
|
||||
Message.broadcast('builder', { x = b.x, y = b.y, z = b.z, heading = b.heading })
|
||||
end
|
||||
end
|
||||
|
||||
function Builder:saveProgress(index)
|
||||
Util.writeTable(
|
||||
fs.combine(BUILDER_DIR, self.schematic.filename .. '.progress'),
|
||||
{ index = index, loc = self.loc }
|
||||
)
|
||||
end
|
||||
|
||||
function Builder:loadProgress(filename)
|
||||
local progress = Util.readTable(fs.combine(BUILDER_DIR, filename))
|
||||
if progress then
|
||||
self.index = progress.index
|
||||
if self.index > #self.schematic.blocks then
|
||||
self.index = 1
|
||||
end
|
||||
self.loc = progress.loc or { }
|
||||
end
|
||||
end
|
||||
|
||||
return Builder
|
||||
84
builder/apis/builder/commands.lua
Normal file
84
builder/apis/builder/commands.lua
Normal file
@@ -0,0 +1,84 @@
|
||||
local Builder = require('builder.builder')
|
||||
local Event = require('event')
|
||||
local Util = require('util')
|
||||
|
||||
local commands = _G.commands
|
||||
local fs = _G.fs
|
||||
local os = _G.os
|
||||
local read = _G.read
|
||||
|
||||
function Builder:begin()
|
||||
local direction = 1
|
||||
local last = #self.schematic.blocks
|
||||
local throttle = Util.throttle()
|
||||
|
||||
local cx, cy, cz = commands.getBlockPosition()
|
||||
if self.loc.x then
|
||||
cx, cy, cz = self.loc.rx, self.loc.ry, self.loc.rz
|
||||
end
|
||||
|
||||
if self.mode == 'destroy' then
|
||||
direction = -1
|
||||
last = 1
|
||||
end
|
||||
|
||||
for i = self.index, last, direction do
|
||||
self.index = i
|
||||
|
||||
local b = self.schematic:getComputedBlock(i)
|
||||
|
||||
if b.id ~= 'minecraft:air' then
|
||||
|
||||
self:logBlock(self.index, b)
|
||||
|
||||
local id = b.id
|
||||
if self.mode == 'destroy' then
|
||||
id = 'minecraft:air'
|
||||
end
|
||||
|
||||
local function placeBlock(bid, dmg, x, y, z)
|
||||
local command = table.concat({
|
||||
"setblock",
|
||||
cx + x + 1,
|
||||
cy + y,
|
||||
cz + z + 1,
|
||||
bid,
|
||||
dmg,
|
||||
}, ' ')
|
||||
|
||||
commands.execAsync(command)
|
||||
|
||||
local result = { os.pullEvent("task_complete") }
|
||||
if not result[4] then
|
||||
Util.print(result[5])
|
||||
if self.mode ~= 'destroy' then
|
||||
read()
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
placeBlock(id, b.odmg, b.x, b.y, b.z)
|
||||
|
||||
if b.twoHigh then
|
||||
local _, topBlock = self.schematic:findIndexAt(b.x, b.z, b.y + 1, true)
|
||||
if topBlock then
|
||||
placeBlock(id, topBlock.odmg, b.x, b.y + 1, b.z)
|
||||
end
|
||||
end
|
||||
|
||||
if self.mode == 'destroy' then
|
||||
self:saveProgress(math.max(self.index, 1))
|
||||
else
|
||||
self:saveProgress(self.index + 1)
|
||||
end
|
||||
else
|
||||
throttle() -- sleep in case there are a large # of skipped blocks
|
||||
end
|
||||
end
|
||||
|
||||
fs.delete(self.schematic.filename .. '.progress')
|
||||
print('Finished')
|
||||
Event.exitPullEvents()
|
||||
end
|
||||
|
||||
return Builder
|
||||
1223
builder/apis/builder/schematic.lua
Normal file
1223
builder/apis/builder/schematic.lua
Normal file
File diff suppressed because it is too large
Load Diff
1254
builder/apis/builder/turtle.lua
Normal file
1254
builder/apis/builder/turtle.lua
Normal file
File diff suppressed because it is too large
Load Diff
870
builder/apis/deflatelua.lua
Normal file
870
builder/apis/deflatelua.lua
Normal file
@@ -0,0 +1,870 @@
|
||||
--[[
|
||||
|
||||
LUA MODULE
|
||||
|
||||
compress.deflatelua - deflate (and gunzip/zlib) implemented in Lua.
|
||||
|
||||
SYNOPSIS
|
||||
|
||||
local DEFLATE = require 'compress.deflatelua'
|
||||
-- uncompress gzip file
|
||||
local fh = assert(io.open'foo.txt.gz', 'rb')
|
||||
local ofh = assert(io.open'foo.txt', 'wb')
|
||||
DEFLATE.gunzip {input=fh, output=ofh}
|
||||
fh:close(); ofh:close()
|
||||
-- can also uncompress from string including zlib and raw DEFLATE formats.
|
||||
|
||||
DESCRIPTION
|
||||
|
||||
This is a pure Lua implementation of decompressing the DEFLATE format,
|
||||
including the related zlib and gzip formats.
|
||||
|
||||
Note: This library only supports decompression.
|
||||
Compression is not currently implemented.
|
||||
|
||||
API
|
||||
|
||||
Note: in the following functions, input stream `fh` may be
|
||||
a file handle, string, or an iterator function that returns strings.
|
||||
Output stream `ofh` may be a file handle or a function that
|
||||
consumes one byte (number 0..255) per call.
|
||||
|
||||
DEFLATE.inflate {input=fh, output=ofh}
|
||||
|
||||
Decompresses input stream `fh` in the DEFLATE format
|
||||
while writing to output stream `ofh`.
|
||||
DEFLATE is detailed in http://tools.ietf.org/html/rfc1951 .
|
||||
|
||||
DEFLATE.gunzip {input=fh, output=ofh, disable_crc=disable_crc}
|
||||
|
||||
Decompresses input stream `fh` with the gzip format
|
||||
while writing to output stream `ofh`.
|
||||
`disable_crc` (defaults to `false`) will disable CRC-32 checking
|
||||
to increase speed.
|
||||
gzip is detailed in http://tools.ietf.org/html/rfc1952 .
|
||||
|
||||
DEFLATE.inflate_zlib {input=fh, output=ofh, disable_crc=disable_crc}
|
||||
|
||||
Decompresses input stream `fh` with the zlib format
|
||||
while writing to output stream `ofh`.
|
||||
`disable_crc` (defaults to `false`) will disable CRC-32 checking
|
||||
to increase speed.
|
||||
zlib is detailed in http://tools.ietf.org/html/rfc1950 .
|
||||
|
||||
DEFLATE.adler32(byte, crc) --> rcrc
|
||||
|
||||
Returns adler32 checksum of byte `byte` (number 0..255) appended
|
||||
to string with adler32 checksum `crc`. This is internally used by
|
||||
`inflate_zlib`.
|
||||
ADLER32 in detailed in http://tools.ietf.org/html/rfc1950 .
|
||||
|
||||
COMMAND LINE UTILITY
|
||||
|
||||
A `gunziplua` command line utility (in folder `bin`) is also provided.
|
||||
This mimicks the *nix `gunzip` utility but is a pure Lua implementation
|
||||
that invokes this library. For help do
|
||||
|
||||
gunziplua -h
|
||||
|
||||
DEPENDENCIES
|
||||
|
||||
Requires 'digest.crc32lua' (used for optional CRC-32 checksum checks).
|
||||
https://github.com/davidm/lua-digest-crc32lua
|
||||
|
||||
Will use a bit library ('bit', 'bit32', 'bit.numberlua') if available. This
|
||||
is not that critical for this library but is required by digest.crc32lua.
|
||||
|
||||
'pythonic.optparse' is only required by the optional `gunziplua`
|
||||
command-line utilty for command line parsing.
|
||||
https://github.com/davidm/lua-pythonic-optparse
|
||||
|
||||
INSTALLATION
|
||||
|
||||
Copy the `compress` directory into your LUA_PATH.
|
||||
|
||||
REFERENCES
|
||||
|
||||
[1] DEFLATE Compressed Data Format Specification version 1.3
|
||||
http://tools.ietf.org/html/rfc1951
|
||||
[2] GZIP file format specification version 4.3
|
||||
http://tools.ietf.org/html/rfc1952
|
||||
[3] http://en.wikipedia.org/wiki/DEFLATE
|
||||
[4] pyflate, by Paul Sladen
|
||||
http://www.paul.sladen.org/projects/pyflate/
|
||||
[5] Compress::Zlib::Perl - partial pure Perl implementation of
|
||||
Compress::Zlib
|
||||
http://search.cpan.org/~nwclark/Compress-Zlib-Perl/Perl.pm
|
||||
|
||||
LICENSE
|
||||
|
||||
(c) 2008-2011 David Manura. Licensed under the same terms as Lua (MIT).
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
(end license)
|
||||
--]]
|
||||
|
||||
local M = {_TYPE='module', _NAME='compress.deflatelua', _VERSION='0.3.20111128'}
|
||||
|
||||
local assert = assert
|
||||
local error = error
|
||||
local ipairs = ipairs
|
||||
local pairs = pairs
|
||||
local print = print
|
||||
local require = require
|
||||
local tostring = tostring
|
||||
local type = type
|
||||
local setmetatable = setmetatable
|
||||
local io = io
|
||||
local math = math
|
||||
local table_sort = table.sort
|
||||
local math_max = math.max
|
||||
local string_char = string.char
|
||||
|
||||
--[[
|
||||
Requires the first module listed that exists, else raises like `require`.
|
||||
If a non-string is encountered, it is returned.
|
||||
Second return value is module name loaded (or '').
|
||||
--]]
|
||||
local function requireany(...)
|
||||
local errs = {}
|
||||
for i = 1, select('#', ...) do local name = select(i, ...)
|
||||
if type(name) ~= 'string' then return name, '' end
|
||||
local ok, mod = pcall(require, name)
|
||||
if ok then return mod, name end
|
||||
errs[#errs+1] = mod
|
||||
end
|
||||
error(table.concat(errs, '\n'), 2)
|
||||
end
|
||||
|
||||
|
||||
--local crc32 = require "digest.crc32lua" . crc32_byte
|
||||
--local bit, name_ = requireany('bit', 'bit32', 'bit.numberlua', nil)
|
||||
local bit
|
||||
local crc32
|
||||
|
||||
local DEBUG = false
|
||||
|
||||
-- Whether to use `bit` library functions in current module.
|
||||
-- Unlike the crc32 library, it doesn't make much difference in this module.
|
||||
local NATIVE_BITOPS = (bit ~= nil)
|
||||
|
||||
local band, lshift, rshift
|
||||
if NATIVE_BITOPS then
|
||||
band = bit.band
|
||||
lshift = bit.lshift
|
||||
rshift = bit.rshift
|
||||
end
|
||||
|
||||
|
||||
local function warn(s)
|
||||
io.stderr:write(s, '\n')
|
||||
end
|
||||
|
||||
|
||||
local function debug(...)
|
||||
print('DEBUG', ...)
|
||||
end
|
||||
|
||||
|
||||
local function runtime_error(s, level)
|
||||
level = level or 1
|
||||
error({s}, level+1)
|
||||
end
|
||||
|
||||
|
||||
local function make_outstate(outbs)
|
||||
local outstate = {}
|
||||
outstate.outbs = outbs
|
||||
outstate.window = {}
|
||||
outstate.window_pos = 1
|
||||
return outstate
|
||||
end
|
||||
|
||||
|
||||
local function output(outstate, byte)
|
||||
-- debug('OUTPUT:', s)
|
||||
local window_pos = outstate.window_pos
|
||||
outstate.outbs(byte)
|
||||
outstate.window[window_pos] = byte
|
||||
outstate.window_pos = window_pos % 32768 + 1 -- 32K
|
||||
end
|
||||
|
||||
|
||||
local function noeof(val)
|
||||
return assert(val, 'unexpected end of file')
|
||||
end
|
||||
|
||||
|
||||
local function hasbit(bits, bit)
|
||||
return bits % (bit + bit) >= bit
|
||||
end
|
||||
|
||||
|
||||
local function memoize(f)
|
||||
local mt = {}
|
||||
local t = setmetatable({}, mt)
|
||||
function mt:__index(k)
|
||||
local v = f(k)
|
||||
t[k] = v
|
||||
return v
|
||||
end
|
||||
return t
|
||||
end
|
||||
|
||||
|
||||
-- small optimization (lookup table for powers of 2)
|
||||
local pow2 = memoize(function(n) return 2^n end)
|
||||
|
||||
--local tbits = memoize(
|
||||
-- function(bits)
|
||||
-- return memoize( function(bit) return getbit(bits, bit) end )
|
||||
-- end )
|
||||
|
||||
|
||||
-- weak metatable marking objects as bitstream type
|
||||
local is_bitstream = setmetatable({}, {__mode='k'})
|
||||
|
||||
|
||||
-- DEBUG
|
||||
-- prints LSB first
|
||||
--[[
|
||||
local function bits_tostring(bits, nbits)
|
||||
local s = ''
|
||||
local tmp = bits
|
||||
local function f()
|
||||
local b = tmp % 2 == 1 and 1 or 0
|
||||
s = s .. b
|
||||
tmp = (tmp - b) / 2
|
||||
end
|
||||
if nbits then
|
||||
for i=1,nbits do f() end
|
||||
else
|
||||
while tmp ~= 0 do f() end
|
||||
end
|
||||
|
||||
return s
|
||||
end
|
||||
--]]
|
||||
|
||||
local function bytestream_from_file(fh)
|
||||
local o = {}
|
||||
function o:read()
|
||||
local sb = fh:read(1)
|
||||
if sb then return sb:byte() end
|
||||
end
|
||||
return o
|
||||
end
|
||||
|
||||
|
||||
local function bytestream_from_string(s)
|
||||
local i = 1
|
||||
local o = {}
|
||||
function o:read()
|
||||
local by
|
||||
if i <= #s then
|
||||
by = s:byte(i)
|
||||
i = i + 1
|
||||
end
|
||||
return by
|
||||
end
|
||||
return o
|
||||
end
|
||||
|
||||
|
||||
local function bytestream_from_function(f)
|
||||
local i = 0
|
||||
local buffer = ''
|
||||
local o = {}
|
||||
function o:read()
|
||||
return f()
|
||||
-- i = i + 1
|
||||
-- if i > #buffer then
|
||||
-- buffer = f()
|
||||
-- if not buffer then return end
|
||||
-- i = 1
|
||||
-- end
|
||||
-- return buffer:byte(i,i)
|
||||
end
|
||||
return o
|
||||
end
|
||||
|
||||
|
||||
local function bitstream_from_bytestream(bys)
|
||||
local buf_byte = 0
|
||||
local buf_nbit = 0
|
||||
local o = {}
|
||||
|
||||
function o:nbits_left_in_byte()
|
||||
return buf_nbit
|
||||
end
|
||||
|
||||
if NATIVE_BITOPS then
|
||||
function o:read(nbits)
|
||||
nbits = nbits or 1
|
||||
while buf_nbit < nbits do
|
||||
local byte = bys:read()
|
||||
if not byte then return end -- note: more calls also return nil
|
||||
buf_byte = buf_byte + lshift(byte, buf_nbit)
|
||||
buf_nbit = buf_nbit + 8
|
||||
end
|
||||
local bits
|
||||
if nbits == 0 then
|
||||
bits = 0
|
||||
elseif nbits == 32 then
|
||||
bits = buf_byte
|
||||
buf_byte = 0
|
||||
else
|
||||
bits = band(buf_byte, rshift(0xffffffff, 32 - nbits))
|
||||
buf_byte = rshift(buf_byte, nbits)
|
||||
end
|
||||
buf_nbit = buf_nbit - nbits
|
||||
return bits
|
||||
end
|
||||
else
|
||||
function o:read(nbits)
|
||||
nbits = nbits or 1
|
||||
while buf_nbit < nbits do
|
||||
local byte = bys:read()
|
||||
if not byte then return end -- note: more calls also return nil
|
||||
buf_byte = buf_byte + pow2[buf_nbit] * byte
|
||||
buf_nbit = buf_nbit + 8
|
||||
end
|
||||
local m = pow2[nbits]
|
||||
local bits = buf_byte % m
|
||||
buf_byte = (buf_byte - bits) / m
|
||||
buf_nbit = buf_nbit - nbits
|
||||
return bits
|
||||
end
|
||||
end
|
||||
|
||||
is_bitstream[o] = true
|
||||
|
||||
return o
|
||||
end
|
||||
|
||||
|
||||
local function get_bitstream(o)
|
||||
local bs
|
||||
if is_bitstream[o] then
|
||||
return o
|
||||
elseif io.type(o) == 'file' then
|
||||
bs = bitstream_from_bytestream(bytestream_from_file(o))
|
||||
elseif type(o) == 'string' then
|
||||
bs = bitstream_from_bytestream(bytestream_from_string(o))
|
||||
elseif type(o) == 'function' then
|
||||
bs = bitstream_from_bytestream(bytestream_from_function(o))
|
||||
else
|
||||
runtime_error 'unrecognized type'
|
||||
end
|
||||
return bs
|
||||
end
|
||||
|
||||
|
||||
local function get_obytestream(o)
|
||||
local bs
|
||||
if io.type(o) == 'file' then
|
||||
bs = function(sbyte) o:write(string_char(sbyte)) end
|
||||
elseif type(o) == 'function' then
|
||||
bs = o
|
||||
else
|
||||
runtime_error('unrecognized type: ' .. tostring(o))
|
||||
end
|
||||
return bs
|
||||
end
|
||||
|
||||
|
||||
local function HuffmanTable(init, is_full)
|
||||
local t = {}
|
||||
if is_full then
|
||||
for val,nbits in pairs(init) do
|
||||
if nbits ~= 0 then
|
||||
t[#t+1] = {val=val, nbits=nbits}
|
||||
--debug('*',val,nbits)
|
||||
end
|
||||
end
|
||||
else
|
||||
for i=1,#init-2,2 do
|
||||
local firstval, nbits, nextval = init[i], init[i+1], init[i+2]
|
||||
--debug(val, nextval, nbits)
|
||||
if nbits ~= 0 then
|
||||
for val=firstval,nextval-1 do
|
||||
t[#t+1] = {val=val, nbits=nbits}
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
table_sort(t, function(a,b)
|
||||
return a.nbits == b.nbits and a.val < b.val or a.nbits < b.nbits
|
||||
end)
|
||||
|
||||
-- assign codes
|
||||
local code = 1 -- leading 1 marker
|
||||
local nbits = 0
|
||||
for i,s in ipairs(t) do
|
||||
if s.nbits ~= nbits then
|
||||
code = code * pow2[s.nbits - nbits]
|
||||
nbits = s.nbits
|
||||
end
|
||||
s.code = code
|
||||
--debug('huffman code:', i, s.nbits, s.val, code, bits_tostring(code))
|
||||
code = code + 1
|
||||
end
|
||||
|
||||
local minbits = math.huge
|
||||
local look = {}
|
||||
for i,s in ipairs(t) do
|
||||
minbits = math.min(minbits, s.nbits)
|
||||
look[s.code] = s.val
|
||||
end
|
||||
|
||||
--for _,o in ipairs(t) do
|
||||
-- debug(':', o.nbits, o.val)
|
||||
--end
|
||||
|
||||
-- function t:lookup(bits) return look[bits] end
|
||||
|
||||
local msb = NATIVE_BITOPS and function(bits, nbits)
|
||||
local res = 0
|
||||
for i=1,nbits do
|
||||
res = lshift(res, 1) + band(bits, 1)
|
||||
bits = rshift(bits, 1)
|
||||
end
|
||||
return res
|
||||
end or function(bits, nbits)
|
||||
local res = 0
|
||||
for i=1,nbits do
|
||||
local b = bits % 2
|
||||
bits = (bits - b) / 2
|
||||
res = res * 2 + b
|
||||
end
|
||||
return res
|
||||
end
|
||||
|
||||
local tfirstcode = memoize(
|
||||
function(bits) return pow2[minbits] + msb(bits, minbits) end)
|
||||
|
||||
function t:read(bs)
|
||||
local code = 1 -- leading 1 marker
|
||||
local nbits = 0
|
||||
while 1 do
|
||||
if nbits == 0 then -- small optimization (optional)
|
||||
code = tfirstcode[noeof(bs:read(minbits))]
|
||||
nbits = nbits + minbits
|
||||
else
|
||||
local b = noeof(bs:read())
|
||||
nbits = nbits + 1
|
||||
code = code * 2 + b -- MSB first
|
||||
--[[NATIVE_BITOPS
|
||||
code = lshift(code, 1) + b -- MSB first
|
||||
--]]
|
||||
end
|
||||
--debug('code?', code, bits_tostring(code))
|
||||
local val = look[code]
|
||||
if val then
|
||||
--debug('FOUND', val)
|
||||
return val
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
return t
|
||||
end
|
||||
|
||||
|
||||
local function parse_gzip_header(bs)
|
||||
-- local FLG_FTEXT = 2^0
|
||||
local FLG_FHCRC = 2^1
|
||||
local FLG_FEXTRA = 2^2
|
||||
local FLG_FNAME = 2^3
|
||||
local FLG_FCOMMENT = 2^4
|
||||
|
||||
local id1 = bs:read(8)
|
||||
local id2 = bs:read(8)
|
||||
if id1 ~= 31 or id2 ~= 139 then
|
||||
runtime_error 'not in gzip format'
|
||||
end
|
||||
local cm = bs:read(8) -- compression method
|
||||
local flg = bs:read(8) -- FLaGs
|
||||
local mtime = bs:read(32) -- Modification TIME
|
||||
local xfl = bs:read(8) -- eXtra FLags
|
||||
local os = bs:read(8) -- Operating System
|
||||
|
||||
if DEBUG then
|
||||
debug("CM=", cm)
|
||||
debug("FLG=", flg)
|
||||
debug("MTIME=", mtime)
|
||||
-- debug("MTIME_str=",os.date("%Y-%m-%d %H:%M:%S",mtime)) -- non-portable
|
||||
debug("XFL=", xfl)
|
||||
debug("OS=", os)
|
||||
end
|
||||
|
||||
if not os then runtime_error 'invalid header' end
|
||||
|
||||
if hasbit(flg, FLG_FEXTRA) then
|
||||
local xlen = bs:read(16)
|
||||
local extra = 0
|
||||
for i=1,xlen do
|
||||
extra = bs:read(8)
|
||||
end
|
||||
if not extra then runtime_error 'invalid header' end
|
||||
end
|
||||
|
||||
local function parse_zstring(bs)
|
||||
repeat
|
||||
local by = bs:read(8)
|
||||
if not by then runtime_error 'invalid header' end
|
||||
until by == 0
|
||||
end
|
||||
|
||||
if hasbit(flg, FLG_FNAME) then
|
||||
parse_zstring(bs)
|
||||
end
|
||||
|
||||
if hasbit(flg, FLG_FCOMMENT) then
|
||||
parse_zstring(bs)
|
||||
end
|
||||
|
||||
if hasbit(flg, FLG_FHCRC) then
|
||||
local crc16 = bs:read(16)
|
||||
if not crc16 then runtime_error 'invalid header' end
|
||||
-- IMPROVE: check CRC. where is an example .gz file that
|
||||
-- has this set?
|
||||
if DEBUG then
|
||||
debug("CRC16=", crc16)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local function parse_zlib_header(bs)
|
||||
local cm = bs:read(4) -- Compression Method
|
||||
local cinfo = bs:read(4) -- Compression info
|
||||
local fcheck = bs:read(5) -- FLaGs: FCHECK (check bits for CMF and FLG)
|
||||
local fdict = bs:read(1) -- FLaGs: FDICT (present dictionary)
|
||||
local flevel = bs:read(2) -- FLaGs: FLEVEL (compression level)
|
||||
local cmf = cinfo * 16 + cm -- CMF (Compresion Method and flags)
|
||||
local flg = fcheck + fdict * 32 + flevel * 64 -- FLaGs
|
||||
|
||||
if cm ~= 8 then -- not "deflate"
|
||||
runtime_error("unrecognized zlib compression method: " + cm)
|
||||
end
|
||||
if cinfo > 7 then
|
||||
runtime_error("invalid zlib window size: cinfo=" + cinfo)
|
||||
end
|
||||
local window_size = 2^(cinfo + 8)
|
||||
|
||||
if (cmf*256 + flg) % 31 ~= 0 then
|
||||
runtime_error("invalid zlib header (bad fcheck sum)")
|
||||
end
|
||||
|
||||
if fdict == 1 then
|
||||
runtime_error("FIX:TODO - FDICT not currently implemented")
|
||||
local dictid_ = bs:read(32)
|
||||
end
|
||||
|
||||
return window_size
|
||||
end
|
||||
|
||||
local function parse_huffmantables(bs)
|
||||
local hlit = bs:read(5) -- # of literal/length codes - 257
|
||||
local hdist = bs:read(5) -- # of distance codes - 1
|
||||
local hclen = noeof(bs:read(4)) -- # of code length codes - 4
|
||||
|
||||
local ncodelen_codes = hclen + 4
|
||||
local codelen_init = {}
|
||||
local codelen_vals = {
|
||||
16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15}
|
||||
for i=1,ncodelen_codes do
|
||||
local nbits = bs:read(3)
|
||||
local val = codelen_vals[i]
|
||||
codelen_init[val] = nbits
|
||||
end
|
||||
local codelentable = HuffmanTable(codelen_init, true)
|
||||
|
||||
local function decode(ncodes)
|
||||
local init = {}
|
||||
local nbits
|
||||
local val = 0
|
||||
while val < ncodes do
|
||||
local codelen = codelentable:read(bs)
|
||||
--FIX:check nil?
|
||||
local nrepeat
|
||||
if codelen <= 15 then
|
||||
nrepeat = 1
|
||||
nbits = codelen
|
||||
--debug('w', nbits)
|
||||
elseif codelen == 16 then
|
||||
nrepeat = 3 + noeof(bs:read(2))
|
||||
-- nbits unchanged
|
||||
elseif codelen == 17 then
|
||||
nrepeat = 3 + noeof(bs:read(3))
|
||||
nbits = 0
|
||||
elseif codelen == 18 then
|
||||
nrepeat = 11 + noeof(bs:read(7))
|
||||
nbits = 0
|
||||
else
|
||||
error 'ASSERT'
|
||||
end
|
||||
for i=1,nrepeat do
|
||||
init[val] = nbits
|
||||
val = val + 1
|
||||
end
|
||||
end
|
||||
local huffmantable = HuffmanTable(init, true)
|
||||
return huffmantable
|
||||
end
|
||||
|
||||
local nlit_codes = hlit + 257
|
||||
local ndist_codes = hdist + 1
|
||||
|
||||
local littable = decode(nlit_codes)
|
||||
local disttable = decode(ndist_codes)
|
||||
|
||||
return littable, disttable
|
||||
end
|
||||
|
||||
|
||||
local tdecode_len_base
|
||||
local tdecode_len_nextrabits
|
||||
local tdecode_dist_base
|
||||
local tdecode_dist_nextrabits
|
||||
local function parse_compressed_item(bs, outstate, littable, disttable)
|
||||
local val = littable:read(bs)
|
||||
--debug(val, val < 256 and string_char(val))
|
||||
if val < 256 then -- literal
|
||||
output(outstate, val)
|
||||
elseif val == 256 then -- end of block
|
||||
return true
|
||||
else
|
||||
if not tdecode_len_base then
|
||||
local t = {[257]=3}
|
||||
local skip = 1
|
||||
for i=258,285,4 do
|
||||
for j=i,i+3 do t[j] = t[j-1] + skip end
|
||||
if i ~= 258 then skip = skip * 2 end
|
||||
end
|
||||
t[285] = 258
|
||||
tdecode_len_base = t
|
||||
--for i=257,285 do debug('T1',i,t[i]) end
|
||||
end
|
||||
if not tdecode_len_nextrabits then
|
||||
local t = {}
|
||||
if NATIVE_BITOPS then
|
||||
for i=257,285 do
|
||||
local j = math_max(i - 261, 0)
|
||||
t[i] = rshift(j, 2)
|
||||
end
|
||||
else
|
||||
for i=257,285 do
|
||||
local j = math_max(i - 261, 0)
|
||||
t[i] = (j - (j % 4)) / 4
|
||||
end
|
||||
end
|
||||
t[285] = 0
|
||||
tdecode_len_nextrabits = t
|
||||
--for i=257,285 do debug('T2',i,t[i]) end
|
||||
end
|
||||
local len_base = tdecode_len_base[val]
|
||||
local nextrabits = tdecode_len_nextrabits[val]
|
||||
local extrabits = bs:read(nextrabits)
|
||||
local len = len_base + extrabits
|
||||
|
||||
if not tdecode_dist_base then
|
||||
local t = {[0]=1}
|
||||
local skip = 1
|
||||
for i=1,29,2 do
|
||||
for j=i,i+1 do t[j] = t[j-1] + skip end
|
||||
if i ~= 1 then skip = skip * 2 end
|
||||
end
|
||||
tdecode_dist_base = t
|
||||
--for i=0,29 do debug('T3',i,t[i]) end
|
||||
end
|
||||
if not tdecode_dist_nextrabits then
|
||||
local t = {}
|
||||
if NATIVE_BITOPS then
|
||||
for i=0,29 do
|
||||
local j = math_max(i - 2, 0)
|
||||
t[i] = rshift(j, 1)
|
||||
end
|
||||
else
|
||||
for i=0,29 do
|
||||
local j = math_max(i - 2, 0)
|
||||
t[i] = (j - (j % 2)) / 2
|
||||
end
|
||||
end
|
||||
tdecode_dist_nextrabits = t
|
||||
--for i=0,29 do debug('T4',i,t[i]) end
|
||||
end
|
||||
local dist_val = disttable:read(bs)
|
||||
local dist_base = tdecode_dist_base[dist_val]
|
||||
local dist_nextrabits = tdecode_dist_nextrabits[dist_val]
|
||||
local dist_extrabits = bs:read(dist_nextrabits)
|
||||
local dist = dist_base + dist_extrabits
|
||||
|
||||
--debug('BACK', len, dist)
|
||||
for i=1,len do
|
||||
local pos = (outstate.window_pos - 1 - dist) % 32768 + 1 -- 32K
|
||||
output(outstate, assert(outstate.window[pos], 'invalid distance'))
|
||||
end
|
||||
end
|
||||
return false
|
||||
end
|
||||
|
||||
|
||||
local function parse_block(bs, outstate)
|
||||
local bfinal = bs:read(1)
|
||||
local btype = bs:read(2)
|
||||
|
||||
local BTYPE_NO_COMPRESSION = 0
|
||||
local BTYPE_FIXED_HUFFMAN = 1
|
||||
local BTYPE_DYNAMIC_HUFFMAN = 2
|
||||
local BTYPE_RESERVED_ = 3
|
||||
|
||||
if DEBUG then
|
||||
debug('bfinal=', bfinal)
|
||||
debug('btype=', btype)
|
||||
end
|
||||
|
||||
if btype == BTYPE_NO_COMPRESSION then
|
||||
bs:read(bs:nbits_left_in_byte())
|
||||
local len = bs:read(16)
|
||||
local nlen_ = noeof(bs:read(16))
|
||||
|
||||
for i=1,len do
|
||||
local by = noeof(bs:read(8))
|
||||
output(outstate, by)
|
||||
end
|
||||
elseif btype == BTYPE_FIXED_HUFFMAN or btype == BTYPE_DYNAMIC_HUFFMAN then
|
||||
local littable, disttable
|
||||
if btype == BTYPE_DYNAMIC_HUFFMAN then
|
||||
littable, disttable = parse_huffmantables(bs)
|
||||
else
|
||||
littable = HuffmanTable {0,8, 144,9, 256,7, 280,8, 288,nil}
|
||||
disttable = HuffmanTable {0,5, 32,nil}
|
||||
end
|
||||
|
||||
repeat
|
||||
local is_done = parse_compressed_item(
|
||||
bs, outstate, littable, disttable)
|
||||
until is_done
|
||||
else
|
||||
runtime_error 'unrecognized compression type'
|
||||
end
|
||||
|
||||
return bfinal ~= 0
|
||||
end
|
||||
|
||||
|
||||
function M.inflate(t)
|
||||
local bs = get_bitstream(t.input)
|
||||
local outbs = get_obytestream(t.output)
|
||||
local outstate = make_outstate(outbs)
|
||||
|
||||
repeat
|
||||
local is_final = parse_block(bs, outstate)
|
||||
until is_final
|
||||
end
|
||||
local inflate = M.inflate
|
||||
|
||||
|
||||
function M.gunzip(t)
|
||||
local bs = get_bitstream(t.input)
|
||||
local outbs = get_obytestream(t.output)
|
||||
local disable_crc = t.disable_crc
|
||||
if disable_crc == nil then disable_crc = false end
|
||||
|
||||
parse_gzip_header(bs)
|
||||
|
||||
local data_crc32 = 0
|
||||
|
||||
inflate{input=bs, output=
|
||||
disable_crc and outbs or
|
||||
function(byte)
|
||||
data_crc32 = crc32(byte, data_crc32)
|
||||
outbs(byte)
|
||||
end
|
||||
}
|
||||
|
||||
bs:read(bs:nbits_left_in_byte())
|
||||
|
||||
local expected_crc32 = bs:read(32)
|
||||
local isize = bs:read(32) -- ignored
|
||||
if DEBUG then
|
||||
debug('crc32=', expected_crc32)
|
||||
debug('isize=', isize)
|
||||
end
|
||||
if not disable_crc and data_crc32 then
|
||||
if data_crc32 ~= expected_crc32 then
|
||||
runtime_error('invalid compressed data--crc error')
|
||||
end
|
||||
end
|
||||
if bs:read() then
|
||||
warn 'trailing garbage ignored'
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
function M.adler32(byte, crc)
|
||||
local s1 = crc % 65536
|
||||
local s2 = (crc - s1) / 65536
|
||||
s1 = (s1 + byte) % 65521
|
||||
s2 = (s2 + s1) % 65521
|
||||
return s2*65536 + s1
|
||||
end -- 65521 is the largest prime smaller than 2^16
|
||||
|
||||
|
||||
function M.inflate_zlib(t)
|
||||
local bs = get_bitstream(t.input)
|
||||
local outbs = get_obytestream(t.output)
|
||||
local disable_crc = t.disable_crc
|
||||
if disable_crc == nil then disable_crc = false end
|
||||
|
||||
local window_size_ = parse_zlib_header(bs)
|
||||
|
||||
local data_adler32 = 1
|
||||
|
||||
inflate{input=bs, output=
|
||||
disable_crc and outbs or
|
||||
function(byte)
|
||||
data_adler32 = M.adler32(byte, data_adler32)
|
||||
outbs(byte)
|
||||
end
|
||||
}
|
||||
|
||||
bs:read(bs:nbits_left_in_byte())
|
||||
|
||||
local b3 = bs:read(8)
|
||||
local b2 = bs:read(8)
|
||||
local b1 = bs:read(8)
|
||||
local b0 = bs:read(8)
|
||||
local expected_adler32 = ((b3*256 + b2)*256 + b1)*256 + b0
|
||||
if DEBUG then
|
||||
debug('alder32=', expected_adler32)
|
||||
end
|
||||
if not disable_crc then
|
||||
if data_adler32 ~= expected_adler32 then
|
||||
runtime_error('invalid compressed data--crc error')
|
||||
end
|
||||
end
|
||||
if bs:read() then
|
||||
warn 'trailing garbage ignored'
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
return M
|
||||
40
builder/base64dl.lua
Normal file
40
builder/base64dl.lua
Normal file
@@ -0,0 +1,40 @@
|
||||
_G.requireInjector()
|
||||
|
||||
local Base64 = require('base64')
|
||||
|
||||
local http = _G.http
|
||||
local os = _G.os
|
||||
local shell = _ENV.shell
|
||||
|
||||
local args = { ... }
|
||||
|
||||
if not args[2] then
|
||||
error('Syntax: base64dl <file name> <url>')
|
||||
end
|
||||
|
||||
local c = http.get(args[2])
|
||||
|
||||
if not c then
|
||||
error('unable to open url')
|
||||
end
|
||||
|
||||
local data = c.readAll()
|
||||
c.close()
|
||||
|
||||
print('size: ' .. #data)
|
||||
local decoded = Base64.decode(data)
|
||||
print('decoded: ' .. #decoded)
|
||||
|
||||
local file = io.open(shell.resolve(args[1]), "wb")
|
||||
if not file then
|
||||
error('Unable to open ' .. args[1], 2)
|
||||
end
|
||||
for k,b in ipairs(decoded) do
|
||||
if (k % 1000) == 0 then
|
||||
os.sleep(0)
|
||||
end
|
||||
file:write(b)
|
||||
end
|
||||
|
||||
file:close()
|
||||
print('done')
|
||||
779
builder/builder.lua
Normal file
779
builder/builder.lua
Normal file
@@ -0,0 +1,779 @@
|
||||
if not _G.turtle and not _G.commands then
|
||||
error('Must be run on a turtle or a command computer')
|
||||
end
|
||||
|
||||
_G.requireInjector()
|
||||
|
||||
local Adapter = require('inventoryAdapter')
|
||||
local Event = require('event')
|
||||
local GPS = require('gps')
|
||||
local itemDB = require('itemDB')
|
||||
local Schematic = require('builder.schematic')
|
||||
local TableDB = require('tableDB')
|
||||
local UI = require('ui')
|
||||
local Util = require('util')
|
||||
|
||||
local colors = _G.colors
|
||||
local fs = _G.fs
|
||||
|
||||
local BUILDER_DIR = 'usr/builder'
|
||||
|
||||
local substitutionPage
|
||||
local Builder
|
||||
|
||||
if _G.commands then
|
||||
Builder = require('builder.commands')
|
||||
else
|
||||
Builder = require('builder.turtle')
|
||||
end
|
||||
|
||||
Builder = Builder()
|
||||
Builder.schematic = Schematic()
|
||||
|
||||
local function convertSingleBack(item)
|
||||
if item then
|
||||
item.id = item.name
|
||||
item.dmg = item.damage
|
||||
item.qty = item.count
|
||||
item.max_size = item.maxCount
|
||||
item.display_name = item.displayName
|
||||
end
|
||||
return item
|
||||
end
|
||||
|
||||
local function convertBack(t)
|
||||
for _,v in pairs(t) do
|
||||
convertSingleBack(v)
|
||||
end
|
||||
return t
|
||||
end
|
||||
|
||||
--[[-- SubDB --]]--
|
||||
local subDB = TableDB({
|
||||
fileName = fs.combine(BUILDER_DIR, 'sub.db'),
|
||||
})
|
||||
|
||||
function subDB:load()
|
||||
if fs.exists(self.fileName) then
|
||||
TableDB.load(self)
|
||||
elseif not Builder.isCommandComputer then
|
||||
self:seedDB()
|
||||
end
|
||||
end
|
||||
|
||||
function subDB:seedDB()
|
||||
self.data = {
|
||||
[ "minecraft:redstone_wire:0" ] = "minecraft:redstone:0",
|
||||
[ "minecraft:wall_sign:0" ] = "minecraft:sign:0",
|
||||
[ "minecraft:standing_sign:0" ] = "minecraft:sign:0",
|
||||
[ "minecraft:potatoes:0" ] = "minecraft:potato:0",
|
||||
[ "minecraft:unlit_redstone_torch:0" ] = "minecraft:redstone_torch:0",
|
||||
[ "minecraft:powered_repeater:0" ] = "minecraft:repeater:0",
|
||||
[ "minecraft:unpowered_repeater:0" ] = "minecraft:repeater:0",
|
||||
[ "minecraft:carrots:0" ] = "minecraft:carrot:0",
|
||||
[ "minecraft:cocoa:0" ] = "minecraft:dye:3",
|
||||
[ "minecraft:unpowered_comparator:0" ] = "minecraft:comparator:0",
|
||||
[ "minecraft:powered_comparator:0" ] = "minecraft:comparator:0",
|
||||
[ "minecraft:piston_head:0" ] = "minecraft:air:0",
|
||||
[ "minecraft:piston_extension:0" ] = "minecraft:air:0",
|
||||
[ "minecraft:portal:0" ] = "minecraft:air:0",
|
||||
[ "minecraft:double_wooden_slab:0" ] = "minecraft:planks:0",
|
||||
[ "minecraft:double_wooden_slab:1" ] = "minecraft:planks:1",
|
||||
[ "minecraft:double_wooden_slab:2" ] = "minecraft:planks:2",
|
||||
[ "minecraft:double_wooden_slab:3" ] = "minecraft:planks:3",
|
||||
[ "minecraft:double_wooden_slab:4" ] = "minecraft:planks:4",
|
||||
[ "minecraft:double_wooden_slab:5" ] = "minecraft:planks:5",
|
||||
[ "minecraft:lit_redstone_lamp:0" ] = "minecraft:redstone_lamp:0",
|
||||
[ "minecraft:double_stone_slab:1" ] = "minecraft:sandstone:0",
|
||||
[ "minecraft:double_stone_slab:2" ] = "minecraft:planks:0",
|
||||
[ "minecraft:double_stone_slab:3" ] = "minecraft:cobblestone:0",
|
||||
[ "minecraft:double_stone_slab:4" ] = "minecraft:brick_block:0",
|
||||
[ "minecraft:double_stone_slab:5" ] = "minecraft:stonebrick:0",
|
||||
[ "minecraft:double_stone_slab:6" ] = "minecraft:nether_brick:0",
|
||||
[ "minecraft:double_stone_slab:7" ] = "minecraft:quartz_block:0",
|
||||
[ "minecraft:double_stone_slab:9" ] = "minecraft:sandstone:2",
|
||||
[ "minecraft:double_stone_slab2:0" ] = "minecraft:sandstone:0",
|
||||
[ "minecraft:stone_slab:2" ] = "minecraft:wooden_slab:0",
|
||||
[ "minecraft:wheat:0" ] = "minecraft:wheat_seeds:0",
|
||||
[ "minecraft:flowing_water:0" ] = "minecraft:air:0",
|
||||
[ "minecraft:lit_furnace:0" ] = "minecraft:furnace:0",
|
||||
[ "minecraft:wall_banner:0" ] = "minecraft:banner:0",
|
||||
[ "minecraft:standing_banner:0" ] = "minecraft:banner:0",
|
||||
[ "minecraft:tripwire:0" ] = "minecraft:string:0",
|
||||
[ "minecraft:pumpkin_stem:0" ] = "minecraft:pumpkin_seeds:0",
|
||||
}
|
||||
self.dirty = true
|
||||
self:flush()
|
||||
end
|
||||
|
||||
function subDB:add(s)
|
||||
TableDB.add(self, { s.id, s.dmg }, table.concat({ s.sid, s.sdmg }, ':'))
|
||||
self:flush()
|
||||
end
|
||||
|
||||
function subDB:remove(s)
|
||||
-- TODO: tableDB.remove should take table key
|
||||
TableDB.remove(self, s.id .. ':' .. s.dmg)
|
||||
self:flush()
|
||||
end
|
||||
|
||||
function subDB:extract(s)
|
||||
local id, dmg = s:match('(.+):(%d+)')
|
||||
return id, tonumber(dmg)
|
||||
end
|
||||
|
||||
function subDB:getSubstitutedItem(id, dmg)
|
||||
local sub = TableDB.get(self, { id, dmg })
|
||||
if sub then
|
||||
id, dmg = self:extract(sub)
|
||||
end
|
||||
return { id = id, dmg = dmg }
|
||||
end
|
||||
|
||||
function subDB:lookupBlocksForSub(sid, sdmg)
|
||||
local t = { }
|
||||
for k,v in pairs(self.data) do
|
||||
local id, dmg = self:extract(v)
|
||||
if id == sid and dmg == sdmg then
|
||||
id, dmg = self:extract(k)
|
||||
t[k] = { id = id, dmg = dmg, sid = sid, sdmg = sdmg }
|
||||
end
|
||||
end
|
||||
return t
|
||||
end
|
||||
|
||||
--[[-- blankPage --]]--
|
||||
local blankPage = UI.Page()
|
||||
function blankPage:draw()
|
||||
self:clear(colors.black)
|
||||
self:setCursorPos(1, 1)
|
||||
end
|
||||
|
||||
function blankPage:enable()
|
||||
self:sync()
|
||||
UI.Page.enable(self)
|
||||
end
|
||||
|
||||
--[[-- selectSubstitutionPage --]]--
|
||||
local selectSubstitutionPage = UI.Page({
|
||||
titleBar = UI.TitleBar({
|
||||
title = 'Select a substitution',
|
||||
previousPage = 'listing'
|
||||
}),
|
||||
grid = UI.ScrollingGrid({
|
||||
columns = {
|
||||
{ heading = 'id', key = 'id' },
|
||||
{ heading = 'dmg', key = 'dmg' },
|
||||
},
|
||||
sortColumn = 'id',
|
||||
height = UI.term.height-1,
|
||||
autospace = true,
|
||||
y = 2,
|
||||
}),
|
||||
})
|
||||
|
||||
function selectSubstitutionPage:enable()
|
||||
self.grid:adjustWidth()
|
||||
self.grid:setIndex(1)
|
||||
UI.Page.enable(self)
|
||||
end
|
||||
|
||||
function selectSubstitutionPage:eventHandler(event)
|
||||
if event.type == 'grid_select' then
|
||||
substitutionPage.sub = event.selected
|
||||
UI:setPage(substitutionPage)
|
||||
elseif event.type == 'key' and event.key == 'q' then
|
||||
UI:setPreviousPage()
|
||||
else
|
||||
return UI.Page.eventHandler(self, event)
|
||||
end
|
||||
return true
|
||||
end
|
||||
|
||||
--[[-- substitutionPage --]]--
|
||||
substitutionPage = UI.Page {
|
||||
titleBar = UI.TitleBar {
|
||||
previousPage = true,
|
||||
title = 'Substitute a block'
|
||||
},
|
||||
menuBar = UI.MenuBar {
|
||||
y = 2,
|
||||
buttons = {
|
||||
{ text = 'Accept', event = 'accept', help = 'Accept' },
|
||||
{ text = 'Revert', event = 'revert', help = 'Restore to original' },
|
||||
{ text = 'Air', event = 'air', help = 'Air' },
|
||||
},
|
||||
},
|
||||
info = UI.Window { y = 4, width = UI.term.width, height = 3 },
|
||||
grid = UI.ScrollingGrid {
|
||||
columns = {
|
||||
{ heading = 'Name', key = 'display_name', width = UI.term.width-9 },
|
||||
{ heading = 'Qty', key = 'fQty', width = 5 },
|
||||
},
|
||||
sortColumn = 'display_name',
|
||||
height = UI.term.height-7,
|
||||
y = 7,
|
||||
},
|
||||
throttle = UI.Throttle { },
|
||||
statusBar = UI.StatusBar { }
|
||||
}
|
||||
|
||||
substitutionPage.menuBar:add({
|
||||
filterLabel = UI.Text({
|
||||
value = 'Search',
|
||||
x = UI.term.width-14,
|
||||
}),
|
||||
filter = UI.TextEntry({
|
||||
x = UI.term.width-7,
|
||||
width = 7,
|
||||
})
|
||||
})
|
||||
|
||||
function substitutionPage.info:draw()
|
||||
local sub = self.parent.sub
|
||||
local inName = itemDB:getName({ name = sub.id, damage = sub.dmg })
|
||||
local outName = ''
|
||||
if sub.sid then
|
||||
outName = itemDB:getName({ name = sub.sid, damage = sub.sdmg })
|
||||
end
|
||||
|
||||
self:clear()
|
||||
self:setCursorPos(1, 1)
|
||||
self:print(' Replace ' .. inName .. '\n')
|
||||
--self:print(' ' .. sub.id .. ':' .. sub.dmg .. '\n', nil, colors.yellow)
|
||||
self:print(' With ' .. outName)
|
||||
end
|
||||
|
||||
function substitutionPage:enable()
|
||||
self.allItems = convertBack(Builder.itemAdapter:refresh())
|
||||
self.grid.values = self.allItems
|
||||
for _,item in pairs(self.grid.values) do
|
||||
item.key = item.id .. ':' .. item.dmg
|
||||
item.lname = string.lower(item.display_name)
|
||||
item.fQty = Util.toBytes(item.qty)
|
||||
end
|
||||
self.grid:update()
|
||||
|
||||
self.menuBar.filter.value = ''
|
||||
self.menuBar.filter.pos = 1
|
||||
self:setFocus(self.menuBar.filter)
|
||||
UI.Page.enable(self)
|
||||
end
|
||||
|
||||
function substitutionPage:applySubstitute(id, dmg)
|
||||
self.sub.sid = id
|
||||
self.sub.sdmg = dmg
|
||||
end
|
||||
|
||||
function substitutionPage:eventHandler(event)
|
||||
if event.type == 'grid_focus_row' then
|
||||
local s = string.format('%s:%d',
|
||||
event.selected.id,
|
||||
event.selected.dmg)
|
||||
|
||||
self.statusBar:setStatus(s)
|
||||
self.statusBar:draw()
|
||||
|
||||
elseif event.type == 'grid_select' then
|
||||
self:applySubstitute(event.selected.id, event.selected.dmg)
|
||||
self.info:draw()
|
||||
|
||||
elseif event.type == 'text_change' then
|
||||
local text = event.text
|
||||
if #text == 0 then
|
||||
self.grid.values = self.allItems
|
||||
else
|
||||
self.grid.values = { }
|
||||
for _,item in pairs(self.allItems) do
|
||||
if string.find(item.lname, text) then
|
||||
table.insert(self.grid.values, item)
|
||||
end
|
||||
end
|
||||
end
|
||||
self.grid:update()
|
||||
self.grid:setIndex(1)
|
||||
self.grid:draw()
|
||||
|
||||
elseif event.type == 'accept' or event.type == 'air' or event.type == 'revert' then
|
||||
self.statusBar:setStatus('Saving changes...')
|
||||
self.statusBar:draw()
|
||||
self:sync()
|
||||
|
||||
if event.type == 'air' then
|
||||
self:applySubstitute('minecraft:air', 0)
|
||||
end
|
||||
|
||||
if event.type == 'revert' then
|
||||
subDB:remove(self.sub)
|
||||
elseif not self.sub.sid then
|
||||
self.statusBar:setStatus('Select a substition')
|
||||
self.statusBar:draw()
|
||||
return UI.Page.eventHandler(self, event)
|
||||
else
|
||||
subDB:add(self.sub)
|
||||
end
|
||||
|
||||
self.throttle:enable()
|
||||
Builder:reloadSchematic(function() self.throttle:update() end)
|
||||
self.throttle:disable()
|
||||
UI:setPage('listing')
|
||||
|
||||
elseif event.type == 'cancel' then
|
||||
UI:setPreviousPage()
|
||||
end
|
||||
|
||||
return UI.Page.eventHandler(self, event)
|
||||
end
|
||||
|
||||
--[[-- ListingPage --]]--
|
||||
local listingPage = UI.Page({
|
||||
titleBar = UI.TitleBar({
|
||||
title = 'Supply List',
|
||||
previousPage = 'start'
|
||||
}),
|
||||
menuBar = UI.MenuBar({
|
||||
y = 2,
|
||||
buttons = {
|
||||
{ text = 'Craft', event = 'craft', help = 'Request crafting' },
|
||||
{ text = 'Refresh', event = 'refresh', help = 'Refresh inventory' },
|
||||
{ text = 'Toggle', event = 'toggle', help = 'Toggles needed blocks' },
|
||||
{ text = 'Substitute', event = 'edit', help = 'Substitute a block' },
|
||||
}
|
||||
}),
|
||||
grid = UI.ScrollingGrid({
|
||||
columns = {
|
||||
{ heading = 'Name', key = 'display_name', width = UI.term.width - 14 },
|
||||
{ heading = 'Need', key = 'need', width = 5 },
|
||||
{ heading = 'Have', key = 'qty', width = 5 },
|
||||
},
|
||||
sortColumn = 'display_name',
|
||||
y = 3,
|
||||
height = UI.term.height-3,
|
||||
help = 'Set a block type or pick a substitute block'
|
||||
}),
|
||||
accelerators = {
|
||||
q = 'menu',
|
||||
c = 'craft',
|
||||
r = 'refresh',
|
||||
t = 'toggle',
|
||||
},
|
||||
statusBar = UI.StatusBar(),
|
||||
fullList = true
|
||||
})
|
||||
|
||||
function listingPage:enable(throttle)
|
||||
listingPage:refresh(throttle)
|
||||
UI.Page.enable(self)
|
||||
end
|
||||
|
||||
function listingPage:eventHandler(event)
|
||||
if event.type == 'craft' then
|
||||
local s = self.grid:getSelected()
|
||||
local item = convertSingleBack(Builder.itemAdapter:getItemInfo({
|
||||
name = s.id,
|
||||
damage = s.dmg,
|
||||
nbtHash = s.nbt_hash,
|
||||
}))
|
||||
if item and item.is_craftable then
|
||||
local qty = math.max(0, s.need - item.qty)
|
||||
|
||||
if item and Builder.itemAdapter.craftItems then
|
||||
Builder.itemAdapter:craftItems({{ name = s.id, damage = s.dmg, nbtHash = s.nbt_hash, count = qty }})
|
||||
local name = s.display_name or s.id
|
||||
self.statusBar:timedStatus('Requested ' .. qty .. ' ' .. name, 3)
|
||||
end
|
||||
else
|
||||
self.statusBar:timedStatus('Unable to craft')
|
||||
end
|
||||
|
||||
elseif event.type == 'grid_focus_row' then
|
||||
self.statusBar:setStatus(event.selected.id .. ':' .. event.selected.dmg)
|
||||
self.statusBar:draw()
|
||||
|
||||
elseif event.type == 'refresh' then
|
||||
self:refresh()
|
||||
self:draw()
|
||||
self.statusBar:timedStatus('Refreshed ', 3)
|
||||
|
||||
elseif event.type == 'toggle' then
|
||||
self.fullList = not self.fullList
|
||||
self:refresh()
|
||||
self:draw()
|
||||
|
||||
elseif event.type == 'menu' then
|
||||
UI:setPage('start')
|
||||
|
||||
elseif event.type == 'edit' or event.type == 'grid_select' then
|
||||
self:manageBlock(self.grid:getSelected())
|
||||
|
||||
elseif event.type == 'focus_change' then
|
||||
if event.focused.help then
|
||||
self.statusBar:timedStatus(event.focused.help, 3)
|
||||
end
|
||||
end
|
||||
|
||||
return UI.Page.eventHandler(self, event)
|
||||
end
|
||||
|
||||
function listingPage.grid:getDisplayValues(row)
|
||||
row = Util.shallowCopy(row)
|
||||
row.need = Util.toBytes(row.need)
|
||||
row.qty = Util.toBytes(row.qty)
|
||||
return row
|
||||
end
|
||||
|
||||
function listingPage.grid:getRowTextColor(row, selected)
|
||||
if row.is_craftable then
|
||||
return colors.yellow
|
||||
end
|
||||
return UI.Grid:getRowTextColor(row, selected)
|
||||
end
|
||||
|
||||
function listingPage:refresh(throttle)
|
||||
local supplyList = Builder:getBlockCounts()
|
||||
|
||||
Builder.itemAdapter:refresh(throttle)
|
||||
|
||||
for _,b in pairs(supplyList) do
|
||||
if b.need > 0 then
|
||||
local item = convertSingleBack(Builder.itemAdapter:getItemInfo({
|
||||
name = b.id,
|
||||
damage = b.dmg,
|
||||
nbtHash = b.nbt_hash,
|
||||
}))
|
||||
|
||||
if item then
|
||||
b.display_name = item.display_name
|
||||
b.qty = item.qty
|
||||
b.is_craftable = item.is_craftable
|
||||
else
|
||||
b.display_name = itemDB:getName({ name = b.id, damage = b.dmg })
|
||||
end
|
||||
end
|
||||
if throttle then
|
||||
throttle()
|
||||
end
|
||||
end
|
||||
|
||||
if self.fullList then
|
||||
self.grid:setValues(supplyList)
|
||||
else
|
||||
local t = {}
|
||||
for _,b in pairs(supplyList) do
|
||||
if self.fullList or b.qty < b.need then
|
||||
table.insert(t, b)
|
||||
end
|
||||
end
|
||||
self.grid:setValues(t)
|
||||
end
|
||||
self.grid:setIndex(1)
|
||||
end
|
||||
|
||||
function listingPage:manageBlock(selected)
|
||||
local substitutes = subDB:lookupBlocksForSub(selected.id, selected.dmg)
|
||||
|
||||
if Util.empty(substitutes) then
|
||||
substitutionPage.sub = { id = selected.id, dmg = selected.dmg }
|
||||
UI:setPage(substitutionPage)
|
||||
elseif Util.size(substitutes) == 1 then
|
||||
local _,sub = next(substitutes)
|
||||
substitutionPage.sub = sub
|
||||
UI:setPage(substitutionPage)
|
||||
else
|
||||
selectSubstitutionPage.selected = selected
|
||||
selectSubstitutionPage.grid:setValues(substitutes)
|
||||
UI:setPage(selectSubstitutionPage)
|
||||
end
|
||||
end
|
||||
|
||||
--[[-- startPage --]]--
|
||||
local wy = 2
|
||||
local my = 3
|
||||
|
||||
if UI.term.width < 30 then
|
||||
wy = 9
|
||||
my = 2
|
||||
end
|
||||
|
||||
local startPage = UI.Page {
|
||||
window = UI.Window {
|
||||
x = UI.term.width-16,
|
||||
y = wy,
|
||||
width = 16,
|
||||
height = 9,
|
||||
backgroundColor = colors.gray,
|
||||
grid = UI.Grid {
|
||||
columns = {
|
||||
{ heading = 'Name', key = 'name', width = 6 },
|
||||
{ heading = 'Value', key = 'value', width = 7 },
|
||||
},
|
||||
disableHeader = true,
|
||||
x = 1,
|
||||
y = 2,
|
||||
width = 16,
|
||||
height = 9,
|
||||
inactive = true,
|
||||
backgroundColor = colors.gray
|
||||
},
|
||||
},
|
||||
menu = UI.Menu {
|
||||
x = 2,
|
||||
y = my,
|
||||
height = 7,
|
||||
backgroundColor = UI.Page.defaults.backgroundColor,
|
||||
menuItems = {
|
||||
{ prompt = 'Set starting level', event = 'startLevel' },
|
||||
{ prompt = 'Set starting block', event = 'startBlock' },
|
||||
{ prompt = 'Set starting point', event = 'startPoint' },
|
||||
{ prompt = 'Supply list', event = 'assignBlocks' },
|
||||
{ prompt = 'Toggle mode', event = 'toggleMode' },
|
||||
{ prompt = 'Begin', event = 'begin' },
|
||||
{ prompt = 'Quit', event = 'quit' }
|
||||
}
|
||||
},
|
||||
throttle = UI.Throttle { },
|
||||
accelerators = {
|
||||
x = 'test',
|
||||
q = 'quit'
|
||||
}
|
||||
}
|
||||
|
||||
function startPage:draw()
|
||||
local t = {
|
||||
{ name = 'mode', value = Builder.mode },
|
||||
{ name = 'start', value = Builder.index },
|
||||
{ name = 'blocks', value = #Builder.schematic.blocks },
|
||||
{ name = 'length', value = Builder.schematic.length },
|
||||
{ name = 'width', value = Builder.schematic.width },
|
||||
{ name = 'height', value = Builder.schematic.height },
|
||||
}
|
||||
|
||||
self.window.grid:setValues(t)
|
||||
UI.Page.draw(self)
|
||||
end
|
||||
|
||||
function startPage:enable()
|
||||
self:setFocus(self.menu)
|
||||
UI.Page.enable(self)
|
||||
end
|
||||
|
||||
function startPage:eventHandler(event)
|
||||
if event.type == 'startLevel' then
|
||||
local dialog = UI.Dialog({
|
||||
title = 'Enter Starting Level',
|
||||
height = 7,
|
||||
form = UI.Form {
|
||||
y = 3, x = 2, height = 4,
|
||||
text = UI.Text({ x = 5, y = 1, textColor = colors.gray,
|
||||
value = '0 - ' .. Builder.schematic.height }),
|
||||
textEntry = UI.TextEntry({ x = 15, y = 1, '0 - 11', width = 7 }),
|
||||
},
|
||||
statusBar = UI.StatusBar(),
|
||||
})
|
||||
|
||||
function dialog:eventHandler(event)
|
||||
if event.type == 'form_complete' then
|
||||
local l = tonumber(self.form.textEntry.value)
|
||||
if l and l < Builder.schematic.height and l >= 0 then
|
||||
for k,v in pairs(Builder.schematic.blocks) do
|
||||
if v.y >= l then
|
||||
Builder.index = k
|
||||
Builder:saveProgress(Builder.index)
|
||||
UI:setPreviousPage()
|
||||
break
|
||||
end
|
||||
end
|
||||
else
|
||||
self.statusBar:timedStatus('Invalid Level', 3)
|
||||
end
|
||||
elseif event.type == 'form_cancel' or event.type == 'cancel' then
|
||||
UI:setPreviousPage()
|
||||
else
|
||||
return UI.Dialog.eventHandler(self, event)
|
||||
end
|
||||
return true
|
||||
end
|
||||
|
||||
dialog:setFocus(dialog.form.textEntry)
|
||||
UI:setPage(dialog)
|
||||
|
||||
elseif event.type == 'startBlock' then
|
||||
local dialog = UI.Dialog {
|
||||
title = 'Enter Block Number',
|
||||
height = 7,
|
||||
form = UI.Form {
|
||||
y = 3, x = 2, height = 4,
|
||||
text = UI.Text { x = 2, y = 1,
|
||||
value = '1 - ' .. #Builder.schematic.blocks, textColor = colors.gray },
|
||||
textEntry = UI.TextEntry { x = 16, y = 1,
|
||||
value = tostring(Builder.index), width = 10, limit = 8 }
|
||||
},
|
||||
statusBar = UI.StatusBar(),
|
||||
}
|
||||
|
||||
function dialog:eventHandler(event)
|
||||
if event.type == 'form_complete' then
|
||||
local bn = tonumber(self.form.textEntry.value)
|
||||
if bn and bn < #Builder.schematic.blocks and bn >= 0 then
|
||||
Builder.index = bn
|
||||
Builder:saveProgress(Builder.index)
|
||||
UI:setPreviousPage()
|
||||
else
|
||||
self.statusBar:timedStatus('Invalid Block', 3)
|
||||
end
|
||||
elseif event.type == 'form_cancel' or event.type == 'cancel' then
|
||||
UI:setPreviousPage()
|
||||
else
|
||||
return UI.Dialog.eventHandler(self, event)
|
||||
end
|
||||
return true
|
||||
end
|
||||
|
||||
dialog:setFocus(dialog.form.textEntry)
|
||||
UI:setPage(dialog)
|
||||
|
||||
elseif event.type == 'startPoint' then
|
||||
local loc = Util.shallowCopy(Builder.loc)
|
||||
if not loc.x then
|
||||
if _G.turtle then
|
||||
local pt = GPS.getPoint()
|
||||
if pt then
|
||||
loc.x = pt.x
|
||||
loc.y = pt.y
|
||||
loc.z = pt.z
|
||||
end
|
||||
elseif _G.commands then
|
||||
loc.x, loc.y, loc.z = _G.commands.getBlockPosition()
|
||||
end
|
||||
end
|
||||
|
||||
local dialog = UI.Dialog {
|
||||
title = 'Set starting point',
|
||||
height = 11,
|
||||
width = 30,
|
||||
form = UI.Form {
|
||||
y = 2, x = 2, ey = -2,
|
||||
values = loc,
|
||||
text1 = UI.Text {
|
||||
x = 1, y = 2, value = 'Turtle location' },
|
||||
xLoc = UI.TextEntry {
|
||||
x = 1, y = 3, formKey = 'x', width = 7, limit = 16, shadowText = 'x', required = true },
|
||||
yLoc = UI.TextEntry {
|
||||
x = 9, y = 3, formKey = 'y', width = 7, limit = 16, shadowText = 'y', required = true },
|
||||
zLoc = UI.TextEntry {
|
||||
x = 17, y = 3, formKey = 'z', width = 7, limit = 16, shadowText = 'z', required = true },
|
||||
text2 = UI.Text {
|
||||
x = 1, y = 5, value = 'Starting Point' },
|
||||
xrLoc = UI.TextEntry {
|
||||
x = 1, y = 6, formKey = 'rx', width = 7, limit = 16, shadowText = 'x', required = true },
|
||||
yrLoc = UI.TextEntry {
|
||||
x = 9, y = 6, formKey = 'ry', width = 7, limit = 16, shadowText = 'y', required = true },
|
||||
zrLoc = UI.TextEntry {
|
||||
x = 17, y = 6, formKey = 'rz', width = 7, limit = 16, shadowText = 'z', required = true },
|
||||
revert = UI.Button {
|
||||
x = 1, y = -2, text = 'Revert', event = 'revert' },
|
||||
},
|
||||
statusBar = UI.StatusBar({ values = 'Optional start point'}),
|
||||
}
|
||||
|
||||
function dialog:eventHandler(event)
|
||||
if event.type == 'form_complete' then
|
||||
for k,v in pairs(loc) do
|
||||
Builder.loc[k] = tonumber(v)
|
||||
end
|
||||
Builder:saveProgress(Builder.index)
|
||||
UI:setPreviousPage()
|
||||
elseif event.type == 'revert' then
|
||||
Builder.loc = { }
|
||||
Builder:saveProgress(Builder.index)
|
||||
UI:setPreviousPage()
|
||||
elseif event.type == 'form_invalid' then
|
||||
self.statusBar:setStatus(event.message)
|
||||
elseif event.type == 'form_cancel' or event.type == 'cancel' then
|
||||
UI:setPreviousPage()
|
||||
else
|
||||
return UI.Dialog.eventHandler(self, event)
|
||||
end
|
||||
return true
|
||||
end
|
||||
|
||||
UI:setPage(dialog)
|
||||
|
||||
elseif event.type == 'assignBlocks' then
|
||||
-- this might be an approximation of the blocks needed
|
||||
-- as the current level's route may or may not have been
|
||||
-- computed
|
||||
Builder:dumpInventory()
|
||||
UI:setPage('listing', function() self.throttle:update() end)
|
||||
self.throttle:disable()
|
||||
|
||||
elseif event.type == 'toggleMode' then
|
||||
if Builder.mode == 'build' then
|
||||
if Builder.index == 1 then
|
||||
Builder.index = #Builder.schematic.blocks
|
||||
end
|
||||
Builder.mode = 'destroy'
|
||||
else
|
||||
if Builder.index == #Builder.schematic.blocks then
|
||||
Builder.index = 1
|
||||
end
|
||||
Builder.mode = 'build'
|
||||
end
|
||||
self:draw()
|
||||
|
||||
elseif event.type == 'begin' then
|
||||
UI:setPage('blank')
|
||||
self:sync()
|
||||
|
||||
print('Reloading schematic')
|
||||
Builder:reloadSchematic(Util.throttle())
|
||||
Builder:begin()
|
||||
|
||||
elseif event.type == 'quit' then
|
||||
UI.term:reset()
|
||||
Event.exitPullEvents()
|
||||
end
|
||||
|
||||
return UI.Page.eventHandler(self, event)
|
||||
end
|
||||
|
||||
--[[-- startup logic --]]--
|
||||
local args = {...}
|
||||
if #args < 1 then
|
||||
error('supply file name')
|
||||
end
|
||||
|
||||
Builder.itemAdapter = Adapter.wrap({ side = 'bottom', direction = 'up' })
|
||||
if not Builder.itemAdapter then
|
||||
error('A chest or ME interface must be below turtle')
|
||||
end
|
||||
|
||||
subDB:load()
|
||||
|
||||
UI.term:reset()
|
||||
print('Loading schematic')
|
||||
Builder.schematic:load(args[1])
|
||||
print('Substituting blocks')
|
||||
|
||||
Builder.subDB = subDB
|
||||
Builder:substituteBlocks(Util.throttle())
|
||||
|
||||
if not fs.exists(BUILDER_DIR) then
|
||||
fs.makeDir(BUILDER_DIR)
|
||||
end
|
||||
|
||||
Builder:loadProgress(Builder.schematic.filename .. '.progress')
|
||||
|
||||
Event.on('build', function()
|
||||
Builder:build()
|
||||
end)
|
||||
|
||||
UI:setPages({
|
||||
listing = listingPage,
|
||||
start = startPage,
|
||||
blank = blankPage
|
||||
})
|
||||
|
||||
UI:setPage('start')
|
||||
|
||||
UI:pullEvents()
|
||||
437
builder/supplier.lua
Normal file
437
builder/supplier.lua
Normal file
@@ -0,0 +1,437 @@
|
||||
_G.requireInjector()
|
||||
|
||||
local Event = require('event')
|
||||
local Logger = require('logger')
|
||||
local MEProvider = require('meProvider')
|
||||
local Message = require('message')
|
||||
local Point = require('point')
|
||||
local TableDB = require('tableDB')
|
||||
local Util = require('util')
|
||||
|
||||
local device = _G.device
|
||||
local os = _G.os
|
||||
local turtle = _G.turtle
|
||||
|
||||
--[[
|
||||
A supplier turtle for the builder turtle. For larger builds, use
|
||||
ender modems.
|
||||
|
||||
Setup:
|
||||
|
||||
1. chest or ME interface at level 0 (bottom of build area)
|
||||
2. builder turtle on top facing the build area
|
||||
3. If facing the build turtle, the supplier turtle is to the right
|
||||
pointing at the chest/interface
|
||||
]]--
|
||||
|
||||
local ChestProvider = require('chestProvider')
|
||||
if Util.getVersion() == 1.8 then
|
||||
ChestProvider = require('chestProvider18')
|
||||
end
|
||||
|
||||
if not device.wireless_modem then
|
||||
error('No wireless modem detected')
|
||||
end
|
||||
|
||||
Logger.filter('modem_send', 'event', 'ui')
|
||||
Logger.setWirelessLogging()
|
||||
|
||||
local __BUILDER_ID = 6
|
||||
local itemInfoDB
|
||||
|
||||
local Builder = {
|
||||
version = '1.70',
|
||||
ccVersion = nil,
|
||||
slots = { },
|
||||
index = 1,
|
||||
fuelItem = { id = 'minecraft:coal', dmg = 0 },
|
||||
resupplying = true,
|
||||
ready = true,
|
||||
}
|
||||
|
||||
--[[-- maxStackDB --]]--
|
||||
local maxStackDB = TableDB({
|
||||
fileName = 'maxstack.db',
|
||||
tabledef = {
|
||||
autokeys = false,
|
||||
type = 'simple',
|
||||
columns = {
|
||||
{ label = 'Key', type = 'key', length = 8 },
|
||||
{ label = 'Quantity', type = 'number', length = 2 }
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
function maxStackDB:get(id, dmg)
|
||||
return self.data[id .. ':' .. dmg] or 64
|
||||
end
|
||||
|
||||
function Builder:dumpInventory()
|
||||
|
||||
local success = true
|
||||
|
||||
for i = 1, 16 do
|
||||
local qty = turtle.getItemCount(i)
|
||||
if qty > 0 then
|
||||
self.itemProvider:insert(i, qty)
|
||||
end
|
||||
if turtle.getItemCount(i) ~= 0 then
|
||||
success = false
|
||||
end
|
||||
end
|
||||
turtle.select(1)
|
||||
|
||||
return success
|
||||
end
|
||||
|
||||
function Builder:dumpInventoryWithCheck()
|
||||
while not self:dumpInventory() do
|
||||
Builder:log('Unable to dump inventory')
|
||||
print('Provider is full or missing - make space or replace')
|
||||
print('Press enter to continue')
|
||||
--turtle.setHeading(0)
|
||||
self.ready = false
|
||||
_G.read()
|
||||
end
|
||||
self.ready = true
|
||||
end
|
||||
|
||||
function Builder:autocraft(supplies)
|
||||
local t = { }
|
||||
|
||||
for _,s in pairs(supplies) do
|
||||
local key = s.id .. ':' .. s.dmg
|
||||
local item = t[key]
|
||||
if not item then
|
||||
item = {
|
||||
id = s.id,
|
||||
dmg = s.dmg,
|
||||
qty = 0,
|
||||
}
|
||||
t[key] = item
|
||||
end
|
||||
item.qty = item.qty + (s.need-s.qty)
|
||||
end
|
||||
|
||||
Builder.itemProvider:craftItems(t)
|
||||
end
|
||||
|
||||
function Builder:refuel()
|
||||
while turtle.getFuelLevel() < 4000 and self.fuelItem do
|
||||
Builder:log('Refueling')
|
||||
turtle.select(1)
|
||||
self.itemProvider:provide(self.fuelItem, 64, 1)
|
||||
if turtle.getItemCount(1) == 0 then
|
||||
Builder:log('Out of fuel, add coal to chest/ME system')
|
||||
--turtle.setHeading(0)
|
||||
os.sleep(5)
|
||||
else
|
||||
turtle.refuel(64)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function Builder:log(...)
|
||||
Logger.log('supplier', ...)
|
||||
Util.print(...)
|
||||
end
|
||||
|
||||
function Builder:getSupplies()
|
||||
|
||||
Builder.itemProvider:refresh()
|
||||
|
||||
local t = { }
|
||||
for _,s in ipairs(self.slots) do
|
||||
if s.need > 0 then
|
||||
local item = Builder.itemProvider:getItemInfo(s)
|
||||
if item then
|
||||
if item.name then
|
||||
s.name = item.name
|
||||
end
|
||||
|
||||
local qty = math.min(s.need-s.qty, item.qty)
|
||||
|
||||
if qty + s.qty > item.max_size then
|
||||
maxStackDB:add({ s.id, s.dmg }, item.max_size)
|
||||
maxStackDB.dirty = true
|
||||
maxStackDB:flush()
|
||||
qty = item.max_size
|
||||
s.need = qty
|
||||
end
|
||||
if qty > 0 then
|
||||
self.itemProvider:provide(item, qty, s.index)
|
||||
s.qty = turtle.getItemCount(s.index)
|
||||
end
|
||||
end
|
||||
end
|
||||
if s.qty < s.need then
|
||||
table.insert(t, s)
|
||||
local name = s.name or s.id .. ':' .. s.dmg
|
||||
local item = itemInfoDB:get({ s.id, s.dmg })
|
||||
if item then
|
||||
name = item.displayName
|
||||
end
|
||||
|
||||
Builder:log('Need %d %s', s.need - s.qty, name)
|
||||
end
|
||||
end
|
||||
|
||||
return t
|
||||
end
|
||||
|
||||
local function moveTowardsX(dx)
|
||||
|
||||
local direction = dx - turtle.point.x
|
||||
local move
|
||||
|
||||
if direction == 0 then
|
||||
return false
|
||||
end
|
||||
|
||||
if direction > 0 and turtle.point.heading == 0 or
|
||||
direction < 0 and turtle.point.heading == 2 then
|
||||
move = turtle.forward
|
||||
else
|
||||
move = turtle.back
|
||||
end
|
||||
|
||||
return move()
|
||||
end
|
||||
|
||||
local function moveTowardsZ(dz)
|
||||
|
||||
local direction = dz - turtle.point.z
|
||||
local move
|
||||
|
||||
if direction == 0 then
|
||||
return false
|
||||
end
|
||||
|
||||
if direction > 0 and turtle.point.heading == 1 or
|
||||
direction < 0 and turtle.point.heading == 3 then
|
||||
move = turtle.forward
|
||||
else
|
||||
move = turtle.back
|
||||
end
|
||||
|
||||
return move()
|
||||
end
|
||||
|
||||
function Builder:finish()
|
||||
Builder.resupplying = true
|
||||
Builder.ready = false
|
||||
if turtle.gotoLocation('supplies') then
|
||||
turtle.setHeading(1)
|
||||
os.sleep(.1) -- random 'Computer is not connected' error...
|
||||
Builder:dumpInventory()
|
||||
Event.exitPullEvents()
|
||||
print('Finished')
|
||||
end
|
||||
end
|
||||
|
||||
function Builder:gotoBuilder()
|
||||
if Builder.lastPoint then
|
||||
turtle.setStatus('tracking')
|
||||
while true do
|
||||
local pt = Point.copy(Builder.lastPoint)
|
||||
pt.y = pt.y + 3
|
||||
if turtle.point.y ~= pt.y then
|
||||
turtle.gotoY(pt.y)
|
||||
else
|
||||
local distance = Point.turtleDistance(turtle.point, pt)
|
||||
if distance <= 3 then
|
||||
Builder:log('Synchronized')
|
||||
break
|
||||
end
|
||||
|
||||
if turtle.point.heading % 2 == 0 then
|
||||
if turtle.point.x == pt.x then
|
||||
turtle.headTowardsZ(pt.z)
|
||||
moveTowardsZ(pt.z)
|
||||
else
|
||||
moveTowardsX(pt.x)
|
||||
end
|
||||
elseif turtle.point.z ~= pt.z then
|
||||
moveTowardsZ(pt.z)
|
||||
else
|
||||
turtle.headTowardsX(pt.x)
|
||||
moveTowardsX(pt.x)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Message.addHandler('builder',
|
||||
function(_, id, msg)
|
||||
if not id or id ~= __BUILDER_ID then
|
||||
return
|
||||
end
|
||||
|
||||
if not Builder.resupplying then
|
||||
local pt = msg.contents
|
||||
pt.y = pt.y + 3
|
||||
|
||||
turtle.setStatus('supervising')
|
||||
turtle.gotoYfirst(pt)
|
||||
end
|
||||
end)
|
||||
|
||||
Message.addHandler('supplyList',
|
||||
function(_, id, msg)
|
||||
if not id or id ~= __BUILDER_ID then
|
||||
return
|
||||
end
|
||||
|
||||
turtle.setStatus('resupplying')
|
||||
Builder.resupplying = true
|
||||
Builder.slots = msg.contents.slots
|
||||
Builder.slotUid = msg.contents.uid
|
||||
|
||||
Builder:log('Received supply list ' .. Builder.slotUid)
|
||||
|
||||
os.sleep(0)
|
||||
if not turtle.gotoLocation('supplies') then
|
||||
Builder:log('Failed to go to supply location')
|
||||
Builder.ready = false
|
||||
Event.exitPullEvents()
|
||||
end
|
||||
turtle.setHeading(1)
|
||||
os.sleep(.2) -- random 'Computer is not connected' error...
|
||||
Builder:dumpInventoryWithCheck()
|
||||
Builder:refuel()
|
||||
|
||||
while true do
|
||||
local supplies = Builder:getSupplies()
|
||||
if #supplies == 0 then
|
||||
break
|
||||
end
|
||||
Builder:autocraft(supplies)
|
||||
turtle.setStatus('waiting')
|
||||
os.sleep(5)
|
||||
end
|
||||
Builder:log('Got all supplies')
|
||||
os.sleep(0)
|
||||
Builder:gotoBuilder()
|
||||
Builder.resupplying = false
|
||||
end)
|
||||
|
||||
Message.addHandler('needSupplies',
|
||||
function(_, id, msg)
|
||||
if not id or id ~= __BUILDER_ID then
|
||||
return
|
||||
end
|
||||
|
||||
if Builder.resupplying or msg.contents.uid ~= Builder.slotUid then
|
||||
Builder:log('No supplies ready')
|
||||
Message.send(__BUILDER_ID, 'gotSupplies')
|
||||
else
|
||||
turtle.setStatus('supplying')
|
||||
Builder:log('Supplying')
|
||||
os.sleep(0)
|
||||
|
||||
local pt = msg.contents.point
|
||||
pt.y = turtle.getPoint().y
|
||||
pt.heading = nil
|
||||
if not turtle.gotoYfirst(pt) then -- location of builder
|
||||
Builder.resupplying = true
|
||||
Message.send(__BUILDER_ID, 'gotSupplies')
|
||||
os.sleep(0)
|
||||
if not turtle.gotoLocation('supplies') then
|
||||
Builder:log('failed to go to supply location')
|
||||
Event.exitPullEvents()
|
||||
end
|
||||
turtle.setHeading(1)
|
||||
return
|
||||
end
|
||||
pt.y = pt.y - 2 -- location where builder should go for the chest to be above
|
||||
|
||||
turtle.select(15)
|
||||
turtle.placeDown()
|
||||
os.sleep(.1) -- random computer not connected error
|
||||
local p = ChestProvider({ direction = 'up', wrapSide = 'bottom' })
|
||||
for i = 1, 16 do
|
||||
p:insert(i, 64)
|
||||
end
|
||||
|
||||
Message.send(__BUILDER_ID, 'gotSupplies', { supplies = true, point = pt })
|
||||
|
||||
Message.waitForMessage('thanks', 5, __BUILDER_ID)
|
||||
--os.sleep(0)
|
||||
|
||||
--p.condenseItems()
|
||||
for i = 1, 16 do
|
||||
p:extract(i, 64)
|
||||
end
|
||||
turtle.digDown()
|
||||
turtle.setStatus('waiting')
|
||||
end
|
||||
end)
|
||||
|
||||
Message.addHandler('finished',
|
||||
function(_, id)
|
||||
if not id or id ~= __BUILDER_ID then
|
||||
return
|
||||
end
|
||||
Builder:finish()
|
||||
end)
|
||||
|
||||
Event.on('turtle_abort',
|
||||
function()
|
||||
turtle.abort(false)
|
||||
turtle.setStatus('aborting')
|
||||
Builder:finish()
|
||||
end)
|
||||
|
||||
local function onTheWay() -- parallel routine
|
||||
while true do
|
||||
local _, _, _, id, msg, _ = os.pullEvent('modem_message')
|
||||
if Builder.ready then
|
||||
if id == __BUILDER_ID and msg and msg.type then
|
||||
if msg.type == 'needSupplies' then
|
||||
Message.send(__BUILDER_ID, 'gotSupplies', { supplies = true })
|
||||
elseif msg.type == 'builder' then
|
||||
Builder.lastPoint = msg.contents
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local args = {...}
|
||||
if #args < 2 then
|
||||
error('syntax: <builder id> <facing>')
|
||||
end
|
||||
|
||||
__BUILDER_ID = tonumber(args[1])
|
||||
|
||||
maxStackDB:load()
|
||||
|
||||
itemInfoDB = TableDB({
|
||||
fileName = 'items.db'
|
||||
})
|
||||
|
||||
itemInfoDB:load()
|
||||
|
||||
Builder.itemProvider = MEProvider({ direction = args[2] })
|
||||
if not Builder.itemProvider:isValid() then
|
||||
local sides = {
|
||||
east = 'west',
|
||||
west = 'east',
|
||||
north = 'south',
|
||||
south = 'north',
|
||||
}
|
||||
|
||||
Builder.itemProvider = ChestProvider({ direction = sides[args[2]], wrapSide = 'front' })
|
||||
if not Builder.itemProvider:isValid() then
|
||||
error('A chest or ME interface must be in front of turtle')
|
||||
end
|
||||
end
|
||||
|
||||
turtle.run(function()
|
||||
turtle.setPoint({ x = -1, z = -2, y = -1, heading = 1 })
|
||||
|
||||
turtle.saveLocation('supplies')
|
||||
|
||||
Event.pullEvents(onTheWay)
|
||||
end)
|
||||
Reference in New Issue
Block a user