This repository has been archived by the owner on Dec 30, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchunk.c
386 lines (319 loc) · 11.9 KB
/
chunk.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
/*
chunk.c - Chunk definition
Provides functionality for Chunk objects
*/
#include <Python.h>
#include <structmember.h>
#include <stdbool.h>
#include "minecraft.h"
#include "tags.h"
// Takes a region file stream and a chunk location and finds and decompresses
// the chunk to the passed buffer
int decompress_chunk( unsigned char *region, unsigned char *decompressed, int x, int z )
{
unsigned int header_offset, chunk_offset, chunk_length, compression_type;
int rc;
printf("Finding chunk (%d, %d)\n", x, z);
header_offset = 4 *((x & 31) + (z & 31) * 32);
chunk_offset = swap_endianness(region + header_offset, 3) * 4096;
if ( chunk_offset == 0 )
{
printf("Chunk is empty, crap!\n");
return 1;
}
printf("Offset: %d | Length: %d\n", chunk_offset, *(region + header_offset + 3) * 4096);
// Read the chunk length from the start of the chunk
chunk_length = swap_endianness(region + chunk_offset, 4);
compression_type = *(region + chunk_offset + 4);
printf("True Length: %d | Compression: %d\n", chunk_length, compression_type);
rc = inf(decompressed, region + chunk_offset + 5, chunk_length - 1, 0);
if( rc < 0 )
{
PyErr_Format(PyExc_Exception, "Couldn't decompress!");
return -1;
}
return 0;
}
/*
Python object-related code
*/
void Chunk_dealloc( Chunk *self )
{
Py_XDECREF(self->world);
Py_XDECREF(self->dict);
self->ob_type->tp_free((PyObject *) self);
}
int Chunk_init( Chunk *self, PyObject *args, PyObject *kwds )
{
Region *region;
PyObject *old, *dict, *world;
unsigned char *buffer; // TODO: Dynamically allocate
int moved, rc;
if( !PyArg_ParseTuple(args, "Oii", &world, &self->x, &self->z) )
return -1;
buffer = calloc(1000000, 1);
region = load_region((World *) world, self->x >> 5, self->z >> 5);
rc = decompress_chunk(region->buffer, buffer, self->x, self->z);
if( rc != 0 )
{
PyErr_Format(PyExc_Exception, "CHUNK EMPTY!");
dump_buffer(buffer, 480);
return -1;
}
// dump_buffer(buffer, 4800);
// Read chunk to dictionary
moved = 0;
old = self->dict;
dict = get_tag(buffer, -1, &moved);
printf("Chunk moved: %d\n", moved);
Py_INCREF(dict);
self->dict = dict;
Py_XDECREF(old);
// Add reference to chunk's parent, the world
old = self->world;
Py_INCREF(world);
self->world = world;
Py_XDECREF(old);
free(buffer);
return 0;
}
static PyObject *Chunk_save( Chunk *self )
{
Region *region;
// Load the region, making sure we convert from chunk to region coordinates
region = load_region((World *) self->world, self->x >> 5, self->z >> 5);
update_region(region, self);
Py_INCREF(Py_None);
return Py_None;
}
char get_nibble( char *byte_array, int index )
{
return index % 2 == 0 ? byte_array[index / 2] & 0x0F : byte_array[index / 2]>>4 & 0x0F;
}
void set_nibble( char *byte_array, int index, unsigned char value )
{
char existing;
value = value & 0x0F; // Ensure only the lower 4 bits are set
value = index % 2 == 0 ? value : value << 4;
existing = index % 2 == 0 ? byte_array[index / 2] & 0xF0 : byte_array[index / 2] & 0x0F;
byte_array[index / 2] = existing | value;
}
// Currently, it is expected that passed arguments will be in coordinates
// relative to the chunk
PyObject *Chunk_get_block( Chunk *self, PyObject *args )
{
PyObject *block_args, *block, *level, *sections, *section;
int i, size, x, y, z;
if( !PyArg_ParseTuple(args, "iii", &x, &y, &z) )
{
PyErr_Format(PyExc_Exception, "Unable to parse arguments.");
Py_INCREF(Py_None);
return Py_None;
}
level = PyDict_GetItemString(self->dict, "Level");
sections = PyDict_GetItemString(level, "Sections");
size = PyList_Size(sections);
section = NULL;
for( i = 0; i < size; i++ )
{
PyObject *current_section;
int sub_y;
current_section = PyList_GetItem(sections, i);
sub_y = PyInt_AsLong(PyDict_GetItemString(current_section, "Y"));
if( sub_y == y / 16 )
{
section = current_section;
break;
}
}
if( section == NULL )
block_args = Py_BuildValue("HBBB", 0, 0, 0, 0);
else
{
int section_y, position;
unsigned short id;
unsigned char data, blocklight, skylight;
char *byte_array;
data = blocklight = skylight = 0;
section_y = y % 16;
position = section_y * 16 * 16 + z * 16 + x;
// We know it will contain the "Blocks" byte array
byte_array = (char *) PyDict_GetItemString(section, "Blocks");
id = (short) byte_array[position];
if( PyDict_Contains(section, PyString_FromString("Add")) )
{
byte_array = (char *) PyDict_GetItemString(section, "Add");
id += get_nibble(byte_array, position) << 8;
}
if( PyDict_Contains(section, PyString_FromString("Data")) )
{
byte_array = (char *) PyDict_GetItemString(section, "Data");
data = get_nibble(byte_array, position);
}
if( PyDict_Contains(section, PyString_FromString("BlockLight")) )
{
byte_array = (char *) PyDict_GetItemString(section, "BlockLight");
blocklight = get_nibble(byte_array, position);
}
if( PyDict_Contains(section, PyString_FromString("SkyLight")) )
{
byte_array = (char *) PyDict_GetItemString(section, "SkyLight");
skylight = get_nibble(byte_array, position);
}
block_args = Py_BuildValue("HBBB", id, data, blocklight, skylight);
}
// TODO: Populate with actual block values
block_args = Py_BuildValue("HBBB", 0, 0, 0, 0);
block = PyObject_CallObject((PyObject *) &minecraft_BlockType, block_args);
Py_INCREF(block);
return block;
}
// Currently, it is expected that passed arguments will be in coordinates
// relative to the chunk
PyObject *Chunk_put_block( Chunk *self, PyObject *args )
{
PyObject *level, *sections, *section;
Block *block;
int i, size, position, x, y, z;
char *byte_array;
if( !PyArg_ParseTuple(args, "iiiO", &x, &y, &z, &block) )
{
PyErr_Format(PyExc_Exception, "Unable to parse arguments.");
Py_INCREF(Py_None);
return Py_None;
}
level = PyDict_GetItemString(self->dict, "Level");
sections = PyDict_GetItemString(level, "Sections");
size = PyList_Size(sections);
section = NULL;
for( i = 0; i < size; i++ )
{
PyObject *current_section;
int sub_y;
current_section = PyList_GetItem(sections, i);
sub_y = PyInt_AsLong(PyDict_GetItemString(current_section, "Y"));
if( sub_y == y / 16 )
{
section = current_section;
break;
}
}
// If a section doesn't exist where this block should go, create it
if( section == NULL )
{
PyObject *new;
new = PyInt_FromLong(y / 16); // This should end up not needing to be INCREF'd I think
printf("Creating new section (%ld)\n", PyInt_AsLong(new));
section = PyDict_New();
Py_INCREF(section);
Py_INCREF(new);
PyDict_SetItemString(section, "Y", new);
// Add doesn't necessarily exist, so we don't need to account for it
// Data
byte_array = calloc(4096, 1);
new = PyByteArray_FromStringAndSize(byte_array, 4096);
Py_INCREF(new);
PyDict_SetItemString(section, "Blocks", new);
free(byte_array);
// Data, BlockLight, and SkyLight
byte_array = calloc(2048, 1);
new = PyByteArray_FromStringAndSize(byte_array, 2048);
Py_INCREF(new);
PyDict_SetItemString(section, "Data", new);
new = PyByteArray_FromStringAndSize(byte_array, 2048);
Py_INCREF(new);
PyDict_SetItemString(section, "BlockLight", new);
new = PyByteArray_FromStringAndSize(byte_array, 2048);
Py_INCREF(new);
PyDict_SetItemString(section, "SkyLight", new);
free(byte_array);
PyList_Append(sections, section);
}
position = (y % 16) * 16 * 16 + z * 16 + x;
byte_array = PyByteArray_AsString(PyDict_GetItemString(section, "Blocks"));
byte_array[position] = block->id;
// set "Add", only if the block ID is greater than 0x0F
if( block->id >> 8 != 0 )
{
if( PyDict_Contains(section, PyString_FromString("Add")) == 0 )
{
PyObject *new;
byte_array = calloc(2048, 1);
new = PyByteArray_FromStringAndSize(byte_array, 2048);
Py_INCREF(new);
PyDict_SetItemString(section, "Add", new);
free(byte_array);
}
byte_array = PyByteArray_AsString(PyDict_GetItemString(section, "Add"));
set_nibble(byte_array, position, block->data >> 8);
}
byte_array = PyByteArray_AsString(PyDict_GetItemString(section, "Data"));
set_nibble(byte_array, position, block->data);
byte_array = PyByteArray_AsString(PyDict_GetItemString(section, "BlockLight"));
set_nibble(byte_array, position, block->blocklight);
byte_array = PyByteArray_AsString(PyDict_GetItemString(section, "SkyLight"));
set_nibble(byte_array, position, block->skylight);
Py_INCREF(Py_None);
return Py_None;
}
// TODO: Placeholder
// Recalculate lighting and anything else that requires calculation
static PyObject *Chunk_calculate( Chunk *self )
{
Py_INCREF(Py_None);
return Py_None;
}
static PyMemberDef Chunk_members[] = {
{"world", T_OBJECT, offsetof(Chunk, world), 0, "World the chunk lives in"},
{"dict", T_OBJECT, offsetof(Chunk, dict), 0, "Chunk attribute dictionary"},
{"x", T_INT, offsetof(Chunk, x), 0, "Chunk X position"},
{"z", T_INT, offsetof(Chunk, z), 0, "Chunk Z position"},
{NULL}
};
static PyMethodDef Chunk_methods[] = {
{"save", (PyCFunction) Chunk_save, METH_NOARGS, "Save the chunk to file"},
{"get_block", (PyCFunction) Chunk_get_block, METH_VARARGS, "Get a block from within the chunk"},
{"put_block", (PyCFunction) Chunk_put_block, METH_VARARGS, "Put a block into the chunk, at the given location"},
{NULL}
};
PyTypeObject minecraft_ChunkType = {
PyObject_HEAD_INIT(NULL)
0, /*ob_size*/
"minecraft.Chunk", /*tp_name*/
sizeof(Chunk), /*tp_basicsize*/
0, /*tp_itemsize*/
(destructor)Chunk_dealloc, /*tp_dealloc*/
0, /*tp_print*/
0, /*tp_getattr*/
0, /*tp_setattr*/
0, /*tp_compare*/
0, /*tp_repr*/
0, /*tp_as_number*/
0, /*tp_as_sequence*/
0, /*tp_as_mapping*/
0, /*tp_hash */
0, /*tp_call*/
0, /*tp_str*/
0, /*tp_getattro*/
0, /*tp_setattro*/
0, /*tp_as_buffer*/
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
"Chunk objects", /* tp_doc */
0, /* tp_traverse */
0, /* tp_clear */
0, /* tp_richcompare */
0, /* tp_weaklistoffset */
0, /* tp_iter */
0, /* tp_iternext */
Chunk_methods, /* tp_methods */
Chunk_members, /* tp_members */
0, /* tp_getset */
0, /* tp_base */
0, /* tp_dict */
0, /* tp_descr_get */
0, /* tp_descr_set */
0, /* tp_dictoffset */
(initproc)Chunk_init, /* tp_init */
0, /* tp_alloc */
0, /* tp_new */
};