Skip to content
Graylin Kim edited this page Jan 18, 2012 · 1 revision

Bit shifts in replay files

(WIP)

Basic shift

Replay files try to use the least amount of space necessary. This means in a couple of cases no bit gets left behind. For this, the file goes into a bit-shifted read. For example, when only 2 bits are needed for some flags, the rightmost 2 bits get read and then the whole stream jumps into a shifted mode. The basic operation for this is as follows:

BBBBBBAA
CCCCCCBB
DDDDDDCC

AA are our original two bits used for some flag. The rest of the byte gets combined with the rightmost 2 bits of the next byte. The other 6 bits get combined with the rightmost 2 of the next byte, and so on, and so on... This is how you read single bytes.

Single byte shifted reads

If you need just a couple of bits, you can just increase the bitshift and snoop a couple of extra bits from the right (XXX: need to double-check this). It might byte-align the stream again if you need all of the carry-over bits (BBBBBB in the previous example)

Multibyte shifted reads

If you need more than 8 bits, you have two choices: either you can just snoop a few extra from the next byte you are snooping from and just increase the bitshift, or you really need to read multiple bytes.

9-(16-current shift) bit read

For the first case, assume we have 2bit-shifted stream like earlier and we need 12 bits, we just snoop those extra and leave the stream in a 6bit-shifted mode:

AAAAAA??
..aaaaAA

The bitstream read will be AAAAAAaaaaAA. (?? is the already-in-place 2bit-shift, .. is the carry for the next byte). If we had wanted 14 bits, we would have byte-aligned the stream again.

More than a few bits

If you need more and going over a second byte-boundary, things get a bit different, but not that much. As long as you need more than the bits handled in the above cases, you just wrap the stream like a normal person and tidy the stream up for a normal shift O:-) An example of a 3-byte read in a 3bit-shifted stream:

AAAAA???
AAABBBBB
BBBCCCCC
.....CCC

As you can see A and B are just wrapped. Be aware that A starts at the left part of the first byte and ??? is already taken for the byte before that. When you read the last byte, you need to make sure you read the RIGHT (pun intended) bits, so the stream is 3bit-shifted normally again.

Types of multibyte data

Bitmasks

Bitmasks are dynamically sized, so just read it :-)

Coordinates

Coordinates X and Y are read as a 20-bit structure (so they increase the shift of the stream by 4 when it is tidied up). First X is read, then Y.

Object IDs

Object IDs are 32-bit integers

Object types

Object types are 16-bit integers. In case of the selection event they are followed by 1 byte indicating a hallucination.