0

modified C extension for get_chunk() raising exception on not found

This commit is contained in:
Andrew Brown
2012-01-16 01:59:16 -05:00
parent f9c24df769
commit 70ef0af00c
4 changed files with 46 additions and 32 deletions

View File

@@ -96,6 +96,10 @@ PyObject *init_chunk_render(void) {
Py_RETURN_NONE; Py_RETURN_NONE;
} }
/*
* Returns the requested chunk data from the requested chunk.
* Returns NULL with an exception set if the requested chunk doesn't exist.
*/
PyObject *get_chunk_data(RenderState *state, ChunkNeighborName neighbor, ChunkDataType type) { PyObject *get_chunk_data(RenderState *state, ChunkNeighborName neighbor, ChunkDataType type) {
int x = state->chunkx; int x = state->chunkx;
int z = state->chunkz; int z = state->chunkz;
@@ -120,8 +124,11 @@ PyObject *get_chunk_data(RenderState *state, ChunkNeighborName neighbor, ChunkDa
} }
chunk = PyObject_CallMethod(state->regionset, "get_chunk", "ii", x, z); chunk = PyObject_CallMethod(state->regionset, "get_chunk", "ii", x, z);
if (chunk == NULL || chunk == Py_None) if (chunk == NULL) {
return chunk; // An exception is already set. RegionSet.get_chunk sets
// ChunkDoesntExist
return NULL;
}
switch (type) { switch (type) {
case BLOCKS: case BLOCKS:
@@ -166,7 +173,7 @@ unsigned char
unsigned char pdata=0; unsigned char pdata=0;
if (state->x == 15) { /* +x direction */ if (state->x == 15) { /* +x direction */
if (state->up_right_blocks != Py_None) { /* just in case we are in the end of the world */ if (state->up_right_blocks != NULL) { /* just in case we are in the end of the world */
if (getArrayByte3D(state->up_right_blocks, 0, y, z) == blockid) { if (getArrayByte3D(state->up_right_blocks, 0, y, z) == blockid) {
pdata = pdata|(1 << 3); pdata = pdata|(1 << 3);
} }
@@ -178,7 +185,7 @@ unsigned char
} }
if (state->y == 15) { /* +y direction*/ if (state->y == 15) { /* +y direction*/
if (state->right_blocks != Py_None) { if (state->right_blocks != NULL) {
if (getArrayByte3D(state->right_blocks, x, 0, z) == blockid) { if (getArrayByte3D(state->right_blocks, x, 0, z) == blockid) {
pdata = pdata|(1 << 2); pdata = pdata|(1 << 2);
} }
@@ -190,7 +197,7 @@ unsigned char
} }
if (state->x == 0) { /* -x direction*/ if (state->x == 0) { /* -x direction*/
if (state->left_blocks != Py_None) { if (state->left_blocks != NULL) {
if (getArrayByte3D(state->left_blocks, 15, y, z) == blockid) { if (getArrayByte3D(state->left_blocks, 15, y, z) == blockid) {
pdata = pdata|(1 << 1); pdata = pdata|(1 << 1);
} }
@@ -202,7 +209,7 @@ unsigned char
} }
if (state->y == 0) { /* -y direction */ if (state->y == 0) { /* -y direction */
if (state->up_left_blocks != Py_None) { if (state->up_left_blocks != NULL) {
if (getArrayByte3D(state->up_left_blocks, x, 15, z) == blockid) { if (getArrayByte3D(state->up_left_blocks, x, 15, z) == blockid) {
pdata = pdata|(1 << 0); pdata = pdata|(1 << 0);
} }
@@ -434,8 +441,7 @@ chunk_render(PyObject *self, PyObject *args) {
/* get the block data directly from numpy: */ /* get the block data directly from numpy: */
blocks_py = get_chunk_data(&state, CURRENT, BLOCKS); blocks_py = get_chunk_data(&state, CURRENT, BLOCKS);
state.blocks = blocks_py; state.blocks = blocks_py;
if (blocks_py == Py_None) { if (blocks_py == NULL) {
PyErr_SetString(PyExc_RuntimeError, "chunk does not exist!");
return NULL; return NULL;
} }
@@ -453,6 +459,9 @@ chunk_render(PyObject *self, PyObject *args) {
up_right_blocks_py = get_chunk_data(&state, UP_RIGHT, BLOCKS); up_right_blocks_py = get_chunk_data(&state, UP_RIGHT, BLOCKS);
state.up_right_blocks = up_right_blocks_py; state.up_right_blocks = up_right_blocks_py;
// Clear any error that was set by the above calls
PyErr_Clear();
/* 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 */
srand(1); srand(1);

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 13 #define OVERVIEWER_EXTENSION_VERSION 14
/* Python PIL, and numpy headers */ /* Python PIL, and numpy headers */
#include <Python.h> #include <Python.h>

View File

@@ -46,7 +46,7 @@ touches_light(unsigned int x, unsigned int y, unsigned int z,
} }
if ((x == 15)) { if ((x == 15)) {
if (up_right_light != Py_None) { if (up_right_light) {
if (getArrayByte3D(up_right_light, 0, y, z) != 0) { if (getArrayByte3D(up_right_light, 0, y, z) != 0) {
return 1; return 1;
} }
@@ -58,7 +58,7 @@ touches_light(unsigned int x, unsigned int y, unsigned int z,
} }
if (x == 0) { if (x == 0) {
if (left_light != Py_None) { if (left_light) {
if (getArrayByte3D(left_light, 15, y, z) != 0) { if (getArrayByte3D(left_light, 15, y, z) != 0) {
return 1; return 1;
} }
@@ -70,7 +70,7 @@ touches_light(unsigned int x, unsigned int y, unsigned int z,
} }
if (y == 15) { if (y == 15) {
if (right_light != Py_None) { if (right_light) {
if (getArrayByte3D(right_light, 0, y, z) != 0) { if (getArrayByte3D(right_light, 0, y, z) != 0) {
return 1; return 1;
} }
@@ -82,7 +82,7 @@ touches_light(unsigned int x, unsigned int y, unsigned int z,
} }
if (y == 0) { if (y == 0) {
if (up_left_light != Py_None) { if (up_left_light) {
if (getArrayByte3D(up_left_light, 15, y, z) != 0) { if (getArrayByte3D(up_left_light, 15, y, z) != 0) {
return 1; return 1;
} }
@@ -227,6 +227,8 @@ cave_start(void *data, RenderState *state, PyObject *support) {
self->up_left_blocklight = get_chunk_data(state, UP_LEFT, BLOCKLIGHT); self->up_left_blocklight = get_chunk_data(state, UP_LEFT, BLOCKLIGHT);
self->up_right_blocklight = get_chunk_data(state, UP_RIGHT, BLOCKLIGHT); self->up_right_blocklight = get_chunk_data(state, UP_RIGHT, BLOCKLIGHT);
} }
// Non-existant neighboring blocks is not an error
PyErr_Clear();
return 0; return 0;
} }
@@ -237,17 +239,17 @@ cave_finish(void *data, RenderState *state) {
self = (RenderPrimitiveCave *)data; self = (RenderPrimitiveCave *)data;
Py_DECREF(self->skylight); Py_DECREF(self->skylight);
Py_DECREF(self->left_skylight); Py_XDECREF(self->left_skylight);
Py_DECREF(self->right_skylight); Py_XDECREF(self->right_skylight);
Py_DECREF(self->up_left_skylight); Py_XDECREF(self->up_left_skylight);
Py_DECREF(self->up_right_skylight); Py_XDECREF(self->up_right_skylight);
if (self->only_lit) { if (self->only_lit) {
Py_DECREF(self->blocklight); Py_DECREF(self->blocklight);
Py_DECREF(self->left_blocklight); Py_XDECREF(self->left_blocklight);
Py_DECREF(self->right_blocklight); Py_XDECREF(self->right_blocklight);
Py_DECREF(self->up_left_blocklight); Py_XDECREF(self->up_left_blocklight);
Py_DECREF(self->up_right_blocklight); Py_XDECREF(self->up_right_blocklight);
} }
} }

View File

@@ -234,9 +234,9 @@ get_lighting_color(RenderPrimitiveLighting *self, RenderState *state,
} }
/* also, make sure we have enough info to correctly calculate lighting */ /* also, make sure we have enough info to correctly calculate lighting */
if (blocks == Py_None || blocks == NULL || if (!blocks ||
skylight == Py_None || skylight == NULL || !skylight ||
blocklight == Py_None || blocklight == NULL) { !blocklight) {
self->calculate_light_color(self, 15, 0, r, g, b); self->calculate_light_color(self, 15, 0, r, g, b);
return; return;
@@ -370,6 +370,9 @@ lighting_start(void *data, RenderState *state, PyObject *support) {
self->up_right_skylight = get_chunk_data(state, UP_RIGHT, SKYLIGHT); self->up_right_skylight = get_chunk_data(state, UP_RIGHT, SKYLIGHT);
self->up_right_blocklight = get_chunk_data(state, UP_RIGHT, BLOCKLIGHT); self->up_right_blocklight = get_chunk_data(state, UP_RIGHT, BLOCKLIGHT);
// Non-existant neighbor block is not an error
PyErr_Clear();
if (self->night) { if (self->night) {
self->calculate_light_color = calculate_light_color_night; self->calculate_light_color = calculate_light_color_night;
} else { } else {
@@ -404,14 +407,14 @@ lighting_finish(void *data, RenderState *state) {
Py_DECREF(self->skylight); Py_DECREF(self->skylight);
Py_DECREF(self->blocklight); Py_DECREF(self->blocklight);
Py_DECREF(self->left_skylight); Py_XDECREF(self->left_skylight);
Py_DECREF(self->left_blocklight); Py_XDECREF(self->left_blocklight);
Py_DECREF(self->right_skylight); Py_XDECREF(self->right_skylight);
Py_DECREF(self->right_blocklight); Py_XDECREF(self->right_blocklight);
Py_DECREF(self->up_left_skylight); Py_XDECREF(self->up_left_skylight);
Py_DECREF(self->up_left_blocklight); Py_XDECREF(self->up_left_blocklight);
Py_DECREF(self->up_right_skylight); Py_XDECREF(self->up_right_skylight);
Py_DECREF(self->up_right_blocklight); Py_XDECREF(self->up_right_blocklight);
} }
static void static void