theoriginalbit

[PokeCC] Save/Load code commented

Jun 17th, 2014
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.39 KB | None | 0 0
  1. --[[
  2.   log2 is used to reverse 2^x this is basic math I wont explain this
  3. --]]
  4. local function log2(colour)
  5.   return math.log(colour) / math.log(2)
  6. end
  7.  
  8. --[[
  9.   iterates through a 2D table, finding the longest row
  10.   this is so we can calculate the full width and height of a 2D table, incase some rows have more columns than others
  11. --]]
  12. local function findLongest( t )
  13.   local l = #t[1]
  14.   for _,v in pairs( t ) do
  15.     l = math.max( l, #v )
  16.   end
  17.   return l
  18. end
  19.  
  20. --[[
  21.   Creates a single byte from a colour and a true/false value
  22.   a byte is 8 bits, we can store a colour in 4 bytes (a nibble) and a boolean in 1 bit
  23.   this means that we have 3 spare bits in the nibble we're using to store the state,
  24.   you could use this for other states if you wish
  25. --]]
  26. local function buildByte( color, state )
  27.   --# turn the boolean into a number and then shift it 4 places to the left so its a high nibble
  28.   --# so we have 00000001 to begin, but after shifting we have 00010000
  29.   local high = bit.blshift(state == true and 1 or 0, 4)
  30.   --# turn the colour into a number 0-15
  31.   local low = log2( color )
  32.   --# combine the low and high bits together into a byte
  33.   return bit.bor( high, low )
  34. end
  35.  
  36. --[[
  37.   unpacks a single byte back into a colour and a true/false value
  38. --]]
  39. local function unpackByte( byte )
  40.   --# shift the boolean value back into a range, so lets assume we have a byte that is colors.red and true
  41.   --# the binary would be 00011110 performing this shift would make it 00000001 which is obviously what we
  42.   --# want 'cause that's how we saved our boolean
  43.   local high = bit.brshift( byte, 4)
  44.   --# read the colour, assuming the same example with 00011110 applying a bitwise AND of 00001111 to it would
  45.   --# result in 00001110 which is colors.red
  46.   local low = bit.band( byte, 0xF )
  47.   --# return the table in the form it was, converting the high bit back to a boolean by checking it its 00000001
  48.   return { c = 2^low, s = high == 1 }
  49. end
  50.  
  51. --[[
  52.   Save the map to a file
  53. --]]
  54. function save(filename, map)
  55.   --# open the file in binary writing mode
  56.   local file = fs.open(filename, "wb")
  57.   file.write(#map) --# height
  58.   file.write(findLongest(map)) --# width
  59.   --# iterate over the 2D table
  60.   for _,row in pairs(map) do
  61.     for _,col in pairs(row) do
  62.       --# write out the byte
  63.       file.write(buildByte(col.c, col.s))
  64.     end
  65.     --# make note that we dont make a newline here, newlines are useless in binary mode
  66.     --# we will have to deal with this in `load`
  67.   end
  68.   --# done
  69.   file.close()
  70. end
  71.  
  72. --[[
  73.   Load the map to a file
  74. --]]
  75. function load(filename)
  76.   --# the map
  77.   local map = {}
  78.   --# open the file in binary read mode
  79.   local file = fs.open(filename, "rb")
  80.   --# read the width and height
  81.   local height = file.read()
  82.   local width = file.read()
  83.   --# tracking information, `bc` is the number of bytes we've read
  84.   local row, col, bc = 0,0,0
  85.   --# read the file, 1 byte at a time
  86.   for byte in file.read do
  87.     --# calculate which row and column it belongs in
  88.     row, col = (bc % height) + 1, (bc % width) + 1
  89.     --# if there is no table for this row, make it
  90.     map[row] = map[row] or {}
  91.     --# unpack the byte into the row/column
  92.     map[row][col] = unpackByte(byte)
  93.     --# increment, we've read the byte
  94.     bc = bc + 1
  95.   end
  96.   --# return the map and close the file, file.close returns nil, so you wont get a value from this
  97.   return map, file.close()
  98. end
Advertisement
Add Comment
Please, Sign In to add comment