Initial commit
This commit is contained in:
893
sys/apis/blocks.lua
Normal file
893
sys/apis/blocks.lua
Normal file
@@ -0,0 +1,893 @@
|
||||
local class = require('class')
|
||||
local TableDB = require('tableDB')
|
||||
|
||||
local blockDB = TableDB({
|
||||
fileName = 'block.db',
|
||||
tabledef = {
|
||||
autokeys = false,
|
||||
columns = {
|
||||
{ name = 'key', type = 'key', length = 8 },
|
||||
{ name = 'id', type = 'number', length = 5 },
|
||||
{ name = 'dmg', type = 'number', length = 2 },
|
||||
{ name = 'name', type = 'string', length = 35 },
|
||||
{ name = 'refname', type = 'string', length = 35 },
|
||||
{ name = 'strId', type = 'string', length = 80 },
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
function blockDB:load(dir, sbDB, btDB)
|
||||
self.fileName = fs.combine(dir, self.fileName)
|
||||
if fs.exists(self.fileName) then
|
||||
TableDB.load(self)
|
||||
else
|
||||
self:seedDB(dir)
|
||||
end
|
||||
end
|
||||
|
||||
function blockDB:seedDB(dir)
|
||||
|
||||
-- http://lua-users.org/wiki/LuaCsv
|
||||
function ParseCSVLine (line,sep)
|
||||
local res = {}
|
||||
local pos = 1
|
||||
sep = sep or ','
|
||||
while true do
|
||||
local c = string.sub(line,pos,pos)
|
||||
if (c == "") then break end
|
||||
if (c == '"') then
|
||||
-- quoted value (ignore separator within)
|
||||
local txt = ""
|
||||
repeat
|
||||
local startp,endp = string.find(line,'^%b""',pos)
|
||||
txt = txt..string.sub(line,startp+1,endp-1)
|
||||
pos = endp + 1
|
||||
c = string.sub(line,pos,pos)
|
||||
if (c == '"') then txt = txt..'"' end
|
||||
-- check first char AFTER quoted string, if it is another
|
||||
-- quoted string without separator, then append it
|
||||
-- this is the way to "escape" the quote char in a quote. example:
|
||||
-- value1,"blub""blip""boing",value3 will result in blub"blip"boing for the middle
|
||||
until (c ~= '"')
|
||||
table.insert(res,txt)
|
||||
assert(c == sep or c == "")
|
||||
pos = pos + 1
|
||||
else
|
||||
-- no quotes used, just look for the first separator
|
||||
local startp,endp = string.find(line,sep,pos)
|
||||
if (startp) then
|
||||
table.insert(res,string.sub(line,pos,startp-1))
|
||||
pos = endp + 1
|
||||
else
|
||||
-- no separator found -> use rest of string and terminate
|
||||
table.insert(res,string.sub(line,pos))
|
||||
break
|
||||
end
|
||||
end
|
||||
end
|
||||
return res
|
||||
end
|
||||
|
||||
local f = fs.open(fs.combine(dir, 'blockIds.csv'), "r")
|
||||
|
||||
if not f then
|
||||
error('unable to read blockIds.csv')
|
||||
end
|
||||
|
||||
local lastID = nil
|
||||
|
||||
while true do
|
||||
|
||||
local data = f.readLine()
|
||||
if not data then
|
||||
break
|
||||
end
|
||||
|
||||
local line = ParseCSVLine(data, ',')
|
||||
|
||||
local strId = line[3] -- string ID
|
||||
local nid -- schematic ID
|
||||
local id = line[3]
|
||||
local dmg = 0
|
||||
local name = line[1]
|
||||
|
||||
if not strId or #strId == 0 then
|
||||
strId = lastID
|
||||
end
|
||||
lastID = strId
|
||||
|
||||
local sep = string.find(line[2], ':')
|
||||
if not sep then
|
||||
nid = tonumber(line[2])
|
||||
dmg = 0
|
||||
else
|
||||
nid = tonumber(string.sub(line[2], 1, sep - 1))
|
||||
dmg = tonumber(string.sub(line[2], sep + 1, #line[2]))
|
||||
end
|
||||
|
||||
self:add(nid, dmg, name, strId)
|
||||
end
|
||||
|
||||
f.close()
|
||||
|
||||
self.dirty = true
|
||||
self:flush()
|
||||
end
|
||||
|
||||
function blockDB:lookup(id, dmg)
|
||||
|
||||
if not id then
|
||||
return
|
||||
end
|
||||
|
||||
if not id or not dmg then error('blockDB:lookup: nil passed', 2) end
|
||||
local key = id .. ':' .. dmg
|
||||
|
||||
return self.data[key]
|
||||
end
|
||||
|
||||
function blockDB:getName(id, dmg)
|
||||
return self:lookupName(id, dmg) or id .. ':' .. dmg
|
||||
end
|
||||
|
||||
function blockDB:lookupName(id, dmg)
|
||||
if not id or not dmg then error('blockDB:lookupName: nil passed', 2) end
|
||||
|
||||
for _,v in pairs(self.data) do
|
||||
if v.strId == id and v.dmg == dmg then
|
||||
return v.name
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function blockDB:add(id, dmg, name, strId)
|
||||
local key = id .. ':' .. dmg
|
||||
|
||||
TableDB.add(self, key, {
|
||||
id = id,
|
||||
dmg = dmg,
|
||||
key = key,
|
||||
name = name,
|
||||
strId = strId,
|
||||
})
|
||||
end
|
||||
|
||||
--[[-- placementDB --]]--
|
||||
-- in memory table that expands the standardBlock and blockType tables for each item/dmg/placement combination
|
||||
local placementDB = TableDB({
|
||||
fileName = 'placement.db'
|
||||
})
|
||||
|
||||
function placementDB:load(dir, sbDB, btDB)
|
||||
|
||||
self.fileName = fs.combine(dir, self.fileName)
|
||||
|
||||
for k,blockType in pairs(sbDB.data) do
|
||||
local bt = btDB.data[blockType]
|
||||
if not bt then
|
||||
error('missing block type: ' .. blockType)
|
||||
end
|
||||
local id, dmg = string.match(k, '(%d+):*(%d+)')
|
||||
self:addSubsForBlockType(tonumber(id), tonumber(dmg), bt)
|
||||
end
|
||||
|
||||
-- testing
|
||||
-- self.dirty = true
|
||||
-- self:flush()
|
||||
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)
|
||||
end
|
||||
end
|
||||
|
||||
function placementDB:add(id, dmg, sid, sdmg, direction)
|
||||
if not id or not dmg then error('placementDB:add: nil passed', 2) end
|
||||
|
||||
local key = id .. ':' .. dmg
|
||||
|
||||
if direction and #direction == 0 then
|
||||
direction = nil
|
||||
end
|
||||
|
||||
self.data[key] = {
|
||||
id = id, -- numeric ID
|
||||
dmg = dmg, -- dmg with placement info
|
||||
key = key,
|
||||
sid = sid, -- string ID
|
||||
sdmg = sdmg, -- dmg without placement info
|
||||
direction = direction,
|
||||
}
|
||||
end
|
||||
|
||||
--[[-- StandardBlockDB --]]--
|
||||
local standardBlockDB = TableDB({
|
||||
fileName = 'standard.db',
|
||||
tabledef = {
|
||||
autokeys = false,
|
||||
type = 'simple',
|
||||
columns = {
|
||||
{ label = 'Key', type = 'key', length = 8 },
|
||||
{ label = 'Block Type', type = 'string', length = 20 }
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
function standardBlockDB:load(dir)
|
||||
self.fileName = fs.combine(dir, self.fileName)
|
||||
|
||||
if fs.exists(self.fileName) then
|
||||
TableDB.load(self)
|
||||
else
|
||||
self:seedDB()
|
||||
end
|
||||
end
|
||||
|
||||
function standardBlockDB:seedDB()
|
||||
self.data = {
|
||||
[ '6:0' ] = 'sapling',
|
||||
[ '6:1' ] = 'sapling',
|
||||
[ '6:2' ] = 'sapling',
|
||||
[ '6:3' ] = 'sapling',
|
||||
[ '6:4' ] = 'sapling',
|
||||
[ '6:5' ] = 'sapling',
|
||||
[ '8:0' ] = 'truncate',
|
||||
[ '9:0' ] = 'truncate',
|
||||
[ '17:0' ] = 'wood',
|
||||
[ '17:1' ] = 'wood',
|
||||
[ '17:2' ] = 'wood',
|
||||
[ '17:3' ] = 'wood',
|
||||
[ '18:0' ] = 'leaves',
|
||||
[ '18:1' ] = 'leaves',
|
||||
[ '18:2' ] = 'leaves',
|
||||
[ '18:3' ] = 'leaves',
|
||||
[ '23:0' ] = 'dispenser',
|
||||
[ '26:0' ] = 'bed',
|
||||
[ '27:0' ] = 'adp-rail',
|
||||
[ '28:0' ] = 'adp-rail',
|
||||
[ '29:0' ] = 'piston',
|
||||
[ '33:0' ] = 'piston',
|
||||
[ '34:0' ] = 'air',
|
||||
[ '36:0' ] = 'air',
|
||||
[ '44:0' ] = 'slab',
|
||||
[ '44:1' ] = 'slab',
|
||||
[ '44:2' ] = 'slab',
|
||||
[ '44:3' ] = 'slab',
|
||||
[ '44:4' ] = 'slab',
|
||||
[ '44:5' ] = 'slab',
|
||||
[ '44:6' ] = 'slab',
|
||||
[ '44:7' ] = 'slab',
|
||||
[ '50:0' ] = 'torch',
|
||||
[ '51:0' ] = 'flatten',
|
||||
[ '53:0' ] = 'stairs',
|
||||
[ '54:0' ] = 'chest-furnace',
|
||||
[ '55:0' ] = 'flatten',
|
||||
[ '59:0' ] = 'flatten',
|
||||
[ '60:0' ] = 'flatten',
|
||||
[ '61:0' ] = 'chest-furnace',
|
||||
[ '62:0' ] = 'chest-furnace',
|
||||
[ '63:0' ] = 'signpost',
|
||||
[ '64:0' ] = 'door',
|
||||
[ '65:0' ] = 'wallsign-ladder',
|
||||
[ '66:0' ] = 'rail',
|
||||
[ '67:0' ] = 'stairs',
|
||||
[ '68:0' ] = 'wallsign',
|
||||
[ '69:0' ] = 'lever',
|
||||
[ '71:0' ] = 'door',
|
||||
[ '75:0' ] = 'torch',
|
||||
[ '76:0' ] = 'torch',
|
||||
[ '77:0' ] = 'button',
|
||||
[ '78:0' ] = 'flatten',
|
||||
[ '81:0' ] = 'flatten',
|
||||
[ '83:0' ] = 'flatten',
|
||||
[ '86:0' ] = 'pumpkin',
|
||||
[ '90:0' ] = 'air',
|
||||
[ '91:0' ] = 'pumpkin',
|
||||
[ '93:0' ] = 'repeater',
|
||||
[ '94:0' ] = 'repeater',
|
||||
[ '96:0' ] = 'trapdoor',
|
||||
[ '99:0' ] = 'flatten',
|
||||
[ '100:0' ] = 'flatten',
|
||||
[ '106:0' ] = 'vine',
|
||||
[ '107:0' ] = 'gate',
|
||||
[ '108:0' ] = 'stairs',
|
||||
[ '109:0' ] = 'stairs',
|
||||
[ '114:0' ] = 'stairs',
|
||||
[ '115:0' ] = 'flatten',
|
||||
[ '117:0' ] = 'flatten',
|
||||
[ '118:0' ] = 'cauldron',
|
||||
[ '126:0' ] = 'slab',
|
||||
[ '126:1' ] = 'slab',
|
||||
[ '126:2' ] = 'slab',
|
||||
[ '126:3' ] = 'slab',
|
||||
[ '126:4' ] = 'slab',
|
||||
[ '126:5' ] = 'slab',
|
||||
[ '127:0' ] = 'cocoa',
|
||||
[ '128:0' ] = 'stairs',
|
||||
[ '130:0' ] = 'chest-furnace',
|
||||
[ '131:0' ] = 'tripwire',
|
||||
[ '132:0' ] = 'flatten',
|
||||
[ '134:0' ] = 'stairs',
|
||||
[ '135:0' ] = 'stairs',
|
||||
[ '136:0' ] = 'stairs',
|
||||
[ '140:0' ] = 'flatten',
|
||||
[ '141:0' ] = 'flatten',
|
||||
[ '142:0' ] = 'flatten',
|
||||
[ '143:0' ] = 'button',
|
||||
[ '144:0' ] = 'mobhead',
|
||||
[ '145:0' ] = 'anvil',
|
||||
[ '146:0' ] = 'chest-furnace',
|
||||
[ '149:0' ] = 'comparator',
|
||||
[ '151:0' ] = 'flatten',
|
||||
[ '154:0' ] = 'hopper',
|
||||
[ '155:2' ] = 'quartz-pillar',
|
||||
[ '156:0' ] = 'stairs',
|
||||
[ '157:0' ] = 'adp-rail',
|
||||
[ '158:0' ] = 'hopper',
|
||||
[ '161:0' ] = 'leaves',
|
||||
[ '161:1' ] = 'leaves',
|
||||
[ '162:0' ] = 'wood',
|
||||
[ '162:1' ] = 'wood',
|
||||
[ '163:0' ] = 'stairs',
|
||||
[ '164:0' ] = 'stairs',
|
||||
[ '167:0' ] = 'trapdoor',
|
||||
[ '170:0' ] = 'flatten',
|
||||
[ '175:0' ] = 'largeplant',
|
||||
[ '175:1' ] = 'largeplant',
|
||||
[ '175:2' ] = 'largeplant', -- double tallgrass - an alternative would be to use grass as the bottom part, bonemeal as top part
|
||||
[ '175:3' ] = 'largeplant',
|
||||
[ '175:4' ] = 'largeplant',
|
||||
[ '175:5' ] = 'largeplant',
|
||||
[ '176:0' ] = 'banner',
|
||||
[ '177:0' ] = 'wall_banner',
|
||||
[ '178:0' ] = 'truncate',
|
||||
[ '180:0' ] = 'stairs',
|
||||
[ '182:0' ] = 'slab',
|
||||
[ '183:0' ] = 'gate',
|
||||
[ '184:0' ] = 'gate',
|
||||
[ '185:0' ] = 'gate',
|
||||
[ '186:0' ] = 'gate',
|
||||
[ '187:0' ] = 'gate',
|
||||
[ '193:0' ] = 'door',
|
||||
[ '194:0' ] = 'door',
|
||||
[ '195:0' ] = 'door',
|
||||
[ '196:0' ] = 'door',
|
||||
[ '197:0' ] = 'door',
|
||||
[ '198:0' ] = 'flatten',
|
||||
[ '205:0' ] = 'slab',
|
||||
[ '210:0' ] = 'flatten',
|
||||
[ '355:0' ] = 'bed',
|
||||
[ '356:0' ] = 'repeater',
|
||||
[ '404:0' ] = 'comparator',
|
||||
}
|
||||
self.dirty = true
|
||||
self:flush()
|
||||
end
|
||||
|
||||
--[[-- BlockTypeDB --]]--
|
||||
local blockTypeDB = TableDB({
|
||||
fileName = 'blocktype.db',
|
||||
tabledef = {
|
||||
autokeys = true,
|
||||
columns = {
|
||||
{ name = 'odmg', type = 'number', length = 2 },
|
||||
{ name = 'sid', type = 'number', length = 5 },
|
||||
{ name = 'sdmg', type = 'number', length = 2 },
|
||||
{ name = 'dir', type = 'string', length = 20 },
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
function blockTypeDB:load(dir)
|
||||
self.fileName = fs.combine(dir, self.fileName)
|
||||
|
||||
if fs.exists(self.fileName) then
|
||||
TableDB.load(self)
|
||||
else
|
||||
self:seedDB()
|
||||
end
|
||||
end
|
||||
|
||||
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]
|
||||
})
|
||||
end
|
||||
self.dirty = true
|
||||
end
|
||||
|
||||
function blockTypeDB:seedDB()
|
||||
|
||||
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('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 },
|
||||
{ 1, nil, 0 },
|
||||
{ 2, nil, 0, 'south' },
|
||||
{ 3, nil, 0, 'north' },
|
||||
{ 4, nil, 0, 'east' },
|
||||
{ 5, nil, 0, 'west' },
|
||||
{ 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' },
|
||||
{ 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, 'south' },
|
||||
{ 1, nil, 0, 'south' },
|
||||
{ 2, nil, 0, 'south' },
|
||||
{ 3, nil, 0, 'south' },
|
||||
{ 4, nil, 0, 'west' },
|
||||
{ 5, nil, 0, 'west' },
|
||||
{ 6, nil, 0, 'west' },
|
||||
{ 7, nil, 0, 'west' },
|
||||
{ 8, nil, 0, 'north' },
|
||||
{ 9, nil, 0, 'north' },
|
||||
{ 10, nil, 0, 'north' },
|
||||
{ 11, nil, 0, 'north' },
|
||||
{ 12, nil, 0, 'east' },
|
||||
{ 13, nil, 0, 'east' },
|
||||
{ 14, nil, 0, 'east' },
|
||||
{ 15, nil, 0, 'east' },
|
||||
})
|
||||
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-block' },
|
||||
{ 9, nil, 0, 'north-block' },
|
||||
{ 10, nil, 0, 'east-block' },
|
||||
{ 11, nil, 0, 'west-block' },
|
||||
{ 12, nil, 0, 'south-block' },
|
||||
{ 13, nil, 0, 'north-block' },
|
||||
{ 14, nil, 0, 'east-block' },
|
||||
{ 15, nil, 0, 'west-block' },
|
||||
})
|
||||
blockTypeDB:addTemp('piston', { -- piston placement is broken in 1.7 -- need to add work around
|
||||
{ 0, nil, 0, 'piston-down' },
|
||||
{ 1, nil, 0 },
|
||||
{ 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 },
|
||||
{ 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 },
|
||||
{ 2, nil, 0, 'south-block' },
|
||||
{ 3, nil, 0, 'north-block' },
|
||||
{ 4, nil, 0, 'east-block' },
|
||||
{ 5, nil, 0, 'west-block' },
|
||||
})
|
||||
blockTypeDB:addTemp('wallsign', {
|
||||
{ 0, nil, 0 },
|
||||
{ 2, 'minecraft:sign', 0, 'south-block' },
|
||||
{ 3, 'minecraft:sign', 0, 'north-block' },
|
||||
{ 4, 'minecraft:sign', 0, 'east-block' },
|
||||
{ 5, 'minecraft:sign', 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('air', {
|
||||
{ 0, 'minecraft:air', 0 },
|
||||
{ 1, 'minecraft:air', 0 },
|
||||
{ 2, 'minecraft:air', 0 },
|
||||
{ 3, 'minecraft:air', 0 },
|
||||
{ 4, 'minecraft:air', 0 },
|
||||
{ 5, 'minecraft:air', 0 },
|
||||
{ 6, 'minecraft:air', 0 },
|
||||
{ 7, 'minecraft:air', 0 },
|
||||
{ 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('truncate', {
|
||||
{ 0, nil, 0 },
|
||||
{ 1, 'minecraft:air', 0 },
|
||||
{ 2, 'minecraft:air', 0 },
|
||||
{ 3, 'minecraft:air', 0 },
|
||||
{ 4, 'minecraft:air', 0 },
|
||||
{ 5, 'minecraft:air', 0 },
|
||||
{ 6, 'minecraft:air', 0 },
|
||||
{ 7, 'minecraft:air', 0 },
|
||||
{ 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('slab', {
|
||||
{ '+0', nil, nil, 'bottom' },
|
||||
{ '+8', nil, nil, 'top' },
|
||||
})
|
||||
blockTypeDB:addTemp('largeplant', {
|
||||
{ '+0', nil, nil, 'east-door' }, -- 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' },
|
||||
{ 1, nil, 0, 'south-door' },
|
||||
{ 2, nil, 0, 'west-door' },
|
||||
{ 3, nil, 0, 'north-door' },
|
||||
{ 4, nil, 0, 'east-door' },
|
||||
{ 5, nil, 0, 'south-door' },
|
||||
{ 6, nil, 0, 'west-door' },
|
||||
{ 7, nil, 0, 'north-door' },
|
||||
{ 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('banner', {
|
||||
{ 0, nil, 0, 'north' },
|
||||
{ 1, nil, 0, 'east' },
|
||||
{ 2, nil, 0, 'east' },
|
||||
{ 3, nil, 0, 'east' },
|
||||
{ 4, nil, 0, 'east' },
|
||||
{ 5, nil, 0, 'east' },
|
||||
{ 6, nil, 0, 'east' },
|
||||
{ 7, nil, 0, 'east' },
|
||||
{ 8, nil, 0, 'south' },
|
||||
{ 9, nil, 0, 'west' },
|
||||
{ 10, nil, 0, 'west' },
|
||||
{ 11, nil, 0, 'west' },
|
||||
{ 12, nil, 0, 'west' },
|
||||
{ 13, nil, 0, 'west' },
|
||||
{ 14, nil, 0, 'west' },
|
||||
{ 15, nil, 0, 'west' },
|
||||
})
|
||||
blockTypeDB:addTemp('wall_banner', {
|
||||
{ 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('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' },
|
||||
})
|
||||
self.dirty = true
|
||||
self:flush()
|
||||
end
|
||||
|
||||
local Blocks = class()
|
||||
function Blocks:init(args)
|
||||
|
||||
Util.merge(self, args)
|
||||
self.blockDB = blockDB
|
||||
|
||||
blockDB:load(self.dir)
|
||||
standardBlockDB:load(self.dir)
|
||||
blockTypeDB:load(self.dir)
|
||||
placementDB:load(self.dir, standardBlockDB, blockTypeDB)
|
||||
end
|
||||
|
||||
-- for an ID / dmg (with placement info) - return the correct block (without the placment info embedded in the dmg)
|
||||
function Blocks:getRealBlock(id, dmg)
|
||||
|
||||
local p = placementDB:get({id, dmg})
|
||||
if p then
|
||||
return { id = p.sid, dmg = p.sdmg, direction = p.direction }
|
||||
end
|
||||
|
||||
local b = blockDB:get({id, dmg})
|
||||
if b then
|
||||
return { id = b.strId, dmg = b.dmg }
|
||||
end
|
||||
|
||||
return { id = id, dmg = dmg }
|
||||
end
|
||||
|
||||
return Blocks
|
||||
103
sys/apis/chestProvider.lua
Normal file
103
sys/apis/chestProvider.lua
Normal file
@@ -0,0 +1,103 @@
|
||||
local class = require('class')
|
||||
local Logger = require('logger')
|
||||
|
||||
local ChestProvider = class()
|
||||
|
||||
function ChestProvider:init(args)
|
||||
|
||||
args = args or { }
|
||||
|
||||
self.stacks = {}
|
||||
self.name = 'chest'
|
||||
self.direction = args.direction or 'up'
|
||||
self.wrapSide = args.wrapSide or 'bottom'
|
||||
self.p = peripheral.wrap(self.wrapSide)
|
||||
end
|
||||
|
||||
function ChestProvider:isValid()
|
||||
return self.p and self.p.getAllStacks
|
||||
end
|
||||
|
||||
function ChestProvider:refresh()
|
||||
if self.p then
|
||||
self.p.condenseItems()
|
||||
self.stacks = self.p.getAllStacks(false)
|
||||
local t = { }
|
||||
for _,s in ipairs(self.stacks) do
|
||||
local key = s.id .. ':' .. s.dmg
|
||||
if t[key] and t[key].qty < 64 then
|
||||
t[key].max_size = t[key].qty
|
||||
else
|
||||
t[key] = {
|
||||
qty = s.qty
|
||||
}
|
||||
end
|
||||
end
|
||||
for _,s in ipairs(self.stacks) do
|
||||
local key = s.id .. ':' .. s.dmg
|
||||
if t[key].max_size then
|
||||
s.max_size = t[key].qty
|
||||
else
|
||||
s.max_size = 64
|
||||
end
|
||||
end
|
||||
end
|
||||
return self.stacks
|
||||
end
|
||||
|
||||
function ChestProvider:getItemInfo(id, dmg)
|
||||
local item = { id = id, dmg = dmg, qty = 0, max_size = 64 }
|
||||
for _,stack in pairs(self.stacks) do
|
||||
if stack.id == id and stack.dmg == dmg then
|
||||
item.name = stack.display_name
|
||||
item.qty = item.qty + stack.qty
|
||||
item.max_size = stack.max_size
|
||||
end
|
||||
end
|
||||
return item
|
||||
end
|
||||
|
||||
function ChestProvider:craft(id, dmg, qty)
|
||||
return false
|
||||
end
|
||||
|
||||
function ChestProvider:craftItems(items)
|
||||
end
|
||||
|
||||
function ChestProvider:provide(item, qty, slot)
|
||||
if self.p then
|
||||
self.stacks = self.p.getAllStacks(false)
|
||||
for key,stack in pairs(self.stacks) do
|
||||
if stack.id == item.id and stack.dmg == item.dmg then
|
||||
local amount = math.min(qty, stack.qty)
|
||||
self.p.pushItemIntoSlot(self.direction, key, amount, slot)
|
||||
qty = qty - amount
|
||||
if qty <= 0 then
|
||||
break
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function ChestProvider:insert(slot, qty)
|
||||
if self.p then
|
||||
local s, m = pcall(function() self.p.pullItem(self.direction, slot, qty) end)
|
||||
if not s and m then
|
||||
print('chestProvider:pullItem')
|
||||
print(m)
|
||||
Logger.log('chestProvider', 'Insert failed, trying again')
|
||||
sleep(1)
|
||||
s, m = pcall(function() self.p.pullItem(self.direction, slot, qty) end)
|
||||
if not s and m then
|
||||
print('chestProvider:pullItem')
|
||||
print(m)
|
||||
Logger.log('chestProvider', 'Insert failed again')
|
||||
else
|
||||
Logger.log('chestProvider', 'Insert successful')
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
return ChestProvider
|
||||
46
sys/apis/class.lua
Normal file
46
sys/apis/class.lua
Normal file
@@ -0,0 +1,46 @@
|
||||
-- From http://lua-users.org/wiki/SimpleLuaClasses
|
||||
-- (with some modifications)
|
||||
|
||||
-- class.lua
|
||||
-- Compatible with Lua 5.1 (not 5.0).
|
||||
return function(base)
|
||||
local c = { } -- a new class instance
|
||||
if type(base) == 'table' then
|
||||
-- our new class is a shallow copy of the base class!
|
||||
for i,v in pairs(base) do
|
||||
c[i] = v
|
||||
end
|
||||
c._base = base
|
||||
end
|
||||
-- the class will be the metatable for all its objects,
|
||||
-- and they will look up their methods in it.
|
||||
c.__index = c
|
||||
|
||||
-- expose a constructor which can be called by <classname>(<args>)
|
||||
setmetatable(c, {
|
||||
__call = function(class_tbl, ...)
|
||||
local obj = {}
|
||||
setmetatable(obj,c)
|
||||
if class_tbl.init then
|
||||
class_tbl.init(obj, ...)
|
||||
else
|
||||
-- make sure that any stuff from the base class is initialized!
|
||||
if base and base.init then
|
||||
base.init(obj, ...)
|
||||
end
|
||||
end
|
||||
return obj
|
||||
end
|
||||
})
|
||||
|
||||
c.is_a =
|
||||
function(self, klass)
|
||||
local m = getmetatable(self)
|
||||
while m do
|
||||
if m == klass then return true end
|
||||
m = m._base
|
||||
end
|
||||
return false
|
||||
end
|
||||
return c
|
||||
end
|
||||
24
sys/apis/config.lua
Normal file
24
sys/apis/config.lua
Normal file
@@ -0,0 +1,24 @@
|
||||
local Util = require('util')
|
||||
|
||||
local Config = { }
|
||||
|
||||
Config.load = function(fname, data)
|
||||
local filename = '/config/' .. fname
|
||||
|
||||
if not fs.exists('/config') then
|
||||
fs.makeDir('/config')
|
||||
end
|
||||
|
||||
if not fs.exists(filename) then
|
||||
Util.writeTable(filename, data)
|
||||
else
|
||||
Util.merge(data, Util.readTable(filename) or { })
|
||||
end
|
||||
end
|
||||
|
||||
Config.update = function(fname, data)
|
||||
local filename = '/config/' .. fname
|
||||
Util.writeTable(filename, data)
|
||||
end
|
||||
|
||||
return Config
|
||||
870
sys/apis/deflatelua.lua
Normal file
870
sys/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
|
||||
177
sys/apis/event.lua
Normal file
177
sys/apis/event.lua
Normal file
@@ -0,0 +1,177 @@
|
||||
local Util = require('util')
|
||||
|
||||
local Event = {
|
||||
uid = 1, -- unique id for handlers
|
||||
}
|
||||
|
||||
local eventHandlers = {
|
||||
namedTimers = {}
|
||||
}
|
||||
|
||||
-- debug purposes
|
||||
function Event.getHandlers()
|
||||
return eventHandlers
|
||||
end
|
||||
|
||||
function Event.addHandler(type, f)
|
||||
local event = eventHandlers[type]
|
||||
if not event then
|
||||
event = {}
|
||||
event.handlers = {}
|
||||
eventHandlers[type] = event
|
||||
end
|
||||
|
||||
local handler = {
|
||||
uid = Event.uid,
|
||||
event = type,
|
||||
f = f,
|
||||
}
|
||||
Event.uid = Event.uid + 1
|
||||
event.handlers[handler.uid] = handler
|
||||
|
||||
return handler
|
||||
end
|
||||
|
||||
function Event.removeHandler(h)
|
||||
if h and h.event then
|
||||
eventHandlers[h.event].handlers[h.uid] = nil
|
||||
end
|
||||
end
|
||||
|
||||
function Event.queueTimedEvent(name, timeout, event, args)
|
||||
Event.addNamedTimer(name, timeout, false,
|
||||
function()
|
||||
os.queueEvent(event, args)
|
||||
end
|
||||
)
|
||||
end
|
||||
|
||||
function Event.addNamedTimer(name, interval, recurring, f)
|
||||
Event.cancelNamedTimer(name)
|
||||
eventHandlers.namedTimers[name] = Event.addTimer(interval, recurring, f)
|
||||
end
|
||||
|
||||
function Event.getNamedTimer(name)
|
||||
return eventHandlers.namedTimers[name]
|
||||
end
|
||||
|
||||
function Event.cancelNamedTimer(name)
|
||||
local timer = Event.getNamedTimer(name)
|
||||
if timer then
|
||||
timer.enabled = false
|
||||
Event.removeHandler(timer)
|
||||
end
|
||||
end
|
||||
|
||||
function Event.isTimerActive(timer)
|
||||
return timer.enabled and
|
||||
os.clock() < timer.start + timer.interval
|
||||
end
|
||||
|
||||
function Event.addTimer(interval, recurring, f)
|
||||
local timer = Event.addHandler('timer',
|
||||
function(t, id)
|
||||
if t.timerId ~= id then
|
||||
return
|
||||
end
|
||||
if t.enabled then
|
||||
t.fired = true
|
||||
t.cf(t, id)
|
||||
end
|
||||
if t.recurring then
|
||||
t.fired = false
|
||||
t.start = os.clock()
|
||||
t.timerId = os.startTimer(t.interval)
|
||||
else
|
||||
Event.removeHandler(t)
|
||||
end
|
||||
end
|
||||
)
|
||||
timer.cf = f
|
||||
timer.interval = interval
|
||||
timer.recurring = recurring
|
||||
timer.start = os.clock()
|
||||
timer.enabled = true
|
||||
timer.timerId = os.startTimer(interval)
|
||||
|
||||
return timer
|
||||
end
|
||||
|
||||
function Event.removeTimer(h)
|
||||
Event.removeHandler(h)
|
||||
end
|
||||
|
||||
function Event.blockUntilEvent(event, timeout)
|
||||
return Event.waitForEvent(event, timeout, os.pullEvent)
|
||||
end
|
||||
|
||||
function Event.waitForEvent(event, timeout, pullEvent)
|
||||
pullEvent = pullEvent or Event.pullEvent
|
||||
|
||||
local timerId = os.startTimer(timeout)
|
||||
repeat
|
||||
local e, p1, p2, p3, p4 = pullEvent()
|
||||
if e == event then
|
||||
return e, p1, p2, p3, p4
|
||||
end
|
||||
until e == 'timer' and p1 == timerId
|
||||
end
|
||||
|
||||
local exitPullEvents = false
|
||||
|
||||
local function _pullEvents()
|
||||
|
||||
--exitPullEvents = false
|
||||
while true do
|
||||
local e = Event.pullEvent()
|
||||
if exitPullEvents or e == 'terminate' then
|
||||
break
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function Event.sleep(t)
|
||||
local timerId = os.startTimer(t or 0)
|
||||
repeat
|
||||
local event, id = Event.pullEvent()
|
||||
until event == 'timer' and id == timerId
|
||||
end
|
||||
|
||||
function Event.pullEvents(...)
|
||||
local routines = { ... }
|
||||
if #routines > 0 then
|
||||
parallel.waitForAny(_pullEvents, ...)
|
||||
else
|
||||
_pullEvents()
|
||||
end
|
||||
end
|
||||
|
||||
function Event.exitPullEvents()
|
||||
exitPullEvents = true
|
||||
os.sleep(0)
|
||||
end
|
||||
|
||||
function Event.pullEvent(eventType)
|
||||
local e = { os.pullEventRaw(eventType) }
|
||||
return Event.processEvent(e)
|
||||
end
|
||||
|
||||
function Event.processEvent(pe)
|
||||
|
||||
local e, p1, p2, p3, p4, p5 = unpack(pe)
|
||||
|
||||
local event = eventHandlers[e]
|
||||
if event then
|
||||
local keys = Util.keys(event.handlers)
|
||||
for _,key in pairs(keys) do
|
||||
local h = event.handlers[key]
|
||||
if h then
|
||||
h.f(h, p1, p2, p3, p4, p5)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
return e, p1, p2, p3, p4, p5
|
||||
end
|
||||
|
||||
return Event
|
||||
142
sys/apis/fileui.lua
Normal file
142
sys/apis/fileui.lua
Normal file
@@ -0,0 +1,142 @@
|
||||
local UI = require('ui')
|
||||
|
||||
return function()
|
||||
|
||||
local columns = {
|
||||
{ heading = 'Name', key = 'name', width = UI.term.width - 9 },
|
||||
}
|
||||
|
||||
if UI.term.width > 28 then
|
||||
columns[1].width = UI.term.width - 16
|
||||
table.insert(columns,
|
||||
{ heading = 'Size', key = 'size', width = 6 }
|
||||
)
|
||||
end
|
||||
|
||||
local selectFile = UI.Page({
|
||||
x = 3,
|
||||
y = 3,
|
||||
ex = -3,
|
||||
ey = -3,
|
||||
backgroundColor = colors.brown,
|
||||
titleBar = UI.TitleBar({
|
||||
title = 'Select file',
|
||||
previousPage = true,
|
||||
event = 'cancel',
|
||||
}),
|
||||
grid = UI.ScrollingGrid({
|
||||
x = 2,
|
||||
y = 2,
|
||||
ex = -2,
|
||||
ey = -4,
|
||||
path = '',
|
||||
sortColumn = 'name',
|
||||
columns = columns,
|
||||
}),
|
||||
path = UI.TextEntry({
|
||||
x = 2,
|
||||
oy = -1,
|
||||
ex = -11,
|
||||
limit = 256,
|
||||
accelerators = {
|
||||
enter = 'path_enter',
|
||||
}
|
||||
}),
|
||||
cancel = UI.Button({
|
||||
text = 'Cancel',
|
||||
ox = -8,
|
||||
oy = -1,
|
||||
event = 'cancel',
|
||||
}),
|
||||
})
|
||||
|
||||
function selectFile:enable(path, fn)
|
||||
self:setPath(path)
|
||||
self.fn = fn
|
||||
UI.Page.enable(self)
|
||||
end
|
||||
|
||||
function selectFile:setPath(path)
|
||||
self.grid.dir = path
|
||||
while not fs.isDir(self.grid.dir) do
|
||||
self.grid.dir = fs.getDir(self.grid.dir)
|
||||
end
|
||||
|
||||
self.path.value = self.grid.dir
|
||||
end
|
||||
|
||||
function selectFile.grid:draw()
|
||||
local files = fs.list(self.dir, true)
|
||||
if #self.dir > 0 then
|
||||
table.insert(files, {
|
||||
name = '..',
|
||||
isDir = true,
|
||||
})
|
||||
end
|
||||
self:setValues(files)
|
||||
self:setIndex(1)
|
||||
UI.Grid.draw(self)
|
||||
end
|
||||
|
||||
function selectFile.grid:getDisplayValues(row)
|
||||
if row.size then
|
||||
row = Util.shallowCopy(row)
|
||||
row.size = Util.toBytes(row.size)
|
||||
end
|
||||
return row
|
||||
end
|
||||
|
||||
function selectFile.grid:getRowTextColor(file, selected)
|
||||
if file.isDir then
|
||||
return colors.cyan
|
||||
end
|
||||
if file.isReadOnly then
|
||||
return colors.pink
|
||||
end
|
||||
return colors.white
|
||||
end
|
||||
|
||||
function selectFile.grid:sortCompare(a, b)
|
||||
if self.sortColumn == 'size' then
|
||||
return a.size < b.size
|
||||
end
|
||||
if a.isDir == b.isDir then
|
||||
return a.name:lower() < b.name:lower()
|
||||
end
|
||||
return a.isDir
|
||||
end
|
||||
|
||||
function selectFile:eventHandler(event)
|
||||
|
||||
if event.type == 'grid_select' then
|
||||
self.grid.dir = fs.combine(self.grid.dir, event.selected.name)
|
||||
self.path.value = self.grid.dir
|
||||
if event.selected.isDir then
|
||||
self.grid:draw()
|
||||
self.path:draw()
|
||||
else
|
||||
UI:setPreviousPage()
|
||||
self.fn(self.path.value)
|
||||
end
|
||||
|
||||
elseif event.type == 'path_enter' then
|
||||
if fs.isDir(self.path.value) then
|
||||
self:setPath(self.path.value)
|
||||
self.grid:draw()
|
||||
self.path:draw()
|
||||
else
|
||||
UI:setPreviousPage()
|
||||
self.fn(self.path.value)
|
||||
end
|
||||
|
||||
elseif event.type == 'cancel' then
|
||||
UI:setPreviousPage()
|
||||
self.fn()
|
||||
else
|
||||
return UI.Page.eventHandler(self, event)
|
||||
end
|
||||
return true
|
||||
end
|
||||
|
||||
return selectFile
|
||||
end
|
||||
16
sys/apis/fs/gitfs.lua
Normal file
16
sys/apis/fs/gitfs.lua
Normal file
@@ -0,0 +1,16 @@
|
||||
local git = require('git')
|
||||
|
||||
local gitfs = { }
|
||||
|
||||
function gitfs.mount(dir, user, repo, branch)
|
||||
if not user or not repo then
|
||||
error('gitfs syntax: user, repo, [branch]')
|
||||
end
|
||||
|
||||
local list = git.list(user, repo, branch)
|
||||
for path, url in pairs(list) do
|
||||
fs.mount(fs.combine(dir, path), 'urlfs', url)
|
||||
end
|
||||
end
|
||||
|
||||
return gitfs
|
||||
61
sys/apis/fs/linkfs.lua
Normal file
61
sys/apis/fs/linkfs.lua
Normal file
@@ -0,0 +1,61 @@
|
||||
local linkfs = { }
|
||||
|
||||
local methods = { 'exists', 'getFreeSpace', 'getSize',
|
||||
'isDir', 'isReadOnly', 'list', 'makeDir', 'open', 'getDrive' }
|
||||
|
||||
for _,m in pairs(methods) do
|
||||
linkfs[m] = function(node, dir, ...)
|
||||
dir = dir:gsub(node.mountPoint, node.source, 1)
|
||||
return fs[m](dir, ...)
|
||||
end
|
||||
end
|
||||
|
||||
function linkfs.mount(dir, source)
|
||||
if not source then
|
||||
error('Source is required')
|
||||
end
|
||||
source = fs.combine(source, '')
|
||||
if fs.isDir(source) then
|
||||
return {
|
||||
source = source,
|
||||
nodes = { },
|
||||
}
|
||||
end
|
||||
return {
|
||||
source = source
|
||||
}
|
||||
end
|
||||
|
||||
function linkfs.copy(node, s, t)
|
||||
s = s:gsub(node.mountPoint, node.source, 1)
|
||||
t = t:gsub(node.mountPoint, node.source, 1)
|
||||
return fs.copy(s, t)
|
||||
end
|
||||
|
||||
function linkfs.delete(node, dir)
|
||||
if dir == node.mountPoint then
|
||||
fs.unmount(node.mountPoint)
|
||||
else
|
||||
dir = dir:gsub(node.mountPoint, node.source, 1)
|
||||
return fs.delete(dir)
|
||||
end
|
||||
end
|
||||
|
||||
function linkfs.find(node, spec)
|
||||
spec = spec:gsub(node.mountPoint, node.source, 1)
|
||||
|
||||
local list = fs.find(spec)
|
||||
for k,f in ipairs(list) do
|
||||
list[k] = f:gsub(node.source, node.mountPoint, 1)
|
||||
end
|
||||
|
||||
return list
|
||||
end
|
||||
|
||||
function linkfs.move(node, s, t)
|
||||
s = s:gsub(node.mountPoint, node.source, 1)
|
||||
t = t:gsub(node.mountPoint, node.source, 1)
|
||||
return fs.move(s, t)
|
||||
end
|
||||
|
||||
return linkfs
|
||||
161
sys/apis/fs/netfs.lua
Normal file
161
sys/apis/fs/netfs.lua
Normal file
@@ -0,0 +1,161 @@
|
||||
local Socket = require('socket')
|
||||
local synchronized = require('sync')
|
||||
|
||||
local netfs = { }
|
||||
|
||||
local function remoteCommand(node, msg)
|
||||
|
||||
if not node.socket then
|
||||
node.socket = Socket.connect(node.id, 139)
|
||||
end
|
||||
|
||||
if not node.socket then
|
||||
error('netfs: Unable to establish connection to ' .. node.id)
|
||||
fs.unmount(node.mountPoint)
|
||||
return
|
||||
end
|
||||
|
||||
local ret
|
||||
synchronized(node.socket, function()
|
||||
node.socket:write(msg)
|
||||
ret = node.socket:read(2)
|
||||
end)
|
||||
|
||||
if ret then
|
||||
return ret.response
|
||||
end
|
||||
node.socket:close()
|
||||
node.socket = nil
|
||||
error('netfs: Connection failed', 2)
|
||||
end
|
||||
|
||||
local methods = { 'delete', 'exists', 'getFreeSpace', 'getSize', 'makeDir' }
|
||||
|
||||
local function resolveDir(dir, node)
|
||||
dir = dir:gsub(node.mountPoint, '', 1)
|
||||
return fs.combine(node.directory, dir)
|
||||
end
|
||||
|
||||
for _,m in pairs(methods) do
|
||||
netfs[m] = function(node, dir)
|
||||
dir = resolveDir(dir, node)
|
||||
|
||||
return remoteCommand(node, {
|
||||
fn = m,
|
||||
args = { dir },
|
||||
})
|
||||
end
|
||||
end
|
||||
|
||||
function netfs.mount(dir, id, directory)
|
||||
if not id or not tonumber(id) then
|
||||
error('ramfs syntax: computerId [directory]')
|
||||
end
|
||||
return {
|
||||
id = tonumber(id),
|
||||
nodes = { },
|
||||
directory = directory or '',
|
||||
}
|
||||
end
|
||||
|
||||
function netfs.getDrive()
|
||||
return 'net'
|
||||
end
|
||||
|
||||
function netfs.complete(node, partial, dir, includeFiles, includeSlash)
|
||||
dir = resolveDir(dir, node)
|
||||
|
||||
return remoteCommand(node, {
|
||||
fn = 'complete',
|
||||
args = { partial, dir, includeFiles, includeSlash },
|
||||
})
|
||||
end
|
||||
|
||||
function netfs.copy(node, s, t)
|
||||
s = resolveDir(s, node)
|
||||
t = resolveDir(t, node)
|
||||
|
||||
return remoteCommand(node, {
|
||||
fn = 'copy',
|
||||
args = { s, t },
|
||||
})
|
||||
end
|
||||
|
||||
function netfs.isDir(node, dir)
|
||||
if dir == node.mountPoint and node.directory == '' then
|
||||
return true
|
||||
end
|
||||
return remoteCommand(node, {
|
||||
fn = 'isDir',
|
||||
args = { resolveDir(dir, node) },
|
||||
})
|
||||
end
|
||||
|
||||
function netfs.isReadOnly(node, dir)
|
||||
if dir == node.mountPoint and node.directory == '' then
|
||||
return false
|
||||
end
|
||||
return remoteCommand(node, {
|
||||
fn = 'isReadOnly',
|
||||
args = { resolveDir(dir, node) },
|
||||
})
|
||||
end
|
||||
|
||||
function netfs.find(node, spec)
|
||||
spec = resolveDir(spec, node)
|
||||
local list = remoteCommand(node, {
|
||||
fn = 'find',
|
||||
args = { spec },
|
||||
})
|
||||
|
||||
for k,f in ipairs(list) do
|
||||
list[k] = fs.combine(node.mountPoint, f)
|
||||
end
|
||||
|
||||
return list
|
||||
end
|
||||
|
||||
function netfs.list(node, dir, full)
|
||||
dir = resolveDir(dir, node)
|
||||
|
||||
local r = remoteCommand(node, {
|
||||
fn = 'list',
|
||||
args = { dir, full },
|
||||
})
|
||||
return r
|
||||
end
|
||||
|
||||
function netfs.move(node, s, t)
|
||||
s = resolveDir(s, node)
|
||||
t = resolveDir(t, node)
|
||||
|
||||
return remoteCommand(node, {
|
||||
fn = 'move',
|
||||
args = { s, t },
|
||||
})
|
||||
end
|
||||
|
||||
function netfs.open(node, fn, fl)
|
||||
fn = resolveDir(fn, node)
|
||||
|
||||
local vfh = remoteCommand(node, {
|
||||
fn = 'open',
|
||||
args = { fn, fl },
|
||||
})
|
||||
|
||||
if vfh then
|
||||
vfh.node = node
|
||||
for _,m in ipairs(vfh.methods) do
|
||||
vfh[m] = function(...)
|
||||
return remoteCommand(node, {
|
||||
fn = 'fileOp',
|
||||
args = { vfh.fileUid, m, ... },
|
||||
})
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
return vfh
|
||||
end
|
||||
|
||||
return netfs
|
||||
153
sys/apis/fs/ramfs.lua
Normal file
153
sys/apis/fs/ramfs.lua
Normal file
@@ -0,0 +1,153 @@
|
||||
local ramfs = { }
|
||||
|
||||
function ramfs.mount(dir, nodeType)
|
||||
if nodeType == 'directory' then
|
||||
return {
|
||||
nodes = { },
|
||||
size = 0,
|
||||
}
|
||||
elseif nodeType == 'file' then
|
||||
return {
|
||||
size = 0,
|
||||
}
|
||||
end
|
||||
error('ramfs syntax: [directory, file]')
|
||||
end
|
||||
|
||||
function ramfs.delete(node, dir)
|
||||
if node.mountPoint == dir then
|
||||
fs.unmount(node.mountPoint)
|
||||
end
|
||||
end
|
||||
|
||||
function ramfs.exists(node, fn)
|
||||
return node.mountPoint == fn
|
||||
end
|
||||
|
||||
function ramfs.getSize(node)
|
||||
return node.size
|
||||
end
|
||||
|
||||
function ramfs.isReadOnly()
|
||||
return false
|
||||
end
|
||||
|
||||
function ramfs.makeDir(node, dir)
|
||||
fs.mount(dir, 'ramfs', 'directory')
|
||||
end
|
||||
|
||||
function ramfs.isDir(node)
|
||||
return not not node.nodes
|
||||
end
|
||||
|
||||
function ramfs.getDrive()
|
||||
return 'ram'
|
||||
end
|
||||
|
||||
function ramfs.list(node, dir, full)
|
||||
if node.nodes and node.mountPoint == dir then
|
||||
local files = { }
|
||||
if full then
|
||||
for f,n in pairs(node.nodes) do
|
||||
table.insert(files, {
|
||||
name = f,
|
||||
isDir = fs.isDir(fs.combine(dir, f)),
|
||||
size = fs.getSize(fs.combine(dir, f)),
|
||||
})
|
||||
end
|
||||
else
|
||||
for k,v in pairs(node.nodes) do
|
||||
table.insert(files, k)
|
||||
end
|
||||
end
|
||||
return files
|
||||
end
|
||||
error('Not a directory')
|
||||
end
|
||||
|
||||
function ramfs.open(node, fn, fl)
|
||||
|
||||
if fl ~= 'r' and fl ~= 'w' and fl ~= 'rb' and fl ~= 'wb' then
|
||||
error('Unsupported mode')
|
||||
end
|
||||
|
||||
if fl == 'r' then
|
||||
if node.mountPoint ~= fn then
|
||||
return
|
||||
end
|
||||
|
||||
local ctr = 0
|
||||
local lines
|
||||
return {
|
||||
readLine = function()
|
||||
if not lines then
|
||||
lines = Util.split(node.contents)
|
||||
end
|
||||
ctr = ctr + 1
|
||||
return lines[ctr]
|
||||
end,
|
||||
readAll = function()
|
||||
return node.contents
|
||||
end,
|
||||
close = function()
|
||||
lines = nil
|
||||
end,
|
||||
}
|
||||
elseif fl == 'w' then
|
||||
node = fs.mount(fn, 'ramfs', 'file')
|
||||
|
||||
local c = ''
|
||||
return {
|
||||
write = function(str)
|
||||
c = c .. str
|
||||
end,
|
||||
writeLine = function(str)
|
||||
c = c .. str .. '\n'
|
||||
end,
|
||||
flush = function()
|
||||
node.contents = c
|
||||
node.size = #c
|
||||
end,
|
||||
close = function()
|
||||
node.contents = c
|
||||
node.size = #c
|
||||
c = nil
|
||||
end,
|
||||
}
|
||||
elseif fl == 'rb' then
|
||||
if node.mountPoint ~= fn or not node.contents then
|
||||
return
|
||||
end
|
||||
|
||||
local ctr = 0
|
||||
return {
|
||||
read = function()
|
||||
ctr = ctr + 1
|
||||
return node.contents[ctr]
|
||||
end,
|
||||
close = function()
|
||||
end,
|
||||
}
|
||||
|
||||
elseif fl == 'wb' then
|
||||
node = fs.mount(fn, 'ramfs', 'file')
|
||||
|
||||
local c = { }
|
||||
return {
|
||||
write = function(b)
|
||||
table.insert(c, b)
|
||||
end,
|
||||
flush = function()
|
||||
node.contents = c
|
||||
node.size = #c
|
||||
end,
|
||||
close = function()
|
||||
node.contents = c
|
||||
node.size = #c
|
||||
c = nil
|
||||
end,
|
||||
}
|
||||
end
|
||||
end
|
||||
|
||||
return ramfs
|
||||
78
sys/apis/fs/urlfs.lua
Normal file
78
sys/apis/fs/urlfs.lua
Normal file
@@ -0,0 +1,78 @@
|
||||
local synchronized = require('sync')
|
||||
|
||||
local urlfs = { }
|
||||
|
||||
function urlfs.mount(dir, url)
|
||||
if not url then
|
||||
error('URL is required')
|
||||
end
|
||||
return {
|
||||
url = url,
|
||||
}
|
||||
end
|
||||
|
||||
function urlfs.delete(node, dir)
|
||||
fs.unmount(dir)
|
||||
end
|
||||
|
||||
function urlfs.exists()
|
||||
return true
|
||||
end
|
||||
|
||||
function urlfs.getSize(node)
|
||||
return node.size or 0
|
||||
end
|
||||
|
||||
function urlfs.isReadOnly()
|
||||
return true
|
||||
end
|
||||
|
||||
function urlfs.isDir()
|
||||
return false
|
||||
end
|
||||
|
||||
function urlfs.getDrive()
|
||||
return 'url'
|
||||
end
|
||||
|
||||
function urlfs.open(node, fn, fl)
|
||||
|
||||
if fl ~= 'r' then
|
||||
error('Unsupported mode')
|
||||
end
|
||||
|
||||
local c = node.cache
|
||||
if not c then
|
||||
synchronized(node.url, function()
|
||||
c = Util.download(node.url)
|
||||
end)
|
||||
if c and #c > 0 then
|
||||
node.cache = c
|
||||
node.size = #c
|
||||
end
|
||||
end
|
||||
|
||||
if not c or #c == 0 then
|
||||
return
|
||||
end
|
||||
|
||||
local ctr = 0
|
||||
local lines
|
||||
return {
|
||||
readLine = function()
|
||||
if not lines then
|
||||
lines = Util.split(c)
|
||||
end
|
||||
ctr = ctr + 1
|
||||
return lines[ctr]
|
||||
end,
|
||||
readAll = function()
|
||||
return c
|
||||
end,
|
||||
close = function()
|
||||
lines = nil
|
||||
end,
|
||||
}
|
||||
end
|
||||
|
||||
return urlfs
|
||||
40
sys/apis/git.lua
Normal file
40
sys/apis/git.lua
Normal file
@@ -0,0 +1,40 @@
|
||||
local json = require('json')
|
||||
|
||||
local TREE_URL = 'https://api.github.com/repos/%s/%s/git/trees/%s?recursive=1'
|
||||
local FILE_URL = 'https://raw.github.com/%s/%s/%s/%s'
|
||||
|
||||
local git = { }
|
||||
|
||||
function git.list(user, repo, branch)
|
||||
branch = branch or 'master'
|
||||
|
||||
local dataUrl = string.format(TREE_URL, user, repo, branch)
|
||||
local contents = Util.download(dataUrl)
|
||||
|
||||
if not contents then
|
||||
error('Invalid repository')
|
||||
end
|
||||
|
||||
local data = json.decode(contents)
|
||||
|
||||
if data.message and data.message:find("API rate limit exceeded") then
|
||||
error("Out of API calls, try again later")
|
||||
end
|
||||
|
||||
if data.message and data.message == "Not found" then
|
||||
error("Invalid repository")
|
||||
end
|
||||
|
||||
local list = { }
|
||||
|
||||
for k,v in pairs(data.tree) do
|
||||
if v.type == "blob" then
|
||||
v.path = v.path:gsub("%s","%%20")
|
||||
list[v.path] = string.format(FILE_URL, user, repo, branch, v.path)
|
||||
end
|
||||
end
|
||||
|
||||
return list
|
||||
end
|
||||
|
||||
return git
|
||||
196
sys/apis/glasses.lua
Normal file
196
sys/apis/glasses.lua
Normal file
@@ -0,0 +1,196 @@
|
||||
local class = require('class')
|
||||
local UI = require('ui')
|
||||
local Event = require('event')
|
||||
local Peripheral = require('peripheral')
|
||||
|
||||
--[[-- Glasses device --]]--
|
||||
local Glasses = class()
|
||||
function Glasses:init(args)
|
||||
|
||||
local defaults = {
|
||||
backgroundColor = colors.black,
|
||||
textColor = colors.white,
|
||||
textScale = .5,
|
||||
backgroundOpacity = .5,
|
||||
multiplier = 2.6665,
|
||||
-- multiplier = 2.333,
|
||||
}
|
||||
defaults.width, defaults.height = term.getSize()
|
||||
|
||||
UI.setProperties(defaults, args)
|
||||
UI.setProperties(self, defaults)
|
||||
|
||||
self.bridge = Peripheral.get({
|
||||
type = 'openperipheral_bridge',
|
||||
method = 'addBox',
|
||||
})
|
||||
self.bridge.clear()
|
||||
|
||||
self.setBackgroundColor = function(...) end
|
||||
self.setTextColor = function(...) end
|
||||
|
||||
self.t = { }
|
||||
for i = 1, self.height do
|
||||
self.t[i] = {
|
||||
text = string.rep(' ', self.width+1),
|
||||
--text = self.bridge.addText(0, 40+i*4, string.rep(' ', self.width+1), 0xffffff),
|
||||
bg = { },
|
||||
textFields = { },
|
||||
}
|
||||
end
|
||||
end
|
||||
|
||||
function Glasses:setBackgroundBox(boxes, ax, bx, y, bgColor)
|
||||
local colors = {
|
||||
[ colors.black ] = 0x000000,
|
||||
[ colors.brown ] = 0x7F664C,
|
||||
[ colors.blue ] = 0x253192,
|
||||
[ colors.red ] = 0xFF0000,
|
||||
[ colors.gray ] = 0x272727,
|
||||
[ colors.lime ] = 0x426A0D,
|
||||
[ colors.green ] = 0x2D5628,
|
||||
[ colors.white ] = 0xFFFFFF
|
||||
}
|
||||
|
||||
local function overlap(box, ax, bx)
|
||||
if bx < box.ax or ax > box.bx then
|
||||
return false
|
||||
end
|
||||
return true
|
||||
end
|
||||
|
||||
for _,box in pairs(boxes) do
|
||||
if overlap(box, ax, bx) then
|
||||
if box.bgColor == bgColor then
|
||||
ax = math.min(ax, box.ax)
|
||||
bx = math.max(bx, box.bx)
|
||||
box.ax = box.bx + 1
|
||||
elseif ax == box.ax then
|
||||
box.ax = bx + 1
|
||||
elseif ax > box.ax then
|
||||
if bx < box.bx then
|
||||
table.insert(boxes, { -- split
|
||||
ax = bx + 1,
|
||||
bx = box.bx,
|
||||
bgColor = box.bgColor
|
||||
})
|
||||
box.bx = ax - 1
|
||||
break
|
||||
else
|
||||
box.ax = box.bx + 1
|
||||
end
|
||||
elseif ax < box.ax then
|
||||
if bx > box.bx then
|
||||
box.ax = box.bx + 1 -- delete
|
||||
else
|
||||
box.ax = bx + 1
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
if bgColor ~= colors.black then
|
||||
table.insert(boxes, {
|
||||
ax = ax,
|
||||
bx = bx,
|
||||
bgColor = bgColor
|
||||
})
|
||||
end
|
||||
|
||||
local deleted
|
||||
repeat
|
||||
deleted = false
|
||||
for k,box in pairs(boxes) do
|
||||
if box.ax > box.bx then
|
||||
if box.box then
|
||||
box.box.delete()
|
||||
end
|
||||
table.remove(boxes, k)
|
||||
deleted = true
|
||||
break
|
||||
end
|
||||
if not box.box then
|
||||
box.box = self.bridge.addBox(
|
||||
math.floor(self.x + (box.ax - 1) * self.multiplier),
|
||||
self.y + y * 4,
|
||||
math.ceil((box.bx - box.ax + 1) * self.multiplier),
|
||||
4,
|
||||
colors[bgColor],
|
||||
self.backgroundOpacity)
|
||||
else
|
||||
box.box.setX(self.x + math.floor((box.ax - 1) * self.multiplier))
|
||||
box.box.setWidth(math.ceil((box.bx - box.ax + 1) * self.multiplier))
|
||||
end
|
||||
end
|
||||
until not deleted
|
||||
end
|
||||
|
||||
function Glasses:write(x, y, text, bg)
|
||||
|
||||
if x < 1 then
|
||||
error(' less ', 6)
|
||||
end
|
||||
if y <= #self.t then
|
||||
local line = self.t[y]
|
||||
local str = line.text
|
||||
str = str:sub(1, x-1) .. text .. str:sub(x + #text)
|
||||
self.t[y].text = str
|
||||
|
||||
for _,tf in pairs(line.textFields) do
|
||||
tf.delete()
|
||||
end
|
||||
line.textFields = { }
|
||||
|
||||
local function split(st)
|
||||
local words = { }
|
||||
local offset = 0
|
||||
while true do
|
||||
local b,e,w = st:find('(%S+)')
|
||||
if not b then
|
||||
break
|
||||
end
|
||||
table.insert(words, {
|
||||
offset = b + offset - 1,
|
||||
text = w,
|
||||
})
|
||||
offset = offset + e
|
||||
st = st:sub(e + 1)
|
||||
end
|
||||
return words
|
||||
end
|
||||
|
||||
local words = split(str)
|
||||
for _,word in pairs(words) do
|
||||
local tf = self.bridge.addText(self.x + word.offset * self.multiplier,
|
||||
self.y+y*4, '', 0xffffff)
|
||||
tf.setScale(self.textScale)
|
||||
tf.setZ(1)
|
||||
tf.setText(word.text)
|
||||
table.insert(line.textFields, tf)
|
||||
end
|
||||
|
||||
self:setBackgroundBox(line.bg, x, x + #text - 1, y, bg)
|
||||
end
|
||||
end
|
||||
|
||||
function Glasses:clear(bg)
|
||||
for _,line in pairs(self.t) do
|
||||
for _,tf in pairs(line.textFields) do
|
||||
tf.delete()
|
||||
end
|
||||
line.textFields = { }
|
||||
line.text = string.rep(' ', self.width+1)
|
||||
-- self.t[i].text.setText('')
|
||||
end
|
||||
end
|
||||
|
||||
function Glasses:reset()
|
||||
self:clear()
|
||||
self.bridge.clear()
|
||||
self.bridge.sync()
|
||||
end
|
||||
|
||||
function Glasses:sync()
|
||||
self.bridge.sync()
|
||||
end
|
||||
|
||||
return Glasses
|
||||
152
sys/apis/gps.lua
Normal file
152
sys/apis/gps.lua
Normal file
@@ -0,0 +1,152 @@
|
||||
local GPS = { }
|
||||
|
||||
function GPS.locate(timeout, debug)
|
||||
local pt = { }
|
||||
timeout = timeout or 10
|
||||
pt.x, pt.y, pt.z = gps.locate(timeout, debug)
|
||||
if pt.x then
|
||||
return pt
|
||||
end
|
||||
end
|
||||
|
||||
function GPS.isAvailable()
|
||||
return device.wireless_modem and GPS.locate()
|
||||
end
|
||||
|
||||
function GPS.getPoint(timeout, debug)
|
||||
|
||||
local pt = GPS.locate(timeout, debug)
|
||||
if not pt then
|
||||
return
|
||||
end
|
||||
|
||||
pt.x = math.floor(pt.x)
|
||||
pt.y = math.floor(pt.y)
|
||||
pt.z = math.floor(pt.z)
|
||||
|
||||
if pocket then
|
||||
pt.y = pt.y - 1
|
||||
end
|
||||
|
||||
return pt
|
||||
end
|
||||
|
||||
function GPS.getHeading()
|
||||
|
||||
if not turtle then
|
||||
return
|
||||
end
|
||||
|
||||
local apt = GPS.locate()
|
||||
if not apt then
|
||||
return
|
||||
end
|
||||
|
||||
local heading = turtle.point.heading
|
||||
|
||||
while not turtle.forward() do
|
||||
turtle.turnRight()
|
||||
if turtle.getHeading() == heading then
|
||||
printError('GPS.getPoint: Unable to move forward')
|
||||
return
|
||||
end
|
||||
end
|
||||
|
||||
local bpt = GPS.locate()
|
||||
if not bpt then
|
||||
return
|
||||
end
|
||||
|
||||
if apt.x < bpt.x then
|
||||
return 0
|
||||
elseif apt.z < bpt.z then
|
||||
return 1
|
||||
elseif apt.x > bpt.x then
|
||||
return 2
|
||||
end
|
||||
return 3
|
||||
end
|
||||
|
||||
function GPS.getPointAndHeading()
|
||||
local heading = GPS.getHeading()
|
||||
if heading then
|
||||
local pt = GPS.getPoint()
|
||||
if pt then
|
||||
pt.heading = heading
|
||||
end
|
||||
return pt
|
||||
end
|
||||
end
|
||||
|
||||
-- from stock gps API
|
||||
local function trilaterate( A, B, C )
|
||||
local a2b = B.position - A.position
|
||||
local a2c = C.position - A.position
|
||||
|
||||
if math.abs( a2b:normalize():dot( a2c:normalize() ) ) > 0.999 then
|
||||
return nil
|
||||
end
|
||||
|
||||
local d = a2b:length()
|
||||
local ex = a2b:normalize( )
|
||||
local i = ex:dot( a2c )
|
||||
local ey = (a2c - (ex * i)):normalize()
|
||||
local j = ey:dot( a2c )
|
||||
local ez = ex:cross( ey )
|
||||
|
||||
local r1 = A.distance
|
||||
local r2 = B.distance
|
||||
local r3 = C.distance
|
||||
|
||||
local x = (r1*r1 - r2*r2 + d*d) / (2*d)
|
||||
local y = (r1*r1 - r3*r3 - x*x + (x-i)*(x-i) + j*j) / (2*j)
|
||||
|
||||
local result = A.position + (ex * x) + (ey * y)
|
||||
|
||||
local zSquared = r1*r1 - x*x - y*y
|
||||
if zSquared > 0 then
|
||||
local z = math.sqrt( zSquared )
|
||||
local result1 = result + (ez * z)
|
||||
local result2 = result - (ez * z)
|
||||
|
||||
local rounded1, rounded2 = result1:round(), result2:round()
|
||||
if rounded1.x ~= rounded2.x or rounded1.y ~= rounded2.y or rounded1.z ~= rounded2.z then
|
||||
return rounded1, rounded2
|
||||
else
|
||||
return rounded1
|
||||
end
|
||||
end
|
||||
return result:round()
|
||||
end
|
||||
|
||||
local function narrow( p1, p2, fix )
|
||||
local dist1 = math.abs( (p1 - fix.position):length() - fix.distance )
|
||||
local dist2 = math.abs( (p2 - fix.position):length() - fix.distance )
|
||||
|
||||
if math.abs(dist1 - dist2) < 0.05 then
|
||||
return p1, p2
|
||||
elseif dist1 < dist2 then
|
||||
return p1:round()
|
||||
else
|
||||
return p2:round()
|
||||
end
|
||||
end
|
||||
-- end stock gps api
|
||||
|
||||
function GPS.trilaterate(tFixes)
|
||||
local pos1, pos2 = trilaterate(tFixes[1], tFixes[2], tFixes[3])
|
||||
|
||||
if pos2 then
|
||||
pos1, pos2 = narrow(pos1, pos2, tFixes[4])
|
||||
end
|
||||
|
||||
if pos1 and pos2 then
|
||||
print("Ambiguous position")
|
||||
print("Could be "..pos1.x..","..pos1.y..","..pos1.z.." or "..pos2.x..","..pos2.y..","..pos2.z )
|
||||
return
|
||||
end
|
||||
|
||||
return pos1
|
||||
end
|
||||
|
||||
return GPS
|
||||
47
sys/apis/history.lua
Normal file
47
sys/apis/history.lua
Normal file
@@ -0,0 +1,47 @@
|
||||
local Util = require('util')
|
||||
|
||||
local History = { }
|
||||
|
||||
function History.load(filename, limit)
|
||||
|
||||
local entries = Util.readLines(filename) or { }
|
||||
local pos = #entries + 1
|
||||
|
||||
return {
|
||||
entries = entries,
|
||||
|
||||
add = function(line)
|
||||
local last = entries[pos] or entries[pos - 1]
|
||||
if not last or line ~= last then
|
||||
table.insert(entries, line)
|
||||
if limit then
|
||||
while #entries > limit do
|
||||
table.remove(entries, 1)
|
||||
end
|
||||
end
|
||||
Util.writeLines(filename, entries)
|
||||
pos = #entries + 1
|
||||
end
|
||||
end,
|
||||
|
||||
setPosition = function(p)
|
||||
pos = p
|
||||
end,
|
||||
|
||||
back = function()
|
||||
if pos > 1 then
|
||||
pos = pos - 1
|
||||
return entries[pos]
|
||||
end
|
||||
end,
|
||||
|
||||
forward = function()
|
||||
if pos <= #entries then
|
||||
pos = pos + 1
|
||||
return entries[pos]
|
||||
end
|
||||
end,
|
||||
}
|
||||
end
|
||||
|
||||
return History
|
||||
69
sys/apis/injector.lua
Normal file
69
sys/apis/injector.lua
Normal file
@@ -0,0 +1,69 @@
|
||||
local resolver, loader
|
||||
|
||||
local function resolveFile(filename, dir, lua_path)
|
||||
|
||||
if filename:sub(1, 1) == "/" then
|
||||
if not fs.exists(filename) then
|
||||
error('Unable to load: ' .. filename, 2)
|
||||
end
|
||||
return filename
|
||||
end
|
||||
|
||||
if dir then
|
||||
local path = fs.combine(dir, filename)
|
||||
if fs.exists(path) and not fs.isDir(path) then
|
||||
return path
|
||||
end
|
||||
end
|
||||
|
||||
if lua_path then
|
||||
for dir in string.gmatch(lua_path, "[^:]+") do
|
||||
local path = fs.combine(dir, filename)
|
||||
if fs.exists(path) and not fs.isDir(path) then
|
||||
return path
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
error('Unable to load: ' .. filename, 2)
|
||||
end
|
||||
|
||||
local function requireWrapper(env)
|
||||
|
||||
local modules = { }
|
||||
|
||||
return function(filename)
|
||||
|
||||
local dir = DIR
|
||||
if not dir and shell and type(shell.dir) == 'function' then
|
||||
dir = shell.dir()
|
||||
end
|
||||
|
||||
local fname = resolver(filename:gsub('%.', '/') .. '.lua',
|
||||
dir or '', LUA_PATH or '/sys/apis')
|
||||
|
||||
local rname = fname:gsub('%/', '.'):gsub('%.lua', '')
|
||||
|
||||
local module = modules[rname]
|
||||
if not module then
|
||||
|
||||
local f, err = loader(fname, env)
|
||||
if not f then
|
||||
error(err)
|
||||
end
|
||||
module = f(rname)
|
||||
modules[rname] = module
|
||||
end
|
||||
|
||||
return module
|
||||
end
|
||||
end
|
||||
|
||||
local args = { ... }
|
||||
resolver = args[1] or resolveFile
|
||||
loader = args[2] or loadfile
|
||||
|
||||
return function(env)
|
||||
setfenv(requireWrapper, env)
|
||||
return requireWrapper(env)
|
||||
end
|
||||
112
sys/apis/input.lua
Normal file
112
sys/apis/input.lua
Normal file
@@ -0,0 +1,112 @@
|
||||
local input = { }
|
||||
|
||||
function input:translate(event, code)
|
||||
|
||||
if event == 'key' then
|
||||
local ch = keys.getName(code)
|
||||
if ch then
|
||||
|
||||
if code == keys.leftCtrl or code == keys.rightCtrl then
|
||||
self.control = true
|
||||
self.combo = false
|
||||
return
|
||||
end
|
||||
|
||||
if code == keys.leftShift or code == keys.rightShift then
|
||||
self.shift = true
|
||||
self.combo = false
|
||||
return
|
||||
end
|
||||
|
||||
if self.shift then
|
||||
if #ch > 1 then
|
||||
ch = 'shift-' .. ch
|
||||
elseif self.control then
|
||||
-- will create control-X
|
||||
-- better than shift-control-x
|
||||
ch = ch:upper()
|
||||
end
|
||||
self.combo = true
|
||||
end
|
||||
|
||||
if self.control then
|
||||
ch = 'control-' .. ch
|
||||
self.combo = true
|
||||
-- even return numbers such as
|
||||
-- control-seven
|
||||
return ch
|
||||
end
|
||||
|
||||
-- filter out characters that will be processed in
|
||||
-- the subsequent char event
|
||||
if ch and #ch > 1 and (code < 2 or code > 11) then
|
||||
return ch
|
||||
end
|
||||
end
|
||||
|
||||
elseif event == 'key_up' then
|
||||
|
||||
if code == keys.leftCtrl or code == keys.rightCtrl then
|
||||
self.control = false
|
||||
elseif code == keys.leftShift or code == keys.rightShift then
|
||||
self.shift = false
|
||||
else
|
||||
return
|
||||
end
|
||||
|
||||
-- only send through the shift / control event if it wasn't
|
||||
-- used in combination with another event
|
||||
if not self.combo then
|
||||
return keys.getName(code)
|
||||
end
|
||||
|
||||
elseif event == 'char' then
|
||||
if not self.control then
|
||||
self.combo = true
|
||||
return event
|
||||
end
|
||||
|
||||
elseif event == 'mouse_click' then
|
||||
|
||||
local buttons = { 'mouse_click', 'mouse_rightclick', 'mouse_doubleclick' }
|
||||
|
||||
self.combo = true
|
||||
if self.shift then
|
||||
return 'shift-' .. buttons[code]
|
||||
end
|
||||
return buttons[code]
|
||||
|
||||
elseif event == "mouse_scroll" then
|
||||
local directions = {
|
||||
[ -1 ] = 'scrollUp',
|
||||
[ 1 ] = 'scrollDown'
|
||||
}
|
||||
return directions[code]
|
||||
|
||||
elseif event == 'paste' then
|
||||
self.combo = true
|
||||
return event
|
||||
|
||||
elseif event == 'mouse_drag' then
|
||||
return event
|
||||
end
|
||||
end
|
||||
|
||||
-- can be useful for testing what keys are generated
|
||||
function input:test()
|
||||
print('press a key...')
|
||||
while true do
|
||||
local e, code = os.pullEvent()
|
||||
|
||||
if e == 'char' and code == 'q' then
|
||||
break
|
||||
end
|
||||
|
||||
local ch = input:translate(e, code)
|
||||
if ch then
|
||||
print(e .. ' ' .. code .. ' ' .. ch)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
return input
|
||||
213
sys/apis/json.lua
Normal file
213
sys/apis/json.lua
Normal file
@@ -0,0 +1,213 @@
|
||||
-- credit ElvishJerricco
|
||||
-- http://pastebin.com/raw.php?i=4nRg9CHU
|
||||
|
||||
local json = { }
|
||||
|
||||
------------------------------------------------------------------ utils
|
||||
local controls = {["\n"]="\\n", ["\r"]="\\r", ["\t"]="\\t", ["\b"]="\\b", ["\f"]="\\f", ["\""]="\\\"", ["\\"]="\\\\"}
|
||||
|
||||
local function isArray(t)
|
||||
local max = 0
|
||||
for k,v in pairs(t) do
|
||||
if type(k) ~= "number" then
|
||||
return false
|
||||
elseif k > max then
|
||||
max = k
|
||||
end
|
||||
end
|
||||
return max == #t
|
||||
end
|
||||
|
||||
local whites = {['\n']=true; ['\r']=true; ['\t']=true; [' ']=true; [',']=true; [':']=true}
|
||||
local function removeWhite(str)
|
||||
while whites[str:sub(1, 1)] do
|
||||
str = str:sub(2)
|
||||
end
|
||||
return str
|
||||
end
|
||||
|
||||
------------------------------------------------------------------ encoding
|
||||
|
||||
local function encodeCommon(val, pretty, tabLevel, tTracking)
|
||||
local str = ""
|
||||
|
||||
-- Tabbing util
|
||||
local function tab(s)
|
||||
str = str .. ("\t"):rep(tabLevel) .. s
|
||||
end
|
||||
|
||||
local function arrEncoding(val, bracket, closeBracket, iterator, loopFunc)
|
||||
str = str .. bracket
|
||||
if pretty then
|
||||
str = str .. "\n"
|
||||
tabLevel = tabLevel + 1
|
||||
end
|
||||
for k,v in iterator(val) do
|
||||
tab("")
|
||||
loopFunc(k,v)
|
||||
str = str .. ","
|
||||
if pretty then str = str .. "\n" end
|
||||
end
|
||||
if pretty then
|
||||
tabLevel = tabLevel - 1
|
||||
end
|
||||
if str:sub(-2) == ",\n" then
|
||||
str = str:sub(1, -3) .. "\n"
|
||||
elseif str:sub(-1) == "," then
|
||||
str = str:sub(1, -2)
|
||||
end
|
||||
tab(closeBracket)
|
||||
end
|
||||
|
||||
-- Table encoding
|
||||
if type(val) == "table" then
|
||||
assert(not tTracking[val], "Cannot encode a table holding itself recursively")
|
||||
tTracking[val] = true
|
||||
if isArray(val) then
|
||||
arrEncoding(val, "[", "]", ipairs, function(k,v)
|
||||
str = str .. encodeCommon(v, pretty, tabLevel, tTracking)
|
||||
end)
|
||||
else
|
||||
arrEncoding(val, "{", "}", pairs, function(k,v)
|
||||
assert(type(k) == "string", "JSON object keys must be strings", 2)
|
||||
str = str .. encodeCommon(k, pretty, tabLevel, tTracking)
|
||||
str = str .. (pretty and ": " or ":") .. encodeCommon(v, pretty, tabLevel, tTracking)
|
||||
end)
|
||||
end
|
||||
-- String encoding
|
||||
elseif type(val) == "string" then
|
||||
str = '"' .. val:gsub("[%c\"\\]", controls) .. '"'
|
||||
-- Number encoding
|
||||
elseif type(val) == "number" or type(val) == "boolean" then
|
||||
str = tostring(val)
|
||||
else
|
||||
error("JSON only supports arrays, objects, numbers, booleans, and strings", 2)
|
||||
end
|
||||
return str
|
||||
end
|
||||
|
||||
function json.encode(val)
|
||||
return encodeCommon(val, false, 0, {})
|
||||
end
|
||||
|
||||
function json.encodePretty(val)
|
||||
return encodeCommon(val, true, 0, {})
|
||||
end
|
||||
|
||||
------------------------------------------------------------------ decoding
|
||||
|
||||
local decodeControls = {}
|
||||
for k,v in pairs(controls) do
|
||||
decodeControls[v] = k
|
||||
end
|
||||
|
||||
local function parseBoolean(str)
|
||||
if str:sub(1, 4) == "true" then
|
||||
return true, removeWhite(str:sub(5))
|
||||
else
|
||||
return false, removeWhite(str:sub(6))
|
||||
end
|
||||
end
|
||||
|
||||
local function parseNull(str)
|
||||
return nil, removeWhite(str:sub(5))
|
||||
end
|
||||
|
||||
local numChars = {['e']=true; ['E']=true; ['+']=true; ['-']=true; ['.']=true}
|
||||
local function parseNumber(str)
|
||||
local i = 1
|
||||
while numChars[str:sub(i, i)] or tonumber(str:sub(i, i)) do
|
||||
i = i + 1
|
||||
end
|
||||
local val = tonumber(str:sub(1, i - 1))
|
||||
str = removeWhite(str:sub(i))
|
||||
return val, str
|
||||
end
|
||||
|
||||
local function parseString(str)
|
||||
str = str:sub(2)
|
||||
local s = ""
|
||||
while str:sub(1,1) ~= "\"" do
|
||||
local next = str:sub(1,1)
|
||||
str = str:sub(2)
|
||||
assert(next ~= "\n", "Unclosed string")
|
||||
|
||||
if next == "\\" then
|
||||
local escape = str:sub(1,1)
|
||||
str = str:sub(2)
|
||||
|
||||
next = assert(decodeControls[next..escape], "Invalid escape character")
|
||||
end
|
||||
|
||||
s = s .. next
|
||||
end
|
||||
return s, removeWhite(str:sub(2))
|
||||
end
|
||||
|
||||
function json.parseArray(str)
|
||||
str = removeWhite(str:sub(2))
|
||||
|
||||
local val = {}
|
||||
local i = 1
|
||||
while str:sub(1, 1) ~= "]" do
|
||||
local v
|
||||
v, str = json.parseValue(str)
|
||||
val[i] = v
|
||||
i = i + 1
|
||||
str = removeWhite(str)
|
||||
end
|
||||
str = removeWhite(str:sub(2))
|
||||
return val, str
|
||||
end
|
||||
|
||||
function json.parseValue(str)
|
||||
local fchar = str:sub(1, 1)
|
||||
if fchar == "{" then
|
||||
return json.parseObject(str)
|
||||
elseif fchar == "[" then
|
||||
return json.parseArray(str)
|
||||
elseif tonumber(fchar) ~= nil or numChars[fchar] then
|
||||
return parseNumber(str)
|
||||
elseif str:sub(1, 4) == "true" or str:sub(1, 5) == "false" then
|
||||
return parseBoolean(str)
|
||||
elseif fchar == "\"" then
|
||||
return parseString(str)
|
||||
elseif str:sub(1, 4) == "null" then
|
||||
return parseNull(str)
|
||||
end
|
||||
end
|
||||
|
||||
function json.parseMember(str)
|
||||
local k, val
|
||||
k, str = json.parseValue(str)
|
||||
val, str = json.parseValue(str)
|
||||
return k, val, str
|
||||
end
|
||||
|
||||
function json.parseObject(str)
|
||||
str = removeWhite(str:sub(2))
|
||||
|
||||
local val = {}
|
||||
while str:sub(1, 1) ~= "}" do
|
||||
local k, v = nil, nil
|
||||
k, v, str = json.parseMember(str)
|
||||
val[k] = v
|
||||
str = removeWhite(str)
|
||||
end
|
||||
str = removeWhite(str:sub(2))
|
||||
return val, str
|
||||
end
|
||||
|
||||
function json.decode(str)
|
||||
str = removeWhite(str)
|
||||
return json.parseValue(str)
|
||||
end
|
||||
|
||||
function json.decodeFromFile(path)
|
||||
local file = assert(fs.open(path, "r"))
|
||||
local decoded = decode(file.readAll())
|
||||
file.close()
|
||||
return decoded
|
||||
end
|
||||
|
||||
return json
|
||||
105
sys/apis/jumper/core/assert.lua
Normal file
105
sys/apis/jumper/core/assert.lua
Normal file
@@ -0,0 +1,105 @@
|
||||
-- Various assertion function for API methods argument-checking
|
||||
|
||||
if (...) then
|
||||
|
||||
-- Dependancies
|
||||
local _PATH = (...):gsub('%.core.assert$','')
|
||||
local Utils = require (_PATH .. '.core.utils')
|
||||
|
||||
-- Local references
|
||||
local lua_type = type
|
||||
local floor = math.floor
|
||||
local concat = table.concat
|
||||
local next = next
|
||||
local pairs = pairs
|
||||
local getmetatable = getmetatable
|
||||
|
||||
-- Is I an integer ?
|
||||
local function isInteger(i)
|
||||
return lua_type(i) ==('number') and (floor(i)==i)
|
||||
end
|
||||
|
||||
-- Override lua_type to return integers
|
||||
local function type(v)
|
||||
return isInteger(v) and 'int' or lua_type(v)
|
||||
end
|
||||
|
||||
-- Does the given array contents match a predicate type ?
|
||||
local function arrayContentsMatch(t,...)
|
||||
local n_count = Utils.arraySize(t)
|
||||
if n_count < 1 then return false end
|
||||
local init_count = t[0] and 0 or 1
|
||||
local n_count = (t[0] and n_count-1 or n_count)
|
||||
local types = {...}
|
||||
if types then types = concat(types) end
|
||||
for i=init_count,n_count,1 do
|
||||
if not t[i] then return false end
|
||||
if types then
|
||||
if not types:match(type(t[i])) then return false end
|
||||
end
|
||||
end
|
||||
return true
|
||||
end
|
||||
|
||||
-- Checks if arg is a valid array map
|
||||
local function isMap(m)
|
||||
if not arrayContentsMatch(m, 'table') then return false end
|
||||
local lsize = Utils.arraySize(m[next(m)])
|
||||
for k,v in pairs(m) do
|
||||
if not arrayContentsMatch(m[k], 'string', 'int') then return false end
|
||||
if Utils.arraySize(v)~=lsize then return false end
|
||||
end
|
||||
return true
|
||||
end
|
||||
|
||||
-- Checks if s is a valid string map
|
||||
local function isStringMap(s)
|
||||
if lua_type(s) ~= 'string' then return false end
|
||||
local w
|
||||
for row in s:gmatch('[^\n\r]+') do
|
||||
if not row then return false end
|
||||
w = w or #row
|
||||
if w ~= #row then return false end
|
||||
end
|
||||
return true
|
||||
end
|
||||
|
||||
-- Does instance derive straight from class
|
||||
local function derives(instance, class)
|
||||
return getmetatable(instance) == class
|
||||
end
|
||||
|
||||
-- Does instance inherits from class
|
||||
local function inherits(instance, class)
|
||||
return (getmetatable(getmetatable(instance)) == class)
|
||||
end
|
||||
|
||||
-- Is arg a boolean
|
||||
local function isBoolean(b)
|
||||
return (b==true or b==false)
|
||||
end
|
||||
|
||||
-- Is arg nil ?
|
||||
local function isNil(n)
|
||||
return (n==nil)
|
||||
end
|
||||
|
||||
local function matchType(value, types)
|
||||
return types:match(type(value))
|
||||
end
|
||||
|
||||
return {
|
||||
arrayContentsMatch = arrayContentsMatch,
|
||||
derives = derives,
|
||||
inherits = inherits,
|
||||
isInteger = isInteger,
|
||||
isBool = isBoolean,
|
||||
isMap = isMap,
|
||||
isStrMap = isStringMap,
|
||||
isOutOfRange = isOutOfRange,
|
||||
isNil = isNil,
|
||||
type = type,
|
||||
matchType = matchType
|
||||
}
|
||||
|
||||
end
|
||||
175
sys/apis/jumper/core/bheap.lua
Normal file
175
sys/apis/jumper/core/bheap.lua
Normal file
@@ -0,0 +1,175 @@
|
||||
--- A light implementation of Binary heaps data structure.
|
||||
-- While running a search, some search algorithms (Astar, Dijkstra, Jump Point Search) have to maintains
|
||||
-- a list of nodes called __open list__. Retrieve from this list the lowest cost node can be quite slow,
|
||||
-- as it normally requires to skim through the full set of nodes stored in this list. This becomes a real
|
||||
-- problem especially when dozens of nodes are being processed (on large maps).
|
||||
--
|
||||
-- The current module implements a <a href="http://www.policyalmanac.org/games/binaryHeaps.htm">binary heap</a>
|
||||
-- data structure, from which the search algorithm will instantiate an open list, and cache the nodes being
|
||||
-- examined during a search. As such, retrieving the lower-cost node is faster and globally makes the search end
|
||||
-- up quickly.
|
||||
--
|
||||
-- This module is internally used by the library on purpose.
|
||||
-- It should normally not be used explicitely, yet it remains fully accessible.
|
||||
--
|
||||
|
||||
--[[
|
||||
Notes:
|
||||
This lighter implementation of binary heaps, based on :
|
||||
https://github.com/Yonaba/Binary-Heaps
|
||||
--]]
|
||||
|
||||
if (...) then
|
||||
|
||||
-- Dependency
|
||||
local Utils = require((...):gsub('%.bheap$','.utils'))
|
||||
|
||||
-- Local reference
|
||||
local floor = math.floor
|
||||
|
||||
-- Default comparison function
|
||||
local function f_min(a,b) return a < b end
|
||||
|
||||
-- Percolates up
|
||||
local function percolate_up(heap, index)
|
||||
if index == 1 then return end
|
||||
local pIndex
|
||||
if index <= 1 then return end
|
||||
if index%2 == 0 then
|
||||
pIndex = index/2
|
||||
else pIndex = (index-1)/2
|
||||
end
|
||||
if not heap._sort(heap._heap[pIndex], heap._heap[index]) then
|
||||
heap._heap[pIndex], heap._heap[index] =
|
||||
heap._heap[index], heap._heap[pIndex]
|
||||
percolate_up(heap, pIndex)
|
||||
end
|
||||
end
|
||||
|
||||
-- Percolates down
|
||||
local function percolate_down(heap,index)
|
||||
local lfIndex,rtIndex,minIndex
|
||||
lfIndex = 2*index
|
||||
rtIndex = lfIndex + 1
|
||||
if rtIndex > heap._size then
|
||||
if lfIndex > heap._size then return
|
||||
else minIndex = lfIndex end
|
||||
else
|
||||
if heap._sort(heap._heap[lfIndex],heap._heap[rtIndex]) then
|
||||
minIndex = lfIndex
|
||||
else
|
||||
minIndex = rtIndex
|
||||
end
|
||||
end
|
||||
if not heap._sort(heap._heap[index],heap._heap[minIndex]) then
|
||||
heap._heap[index],heap._heap[minIndex] = heap._heap[minIndex],heap._heap[index]
|
||||
percolate_down(heap,minIndex)
|
||||
end
|
||||
end
|
||||
|
||||
-- Produces a new heap
|
||||
local function newHeap(template,comp)
|
||||
return setmetatable({_heap = {},
|
||||
_sort = comp or f_min, _size = 0},
|
||||
template)
|
||||
end
|
||||
|
||||
|
||||
--- The `heap` class.<br/>
|
||||
-- This class is callable.
|
||||
-- _Therefore,_ <code>heap(...)</code> _is used to instantiate new heaps_.
|
||||
-- @type heap
|
||||
local heap = setmetatable({},
|
||||
{__call = function(self,...)
|
||||
return newHeap(self,...)
|
||||
end})
|
||||
heap.__index = heap
|
||||
|
||||
--- Checks if a `heap` is empty
|
||||
-- @class function
|
||||
-- @treturn bool __true__ of no item is queued in the heap, __false__ otherwise
|
||||
-- @usage
|
||||
-- if myHeap:empty() then
|
||||
-- print('Heap is empty!')
|
||||
-- end
|
||||
function heap:empty()
|
||||
return (self._size==0)
|
||||
end
|
||||
|
||||
--- Clears the `heap` (removes all items queued in the heap)
|
||||
-- @class function
|
||||
-- @treturn heap self (the calling `heap` itself, can be chained)
|
||||
-- @usage myHeap:clear()
|
||||
function heap:clear()
|
||||
self._heap = {}
|
||||
self._size = 0
|
||||
self._sort = self._sort or f_min
|
||||
return self
|
||||
end
|
||||
|
||||
--- Adds a new item in the `heap`
|
||||
-- @class function
|
||||
-- @tparam value item a new value to be queued in the heap
|
||||
-- @treturn heap self (the calling `heap` itself, can be chained)
|
||||
-- @usage
|
||||
-- myHeap:push(1)
|
||||
-- -- or, with chaining
|
||||
-- myHeap:push(1):push(2):push(4)
|
||||
function heap:push(item)
|
||||
if item then
|
||||
self._size = self._size + 1
|
||||
self._heap[self._size] = item
|
||||
percolate_up(self, self._size)
|
||||
end
|
||||
return self
|
||||
end
|
||||
|
||||
--- Pops from the `heap`.
|
||||
-- Removes and returns the lowest cost item (with respect to the comparison function being used) from the `heap`.
|
||||
-- @class function
|
||||
-- @treturn value a value previously pushed into the heap
|
||||
-- @usage
|
||||
-- while not myHeap:empty() do
|
||||
-- local lowestValue = myHeap:pop()
|
||||
-- ...
|
||||
-- end
|
||||
function heap:pop()
|
||||
local root
|
||||
if self._size > 0 then
|
||||
root = self._heap[1]
|
||||
self._heap[1] = self._heap[self._size]
|
||||
self._heap[self._size] = nil
|
||||
self._size = self._size-1
|
||||
if self._size>1 then
|
||||
percolate_down(self, 1)
|
||||
end
|
||||
end
|
||||
return root
|
||||
end
|
||||
|
||||
--- Restores the `heap` property.
|
||||
-- Reorders the `heap` with respect to the comparison function being used.
|
||||
-- When given argument __item__ (a value existing in the `heap`), will sort from that very item in the `heap`.
|
||||
-- Otherwise, the whole `heap` will be cheacked.
|
||||
-- @class function
|
||||
-- @tparam[opt] value item the modified value
|
||||
-- @treturn heap self (the calling `heap` itself, can be chained)
|
||||
-- @usage myHeap:heapify()
|
||||
function heap:heapify(item)
|
||||
if self._size == 0 then return end
|
||||
if item then
|
||||
local i = Utils.indexOf(self._heap,item)
|
||||
if i then
|
||||
percolate_down(self, i)
|
||||
percolate_up(self, i)
|
||||
end
|
||||
return
|
||||
end
|
||||
for i = floor(self._size/2),1,-1 do
|
||||
percolate_down(self,i)
|
||||
end
|
||||
return self
|
||||
end
|
||||
|
||||
return heap
|
||||
end
|
||||
98
sys/apis/jumper/core/heuristics.lua
Normal file
98
sys/apis/jumper/core/heuristics.lua
Normal file
@@ -0,0 +1,98 @@
|
||||
--- Heuristic functions for search algorithms.
|
||||
-- A <a href="http://theory.stanford.edu/~amitp/GameProgramming/Heuristics.html">distance heuristic</a>
|
||||
-- provides an *estimate of the optimal distance cost* from a given location to a target.
|
||||
-- As such, it guides the pathfinder to the goal, helping it to decide which route is the best.
|
||||
--
|
||||
-- This script holds the definition of some built-in heuristics available through jumper.
|
||||
--
|
||||
-- Distance functions are internally used by the `pathfinder` to evaluate the optimal path
|
||||
-- from the start location to the goal. These functions share the same prototype:
|
||||
-- local function myHeuristic(nodeA, nodeB)
|
||||
-- -- function body
|
||||
-- end
|
||||
-- Jumper features some built-in distance heuristics, namely `MANHATTAN`, `EUCLIDIAN`, `DIAGONAL`, `CARDINTCARD`.
|
||||
-- You can also supply your own heuristic function, following the same template as above.
|
||||
|
||||
|
||||
local abs = math.abs
|
||||
local sqrt = math.sqrt
|
||||
local sqrt2 = sqrt(2)
|
||||
local max, min = math.max, math.min
|
||||
|
||||
local Heuristics = {}
|
||||
--- Manhattan distance.
|
||||
-- <br/>This heuristic is the default one being used by the `pathfinder` object.
|
||||
-- <br/>Evaluates as <code>distance = |dx|+|dy|</code>
|
||||
-- @class function
|
||||
-- @tparam node nodeA a node
|
||||
-- @tparam node nodeB another node
|
||||
-- @treturn number the distance from __nodeA__ to __nodeB__
|
||||
-- @usage
|
||||
-- -- First method
|
||||
-- pathfinder:setHeuristic('MANHATTAN')
|
||||
-- -- Second method
|
||||
-- local Distance = require ('jumper.core.heuristics')
|
||||
-- pathfinder:setHeuristic(Distance.MANHATTAN)
|
||||
function Heuristics.MANHATTAN(nodeA, nodeB)
|
||||
local dx = abs(nodeA._x - nodeB._x)
|
||||
local dy = abs(nodeA._y - nodeB._y)
|
||||
local dz = abs(nodeA._z - nodeB._z)
|
||||
return (dx + dy + dz)
|
||||
end
|
||||
|
||||
--- Euclidian distance.
|
||||
-- <br/>Evaluates as <code>distance = squareRoot(dx*dx+dy*dy)</code>
|
||||
-- @class function
|
||||
-- @tparam node nodeA a node
|
||||
-- @tparam node nodeB another node
|
||||
-- @treturn number the distance from __nodeA__ to __nodeB__
|
||||
-- @usage
|
||||
-- -- First method
|
||||
-- pathfinder:setHeuristic('EUCLIDIAN')
|
||||
-- -- Second method
|
||||
-- local Distance = require ('jumper.core.heuristics')
|
||||
-- pathfinder:setHeuristic(Distance.EUCLIDIAN)
|
||||
function Heuristics.EUCLIDIAN(nodeA, nodeB)
|
||||
local dx = nodeA._x - nodeB._x
|
||||
local dy = nodeA._y - nodeB._y
|
||||
local dz = nodeA._z - nodeB._z
|
||||
return sqrt(dx*dx+dy*dy+dz*dz)
|
||||
end
|
||||
|
||||
--- Diagonal distance.
|
||||
-- <br/>Evaluates as <code>distance = max(|dx|, abs|dy|)</code>
|
||||
-- @class function
|
||||
-- @tparam node nodeA a node
|
||||
-- @tparam node nodeB another node
|
||||
-- @treturn number the distance from __nodeA__ to __nodeB__
|
||||
-- @usage
|
||||
-- -- First method
|
||||
-- pathfinder:setHeuristic('DIAGONAL')
|
||||
-- -- Second method
|
||||
-- local Distance = require ('jumper.core.heuristics')
|
||||
-- pathfinder:setHeuristic(Distance.DIAGONAL)
|
||||
function Heuristics.DIAGONAL(nodeA, nodeB)
|
||||
local dx = abs(nodeA._x - nodeB._x)
|
||||
local dy = abs(nodeA._y - nodeB._y)
|
||||
return max(dx,dy)
|
||||
end
|
||||
|
||||
--- Cardinal/Intercardinal distance.
|
||||
-- <br/>Evaluates as <code>distance = min(dx, dy)*squareRoot(2) + max(dx, dy) - min(dx, dy)</code>
|
||||
-- @class function
|
||||
-- @tparam node nodeA a node
|
||||
-- @tparam node nodeB another node
|
||||
-- @treturn number the distance from __nodeA__ to __nodeB__
|
||||
-- @usage
|
||||
-- -- First method
|
||||
-- pathfinder:setHeuristic('CARDINTCARD')
|
||||
-- -- Second method
|
||||
-- local Distance = require ('jumper.core.heuristics')
|
||||
-- pathfinder:setHeuristic(Distance.CARDINTCARD)
|
||||
function Heuristics.CARDINTCARD(nodeA, nodeB)
|
||||
local dx = abs(nodeA._x - nodeB._x)
|
||||
local dy = abs(nodeA._y - nodeB._y)
|
||||
return min(dx,dy) * sqrt2 + max(dx,dy) - min(dx,dy)
|
||||
end
|
||||
|
||||
return Heuristics
|
||||
32
sys/apis/jumper/core/lookuptable.lua
Normal file
32
sys/apis/jumper/core/lookuptable.lua
Normal file
@@ -0,0 +1,32 @@
|
||||
local addNode(self, node, nextNode, ed)
|
||||
if not self._pathDB[node] then self._pathDB[node] = {} end
|
||||
self._pathDB[node][ed] = (nextNode == ed and node or nextNode)
|
||||
end
|
||||
|
||||
-- Path lookupTable
|
||||
local lookupTable = {}
|
||||
lookupTable.__index = lookupTable
|
||||
|
||||
function lookupTable:new()
|
||||
local lut = {_pathDB = {}}
|
||||
return setmetatable(lut, lookupTable)
|
||||
end
|
||||
|
||||
function lookupTable:addPath(path)
|
||||
local st, ed = path._nodes[1], path._nodes[#path._nodes]
|
||||
for node, count in path:nodes() do
|
||||
local nextNode = path._nodes[count+1]
|
||||
if nextNode then addNode(self, node, nextNode, ed) end
|
||||
end
|
||||
end
|
||||
|
||||
function lookupTable:hasPath(nodeA, nodeB)
|
||||
local found
|
||||
found = self._pathDB[nodeA] and self._path[nodeA][nodeB]
|
||||
if found then return true, true end
|
||||
found = self._pathDB[nodeB] and self._path[nodeB][nodeA]
|
||||
if found then return true, false end
|
||||
return false
|
||||
end
|
||||
|
||||
return lookupTable
|
||||
100
sys/apis/jumper/core/node.lua
Normal file
100
sys/apis/jumper/core/node.lua
Normal file
@@ -0,0 +1,100 @@
|
||||
--- The Node class.
|
||||
-- The `node` represents a cell (or a tile) on a collision map. Basically, for each single cell (tile)
|
||||
-- in the collision map passed-in upon initialization, a `node` object will be generated
|
||||
-- and then cached within the `grid`.
|
||||
--
|
||||
-- In the following implementation, nodes can be compared using the `<` operator. The comparison is
|
||||
-- made with regards of their `f` cost. From a given node being examined, the `pathfinder` will expand the search
|
||||
-- to the next neighbouring node having the lowest `f` cost. See `core.bheap` for more details.
|
||||
--
|
||||
|
||||
if (...) then
|
||||
|
||||
local assert = assert
|
||||
|
||||
--- The `Node` class.<br/>
|
||||
-- This class is callable.
|
||||
-- Therefore,_ <code>Node(...)</code> _acts as a shortcut to_ <code>Node:new(...)</code>.
|
||||
-- @type Node
|
||||
local Node = {}
|
||||
Node.__index = Node
|
||||
|
||||
--- Inits a new `node`
|
||||
-- @class function
|
||||
-- @tparam int x the x-coordinate of the node on the collision map
|
||||
-- @tparam int y the y-coordinate of the node on the collision map
|
||||
-- @treturn node a new `node`
|
||||
-- @usage local node = Node(3,4)
|
||||
function Node:new(x,y,z)
|
||||
return setmetatable({_x = x, _y = y, _z = z, _clearance = {}}, Node)
|
||||
end
|
||||
|
||||
-- Enables the use of operator '<' to compare nodes.
|
||||
-- Will be used to sort a collection of nodes in a binary heap on the basis of their F-cost
|
||||
function Node.__lt(A,B) return (A._f < B._f) end
|
||||
|
||||
--- Returns x-coordinate of a `node`
|
||||
-- @class function
|
||||
-- @treturn number the x-coordinate of the `node`
|
||||
-- @usage local x = node:getX()
|
||||
function Node:getX() return self._x end
|
||||
|
||||
--- Returns y-coordinate of a `node`
|
||||
-- @class function
|
||||
-- @treturn number the y-coordinate of the `node`
|
||||
-- @usage local y = node:getY()
|
||||
function Node:getY() return self._y end
|
||||
|
||||
function Node:getZ() return self._z end
|
||||
|
||||
--- Returns x and y coordinates of a `node`
|
||||
-- @class function
|
||||
-- @treturn number the x-coordinate of the `node`
|
||||
-- @treturn number the y-coordinate of the `node`
|
||||
-- @usage local x, y = node:getPos()
|
||||
function Node:getPos() return self._x, self._y, self._z end
|
||||
|
||||
--- Returns the amount of true [clearance](http://aigamedev.com/open/tutorial/clearance-based-pathfinding/#TheTrueClearanceMetric)
|
||||
-- for a given `node`
|
||||
-- @class function
|
||||
-- @tparam string|int|func walkable the value for walkable locations in the collision map array.
|
||||
-- @treturn int the clearance of the `node`
|
||||
-- @usage
|
||||
-- -- Assuming walkable was 0
|
||||
-- local clearance = node:getClearance(0)
|
||||
function Node:getClearance(walkable)
|
||||
return self._clearance[walkable]
|
||||
end
|
||||
|
||||
--- Removes the clearance value for a given walkable.
|
||||
-- @class function
|
||||
-- @tparam string|int|func walkable the value for walkable locations in the collision map array.
|
||||
-- @treturn node self (the calling `node` itself, can be chained)
|
||||
-- @usage
|
||||
-- -- Assuming walkable is defined
|
||||
-- node:removeClearance(walkable)
|
||||
function Node:removeClearance(walkable)
|
||||
self._clearance[walkable] = nil
|
||||
return self
|
||||
end
|
||||
|
||||
--- Clears temporary cached attributes of a `node`.
|
||||
-- Deletes the attributes cached within a given node after a pathfinding call.
|
||||
-- This function is internally used by the search algorithms, so you should not use it explicitely.
|
||||
-- @class function
|
||||
-- @treturn node self (the calling `node` itself, can be chained)
|
||||
-- @usage
|
||||
-- local thisNode = Node(1,2)
|
||||
-- thisNode:reset()
|
||||
function Node:reset()
|
||||
self._g, self._h, self._f = nil, nil, nil
|
||||
self._opened, self._closed, self._parent = nil, nil, nil
|
||||
return self
|
||||
end
|
||||
|
||||
return setmetatable(Node,
|
||||
{__call = function(self,...)
|
||||
return Node:new(...)
|
||||
end}
|
||||
)
|
||||
end
|
||||
201
sys/apis/jumper/core/path.lua
Normal file
201
sys/apis/jumper/core/path.lua
Normal file
@@ -0,0 +1,201 @@
|
||||
--- The Path class.
|
||||
-- The `path` class is a structure which represents a path (ordered set of nodes) from a start location to a goal.
|
||||
-- An instance from this class would be a result of a request addressed to `Pathfinder:getPath`.
|
||||
--
|
||||
-- This module is internally used by the library on purpose.
|
||||
-- It should normally not be used explicitely, yet it remains fully accessible.
|
||||
--
|
||||
|
||||
|
||||
if (...) then
|
||||
|
||||
-- Dependencies
|
||||
local _PATH = (...):match('(.+)%.path$')
|
||||
local Heuristic = require (_PATH .. '.heuristics')
|
||||
|
||||
-- Local references
|
||||
local abs, max = math.abs, math.max
|
||||
local t_insert, t_remove = table.insert, table.remove
|
||||
|
||||
--- The `Path` class.<br/>
|
||||
-- This class is callable.
|
||||
-- Therefore, <em><code>Path(...)</code></em> acts as a shortcut to <em><code>Path:new(...)</code></em>.
|
||||
-- @type Path
|
||||
local Path = {}
|
||||
Path.__index = Path
|
||||
|
||||
--- Inits a new `path`.
|
||||
-- @class function
|
||||
-- @treturn path a `path`
|
||||
-- @usage local p = Path()
|
||||
function Path:new()
|
||||
return setmetatable({_nodes = {}}, Path)
|
||||
end
|
||||
|
||||
--- Iterates on each single `node` along a `path`. At each step of iteration,
|
||||
-- returns the `node` plus a count value. Aliased as @{Path:nodes}
|
||||
-- @class function
|
||||
-- @treturn node a `node`
|
||||
-- @treturn int the count for the number of nodes
|
||||
-- @see Path:nodes
|
||||
-- @usage
|
||||
-- for node, count in p:iter() do
|
||||
-- ...
|
||||
-- end
|
||||
function Path:iter()
|
||||
local i,pathLen = 1,#self._nodes
|
||||
return function()
|
||||
if self._nodes[i] then
|
||||
i = i+1
|
||||
return self._nodes[i-1],i-1
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
--- Iterates on each single `node` along a `path`. At each step of iteration,
|
||||
-- returns a `node` plus a count value. Alias for @{Path:iter}
|
||||
-- @class function
|
||||
-- @name Path:nodes
|
||||
-- @treturn node a `node`
|
||||
-- @treturn int the count for the number of nodes
|
||||
-- @see Path:iter
|
||||
-- @usage
|
||||
-- for node, count in p:nodes() do
|
||||
-- ...
|
||||
-- end
|
||||
Path.nodes = Path.iter
|
||||
|
||||
--- Evaluates the `path` length
|
||||
-- @class function
|
||||
-- @treturn number the `path` length
|
||||
-- @usage local len = p:getLength()
|
||||
function Path:getLength()
|
||||
local len = 0
|
||||
for i = 2,#self._nodes do
|
||||
len = len + Heuristic.EUCLIDIAN(self._nodes[i], self._nodes[i-1])
|
||||
end
|
||||
return len
|
||||
end
|
||||
|
||||
--- Counts the number of steps.
|
||||
-- Returns the number of waypoints (nodes) in the current path.
|
||||
-- @class function
|
||||
-- @tparam node node a node to be added to the path
|
||||
-- @tparam[opt] int index the index at which the node will be inserted. If omitted, the node will be appended after the last node in the path.
|
||||
-- @treturn path self (the calling `path` itself, can be chained)
|
||||
-- @usage local nSteps = p:countSteps()
|
||||
function Path:addNode(node, index)
|
||||
index = index or #self._nodes+1
|
||||
t_insert(self._nodes, index, node)
|
||||
return self
|
||||
end
|
||||
|
||||
|
||||
--- `Path` filling modifier. Interpolates between non contiguous nodes along a `path`
|
||||
-- to build a fully continuous `path`. This maybe useful when using search algorithms such as Jump Point Search.
|
||||
-- Does the opposite of @{Path:filter}
|
||||
-- @class function
|
||||
-- @treturn path self (the calling `path` itself, can be chained)
|
||||
-- @see Path:filter
|
||||
-- @usage p:fill()
|
||||
function Path:fill()
|
||||
local i = 2
|
||||
local xi,yi,dx,dy
|
||||
local N = #self._nodes
|
||||
local incrX, incrY
|
||||
while true do
|
||||
xi,yi = self._nodes[i]._x,self._nodes[i]._y
|
||||
dx,dy = xi-self._nodes[i-1]._x,yi-self._nodes[i-1]._y
|
||||
if (abs(dx) > 1 or abs(dy) > 1) then
|
||||
incrX = dx/max(abs(dx),1)
|
||||
incrY = dy/max(abs(dy),1)
|
||||
t_insert(self._nodes, i, self._grid:getNodeAt(self._nodes[i-1]._x + incrX, self._nodes[i-1]._y +incrY))
|
||||
N = N+1
|
||||
else i=i+1
|
||||
end
|
||||
if i>N then break end
|
||||
end
|
||||
return self
|
||||
end
|
||||
|
||||
--- `Path` compression modifier. Given a `path`, eliminates useless nodes to return a lighter `path`
|
||||
-- consisting of straight moves. Does the opposite of @{Path:fill}
|
||||
-- @class function
|
||||
-- @treturn path self (the calling `path` itself, can be chained)
|
||||
-- @see Path:fill
|
||||
-- @usage p:filter()
|
||||
function Path:filter()
|
||||
local i = 2
|
||||
local xi,yi,dx,dy, olddx, olddy
|
||||
xi,yi = self._nodes[i]._x, self._nodes[i]._y
|
||||
dx, dy = xi - self._nodes[i-1]._x, yi-self._nodes[i-1]._y
|
||||
while true do
|
||||
olddx, olddy = dx, dy
|
||||
if self._nodes[i+1] then
|
||||
i = i+1
|
||||
xi, yi = self._nodes[i]._x, self._nodes[i]._y
|
||||
dx, dy = xi - self._nodes[i-1]._x, yi - self._nodes[i-1]._y
|
||||
if olddx == dx and olddy == dy then
|
||||
t_remove(self._nodes, i-1)
|
||||
i = i - 1
|
||||
end
|
||||
else break end
|
||||
end
|
||||
return self
|
||||
end
|
||||
|
||||
--- Clones a `path`.
|
||||
-- @class function
|
||||
-- @treturn path a `path`
|
||||
-- @usage local p = path:clone()
|
||||
function Path:clone()
|
||||
local p = Path:new()
|
||||
for node in self:nodes() do p:addNode(node) end
|
||||
return p
|
||||
end
|
||||
|
||||
--- Checks if a `path` is equal to another. It also supports *filtered paths* (see @{Path:filter}).
|
||||
-- @class function
|
||||
-- @tparam path p2 a path
|
||||
-- @treturn boolean a boolean
|
||||
-- @usage print(myPath:isEqualTo(anotherPath))
|
||||
function Path:isEqualTo(p2)
|
||||
local p1 = self:clone():filter()
|
||||
local p2 = p2:clone():filter()
|
||||
for node, count in p1:nodes() do
|
||||
if not p2._nodes[count] then return false end
|
||||
local n = p2._nodes[count]
|
||||
if n._x~=node._x or n._y~=node._y then return false end
|
||||
end
|
||||
return true
|
||||
end
|
||||
|
||||
--- Reverses a `path`.
|
||||
-- @class function
|
||||
-- @treturn path self (the calling `path` itself, can be chained)
|
||||
-- @usage myPath:reverse()
|
||||
function Path:reverse()
|
||||
local _nodes = {}
|
||||
for i = #self._nodes,1,-1 do
|
||||
_nodes[#_nodes+1] = self._nodes[i]
|
||||
end
|
||||
self._nodes = _nodes
|
||||
return self
|
||||
end
|
||||
|
||||
--- Appends a given `path` to self.
|
||||
-- @class function
|
||||
-- @tparam path p a path
|
||||
-- @treturn path self (the calling `path` itself, can be chained)
|
||||
-- @usage myPath:append(anotherPath)
|
||||
function Path:append(p)
|
||||
for node in p:nodes() do self:addNode(node) end
|
||||
return self
|
||||
end
|
||||
|
||||
return setmetatable(Path,
|
||||
{__call = function(self,...)
|
||||
return Path:new(...)
|
||||
end
|
||||
})
|
||||
end
|
||||
168
sys/apis/jumper/core/utils.lua
Normal file
168
sys/apis/jumper/core/utils.lua
Normal file
@@ -0,0 +1,168 @@
|
||||
-- Various utilities for Jumper top-level modules
|
||||
|
||||
if (...) then
|
||||
|
||||
-- Dependencies
|
||||
local _PATH = (...):gsub('%.utils$','')
|
||||
local Path = require (_PATH .. '.path')
|
||||
local Node = require (_PATH .. '.node')
|
||||
|
||||
-- Local references
|
||||
local pairs = pairs
|
||||
local type = type
|
||||
local t_insert = table.insert
|
||||
local assert = assert
|
||||
local coroutine = coroutine
|
||||
|
||||
-- Raw array items count
|
||||
local function arraySize(t)
|
||||
local count = 0
|
||||
for k,v in pairs(t) do
|
||||
count = count+1
|
||||
end
|
||||
return count
|
||||
end
|
||||
|
||||
-- Parses a string map and builds an array map
|
||||
local function stringMapToArray(str)
|
||||
local map = {}
|
||||
local w, h
|
||||
for line in str:gmatch('[^\n\r]+') do
|
||||
if line then
|
||||
w = not w and #line or w
|
||||
assert(#line == w, 'Error parsing map, rows must have the same size!')
|
||||
h = (h or 0) + 1
|
||||
map[h] = {}
|
||||
for char in line:gmatch('.') do
|
||||
map[h][#map[h]+1] = char
|
||||
end
|
||||
end
|
||||
end
|
||||
return map
|
||||
end
|
||||
|
||||
-- Collects and returns the keys of a given array
|
||||
local function getKeys(t)
|
||||
local keys = {}
|
||||
for k,v in pairs(t) do keys[#keys+1] = k end
|
||||
return keys
|
||||
end
|
||||
|
||||
-- Calculates the bounds of a 2d array
|
||||
local function getArrayBounds(map)
|
||||
local min_x, max_x
|
||||
local min_y, max_y
|
||||
for y in pairs(map) do
|
||||
min_y = not min_y and y or (y<min_y and y or min_y)
|
||||
max_y = not max_y and y or (y>max_y and y or max_y)
|
||||
for x in pairs(map[y]) do
|
||||
min_x = not min_x and x or (x<min_x and x or min_x)
|
||||
max_x = not max_x and x or (x>max_x and x or max_x)
|
||||
end
|
||||
end
|
||||
return min_x,max_x,min_y,max_y
|
||||
end
|
||||
|
||||
-- Converts an array to a set of nodes
|
||||
local function arrayToNodes(map)
|
||||
local min_x, max_x
|
||||
local min_y, max_y
|
||||
local min_z, max_z
|
||||
local nodes = {}
|
||||
for y in pairs(map) do
|
||||
min_y = not min_y and y or (y<min_y and y or min_y)
|
||||
max_y = not max_y and y or (y>max_y and y or max_y)
|
||||
nodes[y] = {}
|
||||
for x in pairs(map[y]) do
|
||||
min_x = not min_x and x or (x<min_x and x or min_x)
|
||||
max_x = not max_x and x or (x>max_x and x or max_x)
|
||||
nodes[y][x] = {}
|
||||
for z in pairs(map[y][x]) do
|
||||
min_z = not min_z and z or (z<min_z and z or min_z)
|
||||
max_z = not max_z and z or (z>max_z and z or max_z)
|
||||
nodes[y][x][z] = Node:new(x,y,z)
|
||||
end
|
||||
end
|
||||
end
|
||||
return nodes,
|
||||
(min_x or 0), (max_x or 0),
|
||||
(min_y or 0), (max_y or 0),
|
||||
(min_z or 0), (max_z or 0)
|
||||
end
|
||||
|
||||
-- Iterator, wrapped within a coroutine
|
||||
-- Iterates around a given position following the outline of a square
|
||||
local function around()
|
||||
local iterf = function(x0, y0, z0, s)
|
||||
local x, y, z = x0-s, y0-s, z0-s
|
||||
coroutine.yield(x, y, z)
|
||||
repeat
|
||||
x = x + 1
|
||||
coroutine.yield(x,y,z)
|
||||
until x == x0+s
|
||||
repeat
|
||||
y = y + 1
|
||||
coroutine.yield(x,y,z)
|
||||
until y == y0 + s
|
||||
repeat
|
||||
z = z + 1
|
||||
coroutine.yield(x,y,z)
|
||||
until z == z0 + s
|
||||
repeat
|
||||
x = x - 1
|
||||
coroutine.yield(x, y,z)
|
||||
until x == x0-s
|
||||
repeat
|
||||
y = y - 1
|
||||
coroutine.yield(x,y,z)
|
||||
until y == y0-s+1
|
||||
repeat
|
||||
z = z - 1
|
||||
coroutine.yield(x,y,z)
|
||||
until z == z0-s+1
|
||||
end
|
||||
return coroutine.create(iterf)
|
||||
end
|
||||
|
||||
-- Extract a path from a given start/end position
|
||||
local function traceBackPath(finder, node, startNode)
|
||||
local path = Path:new()
|
||||
path._grid = finder._grid
|
||||
while true do
|
||||
if node._parent then
|
||||
t_insert(path._nodes,1,node)
|
||||
node = node._parent
|
||||
else
|
||||
t_insert(path._nodes,1,startNode)
|
||||
return path
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
-- Lookup for value in a table
|
||||
local indexOf = function(t,v)
|
||||
for i = 1,#t do
|
||||
if t[i] == v then return i end
|
||||
end
|
||||
return nil
|
||||
end
|
||||
|
||||
-- Is i out of range
|
||||
local function outOfRange(i,low,up)
|
||||
return (i< low or i > up)
|
||||
end
|
||||
|
||||
return {
|
||||
arraySize = arraySize,
|
||||
getKeys = getKeys,
|
||||
indexOf = indexOf,
|
||||
outOfRange = outOfRange,
|
||||
getArrayBounds = getArrayBounds,
|
||||
arrayToNodes = arrayToNodes,
|
||||
strToMap = stringMapToArray,
|
||||
around = around,
|
||||
drAround = drAround,
|
||||
traceBackPath = traceBackPath
|
||||
}
|
||||
|
||||
end
|
||||
429
sys/apis/jumper/grid.lua
Normal file
429
sys/apis/jumper/grid.lua
Normal file
@@ -0,0 +1,429 @@
|
||||
--- The Grid class.
|
||||
-- Implementation of the `grid` class.
|
||||
-- The `grid` is a implicit graph which represents the 2D
|
||||
-- world map layout on which the `pathfinder` object will run.
|
||||
-- During a search, the `pathfinder` object needs to save some critical values. These values are cached within each `node`
|
||||
-- object, and the whole set of nodes are tight inside the `grid` object itself.
|
||||
|
||||
if (...) then
|
||||
|
||||
-- Dependencies
|
||||
local _PATH = (...):gsub('%.grid$','')
|
||||
|
||||
-- Local references
|
||||
local Utils = require (_PATH .. '.core.utils')
|
||||
local Assert = require (_PATH .. '.core.assert')
|
||||
local Node = require (_PATH .. '.core.node')
|
||||
|
||||
-- Local references
|
||||
local pairs = pairs
|
||||
local assert = assert
|
||||
local next = next
|
||||
local setmetatable = setmetatable
|
||||
local floor = math.floor
|
||||
local coroutine = coroutine
|
||||
|
||||
-- Offsets for straights moves
|
||||
local straightOffsets = {
|
||||
{x = 1, y = 0, z = 0} --[[W]], {x = -1, y = 0, z = 0}, --[[E]]
|
||||
{x = 0, y = 1, z = 0} --[[S]], {x = 0, y = -1, z = 0}, --[[N]]
|
||||
{x = 0, y = 0, z = 1} --[[U]], {x = 0, y = -0, z = -1}, --[[D]]
|
||||
}
|
||||
|
||||
-- Offsets for diagonal moves
|
||||
local diagonalOffsets = {
|
||||
{x = -1, y = -1} --[[NW]], {x = 1, y = -1}, --[[NE]]
|
||||
{x = -1, y = 1} --[[SW]], {x = 1, y = 1}, --[[SE]]
|
||||
}
|
||||
|
||||
--- The `Grid` class.<br/>
|
||||
-- This class is callable.
|
||||
-- Therefore,_ <code>Grid(...)</code> _acts as a shortcut to_ <code>Grid:new(...)</code>.
|
||||
-- @type Grid
|
||||
local Grid = {}
|
||||
Grid.__index = Grid
|
||||
|
||||
-- Specialized grids
|
||||
local PreProcessGrid = setmetatable({},Grid)
|
||||
local PostProcessGrid = setmetatable({},Grid)
|
||||
PreProcessGrid.__index = PreProcessGrid
|
||||
PostProcessGrid.__index = PostProcessGrid
|
||||
PreProcessGrid.__call = function (self,x,y,z)
|
||||
return self:getNodeAt(x,y,z)
|
||||
end
|
||||
PostProcessGrid.__call = function (self,x,y,z,create)
|
||||
if create then return self:getNodeAt(x,y,z) end
|
||||
return self._nodes[y] and self._nodes[y][x] and self._nodes[y][x][z]
|
||||
end
|
||||
|
||||
--- Inits a new `grid`
|
||||
-- @class function
|
||||
-- @tparam table|string map A collision map - (2D array) with consecutive indices (starting at 0 or 1)
|
||||
-- or a `string` with line-break chars (<code>\n</code> or <code>\r</code>) as row delimiters.
|
||||
-- @tparam[opt] bool cacheNodeAtRuntime When __true__, returns an empty `grid` instance, so that
|
||||
-- later on, indexing a non-cached `node` will cause it to be created and cache within the `grid` on purpose (i.e, when needed).
|
||||
-- This is a __memory-safe__ option, in case your dealing with some tight memory constraints.
|
||||
-- Defaults to __false__ when omitted.
|
||||
-- @treturn grid a new `grid` instance
|
||||
-- @usage
|
||||
-- -- A simple 3x3 grid
|
||||
-- local myGrid = Grid:new({{0,0,0},{0,0,0},{0,0,0}})
|
||||
--
|
||||
-- -- A memory-safe 3x3 grid
|
||||
-- myGrid = Grid('000\n000\n000', true)
|
||||
function Grid:new(map, cacheNodeAtRuntime)
|
||||
if type(map) == 'string' then
|
||||
assert(Assert.isStrMap(map), 'Wrong argument #1. Not a valid string map')
|
||||
map = Utils.strToMap(map)
|
||||
end
|
||||
--assert(Assert.isMap(map),('Bad argument #1. Not a valid map'))
|
||||
assert(Assert.isBool(cacheNodeAtRuntime) or Assert.isNil(cacheNodeAtRuntime),
|
||||
('Bad argument #2. Expected \'boolean\', got %s.'):format(type(cacheNodeAtRuntime)))
|
||||
if cacheNodeAtRuntime then
|
||||
return PostProcessGrid:new(map,walkable)
|
||||
end
|
||||
return PreProcessGrid:new(map,walkable)
|
||||
end
|
||||
|
||||
--- Checks if `node` at [x,y] is __walkable__.
|
||||
-- Will check if `node` at location [x,y] both *exists* on the collision map and *is walkable*
|
||||
-- @class function
|
||||
-- @tparam int x the x-location of the node
|
||||
-- @tparam int y the y-location of the node
|
||||
-- @tparam[opt] string|int|func walkable the value for walkable locations in the collision map array (see @{Grid:new}).
|
||||
-- Defaults to __false__ when omitted.
|
||||
-- If this parameter is a function, it should be prototyped as __f(value)__ and return a `boolean`:
|
||||
-- __true__ when value matches a __walkable__ `node`, __false__ otherwise. If this parameter is not given
|
||||
-- while location [x,y] __is valid__, this actual function returns __true__.
|
||||
-- @tparam[optchain] int clearance the amount of clearance needed. Defaults to 1 (normal clearance) when not given.
|
||||
-- @treturn bool __true__ if `node` exists and is __walkable__, __false__ otherwise
|
||||
-- @usage
|
||||
-- -- Always true
|
||||
-- print(myGrid:isWalkableAt(2,3))
|
||||
--
|
||||
-- -- True if node at [2,3] collision map value is 0
|
||||
-- print(myGrid:isWalkableAt(2,3,0))
|
||||
--
|
||||
-- -- True if node at [2,3] collision map value is 0 and has a clearance higher or equal to 2
|
||||
-- print(myGrid:isWalkableAt(2,3,0,2))
|
||||
--
|
||||
function Grid:isWalkableAt(x, y, z, walkable, clearance)
|
||||
local nodeValue = self._map[y] and self._map[y][x] and self._map[y][x][z]
|
||||
if nodeValue then
|
||||
if not walkable then return true end
|
||||
else
|
||||
return false
|
||||
end
|
||||
local hasEnoughClearance = not clearance and true or false
|
||||
if not hasEnoughClearance then
|
||||
if not self._isAnnotated[walkable] then return false end
|
||||
local node = self:getNodeAt(x,y,z)
|
||||
local nodeClearance = node:getClearance(walkable)
|
||||
hasEnoughClearance = (nodeClearance >= clearance)
|
||||
end
|
||||
if self._eval then
|
||||
return walkable(nodeValue) and hasEnoughClearance
|
||||
end
|
||||
return ((nodeValue == walkable) and hasEnoughClearance)
|
||||
end
|
||||
|
||||
--- Returns the `grid` width.
|
||||
-- @class function
|
||||
-- @treturn int the `grid` width
|
||||
-- @usage print(myGrid:getWidth())
|
||||
function Grid:getWidth()
|
||||
return self._width
|
||||
end
|
||||
|
||||
--- Returns the `grid` height.
|
||||
-- @class function
|
||||
-- @treturn int the `grid` height
|
||||
-- @usage print(myGrid:getHeight())
|
||||
function Grid:getHeight()
|
||||
return self._height
|
||||
end
|
||||
|
||||
--- Returns the collision map.
|
||||
-- @class function
|
||||
-- @treturn map the collision map (see @{Grid:new})
|
||||
-- @usage local map = myGrid:getMap()
|
||||
function Grid:getMap()
|
||||
return self._map
|
||||
end
|
||||
|
||||
--- Returns the set of nodes.
|
||||
-- @class function
|
||||
-- @treturn {{node,...},...} an array of nodes
|
||||
-- @usage local nodes = myGrid:getNodes()
|
||||
function Grid:getNodes()
|
||||
return self._nodes
|
||||
end
|
||||
|
||||
--- Returns the `grid` bounds. Returned values corresponds to the upper-left
|
||||
-- and lower-right coordinates (in tile units) of the actual `grid` instance.
|
||||
-- @class function
|
||||
-- @treturn int the upper-left corner x-coordinate
|
||||
-- @treturn int the upper-left corner y-coordinate
|
||||
-- @treturn int the lower-right corner x-coordinate
|
||||
-- @treturn int the lower-right corner y-coordinate
|
||||
-- @usage local left_x, left_y, right_x, right_y = myGrid:getBounds()
|
||||
function Grid:getBounds()
|
||||
return self._min_x, self._min_y, self._min_z, self._max_x, self._max_y, self._max_z
|
||||
end
|
||||
|
||||
--- Returns neighbours. The returned value is an array of __walkable__ nodes neighbouring a given `node`.
|
||||
-- @class function
|
||||
-- @tparam node node a given `node`
|
||||
-- @tparam[opt] string|int|func walkable the value for walkable locations in the collision map array (see @{Grid:new}).
|
||||
-- Defaults to __false__ when omitted.
|
||||
-- @tparam[optchain] bool allowDiagonal when __true__, allows adjacent nodes are included (8-neighbours).
|
||||
-- Defaults to __false__ when omitted.
|
||||
-- @tparam[optchain] bool tunnel When __true__, allows the `pathfinder` to tunnel through walls when heading diagonally.
|
||||
-- @tparam[optchain] int clearance When given, will prune for the neighbours set all nodes having a clearance value lower than the passed-in value
|
||||
-- Defaults to __false__ when omitted.
|
||||
-- @treturn {node,...} an array of nodes neighbouring a given node
|
||||
-- @usage
|
||||
-- local aNode = myGrid:getNodeAt(5,6)
|
||||
-- local neighbours = myGrid:getNeighbours(aNode, 0, true)
|
||||
function Grid:getNeighbours(node, walkable, allowDiagonal, tunnel, clearance)
|
||||
local neighbours = {}
|
||||
for i = 1,#straightOffsets do
|
||||
local n = self:getNodeAt(
|
||||
node._x + straightOffsets[i].x,
|
||||
node._y + straightOffsets[i].y,
|
||||
node._z + straightOffsets[i].z
|
||||
)
|
||||
if n and self:isWalkableAt(n._x, n._y, n._z, walkable, clearance) then
|
||||
neighbours[#neighbours+1] = n
|
||||
end
|
||||
end
|
||||
|
||||
if not allowDiagonal then return neighbours end
|
||||
|
||||
tunnel = not not tunnel
|
||||
for i = 1,#diagonalOffsets do
|
||||
local n = self:getNodeAt(
|
||||
node._x + diagonalOffsets[i].x,
|
||||
node._y + diagonalOffsets[i].y
|
||||
)
|
||||
if n and self:isWalkableAt(n._x, n._y, walkable, clearance) then
|
||||
if tunnel then
|
||||
neighbours[#neighbours+1] = n
|
||||
else
|
||||
local skipThisNode = false
|
||||
local n1 = self:getNodeAt(node._x+diagonalOffsets[i].x, node._y)
|
||||
local n2 = self:getNodeAt(node._x, node._y+diagonalOffsets[i].y)
|
||||
if ((n1 and n2) and not self:isWalkableAt(n1._x, n1._y, walkable, clearance) and not self:isWalkableAt(n2._x, n2._y, walkable, clearance)) then
|
||||
skipThisNode = true
|
||||
end
|
||||
if not skipThisNode then neighbours[#neighbours+1] = n end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
return neighbours
|
||||
end
|
||||
|
||||
--- Grid iterator. Iterates on every single node
|
||||
-- in the `grid`. Passing __lx, ly, ex, ey__ arguments will iterate
|
||||
-- only on nodes inside the bounding-rectangle delimited by those given coordinates.
|
||||
-- @class function
|
||||
-- @tparam[opt] int lx the leftmost x-coordinate of the rectangle. Default to the `grid` leftmost x-coordinate (see @{Grid:getBounds}).
|
||||
-- @tparam[optchain] int ly the topmost y-coordinate of the rectangle. Default to the `grid` topmost y-coordinate (see @{Grid:getBounds}).
|
||||
-- @tparam[optchain] int ex the rightmost x-coordinate of the rectangle. Default to the `grid` rightmost x-coordinate (see @{Grid:getBounds}).
|
||||
-- @tparam[optchain] int ey the bottom-most y-coordinate of the rectangle. Default to the `grid` bottom-most y-coordinate (see @{Grid:getBounds}).
|
||||
-- @treturn node a `node` on the collision map, upon each iteration step
|
||||
-- @treturn int the iteration count
|
||||
-- @usage
|
||||
-- for node, count in myGrid:iter() do
|
||||
-- print(node:getX(), node:getY(), count)
|
||||
-- end
|
||||
function Grid:iter(lx,ly,lz,ex,ey,ez)
|
||||
local min_x = lx or self._min_x
|
||||
local min_y = ly or self._min_y
|
||||
local min_z = lz or self._min_z
|
||||
local max_x = ex or self._max_x
|
||||
local max_y = ey or self._max_y
|
||||
local max_z = ez or self._max_z
|
||||
|
||||
local x, y, z
|
||||
z = min_z
|
||||
return function()
|
||||
x = not x and min_x or x+1
|
||||
if x > max_x then
|
||||
x = min_x
|
||||
y = y+1
|
||||
end
|
||||
y = not y and min_y or y+1
|
||||
if y > max_y then
|
||||
y = min_y
|
||||
z = z+1
|
||||
end
|
||||
if z > max_z then
|
||||
z = nil
|
||||
end
|
||||
return self._nodes[y] and self._nodes[y][x] and self._nodes[y][x][z] or self:getNodeAt(x,y,z)
|
||||
end
|
||||
end
|
||||
|
||||
--- Grid iterator. Iterates on each node along the outline (border) of a squared area
|
||||
-- centered on the given node.
|
||||
-- @tparam node node a given `node`
|
||||
-- @tparam[opt] int radius the area radius (half-length). Defaults to __1__ when not given.
|
||||
-- @treturn node a `node` at each iteration step
|
||||
-- @usage
|
||||
-- for node in myGrid:around(node, 2) do
|
||||
-- ...
|
||||
-- end
|
||||
function Grid:around(node, radius)
|
||||
local x, y, z = node._x, node._y, node._z
|
||||
radius = radius or 1
|
||||
local _around = Utils.around()
|
||||
local _nodes = {}
|
||||
repeat
|
||||
local state, x, y, z = coroutine.resume(_around,x,y,z,radius)
|
||||
local nodeAt = state and self:getNodeAt(x, y, z)
|
||||
if nodeAt then _nodes[#_nodes+1] = nodeAt end
|
||||
until (not state)
|
||||
local _i = 0
|
||||
return function()
|
||||
_i = _i+1
|
||||
return _nodes[_i]
|
||||
end
|
||||
end
|
||||
|
||||
--- Each transformation. Calls the given function on each `node` in the `grid`,
|
||||
-- passing the `node` as the first argument to function __f__.
|
||||
-- @class function
|
||||
-- @tparam func f a function prototyped as __f(node,...)__
|
||||
-- @tparam[opt] vararg ... args to be passed to function __f__
|
||||
-- @treturn grid self (the calling `grid` itself, can be chained)
|
||||
-- @usage
|
||||
-- local function printNode(node)
|
||||
-- print(node:getX(), node:getY())
|
||||
-- end
|
||||
-- myGrid:each(printNode)
|
||||
function Grid:each(f,...)
|
||||
for node in self:iter() do f(node,...) end
|
||||
return self
|
||||
end
|
||||
|
||||
--- Each (in range) transformation. Calls a function on each `node` in the range of a rectangle of cells,
|
||||
-- passing the `node` as the first argument to function __f__.
|
||||
-- @class function
|
||||
-- @tparam int lx the leftmost x-coordinate coordinate of the rectangle
|
||||
-- @tparam int ly the topmost y-coordinate of the rectangle
|
||||
-- @tparam int ex the rightmost x-coordinate of the rectangle
|
||||
-- @tparam int ey the bottom-most y-coordinate of the rectangle
|
||||
-- @tparam func f a function prototyped as __f(node,...)__
|
||||
-- @tparam[opt] vararg ... args to be passed to function __f__
|
||||
-- @treturn grid self (the calling `grid` itself, can be chained)
|
||||
-- @usage
|
||||
-- local function printNode(node)
|
||||
-- print(node:getX(), node:getY())
|
||||
-- end
|
||||
-- myGrid:eachRange(1,1,8,8,printNode)
|
||||
function Grid:eachRange(lx,ly,ex,ey,f,...)
|
||||
for node in self:iter(lx,ly,ex,ey) do f(node,...) end
|
||||
return self
|
||||
end
|
||||
|
||||
--- Map transformation.
|
||||
-- Calls function __f(node,...)__ on each `node` in a given range, passing the `node` as the first arg to function __f__ and replaces
|
||||
-- it with the returned value. Therefore, the function should return a `node`.
|
||||
-- @class function
|
||||
-- @tparam func f a function prototyped as __f(node,...)__
|
||||
-- @tparam[opt] vararg ... args to be passed to function __f__
|
||||
-- @treturn grid self (the calling `grid` itself, can be chained)
|
||||
-- @usage
|
||||
-- local function nothing(node)
|
||||
-- return node
|
||||
-- end
|
||||
-- myGrid:imap(nothing)
|
||||
function Grid:imap(f,...)
|
||||
for node in self:iter() do
|
||||
node = f(node,...)
|
||||
end
|
||||
return self
|
||||
end
|
||||
|
||||
--- Map in range transformation.
|
||||
-- Calls function __f(node,...)__ on each `node` in a rectangle range, passing the `node` as the first argument to the function and replaces
|
||||
-- it with the returned value. Therefore, the function should return a `node`.
|
||||
-- @class function
|
||||
-- @tparam int lx the leftmost x-coordinate coordinate of the rectangle
|
||||
-- @tparam int ly the topmost y-coordinate of the rectangle
|
||||
-- @tparam int ex the rightmost x-coordinate of the rectangle
|
||||
-- @tparam int ey the bottom-most y-coordinate of the rectangle
|
||||
-- @tparam func f a function prototyped as __f(node,...)__
|
||||
-- @tparam[opt] vararg ... args to be passed to function __f__
|
||||
-- @treturn grid self (the calling `grid` itself, can be chained)
|
||||
-- @usage
|
||||
-- local function nothing(node)
|
||||
-- return node
|
||||
-- end
|
||||
-- myGrid:imap(1,1,6,6,nothing)
|
||||
function Grid:imapRange(lx,ly,ex,ey,f,...)
|
||||
for node in self:iter(lx,ly,ex,ey) do
|
||||
node = f(node,...)
|
||||
end
|
||||
return self
|
||||
end
|
||||
|
||||
-- Specialized grids
|
||||
-- Inits a preprocessed grid
|
||||
function PreProcessGrid:new(map)
|
||||
local newGrid = {}
|
||||
newGrid._map = map
|
||||
newGrid._nodes, newGrid._min_x, newGrid._max_x, newGrid._min_y, newGrid._max_y, newGrid._min_z, newGrid._max_z = Utils.arrayToNodes(newGrid._map)
|
||||
newGrid._width = (newGrid._max_x-newGrid._min_x)+1
|
||||
newGrid._height = (newGrid._max_y-newGrid._min_y)+1
|
||||
newGrid._length = (newGrid._max_z-newGrid._min_z)+1
|
||||
newGrid._isAnnotated = {}
|
||||
return setmetatable(newGrid,PreProcessGrid)
|
||||
end
|
||||
|
||||
-- Inits a postprocessed grid
|
||||
function PostProcessGrid:new(map)
|
||||
local newGrid = {}
|
||||
newGrid._map = map
|
||||
newGrid._nodes = {}
|
||||
newGrid._min_x, newGrid._max_x, newGrid._min_y, newGrid._max_y = Utils.getArrayBounds(newGrid._map)
|
||||
newGrid._width = (newGrid._max_x-newGrid._min_x)+1
|
||||
newGrid._height = (newGrid._max_y-newGrid._min_y)+1
|
||||
newGrid._isAnnotated = {}
|
||||
return setmetatable(newGrid,PostProcessGrid)
|
||||
end
|
||||
|
||||
--- Returns the `node` at location [x,y].
|
||||
-- @class function
|
||||
-- @name Grid:getNodeAt
|
||||
-- @tparam int x the x-coordinate coordinate
|
||||
-- @tparam int y the y-coordinate coordinate
|
||||
-- @treturn node a `node`
|
||||
-- @usage local aNode = myGrid:getNodeAt(2,2)
|
||||
|
||||
-- Gets the node at location <x,y> on a preprocessed grid
|
||||
function PreProcessGrid:getNodeAt(x,y,z)
|
||||
return self._nodes[y] and self._nodes[y][x] and self._nodes[y][x][z] or nil
|
||||
end
|
||||
|
||||
-- Gets the node at location <x,y> on a postprocessed grid
|
||||
function PostProcessGrid:getNodeAt(x,y,z)
|
||||
if not x or not y or not z then return end
|
||||
if Utils.outOfRange(x,self._min_x,self._max_x) then return end
|
||||
if Utils.outOfRange(y,self._min_y,self._max_y) then return end
|
||||
if Utils.outOfRange(z,self._min_z,self._max_z) then return end
|
||||
if not self._nodes[y] then self._nodes[y] = {} end
|
||||
if not self._nodes[y][x] then self._nodes[y][x] = {} end
|
||||
if not self._nodes[y][x][z] then self._nodes[y][x][z] = Node:new(x,y,z) end
|
||||
return self._nodes[y][x][z]
|
||||
end
|
||||
|
||||
return setmetatable(Grid,{
|
||||
__call = function(self,...)
|
||||
return self:new(...)
|
||||
end
|
||||
})
|
||||
|
||||
end
|
||||
412
sys/apis/jumper/pathfinder.lua
Normal file
412
sys/apis/jumper/pathfinder.lua
Normal file
@@ -0,0 +1,412 @@
|
||||
--[[
|
||||
The following License applies to all files within the jumper directory.
|
||||
|
||||
Note that this is only a partial copy of the full jumper code base. Also,
|
||||
the code was modified to support 3D maps.
|
||||
--]]
|
||||
|
||||
--[[
|
||||
This work is under MIT-LICENSE
|
||||
Copyright (c) 2012-2013 Roland Yonaba.
|
||||
|
||||
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.
|
||||
--]]
|
||||
|
||||
--- The Pathfinder class
|
||||
|
||||
--
|
||||
-- Implementation of the `pathfinder` class.
|
||||
|
||||
local _VERSION = ""
|
||||
local _RELEASEDATE = ""
|
||||
|
||||
if (...) then
|
||||
|
||||
-- Dependencies
|
||||
local _PATH = (...):gsub('%.pathfinder$','')
|
||||
local Utils = require (_PATH .. '.core.utils')
|
||||
local Assert = require (_PATH .. '.core.assert')
|
||||
local Heap = require (_PATH .. '.core.bheap')
|
||||
local Heuristic = require (_PATH .. '.core.heuristics')
|
||||
local Grid = require (_PATH .. '.grid')
|
||||
local Path = require (_PATH .. '.core.path')
|
||||
|
||||
-- Internalization
|
||||
local t_insert, t_remove = table.insert, table.remove
|
||||
local floor = math.floor
|
||||
local pairs = pairs
|
||||
local assert = assert
|
||||
local type = type
|
||||
local setmetatable, getmetatable = setmetatable, getmetatable
|
||||
|
||||
--- Finders (search algorithms implemented). Refers to the search algorithms actually implemented in Jumper.
|
||||
--
|
||||
-- <li>[A*](http://en.wikipedia.org/wiki/A*_search_algorithm)</li>
|
||||
-- <li>[Dijkstra](http://en.wikipedia.org/wiki/Dijkstra%27s_algorithm)</li>
|
||||
-- <li>[Theta Astar](http://aigamedev.com/open/tutorials/theta-star-any-angle-paths/)</li>
|
||||
-- <li>[BFS](http://en.wikipedia.org/wiki/Breadth-first_search)</li>
|
||||
-- <li>[DFS](http://en.wikipedia.org/wiki/Depth-first_search)</li>
|
||||
-- <li>[JPS](http://harablog.wordpress.com/2011/09/07/jump-point-search/)</li>
|
||||
-- @finder Finders
|
||||
-- @see Pathfinder:getFinders
|
||||
local Finders = {
|
||||
['ASTAR'] = require (_PATH .. '.search.astar'),
|
||||
-- ['DIJKSTRA'] = require (_PATH .. '.search.dijkstra'),
|
||||
-- ['THETASTAR'] = require (_PATH .. '.search.thetastar'),
|
||||
['BFS'] = require (_PATH .. '.search.bfs'),
|
||||
-- ['DFS'] = require (_PATH .. '.search.dfs'),
|
||||
-- ['JPS'] = require (_PATH .. '.search.jps')
|
||||
}
|
||||
|
||||
-- Will keep track of all nodes expanded during the search
|
||||
-- to easily reset their properties for the next pathfinding call
|
||||
local toClear = {}
|
||||
|
||||
--- Search modes. Refers to the search modes. In ORTHOGONAL mode, 4-directions are only possible when moving,
|
||||
-- including North, East, West, South. In DIAGONAL mode, 8-directions are possible when moving,
|
||||
-- including North, East, West, South and adjacent directions.
|
||||
--
|
||||
-- <li>ORTHOGONAL</li>
|
||||
-- <li>DIAGONAL</li>
|
||||
-- @mode Modes
|
||||
-- @see Pathfinder:getModes
|
||||
local searchModes = {['DIAGONAL'] = true, ['ORTHOGONAL'] = true}
|
||||
|
||||
-- Performs a traceback from the goal node to the start node
|
||||
-- Only happens when the path was found
|
||||
|
||||
--- The `Pathfinder` class.<br/>
|
||||
-- This class is callable.
|
||||
-- Therefore,_ <code>Pathfinder(...)</code> _acts as a shortcut to_ <code>Pathfinder:new(...)</code>.
|
||||
-- @type Pathfinder
|
||||
local Pathfinder = {}
|
||||
Pathfinder.__index = Pathfinder
|
||||
|
||||
--- Inits a new `pathfinder`
|
||||
-- @class function
|
||||
-- @tparam grid grid a `grid`
|
||||
-- @tparam[opt] string finderName the name of the `Finder` (search algorithm) to be used for search.
|
||||
-- Defaults to `ASTAR` when not given (see @{Pathfinder:getFinders}).
|
||||
-- @tparam[optchain] string|int|func walkable the value for __walkable__ nodes.
|
||||
-- If this parameter is a function, it should be prototyped as __f(value)__, returning a boolean:
|
||||
-- __true__ when value matches a __walkable__ `node`, __false__ otherwise.
|
||||
-- @treturn pathfinder a new `pathfinder` instance
|
||||
-- @usage
|
||||
-- -- Example one
|
||||
-- local finder = Pathfinder:new(myGrid, 'ASTAR', 0)
|
||||
--
|
||||
-- -- Example two
|
||||
-- local function walkable(value)
|
||||
-- return value > 0
|
||||
-- end
|
||||
-- local finder = Pathfinder(myGrid, 'JPS', walkable)
|
||||
function Pathfinder:new(grid, finderName, walkable)
|
||||
local newPathfinder = {}
|
||||
setmetatable(newPathfinder, Pathfinder)
|
||||
--newPathfinder:setGrid(grid)
|
||||
newPathfinder:setFinder(finderName)
|
||||
--newPathfinder:setWalkable(walkable)
|
||||
newPathfinder:setMode('DIAGONAL')
|
||||
newPathfinder:setHeuristic('MANHATTAN')
|
||||
newPathfinder:setTunnelling(false)
|
||||
return newPathfinder
|
||||
end
|
||||
|
||||
--- Evaluates [clearance](http://aigamedev.com/open/tutorial/clearance-based-pathfinding/#TheTrueClearanceMetric)
|
||||
-- for the whole `grid`. It should be called only once, unless the collision map or the
|
||||
-- __walkable__ attribute changes. The clearance values are calculated and cached within the grid nodes.
|
||||
-- @class function
|
||||
-- @treturn pathfinder self (the calling `pathfinder` itself, can be chained)
|
||||
-- @usage myFinder:annotateGrid()
|
||||
function Pathfinder:annotateGrid()
|
||||
assert(self._walkable, 'Finder must implement a walkable value')
|
||||
for x=self._grid._max_x,self._grid._min_x,-1 do
|
||||
for y=self._grid._max_y,self._grid._min_y,-1 do
|
||||
local node = self._grid:getNodeAt(x,y)
|
||||
if self._grid:isWalkableAt(x,y,self._walkable) then
|
||||
local nr = self._grid:getNodeAt(node._x+1, node._y)
|
||||
local nrd = self._grid:getNodeAt(node._x+1, node._y+1)
|
||||
local nd = self._grid:getNodeAt(node._x, node._y+1)
|
||||
if nr and nrd and nd then
|
||||
local m = nrd._clearance[self._walkable] or 0
|
||||
m = (nd._clearance[self._walkable] or 0)<m and (nd._clearance[self._walkable] or 0) or m
|
||||
m = (nr._clearance[self._walkable] or 0)<m and (nr._clearance[self._walkable] or 0) or m
|
||||
node._clearance[self._walkable] = m+1
|
||||
else
|
||||
node._clearance[self._walkable] = 1
|
||||
end
|
||||
else node._clearance[self._walkable] = 0
|
||||
end
|
||||
end
|
||||
end
|
||||
self._grid._isAnnotated[self._walkable] = true
|
||||
return self
|
||||
end
|
||||
|
||||
--- Removes [clearance](http://aigamedev.com/open/tutorial/clearance-based-pathfinding/#TheTrueClearanceMetric)values.
|
||||
-- Clears cached clearance values for the current __walkable__.
|
||||
-- @class function
|
||||
-- @treturn pathfinder self (the calling `pathfinder` itself, can be chained)
|
||||
-- @usage myFinder:clearAnnotations()
|
||||
function Pathfinder:clearAnnotations()
|
||||
assert(self._walkable, 'Finder must implement a walkable value')
|
||||
for node in self._grid:iter() do
|
||||
node:removeClearance(self._walkable)
|
||||
end
|
||||
self._grid._isAnnotated[self._walkable] = false
|
||||
return self
|
||||
end
|
||||
|
||||
--- Sets the `grid`. Defines the given `grid` as the one on which the `pathfinder` will perform the search.
|
||||
-- @class function
|
||||
-- @tparam grid grid a `grid`
|
||||
-- @treturn pathfinder self (the calling `pathfinder` itself, can be chained)
|
||||
-- @usage myFinder:setGrid(myGrid)
|
||||
function Pathfinder:setGrid(grid)
|
||||
assert(Assert.inherits(grid, Grid), 'Wrong argument #1. Expected a \'grid\' object')
|
||||
self._grid = grid
|
||||
self._grid._eval = self._walkable and type(self._walkable) == 'function'
|
||||
return self
|
||||
end
|
||||
|
||||
--- Returns the `grid`. This is a reference to the actual `grid` used by the `pathfinder`.
|
||||
-- @class function
|
||||
-- @treturn grid the `grid`
|
||||
-- @usage local myGrid = myFinder:getGrid()
|
||||
function Pathfinder:getGrid()
|
||||
return self._grid
|
||||
end
|
||||
|
||||
--- Sets the __walkable__ value or function.
|
||||
-- @class function
|
||||
-- @tparam string|int|func walkable the value for walkable nodes.
|
||||
-- @treturn pathfinder self (the calling `pathfinder` itself, can be chained)
|
||||
-- @usage
|
||||
-- -- Value '0' is walkable
|
||||
-- myFinder:setWalkable(0)
|
||||
--
|
||||
-- -- Any value greater than 0 is walkable
|
||||
-- myFinder:setWalkable(function(n)
|
||||
-- return n>0
|
||||
-- end
|
||||
function Pathfinder:setWalkable(walkable)
|
||||
assert(Assert.matchType(walkable,'stringintfunctionnil'),
|
||||
('Wrong argument #1. Expected \'string\', \'number\' or \'function\', got %s.'):format(type(walkable)))
|
||||
self._walkable = walkable
|
||||
self._grid._eval = type(self._walkable) == 'function'
|
||||
return self
|
||||
end
|
||||
|
||||
--- Gets the __walkable__ value or function.
|
||||
-- @class function
|
||||
-- @treturn string|int|func the `walkable` value or function
|
||||
-- @usage local walkable = myFinder:getWalkable()
|
||||
function Pathfinder:getWalkable()
|
||||
return self._walkable
|
||||
end
|
||||
|
||||
--- Defines the `finder`. It refers to the search algorithm used by the `pathfinder`.
|
||||
-- Default finder is `ASTAR`. Use @{Pathfinder:getFinders} to get the list of available finders.
|
||||
-- @class function
|
||||
-- @tparam string finderName the name of the `finder` to be used for further searches.
|
||||
-- @treturn pathfinder self (the calling `pathfinder` itself, can be chained)
|
||||
-- @usage
|
||||
-- --To use Breadth-First-Search
|
||||
-- myFinder:setFinder('BFS')
|
||||
-- @see Pathfinder:getFinders
|
||||
function Pathfinder:setFinder(finderName)
|
||||
if not finderName then
|
||||
if not self._finder then
|
||||
finderName = 'ASTAR'
|
||||
else return
|
||||
end
|
||||
end
|
||||
assert(Finders[finderName],'Not a valid finder name!')
|
||||
self._finder = finderName
|
||||
return self
|
||||
end
|
||||
|
||||
--- Returns the name of the `finder` being used.
|
||||
-- @class function
|
||||
-- @treturn string the name of the `finder` to be used for further searches.
|
||||
-- @usage local finderName = myFinder:getFinder()
|
||||
function Pathfinder:getFinder()
|
||||
return self._finder
|
||||
end
|
||||
|
||||
--- Returns the list of all available finders names.
|
||||
-- @class function
|
||||
-- @treturn {string,...} array of built-in finders names.
|
||||
-- @usage
|
||||
-- local finders = myFinder:getFinders()
|
||||
-- for i, finderName in ipairs(finders) do
|
||||
-- print(i, finderName)
|
||||
-- end
|
||||
function Pathfinder:getFinders()
|
||||
return Utils.getKeys(Finders)
|
||||
end
|
||||
|
||||
--- Sets a heuristic. This is a function internally used by the `pathfinder` to find the optimal path during a search.
|
||||
-- Use @{Pathfinder:getHeuristics} to get the list of all available `heuristics`. One can also define
|
||||
-- his own `heuristic` function.
|
||||
-- @class function
|
||||
-- @tparam func|string heuristic `heuristic` function, prototyped as __f(dx,dy)__ or as a `string`.
|
||||
-- @treturn pathfinder self (the calling `pathfinder` itself, can be chained)
|
||||
-- @see Pathfinder:getHeuristics
|
||||
-- @see core.heuristics
|
||||
-- @usage myFinder:setHeuristic('MANHATTAN')
|
||||
function Pathfinder:setHeuristic(heuristic)
|
||||
assert(Heuristic[heuristic] or (type(heuristic) == 'function'),'Not a valid heuristic!')
|
||||
self._heuristic = Heuristic[heuristic] or heuristic
|
||||
return self
|
||||
end
|
||||
|
||||
--- Returns the `heuristic` used. Returns the function itself.
|
||||
-- @class function
|
||||
-- @treturn func the `heuristic` function being used by the `pathfinder`
|
||||
-- @see core.heuristics
|
||||
-- @usage local h = myFinder:getHeuristic()
|
||||
function Pathfinder:getHeuristic()
|
||||
return self._heuristic
|
||||
end
|
||||
|
||||
--- Gets the list of all available `heuristics`.
|
||||
-- @class function
|
||||
-- @treturn {string,...} array of heuristic names.
|
||||
-- @see core.heuristics
|
||||
-- @usage
|
||||
-- local heur = myFinder:getHeuristic()
|
||||
-- for i, heuristicName in ipairs(heur) do
|
||||
-- ...
|
||||
-- end
|
||||
function Pathfinder:getHeuristics()
|
||||
return Utils.getKeys(Heuristic)
|
||||
end
|
||||
|
||||
--- Defines the search `mode`.
|
||||
-- The default search mode is the `DIAGONAL` mode, which implies 8-possible directions when moving (north, south, east, west and diagonals).
|
||||
-- In `ORTHOGONAL` mode, only 4-directions are allowed (north, south, east and west).
|
||||
-- Use @{Pathfinder:getModes} to get the list of all available search modes.
|
||||
-- @class function
|
||||
-- @tparam string mode the new search `mode`.
|
||||
-- @treturn pathfinder self (the calling `pathfinder` itself, can be chained)
|
||||
-- @see Pathfinder:getModes
|
||||
-- @see Modes
|
||||
-- @usage myFinder:setMode('ORTHOGONAL')
|
||||
function Pathfinder:setMode(mode)
|
||||
assert(searchModes[mode],'Invalid mode')
|
||||
self._allowDiagonal = (mode == 'DIAGONAL')
|
||||
return self
|
||||
end
|
||||
|
||||
--- Returns the search mode.
|
||||
-- @class function
|
||||
-- @treturn string the current search mode
|
||||
-- @see Modes
|
||||
-- @usage local mode = myFinder:getMode()
|
||||
function Pathfinder:getMode()
|
||||
return (self._allowDiagonal and 'DIAGONAL' or 'ORTHOGONAL')
|
||||
end
|
||||
|
||||
--- Gets the list of all available search modes.
|
||||
-- @class function
|
||||
-- @treturn {string,...} array of search modes.
|
||||
-- @see Modes
|
||||
-- @usage local modes = myFinder:getModes()
|
||||
-- for modeName in ipairs(modes) do
|
||||
-- ...
|
||||
-- end
|
||||
function Pathfinder:getModes()
|
||||
return Utils.getKeys(searchModes)
|
||||
end
|
||||
|
||||
--- Enables tunnelling. Defines the ability for the `pathfinder` to tunnel through walls when heading diagonally.
|
||||
-- This feature __is not compatible__ with Jump Point Search algorithm (i.e. enabling it will not affect Jump Point Search)
|
||||
-- @class function
|
||||
-- @tparam bool bool a boolean
|
||||
-- @treturn pathfinder self (the calling `pathfinder` itself, can be chained)
|
||||
-- @usage myFinder:setTunnelling(true)
|
||||
function Pathfinder:setTunnelling(bool)
|
||||
assert(Assert.isBool(bool), ('Wrong argument #1. Expected boolean, got %s'):format(type(bool)))
|
||||
self._tunnel = bool
|
||||
return self
|
||||
end
|
||||
|
||||
--- Returns tunnelling feature state.
|
||||
-- @class function
|
||||
-- @treturn bool tunnelling feature actual state
|
||||
-- @usage local isTunnellingEnabled = myFinder:getTunnelling()
|
||||
function Pathfinder:getTunnelling()
|
||||
return self._tunnel
|
||||
end
|
||||
|
||||
--- Calculates a `path`. Returns the `path` from location __[startX, startY]__ to location __[endX, endY]__.
|
||||
-- Both locations must exist on the collision map. The starting location can be unwalkable.
|
||||
-- @class function
|
||||
-- @tparam int startX the x-coordinate for the starting location
|
||||
-- @tparam int startY the y-coordinate for the starting location
|
||||
-- @tparam int endX the x-coordinate for the goal location
|
||||
-- @tparam int endY the y-coordinate for the goal location
|
||||
-- @tparam int clearance the amount of clearance (i.e the pathing agent size) to consider
|
||||
-- @treturn path a path (array of nodes) when found, otherwise nil
|
||||
-- @usage local path = myFinder:getPath(1,1,5,5)
|
||||
function Pathfinder:getPath(startX, startY, startZ, ih, endX, endY, endZ, oh, clearance)
|
||||
|
||||
self:reset()
|
||||
local startNode = self._grid:getNodeAt(startX, startY, startZ)
|
||||
local endNode = self._grid:getNodeAt(endX, endY, endZ)
|
||||
if not startNode or not endNode then
|
||||
return nil
|
||||
end
|
||||
|
||||
startNode._heading = ih
|
||||
endNode._heading = oh
|
||||
|
||||
assert(startNode, ('Invalid location [%d, %d, %d]'):format(startX, startY, startZ))
|
||||
assert(endNode and self._grid:isWalkableAt(endX, endY, endZ),
|
||||
('Invalid or unreachable location [%d, %d, %d]'):format(endX, endY, endZ))
|
||||
local _endNode = Finders[self._finder](self, startNode, endNode, clearance, toClear)
|
||||
if _endNode then
|
||||
return Utils.traceBackPath(self, _endNode, startNode)
|
||||
end
|
||||
return nil
|
||||
end
|
||||
|
||||
--- Resets the `pathfinder`. This function is called internally between successive pathfinding calls, so you should not
|
||||
-- use it explicitely, unless under specific circumstances.
|
||||
-- @class function
|
||||
-- @treturn pathfinder self (the calling `pathfinder` itself, can be chained)
|
||||
-- @usage local path, len = myFinder:getPath(1,1,5,5)
|
||||
function Pathfinder:reset()
|
||||
for node in pairs(toClear) do node:reset() end
|
||||
toClear = {}
|
||||
return self
|
||||
end
|
||||
|
||||
|
||||
-- Returns Pathfinder class
|
||||
Pathfinder._VERSION = _VERSION
|
||||
Pathfinder._RELEASEDATE = _RELEASEDATE
|
||||
return setmetatable(Pathfinder,{
|
||||
__call = function(self,...)
|
||||
return self:new(...)
|
||||
end
|
||||
})
|
||||
|
||||
end
|
||||
88
sys/apis/jumper/search/astar.lua
Normal file
88
sys/apis/jumper/search/astar.lua
Normal file
@@ -0,0 +1,88 @@
|
||||
-- Astar algorithm
|
||||
-- This actual implementation of A-star is based on
|
||||
-- [Nash A. & al. pseudocode](http://aigamedev.com/open/tutorials/theta-star-any-angle-paths/)
|
||||
|
||||
if (...) then
|
||||
|
||||
-- Internalization
|
||||
local ipairs = ipairs
|
||||
local huge = math.huge
|
||||
|
||||
-- Dependancies
|
||||
local _PATH = (...):match('(.+)%.search.astar$')
|
||||
local Heuristics = require (_PATH .. '.core.heuristics')
|
||||
local Heap = require (_PATH.. '.core.bheap')
|
||||
|
||||
-- Updates G-cost
|
||||
local function computeCost(node, neighbour, finder, clearance, heuristic)
|
||||
local mCost, heading = heuristic(neighbour, node) -- Heuristics.EUCLIDIAN(neighbour, node)
|
||||
|
||||
if node._g + mCost < neighbour._g then
|
||||
neighbour._parent = node
|
||||
neighbour._g = node._g + mCost
|
||||
neighbour._heading = heading
|
||||
end
|
||||
end
|
||||
|
||||
-- Updates vertex node-neighbour
|
||||
local function updateVertex(finder, openList, node, neighbour, endNode, clearance, heuristic, overrideCostEval)
|
||||
local oldG = neighbour._g
|
||||
local cmpCost = overrideCostEval or computeCost
|
||||
cmpCost(node, neighbour, finder, clearance, heuristic)
|
||||
if neighbour._g < oldG then
|
||||
local nClearance = neighbour._clearance[finder._walkable]
|
||||
local pushThisNode = clearance and nClearance and (nClearance >= clearance)
|
||||
if (clearance and pushThisNode) or (not clearance) then
|
||||
if neighbour._opened then neighbour._opened = false end
|
||||
neighbour._h = heuristic(endNode, neighbour)
|
||||
neighbour._f = neighbour._g + neighbour._h
|
||||
openList:push(neighbour)
|
||||
neighbour._opened = true
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
-- Calculates a path.
|
||||
-- Returns the path from location `<startX, startY>` to location `<endX, endY>`.
|
||||
return function (finder, startNode, endNode, clearance, toClear, overrideHeuristic, overrideCostEval)
|
||||
|
||||
local heuristic = overrideHeuristic or finder._heuristic
|
||||
local openList = Heap()
|
||||
startNode._g = 0
|
||||
startNode._h = heuristic(endNode, startNode)
|
||||
startNode._f = startNode._g + startNode._h
|
||||
openList:push(startNode)
|
||||
toClear[startNode] = true
|
||||
startNode._opened = true
|
||||
|
||||
while not openList:empty() do
|
||||
local node = openList:pop()
|
||||
node._closed = true
|
||||
if node == endNode then return node end
|
||||
local neighbours = finder._grid:getNeighbours(node, finder._walkable, finder._allowDiagonal, finder._tunnel)
|
||||
for i = 1,#neighbours do
|
||||
local neighbour = neighbours[i]
|
||||
if not neighbour._closed then
|
||||
toClear[neighbour] = true
|
||||
if not neighbour._opened then
|
||||
neighbour._g = huge
|
||||
neighbour._parent = nil
|
||||
end
|
||||
updateVertex(finder, openList, node, neighbour, endNode, clearance, heuristic, overrideCostEval)
|
||||
end
|
||||
end
|
||||
|
||||
--[[
|
||||
printf('x:%d y:%d z:%d g:%d', node._x, node._y, node._z, node._g)
|
||||
for i = 1,#neighbours do
|
||||
local n = neighbours[i]
|
||||
printf('x:%d y:%d z:%d f:%f g:%f h:%d', n._x, n._y, n._z, n._f, n._g, n._heading or -1)
|
||||
end
|
||||
--]]
|
||||
|
||||
end
|
||||
|
||||
return nil
|
||||
end
|
||||
|
||||
end
|
||||
46
sys/apis/jumper/search/bfs.lua
Normal file
46
sys/apis/jumper/search/bfs.lua
Normal file
@@ -0,0 +1,46 @@
|
||||
-- Breadth-First search algorithm
|
||||
|
||||
if (...) then
|
||||
-- Internalization
|
||||
local t_remove = table.remove
|
||||
|
||||
local function breadth_first_search(finder, openList, node, endNode, clearance, toClear)
|
||||
local neighbours = finder._grid:getNeighbours(node, finder._walkable, finder._allowDiagonal, finder._tunnel)
|
||||
for i = 1,#neighbours do
|
||||
local neighbour = neighbours[i]
|
||||
if not neighbour._closed and not neighbour._opened then
|
||||
local nClearance = neighbour._clearance[finder._walkable]
|
||||
local pushThisNode = clearance and nClearance and (nClearance >= clearance)
|
||||
if (clearance and pushThisNode) or (not clearance) then
|
||||
openList[#openList+1] = neighbour
|
||||
neighbour._opened = true
|
||||
neighbour._parent = node
|
||||
toClear[neighbour] = true
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
-- Calculates a path.
|
||||
-- Returns the path from location `<startX, startY>` to location `<endX, endY>`.
|
||||
return function (finder, startNode, endNode, clearance, toClear)
|
||||
|
||||
local openList = {} -- We'll use a FIFO queue (simple array)
|
||||
openList[1] = startNode
|
||||
startNode._opened = true
|
||||
toClear[startNode] = true
|
||||
|
||||
local node
|
||||
while (#openList > 0) do
|
||||
node = openList[1]
|
||||
t_remove(openList,1)
|
||||
node._closed = true
|
||||
if node == endNode then return node end
|
||||
breadth_first_search(finder, openList, node, endNode, clearance, toClear)
|
||||
end
|
||||
|
||||
return nil
|
||||
end
|
||||
|
||||
end
|
||||
133
sys/apis/logger.lua
Normal file
133
sys/apis/logger.lua
Normal file
@@ -0,0 +1,133 @@
|
||||
local Logger = {
|
||||
fn = function() end,
|
||||
filteredEvents = { },
|
||||
}
|
||||
|
||||
function Logger.setLogger(fn)
|
||||
Logger.fn = fn
|
||||
end
|
||||
|
||||
function Logger.disable()
|
||||
Logger.setLogger(function() end)
|
||||
end
|
||||
|
||||
function Logger.setDaemonLogging()
|
||||
Logger.setLogger(function (text)
|
||||
os.queueEvent('log', { text = text })
|
||||
end)
|
||||
end
|
||||
|
||||
function Logger.setMonitorLogging()
|
||||
local debugMon = device.monitor
|
||||
|
||||
if not debugMon then
|
||||
debugMon.setTextScale(.5)
|
||||
debugMon.clear()
|
||||
debugMon.setCursorPos(1, 1)
|
||||
Logger.setLogger(function(text)
|
||||
debugMon.write(text)
|
||||
debugMon.scroll(-1)
|
||||
debugMon.setCursorPos(1, 1)
|
||||
end)
|
||||
end
|
||||
end
|
||||
|
||||
function Logger.setScreenLogging()
|
||||
Logger.setLogger(function(text)
|
||||
local x, y = term.getCursorPos()
|
||||
if x ~= 1 then
|
||||
local sx, sy = term.getSize()
|
||||
term.setCursorPos(1, sy)
|
||||
--term.scroll(1)
|
||||
end
|
||||
print(text)
|
||||
end)
|
||||
end
|
||||
|
||||
function Logger.setWirelessLogging()
|
||||
if device.wireless_modem then
|
||||
Logger.filter('modem_message')
|
||||
Logger.filter('modem_receive')
|
||||
Logger.filter('rednet_message')
|
||||
Logger.setLogger(function(text)
|
||||
device.wireless_modem.transmit(59998, os.getComputerID(), {
|
||||
type = 'log', contents = text
|
||||
})
|
||||
end)
|
||||
Logger.debug('Logging enabled')
|
||||
return true
|
||||
end
|
||||
end
|
||||
|
||||
function Logger.setFileLogging(fileName)
|
||||
fs.delete(fileName)
|
||||
Logger.setLogger(function (text)
|
||||
local logFile
|
||||
|
||||
local mode = 'w'
|
||||
if fs.exists(fileName) then
|
||||
mode = 'a'
|
||||
end
|
||||
local file = io.open(fileName, mode)
|
||||
if file then
|
||||
file:write(text)
|
||||
file:write('\n')
|
||||
file:close()
|
||||
end
|
||||
end)
|
||||
end
|
||||
|
||||
function Logger.log(category, value, ...)
|
||||
if Logger.filteredEvents[category] then
|
||||
return
|
||||
end
|
||||
|
||||
if type(value) == 'table' then
|
||||
local str
|
||||
for k,v in pairs(value) do
|
||||
if not str then
|
||||
str = '{ '
|
||||
else
|
||||
str = str .. ', '
|
||||
end
|
||||
str = str .. k .. '=' .. tostring(v)
|
||||
end
|
||||
if str then
|
||||
value = str .. ' }'
|
||||
else
|
||||
value = '{ }'
|
||||
end
|
||||
elseif type(value) == 'string' then
|
||||
local args = { ... }
|
||||
if #args > 0 then
|
||||
value = string.format(value, unpack(args))
|
||||
end
|
||||
else
|
||||
value = tostring(value)
|
||||
end
|
||||
Logger.fn(category .. ': ' .. value)
|
||||
end
|
||||
|
||||
function Logger.debug(value, ...)
|
||||
Logger.log('debug', value, ...)
|
||||
end
|
||||
|
||||
function Logger.logNestedTable(t, indent)
|
||||
for _,v in ipairs(t) do
|
||||
if type(v) == 'table' then
|
||||
log('table')
|
||||
logNestedTable(v) --, indent+1)
|
||||
else
|
||||
log(v)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function Logger.filter( ...)
|
||||
local events = { ... }
|
||||
for _,event in pairs(events) do
|
||||
Logger.filteredEvents[event] = true
|
||||
end
|
||||
end
|
||||
|
||||
return Logger
|
||||
165
sys/apis/me.lua
Normal file
165
sys/apis/me.lua
Normal file
@@ -0,0 +1,165 @@
|
||||
local ME = {
|
||||
jobList = { }
|
||||
}
|
||||
|
||||
function ME.setDevice(device)
|
||||
ME.p = device
|
||||
--Util.merge(ME, ME.p)
|
||||
|
||||
if not device then
|
||||
error('ME device not attached')
|
||||
end
|
||||
|
||||
for k,v in pairs(ME.p) do
|
||||
if not ME[k] then
|
||||
ME[k] = v
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function ME.isAvailable()
|
||||
return not Util.empty(ME.getAvailableItems())
|
||||
end
|
||||
|
||||
-- Strip off color prefix
|
||||
local function safeString(text)
|
||||
|
||||
local val = text:byte(1)
|
||||
|
||||
if val < 32 or val > 128 then
|
||||
|
||||
local newText = {}
|
||||
for i = 4, #text do
|
||||
local val = text:byte(i)
|
||||
newText[i - 3] = (val > 31 and val < 127) and val or 63
|
||||
end
|
||||
return string.char(unpack(newText))
|
||||
end
|
||||
|
||||
return text
|
||||
end
|
||||
|
||||
function ME.getAvailableItems()
|
||||
local items
|
||||
pcall(function()
|
||||
items = ME.p.getAvailableItems('all')
|
||||
for k,v in pairs(items) do
|
||||
v.id = v.item.id
|
||||
v.name = safeString(v.item.display_name)
|
||||
v.qty = v.item.qty
|
||||
v.dmg = v.item.dmg
|
||||
v.max_dmg = v.item.max_dmg
|
||||
v.nbt_hash = v.item.nbt_hash
|
||||
end
|
||||
end)
|
||||
|
||||
return items or { }
|
||||
end
|
||||
|
||||
function ME.getItemCount(id, dmg, nbt_hash, ignore_dmg)
|
||||
|
||||
local fingerprint = {
|
||||
id = id,
|
||||
nbt_hash = nbt_hash,
|
||||
}
|
||||
|
||||
if not ignore_dmg or ignore_dmg ~= 'yes' then
|
||||
fingerprint.dmg = dmg or 0
|
||||
end
|
||||
|
||||
local item = ME.getItemDetail(fingerprint, false)
|
||||
|
||||
if item then
|
||||
return item.qty
|
||||
end
|
||||
|
||||
return 0
|
||||
end
|
||||
|
||||
function ME.extract(id, dmg, nbt_hash, qty, direction, slot)
|
||||
dmg = dmg or 0
|
||||
qty = qty or 1
|
||||
direction = direction or 'up'
|
||||
return pcall(function()
|
||||
local fingerprint = {
|
||||
dmg = dmg,
|
||||
id = id,
|
||||
nbt_hash = nbt_hash
|
||||
}
|
||||
return ME.exportItem(fingerprint, direction, qty, slot)
|
||||
end)
|
||||
end
|
||||
|
||||
function ME.insert(slot, qty, direction)
|
||||
direction = direction or 'up'
|
||||
return ME.pullItem(direction, slot, qty)
|
||||
end
|
||||
|
||||
function ME.isCrafting()
|
||||
local cpus = ME.p.getCraftingCPUs() or { }
|
||||
for k,v in pairs(cpus) do
|
||||
if v.busy then
|
||||
return true
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function ME.isCPUAvailable()
|
||||
local cpus = ME.p.getCraftingCPUs() or { }
|
||||
local available = false
|
||||
|
||||
for cpu,v in pairs(cpus) do
|
||||
if not v.busy then
|
||||
available = true
|
||||
elseif not ME.jobList[cpu] then -- something else is crafting something (don't know what)
|
||||
return false -- return false since we are in an unknown state
|
||||
end
|
||||
end
|
||||
return available
|
||||
end
|
||||
|
||||
function ME.getJobList()
|
||||
|
||||
local cpus = ME.p.getCraftingCPUs() or { }
|
||||
for cpu,v in pairs(cpus) do
|
||||
if not v.busy then
|
||||
ME.jobList[cpu] = nil
|
||||
end
|
||||
end
|
||||
|
||||
return ME.jobList
|
||||
end
|
||||
|
||||
function ME.craft(id, dmg, nbt_hash, qty)
|
||||
local cpus = ME.p.getCraftingCPUs() or { }
|
||||
for cpu,v in pairs(cpus) do
|
||||
if not v.busy then
|
||||
ME.p.requestCrafting({
|
||||
id = id,
|
||||
dmg = dmg or 0,
|
||||
nbt_hash = nbt_hash,
|
||||
},
|
||||
qty or 1,
|
||||
cpu
|
||||
)
|
||||
|
||||
os.sleep(0) -- tell it to craft, yet it doesn't show busy - try waiting a cycle...
|
||||
cpus = ME.p.getCraftingCPUs() or { }
|
||||
if not cpus[cpu].busy then
|
||||
-- print('sleeping again')
|
||||
os.sleep(.1) -- sigh
|
||||
cpus = ME.p.getCraftingCPUs() or { }
|
||||
end
|
||||
|
||||
-- not working :(
|
||||
if cpus[cpu].busy then
|
||||
ME.jobList[cpu] = { id = id, dmg = dmg, qty = qty, nbt_hash = nbt_hash }
|
||||
return true
|
||||
end
|
||||
break -- only need to try the first available cpu
|
||||
end
|
||||
end
|
||||
return false
|
||||
end
|
||||
|
||||
return ME
|
||||
137
sys/apis/meProvider.lua
Normal file
137
sys/apis/meProvider.lua
Normal file
@@ -0,0 +1,137 @@
|
||||
local class = require('class')
|
||||
local Logger = require('logger')
|
||||
|
||||
local MEProvider = class()
|
||||
|
||||
function MEProvider:init(args)
|
||||
self.items = {}
|
||||
self.name = 'ME'
|
||||
end
|
||||
|
||||
function MEProvider:isValid()
|
||||
local mep = peripheral.wrap('bottom')
|
||||
return mep and mep.getAvailableItems and mep.getAvailableItems()
|
||||
end
|
||||
|
||||
-- Strip off color prefix
|
||||
local function safeString(text)
|
||||
|
||||
local val = text:byte(1)
|
||||
|
||||
if val < 32 or val > 128 then
|
||||
|
||||
local newText = {}
|
||||
for i = 4, #text do
|
||||
local val = text:byte(i)
|
||||
newText[i - 3] = (val > 31 and val < 127) and val or 63
|
||||
end
|
||||
return string.char(unpack(newText))
|
||||
end
|
||||
|
||||
return text
|
||||
end
|
||||
|
||||
function MEProvider:refresh()
|
||||
local mep = peripheral.wrap('bottom')
|
||||
if mep then
|
||||
self.items = mep.getAvailableItems('all')
|
||||
for _,v in pairs(self.items) do
|
||||
Util.merge(v, v.item)
|
||||
v.name = safeString(v.display_name)
|
||||
end
|
||||
end
|
||||
return self.items
|
||||
end
|
||||
|
||||
function MEProvider:getItemInfo(id, dmg)
|
||||
|
||||
for key,item in pairs(self.items) do
|
||||
if item.id == id and item.dmg == dmg then
|
||||
return item
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function MEProvider:craft(id, dmg, qty)
|
||||
|
||||
self:refresh()
|
||||
|
||||
local item = self:getItemInfo(id, dmg)
|
||||
|
||||
if item and item.is_craftable then
|
||||
|
||||
local mep = peripheral.wrap('bottom')
|
||||
if mep then
|
||||
Logger.log('meProvideer', 'requested crafting for: ' .. id .. ':' .. dmg .. ' qty: ' .. qty)
|
||||
mep.requestCrafting({ id = id, dmg = dmg }, qty)
|
||||
return true
|
||||
end
|
||||
end
|
||||
|
||||
return false
|
||||
end
|
||||
|
||||
function MEProvider:craftItems(items)
|
||||
local mep = peripheral.wrap('bottom')
|
||||
|
||||
local cpus = mep.getCraftingCPUs() or { }
|
||||
local count = 0
|
||||
|
||||
for _,cpu in pairs(cpus) do
|
||||
if cpu.busy then
|
||||
return
|
||||
end
|
||||
end
|
||||
|
||||
for _,item in pairs(items) do
|
||||
if count >= #cpus then
|
||||
break
|
||||
end
|
||||
if self:craft(item.id, item.dmg, item.qty) then
|
||||
count = count + 1
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function MEProvider:provide(item, qty, slot)
|
||||
local mep = peripheral.wrap('bottom')
|
||||
if mep then
|
||||
return pcall(function()
|
||||
mep.exportItem({
|
||||
id = item.id,
|
||||
dmg = item.dmg
|
||||
},
|
||||
'up',
|
||||
qty,
|
||||
slot)
|
||||
end)
|
||||
|
||||
--if item.qty then
|
||||
-- item.qty = item.qty - extractedQty
|
||||
--end
|
||||
end
|
||||
end
|
||||
|
||||
function MEProvider:insert(slot, qty)
|
||||
local mep = peripheral.wrap('bottom')
|
||||
if mep then
|
||||
local s, m = pcall(function() mep.pullItem('up', slot, qty) end)
|
||||
if not s and m then
|
||||
print('meProvider:pullItem')
|
||||
print(m)
|
||||
Logger.log('meProvider', 'Insert failed, trying again')
|
||||
sleep(1)
|
||||
s, m = pcall(function() mep.pullItem('up', slot, qty) end)
|
||||
if not s and m then
|
||||
print('meProvider:pullItem')
|
||||
print(m)
|
||||
Logger.log('meProvider', 'Insert failed again')
|
||||
read()
|
||||
else
|
||||
Logger.log('meProvider', 'Insert successful')
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
return MEProvider
|
||||
106
sys/apis/message.lua
Normal file
106
sys/apis/message.lua
Normal file
@@ -0,0 +1,106 @@
|
||||
local Event = require('event')
|
||||
local Logger = require('logger')
|
||||
|
||||
local Message = { }
|
||||
|
||||
local messageHandlers = {}
|
||||
|
||||
function Message.enable()
|
||||
if not device.wireless_modem.isOpen(os.getComputerID()) then
|
||||
device.wireless_modem.open(os.getComputerID())
|
||||
end
|
||||
if not device.wireless_modem.isOpen(60000) then
|
||||
device.wireless_modem.open(60000)
|
||||
end
|
||||
end
|
||||
|
||||
if device and device.wireless_modem then
|
||||
Message.enable()
|
||||
end
|
||||
|
||||
Event.addHandler('device_attach', function(event, deviceName)
|
||||
if deviceName == 'wireless_modem' then
|
||||
Message.enable()
|
||||
end
|
||||
end)
|
||||
|
||||
function Message.addHandler(type, f)
|
||||
table.insert(messageHandlers, {
|
||||
type = type,
|
||||
f = f,
|
||||
enabled = true
|
||||
})
|
||||
end
|
||||
|
||||
function Message.removeHandler(h)
|
||||
for k,v in pairs(messageHandlers) do
|
||||
if v == h then
|
||||
messageHandlers[k] = nil
|
||||
break
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Event.addHandler('modem_message',
|
||||
function(event, side, sendChannel, replyChannel, msg, distance)
|
||||
if msg and msg.type then -- filter out messages from other systems
|
||||
local id = replyChannel
|
||||
Logger.log('modem_receive', { id, msg.type })
|
||||
--Logger.log('modem_receive', msg.contents)
|
||||
for k,h in pairs(messageHandlers) do
|
||||
if h.type == msg.type then
|
||||
-- should provide msg.contents instead of message - type is already known
|
||||
h.f(h, id, msg, distance)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
)
|
||||
|
||||
function Message.send(id, msgType, contents)
|
||||
if not device.wireless_modem then
|
||||
error('No modem attached', 2)
|
||||
end
|
||||
|
||||
if id then
|
||||
Logger.log('modem_send', { tostring(id), msgType })
|
||||
device.wireless_modem.transmit(id, os.getComputerID(), {
|
||||
type = msgType, contents = contents
|
||||
})
|
||||
else
|
||||
Logger.log('modem_send', { 'broadcast', msgType })
|
||||
device.wireless_modem.transmit(60000, os.getComputerID(), {
|
||||
type = msgType, contents = contents
|
||||
})
|
||||
end
|
||||
end
|
||||
|
||||
function Message.broadcast(t, contents)
|
||||
if not device.wireless_modem then
|
||||
error('No modem attached', 2)
|
||||
end
|
||||
|
||||
Message.send(nil, t, contents)
|
||||
-- Logger.log('rednet_send', { 'broadcast', t })
|
||||
-- rednet.broadcast({ type = t, contents = contents })
|
||||
end
|
||||
|
||||
function Message.waitForMessage(msgType, timeout, fromId)
|
||||
local timerId = os.startTimer(timeout)
|
||||
repeat
|
||||
local e, side, _id, id, msg, distance = os.pullEvent()
|
||||
if e == 'modem_message' then
|
||||
if msg and msg.type and msg.type == msgType then
|
||||
if not fromId or id == fromId then
|
||||
return e, id, msg, distance
|
||||
end
|
||||
end
|
||||
end
|
||||
until e == 'timer' and side == timerId
|
||||
end
|
||||
|
||||
function Message.enableWirelessLogging()
|
||||
Logger.setWirelessLogging()
|
||||
end
|
||||
|
||||
return Message
|
||||
76
sys/apis/nft.lua
Normal file
76
sys/apis/nft.lua
Normal file
@@ -0,0 +1,76 @@
|
||||
local Util = require('util')
|
||||
|
||||
local NFT = { }
|
||||
|
||||
-- largely copied from http://www.computercraft.info/forums2/index.php?/topic/5029-145-npaintpro/
|
||||
|
||||
local tColourLookup = { }
|
||||
for n = 1, 16 do
|
||||
tColourLookup[string.byte("0123456789abcdef", n, n)] = 2 ^ (n - 1)
|
||||
end
|
||||
|
||||
local function getColourOf(hex)
|
||||
return tColourLookup[hex:byte()]
|
||||
end
|
||||
|
||||
function NFT.parse(imageText)
|
||||
local image = {
|
||||
fg = { },
|
||||
bg = { },
|
||||
text = { },
|
||||
}
|
||||
|
||||
local num = 1
|
||||
local index = 1
|
||||
for _,sLine in ipairs(Util.split(imageText)) do
|
||||
table.insert(image.fg, { })
|
||||
table.insert(image.bg, { })
|
||||
table.insert(image.text, { })
|
||||
|
||||
--As we're no longer 1-1, we keep track of what index to write to
|
||||
local writeIndex = 1
|
||||
--Tells us if we've hit a 30 or 31 (BG and FG respectively)- next char specifies the curr colour
|
||||
local bgNext, fgNext = false, false
|
||||
--The current background and foreground colours
|
||||
local currBG, currFG = nil,nil
|
||||
for i = 1, #sLine do
|
||||
local nextChar = string.sub(sLine, i, i)
|
||||
if nextChar:byte() == 30 then
|
||||
bgNext = true
|
||||
elseif nextChar:byte() == 31 then
|
||||
fgNext = true
|
||||
elseif bgNext then
|
||||
currBG = getColourOf(nextChar)
|
||||
bgNext = false
|
||||
elseif fgNext then
|
||||
currFG = getColourOf(nextChar)
|
||||
fgNext = false
|
||||
else
|
||||
if nextChar ~= " " and currFG == nil then
|
||||
currFG = colours.white
|
||||
end
|
||||
image.bg[num][writeIndex] = currBG
|
||||
image.fg[num][writeIndex] = currFG
|
||||
image.text[num][writeIndex] = nextChar
|
||||
writeIndex = writeIndex + 1
|
||||
end
|
||||
end
|
||||
image.height = num
|
||||
if not image.width or writeIndex - 1 > image.width then
|
||||
image.width = writeIndex - 1
|
||||
end
|
||||
num = num+1
|
||||
end
|
||||
return image
|
||||
end
|
||||
|
||||
function NFT.load(path)
|
||||
|
||||
local imageText = Util.readFile(path)
|
||||
if not imageText then
|
||||
error('Unable to read image file')
|
||||
end
|
||||
return NFT.parse(imageText)
|
||||
end
|
||||
|
||||
return NFT
|
||||
95
sys/apis/peripheral.lua
Normal file
95
sys/apis/peripheral.lua
Normal file
@@ -0,0 +1,95 @@
|
||||
local Peripheral = { }
|
||||
|
||||
function Peripheral.addDevice(side)
|
||||
local name = side
|
||||
local ptype = peripheral.getType(side)
|
||||
|
||||
if not ptype then
|
||||
return
|
||||
end
|
||||
|
||||
if ptype == 'modem' then
|
||||
if peripheral.call(name, 'isWireless') then
|
||||
ptype = 'wireless_modem'
|
||||
else
|
||||
ptype = 'wired_modem'
|
||||
end
|
||||
end
|
||||
|
||||
local sides = {
|
||||
front = true,
|
||||
back = true,
|
||||
top = true,
|
||||
bottom = true,
|
||||
left = true,
|
||||
right = true
|
||||
}
|
||||
|
||||
if sides[name] then
|
||||
local i = 1
|
||||
local uniqueName = ptype
|
||||
while device[uniqueName] do
|
||||
uniqueName = ptype .. '_' .. i
|
||||
i = i + 1
|
||||
end
|
||||
name = uniqueName
|
||||
end
|
||||
|
||||
device[name] = peripheral.wrap(side)
|
||||
Util.merge(device[name], {
|
||||
name = name,
|
||||
type = ptype,
|
||||
side = side,
|
||||
})
|
||||
|
||||
return device[name]
|
||||
end
|
||||
|
||||
function Peripheral.getBySide(side)
|
||||
return Util.find(device, 'side', side)
|
||||
end
|
||||
|
||||
function Peripheral.getByType(typeName)
|
||||
return Util.find(device, 'type', typeName)
|
||||
end
|
||||
|
||||
function Peripheral.getByMethod(method)
|
||||
for _,p in pairs(device) do
|
||||
if p[method] then
|
||||
return p
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
-- match any of the passed arguments
|
||||
function Peripheral.get(args)
|
||||
|
||||
if type(args) == 'string' then
|
||||
args = { type = args }
|
||||
end
|
||||
|
||||
args = args or { type = pType }
|
||||
|
||||
if args.type then
|
||||
local p = Peripheral.getByType(args.type)
|
||||
if p then
|
||||
return p
|
||||
end
|
||||
end
|
||||
|
||||
if args.method then
|
||||
local p = Peripheral.getByMethod(args.method)
|
||||
if p then
|
||||
return p
|
||||
end
|
||||
end
|
||||
|
||||
if args.side then
|
||||
local p = Peripheral.getBySide(args.side)
|
||||
if p then
|
||||
return p
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
return Peripheral
|
||||
147
sys/apis/point.lua
Normal file
147
sys/apis/point.lua
Normal file
@@ -0,0 +1,147 @@
|
||||
local Point = { }
|
||||
|
||||
function Point.copy(pt)
|
||||
return { x = pt.x, y = pt.y, z = pt.z }
|
||||
end
|
||||
|
||||
function Point.subtract(a, b)
|
||||
a.x = a.x - b.x
|
||||
a.y = a.y - b.y
|
||||
a.z = a.z - b.z
|
||||
end
|
||||
|
||||
-- real distance
|
||||
function Point.pythagoreanDistance(a, b)
|
||||
return math.sqrt(
|
||||
math.pow(a.x - b.x, 2) +
|
||||
math.pow(a.y - b.y, 2) +
|
||||
math.pow(a.z - b.z, 2))
|
||||
end
|
||||
|
||||
-- turtle distance
|
||||
function Point.turtleDistance(a, b)
|
||||
if a.y and b.y then
|
||||
return math.abs(a.x - b.x) +
|
||||
math.abs(a.y - b.y) +
|
||||
math.abs(a.z - b.z)
|
||||
else
|
||||
return math.abs(a.x - b.x) +
|
||||
math.abs(a.z - b.z)
|
||||
end
|
||||
end
|
||||
|
||||
function Point.calculateTurns(ih, oh)
|
||||
if ih == oh then
|
||||
return 0
|
||||
end
|
||||
if (ih % 2) == (oh % 2) then
|
||||
return 2
|
||||
end
|
||||
return 1
|
||||
end
|
||||
|
||||
-- Calculate distance to location including turns
|
||||
-- also returns the resulting heading
|
||||
function Point.calculateMoves(pta, ptb, distance)
|
||||
local heading = pta.heading
|
||||
local moves = distance or Point.turtleDistance(pta, ptb)
|
||||
if (pta.heading % 2) == 0 and pta.z ~= ptb.z then
|
||||
moves = moves + 1
|
||||
if ptb.heading and (ptb.heading % 2 == 1) then
|
||||
heading = ptb.heading
|
||||
elseif ptb.z > pta.z then
|
||||
heading = 1
|
||||
else
|
||||
heading = 3
|
||||
end
|
||||
elseif (pta.heading % 2) == 1 and pta.x ~= ptb.x then
|
||||
moves = moves + 1
|
||||
if ptb.heading and (ptb.heading % 2 == 0) then
|
||||
heading = ptb.heading
|
||||
elseif ptb.x > pta.x then
|
||||
heading = 0
|
||||
else
|
||||
heading = 2
|
||||
end
|
||||
end
|
||||
|
||||
if ptb.heading then
|
||||
if heading ~= ptb.heading then
|
||||
moves = moves + Point.calculateTurns(heading, ptb.heading)
|
||||
heading = ptb.heading
|
||||
end
|
||||
end
|
||||
|
||||
return moves, heading
|
||||
end
|
||||
|
||||
-- given a set of points, find the one taking the least moves
|
||||
function Point.closest(reference, pts)
|
||||
local lpt, lm -- lowest
|
||||
for _,pt in pairs(pts) do
|
||||
local m = Point.calculateMoves(reference, pt)
|
||||
if not lm or m < lm then
|
||||
lpt = pt
|
||||
lm = m
|
||||
end
|
||||
end
|
||||
return lpt
|
||||
end
|
||||
|
||||
function Point.adjacentPoints(pt)
|
||||
local pts = { }
|
||||
|
||||
for _, hi in pairs(turtle.getHeadings()) do
|
||||
table.insert(pts, { x = pt.x + hi.xd, y = pt.y + hi.yd, z = pt.z + hi.zd })
|
||||
end
|
||||
|
||||
return pts
|
||||
end
|
||||
|
||||
return Point
|
||||
|
||||
--[[
|
||||
function Point.toBox(pt, width, length, height)
|
||||
return { ax = pt.x,
|
||||
ay = pt.y,
|
||||
az = pt.z,
|
||||
bx = pt.x + width - 1,
|
||||
by = pt.y + height - 1,
|
||||
bz = pt.z + length - 1
|
||||
}
|
||||
end
|
||||
|
||||
function Point.inBox(pt, box)
|
||||
return pt.x >= box.ax and
|
||||
pt.z >= box.az and
|
||||
pt.x <= box.bx and
|
||||
pt.z <= box.bz
|
||||
end
|
||||
|
||||
Box = { }
|
||||
|
||||
function Box.contain(boundingBox, containedBox)
|
||||
|
||||
local shiftX = boundingBox.ax - containedBox.ax
|
||||
if shiftX > 0 then
|
||||
containedBox.ax = containedBox.ax + shiftX
|
||||
containedBox.bx = containedBox.bx + shiftX
|
||||
end
|
||||
local shiftZ = boundingBox.az - containedBox.az
|
||||
if shiftZ > 0 then
|
||||
containedBox.az = containedBox.az + shiftZ
|
||||
containedBox.bz = containedBox.bz + shiftZ
|
||||
end
|
||||
|
||||
shiftX = boundingBox.bx - containedBox.bx
|
||||
if shiftX < 0 then
|
||||
containedBox.ax = containedBox.ax + shiftX
|
||||
containedBox.bx = containedBox.bx + shiftX
|
||||
end
|
||||
shiftZ = boundingBox.bz - containedBox.bz
|
||||
if shiftZ < 0 then
|
||||
containedBox.az = containedBox.az + shiftZ
|
||||
containedBox.bz = containedBox.bz + shiftZ
|
||||
end
|
||||
end
|
||||
--]]
|
||||
111
sys/apis/process.lua
Normal file
111
sys/apis/process.lua
Normal file
@@ -0,0 +1,111 @@
|
||||
local Process = { }
|
||||
|
||||
function Process:init(args)
|
||||
self.args = { }
|
||||
self.uid = 0
|
||||
self.threads = { }
|
||||
Util.merge(self, args)
|
||||
self.name = self.name or 'Thread:' .. self.uid
|
||||
end
|
||||
|
||||
function Process:isDead()
|
||||
return coroutine.status(self.co) == 'dead'
|
||||
end
|
||||
|
||||
function Process:terminate()
|
||||
print('terminating ' .. self.name)
|
||||
self:resume('terminate')
|
||||
end
|
||||
|
||||
function Process:threadEvent(...)
|
||||
|
||||
for _,key in pairs(Util.keys(self.threads)) do
|
||||
local thread = self.threads[key]
|
||||
if thread then
|
||||
thread:resume(...)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function Process:newThread(name, fn, ...)
|
||||
|
||||
self.uid = self.uid + 1
|
||||
|
||||
local thread = { }
|
||||
setmetatable(thread, { __index = Process })
|
||||
thread:init({
|
||||
fn = fn,
|
||||
name = name,
|
||||
uid = self.uid,
|
||||
})
|
||||
|
||||
local args = { ... }
|
||||
thread.co = coroutine.create(function()
|
||||
|
||||
local s, m = pcall(function() fn(unpack(args)) end)
|
||||
if not s and m then
|
||||
if m == 'Terminated' then
|
||||
printError(thread.name .. ' terminated')
|
||||
else
|
||||
printError(m)
|
||||
end
|
||||
end
|
||||
|
||||
--print('thread died ' .. thread.name)
|
||||
self.threads[thread.uid] = nil
|
||||
|
||||
thread:threadEvent('terminate')
|
||||
|
||||
return s, m
|
||||
end)
|
||||
|
||||
self.threads[thread.uid] = thread
|
||||
|
||||
thread:resume()
|
||||
|
||||
return thread
|
||||
end
|
||||
|
||||
function Process:resume(event, ...)
|
||||
|
||||
-- threads get a chance to process the event regardless of the main process filter
|
||||
self:threadEvent(event, ...)
|
||||
|
||||
if not self.filter or self.filter == event or event == "terminate" then
|
||||
local ok, result = coroutine.resume(self.co, event, ...)
|
||||
if ok then
|
||||
self.filter = result
|
||||
end
|
||||
return ok, result
|
||||
end
|
||||
|
||||
return true, self.filter
|
||||
end
|
||||
|
||||
function Process:pullEvent(filter)
|
||||
|
||||
while true do
|
||||
local e = { os.pullEventRaw() }
|
||||
self:threadEvent(unpack(e))
|
||||
|
||||
if not filter or e[1] == filter or e[1] == 'terminate' then
|
||||
return unpack(e)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function Process:pullEvents(filter)
|
||||
|
||||
while true do
|
||||
local e = { os.pullEventRaw(filter) }
|
||||
self:threadEvent(unpack(e))
|
||||
if e[1] == 'terminate' then
|
||||
return unpack(e)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local process = { }
|
||||
setmetatable(process, { __index = Process })
|
||||
process:init({ name = 'Main', co = coroutine.running() })
|
||||
return process
|
||||
50
sys/apis/profile.lua
Normal file
50
sys/apis/profile.lua
Normal file
@@ -0,0 +1,50 @@
|
||||
local Logger = require('logger')
|
||||
|
||||
local Profile = {
|
||||
start = function() end,
|
||||
stop = function() end,
|
||||
display = function() end,
|
||||
methods = { },
|
||||
}
|
||||
|
||||
local function Profile_display()
|
||||
Logger.log('profile', 'Profiling results')
|
||||
for k,v in pairs(Profile.methods) do
|
||||
Logger.log('profile', '%s: %f %d %f',
|
||||
k, Util.round(v.elapsed, 2), v.count, Util.round(v.elapsed/v.count, 2))
|
||||
end
|
||||
Profile.methods = { }
|
||||
end
|
||||
|
||||
local function Profile_start(name)
|
||||
local p = Profile.methods[name]
|
||||
if not p then
|
||||
p = { }
|
||||
p.elapsed = 0
|
||||
p.count = 0
|
||||
Profile.methods[name] = p
|
||||
end
|
||||
p.clock = os.clock()
|
||||
return p
|
||||
end
|
||||
|
||||
local function Profile_stop(name)
|
||||
local p = Profile.methods[name]
|
||||
p.elapsed = p.elapsed + (os.clock() - p.clock)
|
||||
p.count = p.count + 1
|
||||
end
|
||||
|
||||
function Profile.enable()
|
||||
Logger.log('profile', 'Profiling enabled')
|
||||
Profile.start = Profile_start
|
||||
Profile.stop = Profile_stop
|
||||
Profile.display = Profile_display
|
||||
end
|
||||
|
||||
function Profile.disable()
|
||||
Profile.start = function() end
|
||||
Profile.stop = function() end
|
||||
Profile.display = function() end
|
||||
end
|
||||
|
||||
return Profile
|
||||
58
sys/apis/require.lua
Normal file
58
sys/apis/require.lua
Normal file
@@ -0,0 +1,58 @@
|
||||
local function resolveFile(filename, dir, lua_path)
|
||||
|
||||
local ch = string.sub(filename, 1, 1)
|
||||
if ch == "/" then
|
||||
return filename
|
||||
end
|
||||
|
||||
if dir then
|
||||
local path = fs.combine(dir, filename)
|
||||
if fs.exists(path) and not fs.isDir(path) then
|
||||
return path
|
||||
end
|
||||
end
|
||||
|
||||
if lua_path then
|
||||
for dir in string.gmatch(lua_path, "[^:]+") do
|
||||
local path = fs.combine(dir, filename)
|
||||
if fs.exists(path) and not fs.isDir(path) then
|
||||
return path
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local modules = { }
|
||||
|
||||
return function(filename)
|
||||
|
||||
local dir = DIR
|
||||
if not dir and shell and type(shell.dir) == 'function' then
|
||||
dir = shell.dir()
|
||||
end
|
||||
|
||||
local fname = resolveFile(filename:gsub('%.', '/') .. '.lua',
|
||||
dir or '', LUA_PATH or '/sys/apis')
|
||||
|
||||
if not fname or not fs.exists(fname) then
|
||||
error('Unable to load: ' .. filename, 2)
|
||||
end
|
||||
|
||||
local rname = fname:gsub('%/', '.'):gsub('%.lua', '')
|
||||
|
||||
local module = modules[rname]
|
||||
if not module then
|
||||
|
||||
local f, err = loadfile(fname)
|
||||
if not f then
|
||||
error(err)
|
||||
end
|
||||
setfenv(f, getfenv(1))
|
||||
|
||||
module = f(rname)
|
||||
|
||||
modules[rname] = module
|
||||
end
|
||||
|
||||
return module
|
||||
end
|
||||
1178
sys/apis/schematic.lua
Normal file
1178
sys/apis/schematic.lua
Normal file
File diff suppressed because it is too large
Load Diff
297
sys/apis/sha1.lua
Normal file
297
sys/apis/sha1.lua
Normal file
@@ -0,0 +1,297 @@
|
||||
local sha1 = {
|
||||
_VERSION = "sha.lua 0.5.0",
|
||||
_URL = "https://github.com/kikito/sha.lua",
|
||||
_DESCRIPTION = [[
|
||||
SHA-1 secure hash computation, and HMAC-SHA1 signature computation in Lua (5.1)
|
||||
Based on code originally by Jeffrey Friedl (http://regex.info/blog/lua/sha1)
|
||||
And modified by Eike Decker - (http://cube3d.de/uploads/Main/sha1.txt)
|
||||
]],
|
||||
_LICENSE = [[
|
||||
MIT LICENSE
|
||||
|
||||
Copyright (c) 2013 Enrique Garcia Cota + Eike Decker + Jeffrey Friedl
|
||||
|
||||
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.
|
||||
]]
|
||||
}
|
||||
|
||||
-----------------------------------------------------------------------------------
|
||||
|
||||
-- loading this file (takes a while but grants a boost of factor 13)
|
||||
local PRELOAD_CACHE = false
|
||||
|
||||
local BLOCK_SIZE = 64 -- 512 bits
|
||||
|
||||
-- local storing of global functions (minor speedup)
|
||||
local floor,modf = math.floor,math.modf
|
||||
local char,format,rep = string.char,string.format,string.rep
|
||||
|
||||
-- merge 4 bytes to an 32 bit word
|
||||
local function bytes_to_w32(a,b,c,d) return a*0x1000000+b*0x10000+c*0x100+d end
|
||||
-- split a 32 bit word into four 8 bit numbers
|
||||
local function w32_to_bytes(i)
|
||||
return floor(i/0x1000000)%0x100,floor(i/0x10000)%0x100,floor(i/0x100)%0x100,i%0x100
|
||||
end
|
||||
|
||||
-- shift the bits of a 32 bit word. Don't use negative values for "bits"
|
||||
local function w32_rot(bits,a)
|
||||
local b2 = 2^(32-bits)
|
||||
local a,b = modf(a/b2)
|
||||
return a+b*b2*(2^(bits))
|
||||
end
|
||||
|
||||
-- caching function for functions that accept 2 arguments, both of values between
|
||||
-- 0 and 255. The function to be cached is passed, all values are calculated
|
||||
-- during loading and a function is returned that returns the cached values (only)
|
||||
local function cache2arg(fn)
|
||||
if not PRELOAD_CACHE then return fn end
|
||||
local lut = {}
|
||||
for i=0,0xffff do
|
||||
local a,b = floor(i/0x100),i%0x100
|
||||
lut[i] = fn(a,b)
|
||||
end
|
||||
return function(a,b)
|
||||
return lut[a*0x100+b]
|
||||
end
|
||||
end
|
||||
|
||||
-- splits an 8-bit number into 8 bits, returning all 8 bits as booleans
|
||||
local function byte_to_bits(b)
|
||||
local b = function(n)
|
||||
local b = floor(b/n)
|
||||
return b%2==1
|
||||
end
|
||||
return b(1),b(2),b(4),b(8),b(16),b(32),b(64),b(128)
|
||||
end
|
||||
|
||||
-- builds an 8bit number from 8 booleans
|
||||
local function bits_to_byte(a,b,c,d,e,f,g,h)
|
||||
local function n(b,x) return b and x or 0 end
|
||||
return n(a,1)+n(b,2)+n(c,4)+n(d,8)+n(e,16)+n(f,32)+n(g,64)+n(h,128)
|
||||
end
|
||||
|
||||
-- bitwise "and" function for 2 8bit number
|
||||
local band = cache2arg (function(a,b)
|
||||
local A,B,C,D,E,F,G,H = byte_to_bits(b)
|
||||
local a,b,c,d,e,f,g,h = byte_to_bits(a)
|
||||
return bits_to_byte(
|
||||
A and a, B and b, C and c, D and d,
|
||||
E and e, F and f, G and g, H and h)
|
||||
end)
|
||||
|
||||
-- bitwise "or" function for 2 8bit numbers
|
||||
local bor = cache2arg(function(a,b)
|
||||
local A,B,C,D,E,F,G,H = byte_to_bits(b)
|
||||
local a,b,c,d,e,f,g,h = byte_to_bits(a)
|
||||
return bits_to_byte(
|
||||
A or a, B or b, C or c, D or d,
|
||||
E or e, F or f, G or g, H or h)
|
||||
end)
|
||||
|
||||
-- bitwise "xor" function for 2 8bit numbers
|
||||
local bxor = cache2arg(function(a,b)
|
||||
local A,B,C,D,E,F,G,H = byte_to_bits(b)
|
||||
local a,b,c,d,e,f,g,h = byte_to_bits(a)
|
||||
return bits_to_byte(
|
||||
A ~= a, B ~= b, C ~= c, D ~= d,
|
||||
E ~= e, F ~= f, G ~= g, H ~= h)
|
||||
end)
|
||||
|
||||
-- bitwise complement for one 8bit number
|
||||
local function bnot(x)
|
||||
return 255-(x % 256)
|
||||
end
|
||||
|
||||
-- creates a function to combine to 32bit numbers using an 8bit combination function
|
||||
local function w32_comb(fn)
|
||||
return function(a,b)
|
||||
local aa,ab,ac,ad = w32_to_bytes(a)
|
||||
local ba,bb,bc,bd = w32_to_bytes(b)
|
||||
return bytes_to_w32(fn(aa,ba),fn(ab,bb),fn(ac,bc),fn(ad,bd))
|
||||
end
|
||||
end
|
||||
|
||||
-- create functions for and, xor and or, all for 2 32bit numbers
|
||||
local w32_and = w32_comb(band)
|
||||
local w32_xor = w32_comb(bxor)
|
||||
local w32_or = w32_comb(bor)
|
||||
|
||||
-- xor function that may receive a variable number of arguments
|
||||
local function w32_xor_n(a,...)
|
||||
local aa,ab,ac,ad = w32_to_bytes(a)
|
||||
for i=1,select('#',...) do
|
||||
local ba,bb,bc,bd = w32_to_bytes(select(i,...))
|
||||
aa,ab,ac,ad = bxor(aa,ba),bxor(ab,bb),bxor(ac,bc),bxor(ad,bd)
|
||||
end
|
||||
return bytes_to_w32(aa,ab,ac,ad)
|
||||
end
|
||||
|
||||
-- combining 3 32bit numbers through binary "or" operation
|
||||
local function w32_or3(a,b,c)
|
||||
local aa,ab,ac,ad = w32_to_bytes(a)
|
||||
local ba,bb,bc,bd = w32_to_bytes(b)
|
||||
local ca,cb,cc,cd = w32_to_bytes(c)
|
||||
return bytes_to_w32(
|
||||
bor(aa,bor(ba,ca)), bor(ab,bor(bb,cb)), bor(ac,bor(bc,cc)), bor(ad,bor(bd,cd))
|
||||
)
|
||||
end
|
||||
|
||||
-- binary complement for 32bit numbers
|
||||
local function w32_not(a)
|
||||
return 4294967295-(a % 4294967296)
|
||||
end
|
||||
|
||||
-- adding 2 32bit numbers, cutting off the remainder on 33th bit
|
||||
local function w32_add(a,b) return (a+b) % 4294967296 end
|
||||
|
||||
-- adding n 32bit numbers, cutting off the remainder (again)
|
||||
local function w32_add_n(a,...)
|
||||
for i=1,select('#',...) do
|
||||
a = (a+select(i,...)) % 4294967296
|
||||
end
|
||||
return a
|
||||
end
|
||||
-- converting the number to a hexadecimal string
|
||||
local function w32_to_hexstring(w) return format("%08x",w) end
|
||||
|
||||
local function hex_to_binary(hex)
|
||||
return hex:gsub('..', function(hexval)
|
||||
return string.char(tonumber(hexval, 16))
|
||||
end)
|
||||
end
|
||||
|
||||
-- building the lookuptables ahead of time (instead of littering the source code
|
||||
-- with precalculated values)
|
||||
local xor_with_0x5c = {}
|
||||
local xor_with_0x36 = {}
|
||||
for i=0,0xff do
|
||||
xor_with_0x5c[char(i)] = char(bxor(i,0x5c))
|
||||
xor_with_0x36[char(i)] = char(bxor(i,0x36))
|
||||
end
|
||||
|
||||
-----------------------------------------------------------------------------
|
||||
|
||||
-- calculating the SHA1 for some text
|
||||
function sha1.sha1(msg)
|
||||
local H0,H1,H2,H3,H4 = 0x67452301,0xEFCDAB89,0x98BADCFE,0x10325476,0xC3D2E1F0
|
||||
local msg_len_in_bits = #msg * 8
|
||||
|
||||
local first_append = char(0x80) -- append a '1' bit plus seven '0' bits
|
||||
|
||||
local non_zero_message_bytes = #msg +1 +8 -- the +1 is the appended bit 1, the +8 are for the final appended length
|
||||
local current_mod = non_zero_message_bytes % 64
|
||||
local second_append = current_mod>0 and rep(char(0), 64 - current_mod) or ""
|
||||
|
||||
-- now to append the length as a 64-bit number.
|
||||
local B1, R1 = modf(msg_len_in_bits / 0x01000000)
|
||||
local B2, R2 = modf( 0x01000000 * R1 / 0x00010000)
|
||||
local B3, R3 = modf( 0x00010000 * R2 / 0x00000100)
|
||||
local B4 = 0x00000100 * R3
|
||||
|
||||
local L64 = char( 0) .. char( 0) .. char( 0) .. char( 0) -- high 32 bits
|
||||
.. char(B1) .. char(B2) .. char(B3) .. char(B4) -- low 32 bits
|
||||
|
||||
msg = msg .. first_append .. second_append .. L64
|
||||
|
||||
assert(#msg % 64 == 0)
|
||||
|
||||
local chunks = #msg / 64
|
||||
|
||||
local W = { }
|
||||
local start, A, B, C, D, E, f, K, TEMP
|
||||
local chunk = 0
|
||||
|
||||
while chunk < chunks do
|
||||
--
|
||||
-- break chunk up into W[0] through W[15]
|
||||
--
|
||||
start,chunk = chunk * 64 + 1,chunk + 1
|
||||
|
||||
for t = 0, 15 do
|
||||
W[t] = bytes_to_w32(msg:byte(start, start + 3))
|
||||
start = start + 4
|
||||
end
|
||||
|
||||
--
|
||||
-- build W[16] through W[79]
|
||||
--
|
||||
for t = 16, 79 do
|
||||
-- For t = 16 to 79 let Wt = S1(Wt-3 XOR Wt-8 XOR Wt-14 XOR Wt-16).
|
||||
W[t] = w32_rot(1, w32_xor_n(W[t-3], W[t-8], W[t-14], W[t-16]))
|
||||
end
|
||||
|
||||
A,B,C,D,E = H0,H1,H2,H3,H4
|
||||
|
||||
for t = 0, 79 do
|
||||
if t <= 19 then
|
||||
-- (B AND C) OR ((NOT B) AND D)
|
||||
f = w32_or(w32_and(B, C), w32_and(w32_not(B), D))
|
||||
K = 0x5A827999
|
||||
elseif t <= 39 then
|
||||
-- B XOR C XOR D
|
||||
f = w32_xor_n(B, C, D)
|
||||
K = 0x6ED9EBA1
|
||||
elseif t <= 59 then
|
||||
-- (B AND C) OR (B AND D) OR (C AND D
|
||||
f = w32_or3(w32_and(B, C), w32_and(B, D), w32_and(C, D))
|
||||
K = 0x8F1BBCDC
|
||||
else
|
||||
-- B XOR C XOR D
|
||||
f = w32_xor_n(B, C, D)
|
||||
K = 0xCA62C1D6
|
||||
end
|
||||
|
||||
-- TEMP = S5(A) + ft(B,C,D) + E + Wt + Kt;
|
||||
A,B,C,D,E = w32_add_n(w32_rot(5, A), f, E, W[t], K),
|
||||
A, w32_rot(30, B), C, D
|
||||
end
|
||||
-- Let H0 = H0 + A, H1 = H1 + B, H2 = H2 + C, H3 = H3 + D, H4 = H4 + E.
|
||||
H0,H1,H2,H3,H4 = w32_add(H0, A),w32_add(H1, B),w32_add(H2, C),w32_add(H3, D),w32_add(H4, E)
|
||||
end
|
||||
local f = w32_to_hexstring
|
||||
return f(H0) .. f(H1) .. f(H2) .. f(H3) .. f(H4)
|
||||
end
|
||||
|
||||
|
||||
function sha1.binary(msg)
|
||||
return hex_to_binary(sha1.sha1(msg))
|
||||
end
|
||||
|
||||
function sha1.hmac(key, text)
|
||||
assert(type(key) == 'string', "key passed to sha1.hmac should be a string")
|
||||
assert(type(text) == 'string', "text passed to sha1.hmac should be a string")
|
||||
|
||||
if #key > BLOCK_SIZE then
|
||||
key = sha1.binary(key)
|
||||
end
|
||||
|
||||
local key_xord_with_0x36 = key:gsub('.', xor_with_0x36) .. string.rep(string.char(0x36), BLOCK_SIZE - #key)
|
||||
local key_xord_with_0x5c = key:gsub('.', xor_with_0x5c) .. string.rep(string.char(0x5c), BLOCK_SIZE - #key)
|
||||
|
||||
return sha1.sha1(key_xord_with_0x5c .. sha1.binary(key_xord_with_0x36 .. text))
|
||||
end
|
||||
|
||||
function sha1.hmac_binary(key, text)
|
||||
return hex_to_binary(sha1.hmac(key, text))
|
||||
end
|
||||
|
||||
setmetatable(sha1, {__call = function(_,msg) return sha1.sha1(msg) end })
|
||||
|
||||
return sha1
|
||||
211
sys/apis/socket.lua
Normal file
211
sys/apis/socket.lua
Normal file
@@ -0,0 +1,211 @@
|
||||
local Logger = require('logger')
|
||||
|
||||
local socketClass = { }
|
||||
|
||||
function socketClass:read(timeout)
|
||||
|
||||
if not self.connected then
|
||||
Logger.log('socket', 'read: No connection')
|
||||
return
|
||||
end
|
||||
|
||||
local timerId
|
||||
local filter
|
||||
|
||||
if timeout then
|
||||
timerId = os.startTimer(timeout)
|
||||
elseif self.keepAlive then
|
||||
timerId = os.startTimer(3)
|
||||
else
|
||||
filter = 'modem_message'
|
||||
end
|
||||
|
||||
while true do
|
||||
local e, s, dport, dhost, msg, distance = os.pullEvent(filter)
|
||||
if e == 'modem_message' and
|
||||
dport == self.sport and dhost == self.shost and
|
||||
msg then
|
||||
|
||||
if msg.type == 'DISC' then
|
||||
-- received disconnect from other end
|
||||
self.connected = false
|
||||
self:close()
|
||||
return
|
||||
elseif msg.type == 'DATA' then
|
||||
if msg.data then
|
||||
if timerId then
|
||||
os.cancelTimer(timerId)
|
||||
end
|
||||
return msg.data, distance
|
||||
end
|
||||
end
|
||||
elseif e == 'timer' and s == timerId then
|
||||
if timeout or not self.connected then
|
||||
break
|
||||
end
|
||||
timerId = os.startTimer(3)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function socketClass:write(data)
|
||||
if not self.connected then
|
||||
Logger.log('socket', 'write: No connection')
|
||||
return false
|
||||
end
|
||||
self.transmit(self.dport, self.dhost, {
|
||||
type = 'DATA',
|
||||
data = data,
|
||||
})
|
||||
return true
|
||||
end
|
||||
|
||||
function socketClass:close()
|
||||
if self.connected then
|
||||
Logger.log('socket', 'closing socket ' .. self.sport)
|
||||
self.transmit(self.dport, self.dhost, {
|
||||
type = 'DISC',
|
||||
})
|
||||
self.connected = false
|
||||
end
|
||||
device.wireless_modem.close(self.sport)
|
||||
end
|
||||
|
||||
-- write a ping every second (too much traffic!)
|
||||
local function pinger(socket)
|
||||
|
||||
local process = require('process')
|
||||
|
||||
socket.keepAlive = true
|
||||
|
||||
Logger.log('socket', 'keepAlive enabled')
|
||||
|
||||
process:newThread('socket_ping', function()
|
||||
local timerId = os.startTimer(1)
|
||||
local timeStamp = os.clock()
|
||||
|
||||
while true do
|
||||
local e, id, dport, dhost, msg = os.pullEvent()
|
||||
|
||||
if e == 'modem_message' then
|
||||
if dport == socket.sport and
|
||||
dhost == socket.shost and
|
||||
msg and
|
||||
msg.type == 'PING' then
|
||||
|
||||
timeStamp = os.clock()
|
||||
end
|
||||
elseif e == 'timer' and id == timerId then
|
||||
if os.clock() - timeStamp > 3 then
|
||||
Logger.log('socket', 'Connection timed out')
|
||||
socket:close()
|
||||
break
|
||||
end
|
||||
timerId = os.startTimer(1)
|
||||
socket.transmit(socket.dport, socket.dhost, {
|
||||
type = 'PING',
|
||||
})
|
||||
end
|
||||
end
|
||||
end)
|
||||
end
|
||||
|
||||
local Socket = { }
|
||||
|
||||
local function loopback(port, sport, msg)
|
||||
os.queueEvent('modem_message', 'loopback', port, sport, msg, 0)
|
||||
end
|
||||
|
||||
local function newSocket(isLoopback)
|
||||
for i = 16384, 32768 do
|
||||
if not device.wireless_modem.isOpen(i) then
|
||||
local socket = {
|
||||
shost = os.getComputerID(),
|
||||
sport = i,
|
||||
transmit = device.wireless_modem.transmit,
|
||||
}
|
||||
setmetatable(socket, { __index = socketClass })
|
||||
|
||||
device.wireless_modem.open(socket.sport)
|
||||
|
||||
if isLoopback then
|
||||
socket.transmit = loopback
|
||||
end
|
||||
return socket
|
||||
end
|
||||
end
|
||||
error('No ports available')
|
||||
end
|
||||
|
||||
function Socket.connect(host, port)
|
||||
|
||||
local socket = newSocket(host == os.getComputerID())
|
||||
socket.dhost = host
|
||||
Logger.log('socket', 'connecting to ' .. port)
|
||||
|
||||
socket.transmit(port, socket.sport, {
|
||||
type = 'OPEN',
|
||||
shost = socket.shost,
|
||||
dhost = socket.dhost,
|
||||
})
|
||||
|
||||
local timerId = os.startTimer(3)
|
||||
repeat
|
||||
local e, id, sport, dport, msg = os.pullEvent()
|
||||
if e == 'modem_message' and
|
||||
sport == socket.sport and
|
||||
msg.dhost == socket.shost and
|
||||
msg.type == 'CONN' then
|
||||
|
||||
socket.dport = dport
|
||||
socket.connected = true
|
||||
Logger.log('socket', 'connection established to %d %d->%d',
|
||||
host, socket.sport, socket.dport)
|
||||
|
||||
if msg.keepAlive then
|
||||
pinger(socket)
|
||||
end
|
||||
os.cancelTimer(timerId)
|
||||
|
||||
return socket
|
||||
end
|
||||
until e == 'timer' and id == timerId
|
||||
|
||||
socket:close()
|
||||
end
|
||||
|
||||
function Socket.server(port, keepAlive)
|
||||
|
||||
device.wireless_modem.open(port)
|
||||
Logger.log('socket', 'Waiting for connections on port ' .. port)
|
||||
|
||||
while true do
|
||||
local e, _, sport, dport, msg = os.pullEvent('modem_message')
|
||||
|
||||
if sport == port and
|
||||
msg and
|
||||
msg.dhost == os.getComputerID() and
|
||||
msg.type == 'OPEN' then
|
||||
|
||||
local socket = newSocket(msg.shost == os.getComputerID())
|
||||
socket.dport = dport
|
||||
socket.dhost = msg.shost
|
||||
socket.connected = true
|
||||
|
||||
socket.transmit(socket.dport, socket.sport, {
|
||||
type = 'CONN',
|
||||
dhost = socket.dhost,
|
||||
shost = socket.shost,
|
||||
keepAlive = keepAlive,
|
||||
})
|
||||
Logger.log('socket', 'Connection established %d->%d', socket.sport, socket.dport)
|
||||
|
||||
if keepAlive then
|
||||
pinger(socket)
|
||||
end
|
||||
return socket
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
return Socket
|
||||
24
sys/apis/sync.lua
Normal file
24
sys/apis/sync.lua
Normal file
@@ -0,0 +1,24 @@
|
||||
local syncLocks = { }
|
||||
|
||||
return function(obj, fn)
|
||||
local key = tostring(obj)
|
||||
if syncLocks[key] then
|
||||
local cos = tostring(coroutine.running())
|
||||
table.insert(syncLocks[key], cos)
|
||||
repeat
|
||||
local _, co = os.pullEvent('sync_lock')
|
||||
until co == cos
|
||||
else
|
||||
syncLocks[key] = { }
|
||||
end
|
||||
local s, m = pcall(fn)
|
||||
local co = table.remove(syncLocks[key], 1)
|
||||
if co then
|
||||
os.queueEvent('sync_lock', co)
|
||||
else
|
||||
syncLocks[key] = nil
|
||||
end
|
||||
if not s then
|
||||
error(m)
|
||||
end
|
||||
end
|
||||
53
sys/apis/tableDB.lua
Normal file
53
sys/apis/tableDB.lua
Normal file
@@ -0,0 +1,53 @@
|
||||
local class = require('class')
|
||||
|
||||
local TableDB = class()
|
||||
function TableDB:init(args)
|
||||
local defaults = {
|
||||
fileName = '',
|
||||
dirty = false,
|
||||
data = { },
|
||||
tabledef = { },
|
||||
}
|
||||
Util.merge(defaults, args) -- refactor
|
||||
Util.merge(self, defaults)
|
||||
end
|
||||
|
||||
function TableDB:load()
|
||||
local table = Util.readTable(self.fileName)
|
||||
if table then
|
||||
self.data = table.data
|
||||
self.tabledef = table.tabledef
|
||||
end
|
||||
end
|
||||
|
||||
function TableDB:add(key, entry)
|
||||
if type(key) == 'table' then
|
||||
key = table.concat(key, ':')
|
||||
end
|
||||
self.data[key] = entry
|
||||
self.dirty = true
|
||||
end
|
||||
|
||||
function TableDB:get(key)
|
||||
if type(key) == 'table' then
|
||||
key = table.concat(key, ':')
|
||||
end
|
||||
return self.data[key]
|
||||
end
|
||||
|
||||
function TableDB:remove(key)
|
||||
self.data[key] = nil
|
||||
self.dirty = true
|
||||
end
|
||||
|
||||
function TableDB:flush()
|
||||
if self.dirty then
|
||||
Util.writeTable(self.fileName, {
|
||||
tabledef = self.tabledef,
|
||||
data = self.data,
|
||||
})
|
||||
self.dirty = false
|
||||
end
|
||||
end
|
||||
|
||||
return TableDB
|
||||
145
sys/apis/terminal.lua
Normal file
145
sys/apis/terminal.lua
Normal file
@@ -0,0 +1,145 @@
|
||||
local Terminal = { }
|
||||
|
||||
function Terminal.scrollable(ct, size)
|
||||
|
||||
local size = size or 25
|
||||
local w, h = ct.getSize()
|
||||
local win = window.create(ct, 1, 1, w, h + size, true)
|
||||
local oldWin = Util.shallowCopy(win)
|
||||
local scrollPos = 0
|
||||
|
||||
local function drawScrollbar(oldPos, newPos)
|
||||
local x, y = oldWin.getCursorPos()
|
||||
|
||||
local pos = math.floor(oldPos / size * (h - 1))
|
||||
oldWin.setCursorPos(w, oldPos + pos + 1)
|
||||
oldWin.write(' ')
|
||||
|
||||
pos = math.floor(newPos / size * (h - 1))
|
||||
oldWin.setCursorPos(w, newPos + pos + 1)
|
||||
oldWin.write('#')
|
||||
|
||||
oldWin.setCursorPos(x, y)
|
||||
end
|
||||
|
||||
win.setCursorPos = function(x, y)
|
||||
oldWin.setCursorPos(x, y)
|
||||
if y > scrollPos + h then
|
||||
win.scrollTo(y - h)
|
||||
elseif y < scrollPos then
|
||||
win.scrollTo(y - 2)
|
||||
end
|
||||
end
|
||||
|
||||
win.scrollUp = function()
|
||||
win.scrollTo(scrollPos - 1)
|
||||
end
|
||||
|
||||
win.scrollDown = function()
|
||||
win.scrollTo(scrollPos + 1)
|
||||
end
|
||||
|
||||
win.scrollTo = function(p)
|
||||
p = math.min(math.max(p, 0), size)
|
||||
if p ~= scrollPos then
|
||||
drawScrollbar(scrollPos, p)
|
||||
scrollPos = p
|
||||
win.reposition(1, -scrollPos + 1)
|
||||
end
|
||||
end
|
||||
|
||||
win.clear = function()
|
||||
oldWin.clear()
|
||||
scrollPos = 0
|
||||
end
|
||||
|
||||
drawScrollbar(0, 0)
|
||||
|
||||
return win
|
||||
end
|
||||
|
||||
function Terminal.toGrayscale(ct)
|
||||
|
||||
local scolors = {
|
||||
[ colors.white ] = colors.white,
|
||||
[ colors.orange ] = colors.lightGray,
|
||||
[ colors.magenta ] = colors.lightGray,
|
||||
[ colors.lightBlue ] = colors.lightGray,
|
||||
[ colors.yellow ] = colors.lightGray,
|
||||
[ colors.lime ] = colors.lightGray,
|
||||
[ colors.pink ] = colors.lightGray,
|
||||
[ colors.gray ] = colors.gray,
|
||||
[ colors.lightGray ] = colors.lightGray,
|
||||
[ colors.cyan ] = colors.lightGray,
|
||||
[ colors.purple ] = colors.gray,
|
||||
[ colors.blue ] = colors.gray,
|
||||
[ colors.brown ] = colors.gray,
|
||||
[ colors.green ] = colors.lightGray,
|
||||
[ colors.red ] = colors.gray,
|
||||
[ colors.black ] = colors.black,
|
||||
}
|
||||
|
||||
local methods = { 'setBackgroundColor', 'setBackgroundColour',
|
||||
'setTextColor', 'setTextColour' }
|
||||
for _,v in pairs(methods) do
|
||||
local fn = ct[v]
|
||||
ct[v] = function(c)
|
||||
if scolors[c] then
|
||||
fn(scolors[c])
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local bcolors = {
|
||||
[ '1' ] = '8',
|
||||
[ '2' ] = '8',
|
||||
[ '3' ] = '8',
|
||||
[ '4' ] = '8',
|
||||
[ '5' ] = '8',
|
||||
[ '6' ] = '8',
|
||||
[ '9' ] = '8',
|
||||
[ 'a' ] = '7',
|
||||
[ 'b' ] = '7',
|
||||
[ 'c' ] = '7',
|
||||
[ 'd' ] = '8',
|
||||
[ 'e' ] = '7',
|
||||
}
|
||||
|
||||
local function translate(s)
|
||||
if s then
|
||||
for k,v in pairs(bcolors) do
|
||||
s = s:gsub(k, v)
|
||||
end
|
||||
end
|
||||
return s
|
||||
end
|
||||
|
||||
local fn = ct.blit
|
||||
ct.blit = function(text, fg, bg)
|
||||
fn(text, translate(fg), translate(bg))
|
||||
end
|
||||
end
|
||||
|
||||
function Terminal.copy(ot)
|
||||
local ct = { }
|
||||
for k,v in pairs(ot) do
|
||||
if type(v) == 'function' then
|
||||
ct[k] = v
|
||||
end
|
||||
end
|
||||
return ct
|
||||
end
|
||||
|
||||
function Terminal.mirror(ct, dt)
|
||||
for k,f in pairs(ct) do
|
||||
ct[k] = function(...)
|
||||
local ret = { f(...) }
|
||||
if dt[k] then
|
||||
dt[k](...)
|
||||
end
|
||||
return unpack(ret)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
return Terminal
|
||||
2801
sys/apis/ui.lua
Normal file
2801
sys/apis/ui.lua
Normal file
File diff suppressed because it is too large
Load Diff
530
sys/apis/util.lua
Normal file
530
sys/apis/util.lua
Normal file
@@ -0,0 +1,530 @@
|
||||
local Util = { }
|
||||
|
||||
function Util.tryTimed(timeout, f, ...)
|
||||
local c = os.clock()
|
||||
repeat
|
||||
local ret = f(...)
|
||||
if ret then
|
||||
return ret
|
||||
end
|
||||
until os.clock()-c >= timeout
|
||||
end
|
||||
|
||||
function Util.tryTimes(attempts, f, ...)
|
||||
local result
|
||||
for i = 1, attempts do
|
||||
result = { f(...) }
|
||||
if result[1] then
|
||||
return unpack(result)
|
||||
end
|
||||
end
|
||||
return unpack(result)
|
||||
end
|
||||
|
||||
function Util.print(pattern, ...)
|
||||
|
||||
local function serialize(tbl, width)
|
||||
local str = '{\n'
|
||||
for k, v in pairs(tbl) do
|
||||
local value
|
||||
if type(v) == 'table' then
|
||||
value = string.format('table: %d', Util.size(v))
|
||||
else
|
||||
value = tostring(v)
|
||||
end
|
||||
str = str .. string.format(' %s: %s\n', k, value)
|
||||
end
|
||||
if #str < width then
|
||||
str = str:gsub('\n', '') .. ' }'
|
||||
else
|
||||
str = str .. '}'
|
||||
end
|
||||
return str
|
||||
end
|
||||
|
||||
if type(pattern) == 'string' then
|
||||
print(string.format(pattern, ...))
|
||||
elseif type(pattern) == 'table' then
|
||||
print(serialize(pattern, term.current().getSize()))
|
||||
else
|
||||
print(tostring(pattern))
|
||||
end
|
||||
end
|
||||
|
||||
function Util.runFunction(env, fn, ...)
|
||||
setfenv(fn, env)
|
||||
setmetatable(env, { __index = _G })
|
||||
|
||||
local args = { ... }
|
||||
return pcall(function()
|
||||
return fn(table.unpack(args))
|
||||
end)
|
||||
end
|
||||
|
||||
-- http://lua-users.org/wiki/SimpleRound
|
||||
function Util.round(num, idp)
|
||||
local mult = 10^(idp or 0)
|
||||
return math.floor(num * mult + 0.5) / mult
|
||||
end
|
||||
|
||||
function Util.random(max, min)
|
||||
min = min or 0
|
||||
return math.random(0, max-min) + min
|
||||
end
|
||||
|
||||
--[[ Table functions ]] --
|
||||
function Util.clear(t)
|
||||
local keys = Util.keys(t)
|
||||
for _,k in pairs(keys) do
|
||||
t[k] = nil
|
||||
end
|
||||
end
|
||||
|
||||
function Util.empty(t)
|
||||
return not next(t)
|
||||
end
|
||||
|
||||
function Util.key(t, value)
|
||||
for k,v in pairs(t) do
|
||||
if v == value then
|
||||
return k
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function Util.keys(t)
|
||||
local keys = {}
|
||||
for k in pairs(t) do
|
||||
keys[#keys+1] = k
|
||||
end
|
||||
return keys
|
||||
end
|
||||
|
||||
function Util.merge(obj, args)
|
||||
if args then
|
||||
for k,v in pairs(args) do
|
||||
obj[k] = v
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function Util.deepMerge(obj, args)
|
||||
if args then
|
||||
for k,v in pairs(args) do
|
||||
if type(v) == 'table' then
|
||||
if not obj[k] then
|
||||
obj[k] = { }
|
||||
end
|
||||
Util.deepMerge(obj[k], v)
|
||||
else
|
||||
obj[k] = v
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function Util.transpose(t)
|
||||
local tt = { }
|
||||
for k,v in pairs(t) do
|
||||
tt[v] = k
|
||||
end
|
||||
return tt
|
||||
end
|
||||
|
||||
function Util.find(t, name, value)
|
||||
for k,v in pairs(t) do
|
||||
if v[name] == value then
|
||||
return v, k
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function Util.findAll(t, name, value)
|
||||
local rt = { }
|
||||
for k,v in pairs(t) do
|
||||
if v[name] == value then
|
||||
table.insert(rt, v)
|
||||
end
|
||||
end
|
||||
return rt
|
||||
end
|
||||
|
||||
function Util.shallowCopy(t)
|
||||
local t2 = {}
|
||||
for k,v in pairs(t) do
|
||||
t2[k] = v
|
||||
end
|
||||
return t2
|
||||
end
|
||||
|
||||
function Util.deepCopy(t)
|
||||
if type(t) ~= 'table' then
|
||||
return t
|
||||
end
|
||||
--local mt = getmetatable(t)
|
||||
local res = {}
|
||||
for k,v in pairs(t) do
|
||||
if type(v) == 'table' then
|
||||
v = Util.deepCopy(v)
|
||||
end
|
||||
res[k] = v
|
||||
end
|
||||
--setmetatable(res,mt)
|
||||
return res
|
||||
end
|
||||
|
||||
-- http://snippets.luacode.org/?p=snippets/Filter_a_table_in-place_119
|
||||
function Util.filterInplace(t, predicate)
|
||||
local j = 1
|
||||
|
||||
for i = 1,#t do
|
||||
local v = t[i]
|
||||
if predicate(v) then
|
||||
t[j] = v
|
||||
j = j + 1
|
||||
end
|
||||
end
|
||||
|
||||
while t[j] ~= nil do
|
||||
t[j] = nil
|
||||
j = j + 1
|
||||
end
|
||||
|
||||
return t
|
||||
end
|
||||
|
||||
function Util.filter(it, f)
|
||||
local ot = { }
|
||||
for k,v in pairs(it) do
|
||||
if f(k, v) then
|
||||
ot[k] = v
|
||||
end
|
||||
end
|
||||
return ot
|
||||
end
|
||||
|
||||
function Util.size(list)
|
||||
if type(list) == 'table' then
|
||||
local length = 0
|
||||
table.foreach(list, function() length = length + 1 end)
|
||||
return length
|
||||
end
|
||||
return 0
|
||||
end
|
||||
|
||||
function Util.each(list, func)
|
||||
for index, value in pairs(list) do
|
||||
func(value, index, list)
|
||||
end
|
||||
end
|
||||
|
||||
-- http://stackoverflow.com/questions/15706270/sort-a-table-in-lua
|
||||
function Util.spairs(t, order)
|
||||
local keys = Util.keys(t)
|
||||
|
||||
-- if order function given, sort by it by passing the table and keys a, b,
|
||||
-- otherwise just sort the keys
|
||||
if order then
|
||||
table.sort(keys, function(a,b) return order(t[a], t[b]) end)
|
||||
else
|
||||
table.sort(keys)
|
||||
end
|
||||
|
||||
-- return the iterator function
|
||||
local i = 0
|
||||
return function()
|
||||
i = i + 1
|
||||
if keys[i] then
|
||||
return keys[i], t[keys[i]]
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function Util.first(t, order)
|
||||
local keys = Util.keys(t)
|
||||
if order then
|
||||
table.sort(keys, function(a,b) return order(t[a], t[b]) end)
|
||||
else
|
||||
table.sort(keys)
|
||||
end
|
||||
return keys[1], t[keys[1]]
|
||||
end
|
||||
|
||||
--[[ File functions ]]--
|
||||
function Util.readFile(fname)
|
||||
local f = fs.open(fname, "r")
|
||||
if f then
|
||||
local t = f.readAll()
|
||||
f.close()
|
||||
return t
|
||||
end
|
||||
end
|
||||
|
||||
function Util.writeFile(fname, data)
|
||||
local file = io.open(fname, "w")
|
||||
if not file then
|
||||
error('Unable to open ' .. fname, 2)
|
||||
end
|
||||
file:write(data)
|
||||
file:close()
|
||||
end
|
||||
|
||||
function Util.readLines(fname)
|
||||
local file = fs.open(fname, "r")
|
||||
if file then
|
||||
local t = {}
|
||||
local line = file.readLine()
|
||||
while line do
|
||||
table.insert(t, line)
|
||||
line = file.readLine()
|
||||
end
|
||||
file.close()
|
||||
return t
|
||||
end
|
||||
end
|
||||
|
||||
function Util.writeLines(fname, lines)
|
||||
local file = fs.open(fname, 'w')
|
||||
if file then
|
||||
for _,line in ipairs(lines) do
|
||||
line = file.writeLine(line)
|
||||
end
|
||||
file.close()
|
||||
return true
|
||||
end
|
||||
end
|
||||
|
||||
function Util.readTable(fname)
|
||||
local t = Util.readFile(fname)
|
||||
if t then
|
||||
return textutils.unserialize(t)
|
||||
end
|
||||
end
|
||||
|
||||
function Util.writeTable(fname, data)
|
||||
Util.writeFile(fname, textutils.serialize(data))
|
||||
end
|
||||
|
||||
function Util.loadTable(fname)
|
||||
local fc = Util.readFile(fname)
|
||||
if not fc then
|
||||
return false, 'Unable to read file'
|
||||
end
|
||||
local s, m = loadstring('return ' .. fc, fname)
|
||||
if s then
|
||||
s, m = pcall(s)
|
||||
if s then
|
||||
return m
|
||||
end
|
||||
end
|
||||
return s, m
|
||||
end
|
||||
|
||||
--[[ URL functions ]] --
|
||||
function Util.download(url, filename)
|
||||
local h = http.get(url)
|
||||
if not h then
|
||||
error('Failed to download ' .. url)
|
||||
end
|
||||
local contents = h.readAll()
|
||||
h.close()
|
||||
if not contents then
|
||||
error('Failed to download ' .. url)
|
||||
end
|
||||
|
||||
if filename then
|
||||
Util.writeFile(filename, contents)
|
||||
end
|
||||
return contents
|
||||
end
|
||||
|
||||
function Util.loadUrl(url, env) -- loadfile equivalent
|
||||
local c = Util.download(url)
|
||||
return load(c, url, nil, env)
|
||||
end
|
||||
|
||||
function Util.runUrl(env, url, ...) -- os.run equivalent
|
||||
local fn, m = Util.loadUrl(url, env)
|
||||
if fn then
|
||||
local args = { ... }
|
||||
fn, m = pcall(function() fn(unpack(args)) end)
|
||||
end
|
||||
if not fn and m and m ~= '' then
|
||||
printError(m)
|
||||
end
|
||||
return fn, m
|
||||
end
|
||||
|
||||
--[[ String functions ]] --
|
||||
function Util.toBytes(n)
|
||||
if n >= 1000000 then
|
||||
return string.format('%sM', Util.round(n/1000000, 1))
|
||||
elseif n >= 1000 then
|
||||
return string.format('%sK', Util.round(n/1000, 1))
|
||||
end
|
||||
return tostring(n)
|
||||
end
|
||||
|
||||
function Util.split(str, pattern)
|
||||
pattern = pattern or "(.-)\n"
|
||||
local t = {}
|
||||
local function helper(line) table.insert(t, line) return "" end
|
||||
helper((str:gsub(pattern, helper)))
|
||||
return t
|
||||
end
|
||||
|
||||
function Util.matches(str, pattern)
|
||||
pattern = pattern or '%S+'
|
||||
local t = { }
|
||||
for s in str:gmatch(pattern) do
|
||||
table.insert(t, s)
|
||||
end
|
||||
return t
|
||||
end
|
||||
|
||||
function Util.widthify(s, len)
|
||||
s = s or ''
|
||||
local slen = #s
|
||||
if slen < len then
|
||||
s = s .. string.rep(' ', len - #s)
|
||||
elseif slen > len then
|
||||
s = s:sub(1, len)
|
||||
end
|
||||
return s
|
||||
end
|
||||
|
||||
-- http://snippets.luacode.org/?p=snippets/trim_whitespace_from_string_76
|
||||
function Util.trim(s)
|
||||
return s:find'^%s*$' and '' or s:match'^%s*(.*%S)'
|
||||
end
|
||||
|
||||
-- trim whitespace from left end of string
|
||||
function Util.triml(s)
|
||||
return s:match'^%s*(.*)'
|
||||
end
|
||||
|
||||
-- trim whitespace from right end of string
|
||||
function Util.trimr(s)
|
||||
return s:find'^%s*$' and '' or s:match'^(.*%S)'
|
||||
end
|
||||
-- end http://snippets.luacode.org/?p=snippets/trim_whitespace_from_string_76
|
||||
|
||||
-- word wrapping based on:
|
||||
-- https://www.rosettacode.org/wiki/Word_wrap#Lua and
|
||||
-- http://lua-users.org/wiki/StringRecipes
|
||||
local function splittokens(s)
|
||||
local res = {}
|
||||
for w in s:gmatch("%S+") do
|
||||
res[#res+1] = w
|
||||
end
|
||||
return res
|
||||
end
|
||||
|
||||
local function paragraphwrap(text, linewidth, res)
|
||||
linewidth = linewidth or 75
|
||||
local spaceleft = linewidth
|
||||
local line = {}
|
||||
|
||||
for _, word in ipairs(splittokens(text)) do
|
||||
if #word + 1 > spaceleft then
|
||||
table.insert(res, table.concat(line, ' '))
|
||||
line = { word }
|
||||
spaceleft = linewidth - #word
|
||||
else
|
||||
table.insert(line, word)
|
||||
spaceleft = spaceleft - (#word + 1)
|
||||
end
|
||||
end
|
||||
|
||||
table.insert(res, table.concat(line, ' '))
|
||||
return table.concat(res, '\n')
|
||||
end
|
||||
-- end word wrapping
|
||||
|
||||
function Util.wordWrap(str, limit)
|
||||
|
||||
local longLines = Util.split(str)
|
||||
local lines = { }
|
||||
|
||||
for _,line in ipairs(longLines) do
|
||||
paragraphwrap(line, limit, lines)
|
||||
end
|
||||
|
||||
return lines
|
||||
end
|
||||
|
||||
-- http://lua-users.org/wiki/AlternativeGetOpt
|
||||
local function getopt( arg, options )
|
||||
local tab = {}
|
||||
for k, v in ipairs(arg) do
|
||||
if string.sub( v, 1, 2) == "--" then
|
||||
local x = string.find( v, "=", 1, true )
|
||||
if x then tab[ string.sub( v, 3, x-1 ) ] = string.sub( v, x+1 )
|
||||
else tab[ string.sub( v, 3 ) ] = true
|
||||
end
|
||||
elseif string.sub( v, 1, 1 ) == "-" then
|
||||
local y = 2
|
||||
local l = string.len(v)
|
||||
local jopt
|
||||
while ( y <= l ) do
|
||||
jopt = string.sub( v, y, y )
|
||||
if string.find( options, jopt, 1, true ) then
|
||||
if y < l then
|
||||
tab[ jopt ] = string.sub( v, y+1 )
|
||||
y = l
|
||||
else
|
||||
tab[ jopt ] = arg[ k + 1 ]
|
||||
end
|
||||
else
|
||||
tab[ jopt ] = true
|
||||
end
|
||||
y = y + 1
|
||||
end
|
||||
end
|
||||
end
|
||||
return tab
|
||||
end
|
||||
|
||||
function Util.showOptions(options)
|
||||
print('Arguments: ')
|
||||
for k, v in pairs(options) do
|
||||
print(string.format('-%s %s', v.arg, v.desc))
|
||||
end
|
||||
end
|
||||
|
||||
function Util.getOptions(options, args, ignoreInvalid)
|
||||
local argLetters = ''
|
||||
for _,o in pairs(options) do
|
||||
if o.type ~= 'flag' then
|
||||
argLetters = argLetters .. o.arg
|
||||
end
|
||||
end
|
||||
local rawOptions = getopt(args, argLetters)
|
||||
local argCount = 0
|
||||
|
||||
for k,ro in pairs(rawOptions) do
|
||||
local found = false
|
||||
for _,o in pairs(options) do
|
||||
if o.arg == k then
|
||||
found = true
|
||||
if o.type == 'number' then
|
||||
o.value = tonumber(ro)
|
||||
elseif o.type == 'help' then
|
||||
Util.showOptions(options)
|
||||
return false
|
||||
else
|
||||
o.value = ro
|
||||
end
|
||||
end
|
||||
end
|
||||
if not found and not ignoreInvalid then
|
||||
print('Invalid argument')
|
||||
Util.showOptions(options)
|
||||
return false
|
||||
end
|
||||
end
|
||||
return true, Util.size(rawOptions)
|
||||
|
||||
end
|
||||
|
||||
return Util
|
||||
Reference in New Issue
Block a user