Advertisement
ForeverZer0

RMXP Table Class

Feb 16th, 2012
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 1.41 KB | None | 0 0
  1. class Table
  2.    def initialize(x, y = 1, z = 1)
  3.       @xsize, @ysize, @zsize = x, y, z
  4.       @data = Array.new(x * y * z, 0)
  5.    end
  6.    def [](x, y = 0, z = 0)
  7.       @data[x + y * @xsize + z * @xsize * @ysize]
  8.    end
  9.    def []=(*args)
  10.       x = args[0]
  11.       y = args.size > 2 ? args[1] :0
  12.       z = args.size > 3 ? args[2] :0
  13.       v = args.pop
  14.       @data[x + y * @xsize + z * @xsize * @ysize] = v
  15.    end
  16.    def _dump(d = 0)
  17.       s = [3].pack('L')
  18.       s += [@xsize].pack('L') + [@ysize].pack('L') + [@zsize].pack('L')
  19.       s += [@xsize * @ysize * @zsize].pack('L')
  20.       for z in 0...@zsize
  21.          for y in 0...@ysize
  22.             for x in 0...@xsize
  23.                s += [@data[x + y * @xsize + z * @xsize * @ysize]].pack('S')
  24.             end
  25.          end
  26.       end
  27.       s
  28.    end
  29.    def self._load(s)
  30.       size = s[0, 4].unpack('L')[0]
  31.       nx = s[4, 4].unpack('L')[0]
  32.       ny = s[8, 4].unpack('L')[0]
  33.       nz = s[12, 4].unpack('L')[0]
  34.       data = []
  35.       pointer = 20
  36.       loop do
  37.          data.push(*s[pointer, 2].unpack('S'))
  38.          pointer += 2
  39.          break if pointer > s.size - 1
  40.       end
  41.       t = Table.new(nx, ny, nz)
  42.       n = 0
  43.       for z in 0...nz
  44.          for y in 0...ny
  45.             for x in 0...nx
  46.                t[x, y, z] = data[n]
  47.                n += 1
  48.             end
  49.          end
  50.       end
  51.       t
  52.    end
  53.    attr_reader(:xsize, :ysize, :zsize, :data)
  54. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement