71 lines
2.3 KiB
Lua
71 lines
2.3 KiB
Lua
|
|
local ts = ts or {
|
|
call = function() end,
|
|
eval = function() end,
|
|
}
|
|
|
|
ts.eval [[
|
|
function commandShiftBrick(%x, %y, %z) { commandToServer('shiftBrick', %x, %y, %z); }
|
|
function commandPlantBrick() { commandToServer('plantBrick'); }
|
|
]]
|
|
local function plantBrickAt(brickpos, pos)
|
|
local dx, dy, dz = pos[1]-brickpos[1], pos[2]-brickpos[2], pos[3]-brickpos[3]
|
|
ts.call("commandShiftBrick", dy, -dx, dz)
|
|
ts.call("commandPlantBrick")
|
|
brickpos[1], brickpos[2], brickpos[3] = pos[1], pos[2], pos[3]
|
|
end
|
|
local function buildMemory(mem, romsize, offset, len)
|
|
if type(romsize)~="table" or #romsize<3 then error("You must specify a ROM size.") end
|
|
offset = offset or 0
|
|
|
|
local rombytes = romsize[1]*romsize[2]*romsize[3]/8
|
|
if len and len>rombytes then error("rom not big enough to hold "..len.." bytes (holds "..rombytes..")") end
|
|
if not len then
|
|
for i = 0, 0xFFFF do
|
|
if mem[i] and (i<offset or i>=offset+rombytes) then error("memory does not fit in rom at addr "..string.format("%04X", i)) end
|
|
end
|
|
end
|
|
|
|
local brickpos = {0, 0, 0}
|
|
for x = 0, romsize[1]-1 do
|
|
for y = 0, romsize[2]-1 do
|
|
for z = 0, romsize[3]-1 do
|
|
local addr = offset + ((romsize[3]/8)*(x + y*romsize[1]) + math.floor(z/8))
|
|
local pow = math.pow(2, z%8)
|
|
local data = (addr>=offset and ((not len) or addr<offset+len) and mem[addr]) or 0
|
|
local bit = math.floor(data/pow)%2
|
|
if bit==1 then plantBrickAt(brickpos, {x, -y, z}) end
|
|
end
|
|
end
|
|
end
|
|
end
|
|
|
|
local function memFromBinFile(fn)
|
|
local fi = io.open(fn, "rb") or error("Could not open file: \""..fn.."\"")
|
|
local text = fi:read("*a")
|
|
fi:close()
|
|
|
|
local mem = {}
|
|
--local seenNonzero = false
|
|
for i = 1, #text do
|
|
local c = text:sub(i, i):byte()
|
|
--if c~=0 then seenNonzero = true end
|
|
--if seenNonzero then
|
|
-- mem[i-1] = c
|
|
--end
|
|
if c~=0 then mem[i-1] = c end
|
|
end
|
|
return mem
|
|
end
|
|
|
|
local function strtovec(str) local v = {}; for word in str:gmatch("[^ \t\r\n]+") do table.insert(v, tonumber(word)) end; return v; end
|
|
|
|
function BuildBinFile(fn, romsizes, offsets, lens) local offset = tonumber(offsets); local len = tonumber(lens); local romsize = strtovec(romsizes);
|
|
local mem = memFromBinFile(fn)
|
|
buildMemory(mem, romsize, offset, len)
|
|
end
|
|
|
|
ts.eval [[
|
|
function BuildBinFile(%fn, %romsize, %offset, %len) { luacall("BuildBinFile", strReplace(%fn, "$", "Add-ons/_misc/rom/8608programs/"), %romsize, %offset, %len); }
|
|
]]
|