-
Notifications
You must be signed in to change notification settings - Fork 2
/
terrain.c
159 lines (139 loc) · 3.25 KB
/
terrain.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#include <stdint.h>
#include <assert.h>
#include <ctype.h>
#include <string.h>
#include "terrain.h"
// static void printbytes(uint8_t *ptr, size_t length) {
// for (int i = 0; i < length; ++i) {
// uint8_t c = ptr[i];
// if (isprint(c)) {
// printf("%02X ", c);
// } else {
// printf("%02X ", c);
// }
// }
// printf("\n");
// }
int32_t read_int32(uint8_t **ptr) {
int32_t value = *((int32_t*)(*ptr));
*ptr += 4;
return value;
}
int16_t read_int16(uint8_t **ptr) {
int16_t value = *((int16_t*)(*ptr));
*ptr += 2;
return value;
}
int8_t read_int8(uint8_t **ptr) {
int8_t value = *((int8_t*)(*ptr));
*ptr += 1;
return value;
}
static uint8_t read_uint8(uint8_t **ptr) {
uint8_t value = *((uint8_t*)(*ptr));
*ptr += 1;
return value;
}
static uint16_t read_uint16(uint8_t **ptr) {
uint16_t value = *((uint16_t*)(*ptr));
*ptr += 2;
return value;
}
uint16_t reverse_uint16(uint16_t value) {
return ((value & 0xFF00) >> 8) | ((value & 0x00FF) << 8);
}
const char *byte_to_binary(uint8_t x) {
static char b[9];
b[0] = '\0';
int z;
for (z = 128; z > 0; z >>= 1) {
strcat(b, ((x & z) == z) ? "1" : "0");
}
return b;
}
uint8_t block_type(uint8_t d0) {
return (d0 & 0x38) >> 3;
}
uint8_t block_rot(uint8_t d0) {
return (d0 & 0xC0) >> 6;
}
void read_segment_d0(uint8_t **ptr, uint16_t *left) {
uint8_t tag = read_uint8(ptr);
if (tag == 0x28) {
uint16_t toskip = read_uint8(ptr);
if (toskip == 0xFF) {
toskip = reverse_uint16(read_uint16(ptr));
}
printf("Empty Segment [0x%04X]\n", toskip);
assert(*left >= toskip);
*left -= toskip;
} else {
uint8_t d0 = tag;
uint16_t length_or_tag = read_uint8(ptr);
uint16_t length;
if (length_or_tag == 0xFF) {
length = reverse_uint16(read_uint16(ptr));
} else {
length = length_or_tag;
}
printf("Normal Segment [0x%04X] %s: (block: %u, rot: %u)\n",
length,
byte_to_binary(d0),
block_type(d0),
block_rot(d0));
assert(*left >= length);
*left -= length;
}
}
void read_segment_d1(uint8_t **ptr, uint16_t *left) {
uint8_t tag = read_uint8(ptr);
if (tag == 0x11) {
uint16_t toskip = read_uint8(ptr);
if (toskip == 0xFF) {
toskip = reverse_uint16(read_uint16(ptr));
}
printf("Empty Segment [0x%04X]\n", toskip);
assert(*left >= toskip);
*left -= toskip;
} else {
uint8_t d0 = tag;
uint16_t length_or_tag = read_uint8(ptr);
uint16_t length;
if (length_or_tag == 0xFF) {
length = reverse_uint16(read_uint16(ptr));
} else {
length = length_or_tag;
}
printf("Normal Segment [0x%04X] %s: (material: %u)\n",
length,
byte_to_binary(d0),
d0);
assert(*left >= length);
*left -= length;
}
}
void read_chunk(uint8_t **ptr) {
struct terrain_chunk chunk = {0};
chunk.position_x = read_int16(ptr);
chunk.position_y = read_int16(ptr);
chunk.position_z = read_int16(ptr);
uint16_t left = 0x4000;
printf("Pos: %d, %d, %d\n",
chunk.position_x,
chunk.position_y,
chunk.position_z);
while (left > 0) {
read_segment_d0(ptr, &left);
}
for (int i = 0; i < 4; ++i)
printf("==================================================\n");
left = 0x4000;
while (left > 0) {
read_segment_d1(ptr, &left);
}
}
struct rbx_terrain *translate_terrain(struct rbx_string *source) {
uint8_t *ptr = source->data;
read_chunk(&ptr);
return NULL;
}