ScenarioFiles:Encoding
Scenario files (.SC5) and save game files (.SV5) in Locomotion uses the same encodings methods as Rollercoaster Tycoon 2 (.SC6/.SV6). There are three kinds of encoding; Run-length encoding, String encoding, and Scramble.
Run-length Encoding
There are two types of sequences, selected by the value of the first byte.
First byte (N) | Meaning |
---|---|
0x00 ≤ N ≤ 0x7F
|
Copy the next N+1 bytes as they are. |
0x80 ≤ N ≤ 0xFF
|
Repeat the next 1 byte 257-N times. |
As an example, concider the encoded byte sequence 00 57 FD 65 01 20 48 FE 61 00 21
.
Encoded | Decoded | Comment |
---|---|---|
00 57
|
57
|
Copy 1 byte. |
FD 65
|
65 65 65 65
|
Repeat the byte 4 times. |
01 20 48
|
20 48
|
Copy 2 bytes. |
FE 61
|
61 61 61
|
Repeat the byte 3 times. |
00 21
|
21
|
Copy 1 byte. |
To encode data into RLE, you need to look for sequences of bytes with the same value, or with pairwise distinct values. Let's concider the string "Weeee Haaa!" again. Below I have separated the bytes into groups of distinct and constant values.
57
|
65 65 65 65
|
20 48
|
61 61 61
|
21
|
You can see how this maps to the values in the table before.
String encoding
For the encoding type 0x02
, after you have decoded the RLE, you still are left with one layer of encoding. String encoding is a simple variant of LZ compression. As with RLE, there is one prefix byte, but it's bits is split into two parts.
High 5 bits (O) | Low 3 bits (N) | Meaning |
---|---|---|
11111b
|
111b
|
Copy the next byte of input to the output. |
*
|
*
|
Repeat N+1 bytes of output, starting O-32 bytes from current position. |
It might be good to note that the bytes that are to be copied can span past the current output position. If you copy one byte at the time, the output will get longer, so it is not an error.
As an example, let's go with "Weeee Haaa!" again, since it has much redundancy. It can be encoded as FF 57 FF 65 FA FF 20 FF 48 FF 61 F9 FF 21
.
Encoded | Decoded | Comment |
---|---|---|
FF 57
|
57
|
Add 1 byte. |
FF 6B
|
6B
|
Add 1 byte. |
FB
|
6B 6B 6B
|
FB = 11111 010 . Repeat 3 bytes, starting from the last.
|
FF 20
|
20
|
Add 1 byte. |
FF 48
|
48
|
Add 1 byte. |
FF 61
|
61
|
Add 1 byte. |
F9
|
61 61
|
F9 = 11111 001 . Repeat 2 bytes, starting from the last.
|
The encoding process for this method is much slower than for RLE, as the encoder have to search for (or possibly cache) possible byte sequences to repeat. Since repetition only requires one byte, it is always better to use repetition whenever it is possible. One thing to be careful about, is to not encode a sequence of 8 or more equal bytes as FF (repeat 8 bytes, starting from the last), since that is the code for copying one byte from the input to the output.
Scramble
The only purpose of this method is to make the content harder to read. The bits of each byte is rotated an odd number of bits, depending on the bytes position within the string.
Position | Encoded bits | Decoded bits |
---|---|---|
+4*N+0
|
AAAAAAA B
|
B AAAAAAA
|
+4*N+1
|
AAAAA BBB
|
BBB AAAAA
|
+4*N+2
|
AAA BBBBB
|
BBBBB AAA
|
+4*N+3
|
A BBBBBBB
|
BBBBBBB A
|
As an example, let's go with "Weeee Haaa!" again.
Encoded | AE
|
2B
|
AC
|
B2
|
CA
|
01
|
09
|
B0
|
C2
|
0B
|
24
|
---|---|---|---|---|---|---|---|---|---|---|---|
Rotation | 1 | 3 | 5 | 7 | 1 | 3 | 5 | 7 | 1 | 3 | 5 |
Decoded | 57
|
65
|
65
|
65
|
65
|
20
|
48
|
61
|
61
|
61
|
21
|