Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local function log2(colour)
- return math.log(colour) / math.log(2)
- end
- local function findLongest( t )
- local l = #t[1]
- for _,v in pairs( t ) do
- l = math.max( l, #v )
- end
- return l
- end
- local function buildByte( color, state )
- --# in the high nibble we have 3 extra bits that could be used to store other small data
- local high = bit.band( state == true and 1 or 0, 0xF )
- local low = bit.band( log2( color ), 0xF )
- return bit.bor( bit.blshift( high, 4 ), low )
- end
- local function unpackByte( byte )
- local high = bit.brshift( byte, 4)
- local low = bit.band( byte, 0xF )
- return { c = 2^low, s = high == 1 }
- end
- function save(filename, map)
- local file = fs.open(filename, "wb")
- file.write(#map) --# height
- file.write(findLongest(map)) --# width
- for _,row in pairs(map) do
- for _,col in pairs(row) do
- file.write(buildByte(col.c, col.s))
- end
- end
- file.close()
- end
- function load(filename)
- local map = {}
- local file = fs.open(filename, "rb")
- local width = file.read()
- local height = file.read()
- local row, col, bc = 0,0,0
- for byte in file.read do
- row, col = (bc % width) + 1, (bc % height) + 1
- map[row] = map[row] or {}
- map[row][col] = unpackByte(byte)
- bc = bc + 1
- end
- return map, file.close()
- end
Advertisement
Add Comment
Please, Sign In to add comment