0

all sections now loaded for each chunk, cave mode now works

This commit is contained in:
Aaron Griffith
2012-02-21 14:22:52 -05:00
parent 187b2690f4
commit 136ea70281
3 changed files with 66 additions and 200 deletions

View File

@@ -122,7 +122,6 @@ static inline void load_chunk_section(ChunkData *dest, int i, PyObject *section)
*/ */
int load_chunk(RenderState* state, int x, int z, unsigned char required) { int load_chunk(RenderState* state, int x, int z, unsigned char required) {
ChunkData *dest = &(state->chunks[1 + x][1 + z]); ChunkData *dest = &(state->chunks[1 + x][1 + z]);
int y = state->chunky;
int i; int i;
PyObject *chunk = NULL; PyObject *chunk = NULL;
PyObject *sections = NULL; PyObject *sections = NULL;
@@ -157,7 +156,7 @@ int load_chunk(RenderState* state, int x, int z, unsigned char required) {
} }
/* set up reasonable defaults */ /* set up reasonable defaults */
for (i = 0; i < 3; i++) for (i = 0; i < SECTIONS_PER_CHUNK; i++)
{ {
dest->sections[i].blocks = NULL; dest->sections[i].blocks = NULL;
dest->sections[i].data = NULL; dest->sections[i].data = NULL;
@@ -167,16 +166,15 @@ int load_chunk(RenderState* state, int x, int z, unsigned char required) {
for (i = 0; i < PySequence_Fast_GET_SIZE(sections); i++) { for (i = 0; i < PySequence_Fast_GET_SIZE(sections); i++) {
PyObject *ycoord = NULL; PyObject *ycoord = NULL;
int rely = 0; int sectiony = 0;
PyObject *section = PySequence_Fast_GET_ITEM(sections, i); PyObject *section = PySequence_Fast_GET_ITEM(sections, i);
ycoord = PyDict_GetItemString(section, "Y"); ycoord = PyDict_GetItemString(section, "Y");
if (!ycoord) if (!ycoord)
continue; continue;
rely = PyInt_AsLong(ycoord) + 1 - y; sectiony = PyInt_AsLong(ycoord);
if (rely >= 0 && rely < 3) { if (sectiony >= 0 && sectiony < SECTIONS_PER_CHUNK)
load_chunk_section(dest, rely, section); load_chunk_section(dest, sectiony, section);
}
} }
Py_DECREF(sections); Py_DECREF(sections);
@@ -451,7 +449,7 @@ chunk_render(PyObject *self, PyObject *args) {
Py_DECREF(blockmap); Py_DECREF(blockmap);
return NULL; return NULL;
} }
if (state.chunks[1][1].sections[1].blocks == NULL) { if (state.chunks[1][1].sections[state.chunky].blocks == NULL) {
/* this section doesn't exist, let's skeddadle */ /* this section doesn't exist, let's skeddadle */
render_mode_destroy(rendermode); render_mode_destroy(rendermode);
Py_DECREF(blockmap); Py_DECREF(blockmap);
@@ -459,8 +457,8 @@ chunk_render(PyObject *self, PyObject *args) {
} }
/* set blocks_py, state.blocks, and state.blockdatas as convenience */ /* set blocks_py, state.blocks, and state.blockdatas as convenience */
blocks_py = state.blocks = state.chunks[1][1].sections[1].blocks; blocks_py = state.blocks = state.chunks[1][1].sections[state.chunky].blocks;
state.blockdatas = state.chunks[1][1].sections[1].data; state.blockdatas = state.chunks[1][1].sections[state.chunky].data;
/* set up the random number generator again for each chunk /* set up the random number generator again for each chunk
so tallgrass is in the same place, no matter what mode is used */ so tallgrass is in the same place, no matter what mode is used */
@@ -575,7 +573,7 @@ chunk_render(PyObject *self, PyObject *args) {
for (j = 0; j < 3; j++) { for (j = 0; j < 3; j++) {
if (state.chunks[i][j].loaded) { if (state.chunks[i][j].loaded) {
int k; int k;
for (k = 0; k < 3; k++) { for (k = 0; k < SECTIONS_PER_CHUNK; k++) {
Py_XDECREF(state.chunks[i][j].sections[k].blocks); Py_XDECREF(state.chunks[i][j].sections[k].blocks);
Py_XDECREF(state.chunks[i][j].sections[k].data); Py_XDECREF(state.chunks[i][j].sections[k].data);
Py_XDECREF(state.chunks[i][j].sections[k].skylight); Py_XDECREF(state.chunks[i][j].sections[k].skylight);

View File

@@ -26,7 +26,7 @@
// increment this value if you've made a change to the c extesion // increment this value if you've made a change to the c extesion
// and want to force users to rebuild // and want to force users to rebuild
#define OVERVIEWER_EXTENSION_VERSION 22 #define OVERVIEWER_EXTENSION_VERSION 23
/* Python PIL, and numpy headers */ /* Python PIL, and numpy headers */
#include <Python.h> #include <Python.h>
@@ -67,14 +67,15 @@ PyObject *draw_triangle(PyObject *dest, int inclusive,
typedef struct _RenderMode RenderMode; typedef struct _RenderMode RenderMode;
/* in iterate.c */ /* in iterate.c */
#define SECTIONS_PER_CHUNK 16
typedef struct { typedef struct {
/* whether this chunk is loaded: use load_chunk to load */ /* whether this chunk is loaded: use load_chunk to load */
int loaded; int loaded;
/* the 3 sections: below, current, above */ /* all the sections in a given chunk */
struct { struct {
/* all there is to know about each section */ /* all there is to know about each section */
PyObject *blocks, *data, *skylight, *blocklight; PyObject *blocks, *data, *skylight, *blocklight;
} sections[3]; } sections[SECTIONS_PER_CHUNK];
} ChunkData; } ChunkData;
typedef struct { typedef struct {
/* the regionset object, and chunk coords */ /* the regionset object, and chunk coords */
@@ -142,10 +143,10 @@ typedef enum
DATA, DATA,
BLOCKLIGHT, BLOCKLIGHT,
SKYLIGHT, SKYLIGHT,
} ChunkType; } DataType;
static inline unsigned int get_data(RenderState *state, ChunkType type, int x, int y, int z) static inline unsigned int get_data(RenderState *state, DataType type, int x, int y, int z)
{ {
int chunkx = 1, chunky = 1, chunkz = 1; int chunkx = 1, chunky = state->chunky, chunkz = 1;
PyObject *data_array = NULL; PyObject *data_array = NULL;
if (x >= 16) { if (x >= 16) {
x -= 16; x -= 16;
@@ -154,13 +155,6 @@ static inline unsigned int get_data(RenderState *state, ChunkType type, int x, i
x += 16; x += 16;
chunkx--; chunkx--;
} }
if (y >= 16) {
y -= 16;
chunky++;
} else if (y < 0) {
y += 16;
chunky--;
}
if (z >= 16) { if (z >= 16) {
z -= 16; z -= 16;
chunkz++; chunkz++;
@@ -168,6 +162,17 @@ static inline unsigned int get_data(RenderState *state, ChunkType type, int x, i
z += 16; z += 16;
chunkz--; chunkz--;
} }
while (y >= 16) {
y -= 16;
chunky++;
}
while (y < 0) {
y += 16;
chunky--;
}
if (chunky < 0 || chunky >= SECTIONS_PER_CHUNK)
return 0;
if (!(state->chunks[chunkx][chunkz].loaded)) if (!(state->chunks[chunkx][chunkz].loaded))
{ {

View File

@@ -19,80 +19,22 @@
#include <math.h> #include <math.h>
typedef struct { typedef struct {
/* data used to know where the surface is */
PyObject *skylight;
PyObject *left_skylight;
PyObject *right_skylight;
PyObject *up_left_skylight;
PyObject *up_right_skylight;
/* data used to know where the lit caves are */
PyObject *blocklight;
PyObject *left_blocklight;
PyObject *right_blocklight;
PyObject *up_left_blocklight;
PyObject *up_right_blocklight;
int only_lit; int only_lit;
} RenderPrimitiveCave; } RenderPrimitiveCave;
static inline int static inline int
touches_light(unsigned int x, unsigned int y, unsigned int z, touches_light(RenderState *state, DataType type, unsigned int x, unsigned int y, unsigned int z) {
PyObject *light, PyObject *left_light, PyObject *right_light, if (get_data(state, type, x, y+1, z))
PyObject *up_left_light, PyObject *up_right_light) {
if (getArrayByte3D(light, x, y, z+1) != 0) {
return 1; return 1;
}
if ((x == 15)) { if (get_data(state, type, x+1, y, z))
if (up_right_light) { return 1;
if (getArrayByte3D(up_right_light, 0, y, z) != 0) { if (get_data(state, type, x-1, y, z))
return 1; return 1;
} if (get_data(state, type, x, y, z+1))
} return 1;
} else { if (get_data(state, type, x, y, z-1))
if (getArrayByte3D(light, x+1, y, z) != 0) { return 1;
return 1;
}
}
if (x == 0) {
if (left_light) {
if (getArrayByte3D(left_light, 15, y, z) != 0) {
return 1;
}
}
} else {
if (getArrayByte3D(light, x-1, y, z) != 0) {
return 1;
}
}
if (y == 15) {
if (right_light) {
if (getArrayByte3D(right_light, 0, y, z) != 0) {
return 1;
}
}
} else {
if (getArrayByte3D(light, x, y+1, z) != 0) {
return 1;
}
}
if (y == 0) {
if (up_left_light) {
if (getArrayByte3D(up_left_light, 15, y, z) != 0) {
return 1;
}
}
} else {
if (getArrayByte3D(light, x, y-1, z) != 0) {
return 1;
}
}
return 0; return 0;
} }
@@ -101,72 +43,35 @@ cave_occluded(void *data, RenderState *state, int x, int y, int z) {
/* check for normal occlusion */ /* check for normal occlusion */
/* use ajacent chunks, if not you get blocks spreaded in chunk edges */ /* use ajacent chunks, if not you get blocks spreaded in chunk edges */
if (z != 127) { if (!is_transparent(get_data(state, BLOCKS, x-1, y, z)) &&
if ( (x == 0) && (y != 15) ) { !is_transparent(get_data(state, BLOCKS, x, y, z+1)) &&
if (state->left_blocks != NULL) { !is_transparent(get_data(state, BLOCKS, x, y+1, z))) {
if (!is_transparent(getArrayByte3D(state->left_blocks, 15, y, z)) && return 1;
!is_transparent(getArrayByte3D(state->blocks, x, y, z+1)) &&
!is_transparent(getArrayByte3D(state->blocks, x, y+1, z))) {
return 1;
}
} else {
return 1;
}
}
if ( (x != 0) && (y == 15) ) {
if (state->right_blocks != NULL) {
if (!is_transparent(getArrayByte3D(state->blocks, x-1, y, z)) &&
!is_transparent(getArrayByte3D(state->right_blocks, x, 0, z)) &&
!is_transparent(getArrayByte3D(state->blocks, x, y, z+1))) {
return 1;
}
} else {
return 1;
}
}
if ( (x == 0) && (y == 15) ) {
if ((state->left_blocks != NULL) &&
(state->right_blocks != NULL)) {
if (!is_transparent(getArrayByte3D(state->left_blocks, 15, y, z)) &&
!is_transparent(getArrayByte3D(state->right_blocks, x, 0, z)) &&
!is_transparent(getArrayByte3D(state->blocks, x, y, z+1))) {
return 1;
}
} else {
return 1;
}
}
if ( (x != 0) && (y != 15) &&
!is_transparent(getArrayByte3D(state->blocks, x-1, y, z)) &&
!is_transparent(getArrayByte3D(state->blocks, x, y, z+1)) &&
!is_transparent(getArrayByte3D(state->blocks, x, y+1, z))) {
return 1;
}
} }
/* special handling for section boundaries */
if (x == 0 && (!(state->chunks[0][1].loaded) || state->chunks[0][1].sections[state->chunky].blocks == NULL))
return 1;
if (y == 15 && (state->chunky + 1 >= SECTIONS_PER_CHUNK || state->chunks[1][1].sections[state->chunky + 1].blocks == NULL))
return 1;
if (z == 15 && (!(state->chunks[1][2].loaded) || state->chunks[1][2].sections[state->chunky].blocks == NULL))
return 1;
return 0; return 0;
} }
static int static int
cave_hidden(void *data, RenderState *state, int x, int y, int z) { cave_hidden(void *data, RenderState *state, int x, int y, int z) {
RenderPrimitiveCave* self; RenderPrimitiveCave* self;
int dz = 0; int dy = 0;
self = (RenderPrimitiveCave *)data; self = (RenderPrimitiveCave *)data;
/* check if the block is touching skylight */ /* check if the block is touching skylight */
if (z != 127) { if (touches_light(state, SKYLIGHT, x, y, z)) {
return 1;
if (touches_light(x, y, z, self->skylight, self->left_skylight, self->right_skylight, self->up_left_skylight, self->up_right_skylight)) { }
return 1;
} if (self->only_lit && !touches_light(state, BLOCKLIGHT, x, y, z)) {
if (self->only_lit && !touches_light(x, y, z, self->blocklight, self->left_blocklight, self->right_blocklight, self->up_left_blocklight, self->up_right_blocklight)) {
return 1;
}
} else { /* if z == 127 skip */
return 1; return 1;
} }
@@ -174,18 +79,18 @@ cave_hidden(void *data, RenderState *state, int x, int y, int z) {
* at this point of the code the block has no skylight * at this point of the code the block has no skylight
* but a deep sea can be completely dark * but a deep sea can be completely dark
*/ */
if ((getArrayShort3D(state->blocks, x, y, z) == 9) ||
if ((getArrayByte3D(state->blocks, x, y, z) == 9) || (get_data(state, BLOCKS, x, y+1, z) == 9)) {
(getArrayByte3D(state->blocks, x, y, z+1) == 9)) {
for (dy = y+1; dy < SECTIONS_PER_CHUNK * 16; dy++) {
for (dz = z+1; dz < 127; dz++) { /* go up and check for skylight */ /* go up and check for skylight */
if (getArrayByte3D(self->skylight, x, y, dz) != 0) { if (get_data(state, SKYLIGHT, x, dy, z) != 0) {
return 1; return 1;
} }
if (getArrayByte3D(state->blocks, x, y, dz) != 9) { if (get_data(state, BLOCKS, x, dy, z) != 9) {
/* we are out of the water! and there's no skylight /* we are out of the water! and there's no skylight
* , i.e. is a cave lake or something similar */ * , i.e. is a cave lake or something similar */
return 0; break;
} }
} }
} }
@@ -194,13 +99,6 @@ cave_hidden(void *data, RenderState *state, int x, int y, int z) {
* blocks as hidden for the lighting to look right, since technically our * blocks as hidden for the lighting to look right, since technically our
* hiding depends on occlusion as well * hiding depends on occlusion as well
*/ */
if ( (x != 0) && (y != 15) && (z != 127) &&
!is_transparent(getArrayByte3D(state->blocks, x-1, y, z)) &&
!is_transparent(getArrayByte3D(state->blocks, x, y, z+1)) &&
!is_transparent(getArrayByte3D(state->blocks, x, y+1, z))) {
return 1;
}
return cave_occluded(data, state, x, y, z); return cave_occluded(data, state, x, y, z);
} }
@@ -213,48 +111,13 @@ cave_start(void *data, RenderState *state, PyObject *support) {
if (!render_mode_parse_option(support, "only_lit", "i", &(self->only_lit))) if (!render_mode_parse_option(support, "only_lit", "i", &(self->only_lit)))
return 1; return 1;
/* if there's skylight we are in the surface! */
self->skylight = get_chunk_data(state, CURRENT, SKYLIGHT, 1);
self->left_skylight = get_chunk_data(state, DOWN_LEFT, SKYLIGHT, 1);
self->right_skylight = get_chunk_data(state, DOWN_RIGHT, SKYLIGHT, 1);
self->up_left_skylight = get_chunk_data(state, UP_LEFT, SKYLIGHT, 1);
self->up_right_skylight = get_chunk_data(state, UP_RIGHT, SKYLIGHT, 1);
if (self->only_lit) {
self->blocklight = get_chunk_data(state, CURRENT, BLOCKLIGHT, 1);
self->left_blocklight = get_chunk_data(state, DOWN_LEFT, BLOCKLIGHT, 1);
self->right_blocklight = get_chunk_data(state, DOWN_RIGHT, BLOCKLIGHT, 1);
self->up_left_blocklight = get_chunk_data(state, UP_LEFT, BLOCKLIGHT, 1);
self->up_right_blocklight = get_chunk_data(state, UP_RIGHT, BLOCKLIGHT, 1);
}
return 0; return 0;
} }
static void
cave_finish(void *data, RenderState *state) {
RenderPrimitiveCave* self;
self = (RenderPrimitiveCave *)data;
Py_DECREF(self->skylight);
Py_XDECREF(self->left_skylight);
Py_XDECREF(self->right_skylight);
Py_XDECREF(self->up_left_skylight);
Py_XDECREF(self->up_right_skylight);
if (self->only_lit) {
Py_DECREF(self->blocklight);
Py_XDECREF(self->left_blocklight);
Py_XDECREF(self->right_blocklight);
Py_XDECREF(self->up_left_blocklight);
Py_XDECREF(self->up_right_blocklight);
}
}
RenderPrimitiveInterface primitive_cave = { RenderPrimitiveInterface primitive_cave = {
"cave", sizeof(RenderPrimitiveCave), "cave", sizeof(RenderPrimitiveCave),
cave_start, cave_start,
cave_finish, NULL,
cave_occluded, cave_occluded,
cave_hidden, cave_hidden,
NULL, NULL,